Skip to content

Commit 149f482

Browse files
committed
Replace uses of private/undocumented readyState API
The `readyState` of a newly-created `net.Socket` changed from `'closed'` to `'open'` in Node 14.0.0, so this makes the JS driver work on Node 14. `Connection` now always calls `connect` on its `stream` when `connect` is called on it.
1 parent 0729130 commit 149f482

8 files changed

+18
-36
lines changed

packages/pg/lib/client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ Client.prototype.end = function (cb) {
566566
this._ending = true
567567

568568
// if we have never connected, then end is a noop, callback immediately
569-
if (this.connection.stream.readyState === 'closed') {
569+
if (!this.connection._connecting) {
570570
if (cb) {
571571
cb()
572572
} else {

packages/pg/lib/connection-fast.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ util.inherits(Connection, EventEmitter)
4242
Connection.prototype.connect = function (port, host) {
4343
var self = this
4444

45-
if (this.stream.readyState === 'closed') {
46-
this.stream.connect(port, host)
47-
} else if (this.stream.readyState === 'open') {
48-
this.emit('connect')
49-
}
45+
this._connecting = true
46+
this.stream.connect(port, host)
5047

51-
this.stream.on('connect', function () {
48+
this.stream.once('connect', function () {
5249
if (self._keepAlive) {
5350
self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
5451
}
@@ -187,7 +184,7 @@ const endBuffer = serialize.end()
187184
Connection.prototype.end = function () {
188185
// 0x58 = 'X'
189186
this._ending = true
190-
if (!this.stream.writable) {
187+
if (!this._connecting || !this.stream.writable) {
191188
this.stream.end()
192189
return
193190
}

packages/pg/lib/connection.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ util.inherits(Connection, EventEmitter)
5050
Connection.prototype.connect = function (port, host) {
5151
var self = this
5252

53-
if (this.stream.readyState === 'closed') {
54-
this.stream.connect(port, host)
55-
} else if (this.stream.readyState === 'open') {
56-
this.emit('connect')
57-
}
53+
this._connecting = true
54+
this.stream.connect(port, host)
5855

59-
this.stream.on('connect', function () {
56+
this.stream.once('connect', function () {
6057
if (self._keepAlive) {
6158
self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
6259
}
@@ -316,7 +313,7 @@ Connection.prototype.end = function () {
316313
// 0x58 = 'X'
317314
this.writer.add(emptyBuffer)
318315
this._ending = true
319-
if (!this.stream.writable) {
316+
if (!this._connecting || !this.stream.writable) {
320317
this.stream.end()
321318
return
322319
}

packages/pg/test/unit/client/stream-and-query-error-interaction-tests.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var Client = require(__dirname + '/../../../lib/client')
55

66
test('emits end when not in query', function () {
77
var stream = new (require('events').EventEmitter)()
8+
stream.connect = function () {
9+
// NOOP
10+
}
811
stream.write = function () {
912
// NOOP
1013
}

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

-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ test('Connection', function () {
399399
test('split buffer, single message parsing', function () {
400400
var fullBuffer = buffers.dataRow([null, 'bang', 'zug zug', null, '!'])
401401
var stream = new MemoryStream()
402-
stream.readyState = 'open'
403402
var client = new Connection({
404403
stream: stream,
405404
})

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var stream = new MemoryStream()
55
var con = new Connection({
66
stream: stream,
77
})
8+
con._connecting = true
89

910
assert.received = function (stream, buffer) {
1011
assert.lengthIs(stream.packets, 1)

packages/pg/test/unit/connection/startup-tests.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ test('connection can take existing stream', function () {
77
assert.equal(con.stream, stream)
88
})
99

10-
test('using closed stream', function () {
10+
test('using any stream', function () {
1111
var makeStream = function () {
1212
var stream = new MemoryStream()
13-
stream.readyState = 'closed'
1413
stream.connect = function (port, host) {
1514
this.connectCalled = true
1615
this.port = port
@@ -65,20 +64,3 @@ test('using closed stream', function () {
6564
})
6665
})
6766
})
68-
69-
test('using opened stream', function () {
70-
var stream = new MemoryStream()
71-
stream.readyState = 'open'
72-
stream.connect = function () {
73-
assert.ok(false, 'Should not call open')
74-
}
75-
var con = new Connection({ stream: stream })
76-
test('does not call open', function () {
77-
var hit = false
78-
con.once('connect', function () {
79-
hit = true
80-
})
81-
con.connect()
82-
assert.ok(hit)
83-
})
84-
})

packages/pg/test/unit/test-helper.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ helper.sys.inherits(MemoryStream, EventEmitter)
1313

1414
var p = MemoryStream.prototype
1515

16+
p.connect = function () {
17+
// NOOP
18+
}
19+
1620
p.write = function (packet, cb) {
1721
this.packets.push(packet)
1822
if (cb) {
@@ -30,7 +34,6 @@ p.writable = true
3034

3135
const createClient = function () {
3236
var stream = new MemoryStream()
33-
stream.readyState = 'open'
3437
var client = new Client({
3538
connection: new Connection({ stream: stream }),
3639
})

0 commit comments

Comments
 (0)