From 93bbbe418446ded1a0f1f98c18053f60c33a7941 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 26 Nov 2024 08:08:46 -0800 Subject: [PATCH] fix(ext/fetch): don't throw when `bodyUsed` inspect after upgrade (#27088) Fixes https://github.com/denoland/deno/issues/27083 --- ext/fetch/22_body.js | 9 +++++-- tests/unit/serve_test.ts | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js index c7e977c0b4..a34758d19a 100644 --- a/ext/fetch/22_body.js +++ b/ext/fetch/22_body.js @@ -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; }, diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts index 7d8c6ca06d..f5896bc64b 100644 --- a/tests/unit/serve_test.ts +++ b/tests/unit/serve_test.ts @@ -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(); + const doneDeferred = Promise.withResolvers(); + 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(); + 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; + }, +);