forked from postmanlabs/postman-url-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy.js
242 lines (205 loc) · 5.54 KB
/
legacy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var url = require('url'),
/**
* @private
* @const
* @type {String}
*/
E = '',
/**
* @private
* @const
* @type {String}
*/
QUERY_SEPARATOR = '?',
/**
* @private
* @const
* @type {String}
*/
AMPERSAND = '&',
/**
* @private
* @const
* @type {String}
*/
EQUALS = '=',
/**
* @private
* @const
* @type {String}
*/
PERCENT = '%',
/**
* @private
* @const
* @type {string}
*/
ZERO = '0',
/**
* @private
* @const
* @type {string}
*/
STRING = 'string',
encoder;
encoder = {
/**
* Percent encode a character with given code.
*
* @param {Number} c - character code of the character to encode
* @returns {String} - percent encoding of given character
*/
percentEncode (c) {
var hex = c.toString(16).toUpperCase();
(hex.length === 1) && (hex = ZERO + hex);
return PERCENT + hex;
},
/**
* Checks if character at given index in the buffer is already percent encoded or not.
*
* @param {Buffer} buffer -
* @param {Number} i -
* @returns {Boolean}
*/
isPreEncoded (buffer, i) {
// If it is % check next two bytes for percent encode characters
// looking for pattern %00 - %FF
return (buffer[i] === 0x25 &&
(encoder.isPreEncodedCharacter(buffer[i + 1]) &&
encoder.isPreEncodedCharacter(buffer[i + 2]))
);
},
/**
* Checks if character with given code is valid hexadecimal digit or not.
*
* @param {Number} byte -
* @returns {Boolean}
*/
isPreEncodedCharacter (byte) {
return (byte >= 0x30 && byte <= 0x39) || // 0-9
(byte >= 0x41 && byte <= 0x46) || // A-F
(byte >= 0x61 && byte <= 0x66); // a-f
},
/**
* Checks whether given character should be percent encoded or not for fixture.
*
* @param {Number} byte -
* @returns {Boolean}
*/
charactersToPercentEncode (byte) {
return (byte < 0x23 || byte > 0x7E || // Below # and after ~
byte === 0x3C || byte === 0x3E || // > and <
byte === 0x28 || byte === 0x29 || // ( and )
byte === 0x25 || // %
byte === 0x27 || // '
byte === 0x2A // *
);
},
/**
* Percent encode a query string according to RFC 3986.
* Note: This function is supposed to be used on top of node's inbuilt url encoding
* to solve issue https://github.com/nodejs/node/issues/8321
*
* @param {String} value -
* @returns {String}
*/
encode (value) {
if (!value) { return E; }
var buffer = Buffer.from(value),
ret = E,
i,
ii;
for (i = 0, ii = buffer.length; i < ii; ++i) {
if (encoder.charactersToPercentEncode(buffer[i]) && !encoder.isPreEncoded(buffer, i)) {
ret += encoder.percentEncode(buffer[i]);
}
else {
ret += String.fromCodePoint(buffer[i]); // Only works in ES6 (available in Node v4+)
}
}
return ret;
}
};
/**
* Parses a query string into an array, preserving parameter values
*
* @private
* @param {String} string -
* @returns {*}
*/
function parseQueryString (string) {
var parts;
if (typeof string === STRING) {
parts = string.split(AMPERSAND);
return parts.map(function (param, idx) {
if (param === E && idx !== (parts.length - 1)) {
return { key: null, value: null };
}
var index = (typeof param === STRING) ? param.indexOf(EQUALS) : -1,
paramObj = {};
// this means that there was no value for this key (not even blank,
// so we store this info) and the value is set to null
if (index < 0) {
paramObj.key = param.substr(0, param.length);
paramObj.value = null;
}
else {
paramObj.key = param.substr(0, index);
paramObj.value = param.substr(index + 1);
}
return paramObj;
});
}
return [];
}
/**
* Stringifies a query string, from an array of parameters
*
* @private
* @param {Object[]} parameters -
* @returns {string}
*/
function stringifyQueryParams (parameters) {
return parameters ? parameters.map(function (param) {
var key = param.key,
value = param.value;
if (value === undefined) {
return E;
}
if (key === null) {
key = E;
}
if (value === null) {
return encoder.encode(key);
}
return encoder.encode(key) + EQUALS + encoder.encode(value);
}).join(AMPERSAND) : E;
}
/**
* Converts URL string into Node's Url object with encoded values
*
* @private
* @param {String} urlString -
* @returns {Url}
*/
function toNodeUrl (urlString) {
var parsed = url.parse(urlString),
rawQs = parsed.query,
qs,
search,
path,
str;
if (!(rawQs && rawQs.length)) { return parsed; }
qs = stringifyQueryParams(parseQueryString(rawQs));
search = QUERY_SEPARATOR + qs;
path = parsed.pathname + search;
parsed.query = qs;
parsed.search = search;
parsed.path = path;
str = url.format(parsed);
// Parse again, because Node does not guarantee consistency of properties
return url.parse(str);
}
module.exports = {
toNodeUrl
};