diff --git a/lib/bson/binary.js b/lib/bson/binary.js index f3f695da..6d190bca 100644 --- a/lib/bson/binary.js +++ b/lib/bson/binary.js @@ -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. * @@ -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]' @@ -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 { @@ -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; @@ -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 diff --git a/lib/bson/bson.js b/lib/bson/bson.js index 956f1522..912c5b92 100644 --- a/lib/bson/bson.js +++ b/lib/bson/bson.js @@ -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 @@ -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() {}; @@ -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 @@ -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 diff --git a/lib/bson/decimal128.js b/lib/bson/decimal128.js index 1dc2f003..924513f4 100644 --- a/lib/bson/decimal128.js +++ b/lib/bson/decimal128.js @@ -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)); @@ -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 @@ -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 @@ -265,9 +267,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(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)); } } @@ -275,7 +277,7 @@ Decimal128.fromString = function(string) { 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; @@ -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 @@ -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 @@ -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)); } } @@ -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)); } } } @@ -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) ); } } @@ -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 diff --git a/lib/bson/objectid.js b/lib/bson/objectid.js index f76ecbb8..79de40d2 100644 --- a/lib/bson/objectid.js +++ b/lib/bson/objectid.js @@ -1,6 +1,8 @@ // Custom inspect property name / symbol. var inspect = 'inspect'; +var utils = require('./parser/utils'); + /** * Machine id. * @@ -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) { @@ -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; @@ -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; @@ -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); diff --git a/lib/bson/parser/deserializer.js b/lib/bson/parser/deserializer.js index b7f45d76..be3c8654 100644 --- a/lib/bson/parser/deserializer.js +++ b/lib/bson/parser/deserializer.js @@ -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; @@ -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; @@ -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 @@ -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); diff --git a/lib/bson/parser/utils.js b/lib/bson/parser/utils.js index 6b8395f2..6faa4396 100644 --- a/lib/bson/parser/utils.js +++ b/lib/bson/parser/utils.js @@ -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 };