-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathconnection.ts
1016 lines (911 loc) · 31.7 KB
/
connection.ts
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Substantial parts adapted from https://github.com/brianc/node-postgres
* which is licensed as follows:
*
* The MIT License (MIT)
*
* Copyright (c) 2010 - 2019 Brian Carlson
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import {
bold,
BufReader,
BufWriter,
delay,
joinPath,
rgb24,
yellow,
} from "../deps.ts";
import { DeferredStack } from "../utils/deferred.ts";
import { getSocketName, readUInt32BE } from "../utils/utils.ts";
import { PacketWriter } from "./packet.ts";
import {
Message,
type Notice,
parseBackendKeyMessage,
parseCommandCompleteMessage,
parseNoticeMessage,
parseRowDataMessage,
parseRowDescriptionMessage,
} from "./message.ts";
import {
type Query,
QueryArrayResult,
QueryObjectResult,
type QueryResult,
ResultType,
} from "../query/query.ts";
import type { ClientConfiguration } from "./connection_params.ts";
import * as scram from "./scram.ts";
import {
ConnectionError,
ConnectionParamsError,
PostgresError,
} from "../client/error.ts";
import {
AUTHENTICATION_TYPE,
ERROR_MESSAGE,
INCOMING_AUTHENTICATION_MESSAGES,
INCOMING_QUERY_MESSAGES,
INCOMING_TLS_MESSAGES,
} from "./message_code.ts";
import { hashMd5Password } from "./auth.ts";
import { isDebugOptionEnabled } from "../debug.ts";
// Work around unstable limitation
type ConnectOptions =
| { hostname: string; port: number; transport: "tcp" }
| { path: string; transport: "unix" };
function assertSuccessfulStartup(msg: Message) {
switch (msg.type) {
case ERROR_MESSAGE:
throw new PostgresError(parseNoticeMessage(msg));
}
}
function assertSuccessfulAuthentication(auth_message: Message) {
if (auth_message.type === ERROR_MESSAGE) {
throw new PostgresError(parseNoticeMessage(auth_message));
}
if (auth_message.type !== INCOMING_AUTHENTICATION_MESSAGES.AUTHENTICATION) {
throw new Error(`Unexpected auth response: ${auth_message.type}.`);
}
const responseCode = auth_message.reader.readInt32();
if (responseCode !== 0) {
throw new Error(`Unexpected auth response code: ${responseCode}.`);
}
}
function logNotice(notice: Notice) {
if (notice.severity === "INFO") {
console.info(
`[ ${bold(rgb24(notice.severity, 0xff99ff))} ] : ${notice.message}`,
);
} else if (notice.severity === "NOTICE") {
console.info(`[ ${bold(yellow(notice.severity))} ] : ${notice.message}`);
} else if (notice.severity === "WARNING") {
console.warn(
`[ ${bold(rgb24(notice.severity, 0xff9900))} ] : ${notice.message}`,
);
}
}
function logQuery(query: string) {
console.info(`[ ${bold(rgb24("QUERY", 0x00ccff))} ] : ${query}`);
}
function logResults(rows: unknown[]) {
console.info(`[ ${bold(rgb24("RESULTS", 0x00cc00))} ] :`, rows);
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
// TODO
// - Refactor properties to not be lazily initialized
// or to handle their undefined value
export class Connection {
#bufReader!: BufReader;
#bufWriter!: BufWriter;
#conn!: Deno.Conn;
connected = false;
#connection_params: ClientConfiguration;
#message_header = new Uint8Array(5);
#onDisconnection: () => Promise<void>;
#packetWriter = new PacketWriter();
#pid?: number;
#queryLock: DeferredStack<undefined> = new DeferredStack(1, [undefined]);
// TODO
// Find out what the secret key is for
#secretKey?: number;
#tls?: boolean;
#transport?: "tcp" | "socket";
get pid(): number | undefined {
return this.#pid;
}
/** Indicates if the connection is carried over TLS */
get tls(): boolean | undefined {
return this.#tls;
}
/** Indicates the connection protocol used */
get transport(): "tcp" | "socket" | undefined {
return this.#transport;
}
constructor(
connection_params: ClientConfiguration,
disconnection_callback: () => Promise<void>,
) {
this.#connection_params = connection_params;
this.#onDisconnection = disconnection_callback;
}
/**
* Read single message sent by backend
*/
async #readMessage(): Promise<Message> {
// Clear buffer before reading the message type
this.#message_header.fill(0);
await this.#bufReader.readFull(this.#message_header);
const type = decoder.decode(this.#message_header.slice(0, 1));
// TODO
// Investigate if the ascii terminator is the best way to check for a broken
// session
if (type === "\x00") {
// This error means that the database terminated the session without notifying
// the library
// TODO
// This will be removed once we move to async handling of messages by the frontend
// However, unnotified disconnection will remain a possibility, that will likely
// be handled in another place
throw new ConnectionError("The session was terminated unexpectedly");
}
const length = readUInt32BE(this.#message_header, 1) - 4;
const body = new Uint8Array(length);
await this.#bufReader.readFull(body);
return new Message(type, length, body);
}
async #serverAcceptsTLS(): Promise<boolean> {
const writer = this.#packetWriter;
writer.clear();
writer.addInt32(8).addInt32(80877103).join();
await this.#bufWriter.write(writer.flush());
await this.#bufWriter.flush();
const response = new Uint8Array(1);
await this.#conn.read(response);
switch (String.fromCharCode(response[0])) {
case INCOMING_TLS_MESSAGES.ACCEPTS_TLS:
return true;
case INCOMING_TLS_MESSAGES.NO_ACCEPTS_TLS:
return false;
default:
throw new Error(
`Could not check if server accepts SSL connections, server responded with: ${response}`,
);
}
}
/** https://www.postgresql.org/docs/14/protocol-flow.html#id-1.10.5.7.3 */
async #sendStartupMessage(): Promise<Message> {
const writer = this.#packetWriter;
writer.clear();
// protocol version - 3.0, written as
writer.addInt16(3).addInt16(0);
// explicitly set utf-8 encoding
writer.addCString("client_encoding").addCString("'utf-8'");
// TODO: recognize other parameters
writer.addCString("user").addCString(this.#connection_params.user);
writer.addCString("database").addCString(this.#connection_params.database);
writer
.addCString("application_name")
.addCString(this.#connection_params.applicationName);
const connection_options = Object.entries(this.#connection_params.options);
if (connection_options.length > 0) {
// The database expects options in the --key=value
writer
.addCString("options")
.addCString(
connection_options
.map(([key, value]) => `--${key}=${value}`)
.join(" "),
);
}
// terminator after all parameters were writter
writer.addCString("");
const bodyBuffer = writer.flush();
const bodyLength = bodyBuffer.length + 4;
writer.clear();
const finalBuffer = writer.addInt32(bodyLength).add(bodyBuffer).join();
await this.#bufWriter.write(finalBuffer);
await this.#bufWriter.flush();
return await this.#readMessage();
}
async #openConnection(options: ConnectOptions) {
// @ts-expect-error This will throw in runtime if the options passed to it are socket related and deno is running
// on stable
this.#conn = await Deno.connect(options);
this.#bufWriter = new BufWriter(this.#conn);
this.#bufReader = new BufReader(this.#conn);
}
async #openSocketConnection(path: string, port: number) {
if (Deno.build.os === "windows") {
throw new Error("Socket connection is only available on UNIX systems");
}
const socket = await Deno.stat(path);
if (socket.isFile) {
await this.#openConnection({ path, transport: "unix" });
} else {
const socket_guess = joinPath(path, getSocketName(port));
try {
await this.#openConnection({
path: socket_guess,
transport: "unix",
});
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
throw new ConnectionError(
`Could not open socket in path "${socket_guess}"`,
);
}
throw e;
}
}
}
async #openTlsConnection(
connection: Deno.TcpConn,
options: { hostname: string; caCerts: string[] },
) {
this.#conn = await Deno.startTls(connection, options);
this.#bufWriter = new BufWriter(this.#conn);
this.#bufReader = new BufReader(this.#conn);
}
#resetConnectionMetadata() {
this.connected = false;
this.#packetWriter = new PacketWriter();
this.#pid = undefined;
this.#queryLock = new DeferredStack(1, [undefined]);
this.#secretKey = undefined;
this.#tls = undefined;
this.#transport = undefined;
}
#closeConnection() {
try {
this.#conn.close();
} catch (_e) {
// Swallow if the connection had errored or been closed beforehand
} finally {
this.#resetConnectionMetadata();
}
}
async #startup() {
this.#closeConnection();
const {
host_type,
hostname,
port,
tls: { caCertificates, enabled: tls_enabled, enforce: tls_enforced },
} = this.#connection_params;
if (host_type === "socket") {
await this.#openSocketConnection(hostname, port);
this.#tls = undefined;
this.#transport = "socket";
} else {
// A BufWriter needs to be available in order to check if the server accepts TLS connections
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
this.#transport = "tcp";
if (tls_enabled) {
// If TLS is disabled, we don't even try to connect.
const accepts_tls = await this.#serverAcceptsTLS().catch((e) => {
// Make sure to close the connection if the TLS validation throws
this.#closeConnection();
throw e;
});
// https://www.postgresql.org/docs/14/protocol-flow.html#id-1.10.5.7.11
if (accepts_tls) {
try {
// TODO: handle connection type without castinggaa
// https://github.com/denoland/deno/issues/10200
await this.#openTlsConnection(this.#conn as Deno.TcpConn, {
hostname,
caCerts: caCertificates,
});
this.#tls = true;
} catch (e) {
if (!tls_enforced) {
console.error(
bold(yellow("TLS connection failed with message: ")) +
e.message +
"\n" +
bold("Defaulting to non-encrypted connection"),
);
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
} else {
throw e;
}
}
} else if (tls_enforced) {
// Make sure to close the connection before erroring
this.#closeConnection();
throw new Error(
"The server isn't accepting TLS connections. Change the client configuration so TLS configuration isn't required to connect",
);
}
}
}
try {
let startup_response;
try {
startup_response = await this.#sendStartupMessage();
} catch (e) {
// Make sure to close the connection before erroring or reseting
this.#closeConnection();
if (e instanceof Deno.errors.InvalidData && tls_enabled) {
if (tls_enforced) {
throw new Error(
"The certificate used to secure the TLS connection is invalid: " +
e.message,
);
} else {
console.error(
bold(yellow("TLS connection failed with message: ")) +
e.message +
"\n" +
bold("Defaulting to non-encrypted connection"),
);
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
this.#transport = "tcp";
startup_response = await this.#sendStartupMessage();
}
} else {
throw e;
}
}
assertSuccessfulStartup(startup_response);
await this.#authenticate(startup_response);
// Handle connection status
// Process connection initialization messages until connection returns ready
let message = await this.#readMessage();
while (message.type !== INCOMING_AUTHENTICATION_MESSAGES.READY) {
switch (message.type) {
// Connection error (wrong database or user)
case ERROR_MESSAGE:
await this.#processErrorUnsafe(message, false);
break;
case INCOMING_AUTHENTICATION_MESSAGES.BACKEND_KEY: {
const { pid, secret_key } = parseBackendKeyMessage(message);
this.#pid = pid;
this.#secretKey = secret_key;
break;
}
case INCOMING_AUTHENTICATION_MESSAGES.PARAMETER_STATUS:
break;
case INCOMING_AUTHENTICATION_MESSAGES.NOTICE:
break;
default:
throw new Error(`Unknown response for startup: ${message.type}`);
}
message = await this.#readMessage();
}
this.connected = true;
} catch (e) {
this.#closeConnection();
throw e;
}
}
/**
* Calling startup on a connection twice will create a new session and overwrite the previous one
*
* @param is_reconnection This indicates whether the startup should behave as if there was
* a connection previously established, or if it should attempt to create a connection first
*
* https://www.postgresql.org/docs/14/protocol-flow.html#id-1.10.5.7.3
*/
async startup(is_reconnection: boolean) {
if (is_reconnection && this.#connection_params.connection.attempts === 0) {
throw new Error(
"The client has been disconnected from the database. Enable reconnection in the client to attempt reconnection after failure",
);
}
let reconnection_attempts = 0;
const max_reconnections = this.#connection_params.connection.attempts;
let error: Error | undefined;
// If no connection has been established and the reconnection attempts are
// set to zero, attempt to connect at least once
if (!is_reconnection && this.#connection_params.connection.attempts === 0) {
try {
await this.#startup();
} catch (e) {
error = e;
}
} else {
let interval =
typeof this.#connection_params.connection.interval === "number"
? this.#connection_params.connection.interval
: 0;
while (reconnection_attempts < max_reconnections) {
// Don't wait for the interval on the first connection
if (reconnection_attempts > 0) {
if (
typeof this.#connection_params.connection.interval === "function"
) {
interval = this.#connection_params.connection.interval(interval);
}
if (interval > 0) {
await delay(interval);
}
}
try {
await this.#startup();
break;
} catch (e) {
// TODO
// Eventually distinguish between connection errors and normal errors
reconnection_attempts++;
if (reconnection_attempts === max_reconnections) {
error = e;
}
}
}
}
if (error) {
await this.end();
throw error;
}
}
/**
* Will attempt to authenticate with the database using the provided
* password credentials
*/
async #authenticate(authentication_request: Message) {
const authentication_type = authentication_request.reader.readInt32();
let authentication_result: Message;
switch (authentication_type) {
case AUTHENTICATION_TYPE.NO_AUTHENTICATION:
authentication_result = authentication_request;
break;
case AUTHENTICATION_TYPE.CLEAR_TEXT:
authentication_result = await this.#authenticateWithClearPassword();
break;
case AUTHENTICATION_TYPE.MD5: {
const salt = authentication_request.reader.readBytes(4);
authentication_result = await this.#authenticateWithMd5(salt);
break;
}
case AUTHENTICATION_TYPE.SCM:
throw new Error(
"Database server expected SCM authentication, which is not supported at the moment",
);
case AUTHENTICATION_TYPE.GSS_STARTUP:
throw new Error(
"Database server expected GSS authentication, which is not supported at the moment",
);
case AUTHENTICATION_TYPE.GSS_CONTINUE:
throw new Error(
"Database server expected GSS authentication, which is not supported at the moment",
);
case AUTHENTICATION_TYPE.SSPI:
throw new Error(
"Database server expected SSPI authentication, which is not supported at the moment",
);
case AUTHENTICATION_TYPE.SASL_STARTUP:
authentication_result = await this.#authenticateWithSasl();
break;
default:
throw new Error(`Unknown auth message code ${authentication_type}`);
}
await assertSuccessfulAuthentication(authentication_result);
}
async #authenticateWithClearPassword(): Promise<Message> {
this.#packetWriter.clear();
const password = this.#connection_params.password || "";
const buffer = this.#packetWriter.addCString(password).flush(0x70);
await this.#bufWriter.write(buffer);
await this.#bufWriter.flush();
return this.#readMessage();
}
async #authenticateWithMd5(salt: Uint8Array): Promise<Message> {
this.#packetWriter.clear();
if (!this.#connection_params.password) {
throw new ConnectionParamsError(
"Attempting MD5 authentication with unset password",
);
}
const password = await hashMd5Password(
this.#connection_params.password,
this.#connection_params.user,
salt,
);
const buffer = this.#packetWriter.addCString(password).flush(0x70);
await this.#bufWriter.write(buffer);
await this.#bufWriter.flush();
return this.#readMessage();
}
/**
* https://www.postgresql.org/docs/14/sasl-authentication.html
*/
async #authenticateWithSasl(): Promise<Message> {
if (!this.#connection_params.password) {
throw new ConnectionParamsError(
"Attempting SASL auth with unset password",
);
}
const client = new scram.Client(
this.#connection_params.user,
this.#connection_params.password,
);
const utf8 = new TextDecoder("utf-8");
// SASLInitialResponse
const clientFirstMessage = client.composeChallenge();
this.#packetWriter.clear();
this.#packetWriter.addCString("SCRAM-SHA-256");
this.#packetWriter.addInt32(clientFirstMessage.length);
this.#packetWriter.addString(clientFirstMessage);
this.#bufWriter.write(this.#packetWriter.flush(0x70));
this.#bufWriter.flush();
const maybe_sasl_continue = await this.#readMessage();
switch (maybe_sasl_continue.type) {
case INCOMING_AUTHENTICATION_MESSAGES.AUTHENTICATION: {
const authentication_type = maybe_sasl_continue.reader.readInt32();
if (authentication_type !== AUTHENTICATION_TYPE.SASL_CONTINUE) {
throw new Error(
`Unexpected authentication type in SASL negotiation: ${authentication_type}`,
);
}
break;
}
case ERROR_MESSAGE:
throw new PostgresError(parseNoticeMessage(maybe_sasl_continue));
default:
throw new Error(
`Unexpected message in SASL negotiation: ${maybe_sasl_continue.type}`,
);
}
const sasl_continue = utf8.decode(
maybe_sasl_continue.reader.readAllBytes(),
);
await client.receiveChallenge(sasl_continue);
this.#packetWriter.clear();
this.#packetWriter.addString(await client.composeResponse());
this.#bufWriter.write(this.#packetWriter.flush(0x70));
this.#bufWriter.flush();
const maybe_sasl_final = await this.#readMessage();
switch (maybe_sasl_final.type) {
case INCOMING_AUTHENTICATION_MESSAGES.AUTHENTICATION: {
const authentication_type = maybe_sasl_final.reader.readInt32();
if (authentication_type !== AUTHENTICATION_TYPE.SASL_FINAL) {
throw new Error(
`Unexpected authentication type in SASL finalization: ${authentication_type}`,
);
}
break;
}
case ERROR_MESSAGE:
throw new PostgresError(parseNoticeMessage(maybe_sasl_final));
default:
throw new Error(
`Unexpected message in SASL finalization: ${maybe_sasl_continue.type}`,
);
}
const sasl_final = utf8.decode(maybe_sasl_final.reader.readAllBytes());
await client.receiveResponse(sasl_final);
// Return authentication result
return this.#readMessage();
}
async #simpleQuery(query: Query<ResultType.ARRAY>): Promise<QueryArrayResult>;
async #simpleQuery(
query: Query<ResultType.OBJECT>,
): Promise<QueryObjectResult>;
async #simpleQuery(query: Query<ResultType>): Promise<QueryResult> {
this.#packetWriter.clear();
const buffer = this.#packetWriter.addCString(query.text).flush(0x51);
await this.#bufWriter.write(buffer);
await this.#bufWriter.flush();
let result;
if (query.result_type === ResultType.ARRAY) {
result = new QueryArrayResult(query);
} else {
result = new QueryObjectResult(query);
}
let error: Error | undefined;
let current_message = await this.#readMessage();
// Process messages until ready signal is sent
// Delay error handling until after the ready signal is sent
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE:
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
case INCOMING_QUERY_MESSAGES.COMMAND_COMPLETE: {
result.handleCommandComplete(
parseCommandCompleteMessage(current_message),
);
break;
}
case INCOMING_QUERY_MESSAGES.DATA_ROW: {
const row_data = parseRowDataMessage(current_message);
try {
result.insertRow(row_data, this.#connection_params.controls);
} catch (e) {
error = e;
}
break;
}
case INCOMING_QUERY_MESSAGES.EMPTY_QUERY:
break;
case INCOMING_QUERY_MESSAGES.NOTICE_WARNING: {
const notice = parseNoticeMessage(current_message);
if (
isDebugOptionEnabled(
"notices",
this.#connection_params.controls?.debug,
)
) {
logNotice(notice);
}
result.warnings.push(notice);
break;
}
case INCOMING_QUERY_MESSAGES.PARAMETER_STATUS:
break;
case INCOMING_QUERY_MESSAGES.READY:
break;
case INCOMING_QUERY_MESSAGES.ROW_DESCRIPTION: {
result.loadColumnDescriptions(
parseRowDescriptionMessage(current_message),
);
break;
}
default:
throw new Error(
`Unexpected simple query message: ${current_message.type}`,
);
}
current_message = await this.#readMessage();
}
if (error) throw error;
return result;
}
async #appendQueryToMessage<T extends ResultType>(query: Query<T>) {
this.#packetWriter.clear();
const buffer = this.#packetWriter
.addCString("") // TODO: handle named queries (config.name)
.addCString(query.text)
.addInt16(0)
.flush(0x50);
await this.#bufWriter.write(buffer);
}
async #appendArgumentsToMessage<T extends ResultType>(query: Query<T>) {
this.#packetWriter.clear();
const hasBinaryArgs = query.args.some((arg) => arg instanceof Uint8Array);
// bind statement
this.#packetWriter.clear();
this.#packetWriter
.addCString("") // TODO: unnamed portal
.addCString(""); // TODO: unnamed prepared statement
if (hasBinaryArgs) {
this.#packetWriter.addInt16(query.args.length);
query.args.forEach((arg) => {
this.#packetWriter.addInt16(arg instanceof Uint8Array ? 1 : 0);
});
} else {
this.#packetWriter.addInt16(0);
}
this.#packetWriter.addInt16(query.args.length);
query.args.forEach((arg) => {
if (arg === null || typeof arg === "undefined") {
this.#packetWriter.addInt32(-1);
} else if (arg instanceof Uint8Array) {
this.#packetWriter.addInt32(arg.length);
this.#packetWriter.add(arg);
} else {
const byteLength = encoder.encode(arg).length;
this.#packetWriter.addInt32(byteLength);
this.#packetWriter.addString(arg);
}
});
this.#packetWriter.addInt16(0);
const buffer = this.#packetWriter.flush(0x42);
await this.#bufWriter.write(buffer);
}
/**
* This function appends the query type (in this case prepared statement)
* to the message
*/
async #appendDescribeToMessage() {
this.#packetWriter.clear();
const buffer = this.#packetWriter.addCString("P").flush(0x44);
await this.#bufWriter.write(buffer);
}
async #appendExecuteToMessage() {
this.#packetWriter.clear();
const buffer = this.#packetWriter
.addCString("") // unnamed portal
.addInt32(0)
.flush(0x45);
await this.#bufWriter.write(buffer);
}
async #appendSyncToMessage() {
this.#packetWriter.clear();
const buffer = this.#packetWriter.flush(0x53);
await this.#bufWriter.write(buffer);
}
// TODO
// Rename process function to a more meaningful name and move out of class
async #processErrorUnsafe(msg: Message, recoverable = true) {
const error = new PostgresError(parseNoticeMessage(msg));
if (recoverable) {
let maybe_ready_message = await this.#readMessage();
while (maybe_ready_message.type !== INCOMING_QUERY_MESSAGES.READY) {
maybe_ready_message = await this.#readMessage();
}
}
throw error;
}
/**
* https://www.postgresql.org/docs/14/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
*/
async #preparedQuery(
query: Query<ResultType.ARRAY>,
): Promise<QueryArrayResult>;
async #preparedQuery(
query: Query<ResultType.OBJECT>,
): Promise<QueryObjectResult>;
async #preparedQuery<T extends ResultType>(
query: Query<T>,
): Promise<QueryResult> {
// The parse messages declares the statement, query arguments and the cursor used in the transaction
// The database will respond with a parse response
await this.#appendQueryToMessage(query);
await this.#appendArgumentsToMessage(query);
// The describe message will specify the query type and the cursor in which the current query will be running
// The database will respond with a bind response
await this.#appendDescribeToMessage();
// The execute response contains the portal in which the query will be run and how many rows should it return
await this.#appendExecuteToMessage();
await this.#appendSyncToMessage();
// send all messages to backend
await this.#bufWriter.flush();
let result;
if (query.result_type === ResultType.ARRAY) {
result = new QueryArrayResult(query);
} else {
result = new QueryObjectResult(query);
}
let error: Error | undefined;
let current_message = await this.#readMessage();
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE: {
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
}
case INCOMING_QUERY_MESSAGES.BIND_COMPLETE:
break;
case INCOMING_QUERY_MESSAGES.COMMAND_COMPLETE: {
result.handleCommandComplete(
parseCommandCompleteMessage(current_message),
);
break;
}
case INCOMING_QUERY_MESSAGES.DATA_ROW: {
const row_data = parseRowDataMessage(current_message);
try {
result.insertRow(row_data, this.#connection_params.controls);
} catch (e) {
error = e;
}
break;
}
case INCOMING_QUERY_MESSAGES.NO_DATA:
break;
case INCOMING_QUERY_MESSAGES.NOTICE_WARNING: {
const notice = parseNoticeMessage(current_message);
if (
isDebugOptionEnabled(
"notices",
this.#connection_params.controls?.debug,
)
) {
logNotice(notice);
}
result.warnings.push(notice);
break;
}
case INCOMING_QUERY_MESSAGES.PARAMETER_STATUS:
break;
case INCOMING_QUERY_MESSAGES.PARSE_COMPLETE:
// TODO: add to already parsed queries if
// query has name, so it's not parsed again
break;
case INCOMING_QUERY_MESSAGES.ROW_DESCRIPTION: {
result.loadColumnDescriptions(
parseRowDescriptionMessage(current_message),
);
break;
}
default:
throw new Error(
`Unexpected prepared query message: ${current_message.type}`,
);
}
current_message = await this.#readMessage();
}
if (error) throw error;
return result;
}
async query(query: Query<ResultType.ARRAY>): Promise<QueryArrayResult>;
async query(query: Query<ResultType.OBJECT>): Promise<QueryObjectResult>;
async query(query: Query<ResultType>): Promise<QueryResult> {
if (!this.connected) {
await this.startup(true);
}
await this.#queryLock.pop();
try {
if (
isDebugOptionEnabled("queries", this.#connection_params.controls?.debug)
) {
logQuery(query.text);
}
let result: QueryArrayResult | QueryObjectResult;
if (query.args.length === 0) {
result = await this.#simpleQuery(query);
} else {
result = await this.#preparedQuery(query);
}
if (
isDebugOptionEnabled("results", this.#connection_params.controls?.debug)
) {
logResults(result.rows);
}
return result;
} catch (e) {
if (e instanceof ConnectionError) {
await this.end();
}
throw e;
} finally {
this.#queryLock.push(undefined);
}
}