diff --git a/index.js b/index.js index ab82742..ab58be4 100644 --- a/index.js +++ b/index.js @@ -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|A|U|I|B|L)-' + UID + '-(\\d+)__@"', 'g'); +var PLACE_HOLDER_REGEXP = new RegExp('(\\\\)?"@__(F|R|D|M|S|A|U|I|B|L|X)-' + UID + '-(\\d+)__@"', 'g'); var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g; var IS_PURE_FUNCTION = /function.*?\(/; @@ -73,6 +73,7 @@ module.exports = function serialize(obj, options) { var infinities= []; var bigInts = []; var urls = []; + var bytearrays = []; // Returns placeholders for functions and regexps (identified by index) // which are later replaced by their string representation. @@ -119,6 +120,10 @@ module.exports = function serialize(obj, options) { if(origValue instanceof URL) { return '@__L-' + UID + '-' + (urls.push(origValue) - 1) + '__@'; } + + if(origValue instanceof Uint8Array) { + return '@__X-' + UID + '-' + (bytearrays.push(origValue) - 1) + '__@'; + } } if (type === 'function') { @@ -210,7 +215,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 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0 && urls.length === 0) { + if (functions.length === 0 && regexps.length === 0 && dates.length === 0 && maps.length === 0 && sets.length === 0 && arrays.length === 0 && undefs.length === 0 && infinities.length === 0 && bigInts.length === 0 && urls.length === 0 && bytearrays.length === 0) { return str; } @@ -258,9 +263,12 @@ module.exports = function serialize(obj, options) { } if (type === 'L') { - return "new URL(\"" + urls[valueIndex].toString() + "\")"; + return "new URL(\"" + urls[valueIndex].toString() + "\")"; } + if (type === 'X') { + return `new Uint8Array(${JSON.stringify(Array.from(bytearrays[valueIndex]))})`; + } var fn = functions[valueIndex]; return serializeFunc(fn); diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 6510ec2..e2cdccb 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -412,6 +412,20 @@ describe('serialize( obj )', function () { }); }); + + describe('byte arrays', function () { + it('should serialize byte arrays', function () { + var a = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(serialize(a)).to.be.a('string').equal('new Uint8Array([1,2,3,4,5,6,7,8,9,10])'); + }); + + it('should deserialize a byte array', function () { + var a = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + var b = eval(serialize(a)); + expect(b).to.be.a('Uint8Array').deep.equal(a); + }); + }); + describe('Infinity', function () { it('should serialize Infinity', function () { expect(serialize(Infinity)).to.equal('Infinity'); @@ -529,7 +543,7 @@ describe('serialize( obj )', function () { fn_arrow: () => { return true; } - }; + }; var obj2 = { num: 123, str: 'str',