Skip to content

Commit 24aefba

Browse files
committed
feat: replace new Buffer with modern versions
Replace new Buffer with wrappers around Buffer.alloc and Buffer.from, to avoid deprecation messages. Fixes NODE-1789
1 parent 35beb90 commit 24aefba

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

lib/bson/binary.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ if (typeof global !== 'undefined') {
99
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
1010
}
1111

12+
var utils = require('./parser/utils');
13+
1214
/**
1315
* A class representation of the BSON Binary type.
1416
*
@@ -53,7 +55,7 @@ function Binary(buffer, subType) {
5355
if (typeof buffer === 'string') {
5456
// Different ways of writing the length of the string for the different types
5557
if (typeof Buffer !== 'undefined') {
56-
this.buffer = new Buffer(buffer);
58+
this.buffer = utils.toBuffer(buffer);
5759
} else if (
5860
typeof Uint8Array !== 'undefined' ||
5961
Object.prototype.toString.call(buffer) === '[object Array]'
@@ -68,7 +70,7 @@ function Binary(buffer, subType) {
6870
this.position = buffer.length;
6971
} else {
7072
if (typeof Buffer !== 'undefined') {
71-
this.buffer = new Buffer(Binary.BUFFER_SIZE);
73+
this.buffer = utils.allocBuffer(Binary.BUFFER_SIZE);
7274
} else if (typeof Uint8Array !== 'undefined') {
7375
this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
7476
} else {
@@ -107,7 +109,7 @@ Binary.prototype.put = function put(byte_value) {
107109
} else {
108110
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.buffer)) {
109111
// 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);
111113
// Combine the two buffers together
112114
this.buffer.copy(buffer, 0, 0, this.buffer.length);
113115
this.buffer = buffer;
@@ -150,7 +152,7 @@ Binary.prototype.write = function write(string, offset) {
150152
var buffer = null;
151153
// If we are in node.js
152154
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);
154156
this.buffer.copy(buffer, 0, 0, this.buffer.length);
155157
} else if (Object.prototype.toString.call(this.buffer) === '[object Uint8Array]') {
156158
// Create a new buffer

lib/bson/bson.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var Map = require('./map'),
1818
// Parts of the parser
1919
var deserialize = require('./parser/deserializer'),
2020
serializer = require('./parser/serializer'),
21-
calculateObjectSize = require('./parser/calculate_size');
21+
calculateObjectSize = require('./parser/calculate_size'),
22+
utils = require('./parser/utils');
2223

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

3031
// Current Internal Temporary Serialization Buffer
31-
var buffer = new Buffer(MAXSIZE);
32+
var buffer = utils.allocBuffer(MAXSIZE);
3233

3334
var BSON = function() {};
3435

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

5758
// Resize the internal serialization buffer if needed
5859
if (buffer.length < minInternalBufferSize) {
59-
buffer = new Buffer(minInternalBufferSize);
60+
buffer = utils.allocBuffer(minInternalBufferSize);
6061
}
6162

6263
// Attempt to serialize
@@ -71,7 +72,7 @@ BSON.prototype.serialize = function serialize(object, options) {
7172
[]
7273
);
7374
// Create the final buffer
74-
var finishedBuffer = new Buffer(serializationIndex);
75+
var finishedBuffer = utils.allocBuffer(serializationIndex);
7576
// Copy into the finished buffer
7677
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
7778
// Return the buffer

lib/bson/decimal128.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ var INF_POSITIVE_BUFFER = [
7070

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

73+
var utils = require('./parser/utils');
74+
7375
// Detect if the value is a digit
7476
var isDigit = function(value) {
7577
return !isNaN(parseInt(value, 10));
@@ -143,7 +145,7 @@ var lessThan = function(left, right) {
143145
};
144146

145147
// var longtoHex = function(value) {
146-
// var buffer = new Buffer(8);
148+
// var buffer = utils.allocBuffer(8);
147149
// var index = 0;
148150
// // Encode the low 64 bits of the decimal
149151
// // Encode low bits
@@ -160,7 +162,7 @@ var lessThan = function(left, right) {
160162
// };
161163

162164
// var int32toHex = function(value) {
163-
// var buffer = new Buffer(4);
165+
// var buffer = utils.allocBuffer(4);
164166
// var index = 0;
165167
// // Encode the low 64 bits of the decimal
166168
// // Encode low bits
@@ -265,17 +267,17 @@ Decimal128.fromString = function(string) {
265267
// Check if user passed Infinity or NaN
266268
if (!isDigit(string[index]) && string[index] !== '.') {
267269
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));
269271
} else if (string[index] === 'N') {
270-
return new Decimal128(new Buffer(NAN_BUFFER));
272+
return new Decimal128(utils.toBuffer(NAN_BUFFER));
271273
}
272274
}
273275

274276
// Read all the digits
275277
while (isDigit(string[index]) || string[index] === '.') {
276278
if (string[index] === '.') {
277279
if (sawRadix) {
278-
return new Decimal128(new Buffer(NAN_BUFFER));
280+
return new Decimal128(utils.toBuffer(NAN_BUFFER));
279281
}
280282

281283
sawRadix = true;
@@ -320,7 +322,7 @@ Decimal128.fromString = function(string) {
320322

321323
// No digits read
322324
if (!match || !match[2]) {
323-
return new Decimal128(new Buffer(NAN_BUFFER));
325+
return new Decimal128(utils.toBuffer(NAN_BUFFER));
324326
}
325327

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

333335
// Return not a number
334336
if (string[index]) {
335-
return new Decimal128(new Buffer(NAN_BUFFER));
337+
return new Decimal128(utils.toBuffer(NAN_BUFFER));
336338
}
337339

338340
// Done reading input
@@ -380,7 +382,7 @@ Decimal128.fromString = function(string) {
380382
exponent = EXPONENT_MAX;
381383
break;
382384
} 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));
384386
}
385387
}
386388

@@ -412,7 +414,7 @@ Decimal128.fromString = function(string) {
412414
exponent = EXPONENT_MAX;
413415
break;
414416
} 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));
416418
}
417419
}
418420
}
@@ -462,7 +464,7 @@ Decimal128.fromString = function(string) {
462464
digits[dIdx] = 1;
463465
} else {
464466
return new Decimal128(
465-
new Buffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
467+
utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
466468
);
467469
}
468470
}
@@ -547,7 +549,7 @@ Decimal128.fromString = function(string) {
547549
}
548550

549551
// Encode into a buffer
550-
var buffer = new Buffer(16);
552+
var buffer = utils.allocBuffer(16);
551553
index = 0;
552554

553555
// Encode the low 64 bits of the decimal

lib/bson/objectid.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Custom inspect property name / symbol.
22
var inspect = 'inspect';
33

4+
var utils = require('./parser/utils');
5+
46
/**
57
* Machine id.
68
*
@@ -58,7 +60,7 @@ var ObjectID = function ObjectID(id) {
5860
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
5961
);
6062
} 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'));
6264
} else if (valid && typeof id === 'string' && id.length === 24) {
6365
return ObjectID.createFromHexString(id);
6466
} else if (id != null && id.length === 12) {
@@ -158,7 +160,7 @@ ObjectID.prototype.generate = function(time) {
158160
: process.pid) % 0xffff;
159161
var inc = this.get_inc();
160162
// Buffer used
161-
var buffer = new Buffer(12);
163+
var buffer = utils.allocBuffer(12);
162164
// Encode time
163165
buffer[3] = time & 0xff;
164166
buffer[2] = (time >> 8) & 0xff;
@@ -277,7 +279,7 @@ ObjectID.createPk = function createPk() {
277279
* @return {ObjectID} return the created ObjectID
278280
*/
279281
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]);
281283
// Encode time into first 4 bytes
282284
buffer[3] = time & 0xff;
283285
buffer[2] = (time >> 8) & 0xff;
@@ -315,7 +317,7 @@ ObjectID.createFromHexString = function createFromHexString(string) {
315317
}
316318

