Skip to content

Commit 0a0684d

Browse files
committed
replace new Buffer(string) and new Buffer(array) by bufferFrom
1 parent de4d1a9 commit 0a0684d

File tree

8 files changed

+32
-25
lines changed

8 files changed

+32
-25
lines changed

lib/client.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter;
1111
var util = require('util');
1212
var pgPass = require('pgpass');
1313
var TypeOverrides = require('./type-overrides');
14+
var bufferFrom = require('buffer-from');
1415

1516
var ConnectionParameters = require('./connection-parameters');
1617
var Query = require('./query');
@@ -93,7 +94,7 @@ Client.prototype.connect = function(callback) {
9394
//password request handling
9495
con.on('authenticationMD5Password', checkPgPass(function(msg) {
9596
var inner = Client.md5(self.password + self.user);
96-
var outer = Client.md5(Buffer.concat([new Buffer(inner), msg.salt]));
97+
var outer = Client.md5(Buffer.concat([bufferFrom(inner), msg.salt]));
9798
var md5password = "md5" + outer;
9899
con.password(md5password);
99100
}));

test/buffer-list.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var allocUnsafe = require('buffer-alloc-unsafe');
2+
var bufferFrom = require('buffer-from');
23

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

1314
p.addInt16 = function(val, front) {
14-
return this.add(new Buffer([(val >>> 8), (val >>> 0)]), front);
15+
return this.add(bufferFrom([(val >>> 8), (val >>> 0)]), front);
1516
};
1617

1718
p.getByteLength = function(initial) {
@@ -21,7 +22,7 @@ p.getByteLength = function(initial) {
2122
};
2223

2324
p.addInt32 = function(val, first) {
24-
return this.add(new Buffer([
25+
return this.add(bufferFrom([
2526
(val >>> 24 & 0xFF),
2627
(val >>> 16 & 0xFF),
2728
(val >>> 8 & 0xFF),
@@ -38,7 +39,7 @@ p.addCString = function(val, front) {
3839
};
3940

4041
p.addChar = function(char, first) {
41-
return this.add(new Buffer(char, 'utf8'), first);
42+
return this.add(bufferFrom(char, 'utf8'), first);
4243
};
4344

4445
p.join = function(appendLength, char) {

test/integration/gh-issues/675-tests.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var helper = require('../test-helper');
22
var assert = require('assert');
3+
var bufferFrom = require('buffer-from');
34

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

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

14-
var body = new Buffer('foo');
15+
var body = bufferFrom('foo');
1516
client.query(c, [body], function(err) {
1617
if (err) throw err;
1718

18-
body = new Buffer([]);
19+
body = bufferFrom([]);
1920
client.query(c, [body], function(err, res) {
2021
done();
2122

test/test-buffers.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
require(__dirname+'/test-helper');
2+
var bufferFrom = require('buffer-from');
23
//http://developer.postgresql.org/pgdocs/postgres/protocol-message-formats.html
34

45
var buffers = {};
56
buffers.readyForQuery = function() {
67
return new BufferList()
7-
.add(new Buffer('I'))
8+
.add(bufferFrom('I'))
89
.join(true,'Z');
910
};
1011

@@ -23,7 +24,7 @@ buffers.authenticationCleartextPassword = function() {
2324
buffers.authenticationMD5Password = function() {
2425
return new BufferList()
2526
.addInt32(5)
26-
.add(new Buffer([1, 2, 3, 4]))
27+
.add(bufferFrom([1, 2, 3, 4]))
2728
.join(true, 'R');
2829
};
2930

@@ -71,7 +72,7 @@ buffers.dataRow = function(columns) {
7172
if(col == null) {
7273
buf.addInt32(-1);
7374
} else {
74-
var strBuf = new Buffer(col, 'utf8');
75+
var strBuf = bufferFrom(col, 'utf8');
7576
buf.addInt32(strBuf.length);
7677
buf.add(strBuf);
7778
}
@@ -94,7 +95,7 @@ var errorOrNotice = function(fields) {
9495
buf.addChar(field.type);
9596
buf.addCString(field.value);
9697
});
97-
return buf.add(new Buffer([0]));//terminator
98+
return buf.add(bufferFrom([0]));//terminator
9899
}
99100

100101
buffers.parseComplete = function() {

test/unit/client/md5-password-tests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require(__dirname + '/test-helper');
2+
var bufferFrom = require('buffer-from');
23

34
test('md5 authentication', function() {
45
var client = createClient();
56
client.password = "!";
6-
var salt = new Buffer([1, 2, 3, 4]);
7+
var salt = bufferFrom([1, 2, 3, 4]);
78
client.connection.emit('authenticationMD5Password', {salt: salt});
89

910
test('responds', function() {

test/unit/connection/inbound-parser-tests.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require(__dirname+'/test-helper');
22
return false;
33
var allocUnsafe = require('buffer-alloc-unsafe');
4+
var bufferFrom = require('buffer-from');
45
var Connection = require(__dirname + '/../../../lib/connection');
56
var buffers = require(__dirname + '/../../test-buffers');
67
var PARSE = function(buffer) {
@@ -160,7 +161,7 @@ test('Connection', function() {
160161
testForMessage(plainPasswordBuffer, expectedPlainPasswordMessage);
161162
var msg = testForMessage(md5PasswordBuffer, expectedMD5PasswordMessage);
162163
test('md5 has right salt', function() {
163-
assert.equalBuffers(msg.salt, new Buffer([1, 2, 3, 4]));
164+
assert.equalBuffers(msg.salt, bufferFrom([1, 2, 3, 4]));
164165
});
165166
testForMessage(paramStatusBuffer, expectedParameterStatusMessage);
166167
testForMessage(backendKeyDataBuffer, expectedBackendKeyDataMessage);
@@ -175,7 +176,7 @@ test('Connection', function() {
175176
});
176177

177178
test("no data message", function() {
178-
testForMessage(new Buffer([0x6e, 0, 0, 0, 4]), {
179+
testForMessage(bufferFrom([0x6e, 0, 0, 0, 4]), {
179180
name: 'noData'
180181
});
181182
});

test/unit/connection/outbound-sending-tests.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require(__dirname + "/test-helper");
2+
var bufferFrom = require('buffer-from');
23
var Connection = require(__dirname + '/../../../lib/connection');
34
var stream = new MemoryStream();
45
var con = new Connection({
@@ -104,12 +105,12 @@ test('bind messages', function() {
104105
.addInt16(0)
105106
.addInt16(4)
106107
.addInt32(1)
107-
.add(new Buffer("1"))
108+
.add(bufferFrom("1"))
108109
.addInt32(2)
109-
.add(new Buffer("hi"))
110+
.add(bufferFrom("hi"))
110111
.addInt32(-1)
111112
.addInt32(4)
112-
.add(new Buffer('zing'))
113+
.add(bufferFrom('zing'))
113114
.addInt16(0)
114115
.join(true, 'B');
115116
assert.received(stream, expectedBuffer);
@@ -120,7 +121,7 @@ test('with named statement, portal, and buffer value', function() {
120121
con.bind({
121122
portal: 'bang',
122123
statement: 'woo',
123-
values: ['1', 'hi', null, new Buffer('zing', 'UTF-8')]
124+
values: ['1', 'hi', null, bufferFrom('zing', 'UTF-8')]
124125
});
125126
var expectedBuffer = new BufferList()
126127
.addCString('bang') //portal name
@@ -132,12 +133,12 @@ test('with named statement, portal, and buffer value', function() {
132133
.addInt16(1)//binary
133134
.addInt16(4)
134135
.addInt32(1)
135-
.add(new Buffer("1"))
136+
.add(bufferFrom("1"))
136137
.addInt32(2)
137-
.add(new Buffer("hi"))
138+
.add(bufferFrom("hi"))
138139
.addInt32(-1)
139140
.addInt32(4)
140-
.add(new Buffer('zing', 'UTF-8'))
141+
.add(bufferFrom('zing', 'UTF-8'))
141142
.addInt16(0)
142143
.join(true, 'B');
143144
assert.received(stream, expectedBuffer);
@@ -181,7 +182,7 @@ test('sends sync command', function() {
181182

182183
test('sends end command', function() {
183184
con.end();
184-
var expected = new Buffer([0x58, 0, 0, 0, 4]);
185+
var expected = bufferFrom([0x58, 0, 0, 0, 4]);
185186
assert.received(stream, expected);
186187
});
187188

test/unit/utils-tests.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var helper = require(__dirname + '/test-helper');
22
var utils = require(__dirname + "/../../lib/utils");
33
var defaults = require(__dirname + "/../../lib").defaults;
4-
4+
var bufferFrom = require('buffer-from');
55

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

5252
test('prepareValues: buffer prepared properly', function() {
53-
var buf = new Buffer("quack");
53+
var buf = bufferFrom("quack");
5454
var out = utils.prepareValue(buf);
5555
assert.strictEqual(buf, out);
5656
});
@@ -142,7 +142,7 @@ test('prepareValue: objects with simple toPostgres prepared properly', function(
142142
});
143143

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

167167
test('prepareValue: objects with circular toPostgres rejected', function() {
168-
var buf = new Buffer("zomgcustom!");
168+
var buf = bufferFrom("zomgcustom!");
169169
var customType = {
170170
toPostgres: function() {
171171
return { toPostgres: function () { return customType; } };

0 commit comments

Comments
 (0)