@m2k-5f/pgtx 2.1.0 → 2.3.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 (45) hide show
  1. package/README.md +66 -13
  2. package/dist/clauses/abstract.clause.js +1 -5
  3. package/dist/clauses/array.clause.js +3 -7
  4. package/dist/clauses/empty.clause.js +3 -7
  5. package/dist/clauses/exclude.clause.js +2 -6
  6. package/dist/clauses/fragment.clause.js +4 -8
  7. package/dist/clauses/iden.caluse.js +2 -6
  8. package/dist/clauses/index.js +9 -25
  9. package/dist/clauses/insert.clause.js +2 -6
  10. package/dist/clauses/literal.clause.js +2 -6
  11. package/dist/clauses/update.clause.js +2 -6
  12. package/dist/clauses/where.clause.js +2 -6
  13. package/dist/connection.d.ts +56 -10
  14. package/dist/connection.d.ts.map +1 -1
  15. package/dist/connection.js +220 -117
  16. package/dist/error.d.ts +1 -1
  17. package/dist/error.d.ts.map +1 -1
  18. package/dist/error.js +2 -6
  19. package/dist/index.js +24 -37
  20. package/dist/pool.d.ts +20 -6
  21. package/dist/pool.d.ts.map +1 -1
  22. package/dist/pool.js +40 -41
  23. package/dist/protocol/connection-request-writer.js +13 -18
  24. package/dist/protocol/connection-response-reader.d.ts +5 -0
  25. package/dist/protocol/connection-response-reader.d.ts.map +1 -1
  26. package/dist/protocol/connection-response-reader.js +11 -9
  27. package/dist/protocol/constants.d.ts +1 -0
  28. package/dist/protocol/constants.d.ts.map +1 -1
  29. package/dist/protocol/constants.js +6 -8
  30. package/dist/protocol/socket-authorization.d.ts +5 -1
  31. package/dist/protocol/socket-authorization.d.ts.map +1 -1
  32. package/dist/protocol/socket-authorization.js +38 -34
  33. package/dist/protocol/socket-connector.d.ts.map +1 -1
  34. package/dist/protocol/socket-connector.js +4 -7
  35. package/dist/query.js +2 -6
  36. package/dist/queue.js +1 -5
  37. package/dist/security/md5.js +4 -8
  38. package/dist/security/sasl.js +7 -11
  39. package/dist/transaction.d.ts +7 -5
  40. package/dist/transaction.d.ts.map +1 -1
  41. package/dist/transaction.js +18 -26
  42. package/dist/types.js +0 -2
  43. package/dist/utils/template-compiler.js +3 -6
  44. package/dist/utils/value-parser.js +2 -7
  45. package/package.json +5 -2
package/dist/index.js CHANGED
@@ -1,34 +1,13 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.setTypeParser = exports.Pool = exports.Transaction = exports.Connection = exports.sql = void 0;
18
- const pool_1 = require("./pool");
19
- Object.defineProperty(exports, "Pool", { enumerable: true, get: function () { return pool_1.Pool; } });
20
- const clauses_1 = require("./clauses");
21
- const connection_1 = require("./connection");
22
- Object.defineProperty(exports, "Connection", { enumerable: true, get: function () { return connection_1.Connection; } });
23
- const transaction_1 = require("./transaction");
24
- Object.defineProperty(exports, "Transaction", { enumerable: true, get: function () { return transaction_1.Transaction; } });
25
- const value_parser_1 = require("./utils/value-parser");
26
- Object.defineProperty(exports, "setTypeParser", { enumerable: true, get: function () { return value_parser_1.setTypeParser; } });
1
+ import { Pool as PgtxPool } from "./pool";
2
+ import { IdentifierClause, InsertClause, ArrayClause, UpdateClause, ExcludeUpdateClause, emptyClause, WhereClause, LiteralClause, FragmentClause, } from './clauses';
3
+ import { Connection } from "./connection";
4
+ import { Transaction } from "./transaction";
5
+ import { setTypeParser } from "./utils/value-parser";
27
6
  /**
28
7
  * Core SQL tagging utility for Pgtx.
29
8
  * Provides type-safe helpers for building dynamic queries with recursive support.
30
9
  */
