forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
909 lines (783 loc) · 25.6 KB
/
client.js
File metadata and controls
909 lines (783 loc) · 25.6 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
'use strict'
const EventEmitter = require('events').EventEmitter
const utils = require('./utils')
const nodeUtils = require('node:util')
const sasl = require('./crypto/sasl')
const TypeOverrides = require('./type-overrides')
const ConnectionParameters = require('./connection-parameters')
const Query = require('./query')
const defaults = require('./defaults')
const Connection = require('./connection')
const crypto = require('./crypto/utils')
const activeQueryDeprecationNotice = nodeUtils.deprecate(
() => {},
'Client.activeQuery is deprecated and will be removed in a future version.'
)
const queryQueueDeprecationNotice = nodeUtils.deprecate(
() => {},
'Client.queryQueue is deprecated and will be removed in a future version.'
)
const pgPassDeprecationNotice = nodeUtils.deprecate(
() => {},
'pgpass support is deprecated and will be removed in a future version. ' +
'You can provide an async function as the password property to the Client/Pool constructor that returns a password instead. Within this funciton you can call the pgpass module in your own code.'
)
const byoPromiseDeprecationNotice = nodeUtils.deprecate(
() => {},
'Passing a custom Promise implementation to the Client/Pool constructor is deprecated and will be removed in a future version.'
)
class Client extends EventEmitter {
constructor(config) {
super()
this.connectionParameters = new ConnectionParameters(config)
this.user = this.connectionParameters.user
this.database = this.connectionParameters.database
this.port = this.connectionParameters.port
this.host = this.connectionParameters.host
// "hiding" the password so it doesn't show up in stack traces
// or if the client is console.logged
Object.defineProperty(this, 'password', {
configurable: true,
enumerable: false,
writable: true,
value: this.connectionParameters.password,
})
this.replication = this.connectionParameters.replication
const c = config || {}
if (c.Promise) {
byoPromiseDeprecationNotice()
}
this._Promise = c.Promise || global.Promise
this._types = new TypeOverrides(c.types)
this._ending = false
this._ended = false
this._connecting = false
this._connected = false
this._connectionError = false
this._queryable = true
this._activeQuery = null
this.enableChannelBinding = Boolean(c.enableChannelBinding) // set true to use SCRAM-SHA-256-PLUS when offered
this.connection =
c.connection ||
new Connection({
stream: c.stream,
ssl: this.connectionParameters.ssl,
keepAlive: c.keepAlive || false,
keepAliveInitialDelayMillis: c.keepAliveInitialDelayMillis || 0,
encoding: this.connectionParameters.client_encoding || 'utf8',
})
this._queryQueue = []
this.binary = c.binary || defaults.binary
this.processID = null
this.secretKey = null
this._pipelining = false
this._pipelineQueue = []
this._pipelineActive = false
this.ssl = this.connectionParameters.ssl || false
// As with Password, make SSL->Key (the private key) non-enumerable.
// It won't show up in stack traces
// or if the client is console.logged
if (this.ssl && this.ssl.key) {
Object.defineProperty(this.ssl, 'key', {
enumerable: false,
})
}
this._connectionTimeoutMillis = c.connectionTimeoutMillis || 0
}
get activeQuery() {
activeQueryDeprecationNotice()
return this._activeQuery
}
set activeQuery(val) {
activeQueryDeprecationNotice()
this._activeQuery = val
}
_getActiveQuery() {
return this._activeQuery
}
_errorAllQueries(err) {
const enqueueError = (query) => {
process.nextTick(() => {
query.handleError(err, this.connection)
})
}
const activeQuery = this._getActiveQuery()
if (activeQuery) {
enqueueError(activeQuery)
this._activeQuery = null
}
this._queryQueue.forEach(enqueueError)
this._queryQueue.length = 0
// Also error all pipeline queries
if (this._pipelineQueue) {
this._pipelineQueue.forEach(enqueueError)
this._pipelineQueue.length = 0
}
}
_connect(callback) {
const self = this
const con = this.connection
this._connectionCallback = callback
if (this._connecting || this._connected) {
const err = new Error('Client has already been connected. You cannot reuse a client.')
process.nextTick(() => {
callback(err)
})
return
}
this._connecting = true
if (this._connectionTimeoutMillis > 0) {
this.connectionTimeoutHandle = setTimeout(() => {
con._ending = true
con.stream.destroy(new Error('timeout expired'))
}, this._connectionTimeoutMillis)
if (this.connectionTimeoutHandle.unref) {
this.connectionTimeoutHandle.unref()
}
}
if (this.host && this.host.indexOf('/') === 0) {
con.connect(this.host + '/.s.PGSQL.' + this.port)
} else {
con.connect(this.port, this.host)
}
// once connection is established send startup message
con.on('connect', function () {
if (self.ssl) {
con.requestSsl()
} else {
con.startup(self.getStartupConf())
}
})
con.on('sslconnect', function () {
con.startup(self.getStartupConf())
})
this._attachListeners(con)
con.once('end', () => {
const error = this._ending ? new Error('Connection terminated') : new Error('Connection terminated unexpectedly')
clearTimeout(this.connectionTimeoutHandle)
this._errorAllQueries(error)
this._ended = true
if (!this._ending) {
// if the connection is ended without us calling .end()
// on this client then we have an unexpected disconnection
// treat this as an error unless we've already emitted an error
// during connection.
if (this._connecting && !this._connectionError) {
if (this._connectionCallback) {
this._connectionCallback(error)
} else {
this._handleErrorEvent(error)
}
} else if (!this._connectionError) {
this._handleErrorEvent(error)
}
}
process.nextTick(() => {
this.emit('end')
})
})
}
connect(callback) {
if (callback) {
this._connect(callback)
return
}
return new this._Promise((resolve, reject) => {
this._connect((error) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
_attachListeners(con) {
// password request handling
con.on('authenticationCleartextPassword', this._handleAuthCleartextPassword.bind(this))
// password request handling
con.on('authenticationMD5Password', this._handleAuthMD5Password.bind(this))
// password request handling (SASL)
con.on('authenticationSASL', this._handleAuthSASL.bind(this))
con.on('authenticationSASLContinue', this._handleAuthSASLContinue.bind(this))
con.on('authenticationSASLFinal', this._handleAuthSASLFinal.bind(this))
con.on('backendKeyData', this._handleBackendKeyData.bind(this))
con.on('error', this._handleErrorEvent.bind(this))
con.on('errorMessage', this._handleErrorMessage.bind(this))
con.on('readyForQuery', this._handleReadyForQuery.bind(this))
con.on('notice', this._handleNotice.bind(this))
con.on('rowDescription', this._handleRowDescription.bind(this))
con.on('dataRow', this._handleDataRow.bind(this))
con.on('portalSuspended', this._handlePortalSuspended.bind(this))
con.on('emptyQuery', this._handleEmptyQuery.bind(this))
con.on('commandComplete', this._handleCommandComplete.bind(this))
con.on('parseComplete', this._handleParseComplete.bind(this))
con.on('copyInResponse', this._handleCopyInResponse.bind(this))
con.on('copyData', this._handleCopyData.bind(this))
con.on('notification', this._handleNotification.bind(this))
}
_getPassword(cb) {
const con = this.connection
if (typeof this.password === 'function') {
this._Promise
.resolve()
.then(() => this.password())
.then((pass) => {
if (pass !== undefined) {
if (typeof pass !== 'string') {
con.emit('error', new TypeError('Password must be a string'))
return
}
this.connectionParameters.password = this.password = pass
} else {
this.connectionParameters.password = this.password = null
}
cb()
})
.catch((err) => {
con.emit('error', err)
})
} else if (this.password !== null) {
cb()
} else {
try {
const pgPass = require('pgpass')
pgPass(this.connectionParameters, (pass) => {
if (undefined !== pass) {
pgPassDeprecationNotice()
this.connectionParameters.password = this.password = pass
}
cb()
})
} catch (e) {
this.emit('error', e)
}
}
}
_handleAuthCleartextPassword(msg) {
this._getPassword(() => {
this.connection.password(this.password)
})
}
_handleAuthMD5Password(msg) {
this._getPassword(async () => {
try {
const hashedPassword = await crypto.postgresMd5PasswordHash(this.user, this.password, msg.salt)
this.connection.password(hashedPassword)
} catch (e) {
this.emit('error', e)
}
})
}
_handleAuthSASL(msg) {
this._getPassword(() => {
try {
this.saslSession = sasl.startSession(msg.mechanisms, this.enableChannelBinding && this.connection.stream)
this.connection.sendSASLInitialResponseMessage(this.saslSession.mechanism, this.saslSession.response)
} catch (err) {
this.connection.emit('error', err)
}
})
}
async _handleAuthSASLContinue(msg) {
try {
await sasl.continueSession(
this.saslSession,
this.password,
msg.data,
this.enableChannelBinding && this.connection.stream
)
this.connection.sendSCRAMClientFinalMessage(this.saslSession.response)
} catch (err) {
this.connection.emit('error', err)
}
}
_handleAuthSASLFinal(msg) {
try {
sasl.finalizeSession(this.saslSession, msg.data)
this.saslSession = null
} catch (err) {
this.connection.emit('error', err)
}
}
_handleBackendKeyData(msg) {
this.processID = msg.processID
this.secretKey = msg.secretKey
}
_handleReadyForQuery(msg) {
if (this._connecting) {
this._connecting = false
this._connected = true
clearTimeout(this.connectionTimeoutHandle)
// process possible callback argument to Client#connect
if (this._connectionCallback) {
this._connectionCallback(null, this)
// remove callback for proper error handling
// after the connect event
this._connectionCallback = null
}
this.emit('connect')
}
if (this._pipelining) {
return this._handlePipelineReadyForQuery(msg)
}
const activeQuery = this._getActiveQuery()
this._activeQuery = null
this.readyForQuery = true
if (activeQuery) {
activeQuery.handleReadyForQuery(this.connection)
}
this._pulseQueryQueue()
}
_handlePipelineReadyForQuery(msg) {
// In pipeline mode, handle completed queries
if (this._pipelineQueue.length > 0) {
const completedQuery = this._pipelineQueue.shift()
if (completedQuery) {
completedQuery.handleReadyForQuery(this.connection)
} else {
// No queries in pipeline queue, but we received readyForQuery
// This might happen due to message timing in pipeline mode
// Just mark as ready for more queries
this.readyForQuery = true
}
}
// If no more queries in pipeline, we're ready for more
if (this._pipelineQueue.length === 0) {
this.readyForQuery = true
this.emit('drain')
}
}
// if we receive an error event or error message
// during the connection process we handle it here
_handleErrorWhileConnecting(err) {
if (this._connectionError) {
// TODO(bmc): this is swallowing errors - we shouldn't do this
return
}
this._connectionError = true
clearTimeout(this.connectionTimeoutHandle)
if (this._connectionCallback) {
return this._connectionCallback(err)
}
this.emit('error', err)
}
// if we're connected and we receive an error event from the connection
// this means the socket is dead - do a hard abort of all queries and emit
// the socket error on the client as well
_handleErrorEvent(err) {
if (this._connecting) {
return this._handleErrorWhileConnecting(err)
}
this._queryable = false
this._errorAllQueries(err)
this.emit('error', err)
}
// handle error messages from the postgres backend
_handleErrorMessage(msg) {
if (this._connecting) {
return this._handleErrorWhileConnecting(msg)
}
const activeQuery = this._getActiveQuery()
if (!activeQuery) {
this._handleErrorEvent(msg)
return
}
this._activeQuery = null
activeQuery.handleError(msg, this.connection)
}
_handleRowDescription(msg) {
// delegate rowDescription to active query
const query = this._getCurrentQuery()
if (query) {
query.handleRowDescription(msg)
}
}
_handleDataRow(msg) {
// delegate dataRow to active query
const query = this._getCurrentQuery()
if (query) {
query.handleDataRow(msg)
}
}
_handlePortalSuspended(msg) {
// delegate portalSuspended to active query
const query = this._getCurrentQuery()
if (query) {
query.handlePortalSuspended(this.connection)
}
}
_handleEmptyQuery(msg) {
// delegate emptyQuery to active query
const query = this._getCurrentQuery()
if (query) {
query.handleEmptyQuery(this.connection)
}
}
_handleCommandComplete(msg) {
const query = this._getCurrentQuery()
if (query == null) {
// In pipeline mode, commandComplete might be received after query is processed
// This can happen due to message timing, so we can safely ignore it
if (this._pipelining) {
return
}
const error = new Error('Received unexpected commandComplete message from backend.')
this._handleErrorEvent(error)
return
}
// delegate commandComplete to active query
query.handleCommandComplete(msg, this.connection)
}
_getCurrentQuery() {
if (this._pipelining) {
// In pipeline mode, return the first query in the pipeline queue
return this._pipelineQueue.length > 0 ? this._pipelineQueue[0] : null
}
return this._getActiveQuery()
}
_handleParseComplete() {
const activeQuery = this._getCurrentQuery()
if (activeQuery == null) {
// In pipeline mode, parseComplete might be received before queries are fully processed
// This is normal behavior, so we can safely ignore it
if (this._pipelining) {
return
}
const error = new Error('Received unexpected parseComplete message from backend.')
this._handleErrorEvent(error)
return
}
// if a prepared statement has a name and properly parses
// we track that its already been executed so we don't parse
// it again on the same client
if (activeQuery.name) {
this.connection.parsedStatements[activeQuery.name] = activeQuery.text
}
}
_handleCopyInResponse(msg) {
this._getActiveQuery().handleCopyInResponse(this.connection)
}
_handleCopyData(msg) {
this._getActiveQuery().handleCopyData(msg, this.connection)
}
_handleNotification(msg) {
this.emit('notification', msg)
}
_handleNotice(msg) {
this.emit('notice', msg)
}
getStartupConf() {
const params = this.connectionParameters
const data = {
user: params.user,
database: params.database,
}
const appName = params.application_name || params.fallback_application_name
if (appName) {
data.application_name = appName
}
if (params.replication) {
data.replication = '' + params.replication
}
if (params.statement_timeout) {
data.statement_timeout = String(parseInt(params.statement_timeout, 10))
}
if (params.lock_timeout) {
data.lock_timeout = String(parseInt(params.lock_timeout, 10))
}
if (params.idle_in_transaction_session_timeout) {
data.idle_in_transaction_session_timeout = String(parseInt(params.idle_in_transaction_session_timeout, 10))
}
if (params.options) {
data.options = params.options
}
return data
}
cancel(client, query) {
if (client.activeQuery === query) {
const con = this.connection
if (this.host && this.host.indexOf('/') === 0) {
con.connect(this.host + '/.s.PGSQL.' + this.port)
} else {
con.connect(this.port, this.host)
}
// once connection is established send cancel message
con.on('connect', function () {
con.cancel(client.processID, client.secretKey)
})
} else if (client._queryQueue.indexOf(query) !== -1) {
client._queryQueue.splice(client._queryQueue.indexOf(query), 1)
}
}
setTypeParser(oid, format, parseFn) {
return this._types.setTypeParser(oid, format, parseFn)
}
getTypeParser(oid, format) {
return this._types.getTypeParser(oid, format)
}
// escapeIdentifier and escapeLiteral moved to utility functions & exported
// on PG
// re-exported here for backwards compatibility
escapeIdentifier(str) {
return utils.escapeIdentifier(str)
}
escapeLiteral(str) {
return utils.escapeLiteral(str)
}
_pulseQueryQueue() {
if (this.readyForQuery === true) {
this._activeQuery = this._queryQueue.shift()
const activeQuery = this._getActiveQuery()
if (activeQuery) {
this.readyForQuery = false
this.hasExecuted = true
const queryError = activeQuery.submit(this.connection)
if (queryError) {
process.nextTick(() => {
activeQuery.handleError(queryError, this.connection)
this.readyForQuery = true
this._pulseQueryQueue()
})
}
} else if (this.hasExecuted) {
this._activeQuery = null
this.emit('drain')
}
}
}
query(config, values, callback) {
// can take in strings, config object or query object
let query
let result
let readTimeout
let readTimeoutTimer
let queryCallback
if (config === null || config === undefined) {
throw new TypeError('Client was passed a null or undefined query')
} else if (typeof config.submit === 'function') {
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
result = query = config
if (typeof values === 'function') {
query.callback = query.callback || values
}
} else {
readTimeout = config.query_timeout || this.connectionParameters.query_timeout
query = new Query(config, values, callback)
if (!query.callback) {
result = new this._Promise((resolve, reject) => {
query.callback = (err, res) => (err ? reject(err) : resolve(res))
}).catch((err) => {
// replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
// application that created the query
Error.captureStackTrace(err)
throw err
})
}
}
if (readTimeout) {
queryCallback = query.callback
readTimeoutTimer = setTimeout(() => {
const error = new Error('Query read timeout')
process.nextTick(() => {
query.handleError(error, this.connection)
})
queryCallback(error)
// we already returned an error,
// just do nothing if query completes
query.callback = () => {}
// Remove from queue
const index = this._queryQueue.indexOf(query)
if (index > -1) {
this._queryQueue.splice(index, 1)
}
this._pulseQueryQueue()
}, readTimeout)
query.callback = (err, res) => {
clearTimeout(readTimeoutTimer)
queryCallback(err, res)
}
}
if (this.binary && !query.binary) {
query.binary = true
}
if (query._result && !query._result._types) {
query._result._types = this._types
}
if (!this._queryable) {
process.nextTick(() => {
query.handleError(new Error('Client has encountered a connection error and is not queryable'), this.connection)
})
return result
}
if (this._ending) {
process.nextTick(() => {
query.handleError(new Error('Client was closed and is not queryable'), this.connection)
})
return result
}
if (this._pipelining) {
return this._pipelineQuery(query, result)
}
this._queryQueue.push(query)
this._pulseQueryQueue()
return result
}
ref() {
this.connection.ref()
}
unref() {
this.connection.unref()
}
end(cb) {
this._ending = true
// if we have never connected, then end is a noop, callback immediately
if (!this.connection._connecting || this._ended) {
if (cb) {
cb()
} else {
return this._Promise.resolve()
}
}
if (this._getActiveQuery() || !this._queryable) {
// if we have an active query we need to force a disconnect
// on the socket - otherwise a hung query could block end forever
this.connection.stream.destroy()
} else {
this.connection.end()
}
if (cb) {
this.connection.once('end', cb)
} else {
return new this._Promise((resolve) => {
this.connection.once('end', resolve)
})
}
}
get queryQueue() {
queryQueueDeprecationNotice()
return this._queryQueue
}
get pipelining() {
return this._pipelining
}
set pipelining(value) {
if (typeof value !== 'boolean') {
throw new TypeError('pipelining must be a boolean')
}
if (this._pipelining === value) {
return
}
if (value && !this._connected) {
throw new Error('Cannot enable pipelining mode before connection is established')
}
if (value && this._getActiveQuery()) {
throw new Error('Cannot enable pipelining mode while a query is active')
}
if (value) {
this._enterPipelineMode()
} else {
this._exitPipelineMode()
}
}
_enterPipelineMode() {
if (this._pipelining) {
return
}
this._pipelining = true
this._pipelineActive = true
this._pipelineQueue = []
// Send pipeline mode command to server
this.connection.enterPipelineMode()
}
_exitPipelineMode() {
if (!this._pipelining) {
return
}
// Process any remaining queries in pipeline
if (this._pipelineQueue.length > 0) {
throw new Error('Cannot exit pipeline mode with pending queries')
}
this._pipelining = false
this._pipelineActive = false
// Clear any pending sync timer
if (this._pipelineSyncTimer) {
clearTimeout(this._pipelineSyncTimer)
this._pipelineSyncTimer = null
}
// Send exit pipeline mode command to server
this.connection.exitPipelineMode()
}
_pipelineQuery(query, result) {
// Validate query for pipeline mode
if (query.text && typeof query.text === 'string' && query.text.includes(';')) {
const error = new Error('Multiple SQL commands in a single query are not allowed in pipeline mode')
process.nextTick(() => {
query.handleError(error, this.connection)
})
return result
}
// Disallow simple query protocol in pipeline mode
if (!query.requiresPreparation()) {
const error = new Error('Simple query protocol is not allowed in pipeline mode. Use parameterized queries.')
process.nextTick(() => {
query.handleError(error, this.connection)
})
return result
}
// Add query to pipeline queue
this._pipelineQueue.push(query)
// Submit query using pipeline-specific method
const queryError = this._submitPipelineQuery(query)
if (queryError) {
process.nextTick(() => {
query.handleError(queryError, this.connection)
// Remove from pipeline queue on error
const index = this._pipelineQueue.indexOf(query)
if (index > -1) {
this._pipelineQueue.splice(index, 1)
}
})
} else {
// Schedule a sync after a short delay to allow batching
this._schedulePipelineSync()
}
return result
}
_schedulePipelineSync() {
// Clear any existing sync timer
if (this._pipelineSyncTimer) {
clearTimeout(this._pipelineSyncTimer)
}
// Schedule a sync after a short delay to allow multiple queries to batch
this._pipelineSyncTimer = setTimeout(() => {
if (this._pipelining && this._pipelineQueue.length > 0) {
this.connection.pipelineSync()
}
this._pipelineSyncTimer = null
}, 0) // Use 0 delay to sync on next tick
}
_submitPipelineQuery(query) {
if (typeof query.text !== 'string' && typeof query.name !== 'string') {
return new Error('A query must have either text or a name. Supplying neither is unsupported.')
}
const previous = this.connection.parsedStatements[query.name]
if (query.text && previous && query.text !== previous) {
return new Error(`Prepared statements must be unique - '${query.name}' was used for a different statement`)
}
if (query.values && !Array.isArray(query.values)) {
return new Error('Query values must be an array')
}
// In pipeline mode, we always use extended query protocol
this.connection.stream.cork && this.connection.stream.cork()
try {
query.preparePipeline(this.connection)
} finally {
this.connection.stream.uncork && this.connection.stream.uncork()
}
return null
}
}
// expose a Query constructor
Client.Query = Query
module.exports = Client