From: Jesse Tane Date: Mon, 15 Dec 2014 23:57:07 +0000 (-0500) Subject: mimic SlowBuffer and Buffer#parent just enough to pass node's tests X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=f4aa026742d873eafc9bc67709f7b3dcca2b4e0a;p=buffer.git mimic SlowBuffer and Buffer#parent just enough to pass node's tests --- 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 } /*