@nac3/runtime 2.6.0 → 2.9.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/dist/cli.js CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  /* =====================================================================
3
- NAC CLI v2.6.0 -- the entry point.
3
+ NAC CLI v2.9.0 -- the entry point.
4
4
 
5
- Two modes, and they do not pretend to be each other:
5
+ THREE modes since 2026-11-27, and they do not pretend to be each
6
+ other:
6
7
 
7
8
  URL loads the page in a real browser, asks the runtime what
8
9
  it registered, and REPORTS A CONFORMANCE LEVEL against
@@ -11,12 +12,31 @@
11
12
  time. Add --drive to let it click, which is what
12
13
  decides the two behavioural requirements of level 1.
13
14
 
14
- directory static lint of HTML/PHP files for data-nac-id /
15
- data-nac-role coherence. It CANNOT decide a level --
15
+ directory static lint of the MARKUP AND TEMPLATE files of a tree
16
+ (the list is LINT_EXTENSIONS in cli-lint.mjs; --ext
17
+ adds to it) for data-nac-id / data-nac-role coherence.
18
+ It reports HOW MANY FILES IT READ and refuses when it
19
+ read none, because "Total findings: 0" over a tree it
20
+ could not open is the same line a conforming tree
21
+ prints -- that was PND-134, measured over an ERP whose
22
+ 983 views are .cshtml. It CANNOT decide a level --
16
23
  not one requirement of section 8 is decidable from
17
24
  files on disk -- and it says so instead of printing a
18
25
  number nobody measured.
19
26
 
27
+ harness MOUNTS the page in Node -- parses its markup into a
28
+ synthesised document, loads the runtime, runs the
29
+ page's own scripts -- and decides the requirements that
30
+ do not need a browser, declaring each of the others
31
+ with the reason it cannot. `--harness`. It decides
32
+ FIVE of the eleven and says so; a mode that decided
33
+ eleven would be lying about at least one of them.
34
+ PLAN_LUNES, 2026-11-27: the gap between the two modes
35
+ above was that the directory mode answered "not
36
+ decidable in this mode" for requirements the project
37
+ already knew how to measure -- 18 of its suites mount
38
+ the runtime in Node, which is the rung this mode is.
39
+
20
40
  Audit N-18: before 2.5.0 the URL mode aborted with exit 2 and no
21
41
  mode ever printed a level, while SPEC.md section 8 promised this
22
42
  command "reports the highest level a page reaches".
@@ -30,10 +50,10 @@
30
50
 
31
51
  import fs from 'node:fs';
32
52
  import path from 'node:path';
33
- import { staticLint } from './cli-lint.mjs';
53
+ import { lintTree, manifestInventory, lintableFiles, LINT_EXTENSIONS } from './cli-lint.mjs';
34
54
 
35
- const VERSION = '2.6.0';
36
- const SPEC_VERSION = '2.6';
55
+ const VERSION = '2.9.0';
56
+ const SPEC_VERSION = '2.9';
37
57
 
38
58
  const args = process.argv.slice(2);
39
59
  const cmd = args[0];
@@ -45,13 +65,23 @@ function opt(name, dflt) {
45
65
  const hit = args.find((a) => a.startsWith('--' + name + '='));
46
66
  return hit ? hit.slice(name.length + 3) : dflt;
47
67
  }
68
+ /* --ext=.cshtml,.jsx -- extensions to ADD to the ones the lint already
69
+ reads (PND-134). It adds and never replaces: an adopter who names
70
+ one extension and silently loses the defaults would get a second
71
+ false green, this time wearing the shape of an option. */
72
+ function extraExtensions() {
73
+ return opt('ext', '').split(',').map((e) => e.trim()).filter(Boolean);
74
+ }
48
75
 
