Skip to content

Commit b918b7a

Browse files
authored
perf: twice faster method check (#2495)
1 parent 0690568 commit b918b7a

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

lib/core/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Request {
8080

8181
if (typeof method !== 'string') {
8282
throw new InvalidArgumentError('method must be a string')
83-
} else if (tokenRegExp.exec(method) === null) {
83+
} else if (!util.isValidHTTPToken(method)) {
8484
throw new InvalidArgumentError('invalid request method')
8585
}
8686

lib/core/util.js

+48
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,52 @@ function toUSVString (val) {
442442
return `${val}`
443443
}
444444

445+
/**
446+
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
447+
* @param {number} c
448+
*/
449+
function isTokenCharCode (c) {
450+
switch (c) {
451+
case 0x22:
452+
case 0x28:
453+
case 0x29:
454+
case 0x2c:
455+
case 0x2f:
456+
case 0x3a:
457+
case 0x3b:
458+
case 0x3c:
459+
case 0x3d:
460+
case 0x3e:
461+
case 0x3f:
462+
case 0x40:
463+
case 0x5b:
464+
case 0x5c:
465+
case 0x5d:
466+
case 0x7b:
467+
case 0x7d:
468+
// DQUOTE and "(),/:;<=>?@[\]{}"
469+
return false
470+
default:
471+
// VCHAR %x21-7E
472+
return c >= 0x21 && c <= 0x7e
473+
}
474+
}
475+
476+
/**
477+
* @param {string} characters
478+
*/
479+
function isValidHTTPToken (characters) {
480+
if (characters.length === 0) {
481+
return false
482+
}
483+
for (let i = 0; i < characters.length; ++i) {
484+
if (!isTokenCharCode(characters.charCodeAt(i))) {
485+
return false
486+
}
487+
}
488+
return true
489+
}
490+
445491
// Parsed accordingly to RFC 9110
446492
// https://www.rfc-editor.org/rfc/rfc9110#field.content-range
447493
function parseRangeHeader (range) {
@@ -490,6 +536,8 @@ module.exports = {
490536
isFormDataLike,
491537
buildURL,
492538
addAbortListener,
539+
isValidHTTPToken,
540+
isTokenCharCode,
493541
parseRangeHeader,
494542
nodeMajor,
495543
nodeMinor,

lib/fetch/util.js

+1-47
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { redirectStatusSet, referrerPolicySet: referrerPolicyTokens, badPortsSet } = require('./constants')
44
const { getGlobalOrigin } = require('./global')
55
const { performance } = require('perf_hooks')
6-
const { isBlobLike, toUSVString, ReadableStreamFrom } = require('../core/util')
6+
const { isBlobLike, toUSVString, ReadableStreamFrom, isValidHTTPToken } = require('../core/util')
77
const assert = require('assert')
88
const { isUint8Array } = require('util/types')
99

@@ -103,52 +103,6 @@ function isValidReasonPhrase (statusText) {
103103
return true
104104
}
105105

106-
/**
107-
* @see https://tools.ietf.org/html/rfc7230#section-3.2.6
108-
* @param {number} c
109-
*/
110-
function isTokenCharCode (c) {
111-
switch (c) {
112-
case 0x22:
113-
case 0x28:
114-
case 0x29:
115-
case 0x2c:
116-
case 0x2f:
117-
case 0x3a:
118-
case 0x3b:
119-
case 0x3c:
120-
case 0x3d:
121-
case 0x3e:
122-
case 0x3f:
123-
case 0x40:
124-
case 0x5b:
125-
case 0x5c:
126-
case 0x5d:
127-
case 0x7b:
128-
case 0x7d:
129-
// DQUOTE and "(),/:;<=>?@[\]{}"
130-
return false
131-
default:
132-
// VCHAR %x21-7E
133-
return c >= 0x21 && c <= 0x7e
134-
}
135-
}
136-
137-
/**
138-
* @param {string} characters
139-
*/
140-
function isValidHTTPToken (characters) {
141-
if (characters.length === 0) {
142-
return false
143-
}
144-
for (let i = 0; i < characters.length; ++i) {
145-
if (!isTokenCharCode(characters.charCodeAt(i))) {
146-
return false
147-
}
148-
}
149-
return true
150-
}
151-
152106
/**
153107
* @see https://fetch.spec.whatwg.org/#header-name
154108
* @param {string} potentialValue

0 commit comments

Comments
 (0)