postgrejs 2.12.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 (215) hide show
  1. package/CHANGELOG.md +814 -0
  2. package/LICENSE +21 -0
  3. package/README.md +200 -0
  4. package/cjs/connection/bind-param.js +10 -0
  5. package/cjs/connection/connection.js +279 -0
  6. package/cjs/connection/cursor.js +93 -0
  7. package/cjs/connection/intl-connection.js +243 -0
  8. package/cjs/connection/pool.js +226 -0
  9. package/cjs/connection/portal.js +148 -0
  10. package/cjs/connection/prepared-statement.js +217 -0
  11. package/cjs/constants.js +250 -0
  12. package/cjs/data-type-map.js +119 -0
  13. package/cjs/data-types/bool-type.js +27 -0
  14. package/cjs/data-types/box-type.js +52 -0
  15. package/cjs/data-types/bytea-type.js +29 -0
  16. package/cjs/data-types/char-type.js +27 -0
  17. package/cjs/data-types/circle-type.js +48 -0
  18. package/cjs/data-types/date-type.js +67 -0
  19. package/cjs/data-types/float4-type.js +25 -0
  20. package/cjs/data-types/float8-type.js +25 -0
  21. package/cjs/data-types/int2-type.js +26 -0
  22. package/cjs/data-types/int2-vector-type.js +38 -0
  23. package/cjs/data-types/int4-type.js +26 -0
  24. package/cjs/data-types/int8-type.js +31 -0
  25. package/cjs/data-types/json-type.js +38 -0
  26. package/cjs/data-types/jsonb-type.js +42 -0
  27. package/cjs/data-types/lseg-type.js +52 -0
  28. package/cjs/data-types/numeric-type.js +125 -0
  29. package/cjs/data-types/oid-type.js +32 -0
  30. package/cjs/data-types/point-type.js +39 -0
  31. package/cjs/data-types/time-type.js +54 -0
  32. package/cjs/data-types/timestamp-type.js +81 -0
  33. package/cjs/data-types/timestamptz-type.js +73 -0
  34. package/cjs/data-types/uuid-type.js +39 -0
  35. package/cjs/data-types/varchar-type.js +27 -0
  36. package/cjs/index.js +41 -0
  37. package/cjs/interfaces/command-result.js +2 -0
  38. package/cjs/interfaces/data-mapping-options.js +2 -0
  39. package/cjs/interfaces/data-type.js +2 -0
  40. package/cjs/interfaces/database-connection-params.js +2 -0
  41. package/cjs/interfaces/field-info.js +2 -0
  42. package/cjs/interfaces/query-options.js +2 -0
  43. package/cjs/interfaces/query-result.js +2 -0
  44. package/cjs/interfaces/script-execute-options.js +2 -0
  45. package/cjs/interfaces/script-result.js +2 -0
  46. package/cjs/interfaces/statement-prepare-options.js +2 -0
  47. package/cjs/package.json +3 -0
  48. package/cjs/protocol/backend.js +256 -0
  49. package/cjs/protocol/buffer-reader.js +78 -0
  50. package/cjs/protocol/database-error.js +17 -0
  51. package/cjs/protocol/frontend.js +218 -0
  52. package/cjs/protocol/pg-socket.js +345 -0
  53. package/cjs/protocol/protocol.js +71 -0
  54. package/cjs/protocol/sasl.js +108 -0
  55. package/cjs/protocol/smart-buffer.js +195 -0
  56. package/cjs/safe-event-emitter.js +17 -0
  57. package/cjs/types.js +2 -0
  58. package/cjs/util/array-calculatedim.js +19 -0
  59. package/cjs/util/bigint-methods.js +42 -0
  60. package/cjs/util/config-from-env.js +30 -0
  61. package/cjs/util/connection-config.js +65 -0
  62. package/cjs/util/convert-row-to-object.js +13 -0
  63. package/cjs/util/decode-binaryarray.js +39 -0
  64. package/cjs/util/encode-binaryarray.js +45 -0
  65. package/cjs/util/escape-literal.js +27 -0
  66. package/cjs/util/fast-parseint.js +32 -0
  67. package/cjs/util/get-parsers.js +42 -0
  68. package/cjs/util/parse-array.js +61 -0
  69. package/cjs/util/parse-datetime.js +38 -0
  70. package/cjs/util/parse-row.js +11 -0
  71. package/cjs/util/parse-time.js +29 -0
  72. package/cjs/util/stringify-arrayliteral.js +38 -0
  73. package/cjs/util/stringify-for-sql.js +30 -0
  74. package/cjs/util/wrap-row-description.js +35 -0
  75. package/esm/connection/bind-param.js +6 -0
  76. package/esm/connection/connection.js +275 -0
  77. package/esm/connection/cursor.js +86 -0
  78. package/esm/connection/intl-connection.js +238 -0
  79. package/esm/connection/pool.js +222 -0
  80. package/esm/connection/portal.js +144 -0
  81. package/esm/connection/prepared-statement.js +213 -0
  82. package/esm/constants.js +247 -0
  83. package/esm/data-type-map.js +115 -0
  84. package/esm/data-types/bool-type.js +24 -0
  85. package/esm/data-types/box-type.js +49 -0
  86. package/esm/data-types/bytea-type.js +23 -0
  87. package/esm/data-types/char-type.js +24 -0
  88. package/esm/data-types/circle-type.js +45 -0
  89. package/esm/data-types/date-type.js +64 -0
  90. package/esm/data-types/float4-type.js +22 -0
  91. package/esm/data-types/float8-type.js +22 -0
  92. package/esm/data-types/int2-type.js +23 -0
  93. package/esm/data-types/int2-vector-type.js +35 -0
  94. package/esm/data-types/int4-type.js +23 -0
  95. package/esm/data-types/int8-type.js +28 -0
  96. package/esm/data-types/json-type.js +35 -0
  97. package/esm/data-types/jsonb-type.js +39 -0
  98. package/esm/data-types/lseg-type.js +49 -0
  99. package/esm/data-types/numeric-type.js +121 -0
  100. package/esm/data-types/oid-type.js +29 -0
  101. package/esm/data-types/point-type.js +36 -0
  102. package/esm/data-types/time-type.js +51 -0
  103. package/esm/data-types/timestamp-type.js +78 -0
  104. package/esm/data-types/timestamptz-type.js +70 -0
  105. package/esm/data-types/uuid-type.js +36 -0
  106. package/esm/data-types/varchar-type.js +24 -0
  107. package/esm/index.js +25 -0
  108. package/esm/interfaces/command-result.js +1 -0
  109. package/esm/interfaces/data-mapping-options.js +1 -0
  110. package/esm/interfaces/data-type.js +1 -0
  111. package/esm/interfaces/database-connection-params.js +1 -0
  112. package/esm/interfaces/field-info.js +1 -0
  113. package/esm/interfaces/query-options.js +1 -0
  114. package/esm/interfaces/query-result.js +1 -0
  115. package/esm/interfaces/script-execute-options.js +1 -0
  116. package/esm/interfaces/script-result.js +1 -0
  117. package/esm/interfaces/statement-prepare-options.js +1 -0
  118. package/esm/protocol/backend.js +252 -0
  119. package/esm/protocol/buffer-reader.js +74 -0
  120. package/esm/protocol/database-error.js +13 -0
  121. package/esm/protocol/frontend.js +214 -0
  122. package/esm/protocol/pg-socket.js +338 -0
  123. package/esm/protocol/protocol.js +68 -0
  124. package/esm/protocol/sasl.js +102 -0
  125. package/esm/protocol/smart-buffer.js +168 -0
  126. package/esm/safe-event-emitter.js +13 -0
  127. package/esm/types.js +1 -0
  128. package/esm/util/array-calculatedim.js +15 -0
  129. package/esm/util/bigint-methods.js +37 -0
  130. package/esm/util/config-from-env.js +26 -0
  131. package/esm/util/connection-config.js +57 -0
  132. package/esm/util/convert-row-to-object.js +9 -0
  133. package/esm/util/decode-binaryarray.js +35 -0
  134. package/esm/util/encode-binaryarray.js +41 -0
  135. package/esm/util/escape-literal.js +23 -0
  136. package/esm/util/fast-parseint.js +28 -0
  137. package/esm/util/get-parsers.js +38 -0
  138. package/esm/util/parse-array.js +57 -0
  139. package/esm/util/parse-datetime.js +34 -0
  140. package/esm/util/parse-row.js +7 -0
  141. package/esm/util/parse-time.js +25 -0
  142. package/esm/util/stringify-arrayliteral.js +34 -0
  143. package/esm/util/stringify-for-sql.js +25 -0
  144. package/esm/util/wrap-row-description.js +31 -0
  145. package/package.json +54 -0
  146. package/typings/connection/bind-param.d.ts +6 -0
  147. package/typings/connection/connection.d.ts +110 -0
  148. package/typings/connection/cursor.d.ts +25 -0
  149. package/typings/connection/intl-connection.d.ts +40 -0
  150. package/typings/connection/pool.d.ts +52 -0
  151. package/typings/connection/portal.d.ts +23 -0
  152. package/typings/connection/prepared-statement.d.ts +27 -0
  153. package/typings/constants.d.ts +130 -0
  154. package/typings/data-type-map.d.ts +11 -0
  155. package/typings/data-types/bool-type.d.ts +3 -0
  156. package/typings/data-types/box-type.d.ts +3 -0
  157. package/typings/data-types/bytea-type.d.ts +3 -0
  158. package/typings/data-types/char-type.d.ts +3 -0
  159. package/typings/data-types/circle-type.d.ts +3 -0
  160. package/typings/data-types/date-type.d.ts +3 -0
  161. package/typings/data-types/float4-type.d.ts +3 -0
  162. package/typings/data-types/float8-type.d.ts +3 -0
  163. package/typings/data-types/int2-type.d.ts +3 -0
  164. package/typings/data-types/int2-vector-type.d.ts +3 -0
  165. package/typings/data-types/int4-type.d.ts +3 -0
  166. package/typings/data-types/int8-type.d.ts +3 -0
  167. package/typings/data-types/json-type.d.ts +3 -0
  168. package/typings/data-types/jsonb-type.d.ts +3 -0
  169. package/typings/data-types/lseg-type.d.ts +3 -0
  170. package/typings/data-types/numeric-type.d.ts +12 -0
  171. package/typings/data-types/oid-type.d.ts +4 -0
  172. package/typings/data-types/point-type.d.ts +3 -0
  173. package/typings/data-types/time-type.d.ts +3 -0
  174. package/typings/data-types/timestamp-type.d.ts +3 -0
  175. package/typings/data-types/timestamptz-type.d.ts +3 -0
  176. package/typings/data-types/uuid-type.d.ts +3 -0
  177. package/typings/data-types/varchar-type.d.ts +3 -0
  178. package/typings/index.d.ts +25 -0
  179. package/typings/interfaces/command-result.d.ts +28 -0
  180. package/typings/interfaces/data-mapping-options.d.ts +9 -0
  181. package/typings/interfaces/data-type.d.ts +31 -0
  182. package/typings/interfaces/database-connection-params.d.ts +38 -0
  183. package/typings/interfaces/field-info.d.ts +46 -0
  184. package/typings/interfaces/query-options.d.ts +46 -0
  185. package/typings/interfaces/query-result.d.ts +8 -0
  186. package/typings/interfaces/script-execute-options.d.ts +26 -0
  187. package/typings/interfaces/script-result.d.ts +15 -0
  188. package/typings/interfaces/statement-prepare-options.d.ts +13 -0
  189. package/typings/protocol/backend.d.ts +9 -0
  190. package/typings/protocol/buffer-reader.d.ts +18 -0
  191. package/typings/protocol/database-error.d.ts +20 -0
  192. package/typings/protocol/frontend.d.ts +59 -0
  193. package/typings/protocol/pg-socket.d.ts +51 -0
  194. package/typings/protocol/protocol.d.ts +180 -0
  195. package/typings/protocol/sasl.d.ts +13 -0
  196. package/typings/protocol/smart-buffer.d.ts +39 -0
  197. package/typings/safe-event-emitter.d.ts +5 -0
  198. package/typings/types.d.ts +15 -0
  199. package/typings/util/array-calculatedim.d.ts +1 -0
  200. package/typings/util/bigint-methods.d.ts +3 -0
  201. package/typings/util/config-from-env.d.ts +2 -0
  202. package/typings/util/connection-config.d.ts +3 -0
  203. package/typings/util/convert-row-to-object.d.ts +2 -0
  204. package/typings/util/decode-binaryarray.d.ts +4 -0
  205. package/typings/util/encode-binaryarray.d.ts +5 -0
  206. package/typings/util/escape-literal.d.ts +1 -0
  207. package/typings/util/fast-parseint.d.ts +1 -0
  208. package/typings/util/get-parsers.d.ts +4 -0
  209. package/typings/util/parse-array.d.ts +5 -0
  210. package/typings/util/parse-datetime.d.ts +1 -0
  211. package/typings/util/parse-row.d.ts +3 -0
  212. package/typings/util/parse-time.d.ts +3 -0
  213. package/typings/util/stringify-arrayliteral.d.ts +3 -0
  214. package/typings/util/stringify-for-sql.d.ts +4 -0
  215. package/typings/util/wrap-row-description.d.ts +5 -0
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayPointType = exports.PointType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const POINT_PATTERN1 = /^\( *(-?\d+\.?\d*) *, *(-?\d+\.?\d*) *\)$/;
6
+ const POINT_PATTERN2 = /^(-?\d+\.?\d*) *, *(-?\d+\.?\d*)$/;
7
+ exports.PointType = {
8
+ name: 'point',
9
+ oid: constants_js_1.DataTypeOIDs.point,
10
+ jsType: 'object',
11
+ parseBinary(v) {
12
+ return {
13
+ x: v.readDoubleBE(0),
14
+ y: v.readDoubleBE(8),
15
+ };
16
+ },
17
+ encodeBinary(buf, v) {
18
+ buf.writeDoubleBE(v.x);
19
+ buf.writeDoubleBE(v.y);
20
+ },
21
+ parseText(v) {
22
+ const m = v.match(POINT_PATTERN1) || v.match(POINT_PATTERN2);
23
+ if (!m)
24
+ return undefined;
25
+ return {
26
+ x: parseFloat(m[1]),
27
+ y: parseFloat(m[2]),
28
+ };
29
+ },
30
+ isType(v) {
31
+ return typeof v === 'object' && Object.keys(v).length === 2 && typeof v.x === 'number' && typeof v.y === 'number';
32
+ },
33
+ };
34
+ exports.ArrayPointType = {
35
+ ...exports.PointType,
36
+ name: '_point',
37
+ oid: constants_js_1.DataTypeOIDs._point,
38
+ elementsOID: constants_js_1.DataTypeOIDs.point,
39
+ };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayTimeType = exports.TimeType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const parse_time_js_1 = require("../util/parse-time.js");
6
+ const timeMul = 4294967296;
7
+ exports.TimeType = {
8
+ name: 'time',
9
+ oid: constants_js_1.DataTypeOIDs.time,
10
+ jsType: 'string',
11
+ parseBinary(v, options) {
12
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.time);
13
+ const hi = v.readInt32BE();
14
+ const lo = v.readUInt32BE(4);
15
+ let d = new Date((lo + hi * timeMul) / 1000);
16
+ if (fetchAsString || !options.utcDates)
17
+ d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
18
+ return fetchAsString ? dateToTimeString(d) : d;
19
+ },
20
+ encodeBinary(buf, v, options) {
21
+ if (typeof v === 'string')
22
+ v = (0, parse_time_js_1.parseTime)(v, false, options.utcDates);
23
+ if (!(v instanceof Date))
24
+ v = new Date(v);
25
+ // Postgresql ignores timezone data so we are
26
+ let n = options.utcDates ? v.getTime() : v.getTime() - v.getTimezoneOffset() * 60 * 1000;
27
+ n = n * 1000;
28
+ const hi = Math.floor(n / timeMul);
29
+ const lo = n - hi * timeMul;
30
+ buf.writeInt32BE(hi);
31
+ buf.writeUInt32BE(lo);
32
+ },
33
+ parseText(v, options) {
34
+ if (options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.time))
35
+ return v;
36
+ return (0, parse_time_js_1.parseTime)(v, false, options.utcDates);
37
+ },
38
+ isType(v) {
39
+ return ((v instanceof Date && v.getFullYear() === 1970 && v.getMonth() === 0 && v.getDate() === 1) ||
40
+ (typeof v === 'string' && parse_time_js_1.STRICT_TIME_PATTERN.test(v)));
41
+ },
42
+ };
43
+ function padZero(v) {
44
+ return v < 9 ? '0' + v : '' + v;
45
+ }
46
+ function dateToTimeString(d) {
47
+ return padZero(d.getHours()) + ':' + padZero(d.getMinutes()) + ':' + padZero(d.getSeconds());
48
+ }
49
+ exports.ArrayTimeType = {
50
+ ...exports.TimeType,
51
+ name: '_time',
52
+ oid: constants_js_1.DataTypeOIDs._time,
53
+ elementsOID: constants_js_1.DataTypeOIDs.time,
54
+ };
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayTimestampType = exports.TimestampType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const parse_datetime_js_1 = require("../util/parse-datetime.js");
6
+ const timeShift = 946684800000;
7
+ const timeMul = 4294967296;
8
+ exports.TimestampType = {
9
+ name: 'timestamp',
10
+ oid: constants_js_1.DataTypeOIDs.timestamp,
11
+ jsType: 'Date',
12
+ parseBinary(v, options) {
13
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamp);
14
+ const hi = v.readInt32BE();
15
+ const lo = v.readUInt32BE(4);
16
+ if (lo === 0xffffffff && hi === 0x7fffffff)
17
+ return fetchAsString ? 'infinity' : Infinity;
18
+ if (lo === 0x00000000 && hi === -0x80000000)
19
+ return fetchAsString ? '-infinity' : -Infinity;
20
+ // Shift from 2000 to 1970
21
+ let d = new Date((lo + hi * timeMul) / 1000 + timeShift);
22
+ if (fetchAsString || !options.utcDates)
23
+ d = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
24
+ return fetchAsString ? dateToTimestampString(d) : d;
25
+ },
26
+ encodeBinary(buf, v, options) {
27
+ if (typeof v === 'string')
28
+ v = (0, parse_datetime_js_1.parseDateTime)(v, true, false, options.utcDates);
29
+ if (v === Infinity) {
30
+ buf.writeInt32BE(0x7fffffff); // hi
31
+ buf.writeUInt32BE(0xffffffff); // lo
32
+ return;
33
+ }
34
+ if (v === -Infinity) {
35
+ buf.writeInt32BE(-0x80000000); // hi
36
+ buf.writeUInt32BE(0x00000000); // lo
37
+ return;
38
+ }
39
+ if (!(v instanceof Date))
40
+ v = new Date(v);
41
+ // Postgresql ignores timezone data so we are
42
+ let n = options.utcDates ? v.getTime() : v.getTime() - v.getTimezoneOffset() * 60 * 1000;
43
+ n = (n - timeShift) * 1000;
44
+ const hi = Math.floor(n / timeMul);
45
+ const lo = n - hi * timeMul;
46
+ buf.writeInt32BE(hi);
47
+ buf.writeUInt32BE(lo);
48
+ },
49
+ parseText(v, options) {
50
+ if (options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamp))
51
+ return v;
52
+ return (0, parse_datetime_js_1.parseDateTime)(v, true, false, options.utcDates);
53
+ },
54
+ isType(v) {
55
+ return (v instanceof Date &&
56
+ !(v.getFullYear() === 1970 && v.getMonth() === 0 && v.getDate() === 1) &&
57
+ !(v.getHours() === 0 && v.getMinutes() === 0 && v.getSeconds() === 0 && v.getMilliseconds() === 0));
58
+ },
59
+ };
60
+ function padZero(v) {
61
+ return v < 9 ? '0' + v : '' + v;
62
+ }
63
+ function dateToTimestampString(d) {
64
+ return (d.getFullYear() +
65
+ '-' +
66
+ padZero(d.getMonth() + 1) +
67
+ '-' +
68
+ padZero(d.getDate()) +
69
+ ' ' +
70
+ padZero(d.getHours()) +
71
+ ':' +
72
+ padZero(d.getMinutes()) +
73
+ ':' +
74
+ padZero(d.getSeconds()));
75
+ }
76
+ exports.ArrayTimestampType = {
77
+ ...exports.TimestampType,
78
+ name: '_timestamp',
79
+ oid: constants_js_1.DataTypeOIDs._timestamp,
80
+ elementsOID: constants_js_1.DataTypeOIDs.timestamp,
81
+ };
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayTimestamptzType = exports.TimestamptzType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const parse_datetime_js_1 = require("../util/parse-datetime.js");
6
+ const timeShift = 946684800000;
7
+ const timeMul = 4294967296;
8
+ exports.TimestamptzType = {
9
+ name: 'timestamptz',
10
+ oid: constants_js_1.DataTypeOIDs.timestamptz,
11
+ jsType: 'Date',
12
+ parseBinary(v, options) {
13
+ const fetchAsString = options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamptz);
14
+ const hi = v.readInt32BE();
15
+ const lo = v.readUInt32BE(4);
16
+ if (lo === 0xffffffff && hi === 0x7fffffff)
17
+ return fetchAsString ? 'infinity' : Infinity;
18
+ if (lo === 0x00000000 && hi === -0x80000000)
19
+ return fetchAsString ? '-infinity' : -Infinity;
20
+ // Shift from 2000 to 1970
21
+ let d = new Date((lo + hi * timeMul) / 1000 + timeShift);
22
+ if (fetchAsString || !options.utcDates)
23
+ d = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
24
+ return fetchAsString ? dateToTimestamptzString(d) : d;
25
+ },
26
+ encodeBinary(buf, v, options) {
27
+ if (typeof v === 'string')
28
+ v = (0, parse_datetime_js_1.parseDateTime)(v, true, true, options.utcDates);
29
+ if (v === Infinity) {
30
+ buf.writeInt32BE(0x7fffffff); // hi
31
+ buf.writeUInt32BE(0xffffffff); // lo
32
+ return;
33
+ }
34
+ if (v === -Infinity) {
35
+ buf.writeInt32BE(-0x80000000); // hi
36
+ buf.writeUInt32BE(0x00000000); // lo
37
+ return;
38
+ }
39
+ if (!(v instanceof Date))
40
+ v = new Date(v);
41
+ let n = v.getTime();
42
+ n = (n - timeShift) * 1000;
43
+ const hi = Math.floor(n / timeMul);
44
+ const lo = n - hi * timeMul;
45
+ buf.writeInt32BE(hi);
46
+ buf.writeUInt32BE(lo);
47
+ },
48
+ parseText(v, options) {
49
+ const d = (0, parse_datetime_js_1.parseDateTime)(v, true, true, options.utcDates);
50
+ if (options.fetchAsString && options.fetchAsString.includes(constants_js_1.DataTypeOIDs.timestamptz)) {
51
+ if (d instanceof Date)
52
+ return dateToTimestamptzString(d);
53
+ if (d === Infinity)
54
+ return 'infinity';
55
+ if (d === -Infinity)
56
+ return '-infinity';
57
+ return '';
58
+ }
59
+ return d;
60
+ },
61
+ isType(v) {
62
+ return v instanceof Date;
63
+ },
64
+ };
65
+ function dateToTimestamptzString(d) {
66
+ return d.toISOString().replace('T', ' ');
67
+ }
68
+ exports.ArrayTimestamptzType = {
69
+ ...exports.TimestamptzType,
70
+ name: '_timestamptz',
71
+ oid: constants_js_1.DataTypeOIDs._timestamptz,
72
+ elementsOID: constants_js_1.DataTypeOIDs.timestamptz,
73
+ };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayUuidType = exports.UuidType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ const GUID_PATTERN = /^[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}$/;
6
+ exports.UuidType = {
7
+ name: 'uuid',
8
+ oid: constants_js_1.DataTypeOIDs.uuid,
9
+ jsType: 'String',
10
+ parseBinary(v) {
11
+ return (v.toString('hex', 0, 4) +
12
+ '-' +
13
+ v.toString('hex', 4, 6) +
14
+ '-' +
15
+ v.toString('hex', 6, 8) +
16
+ '-' +
17
+ v.toString('hex', 8, 10) +
18
+ '-' +
19
+ v.toString('hex', 10, 16));
20
+ },
21
+ encodeBinary(buf, v) {
22
+ if (!GUID_PATTERN.test(v))
23
+ throw new Error(`"${v}" is not a valid guid value`);
24
+ const b = Buffer.from(v.replace(/-/g, ''), 'hex');
25
+ buf.writeBuffer(b);
26
+ },
27
+ parseText(v) {
28
+ return v;
29
+ },
30
+ isType(v) {
31
+ return typeof v === 'string' && GUID_PATTERN.test(v);
32
+ },
33
+ };
34
+ exports.ArrayUuidType = {
35
+ ...exports.UuidType,
36
+ name: '_uuid',
37
+ oid: constants_js_1.DataTypeOIDs._uuid,
38
+ elementsOID: constants_js_1.DataTypeOIDs.uuid,
39
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayVarcharType = exports.VarcharType = void 0;
4
+ const constants_js_1 = require("../constants.js");
5
+ exports.VarcharType = {
6
+ name: 'varchar',
7
+ oid: constants_js_1.DataTypeOIDs.varchar,
8
+ jsType: 'string',
9
+ parseBinary(v) {
10
+ return v.toString('utf8');
11
+ },
12
+ encodeBinary(buf, v) {
13
+ buf.writeString('' + v, 'utf8');
14
+ },
15
+ parseText(v) {
16
+ return '' + v;
17
+ },
18
+ isType(v) {
19
+ return typeof v === 'string';
20
+ },
21
+ };
22
+ exports.ArrayVarcharType = {
23
+ ...exports.VarcharType,
24
+ name: '_varchar',
25
+ oid: constants_js_1.DataTypeOIDs._varchar,
26
+ elementsOID: constants_js_1.DataTypeOIDs.varchar,
27
+ };
package/cjs/index.js ADDED
@@ -0,0 +1,41 @@
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
+ __exportStar(require("./constants.js"), exports);
18
+ __exportStar(require("./data-type-map.js"), exports);
19
+ __exportStar(require("./types.js"), exports);
20
+ __exportStar(require("./connection/bind-param.js"), exports);
21
+ __exportStar(require("./connection/connection.js"), exports);
22
+ __exportStar(require("./connection/cursor.js"), exports);
23
+ __exportStar(require("./connection/pool.js"), exports);
24
+ __exportStar(require("./connection/prepared-statement.js"), exports);
25
+ __exportStar(require("./interfaces/command-result.js"), exports);
26
+ __exportStar(require("./interfaces/data-mapping-options.js"), exports);
27
+ __exportStar(require("./interfaces/data-type.js"), exports);
28
+ __exportStar(require("./interfaces/database-connection-params.js"), exports);
29
+ __exportStar(require("./interfaces/field-info.js"), exports);
30
+ __exportStar(require("./interfaces/query-options.js"), exports);
31
+ __exportStar(require("./interfaces/query-result.js"), exports);
32
+ __exportStar(require("./interfaces/script-execute-options.js"), exports);
33
+ __exportStar(require("./interfaces/script-result.js"), exports);
34
+ __exportStar(require("./interfaces/statement-prepare-options.js"), exports);
35
+ __exportStar(require("./util/connection-config.js"), exports);
36
+ __exportStar(require("./util/escape-literal.js"), exports);
37
+ __exportStar(require("./util/parse-datetime.js"), exports);
38
+ __exportStar(require("./util/stringify-arrayliteral.js"), exports);
39
+ __exportStar(require("./util/stringify-for-sql.js"), exports);
40
+ __exportStar(require("./protocol/smart-buffer.js"), exports);
41
+ __exportStar(require("./protocol/database-error.js"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Backend = void 0;
4
+ const buffer_reader_js_1 = require("./buffer-reader.js");
5
+ const protocol_js_1 = require("./protocol.js");
6
+ // 1 byte message type, 4 byte frame length
7
+ const HEADER_LENGTH = 5;
8
+ const ErrorFieldTypes = {
9
+ M: 'message',
10
+ S: 'severity',
11
+ V: 'severity',
12
+ C: 'code',
13
+ D: 'detail',
14
+ H: 'hint',
15
+ P: 'position',
16
+ p: 'internalPosition',
17
+ q: 'internalQuery',
18
+ W: 'where',
19
+ s: 'schema',
20
+ t: 'table',
21
+ c: 'column',
22
+ d: 'dataType',
23
+ n: 'constraint',
24
+ F: 'file',
25
+ L: 'line',
26
+ R: 'routine',
27
+ };
28
+ class Backend {
29
+ reset() {
30
+ this._buf = undefined;
31
+ }
32
+ parse(data, callback) {
33
+ if (this._buf) {
34
+ data = Buffer.concat([this._buf, data]);
35
+ this._buf = undefined;
36
+ }
37
+ const io = new buffer_reader_js_1.BufferReader(data);
38
+ let offsetBookmark;
39
+ while (io.length - io.offset >= HEADER_LENGTH) {
40
+ offsetBookmark = io.offset;
41
+ const code = io.readUInt8();
42
+ const len = io.readUInt32BE();
43
+ // Check if frame data not received yet
44
+ if (io.length - io.offset < len - 4) {
45
+ io.offset = offsetBookmark;
46
+ this._buf = io.readBuffer();
47
+ return;
48
+ }
49
+ const parser = MessageParsers[code];
50
+ const v = parser && parser(io, code, len);
51
+ callback(code, v);
52
+ // Set offset to next message
53
+ io.offset = offsetBookmark + len + 1;
54
+ }
55
+ if (io.offset < io.length)
56
+ this._buf = io.readBuffer(io.length - io.offset);
57
+ }
58
+ }
59
+ exports.Backend = Backend;
60
+ const MessageParsers = {
61
+ [protocol_js_1.Protocol.BackendMessageCode.Authentication]: parseAuthentication,
62
+ [protocol_js_1.Protocol.BackendMessageCode.BackendKeyData]: parseBackendKeyData,
63
+ [protocol_js_1.Protocol.BackendMessageCode.CommandComplete]: parseCommandComplete,
64
+ [protocol_js_1.Protocol.BackendMessageCode.CopyData]: parseCopyData,
65
+ [protocol_js_1.Protocol.BackendMessageCode.CopyInResponse]: parseCopyResponse,
66
+ [protocol_js_1.Protocol.BackendMessageCode.CopyOutResponse]: parseCopyResponse,
67
+ [protocol_js_1.Protocol.BackendMessageCode.CopyBothResponse]: parseCopyResponse,
68
+ [protocol_js_1.Protocol.BackendMessageCode.DataRow]: parseDataRow,
69
+ [protocol_js_1.Protocol.BackendMessageCode.ErrorResponse]: parseErrorResponse,
70
+ [protocol_js_1.Protocol.BackendMessageCode.NoticeResponse]: parseErrorResponse,
71
+ [protocol_js_1.Protocol.BackendMessageCode.NotificationResponse]: parseNotificationResponse,
72
+ [protocol_js_1.Protocol.BackendMessageCode.FunctionCallResponse]: parseFunctionCallResponse,
73
+ [protocol_js_1.Protocol.BackendMessageCode.NegotiateProtocolVersion]: parseNegotiateProtocolVersion,
74
+ [protocol_js_1.Protocol.BackendMessageCode.ParameterDescription]: parseParameterDescription,
75
+ [protocol_js_1.Protocol.BackendMessageCode.ParameterStatus]: parseParameterStatus,
76
+ [protocol_js_1.Protocol.BackendMessageCode.ReadyForQuery]: parseReadyForQuery,
77
+ [protocol_js_1.Protocol.BackendMessageCode.RowDescription]: parseRowDescription,
78
+ };
79
+ function parseAuthentication(io, code, len) {
80
+ const kind = io.readUInt32BE();
81
+ switch (kind) {
82
+ case 0:
83
+ return; // AuthenticationOk
84
+ case 2:
85
+ return {
86
+ kind: 'KerberosV5',
87
+ };
88
+ case 3:
89
+ return {
90
+ kind: 'CleartextPassword',
91
+ };
92
+ case 5:
93
+ return {
94
+ kind: 'MD5Password',
95
+ salt: io.readBuffer(len - 8),
96
+ };
97
+ case 6:
98
+ return {
99
+ kind: 'SCMCredential',
100
+ };
101
+ case 7:
102
+ return {
103
+ kind: 'GSS',
104
+ };
105
+ case 9:
106
+ return {
107
+ kind: 'SSPI',
108
+ };
109
+ case 8:
110
+ return {
111
+ kind: 'GSSContinue',
112
+ data: io.readBuffer(len - 8),
113
+ };
114
+ case 10: {
115
+ const out = {
116
+ kind: 'SASL',
117
+ mechanisms: [],
118
+ };
119
+ let mechanism;
120
+ while ((mechanism = io.readCString())) {
121
+ out.mechanisms.push(mechanism);
122
+ }
123
+ return out;
124
+ }
125
+ case 11:
126
+ return {
127
+ kind: 'SASLContinue',
128
+ data: io.readLString(len - 8, 'utf8'),
129
+ };
130
+ case 12:
131
+ return {
132
+ kind: 'SASLFinal',
133
+ data: io.readLString(len - 8, 'utf8'),
134
+ };
135
+ default:
136
+ throw new Error(`Unknown authentication kind (${kind})`);
137
+ }
138
+ }
139
+ function parseBackendKeyData(io) {
140
+ return {
141
+ processID: io.readUInt32BE(),
142
+ secretKey: io.readUInt32BE(),
143
+ };
144
+ }
145
+ function parseCommandComplete(io) {
146
+ return {
147
+ command: io.readCString('utf8'),
148
+ };
149
+ }
150
+ function parseCopyData(io, code, len) {
151
+ return {
152
+ data: io.readBuffer(len - 4),
153
+ };
154
+ }
155
+ function parseCopyResponse(io) {
156
+ const out = {
157
+ overallFormat: io.readUInt8() === 0 ? protocol_js_1.Protocol.DataFormat.text : protocol_js_1.Protocol.DataFormat.binary,
158
+ columnCount: io.readUInt16BE(),
159
+ };
160
+ if (out.columnCount) {
161
+ out.columnFormats = [];
162
+ for (let i = 0; i < out.columnCount; i++) {
163
+ out.columnFormats.push(io.readUInt16BE() === 0 ? protocol_js_1.Protocol.DataFormat.text : protocol_js_1.Protocol.DataFormat.binary);
164
+ }
165
+ }
166
+ return out;
167
+ }
168
+ function parseDataRow(io) {
169
+ const out = {
170
+ columnCount: io.readUInt16BE(),
171
+ };
172
+ if (out.columnCount) {
173
+ out.columns = [];
174
+ for (let i = 0; i < out.columnCount; i++) {
175
+ // The length of the column value, in bytes (this count does not include itself).
176
+ // Can be zero. As a special case, -1 indicates a NULL column value.
177
+ // No value bytes follow in the NULL case.
178
+ const l = io.readInt32BE();
179
+ if (l < 0)
180
+ out.columns.push(null);
181
+ else
182
+ out.columns.push(io.readBuffer(l));
183
+ }
184
+ }
185
+ return out;
186
+ }
187
+ function parseErrorResponse(io) {
188
+ const out = {};
189
+ let fieldType;
190
+ while ((fieldType = io.readLString(1)) !== '\0') {
191
+ const value = io.readCString('utf8');
192
+ const key = ErrorFieldTypes[fieldType];
193
+ if (key)
194
+ out[key] = value;
195
+ }
196
+ return out;
197
+ }
198
+ function parseNotificationResponse(io) {
199
+ return {
200
+ processId: io.readUInt32BE(),
201
+ channel: io.readCString(),
202
+ payload: io.readCString(),
203
+ };
204
+ }
205
+ function parseFunctionCallResponse(io, code, len) {
206
+ return {
207
+ result: io.readBuffer(len - 4),
208
+ };
209
+ }
210
+ function parseNegotiateProtocolVersion(io) {
211
+ return {
212
+ supportedVersionMinor: io.readUInt32BE(),
213
+ numberOfNotSupportedVersions: io.readUInt32BE(),
214
+ option: io.readCString('utf8'),
215
+ };
216
+ }
217
+ function parseParameterDescription(io) {
218
+ const out = {
219
+ parameterCount: io.readUInt32BE(),
220
+ parameterIds: [],
221
+ };
222
+ for (let i = 0; i < out.parameterCount; i++) {
223
+ out.parameterIds.push(io.readUInt32BE());
224
+ }
225
+ return out;
226
+ }
227
+ function parseParameterStatus(io) {
228
+ return {
229
+ name: io.readCString('utf8'),
230
+ value: io.readCString('utf8'),
231
+ };
232
+ }
233
+ function parseReadyForQuery(io) {
234
+ return {
235
+ status: io.readLString(1),
236
+ };
237
+ }
238
+ function parseRowDescription(io) {
239
+ const fieldCount = io.readUInt16BE();
240
+ const out = {
241
+ fields: [],
242
+ };
243
+ for (let i = 0; i < fieldCount; i++) {
244
+ const field = {
245
+ fieldName: io.readCString('utf8'),
246
+ tableId: io.readInt32BE(),
247
+ columnId: io.readInt16BE(),
248
+ dataTypeId: io.readInt32BE(),
249
+ fixedSize: io.readInt16BE(),
250
+ modifier: io.readInt32BE(),
251
+ format: io.readInt16BE() === 0 ? protocol_js_1.Protocol.DataFormat.text : protocol_js_1.Protocol.DataFormat.binary,
252
+ };
253
+ out.fields.push(field);
254
+ }
255
+ return out;
256
+ }