From f4aa026742d873eafc9bc67709f7b3dcca2b4e0a Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 18:57:07 -0500 Subject: [PATCH] mimic SlowBuffer and Buffer#parent just enough to pass node's tests --- index.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0cc3546..2637df0 100644 --- a/index.js +++ b/index.js @@ -10,11 +10,12 @@ var ieee754 = require('ieee754') var isArray = require('is-array') exports.Buffer = Buffer -exports.SlowBuffer = Buffer +exports.SlowBuffer = SlowBuffer exports.INSPECT_MAX_BYTES = 50 Buffer.poolSize = 8192 // not used by this implementation var kMaxLength = 0x3fffffff +var rootParent = {} /** * If `Buffer.TYPED_ARRAY_SUPPORT`: @@ -120,6 +121,18 @@ function Buffer (subject, encoding, noZero) { } } + if (length > 0 && length <= Buffer.poolSize) + buf.parent = rootParent + + return buf +} + +function SlowBuffer(subject, encoding, noZero) { + if (!(this instanceof SlowBuffer)) + return new SlowBuffer(subject, encoding, noZero) + + var buf = new Buffer(subject, encoding, noZero) + delete buf.parent return buf } @@ -502,16 +515,21 @@ Buffer.prototype.slice = function (start, end) { if (end < start) end = start + var newBuf if (Buffer.TYPED_ARRAY_SUPPORT) { - return Buffer._augment(this.subarray(start, end)) + newBuf = Buffer._augment(this.subarray(start, end)) } else { var sliceLen = end - start - var newBuf = new Buffer(sliceLen, undefined, true) + newBuf = new Buffer(sliceLen, undefined, true) for (var i = 0; i < sliceLen; i++) { newBuf[i] = this[i + start] } - return newBuf } + + if (newBuf.length) + newBuf.parent = this.parent || this + + return newBuf } /* -- 2.34.1