Skip to content

Commit

Permalink
fix(ext/fetch): don't throw when bodyUsed inspect after upgrade (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Nov 26, 2024
1 parent 3a55b67 commit 93bbbe4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ext/fetch/22_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,13 @@ function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
*/
get() {
webidl.assertBranded(this, prototype);
if (this[bodySymbol] !== null) {
return this[bodySymbol].consumed();
try {
if (this[bodySymbol] !== null) {
return this[bodySymbol].consumed();
}
} catch (_) {
// Request is closed.
return true;
}
return false;
},
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4327,3 +4327,55 @@ Deno.test({

await server.shutdown();
});

// https://github.com/denoland/deno/issues/27083
Deno.test(
{ permissions: { net: true } },
async function httpServerWebSocketInspectRequest() {
const ac = new AbortController();
const listeningDeferred = Promise.withResolvers<void>();
const doneDeferred = Promise.withResolvers<void>();
const server = Deno.serve({
handler: (request) => {
const {
response,
socket,
} = Deno.upgradeWebSocket(request);

socket.onopen = () => {
Deno.inspect(request); // should not throw
};
socket.onerror = (e) => {
console.error(e);
fail();
};
socket.onmessage = (m) => {
socket.send(m.data);
socket.close(1001);
};
socket.onclose = () => doneDeferred.resolve();
return response;
},
port: servePort,
signal: ac.signal,
onListen: onListen(listeningDeferred.resolve),
onError: createOnErrorCb(ac),
});

await listeningDeferred.promise;
const def = Promise.withResolvers<void>();
const ws = new WebSocket(`ws://localhost:${servePort}`);
ws.onmessage = (m) => assertEquals(m.data, "foo");
ws.onerror = (e) => {
console.error(e);
fail();
};
ws.onclose = () => def.resolve();
ws.onopen = () => ws.send("foo");

await def.promise;
await doneDeferred.promise;
ac.abort();
await server.finished;
},
);

0 comments on commit 93bbbe4

Please sign in to comment.