postgrejs 2.22.9 → 2.23.1

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 (216) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/{esm/connection → connection}/bind-param.js +2 -0
  3. package/{esm/connection → connection}/connection.js +4 -2
  4. package/{esm/connection → connection}/cursor.js +8 -3
  5. package/{esm/connection → connection}/intl-connection.js +6 -3
  6. package/{esm/connection → connection}/pool.js +4 -1
  7. package/{esm/connection → connection}/portal.js +3 -1
  8. package/{esm/connection → connection}/prepared-statement.js +6 -3
  9. package/{esm/constants.js → constants.js} +1 -1
  10. package/{types/data-type-map.d.ts → data-type-map.d.ts} +2 -2
  11. package/{esm/data-type-map.js → data-type-map.js} +2 -2
  12. package/{esm/data-types → data-types}/int2-vector-type.js +1 -0
  13. package/{types/data-types → data-types}/timestamptz-type.d.ts +1 -1
  14. package/{esm/data-types → data-types}/timestamptz-type.js +1 -0
  15. package/package.json +12 -31
  16. package/{esm/protocol → protocol}/backend.js +1 -0
  17. package/{esm/protocol → protocol}/buffer-reader.js +2 -1
  18. package/{esm/protocol → protocol}/database-error.js +17 -0
  19. package/{esm/protocol → protocol}/frontend.js +2 -0
  20. package/{types/protocol → protocol}/pg-socket.d.ts +1 -1
  21. package/{esm/protocol → protocol}/pg-socket.js +9 -3
  22. package/{esm/protocol → protocol}/smart-buffer.js +9 -5
  23. package/{esm/util → util}/bigint-methods.js +1 -0
  24. package/cjs/connection/bind-param.js +0 -10
  25. package/cjs/connection/connection.js +0 -295
  26. package/cjs/connection/cursor.js +0 -91
  27. package/cjs/connection/intl-connection.js +0 -258
  28. package/cjs/connection/pool.js +0 -236
  29. package/cjs/connection/portal.js +0 -157
  30. package/cjs/connection/prepared-statement.js +0 -226
  31. package/cjs/constants.js +0 -249
  32. package/cjs/data-type-map.js +0 -131
  33. package/cjs/data-types/bool-type.js +0 -33
  34. package/cjs/data-types/box-type.js +0 -52
  35. package/cjs/data-types/bytea-type.js +0 -28
  36. package/cjs/data-types/char-type.js +0 -27
  37. package/cjs/data-types/circle-type.js +0 -51
  38. package/cjs/data-types/date-type.js +0 -75
  39. package/cjs/data-types/float4-type.js +0 -25
  40. package/cjs/data-types/float8-type.js +0 -25
  41. package/cjs/data-types/int2-type.js +0 -26
  42. package/cjs/data-types/int2-vector-type.js +0 -42
  43. package/cjs/data-types/int4-type.js +0 -28
  44. package/cjs/data-types/int8-type.js +0 -36
  45. package/cjs/data-types/json-type.js +0 -40
  46. package/cjs/data-types/jsonb-type.js +0 -44
  47. package/cjs/data-types/lseg-type.js +0 -55
  48. package/cjs/data-types/numeric-type.js +0 -125
  49. package/cjs/data-types/oid-type.js +0 -32
  50. package/cjs/data-types/point-type.js +0 -42
  51. package/cjs/data-types/time-type.js +0 -66
  52. package/cjs/data-types/timestamp-type.js +0 -89
  53. package/cjs/data-types/timestamptz-type.js +0 -76
  54. package/cjs/data-types/uuid-type.js +0 -39
  55. package/cjs/data-types/varchar-type.js +0 -27
  56. package/cjs/index.js +0 -28
  57. package/cjs/interfaces/command-result.js +0 -2
  58. package/cjs/interfaces/data-mapping-options.js +0 -2
  59. package/cjs/interfaces/data-type.js +0 -2
  60. package/cjs/interfaces/database-connection-params.js +0 -2
  61. package/cjs/interfaces/field-info.js +0 -2
  62. package/cjs/interfaces/query-options.js +0 -2
  63. package/cjs/interfaces/query-result.js +0 -2
  64. package/cjs/interfaces/script-execute-options.js +0 -2
  65. package/cjs/interfaces/script-result.js +0 -2
  66. package/cjs/interfaces/statement-prepare-options.js +0 -2
  67. package/cjs/package.json +0 -3
  68. package/cjs/protocol/backend.js +0 -262
  69. package/cjs/protocol/buffer-reader.js +0 -78
  70. package/cjs/protocol/database-error.js +0 -17
  71. package/cjs/protocol/frontend.js +0 -255
  72. package/cjs/protocol/pg-socket.js +0 -367
  73. package/cjs/protocol/protocol.js +0 -71
  74. package/cjs/protocol/sasl.js +0 -108
  75. package/cjs/protocol/smart-buffer.js +0 -178
  76. package/cjs/safe-event-emitter.js +0 -17
  77. package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
  78. package/cjs/types.js +0 -2
  79. package/cjs/util/array-calculatedim.js +0 -18
  80. package/cjs/util/bigint-methods.js +0 -44
  81. package/cjs/util/config-from-env.js +0 -29
  82. package/cjs/util/connection-config.js +0 -61
  83. package/cjs/util/convert-row-to-object.js +0 -12
  84. package/cjs/util/decode-binaryarray.js +0 -38
  85. package/cjs/util/encode-binaryarray.js +0 -44
  86. package/cjs/util/escape-literal.js +0 -26
  87. package/cjs/util/fast-parseint.js +0 -31
  88. package/cjs/util/get-parsers.js +0 -41
  89. package/cjs/util/parse-array.js +0 -60
  90. package/cjs/util/parse-datetime.js +0 -37
  91. package/cjs/util/parse-row.js +0 -11
  92. package/cjs/util/parse-time.js +0 -30
  93. package/cjs/util/stringify-arrayliteral.js +0 -37
  94. package/cjs/util/stringify-for-sql.js +0 -29
  95. package/cjs/util/wrap-row-description.js +0 -34
  96. package/esm/package.json +0 -3
  97. package/types/index.d.cts +0 -25
  98. /package/{types/connection → connection}/bind-param.d.ts +0 -0
  99. /package/{types/connection → connection}/connection.d.ts +0 -0
  100. /package/{types/connection → connection}/cursor.d.ts +0 -0
  101. /package/{types/connection → connection}/intl-connection.d.ts +0 -0
  102. /package/{types/connection → connection}/pool.d.ts +0 -0
  103. /package/{types/connection → connection}/portal.d.ts +0 -0
  104. /package/{types/connection → connection}/prepared-statement.d.ts +0 -0
  105. /package/{types/constants.d.ts → constants.d.ts} +0 -0
  106. /package/{types/data-types → data-types}/bool-type.d.ts +0 -0
  107. /package/{esm/data-types → data-types}/bool-type.js +0 -0
  108. /package/{types/data-types → data-types}/box-type.d.ts +0 -0
  109. /package/{esm/data-types → data-types}/box-type.js +0 -0
  110. /package/{types/data-types → data-types}/bytea-type.d.ts +0 -0
  111. /package/{esm/data-types → data-types}/bytea-type.js +0 -0
  112. /package/{types/data-types → data-types}/char-type.d.ts +0 -0
  113. /package/{esm/data-types → data-types}/char-type.js +0 -0
  114. /package/{types/data-types → data-types}/circle-type.d.ts +0 -0
  115. /package/{esm/data-types → data-types}/circle-type.js +0 -0
  116. /package/{types/data-types → data-types}/date-type.d.ts +0 -0
  117. /package/{esm/data-types → data-types}/date-type.js +0 -0
  118. /package/{types/data-types → data-types}/float4-type.d.ts +0 -0
  119. /package/{esm/data-types → data-types}/float4-type.js +0 -0
  120. /package/{types/data-types → data-types}/float8-type.d.ts +0 -0
  121. /package/{esm/data-types → data-types}/float8-type.js +0 -0
  122. /package/{types/data-types → data-types}/int2-type.d.ts +0 -0
  123. /package/{esm/data-types → data-types}/int2-type.js +0 -0
  124. /package/{types/data-types → data-types}/int2-vector-type.d.ts +0 -0
  125. /package/{types/data-types → data-types}/int4-type.d.ts +0 -0
  126. /package/{esm/data-types → data-types}/int4-type.js +0 -0
  127. /package/{types/data-types → data-types}/int8-type.d.ts +0 -0
  128. /package/{esm/data-types → data-types}/int8-type.js +0 -0
  129. /package/{types/data-types → data-types}/json-type.d.ts +0 -0
  130. /package/{esm/data-types → data-types}/json-type.js +0 -0
  131. /package/{types/data-types → data-types}/jsonb-type.d.ts +0 -0
  132. /package/{esm/data-types → data-types}/jsonb-type.js +0 -0
  133. /package/{types/data-types → data-types}/lseg-type.d.ts +0 -0
  134. /package/{esm/data-types → data-types}/lseg-type.js +0 -0
  135. /package/{types/data-types → data-types}/numeric-type.d.ts +0 -0
  136. /package/{esm/data-types → data-types}/numeric-type.js +0 -0
  137. /package/{types/data-types → data-types}/oid-type.d.ts +0 -0
  138. /package/{esm/data-types → data-types}/oid-type.js +0 -0
  139. /package/{types/data-types → data-types}/point-type.d.ts +0 -0
  140. /package/{esm/data-types → data-types}/point-type.js +0 -0
  141. /package/{types/data-types → data-types}/time-type.d.ts +0 -0
  142. /package/{esm/data-types → data-types}/time-type.js +0 -0
  143. /package/{types/data-types → data-types}/timestamp-type.d.ts +0 -0
  144. /package/{esm/data-types → data-types}/timestamp-type.js +0 -0
  145. /package/{types/data-types → data-types}/uuid-type.d.ts +0 -0
  146. /package/{esm/data-types → data-types}/uuid-type.js +0 -0
  147. /package/{types/data-types → data-types}/varchar-type.d.ts +0 -0
  148. /package/{esm/data-types → data-types}/varchar-type.js +0 -0
  149. /package/{types/index.d.ts → index.d.ts} +0 -0
  150. /package/{esm/index.js → index.js} +0 -0
  151. /package/{types/interfaces → interfaces}/command-result.d.ts +0 -0
  152. /package/{esm/interfaces → interfaces}/command-result.js +0 -0
  153. /package/{types/interfaces → interfaces}/data-mapping-options.d.ts +0 -0
  154. /package/{esm/interfaces → interfaces}/data-mapping-options.js +0 -0
  155. /package/{types/interfaces → interfaces}/data-type.d.ts +0 -0
  156. /package/{esm/interfaces → interfaces}/data-type.js +0 -0
  157. /package/{types/interfaces → interfaces}/database-connection-params.d.ts +0 -0
  158. /package/{esm/interfaces → interfaces}/database-connection-params.js +0 -0
  159. /package/{types/interfaces → interfaces}/field-info.d.ts +0 -0
  160. /package/{esm/interfaces → interfaces}/field-info.js +0 -0
  161. /package/{types/interfaces → interfaces}/query-options.d.ts +0 -0
  162. /package/{esm/interfaces → interfaces}/query-options.js +0 -0
  163. /package/{types/interfaces → interfaces}/query-result.d.ts +0 -0
  164. /package/{esm/interfaces → interfaces}/query-result.js +0 -0
  165. /package/{types/interfaces → interfaces}/script-execute-options.d.ts +0 -0
  166. /package/{esm/interfaces → interfaces}/script-execute-options.js +0 -0
  167. /package/{types/interfaces → interfaces}/script-result.d.ts +0 -0
  168. /package/{esm/interfaces → interfaces}/script-result.js +0 -0
  169. /package/{types/interfaces → interfaces}/statement-prepare-options.d.ts +0 -0
  170. /package/{esm/interfaces → interfaces}/statement-prepare-options.js +0 -0
  171. /package/{types/protocol → protocol}/backend.d.ts +0 -0
  172. /package/{types/protocol → protocol}/buffer-reader.d.ts +0 -0
  173. /package/{types/protocol → protocol}/database-error.d.ts +0 -0
  174. /package/{types/protocol → protocol}/frontend.d.ts +0 -0
  175. /package/{types/protocol → protocol}/protocol.d.ts +0 -0
  176. /package/{esm/protocol → protocol}/protocol.js +0 -0
  177. /package/{types/protocol → protocol}/sasl.d.ts +0 -0
  178. /package/{esm/protocol → protocol}/sasl.js +0 -0
  179. /package/{types/protocol → protocol}/smart-buffer.d.ts +0 -0
  180. /package/{types/safe-event-emitter.d.ts → safe-event-emitter.d.ts} +0 -0
  181. /package/{esm/safe-event-emitter.js → safe-event-emitter.js} +0 -0
  182. /package/{types/types.d.ts → types.d.ts} +0 -0
  183. /package/{esm/types.js → types.js} +0 -0
  184. /package/{types/util → util}/array-calculatedim.d.ts +0 -0
  185. /package/{esm/util → util}/array-calculatedim.js +0 -0
  186. /package/{types/util → util}/bigint-methods.d.ts +0 -0
  187. /package/{types/util → util}/config-from-env.d.ts +0 -0
  188. /package/{esm/util → util}/config-from-env.js +0 -0
  189. /package/{types/util → util}/connection-config.d.ts +0 -0
  190. /package/{esm/util → util}/connection-config.js +0 -0
  191. /package/{types/util → util}/convert-row-to-object.d.ts +0 -0
  192. /package/{esm/util → util}/convert-row-to-object.js +0 -0
  193. /package/{types/util → util}/decode-binaryarray.d.ts +0 -0
  194. /package/{esm/util → util}/decode-binaryarray.js +0 -0
  195. /package/{types/util → util}/encode-binaryarray.d.ts +0 -0
  196. /package/{esm/util → util}/encode-binaryarray.js +0 -0
  197. /package/{types/util → util}/escape-literal.d.ts +0 -0
  198. /package/{esm/util → util}/escape-literal.js +0 -0
  199. /package/{types/util → util}/fast-parseint.d.ts +0 -0
  200. /package/{esm/util → util}/fast-parseint.js +0 -0
  201. /package/{types/util → util}/get-parsers.d.ts +0 -0
  202. /package/{esm/util → util}/get-parsers.js +0 -0
  203. /package/{types/util → util}/parse-array.d.ts +0 -0
  204. /package/{esm/util → util}/parse-array.js +0 -0
  205. /package/{types/util → util}/parse-datetime.d.ts +0 -0
  206. /package/{esm/util → util}/parse-datetime.js +0 -0
  207. /package/{types/util → util}/parse-row.d.ts +0 -0
  208. /package/{esm/util → util}/parse-row.js +0 -0
  209. /package/{types/util → util}/parse-time.d.ts +0 -0
  210. /package/{esm/util → util}/parse-time.js +0 -0
  211. /package/{types/util → util}/stringify-arrayliteral.d.ts +0 -0
  212. /package/{esm/util → util}/stringify-arrayliteral.js +0 -0
  213. /package/{types/util → util}/stringify-for-sql.d.ts +0 -0
  214. /package/{esm/util → util}/stringify-for-sql.js +0 -0
  215. /package/{types/util → util}/wrap-row-description.d.ts +0 -0
  216. /package/{esm/util → util}/wrap-row-description.js +0 -0
