Skip to content

Commit

Permalink
Introduce support for Infinity (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
vthibault committed Feb 16, 2020
1 parent 82bb2d2 commit eed510c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
13 changes: 11 additions & 2 deletions index.js
Expand Up @@ -8,7 +8,7 @@ See the accompanying LICENSE file for terms.

// Generate an internal UID to make the regexp pattern harder to guess.
var UID = Math.floor(Math.random() * 0x10000000000).toString(16);
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U)-' + UID + '-(\\d+)__@"', 'g');
var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S|U|I)-' + UID + '-(\\d+)__@"', 'g');

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

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

if (type === 'number' && !isNaN(origValue) && !isFinite(origValue)) {
return '@__I-' + UID + '-' + (infinities.push(origValue) - 1) + '__@';
}

return value;
}

Expand Down Expand Up @@ -175,7 +180,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) {
if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && undefs.length === 0 && infinities.length === 0) {
return str;
}

Expand Down Expand Up @@ -203,6 +208,10 @@ module.exports = function serialize(obj, options) {
return 'undefined'
}

if (type === 'I') {
return infinities[valueIndex];
}

var fn = functions[valueIndex];

return serializeFunc(fn);
Expand Down
42 changes: 34 additions & 8 deletions test/unit/serialize.js
Expand Up @@ -335,16 +335,18 @@ describe('serialize( obj )', function () {
var regexKey = /.*/;
var m = new Map([
['a', 123],
[regexKey, 456]
[regexKey, 456],
[Infinity, 789]
]);
expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456]])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456]])]}');
expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[new RegExp(".*", ""),456],[Infinity,789]])]}');
});

it('should deserialize a map', function () {
var m = eval(serialize(new Map([
['a', 123],
[null, 456]
[null, 456],
[Infinity, 789]
])));
expect(m).to.be.a('Map');
expect(m.get(null)).to.equal(456);
Expand All @@ -357,23 +359,47 @@ describe('serialize( obj )', function () {
var m = new Set([
'a',
123,
regex
regex,
Infinity
]);
expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", "")])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", "")])]}');
expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,new RegExp(".*", ""),Infinity])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,new RegExp(".*", ""),Infinity])]}');
});

it('should deserialize a set', function () {
var m = eval(serialize(new Set([
'a',
123,
null
null,
Infinity
])));
expect(m).to.be.a('Set');
expect(m.has(null)).to.equal(true);
});
});

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

it('should deserialize Infinity', function () {
var d = eval(serialize(Infinity));
expect(d).to.equal(Infinity);
});

it('should serialize -Infinity', function () {
expect(serialize(-Infinity)).to.equal('-Infinity');
expect(serialize({t: [-Infinity]})).to.be.a('string').equal('{"t":[-Infinity]}');
});

it('should deserialize -Infinity', function () {
var d = eval(serialize(-Infinity));
expect(d).to.equal(-Infinity);
});
});

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 eed510c

Please sign in to comment.