From 7b1fd3d146292df01f61e9eb0202779be8d7da33 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 21 May 2021 10:11:53 +0900 Subject: [PATCH] fix(runtime/http): fix empty blob response (#10689) --- cli/tests/unit/http_test.ts | 22 ++++++++++++++++++++++ runtime/js/40_http.js | 11 +++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index d4c35545f5..9560394f04 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -317,3 +317,25 @@ unitTest( await promise; }, ); + +unitTest( + { perms: { net: true } }, + async function httpServerEmptyBlobResponse() { + const promise = (async () => { + const listener = Deno.listen({ port: 4501 }); + const conn = await listener.accept(); + const httpConn = Deno.serveHttp(conn); + const event = await httpConn.nextRequest(); + assert(event); + const { respondWith } = event; + await respondWith(new Response(new Blob([]))); + httpConn.close(); + listener.close(); + })(); + + const resp = await fetch("http://127.0.0.1:4501/"); + const respBody = await resp.text(); + assertEquals("", respBody); + await promise; + }, +); diff --git a/runtime/js/40_http.js b/runtime/js/40_http.js index eb4d214ca4..d4b658314a 100644 --- a/runtime/js/40_http.js +++ b/runtime/js/40_http.js @@ -132,10 +132,13 @@ } else { const reader = innerResp.body.stream.getReader(); const r1 = await reader.read(); - if (r1.done) throw new TypeError("Unreachable"); - respBody = r1.value; - const r2 = await reader.read(); - if (!r2.done) throw new TypeError("Unreachable"); + if (r1.done) { + respBody = new Uint8Array(0); + } else { + respBody = r1.value; + const r2 = await reader.read(); + if (!r2.done) throw new TypeError("Unreachable"); + } } } else { innerResp.body.streamOrStatic.consumed = true;