From 0f6c3f76b3af7764dfc204052a9381cd85a230f9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 22:09:33 +0100 Subject: [PATCH] querystring: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36315 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott --- lib/internal/querystring.js | 20 +++++++++++++------- lib/querystring.js | 32 +++++++++++++++++--------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/internal/querystring.js b/lib/internal/querystring.js index d6ecae0c0da5c8..505950e462e7ad 100644 --- a/lib/internal/querystring.js +++ b/lib/internal/querystring.js @@ -3,13 +3,19 @@ const { Array, Int8Array, + NumberPrototypeToString, + StringPrototypeCharCodeAt, + StringPrototypeSlice, + StringPrototypeToUpperCase, } = primordials; const { ERR_INVALID_URI } = require('internal/errors').codes; const hexTable = new Array(256); for (let i = 0; i < 256; ++i) - hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase(); + hexTable[i] = '%' + + StringPrototypeToUpperCase((i < 16 ? '0' : '') + + NumberPrototypeToString(i, 16)); const isHexTable = new Int8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15 @@ -47,13 +53,13 @@ function encodeStr(str, noEscapeTable, hexTable) { outer: for (; i < len; i++) { - let c = str.charCodeAt(i); + let c = StringPrototypeCharCodeAt(str, i); // ASCII while (c < 0x80) { if (noEscapeTable[c] !== 1) { if (lastPos < i) - out += str.slice(lastPos, i); + out += StringPrototypeSlice(str, lastPos, i); lastPos = i + 1; out += hexTable[c]; } @@ -61,11 +67,11 @@ function encodeStr(str, noEscapeTable, hexTable) { if (++i === len) break outer; - c = str.charCodeAt(i); + c = StringPrototypeCharCodeAt(str, i); } if (lastPos < i) - out += str.slice(lastPos, i); + out += StringPrototypeSlice(str, lastPos, i); // Multi-byte characters ... if (c < 0x800) { @@ -90,7 +96,7 @@ function encodeStr(str, noEscapeTable, hexTable) { if (i >= len) throw new ERR_INVALID_URI(); - const c2 = str.charCodeAt(i) & 0x3FF; + const c2 = StringPrototypeCharCodeAt(str, i) & 0x3FF; lastPos = i + 1; c = 0x10000 + (((c & 0x3FF) << 10) | c2); @@ -102,7 +108,7 @@ function encodeStr(str, noEscapeTable, hexTable) { if (lastPos === 0) return str; if (lastPos < len) - return out + str.slice(lastPos); + return out + StringPrototypeSlice(str, lastPos); return out; } diff --git a/lib/querystring.js b/lib/querystring.js index 9ff2c07a1ab49f..848a6c3afbe8ad 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -32,6 +32,8 @@ const { ObjectCreate, ObjectKeys, String, + StringPrototypeCharCodeAt, + StringPrototypeSlice, } = primordials; const { Buffer } = require('buffer'); @@ -91,20 +93,20 @@ function unescapeBuffer(s, decodeSpaces) { // Flag to know if some hex chars have been decoded let hasHex = false; while (index < s.length) { - currentChar = s.charCodeAt(index); + currentChar = StringPrototypeCharCodeAt(s, index); if (currentChar === 43 /* '+' */ && decodeSpaces) { out[outIndex++] = 32; // ' ' index++; continue; } if (currentChar === 37 /* '%' */ && index < maxLength) { - currentChar = s.charCodeAt(++index); + currentChar = StringPrototypeCharCodeAt(s, ++index); hexHigh = unhexTable[currentChar]; if (!(hexHigh >= 0)) { out[outIndex++] = 37; // '%' continue; } else { - nextChar = s.charCodeAt(++index); + nextChar = StringPrototypeCharCodeAt(s, ++index); hexLow = unhexTable[nextChar]; if (!(hexLow >= 0)) { out[outIndex++] = 37; // '%' @@ -272,10 +274,10 @@ function stringify(obj, sep, eq, options) { */ function charCodes(str) { if (str.length === 0) return []; - if (str.length === 1) return [str.charCodeAt(0)]; + if (str.length === 1) return [StringPrototypeCharCodeAt(str, 0)]; const ret = new Array(str.length); for (let i = 0; i < str.length; ++i) - ret[i] = str.charCodeAt(i); + ret[i] = StringPrototypeCharCodeAt(str, i); return ret; } const defSepCodes = [38]; // & @@ -319,8 +321,8 @@ function parse(qs, sep, eq, options) { return obj; } - const sepCodes = (!sep ? defSepCodes : charCodes(sep + '')); - const eqCodes = (!eq ? defEqCodes : charCodes(eq + '')); + const sepCodes = (!sep ? defSepCodes : charCodes(String(sep))); + const eqCodes = (!eq ? defEqCodes : charCodes(String(eq))); const sepLen = sepCodes.length; const eqLen = eqCodes.length; @@ -351,7 +353,7 @@ function parse(qs, sep, eq, options) { const plusChar = (customDecode ? '%20' : ' '); let encodeCheck = 0; for (let i = 0; i < qs.length; ++i) { - const code = qs.charCodeAt(i); + const code = StringPrototypeCharCodeAt(qs, i); // Try matching key/value pair separator (e.g. '&') if (code === sepCodes[sepIdx]) { @@ -362,7 +364,7 @@ function parse(qs, sep, eq, options) { // We didn't find the (entire) key/value separator if (lastPos < end) { // Treat the substring as part of the key instead of the value - key += qs.slice(lastPos, end); + key += StringPrototypeSlice(qs, lastPos, end); } else if (key.length === 0) { // We saw an empty substring between separators if (--pairs === 0) @@ -372,7 +374,7 @@ function parse(qs, sep, eq, options) { continue; } } else if (lastPos < end) { - value += qs.slice(lastPos, end); + value += StringPrototypeSlice(qs, lastPos, end); } addKeyVal(obj, key, value, keyEncoded, valEncoded, decode); @@ -394,7 +396,7 @@ function parse(qs, sep, eq, options) { // Key/value separator match! const end = i - eqIdx + 1; if (lastPos < end) - key += qs.slice(lastPos, end); + key += StringPrototypeSlice(qs, lastPos, end); encodeCheck = 0; lastPos = i + 1; } @@ -420,7 +422,7 @@ function parse(qs, sep, eq, options) { } if (code === 43/* + */) { if (lastPos < i) - key += qs.slice(lastPos, i); + key += StringPrototypeSlice(qs, lastPos, i); key += plusChar; lastPos = i + 1; continue; @@ -428,7 +430,7 @@ function parse(qs, sep, eq, options) { } if (code === 43/* + */) { if (lastPos < i) - value += qs.slice(lastPos, i); + value += StringPrototypeSlice(qs, lastPos, i); value += plusChar; lastPos = i + 1; } else if (!valEncoded) { @@ -451,9 +453,9 @@ function parse(qs, sep, eq, options) { // Deal with any leftover key or value data if (lastPos < qs.length) { if (eqIdx < eqLen) - key += qs.slice(lastPos); + key += StringPrototypeSlice(qs, lastPos); else if (sepIdx < sepLen) - value += qs.slice(lastPos); + value += StringPrototypeSlice(qs, lastPos); } else if (eqIdx === 0 && key.length === 0) { // We ended on an empty substring return obj;