-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·355 lines (337 loc) · 9.91 KB
/
Copy pathcli.js
File metadata and controls
executable file
·355 lines (337 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env -S node --disable-warning=DEP0040
// Copyright 2026 will Farrell, and ajv-cmd contributors.
// SPDX-License-Identifier: MIT
// --disable-warning=DEP0040 suppresses: [DEP0040] DeprecationWarning: The `punycode` module is deprecated.
import { createRequire } from "node:module";
import { Command, Option } from "commander";
const { version } = createRequire(import.meta.url)("./package.json");
// Run one command over every input, loading its module only once the action
// actually fires. Statically importing all five made every invocation — `--help`
// included — pay for all of them, and commands/sast.js alone costs ~220ms at
// import time because sast-json-schema compiles five draft meta-schema
// validators at module load.
//
// `suffix` is the extension a batch writes beside each input; commands with no
// output file (validate) pass none.
const lazy = (specifier, suffix) => async (inputs, options) => {
const { CommandFailure, expandInputs, mirrorOutput } = await import(
"./commands/_utils.js"
);
const run = (await import(specifier)).default;
const files = await expandInputs(inputs);
const batch = files.length > 1;
// One -o path cannot receive N outputs; a batch writes beside each input.
if (batch && typeof options.output === "string") {
throw new Error(
`--output takes a single input, received ${files.length}; omit it to write beside each input`,
);
}
let failed = false;
for (const file of files) {
try {
await run(
file,
batch && suffix
? { ...options, output: mirrorOutput(file, suffix) }
: options,
);
} catch (error) {
// A CommandFailure has already printed its own message; anything else
// (missing file, bad JSON, unresolved $ref) still needs reporting. Either
// way the run continues so one bad file cannot hide the rest.
if (!(error instanceof CommandFailure)) {
console.error(error.message);
}
failed = true;
}
}
if (failed) {
process.exit(1);
}
};
const validate = lazy("./commands/validate.js");
const transpile = lazy("./commands/transpile.js", ".js");
const deref = lazy("./commands/deref.js", ".deref.json");
const sast = lazy("./commands/sast.js", ".sast.json");
const ftl = lazy("./commands/ftl.js", ".js");
// AJV options such as `strict` accept `true | false | "log"`. Commander passes
// option arguments through as strings, so coerce the boolean-ish values to real
// booleans while leaving recognized string modes (e.g. "log", "array", "empty")
// untouched.
const parseBoolish = (value) => {
if (value === "true") return true;
if (value === "false") return false;
return value;
};
const parseNumeric = (value) => {
const number = Number(value);
if (Number.isNaN(number)) {
throw new Error(`Expected a number, received "${value}"`);
}
return number;
};
const program = new Command()
.name("ajv")
.version(version)
.description(
"Validate, transpile, dereference, and audit JSON-Schema files using AJV",
);
program
.command("validate", { isDefault: true })
.argument(
"<input...>",
"Paths or glob patterns of JSON-Schema files to validate",
)
.addOption(new Option("--valid", "When not valid throw exit(1)").preset(true))
.addOption(
new Option("--invalid", "When not invalid throw exit(1)").preset(true),
)
.addOption(
new Option(
"-r, --ref-schema-files <refSchemaFiles...>",
"The schema in <input> can reference any of these schemas with $ref keyword.",
),
)
.addOption(
new Option("--strict [strict]", "true/false/log")
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--use-defaults [useDefaults]",
"replace missing properties/items with the values from default keyword",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--coerce-types [coerceTypes]",
"change type of data to match type keyword",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--all-errors [allErrors]",
"report all errors instead of stopping at the first (true/false, default true)",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--no-messages",
"exclude human-readable text messages from errors",
),
)
.addOption(
new Option(
"--loop-enum <loopEnum>",
"max size of enum to compile to expression (rather than to loop)",
).argParser(parseNumeric),
)
.addOption(
new Option(
"-d, --test-data-files <testDataFiles...>",
"The data files to validate against.",
),
)
.action(validate);
program
.command("transpile")
.argument(
"<input...>",
"Paths or glob patterns of JSON-Schema files to transpile",
)
// Docs: https://ajv.js.org/packages/ajv-cli.html
.addOption(
new Option(
"-r, --ref-schema-files <refSchemaFiles...>",
"The schema in <input> can reference any of these schemas with $ref keyword.",
),
)
.addOption(
new Option("--strict [strict]", "true/false/log")
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--use-defaults [useDefaults]",
"replace missing properties/items with the values from default keyword",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--coerce-types [coerceTypes]",
"change type of data to match type keyword",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--all-errors [allErrors]",
"report all errors instead of stopping at the first (true/false, default true)",
)
.preset(true)
.argParser(parseBoolish),
)
.addOption(
new Option(
"--no-messages",
"exclude human-readable text messages from errors",
),
)
.addOption(
new Option(
"--loop-enum <loopEnum>",
"max size of enum to compile to expression (rather than to loop)",
).argParser(parseNumeric),
)
.addOption(
new Option(
"-o, --output <output>",
"Path to store the resulting JavaScript file. Will be in ESM.",
),
)
.action(transpile);
program
.command("deref")
.argument(
"<input...>",
"Paths or glob patterns of JSON-Schema files to deref relative $ref",
)
.addOption(
new Option(
"-r, --ref-schema-files <refSchemaFiles...>",
"The schema in <input> can reference any of these schemas with $ref keyword.",
),
)
.addOption(
new Option(
"--offline",
"Do not fetch remote $ref URLs over the network (resolve local/-r schemas only).",
).preset(true),
)
.addOption(
new Option(
"-o, --output <output>",
"Path to store the resulting JSON-Schema file.",
),
)
.action(deref);
program
.command("sast")
.argument(
"<input...>",
"Paths or glob patterns of JSON-Schema files to audit for security",
)
.addOption(
new Option(
"-r, --ref-schema-files <refSchemaFiles...>",
"The schema in <input> can reference any of these schemas with $ref keyword.",
),
)
.addOption(
new Option("-f, --fail", "When issues found throw exit(1)").preset(true),
)
.addOption(
new Option(
"--override-max-items <overrideMaxItems>",
"Override the max items limit (default 1024). Removes maxItems errors when the array size is within this limit. Values <= 1024 are a no-op.",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--override-max-depth <overrideMaxDepth>",
"Override the max schema depth limit (default 32).",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--override-max-properties <overrideMaxProperties>",
"Override the max properties limit (default 1024). Removes maxProperties errors when the property count is within this limit. Values <= 1024 are a no-op.",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--redos-timeout-ms <redosTimeoutMs>",
"Per-pattern time budget for ReDoS analysis in ms (default 1000). A pattern that exceeds it is fail-closed as unsafe. Raise only for trusted first-party schemas.",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--redos-heap-budget-bytes <redosHeapBudgetBytes>",
"Retained-heap budget for ReDoS analysis in bytes (default 134217728). Once exceeded the remaining patterns are not analyzed. Keep --max-old-space-size well above it.",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--ignore <ignore...>",
"Suppress errors by `instancePath` or `instancePath:keyword` (exact match).",
),
)
.addOption(
new Option(
"--offline",
"Skip DNS lookups for remote $ref URLs (disables SSRF resolution).",
).preset(true),
)
.addOption(
new Option(
"--dns-timeout-ms <dnsTimeoutMs>",
"Per-hostname DNS lookup timeout in ms for SSRF checks (default 5000).",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--dns-concurrency <dnsConcurrency>",
"Max concurrent DNS lookups for SSRF checks (default 10).",
).argParser(parseNumeric),
)
.addOption(
new Option(
"--lang <lang>",
'Target language for deserialization-vector checks. One of: js, py, rb, rs, java, kotlin, clojure, cs, vb, fsharp, php, objc, swift, ex, lua, default. (default: "default" — union of all languages)',
).default("default"),
)
.addOption(
new Option(
"-o, --output <output>",
"Path to store the resulting JSON issues file.",
),
)
.action(sast);
program
.command("ftl")
.argument("<input...>", "Paths or glob patterns of Fluent files to transpile")
.requiredOption(
"--locale <locale...>",
"What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA",
)
.addOption(
new Option(
"-o, --output <output>",
"Path to store the resulting JavaScript file. Will be in ESM.",
),
)
.action(ftl);
// Surface command errors (missing files, invalid JSON, unresolved $refs, …) as
// a clean message + non-zero exit instead of an unhandled-rejection stack trace.
const reportError = (error) => {
console.error(error.message);
process.exit(1);
};
export { program, reportError };
// Only auto-run when invoked as the CLI entry point — `import.meta.main` is true
// solely for the entry module, so importing cli.js in tests does not parse argv.
// The subprocess CLI tests execute this block end-to-end.
if (import.meta.main) {
program.parseAsync().catch(reportError);
}