@blogic-cz/agent-tools 1.6.16 → 1.6.18

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,159 @@
1
+ /** Closed admission grammar, not an evaluator. Unknown syntax never acquires a proof. */
2
+ export function isPassiveJqFilter(filter: string): boolean {
3
+ if (filter.length > 65_536) return false;
4
+ if (new TextEncoder().encode(filter).byteLength > 65_536) return false;
5
+ const tokens: string[] = [];
6
+ const lexer =
7
+ /\s+|"(?:[^"\\\x00-\x1f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"|(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?|[A-Za-z_][A-Za-z0-9_]*|\/\/|==|!=|<=|>=|[.{}[\]():,?|+*/%<>-]/y; // eslint-disable-line eslint/no-control-regex -- JSON strings exclude literal control bytes.
8
+ let offset = 0;
9
+ while (offset < filter.length) {
10
+ lexer.lastIndex = offset;
11
+ const match = lexer.exec(filter);
12
+ if (!match) return false;
13
+ offset = lexer.lastIndex;
14
+ if (!/^\s/.test(match[0])) tokens.push(match[0]);
15
+ if (tokens.length > 4096) return false;
16
+ }
17
+ let position = 0;
18
+ const take = (token: string): boolean => {
19
+ if (tokens[position] !== token) return false;
20
+ position++;
21
+ return true;
22
+ };
23
+ const identifier = (token: string | undefined): boolean =>
24
+ /^[A-Za-z_][A-Za-z0-9_]*$/.test(token ?? "");
25
+ const string = (token: string | undefined): boolean => token?.startsWith('"') === true;
26
+ const number = (token: string | undefined): boolean => /^[0-9]/.test(token ?? "");
27
+ const noArgs = new Set([
28
+ "length",
29
+ "keys",
30
+ "keys_unsorted",
31
+ "type",
32
+ "values",
33
+ "empty",
34
+ "add",
35
+ "min",
36
+ "max",
37
+ "sort",
38
+ "unique",
39
+ "flatten",
40
+ "not",
41
+ "reverse",
42
+ "to_entries",
43
+ "from_entries",
44
+ ]);
45
+ const oneArg = new Set([
46
+ "map",
47
+ "map_values",
48
+ "select",
49
+ "contains",
50
+ "has",
51
+ "sort_by",
52
+ "group_by",
53
+ "unique_by",
54
+ "any",
55
+ "all",
56
+ "join",
57
+ ]);
58
+ const operators = new Set([
59
+ "|",
60
+ "//",
61
+ "==",
62
+ "!=",
63
+ "<",
64
+ ">",
65
+ "<=",
66
+ ">=",
67
+ "+",
68
+ "-",
69
+ "*",
70
+ "/",
71
+ "%",
72
+ "and",
73
+ "or",
74
+ ]);
75
+ const expression = (depth: number, commas = false): boolean => {
76
+ if (depth > 64 || !term(depth)) return false;
77
+ const nextOperator = () =>
78
+ operators.has(tokens[position] ?? "") || (commas && tokens[position] === ",");
79
+ while (nextOperator()) {
80
+ position++;
81
+ if (!term(depth)) return false;
82
+ }
83
+ return true;
84
+ };
85
+ const term = (depth: number): boolean => {
86
+ if (take("-")) return number(tokens[position]) && (position++, true);
87
+ if (take(".")) {
88
+ if (identifier(tokens[position]) || string(tokens[position])) position++;
89
+ } else if (take("(")) {
90
+ if (!expression(depth + 1, true) || !take(")")) return false;
91
+ } else if (take("[")) {
92
+ if (!take("]") && (!expression(depth + 1, true) || !take("]"))) return false;
93
+ } else if (take("{")) {
94
+ if (!take("}")) {
95
+ do {
96
+ const key = tokens[position];
97
+ if (!identifier(key) && !string(key)) return false;
98
+ position++;
99
+ if (take(":")) {
100
+ if (!expression(depth + 1)) return false;
101
+ } else if (!identifier(key)) return false;
102
+ } while (take(","));
103
+ if (!take("}")) return false;
104
+ }
105
+ } else {
106
+ const token = tokens[position];
107
+ if (string(token) || number(token) || ["true", "false", "null"].includes(token ?? ""))
108
+ position++;
109
+ else if (noArgs.has(token ?? "")) position++;
110
+ else if (oneArg.has(token ?? "")) {
111
+ position++;
112
+ if (!take("(") || !expression(depth + 1) || !take(")")) return false;
113
+ } else return false;
114
+ }
115
+ while (true) {
116
+ if (take("?")) continue;
117
+ if (take(".")) {
118
+ if (!identifier(tokens[position]) && !string(tokens[position])) return false;
119
+ position++;
120
+ } else if (take("[")) {
121
+ if (!take("]")) {
122
+ if (take("-")) {
123
+ if (!number(tokens[position])) return false;
124
+ position++;
125
+ } else if (string(tokens[position]) || number(tokens[position])) position++;
126
+ else return false;
127
+ if (!take("]")) return false;
128
+ }
129
+ } else break;
130
+ }
131
+ return true;
132
+ };
133
+ return expression(0, true) && position === tokens.length;
134
+ }
135
+
136
+ /** Only known formatting/input flags and one literal program, followed by file operands. */
137
+ export function passiveJqProgramIndex(argv: string[]): number | undefined {
138
+ if (argv[0]?.split("/").at(-1) !== "jq") return undefined;
139
+ const longOptions = new Set([
140
+ "--raw-output",
141
+ "--raw-input",
142
+ "--compact-output",
143
+ "--slurp",
144
+ "--monochrome-output",
145
+ "--color-output",
146
+ "--null-input",
147
+ "--exit-status",
148
+ "--ascii-output",
149
+ "--join-output",
150
+ "--sort-keys",
151
+ "--unbuffered",
152
+ "--tab",
153
+ ]);
154
+ let index = 1;
155
+ while (/^-[rRcMsScneaj]+$/.test(argv[index] ?? "") || longOptions.has(argv[index] ?? "")) index++;
156
+ if (argv[index] === "--") index++;
157
+ if (argv.slice(index + 1).some((arg) => arg.startsWith("-") && arg !== "-")) return undefined;
158
+ return isPassiveJqFilter(argv[index] ?? "") ? index : undefined;
159
+ }