neonctl 2.22.2 → 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.
- package/README.md +84 -0
- package/analytics.js +5 -2
- package/commands/branches.js +9 -1
- package/commands/connection_string.js +9 -1
- package/commands/functions.js +277 -0
- package/commands/index.js +4 -0
- package/commands/neon_auth.js +1013 -0
- package/commands/projects.js +9 -1
- package/commands/psql.js +6 -1
- package/functions_api.js +44 -0
- package/package.json +15 -5
- package/psql/cli.js +51 -0
- package/psql/command/cmd_cond.js +437 -0
- package/psql/command/cmd_connect.js +815 -0
- package/psql/command/cmd_copy.js +1025 -0
- package/psql/command/cmd_describe.js +1810 -0
- package/psql/command/cmd_format.js +909 -0
- package/psql/command/cmd_io.js +2187 -0
- package/psql/command/cmd_lo.js +385 -0
- package/psql/command/cmd_meta.js +970 -0
- package/psql/command/cmd_misc.js +187 -0
- package/psql/command/cmd_pipeline.js +1141 -0
- package/psql/command/cmd_restrict.js +171 -0
- package/psql/command/cmd_show.js +751 -0
- package/psql/command/dispatch.js +343 -0
- package/psql/command/inputQueue.js +42 -0
- package/psql/command/shared.js +71 -0
- package/psql/complete/filenames.js +139 -0
- package/psql/complete/index.js +104 -0
- package/psql/complete/matcher.js +314 -0
- package/psql/complete/psqlVars.js +247 -0
- package/psql/complete/queries.js +491 -0
- package/psql/complete/rules.js +2387 -0
- package/psql/core/common.js +1250 -0
- package/psql/core/help.js +576 -0
- package/psql/core/mainloop.js +1353 -0
- package/psql/core/prompt.js +437 -0
- package/psql/core/settings.js +684 -0
- package/psql/core/sqlHelp.js +1066 -0
- package/psql/core/startup.js +840 -0
- package/psql/core/syncVars.js +116 -0
- package/psql/core/variables.js +287 -0
- package/psql/describe/formatters.js +1277 -0
- package/psql/describe/processNamePattern.js +270 -0
- package/psql/describe/queries.js +2373 -0
- package/psql/describe/versionGate.js +43 -0
- package/psql/index.js +2005 -0
- package/psql/io/history.js +299 -0
- package/psql/io/input.js +120 -0
- package/psql/io/lineEditor/buffer.js +323 -0
- package/psql/io/lineEditor/complete.js +227 -0
- package/psql/io/lineEditor/filename.js +159 -0
- package/psql/io/lineEditor/index.js +891 -0
- package/psql/io/lineEditor/keymap.js +738 -0
- package/psql/io/lineEditor/vt100.js +363 -0
- package/psql/io/pgpass.js +202 -0
- package/psql/io/pgservice.js +194 -0
- package/psql/io/psqlrc.js +422 -0
- package/psql/print/aligned.js +1756 -0
- package/psql/print/asciidoc.js +248 -0
- package/psql/print/crosstab.js +460 -0
- package/psql/print/csv.js +92 -0
- package/psql/print/html.js +258 -0
- package/psql/print/json.js +96 -0
- package/psql/print/latex.js +396 -0
- package/psql/print/pager.js +265 -0
- package/psql/print/troff.js +258 -0
- package/psql/print/unaligned.js +118 -0
- package/psql/print/units.js +135 -0
- package/psql/scanner/slash.js +513 -0
- package/psql/scanner/sql.js +910 -0
- package/psql/scanner/stringutils.js +390 -0
- package/psql/types/backslash.js +1 -0
- package/psql/types/connection.js +1 -0
- package/psql/types/index.js +7 -0
- package/psql/types/printer.js +1 -0
- package/psql/types/repl.js +1 -0
- package/psql/types/scanner.js +24 -0
- package/psql/types/settings.js +1 -0
- package/psql/types/variables.js +1 -0
- package/psql/wire/connection.js +2844 -0
- package/psql/wire/copy.js +108 -0
- package/psql/wire/notify.js +59 -0
- package/psql/wire/pipeline.js +519 -0
- package/psql/wire/protocol.js +466 -0
- package/psql/wire/sasl.js +296 -0
- package/psql/wire/tls.js +596 -0
- package/test_utils/fixtures.js +1 -0
- package/utils/esbuild.js +147 -0
- package/utils/psql.js +107 -11
- package/utils/zip.js +4 -0
- package/writer.js +1 -1
- package/commands/auth.test.js +0 -211
- package/commands/branches.test.js +0 -460
- package/commands/checkout.test.js +0 -170
- package/commands/connection_string.test.js +0 -196
- package/commands/data_api.test.js +0 -169
- package/commands/databases.test.js +0 -39
- package/commands/help.test.js +0 -9
- package/commands/init.test.js +0 -56
- package/commands/ip_allow.test.js +0 -59
- package/commands/link.test.js +0 -381
- package/commands/operations.test.js +0 -7
- package/commands/orgs.test.js +0 -7
- package/commands/projects.test.js +0 -144
- package/commands/psql.test.js +0 -49
- package/commands/roles.test.js +0 -37
- package/commands/set_context.test.js +0 -159
- package/commands/vpc_endpoints.test.js +0 -69
- package/context.test.js +0 -119
- package/env.test.js +0 -55
- package/utils/formats.test.js +0 -32
- package/writer.test.js +0 -104
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* psql backslash-command argument scanner.
|
|
3
|
+
*
|
|
4
|
+
* Hand-port of PostgreSQL's `src/bin/psql/psqlscanslash.l`. The upstream is a
|
|
5
|
+
* flex-generated state machine with these exclusive states:
|
|
6
|
+
*
|
|
7
|
+
* - `xslashcmd` — reading the command name (the letters after `\`)
|
|
8
|
+
* - `xslashargstart` — skipping whitespace before the next arg; `|` at
|
|
9
|
+
* this position is special in `filepipe` mode
|
|
10
|
+
* - `xslasharg` — reading an unquoted arg (handles `:var`,
|
|
11
|
+
* `:'var'`, `:"var"` substitutions and the start of
|
|
12
|
+
* `'`, `"`, `` ` `` quoted runs)
|
|
13
|
+
* - `xslashquote` — inside `'…'` (C-string-style escapes processed)
|
|
14
|
+
* - `xslashbackquote` — inside `` `…` `` (variable expansion only; the
|
|
15
|
+
* body is shipped to the shell by upstream)
|
|
16
|
+
* - `xslashdquote` — inside `"…"` (literal copy, double quotes kept)
|
|
17
|
+
* - `xslashwholeline` — slurp rest of line, suppressing leading whitespace
|
|
18
|
+
* - `xslashend` — terminator (we don't model it here; the caller
|
|
19
|
+
* knows where the slash command ended)
|
|
20
|
+
*
|
|
21
|
+
* The TS port collapses these into a single {@link scanSlashArgs} function
|
|
22
|
+
* that takes the post-command-name remainder of the input line plus a
|
|
23
|
+
* {@link SlashArgMode} and returns the list of parsed arguments. We hand-roll
|
|
24
|
+
* the state machine instead of attempting to mechanically translate flex
|
|
25
|
+
* rules; the resulting code is easier to read and trivially testable.
|
|
26
|
+
*
|
|
27
|
+
* Behavioural notes vs upstream:
|
|
28
|
+
*
|
|
29
|
+
* - **Whole-line mode** returns a single-element array containing the entire
|
|
30
|
+
* rest-of-line, with leading whitespace suppressed. Empty input still
|
|
31
|
+
* yields `[]` so callers can treat the result uniformly.
|
|
32
|
+
* - **filepipe mode** treats a leading `|` as the start of a shell command
|
|
33
|
+
* and slurps the rest of the line as one argument. Anything else is
|
|
34
|
+
* handled as a normal arg.
|
|
35
|
+
* - **Variable substitution** matches upstream's three forms:
|
|
36
|
+
* `:varname` — plain expansion
|
|
37
|
+
* `:'varname'` — SQL-literal-quoted expansion
|
|
38
|
+
* `:"varname"` — SQL-identifier-quoted expansion
|
|
39
|
+
* `varname` is `[A-Za-z0-9_\x80-\xff]+` (upstream's `variable_char`). When
|
|
40
|
+
* the variable is unset, the colon form is emitted literally — matching
|
|
41
|
+
* the upstream `ECHO` fallback.
|
|
42
|
+
* - **no-vars mode** disables all `:var` substitution; the lexer emits the
|
|
43
|
+
* raw text. Useful for commands that should never expand variables
|
|
44
|
+
* (e.g. `\setenv`'s value argument).
|
|
45
|
+
* - **sql-id / sql-id-keep-case modes** post-process each arg through
|
|
46
|
+
* `dequoteDowncaseIdentifier`, which mirrors upstream's
|
|
47
|
+
* `dequote_downcase_identifier()`: collapse `"…"` quoting, double `""`
|
|
48
|
+
* into a single `"`, and (for `sql-id`) lowercase unquoted letters.
|
|
49
|
+
* - **Backticks** ARE executed here, synchronously via `child_process.execSync`
|
|
50
|
+
* on `sh -c <body>`. Variable references inside the backticked body are
|
|
51
|
+
* expanded first (matching upstream's `xslashbackquote` rules), then the
|
|
52
|
+
* resulting command is run with the inherited environment but NO shell
|
|
53
|
+
* state; the child's stdout (trimmed of one trailing newline) is the
|
|
54
|
+
* arg's value. Non-zero exits / spawn failures are reported on stderr
|
|
55
|
+
* in the upstream `psql:...: error: \!:` shape and substitute the empty
|
|
56
|
+
* string. This is the scanner-level analogue of upstream's `evaluate_backtick`.
|
|
57
|
+
* - **Inside-quote escapes** match upstream `xslashquote`: `\n \t \b \r \f`,
|
|
58
|
+
* octal `\ooo`, hex `\xhh`, and `\<other>` as a literal character. We
|
|
59
|
+
* apply them in-line so the returned arg contains the decoded value.
|
|
60
|
+
*/
|
|
61
|
+
import { execSync } from 'node:child_process';
|
|
62
|
+
import { dequote } from './stringutils.js';
|
|
63
|
+
const WHITESPACE = ' \t\n\r\f\v';
|
|
64
|
+
const VARIABLE_CHAR_RE = /[A-Za-z0-9_\x80-\xff]/;
|
|
65
|
+
const isVarChar = (c) => c !== undefined && VARIABLE_CHAR_RE.test(c);
|
|
66
|
+
const isWhitespace = (c) => c !== undefined && WHITESPACE.includes(c);
|
|
67
|
+
/**
|
|
68
|
+
* SQL-literal-quote a value for the `:'varname'` substitution form.
|
|
69
|
+
* Mirrors libpq's `PQescapeLiteral` for the common case: wrap in `'…'`,
|
|
70
|
+
* double any embedded `'`, and backslash-escape any embedded `\`. Upstream
|
|
71
|
+
* additionally emits an `E` prefix when the value contains backslashes; we
|
|
72
|
+
* preserve that behaviour for compatibility with code that round-trips
|
|
73
|
+
* through the SQL parser.
|
|
74
|
+
*/
|
|
75
|
+
const quoteSqlLiteral = (value) => {
|
|
76
|
+
let needsEscape = false;
|
|
77
|
+
let inner = '';
|
|
78
|
+
for (const c of value) {
|
|
79
|
+
if (c === "'")
|
|
80
|
+
inner += "''";
|
|
81
|
+
else if (c === '\\') {
|
|
82
|
+
inner += '\\\\';
|
|
83
|
+
needsEscape = true;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
inner += c;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return needsEscape ? `E'${inner}'` : `'${inner}'`;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* SQL-identifier-quote a value for the `:"varname"` substitution form.
|
|
93
|
+
* Wraps the value in `"…"` and doubles any embedded `"`.
|
|
94
|
+
*/
|
|
95
|
+
const quoteSqlIdent = (value) => {
|
|
96
|
+
let inner = '';
|
|
97
|
+
for (const c of value) {
|
|
98
|
+
inner += c === '"' ? '""' : c;
|
|
99
|
+
}
|
|
100
|
+
return `"${inner}"`;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Match upstream's `dequote_downcase_identifier()`. Strips out `"…"` quoting
|
|
104
|
+
* (collapsing `""` to `"` inside quotes) and optionally downcases unquoted
|
|
105
|
+
* letters. The transformation is in-place semantically: a string like
|
|
106
|
+
* `FOO"BAR"BAZ` becomes `fooBARbaz` (when `downcase`) or `FOOBARBAZ`
|
|
107
|
+
* (otherwise).
|
|
108
|
+
*/
|
|
109
|
+
const dequoteDowncaseIdentifier = (str, downcase) => {
|
|
110
|
+
let out = '';
|
|
111
|
+
let inquotes = false;
|
|
112
|
+
let i = 0;
|
|
113
|
+
while (i < str.length) {
|
|
114
|
+
const c = str[i];
|
|
115
|
+
if (c === '"') {
|
|
116
|
+
if (inquotes && str[i + 1] === '"') {
|
|
117
|
+
// Keep one quote, drop the other.
|
|
118
|
+
out += '"';
|
|
119
|
+
i += 2;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
inquotes = !inquotes;
|
|
123
|
+
i++;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
out += downcase && !inquotes ? c.toLowerCase() : c;
|
|
127
|
+
i++;
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Attempt to consume one of the `:var`, `:'var'`, `:"var"` variable
|
|
133
|
+
* substitution forms at position `i` in `s`. Returns the new index plus the
|
|
134
|
+
* substituted text, or `null` if no recognised form is present.
|
|
135
|
+
*
|
|
136
|
+
* Caller controls whether the colon forms are honoured at all via
|
|
137
|
+
* `varLookup`: pass `undefined` to disable substitution entirely (`no-vars`
|
|
138
|
+
* mode).
|
|
139
|
+
*/
|
|
140
|
+
const tryConsumeVarSubstitution = (s, i, varLookup) => {
|
|
141
|
+
if (varLookup === undefined)
|
|
142
|
+
return null;
|
|
143
|
+
if (s[i] !== ':')
|
|
144
|
+
return null;
|
|
145
|
+
// :{?varname} — defined-variable test, emits literal TRUE / FALSE.
|
|
146
|
+
// Mirrors upstream `psqlscanslash.l`'s `:\{\?{variable_char}+\}` rule
|
|
147
|
+
// (calls `psqlscan_test_variable`). A malformed expression (missing
|
|
148
|
+
// closing `}` or empty name) falls through to `null` so the caller emits
|
|
149
|
+
// the literal `:` and continues.
|
|
150
|
+
if (s[i + 1] === '{' && s[i + 2] === '?') {
|
|
151
|
+
let j = i + 3;
|
|
152
|
+
while (j < s.length && isVarChar(s[j]))
|
|
153
|
+
j++;
|
|
154
|
+
if (j > i + 3 && s[j] === '}') {
|
|
155
|
+
const name = s.slice(i + 3, j);
|
|
156
|
+
const value = varLookup(name);
|
|
157
|
+
return { end: j + 1, text: value !== undefined ? 'TRUE' : 'FALSE' };
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
// :"varname" — SQL identifier quote
|
|
162
|
+
if (s[i + 1] === '"') {
|
|
163
|
+
let j = i + 2;
|
|
164
|
+
while (j < s.length && isVarChar(s[j]))
|
|
165
|
+
j++;
|
|
166
|
+
if (j > i + 2 && s[j] === '"') {
|
|
167
|
+
const name = s.slice(i + 2, j);
|
|
168
|
+
const value = varLookup(name);
|
|
169
|
+
if (value === undefined) {
|
|
170
|
+
// Upstream still substitutes — passing an empty string would quietly
|
|
171
|
+
// misparse downstream. We instead pass through the literal so the
|
|
172
|
+
// caller can see (and report) the unset reference. This matches the
|
|
173
|
+
// ECHO fallback used by upstream's plain `:varname` form.
|
|
174
|
+
return { end: j + 1, text: s.slice(i, j + 1) };
|
|
175
|
+
}
|
|
176
|
+
return { end: j + 1, text: quoteSqlIdent(value) };
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
// :'varname' — SQL literal quote
|
|
181
|
+
if (s[i + 1] === "'") {
|
|
182
|
+
let j = i + 2;
|
|
183
|
+
while (j < s.length && isVarChar(s[j]))
|
|
184
|
+
j++;
|
|
185
|
+
if (j > i + 2 && s[j] === "'") {
|
|
186
|
+
const name = s.slice(i + 2, j);
|
|
187
|
+
const value = varLookup(name);
|
|
188
|
+
if (value === undefined) {
|
|
189
|
+
return { end: j + 1, text: s.slice(i, j + 1) };
|
|
190
|
+
}
|
|
191
|
+
return { end: j + 1, text: quoteSqlLiteral(value) };
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
// :varname — plain substitution
|
|
196
|
+
if (isVarChar(s[i + 1])) {
|
|
197
|
+
let j = i + 1;
|
|
198
|
+
while (j < s.length && isVarChar(s[j]))
|
|
199
|
+
j++;
|
|
200
|
+
const name = s.slice(i + 1, j);
|
|
201
|
+
const value = varLookup(name);
|
|
202
|
+
if (value === undefined) {
|
|
203
|
+
// Unset → emit literally so it stays visible. Upstream ECHOes the
|
|
204
|
+
// entire `:name` text in this case.
|
|
205
|
+
return { end: j, text: s.slice(i, j) };
|
|
206
|
+
}
|
|
207
|
+
return { end: j, text: value };
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Process the contents of a `'…'` slash-quoted token: handle psql's C-style
|
|
213
|
+
* escapes (\n, \t, \b, \r, \f, octal, hex, and \<other>) and undouble `''`.
|
|
214
|
+
* The opening quote has already been consumed; we read until the matching
|
|
215
|
+
* closing quote and return the decoded payload plus the new index (pointing
|
|
216
|
+
* just past the closing quote).
|
|
217
|
+
*/
|
|
218
|
+
const consumeSingleQuoted = (s, start) => {
|
|
219
|
+
let out = '';
|
|
220
|
+
let i = start;
|
|
221
|
+
while (i < s.length) {
|
|
222
|
+
const c = s[i];
|
|
223
|
+
if (c === "'") {
|
|
224
|
+
if (s[i + 1] === "'") {
|
|
225
|
+
out += "'";
|
|
226
|
+
i += 2;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
return { end: i + 1, text: out };
|
|
230
|
+
}
|
|
231
|
+
if (c === '\\' && i + 1 < s.length) {
|
|
232
|
+
const next = s[i + 1];
|
|
233
|
+
if (next === 'n') {
|
|
234
|
+
out += '\n';
|
|
235
|
+
i += 2;
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (next === 't') {
|
|
239
|
+
out += '\t';
|
|
240
|
+
i += 2;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (next === 'b') {
|
|
244
|
+
out += '\b';
|
|
245
|
+
i += 2;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (next === 'r') {
|
|
249
|
+
out += '\r';
|
|
250
|
+
i += 2;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (next === 'f') {
|
|
254
|
+
out += '\f';
|
|
255
|
+
i += 2;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
// Octal: \ooo (1–3 digits)
|
|
259
|
+
if (next >= '0' && next <= '7') {
|
|
260
|
+
const j = i + 1;
|
|
261
|
+
let octEnd = j;
|
|
262
|
+
while (octEnd < s.length &&
|
|
263
|
+
octEnd - j < 3 &&
|
|
264
|
+
s[octEnd] >= '0' &&
|
|
265
|
+
s[octEnd] <= '7') {
|
|
266
|
+
octEnd++;
|
|
267
|
+
}
|
|
268
|
+
const code = parseInt(s.slice(j, octEnd), 8);
|
|
269
|
+
out += String.fromCharCode(code);
|
|
270
|
+
i = octEnd;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
// Hex: \xhh (1–2 digits)
|
|
274
|
+
if (next === 'x') {
|
|
275
|
+
const j = i + 2;
|
|
276
|
+
const hexRe = /[0-9a-fA-F]/;
|
|
277
|
+
let hexEnd = j;
|
|
278
|
+
while (hexEnd < s.length && hexEnd - j < 2 && hexRe.test(s[hexEnd])) {
|
|
279
|
+
hexEnd++;
|
|
280
|
+
}
|
|
281
|
+
if (hexEnd > j) {
|
|
282
|
+
const code = parseInt(s.slice(j, hexEnd), 16);
|
|
283
|
+
out += String.fromCharCode(code);
|
|
284
|
+
i = hexEnd;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// \<other> → literal next char
|
|
289
|
+
out += next;
|
|
290
|
+
i += 2;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
out += c;
|
|
294
|
+
i++;
|
|
295
|
+
}
|
|
296
|
+
// Unterminated — return what we have. Upstream reports an error; for the
|
|
297
|
+
// scanner-as-library shape we'd rather surface the partial text and let
|
|
298
|
+
// the caller decide. Tests cover both well-formed and unterminated cases.
|
|
299
|
+
return { end: i, text: out };
|
|
300
|
+
};
|
|
301
|
+
/**
|
|
302
|
+
* Process the contents of a `"…"` slash-quoted token. Upstream copies the
|
|
303
|
+
* body verbatim *including the double quotes themselves* (see `xslashdquote`
|
|
304
|
+
* rule, which ECHOes the opening dquote on entry). That preserves
|
|
305
|
+
* SQL-identifier semantics — the caller's `dequoteDowncaseIdentifier()` is
|
|
306
|
+
* what eventually unwraps the quotes for `sql-id` modes.
|
|
307
|
+
*/
|
|
308
|
+
const consumeDoubleQuoted = (s, start) => {
|
|
309
|
+
let i = start;
|
|
310
|
+
while (i < s.length) {
|
|
311
|
+
if (s[i] === '"') {
|
|
312
|
+
return { end: i + 1, text: s.slice(start - 1, i + 1) };
|
|
313
|
+
}
|
|
314
|
+
i++;
|
|
315
|
+
}
|
|
316
|
+
// Unterminated — return what we have, including the opening quote.
|
|
317
|
+
return { end: i, text: s.slice(start - 1, i) };
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
320
|
+
* Test seam for swapping the shell executor. Vitest sets `current` to its
|
|
321
|
+
* own mock; production uses `execSync(cmd, { shell: '/bin/sh' })`. Kept as
|
|
322
|
+
* an exported object so tests can flip it in `beforeEach` without monkey-
|
|
323
|
+
* patching `child_process`.
|
|
324
|
+
*/
|
|
325
|
+
export const BACKTICK_EXECUTOR = {
|
|
326
|
+
current: (cmd) => execSync(cmd, {
|
|
327
|
+
shell: '/bin/sh',
|
|
328
|
+
encoding: 'utf8',
|
|
329
|
+
// Children inherit the parent env but get no stdin pipe — matches
|
|
330
|
+
// upstream's `popen(cmd, "r")` semantics. stderr passes through so
|
|
331
|
+
// shell error output is visible to the user.
|
|
332
|
+
stdio: ['ignore', 'pipe', 'inherit'],
|
|
333
|
+
// Defensive cap: backtick output goes into a slash arg, so a runaway
|
|
334
|
+
// command shouldn't be able to fill arbitrary memory.
|
|
335
|
+
maxBuffer: 1 << 20,
|
|
336
|
+
}),
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* Execute the lexed body of a `` `…` `` token via `sh -c` and return its
|
|
340
|
+
* stdout with one trailing newline stripped (matching shell command-
|
|
341
|
+
* substitution convention). Errors are reported on stderr in the upstream
|
|
342
|
+
* `psql: error: \!: <command>: <message>` shape and substitute the empty
|
|
343
|
+
* string — so a failed backtick never aborts the surrounding slash command.
|
|
344
|
+
*
|
|
345
|
+
* Called only by {@link consumeBackQuoted}; lives at module scope so the
|
|
346
|
+
* scanner stays free of inline I/O and tests can spy on the executor via
|
|
347
|
+
* {@link BACKTICK_EXECUTOR}.
|
|
348
|
+
*/
|
|
349
|
+
const runBacktickCommand = (cmd) => {
|
|
350
|
+
if (cmd.length === 0)
|
|
351
|
+
return '';
|
|
352
|
+
try {
|
|
353
|
+
const out = BACKTICK_EXECUTOR.current(cmd);
|
|
354
|
+
// Trim a single trailing newline; preserve interior newlines so multi-line
|
|
355
|
+
// output (e.g. `\set FOO `cat file``) lands as-is.
|
|
356
|
+
return out.endsWith('\n') ? out.slice(0, -1) : out;
|
|
357
|
+
}
|
|
358
|
+
catch (err) {
|
|
359
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
360
|
+
// Upstream prints `psql:file:line: error: \!: <cmd>: <msg>`. We don't
|
|
361
|
+
// have file/line context in the scanner; emit the prefix verbatim and
|
|
362
|
+
// include the command for diagnosis.
|
|
363
|
+
process.stderr.write(`psql: error: \\!: ${cmd}: ${msg}\n`);
|
|
364
|
+
return '';
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
/**
|
|
368
|
+
* Process the contents of a `` `…` `` slash-backquoted token.
|
|
369
|
+
*
|
|
370
|
+
* Phase 1 (this function): consume the body, expanding `:var` references
|
|
371
|
+
* along the way (matching upstream's `xslashbackquote` rules).
|
|
372
|
+
*
|
|
373
|
+
* Phase 2 (delegated to {@link runBacktickCommand}): run the assembled
|
|
374
|
+
* command via `sh -c` and return its stdout.
|
|
375
|
+
*
|
|
376
|
+
* Returns the command's stdout (one trailing `\n` stripped). Unterminated
|
|
377
|
+
* backticks still execute the body so partial input doesn't silently
|
|
378
|
+
* succeed; tests cover both well-formed and unterminated cases.
|
|
379
|
+
*/
|
|
380
|
+
const consumeBackQuoted = (s, start, varLookup) => {
|
|
381
|
+
let inner = '';
|
|
382
|
+
let i = start;
|
|
383
|
+
while (i < s.length) {
|
|
384
|
+
const c = s[i];
|
|
385
|
+
if (c === '`') {
|
|
386
|
+
return { end: i + 1, text: runBacktickCommand(inner) };
|
|
387
|
+
}
|
|
388
|
+
const sub = tryConsumeVarSubstitution(s, i, varLookup);
|
|
389
|
+
if (sub !== null) {
|
|
390
|
+
inner += sub.text;
|
|
391
|
+
i = sub.end;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
inner += c;
|
|
395
|
+
i++;
|
|
396
|
+
}
|
|
397
|
+
// Unterminated — still run what we accumulated so the user can see the
|
|
398
|
+
// error from `sh` itself rather than silently dropping the command.
|
|
399
|
+
return { end: i, text: runBacktickCommand(inner) };
|
|
400
|
+
};
|
|
401
|
+
/**
|
|
402
|
+
* Lex a single slash-command argument starting at `s[i]`. Returns the parsed
|
|
403
|
+
* argument text and the index just past it, or `null` if no argument is
|
|
404
|
+
* available before end of input.
|
|
405
|
+
*/
|
|
406
|
+
const scanOneArg = (s, i, mode, varLookup) => {
|
|
407
|
+
// Skip leading whitespace (xslashargstart).
|
|
408
|
+
while (i < s.length && isWhitespace(s[i]))
|
|
409
|
+
i++;
|
|
410
|
+
if (i >= s.length)
|
|
411
|
+
return null;
|
|
412
|
+
// filepipe special: a leading `|` flips into whole-line mode for this arg.
|
|
413
|
+
if (mode === 'filepipe' && s[i] === '|') {
|
|
414
|
+
const rest = s.slice(i);
|
|
415
|
+
return { end: s.length, arg: rest };
|
|
416
|
+
}
|
|
417
|
+
// Accumulate the argument piece by piece. Each iteration consumes either:
|
|
418
|
+
// - a single-quoted run
|
|
419
|
+
// - a double-quoted run
|
|
420
|
+
// - a backticked run
|
|
421
|
+
// - a :var / :'var' / :"var" substitution
|
|
422
|
+
// - a literal character (the catch-all)
|
|
423
|
+
// We stop on whitespace or `\` (which begins the next slash command).
|
|
424
|
+
let out = '';
|
|
425
|
+
while (i < s.length) {
|
|
426
|
+
const c = s[i];
|
|
427
|
+
if (isWhitespace(c))
|
|
428
|
+
break;
|
|
429
|
+
if (c === '\\')
|
|
430
|
+
break;
|
|
431
|
+
if (c === "'") {
|
|
432
|
+
const r = consumeSingleQuoted(s, i + 1);
|
|
433
|
+
out += r.text;
|
|
434
|
+
i = r.end;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (c === '"') {
|
|
438
|
+
const r = consumeDoubleQuoted(s, i + 1);
|
|
439
|
+
out += r.text;
|
|
440
|
+
i = r.end;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (c === '`') {
|
|
444
|
+
const r = consumeBackQuoted(s, i + 1, varLookup);
|
|
445
|
+
out += r.text;
|
|
446
|
+
i = r.end;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const sub = tryConsumeVarSubstitution(s, i, varLookup);
|
|
450
|
+
if (sub !== null) {
|
|
451
|
+
out += sub.text;
|
|
452
|
+
i = sub.end;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
out += c;
|
|
456
|
+
i++;
|
|
457
|
+
}
|
|
458
|
+
return { end: i, arg: out };
|
|
459
|
+
};
|
|
460
|
+
/**
|
|
461
|
+
* Scan the argument portion of a backslash command.
|
|
462
|
+
*
|
|
463
|
+
* @param input the rest of the input line *after* the command name (e.g.
|
|
464
|
+
* `" foo 'bar baz'"` for `\echo foo 'bar baz'`)
|
|
465
|
+
* @param mode argument processing mode — see {@link SlashArgMode}
|
|
466
|
+
* @param varLookup callback that resolves `:varname` references. Omit (or
|
|
467
|
+
* pass `undefined`) for `no-vars` mode behaviour even when
|
|
468
|
+
* `mode !== 'no-vars'`.
|
|
469
|
+
*
|
|
470
|
+
* @returns array of parsed argument strings; empty input yields `[]`.
|
|
471
|
+
*/
|
|
472
|
+
export const scanSlashArgs = (input, mode, varLookup) => {
|
|
473
|
+
// Whole-line: return everything, with leading whitespace suppressed and a
|
|
474
|
+
// single trailing newline (if any) preserved verbatim. Empty (or
|
|
475
|
+
// whitespace-only) input yields no args.
|
|
476
|
+
if (mode === 'whole-line') {
|
|
477
|
+
let start = 0;
|
|
478
|
+
while (start < input.length && isWhitespace(input[start]))
|
|
479
|
+
start++;
|
|
480
|
+
if (start >= input.length)
|
|
481
|
+
return [];
|
|
482
|
+
return [input.slice(start)];
|
|
483
|
+
}
|
|
484
|
+
const effectiveLookup = mode === 'no-vars' ? undefined : varLookup;
|
|
485
|
+
const args = [];
|
|
486
|
+
let i = 0;
|
|
487
|
+
while (i < input.length) {
|
|
488
|
+
const result = scanOneArg(input, i, mode, effectiveLookup);
|
|
489
|
+
if (result === null)
|
|
490
|
+
break;
|
|
491
|
+
let arg = result.arg;
|
|
492
|
+
// sql-id / sql-id-keep-case post-process: collapse SQL-identifier
|
|
493
|
+
// quoting, optionally downcasing unquoted letters.
|
|
494
|
+
if (mode === 'sql-id') {
|
|
495
|
+
arg = dequoteDowncaseIdentifier(arg, true);
|
|
496
|
+
}
|
|
497
|
+
else if (mode === 'sql-id-keep-case') {
|
|
498
|
+
arg = dequoteDowncaseIdentifier(arg, false);
|
|
499
|
+
}
|
|
500
|
+
args.push(arg);
|
|
501
|
+
i = result.end;
|
|
502
|
+
// Consume the inter-arg whitespace so the next iteration starts cleanly.
|
|
503
|
+
while (i < input.length && isWhitespace(input[i]))
|
|
504
|
+
i++;
|
|
505
|
+
// Stop on a `\` — start of the next backslash command.
|
|
506
|
+
if (input[i] === '\\')
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
return args;
|
|
510
|
+
};
|
|
511
|
+
// Re-export `dequote` for callers that want to undo `quoteIfNeeded` on
|
|
512
|
+
// scanned args without reaching across modules.
|
|
513
|
+
export { dequote };
|