Skip to content

Commit

Permalink
fix: lazy read body
Browse files Browse the repository at this point in the history
Don't start prefetching the body before headers and status
has been processed.

Fixes: #1271
  • Loading branch information
ronag committed Mar 12, 2022
1 parent 34f1676 commit da9950b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/fetch/index.js
Expand Up @@ -762,16 +762,16 @@ async function schemeFetch (fetchParams) {
switch (scheme) {
case 'about:': {
// If request’s current URL’s path is the string "blank", then return a new response
// whose status message is `OK`, header list is « (`Content-Type`, `text/html;charset=utf-8`) »,
// and body is the empty byte sequence.
// whose status message is `OK`, header list is « (`Content-Type`, `text/html;charset=utf-8`) »,
// and body is the empty byte sequence.
if (path === 'blank') {
const resp = makeResponse({
statusText: 'OK',
headersList: [
'content-type', 'text/html;charset=utf-8'
]
})

resp.urlList = [new URL('about:blank')]
return resp
}
Expand All @@ -784,12 +784,12 @@ async function schemeFetch (fetchParams) {

context.on('terminated', onRequestAborted)

// 1. Run these steps, but abort when the ongoing fetch is terminated:
// 1. Run these steps, but abort when the ongoing fetch is terminated:
// 1a. Let blob be request’s current URL’s blob URL entry’s object.
// https://w3c.github.io/FileAPI/#blob-url-entry
// P.S. Thank God this method is available in node.
const currentURL = requestCurrentURL(request)

// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L52-L56
// Buffer.resolveObjectURL does not ignore URL queries.
if (currentURL.search.length !== 0) {
Expand All @@ -803,7 +803,7 @@ async function schemeFetch (fetchParams) {
return makeNetworkError('invalid method')
}

// 3a. Let response be a new response whose status message is `OK`.
// 3a. Let response be a new response whose status message is `OK`.
const response = makeResponse({ statusText: 'OK', urlList: [currentURL] })

// 4a. Append (`Content-Length`, blob’s size attribute value) to response’s header list.
Expand Down Expand Up @@ -1883,11 +1883,16 @@ function httpNetworkFetch (
decoders.push(new PassThrough())
}

this.decoder = decoders[0].on('drain', resume)
this.decoder = decoders[0].on('drain', resume).on('error', () => {})

const iterator = decoders[decoders.length - 1][Symbol.asyncIterator]()

pullAlgorithm = async (controller) => {
if (resume) {
resume()
resume = null
}

// 4. Set bytes to the result of handling content codings given
// codings and bytes.
let bytes
Expand Down Expand Up @@ -1946,7 +1951,7 @@ function httpNetworkFetch (

resolve(response)

return true
return false
},

onData (chunk) {
Expand Down

0 comments on commit da9950b

Please sign in to comment.