Skip to content

Commit

Permalink
better stack trace for body.json
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Aug 16, 2023
1 parent c83b084 commit 32d9968
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
6 changes: 1 addition & 5 deletions lib/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ class Cache {
const reader = stream.getReader()

// 11.3
readAllBytes(
reader,
(bytes) => bodyReadPromise.resolve(bytes),
(error) => bodyReadPromise.reject(error)
)
readAllBytes(reader).then(bodyReadPromise.resolve, bodyReadPromise.reject)
} else {
bodyReadPromise.resolve(undefined)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ async function specConsumeBody (object, convertBytesToJSValue, instance) {

// 6. Otherwise, fully read object’s body given successSteps,
// errorSteps, and object’s relevant global object.
fullyReadBody(object[kState].body, successSteps, errorSteps)
await fullyReadBody(object[kState].body, successSteps, errorSteps)

// 7. Return promise.
return promise.promise
Expand Down
36 changes: 15 additions & 21 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,17 +812,17 @@ function iteratorResult (pair, kind) {
/**
* @see https://fetch.spec.whatwg.org/#body-fully-read
*/
function fullyReadBody (body, processBody, processBodyError) {
async function fullyReadBody (body, processBody, processBodyError) {
// 1. If taskDestination is null, then set taskDestination to
// the result of starting a new parallel queue.

// 2. Let successSteps given a byte sequence bytes be to queue a
// fetch task to run processBody given bytes, with taskDestination.
const successSteps = (bytes) => queueMicrotask(() => processBody(bytes))
const successSteps = (bytes) => processBody(bytes)

// 3. Let errorSteps be to queue a fetch task to run processBodyError,
// with taskDestination.
const errorSteps = (error) => queueMicrotask(() => processBodyError(error))
const errorSteps = (error) => processBodyError(error)

// 4. Let reader be the result of getting a reader for body’s stream.
// If that threw an exception, then run errorSteps with that
Expand All @@ -837,7 +837,14 @@ function fullyReadBody (body, processBody, processBodyError) {
}

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

/** @type {ReadableStream} */
Expand Down Expand Up @@ -906,36 +913,23 @@ 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, successSteps, failureSteps) {
async function readAllBytes (reader) {
const bytes = []
let byteLength = 0

while (true) {
let done
let chunk

try {
({ done, value: chunk } = await reader.read())
} catch (e) {
// 1. Call failureSteps with e.
failureSteps(e)
return
}
const { done, value: chunk } = await reader.read()

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

// 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
throw new TypeError('Received non-Uint8Array chunk')
}

// 2. Append the bytes represented by chunk to bytes.
Expand Down

0 comments on commit 32d9968

Please sign in to comment.