Skip to content

Commit 5acdebf

Browse files
author
Sophie Saskin
authored
fix(buffer): replace deprecated Buffer constructor
Replace Buffer constructor with buffer.alloc and buffer.from Fixes Node-1461
1 parent 6e3f028 commit 5acdebf

17 files changed

+1693
-1693
lines changed

lib/binary.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Binary {
3838
if (typeof buffer === 'string') {
3939
// Different ways of writing the length of the string for the different types
4040
if (typeof Buffer !== 'undefined') {
41-
this.buffer = new Buffer(buffer);
41+
this.buffer = Buffer.from(buffer);
4242
} else if (typeof Uint8Array !== 'undefined' || Array.isArray(buffer)) {
4343
this.buffer = writeStringToArray(buffer);
4444
} else {
@@ -50,7 +50,7 @@ class Binary {
5050
this.position = buffer.length;
5151
} else {
5252
if (typeof Buffer !== 'undefined') {
53-
this.buffer = new Buffer(Binary.BUFFER_SIZE);
53+
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
5454
} else if (typeof Uint8Array !== 'undefined') {
5555
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
5656
} else {
@@ -87,7 +87,7 @@ class Binary {
8787
} else {
8888
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
8989
// Create additional overflow buffer
90-
let buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
90+
let buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
9191
// Combine the two buffers together
9292
this.buffer.copy(buffer, 0, 0, this.buffer.length);
9393
this.buffer = buffer;
@@ -130,7 +130,7 @@ class Binary {
130130
let buffer = null;
131131
// If we are in node.js
132132
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
133-
buffer = new Buffer(this.buffer.length + string.length);
133+
buffer = Buffer.alloc(this.buffer.length + string.length);
134134
this.buffer.copy(buffer, 0, 0, this.buffer.length);
135135
} else if (isUint8Array(this.buffer)) {
136136
// Create a new buffer

lib/bson.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const ensureBuffer = require('./ensure_buffer');
3131
const MAXSIZE = 1024 * 1024 * 17;
3232

3333
// Current Internal Temporary Serialization Buffer
34-
let buffer = new Buffer(MAXSIZE);
34+
let buffer = Buffer.alloc(MAXSIZE);
3535

3636
/**
3737
* Sets the size of the internal serialization buffer.
@@ -41,7 +41,7 @@ let buffer = new Buffer(MAXSIZE);
4141
function setInternalBufferSize(size) {
4242
// Resize the internal serialization buffer if needed
4343
if (buffer.length < size) {
44-
buffer = new Buffer(size);
44+
buffer = Buffer.alloc(size);
4545
}
4646
}
4747

@@ -68,7 +68,7 @@ function serialize(object, options) {
6868

6969
// Resize the internal serialization buffer if needed
7070
if (buffer.length < minInternalBufferSize) {
71-
buffer = new Buffer(minInternalBufferSize);
71+
buffer = Buffer.alloc(minInternalBufferSize);
7272
}
7373

7474
// Attempt to serialize
@@ -84,7 +84,7 @@ function serialize(object, options) {
8484
);
8585

8686
// Create the final buffer
87-
const finishedBuffer = new Buffer(serializationIndex);
87+
const finishedBuffer = Buffer.alloc(serializationIndex);
8888

8989
// Copy into the finished buffer
9090
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);

lib/decimal128.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ Decimal128.fromString = function(string) {
253253
// Check if user passed Infinity or NaN
254254
if (!isDigit(string[index]) && string[index] !== '.') {
255255
if (string[index] === 'i' || string[index] === 'I') {
256-
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
256+
return new Decimal128(Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
257257
} else if (string[index] === 'N') {
258-
return new Decimal128(new Buffer(NAN_BUFFER));
258+
return new Decimal128(Buffer.from(NAN_BUFFER));
259259
}
260260
}
261261

@@ -298,7 +298,7 @@ Decimal128.fromString = function(string) {
298298
const match = string.substr(++index).match(EXPONENT_REGEX);
299299

300300
// No digits read
301-
if (!match || !match[2]) return new Decimal128(new Buffer(NAN_BUFFER));
301+
if (!match || !match[2]) return new Decimal128(Buffer.from(NAN_BUFFER));
302302

303303
// Get exponent
304304
exponent = parseInt(match[0], 10);
@@ -308,7 +308,7 @@ Decimal128.fromString = function(string) {
308308
}
309309

310310
// Return not a number
311-
if (string[index]) return new Decimal128(new Buffer(NAN_BUFFER));
311+
if (string[index]) return new Decimal128(Buffer.from(NAN_BUFFER));
312312

313313
// Done reading input
314314
// Find first non-zero digit in digits
@@ -437,7 +437,7 @@ Decimal128.fromString = function(string) {
437437
digits[dIdx] = 1;
438438
} else {
439439
return new Decimal128(
440-
new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
440+
Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
441441
);
442442
}
443443
}
@@ -519,7 +519,7 @@ Decimal128.fromString = function(string) {
519519
}
520520

521521
// Encode into a buffer
522-
const buffer = new Buffer(16);
522+
const buffer = Buffer.alloc(16);
523523
index = 0;
524524

525525
// Encode the low 64 bits of the decimal

lib/ensure_buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function ensureBuffer(potentialBuffer) {
1616
}
1717

1818
if (potentialBuffer instanceof Uint8Array) {
19-
return new Buffer(potentialBuffer.buffer);
19+
return Buffer.from(potentialBuffer.buffer);
2020
}
2121

2222
throw new TypeError('Must use either Buffer or Uint8Array');

lib/objectid.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ObjectId {
7676
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
7777
);
7878
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
79-
return new ObjectId(new Buffer(id, 'hex'));
79+
return new ObjectId(Buffer.from(id, 'hex'));
8080
} else if (valid && typeof id === 'string' && id.length === 24) {
8181
return ObjectId.createFromHexString(id);
8282
} else if (id != null && id.length === 12) {
@@ -167,7 +167,7 @@ class ObjectId {
167167
: process.pid) % 0xffff;
168168
const inc = this.get_inc();
169169
// Buffer used
170-
const buffer = new Buffer(12);
170+
const buffer = Buffer.alloc(12);
171171
// Encode time
172172
buffer[3] = time & 0xff;
173173
buffer[2] = (time >> 8) & 0xff;
@@ -278,7 +278,7 @@ class ObjectId {
278278
* @return {ObjectId} return the created ObjectId
279279
*/
280280
static createFromTime(time) {
281-
const buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
281+
const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
282282
// Encode time into first 4 bytes
283283
buffer[3] = time & 0xff;
284284
buffer[2] = (time >> 8) & 0xff;
@@ -304,7 +304,7 @@ class ObjectId {
304304
}
305305

306306
// Use Buffer.from method if available
307-
if (hasBufferType) return new ObjectId(new Buffer(string, 'hex'));
307+
if (hasBufferType) return new ObjectId(Buffer.from(string, 'hex'));
308308

309309
// Calculate lengths
310310
const array = new _Buffer(12);

lib/parser/deserializer.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function deserializeObject(buffer, index, options, isArray) {
144144
object[name] = s;
145145
index = index + stringSize;
146146
} else if (elementType === constants.BSON_DATA_OID) {
147-
const oid = new Buffer(12);
147+
const oid = Buffer.alloc(12);
148148
buffer.copy(oid, 0, index, index + 12);
149149
object[name] = new ObjectId(oid);
150150
index = index + 12;
@@ -249,7 +249,7 @@ function deserializeObject(buffer, index, options, isArray) {
249249
}
250250
} else if (elementType === constants.BSON_DATA_DECIMAL128) {
251251
// Buffer to contain the decimal bytes
252-
const bytes = new Buffer(16);
252+
const bytes = Buffer.alloc(16);
253253
// Copy the next 16 bytes into the bytes buffer
254254
buffer.copy(bytes, 0, index, index + 16);
255255
// Update index
@@ -552,7 +552,7 @@ function deserializeObject(buffer, index, options, isArray) {
552552
index = index + stringSize;
553553

554554
// Read the oid
555-
const oidBuffer = new Buffer(12);
555+
const oidBuffer = Buffer.alloc(12);
556556
buffer.copy(oidBuffer, 0, index, index + 12);
557557
const oid = new ObjectId(oidBuffer);
558558

0 commit comments

Comments
 (0)