diff --git a/doc/api/querystring.md b/doc/api/querystring.md index 34d7675a9c99cc..2b8755df413914 100644 --- a/doc/api/querystring.md +++ b/doc/api/querystring.md @@ -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: '' }); diff --git a/lib/querystring.js b/lib/querystring.js index df954cfbd85211..f0ed3eb155d155 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -159,6 +159,8 @@ function stringifyPrimitive(v) { 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 ''; @@ -173,6 +175,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 ''; diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index 58554f0d85c438..7a001f6a029c42 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -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' }),