postgrejs 3.11.1 → 3.12.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 (187) hide show
  1. package/connection/bind-param.d.ts +21 -0
  2. package/connection/bind-param.js +21 -0
  3. package/connection/connection.d.ts +136 -0
  4. package/connection/connection.js +132 -5
  5. package/connection/copy-stream.d.ts +2 -0
  6. package/connection/copy-stream.js +2 -0
  7. package/connection/cursor.d.ts +67 -5
  8. package/connection/cursor.js +88 -15
  9. package/connection/intl-connection.d.ts +22 -0
  10. package/connection/intl-connection.js +107 -17
  11. package/connection/large-object.d.ts +18 -0
  12. package/connection/large-object.js +17 -0
  13. package/connection/logical-replication.d.ts +15 -0
  14. package/connection/logical-replication.js +6 -0
  15. package/connection/pool.d.ts +80 -0
  16. package/connection/pool.js +82 -9
  17. package/connection/portal.d.ts +37 -0
  18. package/connection/portal.js +29 -0
  19. package/connection/prepared-statement.d.ts +109 -0
  20. package/connection/prepared-statement.js +150 -7
  21. package/constants.d.ts +18 -0
  22. package/constants.js +18 -0
  23. package/data-type-map.d.ts +38 -0
  24. package/data-type-map.js +38 -0
  25. package/data-types/bit-type.d.ts +4 -0
  26. package/data-types/bit-type.js +4 -0
  27. package/data-types/bool-type.d.ts +2 -0
  28. package/data-types/bool-type.js +2 -0
  29. package/data-types/box-type.d.ts +2 -0
  30. package/data-types/box-type.js +2 -0
  31. package/data-types/bytea-type.d.ts +2 -0
  32. package/data-types/bytea-type.js +2 -0
  33. package/data-types/char-type.d.ts +2 -0
  34. package/data-types/char-type.js +2 -0
  35. package/data-types/circle-type.d.ts +2 -0
  36. package/data-types/circle-type.js +2 -0
  37. package/data-types/date-type.d.ts +2 -0
  38. package/data-types/date-type.js +2 -0
  39. package/data-types/float4-type.d.ts +2 -0
  40. package/data-types/float4-type.js +2 -0
  41. package/data-types/float8-type.d.ts +2 -0
  42. package/data-types/float8-type.js +2 -0
  43. package/data-types/inet-type.d.ts +4 -0
  44. package/data-types/inet-type.js +4 -0
  45. package/data-types/int2-type.d.ts +2 -0
  46. package/data-types/int2-type.js +3 -1
  47. package/data-types/int2-vector-type.d.ts +2 -0
  48. package/data-types/int2-vector-type.js +2 -0
  49. package/data-types/int4-type.d.ts +2 -0
  50. package/data-types/int4-type.js +3 -1
  51. package/data-types/int8-type.d.ts +2 -0
  52. package/data-types/int8-type.js +4 -2
  53. package/data-types/interval-type.d.ts +9 -0
  54. package/data-types/interval-type.js +9 -0
  55. package/data-types/json-type.d.ts +2 -0
  56. package/data-types/json-type.js +2 -0
  57. package/data-types/jsonb-type.d.ts +2 -0
  58. package/data-types/jsonb-type.js +2 -0
  59. package/data-types/jsonpath-type.d.ts +2 -0
  60. package/data-types/jsonpath-type.js +2 -0
  61. package/data-types/line-type.d.ts +2 -0
  62. package/data-types/line-type.js +2 -0
  63. package/data-types/lseg-type.d.ts +2 -0
  64. package/data-types/lseg-type.js +2 -0
  65. package/data-types/macaddr-type.d.ts +4 -0
  66. package/data-types/macaddr-type.js +4 -0
  67. package/data-types/money-type.d.ts +1 -0
  68. package/data-types/money-type.js +17 -10
  69. package/data-types/numeric-type.d.ts +2 -0
  70. package/data-types/numeric-type.js +2 -0
  71. package/data-types/oid-type.d.ts +2 -0
  72. package/data-types/oid-type.js +3 -1
  73. package/data-types/oid-vector-type.d.ts +1 -0
  74. package/data-types/oid-vector-type.js +1 -0
  75. package/data-types/path-type.d.ts +4 -0
  76. package/data-types/path-type.js +4 -0
  77. package/data-types/pg-lsn-type.d.ts +2 -0
  78. package/data-types/pg-lsn-type.js +2 -0
  79. package/data-types/point-type.d.ts +2 -0
  80. package/data-types/point-type.js +2 -0
  81. package/data-types/range-type.d.ts +4 -0
  82. package/data-types/range-type.js +4 -0
  83. package/data-types/snapshot-type.d.ts +4 -0
  84. package/data-types/snapshot-type.js +4 -0
  85. package/data-types/tid-type.d.ts +2 -0
  86. package/data-types/tid-type.js +2 -0
  87. package/data-types/time-type.d.ts +2 -0
  88. package/data-types/time-type.js +2 -0
  89. package/data-types/timestamp-type.d.ts +2 -0
  90. package/data-types/timestamp-type.js +2 -0
  91. package/data-types/timestamptz-type.d.ts +2 -0
  92. package/data-types/timestamptz-type.js +2 -0
  93. package/data-types/timetz-type.d.ts +2 -0
  94. package/data-types/timetz-type.js +2 -0
  95. package/data-types/tsvector-type.d.ts +4 -0
  96. package/data-types/tsvector-type.js +4 -0
  97. package/data-types/uuid-type.d.ts +2 -0
  98. package/data-types/uuid-type.js +2 -0
  99. package/data-types/varchar-type.d.ts +2 -0
  100. package/data-types/varchar-type.js +2 -0
  101. package/data-types/xid-type.d.ts +6 -0
  102. package/data-types/xid-type.js +8 -1
  103. package/interfaces/command-result.d.ts +7 -0
  104. package/interfaces/copy-from-rows-options.d.ts +4 -0
  105. package/interfaces/data-mapping-options.d.ts +6 -0
  106. package/interfaces/data-type.d.ts +37 -0
  107. package/interfaces/database-connection-params.d.ts +31 -0
  108. package/interfaces/field-info.d.ts +8 -0
  109. package/interfaces/function-call-options.d.ts +1 -0
  110. package/interfaces/function-call-result.d.ts +6 -0
  111. package/interfaces/pipeline-request.d.ts +2 -0
  112. package/interfaces/query-options.d.ts +7 -0
  113. package/interfaces/query-result.d.ts +7 -0
  114. package/interfaces/script-execute-options.d.ts +4 -0
  115. package/interfaces/script-result.d.ts +1 -0
  116. package/interfaces/statement-prepare-options.d.ts +1 -0
  117. package/package.json +5 -5
  118. package/protocol/backend.d.ts +6 -0
  119. package/protocol/backend.js +35 -5
  120. package/protocol/cert-signature.d.ts +8 -0
  121. package/protocol/cert-signature.js +8 -0
  122. package/protocol/database-error.d.ts +8 -0
  123. package/protocol/database-error.js +8 -0
  124. package/protocol/frontend.d.ts +16 -0
  125. package/protocol/frontend.js +13 -6
  126. package/protocol/pg-socket.d.ts +10 -0
  127. package/protocol/pg-socket.js +14 -7
  128. package/protocol/pgoutput.d.ts +15 -0
  129. package/protocol/pgoutput.js +7 -0
  130. package/protocol/protocol.d.ts +50 -2
  131. package/protocol/protocol.js +17 -1
  132. package/protocol/sasl.d.ts +22 -0
  133. package/protocol/sasl.js +21 -0
  134. package/protocol/smart-buffer.d.ts +4 -0
  135. package/safe-event-emitter.d.ts +7 -0
  136. package/safe-event-emitter.js +7 -0
  137. package/types.d.ts +21 -0
  138. package/util/array-calculatedim.d.ts +10 -0
  139. package/util/array-calculatedim.js +10 -0
  140. package/util/assert-integer.d.ts +40 -25
  141. package/util/assert-integer.js +97 -32
  142. package/util/bigint-methods.d.ts +7 -0
  143. package/util/bigint-methods.js +9 -0
  144. package/util/config-from-env.d.ts +10 -0
  145. package/util/config-from-env.js +10 -0
  146. package/util/connection-config.d.ts +17 -0
  147. package/util/connection-config.js +17 -0
  148. package/util/copy-from-rows.d.ts +12 -0
  149. package/util/copy-from-rows.js +9 -0
  150. package/util/decimal-as-string.d.ts +6 -0
  151. package/util/decimal-as-string.js +6 -0
  152. package/util/decode-binaryarray.d.ts +9 -0
  153. package/util/decode-binaryarray.js +9 -0
  154. package/util/escape-literal.d.ts +11 -0
  155. package/util/escape-literal.js +13 -0
  156. package/util/fast-parseint.d.ts +14 -0
  157. package/util/fast-parseint.js +14 -0
  158. package/util/format-datetime.d.ts +26 -0
  159. package/util/format-datetime.js +26 -0
  160. package/util/get-parsers.d.ts +9 -0
  161. package/util/get-parsers.js +9 -0
  162. package/util/hex-text.d.ts +13 -21
  163. package/util/hex-text.js +24 -21
  164. package/util/local-date.d.ts +2 -41
  165. package/util/local-date.js +39 -41
  166. package/util/parse-array.d.ts +8 -0
  167. package/util/parse-array.js +8 -0
  168. package/util/parse-bytea.d.ts +15 -0
  169. package/util/parse-bytea.js +15 -0
  170. package/util/parse-datetime.d.ts +26 -0
  171. package/util/parse-datetime.js +26 -0
  172. package/util/parse-row.d.ts +23 -2
  173. package/util/parse-row.js +56 -36
  174. package/util/parse-time.d.ts +10 -0
  175. package/util/parse-time.js +10 -0
  176. package/util/resolve-column-formats.d.ts +1 -0
  177. package/util/row-decoder.d.ts +32 -0
  178. package/util/row-decoder.js +22 -0
  179. package/util/stringify-arrayliteral.d.ts +33 -0
  180. package/util/stringify-arrayliteral.js +242 -7
  181. package/util/stringify-for-sql.d.ts +17 -0
  182. package/util/stringify-for-sql.js +17 -0
  183. package/util/unspecified-param.d.ts +26 -2
  184. package/util/unspecified-param.js +26 -2
  185. package/util/wrap-row-description.d.ts +10 -0
  186. package/util/wrap-row-description.js +10 -0
  187. package/CHANGELOG.md +0 -1426