49
76
  function showHelp() {
50
77
  console.log('NAC CLI v' + VERSION);
51
78
  console.log('');
52
79
  console.log('Usage:');
53
80
  console.log(' nac validate <url> [options] load the page and report a conformance level');
54
- console.log(' nac validate <dir> [options] static lint of files (cannot decide a level)');
81
+ console.log(' nac validate <dir> [options] static lint of markup files (cannot decide a level)');
82
+ console.log(' nac validate <dir> --harness MOUNT the page in Node and decide what is');
83
+ console.log(' decidable without a browser (5 of the 11)');
84
+ console.log(' nac plugins <dir> [--json] list the plugins, ids and roles the files declare');
55
85
  console.log(' nac --version');
56
86
  console.log(' nac --help');
57
87
  console.log('');
@@ -64,6 +94,18 @@ function showHelp() {
64
94
  console.log(' --drive-limit=N click at most N ids (default 0 = all)');
65
95
  console.log(' --timeout=MS page load / settle budget (default 30000)');
66
96
  console.log(' --json emit the whole report as JSON instead of prose');
97
+ console.log(' (both modes of validate, and plugins)');
98
+ console.log(' --harness directory mode: mount the page in a synthesised');
99
+ console.log(' document, load the runtime, RUN THE PAGE SCRIPTS,');
100
+ console.log(' and report a verdict per conformance requirement.');
101
+ console.log(' It executes the page in a sandbox with no network;');
102
+ console.log(' the plain directory mode never executes anything.');
103
+ console.log(' --requirements with --harness: the full breakdown, all 11 of them,');
104
+ console.log(' each DECIDED (pass/fail) or NOT-DECIDABLE with why');
105
+ console.log(' --ext=.a,.b ALSO read these extensions in directory mode.');
106
+ console.log(' Added to the ' + LINT_EXTENSIONS.length + ' it already reads, never');
107
+ console.log(' instead of them. Default list:');
108
+ console.log(' ' + LINT_EXTENSIONS.join(' '));
67
109
  console.log('');
68
110
  console.log('URL mode needs Playwright, which is NOT a dependency of this package:');
69
111
  console.log(' npm i -D playwright && npx playwright install chromium');
@@ -76,7 +118,8 @@ function showVersion() {
76
118
  if (!cmd || cmd === '--help' || cmd === '-h') { showHelp(); process.exit(0); }
77
119
  if (cmd === '--version' || cmd === '-v') { showVersion(); process.exit(0); }
78
120
 
79
- if (cmd !== 'validate') {
121
+ const COMMANDS = ['validate', 'plugins'];
122
+ if (COMMANDS.indexOf(cmd) < 0) {
80
123
  console.error('error: unknown command: ' + cmd);
81
124
  showHelp();
82
125
  process.exit(2);
@@ -84,7 +127,7 @@ if (cmd !== 'validate') {
84
127
 
85
128
  const target = args[1];
86
129
  if (!target || target.startsWith('--')) {
87
- console.error('error: validate requires a URL or directory argument');
130
+ console.error('error: ' + cmd + ' requires a URL or directory argument');
88
131
  process.exit(2);
89
132
  }
90
133
 
@@ -230,17 +273,182 @@ function printUrlReport(r) {
230
273
  }
231
274
  }
232
275
 
276
+ /* ---- harness mode (PLAN_LUNES) --------------------------------------
277
+ The third rung: decide what Node can decide, declare the rest. The
278
+ deciding lives in cli-harness.mjs; this prints it. */
279
+
280
+ async function runHarnessMode(target) {
281
+ const mod = await import('./cli-harness.mjs');
282
+ let r;
283
+ try {
284
+ r = await mod.harnessDecide(target, {});
285
+ } catch (e) {
286
+ console.error('error: the harness mode failed: ' + ((e && e.message) || e));
287
+ process.exit(2);
288
+ }
289
+ if (!r.ok) {
290
+ if (flag('json')) {
291
+ console.log(JSON.stringify({ mode: 'harness', ok: false, error: r.error,
292
+ scripts: r.scripts || null }, null, 2));
293
+ } else {
294
+ console.error('error: ' + r.error);
295
+ for (const sc of (r.scripts || [])) {
296
+ console.error(' script ' + sc.src + ': '
297
+ + (sc.ran ? 'ran' : (sc.skipped || sc.error || 'did not run')));
298
+ }
299
+ }
300
+ process.exit(2);
301
+ }
302
+
303
+ if (flag('json')) {
304
+ console.log(JSON.stringify(Object.assign({ cli_version: VERSION }, r), null, 2));
305
+ process.exit(exitCodeFor(r));
306
+ }
307
+
308
+ console.log('Mounted ' + r.page);
309
+ console.log(' runtime ' + (r.runtime_version || '?') + ' from ' + r.runtime_source);
310
+ console.log(' scripts ' + r.scripts.filter((x) => x.ran).length + ' ran, '
311
+ + r.scripts.filter((x) => !x.ran).length + ' not');
312
+ for (const sc of r.scripts) {
313
+ if (sc.ran) continue;
314
+ console.log(' ' + sc.src + ': ' + (sc.skipped || sc.error));
315
+ }
316
+ console.log(' surface ' + r.advertised + ' advertised id(s), ' + r.painted + ' painted');
317
+ if (r.unresolved_advertised.length) {
318
+ console.log(' ' + r.unresolved_advertised.length
319
+ + ' advertised id(s) resolve to nothing: '
320
+ + r.unresolved_advertised.slice(0, 5).join(', '));
321
+ }
322
+ console.log('');
323
+
324
+ if (flag('requirements')) {
325
+ /* The breakdown IS the deliverable. A level without the list of
326
+ what was never measured is worse than no level at all: it reads
327
+ as a certificate. */
328
+ for (const q of r.requirements) {
329
+ const tag = q.verdict === 'pass' ? 'DECIDED pass'
330
+ : q.verdict === 'fail' ? 'DECIDED FAIL'
331
+ : 'NOT-DECIDABLE';
332
+ console.log('[' + tag + '] ' + q.requirement_id + ' (' + q.check + ')');
333
+ console.log(' ' + String(q.text || '').split('\n')[0].slice(0, 120));
334
+ console.log(' -> ' + String(q.detail || '').replace(/\s+/g, ' ').slice(0, 300));
335
+ console.log('');
336
+ }
337
+ }
338
+
339
+ for (const lv of r.by_level) {
340
+ console.log('Level ' + lv.level + ': ' + lv.passed.length + ' passed, '
341
+ + lv.failed.length + ' failed, ' + lv.not_decidable.length
342
+ + ' not decidable in this mode (of ' + lv.total + ')');
343
+ if (lv.failed.length) console.log(' failed: ' + lv.failed.join(', '));
344
+ if (lv.not_decidable.length) console.log(' not decidable: ' + lv.not_decidable.join(', '));
345
+ }
346
+ console.log('');
347
+
348
+ if (r.level.level) {
349
+ console.log('Conformance level: ' + r.level.level
350
+ + ' (' + r.level.decided + ' of ' + r.level.total + ' requirements decided)');
351
+ } else {
352
+ const l1 = r.by_level.filter((x) => x.level === 1)[0] || { failed: [], not_decidable: [] };
353
+ if (l1.failed.length) {
354
+ console.log('Conformance level: NONE -- level 1 is REFUSED by ' + l1.failed.join(', '));
355
+ } else {
356
+ console.log('Conformance level: NOT ASSERTED -- every requirement this mode could decide');
357
+ console.log(' passes, and ' + l1.not_decidable.length + ' of the 4 requirements of level 1');
358
+ console.log(' are not decidable without a browser: ' + l1.not_decidable.join(', '));
359
+ console.log(' A level asserted without them would be a number nobody measured.');
360
+ }
361
+ }
362
+ if (!flag('requirements')) {
363
+ console.log('');
364
+ console.log(' --requirements prints the verdict and the reason for all '
365
+ + r.total + ' requirements.');
366
+ }
367
+ process.exit(exitCodeFor(r));
368
+ }
369
+
370
+ /* THE EXIT CODE OF THIS MODE, and the decision inside it.
371
+
372
+ The URL mode exits 1 when no level could be reported. Inheriting
373
+ that here would make this mode exit 1 on EVERY page, because no
374
+ level can be asserted from Node by construction -- three of the
375
+ four requirements of level 1 are a human judgement or need the page
376
+ driven. A gate whose normal answer is failure is a gate nobody
377
+ wires up.
378
+
379
+ So: exit 1 when a requirement OF LEVEL 1 was decided to FAIL -- the
380
+ page is not even minimally addressable, which is a fact and not a
381
+ limitation of the instrument. A decided failure at level 2 or 3 is
382
+ printed and does NOT fail the run: a page that never aimed at level
383
+ 3 is not broken for lacking an HMAC. And an undecidable requirement
384
+ is never a failure -- that would make the honest declaration cost an
385
+ exit code, which is the incentive this whole mode exists to get
386
+ right. */
387
+ function exitCodeFor(r) {
388
+ return r.requirements.some((q) => q.level === 1 && q.verdict === 'fail') ? 1 : 0;
389
+ }
390
+
233
391
  /* ---- directory mode ------------------------------------------------- */
234
392
 
235
393
  function runDir(dir) {
236
394
  if (!fs.existsSync(dir)) { console.error('error: not found: ' + dir); process.exit(2); }
237
- const findings = staticLint(dir);
395
+ /* The files READ come back with the findings, from the same walk
396
+ (PND-134). Asking twice would let the count and the findings
397
+ disagree, which is the shape of defect this whole release is
398
+ about. */
399
+ const run = lintTree(dir, { extensions: extraExtensions() });
400
+ const findings = run.findings;
238
401
  const min = sevRank[minSev];
239
402
  const failing = findings.filter((f) => (sevRank[f.severity] ?? 2) >= min);
403
+
404
+ /* --json, in THIS mode too (TF-B03 / H-11).
405
+
406
+ The option is declared in the general `Options:` block of the
407
+ help, which covers both modes, and it was only ever read inside
408
+ runUrl. So a consumer who followed the help and piped
409
+ `nac validate ./src --json` into jq got prose, and learned about
410
+ it from a parse error inside their own tool instead of from the
411
+ CLI -- which is the worst place for a contract to break, because
412
+ the error arrives wearing somebody else name.
413
+
414
+ Same envelope shape as URL mode, plus a `mode` field so a reader
415
+ can tell the two reports apart, and `level: null` because a
416
+ static lint cannot decide one -- stated as DATA here, not only in
417
+ the prose below. */
418
+ if (flag('json')) {
419
+ console.log(JSON.stringify({
420
+ mode: 'dir',
421
+ target: dir,
422
+ cli_version: VERSION,
423
+ spec_version: SPEC_VERSION,
424
+ severity_threshold: minSev,
425
+ /* What the walk actually opened. A consumer that only reads
426
+ `total` cannot tell a clean tree from an unread one; these
427
+ three fields are what makes that distinction machine-readable
428
+ as well as printed (PND-134). */
429
+ files_read: run.files.length,
430
+ files: run.files,
431
+ extensions: run.extensions,
432
+ total: findings.length,
433
+ failing: failing.length,
434
+ findings: findings,
435
+ level: null,
436
+ level_undecidable_because:
437
+ 'a static lint reads files; every requirement of SPEC.md section 8 is about what the runtime registered and what the page does',
438
+ }, null, 2));
439
+ process.exit(failing.length > 0 ? 1 : 0);
440
+ }
441
+
240
442
  for (const f of findings) {
241
443
  console.log('[' + f.severity.toUpperCase() + ']', f.code, '--', f.message, f.file ? '(' + f.file + ')' : '');
242
444
  }
243
445
  console.log('');
446
+ /* FIRST the files, then the findings. A finding count on its own is
447
+ the sentence that made this defect invisible for four minor
448
+ versions: a reader takes it for a verdict about the tree, when it
449
+ is a verdict about whatever the walk managed to open. */
450
+ console.log('Files read:', run.files.length,
451
+ '(' + run.extensions.length + ' extensions' + (flag('json') ? '' : '; --ext adds more') + ')');
244
452
  console.log('Total findings:', findings.length, '|', 'Failing at severity >=', minSev + ':', failing.length);
245
453
  console.log('');
246
454
  /* Saying this out loud is the point. The old CLI printed a finding
@@ -256,10 +464,107 @@ function runDir(dir) {
256
464
  process.exit(failing.length > 0 ? 1 : 0);
257
465
  }
258
466
 
467
+ /* ---- plugins: enumerate what the protocol made discoverable --------- */
468
+
469
+ /* TF-B09 / H-11. The CLI had exactly one subcommand, so anything but
470
+ `validate` exited 2 -- and a protocol whose whole argument is that a
471
+ UI becomes DISCOVERABLE shipped no way to ask a tree what it
472
+ declares. Half the work already existed: the lint extracts the
473
+ plugin, the id and the role of every manifest element, and that is
474
+ what its four manifest rules read.
475
+
476
+ Read-only: no browser, no network. It lints nothing and says nothing
477
+ about conformance. It answers "what is in here". */
478
+ function runPlugins(dir) {
479
+ if (!fs.existsSync(dir)) { console.error('error: not found: ' + dir); process.exit(2); }
480
+ const entries = manifestInventory(dir, { extensions: extraExtensions() });
481
+ const filesRead = lintableFiles(dir, extraExtensions());
482
+
483
+ const byPlugin = new Map();
484
+ for (const e of entries) {
485
+ if (!byPlugin.has(e.plugin)) byPlugin.set(e.plugin, []);
486
+ byPlugin.get(e.plugin).push(e);
487
+ }
488
+ const sorted = [...byPlugin].sort((a, b) => a[0].localeCompare(b[0]));
489
+
490
+ if (flag('json')) {
491
+ console.log(JSON.stringify({
492
+ mode: 'plugins',
493
+ target: dir,
494
+ cli_version: VERSION,
495
+ spec_version: SPEC_VERSION,
496
+ files_read: filesRead.length,
497
+ extensions: LINT_EXTENSIONS.concat(extraExtensions()),
498
+ plugin_count: byPlugin.size,
499
+ element_count: entries.length,
500
+ plugins: sorted.map(([plugin, els]) => ({
501
+ plugin,
502
+ files: [...new Set(els.map((e) => e.file))].sort(),
503
+ element_count: els.length,
504
+ roles: [...new Set(els.map((e) => e.role))].sort(),
505
+ elements: els.slice().sort((a, b) => a.id.localeCompare(b.id))
506
+ .map((e) => ({ id: e.id, role: e.role, file: e.file })),
507
+ })),
508
+ }, null, 2));
509
+ process.exit(0);
510
+ }
511
+
512
+ if (byPlugin.size === 0) {
513
+ console.log('No NAC.register call was found under ' + dir
514
+ + ' (' + filesRead.length + ' file(s) read).');
515
+ console.log('');
516
+ if (filesRead.length === 0) {
517
+ /* The same refusal the lint makes, in the command next to it
518
+ (PND-134): zero manifests found in zero files read says
519
+ nothing about the tree, and the old wording said it as if it
520
+ were a fact about the tree. */
521
+ console.log('And it read NO files, so that sentence is not about this tree: it is about');
522
+ console.log('a walk that found nothing it knows how to open. The extensions it reads:');
523
+ console.log(' ' + LINT_EXTENSIONS.concat(extraExtensions()).join(' '));
524
+ console.log('If the markup here lives in another one, name it: --ext=.foo,.bar');
525
+ process.exit(1);
526
+ }
527
+ console.log('That is a fact about the tree, not an error: a page can be marked up with');
528
+ console.log('data-nac-* attributes and never register a manifest. `nac validate` reads');
529
+ console.log('the attributes; this command reads the manifests.');
530
+ process.exit(0);
531
+ }
532
+
533
+ for (const [plugin, els] of sorted) {
534
+ console.log(plugin + ' (' + els.length + ' element' + (els.length === 1 ? '' : 's') + ')');
535
+ const width = Math.max.apply(null, els.map((e) => e.id.length));
536
+ for (const e of els.slice().sort((a, b) => a.id.localeCompare(b.id))) {
537
+ console.log(' ' + e.id.padEnd(width) + ' ' + e.role);
538
+ }
539
+ console.log('');
540
+ }
541
+ console.log(byPlugin.size + ' plugin' + (byPlugin.size === 1 ? '' : 's') + ', '
542
+ + entries.length + ' element' + (entries.length === 1 ? '' : 's') + ', read from '
543
+ + filesRead.length + ' file' + (filesRead.length === 1 ? '' : 's') + '.');
544
+ process.exit(0);
545
+ }
546
+
259
547
  /* ---- dispatch ------------------------------------------------------- */
260
548
 
261
- if (/^https?:\/\//.test(target)) {
549
+ const IS_URL = /^https?:\/\//.test(target);
550
+
551
+ if (cmd === 'plugins') {
552
+ if (IS_URL) {
553
+ console.error('error: plugins reads files; point it at a directory, not a URL');
554
+ console.error(' (what a LIVE page registered is NAC.plugins() in the browser)');
555
+ process.exit(2);
556
+ }
557
+ runPlugins(path.resolve(target));
558
+ } else if (IS_URL) {
559
+ if (flag('harness')) {
560
+ console.error('error: --harness MOUNTS a local page in Node; point it at a file or a');
561
+ console.error(' directory. For a URL the browser mode decides more, not less:');
562
+ console.error(' nac validate ' + target + ' --drive');
563
+ process.exit(2);
564
+ }
262
565
  runUrl(target);
566
+ } else if (flag('harness')) {
567
+ runHarnessMode(path.resolve(target));
263
568
  } else {
264
569
  runDir(path.resolve(target));
265
570
  }