@sublang/spex 5.0.0 → 7.0.0-beta.2
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 +453 -41
- package/dist/cli.js +12 -1
- package/dist/commands/add.d.ts +1 -1
- package/dist/commands/add.js +3 -3
- package/dist/commands/auth-target.d.ts +1 -0
- package/dist/commands/auth-target.js +4 -2
- package/dist/commands/environment.d.ts +15 -21
- package/dist/commands/environment.js +43 -57
- package/dist/commands/export.js +2 -2
- package/dist/commands/home.d.ts +2 -2
- package/dist/commands/home.js +3 -3
- package/dist/commands/list.js +1 -1
- package/dist/commands/login.js +12 -7
- package/dist/commands/logout.js +2 -3
- package/dist/commands/publish.js +16 -16
- package/dist/commands/remove.js +1 -1
- package/dist/commands/run.d.ts +1 -1
- package/dist/commands/run.js +22 -3
- package/dist/commands/search.js +1 -1
- package/dist/commands/settle.d.ts +1 -1
- package/dist/commands/settle.js +3 -3
- package/dist/commands/whoami.js +2 -6
- package/dist/guards/approval.d.ts +56 -0
- package/dist/guards/approval.js +158 -0
- package/dist/guards/checks.d.ts +207 -0
- package/dist/guards/checks.js +681 -0
- package/dist/guards/commands.d.ts +15 -0
- package/dist/guards/commands.js +313 -0
- package/dist/guards/copy.d.ts +67 -0
- package/dist/guards/copy.js +306 -0
- package/dist/guards/declaration.d.ts +45 -0
- package/dist/guards/declaration.js +224 -0
- package/dist/guards/files.d.ts +137 -0
- package/dist/guards/files.js +544 -0
- package/dist/guards/harness.d.ts +58 -0
- package/dist/guards/harness.js +420 -0
- package/dist/guards/items.d.ts +61 -0
- package/dist/guards/items.js +161 -0
- package/dist/guards/patch.d.ts +55 -0
- package/dist/guards/patch.js +258 -0
- package/dist/guards/processes.d.ts +49 -0
- package/dist/guards/processes.js +233 -0
- package/dist/guards/proofs.d.ts +88 -0
- package/dist/guards/proofs.js +503 -0
- package/dist/guards/records.d.ts +176 -0
- package/dist/guards/records.js +513 -0
- package/dist/guards/report.d.ts +26 -0
- package/dist/guards/report.js +492 -0
- package/dist/guards/runner.d.ts +42 -0
- package/dist/guards/runner.js +167 -0
- package/dist/guards/tla.d.ts +51 -0
- package/dist/guards/tla.js +352 -0
- package/dist/guards/tlapm.d.ts +81 -0
- package/dist/guards/tlapm.js +172 -0
- package/dist/guards/tlc.d.ts +191 -0
- package/dist/guards/tlc.js +457 -0
- package/dist/guards/translation.d.ts +66 -0
- package/dist/guards/translation.js +202 -0
- package/dist/guards/types.d.ts +185 -0
- package/dist/guards/types.js +437 -0
- package/dist/guards/verify.d.ts +290 -0
- package/dist/guards/verify.js +1191 -0
- package/dist/guards/xml.d.ts +18 -0
- package/dist/guards/xml.js +105 -0
- package/dist/lib/environment/archive.d.ts +16 -3
- package/dist/lib/environment/archive.js +39 -8
- package/dist/lib/environment/builtins.d.ts +10 -12
- package/dist/lib/environment/builtins.js +18 -20
- package/dist/lib/environment/exports.d.ts +14 -13
- package/dist/lib/environment/exports.js +31 -16
- package/dist/lib/environment/format.d.ts +2 -2
- package/dist/lib/environment/format.js +3 -3
- package/dist/lib/environment/git-source.js +6 -3
- package/dist/lib/environment/install.d.ts +14 -16
- package/dist/lib/environment/install.js +66 -61
- package/dist/lib/environment/lock.d.ts +4 -3
- package/dist/lib/environment/lock.js +6 -5
- package/dist/lib/environment/marker.js +1 -2
- package/dist/lib/environment/own-paths.d.ts +7 -10
- package/dist/lib/environment/own-paths.js +10 -13
- package/dist/lib/environment/placed-record.d.ts +3 -2
- package/dist/lib/environment/placed-record.js +5 -4
- package/dist/lib/environment/registries.d.ts +7 -9
- package/dist/lib/environment/registries.js +9 -36
- package/dist/lib/environment/registry.d.ts +6 -7
- package/dist/lib/environment/registry.js +12 -12
- package/dist/lib/environment/requests.d.ts +6 -5
- package/dist/lib/environment/requests.js +6 -8
- package/dist/lib/environment/resolve.d.ts +4 -4
- package/dist/lib/environment/resolve.js +3 -4
- package/dist/lib/environment/store.d.ts +34 -8
- package/dist/lib/environment/store.js +138 -43
- package/dist/lib/files.d.ts +72 -12
- package/dist/lib/files.js +200 -65
- package/dist/lib/git-host.d.ts +52 -258
- package/dist/lib/git-host.js +52 -265
- package/dist/lib/home-file.d.ts +13 -10
- package/dist/lib/home-file.js +28 -18
- package/dist/lib/instance.d.ts +1 -1
- package/dist/lib/instance.js +1 -1
- package/dist/lint.d.ts +22 -0
- package/dist/lint.js +36 -0
- package/dist/testing/standin-registry.js +1 -1
- package/package.json +7 -3
- package/scaffold/.file-history.json +14 -7
- package/scaffold/i18n/zh/source-pins.json +41 -0
- package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
- package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
- package/scaffold/i18n/zh/specs/map.md +0 -1
- package/scaffold/i18n/zh/specs/meta.md +18 -35
- package/scaffold/i18n/zh/specs/packages/git.md +0 -1
- package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
- package/scaffold/specs/meta.md +18 -3
- package/scripts/test-exit-guard.mjs +28 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { type Inputs } from "./approval.js";
|
|
2
|
+
import { type GuardItems, type ItemCase, type ItemIndex } from "./items.js";
|
|
3
|
+
import { type Patch } from "./patch.js";
|
|
4
|
+
import { type Translation } from "./translation.js";
|
|
5
|
+
import { GuardError, type Case, type Declaration, type Step } from "./types.js";
|
|
6
|
+
/** What binding.yaml declares. */
|
|
7
|
+
export type BindingDeclaration = Declaration & {
|
|
8
|
+
/** Project files and folders a run copies, relative to the root.
|
|
9
|
+
* Empty where the declaration names none: no copy can then be made
|
|
10
|
+
* (guards-36). */
|
|
11
|
+
files: string[];
|
|
12
|
+
/** Commands run in the copy, in order, each an argv list. */
|
|
13
|
+
commands: string[][];
|
|
14
|
+
/** Each probe, a file relative to the root written with `/`, in the
|
|
15
|
+
* declaration's order; empty where it names none. */
|
|
16
|
+
probes: string[];
|
|
17
|
+
/** The command that runs the project's tests with the probes on, as
|
|
18
|
+
* an argv list; null where it names none. */
|
|
19
|
+
tests: string[] | null;
|
|
20
|
+
};
|
|
21
|
+
/** What a binding has to cause or record the events: a module, probes
|
|
22
|
+
* with the test command, both, or neither. */
|
|
23
|
+
export type BindingKind = "module" | "probes" | "both" | "neither";
|
|
24
|
+
/** A binding as loaded from its folder. */
|
|
25
|
+
export type Binding = BindingDeclaration & {
|
|
26
|
+
name: string;
|
|
27
|
+
/** `guards/bindings/<name>`. */
|
|
28
|
+
folder: string;
|
|
29
|
+
/** Every file under the folder, relative to the root, in order. */
|
|
30
|
+
folderFiles: string[];
|
|
31
|
+
/** The folder holds a `binding.mjs`. Without one the guard still
|
|
32
|
+
* loads, and the checks on the code find a failing binding
|
|
33
|
+
* (guards-36). */
|
|
34
|
+
module: boolean;
|
|
35
|
+
/** By its module and its test command. */
|
|
36
|
+
kind: BindingKind;
|
|
37
|
+
};
|
|
38
|
+
/** The limits: the seconds each run of a tool and each command may
|
|
39
|
+
* take. */
|
|
40
|
+
export type Limits = {
|
|
41
|
+
seconds: number;
|
|
42
|
+
};
|
|
43
|
+
/** What guard.yaml holds: `entries` are its `cases` as written, in
|
|
44
|
+
* the file's order. */
|
|
45
|
+
export type GuardFile = {
|
|
46
|
+
binding: string;
|
|
47
|
+
limits: Limits;
|
|
48
|
+
entries: {
|
|
49
|
+
label: string;
|
|
50
|
+
steps: Step[];
|
|
51
|
+
}[];
|
|
52
|
+
};
|
|
53
|
+
/** A kept fault. */
|
|
54
|
+
export type Fault = {
|
|
55
|
+
name: string;
|
|
56
|
+
file: string;
|
|
57
|
+
patch: Patch;
|
|
58
|
+
};
|
|
59
|
+
/** A translation's proofs, `guards/<guard>/proofs/<name>_proofs.tla`.
|
|
60
|
+
* No approval input: every run checks them again. */
|
|
61
|
+
export type Proof = {
|
|
62
|
+
/** The translation's name. */
|
|
63
|
+
name: string;
|
|
64
|
+
/** Its file, relative to the root, with `/`. */
|
|
65
|
+
file: string;
|
|
66
|
+
text: string;
|
|
67
|
+
};
|
|
68
|
+
/** A guard that loads but for its translations: it has none yet. Its
|
|
69
|
+
* declaration is there for `spex verify --keep` to leave in TLA+. */
|
|
70
|
+
export declare class NoTranslation extends GuardError {
|
|
71
|
+
readonly binding: Binding;
|
|
72
|
+
constructor(message: string, binding: Binding);
|
|
73
|
+
}
|
|
74
|
+
/** Everything one guard is made of, loaded and checked. */
|
|
75
|
+
export type Guard = {
|
|
76
|
+
/** The guard item's ID, which is also the folder's name. */
|
|
77
|
+
name: string;
|
|
78
|
+
root: string;
|
|
79
|
+
/** `guards/<name>`. */
|
|
80
|
+
folder: string;
|
|
81
|
+
items: GuardItems;
|
|
82
|
+
binding: Binding;
|
|
83
|
+
limits: Limits;
|
|
84
|
+
/** The guard item's cases with their steps, in the item's order. */
|
|
85
|
+
cases: Case[];
|
|
86
|
+
/** In name order; at least one, and two or more for the guard to pass. */
|
|
87
|
+
translations: Translation[];
|
|
88
|
+
/** In name order; may be empty. */
|
|
89
|
+
faults: Fault[];
|
|
90
|
+
/** In name order, each of a translation; a translation may have
|
|
91
|
+
* none. */
|
|
92
|
+
proofs: Proof[];
|
|
93
|
+
/** The digest of every approval input, labels in order. */
|
|
94
|
+
inputs: Inputs;
|
|
95
|
+
};
|
|
96
|
+
/** Read binding.yaml's text. Errors carry no file name; the caller
|
|
97
|
+
* adds it. */
|
|
98
|
+
export declare function parseBinding(text: string): BindingDeclaration;
|
|
99
|
+
/**
|
|
100
|
+
* Load the binding `guards/bindings/<name>` and digest every file in
|
|
101
|
+
* its folder. A folder with no `binding.mjs` still loads. Every error
|
|
102
|
+
* is a GuardError starting with the file.
|
|
103
|
+
*/
|
|
104
|
+
export declare function loadBinding(root: string, name: string): {
|
|
105
|
+
binding: Binding;
|
|
106
|
+
inputs: Inputs;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Read guard.yaml's text (guards-10). `declarationOf` gives the
|
|
110
|
+
* declaration of the binding the file names, against which every step
|
|
111
|
+
* is decoded: every param stated, the stated effect fields of the
|
|
112
|
+
* right type. A label entered twice is an error here; whether every
|
|
113
|
+
* entry is a case of the guard item is `caseSteps`'s to say. Errors
|
|
114
|
+
* carry no file name; the caller adds it.
|
|
115
|
+
*/
|
|
116
|
+
export declare function parseGuardFile(text: string, declarationOf: (binding: string) => Declaration): GuardFile;
|
|
117
|
+
/**
|
|
118
|
+
* Each case of the guard item with its steps from the guard file, in
|
|
119
|
+
* the item's order (guards-10). A case with no entry, and an entry
|
|
120
|
+
* for no case, is a GuardError naming the label; the caller adds the
|
|
121
|
+
* file.
|
|
122
|
+
*/
|
|
123
|
+
export declare function caseSteps(cases: readonly ItemCase[], entries: GuardFile["entries"]): Case[];
|
|
124
|
+
/** The guards under `<root>/guards`: every folder there other than
|
|
125
|
+
* `bindings`, with or without a guard.yaml, so that a guard whose
|
|
126
|
+
* file is gone or misnamed is reported and not passed over. In name
|
|
127
|
+
* order. */
|
|
128
|
+
export declare function listGuards(root: string): string[];
|
|
129
|
+
/**
|
|
130
|
+
* Load one guard: its items, guard file, binding declaration,
|
|
131
|
+
* translations and kept faults, each checked but a translation's
|
|
132
|
+
* text, which is TLC's to read, with the digest of
|
|
133
|
+
* every approval input. `index` is the tree's items where the caller has
|
|
134
|
+
* read them already. Every error is a GuardError naming the file or
|
|
135
|
+
* item and the cause; the approval record is not read here.
|
|
136
|
+
*/
|
|
137
|
+
export declare function loadGuard(root: string, name: string, index?: ItemIndex): Guard;
|
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package (guards-1, guards-3, guards-10, guards-18), DR-252:
|
|
5
|
+
* a guard's files. A binding's declaration
|
|
6
|
+
* (`guards/bindings/<name>/binding.yaml`) names the values, the
|
|
7
|
+
* events with their params and effects, the files a run copies and
|
|
8
|
+
* the commands that build the copy. The guard file
|
|
9
|
+
* (`guards/<guard>/guard.yaml`) names the binding, fixes the time
|
|
10
|
+
* limit and holds each case of the guard item again as steps. Each
|
|
11
|
+
* translation's proofs are `guards/<guard>/proofs/<name>_proofs.tla`.
|
|
12
|
+
* `loadGuard` reads everything one guard is made of into one checked object, or
|
|
13
|
+
* throws a GuardError naming the file or item and the cause.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
16
|
+
import { join, sep } from "node:path";
|
|
17
|
+
import { digest, fileLabel, itemLabel, sortInputs } from "./approval.js";
|
|
18
|
+
import { inside, leftOut } from "./copy.js";
|
|
19
|
+
import { guardItems, readItems } from "./items.js";
|
|
20
|
+
import { checkNames } from "./declaration.js";
|
|
21
|
+
import { parsePatch } from "./patch.js";
|
|
22
|
+
import { translationNameFault } from "./translation.js";
|
|
23
|
+
import { GuardError, byText, decodeFields, eventFault, fieldFault, mapping, mappingOrNone, parseType, parseYaml, showData, vocabulary, within, } from "./types.js";
|
|
24
|
+
/** A guard that loads but for its translations: it has none yet. Its
|
|
25
|
+
* declaration is there for `spex verify --keep` to leave in TLA+. */
|
|
26
|
+
export class NoTranslation extends GuardError {
|
|
27
|
+
binding;
|
|
28
|
+
constructor(message, binding) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.binding = binding;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const FOLDER_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
34
|
+
// ---------------------------------------------------------------
|
|
35
|
+
// binding.yaml
|
|
36
|
+
// ---------------------------------------------------------------
|
|
37
|
+
function typedFields(raw, vocab) {
|
|
38
|
+
const fields = {};
|
|
39
|
+
for (const [name, type] of Object.entries(mappingOrNone(raw))) {
|
|
40
|
+
within(name, () => {
|
|
41
|
+
const fault = fieldFault(name);
|
|
42
|
+
if (fault !== null)
|
|
43
|
+
throw new GuardError(fault);
|
|
44
|
+
fields[name] = parseType(type, vocab);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return fields;
|
|
48
|
+
}
|
|
49
|
+
/** The error for an entry of `files` that is `guards` or under it. */
|
|
50
|
+
function underGuards(entry) {
|
|
51
|
+
return `files: '${entry}' is under guards/; a binding's folder is copied by itself and is no code under test`;
|
|
52
|
+
}
|
|
53
|
+
/** A set's order as binding.yaml writes it: `text`, or
|
|
54
|
+
* `{ parts: "<separator>" }` with one or more printable ASCII
|
|
55
|
+
* characters. */
|
|
56
|
+
function parseOrder(data) {
|
|
57
|
+
if (data === "text")
|
|
58
|
+
return { rule: "text" };
|
|
59
|
+
const bad = () => {
|
|
60
|
+
throw new GuardError(`order must be text or { parts: "<separator>" }, found ${showData(data)}`);
|
|
61
|
+
};
|
|
62
|
+
if (typeof data !== "object" || data === null || Array.isArray(data))
|
|
63
|
+
return bad();
|
|
64
|
+
const entry = data;
|
|
65
|
+
if (Object.keys(entry).length !== 1 || !Object.hasOwn(entry, "parts"))
|
|
66
|
+
return bad();
|
|
67
|
+
const separator = entry.parts;
|
|
68
|
+
if (typeof separator !== "string" || !/^[\x20-\x7e]+$/.test(separator)) {
|
|
69
|
+
throw new GuardError(`order: parts must be one or more printable ASCII characters, found ${showData(separator)}`);
|
|
70
|
+
}
|
|
71
|
+
return { rule: "parts", separator };
|
|
72
|
+
}
|
|
73
|
+
/** Read binding.yaml's text. Errors carry no file name; the caller
|
|
74
|
+
* adds it. */
|
|
75
|
+
export function parseBinding(text) {
|
|
76
|
+
const top = mapping(parseYaml(text), ["format", "values", "events", "files", "commands", "probes", "tests"]);
|
|
77
|
+
if (top.format !== 1)
|
|
78
|
+
throw new GuardError("format must be 1");
|
|
79
|
+
const sets = {};
|
|
80
|
+
const orders = {};
|
|
81
|
+
for (const [set, raw] of Object.entries(within("values", () => mappingOrNone(top.values)))) {
|
|
82
|
+
within(`values ${set}`, () => {
|
|
83
|
+
// The earlier form, a bare list, held both the set and its
|
|
84
|
+
// order; it is refused with the form that replaces it.
|
|
85
|
+
if (Array.isArray(raw)) {
|
|
86
|
+
throw new GuardError(`a set is written as a mapping, not a list: declared: [<the declared values>], with order: text or order: { parts: "<separator>" } where the set is ordered`);
|
|
87
|
+
}
|
|
88
|
+
const entry = mapping(raw, ["declared", "order"]);
|
|
89
|
+
if (!Array.isArray(entry.declared)) {
|
|
90
|
+
throw new GuardError(`declared: expected a list of values, found ${showData(entry.declared)}`);
|
|
91
|
+
}
|
|
92
|
+
sets[set] = entry.declared;
|
|
93
|
+
if (entry.order !== undefined)
|
|
94
|
+
orders[set] = parseOrder(entry.order);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const vocab = vocabulary(sets, orders);
|
|
98
|
+
const events = {};
|
|
99
|
+
const declared = within("events", () => mapping(top.events));
|
|
100
|
+
for (const [event, raw] of Object.entries(declared)) {
|
|
101
|
+
within(`event ${event}`, () => {
|
|
102
|
+
const fault = eventFault(event);
|
|
103
|
+
if (fault !== null)
|
|
104
|
+
throw new GuardError(fault);
|
|
105
|
+
const entry = mappingOrNone(raw, ["params", "effect"]);
|
|
106
|
+
events[event] = {
|
|
107
|
+
params: within("params", () => typedFields(entry.params, vocab)),
|
|
108
|
+
effect: within("effect", () => typedFields(entry.effect, vocab)),
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (Object.keys(events).length === 0)
|
|
113
|
+
throw new GuardError("events: a binding declares at least one event");
|
|
114
|
+
// The names the declaration in TLA+ would define must be free.
|
|
115
|
+
checkNames({ vocabulary: vocab, events });
|
|
116
|
+
// A declaration may name no files: it loads, and no copy can then
|
|
117
|
+
// be made (guards-36).
|
|
118
|
+
if (top.files !== undefined && top.files !== null && !Array.isArray(top.files)) {
|
|
119
|
+
throw new GuardError("files: expected a list of paths to copy");
|
|
120
|
+
}
|
|
121
|
+
const files = [];
|
|
122
|
+
for (const raw of (top.files ?? [])) {
|
|
123
|
+
if (typeof raw !== "string")
|
|
124
|
+
throw new GuardError(`files: expected a path, found ${showData(raw)}`);
|
|
125
|
+
const path = raw.replace(/^(\.\/)+/, "").replace(/\/+$/, "");
|
|
126
|
+
const parts = path.split("/");
|
|
127
|
+
const outside = path === "" || path === "." || raw.startsWith("/") || /^[A-Za-z]:/.test(raw) || raw.includes("\\") ||
|
|
128
|
+
parts.includes("..") || parts.includes(".") || parts.includes("");
|
|
129
|
+
if (outside) {
|
|
130
|
+
throw new GuardError(`files: '${raw}' must be a path inside the project, written with '/'`);
|
|
131
|
+
}
|
|
132
|
+
if (parts.includes("node_modules") || parts.includes(".git")) {
|
|
133
|
+
throw new GuardError(`files: '${raw}' is in a node_modules or .git folder, which a run never copies`);
|
|
134
|
+
}
|
|
135
|
+
// The binding's own folder is copied by itself, and no kept fault
|
|
136
|
+
// may reach a binding or a guard.
|
|
137
|
+
if (parts[0] === "guards")
|
|
138
|
+
throw new GuardError(underGuards(raw));
|
|
139
|
+
if (files.includes(path))
|
|
140
|
+
throw new GuardError(`files: '${raw}' is listed twice`);
|
|
141
|
+
files.push(path);
|
|
142
|
+
}
|
|
143
|
+
const commands = [];
|
|
144
|
+
if (top.commands !== undefined && top.commands !== null) {
|
|
145
|
+
if (!Array.isArray(top.commands))
|
|
146
|
+
throw new GuardError("commands: expected a list of commands");
|
|
147
|
+
for (const [index, raw] of top.commands.entries()) {
|
|
148
|
+
const ok = Array.isArray(raw) && raw.length > 0 && raw.every((word) => typeof word === "string") && raw[0] !== "";
|
|
149
|
+
if (!ok) {
|
|
150
|
+
throw new GuardError(`commands: command ${index + 1} must be a list of words, the program first, found ${showData(raw)}`);
|
|
151
|
+
}
|
|
152
|
+
commands.push(raw);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const probes = parseProbes(top.probes);
|
|
156
|
+
const tests = parseTests(top.tests);
|
|
157
|
+
if (probes.length > 0 && tests === null) {
|
|
158
|
+
throw new GuardError("probes: a binding with probes names the command that runs the project's tests with them on, as tests: [<program>, <word>, …]");
|
|
159
|
+
}
|
|
160
|
+
if (tests !== null && probes.length === 0) {
|
|
161
|
+
throw new GuardError("tests: a binding with a test command names each probe, as probes: [<path>, …]");
|
|
162
|
+
}
|
|
163
|
+
return { vocabulary: vocab, events, files, commands, probes, tests };
|
|
164
|
+
}
|
|
165
|
+
/** `probes`: one path or more, each inside the project and written
|
|
166
|
+
* with `/`, each once; none where the key is left out. */
|
|
167
|
+
function parseProbes(data) {
|
|
168
|
+
if (data === undefined)
|
|
169
|
+
return [];
|
|
170
|
+
if (!Array.isArray(data) || data.length === 0 || !data.every((path) => typeof path === "string")) {
|
|
171
|
+
throw new GuardError(`probes: expected a list of one path or more, found ${showData(data)}`);
|
|
172
|
+
}
|
|
173
|
+
const probes = [];
|
|
174
|
+
for (const path of data) {
|
|
175
|
+
const parts = path.split("/");
|
|
176
|
+
const outside = path.startsWith("/") || /^[A-Za-z]:/.test(path) || path.includes("\\") ||
|
|
177
|
+
parts.some((part) => ["", ".", "..", "node_modules", ".git"].includes(part));
|
|
178
|
+
if (outside)
|
|
179
|
+
throw new GuardError(`probes: '${path}' must be a path inside the project, written with '/'`);
|
|
180
|
+
if (probes.includes(path))
|
|
181
|
+
throw new GuardError(`probes: '${path}' is listed twice`);
|
|
182
|
+
probes.push(path);
|
|
183
|
+
}
|
|
184
|
+
return probes;
|
|
185
|
+
}
|
|
186
|
+
/** `tests`: one command as an entry of `commands` is written; null
|
|
187
|
+
* where the key is left out. */
|
|
188
|
+
function parseTests(data) {
|
|
189
|
+
if (data === undefined)
|
|
190
|
+
return null;
|
|
191
|
+
const ok = Array.isArray(data) && data.length > 0 && data.every((word) => typeof word === "string") && data[0] !== "";
|
|
192
|
+
if (!ok) {
|
|
193
|
+
throw new GuardError(`tests: expected one command, a list of one word or more, the program first, found ${showData(data)}`);
|
|
194
|
+
}
|
|
195
|
+
return data;
|
|
196
|
+
}
|
|
197
|
+
/** Why a probe of the binding in `folder` cannot be one, or null: it
|
|
198
|
+
* is no file, leads out of the project, whose real path is `base`, is
|
|
199
|
+
* not copied, or is the binding's own declaration or module. */
|
|
200
|
+
function probeFault(root, base, folder, files, path) {
|
|
201
|
+
if (!isFile(abs(root, path)))
|
|
202
|
+
return `probes: '${path}' is no file of the project`;
|
|
203
|
+
if (!inside(base, realpathSync.native(abs(root, path))))
|
|
204
|
+
return `probes: '${path}' leads out of the project`;
|
|
205
|
+
if (!path.startsWith(`${folder}/`) && !amongFiles(files, path)) {
|
|
206
|
+
return `probes: '${path}' is not copied: list it under files, or keep it in the binding's folder`;
|
|
207
|
+
}
|
|
208
|
+
if (path === `${folder}/binding.yaml` || path === `${folder}/binding.mjs`) {
|
|
209
|
+
return `probes: '${path}' is the binding's declaration or its module`;
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
function abs(root, path) {
|
|
214
|
+
return join(root, ...path.split("/"));
|
|
215
|
+
}
|
|
216
|
+
/** The names in a folder, in order, without `.DS_Store`. */
|
|
217
|
+
function names(dir) {
|
|
218
|
+
return readdirSync(dir)
|
|
219
|
+
.filter((name) => name !== ".DS_Store")
|
|
220
|
+
.sort(byText);
|
|
221
|
+
}
|
|
222
|
+
function isDir(path) {
|
|
223
|
+
return existsSync(path) && statSync(path).isDirectory();
|
|
224
|
+
}
|
|
225
|
+
function isFile(path) {
|
|
226
|
+
return existsSync(path) && statSync(path).isFile();
|
|
227
|
+
}
|
|
228
|
+
/** Every file under `folder` (relative to the root), in order,
|
|
229
|
+
* leaving out the entries a copy leaves out, whatever they are, so
|
|
230
|
+
* that a copy holds exactly these files of the folder. An entry that
|
|
231
|
+
* leads out of the project, whose real path is `base`, is refused as
|
|
232
|
+
* a copy refuses it (guards-3). */
|
|
233
|
+
function filesUnder(root, base, folder) {
|
|
234
|
+
const found = [];
|
|
235
|
+
for (const name of names(abs(root, folder))) {
|
|
236
|
+
if (leftOut(name))
|
|
237
|
+
continue;
|
|
238
|
+
const path = `${folder}/${name}`;
|
|
239
|
+
if (existsSync(abs(root, path)) && !inside(base, realpathSync.native(abs(root, path)))) {
|
|
240
|
+
throw new GuardError(`${path} leads out of the project`);
|
|
241
|
+
}
|
|
242
|
+
if (isDir(abs(root, path)))
|
|
243
|
+
found.push(...filesUnder(root, base, path));
|
|
244
|
+
else
|
|
245
|
+
found.push(path);
|
|
246
|
+
}
|
|
247
|
+
return found.sort(byText);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Load the binding `guards/bindings/<name>` and digest every file in
|
|
251
|
+
* its folder. A folder with no `binding.mjs` still loads. Every error
|
|
252
|
+
* is a GuardError starting with the file.
|
|
253
|
+
*/
|
|
254
|
+
export function loadBinding(root, name) {
|
|
255
|
+
const folder = `guards/bindings/${name}`;
|
|
256
|
+
if (!FOLDER_NAME_RE.test(name))
|
|
257
|
+
throw new GuardError(`'${name}' cannot be a binding's name`);
|
|
258
|
+
if (!isDir(abs(root, folder)))
|
|
259
|
+
throw new GuardError(`${folder}: no such binding`);
|
|
260
|
+
const declarationFile = `${folder}/binding.yaml`;
|
|
261
|
+
if (!isFile(abs(root, declarationFile)))
|
|
262
|
+
throw new GuardError(`${declarationFile}: not found`);
|
|
263
|
+
const base = realpathSync.native(root);
|
|
264
|
+
const folderFiles = filesUnder(root, base, folder);
|
|
265
|
+
const inputs = {};
|
|
266
|
+
let text = "";
|
|
267
|
+
for (const path of folderFiles) {
|
|
268
|
+
const bytes = readFileSync(abs(root, path));
|
|
269
|
+
inputs[fileLabel(path)] = digest(bytes);
|
|
270
|
+
if (path === declarationFile)
|
|
271
|
+
text = bytes.toString("utf8");
|
|
272
|
+
}
|
|
273
|
+
const declaration = within(declarationFile, () => {
|
|
274
|
+
const parsed = parseBinding(text);
|
|
275
|
+
// Where names differ only in case, or through a link, an entry
|
|
276
|
+
// may lead under guards/ without saying so.
|
|
277
|
+
const guards = realpathSync.native(abs(root, "guards"));
|
|
278
|
+
for (const path of parsed.files) {
|
|
279
|
+
if (!existsSync(abs(root, path)))
|
|
280
|
+
throw new GuardError(`files: '${path}' is not in the project`);
|
|
281
|
+
const real = realpathSync.native(abs(root, path));
|
|
282
|
+
if (real === guards || real.startsWith(guards + sep))
|
|
283
|
+
throw new GuardError(underGuards(path));
|
|
284
|
+
// A link is followed only where it leads: a path that leads out
|
|
285
|
+
// of the project names files a fresh checkout would lack.
|
|
286
|
+
if (!inside(base, real))
|
|
287
|
+
throw new GuardError(`files: '${path}' leads out of the project`);
|
|
288
|
+
}
|
|
289
|
+
for (const path of parsed.probes) {
|
|
290
|
+
const fault = probeFault(root, base, folder, parsed.files, path);
|
|
291
|
+
if (fault !== null)
|
|
292
|
+
throw new GuardError(fault);
|
|
293
|
+
}
|
|
294
|
+
return parsed;
|
|
295
|
+
});
|
|
296
|
+
// A probe outside the binding's folder is an input by its own path.
|
|
297
|
+
for (const path of declaration.probes) {
|
|
298
|
+
if (!path.startsWith(`${folder}/`))
|
|
299
|
+
inputs[fileLabel(path)] = digest(readFileSync(abs(root, path)));
|
|
300
|
+
}
|
|
301
|
+
// One listing decides what the binding has and what its inputs are.
|
|
302
|
+
const module = folderFiles.includes(`${folder}/binding.mjs`);
|
|
303
|
+
const tested = declaration.tests !== null;
|
|
304
|
+
const kind = module ? (tested ? "both" : "module") : tested ? "probes" : "neither";
|
|
305
|
+
return { binding: { ...declaration, name, folder, folderFiles, module, kind }, inputs };
|
|
306
|
+
}
|
|
307
|
+
// ---------------------------------------------------------------
|
|
308
|
+
// guard.yaml
|
|
309
|
+
// ---------------------------------------------------------------
|
|
310
|
+
/** The most seconds a time limit can be: the longest a timer of Node
|
|
311
|
+
* runs, 2147483647 ms. */
|
|
312
|
+
const MOST_SECONDS = 2147483;
|
|
313
|
+
function limit(raw, name) {
|
|
314
|
+
if (typeof raw !== "number" || !Number.isInteger(raw) || raw < 1 || raw > MOST_SECONDS) {
|
|
315
|
+
throw new GuardError(`limits.${name} must be a whole number from 1 to ${MOST_SECONDS}, found ${showData(raw)}`);
|
|
316
|
+
}
|
|
317
|
+
return raw;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Read guard.yaml's text (guards-10). `declarationOf` gives the
|
|
321
|
+
* declaration of the binding the file names, against which every step
|
|
322
|
+
* is decoded: every param stated, the stated effect fields of the
|
|
323
|
+
* right type. A label entered twice is an error here; whether every
|
|
324
|
+
* entry is a case of the guard item is `caseSteps`'s to say. Errors
|
|
325
|
+
* carry no file name; the caller adds it.
|
|
326
|
+
*/
|
|
327
|
+
export function parseGuardFile(text, declarationOf) {
|
|
328
|
+
const top = mapping(parseYaml(text), ["format", "binding", "limits", "cases"]);
|
|
329
|
+
if (top.format !== 1)
|
|
330
|
+
throw new GuardError("format must be 1");
|
|
331
|
+
if (typeof top.binding !== "string") {
|
|
332
|
+
throw new GuardError(`binding must be a binding's name, found ${showData(top.binding)}`);
|
|
333
|
+
}
|
|
334
|
+
const written = within("limits", () => mapping(top.limits, ["seconds"]));
|
|
335
|
+
const limits = { seconds: limit(written.seconds, "seconds") };
|
|
336
|
+
const declaration = declarationOf(top.binding);
|
|
337
|
+
if (!Array.isArray(top.cases))
|
|
338
|
+
throw new GuardError("cases must be a list");
|
|
339
|
+
const entries = [];
|
|
340
|
+
for (const [index, raw] of top.cases.entries()) {
|
|
341
|
+
const entry = within(`cases entry ${index + 1}`, () => mapping(raw, ["case", "steps"]));
|
|
342
|
+
if (typeof entry.case !== "string" || entry.case.trim() === "") {
|
|
343
|
+
throw new GuardError(`cases entry ${index + 1}: case must be the label of a case of the guard item, found ${showData(entry.case)}`);
|
|
344
|
+
}
|
|
345
|
+
const label = entry.case;
|
|
346
|
+
if (entries.some((other) => other.label === label))
|
|
347
|
+
throw new GuardError(`a case entered twice: ${label}`);
|
|
348
|
+
within(`case '${label}'`, () => {
|
|
349
|
+
if (!Array.isArray(entry.steps) || entry.steps.length === 0) {
|
|
350
|
+
throw new GuardError("steps must be a list of one or more steps");
|
|
351
|
+
}
|
|
352
|
+
const steps = entry.steps.map((rawStep, at) => within(`step ${at + 1}`, () => {
|
|
353
|
+
const step = mapping(rawStep, ["event", "params", "effect"]);
|
|
354
|
+
if (typeof step.event !== "string" || !Object.hasOwn(declaration.events, step.event)) {
|
|
355
|
+
throw new GuardError(`the binding declares no event ${showData(step.event)}`);
|
|
356
|
+
}
|
|
357
|
+
const declared = declaration.events[step.event];
|
|
358
|
+
return {
|
|
359
|
+
event: step.event,
|
|
360
|
+
params: within("params", () => decodeFields(declared.params, step.params ?? {}, declaration.vocabulary)),
|
|
361
|
+
effect: within("effect", () => decodeFields(declared.effect, step.effect ?? {}, declaration.vocabulary, { partial: true })),
|
|
362
|
+
};
|
|
363
|
+
}));
|
|
364
|
+
entries.push({ label, steps });
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return { binding: top.binding, limits, entries };
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Each case of the guard item with its steps from the guard file, in
|
|
371
|
+
* the item's order (guards-10). A case with no entry, and an entry
|
|
372
|
+
* for no case, is a GuardError naming the label; the caller adds the
|
|
373
|
+
* file.
|
|
374
|
+
*/
|
|
375
|
+
export function caseSteps(cases, entries) {
|
|
376
|
+
for (const entry of entries) {
|
|
377
|
+
if (!cases.some((known) => known.label === entry.label)) {
|
|
378
|
+
throw new GuardError(`an entry for no case: ${entry.label}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return cases.map((known) => {
|
|
382
|
+
const entry = entries.find((other) => other.label === known.label);
|
|
383
|
+
if (entry === undefined)
|
|
384
|
+
throw new GuardError(`a case with no entry: ${known.label}`);
|
|
385
|
+
return { label: known.label, kind: known.kind, words: known.words, steps: entry.steps };
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
// ---------------------------------------------------------------
|
|
389
|
+
// One guard
|
|
390
|
+
// ---------------------------------------------------------------
|
|
391
|
+
/** The guards under `<root>/guards`: every folder there other than
|
|
392
|
+
* `bindings`, with or without a guard.yaml, so that a guard whose
|
|
393
|
+
* file is gone or misnamed is reported and not passed over. In name
|
|
394
|
+
* order. */
|
|
395
|
+
export function listGuards(root) {
|
|
396
|
+
const dir = join(root, "guards");
|
|
397
|
+
if (!isDir(dir))
|
|
398
|
+
return [];
|
|
399
|
+
return names(dir).filter((name) => name !== "bindings" && isDir(join(dir, name)));
|
|
400
|
+
}
|
|
401
|
+
/** Whether `path` is one of a binding's `files` or under one of
|
|
402
|
+
* them, in no `node_modules` or `.git` folder: a file a run copies. */
|
|
403
|
+
function amongFiles(files, path) {
|
|
404
|
+
const parts = path.split("/");
|
|
405
|
+
if (parts.includes("node_modules") || parts.includes(".git"))
|
|
406
|
+
return false;
|
|
407
|
+
return files.some((file) => path === file || path.startsWith(`${file}/`));
|
|
408
|
+
}
|
|
409
|
+
/** The `<name><suffix>` files of a folder, in name order: by the name
|
|
410
|
+
* without its extension, so `a` comes before `a-b` (guards-1). */
|
|
411
|
+
function namedFiles(root, folder, suffix) {
|
|
412
|
+
if (!existsSync(abs(root, folder)))
|
|
413
|
+
return [];
|
|
414
|
+
if (!isDir(abs(root, folder)))
|
|
415
|
+
throw new GuardError(`${folder}: expected a folder`);
|
|
416
|
+
return names(abs(root, folder))
|
|
417
|
+
.map((entry) => {
|
|
418
|
+
const path = `${folder}/${entry}`;
|
|
419
|
+
const name = entry.slice(0, -suffix.length);
|
|
420
|
+
if (!entry.endsWith(suffix) || !FOLDER_NAME_RE.test(name) || !isFile(abs(root, path))) {
|
|
421
|
+
throw new GuardError(`${path}: only <name>${suffix} files are kept in this folder`);
|
|
422
|
+
}
|
|
423
|
+
return { name, path };
|
|
424
|
+
})
|
|
425
|
+
.sort((a, b) => byText(a.name, b.name));
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Load one guard: its items, guard file, binding declaration,
|
|
429
|
+
* translations and kept faults, each checked but a translation's
|
|
430
|
+
* text, which is TLC's to read, with the digest of
|
|
431
|
+
* every approval input. `index` is the tree's items where the caller has
|
|
432
|
+
* read them already. Every error is a GuardError naming the file or
|
|
433
|
+
* item and the cause; the approval record is not read here.
|
|
434
|
+
*/
|
|
435
|
+
export function loadGuard(root, name, index) {
|
|
436
|
+
const folder = `guards/${name}`;
|
|
437
|
+
const guardFile = `${folder}/guard.yaml`;
|
|
438
|
+
if (!FOLDER_NAME_RE.test(name) || name === "bindings")
|
|
439
|
+
throw new GuardError(`'${name}' cannot be a guard's name`);
|
|
440
|
+
if (!isFile(abs(root, guardFile)))
|
|
441
|
+
throw new GuardError(`${guardFile}: not found`);
|
|
442
|
+
for (const entry of names(abs(root, folder))) {
|
|
443
|
+
if (!["guard.yaml", "translations", "proofs", "faults", "approval.json"].includes(entry)) {
|
|
444
|
+
throw new GuardError(`${folder}/${entry}: not part of a guard; its folder holds guard.yaml, translations/, proofs/, faults/ and approval.json`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const inputs = {};
|
|
448
|
+
const read = (path) => {
|
|
449
|
+
const bytes = readFileSync(abs(root, path));
|
|
450
|
+
inputs[fileLabel(path)] = digest(bytes);
|
|
451
|
+
return bytes.toString("utf8");
|
|
452
|
+
};
|
|
453
|
+
let binding;
|
|
454
|
+
// A fault in the binding names its own file, not guard.yaml.
|
|
455
|
+
let bindingFault;
|
|
456
|
+
let file;
|
|
457
|
+
try {
|
|
458
|
+
file = within(guardFile, () => parseGuardFile(read(guardFile), (bindingName) => {
|
|
459
|
+
try {
|
|
460
|
+
const loaded = loadBinding(root, bindingName);
|
|
461
|
+
Object.assign(inputs, loaded.inputs);
|
|
462
|
+
binding = loaded.binding;
|
|
463
|
+
return loaded.binding;
|
|
464
|
+
}
|
|
465
|
+
catch (error) {
|
|
466
|
+
if (error instanceof GuardError)
|
|
467
|
+
bindingFault = error;
|
|
468
|
+
throw error;
|
|
469
|
+
}
|
|
470
|
+
}));
|
|
471
|
+
}
|
|
472
|
+
catch (error) {
|
|
473
|
+
throw bindingFault ?? error;
|
|
474
|
+
}
|
|
475
|
+
if (binding === undefined)
|
|
476
|
+
throw new GuardError(`${guardFile}: no binding`);
|
|
477
|
+
const declaration = binding;
|
|
478
|
+
const items = guardItems(index ?? readItems(root), name);
|
|
479
|
+
for (const item of [items.guard, items.behavior, ...items.cited]) {
|
|
480
|
+
inputs[itemLabel(item.id)] = digest(item.text);
|
|
481
|
+
}
|
|
482
|
+
const cases = within(guardFile, () => caseSteps(items.cases, file.entries));
|
|
483
|
+
const translations = namedFiles(root, `${folder}/translations`, ".tla").map((entry) => {
|
|
484
|
+
const fault = translationNameFault(entry.name);
|
|
485
|
+
if (fault !== null)
|
|
486
|
+
throw new GuardError(`${entry.path}: ${fault}`);
|
|
487
|
+
return { name: entry.name, file: entry.path, text: read(entry.path) };
|
|
488
|
+
});
|
|
489
|
+
if (translations.length === 0) {
|
|
490
|
+
throw new NoTranslation(`${folder}/translations: no translation; a guard needs two or more`, declaration);
|
|
491
|
+
}
|
|
492
|
+
// A translation's proofs are read but not digested: they are no
|
|
493
|
+
// approval input, since every run checks them again.
|
|
494
|
+
const proofs = namedFiles(root, `${folder}/proofs`, "_proofs.tla").map((entry) => {
|
|
495
|
+
if (!translations.some((translation) => translation.name === entry.name)) {
|
|
496
|
+
throw new GuardError(`${entry.path}: no translation ${entry.name}; a proof file is named after the translation it proves`);
|
|
497
|
+
}
|
|
498
|
+
return { name: entry.name, file: entry.path, text: readFileSync(abs(root, entry.path), "utf8") };
|
|
499
|
+
});
|
|
500
|
+
// A probe is every file that writes a line or works out what one
|
|
501
|
+
// holds (guards-50), so no kept fault may change one: by its own
|
|
502
|
+
// path, by a link to it, or by a name that differs only in case
|
|
503
|
+
// where the file system ignores case, each compared by real path.
|
|
504
|
+
const probes = new Set(declaration.probes.map((path) => realpathSync.native(abs(root, path))));
|
|
505
|
+
const isProbe = (path) => declaration.probes.includes(path) ||
|
|
506
|
+
(existsSync(abs(root, path)) && probes.has(realpathSync.native(abs(root, path))));
|
|
507
|
+
const faults = namedFiles(root, `${folder}/faults`, ".patch").map((entry) => {
|
|
508
|
+
const patch = within(entry.path, () => {
|
|
509
|
+
const parsed = parsePatch(read(entry.path));
|
|
510
|
+
// A kept fault is a defect in the code a run copies, never in
|
|
511
|
+
// the binding or in an installed dependency. No entry of `files`
|
|
512
|
+
// is under guards/; a path there is refused here all the same.
|
|
513
|
+
// Where the declaration names no files yet, it loads (guards-3)
|
|
514
|
+
// and the failing binding says so; only what holds of any path
|
|
515
|
+
// is then checked here.
|
|
516
|
+
for (const changed of parsed.files) {
|
|
517
|
+
if (isProbe(changed.path)) {
|
|
518
|
+
throw new GuardError(`${changed.path} is a probe of the binding; a kept fault changes the code, never the binding`);
|
|
519
|
+
}
|
|
520
|
+
const parts = changed.path.split("/");
|
|
521
|
+
const never = parts[0] === "guards" || parts.includes("node_modules") || parts.includes(".git");
|
|
522
|
+
if (never || (declaration.files.length > 0 && !amongFiles(declaration.files, changed.path))) {
|
|
523
|
+
throw new GuardError(`${changed.path} is not among the binding's files`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return parsed;
|
|
527
|
+
});
|
|
528
|
+
return { name: entry.name, file: entry.path, patch };
|
|
529
|
+
});
|
|
530
|
+
return {
|
|
531
|
+
name,
|
|
532
|
+
root,
|
|
533
|
+
folder,
|
|
534
|
+
items,
|
|
535
|
+
binding: declaration,
|
|
536
|
+
limits: file.limits,
|
|
537
|
+
cases,
|
|
538
|
+
translations,
|
|
539
|
+
faults,
|
|
540
|
+
proofs,
|
|
541
|
+
inputs: sortInputs(inputs),
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
//# sourceMappingURL=files.js.map
|