Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: make fullyReadBody sync #3603

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/web/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Cache {
const reader = stream.getReader()

// 11.3
readAllBytes(reader).then(bodyReadPromise.resolve, bodyReadPromise.reject)
readAllBytes(reader, bodyReadPromise.resolve, bodyReadPromise.reject)
} else {
bodyReadPromise.resolve(undefined)
}
Expand Down
47 changes: 26 additions & 21 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ function iteratorMixin (name, object, kInternalIterator, keyIndex = 0, valueInde
/**
* @see https://fetch.spec.whatwg.org/#body-fully-read
*/
async function fullyReadBody (body, processBody, processBodyError) {
function fullyReadBody (body, processBody, processBodyError) {
// 1. If taskDestination is null, then set taskDestination to
// the result of starting a new parallel queue.

Expand All @@ -1054,11 +1054,7 @@ async function fullyReadBody (body, processBody, processBodyError) {
}

// 5. Read all bytes from reader, given successSteps and errorSteps.
try {
successSteps(await readAllBytes(reader))
} catch (e) {
errorSteps(e)
}
readAllBytes(reader, successSteps, errorSteps)
}

/**
Expand Down Expand Up @@ -1096,30 +1092,39 @@ function isomorphicEncode (input) {
* @see https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes
* @see https://streams.spec.whatwg.org/#read-loop
* @param {ReadableStreamDefaultReader} reader
* @param {(bytes: Uint8Array) => void} successSteps
* @param {(error: Error) => void} failureSteps
*/
async function readAllBytes (reader) {
async function readAllBytes (reader, successSteps, failureSteps) {
const bytes = []
let byteLength = 0

while (true) {
const { done, value: chunk } = await reader.read()
try {
do {
const { done, value: chunk } = await reader.read()
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

if (done) {
// 1. Call successSteps with bytes.
return Buffer.concat(bytes, byteLength)
}
if (done) {
// 1. Call successSteps with bytes.
successSteps(Buffer.concat(bytes, byteLength))
return
}

// 1. If chunk is not a Uint8Array object, call failureSteps
// with a TypeError and abort these steps.
if (!isUint8Array(chunk)) {
throw new TypeError('Received non-Uint8Array chunk')
}
// 1. If chunk is not a Uint8Array object, call failureSteps
// with a TypeError and abort these steps.
if (!isUint8Array(chunk)) {
failureSteps(TypeError('Received non-Uint8Array chunk'))
return
}

// 2. Append the bytes represented by chunk to bytes.
bytes.push(chunk)
byteLength += chunk.length
// 2. Append the bytes represented by chunk to bytes.
bytes.push(chunk)
byteLength += chunk.length

// 3. Read-loop given reader, bytes, successSteps, and failureSteps.
} while (true)
} catch (e) {
// 1. Call failureSteps with e.
failureSteps(e)
}
}

Expand Down
Loading