@@ -1,255 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Frontend = void 0;
4
- const constants_js_1 = require("../constants.js");
5
- const encode_binaryarray_js_1 = require("../util/encode-binaryarray.js");
6
- const stringify_arrayliteral_js_1 = require("../util/stringify-arrayliteral.js");
7
- const protocol_js_1 = require("./protocol.js");
8
- const smart_buffer_js_1 = require("./smart-buffer.js");
9
- const DataFormat = protocol_js_1.Protocol.DataFormat;
10
- const StaticFlushBuffer = Buffer.from([
11
- protocol_js_1.Protocol.FrontendMessageCode.Flush,
12
- 0x00,
13
- 0x00,
14
- 0x00,
15
- 0x04,
16
- ]);
17
- const StaticTerminateBuffer = Buffer.from([
18
- protocol_js_1.Protocol.FrontendMessageCode.Terminate,
19
- 0x00,
20
- 0x00,
21
- 0x00,
22
- 0x04,
23
- ]);
24
- const StaticSyncBuffer = Buffer.from([
25
- protocol_js_1.Protocol.FrontendMessageCode.Sync,
26
- 0x00,
27
- 0x00,
28
- 0x00,
29
- 0x04,
30
- ]);
31
- class Frontend {
32
- constructor(options) {
33
- this._io = new smart_buffer_js_1.SmartBuffer(options?.buffer);
34
- }
35
- getSSLRequestMessage() {
36
- return this._io
37
- .start()
38
- .writeUInt32BE(8) // Length of message contents in bytes, including self.
39
- .writeUInt16BE(1234)
40
- .writeUInt16BE(5679)
41
- .flush();
42
- }
43
- getStartupMessage(args) {
44
- const io = this._io
45
- .start()
46
- .writeInt32BE(0) // Preserve length
47
- .writeInt16BE(protocol_js_1.Protocol.VERSION_MAJOR)
48
- .writeInt16BE(protocol_js_1.Protocol.VERSION_MINOR);
49
- for (const [k, v] of Object.entries(args)) {
50
- if (k !== 'client_encoding')
51
- io.writeCString(k, 'utf8').writeCString(v, 'utf8');
52
- }
53
- io.writeCString('client_encoding', 'utf8')
54
- .writeCString('UTF8', 'utf8')
55
- .writeUInt8(0);
56
- return setLengthAndFlush(io, 0);
57
- }
58
- getPasswordMessage(password) {
59
- const io = this._io
60
- .start()
61
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.PasswordMessage)
62
- .writeInt32BE(0) // Preserve header
63
- .writeCString(password, 'utf8');
64
- return setLengthAndFlush(io, 1);
65
- }
66
- getSASLMessage(sasl) {
67
- const io = this._io
68
- .start()
69
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.PasswordMessage)
70
- .writeInt32BE(0) // Preserve header
71
- .writeCString(sasl.mechanism, 'utf8')
72
- .writeLString(sasl.clientFirstMessage);
73
- return setLengthAndFlush(io, 1);
74
- }
75
- getSASLFinalMessage(session) {
76
- const io = this._io
77
- .start()
78
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.PasswordMessage)
79
- .writeInt32BE(0) // Preserve header
80
- .writeString(session.clientFinalMessage);
81
- return setLengthAndFlush(io, 1);
82
- }
83
- getParseMessage(args) {
84
- if (args.statement && args.statement.length > 63)
85
- throw new Error('Query name length must be lower than 63');
86
- const io = this._io
87
- .start()
88
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Parse)
89
- .writeInt32BE(0) // Preserve header
90
- .writeCString(args.statement || '', 'utf8')
91
- .writeCString(args.sql, 'utf8')
92
- .writeUInt16BE(args.paramTypes ? args.paramTypes.length : 0);
93
- if (args.paramTypes) {
94
- for (const t of args.paramTypes) {
95
- io.writeUInt32BE(t || 0);
96
- }
97
- }
98
- return setLengthAndFlush(io, 1);
99
- }
100
- getBindMessage(args) {
101
- if (args.portal && args.portal.length > 63)
102
- throw new Error('Portal name length must be lower than 63');
103
- if (args.statement && args.statement.length > 63)
104
- throw new Error('Query name length must be lower than 63');
105
- const io = this._io
106
- .start()
107
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Bind)
108
- .writeInt32BE(0) // Preserve header
109
- .writeCString(args.portal || '', 'utf8')
110
- .writeCString(args.statement || '', 'utf8');
111
- const { params, paramTypes, queryOptions } = args;
112
- const columnFormat = queryOptions.columnFormat != null
113
- ? queryOptions.columnFormat
114
- : constants_js_1.DEFAULT_COLUMN_FORMAT;
115
- if (params && params.length) {
116
- io.writeInt16BE(params.length);
117
- const formatOffset = io.offset;
118
- for (let i = 0; i < params.length; i++) {
119
- io.writeInt16BE(0); // Preserve
120
- }
121
- // Write parameter values
122
- io.writeUInt16BE(params.length);
123
- for (let i = 0; i < params?.length; i++) {
124
- let v = params[i];
125
- if (v === null || v === undefined) {
126
- io.writeInt32BE(-1);
127
- continue;
128
- }
129
- const dataTypeOid = paramTypes ? paramTypes[i] : undefined;
130
- const dt = dataTypeOid ? args.typeMap.get(dataTypeOid) : undefined;
131
- if (dt) {
132
- if (typeof dt.encodeAsNull === 'function' &&
133
- dt.encodeAsNull(v, queryOptions)) {
134
- io.writeInt32BE(-1);
135
- continue;
136
- }
137
- if (typeof dt.encodeBinary === 'function') {
138
- // Set param format to binary
139
- io.buffer.writeInt16BE(protocol_js_1.Protocol.DataFormat.binary, formatOffset + i * 2);
140
- // Preserve data length
141
- io.writeInt32BE(0);
142
- const dataOffset = io.offset;
143
- if (dt.elementsOID) {
144
- // If data type is array
145
- v = Array.isArray(v) ? v : [v];
146
- (0, encode_binaryarray_js_1.encodeBinaryArray)(io, v, dt.elementsOID, queryOptions, dt.encodeBinary, dt.encodeCalculateDim);
147
- }
148
- else {
149
- dt.encodeBinary(io, v, queryOptions);
150
- }
151
- io.buffer.writeInt32BE(io.length - dataOffset, dataOffset - 4); // Update length
152
- }
153
- else if (typeof dt.encodeText === 'function') {
154
- v = dt.elementsOID
155
- ? (0, stringify_arrayliteral_js_1.stringifyArrayLiteral)(v, queryOptions, dt.encodeText)
156
- : dt.encodeText(v, queryOptions);
157
- io.writeLString(v, 'utf8');
158
- }
159
- }
160
- else if (Buffer.isBuffer(v)) {
161
- // Set param format to binary
162
- io.buffer.writeInt16BE(protocol_js_1.Protocol.DataFormat.binary, formatOffset + i * 2);
163
- // Preserve data length
164
- io.writeInt32BE(0);
165
- const dataOffset = io.offset;
166
- io.writeBuffer(v);
167
- io.buffer.writeInt32BE(io.length - dataOffset, dataOffset - 4); // Update length
168
- }
169
- else {
170
- io.writeLString('' + v, 'utf8');
171
- }
172
- }
173
- }
174
- else {
175
- io.writeUInt16BE(0);
176
- io.writeUInt16BE(0);
177
- }
178
- if (Array.isArray(columnFormat)) {
179
- io.writeUInt16BE(columnFormat.length);
180
- for (let i = 0; i < columnFormat.length; i++) {
181
- io.writeUInt16BE(columnFormat[i]);
182
- }
183
- }
184
- else if (columnFormat === DataFormat.binary) {
185
- io.writeUInt16BE(1);
186
- io.writeUInt16BE(DataFormat.binary);
187
- }
188
- else
189
- io.writeUInt16BE(0);
190
- return setLengthAndFlush(io, 1);
191
- }
192
- getDescribeMessage(args) {
193
- if (args.name && args.name.length > 63) {
194
- throw new Error(args.type === 'P'
195
- ? 'Portal'
196
- : 'Statement name length must be lower than 63');
197
- }
198
- const io = this._io
199
- .start()
200
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Describe)
201
- .writeInt32BE(0) // Preserve header
202
- .writeUInt8(args.type.charCodeAt(0))
203
- .writeCString(args.name || '', 'utf8');
204
- return setLengthAndFlush(io, 1);
205
- }
206
- getExecuteMessage(args) {
207
- if (args.fetchCount &&
208
- (args.fetchCount < 0 || args.fetchCount > 4294967295)) {
209
- throw new Error('fetchCount can be between 0 and 4294967295');
210
- }
211
- const io = this._io
212
- .start()
213
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Execute)
214
- .writeInt32BE(0) // Preserve header
215
- .writeCString(args.portal || '', 'utf8')
216
- .writeUInt32BE(args.fetchCount || 0);
217
- return setLengthAndFlush(io, 1);
218
- }
219
- getCloseMessage(args) {
220
- if (args.name && args.name.length > 63) {
221
- throw new Error(args.type === 'P'
222
- ? 'Portal'
223
- : 'Statement name length must be lower than 63');
224
- }
225
- const io = this._io
226
- .start()
227
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Close)
228
- .writeInt32BE(0) // Preserve header
229
- .writeUInt8(args.type.charCodeAt(0))
230
- .writeCString(args.name || '', 'utf8');
231
- return setLengthAndFlush(io, 1);
232
- }
233
- getQueryMessage(sql) {
234
- const io = this._io
235
- .start()
236
- .writeInt8(protocol_js_1.Protocol.FrontendMessageCode.Query)
237
- .writeInt32BE(0) // Preserve header
238
- .writeCString(sql || '', 'utf8');
239
- return setLengthAndFlush(io, 1);
240
- }
241
- getFlushMessage() {
242
- return StaticFlushBuffer;
243
- }
244
- getTerminateMessage() {
245
- return StaticTerminateBuffer;
246
- }
247
- getSyncMessage() {
248
- return StaticSyncBuffer;
249
- }
250
- }
251
- exports.Frontend = Frontend;
252
- function setLengthAndFlush(io, lengthOffset) {
253
- io.buffer.writeUInt32BE(io.length - lengthOffset, lengthOffset);
254
- return io.flush();
255
- }
@@ -1,367 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PgSocket = void 0;
4
- const tslib_1 = require("tslib");
5
- const node_crypto_1 = tslib_1.__importDefault(require("node:crypto"));
6
- const node_net_1 = tslib_1.__importDefault(require("node:net"));
7
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
8
- const node_tls_1 = tslib_1.__importDefault(require("node:tls"));
9
- const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
10
- const constants_js_1 = require("../constants.js");
11
- const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
12
- const backend_js_1 = require("./backend.js");
13
- const database_error_js_1 = require("./database-error.js");
14
- const frontend_js_1 = require("./frontend.js");
15
- const protocol_js_1 = require("./protocol.js");
16
- const sasl_js_1 = require("./sasl.js");
17
- const DEFAULT_PORT_NUMBER = 5432;
18
- const COMMAND_RESULT_PATTERN = /^([^\d]+)(?: (\d+)(?: (\d+))?)?$/;
19
- class PgSocket extends safe_event_emitter_js_1.SafeEventEmitter {
20
- constructor(options) {
21
- super();
22
- this.options = options;
23
- this._state = constants_js_1.ConnectionState.CLOSED;
24
- this._backend = new backend_js_1.Backend();
25
- this._sessionParameters = {};
26
- this._frontend = new frontend_js_1.Frontend({ buffer: options.buffer });
27
- this.setMaxListeners(99);
28
- }
29
- get state() {
30
- if (!this._socket || this._socket.destroyed)
31
- this._state = constants_js_1.ConnectionState.CLOSED;
32
- return this._state;
33
- }
34
- get processID() {
35
- return this._processID;
36
- }
37
- get secretKey() {
38
- return this._secretKey;
39
- }
40
- get sessionParameters() {
41
- return this._sessionParameters;
42
- }
43
- connect() {
44
- if (this._socket)
45
- return;
46
- this._state = constants_js_1.ConnectionState.CONNECTING;
47
- const options = this.options;
48
- const socket = (this._socket = new node_net_1.default.Socket());
49
- const errorHandler = (err) => {
50
- this._state = constants_js_1.ConnectionState.CLOSED;
51
- this._removeListeners();
52
- this._reset();
53
- socket.destroy();
54
- this._socket = undefined;
55
- this.emit('error', err);
56
- };
57
- const connectHandler = () => {
58
- socket.setTimeout(0);
59
- if (this.options.keepAlive || this.options.keepAlive == null)
60
- socket.setKeepAlive(true);
61
- socket.write(this._frontend.getSSLRequestMessage());
62
- socket.once('data', x => {
63
- this._removeListeners();
64
- if (x.toString() === 'S') {
65
- const tslOptions = { ...options.ssl, socket };
66
- if (options.host && node_net_1.default.isIP(options.host) === 0)
67
- tslOptions.servername = options.host;
68
- const tlsSocket = (this._socket = node_tls_1.default.connect(tslOptions));
69
- tlsSocket.once('error', errorHandler);
70
- tlsSocket.once('secureConnect', () => {
71
- this._removeListeners();
72
- this._handleConnect();
73
- });
74
- return;
75
- }
76
- if (x.toString() === 'N') {
77
- if (options.requireSSL) {
78
- return errorHandler(new Error('Server does not support SSL connections'));
79
- }
80
- this._removeListeners();
81
- this._handleConnect();
82
- return;
83
- }
84
- return errorHandler(new Error('There was an error establishing an SSL connection'));
85
- });
86
- };
87
- socket.setNoDelay(true);
88
- socket.setTimeout(options.connectTimeoutMs || 30000, () => errorHandler(new Error('Connection timed out')));
89
- socket.once('error', errorHandler);
90
- socket.once('connect', connectHandler);
91
- this.emit('connecting');
92
- const port = options.port || DEFAULT_PORT_NUMBER;
93
- if (options.host && options.host.startsWith('/')) {
94
- socket.connect(node_path_1.default.join(options.host, '/.s.PGSQL.' + port));
95
- }
96
- else
97
- socket.connect(options.port || DEFAULT_PORT_NUMBER, options.host || 'localhost');
98
- }
99
- close() {
100
- if (!this._socket || this._socket.destroyed) {
101
- this._state = constants_js_1.ConnectionState.CLOSED;
102
- this._socket = undefined;
103
- this._reset();
104
- return;
105
- }
106
- if (this._state === constants_js_1.ConnectionState.CLOSING)
107
- return;
108
- const socket = this._socket;
109
- this._state = constants_js_1.ConnectionState.CLOSING;
110
- this._removeListeners();
111
- socket.once('close', () => this._handleClose());
112
- socket.destroy();
113
- }
114
- sendParseMessage(args, cb) {
115
- if (this.listenerCount('debug'))
116
- this.emit('debug', { location: 'PgSocket.sendParseMessage', args });
117
- this._send(this._frontend.getParseMessage(args), cb);
118
- }
119
- sendBindMessage(args, cb) {
120
- if (this.listenerCount('debug'))
121
- this.emit('debug', { location: 'PgSocket.sendBindMessage', args });
122
- this._send(this._frontend.getBindMessage(args), cb);
123
- }
124
- sendDescribeMessage(args, cb) {
125
- if (this.listenerCount('debug'))
126
- this.emit('debug', { location: 'PgSocket.sendDescribeMessage', args });
127
- this._send(this._frontend.getDescribeMessage(args), cb);
128
- }
129
- sendExecuteMessage(args, cb) {
130
- if (this.listenerCount('debug'))
131
- this.emit('debug', { location: 'PgSocket.sendDescribeMessage', args });
132
- this._send(this._frontend.getExecuteMessage(args), cb);
133
- }
134
- sendCloseMessage(args, cb) {
135
- if (this.listenerCount('debug'))
136
- this.emit('debug', { location: 'PgSocket.sendCloseMessage', args });
137
- this._send(this._frontend.getCloseMessage(args), cb);
138
- }
139
- sendQueryMessage(sql, cb) {
140
- if (this.listenerCount('debug'))
141
- this.emit('debug', { location: 'PgSocket.sendQueryMessage', sql });
142
- this._send(this._frontend.getQueryMessage(sql), cb);
143
- }
144
- sendFlushMessage(cb) {
145
- if (this.listenerCount('debug'))
146
- this.emit('debug', { location: 'PgSocket.sendFlushMessage' });
147
- this._send(this._frontend.getFlushMessage(), cb);
148
- }
149
- sendTerminateMessage(cb) {
150
- if (this.listenerCount('debug'))
151
- this.emit('debug', { location: 'PgSocket.sendTerminateMessage' });
152
- this._send(this._frontend.getTerminateMessage(), cb);
153
- }
154
- sendSyncMessage() {
155
- if (this.listenerCount('debug'))
156
- this.emit('debug', { location: 'PgSocket.sendSyncMessage' });
157
- this._send(this._frontend.getSyncMessage());
158
- }
159
- capture(callback) {
160
- if (this._state === constants_js_1.ConnectionState.CLOSING ||
161
- this._state === constants_js_1.ConnectionState.CLOSED) {
162
- return Promise.reject(new Error('Connection closed'));
163
- }
164
- if (this._state !== constants_js_1.ConnectionState.READY)
165
- return Promise.reject(new Error('Connection is not ready'));
166
- return new Promise((resolve, reject) => {
167
- const done = (err, result) => {
168
- this.removeListener('close', closeHandler);
169
- this.removeListener('error', errorHandler);
170
- this.removeListener('message', msgHandler);
171
- if (err)
172
- reject(err);
173
- else
174
- resolve(result);
175
- };
176
- const errorHandler = (err) => {
177
- this.removeListener('close', closeHandler);
178
- this.removeListener('message', msgHandler);
179
- reject(err);
180
- };
181
- const closeHandler = () => {
182
- this.removeListener('error', errorHandler);
183
- this.removeListener('message', msgHandler);
184
- reject(new Error('Connection closed'));
185
- };
186
- const msgHandler = (code, msg) => {
187
- const x = callback(code, msg, done);
188
- if (putil_promisify_1.default.isPromise(x))
189
- x.catch(err => done(err));
190
- };
191
- this.once('close', closeHandler);
192
- this.once('error', errorHandler);
193
- this.on('message', msgHandler);
194
- });
195
- }
196
- _removeListeners() {
197
- if (!this._socket)
198
- return;
199
- this._socket.removeAllListeners('error');
200
- this._socket.removeAllListeners('connect');
201
- this._socket.removeAllListeners('data');
202
- this._socket.removeAllListeners('close');
203
- }
204
- _reset() {
205
- this._backend.reset();
206
- this._sessionParameters = {};
207
- this._processID = undefined;
208
- this._secretKey = undefined;
209
- this._saslSession = undefined;
210
- }
211
- _handleConnect() {
212
- const socket = this._socket;
213
- if (!socket)
214
- return;
215
- this._state = constants_js_1.ConnectionState.AUTHORIZING;
216
- this._reset();
217
- socket.on('data', (data) => this._handleData(data));
218
- socket.on('error', (err) => this._handleError(err));
219
- socket.on('close', () => this._handleClose());
220
- this._send(this._frontend.getStartupMessage({
221
- user: this.options.user || 'postgres',
222
- database: this.options.database || '',
223
- application_name: this.options.applicationName || '',
224
- }));
225
- }
226
- _handleClose() {
227
- this._reset();
228
- this._socket = undefined;
229
- this._state = constants_js_1.ConnectionState.CLOSED;
230
- this.emit('close');
231
- }
232
- _handleError(err) {
233
- if (this._state !== constants_js_1.ConnectionState.READY) {
234
- this._socket?.end();
235
- }
236
- this.emit('error', err);
237
- }
238
- _handleData(data) {
239
- this._backend.parse(data, (code, payload) => {
240
- try {
241
- switch (code) {
242
- case protocol_js_1.Protocol.BackendMessageCode.Authentication:
243
- this._handleAuthenticationMessage(payload);
244
- break;
245
- case protocol_js_1.Protocol.BackendMessageCode.ErrorResponse:
246
- this.emit('error', new database_error_js_1.DatabaseError(payload));
247
- break;
248
- case protocol_js_1.Protocol.BackendMessageCode.NoticeResponse:
249
- this.emit('notice', payload);
250
- break;
251
- case protocol_js_1.Protocol.BackendMessageCode.NotificationResponse:
252
- this.emit('notification', payload);
253
- break;
254
- case protocol_js_1.Protocol.BackendMessageCode.ParameterStatus:
255
- this._handleParameterStatus(payload);
256
- break;
257
- case protocol_js_1.Protocol.BackendMessageCode.BackendKeyData:
258
- this._handleBackendKeyData(payload);
259
- break;
260
- case protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery:
261
- if (this._state !== constants_js_1.ConnectionState.READY) {
262
- this._state = constants_js_1.ConnectionState.READY;
263
- this.emit('ready');
264
- }
265
- else
266
- this.emit('message', code, payload);
267
- break;
268
- case protocol_js_1.Protocol.BackendMessageCode.CommandComplete: {
269
- const msg = this._handleCommandComplete(payload);
270
- this.emit('message', code, msg);
271
- break;
272
- }
273
- default:
274
- this.emit('message', code, payload);
275
- }
276
- }
277
- catch (e) {
278
- this._handleError(e);
279
- }
280
- });
281
- }
282
- _resolvePassword(cb) {
283
- (async () => {
284
- const pass = typeof this.options.password === 'function'
285
- ? await this.options.password()
286
- : this.options.password;
287
- cb(pass || '');
288
- })().catch(err => this._handleError(err));
289
- }
290
- _handleAuthenticationMessage(msg) {
291
- if (!msg) {
292
- this.emit('authenticate');
293
- return;
294
- }
295
- switch (msg.kind) {
296
- case protocol_js_1.Protocol.AuthenticationMessageKind.CleartextPassword:
297
- this._resolvePassword(password => {
298
- this._send(this._frontend.getPasswordMessage(password));
299
- });
300
- break;
301
- case protocol_js_1.Protocol.AuthenticationMessageKind.MD5Password:
302
- this._resolvePassword(password => {
303
- const md5 = (x) => node_crypto_1.default.createHash('md5').update(x, 'utf8').digest('hex');
304
- const l = md5(password + this.options.user);
305
- const r = md5(Buffer.concat([Buffer.from(l), msg.salt]));
306
- const pass = 'md5' + r;
307
- this._send(this._frontend.getPasswordMessage(pass));
308
- });
309
- break;
310
- case protocol_js_1.Protocol.AuthenticationMessageKind.SASL: {
311
- if (!msg.mechanisms.includes('SCRAM-SHA-256')) {
312
- throw new Error('SASL: Only mechanism SCRAM-SHA-256 is currently supported');
313
- }
314
- const saslSession = (this._saslSession = sasl_js_1.SASL.createSession(this.options.user || '', 'SCRAM-SHA-256'));
315
- this._send(this._frontend.getSASLMessage(saslSession));
316
- break;
317
- }
318
- case protocol_js_1.Protocol.AuthenticationMessageKind.SASLContinue: {
319
- const saslSession = this._saslSession;
320
- if (!saslSession)
321
- throw new Error('SASL: Session not started yet');
322
- this._resolvePassword(password => {
323
- sasl_js_1.SASL.continueSession(saslSession, password, msg.data);
324
- const buf = this._frontend.getSASLFinalMessage(saslSession);
325
- this._send(buf);
326
- });
327
- break;
328
- }
329
- case protocol_js_1.Protocol.AuthenticationMessageKind.SASLFinal: {
330
- const session = this._saslSession;
331
- if (!session)
332
- throw new Error('SASL: Session not started yet');
333
- sasl_js_1.SASL.finalizeSession(session, msg.data);
334
- this._saslSession = undefined;
335
- break;
336
- }
337
- default:
338
- break;
339
- }
340
- }
341
- _handleParameterStatus(msg) {
342
- this._sessionParameters[msg.name] = msg.value;
343
- }
344
- _handleBackendKeyData(msg) {
345
- this._processID = msg.processID;
346
- this._secretKey = msg.secretKey;
347
- }
348
- _handleCommandComplete(msg) {
349
- const m = msg.command && msg.command.match(COMMAND_RESULT_PATTERN);
350
- const result = {
351
- command: m[1],
352
- };
353
- if (m[3] != null) {
354
- result.oid = parseInt(m[2], 10);
355
- result.rowCount = parseInt(m[3], 10);
356
- }
357
- else if (m[2])
358
- result.rowCount = parseInt(m[2], 10);
359
- return result;
360
- }
361
- _send(data, cb) {
362
- if (this._socket && this._socket.writable) {
363
- this._socket.write(data, cb);
364
- }
365
- }
366
- }
367
- exports.PgSocket = PgSocket;