Skip to content

Commit daeb348

Browse files
ZYSzysrvagg
authored andcommitted
lib: move encodeStr function to internal for reusable
PR-URL: #24242 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 91494bf commit daeb348

File tree

3 files changed

+70
-117
lines changed

3 files changed

+70
-117
lines changed

lib/internal/querystring.js

+67
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { ERR_INVALID_URI } = require('internal/errors').codes;
4+
35
const hexTable = new Array(256);
46
for (var i = 0; i < 256; ++i)
57
hexTable[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
@@ -23,7 +25,72 @@ const isHexTable = [
2325
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 256
2426
];
2527

28+
function encodeStr(str, noEscapeTable, hexTable) {
29+
const len = str.length;
30+
if (len === 0)
31+
return '';
32+
33+
var out = '';
34+
var lastPos = 0;
35+
36+
for (var i = 0; i < len; i++) {
37+
var c = str.charCodeAt(i);
38+
39+
// ASCII
40+
if (c < 0x80) {
41+
if (noEscapeTable[c] === 1)
42+
continue;
43+
if (lastPos < i)
44+
out += str.slice(lastPos, i);
45+
lastPos = i + 1;
46+
out += hexTable[c];
47+
continue;
48+
}
49+
50+
if (lastPos < i)
51+
out += str.slice(lastPos, i);
52+
53+
// Multi-byte characters ...
54+
if (c < 0x800) {
55+
lastPos = i + 1;
56+
out += hexTable[0xC0 | (c >> 6)] +
57+
hexTable[0x80 | (c & 0x3F)];
58+
continue;
59+
}
60+
if (c < 0xD800 || c >= 0xE000) {
61+
lastPos = i + 1;
62+
out += hexTable[0xE0 | (c >> 12)] +
63+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
64+
hexTable[0x80 | (c & 0x3F)];
65+
continue;
66+
}
67+
// Surrogate pair
68+
++i;
69+
70+
// This branch should never happen because all URLSearchParams entries
71+
// should already be converted to USVString. But, included for
72+
// completion's sake anyway.
73+
if (i >= len)
74+
throw new ERR_INVALID_URI();
75+
76+
var c2 = str.charCodeAt(i) & 0x3FF;
77+
78+
lastPos = i + 1;
79+
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
80+
out += hexTable[0xF0 | (c >> 18)] +
81+
hexTable[0x80 | ((c >> 12) & 0x3F)] +
82+
hexTable[0x80 | ((c >> 6) & 0x3F)] +
83+
hexTable[0x80 | (c & 0x3F)];
84+
}
85+
if (lastPos === 0)
86+
return str;
87+
if (lastPos < len)
88+
return out + str.slice(lastPos);
89+
return out;
90+
}
91+
2692
module.exports = {
93+
encodeStr,
2794
hexTable,
2895
isHexTable
2996
};

lib/internal/url.js

+1-64
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const util = require('util');
44
const {
5+
encodeStr,
56
hexTable,
67
isHexTable
78
} = require('internal/querystring');
@@ -833,70 +834,6 @@ const noEscape = [
833834
const paramHexTable = hexTable.slice();
834835
paramHexTable[0x20] = '+';
835836

836-
function encodeStr(str, noEscapeTable, hexTable) {
837-
const len = str.length;
838-
if (len === 0)
839-
return '';
840-
841-
var out = '';
842-
var lastPos = 0;
843-
844-
for (var i = 0; i < len; i++) {
845-
var c = str.charCodeAt(i);
846-
847-
// ASCII
848-
if (c < 0x80) {
849-
if (noEscapeTable[c] === 1)
850-
continue;
851-
if (lastPos < i)
852-
out += str.slice(lastPos, i);
853-
lastPos = i + 1;
854-
out += hexTable[c];
855-
continue;
856-
}
857-
858-
if (lastPos < i)
859-
out += str.slice(lastPos, i);
860-
861-
// Multi-byte characters ...
862-
if (c < 0x800) {
863-
lastPos = i + 1;
864-
out += hexTable[0xC0 | (c >> 6)] +
865-
hexTable[0x80 | (c & 0x3F)];
866-
continue;
867-
}
868-
if (c < 0xD800 || c >= 0xE000) {
869-
lastPos = i + 1;
870-
out += hexTable[0xE0 | (c >> 12)] +
871-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
872-
hexTable[0x80 | (c & 0x3F)];
873-
continue;
874-
}
875-
// Surrogate pair
876-
++i;
877-
var c2;
878-
if (i < len)
879-
c2 = str.charCodeAt(i) & 0x3FF;
880-
else {
881-
// This branch should never happen because all URLSearchParams entries
882-
// should already be converted to USVString. But, included for
883-
// completion's sake anyway.
884-
c2 = 0;
885-
}
886-
lastPos = i + 1;
887-
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
888-
out += hexTable[0xF0 | (c >> 18)] +
889-
hexTable[0x80 | ((c >> 12) & 0x3F)] +
890-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
891-
hexTable[0x80 | (c & 0x3F)];
892-
}
893-
if (lastPos === 0)
894-
return str;
895-
if (lastPos < len)
896-
return out + str.slice(lastPos);
897-
return out;
898-
}
899-
900837
// application/x-www-form-urlencoded serializer
901838
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-serializer
902839
function serializeParams(array) {

lib/querystring.js

+2-53
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
'use strict';
2525

2626
const { Buffer } = require('buffer');
27-
const { ERR_INVALID_URI } = require('internal/errors').codes;
2827
const {
28+
encodeStr,
2929
hexTable,
3030
isHexTable
3131
} = require('internal/querystring');
@@ -140,59 +140,8 @@ function qsEscape(str) {
140140
else
141141
str += '';
142142
}
143-
var out = '';
144-
var lastPos = 0;
145-
146-
for (var i = 0; i < str.length; ++i) {
147-
var c = str.charCodeAt(i);
148-
149-
// ASCII
150-
if (c < 0x80) {
151-
if (noEscape[c] === 1)
152-
continue;
153-
if (lastPos < i)
154-
out += str.slice(lastPos, i);
155-
lastPos = i + 1;
156-
out += hexTable[c];
157-
continue;
158-
}
159143

160-
if (lastPos < i)
161-
out += str.slice(lastPos, i);
162-
163-
// Multi-byte characters ...
164-
if (c < 0x800) {
165-
lastPos = i + 1;
166-
out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)];
167-
continue;
168-
}
169-
if (c < 0xD800 || c >= 0xE000) {
170-
lastPos = i + 1;
171-
out += hexTable[0xE0 | (c >> 12)] +
172-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
173-
hexTable[0x80 | (c & 0x3F)];
174-
continue;
175-
}
176-
// Surrogate pair
177-
++i;
178-
179-
if (i >= str.length)
180-
throw new ERR_INVALID_URI();
181-
182-
var c2 = str.charCodeAt(i) & 0x3FF;
183-
184-
lastPos = i + 1;
185-
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
186-
out += hexTable[0xF0 | (c >> 18)] +
187-
hexTable[0x80 | ((c >> 12) & 0x3F)] +
188-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
189-
hexTable[0x80 | (c & 0x3F)];
190-
}
191-
if (lastPos === 0)
192-
return str;
193-
if (lastPos < str.length)
194-
return out + str.slice(lastPos);
195-
return out;
144+
return encodeStr(str, noEscape, hexTable);
196145
}
197146

198147
function stringifyPrimitive(v) {

0 commit comments

Comments
 (0)