Skip to content
Open
54 changes: 26 additions & 28 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,39 +1021,37 @@ function isomorphicEncode (input) {
* @param {ReadableStream<Uint8Array<ArrayBuffer>>} reader
* @param {(bytes: Uint8Array) => void} successSteps
* @param {(error: Error) => void} failureSteps
* @returns {Promise<void>}
* @returns {void}
*/
async function readAllBytes (reader, successSteps, failureSteps) {
try {
const bytes = []
let byteLength = 0

do {
const { done, value: chunk } = await reader.read()

if (done) {
// 1. Call successSteps with bytes.
successSteps(Buffer.concat(bytes, byteLength))
return
}
function readAllBytes (reader, successSteps, failureSteps) {
const bytes = []
let byteLength = 0

const readResultHandler = ({ done, value: chunk }) => {
// close steps
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)) {
failureSteps(new TypeError('Received non-Uint8Array chunk'))
return
}
// chunk steps, given chunk
// 1. If chunk is not a Uint8Array object, call failureSteps
// with a TypeError and abort these steps.
if (!isUint8Array(chunk)) {
failureSteps(new 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)
// 3. Read-loop given reader, bytes, successSteps, and failureSteps.
reader.read().then(readResultHandler, failureSteps)
}

reader.read().then(readResultHandler, failureSteps)
}

/**
Expand Down
Loading