@okfit/engine 0.14.1 → 0.15.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/edits/FrontmatterEdits.js +66 -18
- package/errors.js +24 -3
- package/index.d.ts +400 -49
- package/index.js +8 -6
- package/package.json +19 -18
- package/query/ConceptQuery.js +15 -4
- package/render/context.js +20 -3
- 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/publication.js +3 -3
- package/sync/run.js +13 -4
- 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 +157 -21
- package/verify/select.js +30 -11
- package/verify/splice.js +20 -34
- package/version.js +1 -1
package/sync/run.js
CHANGED
|
@@ -22,6 +22,8 @@ const SkipReason = Schema.Literals([
|
|
|
22
22
|
"unborn",
|
|
23
23
|
"generated-missing",
|
|
24
24
|
"generated-unsupported",
|
|
25
|
+
"integrity-unsupported",
|
|
26
|
+
"renders-unsupported",
|
|
25
27
|
"log-unparseable"
|
|
26
28
|
]);
|
|
27
29
|
/** A selected-`false` placeholder for a mode `--only` excluded from (contract §5). */
|
|
@@ -29,6 +31,7 @@ const UNSELECTED = {
|
|
|
29
31
|
selected: false,
|
|
30
32
|
written: [],
|
|
31
33
|
unchanged: [],
|
|
34
|
+
migrated: [],
|
|
32
35
|
skipped: []
|
|
33
36
|
};
|
|
34
37
|
/**
|
|
@@ -81,7 +84,7 @@ const planSync = Effect.fn("okfit/sync/planSync")(function* (options) {
|
|
|
81
84
|
for (const [id, concept] of bundle.concepts) {
|
|
82
85
|
const generated = concept.frontmatter.generated;
|
|
83
86
|
const digest = yield* Derivation.bodyDigest(concept.document.source);
|
|
84
|
-
const stampedAt =
|
|
87
|
+
const stampedAt = Derivation.recordedBody(concept) === digest ? generated?.at : void 0;
|
|
85
88
|
if (!(stampedAt === void 0 || options.modes.has("log") && wantsLogEntry(window, DateTime.formatIsoDate(stampedAt), Derive.title(concept)))) continue;
|
|
86
89
|
const derived = yield* Derivation.generatedAt({ file: `${bundle.root}/${concept.path}` });
|
|
87
90
|
walked.set(id, derived);
|
|
@@ -98,12 +101,18 @@ const planSync = Effect.fn("okfit/sync/planSync")(function* (options) {
|
|
|
98
101
|
...scope === void 0 ? {} : { scope }
|
|
99
102
|
})
|
|
100
103
|
} : UNSELECTED;
|
|
101
|
-
const index = options.modes.has("index") ?
|
|
102
|
-
|
|
104
|
+
const index = options.modes.has("index") ? {
|
|
105
|
+
...yield* syncIndex(bundle, true, pending),
|
|
106
|
+
migrated: []
|
|
107
|
+
} : UNSELECTED;
|
|
108
|
+
const log = options.modes.has("log") ? {
|
|
109
|
+
...yield* syncLog(bundle, walked, true, options.logSince, pending),
|
|
110
|
+
migrated: []
|
|
111
|
+
} : UNSELECTED;
|
|
103
112
|
let restage;
|
|
104
113
|
if (options.staged !== void 0 && repoRoot !== void 0) {
|
|
105
114
|
const path = yield* Path.Path;
|
|
106
|
-
const writtenIds = new Set(generated.written);
|
|
115
|
+
const writtenIds = /* @__PURE__ */ new Set([...generated.written, ...generated.migrated]);
|
|
107
116
|
const files = [...realPaths === void 0 ? [] : Array.from(realPaths.entries()).filter(([id]) => writtenIds.has(id)).map(([, real]) => real), ...index.written.map((relative) => path.join(bundle.root, relative))];
|
|
108
117
|
if (files.length > 0) restage = {
|
|
109
118
|
repoRoot,
|
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 };
|
package/verify/revoke.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { hasMergeKey, parseTopLevelMapping } from "./locate.js";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { YamlAlias, YamlMap, YamlScalar, YamlSeq } from "@effected/yaml";
|
|
4
|
+
|
|
5
|
+
//#region src/verify/revoke.ts
|
|
6
|
+
const byActor = (actor) => (map) => map.items.some((entry) => entry.key instanceof YamlScalar && entry.key.value === "by" && entry.value instanceof YamlScalar && entry.value.value === actor);
|
|
7
|
+
/**
|
|
8
|
+
* Locate every top-level `verified` entry whose `by` is `actor`. See
|
|
9
|
+
* {@link VerifiedEntriesLocated}. Fails only when the frontmatter YAML is
|
|
10
|
+
* fatally unparseable.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
const locateVerifiedEntries = Effect.fn("okfit/verify/locateVerifiedEntries")(function* (source, actor) {
|
|
15
|
+
const parsed = yield* parseTopLevelMapping(source);
|
|
16
|
+
if ("_tag" in parsed) return parsed;
|
|
17
|
+
const { value, valueStart, contents } = parsed;
|
|
18
|
+
if (contents.style === "flow") return {
|
|
19
|
+
_tag: "unsupported",
|
|
20
|
+
shape: "flow-document"
|
|
21
|
+
};
|
|
22
|
+
const pair = contents.items.find((item) => item.key instanceof YamlScalar && item.key.value === "verified");
|
|
23
|
+
if (pair === void 0) return { _tag: "none" };
|
|
24
|
+
const node = pair.value;
|
|
25
|
+
if (node === null) return {
|
|
26
|
+
_tag: "unsupported",
|
|
27
|
+
shape: "empty"
|
|
28
|
+
};
|
|
29
|
+
if (node instanceof YamlAlias) return {
|
|
30
|
+
_tag: "unsupported",
|
|
31
|
+
shape: "alias"
|
|
32
|
+
};
|
|
33
|
+
if (hasMergeKey(node)) return {
|
|
34
|
+
_tag: "unsupported",
|
|
35
|
+
shape: "merge-key"
|
|
36
|
+
};
|
|
37
|
+
if (node instanceof YamlScalar) return {
|
|
38
|
+
_tag: "unsupported",
|
|
39
|
+
shape: "scalar"
|
|
40
|
+
};
|
|
41
|
+
const lineStart = (offset) => value.lastIndexOf("\n", offset - 1) + 1;
|
|
42
|
+
/** Just past the terminator of the line holding the node end `end`. */
|
|
43
|
+
const lineEnd = (end) => {
|
|
44
|
+
const before = value.lastIndexOf("\n", end - 1);
|
|
45
|
+
if (before !== -1 && /^[ \t]*$/u.test(value.slice(before + 1, end))) return before + 1;
|
|
46
|
+
const nl = value.indexOf("\n", end);
|
|
47
|
+
return nl === -1 ? value.length : nl + 1;
|
|
48
|
+
};
|
|
49
|
+
const keyStart = lineStart(pair.key.offset);
|
|
50
|
+
if (value.slice(keyStart, pair.key.offset).trim() !== "") return {
|
|
51
|
+
_tag: "unsupported",
|
|
52
|
+
shape: "not-a-mapping"
|
|
53
|
+
};
|
|
54
|
+
const keyLine = {
|
|
55
|
+
start: valueStart + keyStart,
|
|
56
|
+
end: valueStart + lineEnd(pair.key.offset + pair.key.length)
|
|
57
|
+
};
|
|
58
|
+
const whole = (tail) => ({
|
|
59
|
+
_tag: "entries",
|
|
60
|
+
removals: [{
|
|
61
|
+
start: keyLine.start,
|
|
62
|
+
end: valueStart + lineEnd(tail)
|
|
63
|
+
}],
|
|
64
|
+
removesAll: true,
|
|
65
|
+
keyLine
|
|
66
|
+
});
|
|
67
|
+
if (node instanceof YamlMap) return byActor(actor)(node) ? whole(node.offset + node.length) : { _tag: "none" };
|
|
68
|
+
if (!(node instanceof YamlSeq)) return {
|
|
69
|
+
_tag: "unsupported",
|
|
70
|
+
shape: "not-a-mapping"
|
|
71
|
+
};
|
|
72
|
+
if (node.style === "flow") return {
|
|
73
|
+
_tag: "unsupported",
|
|
74
|
+
shape: "flow-sequence"
|
|
75
|
+
};
|
|
76
|
+
const matching = node.items.filter((item) => item instanceof YamlMap && byActor(actor)(item));
|
|
77
|
+
if (matching.length === 0) return { _tag: "none" };
|
|
78
|
+
const last = node.items[node.items.length - 1];
|
|
79
|
+
if (matching.length === node.items.length && last !== void 0) return whole(last.offset + last.length);
|
|
80
|
+
const removals = [];
|
|
81
|
+
for (const item of matching) {
|
|
82
|
+
const start = lineStart(item.offset);
|
|
83
|
+
if (!/^ *- *$/u.test(value.slice(start, item.offset))) return {
|
|
84
|
+
_tag: "unsupported",
|
|
85
|
+
shape: "inline-item"
|
|
86
|
+
};
|
|
87
|
+
removals.push({
|
|
88
|
+
start: valueStart + start,
|
|
89
|
+
end: valueStart + lineEnd(item.offset + item.length)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
_tag: "entries",
|
|
94
|
+
removals,
|
|
95
|
+
removesAll: false,
|
|
96
|
+
keyLine
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
//#endregion
|
|
101
|
+
export { locateVerifiedEntries };
|