deno.land / std@0.166.0 / node / querystring.ts

querystring.ts
نووسراو ببینە
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { Buffer } from "./buffer.ts";import { encodeStr, hexTable } from "./internal/querystring.ts";
/** * Alias of querystring.parse() * @legacy */export const decode = parse;
/** * Alias of querystring.stringify() * @legacy */export const encode = stringify;
/** * replaces encodeURIComponent() * @see https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.3.4 */function qsEscape(str: unknown): string { if (typeof str !== "string") { if (typeof str === "object") { str = String(str); } else { str += ""; } } return encodeStr(str as string, noEscape, hexTable);}
/** * Performs URL percent-encoding on the given `str` in a manner that is optimized for the specific requirements of URL query strings. * Used by `querystring.stringify()` and is generally not expected to be used directly. * It is exported primarily to allow application code to provide a replacement percent-encoding implementation if necessary by assigning `querystring.escape` to an alternative function. * @legacy * @see Tested in `test-querystring-escape.js` */export const escape = qsEscape;
export interface ParsedUrlQuery { [key: string]: string | string[] | undefined;}
export interface ParsedUrlQueryInput { [key: string]: | string | number | boolean | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<boolean> | null | undefined;}
interface ParseOptions { /** The function to use when decoding percent-encoded characters in the query string. */ decodeURIComponent?: (string: string) => string; /** Specifies the maximum number of keys to parse. */ maxKeys?: number;}
// deno-fmt-ignoreconst isHexTable = new Int8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32 - 47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64 - 79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80 - 95 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96 - 111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 112 - 127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128 ... 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ... 256]);
function charCodes(str: string): number[] { const ret = new Array(str.length); for (let i = 0; i < str.length; ++i) { ret[i] = str.charCodeAt(i); } return ret;}
function addKeyVal( obj: ParsedUrlQuery, key: string, value: string, keyEncoded: boolean, valEncoded: boolean, decode: (encodedURIComponent: string) => string,) { if (key.length > 0 && keyEncoded) { key = decode(key); } if (value.length > 0 && valEncoded) { value = decode(value); }
if (obj[key] === undefined) { obj[key] = value; } else { const curValue = obj[key]; // A simple Array-specific property check is enough here to // distinguish from a string value and is faster and still safe // since we are generating all of the values being assigned. if ((curValue as string[]).pop) { (curValue as string[])[curValue!.length] = value; } else { obj[key] = [curValue as string, value]; } }}
/** * Parses a URL query string into a collection of key and value pairs. * @param str The URL query string to parse * @param sep The substring used to delimit key and value pairs in the query string. Default: '&'. * @param eq The substring used to delimit keys and values in the query string. Default: '='. * @param options The parse options * @param options.decodeURIComponent The function to use when decoding percent-encoded characters in the query string. Default: `querystring.unescape()`. * @param options.maxKeys Specifies the maximum number of keys to parse. Specify `0` to remove key counting limitations. Default: `1000`. * @legacy * @see Tested in test-querystring.js */export function parse( str: string, sep = "&", eq = "=", { decodeURIComponent = unescape, maxKeys = 1000 }: ParseOptions = {},): ParsedUrlQuery { const obj: ParsedUrlQuery = Object.create(null);
if (typeof str !== "string" || str.length === 0) { return obj; }
const sepCodes = (!sep ? [38] /* & */ : charCodes(String(sep))); const eqCodes = (!eq ? [61] /* = */ : charCodes(String(eq))); const sepLen = sepCodes.length; const eqLen = eqCodes.length;
let pairs = 1000; if (typeof maxKeys === "number") { // -1 is used in place of a value like Infinity for meaning // "unlimited pairs" because of additional checks V8 (at least as of v5.4) // has to do when using variables that contain values like Infinity. Since // `pairs` is always decremented and checked explicitly for 0, -1 works // effectively the same as Infinity, while providing a significant // performance boost. pairs = maxKeys > 0 ? maxKeys : -1; }
let decode = unescape; if (decodeURIComponent) { decode = decodeURIComponent; } const customDecode = (decode !== unescape);
let lastPos = 0; let sepIdx = 0; let eqIdx = 0; let key = ""; let value = ""; let keyEncoded = customDecode; let valEncoded = customDecode; const plusChar = (customDecode ? "%20" : " "); let encodeCheck = 0; for (let i = 0; i < str.length; ++i) { const code = str.charCodeAt(i);
// Try matching key/value pair separator (e.g. '&') if (code === sepCodes[sepIdx]) { if (++sepIdx === sepLen) { // Key/value pair separator match! const end = i - sepIdx + 1; if (eqIdx < eqLen) { // 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 += str.slice(lastPos, end); } else if (key.length === 0) { // We saw an empty substring between separators if (--pairs === 0) { return obj; } lastPos = i + 1; sepIdx = eqIdx = 0; continue; } } else if (lastPos < end) { value += str.slice(lastPos, end); }
addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);
if (--pairs === 0) { return obj; } key = value = ""; encodeCheck = 0; lastPos = i + 1; sepIdx = eqIdx = 0; } } else { sepIdx = 0; // Try matching key/value separator (e.g. '=') if we haven't already if (eqIdx < eqLen) { if (code === eqCodes[eqIdx]) { if (++eqIdx === eqLen) { // Key/value separator match! const end = i - eqIdx + 1; if (lastPos < end) { key += str.slice(lastPos, end); } encodeCheck = 0; lastPos = i + 1; } continue; } else { eqIdx = 0; if (!keyEncoded) { // Try to match an (valid) encoded byte once to minimize unnecessary // calls to string decoding functions if (code === 37 /* % */) { encodeCheck = 1; continue; } else if (encodeCheck > 0) { if (isHexTable[code] === 1) { if (++encodeCheck === 3) { keyEncoded = true; } continue; } else { encodeCheck = 0; } } } } if (code === 43 /* + */) { if (lastPos < i) { key += str.slice(lastPos, i); } key += plusChar; lastPos = i + 1; continue; } } if (code === 43 /* + */) { if (lastPos < i) { value += str.slice(lastPos, i); } value += plusChar; lastPos = i + 1; } else if (!valEncoded) { // Try to match an (valid) encoded byte (once) to minimize unnecessary // calls to string decoding functions if (code === 37 /* % */) { encodeCheck = 1; } else if (encodeCheck > 0) { if (isHexTable[code] === 1) { if (++encodeCheck === 3) { valEncoded = true; } } else { encodeCheck = 0; } } } } }
// Deal with any leftover key or value data if (lastPos < str.length) { if (eqIdx < eqLen) { key += str.slice(lastPos); } else if (sepIdx < sepLen) { value += str.slice(lastPos); } } else if (eqIdx === 0 && key.length === 0) { // We ended on an empty substring return obj; }
addKeyVal(obj, key, value, keyEncoded, valEncoded, decode);
return obj;}
interface StringifyOptions { /** The function to use when converting URL-unsafe characters to percent-encoding in the query string. */ encodeURIComponent: (string: string) => string;}
/** * These characters do not need escaping when generating query strings: * ! - . _ ~ * ' ( ) * * digits * alpha (uppercase) * alpha (lowercase) */// deno-fmt-ignoreconst noEscape = new Int8Array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 32 - 47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 80 - 95 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 112 - 127]);
// deno-lint-ignore no-explicit-anyfunction stringifyPrimitive(v: any): string { if (typeof v === "string") { return v; } if (typeof v === "number" && isFinite(v)) { return "" + v; } if (typeof v === "bigint") { return "" + v; } if (typeof v === "boolean") { return v ? "true" : "false"; } return "";}
function encodeStringifiedCustom( // deno-lint-ignore no-explicit-any v: any, encode: (string: string) => string,): string { return encode(stringifyPrimitive(v));}
// deno-lint-ignore no-explicit-anyfunction encodeStringified(v: any, encode: (string: string) => string): string { if (typeof v === "string") { return (v.length ? encode(v) : ""); } if (typeof v === "number" && isFinite(v)) { // Values >= 1e21 automatically switch to scientific notation which requires // escaping due to the inclusion of a '+' in the output return (Math.abs(v) < 1e21 ? "" + v : encode("" + v)); } if (typeof v === "bigint") { return "" + v; } if (typeof v === "boolean") { return v ? "true" : "false"; } return "";}
/** * Produces a URL query string from a given obj by iterating through the object's "own properties". * @param obj The object to serialize into a URL query string. * @param sep The substring used to delimit key and value pairs in the query string. Default: '&'. * @param eq The substring used to delimit keys and values in the query string. Default: '='. * @param options The stringify options * @param options.encodeURIComponent The function to use when converting URL-unsafe characters to percent-encoding in the query string. Default: `querystring.escape()`. * @legacy * @see Tested in `test-querystring.js` */export function stringify( // deno-lint-ignore no-explicit-any obj: Record<string, any>, sep?: string, eq?: string, options?: StringifyOptions,): string { sep ||= "&"; eq ||= "="; const encode = options ? options.encodeURIComponent : qsEscape; const convert = options ? encodeStringifiedCustom : encodeStringified;
if (obj !== null && typeof obj === "object") { const keys = Object.keys(obj); const len = keys.length; let fields = ""; for (let i = 0; i < len; ++i) { const k = keys[i]; const v = obj[k]; let ks = convert(k, encode); ks += eq;
if (Array.isArray(v)) { const vlen = v.length; if (vlen === 0) continue; if (fields) { fields += sep; } for (let j = 0; j < vlen; ++j) { if (j) { fields += sep; } fields += ks; fields += convert(v[j], encode); } } else { if (fields) { fields += sep; } fields += ks; fields += convert(v, encode); } } return fields; } return "";}
// deno-fmt-ignoreconst unhexTable = new Int8Array([ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47 +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, -1, -1, -1, -1, -1, -1, // 48 - 63 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64 - 79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80 - 95 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96 - 111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112 - 127 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128 ... -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 255]);
/** * A safe fast alternative to decodeURIComponent */export function unescapeBuffer(s: string, decodeSpaces = false): Buffer { const out = Buffer.alloc(s.length); let index = 0; let outIndex = 0; let currentChar; let nextChar; let hexHigh; let hexLow; const maxLength = s.length - 2; // Flag to know if some hex chars have been decoded let hasHex = false; while (index < s.length) { currentChar = s.charCodeAt(index); if (currentChar === 43 /* '+' */ && decodeSpaces) { out[outIndex++] = 32; // ' ' index++; continue; } if (currentChar === 37 /* '%' */ && index < maxLength) { currentChar = s.charCodeAt(++index); hexHigh = unhexTable[currentChar]; if (!(hexHigh >= 0)) { out[outIndex++] = 37; // '%' continue; } else { nextChar = s.charCodeAt(++index); hexLow = unhexTable[nextChar]; if (!(hexLow >= 0)) { out[outIndex++] = 37; // '%' index--; } else { hasHex = true; currentChar = hexHigh * 16 + hexLow; } } } out[outIndex++] = currentChar; index++; } return hasHex ? out.slice(0, outIndex) : out;}
function qsUnescape(s: string): string { try { return decodeURIComponent(s); } catch { return unescapeBuffer(s).toString(); }}
/** * Performs decoding of URL percent-encoded characters on the given `str`. * Used by `querystring.parse()` and is generally not expected to be used directly. * It is exported primarily to allow application code to provide a replacement decoding implementation if necessary by assigning `querystring.unescape` to an alternative function. * @legacy * @see Tested in `test-querystring-escape.js` */export const unescape = qsUnescape;
export default { parse, stringify, decode, encode, unescape, escape, unescapeBuffer,};
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago