-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.ts
1214 lines (1071 loc) · 39.8 KB
/
protocol.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
/**
* Deno implementation of low level Postgresql frontend-backend protocol
* https://www.postgresql.org/docs/13/protocol-message-formats.html
*/
import {
MESSAGE_CODE,
MESSAGE_NAME,
AUTHENTICATION_CODE,
ERROR_MESSAGE,
NOTICE_MESSAGE,
PARAMETER_FORMAT_CODE,
PORTAL_STATEMENT_TYPE,
TRANSACTION_STATUS
} from './constants.ts'
import {
BufferReader,
BufferWriter,
// decode,
encode
} from './buffer.ts'
import {
PacketError
} from './error.ts'
import {
TypeWriter
} from './types.ts'
// deno-lint-ignore no-unused-vars
const DEBUG = false
/**
* be careful pass reader: BuffferReader
* please used BufReader.copy() before pass reader!
*/
export function dumpPacket(reader: BufferReader) {
function printable(digit: number): string {
if (32 <= digit && digit < 127) {
return String.fromCharCode(digit)
}
return '.'
}
console.log(`packetLength: ${reader.buffer.length}`)
// packet detail
let printUints = [], printTexts = ''
while (!reader.ended) {
for (let current = 0; current < 16; current++) {
if (reader.ended) return
const int = reader.readUint8(), hex = Number(int).toString(16)
printUints.push(hex.length === 1 ? ['0', hex].join('') : hex)
if (current === 7) printUints.push(' ')
printTexts += printable(int)
}
console.log(printUints.join(' ') + ' ' + printTexts)
printUints = [], printTexts = ''
}
}
//
// packet reader
//
// backend(B)/server --> frontend(F)/client
export class PacketReader {
constructor(
public packetName: MESSAGE_NAME,
public packetCode: MESSAGE_CODE,
public packetLength: number
) {}
// deno-lint-ignore no-unused-vars
read(reader: BufferReader) {
throw new Error('not implemented')
}
}
export class AuthenticationReader extends PacketReader {
read = (reader: BufferReader): {
packetName: MESSAGE_NAME,
packetCode: MESSAGE_CODE,
packetLength: number
// deno-lint-ignore no-explicit-any
} & any => {
// read authentication code for authenticate message
const code = reader.readInt32() as AUTHENTICATION_CODE
switch (code) {
case AUTHENTICATION_CODE.Ok:
this.packetName = MESSAGE_NAME.AuthenticationOk
break
case AUTHENTICATION_CODE.KerberosV5:
this.packetName = MESSAGE_NAME.AuthenticationKerberosV5
break
case AUTHENTICATION_CODE.CleartextPassword:
if (this.packetLength === 8) {
this.packetName = MESSAGE_NAME.AuthenticationCleartextPassword
}
break
case AUTHENTICATION_CODE.MD5Password:
if (this.packetLength === 12) {
this.packetName = MESSAGE_NAME.AuthenticationMD5Password
const salt: Uint8Array = reader.read(4)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
salt: salt
}
}
break
case AUTHENTICATION_CODE.SCMCredential:
this.packetName = MESSAGE_NAME.AuthenticationSCMCredential
break
case AUTHENTICATION_CODE.GSS:
this.packetName = MESSAGE_NAME.AuthenticationGSS
break
case AUTHENTICATION_CODE.GSSContinue: { // deno-lint no-case-declarations
this.packetName = MESSAGE_NAME.AuthenticationGSSContinue
// except Int32(packetLength) + Int32(AUTHENTICATION_CODE) = 8
const data: Uint8Array = reader.read(this.packetLength - 8)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
data: data
}
// with return statement, without break statement
}
case AUTHENTICATION_CODE.SSPI:
this.packetName = MESSAGE_NAME.AuthenticationSSPI
break
case AUTHENTICATION_CODE.SASL: { // deno-lint no-case-declarations
this.packetName = MESSAGE_NAME.AuthenticationSASL
const mechanisms = []
let mechanism: string
do {
mechanism = reader.readSlice(0x00)
if (mechanism) {
mechanisms.push(mechanism)
}
} while (mechanism)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
mechanisms: mechanisms
}
// with return statement, without break statement
}
case AUTHENTICATION_CODE.SASLContinue: { // deno-lint no-case-declarations
this.packetName = MESSAGE_NAME.AuthenticationSASLContinue
// except Int32(packetLength) + Int32(AUTHENTICATION_CODE) = 8
const data: Uint8Array = reader.read(this.packetLength - 8)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
data: data
}
// with return statement, without break statement
}
case AUTHENTICATION_CODE.SASLFinal: { // deno-lint no-case-declarations
this.packetName = MESSAGE_NAME.AuthenticationSASLFinal
// except Int32(packetLength) + Int32(AUTHENTICATION_CODE) = 8
const data = reader.read(this.packetLength - 8)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
data: data
}
// with return statement, without break statement
}
default:
throw new PacketError(`unknow authenticate message with ${code}`)
}
return this
}
}
export class BackendKeyDataReader extends PacketReader {
read(reader: BufferReader) {
const processId = reader.readInt32()
const secretKey = reader.readInt32()
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
processId: processId,
secretKey: secretKey
}
}
}
export class BindCompleteReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.BindComplete,
/* code */MESSAGE_CODE.BindComplete,
/* length */5)
}
}
export class CloseCompleteReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.CloseComplete,
/* code */MESSAGE_CODE.CloseComplete,
/* length */5)
}
}
const COMMAND_TAG_REGEXP = /^([A-Za-z]+)(?: (\d+))?(?: (\d+))?/
export class CommandCompleteReader extends PacketReader {
/**
* CommandComplete returns:
* For an INSERT command, the tag is INSERT oid rows, where rows is the number of rows inserted. oid used to be the object ID of the inserted row if rows was 1 and the target table had OIDs, but OIDs system columns are not supported anymore; therefore oid is always 0.
* For a DELETE command, the tag is DELETE rows where rows is the number of rows deleted.
* For an UPDATE command, the tag is UPDATE rows where rows is the number of rows updated.
* For a SELECT or CREATE TABLE AS command, the tag is SELECT rows where rows is the number of rows retrieved.
* For a MOVE command, the tag is MOVE rows where rows is the number of rows the cursor's position has been changed by.
* For a FETCH command, the tag is FETCH rows where rows is the number of rows that have been retrieved from the cursor.
* For a COPY command, the tag is COPY rows where rows is the number of rows copied. (Note: the row count appears only in PostgreSQL 8.2 and later.)
*/
read(reader: BufferReader) {
const commandTag = reader.readSlice(0x00)
const metches = COMMAND_TAG_REGEXP.exec(commandTag)
if (metches) {
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
commandTag: commandTag,
// INSERT | DELETE | UPDATE | DELECT | MOVE | FETCH | COPY
command: metches[1],
// metches[3] = command oid rows
// metches[2] = command rows
count: metches[3] ? parseInt(metches[3], 10) : parseInt(metches[2], 10)
}
} else {
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
commandTag: commandTag
}
}
}
}
export class CopyDataReader extends PacketReader {
read(reader: BufferReader) {
// except Int32(packetLength) + Int32(AUTHENTICATION_CODE) = 8
const data: Uint8Array = reader.read(this.packetLength - 8)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
data: data
}
}
}
export class CopyDoneReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.CopyDone,
/* code */MESSAGE_CODE.CopyDone,
/* length */4)
}
}
export class CopyInResponseReader extends PacketReader {
read(reader: BufferReader) {
const format = reader.readInt8() as PARAMETER_FORMAT_CODE // 0x00 = 'text', 0x01 = 'binary'
const count = reader.readInt16() // columns count
const codes: PARAMETER_FORMAT_CODE[] = [] // the format codes to be used for each column
for (let index = 0; index < count; index++) {
codes[index] = reader.readInt16()
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
format: format, // 0x00 = 'text', 0x01 = 'binary'
count: count, // columns count
codes: codes // each must presently be zero (text) or one (binary)
}
}
}
export class CopyOutResponseReader extends PacketReader {
read(reader: BufferReader) {
const format = reader.readInt8() as PARAMETER_FORMAT_CODE // 0x00 = 'text', 0x01 = 'binary'
const count = reader.readInt16() // columns count
const codes: PARAMETER_FORMAT_CODE[] = [] // the format codes to be used for each column
for (let index = 0; index < count; index++) {
codes[index] = reader.readInt16()
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
format: format, // 0x00 = 'text', 0x01 = 'binary'
count: count, // columns count
codes: codes // each must presently be zero (text) or one (binary)
}
}
}
export class CopyBothResponseReader extends PacketReader {
read(reader: BufferReader) {
const format = reader.readInt8() as PARAMETER_FORMAT_CODE // 0x00 = 'text', 0x01 = 'binary'
const count = reader.readInt16() // columns count
const codes: PARAMETER_FORMAT_CODE[] = [] // the format codes to be used for each column
for (let index = 0; index < count; index++) {
codes[index] = reader.readInt16()
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
format: format, // 0x00 = 'text', 0x01 = 'binary'
count: count, // columns count
codes: codes // each must presently be zero (text) or one (binary)
}
}
}
export class DataRowReader extends PacketReader {
read(reader: BufferReader) {
const count = reader.readInt16() // fields count
const fields: (Uint8Array | null)[] = new Array(count)
for (let index = 0; index < count; index++) {
const length = reader.readInt32()
fields[index] = length === -1 ? null : reader.read(length)
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
count: count, // fields count
fields: fields // fields Array(Uint8Array[])
}
}
}
export class EmptyQueryResponseReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.EmptyQueryResponse,
/* code */MESSAGE_CODE.EmptyQueryResponse,
/* length */4)
}
}
export class ErrorResponseReader extends PacketReader {
read(reader: BufferReader) {
const data: Record<string, string> = {}
let name = reader.readString(1)
while (name !== '\0') {
data[name] = reader.readSlice(0x00)
name = reader.readString(1)
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
// error message
severity : data[ERROR_MESSAGE.SEVERITY],
code : data[ERROR_MESSAGE.CODE],
message : data[ERROR_MESSAGE.MESSAGE],
detail : data[ERROR_MESSAGE.DETAIL],
hint : data[ERROR_MESSAGE.HINT],
position : data[ERROR_MESSAGE.POSITION],
internalPosition : data[ERROR_MESSAGE.INTERNAL_POSITION],
internalQuery : data[ERROR_MESSAGE.INTERNAL_QUERY],
where : data[ERROR_MESSAGE.WHERE],
schema : data[ERROR_MESSAGE.SCHEMA],
table : data[ERROR_MESSAGE.TABLE],
column : data[ERROR_MESSAGE.COLUMN],
dataType : data[ERROR_MESSAGE.DATA_TYPE],
constraint : data[ERROR_MESSAGE.CONSTRAINT],
file : data[ERROR_MESSAGE.FILE],
line : data[ERROR_MESSAGE.LINE],
routine : data[ERROR_MESSAGE.ROUTINE]
}
}
}
export class FunctionCallResponseReader extends PacketReader {
read(reader: BufferReader) {
/**
* the length of the function result value, in bytes (this count does not include itself).
* can be zero. as a special case, -1 indicates a NULL function result.
* no value bytes follow in the NULL case.
*/
const length = reader.readInt32()
const result = length === -1 ? null : reader.read(length)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
length: length,
result: result
}
}
}
export class NegotiateProtocolVersionReader extends PacketReader {
read(reader: BufferReader) {
// Newest minor protocol version supported by the server for
// the major protocol version requested by the client
const newestVersion = reader.readInt32()
const count = reader.readInt32()
// for protocol option not recognized by the server, just option name
const options: string[] = []
for (let index = 0; index < count; index++) {
options[index] = reader.readSlice(0x00)
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
newestVersion: newestVersion,
count: count,
options: options
}
}
}
export class NoDataReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.NoData,
/* code */MESSAGE_CODE.NoData,
/* length */5)
}
}
export class NoticeResponseReader extends PacketReader {
read(reader: BufferReader) {
const data: Record<string, string> = {}
let name = reader.readString(1)
while (name !== '\0') {
data[name] = reader.readSlice(0x00)
name = reader.readString(1)
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
// notice message field
severity : data[NOTICE_MESSAGE.SEVERITY],
code : data[NOTICE_MESSAGE.CODE],
message : data[NOTICE_MESSAGE.MESSAGE],
detail : data[NOTICE_MESSAGE.DETAIL],
hint : data[NOTICE_MESSAGE.HINT],
position : data[NOTICE_MESSAGE.POSITION],
internalPosition : data[NOTICE_MESSAGE.INTERNAL_POSITION],
internalQuery : data[NOTICE_MESSAGE.INTERNAL_QUERY],
where : data[NOTICE_MESSAGE.WHERE],
schema : data[NOTICE_MESSAGE.SCHEMA],
table : data[NOTICE_MESSAGE.TABLE],
column : data[NOTICE_MESSAGE.COLUMN],
dataType : data[NOTICE_MESSAGE.DATA_TYPE],
constraint : data[NOTICE_MESSAGE.CONSTRAINT],
file : data[NOTICE_MESSAGE.FILE],
line : data[NOTICE_MESSAGE.LINE],
routine : data[NOTICE_MESSAGE.ROUTINE]
}
}
}
export class NotificationResponseReader extends PacketReader {
read(reader: BufferReader) {
const processId = reader.readInt32()
// the name of the channel that the notify has been raised on
const channel = reader.readSlice(0x00)
// the “payload” string passed from the notifying process
const payload = reader.readSlice(0x00)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
processId: processId,
channel: channel,
payload: payload
}
}
}
export class ParameterDescriptionReader extends PacketReader {
read(reader: BufferReader) {
// the number of parameters used by the statement (can be zero)
const count = reader.readInt16()
// specifies the object ID of the parameter data type
const dataTypeIds = []
for (let index = 0; index < count; index++) {
dataTypeIds[index] = reader.readInt32()
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
count: count,
dataTypeIds: dataTypeIds
}
}
}
export class ParameterStatusReader extends PacketReader {
read(reader: BufferReader) {
// the name of the run-time parameter being reported.
const name = reader.readSlice(0x00)
// the current value of the parameter.
const value = reader.readSlice(0x00)
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
name: name,
value: value
}
}
}
export class ParseCompleteReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.ParseComplete,
/* code */MESSAGE_CODE.ParseComplete,
/* length */5)
}
}
export class PortalSuspendedReader extends PacketReader {
constructor() {
super(
/* name */MESSAGE_NAME.PortalSuspended,
/* code */MESSAGE_CODE.PortalSuspended,
/* length */5)
}
}
export class ReadyForQueryReader extends PacketReader {
read(reader: BufferReader) {
/**
* current backend transaction status indicator. Possible values are:
* 0x49 = 73 = 'I' if idle (not in a transaction block);
* 0x54 = 84 = 'T' if in a transaction block;
* 0x45 = 69 = 'E' if in a failed transaction block (queries will be rejected until block is ended)
*/
const transactionStatus = reader.readInt8() as TRANSACTION_STATUS
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
transactionStatus: transactionStatus
}
}
}
export class RowDescriptionReader extends PacketReader {
readField(reader: BufferReader) {
const name = reader.readSlice(0x00)
const tableId = reader.readInt32()
const columnId = reader.readInt16()
const dataTypeId = reader.readInt32()
const dataTypeSize = reader.readInt16()
const dataTypeModifier = reader.readInt32()
const format = reader.readInt16() as PARAMETER_FORMAT_CODE // 0x00 = 'text', 0x01 = 'binary'
return {
name: name,
tableId: tableId,
columnId: columnId,
dataTypeId: dataTypeId,
dataTypeSize: dataTypeSize,
dataTypeModifier: dataTypeModifier,
format: format
}
}
read(reader: BufferReader) {
// specifies the number of fields in a row (can be zero)
const count = reader.readInt16()
const fields = new Array(count)
for (let index = 0; index < count; index++) {
fields[index] = this.readField(reader)
}
return {
packetName: this.packetName,
packetCode: this.packetCode,
packetLength: this.packetLength,
count: count,
fields: fields
}
}
}
//
// packet writer
//
// frontend(F)/client --> backend(B)/server
export class PacketWriter {
constructor(
public packetName: MESSAGE_NAME,
public packetCode: MESSAGE_CODE,
public packetLength: number
) {}
// deno-lint-ignore no-unused-vars
write(writer: BufferWriter): Uint8Array {
throw new Error('not implemented')
}
/**
* !!!call this last after all other packet build flows!!!
*
* almost all case, `buffer` pass to BufferWriter known its length
* so here just except BYTE1(packetCode) + Int32(packetLength) = 5
*/
writeHeader(writer: BufferWriter) {
// restore current body buffer
const bodyLength = writer.index
const bodyBuffer = writer.buffer.slice(0, bodyLength)
// packetLength
this.packetLength = bodyLength + 4
// rebuild the packet
writer.enlarge(5)
writer.index = 0
// set BYTE1(packetCode) + Int32(packetLength) = 5
writer.writeUint8(this.packetCode) // Byte1() = 1 byte
writer.writeInt32(this.packetLength) // Int32() = 4 byte
// rewrite body buffer
writer.write(bodyBuffer)
writer.index = bodyLength + 5
}
}
const typeWriter = new TypeWriter()
export class BindWriter extends PacketWriter {
constructor(
public portal?: string,
public statement?: string,
public values?: unknown[],
public valueMapper?: (value: unknown) => null | string | Uint8Array
) {
super(
/* name */MESSAGE_NAME.Bind,
/* code */MESSAGE_CODE.Bind,
/* length */1024 // min buffer length by guess 512
)
// valueMapper default as TypeWriter.encode
this.valueMapper = valueMapper ? valueMapper : typeWriter.encode
}
write(writer: BufferWriter) {
const portal = this.portal || '' // an empty string selects the unnamed portal
const statement = this.statement || '' // an empty string selects the unnamed prepared statement
const values = this.values || []
// check Uint8Array exists,
const hasBinaryValues = values.some((value) => value instanceof Uint8Array)
writer.writeString(portal).writeUint8(0x00) // portal name, c string
writer.writeString(statement).writeUint8(0x00) // statement name, c string
// the number of parameter format codes that follow
if (hasBinaryValues) {
writer.writeInt16(values.length)
values.forEach((value) => {
writer.writeInt16(value instanceof Uint8Array
? PARAMETER_FORMAT_CODE.BINARY
: PARAMETER_FORMAT_CODE.TEXT
)
})
} else {
writer.writeInt16(0x00)
}
// this must match the number of parameters needed by the query
writer.writeInt16(values.length)
// the following pair of fields appear for each parameter
values.forEach((value) => {
const mappedValue = this.valueMapper!(value)
// as `null`
if (mappedValue === null || typeof mappedValue === 'undefined') {
writer.writeInt32(-1)
// as `unit8array
} else if (mappedValue instanceof Uint8Array) {
writer.writeInt32(mappedValue.length)
writer.write(mappedValue)
// as `string`
} else {
const byteLength = encode(mappedValue).length
writer.writeInt32(byteLength)
writer.writeString(mappedValue)
}
})
/**
* the number of result-column format codes that follow (denoted R below).
* this can be zero to indicate that there are no result columns
* or that the result columns should all use the default format (text);
* or one, in which case the specified format code is applied to all result columns (if any);
* or it can equal the actual number of result columns of the query.
*/
writer.writeInt16(0x00)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
/**
* cancelRequest without packetCode that is not a standard postgres packet.
* so here, can not extends from PacketWriter, just a single alone class.
*/
export class CancelRequestWriter {
packetName: MESSAGE_NAME = MESSAGE_NAME.CancelRequest
// packetCode: MESSAGE_CODE = '' // cancelRequest without packetCode
packetLength = 0x10 // parseInt(packetLength, 10) = 16
constructor(
public processId: number,
public secretKey: number
) {}
write(writer: BufferWriter): Uint8Array {
writer.writeInt16(this.packetLength)
/**
* the cancel request code. The value is chosen to
* contain 1234 in the most significant 16 bits,
* and 5678 in the least significant 16 bits
*/
writer.writeInt32(80877102)
writer.writeInt32(this.processId)
writer.writeInt32(this.secretKey)
return writer.buffer.slice(0, writer.index)
}
}
export class CloseWriter extends PacketWriter {
constructor(
public type: PORTAL_STATEMENT_TYPE,
public name: string = ''
) {
super(
/* name */MESSAGE_NAME.Close,
/* code */MESSAGE_CODE.Close,
/* length */name.length + 2 // name.length + 0x00 + BYTE1(type)
)
}
write(writer: BufferWriter) {
writer.writeUint8(this.type)
writer.writeString(this.name).writeUint8(0x00)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class CopyDataWriter extends PacketWriter {
constructor(
public data: Uint8Array
) {
super(
/* name */MESSAGE_NAME.CopyData,
/* code */MESSAGE_CODE.CopyData,
/* length */data.length
)
}
write(writer: BufferWriter) {
writer.write(this.data)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class CopyDoneWriter extends PacketWriter {
constructor() {
super(
/* name */MESSAGE_NAME.CopyDone,
/* code */MESSAGE_CODE.CopyDone,
/* length */0// code + length
)
}
write(writer: BufferWriter) {
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class CopyFailWriter extends PacketWriter {
constructor(
public message: string
) {
super(
/* name */MESSAGE_NAME.CopyFail,
/* code */MESSAGE_CODE.CopyFail,
/* length */message.length
)
}
write(writer: BufferWriter) {
writer.writeString(this.message).writeUint8(0x00)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class DescribeWriter extends PacketWriter {
constructor(
public type: PORTAL_STATEMENT_TYPE,
public name: string = ''
) {
super(
/* name */MESSAGE_NAME.Describe,
/* code */MESSAGE_CODE.Describe,
/* length */name.length + 2 // name.length + 0x00 + BYTE1(type)
)
}
write(writer: BufferWriter) {
writer.writeUint8(this.type)
writer.writeString(this.name).writeUint8(0x00)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class ExecuteWriter extends PacketWriter {
constructor(
public portal: string = '',
public rows: number = 0
) {
super(
/* name */MESSAGE_NAME.Execute,
/* code */MESSAGE_CODE.Execute,
/* length */portal.length + 4
)
}
write(writer: BufferWriter) {
writer.writeString(this.portal).writeUint8(0x00)
writer.writeInt32(this.rows)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class FlushWriter extends PacketWriter {
constructor() {
super(
/* name */MESSAGE_NAME.Flush,
/* code */MESSAGE_CODE.Flush,
/* length */0
)
}
write(writer: BufferWriter) {
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
// TODO: waiting to complete the code
export class FunctionCallWriter extends PacketWriter {
constructor() {
super(
/* name */MESSAGE_NAME.FunctionCall,
/* code */MESSAGE_CODE.FunctionCall,
/* length */0
)
}
write(writer: BufferWriter) {
return writer.buffer.slice(0, writer.index)
}
}
export class GSSResponseWriter extends PacketWriter {
constructor(
public data: Uint8Array
) {
super(
/* name */MESSAGE_NAME.GSSResponse,
/* code */MESSAGE_CODE.GSSResponse,
/* length */data.length
)
}
write(writer: BufferWriter) {
writer.write(this.data)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class ParseWriter extends PacketWriter {
constructor(
public statement: string = '', // an empty string selects the unnamed prepared statement
public query: string,
) {
super(
/* name */MESSAGE_NAME.Parse,
/* code */MESSAGE_CODE.Parse,
/* length */statement.length + 1 + query.length + 1 + 2
)
}
write(writer: BufferWriter) {
writer.writeString(this.statement).writeUint8(0x00)
writer.writeString(this.query).writeUint8(0x00)
/**
* the number of parameter data types specified (can be zero).
* note that this is not an indication of the number of parameters
* that might appear in the query string, only the number that
* the frontend wants to prespecify types for.
*/
writer.writeInt16(0x00)
// !!!call this last after all other packet build flows!!!
this.writeHeader(writer)
return writer.buffer.slice(0, writer.index)
}
}
export class PasswordMessageWriter extends PacketWriter {
constructor(
public password: string = ''
) {
super(
/* name */MESSAGE_NAME.PasswordMessage,
/* code */MESSAGE_CODE.PasswordMessage,
/* length */password.length + 1
)
}
write(writer: BufferWriter) {
writer.writeString(this.password).writeUint8(0x00)