Skip to content

Commit

Permalink
fetch: avoid async iteration when request.body is null (#1303)
Browse files Browse the repository at this point in the history
* fetch: avoid iteration when request.body is null

* slightly simplify

Co-authored-by: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
RafaelGSS and ronag committed Mar 28, 2022
1 parent 5b9acf2 commit a211b18
Showing 1 changed file with 49 additions and 48 deletions.
97 changes: 49 additions & 48 deletions lib/fetch/index.js
Expand Up @@ -1664,61 +1664,62 @@ async function httpNetworkFetch (
// 2. Otherwise, return a network error.

// To transmit request’s body body, run these steps:
const requestBody = (async function * () {
// 1. If body is null and fetchParams’s process request end-of-body is
// non-null, then queue a fetch task given fetchParams’s process request
// end-of-body and fetchParams’s task destination.
if (request.body == null && fetchParams.processRequestEndOfBody) {
queueMicrotask(() => fetchParams.processRequestEndOfBody())
} else if (request.body != null) {
// 2. Otherwise, if body is non-null:

// 1. Let processBodyChunk given bytes be these steps:
const processBodyChunk = async function * (bytes) {
// 1. If the ongoing fetch is terminated, then abort these steps.
if (isCancelled(fetchParams)) {
return
}
let requestBody = null
// 1. If body is null and fetchParams’s process request end-of-body is
// non-null, then queue a fetch task given fetchParams’s process request
// end-of-body and fetchParams’s task destination.
if (request.body == null && fetchParams.processRequestEndOfBody) {
queueMicrotask(() => fetchParams.processRequestEndOfBody())
} else if (request.body != null) {
// 2. Otherwise, if body is non-null:

// 1. Let processBodyChunk given bytes be these steps:
const processBodyChunk = async function * (bytes) {
// 1. If the ongoing fetch is terminated, then abort these steps.
if (isCancelled(fetchParams)) {
return
}

// 2. Run this step in parallel: transmit bytes.
yield bytes
// 2. Run this step in parallel: transmit bytes.
yield bytes

// 3. If fetchParams’s process request body is non-null, then run
// fetchParams’s process request body given bytes’s length.
fetchParams.processRequestBodyChunkLength?.(bytes.byteLength)
}
// 3. If fetchParams’s process request body is non-null, then run
// fetchParams’s process request body given bytes’s length.
fetchParams.processRequestBodyChunkLength?.(bytes.byteLength)
}

// 2. Let processEndOfBody be these steps:
const processEndOfBody = () => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}
// 2. Let processEndOfBody be these steps:
const processEndOfBody = () => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}

// 2. If fetchParams’s process request end-of-body is non-null,
// then run fetchParams’s process request end-of-body.
if (fetchParams.processRequestEndOfBody) {
fetchParams.processRequestEndOfBody()
}
// 2. If fetchParams’s process request end-of-body is non-null,
// then run fetchParams’s process request end-of-body.
if (fetchParams.processRequestEndOfBody) {
fetchParams.processRequestEndOfBody()
}
}

// 3. Let processBodyError given e be these steps:
const processBodyError = (e) => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}
// 3. Let processBodyError given e be these steps:
const processBodyError = (e) => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}

// 2. If e is an "AbortError" DOMException, then abort fetchParams’s controller.
if (e.name === 'AbortError') {
fetchParams.controller.abort()
} else {
fetchParams.controller.terminate(e)
}
// 2. If e is an "AbortError" DOMException, then abort fetchParams’s controller.
if (e.name === 'AbortError') {
fetchParams.controller.abort()
} else {
fetchParams.controller.terminate(e)
}
}

// 4. Incrementally read request’s body given processBodyChunk, processEndOfBody,
// processBodyError, and fetchParams’s task destination.
// 4. Incrementally read request’s body given processBodyChunk, processEndOfBody,
// processBodyError, and fetchParams’s task destination.
requestBody = (async function * () {
try {
for await (const bytes of request.body.stream) {
yield * processBodyChunk(bytes)
Expand All @@ -1727,8 +1728,8 @@ async function httpNetworkFetch (
} catch (err) {
processBodyError(err)
}
}
})()
})()
}

try {
const { body, status, statusText, headersList } = await dispatch({ body: requestBody })
Expand Down

0 comments on commit a211b18

Please sign in to comment.