neonctl 2.37.1 → 2.38.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 (159) hide show
  1. package/README.md +15 -700
  2. package/bin/cli.js +3 -0
  3. package/package.json +8 -75
  4. package/dist/analytics.js +0 -168
  5. package/dist/api.js +0 -720
  6. package/dist/auth.js +0 -125
  7. package/dist/callback.html +0 -60
  8. package/dist/cli.js +0 -9
  9. package/dist/commands/api.js +0 -278
  10. package/dist/commands/auth.js +0 -214
  11. package/dist/commands/bootstrap.js +0 -481
  12. package/dist/commands/branches.js +0 -488
  13. package/dist/commands/bucket.js +0 -549
  14. package/dist/commands/checkout.js +0 -321
  15. package/dist/commands/config.js +0 -626
  16. package/dist/commands/connection_string.js +0 -172
  17. package/dist/commands/data_api.js +0 -284
  18. package/dist/commands/databases.js +0 -82
  19. package/dist/commands/deploy.js +0 -26
  20. package/dist/commands/dev.js +0 -698
  21. package/dist/commands/diff.js +0 -222
  22. package/dist/commands/env.js +0 -189
  23. package/dist/commands/functions.js +0 -373
  24. package/dist/commands/index.js +0 -62
  25. package/dist/commands/init.js +0 -73
  26. package/dist/commands/inspect.js +0 -65
  27. package/dist/commands/ip_allow.js +0 -137
  28. package/dist/commands/link.js +0 -1121
  29. package/dist/commands/neon_auth.js +0 -1028
  30. package/dist/commands/operations.js +0 -28
  31. package/dist/commands/orgs.js +0 -24
  32. package/dist/commands/projects.js +0 -413
  33. package/dist/commands/psql.js +0 -62
  34. package/dist/commands/roles.js +0 -65
  35. package/dist/commands/schema_diff.js +0 -151
  36. package/dist/commands/set_context.js +0 -29
  37. package/dist/commands/snapshots.js +0 -455
  38. package/dist/commands/status.js +0 -40
  39. package/dist/commands/user.js +0 -15
  40. package/dist/commands/vpc_endpoints.js +0 -134
  41. package/dist/config.js +0 -11
  42. package/dist/config_format.js +0 -72
  43. package/dist/context.js +0 -231
  44. package/dist/current_branch_fast_path.js +0 -55
  45. package/dist/dev/env.js +0 -244
  46. package/dist/dev/functions.js +0 -70
  47. package/dist/dev/inputs.js +0 -63
  48. package/dist/dev/runtime.js +0 -146
  49. package/dist/env.js +0 -36
  50. package/dist/env_file.js +0 -159
  51. package/dist/errors.js +0 -80
  52. package/dist/functions_api.js +0 -48
  53. package/dist/help.js +0 -146
  54. package/dist/index.js +0 -240
  55. package/dist/log.js +0 -18
  56. package/dist/parameters.gen.js +0 -480
  57. package/dist/pkg.js +0 -25
  58. package/dist/psql/cli.js +0 -53
  59. package/dist/psql/command/cmd_cond.js +0 -437
  60. package/dist/psql/command/cmd_connect.js +0 -820
  61. package/dist/psql/command/cmd_copy.js +0 -1035
  62. package/dist/psql/command/cmd_describe.js +0 -1815
  63. package/dist/psql/command/cmd_format.js +0 -948
  64. package/dist/psql/command/cmd_io.js +0 -2193
  65. package/dist/psql/command/cmd_lo.js +0 -393
  66. package/dist/psql/command/cmd_meta.js +0 -969
  67. package/dist/psql/command/cmd_misc.js +0 -187
  68. package/dist/psql/command/cmd_pipeline.js +0 -1148
  69. package/dist/psql/command/cmd_restrict.js +0 -171
  70. package/dist/psql/command/cmd_show.js +0 -766
  71. package/dist/psql/command/dispatch.js +0 -343
  72. package/dist/psql/command/inputQueue.js +0 -42
  73. package/dist/psql/command/shared.js +0 -71
  74. package/dist/psql/complete/filenames.js +0 -139
  75. package/dist/psql/complete/index.js +0 -104
  76. package/dist/psql/complete/matcher.js +0 -315
  77. package/dist/psql/complete/psqlVars.js +0 -249
  78. package/dist/psql/complete/queries.js +0 -493
  79. package/dist/psql/complete/rules.js +0 -2424
  80. package/dist/psql/core/common.js +0 -1253
  81. package/dist/psql/core/help.js +0 -576
  82. package/dist/psql/core/mainloop.js +0 -1360
  83. package/dist/psql/core/prompt.js +0 -439
  84. package/dist/psql/core/settings.js +0 -686
  85. package/dist/psql/core/sqlHelp.js +0 -1066
  86. package/dist/psql/core/startup.js +0 -846
  87. package/dist/psql/core/syncVars.js +0 -116
  88. package/dist/psql/core/variables.js +0 -287
  89. package/dist/psql/describe/formatters.js +0 -1330
  90. package/dist/psql/describe/processNamePattern.js +0 -270
  91. package/dist/psql/describe/queries.js +0 -2452
  92. package/dist/psql/describe/versionGate.js +0 -44
  93. package/dist/psql/index.js +0 -2030
  94. package/dist/psql/io/history.js +0 -299
  95. package/dist/psql/io/input.js +0 -120
  96. package/dist/psql/io/lineEditor/buffer.js +0 -325
  97. package/dist/psql/io/lineEditor/complete.js +0 -227
  98. package/dist/psql/io/lineEditor/filename.js +0 -159
  99. package/dist/psql/io/lineEditor/index.js +0 -893
  100. package/dist/psql/io/lineEditor/keymap.js +0 -745
  101. package/dist/psql/io/lineEditor/vt100.js +0 -363
  102. package/dist/psql/io/pgpass.js +0 -202
  103. package/dist/psql/io/pgservice.js +0 -194
  104. package/dist/psql/io/psqlrc.js +0 -422
  105. package/dist/psql/print/aligned.js +0 -1748
  106. package/dist/psql/print/asciidoc.js +0 -230
  107. package/dist/psql/print/crosstab.js +0 -463
  108. package/dist/psql/print/csv.js +0 -76
  109. package/dist/psql/print/html.js +0 -240
  110. package/dist/psql/print/json.js +0 -96
  111. package/dist/psql/print/latex.js +0 -379
  112. package/dist/psql/print/pager.js +0 -267
  113. package/dist/psql/print/troff.js +0 -240
  114. package/dist/psql/print/unaligned.js +0 -99
  115. package/dist/psql/print/units.js +0 -188
  116. package/dist/psql/scanner/slash.js +0 -515
  117. package/dist/psql/scanner/sql.js +0 -914
  118. package/dist/psql/scanner/stringutils.js +0 -394
  119. package/dist/psql/types/backslash.js +0 -1
  120. package/dist/psql/types/connection.js +0 -1
  121. package/dist/psql/types/index.js +0 -7
  122. package/dist/psql/types/printer.js +0 -1
  123. package/dist/psql/types/repl.js +0 -1
  124. package/dist/psql/types/scanner.js +0 -24
  125. package/dist/psql/types/settings.js +0 -1
  126. package/dist/psql/types/variables.js +0 -1
  127. package/dist/psql/wire/connection.js +0 -2857
  128. package/dist/psql/wire/copy.js +0 -108
  129. package/dist/psql/wire/notify.js +0 -59
  130. package/dist/psql/wire/pipeline.js +0 -521
  131. package/dist/psql/wire/protocol.js +0 -466
  132. package/dist/psql/wire/sasl.js +0 -294
  133. package/dist/psql/wire/tls.js +0 -602
  134. package/dist/storage_api.js +0 -147
  135. package/dist/test_utils/fixtures.js +0 -121
  136. package/dist/test_utils/oauth_server.js +0 -9
  137. package/dist/types.js +0 -1
  138. package/dist/utils/ai_gateway_notice.js +0 -180
  139. package/dist/utils/api_enums.js +0 -33
  140. package/dist/utils/auth.js +0 -5
  141. package/dist/utils/branch_notice.js +0 -22
  142. package/dist/utils/branch_picker.js +0 -103
  143. package/dist/utils/compute_units.js +0 -28
  144. package/dist/utils/config_diff.js +0 -185
  145. package/dist/utils/enrichers.js +0 -161
  146. package/dist/utils/esbuild.js +0 -158
  147. package/dist/utils/formats.js +0 -18
  148. package/dist/utils/git_diff.js +0 -90
  149. package/dist/utils/inspect_db.js +0 -153
  150. package/dist/utils/inspect_queries.js +0 -372
  151. package/dist/utils/middlewares.js +0 -20
  152. package/dist/utils/openapi.js +0 -114
  153. package/dist/utils/package_manager.js +0 -68
  154. package/dist/utils/point_in_time.js +0 -56
  155. package/dist/utils/psql.js +0 -120
  156. package/dist/utils/string.js +0 -5
  157. package/dist/utils/ui.js +0 -59
  158. package/dist/utils/zip.js +0 -4
  159. package/dist/writer.js +0 -97
