Skip to content

Commit

Permalink
Remove a couple of unnecessary async functions (#2367)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Oct 23, 2023
1 parent 63afd9b commit 24db5d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
8 changes: 3 additions & 5 deletions index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

const fetchImpl = require('./lib/fetch').fetch

module.exports.fetch = async function fetch (resource, init = undefined) {
try {
return await fetchImpl(resource, init)
} catch (err) {
module.exports.fetch = function fetch (resource, init = undefined) {
return fetchImpl(resource, init).catch((err) => {
Error.captureStackTrace(err, this)
throw err
}
})
}
module.exports.FormData = require('./lib/fetch/formdata').FormData
module.exports.Headers = require('./lib/fetch/headers').Headers
Expand Down
53 changes: 27 additions & 26 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Fetch extends EE {
}

// https://fetch.spec.whatwg.org/#fetch-method
async function fetch (input, init = {}) {
function fetch (input, init = {}) {
webidl.argumentLengthCheck(arguments, 1, { header: 'globalThis.fetch' })

// 1. Let p be a new promise.
Expand Down Expand Up @@ -204,7 +204,7 @@ async function fetch (input, init = {}) {
const processResponse = (response) => {
// 1. If locallyAborted is true, terminate these substeps.
if (locallyAborted) {
return
return Promise.resolve()
}

// 2. If response’s aborted flag is set, then:
Expand All @@ -217,7 +217,7 @@ async function fetch (input, init = {}) {
// deserializedError.

abortFetch(p, request, responseObject, controller.serializedAbortReason)
return
return Promise.resolve()
}

// 3. If response is a network error, then reject p with a TypeError
Expand All @@ -226,7 +226,7 @@ async function fetch (input, init = {}) {
p.reject(
Object.assign(new TypeError('fetch failed'), { cause: response.error })
)
return
return Promise.resolve()
}

// 4. Set responseObject to the result of creating a Response object,
Expand Down Expand Up @@ -776,13 +776,13 @@ async function mainFetch (fetchParams, recursive = false) {

// https://fetch.spec.whatwg.org/#concept-scheme-fetch
// given a fetch params fetchParams
async function schemeFetch (fetchParams) {
function schemeFetch (fetchParams) {
// Note: since the connection is destroyed on redirect, which sets fetchParams to a
// cancelled state, we do not want this condition to trigger *unless* there have been
// no redirects. See https://github.com/nodejs/undici/issues/1776
// 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
if (isCancelled(fetchParams) && fetchParams.request.redirectCount === 0) {
return makeAppropriateNetworkError(fetchParams)
return Promise.resolve(makeAppropriateNetworkError(fetchParams))
}

// 2. Let request be fetchParams’s request.
Expand All @@ -798,7 +798,7 @@ async function schemeFetch (fetchParams) {
// and body is the empty byte sequence as a body.

// Otherwise, return a network error.
return makeNetworkError('about scheme is not supported')
return Promise.resolve(makeNetworkError('about scheme is not supported'))
}
case 'blob:': {
if (!resolveObjectURL) {
Expand All @@ -811,15 +811,15 @@ async function schemeFetch (fetchParams) {
// 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 (blobURLEntry.search.length !== 0) {
return makeNetworkError('NetworkError when attempting to fetch resource.')
return Promise.resolve(makeNetworkError('NetworkError when attempting to fetch resource.'))
}

const blobURLEntryObject = resolveObjectURL(blobURLEntry.toString())

// 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')
return Promise.resolve(makeNetworkError('invalid method'))
}

// 3. Let bodyWithType be the result of safely extracting blobURLEntry’s object.
Expand All @@ -846,7 +846,7 @@ async function schemeFetch (fetchParams) {

response.body = body

return response
return Promise.resolve(response)
}
case 'data:': {
// 1. Let dataURLStruct be the result of running the
Expand All @@ -857,7 +857,7 @@ async function schemeFetch (fetchParams) {
// 2. If dataURLStruct is failure, then return a
// network error.
if (dataURLStruct === 'failure') {
return makeNetworkError('failed to fetch the data URL')
return Promise.resolve(makeNetworkError('failed to fetch the data URL'))
}

// 3. Let mimeType be dataURLStruct’s MIME type, serialized.
Expand All @@ -866,28 +866,28 @@ async function schemeFetch (fetchParams) {
// 4. Return a response whose status message is `OK`,
// header list is « (`Content-Type`, mimeType) »,
// and body is dataURLStruct’s body as a body.
return makeResponse({
return Promise.resolve(makeResponse({
statusText: 'OK',
headersList: [
['content-type', { name: 'Content-Type', value: mimeType }]
],
body: safelyExtractBody(dataURLStruct.body)[0]
})
}))
}
case 'file:': {
// For now, unfortunate as it is, file URLs are left as an exercise for the reader.
// When in doubt, return a network error.
return makeNetworkError('not implemented... yet...')
return Promise.resolve(makeNetworkError('not implemented... yet...'))
}
case 'http:':
case 'https:': {
// Return the result of running HTTP fetch given fetchParams.

return await httpFetch(fetchParams)
return httpFetch(fetchParams)
.catch((err) => makeNetworkError(err))
}
default: {
return makeNetworkError('unknown scheme')
return Promise.resolve(makeNetworkError('unknown scheme'))
}
}
}
Expand All @@ -906,7 +906,7 @@ function finalizeResponse (fetchParams, response) {
}

// https://fetch.spec.whatwg.org/#fetch-finale
async function fetchFinale (fetchParams, response) {
function fetchFinale (fetchParams, response) {
// 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] ».
Expand Down Expand Up @@ -990,8 +990,9 @@ async function fetchFinale (fetchParams, response) {
} else {
// 4. Otherwise, fully read response’s body given processBody, processBodyError,
// and fetchParams’s task destination.
await fullyReadBody(response.body, processBody, processBodyError)
return fullyReadBody(response.body, processBody, processBodyError)
}
return Promise.resolve()
}
}

Expand Down Expand Up @@ -1099,7 +1100,7 @@ async function httpFetch (fetchParams) {
}

// https://fetch.spec.whatwg.org/#http-redirect-fetch
async function httpRedirectFetch (fetchParams, response) {
function httpRedirectFetch (fetchParams, response) {
// 1. Let request be fetchParams’s request.
const request = fetchParams.request

Expand All @@ -1125,18 +1126,18 @@ async function httpRedirectFetch (fetchParams, response) {
}
} catch (err) {
// 5. If locationURL is failure, then return a network error.
return makeNetworkError(err)
return Promise.resolve(makeNetworkError(err))
}

// 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network
// error.
if (!urlIsHttpHttpsScheme(locationURL)) {
return makeNetworkError('URL scheme must be a HTTP(S) scheme')
return Promise.resolve(makeNetworkError('URL scheme must be a HTTP(S) scheme'))
}

// 7. If request’s redirect count is 20, then return a network error.
if (request.redirectCount === 20) {
return makeNetworkError('redirect count exceeded')
return Promise.resolve(makeNetworkError('redirect count exceeded'))
}

// 8. Increase request’s redirect count by 1.
Expand All @@ -1150,7 +1151,7 @@ async function httpRedirectFetch (fetchParams, response) {
(locationURL.username || locationURL.password) &&
!sameOrigin(request, locationURL)
) {
return makeNetworkError('cross origin not allowed for request mode "cors"')
return Promise.resolve(makeNetworkError('cross origin not allowed for request mode "cors"'))
}

// 10. If request’s response tainting is "cors" and locationURL includes
Expand All @@ -1159,9 +1160,9 @@ async function httpRedirectFetch (fetchParams, response) {
request.responseTainting === 'cors' &&
(locationURL.username || locationURL.password)
) {
return makeNetworkError(
return Promise.resolve(makeNetworkError(
'URL cannot contain credentials for request mode "cors"'
)
))
}

// 11. If actualResponse’s status is not 303, request’s body is non-null,
Expand All @@ -1171,7 +1172,7 @@ async function httpRedirectFetch (fetchParams, response) {
request.body != null &&
request.body.source == null
) {
return makeNetworkError()
return Promise.resolve(makeNetworkError())
}

// 12. If one of the following is true
Expand Down

0 comments on commit 24db5d4

Please sign in to comment.