diff --git a/lib/fetch/index.js b/lib/fetch/index.js index 6fcd106039e..0b99d546380 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -783,12 +783,17 @@ async function mainFetch (fetchParams, recursive = false) { // https://fetch.spec.whatwg.org/#concept-scheme-fetch // given a fetch params fetchParams async function schemeFetch (fetchParams) { - // let request be fetchParams’s request + // 1. If fetchParams is canceled, then return the appropriate network error for fetchParams. + if (isCancelled(fetchParams)) { + return makeAppropriateNetworkError(fetchParams) + } + + // 2. Let request be fetchParams’s request. const { request } = fetchParams const { protocol: scheme } = requestCurrentURL(request) - // switch on request’s current URL’s scheme, and run the associated steps: + // 3. Switch on request’s current URL’s scheme and run the associated steps: switch (scheme) { case 'about:': { // If request’s current URL’s path is the string "blank", then return a new response @@ -799,29 +804,29 @@ async function schemeFetch (fetchParams) { return makeNetworkError('about scheme is not supported') } case 'blob:': { - resolveObjectURL = resolveObjectURL || require('buffer').resolveObjectURL + if (!resolveObjectURL) { + resolveObjectURL = require('buffer').resolveObjectURL + } - // 1. Run these steps, but abort when the ongoing fetch is terminated: - // 1. 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) + // 1. Let blobURLEntry be request’s current URL’s blob URL entry. + const blobURLEntry = 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) { + if (blobURLEntry.search.length !== 0) { return makeNetworkError('NetworkError when attempting to fetch resource.') } - const blob = resolveObjectURL(currentURL.toString()) + const blobURLEntryObject = resolveObjectURL(blobURLEntry.toString()) - // 2. If request’s method is not `GET` or blob is not a Blob object, then return a network error. - if (request.method !== 'GET' || !isBlobLike(blob)) { + // 2. If request’s method is not `GET`, blobURLEntry is null, or blobURLEntry’s + // object is not a Blob object, then return a network error. + if (request.method !== 'GET' || !isBlobLike(blobURLEntryObject)) { return makeNetworkError('invalid method') } - // 3. Let bodyWithType be the result of safely extracting blobURLEntry’s object. - const bodyWithType = safelyExtractBody(blob) + // 3. Let bodyWithType be the result of safely extracting blobURLEntry’s object. + const bodyWithType = safelyExtractBody(blobURLEntryObject) // 4. Let body be bodyWithType’s body. const body = bodyWithType[0] @@ -845,8 +850,6 @@ async function schemeFetch (fetchParams) { response.body = body return response - // 2. If aborted, then return the appropriate network error for fetchParams. - // TODO } case 'data:': { // 1. Let dataURLStruct be the result of running the