Skip to content

Commit 934ca3a

Browse files
charmanderbrianc
authored andcommittedJun 8, 2017
Remove fallbacks for unsupported Node versions (#1304)
* Remove unsupported Node versions 0.10 and 0.12 from CI * Replace deprecated Buffer constructor with .from/.alloc * Remove Promise polyfill * Make use of Object.assign * Remove checks for versions of Node earlier than 4 * Remove Buffer#indexOf fallback for Node 0.10
1 parent 3757ff7 commit 934ca3a

16 files changed

+65
-106
lines changed
 

‎.travis.yml

-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ addons:
1212

1313
matrix:
1414
include:
15-
- node_js: "0.10"
16-
addons:
17-
postgresql: "9.6"
18-
env: []
19-
- node_js: "0.12"
20-
addons:
21-
postgresql: "9.6"
22-
env: []
2315
- node_js: "4"
2416
addons:
2517
postgresql: "9.6"

‎lib/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Client.prototype.connect = function(callback) {
9494
//password request handling
9595
con.on('authenticationMD5Password', checkPgPass(function(msg) {
9696
var inner = Client.md5(self.password + self.user);
97-
var outer = Client.md5(Buffer.concat([new Buffer(inner), msg.salt]));
97+
var outer = Client.md5(Buffer.concat([Buffer.from(inner), msg.salt]));
9898
var md5password = "md5" + outer;
9999
con.password(md5password);
100100
}));

‎lib/connection.js

+3-18
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,6 @@ var util = require('util');
1313
var Writer = require('buffer-writer');
1414
var Reader = require('packet-reader');
1515

16-
var indexOf =
17-
'indexOf' in Buffer.prototype ?
18-
function indexOf(buffer, value, start) {
19-
return buffer.indexOf(value, start);
20-
} :
21-
function indexOf(buffer, value, start) {
22-
for (var i = start, len = buffer.length; i < len; i++) {
23-
if (buffer[i] === value) {
24-
return i;
25-
}
26-
}
27-
28-
return -1;
29-
};
30-
3116
var TEXT_MODE = 0;
3217
var BINARY_MODE = 1;
3318
var Connection = function(config) {
@@ -311,7 +296,7 @@ Connection.prototype.execute = function(config, more) {
311296
this._send(0x45, more);
312297
};
313298

314-
var emptyBuffer = Buffer(0);
299+
var emptyBuffer = Buffer.alloc(0);
315300

316301
Connection.prototype.flush = function() {
317302
//0x48 = 'H'
@@ -450,7 +435,7 @@ Connection.prototype.parseR = function(buffer, length) {
450435
code = this.parseInt32(buffer);
451436
if(code === 5) { //md5 required
452437
msg.name = 'authenticationMD5Password';
453-
msg.salt = new Buffer(4);
438+
msg.salt = Buffer.alloc(4);
454439
buffer.copy(msg.salt, 0, this.offset, this.offset + 4);
455440
this.offset += 4;
456441
return msg;
@@ -665,7 +650,7 @@ Connection.prototype.readBytes = function(buffer, length) {
665650

666651
Connection.prototype.parseCString = function(buffer) {
667652
var start = this.offset;
668-
var end = indexOf(buffer, 0, start);
653+
var end = buffer.indexOf(0, start);
669654
this.offset = end + 1;
670655
return buffer.toString(this.encoding, start, end);
671656
};

‎package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
"async": "0.9.0",
3131
"co": "4.6.0",
3232
"jshint": "2.5.2",
33-
"lodash": "4.13.1",
34-
"pg-copy-streams": "0.3.0",
35-
"promise-polyfill": "5.2.1"
33+
"pg-copy-streams": "0.3.0"
3634
},
3735
"minNativeVersion": "1.7.0",
3836
"scripts": {

‎test/buffer-list.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ p.add = function(buffer, front) {
99
};
1010

1111
p.addInt16 = function(val, front) {
12-
return this.add(Buffer([(val >>> 8),(val >>> 0)]),front);
12+
return this.add(Buffer.from([(val >>> 8),(val >>> 0)]),front);
1313
};
1414

1515
p.getByteLength = function(initial) {
@@ -19,7 +19,7 @@ p.getByteLength = function(initial) {
1919
};
2020

2121
p.addInt32 = function(val, first) {
22-
return this.add(Buffer([
22+
return this.add(Buffer.from([
2323
(val >>> 24 & 0xFF),
2424
(val >>> 16 & 0xFF),
2525
(val >>> 8 & 0xFF),
@@ -29,14 +29,14 @@ p.addInt32 = function(val, first) {
2929

3030
p.addCString = function(val, front) {
3131
var len = Buffer.byteLength(val);
32-
var buffer = new Buffer(len+1);
32+
var buffer = Buffer.alloc(len+1);
3333
buffer.write(val);
3434
buffer[len] = 0;
3535
return this.add(buffer, front);
3636
};
3737

3838
p.addChar = function(char, first) {
39-
return this.add(Buffer(char,'utf8'), first);
39+
return this.add(Buffer.from(char,'utf8'), first);
4040
};
4141

4242
p.join = function(appendLength, char) {
@@ -49,7 +49,7 @@ p.join = function(appendLength, char) {
4949
this.addChar(char, true);
5050
length++;
5151
}
52-
var result = Buffer(length);
52+
var result = Buffer.alloc(length);
5353
var index = 0;
5454
this.buffers.forEach(function(buffer) {
5555
buffer.copy(result, index, 0);

‎test/integration/client/query-as-promise-tests.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
var helper = require(__dirname + '/../test-helper');
22
var pg = helper.pg;
3-
var semver = require('semver')
4-
5-
if (semver.lt(process.version, '0.12.0')) {
6-
return console.log('promises are not supported in node < v0.10')
7-
}
83

94
process.on('unhandledRejection', function(e) {
105
console.error(e, e.stack)

‎test/integration/connection-pool/idle-timeout-tests.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var helper = require(__dirname + '/test-helper');
2-
var _ = require('lodash')
32

4-
const config = _.extend({ }, helper.config, { idleTimeoutMillis: 50 })
3+
const config = Object.assign({ }, helper.config, { idleTimeoutMillis: 50 })
54

65
test('idle timeout', function() {
76
helper.pg.connect(config, assert.calls(function(err, client, done) {

‎test/integration/connection-pool/yield-support-body.js

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
var semver = require('semver')
2-
if (semver.lt(process.version, '1.0.0')) {
3-
return console.log('yield is not supported in node <= v0.12')
4-
}
5-
require('./yield-support-body')
1+
var helper = require('./test-helper')
2+
var co = require('co')
3+
4+
var tid = setTimeout(function() {
5+
throw new Error('Tests did not complete in time')
6+
}, 1000)
7+
8+
co(function * () {
9+
var client = yield helper.pg.connect()
10+
var res = yield client.query('SELECT $1::text as name', ['foo'])
11+
assert.equal(res.rows[0].name, 'foo')
12+
13+
var threw = false
14+
try {
15+
yield client.query('SELECT LKDSJDSLKFJ')
16+
} catch(e) {
17+
threw = true
18+
}
19+
assert(threw)
20+
client.release()
21+
helper.pg.end()
22+
clearTimeout(tid)
23+
})
24+
.catch(function(e) {
25+
setImmediate(function() {
26+
throw e
27+
})
28+
})

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ helper.pg.connect(helper.config, function(err, client, done) {
1111

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

14-
var body = new Buffer('foo');
14+
var body = Buffer.from('foo');
1515
client.query(c, [body], function(err) {
1616
if (err) throw err;
1717

18-
body = new Buffer([]);
18+
body = Buffer.from([]);
1919
client.query(c, [body], function(err, res) {
2020
done();
2121

‎test/test-buffers.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require(__dirname+'/test-helper');
44
var buffers = {};
55
buffers.readyForQuery = function() {
66
return new BufferList()
7-
.add(Buffer('I'))
7+
.add(Buffer.from('I'))
88
.join(true,'Z');
99
};
1010

@@ -23,7 +23,7 @@ buffers.authenticationCleartextPassword = function() {
2323
buffers.authenticationMD5Password = function() {
2424
return new BufferList()
2525
.addInt32(5)
26-
.add(Buffer([1,2,3,4]))
26+
.add(Buffer.from([1,2,3,4]))
2727
.join(true, 'R');
2828
};
2929

@@ -71,7 +71,7 @@ buffers.dataRow = function(columns) {
7171
if(col == null) {
7272
buf.addInt32(-1);
7373
} else {
74-
var strBuf = new Buffer(col, 'utf8');
74+
var strBuf = Buffer.from(col, 'utf8');
7575
buf.addInt32(strBuf.length);
7676
buf.add(strBuf);
7777
}
@@ -94,7 +94,7 @@ var errorOrNotice = function(fields) {
9494
buf.addChar(field.type);
9595
buf.addCString(field.value);
9696
});
97-
return buf.add(Buffer([0]));//terminator
97+
return buf.add(Buffer.from([0]));//terminator
9898
}
9999

100100
buffers.parseComplete = function() {

‎test/test-helper.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
//make assert a global...
22
assert = require('assert');
33

4-
//support for node@0.10.x
5-
if (typeof Promise == 'undefined') {
6-
global.Promise = require('promise-polyfill')
7-
}
8-
94
var EventEmitter = require('events').EventEmitter;
105
var sys = require('util');
116
var BufferList = require(__dirname+'/buffer-list')

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require(__dirname + '/test-helper');
33
test('md5 authentication', function() {
44
var client = createClient();
55
client.password = "!";
6-
var salt = Buffer([1, 2, 3, 4]);
6+
var salt = Buffer.from([1, 2, 3, 4]);
77
client.connection.emit('authenticationMD5Password', {salt: salt});
88

99
test('responds', function() {

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ test('Connection', function() {
158158
testForMessage(plainPasswordBuffer, expectedPlainPasswordMessage);
159159
var msg = testForMessage(md5PasswordBuffer, expectedMD5PasswordMessage);
160160
test('md5 has right salt', function() {
161-
assert.equalBuffers(msg.salt, Buffer([1,2,3,4]));
161+
assert.equalBuffers(msg.salt, Buffer.from([1,2,3,4]));
162162
});
163163
testForMessage(paramStatusBuffer, expectedParameterStatusMessage);
164164
testForMessage(backendKeyDataBuffer, expectedBackendKeyDataMessage);
@@ -173,7 +173,7 @@ test('Connection', function() {
173173
});
174174

175175
test("no data message", function() {
176-
testForMessage(Buffer([0x6e, 0, 0, 0, 4]), {
176+
testForMessage(Buffer.from([0x6e, 0, 0, 0, 4]), {
177177
name: 'noData'
178178
});
179179
});
@@ -349,7 +349,7 @@ test('Connection', function() {
349349
});
350350

351351
test('parses replication start message', function() {
352-
testForMessage(new Buffer([0x57, 0x00, 0x00, 0x00, 0x04]), {
352+
testForMessage(Buffer.from([0x57, 0x00, 0x00, 0x00, 0x04]), {
353353
name: 'replicationStart',
354354
length: 4
355355
});
@@ -383,8 +383,8 @@ test('split buffer, single message parsing', function() {
383383
});
384384

385385
var testMessageRecievedAfterSpiltAt = function(split) {
386-
var firstBuffer = new Buffer(fullBuffer.length-split);
387-
var secondBuffer = new Buffer(fullBuffer.length-firstBuffer.length);
386+
var firstBuffer = Buffer.alloc(fullBuffer.length-split);
387+
var secondBuffer = Buffer.alloc(fullBuffer.length-firstBuffer.length);
388388
fullBuffer.copy(firstBuffer, 0, 0);
389389
fullBuffer.copy(secondBuffer, 0, firstBuffer.length);
390390
stream.emit('data', firstBuffer);
@@ -416,7 +416,7 @@ test('split buffer, single message parsing', function() {
416416
test('split buffer, multiple message parsing', function() {
417417
var dataRowBuffer = buffers.dataRow(['!']);
418418
var readyForQueryBuffer = buffers.readyForQuery();
419-
var fullBuffer = new Buffer(dataRowBuffer.length + readyForQueryBuffer.length);
419+
var fullBuffer = Buffer.alloc(dataRowBuffer.length + readyForQueryBuffer.length);
420420
dataRowBuffer.copy(fullBuffer, 0, 0);
421421
readyForQueryBuffer.copy(fullBuffer, dataRowBuffer.length, 0);
422422

@@ -449,8 +449,8 @@ test('split buffer, multiple message parsing', function() {
449449
verifyMessages();
450450
});
451451
var splitAndVerifyTwoMessages = function(split) {
452-
var firstBuffer = new Buffer(fullBuffer.length-split);
453-
var secondBuffer = new Buffer(fullBuffer.length-firstBuffer.length);
452+
var firstBuffer = Buffer.alloc(fullBuffer.length-split);
453+
var secondBuffer = Buffer.alloc(fullBuffer.length-firstBuffer.length);
454454
fullBuffer.copy(firstBuffer, 0, 0);
455455
fullBuffer.copy(secondBuffer, 0, firstBuffer.length);
456456
stream.emit('data', firstBuffer);

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ test('bind messages', function() {
104104
.addInt16(0)
105105
.addInt16(4)
106106
.addInt32(1)
107-
.add(Buffer("1"))
107+
.add(Buffer.from("1"))
108108
.addInt32(2)
109-
.add(Buffer("hi"))
109+
.add(Buffer.from("hi"))
110110
.addInt32(-1)
111111
.addInt32(4)
112-
.add(Buffer('zing'))
112+
.add(Buffer.from('zing'))
113113
.addInt16(0)
114114
.join(true, 'B');
115115
assert.received(stream, expectedBuffer);
@@ -120,7 +120,7 @@ test('with named statement, portal, and buffer value', function() {
120120
con.bind({
121121
portal: 'bang',
122122
statement: 'woo',
123-
values: ['1', 'hi', null, new Buffer('zing', 'UTF-8')]
123+
values: ['1', 'hi', null, Buffer.from('zing', 'utf8')]
124124
});
125125
var expectedBuffer = new BufferList()
126126
.addCString('bang') //portal name
@@ -132,12 +132,12 @@ test('with named statement, portal, and buffer value', function() {
132132
.addInt16(1)//binary
133133
.addInt16(4)
134134
.addInt32(1)
135-
.add(Buffer("1"))
135+
.add(Buffer.from("1"))
136136
.addInt32(2)
137-
.add(Buffer("hi"))
137+
.add(Buffer.from("hi"))
138138
.addInt32(-1)
139139
.addInt32(4)
140-
.add(new Buffer('zing', 'UTF-8'))
140+
.add(Buffer.from('zing', 'UTF-8'))
141141
.addInt16(0)
142142
.join(true, 'B');
143143
assert.received(stream, expectedBuffer);
@@ -181,7 +181,7 @@ test('sends sync command', function() {
181181

182182
test('sends end command', function() {
183183
con.end();
184-
var expected = new Buffer([0x58, 0, 0, 0, 4]);
184+
var expected = Buffer.from([0x58, 0, 0, 0, 4]);
185185
assert.received(stream, expected);
186186
});
187187

‎test/unit/utils-tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -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 = Buffer.from("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 = Buffer.from("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 = Buffer.from("zomgcustom!");
169169
var customType = {
170170
toPostgres: function() {
171171
return { toPostgres: function () { return customType; } };

0 commit comments

Comments
 (0)
Please sign in to comment.