317319
// 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'));
319321

320322
// Calculate lengths
321323
var array = new _Buffer(12);

lib/bson/parser/deserializer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ var Long = require('../long').Long,
1414
BSONRegExp = require('../regexp').BSONRegExp,
1515
Binary = require('../binary').Binary;
1616

17+
var utils = require('./utils');
18+
1719
var deserialize = function(buffer, options, isArray) {
1820
options = options == null ? {} : options;
1921
var index = options && options.index ? options.index : 0;
@@ -115,7 +117,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
115117
object[name] = buffer.toString('utf8', index, index + stringSize - 1);
116118
index = index + stringSize;
117119
} else if (elementType === BSON.BSON_DATA_OID) {
118-
var oid = new Buffer(12);
120+
var oid = utils.allocBuffer(12);
119121
buffer.copy(oid, 0, index, index + 12);
120122
object[name] = new ObjectID(oid);
121123
index = index + 12;
@@ -220,7 +222,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
220222
}
221223
} else if (elementType === BSON.BSON_DATA_DECIMAL128) {
222224
// Buffer to contain the decimal bytes
223-
var bytes = new Buffer(16);
225+
var bytes = utils.allocBuffer(16);
224226
// Copy the next 16 bytes into the bytes buffer
225227
buffer.copy(bytes, 0, index, index + 16);
226228
// Update index
@@ -520,7 +522,7 @@ var deserializeObject = function(buffer, index, options, isArray) {
520522
index = index + stringSize;
521523

522524
// Read the oid
523-
var oidBuffer = new Buffer(12);
525+
var oidBuffer = utils.allocBuffer(12);
524526
buffer.copy(oidBuffer, 0, index, index + 12);
525527
oid = new ObjectID(oidBuffer);
526528

lib/bson/parser/utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@ function normalizedFunctionString(fn) {
88
return fn.toString().replace(/function *\(/, 'function (');
99
}
1010

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+
1123
module.exports = {
12-
normalizedFunctionString: normalizedFunctionString
24+
normalizedFunctionString: normalizedFunctionString,
25+
allocBuffer: typeof Buffer.alloc === 'function' ? allocBuffer : newBuffer,
26+
toBuffer: typeof Buffer.from === 'function' ? toBuffer : newBuffer
1327
};
1428

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy