@xenosystem/primitives 0.0.0-stage → 0.1.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/README.md +28 -2
- package/dist/cjs/dirs/atomic.d.ts +9 -0
- package/dist/cjs/dirs/atomic.js +59 -0
- package/dist/cjs/dirs/check-cli.d.ts +2 -0
- package/dist/cjs/dirs/check-cli.js +50 -0
- package/dist/cjs/dirs/check.d.ts +49 -0
- package/dist/cjs/dirs/check.js +158 -0
- package/dist/cjs/dirs/cli.d.ts +2 -0
- package/dist/cjs/dirs/cli.js +72 -0
- package/dist/cjs/dirs/dirs.d.ts +57 -0
- package/dist/cjs/dirs/dirs.js +135 -0
- package/dist/cjs/dirs/doctor.d.ts +31 -0
- package/dist/cjs/dirs/doctor.js +80 -0
- package/dist/cjs/dirs/errors.d.ts +8 -0
- package/dist/cjs/dirs/errors.js +19 -0
- package/dist/cjs/dirs/host.d.ts +4 -0
- package/dist/cjs/dirs/host.js +26 -0
- package/dist/cjs/dirs/index.d.ts +13 -0
- package/dist/cjs/dirs/index.js +55 -0
- package/dist/cjs/dirs/inputs.d.ts +14 -0
- package/dist/cjs/dirs/inputs.js +14 -0
- package/dist/cjs/dirs/layout.d.ts +16 -0
- package/dist/cjs/dirs/layout.js +25 -0
- package/dist/cjs/dirs/lock.d.ts +38 -0
- package/dist/cjs/dirs/lock.js +128 -0
- package/dist/cjs/dirs/migrate.d.ts +134 -0
- package/dist/cjs/dirs/migrate.js +437 -0
- package/dist/cjs/dirs/procinfo.d.ts +10 -0
- package/dist/cjs/dirs/procinfo.js +67 -0
- package/dist/cjs/dirs/registry.d.ts +27 -0
- package/dist/cjs/dirs/registry.js +78 -0
- package/dist/cjs/dirs/roots.d.ts +23 -0
- package/dist/cjs/dirs/roots.js +114 -0
- package/dist/cjs/dirs/segments.d.ts +7 -0
- package/dist/cjs/dirs/segments.js +39 -0
- package/dist/cjs/generated/contract.d.ts +134 -0
- package/dist/cjs/generated/contract.js +233 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/secrets/envelope.d.ts +37 -0
- package/dist/cjs/secrets/envelope.js +90 -0
- package/dist/cjs/secrets/errors.d.ts +10 -0
- package/dist/cjs/secrets/errors.js +20 -0
- package/dist/cjs/secrets/index.d.ts +4 -0
- package/dist/cjs/secrets/index.js +17 -0
- package/dist/cjs/secrets/keystore.d.ts +37 -0
- package/dist/cjs/secrets/keystore.js +41 -0
- package/dist/cjs/secrets/os-stores.d.ts +17 -0
- package/dist/cjs/secrets/os-stores.js +195 -0
- package/dist/cjs/secrets/store.d.ts +32 -0
- package/dist/cjs/secrets/store.js +161 -0
- package/dist/cjs/secrets/testing.d.ts +5 -0
- package/dist/cjs/secrets/testing.js +20 -0
- package/dist/esm/dirs/atomic.d.ts +9 -0
- package/dist/esm/dirs/atomic.js +56 -0
- package/dist/esm/dirs/check-cli.d.ts +2 -0
- package/dist/esm/dirs/check-cli.js +48 -0
- package/dist/esm/dirs/check.d.ts +49 -0
- package/dist/esm/dirs/check.js +152 -0
- package/dist/esm/dirs/cli.d.ts +2 -0
- package/dist/esm/dirs/cli.js +70 -0
- package/dist/esm/dirs/dirs.d.ts +57 -0
- package/dist/esm/dirs/dirs.js +129 -0
- package/dist/esm/dirs/doctor.d.ts +31 -0
- package/dist/esm/dirs/doctor.js +77 -0
- package/dist/esm/dirs/errors.d.ts +8 -0
- package/dist/esm/dirs/errors.js +14 -0
- package/dist/esm/dirs/host.d.ts +4 -0
- package/dist/esm/dirs/host.js +21 -0
- package/dist/esm/dirs/index.d.ts +13 -0
- package/dist/esm/dirs/index.js +13 -0
- package/dist/esm/dirs/inputs.d.ts +14 -0
- package/dist/esm/dirs/inputs.js +11 -0
- package/dist/esm/dirs/layout.d.ts +16 -0
- package/dist/esm/dirs/layout.js +21 -0
- package/dist/esm/dirs/lock.d.ts +38 -0
- package/dist/esm/dirs/lock.js +124 -0
- package/dist/esm/dirs/migrate.d.ts +134 -0
- package/dist/esm/dirs/migrate.js +428 -0
- package/dist/esm/dirs/procinfo.d.ts +10 -0
- package/dist/esm/dirs/procinfo.js +62 -0
- package/dist/esm/dirs/registry.d.ts +27 -0
- package/dist/esm/dirs/registry.js +71 -0
- package/dist/esm/dirs/roots.d.ts +23 -0
- package/dist/esm/dirs/roots.js +107 -0
- package/dist/esm/dirs/segments.d.ts +7 -0
- package/dist/esm/dirs/segments.js +35 -0
- package/dist/esm/generated/contract.d.ts +134 -0
- package/dist/esm/generated/contract.js +230 -0
- package/dist/esm/secrets/envelope.d.ts +37 -0
- package/dist/esm/secrets/envelope.js +81 -0
- package/dist/esm/secrets/errors.d.ts +10 -0
- package/dist/esm/secrets/errors.js +15 -0
- package/dist/esm/secrets/index.d.ts +4 -0
- package/dist/esm/secrets/index.js +4 -0
- package/dist/esm/secrets/keystore.d.ts +37 -0
- package/dist/esm/secrets/keystore.js +37 -0
- package/dist/esm/secrets/os-stores.d.ts +17 -0
- package/dist/esm/secrets/os-stores.js +189 -0
- package/dist/esm/secrets/store.d.ts +32 -0
- package/dist/esm/secrets/store.js +156 -0
- package/dist/esm/secrets/testing.d.ts +5 -0
- package/dist/esm/secrets/testing.js +12 -0
- package/package.json +38 -4
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// xeno-dirs-check [dir] [--json] [--self-test]
|
|
3
|
+
// Exit 0: clean. 1: violations (or the self-test could not fail). 2: usage.
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { checkSource, RULES, scanText } from "./check.js";
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const flag = (n) => args.includes(n);
|
|
8
|
+
const positional = args.filter((a) => !a.startsWith('--'));
|
|
9
|
+
/** Proves the check can fail: every rule must fire on its own sample. Samples are assembled so this file stays clean. */
|
|
10
|
+
function selfTest() {
|
|
11
|
+
const h = 'home', d = 'dir';
|
|
12
|
+
const samples = {
|
|
13
|
+
'homedir-call': ['ts', `const p = os.${h}${d}();`],
|
|
14
|
+
'platform-dir-read': ['ts', `const a = process.env.APP${'DATA'};`],
|
|
15
|
+
'legacy-home-name': ['ts', `const x = join(base, '.xeno-${'agent'}');`],
|
|
16
|
+
'legacy-home-path': ['ts', `const x = '~/.${'xeno'}/config';`],
|
|
17
|
+
'legacy-home-env': ['ts', `const x = process.env.XENO_AGENT_${'HOME'};`],
|
|
18
|
+
};
|
|
19
|
+
let bad = 0;
|
|
20
|
+
for (const id of new Set(RULES.map((r) => r.id))) {
|
|
21
|
+
const [lang, text] = samples[id];
|
|
22
|
+
const hit = scanText('sample', text, lang).some((v) => v.rule === id);
|
|
23
|
+
const clean = scanText('sample', `// ${text}`, lang).length === 0;
|
|
24
|
+
const allowed = scanText('sample', `${text} // xeno-dirs-allow: self-test of the allow marker`, lang).length === 0;
|
|
25
|
+
if (!hit || !clean || !allowed) {
|
|
26
|
+
console.error(`self-test FAILED for ${id}: fires=${hit} comment-ignored=${clean} allow-honoured=${allowed}`);
|
|
27
|
+
bad++;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
console.log(bad === 0 ? `self-test ok: ${new Set(RULES.map((r) => r.id)).size} rules fire, comments and allow markers are honoured` : 'self-test FAILED');
|
|
31
|
+
return bad === 0 ? 0 : 1;
|
|
32
|
+
}
|
|
33
|
+
if (flag('--self-test'))
|
|
34
|
+
process.exit(selfTest());
|
|
35
|
+
if (positional.length > 1) {
|
|
36
|
+
console.error('usage: xeno-dirs-check [dir] [--json] [--self-test]');
|
|
37
|
+
process.exit(2);
|
|
38
|
+
}
|
|
39
|
+
const root = resolve(positional[0] ?? '.');
|
|
40
|
+
const result = checkSource(root);
|
|
41
|
+
if (flag('--json'))
|
|
42
|
+
console.log(JSON.stringify(result, null, 2));
|
|
43
|
+
else {
|
|
44
|
+
for (const v of result.violations)
|
|
45
|
+
console.log(`${v.file}:${v.line} ${v.rule} ${v.description}\n ${v.text}`);
|
|
46
|
+
console.log(result.ok ? `ok: ${result.filesScanned} files, no home-directory lookup or hard-coded XENO path outside the resolver` : `${result.violations.length} violation(s) in ${result.filesScanned} files`);
|
|
47
|
+
}
|
|
48
|
+
process.exit(result.ok ? 0 : 1);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reusable static check every product runs in CI (XENO LOCAL HOME - SPEC.md section 8, check 1): no home-directory
|
|
3
|
+
* lookup and no hard-coded XENO location outside the resolver. A violation is a defect, not a convenience.
|
|
4
|
+
*
|
|
5
|
+
* Rules (each with a stable id so an allow entry names exactly one):
|
|
6
|
+
* homedir-call a call that asks the OS for the home directory (homedir(), home_dir(), Path.home(), UserHomeDir())
|
|
7
|
+
* platform-dir-read reading HOME, USERPROFILE, APPDATA, LOCALAPPDATA or XDG_* from the environment to build a path
|
|
8
|
+
* legacy-home-name the legacy folders .xeno-agent, .xeno-anima, .xeno-code
|
|
9
|
+
* legacy-home-path ~/.xeno, %USERPROFILE%\.xeno, $HOME/.xeno and the %APPDATA%\XENO form (a per-project `.xeno/` is fine)
|
|
10
|
+
* legacy-home-env a XENO_*_HOME variable other than XENO_HOME (the thirteen legacy overrides)
|
|
11
|
+
* A line (or the line above it) carrying `xeno-dirs-allow: <reason>` is exempt; the reason is mandatory.
|
|
12
|
+
*/
|
|
13
|
+
export interface Rule {
|
|
14
|
+
id: string;
|
|
15
|
+
description: string;
|
|
16
|
+
re: RegExp;
|
|
17
|
+
langs: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
export declare const RULES: readonly Rule[];
|
|
20
|
+
export interface Violation {
|
|
21
|
+
file: string;
|
|
22
|
+
line: number;
|
|
23
|
+
rule: string;
|
|
24
|
+
description: string;
|
|
25
|
+
text: string;
|
|
26
|
+
}
|
|
27
|
+
export interface CheckConfig {
|
|
28
|
+
/** Files (relative, `/` separated, or `dir/**`) that are the resolver itself and may do what the rules forbid. */
|
|
29
|
+
resolverFiles?: string[];
|
|
30
|
+
exclude?: string[];
|
|
31
|
+
allow?: {
|
|
32
|
+
file: string;
|
|
33
|
+
rule: string;
|
|
34
|
+
reason: string;
|
|
35
|
+
}[];
|
|
36
|
+
}
|
|
37
|
+
export interface CheckResult {
|
|
38
|
+
ok: boolean;
|
|
39
|
+
filesScanned: number;
|
|
40
|
+
violations: Violation[];
|
|
41
|
+
config: string | null;
|
|
42
|
+
}
|
|
43
|
+
export declare const CONFIG_NAME = "xeno-dirs-check.json";
|
|
44
|
+
export declare function loadConfig(root: string): {
|
|
45
|
+
config: CheckConfig;
|
|
46
|
+
path: string | null;
|
|
47
|
+
};
|
|
48
|
+
export declare function scanText(file: string, text: string, lang: string): Violation[];
|
|
49
|
+
export declare function checkSource(root: string, override?: CheckConfig): CheckResult;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { lstatSync, readdirSync, readFileSync, existsSync } from 'node:fs';
|
|
2
|
+
import { join, relative, sep } from 'node:path';
|
|
3
|
+
const JS = ['ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs', 'mts', 'cts'];
|
|
4
|
+
export const RULES = [
|
|
5
|
+
{ id: 'homedir-call', description: 'asks the OS for the home directory; use the dirs resolver', re: /\b(?:os\.)?homedir\s*\(|\.userInfo\s*\(\s*\)\s*\.homedir\b/, langs: JS },
|
|
6
|
+
{ id: 'homedir-call', description: 'asks the OS for the home directory; use the dirs resolver', re: /\b(?:dirs::|env::)?home_dir\s*\(|\bdirs::(?:config|data|data_local|cache|state)_dir\s*\(|\bProjectDirs::from\b/, langs: ['rs'] },
|
|
7
|
+
{ id: 'homedir-call', description: 'asks the OS for the home directory; use the dirs resolver', re: /\bPath\.home\s*\(|\bexpanduser\s*\(|\bplatformdirs\b|\buser_(?:config|data|cache|state)_dir\s*\(/, langs: ['py'] },
|
|
8
|
+
{ id: 'homedir-call', description: 'asks the OS for the home directory; use the dirs resolver', re: /\bos\.User(?:Home|Config|Cache)Dir\s*\(/, langs: ['go'] },
|
|
9
|
+
{ id: 'platform-dir-read', description: 'reads a platform directory variable; use the dirs resolver', re: /\b(?:process\.env|env)(?:\.|\s*\[\s*['"])(?:HOME|USERPROFILE|APPDATA|LOCALAPPDATA|XDG_[A-Z_]+)\b/, langs: JS },
|
|
10
|
+
{ id: 'platform-dir-read', description: 'reads a platform directory variable; use the dirs resolver', re: /\bvar(?:_os)?\s*\(\s*"(?:HOME|USERPROFILE|APPDATA|LOCALAPPDATA|XDG_[A-Z_]+)"/, langs: ['rs'] },
|
|
11
|
+
{ id: 'platform-dir-read', description: 'reads a platform directory variable; use the dirs resolver', re: /\b(?:os\.environ(?:\.get\s*\(|\s*\[)|os\.getenv\s*\()\s*['"](?:HOME|USERPROFILE|APPDATA|LOCALAPPDATA|XDG_[A-Z_]+)['"]/, langs: ['py'] },
|
|
12
|
+
{ id: 'legacy-home-name', description: 'names a legacy XENO home; use the dirs resolver', re: /(?<![\w-])\.xeno-(?:agent|anima|code)(?![\w-])/, langs: [...JS, 'rs', 'py', 'go'] },
|
|
13
|
+
{ id: 'legacy-home-path', description: 'builds a path into the legacy home folder or the XENO app-data folder by hand', re: /(?:~|\$\{?HOME\}?|%USERPROFILE%|%HOMEPATH%|\$env:USERPROFILE)[\\/]+\.xeno(?![\w-])|(?:%APPDATA%|%LOCALAPPDATA%|AppData[\\/]+(?:Roaming|Local))[\\/]+XENO\b/, langs: [...JS, 'rs', 'py', 'go'] },
|
|
14
|
+
{ id: 'legacy-home-env', description: 'uses a legacy XENO_*_HOME variable; XENO_HOME is the only override', re: /\bXENO_(?!HOME\b)[A-Z0-9_]*_HOME\b/, langs: [...JS, 'rs', 'py', 'go'] },
|
|
15
|
+
];
|
|
16
|
+
const DEFAULT_EXCLUDE_DIRS = new Set(['node_modules', 'dist', 'build', 'target', '.git', 'coverage', '.next', 'out', '.turbo', '.venv', '__pycache__', 'release', '.worktrees']);
|
|
17
|
+
const EXT_LANG = Object.fromEntries([...JS.map((e) => [e, e]), ['rs', 'rs'], ['py', 'py'], ['go', 'go']]);
|
|
18
|
+
const MAX_BYTES = 2_000_000;
|
|
19
|
+
function globToRe(g) {
|
|
20
|
+
// Placeholders first, so the expansions below are not themselves rewritten by the single-star rule.
|
|
21
|
+
const esc = g
|
|
22
|
+
.replace(/\*\*\//g, '@@DIR@@')
|
|
23
|
+
.replace(/\*\*/g, '@@ANY@@')
|
|
24
|
+
.replace(/\*/g, '@@SEG@@')
|
|
25
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
26
|
+
.replace(/@@DIR@@/g, '(?:.*/)?')
|
|
27
|
+
.replace(/@@ANY@@/g, '.*')
|
|
28
|
+
.replace(/@@SEG@@/g, '[^/]*');
|
|
29
|
+
return new RegExp(`^${esc}$`);
|
|
30
|
+
}
|
|
31
|
+
export const CONFIG_NAME = 'xeno-dirs-check.json';
|
|
32
|
+
export function loadConfig(root) {
|
|
33
|
+
const p = join(root, CONFIG_NAME);
|
|
34
|
+
if (!existsSync(p))
|
|
35
|
+
return { config: {}, path: null };
|
|
36
|
+
const c = JSON.parse(readFileSync(p, 'utf8'));
|
|
37
|
+
for (const a of c.allow ?? [])
|
|
38
|
+
if (!a.reason || a.reason.trim().length < 8)
|
|
39
|
+
throw new Error(`${CONFIG_NAME}: allow entry for ${a.file} / ${a.rule} needs a real reason`);
|
|
40
|
+
return { config: c, path: p };
|
|
41
|
+
}
|
|
42
|
+
/** Strips comment text so documentation that names a path is not a violation. Keeps line numbering. */
|
|
43
|
+
function codeLines(text, lang) {
|
|
44
|
+
const out = [];
|
|
45
|
+
let block = false;
|
|
46
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
47
|
+
let line = raw;
|
|
48
|
+
let comment = '';
|
|
49
|
+
if (lang === 'py') {
|
|
50
|
+
const i = line.indexOf('#');
|
|
51
|
+
if (i >= 0) {
|
|
52
|
+
comment = line.slice(i);
|
|
53
|
+
line = line.slice(0, i);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
if (block) {
|
|
58
|
+
const end = line.indexOf('*/');
|
|
59
|
+
if (end < 0) {
|
|
60
|
+
out.push({ code: '', comment: line });
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
comment = line.slice(0, end + 2);
|
|
64
|
+
line = line.slice(end + 2);
|
|
65
|
+
block = false;
|
|
66
|
+
}
|
|
67
|
+
const t = line.trimStart();
|
|
68
|
+
if (t.startsWith('//')) {
|
|
69
|
+
out.push({ code: '', comment: line });
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const open = line.indexOf('/*');
|
|
73
|
+
if (open >= 0 && !/['"`].*\/\*.*['"`]/.test(line)) {
|
|
74
|
+
const close = line.indexOf('*/', open + 2);
|
|
75
|
+
if (close < 0) {
|
|
76
|
+
block = true;
|
|
77
|
+
comment += line.slice(open);
|
|
78
|
+
line = line.slice(0, open);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
comment += line.slice(open, close + 2);
|
|
82
|
+
line = line.slice(0, open) + line.slice(close + 2);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const trailing = line.search(/\s\/\/\s/);
|
|
86
|
+
if (trailing >= 0) {
|
|
87
|
+
comment += line.slice(trailing);
|
|
88
|
+
line = line.slice(0, trailing);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
out.push({ code: line, comment });
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
const ALLOW = /xeno-dirs-allow:\s*(\S.*)$/;
|
|
96
|
+
export function scanText(file, text, lang) {
|
|
97
|
+
const lines = codeLines(text, lang);
|
|
98
|
+
const found = [];
|
|
99
|
+
const rules = RULES.filter((r) => r.langs.includes(lang));
|
|
100
|
+
lines.forEach((l, i) => {
|
|
101
|
+
const allowHere = ALLOW.exec(l.comment) ?? (i > 0 ? ALLOW.exec(lines[i - 1].comment) : null);
|
|
102
|
+
if (/xeno-dirs-allow:\s*$/.test(l.comment) || (allowHere !== null && allowHere[1].trim().length < 4)) {
|
|
103
|
+
found.push({ file, line: i + 1, rule: 'allow-without-reason', description: 'xeno-dirs-allow needs a reason', text: l.comment.trim().slice(0, 160) });
|
|
104
|
+
}
|
|
105
|
+
if (l.code.trim() === '')
|
|
106
|
+
return;
|
|
107
|
+
for (const r of rules) {
|
|
108
|
+
if (r.re.test(l.code) && allowHere === null) {
|
|
109
|
+
if (!found.some((f) => f.line === i + 1 && f.rule === r.id))
|
|
110
|
+
found.push({ file, line: i + 1, rule: r.id, description: r.description, text: l.code.trim().slice(0, 160) });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return found;
|
|
115
|
+
}
|
|
116
|
+
export function checkSource(root, override) {
|
|
117
|
+
const { config: fileConfig, path } = loadConfig(root);
|
|
118
|
+
const config = { ...fileConfig, ...override };
|
|
119
|
+
const resolver = (config.resolverFiles ?? []).map(globToRe);
|
|
120
|
+
const exclude = (config.exclude ?? []).map(globToRe);
|
|
121
|
+
const allow = (config.allow ?? []).map((a) => ({ f: globToRe(a.file), rule: a.rule }));
|
|
122
|
+
const violations = [];
|
|
123
|
+
let scanned = 0;
|
|
124
|
+
const walk = (dir) => {
|
|
125
|
+
for (const name of readdirSync(dir)) {
|
|
126
|
+
const abs = join(dir, name);
|
|
127
|
+
const st = lstatSync(abs);
|
|
128
|
+
if (st.isSymbolicLink())
|
|
129
|
+
continue; // never follow a link out of the tree
|
|
130
|
+
const rel = relative(root, abs).split(sep).join('/');
|
|
131
|
+
if (st.isDirectory()) {
|
|
132
|
+
if (!DEFAULT_EXCLUDE_DIRS.has(name) && !exclude.some((e) => e.test(rel + '/')))
|
|
133
|
+
walk(abs);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const ext = name.includes('.') ? name.slice(name.lastIndexOf('.') + 1).toLowerCase() : '';
|
|
137
|
+
const lang = EXT_LANG[ext];
|
|
138
|
+
if (lang === undefined || st.size > MAX_BYTES || name.endsWith('.min.js') || name.endsWith('.d.ts'))
|
|
139
|
+
continue;
|
|
140
|
+
if (exclude.some((e) => e.test(rel)) || resolver.some((e) => e.test(rel)))
|
|
141
|
+
continue;
|
|
142
|
+
scanned++;
|
|
143
|
+
for (const v of scanText(rel, readFileSync(abs, 'utf8'), lang)) {
|
|
144
|
+
if (allow.some((a) => a.f.test(rel) && a.rule === v.rule))
|
|
145
|
+
continue;
|
|
146
|
+
violations.push(v);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
walk(root);
|
|
151
|
+
return { ok: violations.length === 0, filesScanned: scanned, violations, config: path };
|
|
152
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// xeno-home print every resolved location (the engine behind `xeno home`)
|
|
3
|
+
// xeno-home doctor [--json] list entries no registry path allows (`xeno home doctor`; exit 1 if any)
|
|
4
|
+
// xeno-home migrate [--confirm] copy legacy homes into the layout (dry run unless --confirm; never deletes)
|
|
5
|
+
// xeno-home inputs show the owner-decided contract inputs
|
|
6
|
+
import { contractInputs } from "./inputs.js";
|
|
7
|
+
import { createDirs } from "./dirs.js";
|
|
8
|
+
import { doctor } from "./doctor.js";
|
|
9
|
+
import { hostHome } from "./host.js";
|
|
10
|
+
import { planMigration, runMigration } from "./migrate.js";
|
|
11
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
12
|
+
const json = rest.includes('--json');
|
|
13
|
+
const out = (v) => console.log(JSON.stringify(v, null, 2));
|
|
14
|
+
async function main() {
|
|
15
|
+
const dirs = createDirs();
|
|
16
|
+
if (cmd === undefined || cmd === 'home') {
|
|
17
|
+
const d = dirs.describe();
|
|
18
|
+
if (json)
|
|
19
|
+
out(d);
|
|
20
|
+
else {
|
|
21
|
+
console.log(`platform: ${d.platform}${d.overridden ? ' (XENO_HOME override)' : ''}`);
|
|
22
|
+
for (const [k, v] of Object.entries(d.roots))
|
|
23
|
+
console.log(`${k.padEnd(8)}${v}`);
|
|
24
|
+
console.log(`layout: ${d.layout.state}${'layout' in d.layout ? ` (${d.layout.layout})` : ''}`);
|
|
25
|
+
for (const w of d.warnings)
|
|
26
|
+
console.log(`warning: ${w}`);
|
|
27
|
+
}
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
if (cmd === 'doctor') {
|
|
31
|
+
const r = doctor(dirs, { home: hostHome() });
|
|
32
|
+
if (json)
|
|
33
|
+
out(r);
|
|
34
|
+
else {
|
|
35
|
+
for (const u of r.unregistered)
|
|
36
|
+
console.log(`UNREGISTERED ${u.kind}/${u.path} (${u.type}; ${u.reason})`);
|
|
37
|
+
for (const l of r.legacy.filter((x) => x.present))
|
|
38
|
+
console.log(`legacy home ${l.path} (run: xeno-home migrate)`);
|
|
39
|
+
console.log(r.ok ? 'ok: no unregistered entry in any XENO root' : `${r.unregistered.length} unregistered entr${r.unregistered.length === 1 ? 'y' : 'ies'}`);
|
|
40
|
+
}
|
|
41
|
+
return r.ok ? 0 : 1;
|
|
42
|
+
}
|
|
43
|
+
if (cmd === 'migrate') {
|
|
44
|
+
const plan = planMigration({ dirs, home: hostHome() });
|
|
45
|
+
const confirm = rest.includes('--confirm');
|
|
46
|
+
const result = await runMigration(plan, { dirs, confirm });
|
|
47
|
+
if (json)
|
|
48
|
+
out({ plan: { id: plan.id, homes: plan.homes, entries: plan.entries.length }, result });
|
|
49
|
+
else {
|
|
50
|
+
console.log(`${confirm ? 'migrated' : 'DRY RUN (nothing written; pass --confirm)'} plan ${plan.id}`);
|
|
51
|
+
for (const h of plan.homes)
|
|
52
|
+
console.log(` ${h.name.padEnd(14)}${h.state}`);
|
|
53
|
+
for (const e of result.entries)
|
|
54
|
+
console.log(` ${e.status.padEnd(9)}${e.legacyHome}/${e.sourceRel} -> ${e.destRel}${e.reason ? ` (${e.reason})` : ''}`);
|
|
55
|
+
for (const r of result.refused)
|
|
56
|
+
console.log(` refused ${r.path} (${r.reason})`);
|
|
57
|
+
console.log(` unplaced: ${result.unplaced.count}${result.unplaced.truncated ? '+' : ''} (reported, never guessed)`);
|
|
58
|
+
if (confirm)
|
|
59
|
+
console.log(result.ok ? 'ok: every copy verified by hash; legacy homes left in place' : 'NOT ok: a product must not switch yet');
|
|
60
|
+
}
|
|
61
|
+
return confirm && !result.ok ? 1 : 0;
|
|
62
|
+
}
|
|
63
|
+
if (cmd === 'inputs') {
|
|
64
|
+
out(contractInputs());
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
console.error('usage: xeno-home [home|doctor|migrate [--confirm]|inputs] [--json]');
|
|
68
|
+
return 2;
|
|
69
|
+
}
|
|
70
|
+
main().then((c) => process.exit(c), (e) => { console.error(`error: ${e.message}`); process.exit(1); });
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type AtomicWriteOptions } from './atomic.ts';
|
|
2
|
+
import { type LayoutState } from './layout.ts';
|
|
3
|
+
import { type AcquireOptions, type LockHandle } from './lock.ts';
|
|
4
|
+
import { type Kind, type Platform, type ResolvedRoots } from './roots.ts';
|
|
5
|
+
export interface CreateDirsOptions {
|
|
6
|
+
platform?: Platform;
|
|
7
|
+
home?: string;
|
|
8
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The resolver. Every XENO path comes from here; nothing else may build one. It refuses anything the registry does
|
|
12
|
+
* not list, and refuses to write when the layout on disk is newer than this build understands.
|
|
13
|
+
*/
|
|
14
|
+
export declare class XenoDirs {
|
|
15
|
+
readonly platform: Platform;
|
|
16
|
+
readonly roots: Readonly<Record<Kind, string>>;
|
|
17
|
+
readonly warnings: readonly string[];
|
|
18
|
+
readonly overridden: boolean;
|
|
19
|
+
constructor(resolved: ResolvedRoots);
|
|
20
|
+
/** Root directory of a kind. */
|
|
21
|
+
root(kind: Kind): string;
|
|
22
|
+
/** Path relative to the kind root, `/`-separated, after registry validation. */
|
|
23
|
+
relative(kind: string, area: string, ...segments: string[]): string;
|
|
24
|
+
/** Absolute path for (kind, area, ...segments). Throws DirsError for anything unregistered or invalid. */
|
|
25
|
+
path(kind: Kind, area: string, ...segments: string[]): string;
|
|
26
|
+
/** `run/<area>/<name>.lock`. */
|
|
27
|
+
lockPath(area: string, name: string): string;
|
|
28
|
+
/** `data/layout.json`. */
|
|
29
|
+
layoutPath(): string;
|
|
30
|
+
/** `<kind>/<area>/area.json`, the optional per-area schema marker. */
|
|
31
|
+
areaMetaPath(kind: Kind, area: string): string;
|
|
32
|
+
/** Reads data/layout.json. Never throws for a missing file. */
|
|
33
|
+
readLayout(): LayoutState;
|
|
34
|
+
/** Throws unless this build may write: a newer or unreadable layout marker refuses (spec section 8). */
|
|
35
|
+
assertWritable(): LayoutState;
|
|
36
|
+
/** Creates data/layout.json at the supported version if absent; otherwise checks it. */
|
|
37
|
+
ensureLayout(): Promise<LayoutState>;
|
|
38
|
+
/** The only sanctioned way to write below the roots: registry check, layout check, atomic replace. */
|
|
39
|
+
writeFile(kind: Kind, area: string, segments: readonly string[], data: string | Uint8Array, options?: AtomicWriteOptions): Promise<string>;
|
|
40
|
+
/** Reads a registered file; returns null when absent. */
|
|
41
|
+
readFile(kind: Kind, area: string, segments: readonly string[]): Promise<Buffer | null>;
|
|
42
|
+
/** Acquires `run/<area>/<name>.lock` (spec section 8). */
|
|
43
|
+
acquireLock(area: string, name: string, options?: AcquireOptions): Promise<LockHandle>;
|
|
44
|
+
withLock<T>(area: string, name: string, fn: () => Promise<T>, options?: AcquireOptions): Promise<T>;
|
|
45
|
+
/** Every resolved location, for `xeno home`. */
|
|
46
|
+
describe(): {
|
|
47
|
+
platform: Platform;
|
|
48
|
+
overridden: boolean;
|
|
49
|
+
roots: Record<Kind, string>;
|
|
50
|
+
warnings: readonly string[];
|
|
51
|
+
layout: LayoutState;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Builds a resolver from the host (default) or from explicit inputs (tests, vectors, a fixture root). */
|
|
55
|
+
export declare function createDirs(options?: CreateDirsOptions): XenoDirs;
|
|
56
|
+
/** A resolver rooted entirely under one directory via XENO_HOME; the supported way to isolate tests and fixtures. */
|
|
57
|
+
export declare function createDirsAt(xenoHome: string, platform?: Platform): XenoDirs;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { promises as fsp, readFileSync } from 'node:fs';
|
|
2
|
+
import { DirsError } from "./errors.js";
|
|
3
|
+
import { atomicWriteFile } from "./atomic.js";
|
|
4
|
+
import { hostEnv, hostHome, hostPlatform } from "./host.js";
|
|
5
|
+
import { parseLayout, SUPPORTED_LAYOUT } from "./layout.js";
|
|
6
|
+
import { acquireLockFile } from "./lock.js";
|
|
7
|
+
import { lockRelativePath, registeredRelativePath } from "./registry.js";
|
|
8
|
+
import { joinPath, resolveRoots, SEP } from "./roots.js";
|
|
9
|
+
/**
|
|
10
|
+
* The resolver. Every XENO path comes from here; nothing else may build one. It refuses anything the registry does
|
|
11
|
+
* not list, and refuses to write when the layout on disk is newer than this build understands.
|
|
12
|
+
*/
|
|
13
|
+
export class XenoDirs {
|
|
14
|
+
platform;
|
|
15
|
+
roots;
|
|
16
|
+
warnings;
|
|
17
|
+
overridden;
|
|
18
|
+
constructor(resolved) {
|
|
19
|
+
this.platform = resolved.platform;
|
|
20
|
+
this.roots = resolved.roots;
|
|
21
|
+
this.warnings = resolved.warnings;
|
|
22
|
+
this.overridden = resolved.overridden;
|
|
23
|
+
}
|
|
24
|
+
/** Root directory of a kind. */
|
|
25
|
+
root(kind) {
|
|
26
|
+
return this.roots[kind];
|
|
27
|
+
}
|
|
28
|
+
/** Path relative to the kind root, `/`-separated, after registry validation. */
|
|
29
|
+
relative(kind, area, ...segments) {
|
|
30
|
+
return registeredRelativePath(kind, area, segments);
|
|
31
|
+
}
|
|
32
|
+
/** Absolute path for (kind, area, ...segments). Throws DirsError for anything unregistered or invalid. */
|
|
33
|
+
path(kind, area, ...segments) {
|
|
34
|
+
const rel = registeredRelativePath(kind, area, segments);
|
|
35
|
+
return joinPath(this.platform, this.roots[kind], ...rel.split('/'));
|
|
36
|
+
}
|
|
37
|
+
/** `run/<area>/<name>.lock`. */
|
|
38
|
+
lockPath(area, name) {
|
|
39
|
+
return joinPath(this.platform, this.roots.run, ...lockRelativePath(area, name).split('/'));
|
|
40
|
+
}
|
|
41
|
+
/** `data/layout.json`. */
|
|
42
|
+
layoutPath() {
|
|
43
|
+
return this.roots.data + SEP[this.platform] + 'layout.json';
|
|
44
|
+
}
|
|
45
|
+
/** `<kind>/<area>/area.json`, the optional per-area schema marker. */
|
|
46
|
+
areaMetaPath(kind, area) {
|
|
47
|
+
return this.path(kind, area, 'area.json');
|
|
48
|
+
}
|
|
49
|
+
/** Reads data/layout.json. Never throws for a missing file. */
|
|
50
|
+
readLayout() {
|
|
51
|
+
let text = null;
|
|
52
|
+
try {
|
|
53
|
+
text = readFileSync(this.layoutPath(), 'utf8');
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
if (e.code !== 'ENOENT')
|
|
57
|
+
return { state: 'invalid', reason: 'unreadable' };
|
|
58
|
+
}
|
|
59
|
+
return parseLayout(text);
|
|
60
|
+
}
|
|
61
|
+
/** Throws unless this build may write: a newer or unreadable layout marker refuses (spec section 8). */
|
|
62
|
+
assertWritable() {
|
|
63
|
+
const s = this.readLayout();
|
|
64
|
+
if (s.state === 'newer') {
|
|
65
|
+
throw new DirsError('layout-newer', `the XENO layout on disk is version ${s.layout}; this build understands up to ${SUPPORTED_LAYOUT}. Update the product before it writes.`, { found: s.layout, supported: SUPPORTED_LAYOUT });
|
|
66
|
+
}
|
|
67
|
+
if (s.state === 'invalid') {
|
|
68
|
+
throw new DirsError('layout-invalid', `data/layout.json is not readable (${s.reason}); refusing to write`, { reason: s.reason });
|
|
69
|
+
}
|
|
70
|
+
return s;
|
|
71
|
+
}
|
|
72
|
+
/** Creates data/layout.json at the supported version if absent; otherwise checks it. */
|
|
73
|
+
async ensureLayout() {
|
|
74
|
+
const s = this.assertWritable();
|
|
75
|
+
if (s.state === 'missing') {
|
|
76
|
+
await atomicWriteFile(this.layoutPath(), JSON.stringify({ layout: SUPPORTED_LAYOUT }) + '\n');
|
|
77
|
+
return { state: 'ok', layout: SUPPORTED_LAYOUT };
|
|
78
|
+
}
|
|
79
|
+
return s;
|
|
80
|
+
}
|
|
81
|
+
/** The only sanctioned way to write below the roots: registry check, layout check, atomic replace. */
|
|
82
|
+
async writeFile(kind, area, segments, data, options = {}) {
|
|
83
|
+
const target = this.path(kind, area, ...segments);
|
|
84
|
+
await this.ensureLayout();
|
|
85
|
+
await atomicWriteFile(target, data, kind === 'secrets' ? { mode: 0o600, ...options } : options);
|
|
86
|
+
return target;
|
|
87
|
+
}
|
|
88
|
+
/** Reads a registered file; returns null when absent. */
|
|
89
|
+
async readFile(kind, area, segments) {
|
|
90
|
+
try {
|
|
91
|
+
return await fsp.readFile(this.path(kind, area, ...segments));
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
if (e.code === 'ENOENT')
|
|
95
|
+
return null;
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Acquires `run/<area>/<name>.lock` (spec section 8). */
|
|
100
|
+
acquireLock(area, name, options) {
|
|
101
|
+
return acquireLockFile(this.lockPath(area, name), options);
|
|
102
|
+
}
|
|
103
|
+
async withLock(area, name, fn, options) {
|
|
104
|
+
const lock = await this.acquireLock(area, name, options);
|
|
105
|
+
try {
|
|
106
|
+
return await fn();
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
await lock.release();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Every resolved location, for `xeno home`. */
|
|
113
|
+
describe() {
|
|
114
|
+
return { platform: this.platform, overridden: this.overridden, roots: { ...this.roots }, warnings: this.warnings, layout: this.readLayout() };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Builds a resolver from the host (default) or from explicit inputs (tests, vectors, a fixture root). */
|
|
118
|
+
export function createDirs(options = {}) {
|
|
119
|
+
const input = {
|
|
120
|
+
platform: options.platform ?? hostPlatform(),
|
|
121
|
+
home: options.home ?? hostHome(),
|
|
122
|
+
env: options.env ?? hostEnv(),
|
|
123
|
+
};
|
|
124
|
+
return new XenoDirs(resolveRoots(input));
|
|
125
|
+
}
|
|
126
|
+
/** A resolver rooted entirely under one directory via XENO_HOME; the supported way to isolate tests and fixtures. */
|
|
127
|
+
export function createDirsAt(xenoHome, platform = hostPlatform()) {
|
|
128
|
+
return new XenoDirs(resolveRoots({ platform, home: '', env: { XENO_HOME: xenoHome } }));
|
|
129
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { XenoDirs } from './dirs.ts';
|
|
2
|
+
import type { LayoutState } from './layout.ts';
|
|
3
|
+
import { type Kind } from './roots.ts';
|
|
4
|
+
export interface Unregistered {
|
|
5
|
+
kind: Kind;
|
|
6
|
+
/** Path relative to the kind root, `/` separated. */
|
|
7
|
+
path: string;
|
|
8
|
+
type: 'file' | 'directory' | 'link' | 'other';
|
|
9
|
+
reason: string;
|
|
10
|
+
}
|
|
11
|
+
export interface DoctorReport {
|
|
12
|
+
ok: boolean;
|
|
13
|
+
platform: string;
|
|
14
|
+
roots: Record<Kind, string>;
|
|
15
|
+
layout: LayoutState;
|
|
16
|
+
warnings: readonly string[];
|
|
17
|
+
unregistered: Unregistered[];
|
|
18
|
+
legacy: {
|
|
19
|
+
name: string;
|
|
20
|
+
path: string;
|
|
21
|
+
present: boolean;
|
|
22
|
+
}[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `xeno home doctor`: lists every entry under each root that the registry does not allow (spec section 8, check 2).
|
|
26
|
+
* Registration is by area; what is inside an area belongs to its owner and is not inspected. A root that does not
|
|
27
|
+
* exist yet is fine. Reads only; never changes anything.
|
|
28
|
+
*/
|
|
29
|
+
export declare function doctor(dirs: XenoDirs, options?: {
|
|
30
|
+
home?: string;
|
|
31
|
+
}): DoctorReport;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { lstatSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { INPUTS_JSON } from "../generated/contract.js";
|
|
3
|
+
import { AREAS, findArea } from "./registry.js";
|
|
4
|
+
import { KINDS } from "./roots.js";
|
|
5
|
+
const typeOf = (p) => {
|
|
6
|
+
const s = lstatSync(p);
|
|
7
|
+
return s.isSymbolicLink() ? 'link' : s.isDirectory() ? 'directory' : s.isFile() ? 'file' : 'other';
|
|
8
|
+
};
|
|
9
|
+
const safeList = (p) => { try {
|
|
10
|
+
return readdirSync(p);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return [];
|
|
14
|
+
} };
|
|
15
|
+
const sameOrInside = (parent, child, sep) => {
|
|
16
|
+
const norm = (x) => (sep === '\\' ? x.toLowerCase() : x);
|
|
17
|
+
return norm(child).startsWith(norm(parent) + sep);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* `xeno home doctor`: lists every entry under each root that the registry does not allow (spec section 8, check 2).
|
|
21
|
+
* Registration is by area; what is inside an area belongs to its owner and is not inspected. A root that does not
|
|
22
|
+
* exist yet is fine. Reads only; never changes anything.
|
|
23
|
+
*/
|
|
24
|
+
export function doctor(dirs, options = {}) {
|
|
25
|
+
const sep = dirs.platform === 'windows' ? '\\' : '/';
|
|
26
|
+
const unregistered = [];
|
|
27
|
+
for (const kind of KINDS) {
|
|
28
|
+
const root = dirs.root(kind);
|
|
29
|
+
// Roots nested in another root (Linux secrets under data; the run fallback under state) are allowed there.
|
|
30
|
+
const nestedRoots = KINDS.filter((k) => k !== kind && sameOrInside(root, dirs.root(k), sep)).map((k) => dirs.root(k).slice(root.length + 1).split(sep)[0]);
|
|
31
|
+
const topAllowed = new Set(nestedRoots);
|
|
32
|
+
if (kind === 'data')
|
|
33
|
+
topAllowed.add('layout.json');
|
|
34
|
+
const areasOfKind = AREAS.filter((a) => kind === 'run' || a.uses.includes(kind));
|
|
35
|
+
for (const name of safeList(root)) {
|
|
36
|
+
const abs = root + sep + name;
|
|
37
|
+
if (topAllowed.has(name))
|
|
38
|
+
continue;
|
|
39
|
+
const top = areasOfKind.filter((a) => a.area.split('/')[0] === name);
|
|
40
|
+
if (top.length === 0) {
|
|
41
|
+
const elsewhere = AREAS.some((a) => a.area.split('/')[0] === name);
|
|
42
|
+
unregistered.push({ kind, path: name, type: typeOf(abs), reason: elsewhere ? `area ${name} is not registered for kind ${kind}` : 'not a registered area' });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
// An area with a deeper name (agents/definitions, apps/<slug>) is checked one level down.
|
|
46
|
+
const deeper = top.filter((a) => a.area.includes('/'));
|
|
47
|
+
if (deeper.length === 0)
|
|
48
|
+
continue;
|
|
49
|
+
for (const child of safeList(abs)) {
|
|
50
|
+
const full = `${name}/${child}`;
|
|
51
|
+
const entry = findArea(full);
|
|
52
|
+
if (entry === null || !(kind === 'run' || entry.uses.includes(kind))) {
|
|
53
|
+
unregistered.push({ kind, path: full, type: typeOf(abs + sep + child), reason: entry === null ? 'not a registered area' : `area ${full} is not registered for kind ${kind}` });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const home = options.home;
|
|
59
|
+
const legacy = home === undefined ? [] : INPUTS_JSON.legacyHomes.map((name) => {
|
|
60
|
+
const path = home + sep + name;
|
|
61
|
+
let present = false;
|
|
62
|
+
try {
|
|
63
|
+
present = lstatSync(path).isDirectory() || lstatSync(path).isSymbolicLink();
|
|
64
|
+
}
|
|
65
|
+
catch { /* absent */ }
|
|
66
|
+
return { name, path, present };
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
ok: unregistered.length === 0,
|
|
70
|
+
platform: dirs.platform,
|
|
71
|
+
roots: { ...dirs.roots },
|
|
72
|
+
layout: dirs.readLayout(),
|
|
73
|
+
warnings: dirs.warnings,
|
|
74
|
+
unregistered: unregistered.sort((a, b) => { const x = a.kind + a.path, y = b.kind + b.path; return x < y ? -1 : x > y ? 1 : 0; }),
|
|
75
|
+
legacy,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type DirsErrorCode = 'home-unresolvable' | 'unregistered-area' | 'kind-not-used' | 'locks-only' | 'invalid-segment' | 'layout-newer' | 'layout-invalid' | 'lock-held' | 'lock-corrupt' | 'lock-timeout' | 'migration-refused' | 'invalid-argument';
|
|
2
|
+
/** Every error this family throws. `code` is stable and is what callers branch on. */
|
|
3
|
+
export declare class DirsError extends Error {
|
|
4
|
+
readonly code: DirsErrorCode;
|
|
5
|
+
readonly detail: Record<string, string | number | boolean | null>;
|
|
6
|
+
constructor(code: DirsErrorCode, message: string, detail?: Record<string, string | number | boolean | null>);
|
|
7
|
+
}
|
|
8
|
+
export declare function isDirsError(e: unknown, code?: DirsErrorCode): e is DirsError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Every error this family throws. `code` is stable and is what callers branch on. */
|
|
2
|
+
export class DirsError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
detail;
|
|
5
|
+
constructor(code, message, detail = {}) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'DirsError';
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.detail = detail;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function isDirsError(e, code) {
|
|
13
|
+
return e instanceof DirsError && (code === undefined || e.code === code);
|
|
14
|
+
}
|