From 0d407c10d5fbdc5f4a0a584b34af4fc3e8ef7993 Mon Sep 17 00:00:00 2001 From: Andrey Belym Date: Fri, 14 Dec 2018 11:23:44 +0300 Subject: [PATCH 1/2] Add a test --- test/test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test.js b/test/test.js index 71e91e1..93050ec 100644 --- a/test/test.js +++ b/test/test.js @@ -395,4 +395,16 @@ describe('Regression', function () { global.Uint8Array = savedUint8Array; }); + + it('Should encode objects with null as a prototype', function () { + var obj = Object.create(null); + + obj.foo = 'bar'; + obj.ans = 42; + + var actual = replicator.decode(replicator.encode(obj)); + + assert.strictEqual(actual.foo, 'bar'); + assert.strictEqual(actual.ans, 42); + }); }); From c31578be9d62cb0432593f03e676b4ed3d7586ea Mon Sep 17 00:00:00 2001 From: Andrey Belym Date: Fri, 14 Dec 2018 11:25:03 +0300 Subject: [PATCH 2/2] Fix encoding/decoding when object has null as a proto --- index.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 72f8ec2..b180286 100644 --- a/index.js +++ b/index.js @@ -88,15 +88,15 @@ EncodingTransformer.prototype._handleArray = function (arr) { }; EncodingTransformer.prototype._handlePlainObject = function (obj) { - var result = Object.create(null); + var replicator = this; + var result = Object.create(null); + var ownPropertyNames = Object.getOwnPropertyNames(obj); - for (var key in obj) { - if (obj.hasOwnProperty(key)) { - var resultKey = KEY_REQUIRE_ESCAPING_RE.test(key) ? '#' + key : key; + ownPropertyNames.forEach(function (key) { + var resultKey = KEY_REQUIRE_ESCAPING_RE.test(key) ? '#' + key : key; - result[resultKey] = this._handleValue(obj[key], result, resultKey); - } - } + result[resultKey] = replicator._handleValue(obj[key], result, resultKey); + }); return result; }; @@ -170,20 +170,20 @@ var DecodingTransformer = function (references, transformsMap) { }; DecodingTransformer.prototype._handlePlainObject = function (obj) { - var unescaped = Object.create(null); - - for (var key in obj) { - if (obj.hasOwnProperty(key)) { - this._handleValue(obj[key], obj, key); - - if (KEY_REQUIRE_ESCAPING_RE.test(key)) { - // NOTE: use intermediate object to avoid unescaped and escaped keys interference - // E.g. unescaped "##@t" will be "#@t" which can overwrite escaped "#@t". - unescaped[key.substring(1)] = obj[key]; - delete obj[key]; - } + var replicator = this; + var unescaped = Object.create(null); + var ownPropertyNames = Object.getOwnPropertyNames(obj); + + ownPropertyNames.forEach(function (key) { + replicator._handleValue(obj[key], obj, key); + + if (KEY_REQUIRE_ESCAPING_RE.test(key)) { + // NOTE: use intermediate object to avoid unescaped and escaped keys interference + // E.g. unescaped "##@t" will be "#@t" which can overwrite escaped "#@t". + unescaped[key.substring(1)] = obj[key]; + delete obj[key]; } - } + }); for (var unsecapedKey in unescaped) obj[unsecapedKey] = unescaped[unsecapedKey];