Skip to content

Remove all usage of new Buffer(); use Buffer.from()/Buffer.alloc() #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bytebuffer.js
Original file line number Diff line number Diff line change
@@ -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 {

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

/**
2 changes: 1 addition & 1 deletion src/methods/clone.js
Original file line number Diff line number Diff line change
@@ -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 {
2 changes: 1 addition & 1 deletion src/methods/compact.js
Original file line number Diff line number Diff line change
@@ -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) {
2 changes: 1 addition & 1 deletion src/methods/prepend.js
Original file line number Diff line number Diff line change
@@ -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) {
2 changes: 1 addition & 1 deletion src/methods/resize.js
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions src/methods/static/wrap.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/methods/toBuffer.js
Original file line number Diff line number Diff line change
@@ -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 {
4 changes: 2 additions & 2 deletions tests/suite.js
Original file line number Diff line number Diff line change
@@ -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'
];