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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions benchmarks/core/request-instantiation.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { bench, run } from 'mitata'

import Request from '../../lib/core/request.js'
import DecoratorHandler from '../../lib/handler/decorator-handler.js'

const handler = new DecoratorHandler({})

bench('new Request()', () => {
return new Request('https://localhost', { path: '/', method: 'get', body: null }, handler)
})

await run()
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 @@ -645,6 +645,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 @@ -683,6 +708,8 @@ module.exports = {
isValidHeaderValue,
isTokenCharCode,
parseRangeHeader,
normalizedMethodRecordsBase,
normalizedMethodRecords,
isValidPort,
isHttpOrHttpsPrefixed,
nodeMajor,
Expand Down
16 changes: 9 additions & 7 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,
environmentSettingsObject,
normalizeMethodRecord
environmentSettingsObject
} = 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, kDispatcher } = require('./symbols')
const { webidl } = require('./webidl')
const { URLSerializer } = require('./data-url')
Expand Down Expand Up @@ -349,7 +347,7 @@ class Request {
// 1. Let method be init["method"].
let method = init.method

const mayBeNormalized = normalizeMethodRecord[method]
const mayBeNormalized = normalizedMethodRecords[method]

if (mayBeNormalized !== undefined) {
// Note: Bypass validation DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH and these lowercase ones
Expand All @@ -361,12 +359,16 @@ class Request {
throw new TypeError(`'${method}' is not a valid HTTP method.`)
}

if (forbiddenMethodsSet.has(method.toUpperCase())) {
const upperCase = method.toUpperCase()

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

// 3. Normalize method.
method = normalizeMethod(method)
// https://fetch.spec.whatwg.org/#concept-method-normalize
// Note: must be in uppercase
method = normalizedMethodRecordsBase[upperCase] ?? method

// 4. Set request’s method to method.
request.method = method
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 @@ -791,37 +791,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 @@ -1639,7 +1614,6 @@ module.exports = {
urlHasHttpsScheme,
urlIsHttpHttpsScheme,
readAllBytes,
normalizeMethodRecord,
simpleRangeHeaderValue,
buildContentRange,
parseMetadata,
Expand Down
Loading