Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

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 51e92fc commit 7c71e19
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
12 changes: 6 additions & 6 deletions lib/auth/scram.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ var passwordDigest = function(username, password) {

// XOR two buffers
function xor(a, b) {
if (!Buffer.isBuffer(a)) a = new Buffer(a);
if (!Buffer.isBuffer(b)) b = new Buffer(b);
if (!Buffer.isBuffer(a)) a = Buffer.from(a);
if (!Buffer.isBuffer(b)) b = Buffer.from(b);
const length = Math.max(a.length, b.length);
const res = [];

for (let i = 0; i < length; i += 1) {
res.push(a[i] ^ b[i]);
}

return new Buffer(res).toString('base64');
return Buffer.from(res).toString('base64');
}

function H(method, text) {
Expand Down Expand Up @@ -280,7 +280,7 @@ ScramSHA.prototype.auth = function(server, connections, db, username, password,
var withoutProof = f('c=biws,r=%s', rnonce);
var saltedPassword = HI(
processedPassword,
new Buffer(salt, 'base64'),
Buffer.from(salt, 'base64'),
iterations,
cryptoMethod
);
Expand Down Expand Up @@ -316,7 +316,7 @@ ScramSHA.prototype.auth = function(server, connections, db, username, password,
const cmd = {
saslContinue: 1,
conversationId: r.result.conversationId,
payload: new Binary(new Buffer(clientFinal))
payload: new Binary(Buffer.from(clientFinal))
};

//
Expand All @@ -333,7 +333,7 @@ ScramSHA.prototype.auth = function(server, connections, db, username, password,
var cmd = {
saslContinue: 1,
conversationId: r.result.conversationId,
payload: new Buffer(0)
payload: Buffer.alloc(0)
};

// Write the commmand on the connection
Expand Down
6 changes: 3 additions & 3 deletions lib/connection/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Query.prototype.toBin = function() {
if (self.batchSize !== self.numberToReturn) self.numberToReturn = self.batchSize;

// Allocate write protocol header buffer
var header = new Buffer(
var header = Buffer.alloc(
4 * 4 + // Header
4 + // Flags
Buffer.byteLength(self.ns) +
Expand Down Expand Up @@ -247,7 +247,7 @@ GetMore.prototype.toBin = function() {
// Create command buffer
var index = 0;
// Allocate buffer
var _buffer = new Buffer(length);
var _buffer = Buffer.alloc(length);

// Write header information
// index = write32bit(index, _buffer, length);
Expand Down Expand Up @@ -332,7 +332,7 @@ KillCursor.prototype.toBin = function() {

// Create command buffer
var index = 0;
var _buffer = new Buffer(length);
var _buffer = Buffer.alloc(length);

// Write header information
// index = write32bit(index, _buffer, length);
Expand Down
20 changes: 10 additions & 10 deletions lib/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ var dataHandler = function(self) {
self.bytesRead = self.bytesRead + data.length;

// Reset state of buffer
data = new Buffer(0);
data = Buffer.alloc(0);
} else {
// Copy the missing part of the data into our current buffer
data.copy(self.buffer, self.bytesRead, 0, remainingBytesToRead);
Expand Down Expand Up @@ -374,7 +374,7 @@ var dataHandler = function(self) {
// If we have enough bytes to determine the message size let's do it
if (self.stubBuffer.length + data.length > 4) {
// Prepad the data
var newData = new Buffer(self.stubBuffer.length + data.length);
var newData = Buffer.alloc(self.stubBuffer.length + data.length);
self.stubBuffer.copy(newData, 0);
data.copy(newData, self.stubBuffer.length);
// Reassign for parsing
Expand All @@ -387,13 +387,13 @@ var dataHandler = function(self) {
self.stubBuffer = null;
} else {
// Add the the bytes to the stub buffer
var newStubBuffer = new Buffer(self.stubBuffer.length + data.length);
var newStubBuffer = Buffer.alloc(self.stubBuffer.length + data.length);
// Copy existing stub buffer
self.stubBuffer.copy(newStubBuffer, 0);
// Copy missing part of the data
data.copy(newStubBuffer, self.stubBuffer.length);
// Exit parsing loop
data = new Buffer(0);
data = Buffer.alloc(0);
}
} else {
if (data.length > 4) {
Expand Down Expand Up @@ -423,7 +423,7 @@ var dataHandler = function(self) {
sizeOfMessage < self.maxBsonMessageSize &&
sizeOfMessage > data.length
) {
self.buffer = new Buffer(sizeOfMessage);
self.buffer = Buffer.alloc(sizeOfMessage);
// Copy all the data into the buffer
data.copy(self.buffer, 0);
// Update bytes read
Expand All @@ -433,7 +433,7 @@ var dataHandler = function(self) {
// Ensure stub buffer is null
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
data = Buffer.alloc(0);
} else if (
sizeOfMessage > 4 &&
sizeOfMessage < self.maxBsonMessageSize &&
Expand All @@ -447,7 +447,7 @@ var dataHandler = function(self) {
self.bytesRead = 0;
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
data = Buffer.alloc(0);
// Emit the message
emitMessageHandler(self, emitBuffer);
} catch (err) {
Expand All @@ -474,7 +474,7 @@ var dataHandler = function(self) {
self.bytesRead = 0;
self.stubBuffer = null;
// Exit parsing loop
data = new Buffer(0);
data = Buffer.alloc(0);
} else {
emitBuffer = data.slice(0, sizeOfMessage);
// Reset state of buffer
Expand All @@ -489,11 +489,11 @@ var dataHandler = function(self) {
}
} else {
// Create a buffer that contains the space for the non-complete message
self.stubBuffer = new Buffer(data.length);
self.stubBuffer = Buffer.alloc(data.length);
// Copy the data to the stub buffer
data.copy(self.stubBuffer, 0);
// Exit parsing loop
data = new Buffer(0);
data = Buffer.alloc(0);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/connection/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,14 +1057,14 @@ var serializeCommands = function(self, commands, result, callback) {
if (err) return callback(err, null);

// Create the msgHeader of OP_COMPRESSED
var msgHeader = new Buffer(MESSAGE_HEADER_SIZE);
var msgHeader = Buffer.alloc(MESSAGE_HEADER_SIZE);
msgHeader.writeInt32LE(MESSAGE_HEADER_SIZE + 9 + compressedMessage.length, 0); // messageLength
msgHeader.writeInt32LE(thisCommand.requestId, 4); // requestID
msgHeader.writeInt32LE(0, 8); // responseTo (zero)
msgHeader.writeInt32LE(opcodes.OP_COMPRESSED, 12); // opCode

// Create the compression details of OP_COMPRESSED
var compressionDetails = new Buffer(9);
var compressionDetails = Buffer.alloc(9);
compressionDetails.writeInt32LE(originalCommandOpCode, 0); // originalOpcode
compressionDetails.writeInt32LE(messageToBeCompressed.length, 4); // Size of the uncompressed compressedMessage, excluding the MsgHeader
compressionDetails.writeUInt8(compressorIDs[self.options.agreedCompressor], 8); // compressorID
Expand Down
4 changes: 2 additions & 2 deletions lib/topologies/replset_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,8 @@ function addToList(self, type, ismaster, server, list) {
}

function compareObjectIds(id1, id2) {
var a = new Buffer(id1.toHexString(), 'hex');
var b = new Buffer(id2.toHexString(), 'hex');
var a = Buffer.from(id1.toHexString(), 'hex');
var b = Buffer.from(id2.toHexString(), 'hex');

if (a === b) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/topologies/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function createClientInfo(options) {
// Do we have an application specific string
if (options.appname) {
// Cut at 128 bytes
var buffer = new Buffer(options.appname);
var buffer = Buffer.from(options.appname);
// Return the truncated appname
var appname = buffer.length > 128 ? buffer.slice(0, 128).toString('utf8') : options.appname;
// Add to the clientInfo
Expand Down
4 changes: 2 additions & 2 deletions test/tests/functional/server_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ describe('Server tests', function() {

// Write garbage, force socket closure
try {
var a = new Buffer(100);
var a = Buffer.alloc(100);
for (var i = 0; i < 100; i++) a[i] = i;
result.connection.write(a);
} catch (loopErr) {
Expand Down Expand Up @@ -649,7 +649,7 @@ describe('Server tests', function() {

// Write garbage, force socket closure
try {
var a = new Buffer(100);
var a = Buffer.alloc(100);
for (var i = 0; i < 100; i++) a[i] = i;
result.connection.write(a);
} catch (garbageErr) {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/unit/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function genClusterTime(time) {
return {
clusterTime: new Timestamp(time),
signature: {
hash: new Binary(new Buffer('testing')),
hash: new Binary(Buffer.from('testing')),
keyId: 42
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/tests/unit/scram_iterations_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('SCRAM Iterations Tests', function() {
return request.reply({
ok: 1,
done: false,
payload: new Buffer(scramResponse)
payload: Buffer.from(scramResponse)
});
} else if (doc.saslContinue) {
done('SHOULD NOT BE HERE');
Expand Down

0 comments on commit 7c71e19

Please sign in to comment.