lib: support BigInt in querystring.stringify

Fixes: https://github.com/nodejs/node/issues/36080

PR-URL: https://github.com/nodejs/node/pull/36499
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
raisinten 2020-12-13 18:57:01 +05:30 committed by Michaël Zasso
parent 54e763ddc9
commit 1eb228eafd
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
3 changed files with 25 additions and 2 deletions

View File

@ -122,8 +122,9 @@ The `querystring.stringify()` method produces a URL query string from a
given `obj` by iterating through the object's "own properties".
It serializes the following types of values passed in `obj`:
{string|number|boolean|string[]|number[]|boolean[]}
Any other input values will be coerced to empty strings.
{string|number|bigint|boolean|string[]|number[]|bigint[]|boolean[]}
The numeric values must be finite. Any other input values will be coerced to
empty strings.
```js
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });

View File

@ -162,6 +162,8 @@ function stringifyPrimitive(v) {
return v;
if (typeof v === 'number' && NumberIsFinite(v))
return '' + v;
if (typeof v === 'bigint')
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
@ -176,6 +178,8 @@ function encodeStringified(v, encode) {
// escaping due to the inclusion of a '+' in the output
return (MathAbs(v) < 1e21 ? '' + v : encode('' + v));
}
if (typeof v === 'bigint')
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';

View File

@ -273,6 +273,24 @@ qsWeirdObjects.forEach((testCase) => {
assert.strictEqual(qs.stringify(testCase[0]), testCase[1]);
});
// BigInt values
assert.strictEqual(qs.stringify({ foo: 2n ** 1023n }),
'foo=' + 2n ** 1023n);
assert.strictEqual(qs.stringify([0n, 1n, 2n]),
'0=0&1=1&2=2');
assert.strictEqual(qs.stringify({ foo: 2n ** 1023n },
null,
null,
{ encodeURIComponent: (c) => c }),
'foo=' + 2n ** 1023n);
assert.strictEqual(qs.stringify([0n, 1n, 2n],
null,
null,
{ encodeURIComponent: (c) => c }),
'0=0&1=1&2=2');
// Invalid surrogate pair throws URIError
assert.throws(
() => qs.stringify({ foo: '\udc00' }),