neonctl 2.22.0 → 2.23.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 (116) hide show
  1. package/README.md +242 -16
  2. package/analytics.js +5 -2
  3. package/commands/branches.js +9 -1
  4. package/commands/checkout.js +249 -0
  5. package/commands/connection_string.js +15 -2
  6. package/commands/data_api.js +286 -0
  7. package/commands/functions.js +277 -0
  8. package/commands/index.js +12 -0
  9. package/commands/link.js +667 -0
  10. package/commands/neon_auth.js +1013 -0
  11. package/commands/projects.js +9 -1
  12. package/commands/psql.js +62 -0
  13. package/commands/set_context.js +7 -2
  14. package/context.js +86 -14
  15. package/functions_api.js +44 -0
  16. package/index.js +3 -0
  17. package/package.json +60 -51
  18. package/psql/cli.js +51 -0
  19. package/psql/command/cmd_cond.js +437 -0
  20. package/psql/command/cmd_connect.js +815 -0
  21. package/psql/command/cmd_copy.js +1025 -0
  22. package/psql/command/cmd_describe.js +1810 -0
  23. package/psql/command/cmd_format.js +909 -0
  24. package/psql/command/cmd_io.js +2187 -0
  25. package/psql/command/cmd_lo.js +385 -0
  26. package/psql/command/cmd_meta.js +970 -0
  27. package/psql/command/cmd_misc.js +187 -0
  28. package/psql/command/cmd_pipeline.js +1141 -0
  29. package/psql/command/cmd_restrict.js +171 -0
  30. package/psql/command/cmd_show.js +751 -0
  31. package/psql/command/dispatch.js +343 -0
  32. package/psql/command/inputQueue.js +42 -0
  33. package/psql/command/shared.js +71 -0
  34. package/psql/complete/filenames.js +139 -0
  35. package/psql/complete/index.js +104 -0
  36. package/psql/complete/matcher.js +314 -0
  37. package/psql/complete/psqlVars.js +247 -0
  38. package/psql/complete/queries.js +491 -0
  39. package/psql/complete/rules.js +2387 -0
  40. package/psql/core/common.js +1250 -0
  41. package/psql/core/help.js +576 -0
  42. package/psql/core/mainloop.js +1353 -0
  43. package/psql/core/prompt.js +437 -0
  44. package/psql/core/settings.js +684 -0
  45. package/psql/core/sqlHelp.js +1066 -0
  46. package/psql/core/startup.js +840 -0
  47. package/psql/core/syncVars.js +116 -0
  48. package/psql/core/variables.js +287 -0
  49. package/psql/describe/formatters.js +1277 -0
  50. package/psql/describe/processNamePattern.js +270 -0
  51. package/psql/describe/queries.js +2373 -0
  52. package/psql/describe/versionGate.js +43 -0
  53. package/psql/index.js +2005 -0
  54. package/psql/io/history.js +299 -0
  55. package/psql/io/input.js +120 -0
  56. package/psql/io/lineEditor/buffer.js +323 -0
  57. package/psql/io/lineEditor/complete.js +227 -0
  58. package/psql/io/lineEditor/filename.js +159 -0
  59. package/psql/io/lineEditor/index.js +891 -0
  60. package/psql/io/lineEditor/keymap.js +738 -0
  61. package/psql/io/lineEditor/vt100.js +363 -0
  62. package/psql/io/pgpass.js +202 -0
  63. package/psql/io/pgservice.js +194 -0
  64. package/psql/io/psqlrc.js +422 -0
  65. package/psql/print/aligned.js +1756 -0
  66. package/psql/print/asciidoc.js +248 -0
  67. package/psql/print/crosstab.js +460 -0
  68. package/psql/print/csv.js +92 -0
  69. package/psql/print/html.js +258 -0
  70. package/psql/print/json.js +96 -0
  71. package/psql/print/latex.js +396 -0
  72. package/psql/print/pager.js +265 -0
  73. package/psql/print/troff.js +258 -0
  74. package/psql/print/unaligned.js +118 -0
  75. package/psql/print/units.js +135 -0
  76. package/psql/scanner/slash.js +513 -0
  77. package/psql/scanner/sql.js +910 -0
  78. package/psql/scanner/stringutils.js +390 -0
  79. package/psql/types/backslash.js +1 -0
  80. package/psql/types/connection.js +1 -0
  81. package/psql/types/index.js +7 -0
  82. package/psql/types/printer.js +1 -0
  83. package/psql/types/repl.js +1 -0
  84. package/psql/types/scanner.js +24 -0
  85. package/psql/types/settings.js +1 -0
  86. package/psql/types/variables.js +1 -0
  87. package/psql/wire/connection.js +2844 -0
  88. package/psql/wire/copy.js +108 -0
  89. package/psql/wire/notify.js +59 -0
  90. package/psql/wire/pipeline.js +519 -0
  91. package/psql/wire/protocol.js +466 -0
  92. package/psql/wire/sasl.js +296 -0
  93. package/psql/wire/tls.js +596 -0
  94. package/test_utils/fixtures.js +1 -0
  95. package/utils/enrichers.js +18 -1
  96. package/utils/esbuild.js +147 -0
  97. package/utils/middlewares.js +1 -1
  98. package/utils/psql.js +107 -11
  99. package/utils/zip.js +4 -0
  100. package/writer.js +1 -1
  101. package/commands/auth.test.js +0 -211
  102. package/commands/branches.test.js +0 -460
  103. package/commands/connection_string.test.js +0 -196
  104. package/commands/databases.test.js +0 -39
  105. package/commands/help.test.js +0 -9
  106. package/commands/init.test.js +0 -56
  107. package/commands/ip_allow.test.js +0 -59
  108. package/commands/operations.test.js +0 -7
  109. package/commands/orgs.test.js +0 -7
  110. package/commands/projects.test.js +0 -144
  111. package/commands/roles.test.js +0 -37
  112. package/commands/set_context.test.js +0 -159
  113. package/commands/vpc_endpoints.test.js +0 -69
  114. package/env.test.js +0 -55
  115. package/utils/formats.test.js +0 -32
  116. package/writer.test.js +0 -104
