@formo/cli 1.0.0 → 1.0.2

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.
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ /**
3
+ * SQL helpers for the query command.
4
+ *
5
+ * The Formo query API wraps the SQL you submit so it can paginate the result
6
+ * set and force a machine-readable response:
7
+ *
8
+ * SELECT * FROM (<your query>) LIMIT <n> FORMAT JSON
9
+ *
10
+ * ClickHouse does not allow a `FORMAT` clause inside a subquery, so if your
11
+ * query ends in its own `FORMAT` clause the wrapped statement becomes:
12
+ *
13
+ * SELECT * FROM (SELECT ... FORMAT CSV) LIMIT 100 FORMAT JSON
14
+ *
15
+ * which ClickHouse rejects with a 400. A trailing `FORMAT` (or a trailing
16
+ * semicolon) can never take effect through this endpoint anyway — the outer
17
+ * `FORMAT JSON` always wins, and output shaping is the CLI's `--format` job —
18
+ * so we remove it before sending and let the server wrap a clean query.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.stripTrailingFormatClause = stripTrailingFormatClause;
22
+ /**
23
+ * Strip a trailing, top-level `FORMAT <name>` clause and any trailing
24
+ * semicolons from a SQL statement.
25
+ *
26
+ * The scan is aware of string literals, quoted identifiers, and comments, so
27
+ * `FORMAT`-looking text inside them is never mistaken for a real clause. The
28
+ * match is anchored to the end of the statement, so a `FORMAT` nested inside
29
+ * parentheses (a subquery) or part of an identifier/function such as
30
+ * `formatDateTime(...)` — or a column aliased `format` — is left untouched.
31
+ *
32
+ * Returns the original input unchanged when there is nothing to strip.
33
+ */
34
+ function stripTrailingFormatClause(sql) {
35
+ if (!sql)
36
+ return sql;
37
+ const masked = maskLiterals(sql);
38
+ // A genuine trailing FORMAT clause: the keyword `format` preceded by a word
39
+ // boundary (so `formatDateTime` or a column aliased `format` is safe),
40
+ // followed by exactly one identifier (the format name) and nothing else.
41
+ const formatClause = /(^|[^A-Za-z0-9_])format\s+[A-Za-z_][A-Za-z0-9_]*\s*$/i;
42
+ let end = sql.length;
43
+ let didStrip = false;
44
+ // Peel top-level semicolons and a trailing FORMAT clause repeatedly so any
45
+ // ordering collapses to the bare query, e.g. `... FORMAT CSV;`,
46
+ // `...; FORMAT CSV`, or `... FORMAT CSV ;`. Trailing whitespace and comments
47
+ // are only ever skipped to *look* past them — never removed on their own.
48
+ for (;;) {
49
+ let e = end;
50
+ while (e > 0 && /\s/.test(masked[e - 1]))
51
+ e--;
52
+ if (e === 0)
53
+ break;
54
+ if (masked[e - 1] === ';') {
55
+ end = e - 1;
56
+ didStrip = true;
57
+ continue;
58
+ }
59
+ const match = formatClause.exec(masked.slice(0, e));
60
+ if (match) {
61
+ // Cut at the `format` keyword, after the leading word-boundary char.
62
+ end = match.index + match[1].length;
63
+ didStrip = true;
64
+ continue;
65
+ }
66
+ break;
67
+ }
68
+ if (!didStrip)
69
+ return sql;
70
+ // Tidy the real whitespace now left dangling where the clause used to be.
71
+ // Comments are not whitespace, so any genuine comment is preserved.
72
+ const stripped = sql.slice(0, end).replace(/\s+$/, '');
73
+ // Never manufacture an empty query from non-empty input (degenerate inputs
74
+ // such as a bare `FORMAT JSON`): let the original surface its own error.
75
+ return stripped === '' ? sql : stripped;
76
+ }
77
+ /**
78
+ * Return a copy of `sql` with the *contents* of string literals, quoted
79
+ * identifiers, and comments replaced by spaces, preserving the original length
80
+ * so indices stay aligned with the source. Quote delimiters are kept; comments
81
+ * are blanked entirely. This lets the clause scanner reason about real SQL code
82
+ * without tripping over keywords that merely appear inside literals or comments.
83
+ */
84
+ function maskLiterals(sql) {
85
+ const out = [];
86
+ const n = sql.length;
87
+ let i = 0;
88
+ while (i < n) {
89
+ const c = sql[i];
90
+ const next = i + 1 < n ? sql[i + 1] : '';
91
+ // Line comment: -- ... or # ... to end of line. ClickHouse accepts `#`
92
+ // and `#!` line comments for MySQL compatibility.
93
+ if ((c === '-' && next === '-') || c === '#') {
94
+ while (i < n && sql[i] !== '\n') {
95
+ out.push(' ');
96
+ i++;
97
+ }
98
+ continue;
99
+ }
100
+ // Block comment: /* ... */
101
+ if (c === '/' && next === '*') {
102
+ out.push(' ', ' ');
103
+ i += 2;
104
+ while (i < n && !(sql[i] === '*' && sql[i + 1] === '/')) {
105
+ out.push(' ');
106
+ i++;
107
+ }
108
+ if (i < n) {
109
+ out.push(' ', ' ');
110
+ i += 2;
111
+ }
112
+ continue;
113
+ }
114
+ // String literal or quoted identifier: '...', "...", `...`
115
+ if (c === "'" || c === '"' || c === '`') {
116
+ const quote = c;
117
+ out.push(quote);
118
+ i++;
119
+ while (i < n) {
120
+ const d = sql[i];
121
+ // Backslash escapes are honored inside single-quoted strings.
122
+ if (d === '\\' && quote === "'") {
123
+ out.push(' ');
124
+ i++;
125
+ if (i < n) {
126
+ out.push(' ');
127
+ i++;
128
+ }
129
+ continue;
130
+ }
131
+ if (d === quote) {
132
+ // A doubled delimiter is an escaped quote, not the terminator.
133
+ if (i + 1 < n && sql[i + 1] === quote) {
134
+ out.push(' ', ' ');
135
+ i += 2;
136
+ continue;
137
+ }
138
+ out.push(quote);
139
+ i++;
140
+ break;
141
+ }
142
+ out.push(' ');
143
+ i++;
144
+ }
145
+ continue;
146
+ }
147
+ out.push(c);
148
+ i++;
149
+ }
150
+ return out.join('');
151
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "packageManager": "pnpm@11.1.2",
5
5
  "description": "Formo API CLI — query profiles and analytics data",
6
6
  "repository": {