-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconnection.js
More file actions
368 lines (310 loc) · 10.1 KB
/
connection.js
File metadata and controls
368 lines (310 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
'use strict'
const EventEmitter = require('events').EventEmitter
const { parse, serialize } = require('pg-protocol')
const { getStream, getSecureStream } = require('./stream')
const flushBuffer = serialize.flush()
const syncBuffer = serialize.sync()
const endBuffer = serialize.end()
// TODO(bmc) support binary mode at some point
class Connection extends EventEmitter {
constructor(config) {
super()
config = config || {}
if (typeof config.stream === 'function') {
this._streamFactory = config.stream
this._config = config
this.stream = config.stream(config)
} else {
this._streamFactory = null
this._config = null
this.stream = config.stream || getStream(config.ssl)
}
this._keepAlive = config.keepAlive
this._keepAliveInitialDelayMillis = config.keepAliveInitialDelayMillis
this.parsedStatements = {}
this.ssl = config.ssl || false
this._ending = false
this._emitMessage = false
this._targetSessionAttrs = config.targetSessionAttrs || null
const self = this
this.on('newListener', function (eventName) {
if (eventName === 'message') {
self._emitMessage = true
}
})
}
_newStream() {
return this._streamFactory ? this._streamFactory(this._config) : getStream(this.ssl)
}
connect(port, host) {
const self = this
const hosts = Array.isArray(host) ? host : [host]
const ports = Array.isArray(port) ? port : [port]
let hostIndex = 0
this._connecting = true
const targetAttrs = this._targetSessionAttrs
if (targetAttrs && targetAttrs !== 'any') {
let backendParams = {}
let fetchingState = false
let fetchStateRows = []
let fetchStateError = false
const origEmit = EventEmitter.prototype.emit.bind(self)
const tryNextOrFail = () => {
backendParams = {}
fetchingState = false
fetchStateRows = []
fetchStateError = false
if (hostIndex + 1 < hosts.length) {
hostIndex++
self.stream.removeAllListeners()
self.stream.destroy()
self.stream = self._newStream()
attemptConnect()
} else {
self.emit = origEmit
origEmit('error', new Error(`None of the hosts satisfy the target_session_attrs requirement: ${targetAttrs}`))
}
}
self.emit = function (eventName, ...args) {
if (eventName === 'parameterStatus') {
const msg = args[0]
if (msg) backendParams[msg.parameterName] = msg.parameterValue
return origEmit(eventName, ...args)
}
if (fetchingState) {
if (eventName === 'dataRow') {
fetchStateRows.push(args[0])
return
}
if (eventName === 'rowDescription' || eventName === 'commandComplete') {
return
}
if (eventName === 'errorMessage') {
fetchStateError = true
return
}
if (eventName === 'readyForQuery') {
fetchingState = false
if (!fetchStateError && fetchStateRows.length >= 2) {
const txReadOnly = fetchStateRows[0].fields[0]?.toString('utf8') ?? null
const isRecovery = fetchStateRows[1].fields[0]?.toString('utf8') ?? null
if (txReadOnly !== null) backendParams.default_transaction_read_only = txReadOnly
if (isRecovery !== null) backendParams.in_hot_standby = isRecovery === 't' ? 'on' : 'off'
}
fetchStateRows = []
fetchStateError = false
if (notHostMatchTargetSessionAttrs(targetAttrs, backendParams, hostIndex, hosts)) {
tryNextOrFail()
} else {
self.emit = origEmit
origEmit('readyForQuery', args[0])
}
return
}
}
if (eventName === 'readyForQuery') {
if (!backendParams.in_hot_standby || !backendParams.default_transaction_read_only) {
fetchingState = true
fetchStateRows = []
self.query('SHOW transaction_read_only; SELECT pg_catalog.pg_is_in_recovery()')
return
}
if (notHostMatchTargetSessionAttrs(targetAttrs, backendParams, hostIndex, hosts)) {
tryNextOrFail()
return
}
self.emit = origEmit
return origEmit('readyForQuery', args[0])
}
return origEmit(eventName, ...args)
}
}
const attemptConnect = () => {
const currentHost = hosts[hostIndex]
const currentPort = ports[Math.min(hostIndex, ports.length - 1)]
let connected = false
self.stream.setNoDelay(true)
self.stream.connect(currentPort, currentHost)
self.stream.once('connect', function () {
connected = true
if (self._keepAlive) {
self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
}
self.emit('connect')
})
const reportStreamError = function (error) {
// errors about disconnections should be ignored during disconnect
if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
return
}
if (!connected && hostIndex + 1 < hosts.length) {
hostIndex++
self.stream.removeAllListeners()
self.stream.destroy()
self.stream = self._newStream()
attemptConnect()
return
}
self.emit('error', error)
}
self.stream.on('error', reportStreamError)
const onClose = function () {
self.emit('end')
}
self.stream.on('close', onClose)
if (!self.ssl) {
return self.attachListeners(self.stream)
}
self.stream.once('data', function (buffer) {
const responseCode = buffer.toString('utf8')
switch (responseCode) {
case 'S': // Server supports SSL connections, continue with a secure connection
break
case 'N': // Server does not support SSL connections
self.stream.end()
return self.emit('error', new Error('The server does not support SSL connections'))
default:
// Any other response byte, including 'E' (ErrorResponse) indicating a server error
self.stream.end()
return self.emit('error', new Error('There was an error establishing an SSL connection'))
}
const options = {
socket: self.stream,
}
if (self.ssl !== true) {
Object.assign(options, self.ssl)
if ('key' in self.ssl) {
options.key = self.ssl.key
}
}
const net = require('net')
if (net.isIP && net.isIP(currentHost) === 0) {
options.servername = currentHost
}
// Remove the close listener from the TCP socket before upgrading to TLS.
// Without this, destroying the TLS stream (during host failover) closes the
// underlying TCP socket, which fires 'close' → 'end' even though we are
// still mid-connection to the next host.
const tcpStream = self.stream
tcpStream.removeListener('close', onClose)
tcpStream.removeListener('error', reportStreamError)
try {
self.stream = getSecureStream(options)
} catch (err) {
return self.emit('error', err)
}
self.attachListeners(self.stream)
self.stream.on('error', reportStreamError)
self.stream.on('close', onClose)
self.emit('sslconnect')
})
}
attemptConnect()
}
attachListeners(stream) {
parse(stream, (msg) => {
const eventName = msg.name === 'error' ? 'errorMessage' : msg.name
if (this._emitMessage) {
this.emit('message', msg)
}
this.emit(eventName, msg)
})
}
requestSsl() {
this.stream.write(serialize.requestSsl())
}
startup(config) {
this.stream.write(serialize.startup(config))
}
cancel(processID, secretKey) {
this._send(serialize.cancel(processID, secretKey))
}
password(password) {
this._send(serialize.password(password))
}
sendSASLInitialResponseMessage(mechanism, initialResponse) {
this._send(serialize.sendSASLInitialResponseMessage(mechanism, initialResponse))
}
sendSCRAMClientFinalMessage(additionalData) {
this._send(serialize.sendSCRAMClientFinalMessage(additionalData))
}
_send(buffer) {
if (!this.stream.writable) {
return false
}
return this.stream.write(buffer)
}
query(text) {
this._send(serialize.query(text))
}
// send parse message
parse(query) {
this._send(serialize.parse(query))
}
// send bind message
bind(config) {
this._send(serialize.bind(config))
}
// send execute message
execute(config) {
this._send(serialize.execute(config))
}
flush() {
if (this.stream.writable) {
this.stream.write(flushBuffer)
}
}
sync() {
this._ending = true
this._send(syncBuffer)
}
ref() {
this.stream.ref()
}
unref() {
this.stream.unref()
}
end() {
// 0x58 = 'X'
this._ending = true
if (!this._connecting || !this.stream.writable) {
this.stream.end()
return
}
return this.stream.write(endBuffer, () => {
this.stream.end()
})
}
close(msg) {
this._send(serialize.close(msg))
}
describe(msg) {
this._send(serialize.describe(msg))
}
sendCopyFromChunk(chunk) {
this._send(serialize.copyData(chunk))
}
endCopyFrom() {
this._send(serialize.copyDone())
}
sendCopyFail(msg) {
this._send(serialize.copyFail(msg))
}
}
function notHostMatchTargetSessionAttrs(targetAttrs, params, hostIndex, hosts) {
switch (targetAttrs) {
case 'read-write':
return params.in_hot_standby === 'on' || params.default_transaction_read_only === 'on'
case 'read-only':
return params.in_hot_standby !== 'on' && params.default_transaction_read_only !== 'on'
case 'primary':
return params.in_hot_standby === 'on'
case 'standby':
return params.in_hot_standby === 'off'
case 'prefer-standby':
return params.in_hot_standby === 'off' && hostIndex + 1 < hosts.length
default:
return false
}
}
module.exports = Connection