diff --git a/ext/fetch/23_request.js b/ext/fetch/23_request.js index cfdce01d3ea6bb..5232cc13c96c97 100644 --- a/ext/fetch/23_request.js +++ b/ext/fetch/23_request.js @@ -202,31 +202,29 @@ function cloneInnerRequest(request, skipBody = false) { }; } -/** - * @param {string} m - * @returns {boolean} - */ -function isKnownMethod(m) { - return ( - m === "DELETE" || - m === "GET" || - m === "HEAD" || - m === "OPTIONS" || - m === "POST" || - m === "PUT" - ); -} +// method => normalized method +const KNOWN_METHODS = { + "DELETE": "DELETE", + "delete": "DELETE", + "GET": "GET", + "get": "GET", + "HEAD": "HEAD", + "head": "HEAD", + "OPTIONS": "OPTIONS", + "options": "OPTIONS", + "PATCH": "PATCH", + "patch": "PATCH", + "POST": "POST", + "post": "POST", + "PUT": "PUT", + "put": "PUT", +}; + /** * @param {string} m * @returns {string} */ function validateAndNormalizeMethod(m) { - // Fast path for well-known methods - if (isKnownMethod(m)) { - return m; - } - - // Regular path if (RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, m) === null) { throw new TypeError("Method is not valid."); } @@ -325,9 +323,10 @@ class Request { // 25. if (init.method !== undefined) { - let method = init.method; - method = validateAndNormalizeMethod(method); - request.method = method; + const method = init.method; + // fast path: check for known methods + request.method = KNOWN_METHODS[method] ?? + validateAndNormalizeMethod(method); } // 26.