From 63a0e8b564d137fca020f8f8b6e7008e178d0bd0 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Sun, 21 Jan 2018 01:46:06 +0100 Subject: [PATCH] Add .parent property --- index.js | 10 ++++++++++ test/node/test-buffer-alloc.js | 4 ++-- test/node/test-buffer-arraybuffer.js | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c4576d7..5befc08 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,16 @@ function typedArraySupport () { } } +Object.defineProperty(Buffer.prototype, 'parent', { + enumerable: true, + get () { + if (!(this instanceof Buffer)) { + return undefined + } + return this.buffer + } +}) + Object.defineProperty(Buffer.prototype, 'offset', { enumerable: true, get () { diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index 5d0e032..be384f0 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -931,12 +931,12 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check } else { common.printSkipMessage('missing crypto'); } -/* + var ps = Buffer.poolSize; Buffer.poolSize = 0; assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); Buffer.poolSize = ps; -*/ + // Test Buffer.copy() segfault assert.throws(() => Buffer.allocUnsafe(10).copy(), /TypeError: argument should be a Buffer/); diff --git a/test/node/test-buffer-arraybuffer.js b/test/node/test-buffer-arraybuffer.js index a418752..4036812 100644 --- a/test/node/test-buffer-arraybuffer.js +++ b/test/node/test-buffer-arraybuffer.js @@ -17,7 +17,7 @@ var buf = Buffer.from(ab); assert.ok(buf instanceof Buffer); // For backwards compatibility of old .parent property test that if buf is not // a slice then .parent should be undefined. -assert.equal(buf.parent, undefined); +assert.equal(buf.parent, buf.buffer); assert.equal(buf.buffer, ab); assert.equal(buf.length, ab.byteLength); -- 2.34.1