From ff980eefc36d62c2a798b1e8a3262d418996aad8 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman <17342435+RyanZim@users.noreply.github.com> Date: Fri, 19 Jun 2020 11:02:06 -0400 Subject: [PATCH] Remove all usage of new Buffer(); use Buffer.from()/Buffer.alloc() (#1) Fixes https://github.com/protobufjs/bytebuffer.js/issues/99 --- src/bytebuffer.js | 2 +- src/encodings/base64.js | 2 +- src/encodings/binary.js | 2 +- src/encodings/hex.js | 2 +- src/encodings/utf8.js | 2 +- src/helpers.js | 2 +- src/methods/clone.js | 2 +- src/methods/compact.js | 2 +- src/methods/prepend.js | 2 +- src/methods/resize.js | 2 +- src/methods/static/wrap.js | 6 +++--- src/methods/toBuffer.js | 2 +- tests/suite.js | 4 ++-- 13 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/bytebuffer.js b/src/bytebuffer.js index 73f75cc..1081923 100644 --- a/src/bytebuffer.js +++ b/src/bytebuffer.js @@ -32,7 +32,7 @@ var ByteBuffer = function(capacity, littleEndian, noAssert) { * @type {!Buffer} * @expose */ - this.buffer = capacity === 0 ? EMPTY_BUFFER : new Buffer(capacity); + this.buffer = capacity === 0 ? EMPTY_BUFFER : Buffer.alloc(capacity); //? } else { /** diff --git a/src/encodings/base64.js b/src/encodings/base64.js index 583ffe9..3e30995 100644 --- a/src/encodings/base64.js +++ b/src/encodings/base64.js @@ -46,7 +46,7 @@ ByteBufferPrototype.toBase64 = function(begin, end) { */ ByteBuffer.fromBase64 = function(str, littleEndian) { //? if (NODE) { - return ByteBuffer.wrap(new Buffer(str, "base64"), littleEndian); + return ByteBuffer.wrap(Buffer.from(str, "base64"), littleEndian); //? } else { if (typeof str !== 'string') throw TypeError("str"); diff --git a/src/encodings/binary.js b/src/encodings/binary.js index ef9c531..5cb32b7 100644 --- a/src/encodings/binary.js +++ b/src/encodings/binary.js @@ -49,7 +49,7 @@ ByteBufferPrototype.toBinary = function(begin, end) { */ ByteBuffer.fromBinary = function(str, littleEndian) { //? if (NODE) { - return ByteBuffer.wrap(new Buffer(str, "binary"), littleEndian); + return ByteBuffer.wrap(Buffer.from(str, "binary"), littleEndian); //? } else { if (typeof str !== 'string') throw TypeError("str"); diff --git a/src/encodings/hex.js b/src/encodings/hex.js index 289ecb1..d759544 100644 --- a/src/encodings/hex.js +++ b/src/encodings/hex.js @@ -51,7 +51,7 @@ ByteBuffer.fromHex = function(str, littleEndian, noAssert) { } //? if (NODE) { var bb = new ByteBuffer(0, littleEndian, true); - bb.buffer = new Buffer(str, "hex"); + bb.buffer = Buffer.from(str, "hex"); bb.limit = bb.buffer.length; //? } else { var k = str.length, diff --git a/src/encodings/utf8.js b/src/encodings/utf8.js index c1f7b54..5d8c312 100644 --- a/src/encodings/utf8.js +++ b/src/encodings/utf8.js @@ -52,7 +52,7 @@ ByteBuffer.fromUTF8 = function(str, littleEndian, noAssert) { throw TypeError("Illegal str: Not a string"); //? if (NODE) { var bb = new ByteBuffer(0, littleEndian, noAssert); - bb.buffer = new Buffer(str, "utf8"); + bb.buffer = Buffer.from(str, "utf8"); bb.limit = bb.buffer.length; //? } else { var bb = new ByteBuffer(utfx.calculateUTF16asUTF8(stringSource(str), true)[1], littleEndian, noAssert), diff --git a/src/helpers.js b/src/helpers.js index 3e27c45..333d78b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -5,7 +5,7 @@ * @type {!Buffer} * @inner */ -var EMPTY_BUFFER = new Buffer(0); +var EMPTY_BUFFER = Buffer.alloc(0); //? } else { /** diff --git a/src/methods/clone.js b/src/methods/clone.js index 67bded2..361d45f 100644 --- a/src/methods/clone.js +++ b/src/methods/clone.js @@ -9,7 +9,7 @@ ByteBufferPrototype.clone = function(copy) { var bb = new ByteBuffer(0, this.littleEndian, this.noAssert); if (copy) { //? if (NODE) { - var buffer = new Buffer(this.buffer.length); + var buffer = Buffer.alloc(this.buffer.length); this.buffer.copy(buffer); bb.buffer = buffer; //? } else { diff --git a/src/methods/compact.js b/src/methods/compact.js index 8888378..0d89023 100644 --- a/src/methods/compact.js +++ b/src/methods/compact.js @@ -26,7 +26,7 @@ ByteBufferPrototype.compact = function(begin, end) { return this; } //? if (NODE) { - var buffer = new Buffer(len); + var buffer = Buffer.alloc(len); this.buffer.copy(buffer, 0, begin, end); this.buffer = buffer; //? } else if (DATAVIEW) { diff --git a/src/methods/prepend.js b/src/methods/prepend.js index 879c1e8..1118fec 100644 --- a/src/methods/prepend.js +++ b/src/methods/prepend.js @@ -33,7 +33,7 @@ ByteBufferPrototype.prepend = function(source, encoding, offset) { var diff = len - offset; if (diff > 0) { // Not enough space before offset, so resize + move //? if (NODE) { - var buffer = new Buffer(this.buffer.length + diff); + var buffer = Buffer.alloc(this.buffer.length + diff); this.buffer.copy(buffer, len, offset, this.buffer.length); this.buffer = buffer; //? } else if (DATAVIEW) { diff --git a/src/methods/resize.js b/src/methods/resize.js index b015795..099a8db 100644 --- a/src/methods/resize.js +++ b/src/methods/resize.js @@ -15,7 +15,7 @@ ByteBufferPrototype.resize = function(capacity) { } //? if (NODE) { if (this.buffer.length < capacity) { - var buffer = new Buffer(capacity); + var buffer = Buffer.alloc(capacity); this.buffer.copy(buffer); this.buffer = buffer; } diff --git a/src/methods/static/wrap.js b/src/methods/static/wrap.js index ba759ab..9e4911d 100644 --- a/src/methods/static/wrap.js +++ b/src/methods/static/wrap.js @@ -58,7 +58,7 @@ ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) { k = 0, b; if (buffer instanceof Uint8Array) { // Extract bytes from Uint8Array - b = new Buffer(buffer.length); + b = Buffer.alloc(buffer.length); if (memcpy) { // Fast memcpy(b, 0, buffer.buffer, buffer.byteOffset, buffer.byteOffset + buffer.length); } else { // Slow @@ -67,7 +67,7 @@ ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) { } buffer = b; } else if (buffer instanceof ArrayBuffer) { // Convert ArrayBuffer to Buffer - b = new Buffer(buffer.byteLength); + b = Buffer.alloc(buffer.byteLength); if (memcpy) { // Fast memcpy(b, 0, buffer, 0, buffer.byteLength); } else { // Slow @@ -80,7 +80,7 @@ ByteBuffer.wrap = function(buffer, encoding, littleEndian, noAssert) { } else if (!(buffer instanceof Buffer)) { // Create from octets if it is an error, otherwise fail if (Object.prototype.toString.call(buffer) !== "[object Array]") throw TypeError("Illegal buffer"); - buffer = new Buffer(buffer); + buffer = Buffer.from(buffer); } bb = new ByteBuffer(0, littleEndian, noAssert); if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER diff --git a/src/methods/toBuffer.js b/src/methods/toBuffer.js index 9c6af08..a665565 100644 --- a/src/methods/toBuffer.js +++ b/src/methods/toBuffer.js @@ -18,7 +18,7 @@ ByteBufferPrototype.toBuffer = function(forceCopy) { } //? if (NODE) { if (forceCopy) { - var buffer = new Buffer(limit - offset); + var buffer = Buffer.alloc(limit - offset); this.buffer.copy(buffer, 0, offset, limit); return buffer; } else { diff --git a/tests/suite.js b/tests/suite.js index daa3c5e..2bf1099 100644 --- a/tests/suite.js +++ b/tests/suite.js @@ -126,7 +126,7 @@ function makeSuite(ByteBuffer) { if (type === Buffer) { suite.wrap.Buffer = function(test) { - var buf = new Buffer(1); + var buf = Buffer.alloc(1); buf[0] = 0x01; var bb = ByteBuffer.wrap(buf); test.strictEqual(bb.capacity(), 1); @@ -270,7 +270,7 @@ function makeSuite(ByteBuffer) { ByteBuffer.fromDebug('00 01 02<03>00'), ByteBuffer.fromDebug('00|'), ByteBuffer.fromDebug('<04>'), - type === Buffer ? new Buffer(0) : new ArrayBuffer(0), + type === Buffer ? Buffer.alloc(0) : new ArrayBuffer(0), new Uint8Array(0), '05' ];