From 683b06f6387c64db498fe8f9047503c871acc581 Mon Sep 17 00:00:00 2001 From: GDB Date: Wed, 9 Feb 2022 16:22:55 +0100 Subject: [PATCH] allows binary packet deconstruction/reconstraction containing object with a null prototype --- lib/binary.ts | 4 ++-- test/arraybuffer.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/binary.ts b/lib/binary.ts index 18700e6..65d9789 100644 --- a/lib/binary.ts +++ b/lib/binary.ts @@ -33,7 +33,7 @@ function _deconstructPacket(data, buffers) { } else if (typeof data === "object" && !(data instanceof Date)) { const newData = {}; for (const key in data) { - if (data.hasOwnProperty(key)) { + if (Object.prototype.hasOwnProperty.call(data, key)) { newData[key] = _deconstructPacket(data[key], buffers); } } @@ -68,7 +68,7 @@ function _reconstructPacket(data, buffers) { } } else if (typeof data === "object") { for (const key in data) { - if (data.hasOwnProperty(key)) { + if (Object.prototype.hasOwnProperty.call(data, key)) { data[key] = _reconstructPacket(data[key], buffers); } } diff --git a/test/arraybuffer.js b/test/arraybuffer.js index 6d44057..f81a2d0 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -14,6 +14,21 @@ describe("parser", () => { helpers.test_bin(packet, done); }); + it("encodes an ArrayBuffer into an object with a null prototype", (done) => { + const packet = { + type: PacketType.EVENT, + data: [ + "a", + Object.create(null, { + array: { value: new ArrayBuffer(2), enumerable: true }, + }), + ], + id: 0, + nsp: "/", + }; + helpers.test_bin(packet, done); + }); + it("encodes a TypedArray", (done) => { const array = new Uint8Array(5); for (let i = 0; i < array.length; i++) array[i] = i;