| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,8 @@ if (typeof global !== 'undefined') { | |||
| 9 | 9 | var Buffer = require('buffer').Buffer; // TODO just use global Buffer | |
| 10 | 10 | } | |
| 11 | 11 | ||
| 12 | + var utils = require('./parser/utils'); | ||
| 13 | + | ||
| 12 | 14 | /** | |
| 13 | 15 | * A class representation of the BSON Binary type. | |
| 14 | 16 | * | |
@@ -53,7 +55,7 @@ function Binary(buffer, subType) { | |||
| 53 | 55 | if (typeof buffer === 'string') { | |
| 54 | 56 | // Different ways of writing the length of the string for the different types | |
| 55 | 57 | if (typeof Buffer !== 'undefined') { | |
| 56 | - this.buffer = new Buffer(buffer); | ||
| 58 | + this.buffer = utils.toBuffer(buffer); | ||
| 57 | 59 | } else if ( | |
| 58 | 60 | typeof Uint8Array !== 'undefined' || | |
| 59 | 61 | Object.prototype.toString.call(buffer) === '[object Array]' | |
@@ -68,7 +70,7 @@ function Binary(buffer, subType) { | |||
| 68 | 70 | this.position = buffer.length; | |
| 69 | 71 | } else { | |
| 70 | 72 | if (typeof Buffer !== 'undefined') { | |
| 71 | - this.buffer = new Buffer(Binary.BUFFER_SIZE); | ||
| 73 | + this.buffer = utils.allocBuffer(Binary.BUFFER_SIZE); | ||
| 72 | 74 | } else if (typeof Uint8Array !== 'undefined') { | |
| 73 | 75 | this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE)); | |
| 74 | 76 | } else { | |
@@ -107,7 +109,7 @@ Binary.prototype.put = function put(byte_value) { | |||
| 107 | 109 | } else { | |
| 108 | 110 | if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) { | |
| 109 | 111 | // Create additional overflow buffer | |
| 110 | - var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length); | ||
| 112 | + var buffer = utils.allocBuffer(Binary.BUFFER_SIZE + this.buffer.length); | ||
| 111 | 113 | // Combine the two buffers together | |
| 112 | 114 | this.buffer.copy(buffer, 0, 0, this.buffer.length); | |
| 113 | 115 | this.buffer = buffer; | |
@@ -150,7 +152,7 @@ Binary.prototype.write = function write(string, offset) { | |||
| 150 | 152 | var buffer = null; | |
| 151 | 153 | // If we are in node.js | |
| 152 | 154 | if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) { | |
| 153 | - buffer = new Buffer(this.buffer.length + string.length); | ||
| 155 | + buffer = utils.allocBuffer(this.buffer.length + string.length); | ||
| 154 | 156 | this.buffer.copy(buffer, 0, 0, this.buffer.length); | |
| 155 | 157 | } else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') { | |
| 156 | 158 | // Create a new buffer | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,8 @@ var Map = require('./map'), | |||
| 18 | 18 | // Parts of the parser | |
| 19 | 19 | var deserialize = require('./parser/deserializer'), | |
| 20 | 20 | serializer = require('./parser/serializer'), | |
| 21 | - calculateObjectSize = require('./parser/calculate_size'); | ||
| 21 | + calculateObjectSize = require('./parser/calculate_size'), | ||
| 22 | + utils = require('./parser/utils'); | ||
| 22 | 23 | ||
| 23 | 24 | /** | |
| 24 | 25 | * @ignore | |
@@ -28,7 +29,7 @@ var deserialize = require('./parser/deserializer'), | |||
| 28 | 29 | var MAXSIZE = 1024 * 1024 * 17; | |
| 29 | 30 | ||
| 30 | 31 | // Current Internal Temporary Serialization Buffer | |
| 31 | - var buffer = new Buffer(MAXSIZE); | ||
| 32 | + var buffer = utils.allocBuffer(MAXSIZE); | ||
| 32 | 33 | ||
| 33 | 34 | var BSON = function() {}; | |
| 34 | 35 | ||
@@ -56,7 +57,7 @@ BSON.prototype.serialize = function serialize(object, options) { | |||
| 56 | 57 | ||
| 57 | 58 | // Resize the internal serialization buffer if needed | |
| 58 | 59 | if (buffer.length < minInternalBufferSize) { | |
| 59 | - buffer = new Buffer(minInternalBufferSize); | ||
| 60 | + buffer = utils.allocBuffer(minInternalBufferSize); | ||
| 60 | 61 | } | |
| 61 | 62 | ||
| 62 | 63 | // Attempt to serialize | |
@@ -71,7 +72,7 @@ BSON.prototype.serialize = function serialize(object, options) { | |||
| 71 | 72 | [] | |
| 72 | 73 | ); | |
| 73 | 74 | // Create the final buffer | |
| 74 | - var finishedBuffer = new Buffer(serializationIndex); | ||
| 75 | + var finishedBuffer = utils.allocBuffer(serializationIndex); | ||
| 75 | 76 | // Copy into the finished buffer | |
| 76 | 77 | buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length); | |
| 77 | 78 | // Return the buffer | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,6 +70,8 @@ var INF_POSITIVE_BUFFER = [ | |||
| 70 | 70 | ||
| 71 | 71 | var EXPONENT_REGEX = /^([-+])?(\d+)?$/; | |
| 72 | 72 | ||
| 73 | + var utils = require('./parser/utils'); | ||
| 74 | + | ||
| 73 | 75 | // Detect if the value is a digit | |
| 74 | 76 | var isDigit = function(value) { | |
| 75 | 77 | return !isNaN(parseInt(value, 10)); | |
@@ -143,7 +145,7 @@ var lessThan = function(left, right) { | |||
| 143 | 145 | }; | |
| 144 | 146 | ||
| 145 | 147 | // var longtoHex = function(value) { | |
| 146 | - // var buffer = new Buffer(8); | ||
| 148 | + // var buffer = utils.allocBuffer(8); | ||
| 147 | 149 | // var index = 0; | |
| 148 | 150 | // // Encode the low 64 bits of the decimal | |
| 149 | 151 | // // Encode low bits | |
@@ -160,7 +162,7 @@ var lessThan = function(left, right) { | |||
| 160 | 162 | // }; | |
| 161 | 163 | ||
| 162 | 164 | // var int32toHex = function(value) { | |
| 163 | - // var buffer = new Buffer(4); | ||
| 165 | + // var buffer = utils.allocBuffer(4); | ||
| 164 | 166 | // var index = 0; | |
| 165 | 167 | // // Encode the low 64 bits of the decimal | |
| 166 | 168 | // // Encode low bits | |
@@ -265,17 +267,17 @@ Decimal128.fromString = function(string) { | |||
| 265 | 267 | // Check if user passed Infinity or NaN | |
| 266 | 268 | if (!isDigit(string[index]) && string[index] !== '.') { | |
| 267 | 269 | if (string[index] === 'i' || string[index] === 'I') { | |
| 268 | - return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 270 | + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 269 | 271 | } else if (string[index] === 'N') { | |
| 270 | - return new Decimal128(new Buffer(NAN_BUFFER)); | ||
| 272 | + return new Decimal128(utils.toBuffer(NAN_BUFFER)); | ||
| 271 | 273 | } | |
| 272 | 274 | } | |
| 273 | 275 | ||
| 274 | 276 | // Read all the digits | |
| 275 | 277 | while (isDigit(string[index]) || string[index] === '.') { | |
| 276 | 278 | if (string[index] === '.') { | |
| 277 | 279 | if (sawRadix) { | |
| 278 | - return new Decimal128(new Buffer(NAN_BUFFER)); | ||
| 280 | + return new Decimal128(utils.toBuffer(NAN_BUFFER)); | ||
| 279 | 281 | } | |
| 280 | 282 | ||
| 281 | 283 | sawRadix = true; | |
@@ -320,7 +322,7 @@ Decimal128.fromString = function(string) { | |||
| 320 | 322 | ||
| 321 | 323 | // No digits read | |
| 322 | 324 | if (!match || !match[2]) { | |
| 323 | - return new Decimal128(new Buffer(NAN_BUFFER)); | ||
| 325 | + return new Decimal128(utils.toBuffer(NAN_BUFFER)); | ||
| 324 | 326 | } | |
| 325 | 327 | ||
| 326 | 328 | // Get exponent | |
@@ -332,7 +334,7 @@ Decimal128.fromString = function(string) { | |||
| 332 | 334 | ||
| 333 | 335 | // Return not a number | |
| 334 | 336 | if (string[index]) { | |
| 335 | - return new Decimal128(new Buffer(NAN_BUFFER)); | ||
| 337 | + return new Decimal128(utils.toBuffer(NAN_BUFFER)); | ||
| 336 | 338 | } | |
| 337 | 339 | ||
| 338 | 340 | // Done reading input | |
@@ -380,7 +382,7 @@ Decimal128.fromString = function(string) { | |||
| 380 | 382 | exponent = EXPONENT_MAX; | |
| 381 | 383 | break; | |
| 382 | 384 | } else { | |
| 383 | - return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 385 | + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 384 | 386 | } | |
| 385 | 387 | } | |
| 386 | 388 | ||
@@ -412,7 +414,7 @@ Decimal128.fromString = function(string) { | |||
| 412 | 414 | exponent = EXPONENT_MAX; | |
| 413 | 415 | break; | |
| 414 | 416 | } else { | |
| 415 | - return new Decimal128(new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 417 | + return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)); | ||
| 416 | 418 | } | |
| 417 | 419 | } | |
| 418 | 420 | } | |
@@ -462,7 +464,7 @@ Decimal128.fromString = function(string) { | |||
| 462 | 464 | digits[dIdx] = 1; | |
| 463 | 465 | } else { | |
| 464 | 466 | return new Decimal128( | |
| 465 | - new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER) | ||
| 467 | + utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER) | ||
| 466 | 468 | ); | |
| 467 | 469 | } | |
| 468 | 470 | } | |
@@ -547,7 +549,7 @@ Decimal128.fromString = function(string) { | |||
| 547 | 549 | } | |
| 548 | 550 | ||
| 549 | 551 | // Encode into a buffer | |
| 550 | - var buffer = new Buffer(16); | ||
| 552 | + var buffer = utils.allocBuffer(16); | ||
| 551 | 553 | index = 0; | |
| 552 | 554 | ||
| 553 | 555 | // Encode the low 64 bits of the decimal | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,8 @@ | |||
| 1 | 1 | // Custom inspect property name / symbol. | |
| 2 | 2 | var inspect = 'inspect'; | |
| 3 | 3 | ||
| 4 | + var utils = require('./parser/utils'); | ||
| 5 | + | ||
| 4 | 6 | /** | |
| 5 | 7 | * Machine id. | |
| 6 | 8 | * | |
@@ -58,7 +60,7 @@ var ObjectID = function ObjectID(id) { | |||
| 58 | 60 | 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | |
| 59 | 61 | ); | |
| 60 | 62 | } else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) { | |
| 61 | - return new ObjectID(new Buffer(id, 'hex')); | ||
| 63 | + return new ObjectID(utils.toBuffer(id, 'hex')); | ||
| 62 | 64 | } else if (valid && typeof id === 'string' && id.length === 24) { | |
| 63 | 65 | return ObjectID.createFromHexString(id); | |
| 64 | 66 | } else if (id != null && id.length === 12) { | |
@@ -158,7 +160,7 @@ ObjectID.prototype.generate = function(time) { | |||
| 158 | 160 | : process.pid) % 0xffff; | |
| 159 | 161 | var inc = this.get_inc(); | |
| 160 | 162 | // Buffer used | |
| 161 | - var buffer = new Buffer(12); | ||
| 163 | + var buffer = utils.allocBuffer(12); | ||
| 162 | 164 | // Encode time | |
| 163 | 165 | buffer[3] = time & 0xff; | |
| 164 | 166 | buffer[2] = (time >> 8) & 0xff; | |
@@ -277,7 +279,7 @@ ObjectID.createPk = function createPk() { | |||
| 277 | 279 | * @return {ObjectID} return the created ObjectID | |
| 278 | 280 | */ | |
| 279 | 281 | ObjectID.createFromTime = function createFromTime(time) { | |
| 280 | - var buffer = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
| 282 | + var buffer = utils.toBuffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
| 281 | 283 | // Encode time into first 4 bytes | |
| 282 | 284 | buffer[3] = time & 0xff; | |
| 283 | 285 | buffer[2] = (time >> 8) & 0xff; | |
@@ -315,7 +317,7 @@ ObjectID.createFromHexString = function createFromHexString(string) { | |||
| 315 | 317 | } | |
| 316 | 318 | ||
| 317 | 319 | // Use Buffer.from method if available | |
| 318 | - if (hasBufferType) return new ObjectID(new Buffer(string, 'hex')); | ||
| 320 | + if (hasBufferType) return new ObjectID(utils.toBuffer(string, 'hex')); | ||
| 319 | 321 | ||
| 320 | 322 | // Calculate lengths | |
| 321 | 323 | var array = new _Buffer(12); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,8 @@ var Long = require('../long').Long, | |||
| 14 | 14 | BSONRegExp = require('../regexp').BSONRegExp, | |
| 15 | 15 | Binary = require('../binary').Binary; | |
| 16 | 16 | ||
| 17 | + var utils = require('./utils'); | ||
| 18 | + | ||
| 17 | 19 | var deserialize = function(buffer, options, isArray) { | |
| 18 | 20 | options = options == null ? {} : options; | |
| 19 | 21 | var index = options && options.index ? options.index : 0; | |
@@ -115,7 +117,7 @@ var deserializeObject = function(buffer, index, options, isArray) { | |||
| 115 | 117 | object[name] = buffer.toString('utf8', index, index + stringSize - 1); | |
| 116 | 118 | index = index + stringSize; | |
| 117 | 119 | } else if (elementType === BSON.BSON_DATA_OID) { | |
| 118 | - var oid = new Buffer(12); | ||
| 120 | + var oid = utils.allocBuffer(12); | ||
| 119 | 121 | buffer.copy(oid, 0, index, index + 12); | |
| 120 | 122 | object[name] = new ObjectID(oid); | |
| 121 | 123 | index = index + 12; | |
@@ -220,7 +222,7 @@ var deserializeObject = function(buffer, index, options, isArray) { | |||
| 220 | 222 | } | |
| 221 | 223 | } else if (elementType === BSON.BSON_DATA_DECIMAL128) { | |
| 222 | 224 | // Buffer to contain the decimal bytes | |
| 223 | - var bytes = new Buffer(16); | ||
| 225 | + var bytes = utils.allocBuffer(16); | ||
| 224 | 226 | // Copy the next 16 bytes into the bytes buffer | |
| 225 | 227 | buffer.copy(bytes, 0, index, index + 16); | |
| 226 | 228 | // Update index | |
@@ -520,7 +522,7 @@ var deserializeObject = function(buffer, index, options, isArray) { | |||
| 520 | 522 | index = index + stringSize; | |
| 521 | 523 | ||
| 522 | 524 | // Read the oid | |
| 523 | - var oidBuffer = new Buffer(12); | ||
| 525 | + var oidBuffer = utils.allocBuffer(12); | ||
| 524 | 526 | buffer.copy(oidBuffer, 0, index, index + 12); | |
| 525 | 527 | oid = new ObjectID(oidBuffer); | |
| 526 | 528 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,21 @@ function normalizedFunctionString(fn) { | |||
| 8 | 8 | return fn.toString().replace(/function *\(/, 'function ('); | |
| 9 | 9 | } | |
| 10 | 10 | ||
| 11 | + function newBuffer(item, encoding) { | ||
| 12 | + return new Buffer(item, encoding); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + function allocBuffer() { | ||
| 16 | + return Buffer.alloc.apply(Buffer, arguments); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + function toBuffer() { | ||
| 20 | + return Buffer.from.apply(Buffer, arguments); | ||
| 21 | + } | ||
| 22 | + | ||
| 11 | 23 | module.exports = { | |
| 12 | - normalizedFunctionString: normalizedFunctionString | ||
| 24 | + normalizedFunctionString: normalizedFunctionString, | ||
| 25 | + allocBuffer: typeof Buffer.alloc === 'function' ? allocBuffer : newBuffer, | ||
| 26 | + toBuffer: typeof Buffer.from === 'function' ? toBuffer : newBuffer | ||
| 13 | 27 | }; | |
| 14 | 28 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments