Skip to content

Commit

Permalink
fix most remaining failures
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Dec 1, 2023
1 parent 6a25b4a commit 214785a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
15 changes: 11 additions & 4 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {},
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions test/wpt/server/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
4 changes: 4 additions & 0 deletions test/wpt/status/fetch.status.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 214785a

Please sign in to comment.