diff --git a/lib/web/cache/cache.js b/lib/web/cache/cache.js index 45c6fec3467..5bfd94b3281 100644 --- a/lib/web/cache/cache.js +++ b/lib/web/cache/cache.js @@ -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) } diff --git a/lib/web/fetch/util.js b/lib/web/fetch/util.js index 28d54fe946c..f53991c9440 100644 --- a/lib/web/fetch/util.js +++ b/lib/web/fetch/util.js @@ -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. @@ -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) } /** @@ -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) } }