@sola-air-ui/relay 2.0.0 → 2.0.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +46 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sola-air-ui/relay",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Sola Relay — local data proxy that keeps credentials on-device and streams live data to Sola components",
5
5
  "type": "module",
6
6
  "bin": {
package/src/index.js CHANGED
@@ -9,18 +9,55 @@ const { version: RELAY_VERSION } = JSON.parse(
9
9
  readFileSync(new URL('../package.json', import.meta.url), 'utf8')
10
10
  );
11
11
 
12
+ // Trims whitespace and any run of trailing semicolons (with whitespace between).
13
+ function trimTrailingSemicolons(sql) {
14
+ let end = sql.length;
15
+ while (end > 0 && (sql[end - 1] === ';' || /\s/.test(sql[end - 1]))) end--;
16
+ return sql.slice(0, end).trim();
17
+ }
18
+
19
+ // Replaces string literals with empty ones ('' or "") and comments with a space,
20
+ // in one pass so a quote inside a comment or `--` inside a string is read right.
21
+ // Returns '' for an unterminated string or block comment, which callers reject.
22
+ function stripSqlLiterals(sql) {
23
+ let out = '';
24
+ let i = 0;
25
+ while (i < sql.length) {
26
+ const c = sql[i];
27
+ if (c === "'" || c === '"') {
28
+ let j = i + 1;
29
+ for (;;) {
30
+ if (j >= sql.length) return '';
31
+ if (sql[j] === '\\') { j += 2; continue; }
32
+ if (sql[j] === c) {
33
+ if (sql[j + 1] === c) { j += 2; continue; }
34
+ break;
35
+ }
36
+ j++;
37
+ }
38
+ out += c + c;
39
+ i = j + 1;
40
+ } else if (c === '-' && sql[i + 1] === '-') {
41
+ out += ' ';
42
+ while (i < sql.length && sql[i] !== '\n' && sql[i] !== '\r') i++;
43
+ } else if (c === '/' && sql[i + 1] === '*') {
44
+ const close = sql.indexOf('*/', i + 2);
45
+ if (close < 0) return '';
46
+ out += ' ';
47
+ i = close + 2;
48
+ } else {
49
+ out += c;
50
+ i++;
51
+ }
52
+ }
53
+ return out.trim();
54
+ }
55
+
12
56
  // Only single-statement read-only SELECT queries are permitted on SQL connectors.
13
57
  export function isReadOnlySql(sql) {
14
58
  if (typeof sql !== 'string') return false;
15
- const trimmed = sql.trim().replace(/;+\s*$/, '');
16
- if (!trimmed) return false;
17
- const withoutStrings = trimmed
18
- .replace(/'(?:''|\\.|[^'])*'/g, "''")
19
- .replace(/"(?:""|\\.|[^"])*"/g, '""');
20
- const withoutComments = withoutStrings
21
- .replace(/--[^\r\n]*/g, ' ')
22
- .replace(/\/\*[\s\S]*?\*\//g, ' ')
23
- .trim();
59
+ const withoutComments = stripSqlLiterals(trimTrailingSemicolons(sql));
60
+ if (!withoutComments) return false;
24
61
  if (!/^SELECT\b/i.test(withoutComments)) return false;
25
62
  if (withoutComments.includes(';')) return false;
26
63
  // INTO covers SELECT ... INTO, which creates a table (PostgreSQL) or writes a file