@starci/eslint-canon-be 3.8.19 → 3.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/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/runtime/bundle.json +192 -0
- package/runtime/engine/by-code-unit.mjs +6 -6
- package/runtime/engine/model-config.mjs +98 -76
- package/runtime/engine/orca-config.mjs +22 -17
- package/runtime/engine/release-config.mjs +22 -18
- package/runtime/engine/removed-vocabulary.mjs +21 -20
- package/runtime/engine/resources-config.mjs +42 -35
- package/runtime/engine/runtime-root.mjs +11 -13
- package/runtime/engine/secrets.mjs +170 -102
- package/runtime/engine/sonar-config.mjs +16 -13
- package/runtime/engine/temp-root.mjs +3 -9
- package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
- package/runtime/knowledge/hfs/rules.yaml +6 -6
- package/runtime/knowledge/hfs/slots.yaml +2 -2
- package/runtime/knowledge/sonar-gate.yaml +1 -1
- package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
- package/runtime/scripts/api/git/cat-file.mjs +0 -1
- package/runtime/scripts/api/git/config-get.mjs +1 -2
- package/runtime/scripts/api/git/diff.mjs +0 -1
- package/runtime/scripts/api/git/lib.mjs +32 -32
- package/runtime/scripts/api/git/ls-files.mjs +0 -1
- package/runtime/scripts/api/git/ls-tree.mjs +0 -1
- package/runtime/scripts/api/git/merge-base.mjs +2 -3
- package/runtime/scripts/api/git/remote.mjs +0 -1
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
- package/runtime/scripts/api/git/show.mjs +0 -1
- package/runtime/scripts/hfs/allows.mjs +37 -38
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
- package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
- package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
- package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
- package/runtime/scripts/hfs/architecture/config.mjs +302 -268
- package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
- package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
- package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
- package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
- package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
- package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
- package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
- package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
- package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
- package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
- package/runtime/scripts/hfs/architecture/index.mjs +272 -272
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
- package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
- package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
- package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
- package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
- package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
- package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
- package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
- package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
- package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
- package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
- package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
- package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
- package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
- package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
- package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
- package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
- package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
- package/runtime/scripts/hfs/edition-slots.mjs +43 -40
- package/runtime/scripts/hfs/linear-text.mjs +20 -20
- package/runtime/scripts/hfs/param-names.mjs +6 -11
- package/runtime/scripts/hfs/path-findings.mjs +120 -100
- package/runtime/scripts/hfs/repo-identity.mjs +55 -42
- package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
- package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
- package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
- package/runtime/scripts/hfs/rules/read.mjs +19 -15
- package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
- package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
- package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
- package/runtime/scripts/hfs/slot-classify.mjs +42 -38
- package/runtime/scripts/hfs/slot-errors.mjs +8 -7
- package/runtime/scripts/hfs/slot-imports.mjs +70 -63
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
- package/runtime/scripts/hfs/slot-match.mjs +122 -111
- package/runtime/scripts/hfs/slot-path.mjs +0 -2
- package/runtime/scripts/hfs/slot-required.mjs +65 -59
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
- package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
- package/runtime/scripts/hfs/slots.mjs +175 -152
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
- package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
- package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
- package/runtime/scripts/hfs/view.mjs +38 -40
- package/runtime/scripts/lib/dockerfile.mjs +131 -114
- package/runtime/scripts/lib/env.mjs +3 -6
- package/runtime/scripts/lib/event-contract.mjs +73 -78
- package/runtime/scripts/lib/git.mjs +15 -16
- package/runtime/scripts/lib/glob.mjs +23 -14
- package/runtime/scripts/lib/grammar-lint.mjs +33 -37
- package/runtime/scripts/lib/i18n.mjs +32 -35
- package/runtime/scripts/lib/in-order.mjs +57 -38
- package/runtime/scripts/lib/json-clone.mjs +45 -36
- package/runtime/scripts/lib/json.mjs +26 -14
- package/runtime/scripts/lib/language.mjs +133 -129
- package/runtime/scripts/lib/list.mjs +9 -14
- package/runtime/scripts/lib/mutation-fence.mjs +10 -5
- package/runtime/scripts/lib/path-key.mjs +53 -38
- package/runtime/scripts/lib/read-text.mjs +30 -17
- package/runtime/scripts/lib/regex.mjs +1 -2
- package/runtime/scripts/lib/same-text.mjs +0 -1
- package/runtime/scripts/lib/sops-envelope.mjs +148 -120
- package/runtime/scripts/lib/stack-services.mjs +128 -129
- package/runtime/scripts/lib/ts-analysis.mjs +28 -26
- package/runtime/scripts/lib/ts-ast.mjs +36 -42
- package/runtime/scripts/lib/walk.mjs +33 -27
- package/runtime/scripts/lib/yaml-cached.mjs +4 -4
|
@@ -1,36 +1,35 @@
|
|
|
1
1
|
// git.mjs — pure helpers over git's text output, spawn results and env. The git spawn itself lives in scripts/api/git/ (lib.mjs and its call files).
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* git's C-quoted diff path decoded - `"b\303\251"` a `+++ ` header line prints when core.quotePath
|
|
5
4
|
* covers the name. Octal escapes become \u00XX for JSON.parse (which answers \n, \t, \" and \\);
|
|
6
5
|
* a value not wrapped in quotes passes through unchanged.
|
|
7
6
|
*/
|
|
8
|
-
export const unquoteDiffPath = (value) =>
|
|
9
|
-
|
|
10
|
-
? JSON.parse(value.replace(/\\([0-7]{3})/g, (_, octal) => String.raw`\u00${Number.parseInt(octal, 8).toString(16).padStart(2, '0')}`))
|
|
7
|
+
export const unquoteDiffPath = (value) => /^".*"$/.test(value)
|
|
8
|
+
? JSON.parse(value.replace(/\\([0-7]{3})/g, (_, octal) => String.raw `\u00${Number.parseInt(octal, 8).toString(16).padStart(2, '0')}`))
|
|
11
9
|
: value;
|
|
12
|
-
|
|
13
10
|
// git's repository-local variables (git rev-parse --local-env-vars). A hook or alias run in a linked worktree exports
|
|
14
11
|
// GIT_DIR=<main>/.git/worktrees/<wt>; a spec inheriting it pointed every fixture git at the LIVE .claude repo and re-inited
|
|
15
12
|
// it core.bare=true (2026-09-29, a6f60352c). e2e/setup/isolated-registry.mjs drops the same list.
|
|
16
13
|
export const GIT_LOCAL_ENV_VARS = ['GIT_DIR', 'GIT_COMMON_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_OBJECT_DIRECTORY', 'GIT_ALTERNATE_OBJECT_DIRECTORIES', 'GIT_IMPLICIT_WORK_TREE', 'GIT_PREFIX', 'GIT_CONFIG', 'GIT_CONFIG_PARAMETERS', 'GIT_CONFIG_COUNT', 'GIT_GRAFT_FILE', 'GIT_NO_REPLACE_OBJECTS', 'GIT_REPLACE_REF_BASE', 'GIT_SHALLOW_FILE', 'GIT_INTERNAL_SUPER_PREFIX'];
|
|
17
14
|
/** `parent` (the caller's env) without git's repository-local variables (GIT_CONFIG_KEY_n/VALUE_n go with GIT_CONFIG_COUNT). Pure. */
|
|
18
15
|
export function withoutGitLocalEnv(parent) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
const env = { ...parent };
|
|
17
|
+
for (const key of Object.keys(env))
|
|
18
|
+
if (GIT_LOCAL_ENV_VARS.includes(key) || /^GIT_CONFIG_(KEY|VALUE)_\d+$/.test(key))
|
|
19
|
+
delete env[key];
|
|
20
|
+
return env;
|
|
22
21
|
}
|
|
23
|
-
|
|
24
22
|
/** A git spawn result (a scripts/api/git call file's) as {ok, stdout, error}: ok only on a clean exit, error from stderr or the spawn. Pure. */
|
|
25
23
|
export const gitResultOf = (r) => ({
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
ok: !r.error && r.status === 0,
|
|
25
|
+
stdout: r.stdout ?? '',
|
|
26
|
+
error: (r.stderr ?? '').trim() || String(r.error?.message ?? (r.status == null ? 'timed out' : `exit ${r.status}`)),
|
|
29
27
|
});
|
|
30
|
-
|
|
31
28
|
/** A git spawn result's stdout text; throws the spawn error, or an Error naming `what` with git's stderr and exit status, unless git exited 0. Pure. */
|
|
32
29
|
export function gitOutputOf(r, what = 'git') {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
if (r.error)
|
|
31
|
+
throw r.error;
|
|
32
|
+
if (r.status !== 0)
|
|
33
|
+
throw Object.assign(new Error(`${what} exited ${r.status}: ${(r.stderr ?? '').trim()}`), { status: r.status, stderr: r.stderr });
|
|
34
|
+
return r.stdout;
|
|
36
35
|
}
|
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
// glob.mjs — the path-glob subset ESLint, SonarQube and the scoped-lint config share: `**` spans directories,
|
|
2
2
|
// `*` and `?` stay inside one segment, `{a,b}` alternates.
|
|
3
3
|
import { posixPath } from './path-key.mjs';
|
|
4
|
-
|
|
5
4
|
/** Every alternative a `{a,b}` pattern spells, braces expanded left to right. */
|
|
6
5
|
export function braceVariants(value) {
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const match = /\{([^{}]+)\}/.exec(value);
|
|
7
|
+
return match ? match[1].split(',').flatMap((part) => braceVariants(`${value.slice(0, match.index)}${part}${value.slice(match.index + match[0].length)}`)) : [value];
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
/** The anchored RegExp of one brace-free glob, read as a posix path (backslashes and a leading './' folded). */
|
|
12
10
|
export function globExpression(value) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
const input = posixPath(value);
|
|
12
|
+
let source = '';
|
|
13
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
14
|
+
const c = input.charAt(i);
|
|
15
|
+
if (c === '*' && input[i + 1] === '*') {
|
|
16
|
+
i += 1;
|
|
17
|
+
if (input[i + 1] === '/') {
|
|
18
|
+
i += 1;
|
|
19
|
+
source += '(?:.*/)?';
|
|
20
|
+
}
|
|
21
|
+
else
|
|
22
|
+
source += '.*';
|
|
23
|
+
}
|
|
24
|
+
else if (c === '*')
|
|
25
|
+
source += '[^/]*';
|
|
26
|
+
else if (c === '?')
|
|
27
|
+
source += '[^/]';
|
|
28
|
+
else
|
|
29
|
+
source += /[.+^${}()|[\]\\]/.test(c) ? `\\${c}` : c;
|
|
30
|
+
}
|
|
31
|
+
return new RegExp(`^${source}$`);
|
|
23
32
|
}
|
|
@@ -1,61 +1,57 @@
|
|
|
1
1
|
// Grammar-family lint selection reads the family catalog; an absent selection never chooses a family.
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import { parseYamlCached } from './yaml-cached.mjs';
|
|
4
|
-
|
|
5
4
|
const CATALOG_FILE = new URL('../../knowledge/grammars/index.yaml', import.meta.url);
|
|
6
|
-
|
|
7
5
|
function readCatalog() {
|
|
8
|
-
|
|
6
|
+
return parseYamlCached(fs.readFileSync(CATALOG_FILE, 'utf8'));
|
|
9
7
|
}
|
|
10
|
-
|
|
11
8
|
function selectedFamily(family) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
if (typeof family !== 'string' || !family.trim())
|
|
10
|
+
throw new Error('sides.fe.grammar must explicitly name the selected Grammar family');
|
|
11
|
+
const catalog = readCatalog();
|
|
12
|
+
const matches = (catalog.families ?? []).filter(entry => entry.id === family);
|
|
13
|
+
if (matches.length !== 1)
|
|
14
|
+
throw new Error(`Grammar family ${family} has no declared lint contract in knowledge/grammars/index.yaml`);
|
|
15
|
+
return { catalog, selected: matches[0] };
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
/**
|
|
20
18
|
* Resolve a selection to its declared family id; only a catalog family id resolves, there are no aliases.
|
|
21
19
|
* @param {string} family - The id an app or caller names (sides.fe.grammar).
|
|
22
20
|
* @returns {{ id: string }} The declared family id.
|
|
23
21
|
*/
|
|
24
22
|
export function resolveGrammarFamily(family) {
|
|
25
|
-
|
|
23
|
+
return Object.freeze({ id: selectedFamily(family).selected.id });
|
|
26
24
|
}
|
|
27
|
-
|
|
28
25
|
/** The explicitly selected authored family entry; callers share the catalog lookup rather than inferring a family. */
|
|
29
26
|
export function grammarFamilyFor(family) {
|
|
30
|
-
|
|
27
|
+
return Object.freeze(selectedFamily(family).selected);
|
|
31
28
|
}
|
|
32
|
-
|
|
33
29
|
/** The declared family's lint contract, including an explicit null package when that family has no rules. */
|
|
34
30
|
export function grammarLintFor(family) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
const { selected } = selectedFamily(family);
|
|
32
|
+
if (!selected.lint || !Object.hasOwn(selected.lint, 'package')) {
|
|
33
|
+
throw new Error(`Grammar family ${family} has no declared lint contract in knowledge/grammars/index.yaml`);
|
|
34
|
+
}
|
|
35
|
+
const lint = selected.lint;
|
|
36
|
+
if (lint.package === null)
|
|
37
|
+
return Object.freeze({ family: selected.id, package: null });
|
|
38
|
+
if (typeof lint.package !== 'string' || !lint.package.startsWith('@starci/eslint-grammar-')
|
|
39
|
+
|| typeof lint.factory !== 'string' || !/^[a-zA-Z][a-zA-Z0-9]*$/.test(lint.factory)
|
|
40
|
+
|| typeof lint.prefix !== 'string' || !/^[a-z][a-z0-9-]*$/.test(lint.prefix)) {
|
|
41
|
+
throw new Error(`Grammar family ${family} has an invalid lint contract in knowledge/grammars/index.yaml`);
|
|
42
|
+
}
|
|
43
|
+
return Object.freeze({ family: selected.id, package: lint.package, factory: lint.factory, prefix: lint.prefix });
|
|
47
44
|
}
|
|
48
|
-
|
|
49
45
|
/** The selected family's shared component/export contract, resolved through its catalog reference. */
|
|
50
46
|
export function grammarContractFor(family) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
const { catalog, selected } = selectedFamily(family);
|
|
48
|
+
const owners = (catalog.families ?? []).filter(entry => entry.id === selected.contractRef);
|
|
49
|
+
const contract = owners.length === 1 ? owners[0]?.componentContract : null;
|
|
50
|
+
if (!contract || typeof contract.package !== 'string' || !contract.package
|
|
51
|
+
|| typeof contract.entry !== 'string' || !contract.entry.startsWith(`${contract.package}/`)
|
|
52
|
+
|| typeof contract.styleEntry !== 'string' || !contract.styleEntry.startsWith(`${contract.package}/`)
|
|
53
|
+
|| !Array.isArray(contract.peers) || !contract.peers.every(peer => typeof peer === 'string' && peer.trim())) {
|
|
54
|
+
throw new Error(`Grammar family ${family} has no valid component contract in knowledge/grammars/index.yaml`);
|
|
55
|
+
}
|
|
56
|
+
return Object.freeze({ ...contract, peers: Object.freeze([...contract.peers]) });
|
|
61
57
|
}
|
|
@@ -18,68 +18,65 @@ import path from 'node:path';
|
|
|
18
18
|
import { skillRoot } from '../../engine/runtime-root.mjs';
|
|
19
19
|
import { parseYaml } from '../../engine/yaml.mjs';
|
|
20
20
|
import { DEFAULT_OWNER_LANGUAGE, loadConfig } from '../../engine/config.mjs';
|
|
21
|
-
|
|
22
21
|
export const CATALOG_DIR = 'modules/i18n/messages';
|
|
23
22
|
export const PLACEHOLDER = /\{([A-Za-z_]\w*)\}/g;
|
|
24
|
-
|
|
25
23
|
const cache = new Map();
|
|
26
|
-
|
|
27
24
|
/** The catalog files of a runtime root: repository-relative paths, sorted. */
|
|
28
25
|
export function catalogFiles(root = skillRoot) {
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
const dir = path.join(root, ...CATALOG_DIR.split('/'));
|
|
27
|
+
try {
|
|
28
|
+
return fs.readdirSync(dir).filter((name) => /\.ya?ml$/.test(name)).sort().map((name) => `${CATALOG_DIR}/${name}`);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
31
33
|
}
|
|
32
|
-
|
|
33
34
|
/** Every entry in one catalog scope: `[{ en, vi, area, scope, file }]`. */
|
|
34
35
|
export function catalogEntries(root = skillRoot, { scope = 'runtime' } = {}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
return catalogFiles(root).flatMap((rel) => {
|
|
37
|
+
const doc = parseYaml(fs.readFileSync(path.join(root, ...rel.split('/')), 'utf8'));
|
|
38
|
+
const documentScope = typeof doc?.scope === 'string' ? doc.scope : 'runtime';
|
|
39
|
+
if (documentScope !== scope)
|
|
40
|
+
return [];
|
|
41
|
+
return (Array.isArray(doc?.messages) ? doc.messages : []).map((entry) => ({
|
|
42
|
+
en: entry?.en, vi: entry?.vi, area: doc?.area, scope: documentScope, file: rel,
|
|
43
|
+
}));
|
|
44
|
+
});
|
|
43
45
|
}
|
|
44
|
-
|
|
45
46
|
/** One scoped catalog as a Map from the English source to the Vietnamese text (cached per root and scope). */
|
|
46
47
|
export function loadCatalog(root = skillRoot, { scope = 'runtime' } = {}) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
const key = `${root}\0${scope}`;
|
|
49
|
+
if (!cache.has(key))
|
|
50
|
+
cache.set(key, new Map(catalogEntries(root, { scope }).filter((e) => typeof e.en === 'string' && typeof e.vi === 'string').map((e) => [e.en, e.vi])));
|
|
51
|
+
return cache.get(key);
|
|
50
52
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
53
|
/** The names each `pattern` match captures (group 1) in `text`, in match order (`sort` sorts them). */
|
|
54
54
|
export const captureNames = (text, pattern, { sort = false } = {}) => {
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const names = [...String(text).matchAll(pattern)].map((m) => m[1]);
|
|
56
|
+
return sort ? names.sort(byCodeUnit) : names;
|
|
57
57
|
};
|
|
58
|
-
|
|
59
58
|
/** The names of the placeholders of a message. */
|
|
60
59
|
export const placeholdersOf = (text) => captureNames(text, PLACEHOLDER, { sort: true });
|
|
61
|
-
|
|
62
60
|
/** `text` with each `{name}` replaced by vars[name] (an unknown name is left as written). */
|
|
63
61
|
export const fill = (text, vars = {}) => String(text).replace(PLACEHOLDER, (whole, name) => (Object.hasOwn(vars, name) ? String(vars[name]) : whole));
|
|
64
|
-
|
|
65
62
|
/** The message `en` in `language` (`vi` translates through the catalog; any other language is the English source). */
|
|
66
63
|
export function translate(en, vars = {}, { language = DEFAULT_OWNER_LANGUAGE, root = skillRoot, scope = 'runtime' } = {}) {
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
const vi = language === 'vi' ? loadCatalog(root, { scope }).get(en) : undefined;
|
|
65
|
+
return fill(vi ?? en, vars);
|
|
69
66
|
}
|
|
70
|
-
|
|
71
67
|
/** The owner's language (config.yaml `language`); `fallback` when there is no readable config (DEFAULT_OWNER_LANGUAGE of engine/config.mjs). */
|
|
72
68
|
export function ownerLanguage(fallback = DEFAULT_OWNER_LANGUAGE) {
|
|
73
|
-
|
|
69
|
+
try {
|
|
70
|
+
return loadConfig()?.language ?? fallback;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return fallback;
|
|
74
|
+
}
|
|
74
75
|
}
|
|
75
|
-
|
|
76
76
|
/** A translator bound to one language: `tr(en, vars)`. */
|
|
77
|
-
export const translator = (language = DEFAULT_OWNER_LANGUAGE, { root = skillRoot, scope = 'runtime' } = {}) =>
|
|
78
|
-
(en, vars = {}) => translate(en, vars, { language, root, scope });
|
|
79
|
-
|
|
77
|
+
export const translator = (language = DEFAULT_OWNER_LANGUAGE, { root = skillRoot, scope = 'runtime' } = {}) => (en, vars = {}) => translate(en, vars, { language, root, scope });
|
|
80
78
|
/**
|
|
81
79
|
* A pattern source matching `en` in the owner's language or in English: each language's rendering of the English source,
|
|
82
80
|
* escaped, with the placeholder `slot` (a placeholder name of `en`) replaced by `slotPattern`. Never hardcodes a spelling.
|
|
83
81
|
*/
|
|
84
|
-
export const translatedPattern = (en, slot, slotPattern, languages = ['en', 'vi']) =>
|
|
85
|
-
`(?:${languages.map((language) => escapeRegExp(translate(en, { [slot]: '@@' }, { language })).replace('@@', slotPattern)).join('|')})`;
|
|
82
|
+
export const translatedPattern = (en, slot, slotPattern, languages = ['en', 'vi']) => `(?:${languages.map((language) => escapeRegExp(translate(en, { [slot]: '@@' }, { language })).replace('@@', slotPattern)).join('|')})`;
|
|
@@ -6,66 +6,85 @@
|
|
|
6
6
|
// `for (const x of xs) await fn(x)` does. All of them run on the one `walk` below: promise callbacks, not an async
|
|
7
7
|
// recursion, so the stack and the memory stay flat however long the input is.
|
|
8
8
|
// Provably independent work that needs no order, short-circuit or error contract may use `Promise.all` instead.
|
|
9
|
-
|
|
10
9
|
/** Close `iterator` the way a `for...of` leaves it: `return()` when it has one. */
|
|
11
10
|
const close = (iterator) => iterator.return?.();
|
|
12
|
-
|
|
13
11
|
/**
|
|
14
12
|
* Drive `visit(item, index)` over `items` one at a time. `decide(outcome, item)` runs after each settled visit: a
|
|
15
13
|
* `{ value }` box ends the walk with that value, anything else goes on. Resolves with the box value, or `undefined` when
|
|
16
14
|
* the items ran out; rejects with the first rejection or throw, after closing the iterator.
|
|
17
15
|
*/
|
|
18
16
|
const walk = (items, visit, decide) => new Promise((resolve, reject) => {
|
|
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
|
-
|
|
17
|
+
const iterator = items[Symbol.iterator]();
|
|
18
|
+
let index = 0;
|
|
19
|
+
const fail = (error) => {
|
|
20
|
+
try {
|
|
21
|
+
close(iterator);
|
|
22
|
+
}
|
|
23
|
+
catch { /* the failure being reported wins, as it does in a for...of */ }
|
|
24
|
+
reject(error);
|
|
25
|
+
};
|
|
26
|
+
const stop = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
close(iterator);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
reject(error);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
resolve(value);
|
|
35
|
+
};
|
|
36
|
+
const advance = () => {
|
|
37
|
+
let step, pending;
|
|
38
|
+
try {
|
|
39
|
+
step = iterator.next();
|
|
40
|
+
if (step.done) {
|
|
41
|
+
resolve(undefined);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
pending = visit(step.value, index++);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (step === undefined || step.done)
|
|
48
|
+
reject(error);
|
|
49
|
+
else
|
|
50
|
+
fail(error);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const item = step.value;
|
|
54
|
+
Promise.resolve(pending).then((outcome) => {
|
|
55
|
+
let box;
|
|
56
|
+
try {
|
|
57
|
+
box = decide(outcome, item);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
fail(error);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (box)
|
|
64
|
+
stop(box.value);
|
|
65
|
+
else
|
|
66
|
+
advance();
|
|
67
|
+
}, fail);
|
|
68
|
+
};
|
|
69
|
+
advance();
|
|
46
70
|
});
|
|
47
|
-
|
|
48
71
|
const never = () => undefined;
|
|
49
|
-
|
|
50
72
|
/** `for (const x of items) await fn(x, index)`: resolves undefined once every item has run, one at a time. */
|
|
51
73
|
export const eachInOrder = (items, fn) => walk(items, fn, never);
|
|
52
|
-
|
|
53
74
|
/** The sequential map: `fn(x, index)` for each item in turn, resolving the results in item order. */
|
|
54
75
|
export const mapInOrder = (items, fn) => {
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
const results = [];
|
|
77
|
+
return walk(items, fn, (outcome) => { results.push(outcome); return undefined; }).then(() => results);
|
|
57
78
|
};
|
|
58
|
-
|
|
59
79
|
/** The first item whose `predicate(x, index)` resolves truthy (undefined when none); later items never run. */
|
|
60
80
|
export const findInOrder = (items, predicate) => walk(items, predicate, (outcome, item) => (outcome ? { value: item } : undefined));
|
|
61
|
-
|
|
62
81
|
/**
|
|
63
82
|
* `for (;;) await step(attempt)`: `step(attempt)` runs again, after the previous run settled, until it resolves
|
|
64
83
|
* something other than undefined; that value (null included) is the result. A poll or retry returns undefined to go
|
|
65
84
|
* on and its answer to stop. `attempt` counts from 0.
|
|
66
85
|
*/
|
|
67
86
|
export const repeatInOrder = (step) => walk(forever(), step, (outcome) => (outcome === undefined ? undefined : { value: outcome }));
|
|
68
|
-
|
|
69
87
|
function* forever() {
|
|
70
|
-
|
|
88
|
+
for (let attempt = 0;; attempt++)
|
|
89
|
+
yield attempt;
|
|
71
90
|
}
|
|
@@ -2,52 +2,60 @@
|
|
|
2
2
|
// copy of a value, detached from every reference, frozen state and class of the original. `structuredClone` keeps
|
|
3
3
|
// what JSON drops (undefined members, Dates, Maps, sparse holes) and refuses what JSON accepts (functions,
|
|
4
4
|
// `toJSON` objects), so it is not a substitute; this walks the value once under the JSON.stringify rules.
|
|
5
|
-
|
|
6
5
|
/** The boxed primitives JSON.stringify unwraps: recognised by the internal slot a valueOf call reads, as JSON does. */
|
|
7
6
|
const BOXED = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
{ slot: Number.prototype.valueOf, read: Number },
|
|
8
|
+
{ slot: String.prototype.valueOf, read: String },
|
|
9
|
+
{ slot: Boolean.prototype.valueOf, read: (box) => Boolean.prototype.valueOf.call(box) },
|
|
10
|
+
{ slot: BigInt.prototype.valueOf, read: (box) => BigInt.prototype.valueOf.call(box) },
|
|
12
11
|
];
|
|
13
|
-
|
|
14
12
|
/** Whether `value` carries the internal slot `slot` reads (a valueOf of that boxed type does not throw on it). */
|
|
15
13
|
const hasSlot = (slot, value) => {
|
|
16
|
-
|
|
14
|
+
try {
|
|
15
|
+
slot.call(value);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
17
21
|
};
|
|
18
|
-
|
|
19
22
|
/** What JSON.stringify takes of `holder[key]`: the toJSON result when there is one, a boxed primitive unwrapped. */
|
|
20
23
|
const serialized = (holder, key) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
let value = holder[key];
|
|
25
|
+
if (value !== null && (typeof value === 'object' || typeof value === 'bigint') && typeof value.toJSON === 'function')
|
|
26
|
+
value = value.toJSON(key);
|
|
27
|
+
if (value === null || typeof value !== 'object')
|
|
28
|
+
return value;
|
|
29
|
+
const box = BOXED.find((candidate) => hasSlot(candidate.slot, value));
|
|
30
|
+
return box ? box.read(value) : value;
|
|
26
31
|
};
|
|
27
|
-
|
|
28
32
|
/** A member JSON leaves out of an object (and writes as null in an array). */
|
|
29
33
|
const isAbsent = (value) => value === undefined || typeof value === 'function' || typeof value === 'symbol';
|
|
30
|
-
|
|
31
34
|
const copyValue = (value, ancestors) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
36
|
+
return value;
|
|
37
|
+
if (typeof value === 'number')
|
|
38
|
+
return Number.isFinite(value) ? value + 0 : null;
|
|
39
|
+
if (typeof value === 'bigint')
|
|
40
|
+
throw new TypeError('Do not know how to serialize a BigInt');
|
|
41
|
+
if (ancestors.includes(value))
|
|
42
|
+
throw new TypeError('Converting circular structure to JSON');
|
|
43
|
+
const inside = [...ancestors, value];
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
return Array.from({ length: value.length }, (_, index) => {
|
|
46
|
+
const item = serialized(value, String(index));
|
|
47
|
+
return isAbsent(item) ? null : copyValue(item, inside);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const holder = value;
|
|
51
|
+
const entries = [];
|
|
52
|
+
for (const name of Object.keys(holder)) {
|
|
53
|
+
const member = serialized(holder, name);
|
|
54
|
+
if (!isAbsent(member))
|
|
55
|
+
entries.push([name, copyValue(member, inside)]);
|
|
56
|
+
}
|
|
57
|
+
return Object.fromEntries(entries);
|
|
49
58
|
};
|
|
50
|
-
|
|
51
59
|
/**
|
|
52
60
|
* `value` copied as JSON data: members that are undefined, functions or symbols are dropped (an array item becomes
|
|
53
61
|
* null), non-finite numbers become null and -0 becomes 0, `toJSON` results replace their objects (a Date becomes its ISO string), boxed
|
|
@@ -55,7 +63,8 @@ const copyValue = (value, ancestors) => {
|
|
|
55
63
|
* a symbol) throws a SyntaxError, as parsing the missing text does.
|
|
56
64
|
*/
|
|
57
65
|
export function jsonClone(value) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
66
|
+
const top = serialized({ '': value }, '');
|
|
67
|
+
if (isAbsent(top))
|
|
68
|
+
throw new SyntaxError('"undefined" is not valid JSON');
|
|
69
|
+
return copyValue(top, []);
|
|
61
70
|
}
|
|
@@ -2,31 +2,43 @@
|
|
|
2
2
|
// truncated payload, so a malformed column is the caller's fallback, never a throw. The parse of the
|
|
3
3
|
// literal 'null' is kept as null (callers that want {} instead write `parseJsonOr`).
|
|
4
4
|
import { readParsedFile } from './read-text.mjs';
|
|
5
|
-
|
|
6
5
|
/** `text` as JSON, or `fallback` when it does not parse. */
|
|
6
|
+
// reason: JSON parsed from a ledger column or file is an untyped input boundary; each caller narrows what it reads.
|
|
7
7
|
export const parseJson = (text, fallback = null) => {
|
|
8
|
-
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(text);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return fallback;
|
|
13
|
+
}
|
|
9
14
|
};
|
|
10
|
-
|
|
11
15
|
/** `text` as JSON, or `fallback` when it does not parse OR parses to null/undefined. Rows written
|
|
12
16
|
* as the literal 'null' read back as the fallback too, so a caller that wants an object gets one. */
|
|
17
|
+
// reason: same parsed-JSON boundary as parseJson.
|
|
13
18
|
export const parseJsonOr = (text, fallback = {}) => parseJson(text) ?? fallback;
|
|
14
|
-
|
|
15
19
|
/** A jobs/events row spread with `payload`: the row's payload_json parsed via parseJsonOr
|
|
16
20
|
* (`fallback` defaults to {}). Pass fallback null to keep a raw parseJson result, nulls included. */
|
|
21
|
+
// reason: same parsed-JSON boundary as parseJson.
|
|
17
22
|
export const withPayload = (row, fallback = {}) => (row ? { ...row, payload: parseJsonOr(row.payload_json, fallback) } : row);
|
|
18
|
-
|
|
19
23
|
/** `file` read and parsed as JSON, or `fallback` when the file is missing, unreadable or malformed. */
|
|
24
|
+
// reason: same parsed-JSON boundary as parseJson.
|
|
20
25
|
export const readJsonFile = (file, fallback = null) => readParsedFile(file, JSON.parse, fallback);
|
|
21
|
-
|
|
22
26
|
/** `stdout` as JSON: the trimmed text, else its first-{-to-last-} span; null when neither parses. */
|
|
27
|
+
// reason: same parsed-JSON boundary as parseJson.
|
|
23
28
|
export const jsonFromStdout = (stdout) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
const text = String(stdout ?? '').trim();
|
|
30
|
+
if (!text)
|
|
31
|
+
return null;
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(text);
|
|
34
|
+
}
|
|
35
|
+
catch { /* fall through */ }
|
|
36
|
+
const first = text.indexOf('{'), last = text.lastIndexOf('}');
|
|
37
|
+
if (first >= 0 && last > first) {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(text.slice(first, last + 1));
|
|
40
|
+
}
|
|
41
|
+
catch { /* not json */ }
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
32
44
|
};
|