Skip to content

Commit

Permalink
Remove all usage of new Buffer(); use Buffer.from()/Buffer.alloc()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Jun 18, 2020
1 parent 4144951 commit 658f2f9
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/bytebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/encodings/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/encodings/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/encodings/hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/encodings/utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @type {!Buffer}
* @inner
*/
var EMPTY_BUFFER = new Buffer(0);
var EMPTY_BUFFER = Buffer.alloc(0);
//? } else {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/methods/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/prepend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/methods/static/wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.alloc(buffer);
}
bb = new ByteBuffer(0, littleEndian, noAssert);
if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
Expand Down
2 changes: 1 addition & 1 deletion src/methods/toBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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'
];
Expand Down

0 comments on commit 658f2f9

Please sign in to comment.