Skip to content

Commit

Permalink
fetch: avoid iteration when request.body is null
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Mar 28, 2022
1 parent 65feb05 commit 211bab4
Showing 1 changed file with 61 additions and 53 deletions.
114 changes: 61 additions & 53 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1665,69 +1665,62 @@ async function httpNetworkFetch (

// 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
}

// 2. Run this step in parallel: transmit bytes.
yield bytes
// 2. Otherwise, if body is non-null:

// 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)
// 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. Let processEndOfBody be these steps:
const processEndOfBody = () => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}
// 2. Run this step in parallel: transmit bytes.
yield bytes

// 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. 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
}

// 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 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 e is an "AbortError" DOMException, then abort fetchParams’s controller.
if (e.name === 'AbortError') {
fetchParams.controller.abort()
} else {
fetchParams.controller.terminate(e)
}
// 3. Let processBodyError given e be these steps:
const processBodyError = (e) => {
// 1. If fetchParams is canceled, then abort these steps.
if (isCancelled(fetchParams)) {
return
}

// 4. Incrementally read request’s body given processBodyChunk, processEndOfBody,
// processBodyError, and fetchParams’s task destination.
try {
for await (const bytes of request.body.stream) {
yield * processBodyChunk(bytes)
}
processEndOfBody()
} catch (err) {
processBodyError(err)
// 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.
try {
for await (const bytes of request.body.stream) {
yield * processBodyChunk(bytes)
}
processEndOfBody()
} catch (err) {
processBodyError(err)
}
})()

try {
Expand Down Expand Up @@ -1912,12 +1905,27 @@ async function httpNetworkFetch (

async function dispatch ({ body }) {
const url = requestCurrentURL(request)
// To transmit request’s body body, run these steps:
let bodyRequest
// 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) {
if (fetchParams.processRequestEndOfBody) {
queueMicrotask(() => fetchParams.processRequestEndOfBody())
}
bodyRequest = null
} else {
// 2. requestBody
bodyRequest = fetchParams.controller.dispatcher[kIsMockActive] ? request.body && request.body.source : body
}

return new Promise((resolve, reject) => fetchParams.controller.dispatcher.dispatch(
{
path: url.pathname + url.search,
origin: url.origin,
method: request.method,
body: fetchParams.controller.dispatcher[kIsMockActive] ? request.body && request.body.source : body,
body: bodyRequest,
headers: request.headersList,
maxRedirections: 0
},
Expand Down

0 comments on commit 211bab4

Please sign in to comment.