diff --git a/index.js b/index.js index 3594167..841bb7e 100644 --- a/index.js +++ b/index.js @@ -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.*?\(/; @@ -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. @@ -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; } @@ -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; } @@ -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); diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 0c0df3d..3944344 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -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); @@ -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('')).to.equal('"\\u003C\\u002Fscript\\u003E"');