diff --git a/lib/buffer.js b/lib/buffer.js index e41e3662662609..fe7466d82976e0 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -402,10 +402,6 @@ Object.defineProperty(Buffer.prototype, 'parent', { get: function() { if (!(this instanceof Buffer)) return undefined; - if (this.byteLength === 0 || - this.byteLength === this.buffer.byteLength) { - return undefined; - } return this.buffer; } }); diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index 6d1ed552f24ad5..c80ee10382a968 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -994,7 +994,7 @@ if (common.hasCrypto) { const ps = Buffer.poolSize; Buffer.poolSize = 0; -assert.strictEqual(Buffer.allocUnsafe(1).parent, undefined); +assert(Buffer.allocUnsafe(1).parent instanceof ArrayBuffer); Buffer.poolSize = ps; // Test Buffer.copy() segfault diff --git a/test/parallel/test-buffer-arraybuffer.js b/test/parallel/test-buffer-arraybuffer.js index 864081e14ddeeb..729df40fe1b0ef 100644 --- a/test/parallel/test-buffer-arraybuffer.js +++ b/test/parallel/test-buffer-arraybuffer.js @@ -13,9 +13,7 @@ const 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); diff --git a/test/parallel/test-buffer-parent-property.js b/test/parallel/test-buffer-parent-property.js new file mode 100644 index 00000000000000..5dae996b87feff --- /dev/null +++ b/test/parallel/test-buffer-parent-property.js @@ -0,0 +1,23 @@ +'use strict'; + +/* + * Fix for https://github.com/nodejs/node/issues/8266 + * + * Zero length Buffer objects should expose the `buffer` property of the + * TypedArrays, via the `parent` property. + */ +require('../common'); +const assert = require('assert'); + +// If the length of the buffer object is zero +assert((new Buffer(0)).parent instanceof ArrayBuffer); + +// If the length of the buffer object is equal to the underlying ArrayBuffer +assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer); + +// Same as the previous test, but with user created buffer +const arrayBuffer = new ArrayBuffer(0); +assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer); +assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer); +assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer); +assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer);