@okfit/engine 0.14.1 → 0.16.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/config/anchor.js +1 -1
- package/config/layer.js +1 -1
- package/edits/FrontmatterEdits.js +66 -18
- package/errors.js +102 -6
- package/index.d.ts +685 -53
- package/index.js +12 -6
- package/move/references.js +267 -0
- package/move/run.js +212 -0
- package/package.json +18 -18
- package/query/ConceptQuery.js +15 -4
- package/render/context.js +20 -3
- package/render/move.js +52 -0
- package/render/query.js +53 -6
- package/render/sync.js +4 -1
- package/render/verify.js +43 -1
- package/session/BundleSession.js +3 -1
- package/sync/generated.js +112 -49
- package/sync/index.js +4 -3
- package/sync/publication.js +3 -3
- package/sync/run.js +13 -4
- package/targets/inside.js +15 -0
- package/targets/resolve.js +146 -0
- package/trust/run.js +32 -0
- package/validate/run.js +8 -4
- package/verify/diff.js +63 -0
- package/verify/integrity.js +309 -0
- package/verify/locate.js +3 -10
- package/verify/revoke.js +101 -0
- package/verify/run.js +193 -21
- package/verify/select.js +30 -11
- package/verify/splice.js +20 -34
- package/version.js +1 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { TargetResolutionError } from "../errors.js";
|
|
2
|
+
import { relativeInside } from "./inside.js";
|
|
3
|
+
import { ConceptId } from "@okfit/core";
|
|
4
|
+
import { Effect, FileSystem, Option, Path } from "effect";
|
|
5
|
+
import { posix } from "node:path";
|
|
6
|
+
|
|
7
|
+
//#region src/targets/resolve.ts
|
|
8
|
+
const levenshtein = (a, b) => {
|
|
9
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
10
|
+
for (let i = 1; i <= a.length; i++) {
|
|
11
|
+
let previous = row[0] ?? 0;
|
|
12
|
+
row[0] = i;
|
|
13
|
+
for (let j = 1; j <= b.length; j++) {
|
|
14
|
+
const current = row[j] ?? 0;
|
|
15
|
+
row[j] = Math.min(current + 1, (row[j - 1] ?? 0) + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
16
|
+
previous = current;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return row[b.length] ?? 0;
|
|
20
|
+
};
|
|
21
|
+
const suggest = (arg, bundle) => {
|
|
22
|
+
const wanted = Option.getOrUndefined(ConceptId.normalize(arg));
|
|
23
|
+
if (wanted === void 0) return void 0;
|
|
24
|
+
const ids = [...bundle.concepts.keys()].sort();
|
|
25
|
+
const base = posix.basename(wanted);
|
|
26
|
+
const byBase = ids.filter((id) => posix.basename(id) === base);
|
|
27
|
+
if (byBase.length === 1) return byBase[0];
|
|
28
|
+
const limit = Math.max(2, Math.floor(wanted.length / 3));
|
|
29
|
+
let best;
|
|
30
|
+
for (const id of ids) {
|
|
31
|
+
const distance = levenshtein(wanted, id);
|
|
32
|
+
if (distance <= limit && (best === void 0 || distance < best.distance)) best = {
|
|
33
|
+
id,
|
|
34
|
+
distance
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return best?.id;
|
|
38
|
+
};
|
|
39
|
+
const withSuggestion = (arg, bundle) => {
|
|
40
|
+
const suggestion = suggest(arg, bundle);
|
|
41
|
+
return suggestion === void 0 ? {
|
|
42
|
+
arg,
|
|
43
|
+
reason: "not-found"
|
|
44
|
+
} : {
|
|
45
|
+
arg,
|
|
46
|
+
reason: "not-found",
|
|
47
|
+
suggestion
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/** Bundle-relative posix path of `absolute`, or `undefined` when it lies outside `root`. */
|
|
51
|
+
const inside = (path, root, absolute) => {
|
|
52
|
+
const relative = relativeInside(path, root, absolute);
|
|
53
|
+
return relative === "" ? void 0 : relative;
|
|
54
|
+
};
|
|
55
|
+
const resolve = Effect.fn("okfit/targets/resolve")(function* (args, bundle, cwd, options) {
|
|
56
|
+
const fs = yield* FileSystem.FileSystem;
|
|
57
|
+
const path = yield* Path.Path;
|
|
58
|
+
const files = new Set(bundle.files);
|
|
59
|
+
const acceptFiles = options?.files === true;
|
|
60
|
+
const realOr = (absolute) => fs.realPath(absolute).pipe(Effect.orElseSucceed(() => absolute));
|
|
61
|
+
const root = yield* realOr(bundle.root);
|
|
62
|
+
/** A bundle-relative file path that names a target, or why it does not. */
|
|
63
|
+
const classify = (relative) => {
|
|
64
|
+
if (!files.has(relative)) return void 0;
|
|
65
|
+
if (acceptFiles) return { path: relative };
|
|
66
|
+
if (ConceptId.isReservedFile(relative)) return { failure: "reserved" };
|
|
67
|
+
const id = Option.getOrUndefined(ConceptId.fromPath(relative));
|
|
68
|
+
return id !== void 0 && bundle.concepts.has(id) ? { path: relative } : { failure: "not-a-concept" };
|
|
69
|
+
};
|
|
70
|
+
/** A filesystem candidate (absolute or cwd-relative): only an existing file inside the bundle counts. */
|
|
71
|
+
const onDisk = (absolute) => Effect.gen(function* () {
|
|
72
|
+
if (!(yield* fs.exists(absolute).pipe(Effect.orElseSucceed(() => false)))) return {
|
|
73
|
+
outside: false,
|
|
74
|
+
hit: void 0
|
|
75
|
+
};
|
|
76
|
+
const relative = inside(path, root, yield* realOr(absolute));
|
|
77
|
+
return relative === void 0 ? {
|
|
78
|
+
outside: true,
|
|
79
|
+
hit: void 0
|
|
80
|
+
} : {
|
|
81
|
+
outside: false,
|
|
82
|
+
hit: classify(relative)
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
const resolved = [];
|
|
86
|
+
const failures = [];
|
|
87
|
+
const seen = /* @__PURE__ */ new Set();
|
|
88
|
+
const accept = (arg, relative) => {
|
|
89
|
+
if (seen.has(relative)) return;
|
|
90
|
+
seen.add(relative);
|
|
91
|
+
const id = Option.getOrUndefined(ConceptId.fromPath(relative));
|
|
92
|
+
resolved.push({
|
|
93
|
+
arg,
|
|
94
|
+
path: relative,
|
|
95
|
+
id: id !== void 0 && bundle.concepts.has(id) ? id : void 0
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
for (const arg of args) {
|
|
99
|
+
const fromDisk = yield* onDisk(path.isAbsolute(arg) ? arg : path.resolve(cwd, arg));
|
|
100
|
+
const id = Option.getOrUndefined(ConceptId.normalize(arg.replace(/^\/+/, "")));
|
|
101
|
+
const byId = id === void 0 ? void 0 : classify(ConceptId.toPath(id));
|
|
102
|
+
const diskPath = fromDisk.hit !== void 0 && "path" in fromDisk.hit ? fromDisk.hit.path : void 0;
|
|
103
|
+
const idPath = byId !== void 0 && "path" in byId ? byId.path : void 0;
|
|
104
|
+
if (diskPath !== void 0 && idPath !== void 0 && diskPath !== idPath && !path.isAbsolute(arg)) failures.push({
|
|
105
|
+
arg,
|
|
106
|
+
reason: "ambiguous",
|
|
107
|
+
candidates: [diskPath, idPath]
|
|
108
|
+
});
|
|
109
|
+
else if (diskPath !== void 0) accept(arg, diskPath);
|
|
110
|
+
else if (idPath !== void 0) accept(arg, idPath);
|
|
111
|
+
else if (fromDisk.hit !== void 0 && "failure" in fromDisk.hit) failures.push({
|
|
112
|
+
arg,
|
|
113
|
+
reason: fromDisk.hit.failure
|
|
114
|
+
});
|
|
115
|
+
else if (byId !== void 0 && "failure" in byId) failures.push({
|
|
116
|
+
arg,
|
|
117
|
+
reason: byId.failure
|
|
118
|
+
});
|
|
119
|
+
else failures.push(fromDisk.outside ? {
|
|
120
|
+
arg,
|
|
121
|
+
reason: "outside-bundle"
|
|
122
|
+
} : withSuggestion(arg, bundle));
|
|
123
|
+
}
|
|
124
|
+
if (failures.length > 0) return yield* new TargetResolutionError({ failures });
|
|
125
|
+
return resolved;
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* Concept targets on the CLI (spec section 13): the shared resolver every
|
|
129
|
+
* concept command calls before it reads or writes anything. Resolution order
|
|
130
|
+
* is absolute path, cwd-relative path, then bundle-relative id; a candidate
|
|
131
|
+
* outside the bundle is discarded, never matched. With `files: true` any
|
|
132
|
+
* walked bundle file is accepted, so a reserved or undecodable file can still
|
|
133
|
+
* be validated.
|
|
134
|
+
*
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
var Targets = class {
|
|
138
|
+
constructor() {}
|
|
139
|
+
/** Resolve every argument or fail with every bad one; duplicates collapse to the first spelling. */
|
|
140
|
+
static resolve = resolve;
|
|
141
|
+
/** The nearest concept id to `arg`: a unique basename match first, else the closest by edit distance. */
|
|
142
|
+
static suggest = suggest;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { Targets };
|
package/trust/run.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Bundle } from "@okfit/core";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Trust } from "@okfit/profiles";
|
|
4
|
+
|
|
5
|
+
//#region src/trust/run.ts
|
|
6
|
+
const FIRST = 5;
|
|
7
|
+
/**
|
|
8
|
+
* The opt-in half of `context`: load the bundle and fold `Trust.signals`
|
|
9
|
+
* into counts plus the first five drifted and stale ids. Kept apart from
|
|
10
|
+
* {@link runContext} so the default path never loads the bundle; only
|
|
11
|
+
* `--trust` pays for it. Hashing happens inside `Trust.signals`, and only
|
|
12
|
+
* for concepts a human verification binds.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
const runContextTrust = (options) => Effect.gen(function* () {
|
|
17
|
+
const bundle = yield* Bundle.load({ root: options.bundleRoot });
|
|
18
|
+
const entries = [...(yield* Trust.signals(bundle, options.config, options.now)).entries()].toSorted(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
19
|
+
const driftedIds = entries.filter(([, s]) => s.integrity === "drifted").map(([id]) => id);
|
|
20
|
+
const staleIds = entries.filter(([, s]) => s.stale).map(([id]) => id);
|
|
21
|
+
return {
|
|
22
|
+
concepts: entries.length,
|
|
23
|
+
drifted: driftedIds.length,
|
|
24
|
+
stale: staleIds.length,
|
|
25
|
+
unverified: entries.filter(([, s]) => s.tier === "unverified").length,
|
|
26
|
+
drifted_ids: driftedIds.slice(0, FIRST),
|
|
27
|
+
stale_ids: staleIds.slice(0, FIRST)
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
export { runContextTrust };
|
package/validate/run.js
CHANGED
|
@@ -2,7 +2,7 @@ import { lintResources } from "./resources.js";
|
|
|
2
2
|
import { lintSurfaces } from "./surfaces.js";
|
|
3
3
|
import { Bundle, OkfitConfig, Validate } from "@okfit/core";
|
|
4
4
|
import { Context, Effect, Option } from "effect";
|
|
5
|
-
import { Provenance, Publications } from "@okfit/profiles";
|
|
5
|
+
import { Provenance, Publications, Trust } from "@okfit/profiles";
|
|
6
6
|
|
|
7
7
|
//#region src/validate/run.ts
|
|
8
8
|
/**
|
|
@@ -28,7 +28,7 @@ var Now = class extends Context.Service()("@okfit/engine/Now") {};
|
|
|
28
28
|
* HERE, in `run`: a bundle configured `off` never calls `Provenance.lint`
|
|
29
29
|
* at all. `options.skipProvenance` is no longer that gate — it is threaded
|
|
30
30
|
* through as `Provenance.lint`'s `skipGitTier` option instead, so tier 1
|
|
31
|
-
* (the pure, in-memory
|
|
31
|
+
* (the pure, in-memory recorded-body-digest comparison) still runs and
|
|
32
32
|
* still reports drift even when `skipProvenance` is `true`; only tier 2
|
|
33
33
|
* (the git-derived date comparison, for concepts with no recorded digest)
|
|
34
34
|
* is skipped, and with it the only git spawn `Provenance.lint` can make
|
|
@@ -38,7 +38,7 @@ var Now = class extends Context.Service()("@okfit/engine/Now") {};
|
|
|
38
38
|
* the same `report.lint` array every other core lint diagnostic already
|
|
39
39
|
* does.
|
|
40
40
|
*
|
|
41
|
-
* It then appends `lintResources`, `Publications.lint` (`publication-drift`
|
|
41
|
+
* It then appends `Trust.lint` (`verified-drift`, which gates itself on its own severity), `lintResources`, `Publications.lint` (`publication-drift`
|
|
42
42
|
* and `publication-orphan`, only under the `software-project` profile) and `lintSurfaces` (`surface-unmatched`), each
|
|
43
43
|
* of which gates itself on its own severity.
|
|
44
44
|
*
|
|
@@ -59,6 +59,8 @@ const run = (options) => Effect.gen(function* () {
|
|
|
59
59
|
onSome: (profile) => profile.check(bundle)
|
|
60
60
|
});
|
|
61
61
|
const provenance = OkfitConfig.severityFor(options.config, "generated-at-drift") === "off" ? [] : yield* Provenance.lint(bundle, options.config, { skipGitTier: options.skipProvenance === true });
|
|
62
|
+
const signals = yield* Trust.signals(bundle, options.config, options.now);
|
|
63
|
+
const drift = yield* Trust.lint(bundle, options.config, options.now, signals);
|
|
62
64
|
const resources = yield* lintResources(bundle, options.config);
|
|
63
65
|
const publications = Option.exists(options.profile, (profile) => profile.name === "software-project") ? yield* Publications.lint(bundle, options.config) : [];
|
|
64
66
|
const surfaces = yield* lintSurfaces(bundle, options.config);
|
|
@@ -69,12 +71,14 @@ const run = (options) => Effect.gen(function* () {
|
|
|
69
71
|
lint: [
|
|
70
72
|
...report.lint,
|
|
71
73
|
...provenance,
|
|
74
|
+
...drift,
|
|
72
75
|
...resources,
|
|
73
76
|
...publications,
|
|
74
77
|
...surfaces
|
|
75
78
|
]
|
|
76
79
|
},
|
|
77
|
-
profileDiagnostics
|
|
80
|
+
profileDiagnostics,
|
|
81
|
+
signals
|
|
78
82
|
};
|
|
79
83
|
});
|
|
80
84
|
|
package/verify/diff.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { DateTime, Effect, Path } from "effect";
|
|
2
|
+
import { Git, GitCommandError } from "@effected/git";
|
|
3
|
+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
4
|
+
|
|
5
|
+
//#region src/verify/diff.ts
|
|
6
|
+
/**
|
|
7
|
+
* The patch between the version of `file` a human verified (`at`) and what is
|
|
8
|
+
* on disk now.
|
|
9
|
+
*
|
|
10
|
+
* The baseline is the newest commit touching the file whose committer date is
|
|
11
|
+
* at or before `at`; the patch is `git diff <baseline> -- <file>`.
|
|
12
|
+
*
|
|
13
|
+
* Git access: `@effected/git`'s `Git.log` (scoped to the file, newest first,
|
|
14
|
+
* filtered here on `committedAt`) finds the baseline. `Git.log` has no `until`,
|
|
15
|
+
* and no `Git` member returns a patch (`show` returns a blob, `nameStatus` only
|
|
16
|
+
* paths), so the diff itself runs `git diff` through `effect/process`'s
|
|
17
|
+
* `ChildProcessSpawner`, the same spawner `Git.layer` is built on. Spawn
|
|
18
|
+
* failure maps onto `Git`'s own `GitCommandError`.
|
|
19
|
+
*
|
|
20
|
+
* @param options - `file` is the absolute path of the concept file; `at` is the
|
|
21
|
+
* time the binding recorded.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
const changeSince = Effect.fn("okfit/verify/changeSince")(function* (options) {
|
|
25
|
+
const git = yield* Git;
|
|
26
|
+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
27
|
+
const path = yield* Path.Path;
|
|
28
|
+
const cwd = path.dirname(options.file);
|
|
29
|
+
const name = path.basename(options.file);
|
|
30
|
+
const log = yield* git.log(cwd, { paths: [name] }).pipe(Effect.catchTag("NotARepositoryError", () => Effect.succeed(void 0)));
|
|
31
|
+
if (log === void 0) return {
|
|
32
|
+
_tag: "no-baseline",
|
|
33
|
+
reason: "not-a-repository"
|
|
34
|
+
};
|
|
35
|
+
const baseline = log.find((entry) => DateTime.isLessThanOrEqualTo(entry.committedAt, options.at));
|
|
36
|
+
if (baseline === void 0) return {
|
|
37
|
+
_tag: "no-baseline",
|
|
38
|
+
reason: "no-commit-before"
|
|
39
|
+
};
|
|
40
|
+
const args = [
|
|
41
|
+
"diff",
|
|
42
|
+
"--no-color",
|
|
43
|
+
"--no-ext-diff",
|
|
44
|
+
baseline.sha,
|
|
45
|
+
"--",
|
|
46
|
+
name
|
|
47
|
+
];
|
|
48
|
+
const patch = yield* spawner.string(ChildProcess.setCwd(ChildProcess.make("git", args), cwd)).pipe(Effect.mapError((error) => new GitCommandError({
|
|
49
|
+
kind: "failed",
|
|
50
|
+
args,
|
|
51
|
+
cwd,
|
|
52
|
+
stderr: "",
|
|
53
|
+
detail: error.message
|
|
54
|
+
})));
|
|
55
|
+
return {
|
|
56
|
+
_tag: "diff",
|
|
57
|
+
baseline: baseline.sha.slice(0, 7),
|
|
58
|
+
patch
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { changeSince };
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { hasMergeKey, parseTopLevelMapping } from "./locate.js";
|
|
2
|
+
import { Integrity } from "@okfit/core";
|
|
3
|
+
import { Effect, Option, Schema } from "effect";
|
|
4
|
+
import { MarkdownEdit } from "@effected/markdown";
|
|
5
|
+
import { YamlAlias, YamlMap, YamlScalar, YamlSeq } from "@effected/yaml";
|
|
6
|
+
|
|
7
|
+
//#region src/verify/integrity.ts
|
|
8
|
+
const decodeIntegrity = Schema.decodeUnknownOption(Integrity);
|
|
9
|
+
/**
|
|
10
|
+
* The plain value of `node` with every plain scalar kept as its source text:
|
|
11
|
+
* a digest such as `0000000000000000` or `1234567890123456` is a YAML number
|
|
12
|
+
* to `toValue()`, but the `Digest` schema wants the string okfit wrote.
|
|
13
|
+
*/
|
|
14
|
+
const rawValue = (node, text) => {
|
|
15
|
+
if (node instanceof YamlMap) {
|
|
16
|
+
const out = {};
|
|
17
|
+
for (const item of node.items) if (item.key instanceof YamlScalar) Object.defineProperty(out, String(item.key.value), {
|
|
18
|
+
value: rawValue(item.value, text),
|
|
19
|
+
writable: true,
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true
|
|
22
|
+
});
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
if (node instanceof YamlScalar && node.style === "plain" && node.value !== null) return text.slice(node.offset, node.offset + node.length);
|
|
26
|
+
return node === null ? null : node.toValue();
|
|
27
|
+
};
|
|
28
|
+
const isKey = (name) => (item) => item.key instanceof YamlScalar && item.key.value === name;
|
|
29
|
+
/** Start of the line holding `offset`, to just past the next terminator at or after `tail`. */
|
|
30
|
+
const lineSpan = (value, keyOffset, tail) => {
|
|
31
|
+
const start = value.lastIndexOf("\n", keyOffset - 1) + 1;
|
|
32
|
+
if (value.slice(start, keyOffset).trim() !== "") return void 0;
|
|
33
|
+
const end = tail > 0 && value[tail - 1] === "\n" ? tail : (() => {
|
|
34
|
+
const nl = value.indexOf("\n", tail);
|
|
35
|
+
return nl === -1 ? value.length : nl + 1;
|
|
36
|
+
})();
|
|
37
|
+
return start >= end ? void 0 : {
|
|
38
|
+
start,
|
|
39
|
+
end
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Locate the top-level `integrity` key. See {@link IntegrityLocated}.
|
|
44
|
+
*
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
const locateIntegrity = Effect.fn("okfit/verify/locateIntegrity")(function* (source) {
|
|
48
|
+
const parsed = yield* parseTopLevelMapping(source);
|
|
49
|
+
if ("_tag" in parsed) return parsed;
|
|
50
|
+
const { value, valueStart, contents } = parsed;
|
|
51
|
+
if (contents.style === "flow") return {
|
|
52
|
+
_tag: "unsupported",
|
|
53
|
+
shape: "flow-document"
|
|
54
|
+
};
|
|
55
|
+
const pair = contents.items.find(isKey("integrity"));
|
|
56
|
+
if (pair === void 0) return {
|
|
57
|
+
_tag: "absent",
|
|
58
|
+
insertAt: valueStart + value.length
|
|
59
|
+
};
|
|
60
|
+
const node = pair.value;
|
|
61
|
+
if (!(node === null || node instanceof YamlScalar && node.style === "plain" && node.value === null && node.tag === void 0 && node.anchor === void 0)) {
|
|
62
|
+
if (node instanceof YamlAlias) return {
|
|
63
|
+
_tag: "unsupported",
|
|
64
|
+
shape: "alias"
|
|
65
|
+
};
|
|
66
|
+
if (hasMergeKey(node)) return {
|
|
67
|
+
_tag: "unsupported",
|
|
68
|
+
shape: "merge-key"
|
|
69
|
+
};
|
|
70
|
+
if (node instanceof YamlScalar) return {
|
|
71
|
+
_tag: "unsupported",
|
|
72
|
+
shape: "scalar"
|
|
73
|
+
};
|
|
74
|
+
if (node instanceof YamlSeq) return {
|
|
75
|
+
_tag: "unsupported",
|
|
76
|
+
shape: "sequence"
|
|
77
|
+
};
|
|
78
|
+
if (!(node instanceof YamlMap)) return {
|
|
79
|
+
_tag: "unsupported",
|
|
80
|
+
shape: "not-a-mapping"
|
|
81
|
+
};
|
|
82
|
+
if (node.style === "flow") return {
|
|
83
|
+
_tag: "unsupported",
|
|
84
|
+
shape: "flow-mapping"
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const keyEnd = pair.key.offset + pair.key.length;
|
|
88
|
+
let tail = node === null ? keyEnd : Math.max(keyEnd, node.offset + node.length);
|
|
89
|
+
if (node instanceof YamlMap) {
|
|
90
|
+
const last = node.items[node.items.length - 1];
|
|
91
|
+
if (last !== void 0) {
|
|
92
|
+
const lastEnd = last.value !== null ? last.value.offset + last.value.length : last.key.offset + last.key.length;
|
|
93
|
+
tail = Math.max(keyEnd, lastEnd);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const span = lineSpan(value, pair.key.offset, tail);
|
|
97
|
+
if (span === void 0) return {
|
|
98
|
+
_tag: "unsupported",
|
|
99
|
+
shape: "not-a-mapping"
|
|
100
|
+
};
|
|
101
|
+
const slots = [];
|
|
102
|
+
let indent = " ";
|
|
103
|
+
if (node instanceof YamlMap) for (const [index, item] of node.items.entries()) {
|
|
104
|
+
if (!(item.key instanceof YamlScalar)) return {
|
|
105
|
+
_tag: "unsupported",
|
|
106
|
+
shape: "integrity-layout"
|
|
107
|
+
};
|
|
108
|
+
let itemTail = item.value !== null ? item.value.offset + item.value.length : item.key.offset + item.key.length;
|
|
109
|
+
while (itemTail > item.key.offset && (value[itemTail - 1] === " " || value[itemTail - 1] === " ")) itemTail--;
|
|
110
|
+
const itemSpan = lineSpan(value, item.key.offset, itemTail);
|
|
111
|
+
const previous = slots.at(-1);
|
|
112
|
+
if (itemSpan === void 0 || previous !== void 0 && valueStart + itemSpan.start < previous.end) return {
|
|
113
|
+
_tag: "unsupported",
|
|
114
|
+
shape: "integrity-layout"
|
|
115
|
+
};
|
|
116
|
+
const column = value.slice(itemSpan.start, item.key.offset);
|
|
117
|
+
if (index === 0) indent = column;
|
|
118
|
+
else if (column !== indent) return {
|
|
119
|
+
_tag: "unsupported",
|
|
120
|
+
shape: "integrity-layout"
|
|
121
|
+
};
|
|
122
|
+
slots.push({
|
|
123
|
+
key: String(item.key.value),
|
|
124
|
+
start: valueStart + itemSpan.start,
|
|
125
|
+
end: valueStart + itemSpan.end
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const raw = node instanceof YamlMap ? rawValue(node, value) : void 0;
|
|
129
|
+
const current = raw === void 0 ? void 0 : Option.getOrUndefined(decodeIntegrity(raw));
|
|
130
|
+
const verifiedRaw = raw?.verified;
|
|
131
|
+
const boundByRaw = verifiedRaw !== null && typeof verifiedRaw === "object" ? verifiedRaw.by : void 0;
|
|
132
|
+
const bareKey = node === null || node instanceof YamlScalar && node.length === 0;
|
|
133
|
+
return {
|
|
134
|
+
_tag: "replace",
|
|
135
|
+
start: valueStart + span.start,
|
|
136
|
+
end: valueStart + span.end,
|
|
137
|
+
current,
|
|
138
|
+
slots,
|
|
139
|
+
indent,
|
|
140
|
+
boundBy: typeof boundByRaw === "string" ? boundByRaw : void 0,
|
|
141
|
+
bareKey
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
/** What YAML 1.2 Core resolves to a number: all digits, or digits `e` digits. */
|
|
145
|
+
const NUMERIC_DIGEST = /^[0-9]+(?:e[0-9]+)?$/u;
|
|
146
|
+
/**
|
|
147
|
+
* A digest as okfit writes it: single-quoted exactly when YAML 1.2 Core would
|
|
148
|
+
* resolve the plain spelling to a number (all digits, or digits `e` digits),
|
|
149
|
+
* so a reader always gets the string back; plain otherwise.
|
|
150
|
+
*
|
|
151
|
+
* @internal
|
|
152
|
+
*/
|
|
153
|
+
const formatDigest = (digest) => NUMERIC_DIGEST.test(digest) ? `'${digest}'` : digest;
|
|
154
|
+
/**
|
|
155
|
+
* The canonical block YAML for `fields`, fixed key order, terminator after
|
|
156
|
+
* every line; `""` when both slots are absent. Never a YAML serialiser.
|
|
157
|
+
*
|
|
158
|
+
* @internal
|
|
159
|
+
*/
|
|
160
|
+
const renderIntegrity = (fields, newline) => {
|
|
161
|
+
const lines = [];
|
|
162
|
+
if (fields.generated !== void 0) lines.push(" generated:", ` body: ${formatDigest(fields.generated.body)}`);
|
|
163
|
+
if (fields.verified !== void 0) lines.push(" verified:", ` by: ${fields.verified.by}`, ` at: ${fields.verified.at}`, ` content: ${formatDigest(fields.verified.content)}`);
|
|
164
|
+
return lines.length === 0 ? "" : ["integrity:", ...lines].map((line) => `${line}${newline}`).join("");
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* The one edit that writes `fields` as the whole `integrity` block (insert
|
|
168
|
+
* when absent, replace otherwise); deletes the block when `fields` is empty.
|
|
169
|
+
*
|
|
170
|
+
* @internal
|
|
171
|
+
*/
|
|
172
|
+
const spliceIntegrity = (target, fields, newline) => MarkdownEdit.make({
|
|
173
|
+
offset: target._tag === "absent" ? target.insertAt : target.start,
|
|
174
|
+
length: target._tag === "absent" ? 0 : target.end - target.start,
|
|
175
|
+
content: renderIntegrity(fields, newline)
|
|
176
|
+
});
|
|
177
|
+
/**
|
|
178
|
+
* Rewrite only one slot of the located `integrity` block, driven by the parsed
|
|
179
|
+
* item spans: every other byte (the key line and its comment, the other slot,
|
|
180
|
+
* unknown keys, comments between items, quoting) is copied as it was. A
|
|
181
|
+
* present slot (however its key is quoted) is replaced in place; a missing one
|
|
182
|
+
* is added first (`generated`) or last (`verified`), matching
|
|
183
|
+
* {@link renderIntegrity}'s order. Only an absent block, or one with no items
|
|
184
|
+
* and value text (`integrity: ~`), is written whole -- there is nothing in it
|
|
185
|
+
* to lose. No edit here is a zero-length insert at the end of the block, so it
|
|
186
|
+
* cannot collide with a key appended at the end of the frontmatter.
|
|
187
|
+
*/
|
|
188
|
+
const spliceSlot = (target, text, slot, slotLines, whole, newline) => {
|
|
189
|
+
if (target._tag === "absent") return spliceIntegrity(target, whole, newline);
|
|
190
|
+
const terminated = (start, end) => {
|
|
191
|
+
const lines = text.slice(start, end);
|
|
192
|
+
return lines.endsWith("\n") ? lines : `${lines}${newline}`;
|
|
193
|
+
};
|
|
194
|
+
if (target.slots.length === 0) {
|
|
195
|
+
if (!target.bareKey) return spliceIntegrity(target, whole, newline);
|
|
196
|
+
return MarkdownEdit.make({
|
|
197
|
+
offset: target.start,
|
|
198
|
+
length: target.end - target.start,
|
|
199
|
+
content: terminated(target.start, target.end) + slotLines(target.indent, newline)
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
const lines = slotLines(target.indent, newline);
|
|
203
|
+
const found = target.slots.find((entry) => entry.key === slot);
|
|
204
|
+
if (found !== void 0) return MarkdownEdit.make({
|
|
205
|
+
offset: found.start,
|
|
206
|
+
length: found.end - found.start,
|
|
207
|
+
content: lines
|
|
208
|
+
});
|
|
209
|
+
if (slot === "generated") {
|
|
210
|
+
const first = target.slots[0];
|
|
211
|
+
return MarkdownEdit.make({
|
|
212
|
+
offset: first.start,
|
|
213
|
+
length: 0,
|
|
214
|
+
content: lines
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
const last = target.slots.at(-1);
|
|
218
|
+
return MarkdownEdit.make({
|
|
219
|
+
offset: last.start,
|
|
220
|
+
length: last.end - last.start,
|
|
221
|
+
content: terminated(last.start, last.end) + lines
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* The one edit that sets `integrity.generated.body` and leaves every other byte
|
|
226
|
+
* of the block (the `verified` slot, unknown keys, comments, quoting) as it was.
|
|
227
|
+
* Inserts the whole block when absent or null; otherwise rewrites only the
|
|
228
|
+
* `generated` slot's lines, or adds that slot first when missing. `text` is the
|
|
229
|
+
* same BOM-stripped source the target was located in.
|
|
230
|
+
*
|
|
231
|
+
* @internal
|
|
232
|
+
*/
|
|
233
|
+
const spliceIntegrityGenerated = (target, text, body, newline) => spliceSlot(target, text, "generated", (indent, nl) => `${indent}generated:${nl}${indent} body: ${formatDigest(body)}${nl}`, { generated: { body } }, newline);
|
|
234
|
+
/**
|
|
235
|
+
* The one edit that sets the `integrity.verified` binding and leaves every other
|
|
236
|
+
* byte of the block (the `generated` slot, unknown keys, comments, quoting) as it
|
|
237
|
+
* was; the mirror of {@link spliceIntegrityGenerated}. Inserts the whole block
|
|
238
|
+
* when absent or null; otherwise rewrites only the `verified` slot's lines, or
|
|
239
|
+
* appends that slot when missing. `text` is the same BOM-stripped source the
|
|
240
|
+
* target was located in.
|
|
241
|
+
*
|
|
242
|
+
* @internal
|
|
243
|
+
*/
|
|
244
|
+
const spliceIntegrityVerified = (target, text, binding, newline) => spliceSlot(target, text, "verified", (indent, nl) => `${indent}verified:${nl}${indent} by: ${binding.by}${nl}${indent} at: ${binding.at}${nl}${indent} content: ${formatDigest(binding.content)}${nl}`, { verified: binding }, newline);
|
|
245
|
+
/**
|
|
246
|
+
* The one edit that removes the `integrity.verified` slot and nothing else:
|
|
247
|
+
* only that parsed item's lines go (however its key is quoted); every other
|
|
248
|
+
* byte of the block (the `generated` slot, unknown keys, comments, quoting)
|
|
249
|
+
* stays. When it is the block's only item, the whole block goes instead.
|
|
250
|
+
* `none` when the block is absent; fails closed (`unsupported`) when the
|
|
251
|
+
* parsed block has no `verified` item, never falling back to a whole-block
|
|
252
|
+
* rewrite. `text` is unused (the spans come from {@link locateIntegrity}) and
|
|
253
|
+
* kept for call-site symmetry with the splice functions.
|
|
254
|
+
*
|
|
255
|
+
* @internal
|
|
256
|
+
*/
|
|
257
|
+
const removeIntegrityVerified = (target, _text) => {
|
|
258
|
+
if (target._tag === "absent") return { _tag: "none" };
|
|
259
|
+
const found = target.slots.find((entry) => entry.key === "verified");
|
|
260
|
+
if (found === void 0) return {
|
|
261
|
+
_tag: "unsupported",
|
|
262
|
+
shape: "integrity-layout"
|
|
263
|
+
};
|
|
264
|
+
const [offset, end] = target.slots.length === 1 ? [target.start, target.end] : [found.start, found.end];
|
|
265
|
+
return {
|
|
266
|
+
_tag: "edit",
|
|
267
|
+
edit: MarkdownEdit.make({
|
|
268
|
+
offset,
|
|
269
|
+
length: end - offset,
|
|
270
|
+
content: ""
|
|
271
|
+
})
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Locate the legacy `generated.body_sha256` line (whole line plus terminator)
|
|
276
|
+
* so it can be removed. `none` for every other shape.
|
|
277
|
+
*
|
|
278
|
+
* @internal
|
|
279
|
+
*/
|
|
280
|
+
const locateLegacyBodySha256 = Effect.fn("okfit/verify/locateLegacyBodySha256")(function* (source) {
|
|
281
|
+
const none = { _tag: "none" };
|
|
282
|
+
const parsed = yield* parseTopLevelMapping(source);
|
|
283
|
+
if ("_tag" in parsed) return none;
|
|
284
|
+
const { value, valueStart, contents } = parsed;
|
|
285
|
+
if (contents.style === "flow") return none;
|
|
286
|
+
const generated = contents.items.find(isKey("generated"))?.value;
|
|
287
|
+
if (!(generated instanceof YamlMap) || generated.style === "flow") return none;
|
|
288
|
+
const pair = generated.items.find(isKey("body_sha256"));
|
|
289
|
+
const scalar = pair?.value;
|
|
290
|
+
if (pair === void 0 || !(scalar instanceof YamlScalar)) return none;
|
|
291
|
+
if (scalar.style === "block-literal" || scalar.style === "block-folded") return none;
|
|
292
|
+
const span = lineSpan(value, pair.key.offset, scalar.offset + scalar.length);
|
|
293
|
+
if (span === void 0) return none;
|
|
294
|
+
return {
|
|
295
|
+
_tag: "line",
|
|
296
|
+
start: valueStart + span.start,
|
|
297
|
+
end: valueStart + span.end,
|
|
298
|
+
value: String(scalar.value)
|
|
299
|
+
};
|
|
300
|
+
});
|
|
301
|
+
/** Delete exactly the located line and its terminator. @internal */
|
|
302
|
+
const removeLine = (located) => MarkdownEdit.make({
|
|
303
|
+
offset: located.start,
|
|
304
|
+
length: located.end - located.start,
|
|
305
|
+
content: ""
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
//#endregion
|
|
309
|
+
export { formatDigest, locateIntegrity, locateLegacyBodySha256, removeIntegrityVerified, removeLine, renderIntegrity, spliceIntegrity, spliceIntegrityGenerated, spliceIntegrityVerified };
|
package/verify/locate.js
CHANGED
|
@@ -48,6 +48,8 @@ const indentAt = (value, offset) => {
|
|
|
48
48
|
* carries a `<<` key. That is every level a legal `Verification.List` can
|
|
49
49
|
* reach; anything deeper already fails to decode as a `Verification`
|
|
50
50
|
* (contract §11 item 3).
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
51
53
|
*/
|
|
52
54
|
const hasMergeKey = (node) => {
|
|
53
55
|
if (node instanceof YamlMap) return node.items.some((pair) => pair.key instanceof YamlScalar && pair.key.value === "<<");
|
|
@@ -257,15 +259,6 @@ const locateGeneratedField = Effect.fn("okfit/verify/locateGeneratedField")(func
|
|
|
257
259
|
});
|
|
258
260
|
const locateGenerated = (source) => locateGeneratedField(source, "at");
|
|
259
261
|
/**
|
|
260
|
-
* As {@link locateGenerated}, but for the top-level `generated.body_sha256`
|
|
261
|
-
* key (issue #19). Shares every branch with `locateGenerated` -- the two
|
|
262
|
-
* fields are siblings of the same `generated:` block mapping -- parametrized
|
|
263
|
-
* only by which key name is sought.
|
|
264
|
-
*
|
|
265
|
-
* @internal
|
|
266
|
-
*/
|
|
267
|
-
const locateGeneratedBodySha256 = (source) => locateGeneratedField(source, "body_sha256");
|
|
268
|
-
/**
|
|
269
262
|
* Locate a top-level scalar key (`key`) inside a concept's frontmatter,
|
|
270
263
|
* mirroring `locateGeneratedField`'s parsing prologue. Unlike `generated`'s
|
|
271
264
|
* fields, this key is a direct top-level sibling, not nested inside a block
|
|
@@ -382,4 +375,4 @@ const locateVerifiedAt = Effect.fn("okfit/verify/locateVerifiedAt")(function* (s
|
|
|
382
375
|
});
|
|
383
376
|
|
|
384
377
|
//#endregion
|
|
385
|
-
export { detectNewline, documentNewline, indentAt, locate, locateGenerated, locateGeneratedBlock,
|
|
378
|
+
export { detectNewline, documentNewline, hasMergeKey, indentAt, locate, locateGenerated, locateGeneratedBlock, locateTopLevelScalar, locateVerifiedAt, parseTopLevelMapping, stripBom };
|