@@ -0,0 +1,437 @@
1
+ /**
2
+ * psql Prompt renderer.
3
+ *
4
+ * TypeScript port of `get_prompt()` from `src/bin/psql/prompt.c`. Walks the
5
+ * prompt template (PROMPT1/PROMPT2/PROMPT3) and expands `%`-escapes against
6
+ * a `PromptContext` snapshot.
7
+ *
8
+ * Differences from upstream:
9
+ *
10
+ * - Upstream takes `promptStatus_t status, ConditionalStack cstack` and
11
+ * pulls everything else off `pset`. We accept an explicit `PromptContext`
12
+ * so the renderer is a pure function (easier to test, no module-level
13
+ * state).
14
+ * - `%s` ("service name") is provided from `settings.vars.SERVICE` per
15
+ * upstream. We keep that behaviour. The WP-07 spec mentions a "current
16
+ * timestamp" semantic for `%s`/`%S` — that is NOT what psql does today,
17
+ * so we follow upstream rather than the spec note.
18
+ * - `%S` is `search_path` from the connection's GUC report (upstream uses
19
+ * `PQparameterStatus(pset.db, "search_path")`). With no Connection yet,
20
+ * we fall back to `?`.
21
+ * - `%#` checks the `IS_SUPERUSER` psql variable (set by upstream's
22
+ * `is_superuser()` via the connect-time hook). Without a Connection we
23
+ * default to non-superuser (`>`).
24
+ * - `%P` outputs `off`/`on`/`abort` to match upstream's literal strings.
25
+ * - `%i` outputs `primary` / `standby` / `?` (upstream reads the GUC
26
+ * `in_hot_standby`); when no connection, `?`. The WP-07 spec asked for
27
+ * "if-state from CondStack" — that conflicts with upstream `%i` and we
28
+ * follow upstream. `%?` remains a no-op marker in upstream too.
29
+ * - `%[`/`%]` are upstream's readline non-printing markers. We strip them
30
+ * (emit nothing) for now; a future WP wiring readline integration can
31
+ * re-introduce them.
32
+ * - Backtick command interpolation (`%`\``cmd`\``) is executed synchronously
33
+ * via `child_process.execSync` on `sh -c <cmd>`. Output is substituted
34
+ * verbatim minus one trailing newline. Errors print to stderr and yield
35
+ * an empty substitution — matching upstream's `get_prompt`, which re-runs
36
+ * the command on every render rather than caching.
37
+ * - Unknown `%X` escapes pass through as literal `X` (the upstream
38
+ * `default:` branch). `%w` width is computed from PROMPT1 when present
39
+ * and consumed by a subsequent PROMPT2 render via the optional
40
+ * `lastPrompt1Width` field on the context.
41
+ */
42
+ import { execSync } from 'node:child_process';
43
+ /**
44
+ * Test seam for the prompt-level backtick executor. The default
45
+ * implementation calls `execSync(cmd, { shell: '/bin/sh' })`; tests can
46
+ * replace `.current` with a synchronous mock to avoid actually spawning
47
+ * a child. Same pattern as `BACKTICK_EXECUTOR` in `scanner/slash.ts`.
48
+ */
49
+ export const PROMPT_BACKTICK_EXECUTOR = {
50
+ current: (cmd) => execSync(cmd, {
51
+ shell: '/bin/sh',
52
+ encoding: 'utf8',
53
+ stdio: ['ignore', 'pipe', 'inherit'],
54
+ // Keep prompt rendering responsive — a heavy backtick command should
55
+ // not be able to fill arbitrary memory or hang the prompt forever.
56
+ maxBuffer: 1 << 16,
57
+ }),
58
+ };
59
+ /**
60
+ * Run a single backtick command and return its stdout, with one trailing
61
+ * newline trimmed. On failure (non-zero exit / spawn error) print the
62
+ * error to stderr and return the empty string so the prompt still renders.
63
+ */
64
+ const runPromptBacktick = (cmd) => {
65
+ if (cmd.length === 0)
66
+ return '';
67
+ try {
68
+ const out = PROMPT_BACKTICK_EXECUTOR.current(cmd);
69
+ return out.endsWith('\n') ? out.slice(0, -1) : out;
70
+ }
71
+ catch (err) {
72
+ const msg = err instanceof Error ? err.message : String(err);
73
+ process.stderr.write(`psql: error: \\!: ${cmd}: ${msg}\n`);
74
+ return '';
75
+ }
76
+ };
77
+ /**
78
+ * Render a prompt by name. Selects the template from
79
+ * `settings.prompt{1,2,3}` and delegates to the escape-walking core.
80
+ */
81
+ export const renderPromptByName = (name, ctx) => {
82
+ const template = name === 'PROMPT1'
83
+ ? ctx.settings.prompt1
84
+ : name === 'PROMPT2'
85
+ ? ctx.settings.prompt2
86
+ : ctx.settings.prompt3;
87
+ const isPrompt1 = name === 'PROMPT1';
88
+ const out = renderPrompt(template, ctx);
89
+ if (isPrompt1) {
90
+ // Stash the visible width on the context so a subsequent PROMPT2 render
91
+ // sharing this context object can line up with `%w`.
92
+ ctx.lastPrompt1Width = visibleWidth(out);
93
+ }
94
+ return out;
95
+ };
96
+ /**
97
+ * Render an arbitrary prompt template. Pure function over `(template, ctx)`.
98
+ */
99
+ export const renderPrompt = (template, ctx) => {
100
+ let out = '';
101
+ let i = 0;
102
+ while (i < template.length) {
103
+ const ch = template[i];
104
+ if (ch !== '%') {
105
+ out += ch;
106
+ i += 1;
107
+ continue;
108
+ }
109
+ // We have `%X`; advance past the `%` and resolve.
110
+ i += 1;
111
+ if (i >= template.length) {
112
+ // Trailing `%` — emit nothing, matches upstream (esc stays true and
113
+ // the loop exits without strlcat).
114
+ break;
115
+ }
116
+ const escape = template[i];
117
+ const { text, consumed } = expandEscape(escape, template, i, ctx);
118
+ out += text;
119
+ i += consumed;
120
+ }
121
+ return out;
122
+ };
123
+ const expandEscape = (escape, template,
124
+ /** Index of `escape` within `template`. */
125
+ start, ctx) => {
126
+ const { settings } = ctx;
127
+ const db = settings.db;
128
+ switch (escape) {
129
+ case '%':
130
+ return { text: '%', consumed: 1 };
131
+ case '/':
132
+ // Current database.
133
+ return { text: db ? currentDatabase(ctx) : '', consumed: 1 };
134
+ case '~': {
135
+ // Like %/, but "~" when current_db matches the user's default db.
136
+ // Upstream compares PQdb(pset.db) against PQuser(pset.db) and
137
+ // against $PGDATABASE.
138
+ if (!db)
139
+ return { text: '', consumed: 1 };
140
+ const cdb = currentDatabase(ctx);
141
+ const user = currentUser(ctx);
142
+ const pgdb = process.env.PGDATABASE;
143
+ if (cdb === user || (pgdb !== undefined && pgdb === cdb)) {
144
+ return { text: '~', consumed: 1 };
145
+ }
146
+ return { text: cdb, consumed: 1 };
147
+ }
148
+ case 'M':
149
+ case 'm':
150
+ return {
151
+ text: db ? hostString(ctx, escape === 'm') : '',
152
+ consumed: 1,
153
+ };
154
+ case '>':
155
+ return { text: db ? portString(ctx) : '', consumed: 1 };
156
+ case 'n':
157
+ return { text: db ? currentUser(ctx) : '', consumed: 1 };
158
+ case 'p': {
159
+ if (!db)
160
+ return { text: '', consumed: 1 };
161
+ const pid = backendPid(ctx);
162
+ return { text: pid !== null ? String(pid) : '', consumed: 1 };
163
+ }
164
+ case 'P': {
165
+ if (!db)
166
+ return { text: '', consumed: 1 };
167
+ // Upstream emits "off" / "on" / "abort" (no trailing 'ed').
168
+ const state = ctx.pipelineState ?? 'off';
169
+ const text = state === 'on' ? 'on' : state === 'aborted' ? 'abort' : 'off';
170
+ return { text, consumed: 1 };
171
+ }
172
+ case 'i': {
173
+ // Hot-standby indicator. Upstream reads in_hot_standby GUC.
174
+ if (!db)
175
+ return { text: '', consumed: 1 };
176
+ const hs = parameterStatus(ctx, 'in_hot_standby');
177
+ if (hs === undefined)
178
+ return { text: '?', consumed: 1 };
179
+ return { text: hs === 'on' ? 'standby' : 'primary', consumed: 1 };
180
+ }
181
+ case 's': {
182
+ // service name from psql var SERVICE (upstream prompt.c).
183
+ const svc = settings.vars.get('SERVICE');
184
+ return { text: svc ?? '', consumed: 1 };
185
+ }
186
+ case 'S': {
187
+ // search_path; `?` when unavailable (older servers or no conn).
188
+ if (!db)
189
+ return { text: '?', consumed: 1 };
190
+ const sp = parameterStatus(ctx, 'search_path');
191
+ return { text: sp ?? '?', consumed: 1 };
192
+ }
193
+ case 'l':
194
+ return { text: String(ctx.lineNumber), consumed: 1 };
195
+ case 'w': {
196
+ // Whitespace padding the width of the last PROMPT1 render.
197
+ const width = Math.max(0, ctx.lastPrompt1Width ?? 0);
198
+ return { text: db ? ' '.repeat(width) : '', consumed: 1 };
199
+ }
200
+ case 'a':
201
+ // Bell. Upstream falls into default, which is just literal 'a' (no
202
+ // bell handling in get_prompt). The WP-07 spec explicitly asks for
203
+ // ^G though, and emitting BEL is harmless. Honour the spec.
204
+ return { text: '\x07', consumed: 1 };
205
+ case 'R': {
206
+ const text = expandR(ctx);
207
+ return { text, consumed: 1 };
208
+ }
209
+ case 'x': {
210
+ if (!db)
211
+ return { text: '?', consumed: 1 };
212
+ const tx = ctx.inTransaction ?? 'idle';
213
+ if (tx === 'in-block')
214
+ return { text: '*', consumed: 1 };
215
+ if (tx === 'failed')
216
+ return { text: '!', consumed: 1 };
217
+ if (tx === 'unknown')
218
+ return { text: '?', consumed: 1 };
219
+ return { text: '', consumed: 1 };
220
+ }
221
+ case '#': {
222
+ // Superuser indicator. Upstream calls is_superuser() which checks
223
+ // pset.vars["IS_SUPERUSER"] === "on".
224
+ const isSuper = settings.vars.get('IS_SUPERUSER') === 'on';
225
+ return { text: isSuper ? '#' : '>', consumed: 1 };
226
+ }
227
+ case '?':
228
+ // Reserved by upstream ("not here yet"). Emit nothing.
229
+ return { text: '', consumed: 1 };
230
+ case '[':
231
+ case ']':
232
+ // Readline non-printing markers. Strip — no terminal info embedded.
233
+ return { text: '', consumed: 1 };
234
+ case '`': {
235
+ // Backtick command: read until the matching `` ` `` after the opening
236
+ // backtick, then run the body through `sh -c` and substitute its
237
+ // stdout. Upstream `get_prompt` re-runs the command on every render
238
+ // rather than caching, so we do the same — callers who want caching
239
+ // should stash the rendered prompt themselves.
240
+ const close = template.indexOf('`', start + 1);
241
+ if (close === -1) {
242
+ // Unterminated — consume the rest defensively without spawning.
243
+ return { text: '', consumed: template.length - start };
244
+ }
245
+ const body = template.slice(start + 1, close);
246
+ return { text: runPromptBacktick(body), consumed: close - start + 1 };
247
+ }
248
+ case ':': {
249
+ // Variable interpolation %:name:
250
+ const close = template.indexOf(':', start + 1);
251
+ if (close === -1) {
252
+ return { text: '', consumed: template.length - start };
253
+ }
254
+ const name = template.slice(start + 1, close);
255
+ const val = settings.vars.get(name);
256
+ return { text: val ?? '', consumed: close - start + 1 };
257
+ }
258
+ case '0':
259
+ case '1':
260
+ case '2':
261
+ case '3':
262
+ case '4':
263
+ case '5':
264
+ case '6':
265
+ case '7': {
266
+ // Octal byte value `%nnn`. Upstream uses strtol(p, &p, 8), which
267
+ // greedily consumes 1–3 octal digits.
268
+ let consumed = 1;
269
+ let body = escape;
270
+ while (consumed < 3 && start + consumed < template.length) {
271
+ const next = template[start + consumed];
272
+ if (next >= '0' && next <= '7') {
273
+ body += next;
274
+ consumed += 1;
275
+ }
276
+ else {
277
+ break;
278
+ }
279
+ }
280
+ const byte = parseInt(body, 8);
281
+ return { text: String.fromCharCode(byte), consumed };
282
+ }
283
+ default:
284
+ // Unknown escape → literal character (matches upstream default:).
285
+ return { text: escape, consumed: 1 };
286
+ }
287
+ };
288
+ /**
289
+ * Expand `%R`. PROMPT1 reflects the connection / cond-stack / singleline
290
+ * state. PROMPT2 reflects the scanner's promptStatus — upstream renders a
291
+ * mnemonic character for *why* the parser is still hanging on for more
292
+ * input:
293
+ *
294
+ * - `-` plain continuation (default)
295
+ * - `'` inside a single-quoted string
296
+ * - `"` inside a double-quoted identifier
297
+ * - `$` inside a dollar-quoted block
298
+ * - `*` inside a block comment
299
+ * - `(` inside unmatched parens
300
+ *
301
+ * PROMPT3 is the COPY indicator; upstream falls into the `default` branch
302
+ * and emits nothing — we do the same.
303
+ */
304
+ const expandR = (ctx) => {
305
+ const { promptStatus, cond, settings } = ctx;
306
+ switch (promptStatus) {
307
+ case 'ready': {
308
+ if (!isConditionalActive(cond))
309
+ return '@';
310
+ if (!settings.db)
311
+ return '!';
312
+ if (!settings.singleline)
313
+ return '=';
314
+ return '^';
315
+ }
316
+ case 'continue':
317
+ return '-';
318
+ case 'continue-quote':
319
+ return "'";
320
+ case 'continue-dquote':
321
+ return '"';
322
+ case 'continue-dollar':
323
+ return '$';
324
+ case 'comment':
325
+ return '*';
326
+ case 'paren':
327
+ return '(';
328
+ case 'copy':
329
+ // PROMPT3 / COPY path — upstream emits nothing for %R here.
330
+ return '';
331
+ default:
332
+ return '';
333
+ }
334
+ };
335
+ /**
336
+ * Return `false` if the current `\if` branch is non-active (we're inside a
337
+ * skipped block). Mirrors upstream `conditional_active`. Inactive states are
338
+ * `false`, `else-false`, and `ignored`; everything else (including the
339
+ * sentinel `none` for "no \if active") is treated as active.
340
+ */
341
+ const isConditionalActive = (cond) => {
342
+ const top = cond.top();
343
+ if (!top)
344
+ return true;
345
+ const inactive = ['false', 'else-false', 'ignored'];
346
+ return !inactive.includes(top.state);
347
+ };
348
+ /**
349
+ * Visible width of a rendered prompt — used to seed `%w` on PROMPT2. We
350
+ * subtract ANSI escape sequences and the unicode width is approximated by
351
+ * code-point count (full Unicode width tables live in the printer WP).
352
+ */
353
+ const visibleWidth = (s) => {
354
+ // Strip CSI escapes (ESC `[` … letter). Conservative — only trims the
355
+ // common ANSI color form psql emits. Newlines reset the count.
356
+ // eslint-disable-next-line no-control-regex
357
+ const stripped = s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, '');
358
+ const lastNl = stripped.lastIndexOf('\n');
359
+ const tail = lastNl === -1 ? stripped : stripped.slice(lastNl + 1);
360
+ // Code-unit count — good enough for ASCII prompts; CJK / surrogate-pair
361
+ // width handling is a printer-WP concern.
362
+ return tail.length;
363
+ };
364
+ const currentDatabase = (ctx) => {
365
+ const db = ctx.settings.db;
366
+ if (!db)
367
+ return '';
368
+ // Prefer a libpq-style accessor if the runtime Connection exposes one;
369
+ // otherwise fall back to the application's GUC view.
370
+ const meta = db;
371
+ if (typeof meta.database === 'string' && meta.database.length > 0) {
372
+ return meta.database;
373
+ }
374
+ return parameterStatus(ctx, 'database') ?? '';
375
+ };
376
+ const currentUser = (ctx) => {
377
+ const db = ctx.settings.db;
378
+ if (!db)
379
+ return '';
380
+ const meta = db;
381
+ if (typeof meta.user === 'string' && meta.user.length > 0) {
382
+ return meta.user;
383
+ }
384
+ return parameterStatus(ctx, 'session_authorization') ?? '';
385
+ };
386
+ const hostString = (ctx, short) => {
387
+ const db = ctx.settings.db;
388
+ if (!db)
389
+ return '';
390
+ const meta = db;
391
+ const host = typeof meta.host === 'string' ? meta.host : '';
392
+ if (host.length === 0 || host.startsWith('/')) {
393
+ // UNIX socket / unknown — upstream emits "[local]" or "[local:path]".
394
+ if (host.length === 0 || short)
395
+ return '[local]';
396
+ return `[local:${host}]`;
397
+ }
398
+ if (short) {
399
+ const dot = host.indexOf('.');
400
+ return dot === -1 ? host : host.slice(0, dot);
401
+ }
402
+ return host;
403
+ };
404
+ const portString = (ctx) => {
405
+ const db = ctx.settings.db;
406
+ if (!db)
407
+ return '';
408
+ const meta = db;
409
+ if (typeof meta.port === 'number' && Number.isFinite(meta.port)) {
410
+ return String(meta.port);
411
+ }
412
+ if (typeof meta.port === 'string' && meta.port.length > 0) {
413
+ return meta.port;
414
+ }
415
+ return '';
416
+ };
417
+ const backendPid = (ctx) => {
418
+ const db = ctx.settings.db;
419
+ if (!db)
420
+ return null;
421
+ const meta = db;
422
+ if (typeof meta.pid === 'number' && Number.isFinite(meta.pid)) {
423
+ return meta.pid;
424
+ }
425
+ return null;
426
+ };
427
+ const parameterStatus = (ctx, name) => {
428
+ const db = ctx.settings.db;
429
+ if (!db)
430
+ return undefined;
431
+ try {
432
+ return db.parameterStatus(name);
433
+ }
434
+ catch {
435
+ return undefined;
436
+ }
437
+ };