31
- exports.sql = {
10
+ export const sql = {
32
11
  /**
33
12
  * Creates a VALUES clause for INSERT queries.
34
13
  * Supports single objects and arrays of objects.
@@ -41,7 +20,7 @@ exports.sql = {
41
20
  * sql.insert([{ id: 1 }, { id: 2 }])
42
21
  * // Result: (id) VALUES ($1), ($2)
43
22
  */
44
- insert: clauses_1.InsertClause.create,
23
+ insert: InsertClause.create,
45
24
  /**
46
25
  * Generates a SET clause for UPDATE queries from a JavaScript object.
47
26
  *
@@ -49,7 +28,7 @@ exports.sql = {
49
28
  * sql.update({ status: 'active', updated_at: new Date() })
50
29
  * // Result: status = $1, updated_at = $2
51
30
  */
52
- update: clauses_1.UpdateClause.create,
31
+ update: UpdateClause.create,
53
32
  /**
54
33
  * Generates an assignment list for ON CONFLICT DO UPDATE using the EXCLUDED table.
55
34
  *
@@ -57,7 +36,7 @@ exports.sql = {
57
36
  * sql`INSERT INTO users ${sql.insert(data)} ON CONFLICT (id) DO UPDATE SET ${sql.excluded(['name', 'email'])}`
58
37
  * // Result: name = EXCLUDED.name, email = EXCLUDED.email
59
38
  */
60
- excluded: clauses_1.ExcludeUpdateClause.create,
39
+ excluded: ExcludeUpdateClause.create,
61
40
  /**
62
41
  * Represents a safe empty SQL fragment.
63
42
  * Useful for dynamic query building when a condition or list might be optional.
@@ -67,7 +46,7 @@ exports.sql = {
67
46
  * sql`SELECT * FROM users ${filters.length ? sql.fragment`WHERE ...` : sql.empty}`
68
47
  * // Result: SELECT * FROM users
69
48
  */
70
- empty: clauses_1.emptyClause,
49
+ empty: emptyClause,
71
50
  /**
72
51
  * Generates a list of conditions for a WHERE clause from a JavaScript object.
73
52
  * Works similarly to sql.update, but uses ' AND ' as a separator instead of a comma.
@@ -80,7 +59,7 @@ exports.sql = {
80
59
  * sql`DELETE FROM tasks WHERE ${sql.where({ id: 10, user_id: 5 })}`
81
60
  * // Result: id = $1 AND user_id = $2
82
61
  */
83
- where: clauses_1.WhereClause.create,
62
+ where: WhereClause.create,
84
63
  /**
85
64
  * Safely escapes SQL identifiers (table or column names) using double quotes.
86
65
  *
@@ -92,7 +71,7 @@ exports.sql = {
92
71
  * sql.ident('table.column')
93
72
  * // Result: "table.column"
94
73
  */
95
- ident: clauses_1.IdentifierClause.create,
74
+ ident: IdentifierClause.create,
96
75
  /**
97
76
  * Injects raw, unescaped SQL strings.
98
77
  * ⚠️ Use with caution to prevent SQL injection!
@@ -101,7 +80,7 @@ exports.sql = {
101
80
  * sql.literal('DESC')
102
81
  * // Result: DESC
103
82
  */
104
- literal: clauses_1.LiteralClause.create,
83
+ literal: LiteralClause.create,
105
84
  /**
106
85
  * Creates a reusable, recursive SQL fragment.
107
86
  * Fragments can be nested within each other; argument numbering is handled automatically.
@@ -111,7 +90,7 @@ exports.sql = {
111
90
  * sql`SELECT * FROM users WHERE ${filter} AND status = ${'active'}`
112
91
  * // Result: SELECT * FROM users WHERE age > $1 AND status = $2
113
92
  */
114
- fragment: clauses_1.FragmentClause.create,
93
+ fragment: FragmentClause.create,
115
94
  /**
116
95
  * Formats an array for dynamic lists (IN clauses, column lists, or joined conditions).
117
96
  * Supports recursive Clauses (fragments, idents) within the array.
@@ -132,6 +111,14 @@ exports.sql = {
132
111
  * sql`SELECT ${sql.array([sql.ident('id'), sql.ident('name')])}`
133
112
  * // Result: SELECT "id", "name"
134
113
  */
135
- array: clauses_1.ArrayClause.create,
114
+ array: ArrayClause.create,
136
115
  };
137
- __exportStar(require("./clauses"), exports);
116
+ /**
117
+ * Main Pgtx Connection Pool.
118
+ * Manages connections, transactions (including SAVEPOINTs), and prepared statements.
119
+ */
120
+ export { Connection as Connection };
121
+ export { Transaction as Transaction };
122
+ export { PgtxPool as Pool };
123
+ export { setTypeParser as setTypeParser };
124
+ export * from "./clauses";
package/dist/pool.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { Connection, ConnectionParams } from "./connection";
2
2
  import { Transaction } from "./transaction";
3
+ import { Future } from "fluent-future";
4
+ import { PostgresError } from "./error";
3
5
  type PoolParams = ConnectionParams & {
4
6
  max?: number;
5
7
  };
@@ -35,7 +37,7 @@ type PoolParams = ConnectionParams & {
35
37
  * }
36
38
  *
37
39
  * // Clean up
38
- * await pool.close()
40
+ * pool.close()
39
41
  * ```
40
42
  */
41
43
  export declare class Pool {
@@ -65,7 +67,7 @@ export declare class Pool {
65
67
  * }
66
68
  * ```
67
69
  */
68
- acquire(): Promise<Connection>;
70
+ acquire(): Future<Connection, Error>;
69
71
  /**
70
72
  * Releases the connection back to the pool.
71
73
  *
@@ -103,7 +105,7 @@ export declare class Pool {
103
105
  * })
104
106
  * ```
105
107
  */
106
- begin<T>(txCallback: (transaction: Transaction) => Promise<T>): Promise<T>;
108
+ begin<T>(txCallback: (transaction: Transaction) => Promise<T>): Future<T, Error>;
107
109
  /**
108
110
  * Executes a one-off query using pipeline.
109
111
  *
@@ -127,7 +129,19 @@ export declare class Pool {
127
129
  * const users = await pool.query<User>`SELECT * FROM users`
128
130
  * ```
129
131
  */
130
- query<T extends Record<string, any>>(templates: TemplateStringsArray, ...args: any[]): Promise<T[]>;
132
+ query<T extends Record<string, any>>(templates: TemplateStringsArray, ...args: any[]): Future<T[], Error>;
133
+ /**
134
+ * Sends an asynchronous notification to a channel via `pg_notify`.
135
+ *
136
+ * @param channelName - The channel identifier
137
+ * @param payload - Optional string data (max 8000 bytes)
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * await pool.notify('events', 'hello')
142
+ * ```
143
+ */
144
+ notify(channelName: string, payload?: string): Future<[], PostgresError>;
131
145
  /**
132
146
  * Number of available (idle) connections in the pool.
133
147
  */
@@ -145,10 +159,10 @@ export declare class Pool {
145
159
  *
146
160
  * @example
147
161
  * ```ts
148
- * await pool.close()
162
+ * pool.close()
149
163
  * ```
150
164
  */
151
- close(): Promise<void>;
165
+ close(): void;
152
166
  }
153
167
  export {};
154
168
  //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IAAI,IAAI,WAEP;IAGD;;;OAGG;IACH,IAAI,KAAK,WAER;IAGD;;;;;;;;;;OAUG;IACH,KAAK;CAeR"}
package/dist/pool.js CHANGED
@@ -1,8 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Pool = void 0;
4
- const connection_1 = require("./connection");
5
- const queue_1 = require("./queue");
1
+ import { Connection } from "./connection";
2
+ import { Queue } from "./queue";
3
+ import { Future, Resolve } from "fluent-future";
6
4
  /**
7
5
  * The main entry point for Pgtx.
8
6
  * Manages a connection pool and provides high-level API for queries and transactions.
@@ -35,18 +33,18 @@ const queue_1 = require("./queue");
35
33
  * }
36
34
  *
37
35
  * // Clean up
38
- * await pool.close()
36
+ * pool.close()
39
37
  * ```
40
38
  */
41
- class Pool {
39
+ export class Pool {
42
40
  _checkClosed() {
43
41
  if (this._isClosed)
44
42
  throw new Error('Pool closed');
45
43
  }
46
44
  constructor(params) {
47
- this._available = new queue_1.Queue();
45
+ this._available = new Queue();
48
46
  this._total = 0;
49
- this._waiting = new queue_1.Queue();
47
+ this._waiting = new Queue();
50
48
  this._isClosed = false;
51
49
  this._config = params;
52
50
  this._max = params.max || 20;
@@ -69,29 +67,24 @@ class Pool {
69
67
  * }
70
68
  * ```
71
69
  */
72
- async acquire() {
70
+ acquire() {
73
71
  this._checkClosed();
74
72
  while (this._available.hasMore) {
75
73
  const conn = this._available.get();
76
74
  this._available.next();
77
- if (conn.isAlive) {
78
- return conn;
75
+ if (conn.isOpened) {
76
+ return Resolve(conn);
79
77
  }
80
78
  this._total--;
81
79
  }
82
80
  if (this._total < this._max) {
83
81
  this._total++;
84
- try {
85
- return await connection_1.Connection.new(this._config);
86
- }
87
- catch (err) {
88
- this._total--;
89
- throw err;
90
- }
82
+ return Future.of(Connection.new(this._config))
83
+ .tapErr(() => this._total--);
91
84
  }
92
- return new Promise((resolve, reject) => {
85
+ return Future.of(new Promise((resolve, reject) => {
93
86
  this._waiting.push({ resolve, reject });
94
- });
87
+ }));
95
88
  }
96
89
  /**
97
90
  * Releases the connection back to the pool.
@@ -113,7 +106,7 @@ class Pool {
113
106
  */
114
107
  release(conn) {
115
108
  this._checkClosed();
116
- if (!conn.isAlive) {
109
+ if (!conn.isOpened) {
117
110
  this._total--;
118
111
  return;
119
112
  }
@@ -143,15 +136,11 @@ class Pool {
143
136
  * })
144
137
  * ```
145
138
  */
146
- async begin(txCallback) {
139
+ begin(txCallback) {
147
140
  this._checkClosed();
148
- const conn = await this.acquire();
149
- try {
150
- return conn.begin(txCallback);
151
- }
152
- finally {
153
- this.release(conn);
154
- }
141
+ return this.acquire()
142
+ .andThen(conn => conn.begin(txCallback)
143
+ .finally(() => this.release(conn)));
155
144
  }
156
145
  /**
157
146
  * Executes a one-off query using pipeline.
@@ -180,7 +169,7 @@ class Pool {
180
169
  this._checkClosed();
181
170
  while (this._available.hasMore) {
182
171
  const conn = this._available.get();
183
- if (conn.isAlive) {
172
+ if (conn.isOpened) {
184
173
  return conn.query(templates, ...args);
185
174
  }
186
175
  this._available.next();
@@ -188,22 +177,33 @@ class Pool {
188
177
  }
189
178
  if (this._total < this._max) {
190
179
  this._total++;
191
- return connection_1.Connection.new(this._config)
192
- .catch(err => {
193
- this._total--;
194
- throw err;
195
- })
196
- .then(conn => {
180
+ return Future.of(Connection.new(this._config))
181
+ .tapErr(() => this._total--)
182
+ .andThen(conn => {
197
183
  this.release(conn);
198
184
  return conn.query(templates, ...args);
199
185
  });
200
186
  }
201
187
  return this.acquire()
202
- .then(conn => {
188
+ .andThen(conn => {
203
189
  this.release(conn);
204
190
  return conn.query(templates, ...args);
205
191
  });
206
192
  }
193
+ /**
194
+ * Sends an asynchronous notification to a channel via `pg_notify`.
195
+ *
196
+ * @param channelName - The channel identifier
197
+ * @param payload - Optional string data (max 8000 bytes)
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * await pool.notify('events', 'hello')
202
+ * ```
203
+ */
204
+ notify(channelName, payload = "") {
205
+ return this.query `select pg_notify(${channelName}, ${payload})`;
206
+ }
207
207
  /**
208
208
  * Number of available (idle) connections in the pool.
209
209
  */
@@ -225,10 +225,10 @@ class Pool {
225
225
  *
226
226
  * @example
227
227
  * ```ts
228
- * await pool.close()
228
+ * pool.close()
229
229
  * ```
230
230
  */
231
- async close() {
231
+ close() {
232
232
  while (this._available.hasMore) {
233
233
  const conn = this._available.get();
234
234
  this._available.next();
@@ -242,4 +242,3 @@ class Pool {
242
242
  this._total = 0;
243
243
  }
244
244
  }
245
- exports.Pool = Pool;
@@ -1,8 +1,5 @@
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 {
1
+ import { RequestTypes } from "./constants";
2
+ export class ConnectionRequestBuffer {
6
3
  constructor(buffer, offset = 0, lastRequestLenByteOffset = 0) {
7
4
  this.buffer = buffer;
8
5
  this.offset = offset;
@@ -75,8 +72,7 @@ class ConnectionRequestBuffer {
75
72
  this.lastRequestLenByteOffset = 0;
76
73
  }
77
74
  }
78
- exports.ConnectionRequestBuffer = ConnectionRequestBuffer;
79
- class ConnectionRequestWriter {
75
+ export class ConnectionRequestWriter {
80
76
  constructor(buffer) {
81
77
  this.buffer = buffer;
82
78
  }
@@ -84,13 +80,13 @@ class ConnectionRequestWriter {
84
80
  return new ConnectionRequestWriter(ConnectionRequestBuffer.new(65536));
85
81
  }
86
82
  writeQuery(text) {
87
- this.buffer.startRequest(constants_1.RequestTypes.SimpleQuery)
83
+ this.buffer.startRequest(RequestTypes.SimpleQuery)
88
84
  .writeCString(text)
89
85
  .endRequest();
90
86
  return this;
91
87
  }
92
88
  writeParse(name, text) {
93
- this.buffer.startRequest(constants_1.RequestTypes.Parse)
89
+ this.buffer.startRequest(RequestTypes.Parse)
94
90
  .writeCString(name)
95
91
  .writeCString(text)
96
92
  .writeInt16(0)
@@ -98,14 +94,14 @@ class ConnectionRequestWriter {
98
94
  return this;
99
95
  }
100
96
  writeDescribe(statementName) {
101
- this.buffer.startRequest(constants_1.RequestTypes.Describe)
97
+ this.buffer.startRequest(RequestTypes.Describe)
102
98
  .writeChar('S')
103
99
  .writeCString(statementName)
104
100
  .endRequest();
105
101
  return this;
106
102
  }
107
103
  writeBind(portName, statementName, params) {
108
- const request = this.buffer.startRequest(constants_1.RequestTypes.Bind)
104
+ const request = this.buffer.startRequest(RequestTypes.Bind)
109
105
  .writeCString(portName)
110
106
  .writeCString(statementName)
111
107
  .writeInt16(0)
@@ -118,7 +114,7 @@ class ConnectionRequestWriter {
118
114
  return this;
119
115
  }
120
116
  writeClose(name) {
121
- this.buffer.startRequest(constants_1.RequestTypes.Close)
117
+ this.buffer.startRequest(RequestTypes.Close)
122
118
  .writeChar("P")
123
119
  .writeCString(name)
124
120
  .endRequest();
@@ -134,24 +130,24 @@ class ConnectionRequestWriter {
134
130
  return this;
135
131
  }
136
132
  writeExecute(portName) {
137
- this.buffer.startRequest(constants_1.RequestTypes.Execute)
133
+ this.buffer.startRequest(RequestTypes.Execute)
138
134
  .writeCString(portName)
139
135
  .writeInt32(0)
140
136
  .endRequest();
141
137
  return this;
142
138
  }
143
139
  writeSync() {
144
- this.buffer.startRequest(constants_1.RequestTypes.Sync).endRequest();
140
+ this.buffer.startRequest(RequestTypes.Sync).endRequest();
145
141
  return this;
146
142
  }
147
143
  writePassword(password) {
148
- this.buffer.startRequest(constants_1.RequestTypes.Password)
144
+ this.buffer.startRequest(RequestTypes.Password)
149
145
  .writeCString(password)
150
146
  .endRequest();
151
147
  return this;
152
148
  }
153
149
  writeSaslInitial(mechanism, clientFirstMessage) {
154
- this.buffer.startRequest(constants_1.RequestTypes.Password)
150
+ this.buffer.startRequest(RequestTypes.Password)
155
151
  .writeCString(mechanism)
156
152
  .writeInt32(Buffer.byteLength(clientFirstMessage, 'utf-8'))
157
153
  .writeString(clientFirstMessage)
@@ -159,7 +155,7 @@ class ConnectionRequestWriter {
159
155
  return this;
160
156
  }
161
157
  writeSaslResponse(clientFinalMessage) {
162
- this.buffer.startRequest(constants_1.RequestTypes.Password)
158
+ this.buffer.startRequest(RequestTypes.Password)
163
159
  .writeString(clientFinalMessage)
164
160
  .endRequest();
165
161
  return this;
@@ -172,4 +168,3 @@ class ConnectionRequestWriter {
172
168
  return this;
173
169
  }
174
170
  }
175
- exports.ConnectionRequestWriter = ConnectionRequestWriter;
@@ -1,6 +1,7 @@
1
1
  import { AuthenticationCode, ResponseType, TransactionStatus } from "./constants";
2
2
  import { ColumnDescription } from "../types";
3
3
  import { PostgresError } from "../error";
4
+ import { ChannelName } from "../connection";
4
5
  export declare class ConnectionResponseBuffer {
5
6
  private buffer;
6
7
  private caret;
@@ -38,6 +39,10 @@ export declare class ConnectionResponseReader {
38
39
  readSaslMessage(): string;
39
40
  readRowDescription(): ColumnDescription[];
40
41
  readDataRow(): (string | null)[];
42
+ readNotificationResponse(): {
43
+ name: ChannelName;
44
+ payload: string;
45
+ };
41
46
  readCommandComplete(): string;
42
47
  hasMore(): boolean;
43
48
  hasFullPacket(): boolean;
@@ -1 +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;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAGxC,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,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AAa,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,IAAI,aAAa;IA8ClC,iBAAiB,IAEoB,iBAAiB;IAItD,kBAAkB,IAAI,MAAM,EAAE;IAa9B,eAAe,IAAI,MAAM;IAOzB,kBAAkB;IAuBlB,WAAW;IAmBX,wBAAwB;;;;IAUxB,mBAAmB,IAAI,MAAM;IAM7B,OAAO;IAKP,aAAa;IAKb,iBAAiB;IAKjB,iBAAiB;IAKjB,gBAAgB;IAKhB,wBAAwB;IAQxB,UAAU;CAGb"}
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectionResponseReader = exports.ConnectionResponseBuffer = void 0;
4
- const error_1 = require("../error");
5
- class ConnectionResponseBuffer {
1
+ import { PostgresError } from "../error";
2
+ export class ConnectionResponseBuffer {
6
3
  constructor(buffer) {
7
4
  this.buffer = buffer;
8
5
  this.caret = 0;
@@ -57,8 +54,7 @@ class ConnectionResponseBuffer {
57
54
  return this.buffer.subarray(this.caret);
58
55
  }
59
56
  }
60
- exports.ConnectionResponseBuffer = ConnectionResponseBuffer;
61
- class ConnectionResponseReader {
57
+ export class ConnectionResponseReader {
62
58
  constructor(buffer) {
63
59
  this.buffer = buffer;
64
60
  this.currentPacketLength = 0;
@@ -136,7 +132,7 @@ class ConnectionResponseReader {
136
132
  break;
137
133
  }
138
134
  }
139
- return new error_1.PostgresError(message, code, detail, severity, where, hint, position, dataType, constraint);
135
+ return new PostgresError(message, code, detail, severity, where, hint, position, dataType, constraint);
140
136
  }
141
137
  readReadyForQuery() {
142
138
  this.buffer.readInt32();
@@ -188,6 +184,13 @@ class ConnectionResponseReader {
188
184
  }
189
185
  return rowValues;
190
186
  }
187
+ readNotificationResponse() {
188
+ this.buffer.readInt32();
189
+ this.buffer.readInt32();
190
+ const name = this.buffer.readCString();
191
+ const payload = this.buffer.readCString();
192
+ return { name, payload };
193
+ }
191
194
  readCommandComplete() {
192
195
  this.buffer.readInt32();
193
196
  return this.buffer.readCString();
@@ -218,4 +221,3 @@ class ConnectionResponseReader {
218
221
  this.buffer.readInt32();
219
222
  }
220
223
  }
221
- exports.ConnectionResponseReader = ConnectionResponseReader;
@@ -26,6 +26,7 @@ export declare const ResponseTypes: {
26
26
  readonly ParameterDescription: "t";
27
27
  readonly NoData: "n";
28
28
  readonly Notice: "N";
29
+ readonly NotificationResponse: "A";
29
30
  };
30
31
  export type ResponseType = ValueOF<typeof ResponseTypes>;
31
32
  export declare const AuthenticationCodes: {
@@ -1 +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"}
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;;;;;;;;;;;;;;;;;CAiBhB,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"}
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionStatuses = exports.AuthenticationCodes = exports.ResponseTypes = exports.RequestTypes = void 0;
4
- exports.RequestTypes = {
1
+ export const RequestTypes = {
5
2
  SimpleQuery: "Q",
6
3
  Parse: "P",
7
4
  Bind: "B",
@@ -11,7 +8,7 @@ exports.RequestTypes = {
11
8
  Describe: 'D',
12
9
  Close: "C"
13
10
  };
14
- exports.ResponseTypes = {
11
+ export const ResponseTypes = {
15
12
  RowDescription: "T",
16
13
  DataRow: "D",
17
14
  ComandComplete: "C",
@@ -26,9 +23,10 @@ exports.ResponseTypes = {
26
23
  CloseComplete: "3",
27
24
  ParameterDescription: "t",
28
25
  NoData: "n",
29
- Notice: "N"
26
+ Notice: "N",
27
+ NotificationResponse: "A"
30
28
  };
31
- exports.AuthenticationCodes = {
29
+ export const AuthenticationCodes = {
32
30
  Ok: 0,
33
31
  CleartextPassword: 3,
34
32
  MD5Password: 5,
@@ -36,7 +34,7 @@ exports.AuthenticationCodes = {
36
34
  SASLContinue: 11,
37
35
  SASLFinal: 12,
38
36
  };
39
- exports.TransactionStatuses = {
37
+ export const TransactionStatuses = {
40
38
  Idle: "I",
41
39
  InTransactionBlock: "T",
42
40
  FailedTransactionBlock: "E",
@@ -1,5 +1,7 @@
1
1
  import { Socket } from "node:net";
2
2
  import { ConnectionRequestWriter } from "./connection-request-writer";
3
+ import { PostgresError } from "../error";
4
+ import { Future } from "fluent-future";
3
5
  export type AuthorizationParams = {
4
6
  host: string;
5
7
  port: number;
@@ -7,5 +9,7 @@ export type AuthorizationParams = {
7
9
  database: string;
8
10
  password?: string;
9
11
  };
10
- export declare const createAuthorizedSocket: (writer: ConnectionRequestWriter, params: AuthorizationParams) => Promise<Socket>;
12
+ export declare const ErrNonceMismatch: PostgresError;
13
+ export declare const ErrPasswordRequired: PostgresError;
14
+ export declare const createAuthorizedSocket: (writer: ConnectionRequestWriter, params: AuthorizationParams) => Future<Socket, PostgresError>;
11
15
  //# sourceMappingURL=socket-authorization.d.ts.map
@@ -1 +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"}
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;AAKrE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAGtC,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,gBAAgB,eAAmF,CAAA;AAChH,eAAO,MAAM,mBAAmB,eAAqE,CAAA;AAIrG,eAAO,MAAM,sBAAsB,GAAI,QAAQ,uBAAuB,EAAE,QAAQ,mBAAmB,kCAwHlG,CAAA"}