Skip to content

Commit

Permalink
fetch: make fullyReadBody sync (nodejs#3603)
Browse files Browse the repository at this point in the history
* fetch: make fullyReadBody sync

* fix cache
  • Loading branch information
Uzlopak committed Sep 15, 2024
1 parent 881fd5d commit 56f9156
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
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()

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

0 comments on commit 56f9156

Please sign in to comment.