From e997db9d7b1e47769329e412db7ffedbe182614a Mon Sep 17 00:00:00 2001 From: Sergey Golovin Date: Thu, 1 Mar 2018 21:56:39 +0300 Subject: [PATCH 1/2] internal/url: refactor "escapeParam" function to make it common --- lib/internal/url.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 0bdfc4783e65f3..d3be73662a51d8 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -805,7 +805,7 @@ const noEscape = [ const paramHexTable = hexTable.slice(); paramHexTable[0x20] = '+'; -function escapeParam(str) { +function encodeStr(str, noEscapeTable, hexTable) { const len = str.length; if (len === 0) return ''; @@ -818,12 +818,12 @@ function escapeParam(str) { // ASCII if (c < 0x80) { - if (noEscape[c] === 1) + if (noEscapeTable[c] === 1) continue; if (lastPos < i) out += str.slice(lastPos, i); lastPos = i + 1; - out += paramHexTable[c]; + out += hexTable[c]; continue; } @@ -833,15 +833,15 @@ function escapeParam(str) { // Multi-byte characters ... if (c < 0x800) { lastPos = i + 1; - out += paramHexTable[0xC0 | (c >> 6)] + - paramHexTable[0x80 | (c & 0x3F)]; + out += hexTable[0xC0 | (c >> 6)] + + hexTable[0x80 | (c & 0x3F)]; continue; } if (c < 0xD800 || c >= 0xE000) { lastPos = i + 1; - out += paramHexTable[0xE0 | (c >> 12)] + - paramHexTable[0x80 | ((c >> 6) & 0x3F)] + - paramHexTable[0x80 | (c & 0x3F)]; + out += hexTable[0xE0 | (c >> 12)] + + hexTable[0x80 | ((c >> 6) & 0x3F)] + + hexTable[0x80 | (c & 0x3F)]; continue; } // Surrogate pair @@ -857,10 +857,10 @@ function escapeParam(str) { } lastPos = i + 1; c = 0x10000 + (((c & 0x3FF) << 10) | c2); - out += paramHexTable[0xF0 | (c >> 18)] + - paramHexTable[0x80 | ((c >> 12) & 0x3F)] + - paramHexTable[0x80 | ((c >> 6) & 0x3F)] + - paramHexTable[0x80 | (c & 0x3F)]; + out += hexTable[0xF0 | (c >> 18)] + + hexTable[0x80 | ((c >> 12) & 0x3F)] + + hexTable[0x80 | ((c >> 6) & 0x3F)] + + hexTable[0x80 | (c & 0x3F)]; } if (lastPos === 0) return str; @@ -876,9 +876,17 @@ function serializeParams(array) { if (len === 0) return ''; - var output = `${escapeParam(array[0])}=${escapeParam(array[1])}`; - for (var i = 2; i < len; i += 2) - output += `&${escapeParam(array[i])}=${escapeParam(array[i + 1])}`; + const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable); + const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable); + let output = + `${firstEncodedParam}=${firstEncodedValue}`; + + for (var i = 2; i < len; i += 2) { + const encodedParam = encodeStr(array[i], noEscape, paramHexTable); + const encodedValue = encodeStr(array[i + 1], noEscape, paramHexTable); + output += `&${encodedParam}=${encodedValue}`; + } + return output; } @@ -1422,5 +1430,6 @@ module.exports = { domainToUnicode, urlToOptions, formatSymbol: kFormat, - searchParamsSymbol: searchParams + searchParamsSymbol: searchParams, + encodeStr }; From 43b60aff0305ecdc69fd054d4e8d828ee4862c8d Mon Sep 17 00:00:00 2001 From: Sergey Golovin Date: Thu, 1 Mar 2018 22:18:37 +0300 Subject: [PATCH 2/2] url: remove redundant function --- lib/internal/url.js | 3 +- lib/url.js | 94 ++++++++++----------------------------------- 2 files changed, 21 insertions(+), 76 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index d3be73662a51d8..187a3e1688d28a 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -878,8 +878,7 @@ function serializeParams(array) { const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable); const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable); - let output = - `${firstEncodedParam}=${firstEncodedValue}`; + let output = `${firstEncodedParam}=${firstEncodedValue}`; for (var i = 2; i < len; i += 2) { const encodedParam = encodeStr(array[i], noEscape, paramHexTable); diff --git a/lib/url.js b/lib/url.js index ab4b2b4647edd2..2fd19e8f65571c 100644 --- a/lib/url.js +++ b/lib/url.js @@ -36,7 +36,8 @@ const { URLSearchParams, domainToASCII, domainToUnicode, - formatSymbol + formatSymbol, + encodeStr, } = require('internal/url'); // Original url.parse() API @@ -504,10 +505,27 @@ function urlFormat(urlObject, options) { return urlObject.format(); } +// These characters do not need escaping: +// ! - . _ ~ +// ' ( ) * : +// digits +// alpha (uppercase) +// alpha (lowercase) +const noEscapeAuth = [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F + 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x30 - 0x3F + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F +]; + Url.prototype.format = function format() { var auth = this.auth || ''; if (auth) { - auth = encodeAuth(auth); + auth = encodeStr(auth, noEscapeAuth, hexTable); auth += '@'; } @@ -890,78 +908,6 @@ Url.prototype.parseHost = function parseHost() { if (host) this.hostname = host; }; -// These characters do not need escaping: -// ! - . _ ~ -// ' ( ) * : -// digits -// alpha (uppercase) -// alpha (lowercase) -const noEscapeAuth = [ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F - 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x30 - 0x3F - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F -]; - -function encodeAuth(str) { - // faster encodeURIComponent alternative for encoding auth uri components - var out = ''; - var lastPos = 0; - for (var i = 0; i < str.length; ++i) { - var c = str.charCodeAt(i); - - // ASCII - if (c < 0x80) { - if (noEscapeAuth[c] === 1) - continue; - if (lastPos < i) - out += str.slice(lastPos, i); - lastPos = i + 1; - out += hexTable[c]; - continue; - } - - if (lastPos < i) - out += str.slice(lastPos, i); - - // Multi-byte characters ... - if (c < 0x800) { - lastPos = i + 1; - out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]; - continue; - } - if (c < 0xD800 || c >= 0xE000) { - lastPos = i + 1; - out += hexTable[0xE0 | (c >> 12)] + - hexTable[0x80 | ((c >> 6) & 0x3F)] + - hexTable[0x80 | (c & 0x3F)]; - continue; - } - // Surrogate pair - ++i; - var c2; - if (i < str.length) - c2 = str.charCodeAt(i) & 0x3FF; - else - c2 = 0; - lastPos = i + 1; - c = 0x10000 + (((c & 0x3FF) << 10) | c2); - out += hexTable[0xF0 | (c >> 18)] + - hexTable[0x80 | ((c >> 12) & 0x3F)] + - hexTable[0x80 | ((c >> 6) & 0x3F)] + - hexTable[0x80 | (c & 0x3F)]; - } - if (lastPos === 0) - return str; - if (lastPos < str.length) - return out + str.slice(lastPos); - return out; -} - module.exports = { // Original API Url,