Skip to content

Commit

Permalink
replace new Buffer(string) and new Buffer(array) by bufferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Dec 28, 2016
1 parent de4d1a9 commit 0a0684d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter;
var util = require('util');
var pgPass = require('pgpass');
var TypeOverrides = require('./type-overrides');
var bufferFrom = require('buffer-from');

var ConnectionParameters = require('./connection-parameters');
var Query = require('./query');
Expand Down Expand Up @@ -93,7 +94,7 @@ Client.prototype.connect = function(callback) {
//password request handling
con.on('authenticationMD5Password', checkPgPass(function(msg) {
var inner = Client.md5(self.password + self.user);
var outer = Client.md5(Buffer.concat([new Buffer(inner), msg.salt]));
var outer = Client.md5(Buffer.concat([bufferFrom(inner), msg.salt]));
var md5password = "md5" + outer;
con.password(md5password);
}));
Expand Down
7 changes: 4 additions & 3 deletions test/buffer-list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var allocUnsafe = require('buffer-alloc-unsafe');
var bufferFrom = require('buffer-from');

BufferList = function() {
this.buffers = [];
Expand All @@ -11,7 +12,7 @@ p.add = function(buffer, front) {
};

p.addInt16 = function(val, front) {
return this.add(new Buffer([(val >>> 8), (val >>> 0)]), front);
return this.add(bufferFrom([(val >>> 8), (val >>> 0)]), front);
};

p.getByteLength = function(initial) {
Expand All @@ -21,7 +22,7 @@ p.getByteLength = function(initial) {
};

p.addInt32 = function(val, first) {
return this.add(new Buffer([
return this.add(bufferFrom([
(val >>> 24 & 0xFF),
(val >>> 16 & 0xFF),
(val >>> 8 & 0xFF),
Expand All @@ -38,7 +39,7 @@ p.addCString = function(val, front) {
};

p.addChar = function(char, first) {
return this.add(new Buffer(char, 'utf8'), first);
return this.add(bufferFrom(char, 'utf8'), first);
};

p.join = function(appendLength, char) {
Expand Down
5 changes: 3 additions & 2 deletions test/integration/gh-issues/675-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var helper = require('../test-helper');
var assert = require('assert');
var bufferFrom = require('buffer-from');

helper.pg.connect(helper.config, function(err, client, done) {
if (err) throw err;
Expand All @@ -11,11 +12,11 @@ helper.pg.connect(helper.config, function(err, client, done) {

c = 'INSERT INTO posts (body) VALUES ($1) RETURNING *';

var body = new Buffer('foo');
var body = bufferFrom('foo');
client.query(c, [body], function(err) {
if (err) throw err;

body = new Buffer([]);
body = bufferFrom([]);
client.query(c, [body], function(err, res) {
done();

Expand Down
9 changes: 5 additions & 4 deletions test/test-buffers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require(__dirname+'/test-helper');
var bufferFrom = require('buffer-from');
//http://developer.postgresql.org/pgdocs/postgres/protocol-message-formats.html

var buffers = {};
buffers.readyForQuery = function() {
return new BufferList()
.add(new Buffer('I'))
.add(bufferFrom('I'))
.join(true,'Z');
};

Expand All @@ -23,7 +24,7 @@ buffers.authenticationCleartextPassword = function() {
buffers.authenticationMD5Password = function() {
return new BufferList()
.addInt32(5)
.add(new Buffer([1, 2, 3, 4]))
.add(bufferFrom([1, 2, 3, 4]))
.join(true, 'R');
};

Expand Down Expand Up @@ -71,7 +72,7 @@ buffers.dataRow = function(columns) {
if(col == null) {
buf.addInt32(-1);
} else {
var strBuf = new Buffer(col, 'utf8');
var strBuf = bufferFrom(col, 'utf8');
buf.addInt32(strBuf.length);
buf.add(strBuf);
}
Expand All @@ -94,7 +95,7 @@ var errorOrNotice = function(fields) {
buf.addChar(field.type);
buf.addCString(field.value);
});
return buf.add(new Buffer([0]));//terminator
return buf.add(bufferFrom([0]));//terminator
}

buffers.parseComplete = function() {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/client/md5-password-tests.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require(__dirname + '/test-helper');
var bufferFrom = require('buffer-from');

test('md5 authentication', function() {
var client = createClient();
client.password = "!";
var salt = new Buffer([1, 2, 3, 4]);
var salt = bufferFrom([1, 2, 3, 4]);
client.connection.emit('authenticationMD5Password', {salt: salt});

test('responds', function() {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/connection/inbound-parser-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require(__dirname+'/test-helper');
return false;
var allocUnsafe = require('buffer-alloc-unsafe');
var bufferFrom = require('buffer-from');
var Connection = require(__dirname + '/../../../lib/connection');
var buffers = require(__dirname + '/../../test-buffers');
var PARSE = function(buffer) {
Expand Down Expand Up @@ -160,7 +161,7 @@ test('Connection', function() {
testForMessage(plainPasswordBuffer, expectedPlainPasswordMessage);
var msg = testForMessage(md5PasswordBuffer, expectedMD5PasswordMessage);
test('md5 has right salt', function() {
assert.equalBuffers(msg.salt, new Buffer([1, 2, 3, 4]));
assert.equalBuffers(msg.salt, bufferFrom([1, 2, 3, 4]));
});
testForMessage(paramStatusBuffer, expectedParameterStatusMessage);
testForMessage(backendKeyDataBuffer, expectedBackendKeyDataMessage);
Expand All @@ -175,7 +176,7 @@ test('Connection', function() {
});

test("no data message", function() {
testForMessage(new Buffer([0x6e, 0, 0, 0, 4]), {
testForMessage(bufferFrom([0x6e, 0, 0, 0, 4]), {
name: 'noData'
});
});
Expand Down
17 changes: 9 additions & 8 deletions test/unit/connection/outbound-sending-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require(__dirname + "/test-helper");
var bufferFrom = require('buffer-from');
var Connection = require(__dirname + '/../../../lib/connection');
var stream = new MemoryStream();
var con = new Connection({
Expand Down Expand Up @@ -104,12 +105,12 @@ test('bind messages', function() {
.addInt16(0)
.addInt16(4)
.addInt32(1)
.add(new Buffer("1"))
.add(bufferFrom("1"))
.addInt32(2)
.add(new Buffer("hi"))
.add(bufferFrom("hi"))
.addInt32(-1)
.addInt32(4)
.add(new Buffer('zing'))
.add(bufferFrom('zing'))
.addInt16(0)
.join(true, 'B');
assert.received(stream, expectedBuffer);
Expand All @@ -120,7 +121,7 @@ test('with named statement, portal, and buffer value', function() {
con.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, new Buffer('zing', 'UTF-8')]
values: ['1', 'hi', null, bufferFrom('zing', 'UTF-8')]
});
var expectedBuffer = new BufferList()
.addCString('bang') //portal name
Expand All @@ -132,12 +133,12 @@ test('with named statement, portal, and buffer value', function() {
.addInt16(1)//binary
.addInt16(4)
.addInt32(1)
.add(new Buffer("1"))
.add(bufferFrom("1"))
.addInt32(2)
.add(new Buffer("hi"))
.add(bufferFrom("hi"))
.addInt32(-1)
.addInt32(4)
.add(new Buffer('zing', 'UTF-8'))
.add(bufferFrom('zing', 'UTF-8'))
.addInt16(0)
.join(true, 'B');
assert.received(stream, expectedBuffer);
Expand Down Expand Up @@ -181,7 +182,7 @@ test('sends sync command', function() {

test('sends end command', function() {
con.end();
var expected = new Buffer([0x58, 0, 0, 0, 4]);
var expected = bufferFrom([0x58, 0, 0, 0, 4]);
assert.received(stream, expected);
});

Expand Down
8 changes: 4 additions & 4 deletions test/unit/utils-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var helper = require(__dirname + '/test-helper');
var utils = require(__dirname + "/../../lib/utils");
var defaults = require(__dirname + "/../../lib").defaults;

var bufferFrom = require('buffer-from');

test('ensure types is exported on root object', function() {
var pg = require('../../lib')
Expand Down Expand Up @@ -50,7 +50,7 @@ test('normalizing query configs', function() {
})

test('prepareValues: buffer prepared properly', function() {
var buf = new Buffer("quack");
var buf = bufferFrom("quack");
var out = utils.prepareValue(buf);
assert.strictEqual(buf, out);
});
Expand Down Expand Up @@ -142,7 +142,7 @@ test('prepareValue: objects with simple toPostgres prepared properly', function(
});

test('prepareValue: objects with complex toPostgres prepared properly', function() {
var buf = new Buffer("zomgcustom!");
var buf = bufferFrom("zomgcustom!");
var customType = {
toPostgres: function() {
return [1, 2];
Expand All @@ -165,7 +165,7 @@ test('prepareValue: objects with toPostgres receive prepareValue', function() {
});

test('prepareValue: objects with circular toPostgres rejected', function() {
var buf = new Buffer("zomgcustom!");
var buf = bufferFrom("zomgcustom!");
var customType = {
toPostgres: function() {
return { toPostgres: function () { return customType; } };
Expand Down

0 comments on commit 0a0684d

Please sign in to comment.