Skip to content

Commit

Permalink
fix: fetch forward context to fetchFinale
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jan 12, 2022
1 parent 01302e6 commit ced66e9
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions lib/fetch/index.js
Expand Up @@ -687,7 +687,7 @@ async function mainFetch (fetchParams, recursive = false) {
// 1. Let processBodyError be this step: run fetch finale given fetchParams
// and a network error.
const processBodyError = (reason) =>
fetchFinale(fetchParams, makeNetworkError(reason))
fetchFinale.call(context, fetchParams, makeNetworkError(reason))

// 2. If request’s response tainting is "opaque", or response’s body is null,
// then run processBodyError and abort these steps.
Expand All @@ -710,7 +710,7 @@ async function mainFetch (fetchParams, recursive = false) {
response.body = safelyExtractBody(bytes)[0]

// 3. Run fetch finale given fetchParams and response.
fetchFinale(fetchParams, response)
fetchFinale.call(context, fetchParams, response)
}

// 4. Fully read response’s body given processBody and processBodyError.
Expand All @@ -721,7 +721,7 @@ async function mainFetch (fetchParams, recursive = false) {
}
} else {
// 21. Otherwise, run fetch finale given fetchParams and response.
fetchFinale(fetchParams, response)
fetchFinale.call(context, fetchParams, response)
}
}

Expand All @@ -742,30 +742,44 @@ function finalizeResponse (fetchParams, response) {
function fetchFinale (fetchParams, response) {
const context = this

// 1. If fetchParams’s process response is non-null,
// then queue a fetch task to run fetchParams’s process response
// given response, with fetchParams’s task destination.
// 1. If response is a network error, then:
if (response.type === 'error') {
// 1. Set response’s URL list to « fetchParams’s request’s URL list[0] ».
response.urlList = [fetchParams.request.urlList[0]]

// 2. Set response’s timing info to the result of creating an opaque timing
// info for fetchParams’s timing info.
response.timingInfo = createOpaqueTimingInfo({
startTime: fetchParams.timingInfo.startTime
})
}

// 2. Let processResponseEndOfBody be the following steps:
// TODO

// 3. If fetchParams’s process response is non-null, then queue a fetch task
// to run fetchParams’s process response given response, with fetchParams’s
// task destination.
if (fetchParams.processResponse != null) {
fetchParams.processResponse(response)
}

// 2. If fetchParams’s process response consume is non-null, then:.
// TODO
// 1. Let processBody given nullOrBytes be this step: run fetchParams’s
// process response consume given response and nullOrBytes.on.
// TODO
// 2. Let processBodyError be this step: run fetchParams’s process
// response consume given response and failure.on.
// TODO
// 3. If response’s body is null, then queue a fetch task to run
// processBody given null, with fetchParams’s task destination.on.
// TODO
// 4. Otherwise, fully read response’s body given processBody,
// processBodyError, and fetchParams’s task destination.on.
// TODO

// TODO (spec): The spec doesn't specify this but we need to
// terminate fetch if we have an error response.
// 4. If fetchParams’s process response is non-null, then queue a fetch task
// to run fetchParams’s process response given response, with fetchParams’s
// task destination.
// TODO

// 5. If response’s body is null, then run processResponseEndOfBody.
// TODO

// 6. Otherwise:
// TODO

// 7. If fetchParams’s process response consume body is non-null, then:
// TODO

// TODO: This is a workaround. Until the above has been implemented, i.e.
// we need to either fully consume the body or terminate the fetch.
if (response.type === 'error') {
context.terminate({ reason: response.error })
}
Expand Down

0 comments on commit ced66e9

Please sign in to comment.