From 214785ade832c6ae911e888e9af1dc58396423ba Mon Sep 17 00:00:00 2001 From: Khafra Date: Fri, 1 Dec 2023 13:34:47 -0500 Subject: [PATCH] fix most remaining failures --- lib/fetch/body.js | 15 +++++++++++---- lib/fetch/index.js | 5 ++++- lib/fetch/util.js | 3 ++- test/wpt/server/server.mjs | 1 + test/wpt/status/fetch.status.json | 4 ++++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/fetch/body.js b/lib/fetch/body.js index 19e5a8a2658..58da2941a85 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -50,9 +50,12 @@ function extractBody (object, keepalive = false) { // up stream with byte reading support. stream = new ReadableStream({ async pull (controller) { - controller.enqueue( - typeof source === 'string' ? textEncoder.encode(source) : source - ) + const buffer = typeof source === 'string' ? textEncoder.encode(source) : source + + if (buffer.byteLength) { + controller.enqueue(buffer) + } + queueMicrotask(() => readableStreamClose(controller)) }, start () {}, @@ -223,13 +226,17 @@ function extractBody (object, keepalive = false) { // When running action is done, close stream. queueMicrotask(() => { controller.close() + controller.byobRequest?.respond(0) }) } else { // Whenever one or more bytes are available and stream is not errored, // enqueue a Uint8Array wrapping an ArrayBuffer containing the available // bytes into stream. if (!isErrored(stream)) { - controller.enqueue(new Uint8Array(value)) + const buffer = new Uint8Array(value) + if (buffer.byteLength) { + controller.enqueue(buffer) + } } } return controller.desiredSize > 0 diff --git a/lib/fetch/index.js b/lib/fetch/index.js index cf76fc91e10..da193c93ee0 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -2009,7 +2009,10 @@ async function httpNetworkFetch ( // 7. Enqueue a Uint8Array wrapping an ArrayBuffer containing bytes // into stream. - fetchParams.controller.controller.enqueue(new Uint8Array(bytes)) + const buffer = new Uint8Array(bytes) + if (buffer.byteLength) { + fetchParams.controller.controller.enqueue(buffer) + } // 8. If stream is errored, then terminate the ongoing fetch. if (isErrored(stream)) { diff --git a/lib/fetch/util.js b/lib/fetch/util.js index 0c7260669da..e0426be3bcd 100644 --- a/lib/fetch/util.js +++ b/lib/fetch/util.js @@ -928,9 +928,10 @@ function isomorphicDecode (input) { function readableStreamClose (controller) { try { controller.close() + controller.byobRequest?.respond(0) } catch (err) { // TODO: add comment explaining why this error occurs. - if (!err.message.includes('Controller is already closed')) { + if (!err.message.includes('Controller is already closed') && !err.message.includes('ReadableStream is already closed')) { throw err } } diff --git a/test/wpt/server/server.mjs b/test/wpt/server/server.mjs index 82b9080f407..d12b4936978 100644 --- a/test/wpt/server/server.mjs +++ b/test/wpt/server/server.mjs @@ -36,6 +36,7 @@ const server = createServer(async (req, res) => { res.setHeader('content-type', 'text/html') // fall through } + case '/fetch/content-encoding/resources/big.text.gz': case '/service-workers/cache-storage/resources/simple.txt': case '/fetch/content-encoding/resources/foo.octetstream.gz': case '/fetch/content-encoding/resources/foo.text.gz': diff --git a/test/wpt/status/fetch.status.json b/test/wpt/status/fetch.status.json index 1796ee9cd89..34f91ed8375 100644 --- a/test/wpt/status/fetch.status.json +++ b/test/wpt/status/fetch.status.json @@ -232,6 +232,10 @@ "note": "document is not defined", "skip": true }, + "redirect-keepalive.https.any.js": { + "note": "document is not defined", + "skip": true + }, "redirect-location-escape.tentative.any.js": { "note": "TODO(@KhafraDev): crashes runner", "skip": true