@m2k-5f/pgtx 1.4.11 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +225 -128
  3. package/dist/clauses/array.clause.js +1 -1
  4. package/dist/clauses/empty.clause.d.ts +1 -1
  5. package/dist/clauses/empty.clause.d.ts.map +1 -1
  6. package/dist/clauses/empty.clause.js +2 -2
  7. package/dist/clauses/fragment.clause.d.ts +1 -1
  8. package/dist/clauses/fragment.clause.d.ts.map +1 -1
  9. package/dist/clauses/fragment.clause.js +3 -5
  10. package/dist/clauses/iden.caluse.d.ts +1 -1
  11. package/dist/clauses/iden.caluse.d.ts.map +1 -1
  12. package/dist/clauses/index.d.ts +10 -0
  13. package/dist/clauses/index.d.ts.map +1 -0
  14. package/dist/clauses/index.js +25 -0
  15. package/dist/clauses/insert.clause.d.ts +1 -1
  16. package/dist/clauses/insert.clause.d.ts.map +1 -1
  17. package/dist/clauses/insert.clause.js +1 -1
  18. package/dist/clauses/literal.clause.d.ts +1 -1
  19. package/dist/clauses/literal.clause.d.ts.map +1 -1
  20. package/dist/clauses/update.clause.d.ts +1 -1
  21. package/dist/clauses/update.clause.d.ts.map +1 -1
  22. package/dist/clauses/update.clause.js +1 -1
  23. package/dist/clauses/where.clause.d.ts +1 -1
  24. package/dist/clauses/where.clause.d.ts.map +1 -1
  25. package/dist/clauses/where.clause.js +1 -1
  26. package/dist/connection.d.ts +135 -31
  27. package/dist/connection.d.ts.map +1 -1
  28. package/dist/connection.js +290 -59
  29. package/dist/index.d.ts +2 -10
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +10 -18
  32. package/dist/pool.d.ts +126 -32
  33. package/dist/pool.d.ts.map +1 -1
  34. package/dist/pool.js +203 -54
  35. package/dist/protocol/connection-request-writer.d.ts +38 -0
  36. package/dist/protocol/connection-request-writer.d.ts.map +1 -0
  37. package/dist/protocol/connection-request-writer.js +175 -0
  38. package/dist/protocol/connection-response-reader.d.ts +49 -0
  39. package/dist/protocol/connection-response-reader.d.ts.map +1 -0
  40. package/dist/protocol/connection-response-reader.js +185 -0
  41. package/dist/protocol/constants.d.ts +46 -0
  42. package/dist/protocol/constants.d.ts.map +1 -0
  43. package/dist/protocol/constants.js +43 -0
  44. package/dist/protocol/socket-authorization.d.ts +11 -0
  45. package/dist/protocol/socket-authorization.d.ts.map +1 -0
  46. package/dist/protocol/socket-authorization.js +95 -0
  47. package/dist/protocol/socket-connector.d.ts +17 -0
  48. package/dist/protocol/socket-connector.d.ts.map +1 -0
  49. package/dist/protocol/socket-connector.js +48 -0
  50. package/dist/queue.d.ts +10 -0
  51. package/dist/queue.d.ts.map +1 -0
  52. package/dist/queue.js +27 -0
  53. package/dist/security/md5.d.ts +2 -0
  54. package/dist/security/md5.d.ts.map +1 -0
  55. package/dist/security/md5.js +12 -0
  56. package/dist/security/sasl.d.ts +6 -0
  57. package/dist/security/sasl.d.ts.map +1 -0
  58. package/dist/security/sasl.js +29 -0
  59. package/dist/transaction.d.ts +1 -2
  60. package/dist/transaction.d.ts.map +1 -1
  61. package/dist/transaction.js +4 -4
  62. package/dist/types.d.ts +6 -8
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/utils/template-compiler.d.ts +3 -0
  65. package/dist/utils/template-compiler.d.ts.map +1 -0
  66. package/dist/utils/template-compiler.js +64 -0
  67. package/dist/utils/value-parser.d.ts +6 -0
  68. package/dist/utils/value-parser.d.ts.map +1 -0
  69. package/dist/utils/value-parser.js +101 -0
  70. package/package.json +8 -9
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectionRequestWriter = exports.ConnectionRequestBuffer = void 0;
4
+ const constants_1 = require("./constants");
5
+ class ConnectionRequestBuffer {
6
+ constructor(buffer, offset = 0, lastRequestLenByteOffset = 0) {
7
+ this.buffer = buffer;
8
+ this.offset = offset;
9
+ this.lastRequestLenByteOffset = lastRequestLenByteOffset;
10
+ }
11
+ static new(capacity) {
12
+ return new ConnectionRequestBuffer(Buffer.alloc(capacity));
13
+ }
14
+ ensureCapacity(needed) {
15
+ const required = needed + this.offset;
16
+ if (required <= this.buffer.length)
17
+ return;
18
+ let newCapacity = this.buffer.length * 2;
19
+ if (newCapacity < required) {
20
+ newCapacity = required;
21
+ }
22
+ const buffer = Buffer.alloc(newCapacity);
23
+ this.buffer.copy(buffer, 0, 0, this.offset);
24
+ this.buffer = buffer;
25
+ return this;
26
+ }
27
+ writeCString(string) {
28
+ this.ensureCapacity(Buffer.byteLength(string) + 1);
29
+ this.offset += this.buffer.write(string, this.offset, 'utf-8');
30
+ this.buffer[this.offset] = 0;
31
+ this.offset++;
32
+ return this;
33
+ }
34
+ writeString(string) {
35
+ this.ensureCapacity(Buffer.byteLength(string));
36
+ this.offset += this.buffer.write(string, this.offset, 'utf-8');
37
+ return this;
38
+ }
39
+ writeInt32(number) {
40
+ this.ensureCapacity(4);
41
+ this.buffer.writeInt32BE(number, this.offset);
42
+ this.offset += 4;
43
+ return this;
44
+ }
45
+ writeInt16(number) {
46
+ this.ensureCapacity(2);
47
+ this.buffer.writeInt16BE(number, this.offset);
48
+ this.offset += 2;
49
+ return this;
50
+ }
51
+ writeChar(char) {
52
+ this.ensureCapacity(1);
53
+ this.buffer[this.offset] = char.charCodeAt(0);
54
+ this.offset++;
55
+ return this;
56
+ }
57
+ startRequest(requestType) {
58
+ this.writeChar(requestType);
59
+ this.lastRequestLenByteOffset = this.offset;
60
+ return this.writeInt32(0);
61
+ }
62
+ startMessage() {
63
+ this.lastRequestLenByteOffset = this.offset;
64
+ return this.writeInt32(0);
65
+ }
66
+ endRequest() {
67
+ this.buffer.writeInt32BE(this.offset - (this.lastRequestLenByteOffset), this.lastRequestLenByteOffset);
68
+ return this;
69
+ }
70
+ asBuffer() {
71
+ return this.buffer.subarray(0, this.offset);
72
+ }
73
+ clear() {
74
+ this.offset = 0;
75
+ this.lastRequestLenByteOffset = 0;
76
+ }
77
+ }
78
+ exports.ConnectionRequestBuffer = ConnectionRequestBuffer;
79
+ class ConnectionRequestWriter {
80
+ constructor(buffer) {
81
+ this.buffer = buffer;
82
+ }
83
+ static new() {
84
+ return new ConnectionRequestWriter(ConnectionRequestBuffer.new(65536));
85
+ }
86
+ writeQuery(text) {
87
+ this.buffer.startRequest(constants_1.RequestTypes.SimpleQuery)
88
+ .writeCString(text)
89
+ .endRequest();
90
+ return this;
91
+ }
92
+ writeParse(name, text) {
93
+ this.buffer.startRequest(constants_1.RequestTypes.Parse)
94
+ .writeCString(name)
95
+ .writeCString(text)
96
+ .writeInt16(0)
97
+ .endRequest();
98
+ return this;
99
+ }
100
+ writeDescribe(statementName) {
101
+ this.buffer.startRequest(constants_1.RequestTypes.Describe)
102
+ .writeChar('S')
103
+ .writeCString(statementName)
104
+ .endRequest();
105
+ return this;
106
+ }
107
+ writeBind(portName, statementName, params) {
108
+ const request = this.buffer.startRequest(constants_1.RequestTypes.Bind)
109
+ .writeCString(portName)
110
+ .writeCString(statementName)
111
+ .writeInt16(0)
112
+ .writeInt16(params.length);
113
+ params.forEach(param => {
114
+ request.writeInt32(param ? Buffer.byteLength(param) : -1);
115
+ param && request.writeString(param);
116
+ });
117
+ request.writeInt16(0).endRequest();
118
+ return this;
119
+ }
120
+ writeClose(name) {
121
+ this.buffer.startRequest(constants_1.RequestTypes.Close)
122
+ .writeChar("P")
123
+ .writeCString(name)
124
+ .endRequest();
125
+ return this;
126
+ }
127
+ writeStartup(user, database) {
128
+ this.buffer.startMessage()
129
+ .writeInt32(196608)
130
+ .writeCString('user').writeCString(user)
131
+ .writeCString('database').writeCString(database)
132
+ .writeChar('\0')
133
+ .endRequest();
134
+ return this;
135
+ }
136
+ writeExecute(portName) {
137
+ this.buffer.startRequest(constants_1.RequestTypes.Execute)
138
+ .writeCString(portName)
139
+ .writeInt32(0)
140
+ .endRequest();
141
+ return this;
142
+ }
143
+ writeSync() {
144
+ this.buffer.startRequest(constants_1.RequestTypes.Sync).endRequest();
145
+ return this;
146
+ }
147
+ writePassword(password) {
148
+ this.buffer.startRequest(constants_1.RequestTypes.Password)
149
+ .writeCString(password)
150
+ .endRequest();
151
+ return this;
152
+ }
153
+ writeSaslInitial(mechanism, clientFirstMessage) {
154
+ this.buffer.startRequest(constants_1.RequestTypes.Password)
155
+ .writeCString(mechanism)
156
+ .writeInt32(Buffer.byteLength(clientFirstMessage, 'utf-8'))
157
+ .writeString(clientFirstMessage)
158
+ .endRequest();
159
+ return this;
160
+ }
161
+ writeSaslResponse(clientFinalMessage) {
162
+ this.buffer.startRequest(constants_1.RequestTypes.Password)
163
+ .writeString(clientFinalMessage)
164
+ .endRequest();
165
+ return this;
166
+ }
167
+ asBuffer() {
168
+ return this.buffer.asBuffer();
169
+ }
170
+ clear() {
171
+ this.buffer.clear();
172
+ return this;
173
+ }
174
+ }
175
+ exports.ConnectionRequestWriter = ConnectionRequestWriter;
@@ -0,0 +1,49 @@
1
+ import { AuthenticationCode, ResponseType, TransactionStatus } from "./constants";
2
+ import { ColumnDescription } from "../types";
3
+ export declare class ConnectionResponseBuffer {
4
+ private buffer;
5
+ private caret;
6
+ private constructor();
7
+ static from(buffer: Buffer): ConnectionResponseBuffer;
8
+ readChar(): string;
9
+ readInt32(): number;
10
+ readInt16(): number;
11
+ readCString(): string;
12
+ readRawString(length: number): string;
13
+ readBytes(length: number): Buffer;
14
+ hasMore(): boolean;
15
+ hasFullPacket(): boolean;
16
+ getResidualBuffer(): Buffer<ArrayBufferLike>;
17
+ }
18
+ export declare class ConnectionResponseReader {
19
+ private buffer;
20
+ private currentPacketLength;
21
+ private constructor();
22
+ static from(buffer: Buffer): ConnectionResponseReader;
23
+ readType(): ResponseType;
24
+ readAuthentication(): AuthenticationCode;
25
+ readMD5Salt(): Buffer<ArrayBufferLike>;
26
+ readParameterStatus(): {
27
+ name: string;
28
+ value: string;
29
+ };
30
+ readBackendKeyData(): {
31
+ PID: number;
32
+ secret: number;
33
+ };
34
+ readErrorResponse(): string;
35
+ readReadyForQuery(): TransactionStatus;
36
+ readSaslMechanisms(): string[];
37
+ readSaslMessage(): string;
38
+ readRowDescription(): ColumnDescription[];
39
+ readDataRow(): (string | null)[];
40
+ readCommandComplete(): string;
41
+ hasMore(): boolean;
42
+ hasFullPacket(): boolean;
43
+ getResidualBuffer(): Buffer<ArrayBufferLike>;
44
+ readParseComplete(): void;
45
+ readBindComplete(): void;
46
+ readParameterDescription(): void;
47
+ readNoData(): void;
48
+ }
49
+ //# sourceMappingURL=connection-response-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection-response-reader.d.ts","sourceRoot":"","sources":["../../src/protocol/connection-response-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACjF,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAE5C,qBAAa,wBAAwB;IAI7B,OAAO,CAAC,MAAM;IAHlB,OAAO,CAAC,KAAK,CAAI;IAEjB,OAAO;IAKP,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM;IAK1B,QAAQ,IAAI,MAAM;IAOlB,SAAS,IAAI,MAAM;IAOnB,SAAS,IAAI,MAAM;IAOnB,WAAW,IAAI,MAAM;IAQrB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAOrC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAOjC,OAAO,IAAI,OAAO;IAKlB,aAAa,IAAI,OAAO;IAaxB,iBAAiB;CAGpB;AAGD,qBAAa,wBAAwB;IAI7B,OAAO,CAAC,MAAM;IAHlB,OAAO,CAAC,mBAAmB,CAAK;IAEhC,OAAO;IAKP,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM;IAK1B,QAAQ,IAC6B,YAAY;IAIjD,kBAAkB,IAGoB,kBAAkB;IAIxD,WAAW;IAKX,mBAAmB;;;;IAUnB,kBAAkB;;;;IAUlB,iBAAiB;IAgBjB,iBAAiB,IAEoB,iBAAiB;IAItD,kBAAkB,IAAI,MAAM,EAAE;IAa9B,eAAe,IAAI,MAAM;IAOzB,kBAAkB;IAuBlB,WAAW;IAmBX,mBAAmB,IAAI,MAAM;IAM7B,OAAO;IAKP,aAAa;IAKb,iBAAiB;IAKjB,iBAAiB;IAKjB,gBAAgB;IAKhB,wBAAwB;IAQxB,UAAU;CAGb"}
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConnectionResponseReader = exports.ConnectionResponseBuffer = void 0;
4
+ class ConnectionResponseBuffer {
5
+ constructor(buffer) {
6
+ this.buffer = buffer;
7
+ this.caret = 0;
8
+ }
9
+ static from(buffer) {
10
+ return new ConnectionResponseBuffer(buffer);
11
+ }
12
+ readChar() {
13
+ const char = String.fromCharCode(this.buffer[this.caret]);
14
+ this.caret += 1;
15
+ return char;
16
+ }
17
+ readInt32() {
18
+ const value = this.buffer.readInt32BE(this.caret);
19
+ this.caret += 4;
20
+ return value;
21
+ }
22
+ readInt16() {
23
+ const value = this.buffer.readInt16BE(this.caret);
24
+ this.caret += 2;
25
+ return value;
26
+ }
27
+ readCString() {
28
+ const end = this.buffer.indexOf(0, this.caret);
29
+ const text = this.buffer.toString('utf-8', this.caret, end);
30
+ this.caret = end + 1;
31
+ return text;
32
+ }
33
+ readRawString(length) {
34
+ const text = this.buffer.toString('utf-8', this.caret, this.caret + length);
35
+ this.caret += length;
36
+ return text;
37
+ }
38
+ readBytes(length) {
39
+ const slice = this.buffer.subarray(this.caret, this.caret + length);
40
+ this.caret += length;
41
+ return slice;
42
+ }
43
+ hasMore() {
44
+ return this.caret < this.buffer.length;
45
+ }
46
+ hasFullPacket() {
47
+ const availableBytes = this.buffer.length - this.caret;
48
+ if (availableBytes < 5)
49
+ return false;
50
+ const packetLength = this.buffer.readInt32BE(this.caret + 1);
51
+ if (availableBytes < 1 + packetLength)
52
+ return false;
53
+ return true;
54
+ }
55
+ getResidualBuffer() {
56
+ return this.buffer.subarray(this.caret);
57
+ }
58
+ }
59
+ exports.ConnectionResponseBuffer = ConnectionResponseBuffer;
60
+ class ConnectionResponseReader {
61
+ constructor(buffer) {
62
+ this.buffer = buffer;
63
+ this.currentPacketLength = 0;
64
+ }
65
+ static from(buffer) {
66
+ return new ConnectionResponseReader(ConnectionResponseBuffer.from(buffer));
67
+ }
68
+ readType() {
69
+ return this.buffer.readChar();
70
+ }
71
+ readAuthentication() {
72
+ this.currentPacketLength = this.buffer.readInt32();
73
+ return this.buffer.readInt32();
74
+ }
75
+ readMD5Salt() {
76
+ return this.buffer.readBytes(4);
77
+ }
78
+ readParameterStatus() {
79
+ this.buffer.readInt32();
80
+ return {
81
+ name: this.buffer.readCString(),
82
+ value: this.buffer.readCString()
83
+ };
84
+ }
85
+ readBackendKeyData() {
86
+ this.buffer.readInt32();
87
+ return {
88
+ PID: this.buffer.readInt32(),
89
+ secret: this.buffer.readInt32()
90
+ };
91
+ }
92
+ readErrorResponse() {
93
+ this.buffer.readInt32();
94
+ let message = '';
95
+ while (true) {
96
+ const marker = this.buffer.readChar();
97
+ if (marker === '\0')
98
+ break;
99
+ const text = this.buffer.readCString();
100
+ if (marker === 'M')
101
+ message += text;
102
+ }
103
+ return message;
104
+ }
105
+ readReadyForQuery() {
106
+ this.buffer.readInt32();
107
+ return this.buffer.readChar();
108
+ }
109
+ readSaslMechanisms() {
110
+ const mechanisms = [];
111
+ while (true) {
112
+ const mech = this.buffer.readCString();
113
+ if (mech === "")
114
+ break;
115
+ mechanisms.push(mech);
116
+ }
117
+ return mechanisms;
118
+ }
119
+ readSaslMessage() {
120
+ const dataLength = this.currentPacketLength - 4 - 4;
121
+ return this.buffer.readRawString(dataLength);
122
+ }
123
+ readRowDescription() {
124
+ this.buffer.readInt32();
125
+ const columnsCount = this.buffer.readInt16();
126
+ const columns = new Array(columnsCount);
127
+ for (let i = 0; i < columnsCount; i++) {
128
+ const name = this.buffer.readCString();
129
+ this.buffer.readInt32();
130
+ this.buffer.readInt16();
131
+ columns[i] = {
132
+ name: name,
133
+ typeOID: this.buffer.readInt32(),
134
+ };
135
+ this.buffer.readInt32();
136
+ this.buffer.readInt32();
137
+ }
138
+ return columns;
139
+ }
140
+ readDataRow() {
141
+ this.buffer.readInt32();
142
+ const fieldsCount = this.buffer.readInt16();
143
+ const rowValues = [];
144
+ for (let i = 0; i < fieldsCount; i++) {
145
+ const fieldLength = this.buffer.readInt32();
146
+ if (fieldLength === -1) {
147
+ rowValues.push(null);
148
+ }
149
+ else {
150
+ rowValues.push(this.buffer.readRawString(fieldLength));
151
+ }
152
+ }
153
+ return rowValues;
154
+ }
155
+ readCommandComplete() {
156
+ this.buffer.readInt32();
157
+ return this.buffer.readCString();
158
+ }
159
+ hasMore() {
160
+ return this.buffer.hasMore();
161
+ }
162
+ hasFullPacket() {
163
+ return this.buffer.hasFullPacket();
164
+ }
165
+ getResidualBuffer() {
166
+ return this.buffer.getResidualBuffer();
167
+ }
168
+ readParseComplete() {
169
+ this.buffer.readInt32();
170
+ }
171
+ readBindComplete() {
172
+ this.buffer.readInt32();
173
+ }
174
+ readParameterDescription() {
175
+ this.buffer.readInt32();
176
+ const count = this.buffer.readInt16();
177
+ for (let i = 0; i < count; i++) {
178
+ this.buffer.readInt32();
179
+ }
180
+ }
181
+ readNoData() {
182
+ this.buffer.readInt32();
183
+ }
184
+ }
185
+ exports.ConnectionResponseReader = ConnectionResponseReader;
@@ -0,0 +1,46 @@
1
+ import { ValueOF } from "../types";
2
+ export declare const RequestTypes: {
3
+ readonly SimpleQuery: "Q";
4
+ readonly Parse: "P";
5
+ readonly Bind: "B";
6
+ readonly Execute: "E";
7
+ readonly Sync: "S";
8
+ readonly Password: "p";
9
+ readonly Describe: "D";
10
+ readonly Close: "C";
11
+ };
12
+ export type RequestType = ValueOF<typeof RequestTypes>;
13
+ export declare const ResponseTypes: {
14
+ readonly RowDescription: "T";
15
+ readonly DataRow: "D";
16
+ readonly ComandComplete: "C";
17
+ readonly ReadyForQuery: "Z";
18
+ readonly ErrorResponse: "E";
19
+ readonly NoticeResponse: "N";
20
+ readonly Authentication: "R";
21
+ readonly BackendKeyData: "K";
22
+ readonly ParamaterStatus: "S";
23
+ readonly ParseComplete: "1";
24
+ readonly BindComplete: "2";
25
+ readonly CloseComplete: "3";
26
+ readonly ParameterDescription: "t";
27
+ readonly NoData: "n";
28
+ readonly Notice: "N";
29
+ };
30
+ export type ResponseType = ValueOF<typeof ResponseTypes>;
31
+ export declare const AuthenticationCodes: {
32
+ readonly Ok: 0;
33
+ readonly CleartextPassword: 3;
34
+ readonly MD5Password: 5;
35
+ readonly SASL: 10;
36
+ readonly SASLContinue: 11;
37
+ readonly SASLFinal: 12;
38
+ };
39
+ export type AuthenticationCode = ValueOF<typeof AuthenticationCodes>;
40
+ export declare const TransactionStatuses: {
41
+ readonly Idle: "I";
42
+ readonly InTransactionBlock: "T";
43
+ readonly FailedTransactionBlock: "E";
44
+ };
45
+ export type TransactionStatus = ValueOF<typeof TransactionStatuses>;
46
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/protocol/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAElC,eAAO,MAAM,YAAY;;;;;;;;;CASf,CAAA;AAEV,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,YAAY,CAAC,CAAA;AAGtD,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;CAgBhB,CAAA;AAGV,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,aAAa,CAAC,CAAA;AAGxD,eAAO,MAAM,mBAAmB;;;;;;;CAOtB,CAAA;AAEV,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,OAAO,mBAAmB,CAAC,CAAA;AAGpE,eAAO,MAAM,mBAAmB;;;;CAItB,CAAA;AAEV,MAAM,MAAM,iBAAiB,GAAG,OAAO,CAAC,OAAO,mBAAmB,CAAC,CAAA"}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionStatuses = exports.AuthenticationCodes = exports.ResponseTypes = exports.RequestTypes = void 0;
4
+ exports.RequestTypes = {
5
+ SimpleQuery: "Q",
6
+ Parse: "P",
7
+ Bind: "B",
8
+ Execute: "E",
9
+ Sync: "S",
10
+ Password: "p",
11
+ Describe: 'D',
12
+ Close: "C"
13
+ };
14
+ exports.ResponseTypes = {
15
+ RowDescription: "T",
16
+ DataRow: "D",
17
+ ComandComplete: "C",
18
+ ReadyForQuery: "Z",
19
+ ErrorResponse: "E",
20
+ NoticeResponse: "N",
21
+ Authentication: "R",
22
+ BackendKeyData: "K",
23
+ ParamaterStatus: "S",
24
+ ParseComplete: "1",
25
+ BindComplete: "2",
26
+ CloseComplete: "3",
27
+ ParameterDescription: "t",
28
+ NoData: "n",
29
+ Notice: "N"
30
+ };
31
+ exports.AuthenticationCodes = {
32
+ Ok: 0,
33
+ CleartextPassword: 3,
34
+ MD5Password: 5,
35
+ SASL: 10,
36
+ SASLContinue: 11,
37
+ SASLFinal: 12,
38
+ };
39
+ exports.TransactionStatuses = {
40
+ Idle: "I",
41
+ InTransactionBlock: "T",
42
+ FailedTransactionBlock: "E",
43
+ };
@@ -0,0 +1,11 @@
1
+ import { Socket } from "node:net";
2
+ import { ConnectionRequestWriter } from "./connection-request-writer";
3
+ export type AuthorizationParams = {
4
+ host: string;
5
+ port: number;
6
+ user: string;
7
+ database: string;
8
+ password?: string;
9
+ };
10
+ export declare const createAuthorizedSocket: (writer: ConnectionRequestWriter, params: AuthorizationParams) => Promise<Socket>;
11
+ //# sourceMappingURL=socket-authorization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket-authorization.d.ts","sourceRoot":"","sources":["../../src/protocol/socket-authorization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,EAAE,MAAM,UAAU,CAAA;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAA;AAOrE,MAAM,MAAM,mBAAmB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA;AAGD,eAAO,MAAM,sBAAsB,GAAI,QAAQ,uBAAuB,EAAE,QAAQ,mBAAmB,oBAiHlG,CAAA"}
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAuthorizedSocket = void 0;
4
+ const node_net_1 = require("node:net");
5
+ const sasl_1 = require("../security/sasl");
6
+ const socket_connector_1 = require("./socket-connector");
7
+ const constants_1 = require("./constants");
8
+ const md5_1 = require("../security/md5");
9
+ const createAuthorizedSocket = (writer, params) => {
10
+ return new Promise((resolve, reject) => {
11
+ const nonce = (0, sasl_1.generateNonce)();
12
+ let clientMessage = '';
13
+ let serverMessage = '';
14
+ const socket = (0, node_net_1.createConnection)({ host: params.host, port: params.port });
15
+ const connector = new socket_connector_1.SocketConnector(socket, (type, reader) => {
16
+ writer.clear();
17
+ switch (type) {
18
+ case constants_1.ResponseTypes.Authentication: {
19
+ switch (reader.readAuthentication()) {
20
+ case constants_1.AuthenticationCodes.Ok: break;
21
+ case constants_1.AuthenticationCodes.CleartextPassword: {
22
+ if (!params.password)
23
+ throw new Error('The authorization method requires a password.');
24
+ connector.write(writer.writePassword(params.password));
25
+ break;
26
+ }
27
+ case constants_1.AuthenticationCodes.MD5Password: {
28
+ const salt = reader.readMD5Salt();
29
+ if (!params.password)
30
+ throw new Error('The authorization method requires a password.');
31
+ const password = (0, md5_1.encryptMd5)(params.password, params.user, salt);
32
+ connector.write(writer.writePassword(password));
33
+ break;
34
+ }
35
+ case constants_1.AuthenticationCodes.SASL: {
36
+ reader.readSaslMechanisms();
37
+ if (!params.password)
38
+ throw new Error('The authorization method requires a password.');
39
+ clientMessage = `n=${params.user},r=${nonce}`;
40
+ connector.write(writer.writeSaslInitial('SCRAM-SHA-256', `n,,${clientMessage}`));
41
+ break;
42
+ }
43
+ case constants_1.AuthenticationCodes.SASLContinue: {
44
+ serverMessage = reader.readSaslMessage();
45
+ if (!params.password)
46
+ throw new Error('The authorization method requires a password.');
47
+ const parts = Object.fromEntries(serverMessage.split(',').map(x => x.split('=')));
48
+ const serverNonce = parts.r;
49
+ const saltBase64 = parts.s;
50
+ const iterations = parseInt(parts.i, 10);
51
+ if (!serverNonce.startsWith(nonce)) {
52
+ connector.destroy();
53
+ return reject("Protocol violation: server nonce doesn't match client nonce");
54
+ }
55
+ const clientFinalMessageWithoutProof = `c=biws,r=${serverNonce}`;
56
+ const authMessage = `${clientMessage},${serverMessage},${clientFinalMessageWithoutProof}`;
57
+ const { clientProof } = (0, sasl_1.calculateScramAuth)(params.password, saltBase64, iterations, authMessage);
58
+ const clientFinalMessage = `${clientFinalMessageWithoutProof},p=${clientProof}`;
59
+ connector.write(writer.writeSaslResponse(clientFinalMessage));
60
+ break;
61
+ }
62
+ case constants_1.AuthenticationCodes.SASLFinal: {
63
+ reader.readSaslMessage();
64
+ break;
65
+ }
66
+ }
67
+ break;
68
+ }
69
+ case constants_1.ResponseTypes.ParamaterStatus: {
70
+ reader.readParameterStatus();
71
+ break;
72
+ }
73
+ case constants_1.ResponseTypes.ErrorResponse: {
74
+ const message = reader.readErrorResponse();
75
+ connector.destroy();
76
+ reject(message);
77
+ return;
78
+ }
79
+ case constants_1.ResponseTypes.BackendKeyData: {
80
+ reader.readBackendKeyData();
81
+ }
82
+ case constants_1.ResponseTypes.ReadyForQuery: {
83
+ reader.readReadyForQuery();
84
+ resolve(connector.unwrapSocket());
85
+ return;
86
+ }
87
+ }
88
+ }, error => {
89
+ reject(error);
90
+ });
91
+ connector.write(writer.writeStartup(params.user, params.database));
92
+ writer.clear();
93
+ });
94
+ };
95
+ exports.createAuthorizedSocket = createAuthorizedSocket;
@@ -0,0 +1,17 @@
1
+ import { Socket } from 'net';
2
+ import { ResponseType } from './constants';
3
+ import { ConnectionResponseReader } from '../protocol/connection-response-reader';
4
+ import { ConnectionRequestWriter } from '../protocol/connection-request-writer';
5
+ export declare class SocketConnector {
6
+ private _socket;
7
+ private _onData;
8
+ private _onError;
9
+ private residualBuffer;
10
+ private _isDestroyed;
11
+ constructor(_socket: Socket, _onData: (type: ResponseType, reader: ConnectionResponseReader) => void, _onError: (error: Error) => void);
12
+ write(writer: ConnectionRequestWriter): void;
13
+ unwrapSocket(): Socket;
14
+ destroy(): void;
15
+ get isDestroyed(): boolean;
16
+ }
17
+ //# sourceMappingURL=socket-connector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket-connector.d.ts","sourceRoot":"","sources":["../../src/protocol/socket-connector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAA;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,wBAAwB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAA;AAE/E,qBAAa,eAAe;IAKpB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,QAAQ;IANpB,OAAO,CAAC,cAAc,CAAsB;IAC5C,OAAO,CAAC,YAAY,CAAQ;gBAGhB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,wBAAwB,KAAK,IAAI,EACvE,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI;IA4B5C,KAAK,CAAC,MAAM,EAAE,uBAAuB;IAOrC,YAAY;IAQZ,OAAO;IAMP,IAAI,WAAW,YAA6B;CAC/C"}