@@ -1,240 +0,0 @@
1
- import { boolDisplayOf, renderCellValue } from "./units.js";
2
- /**
3
- * Troff MS printer.
4
- *
5
- * Mirrors print.c `print_troff_ms_text` and `print_troff_ms_vertical`.
6
- *
7
- * Output shape (flat, with title and one footer):
8
- * .LP
9
- * .DS C
10
- * title
11
- * .DE
12
- * .LP
13
- * .TS
14
- * center;
15
- * l | l | r.
16
- * \fIcol1\fP \fIcol2\fP \fIcol3\fP
17
- * _
18
- * val1 val2 val3
19
- * .TE
20
- * .DS L
21
- * (N rows)
22
- * .DE
23
- *
24
- * Output shape (expanded, `\pset expanded on`):
25
- * .LP
26
- * .TS
27
- * center;
28
- * c s.
29
- * \fIRecord 1\fP
30
- * _ # if border>=1
31
- * .T&
32
- * c | l. # `c l.` when border != 1
33
- * colname1 val1
34
- * colname2 val2
35
- * ...
36
- * .T&
37
- * c s.
38
- * \fIRecord 2\fP
39
- * ...
40
- * .TE
41
- * .DS L
42
- * .DE
43
- *
44
- * - `topt.border` is clamped to 0..2. `border == 2` uses `center box;`;
45
- * otherwise just `center;`. Border > 0 inserts ` | ` between column
46
- * spec letters.
47
- * - Numeric columns get `r`, others `l` (per the OID heuristic).
48
- * - Tab is the field separator (consistent with `.TS` defaults).
49
- * - The only structurally-hostile character is `\\`, which becomes
50
- * `\(rs` (troff's "reverse solidus" glyph). Everything else passes
51
- * through verbatim — troff ms is a byte stream.
52
- * - Expanded mode emits a `.T&\n<spec>.\n` re-spec block between the
53
- * "Record N" header (`c s.`) and the body (`c l.` / `c | l.`); under
54
- * tuples-only the body spec is set once as `c l;` after the table
55
- * header. No default `(N rows)` footer.
56
- */
57
- // INT2, INT4, INT8, FLOAT4, FLOAT8, NUMERIC, INTERVAL.
58
- const NUMERIC_OIDS = new Set([21, 23, 20, 700, 701, 1700, 1186]);
59
- export const troffMsPrinter = {
60
- format: "troff-ms",
61
- printQuery(rs, opts, out) {
62
- const topt = opts.topt;
63
- if (topt.expanded === "on") {
64
- return printExpanded(rs, opts, out);
65
- }
66
- return printFlat(rs, opts, out);
67
- },
68
- };
69
- const printFlat = (rs, opts, out) => {
70
- const topt = opts.topt;
71
- const tuplesOnly = topt.tuplesOnly;
72
- const startTable = topt.startTable;
73
- const stopTable = topt.stopTable;
74
- const border = clampBorder(topt.border);
75
- const nullPrint = opts.nullPrint !== "" ? opts.nullPrint : topt.nullPrint;
76
- const title = opts.title ?? topt.title;
77
- const footers = opts.footers ?? topt.footers;
78
- const headers = rs.fields.map((f) => f.name);
79
- const ncols = rs.fields.length;
80
- const aligns = rs.fields.map((f) => NUMERIC_OIDS.has(f.dataTypeID) ? "r" : "l");
81
- const boolDisplay = boolDisplayOf(topt);
82
- const cells = rs.rows.map((row) => row.map((cell, i) => renderCell(cell, nullPrint, topt.numericLocale, rs.fields[i]?.dataTypeID, boolDisplay)));
83
- let buf = "";
84
- if (startTable) {
85
- if (!tuplesOnly && title) {
86
- buf += ".LP\n.DS C\n";
87
- buf += escapeTroff(title);
88
- buf += "\n.DE\n";
89
- }
90
- buf += ".LP\n.TS\n";
91
- buf += border === 2 ? "center box;\n" : "center;\n";
92
- aligns.forEach((a, idx) => {
93
- buf += a;
94
- if (border > 0 && idx < ncols - 1)
95
- buf += " | ";
96
- });
97
- buf += ".\n";
98
- if (!tuplesOnly) {
99
- headers.forEach((h, idx) => {
100
- if (idx !== 0)
101
- buf += "\t";
102
- buf += "\\fI" + escapeTroff(h) + "\\fP";
103
- });
104
- buf += "\n_\n";
105
- }
106
- }
107
- cells.forEach((row) => {
108
- row.forEach((value, idx) => {
109
- buf += escapeTroff(value);
110
- if (idx === ncols - 1)
111
- buf += "\n";
112
- else
113
- buf += "\t";
114
- });
115
- });
116
- if (stopTable) {
117
- buf += ".TE\n.DS L\n";
118
- if (!tuplesOnly) {
119
- const effective = effectiveFooters(rs, topt, footers);
120
- for (const f of effective) {
121
- buf += escapeTroff(f) + "\n";
122
- }
123
- }
124
- buf += ".DE\n";
125
- }
126
- out.write(buf);
127
- return Promise.resolve();
128
- };
129
- const printExpanded = (rs, opts, out) => {
130
- const topt = opts.topt;
131
- const tuplesOnly = topt.tuplesOnly;
132
- const startTable = topt.startTable;
133
- const stopTable = topt.stopTable;
134
- const border = clampBorder(topt.border);
135
- const nullPrint = opts.nullPrint !== "" ? opts.nullPrint : topt.nullPrint;
136
- const title = opts.title ?? topt.title;
137
- const footers = opts.footers ?? topt.footers;
138
- const headers = rs.fields.map((f) => f.name);
139
- const boolDisplay = boolDisplayOf(topt);
140
- const cells = rs.rows.map((row) => row.map((cell, i) => renderCell(cell, nullPrint, topt.numericLocale, rs.fields[i]?.dataTypeID, boolDisplay)));
141
- let buf = "";
142
- // currentFormat: 0 = none yet, 1 = "Record N" header (c s),
143
- // 2 = body (c l or c | l). Upstream uses the same tri-state to
144
- // decide when to emit `.T&` separators.
145
- let currentFormat = 0;
146
- if (startTable) {
147
- if (!tuplesOnly && title) {
148
- buf += ".LP\n.DS C\n";
149
- buf += escapeTroff(title);
150
- buf += "\n.DE\n";
151
- }
152
- buf += ".LP\n.TS\n";
153
- buf += border === 2 ? "center box;\n" : "center;\n";
154
- // Under tuples-only, upstream emits a one-shot `c l;` body spec
155
- // here so each Record's first .T& block is omitted.
156
- if (tuplesOnly) {
157
- buf += "c l;\n";
158
- }
159
- }
160
- else {
161
- // Continuation: assume body spec is already in effect.
162
- currentFormat = 2;
163
- }
164
- let record = topt.prior + 1;
165
- cells.forEach((row, rowIdx) => {
166
- if (!tuplesOnly) {
167
- if (currentFormat !== 1) {
168
- if (border === 2 && rowIdx > 0)
169
- buf += "_\n";
170
- if (currentFormat !== 0)
171
- buf += ".T&\n";
172
- buf += "c s.\n";
173
- currentFormat = 1;
174
- }
175
- buf += `\\fIRecord ${String(record)}\\fP\n`;
176
- record += 1;
177
- }
178
- if (border >= 1)
179
- buf += "_\n";
180
- if (!tuplesOnly) {
181
- if (currentFormat !== 2) {
182
- if (currentFormat !== 0)
183
- buf += ".T&\n";
184
- buf += border !== 1 ? "c l.\n" : "c | l.\n";
185
- currentFormat = 2;
186
- }
187
- }
188
- row.forEach((value, idx) => {
189
- buf += escapeTroff(headers[idx]);
190
- buf += "\t";
191
- buf += escapeTroff(value);
192
- buf += "\n";
193
- });
194
- });
195
- if (stopTable) {
196
- buf += ".TE\n.DS L\n";
197
- // Expanded mode does NOT emit the default "(N rows)" footer.
198
- if (!tuplesOnly && footers && footers.length > 0) {
199
- for (const f of footers) {
200
- buf += escapeTroff(f) + "\n";
201
- }
202
- }
203
- buf += ".DE\n";
204
- }
205
- out.write(buf);
206
- return Promise.resolve();
207
- };
208
- const clampBorder = (b) => {
209
- if (b > 2)
210
- return 2;
211
- if (b < 0)
212
- return 0;
213
- return b;
214
- };
215
- const effectiveFooters = (rs, topt, footers) => {
216
- if (footers && footers.length > 0)
217
- return footers;
218
- if (topt.defaultFooter) {
219
- const n = rs.rows.length;
220
- return [`(${String(n)} ${n === 1 ? "row" : "rows"})`];
221
- }
222
- return [];
223
- };
224
- const escapeTroff = (input) => {
225
- // Only `\` is dangerous (it begins a troff escape). Map it to `\(rs`,
226
- // which renders the reverse-solidus glyph. Newlines flow through
227
- // unchanged — `.TS` treats each line as a new row, so we never want
228
- // raw newlines inside a cell; that constraint is enforced upstream
229
- // by the caller, not here. (`renderCell` similarly never emits a raw
230
- // newline today.)
231
- let out = "";
232
- for (const ch of input) {
233
- if (ch === "\\")
234
- out += "\\(rs";
235
- else
236
- out += ch;
237
- }
238
- return out;
239
- };
240
- const renderCell = (cell, nullPrint, numericLocale, dataTypeID, boolDisplay) => renderCellValue(cell, nullPrint, numericLocale, dataTypeID, boolDisplay);
@@ -1,99 +0,0 @@
1
- import { boolDisplayOf, renderCellValue } from "./units.js";
2
- /**
3
- * Unaligned tabular printer.
4
- *
5
- * Mirrors print.c `print_unaligned_text` / `print_unaligned_vertical`.
6
- *
7
- * Field separator defaults to `|`, record separator defaults to `\n`,
8
- * both honor `opts.topt.fieldSep` / `recordSep`. NULL cells render
9
- * `opts.nullPrint` (which falls back to `topt.nullPrint`, default `''`).
10
- *
11
- * Expanded (`\x`) mode prints `colname|value` per line with a blank
12
- * record-sep gap between rows.
13
- *
14
- * The default footer `(N rows)` is only emitted when not in expanded
15
- * mode and `topt.defaultFooter` is true; same as upstream psql.
16
- */
17
- export const unalignedPrinter = {
18
- format: "unaligned",
19
- printQuery(rs, opts, out) {
20
- const topt = opts.topt;
21
- const fieldSep = topt.fieldSep !== "" ? topt.fieldSep : "|";
22
- const recordSep = topt.recordSep !== "" ? topt.recordSep : "\n";
23
- const nullPrint = opts.nullPrint !== "" ? opts.nullPrint : topt.nullPrint;
24
- const expanded = topt.expanded === "on";
25
- const tuplesOnly = topt.tuplesOnly;
26
- const headers = rs.fields.map((f) => f.name);
27
- const boolDisplay = boolDisplayOf(topt);
28
- const cells = rs.rows.map((row) => row.map((cell, i) => renderCell(cell, nullPrint, topt.numericLocale, rs.fields[i]?.dataTypeID, boolDisplay)));
29
- let outBuf = "";
30
- if (expanded) {
31
- // Vertical mode mirrors print.c `print_unaligned_vertical`: each
32
- // record gap is a DOUBLE recordSep, the title (if any) emits
33
- // without a trailing separator (the loop handles that), inter-
34
- // column lines get one recordSep, and the closing footer block is
35
- // preceded by a recordSep with one more recordSep separating each
36
- // footer entry.
37
- let needRecordSep = false;
38
- if (!tuplesOnly && opts.title) {
39
- outBuf += opts.title;
40
- needRecordSep = true;
41
- }
42
- cells.forEach((row) => {
43
- if (needRecordSep) {
44
- outBuf += recordSep + recordSep;
45
- needRecordSep = false;
46
- }
47
- row.forEach((value, colIdx) => {
48
- outBuf += headers[colIdx] + fieldSep + value;
49
- if (colIdx < row.length - 1) {
50
- outBuf += recordSep;
51
- }
52
- else {
53
- needRecordSep = true;
54
- }
55
- });
56
- });
57
- if (!tuplesOnly && opts.footers && opts.footers.length > 0) {
58
- outBuf += recordSep;
59
- for (const footer of opts.footers) {
60
- outBuf += recordSep + footer;
61
- }
62
- needRecordSep = true;
63
- }
64
- if (needRecordSep)
65
- outBuf += recordSep;
66
- }
67
- else {
68
- // Horizontal mode.
69
- if (!tuplesOnly && opts.title) {
70
- outBuf += opts.title + recordSep;
71
- }
72
- if (!tuplesOnly) {
73
- outBuf += headers.join(fieldSep) + recordSep;
74
- }
75
- cells.forEach((row) => {
76
- outBuf += row.join(fieldSep) + recordSep;
77
- });
78
- // Default `(N rows)` footer is suppressed when the caller supplied its
79
- // own footers — upstream (and aligned.ts) print user footers INSTEAD of
80
- // the row count, not in addition (review: unaligned footer duplication).
81
- const hasUserFooters = opts.footers !== undefined &&
82
- opts.footers !== null &&
83
- opts.footers.length > 0;
84
- if (!tuplesOnly && topt.defaultFooter && !hasUserFooters) {
85
- const n = rs.rows.length;
86
- outBuf +=
87
- `(${String(n)} ${n === 1 ? "row" : "rows"})` + recordSep;
88
- }
89
- if (!tuplesOnly && hasUserFooters) {
90
- for (const footer of opts.footers ?? []) {
91
- outBuf += footer + recordSep;
92
- }
93
- }
94
- }
95
- out.write(outBuf);
96
- return Promise.resolve();
97
- },
98
- };
99
- const renderCell = (cell, nullPrint, numericLocale, dataTypeID, boolDisplay) => renderCellValue(cell, nullPrint, numericLocale, dataTypeID, boolDisplay);
@@ -1,188 +0,0 @@
1
- /**
2
- * Numeric, byte size, and duration formatting helpers.
3
- *
4
- * Mirrors psql's `\pset numericlocale`, the server-side
5
- * `pg_size_pretty()` output, and the `\timing` line format.
6
- */
7
- const NUMERIC_RE = /^-?\d+(\.\d+)?$/;
8
- /** PostgreSQL `bool` type OID. Cells of this column type honour the
9
- * `\pset display_true` / `\pset display_false` strings (PG 19+). */
10
- export const BOOLOID = 16;
11
- /**
12
- * Extract the `\pset display_true`/`display_false` pair from a print-table
13
- * options object, defaulting to psql's `'t'`/`'f'`. Kept here so every
14
- * printer derives `boolDisplay` the same way.
15
- */
16
- export const boolDisplayOf = (topt) => ({
17
- true: topt.truePrint ?? "t",
18
- false: topt.falsePrint ?? "f",
19
- });
20
- /**
21
- * Render a single result cell to its display string, shared by every
22
- * table-format printer (aligned/unaligned/csv/html/latex/troff/asciidoc).
23
- *
24
- * Boolean handling mirrors upstream print.c: a cell in a BOOLOID column
25
- * arriving over the wire as the text `'t'`/`'f'` (or a JS boolean, for
26
- * synthetic result sets) renders through `boolDisplay`, which carries the
27
- * `\pset display_true`/`display_false` values. `dataTypeID`/`boolDisplay`
28
- * are optional so existing callers that don't thread column type through
29
- * keep the prior behaviour (`'t'`/`'f'`).
30
- */
31
- export const renderCellValue = (cell, nullPrint, numericLocale, dataTypeID, boolDisplay, locale) => {
32
- if (cell === null || cell === undefined)
33
- return nullPrint;
34
- const trueStr = boolDisplay?.true ?? "t";
35
- const falseStr = boolDisplay?.false ?? "f";
36
- // BOOLOID column: the text-format wire value is 't'/'f'. Map through the
37
- // configured display strings (upstream keys on the 't' first byte).
38
- if (dataTypeID === BOOLOID && typeof cell === "string") {
39
- if (cell === "t" || cell === "f")
40
- return cell === "t" ? trueStr : falseStr;
41
- }
42
- if (typeof cell === "string") {
43
- return formatNumericLocale(cell, numericLocale, locale);
44
- }
45
- if (typeof cell === "number" || typeof cell === "bigint") {
46
- return formatNumericLocale(cell.toString(), numericLocale, locale);
47
- }
48
- if (typeof cell === "boolean")
49
- return cell ? trueStr : falseStr;
50
- if (cell instanceof Date)
51
- return cell.toISOString();
52
- if (cell instanceof Uint8Array) {
53
- // Bytea -> hex escape, matching libpq's `\x` form.
54
- let hex = "\\x";
55
- for (const b of cell)
56
- hex += b.toString(16).padStart(2, "0");
57
- return hex;
58
- }
59
- return JSON.stringify(cell);
60
- };
61
- /**
62
- * Format a numeric string using locale-aware thousand separators and
63
- * decimal point. Equivalent to print.c `format_numeric_locale` but
64
- * implemented through the host's `Intl.NumberFormat`.
65
- *
66
- * When `useLocale` is false (the psql default) the input is returned
67
- * unchanged. Only operates on values whose stringified form matches
68
- * `^-?\d+(\.\d+)?$`; anything else (NaN, scientific notation, etc.) is
69
- * returned as-is so we never mangle non-numeric data passed via
70
- * `numericlocale`.
71
- */
72
- export const formatNumericLocale = (value, useLocale, locale) => {
73
- if (!useLocale)
74
- return value;
75
- if (!NUMERIC_RE.test(value))
76
- return value;
77
- const dotIdx = value.indexOf(".");
78
- const fractionDigits = dotIdx === -1 ? 0 : value.length - dotIdx - 1;
79
- // Use BigInt for the integer part to avoid float precision loss on
80
- // very large integers; format the fractional component as a separate
81
- // number so we keep the exact digit count the input had.
82
- const negative = value.startsWith("-");
83
- const unsigned = negative ? value.slice(1) : value;
84
- const [intPart, fracPart = ""] = unsigned.split(".");
85
- const intFormatted = new Intl.NumberFormat(locale, {
86
- useGrouping: true,
87
- maximumFractionDigits: 0,
88
- }).format(BigInt(intPart));
89
- if (fractionDigits === 0) {
90
- return negative ? `-${intFormatted}` : intFormatted;
91
- }
92
- // Discover this locale's decimal separator.
93
- const decimalSep = new Intl.NumberFormat(locale)
94
- .formatToParts(1.1)
95
- .find((p) => p.type === "decimal")?.value ?? ".";
96
- const out = `${intFormatted}${decimalSep}${fracPart}`;
97
- return negative ? `-${out}` : out;
98
- };
99
- const BYTE_UNITS = [
100
- { name: "bytes", bits: 0 },
101
- { name: "kB", bits: 10 },
102
- { name: "MB", bits: 20 },
103
- { name: "GB", bits: 30 },
104
- { name: "TB", bits: 40 },
105
- { name: "PB", bits: 50 },
106
- ];
107
- const SI_UNITS = [
108
- { name: "B", bits: 0 },
109
- { name: "kB", bits: 10 },
110
- { name: "MB", bits: 20 },
111
- { name: "GB", bits: 30 },
112
- { name: "TB", bits: 40 },
113
- { name: "PB", bits: 50 },
114
- ];
115
- /**
116
- * Pretty-print a byte count.
117
- *
118
- * Follows pg's `pg_size_pretty` ladder: values under 10 240 use the
119
- * `bytes` unit, otherwise we promote until the magnitude is below
120
- * 20 * 1024 in the next unit and emit one decimal place.
121
- *
122
- * `opts.si` swaps the smallest unit name from `bytes` to `B`. (psql
123
- * itself does not have an SI flag, but we expose one because callers
124
- * outside the print path want a shorter `0 B` rendering.)
125
- */
126
- export const formatByteSize = (bytes, opts) => {
127
- const units = opts?.si ? SI_UNITS : BYTE_UNITS;
128
- const sign = bytes < 0 ? "-" : "";
129
- let abs = Math.abs(bytes);
130
- // Bytes unit: render integer count.
131
- if (abs < 10 * 1024) {
132
- return `${sign}${abs.toString()} ${units[0].name}`;
133
- }
134
- // Promote until the value, scaled to the next unit, would be below
135
- // 20 * 1024. Mirrors the original loop's threshold check.
136
- let unitIndex = 1;
137
- abs = abs / 1024;
138
- while (unitIndex < units.length - 1 && abs >= 20 * 1024) {
139
- abs = abs / 1024;
140
- unitIndex++;
141
- }
142
- // One decimal place once we leave the `bytes` regime, matching the
143
- // `0 B`, `1.0 kB`, `1.5 MB` shape the WP spec calls out.
144
- return `${sign}${abs.toFixed(1)} ${units[unitIndex].name}`;
145
- };
146
- /**
147
- * Render a duration in the upstream `\timing` style. psql scales the
148
- * granularity to keep the line compact:
149
- *
150
- * - `< 1 ms` → `Time: 0.123 ms` (three decimal digits)
151
- * - `< 1 s` → `Time: 123.456 ms`
152
- * - `< 1 min` → `Time: 12.345 s`
153
- * - `< 1 hour` → `Time: 12 m 34.567 s`
154
- * - `>= 1 hour` → `Time: 1 h 23 m 45.678 s`
155
- *
156
- * Mirrors the `PrintTiming()` ladder in `src/bin/psql/common.c`. Negative
157
- * or non-finite inputs fall through the ladder as `0`.
158
- */
159
- export const formatDurationMs = (ms) => {
160
- return `Time: ${formatDurationBody(ms)}`;
161
- };
162
- /**
163
- * Format just the scaled-duration body (without the `Time: ` prefix) so
164
- * callers like the `\watch` header can reuse the same ladder without
165
- * dragging the prefix along.
166
- */
167
- export const formatDurationBody = (ms) => {
168
- const safe = Number.isFinite(ms) && ms > 0 ? ms : 0;
169
- // Under one second: render in milliseconds with three decimal places.
170
- if (safe < 1000) {
171
- return `${safe.toFixed(3)} ms`;
172
- }
173
- const totalSeconds = safe / 1000;
174
- // Under one minute: render in seconds with three decimal places.
175
- if (totalSeconds < 60) {
176
- return `${totalSeconds.toFixed(3)} s`;
177
- }
178
- // Under one hour: `M m SS.sss s`.
179
- const totalMinutes = Math.floor(totalSeconds / 60);
180
- const remainingSeconds = totalSeconds - totalMinutes * 60;
181
- if (totalMinutes < 60) {
182
- return `${String(totalMinutes)} m ${remainingSeconds.toFixed(3)} s`;
183
- }
184
- // One hour or more: `H h MM m SS.sss s`.
185
- const hours = Math.floor(totalMinutes / 60);
186
- const minutes = totalMinutes - hours * 60;
187
- return `${String(hours)} h ${String(minutes)} m ${remainingSeconds.toFixed(3)} s`;
188
- };