Skip to content

Commit

Permalink
Replaced Buffer() with .alloc and .from : #321
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 29, 2018
1 parent 6a6f74f commit a1a0eeb
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/bag.js
Expand Up @@ -71,7 +71,7 @@ Object.defineProperties(Bag.prototype, {
* Parse the bag content.
*/
Bag.prototype._parse = function () {
var buffer = new Buffer(this.serialized, 'base64'),
var buffer = Buffer.from(this.serialized, 'base64'),
mode = buffer.readUInt8(0);

if ((mode & 1) === 1) {
Expand Down
6 changes: 3 additions & 3 deletions lib/client/network/conn.js
Expand Up @@ -245,7 +245,7 @@ Connection.prototype.negotiateProtocol = function() {
if (data.length > 2) {
process.nextTick(
function() {
var remainingData = new Buffer(data.length - 2);
var remainingData = Buffer.alloc(data.length - 2);
data.copy(remainingData, 0, 2);
if (remainingData.length) {
this.handleSocketData(remainingData);
Expand Down Expand Up @@ -332,7 +332,7 @@ Connection.prototype.bindToSocket = function() {
Connection.prototype.handleSocketData = function(data) {
var buffer, result, offset, processed, lastOp;
if (this.remaining) {
buffer = new Buffer(this.remaining.length + data.length);
buffer = Buffer.alloc(this.remaining.length + data.length);
this.remaining.copy(buffer);
data.copy(buffer, this.remaining.length);
} else {
Expand All @@ -349,7 +349,7 @@ Connection.prototype.handleSocketData = function(data) {

// TODO refactor. In case of pending buffers but not Reading the precess should continue on the remaining Buffer
if (lastOp != OperationStatus.READING) {
this.handleSocketData(new Buffer(0));
this.handleSocketData( Buffer.alloc(0));
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/client/network/protocol37/operation-queue.js
Expand Up @@ -116,7 +116,7 @@ OperationQueue.prototype.unbindFromSocket = function () {
OperationQueue.prototype.handleChunk = function (data) {
var buffer, result, offset;
if (this.remaining) {
buffer = new Buffer(this.remaining.length + data.length);
buffer = Buffer.alloc(this.remaining.length + data.length);
this.remaining.copy(buffer);
data.copy(buffer, this.remaining.length);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/client/network/protocol37/operation.js
Expand Up @@ -85,7 +85,7 @@ Operation.prototype.buffer = function() {
size += item[0];
}

buffer = new Buffer(size);
buffer = Buffer.alloc(size);

for (i = 0; i < total; i++) {
item = commands[i];
Expand Down Expand Up @@ -305,7 +305,7 @@ Operation.prototype.writeVarintString = function(data) {
};

function encodeString(data) {
return new Buffer(data);
return Buffer.from(data);
}

// # Read Operations
Expand Down Expand Up @@ -591,7 +591,7 @@ Operation.prototype.parseLivePush = function(buffer, offset, result) {
offset += 1;
let length = buffer.readInt32BE(offset);
offset += 4;
let recordBuffer = new Buffer(length);
let recordBuffer = Buffer.alloc(length);
buffer.copy(recordBuffer, 0, offset, offset + length);
offset += length;
event.data = deserializer.deserialize(
Expand All @@ -604,7 +604,7 @@ Operation.prototype.parseLivePush = function(buffer, offset, result) {
offset += 1;
length = buffer.readInt32BE(offset);
offset += 4;
recordBuffer = new Buffer(length);
recordBuffer = Buffer.alloc(length);
buffer.copy(recordBuffer, 0, offset, offset + length);
offset += length;
event.before = deserializer.deserialize(
Expand Down Expand Up @@ -1283,7 +1283,7 @@ Operation.prototype.parsePushedData = function(
let length = buffer.readInt32BE(offset);
offset += constants.BYTES_INT;

var recordBuffer = new Buffer(length);
var recordBuffer = Buffer.alloc(length);
buffer.copy(recordBuffer, 0, offset, offset + length);
let hosts = [];
for (let i = 0; i < length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion lib/client/network/protocol37/operations/connect.js
Expand Up @@ -10,7 +10,7 @@ module.exports = Operation.extend({
this
.writeByte(this.opCode)
.writeInt(-1)
.writeBytes(new Buffer(0))
.writeBytes(Buffer.alloc(0))
.writeString(this.data.username)
.writeString(this.data.password);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/client/network/protocol37/operations/db-open.js
Expand Up @@ -9,7 +9,7 @@ module.exports = Operation.extend({
this
.writeByte(this.opCode)
.writeInt(-1)
.writeBytes(new Buffer(0))
.writeBytes(Buffer.alloc(0))
.writeString(this.data.name)
.writeString(this.data.username)
.writeString(this.data.password);
Expand Down
2 changes: 1 addition & 1 deletion lib/client/network/protocol37/operations/query.js
Expand Up @@ -47,7 +47,7 @@ module.exports = Operation.extend({
}
content = serializer.serializeDocument(doc);
} else {
content = new Buffer(0);
content = Buffer.alloc(0);
}
return content;
},
Expand Down
Expand Up @@ -22,7 +22,7 @@ module.exports = Operation.extend({
};
content = serializer.serializeDocument(doc);
} else {
content = new Buffer(0);
content = Buffer.alloc(0);
}
return content;
},
Expand Down
14 changes: 7 additions & 7 deletions lib/client/network/protocol37/writer.js
Expand Up @@ -12,7 +12,7 @@ var Long = require('../../../long').Long,
* @return {Buffer} The buffer containing the data.
*/
function writeByte (data) {
return new Buffer([data]);
return Buffer.from([data]);
}

/**
Expand All @@ -22,7 +22,7 @@ function writeByte (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeInt (data) {
var buf = new Buffer(constants.BYTES_INT);
var buf = Buffer.alloc(constants.BYTES_INT);
buf.writeInt32BE(data, 0);
return buf;
}
Expand All @@ -34,7 +34,7 @@ function writeInt (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeLong (data) {
var buf = new Buffer(constants.BYTES_LONG),
var buf = Buffer.alloc(constants.BYTES_LONG),
value = Long.fromNumber(data);

buf.fill(0);
Expand All @@ -51,7 +51,7 @@ function writeLong (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeShort (data) {
var buf = new Buffer(constants.BYTES_SHORT);
var buf = Buffer.alloc(constants.BYTES_SHORT);
buf.writeInt16BE(data, 0);
return buf;
}
Expand All @@ -63,7 +63,7 @@ function writeShort (data) {
*/
function writeBytes (data) {
var length = data.length,
buf = new Buffer(constants.BYTES_INT + length);
buf = Buffer.alloc(constants.BYTES_INT + length);
buf.writeInt32BE(length, 0);
data.copy(buf, constants.BYTES_INT);
return buf;
Expand All @@ -81,14 +81,14 @@ function writeString (data) {
}
var stringBuf = encodeString(data),
length = stringBuf.length,
buf = new Buffer(constants.BYTES_INT + length);
buf = Buffer.alloc(constants.BYTES_INT + length);
buf.writeInt32BE(length, 0);
stringBuf.copy(buf, constants.BYTES_INT, 0, stringBuf.length);
return buf;
}

function encodeString (data) {
return new Buffer(data);
return Buffer.from(data);
}

exports.writeByte = writeByte;
Expand Down
6 changes: 3 additions & 3 deletions lib/transport/binary/connection.js
Expand Up @@ -244,7 +244,7 @@ Connection.prototype.negotiateProtocol = function () {

if (data.length > 2) {
process.nextTick(function () {
var remainingData = new Buffer(data.length - 2);
var remainingData = Buffer.alloc(data.length - 2);
data.copy(remainingData, 0, 2);
if (remainingData.length) {
this.handleSocketData(remainingData);
Expand Down Expand Up @@ -319,7 +319,7 @@ Connection.prototype.handleSocketData = function (data) {
if (this.remaining) {


buffer = new Buffer(this.remaining.length + data.length);
buffer = Buffer.alloc(this.remaining.length + data.length);
this.remaining.copy(buffer);
data.copy(buffer, this.remaining.length);
}
Expand All @@ -338,7 +338,7 @@ Connection.prototype.handleSocketData = function (data) {
this.remaining = buffer.slice(offset);
// TODO refactor. In case of pending buffers but not Reading the precess should continue on the remaining Buffer
if (lastOp != OperationStatus.READING) {
this.handleSocketData(new Buffer(0));
this.handleSocketData(Buffer.alloc(0));
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/binary/protocol33/operation-queue.js
Expand Up @@ -116,7 +116,7 @@ OperationQueue.prototype.unbindFromSocket = function () {
OperationQueue.prototype.handleChunk = function (data) {
var buffer, result, offset;
if (this.remaining) {
buffer = new Buffer(this.remaining.length + data.length);
buffer = Buffer.alloc(this.remaining.length + data.length);
this.remaining.copy(buffer);
data.copy(buffer, this.remaining.length);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/transport/binary/protocol33/operation.js
Expand Up @@ -84,7 +84,7 @@ Operation.prototype.buffer = function () {
size += item[0];
}

buffer = new Buffer(size);
buffer = Buffer.alloc(size);

for (i = 0; i < total; i++) {
item = commands[i];
Expand Down Expand Up @@ -237,7 +237,7 @@ Operation.prototype.writeString = function (data) {
};

function encodeString(data) {
return new Buffer(data);
return Buffer.from(data);
}

// # Read Operations
Expand Down Expand Up @@ -419,7 +419,7 @@ Operation.prototype.consume = function (buffer, offset) {
offset += 8;
var contentLength = buffer.readInt32BE(offset);
offset += 4;
var recordBuffer = new Buffer(contentLength);
var recordBuffer = Buffer.alloc(contentLength);
buffer.copy(recordBuffer, 0, offset, offset + contentLength);
var content = deserializer.deserialize(recordBuffer, this.data.transformerFunctions);

Expand Down Expand Up @@ -962,7 +962,7 @@ Operation.prototype.parsePushedData = function (buffer, offset, context, fieldNa
//asString = buffer.toString('utf8', offset, offset + length);


var recordBuffer = new Buffer(length);
var recordBuffer = Buffer.alloc(length);
buffer.copy(recordBuffer, 0, offset, offset + length);

context[fieldName] = deserializer.deserialize(recordBuffer, this.data.transformerFunctions);
Expand Down
14 changes: 7 additions & 7 deletions lib/transport/binary/protocol33/writer.js
Expand Up @@ -12,7 +12,7 @@ var Long = require('../../../long').Long,
* @return {Buffer} The buffer containing the data.
*/
function writeByte (data) {
return new Buffer([data]);
return Buffer.from([data]);
}

/**
Expand All @@ -22,7 +22,7 @@ function writeByte (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeInt (data) {
var buf = new Buffer(constants.BYTES_INT);
var buf = Buffer.alloc(constants.BYTES_INT);
buf.writeInt32BE(data, 0);
return buf;
}
Expand All @@ -34,7 +34,7 @@ function writeInt (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeLong (data) {
var buf = new Buffer(constants.BYTES_LONG),
var buf = Buffer.alloc(constants.BYTES_LONG),
value = Long.fromNumber(data);

buf.fill(0);
Expand All @@ -51,7 +51,7 @@ function writeLong (data) {
* @return {Buffer} The buffer containing the data.
*/
function writeShort (data) {
var buf = new Buffer(constants.BYTES_SHORT);
var buf = Buffer.alloc(constants.BYTES_SHORT);
buf.writeInt16BE(data, 0);
return buf;
}
Expand All @@ -63,7 +63,7 @@ function writeShort (data) {
*/
function writeBytes (data) {
var length = data.length,
buf = new Buffer(constants.BYTES_INT + length);
buf = Buffer.alloc(constants.BYTES_INT + length);
buf.writeInt32BE(length, 0);
data.copy(buf, constants.BYTES_INT);
return buf;
Expand All @@ -81,14 +81,14 @@ function writeString (data) {
}
var stringBuf = encodeString(data),
length = stringBuf.length,
buf = new Buffer(constants.BYTES_INT + length);
buf = Buffer.alloc(constants.BYTES_INT + length);
buf.writeInt32BE(length, 0);
stringBuf.copy(buf, constants.BYTES_INT, 0, stringBuf.length);
return buf;
}

function encodeString (data) {
return new Buffer(data);
return Buffer.from(data);
}

exports.writeByte = writeByte;
Expand Down
2 changes: 1 addition & 1 deletion test/database/database-auto-tx-test.js
Expand Up @@ -5,7 +5,7 @@ var Transaction = require("../../lib/db/transaction"),

describe("ODatabase API - Transaction", function() {
function createBinaryRecord(text) {
var record = new Buffer(text);
var record = Buffer.from(text);
record["@type"] = "b";
record["@class"] = "V";
return record;
Expand Down
2 changes: 1 addition & 1 deletion test/database/database-transaction-test.js
Expand Up @@ -5,7 +5,7 @@ var Transaction = require("../../lib/db/transaction"),

describe("ODatabase API - Transaction", function() {
function createBinaryRecord(text) {
var record = new Buffer(text);
var record = Buffer.from(text);
record["@type"] = "b";
record["@class"] = "V";
return record;
Expand Down
2 changes: 1 addition & 1 deletion test/db/transaction-test.js
Expand Up @@ -4,7 +4,7 @@ var Transaction = require('../../lib/db/transaction'),

describe("Database API - Transaction", function () {
function createBinaryRecord(text) {
var record = new Buffer(text);
var record = Buffer.from(text);
record['@type'] = 'b';
record['@class'] = 'V';
return record;
Expand Down
4 changes: 2 additions & 2 deletions test/transport/binary/protocol19/deserializer-test.js
Expand Up @@ -261,14 +261,14 @@ describe("Deserializer", function () {
});
describe('eatBinary()', function () {
it('should eat a binary field', function () {
var input = new Buffer('Hello World', 'utf8');
var input = Buffer.from('Hello World', 'utf8');
var parsed = deserializer.eatBinary(input.toString('base64') + '_');
parsed[0].should.be.instanceOf(Buffer);
parsed[0].should.eql(input);
parsed[1].length.should.equal(0);
});
it('should eat an empty binary field', function () {
var input = new Buffer('', 'utf8');
var input = Buffer.from('', 'utf8');
var parsed = deserializer.eatBinary(input.toString('base64') + '_');
parsed[0].should.be.instanceOf(Buffer);
parsed[0].should.eql(input);
Expand Down
6 changes: 3 additions & 3 deletions test/transport/binary/protocol19/operation-test.js
Expand Up @@ -36,7 +36,7 @@ describe('Operation', function () {
});

it('should contain the correct contents', function () {
var expected = Buffer(25);
var expected = Buffer.alloc(25);
expected[0] = 1;
expected.writeInt32BE(123, 1);
expected.writeInt32BE(12, 5);
Expand Down Expand Up @@ -124,10 +124,10 @@ describe('Operation', function () {
});
describe('Operation::consume() with exhausted buffers', function () {
beforeEach(function () {
this.first = new Buffer(5);
this.first = Buffer.alloc(5);
this.first[0] = 1; // opode
this.first.writeInt32BE(123, 1); // num
this.last = new Buffer(26);
this.last = Buffer.alloc(26);
this.last.writeInt32BE(12, 0); // string length
this.last.write('Hello World!', 4); // string content
this.last.writeInt16BE(5, 16); // shorts
Expand Down

0 comments on commit a1a0eeb

Please sign in to comment.