Skip to content

Commit

Permalink
Revert "Revert "support for bigint (#80)""
Browse files Browse the repository at this point in the history
This reverts commit 7cee7e4.
  • Loading branch information
okuryu committed May 28, 2020
1 parent b54341e commit 418dd82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var randomBytes = require('randombytes');
// Generate an internal UID to make the regexp pattern harder to guess.
var UID_LENGTH = 16;
var UID = generateUID();
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|U|I)-' + UID + '-(\\d+)__@"', 'g');
var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|U|I|B)-' + UID + '-(\\d+)__@"', 'g');

var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
var IS_PURE_FUNCTION = /function.*?\(/;
Expand Down Expand Up @@ -70,6 +70,7 @@ module.exports = function serialize(obj, options) {
var sets = [];
var undefs = [];
var infinities= [];
var bigInts = [];

// Returns placeholders for functions and regexps (identified by index)
// which are later replaced by their string representation.
Expand Down Expand Up @@ -119,6 +120,10 @@ module.exports = function serialize(obj, options) {
return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@';
}

if (type === 'bigint') {
return '@__B-' + UID + '-' + (bigInts.push(origValue) - 1) + '__@';
}

return value;
}

Expand Down Expand Up @@ -192,7 +197,7 @@ module.exports = function serialize(obj, options) {
str = str.replace(UNSAFE_CHARS_REGEXP, escapeUnsafeChars);
}

if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0) {
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0) {
return str;
}

Expand Down Expand Up @@ -231,6 +236,10 @@ module.exports = function serialize(obj, options) {
return infinities[valueIndex];
}

if (type === 'B') {
return "BigInt(\"" + bigInts[valueIndex] + "\")";
}

var fn = functions[valueIndex];

return serializeFunc(fn);
Expand Down
18 changes: 18 additions & 0 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,24 @@ describe('serialize( obj )', function () {
});
});

describe('BigInt', function () {
it('should serialize BigInt', function () {
var b = BigInt(9999);
expect(serialize(b)).to.equal('BigInt("9999")');
expect(serialize({t: [b]})).to.be.a('string').equal('{"t":[BigInt("9999")]}');
});

it('should deserialize BigInt', function () {
var d = eval(serialize(BigInt(9999)));
expect(d).to.be.a('BigInt');
expect(d.toString()).to.equal('9999');
});

it('should throw error for invalid bigint', function () {
expect(() => serialize(BigInt('abc'))).to.throw(Error);
});
});

describe('XSS', function () {
it('should encode unsafe HTML chars to Unicode', function () {
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');
Expand Down

0 comments on commit 418dd82

Please sign in to comment.