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,385 @@
1
+ /**
2
+ * psql large-object backslash commands (WP-23).
3
+ *
4
+ * TypeScript port of upstream `src/bin/psql/large_obj.c`:
5
+ *
6
+ * - `\lo_list` / `\lo_list+` — list large-object metadata
7
+ * - `\lo_import FILE [COMMENT]` — read a local file, store as new LO
8
+ * - `\lo_export OID FILE` — read LO, write to a local file
9
+ * - `\lo_unlink OID` — delete an LO
10
+ *
11
+ * The upstream `do_lo_*` functions wrap their work in
12
+ * `start_lo_xact`/`finish_lo_xact` and call libpq's lo client API
13
+ * (`lo_import`, `lo_export`, `lo_unlink`). We don't have libpq's
14
+ * client-side large-object API in this TS port, so we drive the same
15
+ * operations through the server-side functions that psql ≥ 9.4 exposes:
16
+ *
17
+ * - `pg_catalog.lo_from_bytea(0, $bytea)` returns the new OID in one
18
+ * call (replaces the upstream lo_creat + lo_write loop).
19
+ * - `pg_catalog.lo_get($oid)` returns the bytes as a `bytea` value.
20
+ * - `pg_catalog.lo_unlink($oid)` deletes the LO.
21
+ *
22
+ * All three calls use the connection's extended-query path (WP-21) for
23
+ * parameter binding, which keeps us out of the libpq escape dance.
24
+ * Bytea payloads are sent as `\x<hex>` text — the server's text-format
25
+ * bytea parser converts back to bytes.
26
+ *
27
+ * `\lo_list` runs the same SELECT against `pg_largeobject_metadata` that
28
+ * upstream's `listLargeObjects()` from `describe.c` uses (already ported
29
+ * in WP-20's `queries.ts::listLargeObjects`). We register a primary
30
+ * `lo_list` / `lo_list+` spec here so the dispatcher takes our entry
31
+ * over the existing alias `dl::lo_list` (which would not match the
32
+ * `+` suffix).
33
+ *
34
+ * Variable side-effects: `\lo_import` sets `LASTOID` to the new OID
35
+ * (mirrors `do_lo_import`'s `SetVariable(pset.vars, "LASTOID", oidbuf)`).
36
+ */
37
+ import { promises as fsPromises } from 'node:fs';
38
+ import { Buffer } from 'node:buffer';
39
+ import { alignedPrinter } from '../print/aligned.js';
40
+ import { listLargeObjects } from '../describe/queries.js';
41
+ import { writeErr, writeOut } from './shared.js';
42
+ // ---------------------------------------------------------------------------
43
+ // Helpers shared by all four commands
44
+ // ---------------------------------------------------------------------------
45
+ /** Return the live connection, or null. */
46
+ const conn = (ctx) => ctx.settings.db;
47
+ /** Emit "no current connection" error in the psql style. */
48
+ const noConn = (ctx) => {
49
+ writeErr(`\\${ctx.cmdName}: no connection to the server\n`);
50
+ ctx.settings.lastErrorResult = { message: 'no connection to the server' };
51
+ return { status: 'error' };
52
+ };
53
+ /** Pull the diagnostic-style error message off a thrown value. */
54
+ const errMsg = (err) => err instanceof Error ? err.message : String(err);
55
+ /**
56
+ * Encode a `Buffer` as a Postgres text-format bytea literal: `\x<hex>`.
57
+ * Hex form is unambiguous and works regardless of `bytea_output` or
58
+ * `standard_conforming_strings`.
59
+ */
60
+ const byteaText = (buf) => `\\x${buf.toString('hex')}`;
61
+ /**
62
+ * Parse a string argument as an unsigned 32-bit OID. Returns `null` on
63
+ * malformed input (negative, non-integer, or out of range). Matches the
64
+ * permissive behaviour of upstream `atooid`, which accepts any leading
65
+ * digit run.
66
+ */
67
+ const parseOid = (raw) => {
68
+ if (!/^\d+$/.test(raw))
69
+ return null;
70
+ const n = Number(raw);
71
+ if (!Number.isFinite(n) || n < 0 || n > 0xffffffff)
72
+ return null;
73
+ return n;
74
+ };
75
+ /**
76
+ * Coerce a single cell coming back from the protocol layer into a string.
77
+ * Used by the `\lo_list` renderer. Matches the helper in
78
+ * `describe/formatters.ts`.
79
+ */
80
+ const cellToString = (v) => {
81
+ if (v === null || v === undefined)
82
+ return '';
83
+ if (typeof v === 'string')
84
+ return v;
85
+ if (Buffer.isBuffer(v))
86
+ return v.toString('utf-8');
87
+ if (typeof v === 'number' ||
88
+ typeof v === 'boolean' ||
89
+ typeof v === 'bigint') {
90
+ return String(v);
91
+ }
92
+ try {
93
+ return JSON.stringify(v);
94
+ }
95
+ catch {
96
+ return '';
97
+ }
98
+ };
99
+ // ---------------------------------------------------------------------------
100
+ // \lo_list / \lo_list+
101
+ // ---------------------------------------------------------------------------
102
+ /**
103
+ * Render the result of `listLargeObjects` through the aligned printer.
104
+ * The query body itself lives in `describe/queries.ts` (WP-20) — we just
105
+ * dispatch to it and feed the result through `alignedPrinter` with the
106
+ * upstream "Large objects" title.
107
+ */
108
+ const runLoList = async (ctx, verbose) => {
109
+ const c = conn(ctx);
110
+ if (!c)
111
+ return noConn(ctx);
112
+ const query = listLargeObjects({ verbose, serverVersion: c.serverVersion });
113
+ try {
114
+ const rs = await c.query(query.sql, query.params);
115
+ const coerced = {
116
+ ...rs,
117
+ rows: rs.rows.map((row) => row.map((v) => v === null || v === undefined ? null : cellToString(v))),
118
+ };
119
+ const titleOverride = query.description ?? ctx.settings.popt.title;
120
+ const opts = {
121
+ ...ctx.settings.popt,
122
+ title: titleOverride,
123
+ topt: {
124
+ ...ctx.settings.popt.topt,
125
+ title: titleOverride ?? ctx.settings.popt.topt.title,
126
+ },
127
+ };
128
+ await alignedPrinter.printQuery(coerced, opts, process.stdout);
129
+ return { status: 'ok' };
130
+ }
131
+ catch (err) {
132
+ writeErr(`\\${ctx.cmdName}: ${errMsg(err)}\n`);
133
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
134
+ return { status: 'error' };
135
+ }
136
+ };
137
+ /** `\lo_list` — non-verbose listing. */
138
+ export const cmdLoList = {
139
+ name: 'lo_list',
140
+ helpKey: 'lo_list',
141
+ run: (ctx) => runLoList(ctx, false),
142
+ };
143
+ /** `\lo_list+` — verbose listing (adds Access privileges column). */
144
+ export const cmdLoListPlus = {
145
+ name: 'lo_list+',
146
+ helpKey: 'lo_list',
147
+ run: (ctx) => runLoList(ctx, true),
148
+ };
149
+ // ---------------------------------------------------------------------------
150
+ // \lo_import FILE [COMMENT]
151
+ // ---------------------------------------------------------------------------
152
+ /**
153
+ * `\lo_import FILE [COMMENT]`.
154
+ *
155
+ * Strategy:
156
+ * 1. `fs.readFile(file)` → Buffer.
157
+ * 2. `SELECT pg_catalog.lo_from_bytea(0, $1::bytea)` → new OID. The
158
+ * bytea param is the file's bytes serialized as `\x<hex>` text.
159
+ * 3. If COMMENT was supplied: `COMMENT ON LARGE OBJECT <oid> IS
160
+ * '<escaped>'` (single execSimple round-trip, escaped via the
161
+ * connection's `escapeLiteral`).
162
+ * 4. Print `lo_import <oid>\n` and set the `LASTOID` variable.
163
+ *
164
+ * Errors fall through to the standard `\lo_import: <msg>` diagnostic.
165
+ */
166
+ export const cmdLoImport = {
167
+ name: 'lo_import',
168
+ helpKey: 'lo_import',
169
+ run: async (ctx) => {
170
+ const c = conn(ctx);
171
+ if (!c)
172
+ return noConn(ctx);
173
+ const file = ctx.nextArg('normal');
174
+ if (file === null || file.length === 0) {
175
+ writeErr('\\lo_import: missing required argument\n');
176
+ ctx.settings.lastErrorResult = { message: 'missing required argument' };
177
+ return { status: 'error' };
178
+ }
179
+ // Comment is the next lexed slash-arg token, mirroring upstream
180
+ // `do_lo_import`'s second `psql_scan_slash_option(OT_NORMAL)`. Using the
181
+ // lexer (not the raw line) means it picks up the token AFTER the file —
182
+ // `restOfLine()` ignored the read cursor and re-included the filename
183
+ // itself in the comment.
184
+ const commentRaw = ctx.nextArg('normal');
185
+ const comment = commentRaw !== null && commentRaw.length > 0 ? commentRaw : null;
186
+ let bytes;
187
+ try {
188
+ bytes = await fsPromises.readFile(file);
189
+ }
190
+ catch (err) {
191
+ writeErr(`\\lo_import: ${errMsg(err)}\n`);
192
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
193
+ return { status: 'error' };
194
+ }
195
+ let oidStr;
196
+ try {
197
+ const rs = await c.query('SELECT pg_catalog.lo_from_bytea(0, $1::bytea)', [byteaText(bytes)]);
198
+ if (rs.rows.length === 0) {
199
+ throw new Error('lo_from_bytea returned no rows');
200
+ }
201
+ oidStr = cellToString(rs.rows[0][0]);
202
+ if (!/^\d+$/.test(oidStr)) {
203
+ throw new Error(`lo_from_bytea returned invalid oid: ${oidStr}`);
204
+ }
205
+ }
206
+ catch (err) {
207
+ writeErr(`\\lo_import: ${errMsg(err)}\n`);
208
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
209
+ return { status: 'error' };
210
+ }
211
+ if (comment !== null) {
212
+ try {
213
+ await c.execSimple(`COMMENT ON LARGE OBJECT ${oidStr} IS ${c.escapeLiteral(comment)}`);
214
+ }
215
+ catch (err) {
216
+ writeErr(`\\lo_import: ${errMsg(err)}\n`);
217
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
218
+ return { status: 'error' };
219
+ }
220
+ }
221
+ // Side effect: set LASTOID (matches upstream `do_lo_import`).
222
+ ctx.settings.vars.set('LASTOID', oidStr);
223
+ writeOut(`lo_import ${oidStr}\n`);
224
+ return { status: 'ok' };
225
+ },
226
+ };
227
+ // ---------------------------------------------------------------------------
228
+ // \lo_export OID FILE
229
+ // ---------------------------------------------------------------------------
230
+ /**
231
+ * `\lo_export OID FILE`.
232
+ *
233
+ * `SELECT pg_catalog.lo_get($1::oid)` returns a single-row, single-col
234
+ * result whose cell is the LO's bytes. We then `fs.writeFile` to the
235
+ * supplied path. The protocol layer decodes bytea text into a `Buffer`
236
+ * for us when the column oid is bytea; if we get a `\x...` string back
237
+ * we decode it explicitly.
238
+ *
239
+ * Print `lo_export\n` on success — matches upstream `do_lo_export`.
240
+ */
241
+ export const cmdLoExport = {
242
+ name: 'lo_export',
243
+ helpKey: 'lo_export',
244
+ run: async (ctx) => {
245
+ const c = conn(ctx);
246
+ if (!c)
247
+ return noConn(ctx);
248
+ const oidArg = ctx.nextArg('normal');
249
+ const file = ctx.nextArg('normal');
250
+ if (oidArg === null || file === null || file.length === 0) {
251
+ writeErr('\\lo_export: missing required argument\n');
252
+ ctx.settings.lastErrorResult = { message: 'missing required argument' };
253
+ return { status: 'error' };
254
+ }
255
+ const oid = parseOid(oidArg);
256
+ if (oid === null) {
257
+ writeErr(`\\lo_export: "${oidArg}" is not a valid large object OID\n`);
258
+ ctx.settings.lastErrorResult = { message: 'invalid OID' };
259
+ return { status: 'error' };
260
+ }
261
+ let bytes;
262
+ try {
263
+ const rs = await c.query('SELECT pg_catalog.lo_get($1::oid)', [oid]);
264
+ if (rs.rows.length === 0) {
265
+ throw new Error('lo_get returned no rows');
266
+ }
267
+ const cell = rs.rows[0][0];
268
+ bytes = coerceBytea(cell);
269
+ }
270
+ catch (err) {
271
+ writeErr(`\\lo_export: ${errMsg(err)}\n`);
272
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
273
+ return { status: 'error' };
274
+ }
275
+ try {
276
+ await fsPromises.writeFile(file, bytes);
277
+ }
278
+ catch (err) {
279
+ writeErr(`\\lo_export: ${errMsg(err)}\n`);
280
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
281
+ return { status: 'error' };
282
+ }
283
+ writeOut('lo_export\n');
284
+ return { status: 'ok' };
285
+ },
286
+ };
287
+ /**
288
+ * Decode a bytea cell coming back from the protocol. The connection may
289
+ * deliver:
290
+ * - a `Buffer` (decoded by a future binary-format path)
291
+ * - a `\x<hex>` string (text format, modern)
292
+ * - a legacy `octal-escape` string (text format, pre-9.0 servers; we
293
+ * don't generate this but it's the historical default).
294
+ */
295
+ const coerceBytea = (cell) => {
296
+ if (Buffer.isBuffer(cell))
297
+ return cell;
298
+ if (typeof cell !== 'string') {
299
+ throw new Error(`lo_get returned unexpected cell type: ${typeof cell}`);
300
+ }
301
+ if (cell.startsWith('\\x')) {
302
+ return Buffer.from(cell.slice(2), 'hex');
303
+ }
304
+ // Legacy octal-escape decode (`\\\\NNN` → byte, `\\\\` → `\\`, others
305
+ // pass through). Upstream `PQunescapeBytea` does the same.
306
+ const out = [];
307
+ let i = 0;
308
+ while (i < cell.length) {
309
+ if (cell[i] === '\\') {
310
+ if (cell[i + 1] === '\\') {
311
+ out.push(0x5c);
312
+ i += 2;
313
+ continue;
314
+ }
315
+ if (/^[0-7][0-7][0-7]$/.test(cell.slice(i + 1, i + 4))) {
316
+ out.push(parseInt(cell.slice(i + 1, i + 4), 8));
317
+ i += 4;
318
+ continue;
319
+ }
320
+ }
321
+ out.push(cell.charCodeAt(i));
322
+ i++;
323
+ }
324
+ return Buffer.from(out);
325
+ };
326
+ // ---------------------------------------------------------------------------
327
+ // \lo_unlink OID
328
+ // ---------------------------------------------------------------------------
329
+ /**
330
+ * `\lo_unlink OID` — drop a large object by OID. Implemented via
331
+ * `SELECT pg_catalog.lo_unlink($1::oid)`. The function returns `1` on
332
+ * success or raises an ERROR on missing OID; we just surface either
333
+ * outcome.
334
+ *
335
+ * Print `lo_unlink <oid>\n` on success.
336
+ */
337
+ export const cmdLoUnlink = {
338
+ name: 'lo_unlink',
339
+ helpKey: 'lo_unlink',
340
+ run: async (ctx) => {
341
+ const c = conn(ctx);
342
+ if (!c)
343
+ return noConn(ctx);
344
+ const oidArg = ctx.nextArg('normal');
345
+ if (oidArg === null) {
346
+ writeErr('\\lo_unlink: missing required argument\n');
347
+ ctx.settings.lastErrorResult = { message: 'missing required argument' };
348
+ return { status: 'error' };
349
+ }
350
+ const oid = parseOid(oidArg);
351
+ if (oid === null) {
352
+ writeErr(`\\lo_unlink: "${oidArg}" is not a valid large object OID\n`);
353
+ ctx.settings.lastErrorResult = { message: 'invalid OID' };
354
+ return { status: 'error' };
355
+ }
356
+ try {
357
+ await c.query('SELECT pg_catalog.lo_unlink($1::oid)', [oid]);
358
+ }
359
+ catch (err) {
360
+ writeErr(`\\lo_unlink: ${errMsg(err)}\n`);
361
+ ctx.settings.lastErrorResult = { message: errMsg(err) };
362
+ return { status: 'error' };
363
+ }
364
+ writeOut(`lo_unlink ${String(oid)}\n`);
365
+ return { status: 'ok' };
366
+ },
367
+ };
368
+ // ---------------------------------------------------------------------------
369
+ // Registration
370
+ // ---------------------------------------------------------------------------
371
+ /**
372
+ * Register the four large-object commands on the supplied registry.
373
+ * Called from `dispatch.ts::defaultRegistry()` (one new line).
374
+ *
375
+ * Note: `\lo_list` and `\lo_list+` shadow the existing `dl::lo_list`
376
+ * alias from `cmd_describe.ts` — the registry's lookup checks primary
377
+ * names before alias mappings, so this registration is the winning one.
378
+ */
379
+ export const registerLargeObjectCommands = (registry) => {
380
+ registry.register(cmdLoList);
381
+ registry.register(cmdLoListPlus);
382
+ registry.register(cmdLoImport);
383
+ registry.register(cmdLoExport);
384
+ registry.register(cmdLoUnlink);
385
+ };