Skip to content

Commit 759dd85

Browse files
Sophie Saskinmbroadst
Sophie Saskin
authored andcommittedAug 8, 2018
fix(buffer): replace deprecated Buffer constructor
Replace Buffer constructor with buffer.alloc and buffer.from Fixes Node-1461
1 parent cb9d915 commit 759dd85

13 files changed

+63
-63
lines changed
 

‎lib/gridfs-stream/upload.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function GridFSBucketWriteStream(bucket, filename, options) {
4141

4242
this.id = options.id ? options.id : core.BSON.ObjectId();
4343
this.chunkSizeBytes = this.options.chunkSizeBytes;
44-
this.bufToStore = new Buffer(this.chunkSizeBytes);
44+
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
4545
this.length = 0;
4646
this.md5 = !options.disableMD5 && crypto.createHash('md5');
4747
this.n = 0;
@@ -389,7 +389,7 @@ function doWrite(_this, chunk, encoding, callback) {
389389
return false;
390390
}
391391

392-
var inputBuf = Buffer.isBuffer(chunk) ? chunk : new Buffer(chunk, encoding);
392+
var inputBuf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
393393

394394
_this.length += inputBuf.length;
395395

@@ -501,7 +501,7 @@ function writeRemnant(_this, callback) {
501501

502502
// Create a new buffer to make sure the buffer isn't bigger than it needs
503503
// to be.
504-
var remnant = new Buffer(_this.pos);
504+
var remnant = Buffer.alloc(_this.pos);
505505
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
506506
if (_this.md5) {
507507
_this.md5.update(remnant);

‎lib/gridfs/chunk.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ var Chunk = function(file, mongoObject, writeConcern) {
2929
this.data = new Binary();
3030

3131
if (typeof mongoObjectFinal.data === 'string') {
32-
var buffer = new Buffer(mongoObjectFinal.data.length);
32+
var buffer = Buffer.alloc(mongoObjectFinal.data.length);
3333
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
3434
this.data = new Binary(buffer);
3535
} else if (Array.isArray(mongoObjectFinal.data)) {
36-
buffer = new Buffer(mongoObjectFinal.data.length);
36+
buffer = Buffer.alloc(mongoObjectFinal.data.length);
3737
var data = mongoObjectFinal.data.join('');
3838
buffer.write(data, 0, data.length, 'binary');
3939
this.data = new Binary(buffer);
@@ -91,7 +91,7 @@ Chunk.prototype.readSlice = function(length) {
9191
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
9292
} else {
9393
//Native BSON
94-
data = new Buffer(length);
94+
data = Buffer.alloc(length);
9595
length = this.data.readInto(data, this.internalPosition);
9696
}
9797
this.internalPosition = this.internalPosition + length;

‎lib/gridfs/grid_store.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ var writeFile = function(self, file, options, callback) {
414414
// Write a chunk
415415
var writeChunk = function() {
416416
// Allocate the buffer
417-
var _buffer = new Buffer(self.chunkSize);
417+
var _buffer = Buffer.alloc(self.chunkSize);
418418
// Read the file
419419
fs.read(file, _buffer, 0, _buffer.length, offset, function(err, bytesRead, data) {
420420
if (err) return callback(err, self);
@@ -743,7 +743,7 @@ GridStore.prototype.read = function(length, buffer, options, callback) {
743743
var read = function(self, length, buffer, options, callback) {
744744
// The data is a c-terminated string and thus the length - 1
745745
var finalLength = length == null ? self.length - self.position : length;
746-
var finalBuffer = buffer == null ? new Buffer(finalLength) : buffer;
746+
var finalBuffer = buffer == null ? Buffer.alloc(finalLength) : buffer;
747747
// Add a index to buffer to keep track of writing position or apply current index
748748
finalBuffer._index = buffer != null && buffer._index != null ? buffer._index : 0;
749749

@@ -1550,7 +1550,7 @@ var _writeNormal = function(self, data, close, options, callback) {
15501550
if (Buffer.isBuffer(data)) {
15511551
return writeBuffer(self, data, close, callback);
15521552
} else {
1553-
return writeBuffer(self, new Buffer(data, 'binary'), close, callback);
1553+
return writeBuffer(self, Buffer.from(data, 'binary'), close, callback);
15541554
}
15551555
};
15561556

‎test/functional/cursor_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ describe('Cursor', function() {
21962196
if (++i === 5) {
21972197
client.topology
21982198
.connections()[0]
2199-
.write(new Buffer('312312321321askdjljsaNCKnablibh'));
2199+
.write(Buffer.from('312312321321askdjljsaNCKnablibh'));
22002200
}
22012201
});
22022202

‎test/functional/cursorstream_tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('Cursor Streams', function() {
101101
var docs = [];
102102

103103
for (var i = 0; i < 10000; i++) {
104-
docs.push({ a: i, bin: new Binary(new Buffer(256)) });
104+
docs.push({ a: i, bin: new Binary(Buffer.alloc(256)) });
105105
}
106106

107107
var j = 0;
@@ -179,7 +179,7 @@ describe('Cursor Streams', function() {
179179
var counter2 = 0;
180180

181181
for (var i = 0; i < 1000; i++) {
182-
docs.push({ a: i, bin: new Binary(new Buffer(256)) });
182+
docs.push({ a: i, bin: new Binary(Buffer.alloc(256)) });
183183
}
184184

185185
var client = self.configuration.newClient(self.configuration.writeConcernMax(), {
@@ -238,7 +238,7 @@ describe('Cursor Streams', function() {
238238
var docs = [];
239239

240240
for (var i = 0; i < 2000; i++) {
241-
docs.push({ a: i, b: new Binary(new Buffer(1024)) });
241+
docs.push({ a: i, b: new Binary(Buffer.alloc(1024)) });
242242
}
243243

244244
var allDocs = [];

‎test/functional/find_tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ describe('Find', function() {
22192219
a: 1,
22202220
b:
22212221
'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld',
2222-
c: new Binary(new Buffer(1024))
2222+
c: new Binary(Buffer.alloc(1024))
22232223
});
22242224
}
22252225

@@ -2233,7 +2233,7 @@ describe('Find', function() {
22332233
a: 1,
22342234
b:
22352235
'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld',
2236-
c: new Binary(new Buffer(1024))
2236+
c: new Binary(Buffer.alloc(1024))
22372237
});
22382238
}
22392239

‎test/functional/gridfs_stream_tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ describe('GridFS Stream', function() {
10551055
testSpec.act.arguments.filename,
10561056
testSpec.act.arguments.options
10571057
);
1058-
var buf = new Buffer(testSpec.act.arguments.source.$hex, 'hex');
1058+
var buf = Buffer.from(testSpec.act.arguments.source.$hex, 'hex');
10591059

10601060
res.on('error', function(err) {
10611061
test.equal(null, err);
@@ -1112,7 +1112,7 @@ describe('GridFS Stream', function() {
11121112

11131113
var _runTest = function() {
11141114
var bucket = new GridFSBucket(db, { bucketName: BUCKET_NAME });
1115-
var res = new Buffer(0);
1115+
var res = Buffer.alloc(0);
11161116

11171117
var download = bucket.openDownloadStream(
11181118
EJSON.parse(JSON.stringify(testSpec.act.arguments.id), { relaxed: true })
@@ -1235,7 +1235,7 @@ describe('GridFS Stream', function() {
12351235
keys.forEach(function(key) {
12361236
if (doc[key] && typeof doc[key] === 'object') {
12371237
if (doc[key].$hex != null) {
1238-
doc[key] = new Buffer(doc[key].$hex, 'hex');
1238+
doc[key] = Buffer.from(doc[key].$hex, 'hex');
12391239
} else {
12401240
convert$hexToBuffer(doc[key]);
12411241
}

‎test/functional/gridfs_tests.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ describe('GridFS', function() {
16651665
gridStore.open(function(err, gridStore) {
16661666
expect(err).to.not.exist;
16671667

1668-
gridStore.write(new Buffer('012345678901234567890', 'utf8'), function(err, gridStore) {
1668+
gridStore.write(Buffer.from('012345678901234567890', 'utf8'), function(err, gridStore) {
16691669
expect(err).to.not.exist;
16701670

16711671
gridStore.close(function() {
@@ -1727,7 +1727,7 @@ describe('GridFS', function() {
17271727
gridStore.open(function(err, gridStore) {
17281728
expect(err).to.not.exist;
17291729

1730-
gridStore.write(new Buffer('012345678901234567890', 'utf8'), function(err, gridStore) {
1730+
gridStore.write(Buffer.from('012345678901234567890', 'utf8'), function(err, gridStore) {
17311731
expect(err).to.not.exist;
17321732

17331733
gridStore.close(function() {
@@ -1851,7 +1851,7 @@ describe('GridFS', function() {
18511851
expect(err).to.not.exist;
18521852

18531853
// Create a chunkSize Buffer
1854-
var buffer = new Buffer(chunkSize);
1854+
var buffer = Buffer.alloc(chunkSize);
18551855

18561856
// Write the buffer
18571857
gridStore.write(buffer, function(err, gridStore) {
@@ -2109,7 +2109,7 @@ describe('GridFS', function() {
21092109
gridStore.open(function(err, gridStore) {
21102110
expect(err).to.not.exist;
21112111

2112-
var d = new Buffer(5000);
2112+
var d = Buffer.alloc(5000);
21132113
for (var j = 0; j < 5000; j++) {
21142114
d[j] = 43;
21152115
}
@@ -2174,7 +2174,7 @@ describe('GridFS', function() {
21742174
var gridStoreR = new GridStore(db, 'test_gs_read_stream', 'r');
21752175
var gridStoreW = new GridStore(db, 'test_gs_read_stream', 'w', { chunkSize: 56 });
21762176
// var data = fs.readFileSync("./test/gridstore/test_gs_weird_bug.png");
2177-
var data = new Buffer(100);
2177+
var data = Buffer.alloc(100);
21782178
for (var i = 0; i < 100; i++) {
21792179
data[i] = i;
21802180
}
@@ -2210,7 +2210,7 @@ describe('GridFS', function() {
22102210
expect(err).to.not.exist;
22112211

22122212
// Put together all the chunks
2213-
var streamData = new Buffer(data.length);
2213+
var streamData = Buffer.alloc(data.length);
22142214
var index = 0;
22152215
var i;
22162216
for (i = 0; i < chunks.length; i++) {
@@ -2374,7 +2374,7 @@ describe('GridFS', function() {
23742374
gridStore.open(function(err, gridStore) {
23752375
expect(err).to.not.exist;
23762376

2377-
var data = new Buffer('hello world', 'utf8');
2377+
var data = Buffer.from('hello world', 'utf8');
23782378

23792379
gridStore.write(data, function(err, gridStore) {
23802380
expect(err).to.not.exist;
@@ -2689,7 +2689,7 @@ describe('GridFS', function() {
26892689
// Open the file
26902690
gridStore.open(function(err, gridStore) {
26912691
expect(err).to.not.exist;
2692-
var data = new Buffer(gridStore.chunkSize * 3);
2692+
var data = Buffer.alloc(gridStore.chunkSize * 3);
26932693
// Write the binary file data to GridFS
26942694
gridStore.write(data, function(err, gridStore) {
26952695
expect(err).to.not.exist;
@@ -2878,7 +2878,7 @@ describe('GridFS', function() {
28782878
expect(err).to.not.exist;
28792879
var db = client.db(configuration.db);
28802880
var gridStore = new GridStore(db, 'test_gs_check_high_bits', 'w');
2881-
var data = new Buffer(255);
2881+
var data = Buffer.alloc(255);
28822882
for (var i = 0; i < 255; i++) {
28832883
data[i] = i;
28842884
}
@@ -2896,7 +2896,7 @@ describe('GridFS', function() {
28962896
// change testvalue into a string like "0,1,2,...,255"
28972897
test.equal(data.toString('hex'), fileData.toString('hex'));
28982898
// test.equal(Array.prototype.join.call(data),
2899-
// Array.prototype.join.call(new Buffer(fileData, "binary")));
2899+
// Array.prototype.join.call(Buffer.from(fileData, "binary")));
29002900
client.close();
29012901
done();
29022902
});
@@ -3364,7 +3364,7 @@ describe('GridFS', function() {
33643364

33653365
var write = function(left, callback) {
33663366
if (left === 0) return callback();
3367-
gridStore.write(new Buffer(5000), function() {
3367+
gridStore.write(Buffer.alloc(5000), function() {
33683368
left = left - 1;
33693369
write(left, callback);
33703370
});
@@ -3467,7 +3467,7 @@ describe('GridFS', function() {
34673467
expect(err).to.not.exist;
34683468
var db = client.db(configuration.db);
34693469
// Massive data Buffer
3470-
var data = new Buffer(1024 * 512);
3470+
var data = Buffer.alloc(1024 * 512);
34713471
// Set some data in the buffer at a point we want to read in the next chunk
34723472
data.write('Hello world!', 1024 * 256);
34733473

@@ -3729,7 +3729,7 @@ describe('GridFS', function() {
37293729
gridStore.chunkSize = 512;
37303730

37313731
// Write multiple of chunk size
3732-
gridStore.write(new Buffer(gridStore.chunkSize * 4), function(err) {
3732+
gridStore.write(Buffer.alloc(gridStore.chunkSize * 4), function(err) {
37333733
expect(err).to.not.exist;
37343734

37353735
gridStore.close(function(err) {
@@ -3800,7 +3800,7 @@ describe('GridFS', function() {
38003800
gridStore.chunkSize = 512;
38013801

38023802
// Get the data
3803-
var data = new Buffer(gridStore.chunkSize * 2);
3803+
var data = Buffer.alloc(gridStore.chunkSize * 2);
38043804
for (var i = 0; i < gridStore.chunkSize * 2; i++) {
38053805
data[i] = 0;
38063806
}
@@ -3873,7 +3873,7 @@ describe('GridFS', function() {
38733873
ObjectID = configuration.require.ObjectID;
38743874

38753875
// Create a test buffer
3876-
var buffer = new Buffer(200033);
3876+
var buffer = Buffer.alloc(200033);
38773877

38783878
// Use connect method to connect to the Server
38793879
MongoClient.connect(configuration.url(), { sslValidate: false }, function(err, client) {
@@ -3917,7 +3917,7 @@ describe('GridFS', function() {
39173917
var succeeded = [];
39183918

39193919
// Create a test buffer
3920-
var buffer = new Buffer(2000);
3920+
var buffer = Buffer.alloc(2000);
39213921

39223922
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
39233923
client.connect(function(err, client) {

‎test/functional/insert_tests.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1567,11 +1567,11 @@ describe('Insert', function() {
15671567
client.connect(function(err, client) {
15681568
var db = client.db(configuration.db);
15691569
db.createCollection('shouldAttempToForceBsonSize', function(err, collection) {
1570-
// var doc = {a:1, b:new Binary(new Buffer(16777216)/5)}
1570+
// var doc = {a:1, b:new Binary(Buffer.alloc(16777216)/5)}
15711571
var doc = [
1572-
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) },
1573-
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) },
1574-
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) }
1572+
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) },
1573+
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) },
1574+
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) }
15751575
];
15761576

15771577
collection.insert(doc, configuration.writeConcernMax(), function(err, result) {
@@ -1782,7 +1782,7 @@ describe('Insert', function() {
17821782
symbol: new Symbol('abcdefghijkl'),
17831783
objid: new ObjectID('abcdefghijkl'),
17841784
double: new Double(1),
1785-
binary: new Binary(new Buffer('hello world')),
1785+
binary: new Binary(Buffer.from('hello world')),
17861786
minkey: new MinKey(),
17871787
maxkey: new MaxKey(),
17881788
code: new Code('function () {}', { a: 55 })
@@ -1804,7 +1804,7 @@ describe('Insert', function() {
18041804
test.equal(null, err);
18051805
test.equal(1, doc.double);
18061806

1807-
collection.findOne({ binary: new Binary(new Buffer('hello world')) }, function(
1807+
collection.findOne({ binary: new Binary(Buffer.from('hello world')) }, function(
18081808
err,
18091809
doc
18101810
) {
@@ -1872,7 +1872,7 @@ describe('Insert', function() {
18721872
symbol: new Symbol('abcdefghijkl'),
18731873
objid: new ObjectID('abcdefghijkl'),
18741874
double: new Double(1),
1875-
binary: new Binary(new Buffer('hello world')),
1875+
binary: new Binary(Buffer.from('hello world')),
18761876
minkey: new MinKey(),
18771877
maxkey: new MaxKey(),
18781878
code: new Code('function () {}', { a: 55 })
@@ -1894,7 +1894,7 @@ describe('Insert', function() {
18941894
test.equal(null, err);
18951895
test.equal(1, doc.double);
18961896

1897-
collection.findOne({ binary: new Binary(new Buffer('hello world')) }, function(
1897+
collection.findOne({ binary: new Binary(Buffer.from('hello world')) }, function(
18981898
err,
18991899
doc
19001900
) {
@@ -2095,7 +2095,7 @@ describe('Insert', function() {
20952095
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
20962096
client.connect(function(err, client) {
20972097
var db = client.db(configuration.db);
2098-
var k = new Buffer(15);
2098+
var k = Buffer.alloc(15);
20992099
for (var i = 0; i < 15; i++) k[i] = 0;
21002100

21012101
k.write('hello');

0 commit comments

Comments
 (0)
Please sign in to comment.