Skip to content

Commit

Permalink
fix(buffer): replace deprecated Buffer constructor
Browse files Browse the repository at this point in the history
Replace Buffer constructor with buffer.alloc and buffer.from

Fixes Node-1461
  • Loading branch information
Sophie Saskin authored Aug 8, 2018
1 parent 6e3f028 commit 5acdebf
Show file tree
Hide file tree
Showing 17 changed files with 1,693 additions and 1,693 deletions.
8 changes: 4 additions & 4 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Binary {
if (typeof buffer === 'string') {
// Different ways of writing the length of the string for the different types
if (typeof Buffer !== 'undefined') {
this.buffer = new Buffer(buffer);
this.buffer = Buffer.from(buffer);
} else if (typeof Uint8Array !== 'undefined' || Array.isArray(buffer)) {
this.buffer = writeStringToArray(buffer);
} else {
Expand All @@ -50,7 +50,7 @@ class Binary {
this.position = buffer.length;
} else {
if (typeof Buffer !== 'undefined') {
this.buffer = new Buffer(Binary.BUFFER_SIZE);
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
} else if (typeof Uint8Array !== 'undefined') {
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
} else {
Expand Down Expand Up @@ -87,7 +87,7 @@ class Binary {
} else {
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
// Create additional overflow buffer
let buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
let buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
// Combine the two buffers together
this.buffer.copy(buffer, 0, 0, this.buffer.length);
this.buffer = buffer;
Expand Down Expand Up @@ -130,7 +130,7 @@ class Binary {
let buffer = null;
// If we are in node.js
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
buffer = new Buffer(this.buffer.length + string.length);
buffer = Buffer.alloc(this.buffer.length + string.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
} else if (isUint8Array(this.buffer)) {
// Create a new buffer
Expand Down
8 changes: 4 additions & 4 deletions lib/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ensureBuffer = require('./ensure_buffer');
const MAXSIZE = 1024 * 1024 * 17;

// Current Internal Temporary Serialization Buffer
let buffer = new Buffer(MAXSIZE);
let buffer = Buffer.alloc(MAXSIZE);

/**
* Sets the size of the internal serialization buffer.
Expand All @@ -41,7 +41,7 @@ let buffer = new Buffer(MAXSIZE);
function setInternalBufferSize(size) {
// Resize the internal serialization buffer if needed
if (buffer.length < size) {
buffer = new Buffer(size);
buffer = Buffer.alloc(size);
}
}

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

// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = new Buffer(minInternalBufferSize);
buffer = Buffer.alloc(minInternalBufferSize);
}

// Attempt to serialize
Expand All @@ -84,7 +84,7 @@ function serialize(object, options) {
);

// Create the final buffer
const finishedBuffer = new Buffer(serializationIndex);
const finishedBuffer = Buffer.alloc(serializationIndex);

// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
Expand Down
12 changes: 6 additions & 6 deletions lib/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ Decimal128.fromString = function(string) {
// Check if user passed Infinity or NaN
if (!isDigit(string[index]) && string[index] !== '.') {
if (string[index] === 'i' || string[index] === 'I') {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
return new Decimal128(Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
} else if (string[index] === 'N') {
return new Decimal128(new Buffer(NAN_BUFFER));
return new Decimal128(Buffer.from(NAN_BUFFER));
}
}

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

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

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

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

// Done reading input
// Find first non-zero digit in digits
Expand Down Expand Up @@ -437,7 +437,7 @@ Decimal128.fromString = function(string) {
digits[dIdx] = 1;
} else {
return new Decimal128(
new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
);
}
}
Expand Down Expand Up @@ -519,7 +519,7 @@ Decimal128.fromString = function(string) {
}

// Encode into a buffer
const buffer = new Buffer(16);
const buffer = Buffer.alloc(16);
index = 0;

// Encode the low 64 bits of the decimal
Expand Down
2 changes: 1 addition & 1 deletion lib/ensure_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function ensureBuffer(potentialBuffer) {
}

if (potentialBuffer instanceof Uint8Array) {
return new Buffer(potentialBuffer.buffer);
return Buffer.from(potentialBuffer.buffer);
}

throw new TypeError('Must use either Buffer or Uint8Array');
Expand Down
8 changes: 4 additions & 4 deletions lib/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ObjectId {
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
);
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
return new ObjectId(new Buffer(id, 'hex'));
return new ObjectId(Buffer.from(id, 'hex'));
} else if (valid && typeof id === 'string' && id.length === 24) {
return ObjectId.createFromHexString(id);
} else if (id != null && id.length === 12) {
Expand Down Expand Up @@ -167,7 +167,7 @@ class ObjectId {
: process.pid) % 0xffff;
const inc = this.get_inc();
// Buffer used
const buffer = new Buffer(12);
const buffer = Buffer.alloc(12);
// Encode time
buffer[3] = time & 0xff;
buffer[2] = (time >> 8) & 0xff;
Expand Down Expand Up @@ -278,7 +278,7 @@ class ObjectId {
* @return {ObjectId} return the created ObjectId
*/
static createFromTime(time) {
const buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Encode time into first 4 bytes
buffer[3] = time & 0xff;
buffer[2] = (time >> 8) & 0xff;
Expand All @@ -304,7 +304,7 @@ class ObjectId {
}

// Use Buffer.from method if available
if (hasBufferType) return new ObjectId(new Buffer(string, 'hex'));
if (hasBufferType) return new ObjectId(Buffer.from(string, 'hex'));

// Calculate lengths
const array = new _Buffer(12);
Expand Down
6 changes: 3 additions & 3 deletions lib/parser/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function deserializeObject(buffer, index, options, isArray) {
object[name] = s;
index = index + stringSize;
} else if (elementType === constants.BSON_DATA_OID) {
const oid = new Buffer(12);
const oid = Buffer.alloc(12);
buffer.copy(oid, 0, index, index + 12);
object[name] = new ObjectId(oid);
index = index + 12;
Expand Down Expand Up @@ -249,7 +249,7 @@ function deserializeObject(buffer, index, options, isArray) {
}
} else if (elementType === constants.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
const bytes = new Buffer(16);
const bytes = Buffer.alloc(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
Expand Down Expand Up @@ -552,7 +552,7 @@ function deserializeObject(buffer, index, options, isArray) {
index = index + stringSize;

// Read the oid
const oidBuffer = new Buffer(12);
const oidBuffer = Buffer.alloc(12);
buffer.copy(oidBuffer, 0, index, index + 12);
const oid = new ObjectId(oidBuffer);

Expand Down
Loading

0 comments on commit 5acdebf

Please sign in to comment.