Skip to content

Commit

Permalink
perf: twice faster method check (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored Dec 8, 2023
1 parent 0690568 commit b918b7a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Request {

if (typeof method !== 'string') {
throw new InvalidArgumentError('method must be a string')
} else if (tokenRegExp.exec(method) === null) {
} else if (!util.isValidHTTPToken(method)) {
throw new InvalidArgumentError('invalid request method')
}

Expand Down
48 changes: 48 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,52 @@ function toUSVString (val) {
return `${val}`
}

/**
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
* @param {number} c
*/
function isTokenCharCode (c) {
switch (c) {
case 0x22:
case 0x28:
case 0x29:
case 0x2c:
case 0x2f:
case 0x3a:
case 0x3b:
case 0x3c:
case 0x3d:
case 0x3e:
case 0x3f:
case 0x40:
case 0x5b:
case 0x5c:
case 0x5d:
case 0x7b:
case 0x7d:
// DQUOTE and "(),/:;<=>?@[\]{}"
return false
default:
// VCHAR %x21-7E
return c >= 0x21 && c <= 0x7e
}
}

/**
* @param {string} characters
*/
function isValidHTTPToken (characters) {
if (characters.length === 0) {
return false
}
for (let i = 0; i < characters.length; ++i) {
if (!isTokenCharCode(characters.charCodeAt(i))) {
return false
}
}
return true
}

// Parsed accordingly to RFC 9110
// https://www.rfc-editor.org/rfc/rfc9110#field.content-range
function parseRangeHeader (range) {
Expand Down Expand Up @@ -490,6 +536,8 @@ module.exports = {
isFormDataLike,
buildURL,
addAbortListener,
isValidHTTPToken,
isTokenCharCode,
parseRangeHeader,
nodeMajor,
nodeMinor,
Expand Down
48 changes: 1 addition & 47 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { redirectStatusSet, referrerPolicySet: referrerPolicyTokens, badPortsSet } = require('./constants')
const { getGlobalOrigin } = require('./global')
const { performance } = require('perf_hooks')
const { isBlobLike, toUSVString, ReadableStreamFrom } = require('../core/util')
const { isBlobLike, toUSVString, ReadableStreamFrom, isValidHTTPToken } = require('../core/util')
const assert = require('assert')
const { isUint8Array } = require('util/types')

Expand Down Expand Up @@ -103,52 +103,6 @@ function isValidReasonPhrase (statusText) {
return true
}

/**
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
* @param {number} c
*/
function isTokenCharCode (c) {
switch (c) {
case 0x22:
case 0x28:
case 0x29:
case 0x2c:
case 0x2f:
case 0x3a:
case 0x3b:
case 0x3c:
case 0x3d:
case 0x3e:
case 0x3f:
case 0x40:
case 0x5b:
case 0x5c:
case 0x5d:
case 0x7b:
case 0x7d:
// DQUOTE and "(),/:;<=>?@[\]{}"
return false
default:
// VCHAR %x21-7E
return c >= 0x21 && c <= 0x7e
}
}

/**
* @param {string} characters
*/
function isValidHTTPToken (characters) {
if (characters.length === 0) {
return false
}
for (let i = 0; i < characters.length; ++i) {
if (!isTokenCharCode(characters.charCodeAt(i))) {
return false
}
}
return true
}

/**
* @see https://fetch.spec.whatwg.org/#header-name
* @param {string} potentialValue
Expand Down

0 comments on commit b918b7a

Please sign in to comment.