Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimization of request instantiation #3107

Merged
merged 17 commits into from
Jun 6, 2024
Merged
7 changes: 4 additions & 3 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const {
isBlobLike,
buildURL,
validateHandler,
getServerName
getServerName,
normalizedMethodRecords
} = require('./util')
const { channels } = require('./diagnostics.js')
const { headerNameLowerCasedRecord } = require('./constants')
Expand Down Expand Up @@ -51,13 +52,13 @@ class Request {
method !== 'CONNECT'
) {
throw new InvalidArgumentError('path must be an absolute URL or start with a slash')
} else if (invalidPathRegex.exec(path) !== null) {
} else if (invalidPathRegex.test(path)) {
throw new InvalidArgumentError('invalid request path')
}

if (typeof method !== 'string') {
throw new InvalidArgumentError('method must be a string')
} else if (!isValidHTTPToken(method)) {
} else if (normalizedMethodRecords[method] === undefined && !isValidHTTPToken(method)) {
throw new InvalidArgumentError('invalid request method')
}

Expand Down
27 changes: 27 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,31 @@ function errorRequest (client, request, err) {
const kEnumerableProperty = Object.create(null)
kEnumerableProperty.enumerable = true

const normalizedMethodRecordsBase = {
delete: 'DELETE',
DELETE: 'DELETE',
get: 'GET',
GET: 'GET',
head: 'HEAD',
HEAD: 'HEAD',
options: 'OPTIONS',
OPTIONS: 'OPTIONS',
post: 'POST',
POST: 'POST',
put: 'PUT',
PUT: 'PUT'
}

const normalizedMethodRecords = {
...normalizedMethodRecordsBase,
patch: 'patch',
PATCH: 'PATCH'
}

// Note: object prototypes should not be able to be referenced. e.g. `Object#hasOwnProperty`.
Object.setPrototypeOf(normalizedMethodRecordsBase, null)
Object.setPrototypeOf(normalizedMethodRecords, null)

tsctx marked this conversation as resolved.
Show resolved Hide resolved
module.exports = {
kEnumerableProperty,
nop,
Expand Down Expand Up @@ -600,6 +625,8 @@ module.exports = {
isValidHeaderChar,
isTokenCharCode,
parseRangeHeader,
normalizedMethodRecordsBase,
normalizedMethodRecords,
nodeMajor,
nodeMinor,
nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13),
Expand Down
48 changes: 23 additions & 25 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const nodeUtil = require('node:util')
const {
isValidHTTPToken,
sameOrigin,
normalizeMethod,
makePolicyContainer,
normalizeMethodRecord
makePolicyContainer
} = require('./util')
const {
forbiddenMethodsSet,
Expand All @@ -24,7 +22,7 @@ const {
requestCache,
requestDuplex
} = require('./constants')
const { kEnumerableProperty } = util
const { kEnumerableProperty, normalizedMethodRecordsBase, normalizedMethodRecords } = util
const { kHeaders, kSignal, kState, kGuard, kRealm, kDispatcher } = require('./symbols')
const { webidl } = require('./webidl')
const { getGlobalOrigin } = require('./global')
Expand All @@ -39,6 +37,21 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
signal.removeEventListener('abort', abort)
})

function validateAndNormalizeMethod (method) {
if (!isValidHTTPToken(method)) {
throw new TypeError(`'${method}' is not a valid HTTP method.`)
}

const upperCase = method.toUpperCase()

if (forbiddenMethodsSet.has(upperCase)) {
throw new TypeError(`'${method}' HTTP method is unsupported.`)
}

// Note: must be in uppercase
return normalizedMethodRecordsBase[upperCase] ?? method
}
tsctx marked this conversation as resolved.
Show resolved Hide resolved

let patchMethodWarning = false

// https://fetch.spec.whatwg.org/#request-class
Expand Down Expand Up @@ -318,30 +331,15 @@ class Request {
// 25. If init["method"] exists, then:
if (init.method !== undefined) {
// 1. Let method be init["method"].
let method = init.method
const method = init.method

const mayBeNormalized = normalizeMethodRecord[method]
// 2. If method is not a method or method is a forbidden method, then
// throw a TypeError.

if (mayBeNormalized !== undefined) {
// Note: Bypass validation DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH and these lowercase ones
request.method = mayBeNormalized
} else {
// 2. If method is not a method or method is a forbidden method, then
// throw a TypeError.
if (!isValidHTTPToken(method)) {
throw new TypeError(`'${method}' is not a valid HTTP method.`)
}

if (forbiddenMethodsSet.has(method.toUpperCase())) {
throw new TypeError(`'${method}' HTTP method is unsupported.`)
}

// 3. Normalize method.
method = normalizeMethod(method)
// 3. Normalize method.

// 4. Set request’s method to method.
request.method = method
}
// 4. Set request’s method to method.
request.method = normalizedMethodRecords[method] ?? validateAndNormalizeMethod(method)

if (!patchMethodWarning && request.method === 'patch') {
process.emitWarning('Using `patch` is highly likely to result in a `405 Method Not Allowed`. `PATCH` is much more likely to succeed.', {
Expand Down
30 changes: 2 additions & 28 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { redirectStatusSet, referrerPolicySet: referrerPolicyTokens, badPortsSet
const { getGlobalOrigin } = require('./global')
const { collectASequenceOfCodePoints, collectAnHTTPQuotedString, removeChars, parseMIMEType } = require('./data-url')
const { performance } = require('node:perf_hooks')
const { isBlobLike, ReadableStreamFrom, isValidHTTPToken } = require('../../core/util')
const { isBlobLike, ReadableStreamFrom, isValidHTTPToken, normalizedMethodRecordsBase } = require('../../core/util')
const assert = require('node:assert')
const { isUint8Array } = require('node:util/types')
const { webidl } = require('./webidl')
Expand Down Expand Up @@ -783,37 +783,12 @@ function isCancelled (fetchParams) {
fetchParams.controller.state === 'terminated'
}

const normalizeMethodRecordBase = {
delete: 'DELETE',
DELETE: 'DELETE',
get: 'GET',
GET: 'GET',
head: 'HEAD',
HEAD: 'HEAD',
options: 'OPTIONS',
OPTIONS: 'OPTIONS',
post: 'POST',
POST: 'POST',
put: 'PUT',
PUT: 'PUT'
}

const normalizeMethodRecord = {
...normalizeMethodRecordBase,
patch: 'patch',
PATCH: 'PATCH'
}

// Note: object prototypes should not be able to be referenced. e.g. `Object#hasOwnProperty`.
Object.setPrototypeOf(normalizeMethodRecordBase, null)
Object.setPrototypeOf(normalizeMethodRecord, null)

/**
* @see https://fetch.spec.whatwg.org/#concept-method-normalize
* @param {string} method
*/
function normalizeMethod (method) {
return normalizeMethodRecordBase[method.toLowerCase()] ?? method
return normalizedMethodRecordsBase[method.toLowerCase()] ?? method
}

// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string
Expand Down Expand Up @@ -1606,7 +1581,6 @@ module.exports = {
urlHasHttpsScheme,
urlIsHttpHttpsScheme,
readAllBytes,
normalizeMethodRecord,
simpleRangeHeaderValue,
buildContentRange,
parseMetadata,
Expand Down
Loading