Skip to content

Commit

Permalink
fix(buffer): replace deprecated Buffer constructor
Browse files Browse the repository at this point in the history
Replace Buffer constructor with buffer.alloc and buffer.from

Fixes Node-1461
  • Loading branch information
Sophie Saskin authored and mbroadst committed Aug 8, 2018
1 parent cb9d915 commit 759dd85
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 63 deletions.
6 changes: 3 additions & 3 deletions lib/gridfs-stream/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function GridFSBucketWriteStream(bucket, filename, options) {

this.id = options.id ? options.id : core.BSON.ObjectId();
this.chunkSizeBytes = this.options.chunkSizeBytes;
this.bufToStore = new Buffer(this.chunkSizeBytes);
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
Expand Down Expand Up @@ -389,7 +389,7 @@ function doWrite(_this, chunk, encoding, callback) {
return false;
}

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

_this.length += inputBuf.length;

Expand Down Expand Up @@ -501,7 +501,7 @@ function writeRemnant(_this, callback) {

// Create a new buffer to make sure the buffer isn't bigger than it needs
// to be.
var remnant = new Buffer(_this.pos);
var remnant = Buffer.alloc(_this.pos);
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
if (_this.md5) {
_this.md5.update(remnant);
Expand Down
6 changes: 3 additions & 3 deletions lib/gridfs/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ var Chunk = function(file, mongoObject, writeConcern) {
this.data = new Binary();

if (typeof mongoObjectFinal.data === 'string') {
var buffer = new Buffer(mongoObjectFinal.data.length);
var buffer = Buffer.alloc(mongoObjectFinal.data.length);
buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
this.data = new Binary(buffer);
} else if (Array.isArray(mongoObjectFinal.data)) {
buffer = new Buffer(mongoObjectFinal.data.length);
buffer = Buffer.alloc(mongoObjectFinal.data.length);
var data = mongoObjectFinal.data.join('');
buffer.write(data, 0, data.length, 'binary');
this.data = new Binary(buffer);
Expand Down Expand Up @@ -91,7 +91,7 @@ Chunk.prototype.readSlice = function(length) {
data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
} else {
//Native BSON
data = new Buffer(length);
data = Buffer.alloc(length);
length = this.data.readInto(data, this.internalPosition);
}
this.internalPosition = this.internalPosition + length;
Expand Down
6 changes: 3 additions & 3 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ var writeFile = function(self, file, options, callback) {
// Write a chunk
var writeChunk = function() {
// Allocate the buffer
var _buffer = new Buffer(self.chunkSize);
var _buffer = Buffer.alloc(self.chunkSize);
// Read the file
fs.read(file, _buffer, 0, _buffer.length, offset, function(err, bytesRead, data) {
if (err) return callback(err, self);
Expand Down Expand Up @@ -743,7 +743,7 @@ GridStore.prototype.read = function(length, buffer, options, callback) {
var read = function(self, length, buffer, options, callback) {
// The data is a c-terminated string and thus the length - 1
var finalLength = length == null ? self.length - self.position : length;
var finalBuffer = buffer == null ? new Buffer(finalLength) : buffer;
var finalBuffer = buffer == null ? Buffer.alloc(finalLength) : buffer;
// Add a index to buffer to keep track of writing position or apply current index
finalBuffer._index = buffer != null && buffer._index != null ? buffer._index : 0;

Expand Down Expand Up @@ -1550,7 +1550,7 @@ var _writeNormal = function(self, data, close, options, callback) {
if (Buffer.isBuffer(data)) {
return writeBuffer(self, data, close, callback);
} else {
return writeBuffer(self, new Buffer(data, 'binary'), close, callback);
return writeBuffer(self, Buffer.from(data, 'binary'), close, callback);
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ describe('Cursor', function() {
if (++i === 5) {
client.topology
.connections()[0]
.write(new Buffer('312312321321askdjljsaNCKnablibh'));
.write(Buffer.from('312312321321askdjljsaNCKnablibh'));
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/functional/cursorstream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Cursor Streams', function() {
var docs = [];

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

var j = 0;
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Cursor Streams', function() {
var counter2 = 0;

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

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

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

var allDocs = [];
Expand Down
4 changes: 2 additions & 2 deletions test/functional/find_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,7 @@ describe('Find', function() {
a: 1,
b:
'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld',
c: new Binary(new Buffer(1024))
c: new Binary(Buffer.alloc(1024))
});
}

Expand All @@ -2233,7 +2233,7 @@ describe('Find', function() {
a: 1,
b:
'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld',
c: new Binary(new Buffer(1024))
c: new Binary(Buffer.alloc(1024))
});
}

Expand Down
6 changes: 3 additions & 3 deletions test/functional/gridfs_stream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ describe('GridFS Stream', function() {
testSpec.act.arguments.filename,
testSpec.act.arguments.options
);
var buf = new Buffer(testSpec.act.arguments.source.$hex, 'hex');
var buf = Buffer.from(testSpec.act.arguments.source.$hex, 'hex');

res.on('error', function(err) {
test.equal(null, err);
Expand Down Expand Up @@ -1112,7 +1112,7 @@ describe('GridFS Stream', function() {

var _runTest = function() {
var bucket = new GridFSBucket(db, { bucketName: BUCKET_NAME });
var res = new Buffer(0);
var res = Buffer.alloc(0);

var download = bucket.openDownloadStream(
EJSON.parse(JSON.stringify(testSpec.act.arguments.id), { relaxed: true })
Expand Down Expand Up @@ -1235,7 +1235,7 @@ describe('GridFS Stream', function() {
keys.forEach(function(key) {
if (doc[key] && typeof doc[key] === 'object') {
if (doc[key].$hex != null) {
doc[key] = new Buffer(doc[key].$hex, 'hex');
doc[key] = Buffer.from(doc[key].$hex, 'hex');
} else {
convert$hexToBuffer(doc[key]);
}
Expand Down
32 changes: 16 additions & 16 deletions test/functional/gridfs_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ describe('GridFS', function() {
gridStore.open(function(err, gridStore) {
expect(err).to.not.exist;

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

gridStore.close(function() {
Expand Down Expand Up @@ -1727,7 +1727,7 @@ describe('GridFS', function() {
gridStore.open(function(err, gridStore) {
expect(err).to.not.exist;

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

gridStore.close(function() {
Expand Down Expand Up @@ -1851,7 +1851,7 @@ describe('GridFS', function() {
expect(err).to.not.exist;

// Create a chunkSize Buffer
var buffer = new Buffer(chunkSize);
var buffer = Buffer.alloc(chunkSize);

// Write the buffer
gridStore.write(buffer, function(err, gridStore) {
Expand Down Expand Up @@ -2109,7 +2109,7 @@ describe('GridFS', function() {
gridStore.open(function(err, gridStore) {
expect(err).to.not.exist;

var d = new Buffer(5000);
var d = Buffer.alloc(5000);
for (var j = 0; j < 5000; j++) {
d[j] = 43;
}
Expand Down Expand Up @@ -2174,7 +2174,7 @@ describe('GridFS', function() {
var gridStoreR = new GridStore(db, 'test_gs_read_stream', 'r');
var gridStoreW = new GridStore(db, 'test_gs_read_stream', 'w', { chunkSize: 56 });
// var data = fs.readFileSync("./test/gridstore/test_gs_weird_bug.png");
var data = new Buffer(100);
var data = Buffer.alloc(100);
for (var i = 0; i < 100; i++) {
data[i] = i;
}
Expand Down Expand Up @@ -2210,7 +2210,7 @@ describe('GridFS', function() {
expect(err).to.not.exist;

// Put together all the chunks
var streamData = new Buffer(data.length);
var streamData = Buffer.alloc(data.length);
var index = 0;
var i;
for (i = 0; i < chunks.length; i++) {
Expand Down Expand Up @@ -2374,7 +2374,7 @@ describe('GridFS', function() {
gridStore.open(function(err, gridStore) {
expect(err).to.not.exist;

var data = new Buffer('hello world', 'utf8');
var data = Buffer.from('hello world', 'utf8');

gridStore.write(data, function(err, gridStore) {
expect(err).to.not.exist;
Expand Down Expand Up @@ -2689,7 +2689,7 @@ describe('GridFS', function() {
// Open the file
gridStore.open(function(err, gridStore) {
expect(err).to.not.exist;
var data = new Buffer(gridStore.chunkSize * 3);
var data = Buffer.alloc(gridStore.chunkSize * 3);
// Write the binary file data to GridFS
gridStore.write(data, function(err, gridStore) {
expect(err).to.not.exist;
Expand Down Expand Up @@ -2878,7 +2878,7 @@ describe('GridFS', function() {
expect(err).to.not.exist;
var db = client.db(configuration.db);
var gridStore = new GridStore(db, 'test_gs_check_high_bits', 'w');
var data = new Buffer(255);
var data = Buffer.alloc(255);
for (var i = 0; i < 255; i++) {
data[i] = i;
}
Expand All @@ -2896,7 +2896,7 @@ describe('GridFS', function() {
// change testvalue into a string like "0,1,2,...,255"
test.equal(data.toString('hex'), fileData.toString('hex'));
// test.equal(Array.prototype.join.call(data),
// Array.prototype.join.call(new Buffer(fileData, "binary")));
// Array.prototype.join.call(Buffer.from(fileData, "binary")));
client.close();
done();
});
Expand Down Expand Up @@ -3364,7 +3364,7 @@ describe('GridFS', function() {

var write = function(left, callback) {
if (left === 0) return callback();
gridStore.write(new Buffer(5000), function() {
gridStore.write(Buffer.alloc(5000), function() {
left = left - 1;
write(left, callback);
});
Expand Down Expand Up @@ -3467,7 +3467,7 @@ describe('GridFS', function() {
expect(err).to.not.exist;
var db = client.db(configuration.db);
// Massive data Buffer
var data = new Buffer(1024 * 512);
var data = Buffer.alloc(1024 * 512);
// Set some data in the buffer at a point we want to read in the next chunk
data.write('Hello world!', 1024 * 256);

Expand Down Expand Up @@ -3729,7 +3729,7 @@ describe('GridFS', function() {
gridStore.chunkSize = 512;

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

gridStore.close(function(err) {
Expand Down Expand Up @@ -3800,7 +3800,7 @@ describe('GridFS', function() {
gridStore.chunkSize = 512;

// Get the data
var data = new Buffer(gridStore.chunkSize * 2);
var data = Buffer.alloc(gridStore.chunkSize * 2);
for (var i = 0; i < gridStore.chunkSize * 2; i++) {
data[i] = 0;
}
Expand Down Expand Up @@ -3873,7 +3873,7 @@ describe('GridFS', function() {
ObjectID = configuration.require.ObjectID;

// Create a test buffer
var buffer = new Buffer(200033);
var buffer = Buffer.alloc(200033);

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

// Create a test buffer
var buffer = new Buffer(2000);
var buffer = Buffer.alloc(2000);

var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function(err, client) {
Expand Down
18 changes: 9 additions & 9 deletions test/functional/insert_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1567,11 +1567,11 @@ describe('Insert', function() {
client.connect(function(err, client) {
var db = client.db(configuration.db);
db.createCollection('shouldAttempToForceBsonSize', function(err, collection) {
// var doc = {a:1, b:new Binary(new Buffer(16777216)/5)}
// var doc = {a:1, b:new Binary(Buffer.alloc(16777216)/5)}
var doc = [
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) },
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) },
{ a: 1, b: new Binary(new Buffer(16777216 / 3)) }
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) },
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) },
{ a: 1, b: new Binary(Buffer.alloc(16777216 / 3)) }
];

collection.insert(doc, configuration.writeConcernMax(), function(err, result) {
Expand Down Expand Up @@ -1782,7 +1782,7 @@ describe('Insert', function() {
symbol: new Symbol('abcdefghijkl'),
objid: new ObjectID('abcdefghijkl'),
double: new Double(1),
binary: new Binary(new Buffer('hello world')),
binary: new Binary(Buffer.from('hello world')),
minkey: new MinKey(),
maxkey: new MaxKey(),
code: new Code('function () {}', { a: 55 })
Expand All @@ -1804,7 +1804,7 @@ describe('Insert', function() {
test.equal(null, err);
test.equal(1, doc.double);

collection.findOne({ binary: new Binary(new Buffer('hello world')) }, function(
collection.findOne({ binary: new Binary(Buffer.from('hello world')) }, function(
err,
doc
) {
Expand Down Expand Up @@ -1872,7 +1872,7 @@ describe('Insert', function() {
symbol: new Symbol('abcdefghijkl'),
objid: new ObjectID('abcdefghijkl'),
double: new Double(1),
binary: new Binary(new Buffer('hello world')),
binary: new Binary(Buffer.from('hello world')),
minkey: new MinKey(),
maxkey: new MaxKey(),
code: new Code('function () {}', { a: 55 })
Expand All @@ -1894,7 +1894,7 @@ describe('Insert', function() {
test.equal(null, err);
test.equal(1, doc.double);

collection.findOne({ binary: new Binary(new Buffer('hello world')) }, function(
collection.findOne({ binary: new Binary(Buffer.from('hello world')) }, function(
err,
doc
) {
Expand Down Expand Up @@ -2095,7 +2095,7 @@ describe('Insert', function() {
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function(err, client) {
var db = client.db(configuration.db);
var k = new Buffer(15);
var k = Buffer.alloc(15);
for (var i = 0; i < 15; i++) k[i] = 0;

k.write('hello');
Expand Down
Loading

0 comments on commit 759dd85

Please sign in to comment.