Skip to content

Commit

Permalink
feat: replace new Buffer with modern versions
Browse files Browse the repository at this point in the history
Replace new Buffer with wrappers around Buffer.alloc and
Buffer.from, to avoid deprecation messages.

Fixes NODE-1789
  • Loading branch information
daprahamian committed Mar 8, 2019
1 parent 35beb90 commit 24aefba
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
10 changes: 6 additions & 4 deletions lib/bson/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if (typeof global !== 'undefined') {
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
}

var utils = require('./parser/utils');

/**
* A class representation of the BSON Binary type.
*
Expand Down Expand Up @@ -53,7 +55,7 @@ function Binary(buffer, subType) {
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 = utils.toBuffer(buffer);
} else if (
typeof Uint8Array !== 'undefined' ||
Object.prototype.toString.call(buffer) === '[object Array]'
Expand All @@ -68,7 +70,7 @@ function Binary(buffer, subType) {
this.position = buffer.length;
} else {
if (typeof Buffer !== 'undefined') {
this.buffer = new Buffer(Binary.BUFFER_SIZE);
this.buffer = utils.allocBuffer(Binary.BUFFER_SIZE);
} else if (typeof Uint8Array !== 'undefined') {
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
} else {
Expand Down Expand Up @@ -107,7 +109,7 @@ Binary.prototype.put = function put(byte_value) {
} else {
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
// Create additional overflow buffer
var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length);
var buffer = utils.allocBuffer(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 @@ -150,7 +152,7 @@ Binary.prototype.write = function write(string, offset) {
var 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 = utils.allocBuffer(this.buffer.length + string.length);
this.buffer.copy(buffer, 0, 0, this.buffer.length);
} else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
// Create a new buffer
Expand Down
9 changes: 5 additions & 4 deletions lib/bson/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var Map = require('./map'),
// Parts of the parser
var deserialize = require('./parser/deserializer'),
serializer = require('./parser/serializer'),
calculateObjectSize = require('./parser/calculate_size');
calculateObjectSize = require('./parser/calculate_size'),
utils = require('./parser/utils');

/**
* @ignore
Expand All @@ -28,7 +29,7 @@ var deserialize = require('./parser/deserializer'),
var MAXSIZE = 1024 * 1024 * 17;

// Current Internal Temporary Serialization Buffer
var buffer = new Buffer(MAXSIZE);
var buffer = utils.allocBuffer(MAXSIZE);

var BSON = function() {};

Expand Down Expand Up @@ -56,7 +57,7 @@ BSON.prototype.serialize = function serialize(object, options) {

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

// Attempt to serialize
Expand All @@ -71,7 +72,7 @@ BSON.prototype.serialize = function serialize(object, options) {
[]
);
// Create the final buffer
var finishedBuffer = new Buffer(serializationIndex);
var finishedBuffer = utils.allocBuffer(serializationIndex);
// Copy into the finished buffer
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
// Return the buffer
Expand Down
24 changes: 13 additions & 11 deletions lib/bson/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ var INF_POSITIVE_BUFFER = [

var EXPONENT_REGEX = /^([-+])?(\d+)?$/;

var utils = require('./parser/utils');

// Detect if the value is a digit
var isDigit = function(value) {
return !isNaN(parseInt(value, 10));
Expand Down Expand Up @@ -143,7 +145,7 @@ var lessThan = function(left, right) {
};

// var longtoHex = function(value) {
// var buffer = new Buffer(8);
// var buffer = utils.allocBuffer(8);
// var index = 0;
// // Encode the low 64 bits of the decimal
// // Encode low bits
Expand All @@ -160,7 +162,7 @@ var lessThan = function(left, right) {
// };

// var int32toHex = function(value) {
// var buffer = new Buffer(4);
// var buffer = utils.allocBuffer(4);
// var index = 0;
// // Encode the low 64 bits of the decimal
// // Encode low bits
Expand Down Expand Up @@ -265,17 +267,17 @@ 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(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
} else if (string[index] === 'N') {
return new Decimal128(new Buffer(NAN_BUFFER));
return new Decimal128(utils.toBuffer(NAN_BUFFER));
}
}

// Read all the digits
while (isDigit(string[index]) || string[index] === '.') {
if (string[index] === '.') {
if (sawRadix) {
return new Decimal128(new Buffer(NAN_BUFFER));
return new Decimal128(utils.toBuffer(NAN_BUFFER));
}

sawRadix = true;
Expand Down Expand Up @@ -320,7 +322,7 @@ Decimal128.fromString = function(string) {

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

// Get exponent
Expand All @@ -332,7 +334,7 @@ Decimal128.fromString = function(string) {

// Return not a number
if (string[index]) {
return new Decimal128(new Buffer(NAN_BUFFER));
return new Decimal128(utils.toBuffer(NAN_BUFFER));
}

// Done reading input
Expand Down Expand Up @@ -380,7 +382,7 @@ Decimal128.fromString = function(string) {
exponent = EXPONENT_MAX;
break;
} else {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
}

Expand Down Expand Up @@ -412,7 +414,7 @@ Decimal128.fromString = function(string) {
exponent = EXPONENT_MAX;
break;
} else {
return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
}
}
}
Expand Down Expand Up @@ -462,7 +464,7 @@ Decimal128.fromString = function(string) {
digits[dIdx] = 1;
} else {
return new Decimal128(
new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
);
}
}
Expand Down Expand Up @@ -547,7 +549,7 @@ Decimal128.fromString = function(string) {
}

// Encode into a buffer
var buffer = new Buffer(16);
var buffer = utils.allocBuffer(16);
index = 0;

// Encode the low 64 bits of the decimal
Expand Down
10 changes: 6 additions & 4 deletions lib/bson/objectid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Custom inspect property name / symbol.
var inspect = 'inspect';

var utils = require('./parser/utils');

/**
* Machine id.
*
Expand Down Expand Up @@ -58,7 +60,7 @@ var ObjectID = function ObjectID(id) {
'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(utils.toBuffer(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 @@ -158,7 +160,7 @@ ObjectID.prototype.generate = function(time) {
: process.pid) % 0xffff;
var inc = this.get_inc();
// Buffer used
var buffer = new Buffer(12);
var buffer = utils.allocBuffer(12);
// Encode time
buffer[3] = time & 0xff;
buffer[2] = (time >> 8) & 0xff;
Expand Down Expand Up @@ -277,7 +279,7 @@ ObjectID.createPk = function createPk() {
* @return {ObjectID} return the created ObjectID
*/
ObjectID.createFromTime = function createFromTime(time) {
var buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
var buffer = utils.toBuffer([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 Down Expand Up @@ -315,7 +317,7 @@ ObjectID.createFromHexString = function createFromHexString(string) {
}

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

// Calculate lengths
var array = new _Buffer(12);
Expand Down
8 changes: 5 additions & 3 deletions lib/bson/parser/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var Long = require('../long').Long,
BSONRegExp = require('../regexp').BSONRegExp,
Binary = require('../binary').Binary;

var utils = require('./utils');

var deserialize = function(buffer, options, isArray) {
options = options == null ? {} : options;
var index = options && options.index ? options.index : 0;
Expand Down Expand Up @@ -115,7 +117,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
object[name] = buffer.toString('utf8', index, index + stringSize - 1);
index = index + stringSize;
} else if (elementType === BSON.BSON_DATA_OID) {
var oid = new Buffer(12);
var oid = utils.allocBuffer(12);
buffer.copy(oid, 0, index, index + 12);
object[name] = new ObjectID(oid);
index = index + 12;
Expand Down Expand Up @@ -220,7 +222,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
}
} else if (elementType === BSON.BSON_DATA_DECIMAL128) {
// Buffer to contain the decimal bytes
var bytes = new Buffer(16);
var bytes = utils.allocBuffer(16);
// Copy the next 16 bytes into the bytes buffer
buffer.copy(bytes, 0, index, index + 16);
// Update index
Expand Down Expand Up @@ -520,7 +522,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
index = index + stringSize;

// Read the oid
var oidBuffer = new Buffer(12);
var oidBuffer = utils.allocBuffer(12);
buffer.copy(oidBuffer, 0, index, index + 12);
oid = new ObjectID(oidBuffer);

Expand Down
16 changes: 15 additions & 1 deletion lib/bson/parser/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ function normalizedFunctionString(fn) {
return fn.toString().replace(/function *\(/, 'function (');
}

function newBuffer(item, encoding) {
return new Buffer(item, encoding);
}

function allocBuffer() {
return Buffer.alloc.apply(Buffer, arguments);
}

function toBuffer() {
return Buffer.from.apply(Buffer, arguments);
}

module.exports = {
normalizedFunctionString: normalizedFunctionString
normalizedFunctionString: normalizedFunctionString,
allocBuffer: typeof Buffer.alloc === 'function' ? allocBuffer : newBuffer,
toBuffer: typeof Buffer.from === 'function' ? toBuffer : newBuffer
};

0 comments on commit 24aefba

Please sign in to comment.