@nac3/runtime 2.2.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/CHANGELOG.md +116 -0
- package/LICENSE +201 -0
- package/README.md +140 -0
- package/SPEC.md +602 -0
- package/dist/cli-lint.mjs +142 -0
- package/dist/cli.js +71 -0
- package/dist/nac-chat-client.browser.js +703 -0
- package/dist/nac-chat-client.cjs +708 -0
- package/dist/nac-chat-client.d.ts +80 -0
- package/dist/nac-chat-client.mjs +708 -0
- package/dist/nac-v2-extensions.browser.js +2096 -0
- package/dist/nac-v2-extensions.cjs +2101 -0
- package/dist/nac-v2-extensions.d.ts +80 -0
- package/dist/nac-v2-extensions.mjs +2101 -0
- package/dist/nac.browser.js +5218 -0
- package/dist/nac.cjs +5223 -0
- package/dist/nac.d.ts +80 -0
- package/dist/nac.mjs +5223 -0
- package/package.json +81 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Static lint helper for NAC CLI.
|
|
2
|
+
// Plain regex-based; intentionally tolerant of malformed input.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const CANONICAL_ROLES = new Set([
|
|
8
|
+
'plugin','section','region','action','field','option','tab',
|
|
9
|
+
'breadcrumb-item','accordion-toggle','step','pagination-item',
|
|
10
|
+
'confirm-button','sort-control','filter-control','data-table',
|
|
11
|
+
'navigation','confirm-dialog','button'
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
function walk(dir, exts) {
|
|
15
|
+
const out = [];
|
|
16
|
+
function rec(p) {
|
|
17
|
+
const st = fs.statSync(p);
|
|
18
|
+
if (st.isDirectory()) {
|
|
19
|
+
for (const e of fs.readdirSync(p)) rec(path.join(p, e));
|
|
20
|
+
} else if (exts.some(x => p.endsWith(x))) {
|
|
21
|
+
out.push(p);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
rec(dir);
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function extractDomPairs(content) {
|
|
29
|
+
// Match every (data-nac-id="X", data-nac-role="Y") pair within an
|
|
30
|
+
// attribute-soup window. The naive approach: for each id match, look
|
|
31
|
+
// back/forward 240 chars for a role match on the same element.
|
|
32
|
+
const out = [];
|
|
33
|
+
const idRx = /data-nac-id="([^"]+)"/g;
|
|
34
|
+
let m;
|
|
35
|
+
while ((m = idRx.exec(content)) !== null) {
|
|
36
|
+
const id = m[1];
|
|
37
|
+
const start = Math.max(0, m.index - 240);
|
|
38
|
+
const end = Math.min(content.length, m.index + m[0].length + 240);
|
|
39
|
+
const window = content.slice(start, end);
|
|
40
|
+
// Only accept role matches inside the same element tag (between
|
|
41
|
+
// the closest '<' before the id and the closest '>' after).
|
|
42
|
+
const tagOpen = content.lastIndexOf('<', m.index);
|
|
43
|
+
const tagClose = content.indexOf('>', m.index);
|
|
44
|
+
let role = null;
|
|
45
|
+
if (tagOpen >= 0 && tagClose >= 0) {
|
|
46
|
+
const tagStr = content.slice(tagOpen, tagClose + 1);
|
|
47
|
+
const rm = /data-nac-role="([^"]+)"/.exec(tagStr);
|
|
48
|
+
if (rm) role = rm[1];
|
|
49
|
+
}
|
|
50
|
+
out.push({ id, role });
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function extractManifestEntries(content) {
|
|
56
|
+
// Best-effort: find NAC.register({ ... }) and extract elements[].id
|
|
57
|
+
// + role. Tolerant of multi-line.
|
|
58
|
+
const out = [];
|
|
59
|
+
const rx = /NAC\.register\s*\(\s*\{[\s\S]*?\}\s*\)/g;
|
|
60
|
+
let m;
|
|
61
|
+
while ((m = rx.exec(content)) !== null) {
|
|
62
|
+
const block = m[0];
|
|
63
|
+
const slugM = /plugin_slug\s*:\s*['"]([^'"]+)['"]/.exec(block);
|
|
64
|
+
const plugin = slugM ? slugM[1] : '<unknown>';
|
|
65
|
+
// Each element entry: { id: 'X', role: 'Y', ... }
|
|
66
|
+
const elRx = /\{[^{}]*?id\s*:\s*['"]([^'"]+)['"][^{}]*?role\s*:\s*['"]([^'"]+)['"][^{}]*?\}/g;
|
|
67
|
+
let em;
|
|
68
|
+
while ((em = elRx.exec(block)) !== null) {
|
|
69
|
+
out.push({ plugin, id: em[1], role: em[2] });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function staticLint(dir) {
|
|
76
|
+
const findings = [];
|
|
77
|
+
const files = walk(dir, ['.html', '.htm', '.php']);
|
|
78
|
+
for (const file of files) {
|
|
79
|
+
let content;
|
|
80
|
+
try { content = fs.readFileSync(file, 'utf8'); }
|
|
81
|
+
catch (_) { continue; }
|
|
82
|
+
const rel = path.relative(dir, file);
|
|
83
|
+
|
|
84
|
+
const domPairs = extractDomPairs(content);
|
|
85
|
+
const manifestEntries = extractManifestEntries(content);
|
|
86
|
+
|
|
87
|
+
// Rule 1: tab_role_drift -- DOM id like tab.X must have role 'tab'.
|
|
88
|
+
for (const p of domPairs) {
|
|
89
|
+
if (/^tab\./.test(p.id) && p.role !== 'tab') {
|
|
90
|
+
findings.push({
|
|
91
|
+
severity: 'error',
|
|
92
|
+
code: 'tab_role_drift',
|
|
93
|
+
file: rel,
|
|
94
|
+
message: 'data-nac-id="' + p.id + '" should have data-nac-role="tab" (got ' +
|
|
95
|
+
(p.role === null ? 'null' : '"' + p.role + '"') + ')'
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Rule 2: manifest_role_unknown -- manifest element role outside canonical set.
|
|
101
|
+
for (const e of manifestEntries) {
|
|
102
|
+
if (!CANONICAL_ROLES.has(e.role)) {
|
|
103
|
+
findings.push({
|
|
104
|
+
severity: 'error',
|
|
105
|
+
code: 'manifest_role_unknown',
|
|
106
|
+
file: rel,
|
|
107
|
+
message: 'plugin "' + e.plugin + '" element id="' + e.id + '" has unknown role="' + e.role + '"'
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Rule 3: manifest_dom_role_mismatch -- manifest says X, DOM says Y for same id.
|
|
113
|
+
const domByLastSeg = new Map();
|
|
114
|
+
for (const p of domPairs) domByLastSeg.set(p.id, p.role);
|
|
115
|
+
for (const e of manifestEntries) {
|
|
116
|
+
const domRole = domByLastSeg.get(e.id);
|
|
117
|
+
if (domRole && domRole !== e.role) {
|
|
118
|
+
findings.push({
|
|
119
|
+
severity: 'error',
|
|
120
|
+
code: 'manifest_dom_role_mismatch',
|
|
121
|
+
file: rel,
|
|
122
|
+
message: 'plugin "' + e.plugin + '" id="' + e.id + '": manifest says role="' + e.role +
|
|
123
|
+
'" but DOM says role="' + domRole + '"'
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Rule 4: tab id with manifest role !== 'tab'.
|
|
129
|
+
for (const e of manifestEntries) {
|
|
130
|
+
if (/^tab\./.test(e.id) && e.role !== 'tab') {
|
|
131
|
+
findings.push({
|
|
132
|
+
severity: 'error',
|
|
133
|
+
code: 'tab_id_manifest_role_drift',
|
|
134
|
+
file: rel,
|
|
135
|
+
message: 'plugin "' + e.plugin + '" id="' + e.id + '" looks like a tab but manifest role="' +
|
|
136
|
+
e.role + '" (NAC.tab() will not find it)'
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return findings;
|
|
142
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// NAC CLI v2.2.0
|
|
3
|
+
// Usage:
|
|
4
|
+
// nac validate <url-or-dir> [--severity=error|warn|info]
|
|
5
|
+
// nac --version
|
|
6
|
+
//
|
|
7
|
+
// Modes:
|
|
8
|
+
// - URL: spawn headless Chromium, load URL, run NAC.validate_global(),
|
|
9
|
+
// report findings.
|
|
10
|
+
// - Directory: static lint of HTML/PHP files for data-nac-id /
|
|
11
|
+
// data-nac-role coherence.
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { staticLint } from './cli-lint.mjs';
|
|
16
|
+
|
|
17
|
+
const args = process.argv.slice(2);
|
|
18
|
+
const cmd = args[0];
|
|
19
|
+
|
|
20
|
+
function showHelp() {
|
|
21
|
+
console.log('NAC CLI v2.2.0');
|
|
22
|
+
console.log('');
|
|
23
|
+
console.log('Usage:');
|
|
24
|
+
console.log(' nac validate <url-or-dir> [--severity=error|warn|info]');
|
|
25
|
+
console.log(' nac --version');
|
|
26
|
+
console.log(' nac --help');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function showVersion() {
|
|
30
|
+
console.log('@nac3/runtime CLI 2.2.0 (Native Agent Contract v2.2)');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!cmd || cmd === '--help' || cmd === '-h') { showHelp(); process.exit(0); }
|
|
34
|
+
if (cmd === '--version' || cmd === '-v') { showVersion(); process.exit(0); }
|
|
35
|
+
|
|
36
|
+
if (cmd === 'validate') {
|
|
37
|
+
const target = args[1];
|
|
38
|
+
if (!target) {
|
|
39
|
+
console.error('error: validate requires a URL or directory argument');
|
|
40
|
+
process.exit(2);
|
|
41
|
+
}
|
|
42
|
+
const sevArg = args.find(a => a.startsWith('--severity='));
|
|
43
|
+
const minSev = sevArg ? sevArg.split('=')[1] : 'error';
|
|
44
|
+
|
|
45
|
+
const isUrl = /^https?:\/\//.test(target);
|
|
46
|
+
if (isUrl) {
|
|
47
|
+
console.error('error: URL validation requires Playwright. Run:');
|
|
48
|
+
console.error(' npm install --save-dev @playwright/test');
|
|
49
|
+
console.error(' npx playwright install chromium');
|
|
50
|
+
console.error('then re-run. Static lint of a directory works without Playwright.');
|
|
51
|
+
process.exit(2);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const dir = path.resolve(target);
|
|
55
|
+
if (!fs.existsSync(dir)) { console.error('error: not found:', dir); process.exit(2); }
|
|
56
|
+
const findings = staticLint(dir);
|
|
57
|
+
const sevRank = { info: 0, warn: 1, error: 2 };
|
|
58
|
+
const min = sevRank[minSev] ?? 2;
|
|
59
|
+
const failing = findings.filter(f => (sevRank[f.severity] ?? 2) >= min);
|
|
60
|
+
for (const f of findings) {
|
|
61
|
+
const tag = '[' + f.severity.toUpperCase() + ']';
|
|
62
|
+
console.log(tag, f.code, '--', f.message, f.file ? '(' + f.file + ')' : '');
|
|
63
|
+
}
|
|
64
|
+
console.log('');
|
|
65
|
+
console.log('Total findings:', findings.length, '|', 'Failing at severity >=', minSev + ':', failing.length);
|
|
66
|
+
process.exit(failing.length > 0 ? 1 : 0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
console.error('error: unknown command:', cmd);
|
|
70
|
+
showHelp();
|
|
71
|
+
process.exit(2);
|