diff --git a/lib/buffer.js b/lib/buffer.js index e41e3662662609..12f40693400c02 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -30,9 +30,13 @@ binding.setupBufferJS(Buffer.prototype, bindingObj); const zeroFill = bindingObj.zeroFill || [0]; function createUnsafeBuffer(size) { + return new FastBuffer(createUnsafeArrayBuffer(size)); +} + +function createUnsafeArrayBuffer(size) { zeroFill[0] = 0; try { - return new FastBuffer(size); + return new ArrayBuffer(size); } finally { zeroFill[0] = 1; } @@ -40,7 +44,7 @@ function createUnsafeBuffer(size) { function createPool() { poolSize = Buffer.poolSize; - allocPool = createUnsafeBuffer(poolSize); + allocPool = createUnsafeArrayBuffer(poolSize); poolOffset = 0; } createPool(); @@ -183,7 +187,7 @@ function allocate(size) { if (size < (Buffer.poolSize >>> 1)) { if (size > (poolSize - poolOffset)) createPool(); - var b = allocPool.slice(poolOffset, poolOffset + size); + var b = new FastBuffer(allocPool, poolOffset, size); poolOffset += size; alignPool(); return b; @@ -210,8 +214,12 @@ function fromString(string, encoding) { if (length > (poolSize - poolOffset)) createPool(); - var actual = allocPool.write(string, poolOffset, encoding); - var b = allocPool.slice(poolOffset, poolOffset + actual); + var b = new FastBuffer(allocPool, poolOffset, length); + var actual = b.write(string, encoding); + if (actual !== length) { + // byteLength() may overestimate. That’s a rare case, though. + b = new FastBuffer(allocPool, poolOffset, actual); + } poolOffset += actual; alignPool(); return b;