@@ -1,8 +1,18 @@
1
1
  import { fastParseInt } from './fast-parseint.js';
2
2
  // noinspection RegExpUnnecessaryNonCapturingGroup
3
+ /** A time as the server writes one: two digits a part, colons in place. */
3
4
  export const STRICT_TIME_PATTERN = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.(\d+))?(?:(Z)|(?:([+-])([01]?[0-9]|2[0-3]):?([0-5][0-9])?))?$/;
4
5
  // noinspection RegExpUnnecessaryNonCapturingGroup
6
+ /** The same, loosened for what a caller might pass as a parameter - the colons and the seconds may be left out. */
5
7
  export const TIME_PATTERN = /^([01][0-9]|2[0-3]):?([0-5][0-9]):?([0-5][0-9])?(?:\.(\d+))?(?:(Z)|(?:([+-])([01]?[0-9]|2[0-3]):?([0-5][0-9])?))?$/;
8
+ /**
9
+ * Reads a time of day into a Date on the epoch day.
10
+ *
11
+ * @param str The value's text.
12
+ * @param parseTimeZone Whether an offset in the text should be applied.
13
+ * @param utc Whether to build the Date in UTC rather than locally.
14
+ * @returns A Date carrying that clock time.
15
+ */
6
16
  export function parseTime(str, parseTimeZone, utc) {
7
17
  const m = str.match(TIME_PATTERN);
8
18
  if (!m)
@@ -2,6 +2,7 @@ import type { DataTypeMap } from '../data-type-map.js';
2
2
  import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
3
3
  import { Protocol } from '../protocol/protocol.js';
4
4
  import type { Maybe } from '../types.js';
5
+ /** What decides the per-column format codes a Bind asks for. */
5
6
  export interface ColumnFormatOptions extends DataMappingOptions {
6
7
  columnFormat?: Protocol.DataFormat | Protocol.DataFormat[];
7
8
  }
@@ -32,16 +32,48 @@ export declare abstract class RowDecoder {
32
32
  */
33
33
  readonly rowType: 'array' | 'object' | 'custom';
34
34
  abstract decode(parsers: AnyParseFunction[], data: Buffer, columnCount: number, options: DataMappingOptions, fields: FieldInfo[]): any;
35
+ /**
36
+ * The same, reading the row where it already is: `buffer` is the
37
+ * socket's own read buffer and the row's values run for `len` bytes
38
+ * from `offset`.
39
+ *
40
+ * Optional, and what a decoder implements when it has no use for a
41
+ * `Buffer` of its own - which is every decoder that reads its values
42
+ * and does not keep them. Implementing it means no view is built for
43
+ * the row at all; a decoder that only implements `decode()` still gets
44
+ * one, exactly as before.
45
+ */
46
+ decodeAt?(parsers: AnyParseFunction[], buffer: Buffer, offset: number, len: number, columnCount: number, options: DataMappingOptions, fields: FieldInfo[]): any;
35
47
  }
48
+ /** Reads each row into an array of values, in column order. */
49
+ /**
50
+ * A row's bytes as a `Buffer` of their own.
51
+ *
52
+ * For the paths that set a row aside and read it later, and for a
53
+ * caller's decoder that only implements `decode()`.
54
+ *
55
+ * @param msg The row as it arrived.
56
+ * @returns A view of just its values.
57
+ */
58
+ export declare function dataRowBytes(msg: {
59
+ buffer: Buffer;
60
+ offset: number;
61
+ len: number;
62
+ }): Buffer;
36
63
  export declare class ArrayRowDecoder extends RowDecoder {
37
64
  readonly rowType: "array";
38
65
  decode(parsers: AnyParseFunction[], data: Buffer, columnCount: number, options: DataMappingOptions): any[];
66
+ decodeAt(parsers: AnyParseFunction[], buffer: Buffer, offset: number, _len: number, columnCount: number, options: DataMappingOptions): any[];
39
67
  }
68
+ /** Reads each row into an object keyed by column name. */
40
69
  export declare class ObjectRowDecoder extends RowDecoder {
41
70
  readonly rowType: "object";
42
71
  decode(parsers: AnyParseFunction[], data: Buffer, columnCount: number, options: DataMappingOptions, fields: FieldInfo[]): object;
72
+ decodeAt(parsers: AnyParseFunction[], buffer: Buffer, offset: number, _len: number, columnCount: number, options: DataMappingOptions, fields: FieldInfo[]): object;
43
73
  }
74
+ /** The array decoder every query shares - it holds no state. */
44
75
  export declare const DEFAULT_ARRAY_ROW_DECODER: ArrayRowDecoder;
76
+ /** The object decoder every query shares - it holds no state. */
45
77
  export declare const DEFAULT_OBJECT_ROW_DECODER: ObjectRowDecoder;
46
78
  interface RowDecoderOptions {
47
79
  rowDecoder?: 'array' | 'object' | RowDecoder;
@@ -30,19 +30,41 @@ export class RowDecoder {
30
30
  */
31
31
  rowType = 'custom';
32
32
  }
33
+ /** Reads each row into an array of values, in column order. */
34
+ /**
35
+ * A row's bytes as a `Buffer` of their own.
36
+ *
37
+ * For the paths that set a row aside and read it later, and for a
38
+ * caller's decoder that only implements `decode()`.
39
+ *
40
+ * @param msg The row as it arrived.
41
+ * @returns A view of just its values.
42
+ */
43
+ export function dataRowBytes(msg) {
44
+ return msg.buffer.subarray(msg.offset, msg.offset + msg.len);
45
+ }
33
46
  export class ArrayRowDecoder extends RowDecoder {
34
47
  rowType = 'array';
35
48
  decode(parsers, data, columnCount, options) {
36
49
  return parseRow(parsers, data, columnCount, options);
37
50
  }
51
+ decodeAt(parsers, buffer, offset, _len, columnCount, options) {
52
+ return parseRow(parsers, buffer, columnCount, options, offset);
53
+ }
38
54
  }
55
+ /** Reads each row into an object keyed by column name. */
39
56
  export class ObjectRowDecoder extends RowDecoder {
40
57
  rowType = 'object';
41
58
  decode(parsers, data, columnCount, options, fields) {
42
59
  return parseObjectRow(parsers, data, columnCount, options, fields);
43
60
  }
61
+ decodeAt(parsers, buffer, offset, _len, columnCount, options, fields) {
62
+ return parseObjectRow(parsers, buffer, columnCount, options, fields, offset);
63
+ }
44
64
  }
65
+ /** The array decoder every query shares - it holds no state. */
45
66
  export const DEFAULT_ARRAY_ROW_DECODER = new ArrayRowDecoder();
67
+ /** The object decoder every query shares - it holds no state. */
46
68
  export const DEFAULT_OBJECT_ROW_DECODER = new ObjectRowDecoder();
47
69
  /**
48
70
  * Collapses the `rowDecoder`/`objectRows` query options into one RowDecoder
@@ -1,3 +1,36 @@
1
1
  import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
2
2
  import type { EncodeTextFunction } from '../types.js';
3
+ /**
4
+ * Renders an array as the literal the server parses - `{1,2,{3,4}}`.
5
+ *
6
+ * @param value The array, nested to any depth.
7
+ * @param options Passed to the element encoder.
8
+ * @param encode The element type's own text encoder, when the type is
9
+ * known; without one each element is taken as it stringifies.
10
+ * @returns The literal.
11
+ */
3
12
  export declare function stringifyArrayLiteral(value: any[], options?: DataMappingOptions, encode?: EncodeTextFunction): string;
13
+ /**
14
+ * Writes an array as a length-prefixed literal, straight into `io`,
15
+ * without building the literal as a string first.
16
+ *
17
+ * The text is the one `stringifyArrayLiteral()` returns, so what reaches
18
+ * the server is the same either way. Only an array of numbers with no
19
+ * element encoder takes this path - the untyped-parameter case, and the
20
+ * only one where a numeric array becomes a literal at all, since a
21
+ * declared one keeps the binary encoding. Anything else, and anything
22
+ * under `WRITE_BYTES_FROM` elements, is handed to the string path.
23
+ *
24
+ * @param io The buffer to write into - the connection's own.
25
+ * @param value The array to write.
26
+ * @param options Passed to the element encoder, when there is one.
27
+ * @param encode The element type's own text encoder, if the type was
28
+ * declared.
29
+ */
30
+ export declare function writeArrayLiteral(io: {
31
+ buffer: Buffer;
32
+ size: number;
33
+ writeInt32BE(v: number): number;
34
+ writeBytes(b: Buffer): number;
35
+ writeLString(s?: string, encoding?: BufferEncoding): number;
36
+ }, value: any[], options?: DataMappingOptions, encode?: EncodeTextFunction): void;
@@ -1,4 +1,27 @@
1
1
  import { arrayCalculateDim } from './array-calculatedim.js';
2
+ import { arrayLeaf } from './array-leaf.js';
3
+ /**
4
+ * What a number's own text looks like: digits, one optional dot, an
5
+ * optional exponent, an optional leading minus. An element that matches
6
+ * goes out bare - `{1,2}` rather than `{"1","2"}`, which is what the
7
+ * server itself prints and what every other client writes.
8
+ */
9
+ /* Deliberately narrower than "has no special characters": it is applied
10
+ only to what a JS number or bigint encoded to, and matching it is the
11
+ evidence that the encoder really did produce a number's text. NaN and
12
+ Infinity fail it and stay quoted - both forms parse the same, so that
13
+ costs nothing - and so does anything an unexpected encoder made of a
14
+ number, a date type handed a timestamp say. */
15
+ const BARE_NUMBER = /^-?\d+(\.\d+)?([eE][-+]?\d+)?$/;
16
+ /**
17
+ * Renders an array as the literal the server parses - `{1,2,{3,4}}`.
18
+ *
19
+ * @param value The array, nested to any depth.
20
+ * @param options Passed to the element encoder.
21
+ * @param encode The element type's own text encoder, when the type is
22
+ * known; without one each element is taken as it stringifies.
23
+ * @returns The literal.
24
+ */
2
25
  export function stringifyArrayLiteral(value, options, encode) {
3
26
  const dim = arrayCalculateDim(value);
4
27
  // `dim` is built once above and never mutated, so its last index is
@@ -9,19 +32,24 @@ export function stringifyArrayLiteral(value, options, encode) {
9
32
  const lastLevel = dim.length - 1;
10
33
  const writeDim = (arr, level) => {
11
34
  const elemCount = dim[level];
12
- const out = [];
13
35
  const isLeafLevel = level >= lastLevel;
36
+ // Concatenated rather than collected and joined: V8 builds a rope
37
+ // and flattens it once, where an array of one string per element is
38
+ // a second allocation per element before the join sees any of them.
39
+ let out = '{';
14
40
  for (let i = 0; i < elemCount; i++) {
41
+ if (i)
42
+ out += ',';
15
43
  let x = arr && arr[i];
16
44
  if (!isLeafLevel) {
17
45
  if (x != null && !Array.isArray(x))
18
46
  x = [x];
19
- out.push(writeDim(x, level + 1));
47
+ out += writeDim(x, level + 1);
20
48
  continue;
21
49
  }
22
50
  // if value is null
23
51
  if (x == null) {
24
- out.push('NULL');
52
+ out += 'NULL';
25
53
  continue;
26
54
  }
27
55
  /* c8 ignore start - dim is (re)computed above from a DFS that visits
@@ -29,18 +57,225 @@ export function stringifyArrayLiteral(value, options, encode) {
29
57
  can never actually be an array once `level` reaches the leaf -
30
58
  if it were, dim would already have gone one level deeper there. */
31
59
  if (Array.isArray(x)) {
32
- out.push(stringifyArrayLiteral(x, options, encode));
60
+ out += stringifyArrayLiteral(x, options, encode);
33
61
  continue;
34
62
  }
35
63
  /* c8 ignore stop */
64
+ // Asked before the encoder runs, so the test below is only ever
65
+ // applied to a value that really was a number.
66
+ const isNumber = typeof x === 'number' || typeof x === 'bigint';
36
67
  if (encode)
37
68
  x = encode(x, options || {});
38
- out.push(escapeArrayItem('' + x));
69
+ const s = '' + x;
70
+ out += isNumber && BARE_NUMBER.test(s) ? s : escapeArrayItem(s);
39
71
  }
40
- return '{' + out.join(',') + '}';
72
+ return out + '}';
41
73
  };
42
74
  return writeDim(value, 0);
43
75
  }
76
+ /** Quotes one element, escaping the two characters that need it. */
77
+ /* Backslash and double quote are the only two the literal grammar gives
78
+ meaning to inside a quoted element, and neither is in most values - so
79
+ they are looked for before anything is rewritten. The two replace()
80
+ calls each walk the string and build another one even when they change
81
+ nothing, which for a column of ordinary text was half the cost of
82
+ writing the literal. */
44
83
  function escapeArrayItem(str) {
45
- return '"' + str.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
84
+ return str.indexOf('\\') < 0 && str.indexOf('"') < 0
85
+ ? '"' + str + '"'
86
+ : '"' + str.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
87
+ }
88
+ /** How many bytes writeArrayLiteral() writes into `io` at a time. */
89
+ /* Bounded rather than grown to fit: a 100 000-element array is 1.1MB of
90
+ literal, and a scratch sized to the largest one ever written would
91
+ hold that for the life of the process. Flushing every 64KB keeps this
92
+ constant, and what it flushes into is the connection's own buffer,
93
+ which already knows how to grow and to hand its pages back when
94
+ idle. */
95
+ const CHUNK = 64 * 1024;
96
+ /** The scratch itself, reused by every call. */
97
+ const chunk = Buffer.allocUnsafe(CHUNK);
98
+ /**
99
+ * Whether the digit lane applies to this value - see writeArrayLiteral().
100
+ * Asked of the array's first leaf to choose a path, and of every element
101
+ * on the way past.
102
+ */
103
+ function isInt32(v) {
104
+ return (typeof v === 'number' &&
105
+ v >= -2147483648 &&
106
+ v <= 2147483647 &&
107
+ Number.isInteger(v));
108
+ }
109
+ /** Digits, written backwards, before they are copied out forwards. */
110
+ const DIGITS = new Uint8Array(24);
111
+ /** Below this many elements the literal is built as a string instead. */
112
+ /* Writing bytes wins from about a hundred elements up and loses below
113
+ it: Buffer.write() has a per-call cost that `+=` does not, and for a
114
+ handful of short elements that cost is the whole of the work. The
115
+ number is where the two met when measured, so it is a floor against a
116
+ small regression rather than a tuned parameter. */
117
+ const WRITE_BYTES_FROM = 16;
118
+ /**
119
+ * Writes an array as a length-prefixed literal, straight into `io`,
120
+ * without building the literal as a string first.
121
+ *
122
+ * The text is the one `stringifyArrayLiteral()` returns, so what reaches
123
+ * the server is the same either way. Only an array of numbers with no
124
+ * element encoder takes this path - the untyped-parameter case, and the
125
+ * only one where a numeric array becomes a literal at all, since a
126
+ * declared one keeps the binary encoding. Anything else, and anything
127
+ * under `WRITE_BYTES_FROM` elements, is handed to the string path.
128
+ *
129
+ * @param io The buffer to write into - the connection's own.
130
+ * @param value The array to write.
131
+ * @param options Passed to the element encoder, when there is one.
132
+ * @param encode The element type's own text encoder, if the type was
133
+ * declared.
134
+ */
135
+ /* The string was never wanted: writeLString() encoded it to UTF-8 and
136
+ threw it away. For a 100 000-element int4[] it is 1.1MB, built out of
137
+ ~200 000 rope nodes, flattened once and encoded once - counted with
138
+ --trace-gc over 100 calls, that step collects 10.1 MB a call.
139
+
140
+ The saving is not in avoiding the string, though. Writing bytes
141
+ through Buffer.write() per element is *slower* than letting V8 build a
142
+ rope and flatten it once - 1.1x to 1.35x slower across every size and
143
+ type, which is why this is not simply the byte-writing version of
144
+ stringifyArrayLiteral(). What pays is never producing the element's
145
+ text at all: an integer's digits go into the buffer from the number
146
+ itself, with no string and no per-element call. Against the string
147
+ path on an int4[], medians of alternating runs:
148
+
149
+ elements 64 1 000 100 000
150
+ string 2.38 us 22.2 us 3180 us
151
+ digits 0.88 us 4.8 us 917 us
152
+ 2.7x 4.6x 3.5x
153
+
154
+ What a caller waits for moves less, and the difference is worth
155
+ knowing before quoting one: inserting the same array over a loopback
156
+ connection is 14.21ms against 12.51ms, 1.14x, because the server's own
157
+ parse of a 1.1MB literal is most of it. The allocation is gone either
158
+ way - 10.10 MB a call to 0.00.
159
+
160
+ So the fast lane is integers, and everything else on this path - a
161
+ float, a bigint too large to be exact, anything a reader of the array
162
+ did not expect - falls back to its own text and is written as such. */
163
+ export function writeArrayLiteral(io, value, options, encode) {
164
+ if (encode ||
165
+ !Array.isArray(value) ||
166
+ value.length < WRITE_BYTES_FROM ||
167
+ !isInt32(arrayLeaf(value))) {
168
+ io.writeLString(stringifyArrayLiteral(value, options, encode), 'utf8');
169
+ return;
170
+ }
171
+ const dim = arrayCalculateDim(value);
172
+ const lastLevel = dim.length - 1;
173
+ // The length is only known once everything is written, so the field is
174
+ // reserved now and filled in at the end - the same shape the binary
175
+ // encoders use for their own payloads.
176
+ io.writeInt32BE(0);
177
+ const lengthAt = io.size - 4;
178
+ const start = io.size;
179
+ let p = 0;
180
+ const writeDim = (arr, level) => {
181
+ const elemCount = dim[level];
182
+ const isLeafLevel = level >= lastLevel;
183
+ // No room check here: this is entered either at the top level, with
184
+ // an empty scratch, or from inside the loop below, which has just
185
+ // made room for the separator, the longest number, and both braces.
186
+ chunk[p++] = 123; /* { */
187
+ let x;
188
+ let s;
189
+ let v;
190
+ let q;
191
+ let i;
192
+ for (i = 0; i < elemCount; i++) {
193
+ // Every element writes at most a separator, a sign and 24 digits,
194
+ // plus the two braces a nested dimension would open and close
195
+ // around them; the text branch reserves its own room below. That
196
+ // headroom is what lets the braces be written unchecked.
197
+ if (p > CHUNK - 32) {
198
+ io.writeBytes(chunk.subarray(0, p));
199
+ p = 0;
200
+ }
201
+ if (i)
202
+ chunk[p++] = 44; /* , */
203
+ x = arr && arr[i];
204
+ if (!isLeafLevel) {
205
+ if (x != null && !Array.isArray(x))
206
+ x = [x];
207
+ writeDim(x, level + 1);
208
+ continue;
209
+ }
210
+ if (x == null) {
211
+ chunk[p++] = 78; /* N */
212
+ chunk[p++] = 85; /* U */
213
+ chunk[p++] = 76; /* L */
214
+ chunk[p++] = 76; /* L */
215
+ continue;
216
+ }
217
+ // int32 rather than any safe integer: the digits come out of a
218
+ // repeated `(v / 10) | 0`, which is exact and fast only within
219
+ // that range. Above it, dividing a double sixteen times per
220
+ // element is slower than letting V8 print the number - measured at
221
+ // 11x slower on values near 9e15 - so those go the same way as a
222
+ // float does, through their own text.
223
+ if (x >= -2147483648 && x <= 2147483647 && Number.isInteger(x)) {
224
+ v = x;
225
+ if (v < 0) {
226
+ chunk[p++] = 45; /* - */
227
+ v = -v;
228
+ }
229
+ q = 24;
230
+ do {
231
+ DIGITS[--q] = 48 + (v % 10);
232
+ v = (v / 10) | 0;
233
+ } while (v);
234
+ while (q < 24)
235
+ chunk[p++] = DIGITS[q++];
236
+ continue;
237
+ }
238
+ // Anything else: its own text, quoted exactly as the string path
239
+ // would quote it.
240
+ // Asked before the text is produced, and of the value rather than
241
+ // of its spelling - the same rule the string path states: a
242
+ // string's own text is quoted whatever it looks like.
243
+ const isNumber = typeof x === 'number' || typeof x === 'bigint';
244
+ s = '' + x;
245
+ const bare = isNumber && BARE_NUMBER.test(s);
246
+ if (!bare)
247
+ s =
248
+ s.indexOf('\\') < 0 && s.indexOf('"') < 0
249
+ ? s
250
+ : s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
251
+ // A UTF-16 code unit is at most three UTF-8 bytes, which is the
252
+ // room to have without measuring the string first. The two quotes
253
+ // are in it, and one byte more for the `}` that may follow this
254
+ // element without a check of its own.
255
+ const bound = s.length * 3 + 3;
256
+ if (p + bound > CHUNK) {
257
+ io.writeBytes(chunk.subarray(0, p));
258
+ p = 0;
259
+ }
260
+ if (bound > CHUNK) {
261
+ // One element larger than the whole scratch - a long text value.
262
+ // It goes out on its own rather than growing anything, and
263
+ // `Buffer.write` would otherwise have truncated it in silence.
264
+ io.writeBytes(Buffer.from(bare ? s : '"' + s + '"', 'utf8'));
265
+ continue;
266
+ }
267
+ if (bare)
268
+ p += chunk.write(s, p, 'utf8');
269
+ else {
270
+ chunk[p++] = 34; /* " */
271
+ p += chunk.write(s, p, 'utf8');
272
+ chunk[p++] = 34;
273
+ }
274
+ }
275
+ chunk[p++] = 125; /* } */
276
+ };
277
+ writeDim(value, 0);
278
+ if (p)
279
+ io.writeBytes(chunk.subarray(0, p));
280
+ io.buffer.writeInt32BE(io.size - start, lengthAt);
46
281
  }
@@ -1,4 +1,21 @@
1
1
  import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
2
2
  import type { EncodeTextFunction } from '../types.js';
3
+ /**
4
+ * Renders an array as the `ARRAY[...]` an SQL statement can carry.
5
+ *
6
+ * For the `sql` tag, which puts values into the text rather than sending
7
+ * them as parameters.
8
+ *
9
+ * @param arr The array, nested to any depth.
10
+ * @param options Passed to the element encoder.
11
+ * @returns The literal, cast to the element type it was built for.
12
+ */
3
13
  export declare function stringifyArrayForSQL(v: any[], options?: DataMappingOptions, encode?: EncodeTextFunction): string;
14
+ /**
15
+ * Renders one value as the literal an SQL statement can carry.
16
+ *
17
+ * @param v The value.
18
+ * @param options Passed to the type's own text encoder.
19
+ * @returns The literal, quoted and escaped as the value needs.
20
+ */
4
21
  export declare function stringifyValueForSQL(v: any, options?: DataMappingOptions, encode?: EncodeTextFunction): string;
@@ -3,6 +3,16 @@ import { GlobalTypeMap } from '../data-type-map.js';
3
3
  import { UuidType } from '../data-types/uuid-type.js';
4
4
  import { escapeLiteral } from './escape-literal.js';
5
5
  import { formatDateParam } from './format-datetime.js';
6
+ /**
7
+ * Renders an array as the `ARRAY[...]` an SQL statement can carry.
8
+ *
9
+ * For the `sql` tag, which puts values into the text rather than sending
10
+ * them as parameters.
11
+ *
12
+ * @param arr The array, nested to any depth.
13
+ * @param options Passed to the element encoder.
14
+ * @returns The literal, cast to the element type it was built for.
15
+ */
6
16
  export function stringifyArrayForSQL(v, options, encode) {
7
17
  const arr = v.map(x => stringifyValueForSQL(x, options, encode));
8
18
  return 'ARRAY[' + arr.join(',') + ']';
@@ -38,6 +48,13 @@ function stringifyObjectForSQL(v, options) {
38
48
  dataType.name);
39
49
  return escapeLiteral(JSON.stringify(v)) + '::json';
40
50
  }
51
+ /**
52
+ * Renders one value as the literal an SQL statement can carry.
53
+ *
54
+ * @param v The value.
55
+ * @param options Passed to the type's own text encoder.
56
+ * @returns The literal, quoted and escaped as the value needs.
57
+ */
41
58
  export function stringifyValueForSQL(v, options, encode) {
42
59
  if (v == null)
43
60
  return 'null';
@@ -36,7 +36,31 @@
36
36
  * or of one of this client's own classes has one type it can be, so
37
37
  * naming it says nothing the server would have decided differently -
38
38
  * and it keeps the binary encoding, which the text literal gives up.
39
- * A caller who wants that back for a large numeric array names the type
40
- * with `new BindParam(DataTypeOIDs._float8, v)`.
39
+ *
40
+ * What that giving up costs, which the commit making this change put at
41
+ * nothing measurable and is not: inserting the same value over a
42
+ * loopback connection, alternating within each round, medians of nine -
43
+ *
44
+ * ```
45
+ * as text as BindParam
46
+ * float8[] 1 000 2.33ms 0.91ms 2.6x
47
+ * float8[] 10 000 17.34ms 3.75ms 4.6x
48
+ * float8[] 100 000 143.0ms 26.9ms 5.3x
49
+ * int4[] 100 000 24.5ms 15.5ms 1.6x
50
+ * ```
51
+ *
52
+ * It grows with the array and with how wide an element's text is
53
+ * against its binary: a float8 is 8 bytes against about 19 characters,
54
+ * an int4 is 4 against up to 11 - and most of what a float8[] costs is
55
+ * the server's own parse of that text, not the writing of it. The
56
+ * original measurement was of the smallest of these shapes and read it
57
+ * as level; it is not, at any size.
58
+ *
59
+ * So this is a real price, paid for a correctness the client cannot buy
60
+ * any other way before the server has spoken. A caller who knows the
61
+ * column - and for a large numeric array that is worth knowing - names
62
+ * the type with `new BindParam(DataTypeOIDs._float8, v)` and gets the
63
+ * binary encoding back with nothing given up, since a named type is an
64
+ * answer rather than a guess.
41
65
  */
42
66
  export declare function isUnspecifiedParam(v: any): boolean;
@@ -37,8 +37,32 @@ import { arrayLeaf } from './array-leaf.js';
37
37
  * or of one of this client's own classes has one type it can be, so
38
38
  * naming it says nothing the server would have decided differently -
39
39
  * and it keeps the binary encoding, which the text literal gives up.
40
- * A caller who wants that back for a large numeric array names the type
41
- * with `new BindParam(DataTypeOIDs._float8, v)`.
40
+ *
41
+ * What that giving up costs, which the commit making this change put at
42
+ * nothing measurable and is not: inserting the same value over a
43
+ * loopback connection, alternating within each round, medians of nine -
44
+ *
45
+ * ```
46
+ * as text as BindParam
47
+ * float8[] 1 000 2.33ms 0.91ms 2.6x
48
+ * float8[] 10 000 17.34ms 3.75ms 4.6x
49
+ * float8[] 100 000 143.0ms 26.9ms 5.3x
50
+ * int4[] 100 000 24.5ms 15.5ms 1.6x
51
+ * ```
52
+ *
53
+ * It grows with the array and with how wide an element's text is
54
+ * against its binary: a float8 is 8 bytes against about 19 characters,
55
+ * an int4 is 4 against up to 11 - and most of what a float8[] costs is
56
+ * the server's own parse of that text, not the writing of it. The
57
+ * original measurement was of the smallest of these shapes and read it
58
+ * as level; it is not, at any size.
59
+ *
60
+ * So this is a real price, paid for a correctness the client cannot buy
61
+ * any other way before the server has spoken. A caller who knows the
62
+ * column - and for a large numeric array that is worth knowing - names
63
+ * the type with `new BindParam(DataTypeOIDs._float8, v)` and gets the
64
+ * binary encoding back with nothing given up, since a named type is an
65
+ * answer rather than a guess.
42
66
  */
43
67
  export function isUnspecifiedParam(v) {
44
68
  const x = arrayLeaf(v);
@@ -3,4 +3,14 @@ import type { DataTypeMap } from '../data-type-map.js';
3
3
  import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
4
4
  import type { FieldInfo } from '../interfaces/field-info.js';
5
5
  import type { Protocol } from '../protocol/protocol.js';
6
+ /**
7
+ * Turns the server's RowDescription into the `FieldInfo[]` a caller
8
+ * sees, naming each column's type and what it maps to.
9
+ *
10
+ * @param typeMap Where the types are registered.
11
+ * @param fields The columns, as the server described them.
12
+ * @param columnFormat What format they are coming back in.
13
+ * @param options The mapping options in force.
14
+ * @returns One FieldInfo per column, in order.
15
+ */
6
16
  export declare function wrapRowDescription(typeMap: DataTypeMap, fields: Protocol.RowDescription[], columnFormat: DataFormat | DataFormat[], mappingOptions?: DataMappingOptions): FieldInfo[];
@@ -9,6 +9,16 @@ const DataTypeOIDByName = {};
9
9
  for (const oid of Object.keys(DataTypeNames)) {
10
10
  DataTypeOIDByName[DataTypeNames[oid]] = Number(oid);
11
11
  }
12
+ /**
13
+ * Turns the server's RowDescription into the `FieldInfo[]` a caller
14
+ * sees, naming each column's type and what it maps to.
15
+ *
16
+ * @param typeMap Where the types are registered.
17
+ * @param fields The columns, as the server described them.
18
+ * @param columnFormat What format they are coming back in.
19
+ * @param options The mapping options in force.
20
+ * @returns One FieldInfo per column, in order.
21
+ */
12
22
  export function wrapRowDescription(typeMap, fields, columnFormat, mappingOptions) {
13
23
  const asString = mappingOptions?.fetchAsString;
14
24
  return fields.map((f, idx) => {