@intellectual-frontiers/ergonex 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/desired.d.ts +27 -0
- package/dist/diff.d.ts +12 -0
- package/dist/doctor.d.ts +79 -0
- package/dist/findings.d.ts +85 -0
- package/dist/layout.d.ts +83 -0
- package/dist/manifest.d.ts +84 -0
- package/dist/mod.d.ts +29 -0
- package/dist/mod.js +4219 -0
- package/dist/modules/external.d.ts +31 -0
- package/dist/modules/mod.d.ts +10 -0
- package/dist/modules/name.d.ts +32 -0
- package/dist/modules/registry.d.ts +51 -0
- package/dist/modules/types.d.ts +178 -0
- package/dist/places.d.ts +35 -0
- package/dist/plan.d.ts +176 -0
- package/dist/platform/k-args.d.ts +28 -0
- package/dist/policy.d.ts +39 -0
- package/dist/portable.d.ts +111 -0
- package/dist/render.d.ts +95 -0
- package/dist/setup/evidence.d.ts +58 -0
- package/dist/shell/block.d.ts +45 -0
- package/dist/shell/mod.d.ts +8 -0
- package/dist/shell/snippet.d.ts +43 -0
- package/dist/state-record.d.ts +59 -0
- package/dist/state.d.ts +64 -0
- package/dist/templates/data.d.ts +25 -0
- package/dist/templates/hooks.d.ts +13 -0
- package/dist/templates/mod.d.ts +16 -0
- package/dist/tools/catalog.d.ts +70 -0
- package/dist/types.d.ts +51 -0
- package/dist/vendor/ts/githost/capabilities.d.ts +18 -0
- package/dist/vendor/ts/githost/git.d.ts +16 -0
- package/dist/vendor/ts/githost/github.d.ts +15 -0
- package/dist/vendor/ts/githost/gitlab.d.ts +15 -0
- package/dist/vendor/ts/githost/mod.d.ts +29 -0
- package/dist/vendor/ts/githost/remote.d.ts +59 -0
- package/dist/vendor/ts/githost/types.d.ts +101 -0
- package/dist/vendor/ts/kernel/archive.d.ts +147 -0
- package/dist/vendor/ts/kernel/args.d.ts +240 -0
- package/dist/vendor/ts/kernel/assess.d.ts +247 -0
- package/dist/vendor/ts/kernel/attest.d.ts +164 -0
- package/dist/vendor/ts/kernel/base64.d.ts +24 -0
- package/dist/vendor/ts/kernel/cbor.d.ts +39 -0
- package/dist/vendor/ts/kernel/cms.d.ts +106 -0
- package/dist/vendor/ts/kernel/commands.d.ts +103 -0
- package/dist/vendor/ts/kernel/cose.d.ts +34 -0
- package/dist/vendor/ts/kernel/crypt.d.ts +30 -0
- package/dist/vendor/ts/kernel/csv.d.ts +38 -0
- package/dist/vendor/ts/kernel/der.d.ts +67 -0
- package/dist/vendor/ts/kernel/documentation.d.ts +118 -0
- package/dist/vendor/ts/kernel/dsse.d.ts +137 -0
- package/dist/vendor/ts/kernel/evidence.d.ts +212 -0
- package/dist/vendor/ts/kernel/glob.d.ts +64 -0
- package/dist/vendor/ts/kernel/grc.d.ts +356 -0
- package/dist/vendor/ts/kernel/http.d.ts +46 -0
- package/dist/vendor/ts/kernel/json5.d.ts +46 -0
- package/dist/vendor/ts/kernel/layers.d.ts +35 -0
- package/dist/vendor/ts/kernel/md5.d.ts +19 -0
- package/dist/vendor/ts/kernel/media-types.d.ts +28 -0
- package/dist/vendor/ts/kernel/mod.d.ts +95 -0
- package/dist/vendor/ts/kernel/ocsf-types.auto.d.ts +4374 -0
- package/dist/vendor/ts/kernel/ocsf.d.ts +160 -0
- package/dist/vendor/ts/kernel/oscal.d.ts +356 -0
- package/dist/vendor/ts/kernel/otel.d.ts +365 -0
- package/dist/vendor/ts/kernel/otlp.d.ts +83 -0
- package/dist/vendor/ts/kernel/palette.d.ts +44 -0
- package/dist/vendor/ts/kernel/png.d.ts +124 -0
- package/dist/vendor/ts/kernel/rdf-schema.d.ts +190 -0
- package/dist/vendor/ts/kernel/rdf-term.d.ts +162 -0
- package/dist/vendor/ts/kernel/rdf-vocabularies.d.ts +148 -0
- package/dist/vendor/ts/kernel/rdf-write.d.ts +25 -0
- package/dist/vendor/ts/kernel/rdf.d.ts +35 -0
- package/dist/vendor/ts/kernel/render.d.ts +111 -0
- package/dist/vendor/ts/kernel/schedule.d.ts +207 -0
- package/dist/vendor/ts/kernel/semver.d.ts +94 -0
- package/dist/vendor/ts/kernel/sha256.d.ts +29 -0
- package/dist/vendor/ts/kernel/signals.d.ts +371 -0
- package/dist/vendor/ts/kernel/sigv4.d.ts +67 -0
- package/dist/vendor/ts/kernel/test-record.d.ts +222 -0
- package/dist/vendor/ts/kernel/test-report.d.ts +294 -0
- package/dist/vendor/ts/kernel/toml.d.ts +51 -0
- package/dist/vendor/ts/kernel/trace.d.ts +169 -0
- package/dist/vendor/ts/kernel/ts-literal.d.ts +62 -0
- package/dist/vendor/ts/kernel/uuid.d.ts +53 -0
- package/dist/vendor/ts/kernel/vt.d.ts +78 -0
- package/dist/vendor/ts/kernel/webauthn.d.ts +212 -0
- package/dist/vendor/ts/kernel/width.d.ts +63 -0
- package/dist/vendor/ts/kernel/xml.d.ts +73 -0
- package/dist/vendor/ts/kernel/yaml.d.ts +44 -0
- package/dist/vendor/ts/runtime/http-client.d.ts +149 -0
- package/ergonex.mjs +140 -140
- package/package.json +17 -5
package/dist/mod.js
ADDED
|
@@ -0,0 +1,4219 @@
|
|
|
1
|
+
// src/diff.ts
|
|
2
|
+
var CONTEXT = 3;
|
|
3
|
+
function linesOf(text) {
|
|
4
|
+
if (text === "")
|
|
5
|
+
return [];
|
|
6
|
+
const lines = text.split(`
|
|
7
|
+
`);
|
|
8
|
+
if (lines.at(-1) === "")
|
|
9
|
+
lines.pop();
|
|
10
|
+
return lines;
|
|
11
|
+
}
|
|
12
|
+
function script(a, b) {
|
|
13
|
+
const n = a.length;
|
|
14
|
+
const m = b.length;
|
|
15
|
+
const table = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
16
|
+
for (let i = n - 1;i >= 0; i--) {
|
|
17
|
+
for (let j = m - 1;j >= 0; j--) {
|
|
18
|
+
const row = table[i];
|
|
19
|
+
const below = table[i + 1];
|
|
20
|
+
if (row === undefined || below === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
row[j] = a[i] === b[j] ? (below[j + 1] ?? 0) + 1 : Math.max(below[j] ?? 0, row[j + 1] ?? 0);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const ops = [];
|
|
26
|
+
let i = 0;
|
|
27
|
+
let j = 0;
|
|
28
|
+
while (i < n && j < m) {
|
|
29
|
+
if (a[i] === b[j]) {
|
|
30
|
+
ops.push({ kind: " ", line: a[i] ?? "" });
|
|
31
|
+
i++;
|
|
32
|
+
j++;
|
|
33
|
+
} else if ((table[i + 1]?.[j] ?? 0) >= (table[i]?.[j + 1] ?? 0)) {
|
|
34
|
+
ops.push({ kind: "-", line: a[i] ?? "" });
|
|
35
|
+
i++;
|
|
36
|
+
} else {
|
|
37
|
+
ops.push({ kind: "+", line: b[j] ?? "" });
|
|
38
|
+
j++;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
while (i < n)
|
|
42
|
+
ops.push({ kind: "-", line: a[i++] ?? "" });
|
|
43
|
+
while (j < m)
|
|
44
|
+
ops.push({ kind: "+", line: b[j++] ?? "" });
|
|
45
|
+
return ops;
|
|
46
|
+
}
|
|
47
|
+
function unifiedDiff(name, before, after) {
|
|
48
|
+
if (before === after)
|
|
49
|
+
return "";
|
|
50
|
+
const ops = script(linesOf(before), linesOf(after));
|
|
51
|
+
const changed = ops.flatMap((o, i) => o.kind === " " ? [] : [i]);
|
|
52
|
+
if (changed.length === 0)
|
|
53
|
+
return "";
|
|
54
|
+
const groups = [];
|
|
55
|
+
for (const i of changed) {
|
|
56
|
+
const last = groups.at(-1);
|
|
57
|
+
if (last !== undefined && i - last.to <= 2 * CONTEXT + 1)
|
|
58
|
+
last.to = i;
|
|
59
|
+
else
|
|
60
|
+
groups.push({ from: i, to: i });
|
|
61
|
+
}
|
|
62
|
+
const out = [`--- a/${name}`, `+++ b/${name}`];
|
|
63
|
+
for (const g of groups) {
|
|
64
|
+
const start = Math.max(0, g.from - CONTEXT);
|
|
65
|
+
const end = Math.min(ops.length - 1, g.to + CONTEXT);
|
|
66
|
+
const slice = ops.slice(start, end + 1);
|
|
67
|
+
const before0 = ops.slice(0, start).filter((o) => o.kind !== "+").length;
|
|
68
|
+
const after0 = ops.slice(0, start).filter((o) => o.kind !== "-").length;
|
|
69
|
+
const oldCount = slice.filter((o) => o.kind !== "+").length;
|
|
70
|
+
const newCount = slice.filter((o) => o.kind !== "-").length;
|
|
71
|
+
out.push(`@@ -${oldCount === 0 ? before0 : before0 + 1},${oldCount} +${newCount === 0 ? after0 : after0 + 1},${newCount} @@`);
|
|
72
|
+
for (const o of slice)
|
|
73
|
+
out.push(`${o.kind}${o.line}`);
|
|
74
|
+
}
|
|
75
|
+
return `${out.join(`
|
|
76
|
+
`)}
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
// ../core/src/absurd.ts
|
|
80
|
+
function absurd(value) {
|
|
81
|
+
throw new Error(`an alternative was not handled: ${JSON.stringify(value)}`);
|
|
82
|
+
}
|
|
83
|
+
// ../core/src/result.ts
|
|
84
|
+
function ok(value) {
|
|
85
|
+
return { kind: "ok", value };
|
|
86
|
+
}
|
|
87
|
+
function err(error) {
|
|
88
|
+
return { kind: "err", error };
|
|
89
|
+
}
|
|
90
|
+
var done = ok(undefined);
|
|
91
|
+
|
|
92
|
+
// ../core/src/decode.ts
|
|
93
|
+
function describe(input) {
|
|
94
|
+
if (input === null)
|
|
95
|
+
return "null";
|
|
96
|
+
if (input === undefined)
|
|
97
|
+
return "nothing";
|
|
98
|
+
if (Array.isArray(input))
|
|
99
|
+
return "an array";
|
|
100
|
+
const t = typeof input;
|
|
101
|
+
if (t === "string")
|
|
102
|
+
return `the string ${JSON.stringify(input).slice(0, 60)}`;
|
|
103
|
+
if (t === "object")
|
|
104
|
+
return "an object";
|
|
105
|
+
if (t === "function")
|
|
106
|
+
return "a function";
|
|
107
|
+
if (typeof input === "symbol")
|
|
108
|
+
return "a symbol";
|
|
109
|
+
if (typeof input === "number" || typeof input === "bigint" || typeof input === "boolean")
|
|
110
|
+
return `${t} ${String(input)}`;
|
|
111
|
+
return t;
|
|
112
|
+
}
|
|
113
|
+
function fail(path, expected, input) {
|
|
114
|
+
return err({ kind: "decode", path, expected, found: describe(input) });
|
|
115
|
+
}
|
|
116
|
+
function formatDecodeError(e) {
|
|
117
|
+
return `${e.path}: expected ${e.expected}, found ${e.found}`;
|
|
118
|
+
}
|
|
119
|
+
var string = (input, path = "$") => typeof input === "string" ? ok(input) : fail(path, "a string", input);
|
|
120
|
+
var number = (input, path = "$") => typeof input === "number" && Number.isFinite(input) ? ok(input) : fail(path, "a finite number", input);
|
|
121
|
+
var integer = (input, path = "$") => typeof input === "number" && Number.isInteger(input) ? ok(input) : fail(path, "an integer", input);
|
|
122
|
+
var boolean = (input, path = "$") => typeof input === "boolean" ? ok(input) : fail(path, "a boolean", input);
|
|
123
|
+
var unknownValue = (input) => ok(input);
|
|
124
|
+
function literal(...values) {
|
|
125
|
+
return (input, path = "$") => typeof input === "string" && values.includes(input) ? ok(input) : fail(path, `one of ${values.join(", ")}`, input);
|
|
126
|
+
}
|
|
127
|
+
function nullable(inner) {
|
|
128
|
+
return (input, path = "$") => input === null ? ok(null) : inner(input, path);
|
|
129
|
+
}
|
|
130
|
+
function array(element) {
|
|
131
|
+
return (input, path = "$") => {
|
|
132
|
+
if (!Array.isArray(input))
|
|
133
|
+
return fail(path, "an array", input);
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const [i, value] of input.entries()) {
|
|
136
|
+
const r = element(value, `${path}[${i}]`);
|
|
137
|
+
if (r.kind === "err")
|
|
138
|
+
return r;
|
|
139
|
+
out.push(r.value);
|
|
140
|
+
}
|
|
141
|
+
return ok(out);
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
var isPlainObject = (input) => typeof input === "object" && input !== null && !Array.isArray(input);
|
|
145
|
+
function record(value) {
|
|
146
|
+
return (input, path = "$") => {
|
|
147
|
+
if (!isPlainObject(input))
|
|
148
|
+
return fail(path, "an object", input);
|
|
149
|
+
const out = {};
|
|
150
|
+
for (const key of Object.keys(input)) {
|
|
151
|
+
const r = value(Reflect.get(input, key), `${path}.${key}`);
|
|
152
|
+
if (r.kind === "err")
|
|
153
|
+
return r;
|
|
154
|
+
out[key] = r.value;
|
|
155
|
+
}
|
|
156
|
+
return ok(out);
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function object(required, optional) {
|
|
160
|
+
return (input, path = "$") => {
|
|
161
|
+
if (!isPlainObject(input))
|
|
162
|
+
return fail(path, "an object", input);
|
|
163
|
+
const out = {};
|
|
164
|
+
for (const [key, decoder] of Object.entries(required)) {
|
|
165
|
+
const r = decoder(Reflect.get(input, key), `${path}.${key}`);
|
|
166
|
+
if (r.kind === "err")
|
|
167
|
+
return r;
|
|
168
|
+
out[key] = r.value;
|
|
169
|
+
}
|
|
170
|
+
for (const [key, decoder] of Object.entries(optional ?? {})) {
|
|
171
|
+
const value = Reflect.get(input, key);
|
|
172
|
+
if (value === undefined)
|
|
173
|
+
continue;
|
|
174
|
+
const r = decoder(value, `${path}.${key}`);
|
|
175
|
+
if (r.kind === "err")
|
|
176
|
+
return r;
|
|
177
|
+
out[key] = r.value;
|
|
178
|
+
}
|
|
179
|
+
return ok(out);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function oneOf(...alternatives) {
|
|
183
|
+
return (input, path = "$") => {
|
|
184
|
+
for (const decoder of alternatives) {
|
|
185
|
+
const r = decoder(input, path);
|
|
186
|
+
if (r.kind === "ok")
|
|
187
|
+
return r;
|
|
188
|
+
}
|
|
189
|
+
return fail(path, `one of ${alternatives.length} alternatives`, input);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function mapDecoder(base, f) {
|
|
193
|
+
return (input, path = "$") => {
|
|
194
|
+
const r = base(input, path);
|
|
195
|
+
return r.kind === "ok" ? ok(f(r.value)) : r;
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function refine(base, name, valid) {
|
|
199
|
+
return (input, path = "$") => {
|
|
200
|
+
const r = base(input, path);
|
|
201
|
+
if (r.kind === "err")
|
|
202
|
+
return r;
|
|
203
|
+
return valid(r.value) ? ok(r.value) : fail(path, `a ${name}`, input);
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function json(text, decoder) {
|
|
207
|
+
let value;
|
|
208
|
+
try {
|
|
209
|
+
value = JSON.parse(text);
|
|
210
|
+
} catch (e) {
|
|
211
|
+
return err({ kind: "decode", path: "$", expected: "JSON", found: e instanceof Error ? e.message : String(e) });
|
|
212
|
+
}
|
|
213
|
+
return decoder(value);
|
|
214
|
+
}
|
|
215
|
+
// ../core/src/text.ts
|
|
216
|
+
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;?]*[A-Za-z]`, "g");
|
|
217
|
+
// ../ts/kernel/archive.ts
|
|
218
|
+
var CRC_TABLE = Array.from({ length: 256 }, (_, i) => {
|
|
219
|
+
let c = i;
|
|
220
|
+
for (let k = 0;k < 8; k++)
|
|
221
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
222
|
+
return c >>> 0;
|
|
223
|
+
});
|
|
224
|
+
function dosDate(year, month, day) {
|
|
225
|
+
return year - 1980 << 9 | month << 5 | day;
|
|
226
|
+
}
|
|
227
|
+
var ZIP_EPOCH = dosDate(1980, 1, 1);
|
|
228
|
+
var NO_PENDING = { pax: new Map, longName: undefined, longLink: undefined };
|
|
229
|
+
var MAX_META = 1 << 20;
|
|
230
|
+
// ../ts/kernel/base64.ts
|
|
231
|
+
function fromBase64(text) {
|
|
232
|
+
const t = text.replace(/[\t\n\r ]/g, "");
|
|
233
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(t))
|
|
234
|
+
return err("the text is not padded standard base64");
|
|
235
|
+
const s = atob(t);
|
|
236
|
+
const out = new Uint8Array(s.length);
|
|
237
|
+
for (let i = 0;i < s.length; i++)
|
|
238
|
+
out[i] = s.charCodeAt(i);
|
|
239
|
+
return ok(out);
|
|
240
|
+
}
|
|
241
|
+
// ../ts/kernel/args.ts
|
|
242
|
+
function flag(o) {
|
|
243
|
+
const negatable = o.negatable === true;
|
|
244
|
+
return {
|
|
245
|
+
kind: "flag",
|
|
246
|
+
short: o.short,
|
|
247
|
+
help: o.help,
|
|
248
|
+
negatable,
|
|
249
|
+
fallback: negatable && o.default === undefined ? { kind: "none" } : { kind: "default", value: o.default ?? false }
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function value(o) {
|
|
253
|
+
return {
|
|
254
|
+
kind: "value",
|
|
255
|
+
short: o.short,
|
|
256
|
+
help: o.help,
|
|
257
|
+
valueName: o.valueName ?? "VALUE",
|
|
258
|
+
parse: o.parse ?? text,
|
|
259
|
+
fallback: o.required === true ? { kind: "required" } : ("default" in o) ? { kind: "default", value: o.default } : { kind: "none" }
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
var text = (t) => ok(t);
|
|
263
|
+
function longName(key) {
|
|
264
|
+
return `--${key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}`;
|
|
265
|
+
}
|
|
266
|
+
function editDistance(a, b) {
|
|
267
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
268
|
+
for (let i = 1;i <= a.length; i++) {
|
|
269
|
+
const row = [i];
|
|
270
|
+
for (let j = 1;j <= b.length; j++) {
|
|
271
|
+
row.push(Math.min((prev[j] ?? 0) + 1, (row[j - 1] ?? 0) + 1, (prev[j - 1] ?? 0) + (a[i - 1] === b[j - 1] ? 0 : 1)));
|
|
272
|
+
}
|
|
273
|
+
prev = row;
|
|
274
|
+
}
|
|
275
|
+
return prev[b.length] ?? 0;
|
|
276
|
+
}
|
|
277
|
+
function suggest(word, candidates) {
|
|
278
|
+
let best;
|
|
279
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
280
|
+
for (const c of candidates) {
|
|
281
|
+
const d = editDistance(word, c);
|
|
282
|
+
if (d < bestDistance) {
|
|
283
|
+
best = c;
|
|
284
|
+
bestDistance = d;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return bestDistance <= Math.max(2, Math.floor(word.length / 2)) ? best : undefined;
|
|
288
|
+
}
|
|
289
|
+
function asksHelp(word) {
|
|
290
|
+
return word === "--help" || /^-[A-Za-z]*h[A-Za-z]*$/.test(word);
|
|
291
|
+
}
|
|
292
|
+
function operandWord(o) {
|
|
293
|
+
switch (o.arity) {
|
|
294
|
+
case "one":
|
|
295
|
+
return o.name;
|
|
296
|
+
case "optional":
|
|
297
|
+
return `[${o.name}]`;
|
|
298
|
+
case "many":
|
|
299
|
+
return `[${o.name}...]`;
|
|
300
|
+
default:
|
|
301
|
+
return absurd(o.arity);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function parseArgs(argv, spec, operands = []) {
|
|
305
|
+
const end = argv.indexOf("--");
|
|
306
|
+
if ((end < 0 ? argv : argv.slice(0, end)).some(asksHelp))
|
|
307
|
+
return ok({ kind: "help" });
|
|
308
|
+
const entries = Object.keys(spec).flatMap((key) => {
|
|
309
|
+
const option = spec[key];
|
|
310
|
+
return option === undefined ? [] : [{ key, long: longName(key), option }];
|
|
311
|
+
});
|
|
312
|
+
const longs = [
|
|
313
|
+
...entries.flatMap((e) => e.option.kind === "flag" && e.option.negatable ? [e.long, `--no-${e.long.slice(2)}`] : [e.long]),
|
|
314
|
+
"--help"
|
|
315
|
+
];
|
|
316
|
+
const given = new Map;
|
|
317
|
+
const listed = new Map;
|
|
318
|
+
const positionals = [];
|
|
319
|
+
let rest = [];
|
|
320
|
+
let i = 0;
|
|
321
|
+
const take = (e, written, inline, negated) => {
|
|
322
|
+
const o = e.option;
|
|
323
|
+
if (o.kind !== "many" && given.has(e.key))
|
|
324
|
+
return { kind: "repeated-option", option: e.long };
|
|
325
|
+
if (o.kind === "flag") {
|
|
326
|
+
if (inline !== undefined)
|
|
327
|
+
return { kind: "unexpected-value", option: written, value: inline };
|
|
328
|
+
given.set(e.key, !negated);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const t = inline ?? argv[++i];
|
|
332
|
+
if (t === undefined)
|
|
333
|
+
return { kind: "missing-value", option: written };
|
|
334
|
+
const parsed = o.parse(t);
|
|
335
|
+
if (parsed.kind === "err") {
|
|
336
|
+
const reason = typeof parsed.error === "string" ? parsed.error : `expected ${parsed.error.expected}, found ${parsed.error.found}`;
|
|
337
|
+
return { kind: "invalid-value", option: written, value: t, reason };
|
|
338
|
+
}
|
|
339
|
+
if (o.kind === "value")
|
|
340
|
+
given.set(e.key, parsed.value);
|
|
341
|
+
else
|
|
342
|
+
listed.set(e.key, [...listed.get(e.key) ?? [], parsed.value]);
|
|
343
|
+
return;
|
|
344
|
+
};
|
|
345
|
+
for (;i < argv.length; i++) {
|
|
346
|
+
const word = argv[i] ?? "";
|
|
347
|
+
if (word === "--") {
|
|
348
|
+
rest = argv.slice(i + 1);
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
if (word.startsWith("--")) {
|
|
352
|
+
const eq = word.indexOf("=");
|
|
353
|
+
const name = eq < 0 ? word : word.slice(0, eq);
|
|
354
|
+
const inline = eq < 0 ? undefined : word.slice(eq + 1);
|
|
355
|
+
if (name === "--help")
|
|
356
|
+
return err({ kind: "unexpected-value", option: name, value: inline ?? "" });
|
|
357
|
+
const direct = entries.find((e) => e.long === name);
|
|
358
|
+
const positive = name.startsWith("--no-") ? entries.find((e) => e.long === `--${name.slice(5)}`) : undefined;
|
|
359
|
+
const negated = direct === undefined && positive !== undefined && positive.option.kind === "flag" && positive.option.negatable;
|
|
360
|
+
const e = direct ?? (negated ? positive : undefined);
|
|
361
|
+
if (e === undefined)
|
|
362
|
+
return err({ kind: "unknown-option", option: name, suggestion: suggest(name, longs) });
|
|
363
|
+
const problem = take(e, name, inline, negated);
|
|
364
|
+
if (problem !== undefined)
|
|
365
|
+
return err(problem);
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if (word.length < 2 || !word.startsWith("-") || /^-[0-9]/.test(word)) {
|
|
369
|
+
positionals.push(word);
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
for (let j = 1;j < word.length; j++) {
|
|
373
|
+
const letter = word.charAt(j);
|
|
374
|
+
const written = `-${letter}`;
|
|
375
|
+
const e = entries.find((x) => x.option.short === letter);
|
|
376
|
+
if (e === undefined)
|
|
377
|
+
return err({ kind: "unknown-option", option: written, suggestion: suggest(`-${word}`, longs) });
|
|
378
|
+
const after = word.slice(j + 1);
|
|
379
|
+
if (e.option.kind === "flag") {
|
|
380
|
+
if (after.startsWith("="))
|
|
381
|
+
return err({ kind: "unexpected-value", option: written, value: after.slice(1) });
|
|
382
|
+
} else if (after !== "") {
|
|
383
|
+
return err({ kind: "attached-value", option: written, word });
|
|
384
|
+
}
|
|
385
|
+
const problem = take(e, written, undefined, false);
|
|
386
|
+
if (problem !== undefined)
|
|
387
|
+
return err(problem);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const values = [];
|
|
391
|
+
for (const e of entries) {
|
|
392
|
+
const o = e.option;
|
|
393
|
+
switch (o.kind) {
|
|
394
|
+
case "many":
|
|
395
|
+
values.push([e.key, listed.get(e.key) ?? []]);
|
|
396
|
+
break;
|
|
397
|
+
case "flag":
|
|
398
|
+
case "value": {
|
|
399
|
+
if (given.has(e.key)) {
|
|
400
|
+
values.push([e.key, given.get(e.key)]);
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
const f = o.fallback;
|
|
404
|
+
switch (f.kind) {
|
|
405
|
+
case "none":
|
|
406
|
+
values.push([e.key, undefined]);
|
|
407
|
+
break;
|
|
408
|
+
case "default":
|
|
409
|
+
values.push([e.key, f.value]);
|
|
410
|
+
break;
|
|
411
|
+
case "required":
|
|
412
|
+
return err({ kind: "missing-option", option: e.long });
|
|
413
|
+
default:
|
|
414
|
+
return absurd(f);
|
|
415
|
+
}
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
default:
|
|
419
|
+
return absurd(o);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const least = operands.filter((o) => o.arity === "one");
|
|
423
|
+
const most = operands.some((o) => o.arity === "many") ? Number.POSITIVE_INFINITY : operands.filter((o) => o.arity !== "many").length;
|
|
424
|
+
const missing = least[positionals.length];
|
|
425
|
+
if (missing !== undefined)
|
|
426
|
+
return err({ kind: "missing-argument", argument: missing.name });
|
|
427
|
+
const extra = positionals[most];
|
|
428
|
+
if (extra !== undefined)
|
|
429
|
+
return err({ kind: "unexpected-argument", argument: extra, takes: operands.map(operandWord) });
|
|
430
|
+
const options = Object.fromEntries(values);
|
|
431
|
+
return ok({ kind: "args", options, positionals, rest });
|
|
432
|
+
}
|
|
433
|
+
function formatUsageError(e, command = "this command") {
|
|
434
|
+
switch (e.kind) {
|
|
435
|
+
case "unknown-option":
|
|
436
|
+
return `${e.option} is not an option of ${command}${e.suggestion === undefined ? "" : `; did you mean ${e.suggestion}?`}`;
|
|
437
|
+
case "missing-value":
|
|
438
|
+
return `${e.option} needs a value`;
|
|
439
|
+
case "unexpected-value":
|
|
440
|
+
return `${e.option} takes no value, but was given ${JSON.stringify(e.value)}`;
|
|
441
|
+
case "attached-value":
|
|
442
|
+
return `${e.option} takes its value as the next word, not attached as in ${e.word}`;
|
|
443
|
+
case "invalid-value":
|
|
444
|
+
return `${JSON.stringify(e.value)} is not a valid value of ${e.option}: ${e.reason}`;
|
|
445
|
+
case "repeated-option":
|
|
446
|
+
return `${e.option} is given more than once`;
|
|
447
|
+
case "missing-option":
|
|
448
|
+
return `${e.option} is required`;
|
|
449
|
+
case "missing-argument":
|
|
450
|
+
return `${command} needs ${e.argument}`;
|
|
451
|
+
case "unexpected-argument":
|
|
452
|
+
return `${command} does not take ${e.argument}: ${e.takes.length === 0 ? "it takes no arguments" : `it takes ${e.takes.join(" ")}`}`;
|
|
453
|
+
default:
|
|
454
|
+
return absurd(e);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function shown(v) {
|
|
458
|
+
return typeof v === "string" || typeof v === "number" || typeof v === "boolean" || typeof v === "bigint" ? String(v) : JSON.stringify(v);
|
|
459
|
+
}
|
|
460
|
+
function optionLine(e) {
|
|
461
|
+
const o = e.option;
|
|
462
|
+
const short = o.short === undefined ? " " : `-${o.short}, `;
|
|
463
|
+
switch (o.kind) {
|
|
464
|
+
case "flag":
|
|
465
|
+
return {
|
|
466
|
+
left: `${short}${o.negatable ? `--[no-]${e.long.slice(2)}` : e.long}`,
|
|
467
|
+
note: o.negatable && o.fallback.kind === "default" ? ` (default: ${o.fallback.value ? e.long : `--no-${e.long.slice(2)}`})` : ""
|
|
468
|
+
};
|
|
469
|
+
case "value": {
|
|
470
|
+
const f = o.fallback;
|
|
471
|
+
const note = f.kind === "default" ? ` (default: ${shown(f.value)})` : f.kind === "required" ? " (required)" : "";
|
|
472
|
+
return { left: `${short}${e.long} ${o.valueName}`, note };
|
|
473
|
+
}
|
|
474
|
+
case "many":
|
|
475
|
+
return { left: `${short}${e.long} ${o.valueName}`, note: " (repeatable)" };
|
|
476
|
+
default:
|
|
477
|
+
return absurd(o);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function columns(rows) {
|
|
481
|
+
const width = Math.max(0, ...rows.map(([l]) => l.length));
|
|
482
|
+
return rows.map(([l, r]) => ` ${l.padEnd(width)} ${r}`.trimEnd());
|
|
483
|
+
}
|
|
484
|
+
function helpOf(program, summary, spec, operands = []) {
|
|
485
|
+
const entries = Object.keys(spec).flatMap((key) => {
|
|
486
|
+
const option = spec[key];
|
|
487
|
+
return option === undefined ? [] : [{ key, long: longName(key), option }];
|
|
488
|
+
});
|
|
489
|
+
const required = entries.flatMap((e) => e.option.kind === "value" && e.option.fallback.kind === "required" ? [`${e.long} ${e.option.valueName}`] : []);
|
|
490
|
+
const usage = [program, "[OPTIONS]", ...required, ...operands.map(operandWord)].join(" ");
|
|
491
|
+
const rows = entries.map((e) => {
|
|
492
|
+
const line = optionLine(e);
|
|
493
|
+
return [line.left, `${e.option.help}${line.note}`];
|
|
494
|
+
});
|
|
495
|
+
return [
|
|
496
|
+
`${program}: ${summary}`,
|
|
497
|
+
"",
|
|
498
|
+
`Usage: ${usage}`,
|
|
499
|
+
"",
|
|
500
|
+
"Options:",
|
|
501
|
+
...columns([...rows, ["-h, --help", "Show this help and do nothing else."]]),
|
|
502
|
+
...operands.length === 0 ? [] : ["", "Arguments:", ...columns(operands.map((o) => [operandWord(o), o.help]))]
|
|
503
|
+
].join(`
|
|
504
|
+
`);
|
|
505
|
+
}
|
|
506
|
+
// ../ts/kernel/grc.ts
|
|
507
|
+
var INSTANT = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:[.,](\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/i;
|
|
508
|
+
var CANONICAL_INSTANT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
|
509
|
+
function instantOf(text) {
|
|
510
|
+
const m = INSTANT.exec(text.trim());
|
|
511
|
+
if (m === null)
|
|
512
|
+
return;
|
|
513
|
+
const [y, mo, d, h, mi, s] = [m[1], m[2], m[3], m[4], m[5], m[6]].map((x) => Number(x ?? ""));
|
|
514
|
+
if (y === undefined || mo === undefined || d === undefined || h === undefined || mi === undefined || s === undefined)
|
|
515
|
+
return;
|
|
516
|
+
const ms = Number((m[7] ?? "").padEnd(3, "0").slice(0, 3));
|
|
517
|
+
if (mo < 1 || mo > 12 || d < 1 || d > daysInMonth(y, mo) || h > 23 || mi > 59 || s > 59)
|
|
518
|
+
return;
|
|
519
|
+
const zone = (m[8] ?? "Z").toUpperCase();
|
|
520
|
+
let offset = 0;
|
|
521
|
+
if (zone !== "Z") {
|
|
522
|
+
const oh = Number(zone.slice(1, 3));
|
|
523
|
+
const om = Number(zone.slice(4, 6));
|
|
524
|
+
if (oh > 23 || om > 59)
|
|
525
|
+
return;
|
|
526
|
+
offset = (zone.startsWith("-") ? -1 : 1) * (oh * 60 + om) * 60000;
|
|
527
|
+
}
|
|
528
|
+
const date = new Date(0);
|
|
529
|
+
date.setUTCFullYear(y, mo - 1, d);
|
|
530
|
+
date.setUTCHours(h, mi, s, ms);
|
|
531
|
+
return instantFromEpochMs(date.getTime() - offset);
|
|
532
|
+
}
|
|
533
|
+
function instantFromEpochMs(ms) {
|
|
534
|
+
if (!Number.isFinite(ms))
|
|
535
|
+
return;
|
|
536
|
+
const date = new Date(Math.trunc(ms));
|
|
537
|
+
if (Number.isNaN(date.getTime()))
|
|
538
|
+
return;
|
|
539
|
+
const text = date.toISOString();
|
|
540
|
+
return CANONICAL_INSTANT.test(text) ? text : undefined;
|
|
541
|
+
}
|
|
542
|
+
function daysInMonth(year, month) {
|
|
543
|
+
const leap = year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
544
|
+
return month === 2 ? leap ? 29 : 28 : [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
545
|
+
}
|
|
546
|
+
var DURATION = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:[.,]\d+)?)S)?)?$/;
|
|
547
|
+
function parseDuration(text) {
|
|
548
|
+
const m = DURATION.exec(text);
|
|
549
|
+
if (m === null || text === "P" || text.endsWith("T"))
|
|
550
|
+
return err({ kind: "duration", text });
|
|
551
|
+
const n = (i) => Number((m[i] ?? "0").replace(",", "."));
|
|
552
|
+
return ok({ text, years: n(1), months: n(2), weeks: n(3), days: n(4), hours: n(5), minutes: n(6), seconds: n(7) });
|
|
553
|
+
}
|
|
554
|
+
var ROLE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+)*$/;
|
|
555
|
+
function roleOf(text) {
|
|
556
|
+
return ROLE.test(text) ? text : undefined;
|
|
557
|
+
}
|
|
558
|
+
var SEVERITIES = ["info", "low", "medium", "high", "critical"];
|
|
559
|
+
var refined = (name, f) => {
|
|
560
|
+
return (input, path = "$") => {
|
|
561
|
+
if (typeof input === "string") {
|
|
562
|
+
const v = f(input);
|
|
563
|
+
if (v !== undefined)
|
|
564
|
+
return ok(v);
|
|
565
|
+
}
|
|
566
|
+
return err({
|
|
567
|
+
kind: "decode",
|
|
568
|
+
path,
|
|
569
|
+
expected: name,
|
|
570
|
+
found: typeof input === "string" ? `the string ${JSON.stringify(input).slice(0, 60)}` : typeof input
|
|
571
|
+
});
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
var instantDecoder = refined("an ISO 8601 date-time with a zone", instantOf);
|
|
575
|
+
var roleDecoder = refined("a role (lower-case words joined by - _ . or /)", roleOf);
|
|
576
|
+
var durationDecoder = refined("an ISO 8601 duration", (t) => {
|
|
577
|
+
const r = parseDuration(t);
|
|
578
|
+
return r.kind === "ok" ? r.value : undefined;
|
|
579
|
+
});
|
|
580
|
+
var severityDecoder = literal(...SEVERITIES);
|
|
581
|
+
var waiverInput = mapDecoder(object({ id: string, control: string, subjects: array(string), reason: string, approvedBy: roleDecoder, grantedAt: instantDecoder }, { expiresAt: instantDecoder }), (w) => w);
|
|
582
|
+
|
|
583
|
+
// ../ts/kernel/assess.ts
|
|
584
|
+
var impactDeclarationDecoder = object({ paths: array(string), controls: array(string) });
|
|
585
|
+
var RISK_LEVELS = ["very-low", "low", "moderate", "high", "very-high"];
|
|
586
|
+
var levelDecoder = literal(...RISK_LEVELS);
|
|
587
|
+
var riskDecoder = object({
|
|
588
|
+
id: string,
|
|
589
|
+
title: string,
|
|
590
|
+
likelihood: levelDecoder,
|
|
591
|
+
impact: levelDecoder,
|
|
592
|
+
treatment: literal("accept", "mitigate", "transfer", "avoid"),
|
|
593
|
+
owner: roleDecoder,
|
|
594
|
+
reviewBy: instantDecoder
|
|
595
|
+
}, { description: string, controls: array(string) });
|
|
596
|
+
// ../ts/kernel/sha256.ts
|
|
597
|
+
var SHA256 = /^[0-9a-f]{64}$/;
|
|
598
|
+
function sha256Of(text) {
|
|
599
|
+
const t = text.trim().toLowerCase();
|
|
600
|
+
return SHA256.test(t) ? t : undefined;
|
|
601
|
+
}
|
|
602
|
+
function toHex(bytes) {
|
|
603
|
+
let out = "";
|
|
604
|
+
for (const b of bytes)
|
|
605
|
+
out += b.toString(16).padStart(2, "0");
|
|
606
|
+
return out;
|
|
607
|
+
}
|
|
608
|
+
async function sha256(data) {
|
|
609
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
610
|
+
const digest = await crypto.subtle.digest("SHA-256", new Uint8Array(bytes));
|
|
611
|
+
return toHex(new Uint8Array(digest));
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ../ts/kernel/dsse.ts
|
|
615
|
+
var utf8 = new TextEncoder;
|
|
616
|
+
var lowerHex = (input, path = "$") => typeof input === "string" && /^(?:[0-9a-f]{2})+$/.test(input) ? ok(input) : err({ kind: "decode", path, expected: "lower-case hexadecimal", found: describe(input) });
|
|
617
|
+
var resourceDescriptorDecoder = object({ name: string, digest: record(lowerHex) }, { uri: string, mediaType: string, annotations: record(unknownValue) });
|
|
618
|
+
var base64Text = (input, path = "$") => typeof input === "string" && fromBase64(input).kind === "ok" ? ok(input) : err({ kind: "decode", path, expected: "standard base64", found: describe(input) });
|
|
619
|
+
var envelopeDecoder = object({
|
|
620
|
+
payload: base64Text,
|
|
621
|
+
payloadType: string,
|
|
622
|
+
signatures: array(object({ sig: base64Text }, { keyid: string }))
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// ../ts/kernel/evidence.ts
|
|
626
|
+
var GENESIS = "0".repeat(64);
|
|
627
|
+
var sha256Decoder = (input, path = "$") => {
|
|
628
|
+
const s = typeof input === "string" && /^[0-9a-f]{64}$/.test(input) ? sha256Of(input) : undefined;
|
|
629
|
+
return s === undefined ? err({ kind: "decode", path, expected: "a SHA-256 (64 lower-case hexadecimal digits)", found: typeof input }) : ok(s);
|
|
630
|
+
};
|
|
631
|
+
var ledgerEntryDecoder = object({
|
|
632
|
+
seq: refine(integer, "positive", (n) => n >= 1),
|
|
633
|
+
prev: sha256Decoder,
|
|
634
|
+
at: instantDecoder,
|
|
635
|
+
kind: string,
|
|
636
|
+
subject: object({ kind: string, id: string }),
|
|
637
|
+
evidence: sha256Decoder,
|
|
638
|
+
digest: sha256Decoder
|
|
639
|
+
});
|
|
640
|
+
var manifestDecoder = object({
|
|
641
|
+
files: array(object({ path: string, size: refine(integer, "size", (n) => n >= 0), sha256: sha256Decoder }))
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
// ../ts/kernel/uuid.ts
|
|
645
|
+
var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
646
|
+
function uuidOf(text) {
|
|
647
|
+
const t = text.trim().toLowerCase();
|
|
648
|
+
const bare = t.startsWith("urn:uuid:") ? t.slice("urn:uuid:".length) : t;
|
|
649
|
+
return UUID.test(bare) ? bare : undefined;
|
|
650
|
+
}
|
|
651
|
+
function constant(text) {
|
|
652
|
+
const u = uuidOf(text);
|
|
653
|
+
if (u === undefined)
|
|
654
|
+
throw new Error(`not a UUID: ${text}`);
|
|
655
|
+
return u;
|
|
656
|
+
}
|
|
657
|
+
var NIL_UUID = constant("00000000-0000-0000-0000-000000000000");
|
|
658
|
+
var NAMESPACE_DNS = constant("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
|
|
659
|
+
var NAMESPACE_URL = constant("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
|
|
660
|
+
var NAMESPACE_OID = constant("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
|
|
661
|
+
var NAMESPACE_X500 = constant("6ba7b814-9dad-11d1-80b4-00c04fd430c8");
|
|
662
|
+
var utf82 = new TextEncoder;
|
|
663
|
+
|
|
664
|
+
// ../ts/kernel/attest.ts
|
|
665
|
+
var PASSING = new Set(["pass"]);
|
|
666
|
+
// ../ts/kernel/cbor.ts
|
|
667
|
+
var UTF8 = new TextDecoder("utf-8", { fatal: true });
|
|
668
|
+
// ../ts/kernel/glob.ts
|
|
669
|
+
var ESCAPE_OUTSIDE = new Set(["!", "$", "(", ")", "*", "+", ".", "=", "?", "[", "\\", "^", "{", "|"]);
|
|
670
|
+
var ESCAPE_INSIDE = new Set(["-", "\\", "]"]);
|
|
671
|
+
var CLASSES = new Map([
|
|
672
|
+
["alnum", "\\dA-Za-z"],
|
|
673
|
+
["alpha", "A-Za-z"],
|
|
674
|
+
["ascii", "\\x00-\\x7F"],
|
|
675
|
+
["blank", "\\t "],
|
|
676
|
+
["cntrl", "\\x00-\\x1F\\x7F"],
|
|
677
|
+
["digit", "\\d"],
|
|
678
|
+
["graph", "\\x21-\\x7E"],
|
|
679
|
+
["lower", "a-z"],
|
|
680
|
+
["print", "\\x20-\\x7E"],
|
|
681
|
+
["punct", "!-\\/:-@\\[-`{-~"],
|
|
682
|
+
["space", "\\s\\v"],
|
|
683
|
+
["upper", "A-Z"],
|
|
684
|
+
["word", "\\w"],
|
|
685
|
+
["xdigit", "\\dA-Fa-f"]
|
|
686
|
+
]);
|
|
687
|
+
// ../ts/kernel/http.ts
|
|
688
|
+
var SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
|
|
689
|
+
// ../ts/kernel/md5.ts
|
|
690
|
+
var T = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) >>> 0);
|
|
691
|
+
// ../ts/kernel/semver.ts
|
|
692
|
+
var PATTERN = /^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/;
|
|
693
|
+
function parseSemVer(text) {
|
|
694
|
+
const m = PATTERN.exec(text.trim());
|
|
695
|
+
if (m === null)
|
|
696
|
+
return err(`${JSON.stringify(text)} is not a semantic version (major.minor.patch[-pre][+build])`);
|
|
697
|
+
const [, major = "", minor = "", patch = "", pre, build] = m;
|
|
698
|
+
const parts = [Number(major), Number(minor), Number(patch)];
|
|
699
|
+
if (!parts.every(Number.isSafeInteger))
|
|
700
|
+
return err(`${JSON.stringify(text)} has a number too large`);
|
|
701
|
+
return ok({
|
|
702
|
+
major: parts[0],
|
|
703
|
+
minor: parts[1],
|
|
704
|
+
patch: parts[2],
|
|
705
|
+
...pre === undefined ? {} : { pre },
|
|
706
|
+
...build === undefined ? {} : { build }
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
var IDENT = "(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)";
|
|
710
|
+
var PART = "(0|[1-9][0-9]*|[xX*])";
|
|
711
|
+
var PARTIAL = new RegExp(`^v?${PART}(?:\\.${PART}(?:\\.${PART}(?:-(${IDENT}(?:\\.${IDENT})*))?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?)?)?$`);
|
|
712
|
+
var ver = (major, minor, patch, pre) => ({
|
|
713
|
+
major,
|
|
714
|
+
minor,
|
|
715
|
+
patch,
|
|
716
|
+
...pre === undefined ? {} : { pre }
|
|
717
|
+
});
|
|
718
|
+
var at = (op, version) => ({ op, version });
|
|
719
|
+
var NOTHING = [at("<", ver(0, 0, 0, "0"))];
|
|
720
|
+
// ../ts/kernel/oscal.ts
|
|
721
|
+
var OPEN_OUTCOMES = new Set(["fail", "warn", "gap", "error", "accepted"]);
|
|
722
|
+
// ../ts/kernel/otel.ts
|
|
723
|
+
var TRACE_ID = /^[0-9a-f]{32}$/;
|
|
724
|
+
var SPAN_ID = /^[0-9a-f]{16}$/;
|
|
725
|
+
var ZEROS = /^0+$/;
|
|
726
|
+
function parseTraceId(text) {
|
|
727
|
+
if (!TRACE_ID.test(text))
|
|
728
|
+
return err("a trace id is 32 lower-case hexadecimal digits");
|
|
729
|
+
if (ZEROS.test(text))
|
|
730
|
+
return err("a trace id of all zeros is not valid");
|
|
731
|
+
return ok(text);
|
|
732
|
+
}
|
|
733
|
+
function parseSpanId(text) {
|
|
734
|
+
if (!SPAN_ID.test(text))
|
|
735
|
+
return err("a span id is 16 lower-case hexadecimal digits");
|
|
736
|
+
if (ZEROS.test(text))
|
|
737
|
+
return err("a span id of all zeros is not valid");
|
|
738
|
+
return ok(text);
|
|
739
|
+
}
|
|
740
|
+
var SAMPLED = 1;
|
|
741
|
+
var RANDOM = 2;
|
|
742
|
+
var KNOWN_FLAGS = SAMPLED | RANDOM;
|
|
743
|
+
var TRACESTATE_MAX_MEMBERS = 32;
|
|
744
|
+
var KEY_CHAR = "[a-z0-9_\\-*/]";
|
|
745
|
+
var TRACESTATE_KEY = new RegExp(`^(?:[a-z]${KEY_CHAR}{0,255}|[a-z0-9]${KEY_CHAR}{0,240}@[a-z]${KEY_CHAR}{0,13})$`);
|
|
746
|
+
var TRACESTATE_VALUE = /^[\x20-\x2b\x2d-\x3c\x3e-\x7e]{0,255}[\x21-\x2b\x2d-\x3c\x3e-\x7e]$/;
|
|
747
|
+
function isTraceStateKey(key) {
|
|
748
|
+
return TRACESTATE_KEY.test(key);
|
|
749
|
+
}
|
|
750
|
+
function isTraceStateValue(value) {
|
|
751
|
+
return TRACESTATE_VALUE.test(value);
|
|
752
|
+
}
|
|
753
|
+
function parseTracestate(header) {
|
|
754
|
+
const out = [];
|
|
755
|
+
const seen = new Set;
|
|
756
|
+
for (const raw of header.split(",")) {
|
|
757
|
+
if (out.length >= TRACESTATE_MAX_MEMBERS)
|
|
758
|
+
break;
|
|
759
|
+
const member = raw.replace(/^[ \t]+|[ \t]+$/g, "");
|
|
760
|
+
if (member === "")
|
|
761
|
+
continue;
|
|
762
|
+
const eq = member.indexOf("=");
|
|
763
|
+
if (eq < 0)
|
|
764
|
+
continue;
|
|
765
|
+
const key = member.slice(0, eq);
|
|
766
|
+
const value = member.slice(eq + 1);
|
|
767
|
+
if (!isTraceStateKey(key) || !isTraceStateValue(value) || seen.has(key))
|
|
768
|
+
continue;
|
|
769
|
+
seen.add(key);
|
|
770
|
+
out.push({ key, value });
|
|
771
|
+
}
|
|
772
|
+
return out;
|
|
773
|
+
}
|
|
774
|
+
var SEVERITY = { TRACE: 1, DEBUG: 5, INFO: 9, WARN: 13, ERROR: 17, FATAL: 21 };
|
|
775
|
+
function isSeverityNumber(n) {
|
|
776
|
+
return Number.isInteger(n) && n >= 1 && n <= 24;
|
|
777
|
+
}
|
|
778
|
+
var TWO_56 = 2 ** 56;
|
|
779
|
+
var CREDENTIAL_WORDS = new Set(["key", "code", "sig", "pwd", "pass", "session", "sid", "otp", "awsaccesskeyid", "sas", "se", "sp", "sv"]);
|
|
780
|
+
// ../ts/kernel/otlp.ts
|
|
781
|
+
var SPAN_KINDS = ["internal", "server", "client", "producer", "consumer"];
|
|
782
|
+
class Failure extends Error {
|
|
783
|
+
error;
|
|
784
|
+
constructor(error) {
|
|
785
|
+
super(error.path);
|
|
786
|
+
this.error = error;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
function failure(path, expected, input) {
|
|
790
|
+
return { kind: "decode", path, expected, found: describe(input) };
|
|
791
|
+
}
|
|
792
|
+
function message(build) {
|
|
793
|
+
return (input, path = "$") => {
|
|
794
|
+
if (typeof input !== "object" || input === null || Array.isArray(input))
|
|
795
|
+
return err(failure(path, "an object", input));
|
|
796
|
+
const read = (key, decoder) => {
|
|
797
|
+
const r = decoder(Reflect.get(input, key), `${path}.${key}`);
|
|
798
|
+
if (r.kind === "err")
|
|
799
|
+
throw new Failure(r.error);
|
|
800
|
+
return r.value;
|
|
801
|
+
};
|
|
802
|
+
const f = {
|
|
803
|
+
req: read,
|
|
804
|
+
opt: (key, decoder, fallback) => {
|
|
805
|
+
const v = Reflect.get(input, key);
|
|
806
|
+
return v === undefined || v === null ? fallback : read(key, decoder);
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
try {
|
|
810
|
+
return ok(build(f, path));
|
|
811
|
+
} catch (e) {
|
|
812
|
+
if (e instanceof Failure)
|
|
813
|
+
return err(e.error);
|
|
814
|
+
throw e;
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
function list(element) {
|
|
819
|
+
return (input, path = "$") => {
|
|
820
|
+
if (!Array.isArray(input))
|
|
821
|
+
return err(failure(path, "an array", input));
|
|
822
|
+
const out = [];
|
|
823
|
+
for (const [i, value] of input.entries()) {
|
|
824
|
+
const r = element(value, `${path}[${i}]`);
|
|
825
|
+
if (r.kind === "err")
|
|
826
|
+
return r;
|
|
827
|
+
out.push(r.value);
|
|
828
|
+
}
|
|
829
|
+
return ok(out);
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
var str = (input, path = "$") => typeof input === "string" ? ok(input) : err(failure(path, "a string", input));
|
|
833
|
+
var bool = (input, path = "$") => typeof input === "boolean" ? ok(input) : err(failure(path, "a boolean", input));
|
|
834
|
+
var int64 = (input, path = "$") => {
|
|
835
|
+
if (typeof input === "number" && Number.isInteger(input))
|
|
836
|
+
return ok(BigInt(input));
|
|
837
|
+
if (typeof input === "string" && /^-?[0-9]+$/.test(input))
|
|
838
|
+
return ok(BigInt(input));
|
|
839
|
+
return err(failure(path, "a 64-bit integer (a number or a decimal string)", input));
|
|
840
|
+
};
|
|
841
|
+
var count = (input, path = "$") => {
|
|
842
|
+
const r = int64(input, path);
|
|
843
|
+
if (r.kind === "err")
|
|
844
|
+
return r;
|
|
845
|
+
return r.value >= 0n && r.value <= BigInt(Number.MAX_SAFE_INTEGER) ? ok(Number(r.value)) : err(failure(path, "a count", input));
|
|
846
|
+
};
|
|
847
|
+
var double = (input, path = "$") => {
|
|
848
|
+
if (typeof input === "number")
|
|
849
|
+
return ok(input);
|
|
850
|
+
if (input === "NaN")
|
|
851
|
+
return ok(NaN);
|
|
852
|
+
if (input === "Infinity")
|
|
853
|
+
return ok(Infinity);
|
|
854
|
+
if (input === "-Infinity")
|
|
855
|
+
return ok(-Infinity);
|
|
856
|
+
return err(failure(path, "a double", input));
|
|
857
|
+
};
|
|
858
|
+
var bytes = (input, path = "$") => {
|
|
859
|
+
if (typeof input !== "string")
|
|
860
|
+
return err(failure(path, "base64", input));
|
|
861
|
+
const r = fromBase64(input);
|
|
862
|
+
return r.kind === "ok" ? r : err(failure(path, "base64", input));
|
|
863
|
+
};
|
|
864
|
+
var traceId = (input, path = "$") => {
|
|
865
|
+
const r = typeof input === "string" ? parseTraceId(input.toLowerCase()) : err("");
|
|
866
|
+
return r.kind === "ok" ? r : err(failure(path, "a trace id (32 hexadecimal digits, not all zeros)", input));
|
|
867
|
+
};
|
|
868
|
+
var spanId = (input, path = "$") => {
|
|
869
|
+
const r = typeof input === "string" ? parseSpanId(input.toLowerCase()) : err("");
|
|
870
|
+
return r.kind === "ok" ? r : err(failure(path, "a span id (16 hexadecimal digits, not all zeros)", input));
|
|
871
|
+
};
|
|
872
|
+
function enumeration(names) {
|
|
873
|
+
return (input, path = "$") => {
|
|
874
|
+
if (typeof input === "number" && Number.isInteger(input) && input >= 0 && input < names.length)
|
|
875
|
+
return ok(input);
|
|
876
|
+
if (typeof input === "string") {
|
|
877
|
+
const i = names.indexOf(input);
|
|
878
|
+
if (i >= 0)
|
|
879
|
+
return ok(i);
|
|
880
|
+
}
|
|
881
|
+
return err(failure(path, `one of ${names.join(", ")} or its number`, input));
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
var spanKindEnum = enumeration([
|
|
885
|
+
"SPAN_KIND_UNSPECIFIED",
|
|
886
|
+
"SPAN_KIND_INTERNAL",
|
|
887
|
+
"SPAN_KIND_SERVER",
|
|
888
|
+
"SPAN_KIND_CLIENT",
|
|
889
|
+
"SPAN_KIND_PRODUCER",
|
|
890
|
+
"SPAN_KIND_CONSUMER"
|
|
891
|
+
]);
|
|
892
|
+
var statusCodeEnum = enumeration(["STATUS_CODE_UNSET", "STATUS_CODE_OK", "STATUS_CODE_ERROR"]);
|
|
893
|
+
var temporalityEnum = enumeration([
|
|
894
|
+
"AGGREGATION_TEMPORALITY_UNSPECIFIED",
|
|
895
|
+
"AGGREGATION_TEMPORALITY_DELTA",
|
|
896
|
+
"AGGREGATION_TEMPORALITY_CUMULATIVE"
|
|
897
|
+
]);
|
|
898
|
+
var SEVERITY_NAMES = ["SEVERITY_NUMBER_UNSPECIFIED"].concat(["TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"].flatMap((r) => ["", "2", "3", "4"].map((n) => `SEVERITY_NUMBER_${r}${n}`)));
|
|
899
|
+
var severityEnum = (input, path = "$") => {
|
|
900
|
+
const r = enumeration(SEVERITY_NAMES)(input, path);
|
|
901
|
+
if (r.kind === "err")
|
|
902
|
+
return r;
|
|
903
|
+
return isSeverityNumber(r.value) ? ok(r.value) : err(failure(path, "a severity number from 1 to 24", input));
|
|
904
|
+
};
|
|
905
|
+
var decodeAnyValue = (input, path = "$") => {
|
|
906
|
+
if (typeof input !== "object" || input === null || Array.isArray(input))
|
|
907
|
+
return err(failure(path, "an AnyValue object", input));
|
|
908
|
+
const has = (key) => {
|
|
909
|
+
const v = Reflect.get(input, key);
|
|
910
|
+
return v !== undefined && v !== null;
|
|
911
|
+
};
|
|
912
|
+
const at = (key) => Reflect.get(input, key);
|
|
913
|
+
if (has("stringValue"))
|
|
914
|
+
return str(at("stringValue"), `${path}.stringValue`);
|
|
915
|
+
if (has("boolValue"))
|
|
916
|
+
return bool(at("boolValue"), `${path}.boolValue`);
|
|
917
|
+
if (has("intValue"))
|
|
918
|
+
return int64(at("intValue"), `${path}.intValue`);
|
|
919
|
+
if (has("doubleValue"))
|
|
920
|
+
return double(at("doubleValue"), `${path}.doubleValue`);
|
|
921
|
+
if (has("bytesValue"))
|
|
922
|
+
return bytes(at("bytesValue"), `${path}.bytesValue`);
|
|
923
|
+
if (has("arrayValue"))
|
|
924
|
+
return message((f) => f.opt("values", list(decodeAnyValue), []))(at("arrayValue"), `${path}.arrayValue`);
|
|
925
|
+
if (has("kvlistValue")) {
|
|
926
|
+
return message((f) => {
|
|
927
|
+
const out = {};
|
|
928
|
+
for (const kv of f.opt("values", list(keyValue(decodeAnyValue)), [])) {
|
|
929
|
+
Object.defineProperty(out, kv.key, { value: kv.value, enumerable: true, writable: true, configurable: true });
|
|
930
|
+
}
|
|
931
|
+
return out;
|
|
932
|
+
})(at("kvlistValue"), `${path}.kvlistValue`);
|
|
933
|
+
}
|
|
934
|
+
return err(failure(path, "an AnyValue holding a value", input));
|
|
935
|
+
};
|
|
936
|
+
function keyValue(value) {
|
|
937
|
+
return message((f) => ({ key: f.req("key", str), value: f.req("value", value) }));
|
|
938
|
+
}
|
|
939
|
+
var decodeAttributeValue = (input, path = "$") => {
|
|
940
|
+
const r = decodeAnyValue(input, path);
|
|
941
|
+
if (r.kind === "err")
|
|
942
|
+
return r;
|
|
943
|
+
const v = r.value;
|
|
944
|
+
if (typeof v !== "object" || v instanceof Uint8Array)
|
|
945
|
+
return ok(v);
|
|
946
|
+
if (Array.isArray(v)) {
|
|
947
|
+
const values = v;
|
|
948
|
+
if (values.every((e) => typeof e === "string"))
|
|
949
|
+
return ok(values.filter((e) => typeof e === "string"));
|
|
950
|
+
if (values.every((e) => typeof e === "boolean"))
|
|
951
|
+
return ok(values.filter((e) => typeof e === "boolean"));
|
|
952
|
+
if (values.every((e) => typeof e === "bigint"))
|
|
953
|
+
return ok(values.filter((e) => typeof e === "bigint"));
|
|
954
|
+
if (values.every((e) => typeof e === "number"))
|
|
955
|
+
return ok(values.filter((e) => typeof e === "number"));
|
|
956
|
+
}
|
|
957
|
+
return err(failure(path, "an attribute value (a primitive or a homogeneous array of primitives)", input));
|
|
958
|
+
};
|
|
959
|
+
var decodeAttributes = (input, path = "$") => {
|
|
960
|
+
const r = list(keyValue(decodeAttributeValue))(input, path);
|
|
961
|
+
if (r.kind === "err")
|
|
962
|
+
return r;
|
|
963
|
+
const out = {};
|
|
964
|
+
for (const kv of r.value)
|
|
965
|
+
Object.defineProperty(out, kv.key, { value: kv.value, enumerable: true, writable: true, configurable: true });
|
|
966
|
+
return ok(out);
|
|
967
|
+
};
|
|
968
|
+
var resource = message((f) => ({ attributes: f.opt("attributes", decodeAttributes, {}) }));
|
|
969
|
+
var scope = message((f) => {
|
|
970
|
+
const version = f.opt("version", str, undefined);
|
|
971
|
+
const attributes = f.opt("attributes", decodeAttributes, undefined);
|
|
972
|
+
return {
|
|
973
|
+
name: f.opt("name", str, ""),
|
|
974
|
+
...version === undefined ? {} : { version },
|
|
975
|
+
...attributes === undefined ? {} : { attributes }
|
|
976
|
+
};
|
|
977
|
+
});
|
|
978
|
+
var event = message((f) => ({
|
|
979
|
+
name: f.opt("name", str, ""),
|
|
980
|
+
time: f.opt("timeUnixNano", int64, 0n),
|
|
981
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
982
|
+
droppedAttributesCount: f.opt("droppedAttributesCount", count, 0)
|
|
983
|
+
}));
|
|
984
|
+
var link = message((f) => ({
|
|
985
|
+
traceId: f.req("traceId", traceId),
|
|
986
|
+
spanId: f.req("spanId", spanId),
|
|
987
|
+
traceState: parseTracestate(f.opt("traceState", str, "")),
|
|
988
|
+
traceFlags: f.opt("flags", count, 0) & 255,
|
|
989
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
990
|
+
droppedAttributesCount: f.opt("droppedAttributesCount", count, 0)
|
|
991
|
+
}));
|
|
992
|
+
var status = message((f) => {
|
|
993
|
+
const code = f.opt("code", statusCodeEnum, 0);
|
|
994
|
+
if (code === 1)
|
|
995
|
+
return { kind: "ok" };
|
|
996
|
+
if (code === 2)
|
|
997
|
+
return { kind: "error", message: f.opt("message", str, "") };
|
|
998
|
+
return { kind: "unset" };
|
|
999
|
+
});
|
|
1000
|
+
var optionalSpanId = (input, path = "$") => input === "" ? ok(undefined) : spanId(input, path);
|
|
1001
|
+
var decodeSpan = message((f) => {
|
|
1002
|
+
const parentSpanId = f.opt("parentSpanId", optionalSpanId, undefined);
|
|
1003
|
+
const kind = f.opt("kind", spanKindEnum, 1);
|
|
1004
|
+
return {
|
|
1005
|
+
name: f.opt("name", str, ""),
|
|
1006
|
+
context: {
|
|
1007
|
+
traceId: f.req("traceId", traceId),
|
|
1008
|
+
spanId: f.req("spanId", spanId),
|
|
1009
|
+
traceFlags: f.opt("flags", count, 0) & 255,
|
|
1010
|
+
traceState: parseTracestate(f.opt("traceState", str, "")),
|
|
1011
|
+
isRemote: false
|
|
1012
|
+
},
|
|
1013
|
+
...parentSpanId === undefined ? {} : { parentSpanId },
|
|
1014
|
+
kind: SPAN_KINDS[kind - 1] ?? "internal",
|
|
1015
|
+
startTime: f.opt("startTimeUnixNano", int64, 0n),
|
|
1016
|
+
endTime: f.opt("endTimeUnixNano", int64, 0n),
|
|
1017
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
1018
|
+
events: f.opt("events", list(event), []),
|
|
1019
|
+
links: f.opt("links", list(link), []),
|
|
1020
|
+
status: f.opt("status", status, { kind: "unset" }),
|
|
1021
|
+
droppedAttributesCount: f.opt("droppedAttributesCount", count, 0),
|
|
1022
|
+
droppedEventsCount: f.opt("droppedEventsCount", count, 0),
|
|
1023
|
+
droppedLinksCount: f.opt("droppedLinksCount", count, 0)
|
|
1024
|
+
};
|
|
1025
|
+
});
|
|
1026
|
+
var optionalTraceId = (input, path = "$") => input === "" ? ok(undefined) : traceId(input, path);
|
|
1027
|
+
var decodeLogRecord = message((f) => {
|
|
1028
|
+
const body = f.opt("body", decodeAnyValue, undefined);
|
|
1029
|
+
const traceFlags = f.opt("flags", count, undefined);
|
|
1030
|
+
const tid = f.opt("traceId", optionalTraceId, undefined);
|
|
1031
|
+
const sid = f.opt("spanId", optionalSpanId, undefined);
|
|
1032
|
+
const eventName = f.opt("eventName", str, undefined);
|
|
1033
|
+
return {
|
|
1034
|
+
time: f.opt("timeUnixNano", int64, 0n),
|
|
1035
|
+
observedTime: f.opt("observedTimeUnixNano", int64, 0n),
|
|
1036
|
+
severityNumber: f.req("severityNumber", severityEnum),
|
|
1037
|
+
severityText: f.opt("severityText", str, ""),
|
|
1038
|
+
...body === undefined ? {} : { body },
|
|
1039
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
1040
|
+
droppedAttributesCount: f.opt("droppedAttributesCount", count, 0),
|
|
1041
|
+
...eventName === undefined ? {} : { eventName },
|
|
1042
|
+
...tid === undefined ? {} : { traceId: tid },
|
|
1043
|
+
...sid === undefined ? {} : { spanId: sid },
|
|
1044
|
+
...traceFlags === undefined ? {} : { traceFlags: traceFlags & 255 }
|
|
1045
|
+
};
|
|
1046
|
+
});
|
|
1047
|
+
var numberPoint = message((f, path) => {
|
|
1048
|
+
const asInt = f.opt("asInt", int64, undefined);
|
|
1049
|
+
const asDouble = f.opt("asDouble", double, undefined);
|
|
1050
|
+
const value = asInt ?? asDouble;
|
|
1051
|
+
if (value === undefined)
|
|
1052
|
+
throw new Failure(failure(`${path}.asDouble`, "asInt or asDouble", undefined));
|
|
1053
|
+
return {
|
|
1054
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
1055
|
+
startTime: f.opt("startTimeUnixNano", int64, 0n),
|
|
1056
|
+
time: f.opt("timeUnixNano", int64, 0n),
|
|
1057
|
+
value
|
|
1058
|
+
};
|
|
1059
|
+
});
|
|
1060
|
+
var histogramPoint = message((f, path) => {
|
|
1061
|
+
const sum = f.opt("sum", double, undefined);
|
|
1062
|
+
const min = f.opt("min", double, undefined);
|
|
1063
|
+
const max = f.opt("max", double, undefined);
|
|
1064
|
+
const bounds = f.opt("explicitBounds", list(double), []);
|
|
1065
|
+
const bucketCounts = f.opt("bucketCounts", list(count), []);
|
|
1066
|
+
if (bucketCounts.length !== 0 && bucketCounts.length !== bounds.length + 1) {
|
|
1067
|
+
throw new Failure(failure(`${path}.bucketCounts`, `${bounds.length + 1} bucket counts`, bucketCounts.length));
|
|
1068
|
+
}
|
|
1069
|
+
return {
|
|
1070
|
+
attributes: f.opt("attributes", decodeAttributes, {}),
|
|
1071
|
+
startTime: f.opt("startTimeUnixNano", int64, 0n),
|
|
1072
|
+
time: f.opt("timeUnixNano", int64, 0n),
|
|
1073
|
+
count: f.opt("count", count, 0),
|
|
1074
|
+
...sum === undefined ? {} : { sum },
|
|
1075
|
+
...min === undefined ? {} : { min },
|
|
1076
|
+
...max === undefined ? {} : { max },
|
|
1077
|
+
bounds,
|
|
1078
|
+
bucketCounts
|
|
1079
|
+
};
|
|
1080
|
+
});
|
|
1081
|
+
function temporalityOf(n) {
|
|
1082
|
+
return n === 1 ? "delta" : "cumulative";
|
|
1083
|
+
}
|
|
1084
|
+
var decodeMetric = message((f, path) => {
|
|
1085
|
+
const head = { name: f.opt("name", str, ""), description: f.opt("description", str, ""), unit: f.opt("unit", str, "") };
|
|
1086
|
+
const sum = f.opt("sum", message((g) => ({
|
|
1087
|
+
kind: "sum",
|
|
1088
|
+
points: g.opt("dataPoints", list(numberPoint), []),
|
|
1089
|
+
temporality: temporalityOf(g.opt("aggregationTemporality", temporalityEnum, 2)),
|
|
1090
|
+
monotonic: g.opt("isMonotonic", bool, false)
|
|
1091
|
+
})), undefined);
|
|
1092
|
+
if (sum !== undefined)
|
|
1093
|
+
return { ...head, data: sum };
|
|
1094
|
+
const gauge = f.opt("gauge", message((g) => ({ kind: "gauge", points: g.opt("dataPoints", list(numberPoint), []) })), undefined);
|
|
1095
|
+
if (gauge !== undefined)
|
|
1096
|
+
return { ...head, data: gauge };
|
|
1097
|
+
const histogram = f.opt("histogram", message((g) => ({
|
|
1098
|
+
kind: "histogram",
|
|
1099
|
+
points: g.opt("dataPoints", list(histogramPoint), []),
|
|
1100
|
+
temporality: temporalityOf(g.opt("aggregationTemporality", temporalityEnum, 2))
|
|
1101
|
+
})), undefined);
|
|
1102
|
+
if (histogram !== undefined)
|
|
1103
|
+
return { ...head, data: histogram };
|
|
1104
|
+
throw new Failure(failure(`${path}.sum`, "a sum, a gauge or a histogram", undefined));
|
|
1105
|
+
});
|
|
1106
|
+
// ../ts/kernel/palette.ts
|
|
1107
|
+
var ESC = String.fromCharCode(27);
|
|
1108
|
+
// ../ts/kernel/png.ts
|
|
1109
|
+
var MAX_PIXELS = 2 ** 24;
|
|
1110
|
+
var COLOUR_TYPES = new Map([
|
|
1111
|
+
[0, { samples: 1, depths: [1, 2, 4, 8, 16], name: "greyscale" }],
|
|
1112
|
+
[2, { samples: 3, depths: [8, 16], name: "RGB" }],
|
|
1113
|
+
[3, { samples: 1, depths: [1, 2, 4, 8], name: "palette" }],
|
|
1114
|
+
[4, { samples: 2, depths: [8, 16], name: "greyscale with alpha" }],
|
|
1115
|
+
[6, { samples: 4, depths: [8, 16], name: "RGBA" }]
|
|
1116
|
+
]);
|
|
1117
|
+
var PRECISION_BITS = 32 - 8 - 2;
|
|
1118
|
+
var ONE = 2 ** PRECISION_BITS;
|
|
1119
|
+
// ../ts/kernel/rdf-term.ts
|
|
1120
|
+
var SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
1121
|
+
var FORBIDDEN = new Set([...'<>"{}|^`\\']);
|
|
1122
|
+
function parseIri(text) {
|
|
1123
|
+
for (let at = 0;at < text.length; at++) {
|
|
1124
|
+
if (text.charCodeAt(at) <= 32 || FORBIDDEN.has(text.charAt(at)))
|
|
1125
|
+
return err({ kind: "forbidden-character", text, at });
|
|
1126
|
+
}
|
|
1127
|
+
if (!SCHEME.test(text))
|
|
1128
|
+
return err({ kind: "not-absolute", text });
|
|
1129
|
+
return ok(text);
|
|
1130
|
+
}
|
|
1131
|
+
function formatIriError(e) {
|
|
1132
|
+
switch (e.kind) {
|
|
1133
|
+
case "not-absolute":
|
|
1134
|
+
return `${JSON.stringify(e.text)} is not an absolute IRI: it has no scheme`;
|
|
1135
|
+
case "forbidden-character":
|
|
1136
|
+
return `${JSON.stringify(e.text)} is not an IRI: the character at ${e.at} may not be in one`;
|
|
1137
|
+
default:
|
|
1138
|
+
return absurd(e);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
class RdfFault extends Error {
|
|
1143
|
+
constructor(message) {
|
|
1144
|
+
super(message);
|
|
1145
|
+
this.name = "RdfFault";
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
function checked(text) {
|
|
1149
|
+
const r = parseIri(text);
|
|
1150
|
+
if (r.kind === "err")
|
|
1151
|
+
throw new RdfFault(formatIriError(r.error));
|
|
1152
|
+
return r.value;
|
|
1153
|
+
}
|
|
1154
|
+
function named(iri) {
|
|
1155
|
+
return { kind: "named", iri };
|
|
1156
|
+
}
|
|
1157
|
+
var PREFIX = /^(?:[A-Za-z][A-Za-z0-9_-]*)?$/;
|
|
1158
|
+
function namespace(prefix, iri) {
|
|
1159
|
+
if (!PREFIX.test(prefix))
|
|
1160
|
+
throw new RdfFault(`${JSON.stringify(prefix)} is not a prefix`);
|
|
1161
|
+
return { prefix, iri: checked(iri) };
|
|
1162
|
+
}
|
|
1163
|
+
function iriIn(ns, local) {
|
|
1164
|
+
return checked(`${ns.iri}${local}`);
|
|
1165
|
+
}
|
|
1166
|
+
function vocabulary(ns, terms) {
|
|
1167
|
+
const out = {};
|
|
1168
|
+
for (const local of Object.keys(terms))
|
|
1169
|
+
out[local] = { kind: "named", iri: iriIn(ns, local), curie: `${ns.prefix}:${local}` };
|
|
1170
|
+
return out;
|
|
1171
|
+
}
|
|
1172
|
+
var XSD = "http://www.w3.org/2001/XMLSchema#";
|
|
1173
|
+
var XSD_STRING = `${XSD}string`;
|
|
1174
|
+
var XSD_INTEGER = `${XSD}integer`;
|
|
1175
|
+
var XSD_BOOLEAN = `${XSD}boolean`;
|
|
1176
|
+
var RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
|
|
1177
|
+
var ESCAPES = new Map([
|
|
1178
|
+
["\b", "\\b"],
|
|
1179
|
+
["\t", "\\t"],
|
|
1180
|
+
[`
|
|
1181
|
+
`, "\\n"],
|
|
1182
|
+
["\f", "\\f"],
|
|
1183
|
+
["\r", "\\r"],
|
|
1184
|
+
['"', "\\\""],
|
|
1185
|
+
["\\", "\\\\"]
|
|
1186
|
+
]);
|
|
1187
|
+
// ../ts/kernel/rdf-vocabularies.ts
|
|
1188
|
+
var RDF = namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
|
|
1189
|
+
var rdf = vocabulary(RDF, {
|
|
1190
|
+
type: "rdf:Property",
|
|
1191
|
+
first: "rdf:Property",
|
|
1192
|
+
rest: "rdf:Property",
|
|
1193
|
+
value: "rdf:Property",
|
|
1194
|
+
Property: "rdfs:Class",
|
|
1195
|
+
List: "rdfs:Class",
|
|
1196
|
+
nil: "rdf:List",
|
|
1197
|
+
langString: "rdfs:Datatype"
|
|
1198
|
+
});
|
|
1199
|
+
var RDFS = namespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
|
|
1200
|
+
var rdfs = vocabulary(RDFS, {
|
|
1201
|
+
Resource: "rdfs:Class",
|
|
1202
|
+
Class: "rdfs:Class",
|
|
1203
|
+
Datatype: "rdfs:Class",
|
|
1204
|
+
Literal: "rdfs:Class",
|
|
1205
|
+
label: "rdf:Property",
|
|
1206
|
+
comment: "rdf:Property",
|
|
1207
|
+
domain: "rdf:Property",
|
|
1208
|
+
range: "rdf:Property",
|
|
1209
|
+
subClassOf: "rdf:Property",
|
|
1210
|
+
subPropertyOf: "rdf:Property",
|
|
1211
|
+
seeAlso: "rdf:Property",
|
|
1212
|
+
isDefinedBy: "rdf:Property"
|
|
1213
|
+
});
|
|
1214
|
+
var OWL = namespace("owl", "http://www.w3.org/2002/07/owl#");
|
|
1215
|
+
var owl = vocabulary(OWL, {
|
|
1216
|
+
Ontology: "rdfs:Class",
|
|
1217
|
+
Class: "rdfs:Class",
|
|
1218
|
+
ObjectProperty: "rdfs:Class",
|
|
1219
|
+
DatatypeProperty: "rdfs:Class",
|
|
1220
|
+
AnnotationProperty: "rdfs:Class",
|
|
1221
|
+
NamedIndividual: "rdfs:Class",
|
|
1222
|
+
Thing: "rdfs:Class",
|
|
1223
|
+
versionInfo: "rdf:Property",
|
|
1224
|
+
versionIRI: "rdf:Property",
|
|
1225
|
+
imports: "rdf:Property",
|
|
1226
|
+
sameAs: "rdf:Property",
|
|
1227
|
+
equivalentClass: "rdf:Property"
|
|
1228
|
+
});
|
|
1229
|
+
var XSD2 = namespace("xsd", "http://www.w3.org/2001/XMLSchema#");
|
|
1230
|
+
var xsd = vocabulary(XSD2, {
|
|
1231
|
+
string: "rdfs:Datatype",
|
|
1232
|
+
boolean: "rdfs:Datatype",
|
|
1233
|
+
integer: "rdfs:Datatype",
|
|
1234
|
+
nonNegativeInteger: "rdfs:Datatype",
|
|
1235
|
+
positiveInteger: "rdfs:Datatype",
|
|
1236
|
+
decimal: "rdfs:Datatype",
|
|
1237
|
+
double: "rdfs:Datatype",
|
|
1238
|
+
date: "rdfs:Datatype",
|
|
1239
|
+
dateTime: "rdfs:Datatype",
|
|
1240
|
+
duration: "rdfs:Datatype",
|
|
1241
|
+
anyURI: "rdfs:Datatype",
|
|
1242
|
+
language: "rdfs:Datatype"
|
|
1243
|
+
});
|
|
1244
|
+
var SKOS = namespace("skos", "http://www.w3.org/2004/02/skos/core#");
|
|
1245
|
+
var skos = vocabulary(SKOS, {
|
|
1246
|
+
Concept: "rdfs:Class",
|
|
1247
|
+
ConceptScheme: "rdfs:Class",
|
|
1248
|
+
Collection: "rdfs:Class",
|
|
1249
|
+
inScheme: "rdf:Property",
|
|
1250
|
+
hasTopConcept: "rdf:Property",
|
|
1251
|
+
topConceptOf: "rdf:Property",
|
|
1252
|
+
prefLabel: "rdf:Property",
|
|
1253
|
+
altLabel: "rdf:Property",
|
|
1254
|
+
definition: "rdf:Property",
|
|
1255
|
+
notation: "rdf:Property",
|
|
1256
|
+
broader: "rdf:Property",
|
|
1257
|
+
narrower: "rdf:Property",
|
|
1258
|
+
related: "rdf:Property",
|
|
1259
|
+
note: "rdf:Property",
|
|
1260
|
+
scopeNote: "rdf:Property",
|
|
1261
|
+
example: "rdf:Property"
|
|
1262
|
+
});
|
|
1263
|
+
var DCTERMS = namespace("dcterms", "http://purl.org/dc/terms/");
|
|
1264
|
+
var dcterms = vocabulary(DCTERMS, {
|
|
1265
|
+
identifier: "rdf:Property",
|
|
1266
|
+
title: "rdf:Property",
|
|
1267
|
+
description: "rdf:Property",
|
|
1268
|
+
source: "rdf:Property",
|
|
1269
|
+
license: "rdf:Property",
|
|
1270
|
+
created: "rdf:Property",
|
|
1271
|
+
modified: "rdf:Property",
|
|
1272
|
+
date: "rdf:Property",
|
|
1273
|
+
creator: "rdf:Property",
|
|
1274
|
+
publisher: "rdf:Property",
|
|
1275
|
+
isPartOf: "rdf:Property",
|
|
1276
|
+
hasPart: "rdf:Property",
|
|
1277
|
+
references: "rdf:Property",
|
|
1278
|
+
conformsTo: "rdf:Property",
|
|
1279
|
+
Standard: "rdfs:Class"
|
|
1280
|
+
});
|
|
1281
|
+
var PROV = namespace("prov", "http://www.w3.org/ns/prov#");
|
|
1282
|
+
var prov = vocabulary(PROV, {
|
|
1283
|
+
Entity: "rdfs:Class",
|
|
1284
|
+
Activity: "rdfs:Class",
|
|
1285
|
+
Agent: "rdfs:Class",
|
|
1286
|
+
Person: "rdfs:Class",
|
|
1287
|
+
Organization: "rdfs:Class",
|
|
1288
|
+
SoftwareAgent: "rdfs:Class",
|
|
1289
|
+
wasGeneratedBy: "rdf:Property",
|
|
1290
|
+
wasDerivedFrom: "rdf:Property",
|
|
1291
|
+
wasAttributedTo: "rdf:Property",
|
|
1292
|
+
wasAssociatedWith: "rdf:Property",
|
|
1293
|
+
wasInformedBy: "rdf:Property",
|
|
1294
|
+
actedOnBehalfOf: "rdf:Property",
|
|
1295
|
+
used: "rdf:Property",
|
|
1296
|
+
startedAtTime: "rdf:Property",
|
|
1297
|
+
endedAtTime: "rdf:Property",
|
|
1298
|
+
generatedAtTime: "rdf:Property",
|
|
1299
|
+
atLocation: "rdf:Property",
|
|
1300
|
+
value: "rdf:Property"
|
|
1301
|
+
});
|
|
1302
|
+
var SH = namespace("sh", "http://www.w3.org/ns/shacl#");
|
|
1303
|
+
var sh = vocabulary(SH, {
|
|
1304
|
+
NodeShape: "rdfs:Class",
|
|
1305
|
+
PropertyShape: "rdfs:Class",
|
|
1306
|
+
targetClass: "rdf:Property",
|
|
1307
|
+
property: "rdf:Property",
|
|
1308
|
+
path: "rdf:Property",
|
|
1309
|
+
datatype: "rdf:Property",
|
|
1310
|
+
class: "rdf:Property",
|
|
1311
|
+
node: "rdf:Property",
|
|
1312
|
+
minCount: "rdf:Property",
|
|
1313
|
+
maxCount: "rdf:Property",
|
|
1314
|
+
pattern: "rdf:Property",
|
|
1315
|
+
in: "rdf:Property",
|
|
1316
|
+
order: "rdf:Property",
|
|
1317
|
+
name: "rdf:Property",
|
|
1318
|
+
description: "rdf:Property"
|
|
1319
|
+
});
|
|
1320
|
+
// ../ts/kernel/rdf-schema.ts
|
|
1321
|
+
function rangeOf(kind, words) {
|
|
1322
|
+
return words === undefined ? { kind } : { kind, words };
|
|
1323
|
+
}
|
|
1324
|
+
var range = {
|
|
1325
|
+
string: rangeOf("string"),
|
|
1326
|
+
langString: rangeOf("lang"),
|
|
1327
|
+
text: rangeOf("text"),
|
|
1328
|
+
boolean: rangeOf("boolean"),
|
|
1329
|
+
integer: rangeOf("integer"),
|
|
1330
|
+
ref: () => rangeOf("ref"),
|
|
1331
|
+
oneOf: (words) => rangeOf("string", words)
|
|
1332
|
+
};
|
|
1333
|
+
function one(property, r) {
|
|
1334
|
+
return { property, range: r, cardinality: "one" };
|
|
1335
|
+
}
|
|
1336
|
+
function optional(property, r) {
|
|
1337
|
+
return { property, range: r, cardinality: "optional" };
|
|
1338
|
+
}
|
|
1339
|
+
function many(property, r) {
|
|
1340
|
+
return { property, range: r, cardinality: "many" };
|
|
1341
|
+
}
|
|
1342
|
+
function isValue(x) {
|
|
1343
|
+
return typeof x === "string" || typeof x === "number" || typeof x === "boolean" || typeof x === "object" && x !== null && "kind" in x;
|
|
1344
|
+
}
|
|
1345
|
+
function valuesIn(x) {
|
|
1346
|
+
if (x === undefined)
|
|
1347
|
+
return [];
|
|
1348
|
+
if (Array.isArray(x))
|
|
1349
|
+
return x.filter(isValue);
|
|
1350
|
+
return isValue(x) ? [x] : [];
|
|
1351
|
+
}
|
|
1352
|
+
function classSchema(cls, fields) {
|
|
1353
|
+
const klass = named(cls.iri);
|
|
1354
|
+
return {
|
|
1355
|
+
class: klass,
|
|
1356
|
+
curie: cls.curie,
|
|
1357
|
+
fields,
|
|
1358
|
+
make: (subject, values) => {
|
|
1359
|
+
const own = values;
|
|
1360
|
+
const statements = [{ predicate: named(RDF_TYPE), objects: [klass] }];
|
|
1361
|
+
for (const [name, f] of Object.entries(fields)) {
|
|
1362
|
+
const objects = valuesIn(own[name]);
|
|
1363
|
+
if (objects.length === 0)
|
|
1364
|
+
continue;
|
|
1365
|
+
statements.push(f.range.words === undefined ? { predicate: f.property, objects } : { predicate: f.property, objects, words: f.range.words });
|
|
1366
|
+
}
|
|
1367
|
+
const made = { ...named(subject.iri), class: klass, values, statements };
|
|
1368
|
+
return made;
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
var OwlOntology = classSchema(owl.Ontology, {
|
|
1373
|
+
label: optional(rdfs.label, range.langString),
|
|
1374
|
+
comment: optional(rdfs.comment, range.langString),
|
|
1375
|
+
description: optional(dcterms.description, range.langString),
|
|
1376
|
+
versionInfo: optional(owl.versionInfo, range.string),
|
|
1377
|
+
versionIri: optional(owl.versionIRI, range.ref())
|
|
1378
|
+
});
|
|
1379
|
+
var OwlClass = classSchema(owl.Class, {
|
|
1380
|
+
label: optional(rdfs.label, range.langString),
|
|
1381
|
+
comment: optional(rdfs.comment, range.langString),
|
|
1382
|
+
subClassOf: many(rdfs.subClassOf, range.ref()),
|
|
1383
|
+
definition: optional(skos.definition, range.langString)
|
|
1384
|
+
});
|
|
1385
|
+
var PROPERTY = {
|
|
1386
|
+
label: optional(rdfs.label, range.langString),
|
|
1387
|
+
comment: optional(rdfs.comment, range.langString),
|
|
1388
|
+
domain: optional(rdfs.domain, range.ref()),
|
|
1389
|
+
range: optional(rdfs.range, range.ref()),
|
|
1390
|
+
definition: optional(skos.definition, range.langString)
|
|
1391
|
+
};
|
|
1392
|
+
var ObjectProperty = classSchema(owl.ObjectProperty, PROPERTY);
|
|
1393
|
+
var DatatypeProperty = classSchema(owl.DatatypeProperty, PROPERTY);
|
|
1394
|
+
var ConceptSchemeSchema = classSchema(skos.ConceptScheme, {
|
|
1395
|
+
prefLabel: one(skos.prefLabel, range.langString),
|
|
1396
|
+
definition: optional(skos.definition, range.langString)
|
|
1397
|
+
});
|
|
1398
|
+
var ConceptSchema = classSchema(skos.Concept, {
|
|
1399
|
+
inScheme: one(skos.inScheme, range.ref()),
|
|
1400
|
+
notation: one(skos.notation, range.string),
|
|
1401
|
+
prefLabel: one(skos.prefLabel, range.langString),
|
|
1402
|
+
definition: optional(skos.definition, range.langString)
|
|
1403
|
+
});
|
|
1404
|
+
// ../ts/kernel/signals.ts
|
|
1405
|
+
var ATTRIBUTE_TYPES = new Set([
|
|
1406
|
+
"string",
|
|
1407
|
+
"boolean",
|
|
1408
|
+
"int",
|
|
1409
|
+
"double",
|
|
1410
|
+
"bytes",
|
|
1411
|
+
"string[]",
|
|
1412
|
+
"boolean[]",
|
|
1413
|
+
"int[]",
|
|
1414
|
+
"double[]"
|
|
1415
|
+
]);
|
|
1416
|
+
function defineSignals(kinds) {
|
|
1417
|
+
for (const [name, d] of Object.entries(kinds)) {
|
|
1418
|
+
const declared = new Set(Object.keys(d.attributes).map((k) => k.replace(/\?$/, "")));
|
|
1419
|
+
for (const [k, t] of Object.entries(d.attributes)) {
|
|
1420
|
+
if (!ATTRIBUTE_TYPES.has(t))
|
|
1421
|
+
throw new Error(`${name}: the attribute ${k} has the type ${t}, which does not exist`);
|
|
1422
|
+
}
|
|
1423
|
+
for (const s of d.secret ?? [])
|
|
1424
|
+
if (!declared.has(s))
|
|
1425
|
+
throw new Error(`${name}: the secret attribute ${s} is not declared`);
|
|
1426
|
+
if (d.signal === "histogram" && d.bounds !== undefined) {
|
|
1427
|
+
const b = d.bounds;
|
|
1428
|
+
if (b.some((x, i) => !Number.isFinite(x) || i > 0 && x <= (b[i - 1] ?? -Infinity))) {
|
|
1429
|
+
throw new Error(`${name}: the histogram bounds do not increase`);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
return { kinds };
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
// ../ts/kernel/width.ts
|
|
1437
|
+
var ESC2 = String.fromCharCode(27);
|
|
1438
|
+
var BEL = String.fromCharCode(7);
|
|
1439
|
+
var ESCAPE = new RegExp(`${ESC2}(?:\\[[0-?]*[ -/]*[@-~]?|[\\]PX^_][^${BEL}${ESC2}]*(?:${BEL}|${ESC2}\\\\)?|[ -/]*[0-~]|)`, "g");
|
|
1440
|
+
var segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
1441
|
+
var SGR = new RegExp(`^${ESC2}\\[([0-9;:]*)m$`);
|
|
1442
|
+
var RESET = `${ESC2}[0m`;
|
|
1443
|
+
|
|
1444
|
+
// ../ts/kernel/render.ts
|
|
1445
|
+
var ESC3 = String.fromCharCode(27);
|
|
1446
|
+
var CONTROL = new RegExp(`[${String.fromCharCode(0)}-${String.fromCharCode(31)}${String.fromCharCode(127)}-${String.fromCharCode(159)}]`, "g");
|
|
1447
|
+
// ../ts/kernel/xml.ts
|
|
1448
|
+
var FORBIDDEN2 = new RegExp(`[${String.fromCharCode(0)}-${String.fromCharCode(8)}${String.fromCharCode(11, 12)}${String.fromCharCode(14)}-${String.fromCharCode(31)}\\ufffe\\uffff]`, "g");
|
|
1449
|
+
// ../ts/kernel/sigv4.ts
|
|
1450
|
+
var UNSIGNED_HEADERS = new Set(["authorization", "connection", "expect", "user-agent", "x-amzn-trace-id", "traceparent", "tracestate"]);
|
|
1451
|
+
var encoder = new TextEncoder;
|
|
1452
|
+
// ../ts/kernel/test-report.ts
|
|
1453
|
+
var STYLE = "body{font:15px/1.5 system-ui,sans-serif;margin:2rem auto;max-width:72rem;padding:0 1rem;color:#1b1b1b;background:#fff}" + "table{border-collapse:collapse;margin:.5rem 0;width:100%}th,td{border:1px solid #c8c8c8;padding:.25rem .5rem;text-align:left;vertical-align:top;overflow-wrap:anywhere}" + "th{background:#f0f0f0}h1{font-size:1.6rem}h2{font-size:1.2rem;margin-top:2rem}" + "@media (prefers-color-scheme:dark){body{color:#e8e8e8;background:#161616}th{background:#2a2a2a}th,td{border-color:#444}}";
|
|
1454
|
+
// ../ts/kernel/toml.ts
|
|
1455
|
+
var CONTROL2 = new RegExp(`[${String.fromCharCode(0)}-${String.fromCharCode(8, 10)}-${String.fromCharCode(31, 127)}]`);
|
|
1456
|
+
// ../ts/kernel/webauthn.ts
|
|
1457
|
+
var clientData = object({ type: string, challenge: string, origin: string }, { crossOrigin: boolean, topOrigin: string });
|
|
1458
|
+
var kind = literal("public-key");
|
|
1459
|
+
var extensionResults = record(unknownValue);
|
|
1460
|
+
// src/portable.ts
|
|
1461
|
+
function describePortableError(e) {
|
|
1462
|
+
switch (e.kind) {
|
|
1463
|
+
case "empty":
|
|
1464
|
+
return "a path segment is empty";
|
|
1465
|
+
case "dot":
|
|
1466
|
+
return `the path segment ${JSON.stringify(e.segment)} would leave its folder`;
|
|
1467
|
+
case "forbidden-character":
|
|
1468
|
+
return `the path segment ${JSON.stringify(e.segment)} holds ${JSON.stringify(e.character)}, which Windows does not allow in a name`;
|
|
1469
|
+
case "control-character":
|
|
1470
|
+
return `the path segment ${JSON.stringify(e.segment)} holds a control character`;
|
|
1471
|
+
case "trailing-dot-or-space":
|
|
1472
|
+
return `the path segment ${JSON.stringify(e.segment)} ends in a dot or a space, which Windows drops`;
|
|
1473
|
+
case "device-name":
|
|
1474
|
+
return `the path segment ${JSON.stringify(e.segment)} is the Windows device name ${e.device}, with or without an extension, in any case`;
|
|
1475
|
+
case "too-long":
|
|
1476
|
+
return `the path segment is ${e.bytes} bytes in UTF-8, and a name may have 255`;
|
|
1477
|
+
case "not-nfc":
|
|
1478
|
+
return `the path segment ${JSON.stringify(e.segment)} is not in Unicode normalization form NFC, so it would be another name on macOS`;
|
|
1479
|
+
default:
|
|
1480
|
+
return absurd(e);
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
var FORBIDDEN3 = new Set(["<", ">", ":", '"', "/", "\\", "|", "?", "*"]);
|
|
1484
|
+
var DEVICE = /^(?:CON|PRN|AUX|NUL|CONIN\$|CONOUT\$|COM[0-9\u00b9\u00b2\u00b3]|LPT[0-9\u00b9\u00b2\u00b3])$/i;
|
|
1485
|
+
function isDeviceName(name) {
|
|
1486
|
+
return DEVICE.test((name.split(".")[0] ?? "").replace(/ +$/, ""));
|
|
1487
|
+
}
|
|
1488
|
+
var MAX_SEGMENT_BYTES = 255;
|
|
1489
|
+
function segment(text) {
|
|
1490
|
+
if (text === "")
|
|
1491
|
+
return err({ kind: "empty" });
|
|
1492
|
+
if (text === "." || text === "..")
|
|
1493
|
+
return err({ kind: "dot", segment: text });
|
|
1494
|
+
for (const ch of text) {
|
|
1495
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
1496
|
+
if (code < 32 || code === 127)
|
|
1497
|
+
return err({ kind: "control-character", segment: text });
|
|
1498
|
+
if (FORBIDDEN3.has(ch))
|
|
1499
|
+
return err({ kind: "forbidden-character", segment: text, character: ch });
|
|
1500
|
+
}
|
|
1501
|
+
if (text.endsWith(".") || text.endsWith(" "))
|
|
1502
|
+
return err({ kind: "trailing-dot-or-space", segment: text });
|
|
1503
|
+
const stem = (text.split(".")[0] ?? "").replace(/ +$/, "");
|
|
1504
|
+
if (isDeviceName(text))
|
|
1505
|
+
return err({
|
|
1506
|
+
kind: "device-name",
|
|
1507
|
+
segment: text,
|
|
1508
|
+
device: stem.toUpperCase()
|
|
1509
|
+
});
|
|
1510
|
+
const bytes = new TextEncoder().encode(text).length;
|
|
1511
|
+
if (bytes > MAX_SEGMENT_BYTES)
|
|
1512
|
+
return err({ kind: "too-long", segment: text.slice(0, 40), bytes });
|
|
1513
|
+
if (text !== text.normalize("NFC"))
|
|
1514
|
+
return err({ kind: "not-nfc", segment: text });
|
|
1515
|
+
return ok(text);
|
|
1516
|
+
}
|
|
1517
|
+
function repoPath(segments) {
|
|
1518
|
+
if (segments.length === 0)
|
|
1519
|
+
return err({ kind: "empty" });
|
|
1520
|
+
const made = [];
|
|
1521
|
+
for (const s of segments) {
|
|
1522
|
+
const r = segment(s);
|
|
1523
|
+
if (r.kind === "err")
|
|
1524
|
+
return r;
|
|
1525
|
+
made.push(r.value);
|
|
1526
|
+
}
|
|
1527
|
+
return ok(made.join("/"));
|
|
1528
|
+
}
|
|
1529
|
+
function segmentsOf(path) {
|
|
1530
|
+
return path.split("/");
|
|
1531
|
+
}
|
|
1532
|
+
function child2(path, name) {
|
|
1533
|
+
return `${path}/${name}`;
|
|
1534
|
+
}
|
|
1535
|
+
function nativePath(root, path, separator) {
|
|
1536
|
+
const base = root.replace(/[\\/]+$/, "");
|
|
1537
|
+
return [base, ...segmentsOf(path)].join(separator);
|
|
1538
|
+
}
|
|
1539
|
+
function foldKey(path) {
|
|
1540
|
+
return path.normalize("NFC").toUpperCase().toLowerCase().normalize("NFC");
|
|
1541
|
+
}
|
|
1542
|
+
function collisions(items) {
|
|
1543
|
+
const groups = new Map;
|
|
1544
|
+
for (const item of items) {
|
|
1545
|
+
const key = foldKey(item.path);
|
|
1546
|
+
const group = groups.get(key);
|
|
1547
|
+
if (group === undefined)
|
|
1548
|
+
groups.set(key, [{ ...item }]);
|
|
1549
|
+
else
|
|
1550
|
+
group.push({ ...item });
|
|
1551
|
+
}
|
|
1552
|
+
return [...groups.entries()].filter(([, members]) => members.length > 1).map(([key, members]) => ({ key, members }));
|
|
1553
|
+
}
|
|
1554
|
+
var WINDOWS_PATH_LIMIT = 259;
|
|
1555
|
+
function checkPathLength(windowsPath, longPathsEnabled) {
|
|
1556
|
+
if (longPathsEnabled || windowsPath.length <= WINDOWS_PATH_LIMIT)
|
|
1557
|
+
return ok(windowsPath);
|
|
1558
|
+
return err({
|
|
1559
|
+
kind: "path-too-long",
|
|
1560
|
+
path: windowsPath,
|
|
1561
|
+
length: windowsPath.length,
|
|
1562
|
+
limit: WINDOWS_PATH_LIMIT
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
var REPO_PATH_ROOM = 150;
|
|
1566
|
+
function repoPathLength(root, path, separator) {
|
|
1567
|
+
const length = nativePath(root, path, separator).length;
|
|
1568
|
+
if (length > WINDOWS_PATH_LIMIT)
|
|
1569
|
+
return { kind: "too-long", length };
|
|
1570
|
+
if (length > REPO_PATH_ROOM)
|
|
1571
|
+
return { kind: "no-room", length };
|
|
1572
|
+
return { kind: "fits", length };
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
// src/render.ts
|
|
1576
|
+
function describeGenerateError(e) {
|
|
1577
|
+
switch (e.kind) {
|
|
1578
|
+
case "bad-path":
|
|
1579
|
+
return `${JSON.stringify(e.path)} cannot be the path of a generated file: ${e.reason}`;
|
|
1580
|
+
case "not-auto":
|
|
1581
|
+
return `${e.path} is a generated file whose name does not carry .auto. (only a Git hook in .ergonex/hooks/ may lack it)`;
|
|
1582
|
+
case "collision":
|
|
1583
|
+
return `${e.path} is the same path as ${e.other} after Unicode normalization and case folding; both are refused`;
|
|
1584
|
+
case "inside-repository":
|
|
1585
|
+
return `${e.path} is inside the working tree of ${e.repository}; a generated file is never written into a repository`;
|
|
1586
|
+
case "bad-data":
|
|
1587
|
+
return `${e.what}: ${e.reason}`;
|
|
1588
|
+
default:
|
|
1589
|
+
return absurd(e);
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
var HOOKS_FOLDER = ".ergonex/hooks";
|
|
1593
|
+
function headerText(generator) {
|
|
1594
|
+
return `Generated by ${generator}. Do not edit by hand: change workspace.ts and run \`ergonex render\`.`;
|
|
1595
|
+
}
|
|
1596
|
+
function normalizeText(text) {
|
|
1597
|
+
const lf = text.replace(/^/, "").replaceAll(`\r
|
|
1598
|
+
`, `
|
|
1599
|
+
`).replaceAll("\r", `
|
|
1600
|
+
`);
|
|
1601
|
+
return `${lf.replace(/\n+$/, "")}
|
|
1602
|
+
`;
|
|
1603
|
+
}
|
|
1604
|
+
function isAutoName(name) {
|
|
1605
|
+
return name.includes(".auto.") || name.endsWith(".auto");
|
|
1606
|
+
}
|
|
1607
|
+
function generatedPath(path) {
|
|
1608
|
+
const made = repoPath(path.split("/"));
|
|
1609
|
+
if (made.kind === "err")
|
|
1610
|
+
return err({
|
|
1611
|
+
kind: "bad-path",
|
|
1612
|
+
path,
|
|
1613
|
+
reason: describePortableError(made.error)
|
|
1614
|
+
});
|
|
1615
|
+
return made;
|
|
1616
|
+
}
|
|
1617
|
+
var noBom = (text) => text.replace(/^\uFEFF/, "");
|
|
1618
|
+
function contentOf(body, generator) {
|
|
1619
|
+
const header = headerText(generator);
|
|
1620
|
+
switch (body.format) {
|
|
1621
|
+
case "json":
|
|
1622
|
+
return normalizeText(JSON.stringify({ _generated: header, ...body.value }, null, 2));
|
|
1623
|
+
case "markdown":
|
|
1624
|
+
return normalizeText(`<!-- ${header} -->
|
|
1625
|
+
|
|
1626
|
+
${noBom(body.text)}`);
|
|
1627
|
+
case "yaml":
|
|
1628
|
+
case "gitconfig":
|
|
1629
|
+
case "envrc":
|
|
1630
|
+
return normalizeText(`# ${header}
|
|
1631
|
+
|
|
1632
|
+
${noBom(body.text)}`);
|
|
1633
|
+
case "sh": {
|
|
1634
|
+
const text = noBom(body.text);
|
|
1635
|
+
const m = /^(#![^\n]*)\n/.exec(text);
|
|
1636
|
+
return m === null ? normalizeText(`# ${header}
|
|
1637
|
+
${text}`) : normalizeText(`${m[1]}
|
|
1638
|
+
# ${header}
|
|
1639
|
+
${text.slice(m[0].length)}`);
|
|
1640
|
+
}
|
|
1641
|
+
default:
|
|
1642
|
+
return absurd(body);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
function generated(path, body, options) {
|
|
1646
|
+
const made = generatedPath(path);
|
|
1647
|
+
if (made.kind === "err")
|
|
1648
|
+
return made;
|
|
1649
|
+
const name = made.value.split("/").at(-1) ?? "";
|
|
1650
|
+
const isHook = made.value.startsWith(`${HOOKS_FOLDER}/`) && made.value.split("/").length === 3;
|
|
1651
|
+
if (!isAutoName(name) && !isHook)
|
|
1652
|
+
return err({ kind: "not-auto", path });
|
|
1653
|
+
return ok({
|
|
1654
|
+
path: made.value,
|
|
1655
|
+
content: contentOf(body, options.generator),
|
|
1656
|
+
mode: options.mode ?? "0644",
|
|
1657
|
+
generator: options.generator
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
function fileDigest(file) {
|
|
1661
|
+
return sha256(file.content);
|
|
1662
|
+
}
|
|
1663
|
+
function checkFiles(files, repositories) {
|
|
1664
|
+
const errors = [];
|
|
1665
|
+
for (const c of collisions(files.map((f) => ({ id: f.path, path: f.path })))) {
|
|
1666
|
+
for (const m of c.members) {
|
|
1667
|
+
const other = c.members.find((o) => o !== m);
|
|
1668
|
+
errors.push({
|
|
1669
|
+
kind: "collision",
|
|
1670
|
+
path: m.path,
|
|
1671
|
+
other: other?.path ?? m.path
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
for (const f of files) {
|
|
1676
|
+
const key = foldKey(f.path);
|
|
1677
|
+
const inside = repositories.find((r) => key.startsWith(`${foldKey(r)}/`));
|
|
1678
|
+
if (inside !== undefined)
|
|
1679
|
+
errors.push({
|
|
1680
|
+
kind: "inside-repository",
|
|
1681
|
+
path: f.path,
|
|
1682
|
+
repository: inside
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
if (errors.length > 0)
|
|
1686
|
+
return err(errors);
|
|
1687
|
+
return ok([...files].sort((a, b) => foldKey(a.path) < foldKey(b.path) ? -1 : foldKey(a.path) > foldKey(b.path) ? 1 : 0));
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
// src/desired.ts
|
|
1691
|
+
function settingsOf(manifest, facts) {
|
|
1692
|
+
const settings = {
|
|
1693
|
+
"core.hooksPath": `${facts.homePosix.replace(/\/+$/, "")}/.ergonex/hooks`
|
|
1694
|
+
};
|
|
1695
|
+
if (manifest.policy.requiresSignedCommits) {
|
|
1696
|
+
settings["commit.gpgsign"] = "true";
|
|
1697
|
+
if (manifest.signing !== undefined) {
|
|
1698
|
+
settings["gpg.format"] = manifest.signing.format;
|
|
1699
|
+
if (manifest.signing.key !== undefined)
|
|
1700
|
+
settings["user.signingkey"] = manifest.signing.key;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
if (manifest.identity !== undefined) {
|
|
1704
|
+
settings["user.name"] = manifest.identity.name;
|
|
1705
|
+
settings["user.email"] = manifest.identity.email;
|
|
1706
|
+
}
|
|
1707
|
+
if (facts.windows)
|
|
1708
|
+
settings["core.longpaths"] = "true";
|
|
1709
|
+
return settings;
|
|
1710
|
+
}
|
|
1711
|
+
async function desiredOf(manifest, files, facts) {
|
|
1712
|
+
const settings = settingsOf(manifest, facts);
|
|
1713
|
+
const repos = manifest.repositories.map((r) => ({
|
|
1714
|
+
path: r.path,
|
|
1715
|
+
ref: r.ref,
|
|
1716
|
+
cloneUrl: r.cloneUrl,
|
|
1717
|
+
branch: r.branch,
|
|
1718
|
+
settings
|
|
1719
|
+
}));
|
|
1720
|
+
const worktrees = manifest.repositories.flatMap((r) => r.worktrees.map((w) => ({ path: w.path, repo: r.path, branch: w.branch })));
|
|
1721
|
+
const desiredFiles = [];
|
|
1722
|
+
for (const f of files)
|
|
1723
|
+
desiredFiles.push({ path: f.path, sha256: await fileDigest(f) });
|
|
1724
|
+
return { repos, worktrees, files: desiredFiles, hostChanges: [] };
|
|
1725
|
+
}
|
|
1726
|
+
async function digestsOf(files) {
|
|
1727
|
+
const out = new Map;
|
|
1728
|
+
for (const f of files)
|
|
1729
|
+
out.set(f.path, await fileDigest(f));
|
|
1730
|
+
return out;
|
|
1731
|
+
}
|
|
1732
|
+
// src/doctor.ts
|
|
1733
|
+
function severityOf(level) {
|
|
1734
|
+
switch (level) {
|
|
1735
|
+
case "ok":
|
|
1736
|
+
return;
|
|
1737
|
+
case "suggest":
|
|
1738
|
+
return "Informational";
|
|
1739
|
+
case "warn":
|
|
1740
|
+
return "Warning";
|
|
1741
|
+
case "error":
|
|
1742
|
+
return "Error";
|
|
1743
|
+
default:
|
|
1744
|
+
return absurd(level);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
function findingsOf(checks, today) {
|
|
1748
|
+
return checks.flatMap((c) => {
|
|
1749
|
+
const severity = severityOf(c.level);
|
|
1750
|
+
return severity === undefined ? [] : [
|
|
1751
|
+
{
|
|
1752
|
+
ruleId: c.id,
|
|
1753
|
+
severity,
|
|
1754
|
+
message: c.message,
|
|
1755
|
+
subject: c.subject,
|
|
1756
|
+
firstSeen: today
|
|
1757
|
+
}
|
|
1758
|
+
];
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
function classifyFs(type, wsl) {
|
|
1762
|
+
const t = type.toLowerCase();
|
|
1763
|
+
if (["msdos", "vfat", "fat", "fat32", "fat16"].includes(t))
|
|
1764
|
+
return "fat";
|
|
1765
|
+
if (t === "exfat")
|
|
1766
|
+
return "exfat";
|
|
1767
|
+
if (["9p", "drvfs", "v9fs"].includes(t) && wsl)
|
|
1768
|
+
return "wsl-windows-drive";
|
|
1769
|
+
if ([
|
|
1770
|
+
"nfs",
|
|
1771
|
+
"nfs3",
|
|
1772
|
+
"nfs4",
|
|
1773
|
+
"cifs",
|
|
1774
|
+
"smb",
|
|
1775
|
+
"smb2",
|
|
1776
|
+
"smb3",
|
|
1777
|
+
"smbfs",
|
|
1778
|
+
"afpfs",
|
|
1779
|
+
"9p",
|
|
1780
|
+
"v9fs",
|
|
1781
|
+
"sshfs",
|
|
1782
|
+
"fuse.sshfs",
|
|
1783
|
+
"webdav",
|
|
1784
|
+
"ncpfs",
|
|
1785
|
+
"fuse.rclone"
|
|
1786
|
+
].includes(t))
|
|
1787
|
+
return "network";
|
|
1788
|
+
return "ok";
|
|
1789
|
+
}
|
|
1790
|
+
function fsCheck(probe, home) {
|
|
1791
|
+
if (probe.kind === "not-run")
|
|
1792
|
+
return {
|
|
1793
|
+
id: "ergonex/filesystem",
|
|
1794
|
+
level: "suggest",
|
|
1795
|
+
subject: home,
|
|
1796
|
+
message: `the file system of the root was not probed: ${probe.reason}`
|
|
1797
|
+
};
|
|
1798
|
+
const kind = classifyFs(probe.type, probe.wsl);
|
|
1799
|
+
const at = (level, message) => ({
|
|
1800
|
+
id: "ergonex/filesystem",
|
|
1801
|
+
level,
|
|
1802
|
+
subject: home,
|
|
1803
|
+
message
|
|
1804
|
+
});
|
|
1805
|
+
switch (kind) {
|
|
1806
|
+
case "ok":
|
|
1807
|
+
return at("ok", `the root is on a file system of type ${probe.type}`);
|
|
1808
|
+
case "fat":
|
|
1809
|
+
return at("warn", `the root is on a FAT file system (${probe.type}): it has no execute bit and a two-second time resolution, so hooks may not run; move the root to NTFS, APFS or ext4`);
|
|
1810
|
+
case "exfat":
|
|
1811
|
+
return at("warn", "the root is on an exFAT file system: it has no execute bit and a two-second time resolution, so hooks may not run; move the root to NTFS, APFS or ext4");
|
|
1812
|
+
case "network":
|
|
1813
|
+
return at("warn", `the root is on a network share (${probe.type}): its locks are not reliable, so two runs may write at once; move the root to a local disk`);
|
|
1814
|
+
case "wsl-windows-drive":
|
|
1815
|
+
return at("warn", "the root is on a Windows drive seen from WSL (/mnt/c): it is slow and case-insensitive; keep the root in the Linux file system, or run ergonex from Windows");
|
|
1816
|
+
default:
|
|
1817
|
+
return absurd(kind);
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
var MIN_NODE_MAJOR = 26;
|
|
1821
|
+
function runtimeChecks(runtime, tools) {
|
|
1822
|
+
const out = [];
|
|
1823
|
+
const major = parseSemVer(runtime.version.replace(/^v/, ""));
|
|
1824
|
+
if (runtime.name === "node" && major.kind === "ok" && major.value.major < MIN_NODE_MAJOR)
|
|
1825
|
+
out.push({
|
|
1826
|
+
id: "ergonex/runtime",
|
|
1827
|
+
level: "error",
|
|
1828
|
+
subject: "node",
|
|
1829
|
+
message: `ergonex runs on Node ${MIN_NODE_MAJOR} or later, and this is Node ${runtime.version}; install Node ${MIN_NODE_MAJOR} or run ergonex with Bun`
|
|
1830
|
+
});
|
|
1831
|
+
else
|
|
1832
|
+
out.push({
|
|
1833
|
+
id: "ergonex/runtime",
|
|
1834
|
+
level: "ok",
|
|
1835
|
+
subject: runtime.name,
|
|
1836
|
+
message: `running on ${runtime.name} ${runtime.version}`
|
|
1837
|
+
});
|
|
1838
|
+
for (const t of tools) {
|
|
1839
|
+
if (t.version === undefined) {
|
|
1840
|
+
const needed = t.name === "git";
|
|
1841
|
+
const isRuntime = t.name === runtime.name;
|
|
1842
|
+
out.push({
|
|
1843
|
+
id: "ergonex/tool",
|
|
1844
|
+
level: needed ? "error" : isRuntime ? "ok" : "suggest",
|
|
1845
|
+
subject: t.name,
|
|
1846
|
+
message: needed ? `git is not installed or not on the PATH; ergonex needs it, and does not install it: install git from your system's package manager` : `${t.name} was not found on the PATH; ${t.name === "bun" ? "Bun is the default runtime" : `Node ${MIN_NODE_MAJOR} is the backup runtime`}, and ergonex does not install it: install it to have both`
|
|
1847
|
+
});
|
|
1848
|
+
} else
|
|
1849
|
+
out.push({
|
|
1850
|
+
id: "ergonex/tool",
|
|
1851
|
+
level: "ok",
|
|
1852
|
+
subject: t.name,
|
|
1853
|
+
message: `${t.name} ${t.version}`
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
return out;
|
|
1857
|
+
}
|
|
1858
|
+
function pathChecks(f) {
|
|
1859
|
+
if (!f.windows)
|
|
1860
|
+
return [
|
|
1861
|
+
{
|
|
1862
|
+
id: "ergonex/path-length",
|
|
1863
|
+
level: "ok",
|
|
1864
|
+
subject: f.home,
|
|
1865
|
+
message: "the 259-character limit of Windows does not apply on this system"
|
|
1866
|
+
}
|
|
1867
|
+
];
|
|
1868
|
+
const long = f.longPaths === "enabled";
|
|
1869
|
+
const tooLong = f.paths.flatMap((p) => {
|
|
1870
|
+
const native = nativePath(f.home, p, "\\");
|
|
1871
|
+
const r = checkPathLength(native, long);
|
|
1872
|
+
return r.kind === "err" ? [r.error] : [];
|
|
1873
|
+
});
|
|
1874
|
+
const out = tooLong.map((e) => ({
|
|
1875
|
+
id: "ergonex/path-too-long",
|
|
1876
|
+
level: "error",
|
|
1877
|
+
subject: e.path,
|
|
1878
|
+
message: `${e.path} is ${e.length} characters, over the ${WINDOWS_PATH_LIMIT} Windows allows while long paths are off; run \`ergonex setup\` to turn long paths on, or move the root to a shorter path`
|
|
1879
|
+
}));
|
|
1880
|
+
if (!long && tooLong.length === 0)
|
|
1881
|
+
out.push({
|
|
1882
|
+
id: "ergonex/windows-long-paths",
|
|
1883
|
+
level: "suggest",
|
|
1884
|
+
subject: f.home,
|
|
1885
|
+
message: f.longPaths === "unknown" ? "Windows long paths are not known to be enabled (the setting could not be read); no path of the workspace exceeds 259 characters yet; run `ergonex setup` to turn them on" : "Windows long paths not enabled; no path of the workspace exceeds 259 characters yet; run `ergonex setup` to turn them on"
|
|
1886
|
+
});
|
|
1887
|
+
if (long)
|
|
1888
|
+
out.push({
|
|
1889
|
+
id: "ergonex/windows-long-paths",
|
|
1890
|
+
level: "ok",
|
|
1891
|
+
subject: f.home,
|
|
1892
|
+
message: "Windows long paths are enabled"
|
|
1893
|
+
});
|
|
1894
|
+
return out;
|
|
1895
|
+
}
|
|
1896
|
+
function repoPathChecks(f) {
|
|
1897
|
+
const out = [];
|
|
1898
|
+
for (const repo of f.repositories) {
|
|
1899
|
+
const native = nativePath(f.home, repo, f.windows ? "\\" : "/");
|
|
1900
|
+
const r = repoPathLength(f.home, repo, f.windows ? "\\" : "/");
|
|
1901
|
+
switch (r.kind) {
|
|
1902
|
+
case "fits":
|
|
1903
|
+
break;
|
|
1904
|
+
case "no-room":
|
|
1905
|
+
out.push({
|
|
1906
|
+
id: "ergonex/repo-path-room",
|
|
1907
|
+
level: "suggest",
|
|
1908
|
+
subject: native,
|
|
1909
|
+
message: `${native} is ${r.length} characters, over ${REPO_PATH_ROOM}: that leaves little room for the files inside the repository; move the root to a shorter path`
|
|
1910
|
+
});
|
|
1911
|
+
break;
|
|
1912
|
+
case "too-long":
|
|
1913
|
+
out.push({
|
|
1914
|
+
id: "ergonex/repo-path-too-long",
|
|
1915
|
+
level: "error",
|
|
1916
|
+
subject: native,
|
|
1917
|
+
message: `${native} is ${r.length} characters, over ${WINDOWS_PATH_LIMIT}, which is too long for a repository whether or not long paths are enabled; move the root to a shorter path or shorten the repository's path in the manifest`
|
|
1918
|
+
});
|
|
1919
|
+
break;
|
|
1920
|
+
default:
|
|
1921
|
+
return absurd(r);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
return out;
|
|
1925
|
+
}
|
|
1926
|
+
function hookChecks(facts, wantedHooksPath) {
|
|
1927
|
+
return facts.filter((f) => f.present).map((f) => f.hooksPath === wantedHooksPath ? {
|
|
1928
|
+
id: "ergonex/hooks",
|
|
1929
|
+
level: "ok",
|
|
1930
|
+
subject: f.repo,
|
|
1931
|
+
message: "the workspace's hooks are set"
|
|
1932
|
+
} : {
|
|
1933
|
+
id: "ergonex/hooks",
|
|
1934
|
+
level: "warn",
|
|
1935
|
+
subject: f.repo,
|
|
1936
|
+
message: f.hooksPath === undefined ? `${f.repo} does not use the workspace's hooks (core.hooksPath is not set in its local configuration); run \`ergonex apply\`` : `${f.repo} uses other hooks than the workspace's (its core.hooksPath is elsewhere); run \`ergonex apply\` to replace it`
|
|
1937
|
+
});
|
|
1938
|
+
}
|
|
1939
|
+
// src/findings.ts
|
|
1940
|
+
var SEVERITIES2 = ["Informational", "Warning", "Error"];
|
|
1941
|
+
function sarifLevel(severity) {
|
|
1942
|
+
switch (severity) {
|
|
1943
|
+
case "Informational":
|
|
1944
|
+
return "note";
|
|
1945
|
+
case "Warning":
|
|
1946
|
+
return "warning";
|
|
1947
|
+
case "Error":
|
|
1948
|
+
return "error";
|
|
1949
|
+
default:
|
|
1950
|
+
return absurd(severity);
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
function worst(findings) {
|
|
1954
|
+
let top;
|
|
1955
|
+
for (const f of findings)
|
|
1956
|
+
if (top === undefined || SEVERITIES2.indexOf(f.severity) > SEVERITIES2.indexOf(top))
|
|
1957
|
+
top = f.severity;
|
|
1958
|
+
return top;
|
|
1959
|
+
}
|
|
1960
|
+
function exitStatusOf(findings) {
|
|
1961
|
+
const top = worst(findings);
|
|
1962
|
+
return top === "Warning" || top === "Error" ? "Findings" : "Ok";
|
|
1963
|
+
}
|
|
1964
|
+
var DAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
1965
|
+
function dayNumber(day) {
|
|
1966
|
+
const m = DAY.exec(day);
|
|
1967
|
+
if (m === null)
|
|
1968
|
+
return;
|
|
1969
|
+
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
1970
|
+
const ms = Date.UTC(y, mo - 1, d);
|
|
1971
|
+
const back = new Date(ms);
|
|
1972
|
+
if (back.getUTCFullYear() !== y || back.getUTCMonth() !== mo - 1 || back.getUTCDate() !== d)
|
|
1973
|
+
return;
|
|
1974
|
+
return Math.round(ms / 86400000);
|
|
1975
|
+
}
|
|
1976
|
+
function daysBetween(from, to) {
|
|
1977
|
+
const a = dayNumber(from);
|
|
1978
|
+
const b = dayNumber(to);
|
|
1979
|
+
if (a === undefined)
|
|
1980
|
+
return err(`${JSON.stringify(from)} is not a day (YYYY-MM-DD)`);
|
|
1981
|
+
if (b === undefined)
|
|
1982
|
+
return err(`${JSON.stringify(to)} is not a day (YYYY-MM-DD)`);
|
|
1983
|
+
return ok(b - a);
|
|
1984
|
+
}
|
|
1985
|
+
var UNAPPROVED_GRACE_DAYS = 90;
|
|
1986
|
+
function unapprovedSeverity(firstSeen, today, graceDays = UNAPPROVED_GRACE_DAYS) {
|
|
1987
|
+
const age = daysBetween(firstSeen, today);
|
|
1988
|
+
return age.kind === "ok" && age.value > graceDays ? "Warning" : "Informational";
|
|
1989
|
+
}
|
|
1990
|
+
function firstSeenOf(previous, key, today) {
|
|
1991
|
+
const before = Object.hasOwn(previous, key) ? previous[key] : undefined;
|
|
1992
|
+
return before !== undefined && dayNumber(before) !== undefined ? before : today;
|
|
1993
|
+
}
|
|
1994
|
+
function toSarif(findings, tool = "ergonex") {
|
|
1995
|
+
const rules = [...new Set(findings.map((f) => f.ruleId))].sort().map((id) => ({ id }));
|
|
1996
|
+
return {
|
|
1997
|
+
$schema: "https://json.schemastore.org/sarif-2.1.0.json",
|
|
1998
|
+
version: "2.1.0",
|
|
1999
|
+
runs: [
|
|
2000
|
+
{
|
|
2001
|
+
tool: {
|
|
2002
|
+
driver: {
|
|
2003
|
+
name: tool,
|
|
2004
|
+
informationUri: "https://intellectual-frontiers.github.io/.github/",
|
|
2005
|
+
rules
|
|
2006
|
+
}
|
|
2007
|
+
},
|
|
2008
|
+
results: findings.map((f) => ({
|
|
2009
|
+
ruleId: f.ruleId,
|
|
2010
|
+
level: sarifLevel(f.severity),
|
|
2011
|
+
message: { text: f.message },
|
|
2012
|
+
locations: [{ logicalLocations: [{ name: f.subject }] }],
|
|
2013
|
+
properties: { firstSeen: f.firstSeen }
|
|
2014
|
+
}))
|
|
2015
|
+
}
|
|
2016
|
+
]
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
// src/state-record.ts
|
|
2021
|
+
var ERGONEX_VERSION = "0.3.0";
|
|
2022
|
+
var STATE_RECORD_PATH = ".ergonex/state.auto.json";
|
|
2023
|
+
var EMPTY_RECORD = {
|
|
2024
|
+
ergonex: ERGONEX_VERSION,
|
|
2025
|
+
files: {},
|
|
2026
|
+
firstSeen: {}
|
|
2027
|
+
};
|
|
2028
|
+
var hash = (input, path = "$") => {
|
|
2029
|
+
const s = typeof input === "string" ? sha256Of(input) : undefined;
|
|
2030
|
+
return s === undefined ? err({
|
|
2031
|
+
kind: "decode",
|
|
2032
|
+
path,
|
|
2033
|
+
expected: "a SHA-256 (64 hexadecimal digits)",
|
|
2034
|
+
found: typeof input
|
|
2035
|
+
}) : ok(s);
|
|
2036
|
+
};
|
|
2037
|
+
var day = (input, path = "$") => typeof input === "string" && dayNumber(input) !== undefined ? ok(input) : err({
|
|
2038
|
+
kind: "decode",
|
|
2039
|
+
path,
|
|
2040
|
+
expected: "a day (YYYY-MM-DD)",
|
|
2041
|
+
found: typeof input
|
|
2042
|
+
});
|
|
2043
|
+
var shape = object({
|
|
2044
|
+
ergonex: string,
|
|
2045
|
+
files: record(object({ sha256: hash, generator: string })),
|
|
2046
|
+
firstSeen: record(day)
|
|
2047
|
+
}, { plan: hash });
|
|
2048
|
+
function decodeStateRecord(text) {
|
|
2049
|
+
const r = json(text, shape);
|
|
2050
|
+
return r.kind === "ok" ? ok(r.value) : err(formatDecodeError(r.error));
|
|
2051
|
+
}
|
|
2052
|
+
function encodeStateRecord(r) {
|
|
2053
|
+
const sorted = (o) => Object.fromEntries(Object.entries(o).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0));
|
|
2054
|
+
const value = {
|
|
2055
|
+
_generated: headerText("ergonex state"),
|
|
2056
|
+
ergonex: r.ergonex,
|
|
2057
|
+
...r.plan === undefined ? {} : { plan: r.plan },
|
|
2058
|
+
files: sorted(r.files),
|
|
2059
|
+
firstSeen: sorted(r.firstSeen)
|
|
2060
|
+
};
|
|
2061
|
+
return `${JSON.stringify(value, null, 2)}
|
|
2062
|
+
`;
|
|
2063
|
+
}
|
|
2064
|
+
function withFile(r, file, digest) {
|
|
2065
|
+
return {
|
|
2066
|
+
...r,
|
|
2067
|
+
files: {
|
|
2068
|
+
...r.files,
|
|
2069
|
+
[file.path]: { sha256: digest, generator: file.generator }
|
|
2070
|
+
}
|
|
2071
|
+
};
|
|
2072
|
+
}
|
|
2073
|
+
function withoutFile(r, path) {
|
|
2074
|
+
return {
|
|
2075
|
+
...r,
|
|
2076
|
+
files: Object.fromEntries(Object.entries(r.files).filter(([p]) => p !== path))
|
|
2077
|
+
};
|
|
2078
|
+
}
|
|
2079
|
+
function verifyFindings(facts, today) {
|
|
2080
|
+
const out = [];
|
|
2081
|
+
for (const f of facts) {
|
|
2082
|
+
const at = (ruleId, severity, message) => void out.push({
|
|
2083
|
+
ruleId,
|
|
2084
|
+
severity,
|
|
2085
|
+
message,
|
|
2086
|
+
subject: f.path,
|
|
2087
|
+
firstSeen: today
|
|
2088
|
+
});
|
|
2089
|
+
if (f.recorded !== undefined && f.onDisk === undefined) {
|
|
2090
|
+
at("ergonex/generated-missing", "Warning", `${f.path} is recorded as written and is not there; run \`ergonex render\` to write it again`);
|
|
2091
|
+
} else if (f.recorded !== undefined && f.onDisk !== undefined && f.onDisk !== f.recorded) {
|
|
2092
|
+
at("ergonex/generated-edited", "Error", `${f.path} was edited by hand (its bytes are not the ones recorded); \`ergonex render --force\` replaces it`);
|
|
2093
|
+
} else if (f.recorded === undefined && f.onDisk !== undefined) {
|
|
2094
|
+
at("ergonex/generated-unrecorded", "Warning", `${f.path} is a generated file the state record does not hold; run \`ergonex render\` to record it, or remove it`);
|
|
2095
|
+
}
|
|
2096
|
+
const edited = f.recorded !== undefined && f.onDisk !== undefined && f.onDisk !== f.recorded;
|
|
2097
|
+
const missing = f.recorded !== undefined && f.onDisk === undefined;
|
|
2098
|
+
if (f.wanted !== undefined && f.onDisk !== f.wanted && !edited && !missing) {
|
|
2099
|
+
at("ergonex/generated-drift", "Warning", f.onDisk === undefined ? `${f.path} is not written yet; run \`ergonex render\`` : `${f.path} would be written differently by the templates now; run \`ergonex render\``);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
return out;
|
|
2103
|
+
}
|
|
2104
|
+
// src/platform/k-args.ts
|
|
2105
|
+
var editDistance2 = editDistance;
|
|
2106
|
+
var flag2 = flag;
|
|
2107
|
+
var formatUsageError2 = formatUsageError;
|
|
2108
|
+
var helpOf2 = helpOf;
|
|
2109
|
+
var parseArgs2 = parseArgs;
|
|
2110
|
+
var value2 = value;
|
|
2111
|
+
|
|
2112
|
+
// src/modules/name.ts
|
|
2113
|
+
var MAX_MODULE_NAME = 32;
|
|
2114
|
+
function describeNameError(e) {
|
|
2115
|
+
switch (e.kind) {
|
|
2116
|
+
case "empty":
|
|
2117
|
+
return "a module name is not empty";
|
|
2118
|
+
case "too-long":
|
|
2119
|
+
return `a module name has at most ${MAX_MODULE_NAME} characters, not ${e.length}`;
|
|
2120
|
+
case "bad-start":
|
|
2121
|
+
return `${JSON.stringify(e.text)} does not begin with a lower-case letter, as a module name must`;
|
|
2122
|
+
case "bad-character":
|
|
2123
|
+
return `${JSON.stringify(e.text)} holds ${JSON.stringify(e.character)}, and a module name is lower-case letters, digits and hyphens`;
|
|
2124
|
+
default:
|
|
2125
|
+
return absurd(e);
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
function moduleName(text) {
|
|
2129
|
+
if (text === "")
|
|
2130
|
+
return err({ kind: "empty" });
|
|
2131
|
+
if (text.length > MAX_MODULE_NAME)
|
|
2132
|
+
return err({ kind: "too-long", length: text.length });
|
|
2133
|
+
if (!/^[a-z]/.test(text))
|
|
2134
|
+
return err({ kind: "bad-start", text });
|
|
2135
|
+
const bad = /[^a-z0-9-]/u.exec(text);
|
|
2136
|
+
if (bad !== null)
|
|
2137
|
+
return err({ kind: "bad-character", text, character: bad[0] });
|
|
2138
|
+
return ok(text);
|
|
2139
|
+
}
|
|
2140
|
+
function nearestNames(word, names) {
|
|
2141
|
+
return names.map((n) => ({ n, d: editDistance2(word, n) })).filter((x) => x.d <= 2).sort((a, b) => a.d - b.d || (a.n < b.n ? -1 : a.n > b.n ? 1 : 0)).slice(0, 3).map((x) => x.n);
|
|
2142
|
+
}
|
|
2143
|
+
|
|
2144
|
+
// src/modules/external.ts
|
|
2145
|
+
var CONTRACT_VERSION = 1;
|
|
2146
|
+
var FIELDS = {
|
|
2147
|
+
contract: {
|
|
2148
|
+
type: "integer",
|
|
2149
|
+
description: "The version of this contract the module speaks."
|
|
2150
|
+
},
|
|
2151
|
+
name: {
|
|
2152
|
+
type: "string",
|
|
2153
|
+
description: "The module's name, as `ergonex <name>` runs it."
|
|
2154
|
+
},
|
|
2155
|
+
summary: {
|
|
2156
|
+
type: "string",
|
|
2157
|
+
description: "One sentence on what the module does."
|
|
2158
|
+
},
|
|
2159
|
+
commands: { type: "array", description: "The commands the module has." }
|
|
2160
|
+
};
|
|
2161
|
+
var COMMAND_FIELDS = {
|
|
2162
|
+
name: { type: "string", description: "The command's name." },
|
|
2163
|
+
summary: {
|
|
2164
|
+
type: "string",
|
|
2165
|
+
description: "One sentence on what the command does."
|
|
2166
|
+
}
|
|
2167
|
+
};
|
|
2168
|
+
var commandDecoder = object({
|
|
2169
|
+
name: string,
|
|
2170
|
+
summary: string
|
|
2171
|
+
});
|
|
2172
|
+
var manifestDecoder2 = object({
|
|
2173
|
+
contract: integer,
|
|
2174
|
+
name: string,
|
|
2175
|
+
summary: string,
|
|
2176
|
+
commands: array(commandDecoder)
|
|
2177
|
+
});
|
|
2178
|
+
function decodeExternalManifest(text) {
|
|
2179
|
+
const r = json(text, manifestDecoder2);
|
|
2180
|
+
if (r.kind === "err")
|
|
2181
|
+
return err(formatDecodeError(r.error));
|
|
2182
|
+
const m = r.value;
|
|
2183
|
+
if (m.contract !== CONTRACT_VERSION)
|
|
2184
|
+
return err(`the module speaks contract ${m.contract}, and this ergonex speaks ${CONTRACT_VERSION}`);
|
|
2185
|
+
const name = moduleName(m.name);
|
|
2186
|
+
if (name.kind === "err")
|
|
2187
|
+
return err(`the module names itself ${JSON.stringify(m.name)}, which is not a module name`);
|
|
2188
|
+
return ok(m);
|
|
2189
|
+
}
|
|
2190
|
+
function externalManifestSchema() {
|
|
2191
|
+
const property = (f) => ({
|
|
2192
|
+
type: f.type,
|
|
2193
|
+
description: f.description
|
|
2194
|
+
});
|
|
2195
|
+
return {
|
|
2196
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
2197
|
+
$id: "https://intellectual-frontiers.github.io/.github/schemas/ergonex-manifest.json",
|
|
2198
|
+
title: "The answer of an ergonex module to --ergonex-manifest",
|
|
2199
|
+
type: "object",
|
|
2200
|
+
required: Object.keys(FIELDS),
|
|
2201
|
+
additionalProperties: true,
|
|
2202
|
+
properties: {
|
|
2203
|
+
contract: { ...property(FIELDS.contract), const: CONTRACT_VERSION },
|
|
2204
|
+
name: { ...property(FIELDS.name), pattern: "^[a-z][a-z0-9-]{0,31}$" },
|
|
2205
|
+
summary: property(FIELDS.summary),
|
|
2206
|
+
commands: {
|
|
2207
|
+
...property(FIELDS.commands),
|
|
2208
|
+
items: {
|
|
2209
|
+
type: "object",
|
|
2210
|
+
required: Object.keys(COMMAND_FIELDS),
|
|
2211
|
+
additionalProperties: true,
|
|
2212
|
+
properties: {
|
|
2213
|
+
name: property(COMMAND_FIELDS.name),
|
|
2214
|
+
summary: property(COMMAND_FIELDS.summary)
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2221
|
+
// src/modules/types.ts
|
|
2222
|
+
var EXIT_STATUSES = ["Ok", "Findings", "Usage", "Environment"];
|
|
2223
|
+
function exitCodeOf(status) {
|
|
2224
|
+
switch (status) {
|
|
2225
|
+
case "Ok":
|
|
2226
|
+
return 0;
|
|
2227
|
+
case "Findings":
|
|
2228
|
+
return 1;
|
|
2229
|
+
case "Usage":
|
|
2230
|
+
return 2;
|
|
2231
|
+
case "Environment":
|
|
2232
|
+
return 3;
|
|
2233
|
+
default:
|
|
2234
|
+
return absurd(status);
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
var MODULE_SOURCES = ["BuiltIn", "Registered", "Path"];
|
|
2238
|
+
function precedenceOf(source) {
|
|
2239
|
+
switch (source) {
|
|
2240
|
+
case "BuiltIn":
|
|
2241
|
+
return 1;
|
|
2242
|
+
case "Registered":
|
|
2243
|
+
return 2;
|
|
2244
|
+
case "Path":
|
|
2245
|
+
return 3;
|
|
2246
|
+
default:
|
|
2247
|
+
return absurd(source);
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
var GLOBAL_OPTIONS = {
|
|
2251
|
+
home: value2({
|
|
2252
|
+
help: "The root of the workspace for this run (else $ERGONEX_HOME, else $HOME/workspaces).",
|
|
2253
|
+
valueName: "DIR"
|
|
2254
|
+
}),
|
|
2255
|
+
json: flag2({ help: "Write the report as JSON." }),
|
|
2256
|
+
sarif: flag2({ help: "Write the findings as a SARIF 2.1.0 log." }),
|
|
2257
|
+
noColor: flag2({ help: "Write no colour." })
|
|
2258
|
+
};
|
|
2259
|
+
function globalsOf(options) {
|
|
2260
|
+
const home = options["home"];
|
|
2261
|
+
return {
|
|
2262
|
+
...typeof home === "string" && home !== "" ? { home } : {},
|
|
2263
|
+
json: options["json"] === true,
|
|
2264
|
+
sarif: options["sarif"] === true,
|
|
2265
|
+
noColor: options["noColor"] === true
|
|
2266
|
+
};
|
|
2267
|
+
}
|
|
2268
|
+
function defineCommand(d) {
|
|
2269
|
+
const operands = d.operands ?? [];
|
|
2270
|
+
const help = (program) => helpOf2(program, d.summary, d.options, operands);
|
|
2271
|
+
return {
|
|
2272
|
+
name: d.name,
|
|
2273
|
+
summary: d.summary,
|
|
2274
|
+
help,
|
|
2275
|
+
execute: async (words, base) => {
|
|
2276
|
+
const program = `ergonex ${d.name}`;
|
|
2277
|
+
const parsed = parseArgs2(words, d.options, operands);
|
|
2278
|
+
if (parsed.kind === "err") {
|
|
2279
|
+
return {
|
|
2280
|
+
command: d.name,
|
|
2281
|
+
outcome: err({
|
|
2282
|
+
kind: "usage",
|
|
2283
|
+
message: `${formatUsageError2(parsed.error, program)}
|
|
2284
|
+
|
|
2285
|
+
${help(program)}`
|
|
2286
|
+
})
|
|
2287
|
+
};
|
|
2288
|
+
}
|
|
2289
|
+
if (parsed.value.kind === "help") {
|
|
2290
|
+
return {
|
|
2291
|
+
command: d.name,
|
|
2292
|
+
outcome: ok({
|
|
2293
|
+
text: help(program),
|
|
2294
|
+
data: null,
|
|
2295
|
+
findings: [],
|
|
2296
|
+
noEvidence: true
|
|
2297
|
+
})
|
|
2298
|
+
};
|
|
2299
|
+
}
|
|
2300
|
+
const globals = globalsOf(parsed.value.options);
|
|
2301
|
+
const ctx = {
|
|
2302
|
+
...base,
|
|
2303
|
+
globals,
|
|
2304
|
+
home: base.homeFor(globals.home)
|
|
2305
|
+
};
|
|
2306
|
+
return {
|
|
2307
|
+
command: d.name,
|
|
2308
|
+
globals,
|
|
2309
|
+
outcome: await d.run(ctx, parsed.value)
|
|
2310
|
+
};
|
|
2311
|
+
}
|
|
2312
|
+
};
|
|
2313
|
+
}
|
|
2314
|
+
function dispatch(m, words) {
|
|
2315
|
+
const only = m.commands.length === 1 ? m.commands[0] : undefined;
|
|
2316
|
+
if (only !== undefined && only.name === m.name)
|
|
2317
|
+
return { kind: "command", command: only, words };
|
|
2318
|
+
const [first, ...rest] = words;
|
|
2319
|
+
if (first === undefined || first === "--help" || first === "-h")
|
|
2320
|
+
return { kind: "help" };
|
|
2321
|
+
const command = m.commands.find((c) => c.name === first);
|
|
2322
|
+
if (command === undefined)
|
|
2323
|
+
return {
|
|
2324
|
+
kind: "unknown",
|
|
2325
|
+
word: first,
|
|
2326
|
+
known: m.commands.map((c) => c.name)
|
|
2327
|
+
};
|
|
2328
|
+
return { kind: "command", command, words: rest };
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2331
|
+
// src/modules/registry.ts
|
|
2332
|
+
var registeredShape = object({
|
|
2333
|
+
modules: array(object({ name: string, program: string }, { sha256: string, summary: string }))
|
|
2334
|
+
});
|
|
2335
|
+
function decodeRegistered(text) {
|
|
2336
|
+
const r = json(text, registeredShape);
|
|
2337
|
+
if (r.kind === "err")
|
|
2338
|
+
return err(formatDecodeError(r.error));
|
|
2339
|
+
const out = [];
|
|
2340
|
+
const seen = new Set;
|
|
2341
|
+
for (const [i, m] of r.value.modules.entries()) {
|
|
2342
|
+
const name = moduleName(m.name);
|
|
2343
|
+
if (name.kind === "err")
|
|
2344
|
+
return err(`$.modules[${i}].name: ${JSON.stringify(m.name)} is not a module name`);
|
|
2345
|
+
if (seen.has(m.name))
|
|
2346
|
+
return err(`$.modules[${i}].name: ${m.name} is registered twice`);
|
|
2347
|
+
seen.add(m.name);
|
|
2348
|
+
const hash = m.sha256 === undefined ? undefined : sha256Of(m.sha256);
|
|
2349
|
+
if (m.sha256 !== undefined && hash === undefined)
|
|
2350
|
+
return err(`$.modules[${i}].sha256: not a SHA-256 (64 hexadecimal digits)`);
|
|
2351
|
+
out.push({
|
|
2352
|
+
name: m.name,
|
|
2353
|
+
program: m.program,
|
|
2354
|
+
...hash === undefined ? {} : { sha256: hash },
|
|
2355
|
+
...m.summary === undefined ? {} : { summary: m.summary }
|
|
2356
|
+
});
|
|
2357
|
+
}
|
|
2358
|
+
return ok(out);
|
|
2359
|
+
}
|
|
2360
|
+
function resolveAll(c) {
|
|
2361
|
+
const chosen = new Map;
|
|
2362
|
+
const hidden = [];
|
|
2363
|
+
const bySource = (s) => {
|
|
2364
|
+
switch (s) {
|
|
2365
|
+
case "BuiltIn":
|
|
2366
|
+
return c.builtIn;
|
|
2367
|
+
case "Registered":
|
|
2368
|
+
return c.registered;
|
|
2369
|
+
case "Path":
|
|
2370
|
+
return c.path;
|
|
2371
|
+
default:
|
|
2372
|
+
return absurd(s);
|
|
2373
|
+
}
|
|
2374
|
+
};
|
|
2375
|
+
for (const source of [...MODULE_SOURCES].sort((a, b) => precedenceOf(a) - precedenceOf(b))) {
|
|
2376
|
+
for (const m of bySource(source)) {
|
|
2377
|
+
const first = chosen.get(m.name);
|
|
2378
|
+
if (first === undefined)
|
|
2379
|
+
chosen.set(m.name, m);
|
|
2380
|
+
else
|
|
2381
|
+
hidden.push({ module: m, by: first });
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
return {
|
|
2385
|
+
modules: [...chosen.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0),
|
|
2386
|
+
hidden
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2389
|
+
function findModule(c, name) {
|
|
2390
|
+
return resolveAll(c).modules.find((m) => m.name === name);
|
|
2391
|
+
}
|
|
2392
|
+
function hiddenFindings(r, today) {
|
|
2393
|
+
return r.hidden.map(({ module, by }) => ({
|
|
2394
|
+
ruleId: "ergonex/module-hidden",
|
|
2395
|
+
severity: "Informational",
|
|
2396
|
+
message: `the module ${module.name} (${module.source}: ${module.where}) is hidden by the one of the same name that is ${by.source} (${by.where}), which runs`,
|
|
2397
|
+
subject: module.name,
|
|
2398
|
+
firstSeen: today
|
|
2399
|
+
}));
|
|
2400
|
+
}
|
|
2401
|
+
// src/policy.ts
|
|
2402
|
+
function policyOf(input = {}) {
|
|
2403
|
+
const regulatedDomain = input.regulatedDomain ?? [];
|
|
2404
|
+
return {
|
|
2405
|
+
requiresSignedCommits: regulatedDomain.length > 0 || input.requiresSignedCommits === true,
|
|
2406
|
+
regulatedDomain,
|
|
2407
|
+
unapprovedGraceDays: input.unapprovedGraceDays ?? UNAPPROVED_GRACE_DAYS,
|
|
2408
|
+
allowedGitHost: (input.allowedGitHost ?? []).map((h) => h.toLowerCase())
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
function isRegulated(policy) {
|
|
2412
|
+
return policy.regulatedDomain.length > 0;
|
|
2413
|
+
}
|
|
2414
|
+
function unsignedCommitSeverity(policy, signedAndVerified) {
|
|
2415
|
+
if (signedAndVerified)
|
|
2416
|
+
return;
|
|
2417
|
+
return isRegulated(policy) ? "Error" : "Warning";
|
|
2418
|
+
}
|
|
2419
|
+
function hookFailsWhenUnableToCheck(policy) {
|
|
2420
|
+
return isRegulated(policy);
|
|
2421
|
+
}
|
|
2422
|
+
function hostAllowed(policy, host) {
|
|
2423
|
+
return policy.allowedGitHost.length === 0 || policy.allowedGitHost.includes(host.toLowerCase());
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
// src/templates/agents.ts
|
|
2427
|
+
function agentsText(data) {
|
|
2428
|
+
const { manifest } = data;
|
|
2429
|
+
const groups = [...new Set(manifest.repositories.map((r) => r.group ?? ""))].sort();
|
|
2430
|
+
const repositories = groups.flatMap((g) => [
|
|
2431
|
+
g === "" ? "Repositories in no group:" : `Group \`${g}\` (opens as \`${g}.auto.code-workspace\`):`,
|
|
2432
|
+
"",
|
|
2433
|
+
...manifest.repositories.filter((r) => (r.group ?? "") === g).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0).map((r) => `- \`${r.path}\` (${r.cloneUrl})`),
|
|
2434
|
+
""
|
|
2435
|
+
]);
|
|
2436
|
+
return [
|
|
2437
|
+
"# Workspace guide for agents",
|
|
2438
|
+
"",
|
|
2439
|
+
"This is an Intellectual Frontiers workspace, managed by `ergonex`. It is the place a person or an agent works in; read this before you change anything here.",
|
|
2440
|
+
"",
|
|
2441
|
+
"## Layout",
|
|
2442
|
+
"",
|
|
2443
|
+
"- The root is `$ERGONEX_HOME` (the folder this file is in). A repository is at `<host>/<path>/<repo>` below it and mirrors its remote.",
|
|
2444
|
+
"- A worktree is at `.worktrees/<host>/<path>/<repo>/<branch>`. Make one only where another person or agent, or uncommitted work, is on the repository; otherwise work in the repository's own checkout.",
|
|
2445
|
+
"- The workspace's own folder is `.ergonex/`: the manifest `workspace.ts` (the source of everything generated), the generated Git configuration, the hooks and the lists of tools and modules. What ergonex remembers (the state record, the evidence of every run) is in its state folder, and the tools it installs are in its data folder (`ergonex paths` says where).",
|
|
2446
|
+
"- A file whose name carries `.auto.` is generated. Do not edit it: change `workspace.ts` and run `ergonex render`.",
|
|
2447
|
+
"",
|
|
2448
|
+
"## The repositories",
|
|
2449
|
+
"",
|
|
2450
|
+
...manifest.repositories.length === 0 ? ["The manifest names none yet.", ""] : repositories,
|
|
2451
|
+
"## Rules",
|
|
2452
|
+
"",
|
|
2453
|
+
"- Plan before apply: `ergonex plan` shows what would change and changes nothing; `ergonex apply` carries it out.",
|
|
2454
|
+
"- Fast-forward only: nothing here rebases, resets, merges, forces or discards. A repository with changes of its own, or one that has diverged, is left as it is and reported.",
|
|
2455
|
+
"- One issue for every commit: each commit message names its issue on a line of its own (`Refs #N`, and `Closes #N` on the commit that finishes it); the `commit-msg` hook checks it.",
|
|
2456
|
+
"- No credential in a file: never write a token, a password or a private key into the manifest, a generated file, a remote URL or a repository's configuration; the `pre-commit` hook scans for them.",
|
|
2457
|
+
...isRegulated(manifest.policy) ? [
|
|
2458
|
+
`- Signed commits: this workspace names a regulated domain (${manifest.policy.regulatedDomain.join(", ")}); every commit must be signed and verified, and the hooks fail where they cannot check.`
|
|
2459
|
+
] : manifest.policy.requiresSignedCommits ? ["- Signed commits: the policy requires every commit to be signed and verified."] : [],
|
|
2460
|
+
"",
|
|
2461
|
+
"## Commands",
|
|
2462
|
+
"",
|
|
2463
|
+
"- `ergonex status`: every repository, its state, how far it is ahead and behind, whether it is locked.",
|
|
2464
|
+
"- `ergonex plan`, `ergonex apply`: the typed plan, and carrying it out.",
|
|
2465
|
+
"- `ergonex render`, `ergonex verify`: write the generated files, and check them against the state record.",
|
|
2466
|
+
"- `ergonex doctor`: check the runtime, the layout, the file system, the policy and the generated files.",
|
|
2467
|
+
"- `ergonex worktree create|list|prune`: worktrees, named for their issue.",
|
|
2468
|
+
"- `ergonex list`, `ergonex help`: the modules.",
|
|
2469
|
+
"",
|
|
2470
|
+
"Every run writes one evidence record into ergonex's state folder (`ergonex paths` says where); none holds a credential."
|
|
2471
|
+
].join(`
|
|
2472
|
+
`);
|
|
2473
|
+
}
|
|
2474
|
+
var agentsTemplate = {
|
|
2475
|
+
name: "agents",
|
|
2476
|
+
render: (data) => {
|
|
2477
|
+
const file = generated("AGENTS.auto.md", { format: "markdown", text: agentsText(data) }, { generator: "ergonex templates/agents" });
|
|
2478
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2479
|
+
}
|
|
2480
|
+
};
|
|
2481
|
+
|
|
2482
|
+
// src/templates/code-workspace.ts
|
|
2483
|
+
var GENERATOR = "ergonex templates/code-workspace";
|
|
2484
|
+
var codeWorkspaceTemplate = {
|
|
2485
|
+
name: "code-workspace",
|
|
2486
|
+
render: (data) => {
|
|
2487
|
+
const groups = [...new Set(data.manifest.repositories.flatMap((r) => r.group === undefined ? [] : [r.group]))].sort();
|
|
2488
|
+
const files = [];
|
|
2489
|
+
const errors = [];
|
|
2490
|
+
for (const group of groups) {
|
|
2491
|
+
if (group.includes("/") || group.includes("\\")) {
|
|
2492
|
+
errors.push({
|
|
2493
|
+
kind: "bad-path",
|
|
2494
|
+
path: `${group}.auto.code-workspace`,
|
|
2495
|
+
reason: "a group's name is one name, with no path separator in it"
|
|
2496
|
+
});
|
|
2497
|
+
continue;
|
|
2498
|
+
}
|
|
2499
|
+
const folders = data.manifest.repositories.filter((r) => r.group === group).sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0).map((r) => ({
|
|
2500
|
+
name: segmentsOf(r.path).at(-1) ?? r.path,
|
|
2501
|
+
path: r.path
|
|
2502
|
+
}));
|
|
2503
|
+
const file = generated(`${group}.auto.code-workspace`, { format: "json", value: { folders, settings: {} } }, { generator: GENERATOR });
|
|
2504
|
+
if (file.kind === "err")
|
|
2505
|
+
errors.push(file.error);
|
|
2506
|
+
else
|
|
2507
|
+
files.push(file.value);
|
|
2508
|
+
}
|
|
2509
|
+
return errors.length > 0 ? err(errors) : ok(files);
|
|
2510
|
+
}
|
|
2511
|
+
};
|
|
2512
|
+
|
|
2513
|
+
// src/templates/envrc.ts
|
|
2514
|
+
var envrcTemplate = {
|
|
2515
|
+
name: "envrc",
|
|
2516
|
+
render: (data) => {
|
|
2517
|
+
if (!data.direnv)
|
|
2518
|
+
return ok([]);
|
|
2519
|
+
const file = generated(".envrc.auto", { format: "envrc", text: 'PATH_add "$HOME/.local/bin"' }, { generator: "ergonex templates/envrc" });
|
|
2520
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2521
|
+
}
|
|
2522
|
+
};
|
|
2523
|
+
|
|
2524
|
+
// src/templates/git-include.ts
|
|
2525
|
+
var gitIncludeTemplate = {
|
|
2526
|
+
name: "git-include",
|
|
2527
|
+
render: (data) => {
|
|
2528
|
+
const id = data.manifest.identity;
|
|
2529
|
+
const text = id === undefined || /[\r\n"\\]/.test(`${id.name}${id.email}`) ? "# The manifest names no identity, so this file sets nothing." : `[user]
|
|
2530
|
+
name = "${id.name}"
|
|
2531
|
+
email = "${id.email}"`;
|
|
2532
|
+
const file = generated(".ergonex/git.auto.conf", { format: "gitconfig", text }, { generator: "ergonex templates/git-include" });
|
|
2533
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2536
|
+
|
|
2537
|
+
// src/templates/hooks.ts
|
|
2538
|
+
var HOOK_NAMES = ["pre-commit", "commit-msg", "pre-push"];
|
|
2539
|
+
var HOOK_PURPOSE = {
|
|
2540
|
+
"pre-commit": "the plaintext-token scan of the staged changes (0002-workspace-model FR-031)",
|
|
2541
|
+
"commit-msg": "the issue every commit names (0020-ifccm)",
|
|
2542
|
+
"pre-push": "signed commits where the policy requires them (0002-workspace-model FR-032)"
|
|
2543
|
+
};
|
|
2544
|
+
function hookScript(name, strict) {
|
|
2545
|
+
return [
|
|
2546
|
+
"#!/bin/sh",
|
|
2547
|
+
`# ${name}: ${HOOK_PURPOSE[name]}.`,
|
|
2548
|
+
"# It only reads: it changes no commit, no tree and no configuration, and it uses no network.",
|
|
2549
|
+
'ERGONEX_HOME=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)',
|
|
2550
|
+
"unable() {",
|
|
2551
|
+
` echo "ergonex ${name} hook: cannot run its checker: $1" >&2`,
|
|
2552
|
+
...strict ? [' echo "ergonex: this workspace names a regulated domain, so the hook stops here; fix the cause and try again." >&2', " exit 1"] : [' echo "ergonex: this workspace names no regulated domain, so the hook lets it pass." >&2', " exit 0"],
|
|
2553
|
+
"}",
|
|
2554
|
+
"if ! command -v bun >/dev/null 2>&1 && ! command -v node >/dev/null 2>&1; then",
|
|
2555
|
+
' unable "neither bun nor node is on the PATH"',
|
|
2556
|
+
"fi",
|
|
2557
|
+
"if ! command -v ergonex >/dev/null 2>&1; then",
|
|
2558
|
+
' unable "ergonex is not on the PATH (install it with: bun add -g @intellectual-frontiers/ergonex, or run: ergonex shell --install)"',
|
|
2559
|
+
"fi",
|
|
2560
|
+
`ergonex hook ${name} --home "$ERGONEX_HOME" -- "$@" >&2`,
|
|
2561
|
+
"status=$?",
|
|
2562
|
+
'case "$status" in',
|
|
2563
|
+
" 0) exit 0 ;;",
|
|
2564
|
+
" 1) exit 1 ;;",
|
|
2565
|
+
' *) unable "ergonex ended with status $status" ;;',
|
|
2566
|
+
"esac"
|
|
2567
|
+
].join(`
|
|
2568
|
+
`);
|
|
2569
|
+
}
|
|
2570
|
+
var hooksTemplate = {
|
|
2571
|
+
name: "hooks",
|
|
2572
|
+
render: (data) => {
|
|
2573
|
+
const strict = hookFailsWhenUnableToCheck(data.manifest.policy);
|
|
2574
|
+
const files = [];
|
|
2575
|
+
const errors = [];
|
|
2576
|
+
for (const name of HOOK_NAMES) {
|
|
2577
|
+
const file = generated(`${HOOKS_FOLDER}/${name}`, { format: "sh", text: hookScript(name, strict) }, { generator: "ergonex templates/hooks", mode: "0755" });
|
|
2578
|
+
if (file.kind === "err")
|
|
2579
|
+
errors.push(file.error);
|
|
2580
|
+
else
|
|
2581
|
+
files.push(file.value);
|
|
2582
|
+
}
|
|
2583
|
+
return errors.length > 0 ? err(errors) : ok(files);
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2586
|
+
|
|
2587
|
+
// src/templates/json-files.ts
|
|
2588
|
+
var toolsJsonTemplate = {
|
|
2589
|
+
name: "tools-json",
|
|
2590
|
+
render: (data) => {
|
|
2591
|
+
const file = generated(".ergonex/tools.auto.json", {
|
|
2592
|
+
format: "json",
|
|
2593
|
+
value: {
|
|
2594
|
+
tools: [...data.tools].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0).map((t) => ({
|
|
2595
|
+
name: t.name,
|
|
2596
|
+
version: t.version,
|
|
2597
|
+
provides: [...t.provides],
|
|
2598
|
+
artifacts: [...t.artifacts].sort((x, y) => `${x.os}/${x.arch}` < `${y.os}/${y.arch}` ? -1 : 1).map((a) => ({
|
|
2599
|
+
os: a.os,
|
|
2600
|
+
arch: a.arch,
|
|
2601
|
+
url: a.url,
|
|
2602
|
+
sha256: a.sha256,
|
|
2603
|
+
kind: a.kind,
|
|
2604
|
+
bin: a.bin
|
|
2605
|
+
}))
|
|
2606
|
+
}))
|
|
2607
|
+
}
|
|
2608
|
+
}, { generator: "ergonex templates/tools-json" });
|
|
2609
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2610
|
+
}
|
|
2611
|
+
};
|
|
2612
|
+
var modulesJsonTemplate = {
|
|
2613
|
+
name: "modules-json",
|
|
2614
|
+
render: (data) => {
|
|
2615
|
+
const modules = [...data.manifest.modules].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0).map((m) => ({
|
|
2616
|
+
name: m.name,
|
|
2617
|
+
program: m.program,
|
|
2618
|
+
...m.sha256 === undefined ? {} : { sha256: m.sha256 },
|
|
2619
|
+
...m.summary === undefined ? {} : { summary: m.summary }
|
|
2620
|
+
}));
|
|
2621
|
+
const file = generated(".ergonex/modules.auto.json", { format: "json", value: { modules } }, { generator: "ergonex templates/modules-json" });
|
|
2622
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2623
|
+
}
|
|
2624
|
+
};
|
|
2625
|
+
|
|
2626
|
+
// src/templates/lefthook.ts
|
|
2627
|
+
var argument = (name) => name === "commit-msg" ? " {1}" : "";
|
|
2628
|
+
var lefthookTemplate = {
|
|
2629
|
+
name: "lefthook",
|
|
2630
|
+
render: () => {
|
|
2631
|
+
const text = HOOK_NAMES.flatMap((name) => [
|
|
2632
|
+
`${name}:`,
|
|
2633
|
+
" commands:",
|
|
2634
|
+
" ergonex:",
|
|
2635
|
+
` run: sh .ergonex/hooks/${name}${argument(name)}`
|
|
2636
|
+
]).join(`
|
|
2637
|
+
`);
|
|
2638
|
+
const file = generated("lefthook.auto.yml", { format: "yaml", text }, { generator: "ergonex templates/lefthook" });
|
|
2639
|
+
return file.kind === "err" ? err([file.error]) : ok([file.value]);
|
|
2640
|
+
}
|
|
2641
|
+
};
|
|
2642
|
+
|
|
2643
|
+
// src/templates/mod.ts
|
|
2644
|
+
var TEMPLATES = [
|
|
2645
|
+
agentsTemplate,
|
|
2646
|
+
codeWorkspaceTemplate,
|
|
2647
|
+
envrcTemplate,
|
|
2648
|
+
gitIncludeTemplate,
|
|
2649
|
+
hooksTemplate,
|
|
2650
|
+
lefthookTemplate,
|
|
2651
|
+
modulesJsonTemplate,
|
|
2652
|
+
toolsJsonTemplate
|
|
2653
|
+
];
|
|
2654
|
+
function renderAll(data) {
|
|
2655
|
+
const files = [];
|
|
2656
|
+
const errors = [];
|
|
2657
|
+
for (const t of TEMPLATES) {
|
|
2658
|
+
const r = t.render(data);
|
|
2659
|
+
if (r.kind === "err")
|
|
2660
|
+
errors.push(...r.error);
|
|
2661
|
+
else
|
|
2662
|
+
files.push(...r.value);
|
|
2663
|
+
}
|
|
2664
|
+
if (errors.length > 0)
|
|
2665
|
+
return err(errors);
|
|
2666
|
+
return checkFiles(files, data.manifest.repositories.map((r) => r.path));
|
|
2667
|
+
}
|
|
2668
|
+
// src/shell/block.ts
|
|
2669
|
+
var BEGIN = "# >>> ergonex >>>";
|
|
2670
|
+
var END = "# <<< ergonex <<<";
|
|
2671
|
+
function linesKept(text) {
|
|
2672
|
+
return text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
2673
|
+
}
|
|
2674
|
+
function blocks(lines) {
|
|
2675
|
+
const spans = [];
|
|
2676
|
+
let open;
|
|
2677
|
+
for (const [i, raw] of lines.entries()) {
|
|
2678
|
+
const line = raw.replace(/\r?\n$/, "");
|
|
2679
|
+
if (line === BEGIN) {
|
|
2680
|
+
if (open !== undefined)
|
|
2681
|
+
return err({ kind: "unterminated", line: open + 1 });
|
|
2682
|
+
open = i;
|
|
2683
|
+
} else if (line === END) {
|
|
2684
|
+
if (open === undefined)
|
|
2685
|
+
return err({ kind: "unopened", line: i + 1 });
|
|
2686
|
+
spans.push({ from: open, to: i + 1 });
|
|
2687
|
+
open = undefined;
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
if (open !== undefined)
|
|
2691
|
+
return err({ kind: "unterminated", line: open + 1 });
|
|
2692
|
+
return ok(spans);
|
|
2693
|
+
}
|
|
2694
|
+
function blockText(snippet, eol = `
|
|
2695
|
+
`) {
|
|
2696
|
+
const body = snippet.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
2697
|
+
return [BEGIN, ...body, END].map((l) => `${l}${eol}`).join("");
|
|
2698
|
+
}
|
|
2699
|
+
function installBlock(existing, snippet) {
|
|
2700
|
+
if (existing === undefined)
|
|
2701
|
+
return err({ kind: "absent" });
|
|
2702
|
+
const text = existing;
|
|
2703
|
+
const eol = text.includes(`\r
|
|
2704
|
+
`) ? `\r
|
|
2705
|
+
` : `
|
|
2706
|
+
`;
|
|
2707
|
+
const block = blockText(snippet, eol);
|
|
2708
|
+
const lines = linesKept(text);
|
|
2709
|
+
const found = blocks(lines);
|
|
2710
|
+
if (found.kind === "err")
|
|
2711
|
+
return found;
|
|
2712
|
+
const spans = found.value;
|
|
2713
|
+
if (spans.length === 0) {
|
|
2714
|
+
const lead = text === "" || text.endsWith(`
|
|
2715
|
+
`) ? "" : eol;
|
|
2716
|
+
return ok({ text: `${text}${lead}${block}`, changed: true });
|
|
2717
|
+
}
|
|
2718
|
+
const out = [];
|
|
2719
|
+
let at = 0;
|
|
2720
|
+
for (const [i, s] of spans.entries()) {
|
|
2721
|
+
out.push(...lines.slice(at, s.from));
|
|
2722
|
+
if (i === 0)
|
|
2723
|
+
out.push(block);
|
|
2724
|
+
at = s.to;
|
|
2725
|
+
}
|
|
2726
|
+
out.push(...lines.slice(at));
|
|
2727
|
+
const next = out.join("");
|
|
2728
|
+
return ok({ text: next, changed: next !== text });
|
|
2729
|
+
}
|
|
2730
|
+
function removeBlock(existing) {
|
|
2731
|
+
const lines = linesKept(existing);
|
|
2732
|
+
const found = blocks(lines);
|
|
2733
|
+
if (found.kind === "err")
|
|
2734
|
+
return found;
|
|
2735
|
+
if (found.value.length === 0)
|
|
2736
|
+
return ok({ text: existing, changed: false });
|
|
2737
|
+
const drop = new Set(found.value.flatMap((s) => Array.from({ length: s.to - s.from }, (_, k) => s.from + k)));
|
|
2738
|
+
return ok({
|
|
2739
|
+
text: lines.filter((_, i) => !drop.has(i)).join(""),
|
|
2740
|
+
changed: true
|
|
2741
|
+
});
|
|
2742
|
+
}
|
|
2743
|
+
// src/shell/snippet.ts
|
|
2744
|
+
var SHELLS = ["bash", "zsh", "fish", "pwsh", "nu"];
|
|
2745
|
+
var posixQuote = (s) => `'${s.replaceAll("'", `'\\''`)}'`;
|
|
2746
|
+
var fishQuote = (s) => `'${s.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}'`;
|
|
2747
|
+
var pwshQuote = (s) => `'${s.replaceAll("'", "''")}'`;
|
|
2748
|
+
var nuQuote = (s) => `"${s.replaceAll("\\", "\\\\").replaceAll('"', "\\\"")}"`;
|
|
2749
|
+
var words = (c) => c.map((m) => m.module).join(" ");
|
|
2750
|
+
function posix(f, shell) {
|
|
2751
|
+
const home = f.home.kind === "default" ? "" : posixQuote(f.home.path);
|
|
2752
|
+
const several = f.completions.filter((m) => m.commands.length > 1);
|
|
2753
|
+
const completion = shell === "bash" ? [
|
|
2754
|
+
"_ergonex_complete() {",
|
|
2755
|
+
' local cur="${COMP_WORDS[COMP_CWORD]}"',
|
|
2756
|
+
' if [ "$COMP_CWORD" -eq 1 ]; then',
|
|
2757
|
+
` COMPREPLY=($(compgen -W ${posixQuote(words(f.completions))} -- "$cur"))`,
|
|
2758
|
+
' elif [ "$COMP_CWORD" -eq 2 ]; then',
|
|
2759
|
+
' case "${COMP_WORDS[1]}" in',
|
|
2760
|
+
...several.map((m) => ` ${m.module}) COMPREPLY=($(compgen -W ${posixQuote(m.commands.join(" "))} -- "$cur")) ;;`),
|
|
2761
|
+
" esac",
|
|
2762
|
+
" fi",
|
|
2763
|
+
"}",
|
|
2764
|
+
"complete -F _ergonex_complete ergonex"
|
|
2765
|
+
] : [
|
|
2766
|
+
"_ergonex_complete() {",
|
|
2767
|
+
" if (( CURRENT == 2 )); then",
|
|
2768
|
+
` compadd -- ${words(f.completions)}`,
|
|
2769
|
+
" elif (( CURRENT == 3 )); then",
|
|
2770
|
+
' case "${words[2]}" in',
|
|
2771
|
+
...several.map((m) => ` ${m.module}) compadd -- ${m.commands.join(" ")} ;;`),
|
|
2772
|
+
" esac",
|
|
2773
|
+
" fi",
|
|
2774
|
+
"}",
|
|
2775
|
+
"if (( $+functions[compdef] )); then compdef _ergonex_complete ergonex; fi"
|
|
2776
|
+
];
|
|
2777
|
+
return [
|
|
2778
|
+
"# IF Workspaces (ergonex): the root, the pinned tools on the PATH, the completions of ergonex, and wcd.",
|
|
2779
|
+
f.home.kind === "default" ? 'export ERGONEX_HOME="${ERGONEX_HOME:-$HOME/workspaces}"' : `if [ -z "\${ERGONEX_HOME:-}" ]; then export ERGONEX_HOME=${home}; fi`,
|
|
2780
|
+
"# the shims of the tools ergonex installs are in ~/.local/bin, which the XDG specification names for a person's own programs (0031-install-locations)",
|
|
2781
|
+
'case ":$PATH:" in',
|
|
2782
|
+
' *":$HOME/.local/bin:"*) ;;',
|
|
2783
|
+
' *) export PATH="$HOME/.local/bin:$PATH" ;;',
|
|
2784
|
+
"esac",
|
|
2785
|
+
"# the person's GitHub sign-in as the token for reading packages, kept in the environment of this shell and never in a file (ergonex 0004-onboarding FR-004).",
|
|
2786
|
+
'if [ -z "${ERGONEX_GITHUB_TOKEN:-}" ] && command -v gh >/dev/null 2>&1; then',
|
|
2787
|
+
' if ERGONEX_GITHUB_TOKEN="$(gh auth token 2>/dev/null)" && [ -n "$ERGONEX_GITHUB_TOKEN" ]; then export ERGONEX_GITHUB_TOKEN; else unset ERGONEX_GITHUB_TOKEN; fi',
|
|
2788
|
+
"fi",
|
|
2789
|
+
...completion,
|
|
2790
|
+
"wcd() {",
|
|
2791
|
+
' if [ -z "$1" ]; then echo "usage: wcd NAME" >&2; return 2; fi',
|
|
2792
|
+
` _ergonex_hits=$(find "$ERGONEX_HOME" -maxdepth 8 \\( -name .worktrees -o -name .ergonex -o -name .git \\) -prune -o -type d -name "$1" -exec test -e '{}/.git' \\; -print 2>/dev/null | sort)`,
|
|
2793
|
+
' if [ -z "$_ergonex_hits" ]; then',
|
|
2794
|
+
' echo "wcd: no repository named $1 in $ERGONEX_HOME" >&2; unset _ergonex_hits; return 1',
|
|
2795
|
+
" fi",
|
|
2796
|
+
` if [ "$(printf '%s\\n' "$_ergonex_hits" | wc -l)" -gt 1 ]; then`,
|
|
2797
|
+
` echo "wcd: more than one repository is named $1:" >&2; printf '%s\\n' "$_ergonex_hits" >&2; unset _ergonex_hits; return 1`,
|
|
2798
|
+
" fi",
|
|
2799
|
+
' cd "$_ergonex_hits" || { unset _ergonex_hits; return 1; }',
|
|
2800
|
+
" unset _ergonex_hits",
|
|
2801
|
+
"}"
|
|
2802
|
+
].join(`
|
|
2803
|
+
`);
|
|
2804
|
+
}
|
|
2805
|
+
function fish(f) {
|
|
2806
|
+
const home = f.home.kind === "default" ? '"$HOME/workspaces"' : fishQuote(f.home.path);
|
|
2807
|
+
const several = f.completions.filter((m) => m.commands.length > 1);
|
|
2808
|
+
return [
|
|
2809
|
+
"# IF Workspaces (ergonex): the root, the pinned tools on the PATH, the completions of ergonex, and wcd.",
|
|
2810
|
+
`set -q ERGONEX_HOME; or set -gx ERGONEX_HOME ${home}`,
|
|
2811
|
+
"# the shims of the tools ergonex installs are in ~/.local/bin, which the XDG specification names for a person's own programs (0031-install-locations)",
|
|
2812
|
+
'fish_add_path -g "$HOME/.local/bin"',
|
|
2813
|
+
"# the person's GitHub sign-in as the token for reading packages, kept in the environment of this shell and never in a file (ergonex 0004-onboarding FR-004).",
|
|
2814
|
+
"if not set -q ERGONEX_GITHUB_TOKEN; and command -q gh",
|
|
2815
|
+
' set -l token (gh auth token 2>/dev/null); and test -n "$token"; and set -gx ERGONEX_GITHUB_TOKEN $token',
|
|
2816
|
+
"end",
|
|
2817
|
+
`complete -c ergonex -f -n __fish_use_subcommand -a ${fishQuote(words(f.completions))}`,
|
|
2818
|
+
...several.map((m) => `complete -c ergonex -f -n '__fish_seen_subcommand_from ${m.module}' -a ${fishQuote(m.commands.join(" "))}`),
|
|
2819
|
+
"function wcd",
|
|
2820
|
+
" if test (count $argv) -lt 1",
|
|
2821
|
+
" echo 'usage: wcd NAME' >&2; return 2",
|
|
2822
|
+
" end",
|
|
2823
|
+
" set -l hits (find $ERGONEX_HOME -maxdepth 8 '(' -name .worktrees -o -name .ergonex -o -name .git ')' -prune -o -type d -name $argv[1] -exec test -e '{}/.git' ';' -print 2>/dev/null | sort)",
|
|
2824
|
+
" if test (count $hits) -eq 0",
|
|
2825
|
+
' echo "wcd: no repository named $argv[1] in $ERGONEX_HOME" >&2; return 1',
|
|
2826
|
+
" end",
|
|
2827
|
+
" if test (count $hits) -gt 1",
|
|
2828
|
+
` echo "wcd: more than one repository is named $argv[1]:" >&2; printf '%s\\n' $hits >&2; return 1`,
|
|
2829
|
+
" end",
|
|
2830
|
+
" cd $hits[1]",
|
|
2831
|
+
"end"
|
|
2832
|
+
].join(`
|
|
2833
|
+
`);
|
|
2834
|
+
}
|
|
2835
|
+
function pwsh(f) {
|
|
2836
|
+
const home = f.home.kind === "default" ? "(Join-Path $HOME 'workspaces')" : pwshQuote(f.home.path);
|
|
2837
|
+
const several = f.completions.filter((m) => m.commands.length > 1);
|
|
2838
|
+
return [
|
|
2839
|
+
"# IF Workspaces (ergonex): the root, the pinned tools on the PATH, the completions of ergonex, and wcd.",
|
|
2840
|
+
`if (-not $env:ERGONEX_HOME) { $env:ERGONEX_HOME = ${home} }`,
|
|
2841
|
+
"# the shims of the tools ergonex installs: ~/.local/bin (XDG) on Linux and macOS, and the tools folder of ergonex's data on Windows (0031-install-locations)",
|
|
2842
|
+
"$ergonexBin = if ($IsLinux -or $IsMacOS) { Join-Path $HOME '.local/bin' } else { Join-Path $(if ($env:XDG_DATA_HOME) { $env:XDG_DATA_HOME } else { $env:LOCALAPPDATA }) 'ergonex\\tools\\bin' }",
|
|
2843
|
+
"if (($env:PATH -split [IO.Path]::PathSeparator) -notcontains $ergonexBin) { $env:PATH = $ergonexBin + [IO.Path]::PathSeparator + $env:PATH }",
|
|
2844
|
+
"# the person's GitHub sign-in as the token for reading packages, kept in the environment of this shell and never in a file (ergonex 0004-onboarding FR-004).",
|
|
2845
|
+
"if (-not $env:ERGONEX_GITHUB_TOKEN -and (Get-Command gh -ErrorAction SilentlyContinue)) {",
|
|
2846
|
+
" $ergonexToken = (& gh auth token 2>$null)",
|
|
2847
|
+
' if ($LASTEXITCODE -eq 0 -and $ergonexToken) { $env:ERGONEX_GITHUB_TOKEN = "$ergonexToken".Trim() }',
|
|
2848
|
+
"}",
|
|
2849
|
+
"Register-ArgumentCompleter -Native -CommandName ergonex -ScriptBlock {",
|
|
2850
|
+
" param($wordToComplete, $commandAst, $cursorPosition)",
|
|
2851
|
+
" $parts = @($commandAst.CommandElements | ForEach-Object { $_.ToString() })",
|
|
2852
|
+
` $choices = if ($parts.Count -le 1 -or ($parts.Count -eq 2 -and $wordToComplete)) { ${pwshQuote(words(f.completions))} -split ' ' }`,
|
|
2853
|
+
...several.map((m) => ` elseif ($parts[1] -eq '${m.module}') { ${pwshQuote(m.commands.join(" "))} -split ' ' }`),
|
|
2854
|
+
" else { @() }",
|
|
2855
|
+
` $choices | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { [Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }`,
|
|
2856
|
+
"}",
|
|
2857
|
+
"function wcd {",
|
|
2858
|
+
" param([Parameter(Mandatory = $true)][string]$Name)",
|
|
2859
|
+
" $hits = @(Get-ChildItem -Path $env:ERGONEX_HOME -Directory -Recurse -Depth 7 -Filter $Name -ErrorAction SilentlyContinue |",
|
|
2860
|
+
" Where-Object { (Test-Path (Join-Path $_.FullName '.git')) -and $_.FullName -notmatch '[\\\\/]\\.(worktrees|ergonex)[\\\\/]' })",
|
|
2861
|
+
' if ($hits.Count -eq 0) { Write-Error "wcd: no repository named $Name in $env:ERGONEX_HOME"; return }',
|
|
2862
|
+
' if ($hits.Count -gt 1) { Write-Error ("wcd: more than one repository is named ${Name}: " + ($hits.FullName -join ", ")); return }',
|
|
2863
|
+
" Set-Location $hits[0].FullName",
|
|
2864
|
+
"}"
|
|
2865
|
+
].join(`
|
|
2866
|
+
`);
|
|
2867
|
+
}
|
|
2868
|
+
function nu(f) {
|
|
2869
|
+
const home = f.home.kind === "default" ? '($nu.home-path | path join "workspaces")' : nuQuote(f.home.path);
|
|
2870
|
+
const several = f.completions.filter((m) => m.commands.length > 1);
|
|
2871
|
+
return [
|
|
2872
|
+
"# IF Workspaces (ergonex): the root, the pinned tools on the PATH, the completions of ergonex, and wcd.",
|
|
2873
|
+
`$env.ERGONEX_HOME = ($env.ERGONEX_HOME? | default ${home})`,
|
|
2874
|
+
"# the shims of the tools ergonex installs: ~/.local/bin (XDG) on Linux and macOS, and the tools folder of ergonex's data on Windows (0031-install-locations)",
|
|
2875
|
+
`let ergonex_bin = (if ($nu.os-info.name == "windows") { (($env.XDG_DATA_HOME? | default $env.LOCALAPPDATA) | path join "ergonex" "tools" "bin") } else { ($env.HOME | path join ".local" "bin") })`,
|
|
2876
|
+
"$env.PATH = ($env.PATH | split row (char esep) | where { |p| $p != $ergonex_bin } | prepend $ergonex_bin)",
|
|
2877
|
+
"# the person's GitHub sign-in as the token for reading packages, kept in the environment of this shell and never in a file (ergonex 0004-onboarding FR-004).",
|
|
2878
|
+
'if (($env.ERGONEX_GITHUB_TOKEN? | default "") == "") and (which gh | is-not-empty) {',
|
|
2879
|
+
" let result = (do { gh auth token } | complete)",
|
|
2880
|
+
" if $result.exit_code == 0 and ($result.stdout | str trim | is-not-empty) { $env.ERGONEX_GITHUB_TOKEN = ($result.stdout | str trim) }",
|
|
2881
|
+
"}",
|
|
2882
|
+
`def "nu-complete ergonex" [] { [${f.completions.map((m) => nuQuote(m.module)).join(" ")}] }`,
|
|
2883
|
+
...several.map((m) => `def "nu-complete ergonex ${m.module}" [] { [${m.commands.map(nuQuote).join(" ")}] }`),
|
|
2884
|
+
'extern "ergonex" [ module?: string@"nu-complete ergonex", ...rest: string ]',
|
|
2885
|
+
"def --env wcd [name: string] {",
|
|
2886
|
+
' let hits = (glob ($env.ERGONEX_HOME | path join "**" $name) --depth 8 --no-file | where { |p| (($p | path join ".git") | path exists) and not ($p =~ "[\\\\/]\\.(worktrees|ergonex)[\\\\/]") } | sort)',
|
|
2887
|
+
' if ($hits | is-empty) { error make { msg: $"wcd: no repository named ($name) in ($env.ERGONEX_HOME)" } }',
|
|
2888
|
+
' if ($hits | length) > 1 { error make { msg: $"wcd: more than one repository is named ($name): ($hits | str join ", ")" } }',
|
|
2889
|
+
" cd ($hits | first)",
|
|
2890
|
+
"}"
|
|
2891
|
+
].join(`
|
|
2892
|
+
`);
|
|
2893
|
+
}
|
|
2894
|
+
function snippetFor(shell, facts) {
|
|
2895
|
+
switch (shell) {
|
|
2896
|
+
case "bash":
|
|
2897
|
+
case "zsh":
|
|
2898
|
+
return `${posix(facts, shell)}
|
|
2899
|
+
`;
|
|
2900
|
+
case "fish":
|
|
2901
|
+
return `${fish(facts)}
|
|
2902
|
+
`;
|
|
2903
|
+
case "pwsh":
|
|
2904
|
+
return `${pwsh(facts)}
|
|
2905
|
+
`;
|
|
2906
|
+
case "nu":
|
|
2907
|
+
return `${nu(facts)}
|
|
2908
|
+
`;
|
|
2909
|
+
default:
|
|
2910
|
+
return absurd(shell);
|
|
2911
|
+
}
|
|
2912
|
+
}
|
|
2913
|
+
function rcFileFor(shell, f) {
|
|
2914
|
+
const home = f.home.replace(/[\\/]+$/, "");
|
|
2915
|
+
const config = f.xdgConfigHome !== undefined && f.xdgConfigHome !== "" ? f.xdgConfigHome.replace(/[\\/]+$/, "") : `${home}/.config`;
|
|
2916
|
+
switch (shell) {
|
|
2917
|
+
case "bash":
|
|
2918
|
+
return `${home}/.bashrc`;
|
|
2919
|
+
case "zsh":
|
|
2920
|
+
return `${f.zdotdir !== undefined && f.zdotdir !== "" ? f.zdotdir.replace(/[\\/]+$/, "") : home}/.zshrc`;
|
|
2921
|
+
case "fish":
|
|
2922
|
+
return `${config}/fish/conf.d/ergonex.fish`;
|
|
2923
|
+
case "pwsh":
|
|
2924
|
+
return f.windows ? `${home}/Documents/PowerShell/Microsoft.PowerShell_profile.ps1` : `${config}/powershell/Microsoft.PowerShell_profile.ps1`;
|
|
2925
|
+
case "nu":
|
|
2926
|
+
return f.darwin && f.xdgConfigHome === undefined ? `${home}/Library/Application Support/nushell/config.nu` : `${config}/nushell/config.nu`;
|
|
2927
|
+
default:
|
|
2928
|
+
return absurd(shell);
|
|
2929
|
+
}
|
|
2930
|
+
}
|
|
2931
|
+
// ../ts/runtime/otel.ts
|
|
2932
|
+
var RETRYABLE_STATUSES = new Set([429, 502, 503, 504]);
|
|
2933
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
2934
|
+
var RUNTIME_SIGNALS = defineSignals({
|
|
2935
|
+
process: {
|
|
2936
|
+
signal: "span",
|
|
2937
|
+
spanKind: "client",
|
|
2938
|
+
role: "detail",
|
|
2939
|
+
description: "A program run by proc: its name and option names alone (FR-006), and its exit status.",
|
|
2940
|
+
attributes: { "process.executable.name": "string", "process.command_args": "string[]", "process.exit.code?": "int" }
|
|
2941
|
+
},
|
|
2942
|
+
"http.client": {
|
|
2943
|
+
signal: "span",
|
|
2944
|
+
spanKind: "client",
|
|
2945
|
+
role: "detail",
|
|
2946
|
+
description: "A request made by the HTTP client; the URL redacted (FR-006).",
|
|
2947
|
+
attributes: {
|
|
2948
|
+
"http.request.method": "string",
|
|
2949
|
+
"url.full": "string",
|
|
2950
|
+
"server.address": "string",
|
|
2951
|
+
"server.port?": "int",
|
|
2952
|
+
"http.response.status_code?": "int",
|
|
2953
|
+
"error.type?": "string"
|
|
2954
|
+
}
|
|
2955
|
+
},
|
|
2956
|
+
"http.server": {
|
|
2957
|
+
signal: "span",
|
|
2958
|
+
spanKind: "server",
|
|
2959
|
+
role: "detail",
|
|
2960
|
+
description: "A request answered by serve; its parent is the request's traceparent.",
|
|
2961
|
+
attributes: {
|
|
2962
|
+
"http.request.method": "string",
|
|
2963
|
+
"url.path": "string",
|
|
2964
|
+
"url.scheme": "string",
|
|
2965
|
+
"server.address?": "string",
|
|
2966
|
+
"server.port?": "int",
|
|
2967
|
+
"http.response.status_code?": "int"
|
|
2968
|
+
}
|
|
2969
|
+
},
|
|
2970
|
+
"otel.warning": {
|
|
2971
|
+
signal: "log",
|
|
2972
|
+
severity: SEVERITY.WARN,
|
|
2973
|
+
role: "none",
|
|
2974
|
+
description: "Signals were not sent: an export failed, no collector answered, or the wait at exit ran out.",
|
|
2975
|
+
attributes: {}
|
|
2976
|
+
}
|
|
2977
|
+
});
|
|
2978
|
+
var UNTRACED = new WeakSet;
|
|
2979
|
+
|
|
2980
|
+
// ../ts/runtime/proc.ts
|
|
2981
|
+
import process from "node:process";
|
|
2982
|
+
var args = Object.freeze(process.argv.slice(2));
|
|
2983
|
+
var pid = process.pid;
|
|
2984
|
+
function osOf(platform) {
|
|
2985
|
+
switch (platform) {
|
|
2986
|
+
case "linux":
|
|
2987
|
+
case "darwin":
|
|
2988
|
+
case "freebsd":
|
|
2989
|
+
case "netbsd":
|
|
2990
|
+
case "openbsd":
|
|
2991
|
+
case "aix":
|
|
2992
|
+
case "android":
|
|
2993
|
+
return platform;
|
|
2994
|
+
case "win32":
|
|
2995
|
+
return "windows";
|
|
2996
|
+
case "sunos":
|
|
2997
|
+
return "solaris";
|
|
2998
|
+
default:
|
|
2999
|
+
return "other";
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
function archOf(arch) {
|
|
3003
|
+
switch (arch) {
|
|
3004
|
+
case "x64":
|
|
3005
|
+
return "x86_64";
|
|
3006
|
+
case "arm64":
|
|
3007
|
+
return "aarch64";
|
|
3008
|
+
case "arm":
|
|
3009
|
+
case "riscv64":
|
|
3010
|
+
case "s390x":
|
|
3011
|
+
case "ppc64":
|
|
3012
|
+
return arch;
|
|
3013
|
+
default:
|
|
3014
|
+
return "other";
|
|
3015
|
+
}
|
|
3016
|
+
}
|
|
3017
|
+
function tripleOf(os, arch) {
|
|
3018
|
+
switch (os) {
|
|
3019
|
+
case "linux":
|
|
3020
|
+
return `${arch}-unknown-linux-gnu`;
|
|
3021
|
+
case "darwin":
|
|
3022
|
+
return `${arch}-apple-darwin`;
|
|
3023
|
+
case "windows":
|
|
3024
|
+
return `${arch}-pc-windows-msvc`;
|
|
3025
|
+
case "android":
|
|
3026
|
+
return `${arch}-linux-android`;
|
|
3027
|
+
case "freebsd":
|
|
3028
|
+
case "netbsd":
|
|
3029
|
+
case "openbsd":
|
|
3030
|
+
case "solaris":
|
|
3031
|
+
case "aix":
|
|
3032
|
+
case "other":
|
|
3033
|
+
return `${arch}-unknown-${os}`;
|
|
3034
|
+
default:
|
|
3035
|
+
return absurd(os);
|
|
3036
|
+
}
|
|
3037
|
+
}
|
|
3038
|
+
var build = Object.freeze({
|
|
3039
|
+
os: osOf(process.platform),
|
|
3040
|
+
arch: archOf(process.arch),
|
|
3041
|
+
target: tripleOf(osOf(process.platform), archOf(process.arch))
|
|
3042
|
+
});
|
|
3043
|
+
|
|
3044
|
+
// ../ts/runtime/http-client.ts
|
|
3045
|
+
var exchangeDecoder = object({
|
|
3046
|
+
method: literal("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"),
|
|
3047
|
+
url: string,
|
|
3048
|
+
status: number,
|
|
3049
|
+
headers: record(string),
|
|
3050
|
+
body: string
|
|
3051
|
+
});
|
|
3052
|
+
|
|
3053
|
+
// ../ts/githost/capabilities.ts
|
|
3054
|
+
var COMMON_CAPABILITIES = ["CloneUrl", "Credentials", "DefaultBranch", "SignedCommits"];
|
|
3055
|
+
var HOSTED = [...COMMON_CAPABILITIES, "ProtectedBranches", "Issues", "ChangeRequests"];
|
|
3056
|
+
var CAPABILITIES = {
|
|
3057
|
+
Github: new Set(HOSTED),
|
|
3058
|
+
Gitlab: new Set([...HOSTED, "NestedGroups"]),
|
|
3059
|
+
Git: new Set(COMMON_CAPABILITIES)
|
|
3060
|
+
};
|
|
3061
|
+
function supports(kind, capability) {
|
|
3062
|
+
return CAPABILITIES[kind].has(capability);
|
|
3063
|
+
}
|
|
3064
|
+
function maxDepth(kind) {
|
|
3065
|
+
return supports(kind, "NestedGroups") ? Number.POSITIVE_INFINITY : 2;
|
|
3066
|
+
}
|
|
3067
|
+
|
|
3068
|
+
// ../ts/githost/remote.ts
|
|
3069
|
+
function describeRemoteError(e) {
|
|
3070
|
+
switch (e.kind) {
|
|
3071
|
+
case "empty":
|
|
3072
|
+
return "the remote is empty";
|
|
3073
|
+
case "local-path":
|
|
3074
|
+
return "the remote is a path on this computer, not a repository on a Git host";
|
|
3075
|
+
case "unsupported-scheme":
|
|
3076
|
+
return `the remote's scheme ${e.scheme}: is not https, http, git or ssh`;
|
|
3077
|
+
case "no-host":
|
|
3078
|
+
return "the remote names no host";
|
|
3079
|
+
case "no-path":
|
|
3080
|
+
return "the remote names no owner and repository";
|
|
3081
|
+
case "bad-segment":
|
|
3082
|
+
return `the remote's path is not valid: ${e.reason}`;
|
|
3083
|
+
default:
|
|
3084
|
+
return absurd(e);
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
function hostKindOf(host, hostKinds = {}) {
|
|
3088
|
+
const h = host.toLowerCase();
|
|
3089
|
+
if (h === "github.com")
|
|
3090
|
+
return "Github";
|
|
3091
|
+
if (h === "gitlab.com")
|
|
3092
|
+
return "Gitlab";
|
|
3093
|
+
const named = Object.hasOwn(hostKinds, h) ? hostKinds[h] : undefined;
|
|
3094
|
+
return named ?? "Git";
|
|
3095
|
+
}
|
|
3096
|
+
function safeDecode(segment) {
|
|
3097
|
+
try {
|
|
3098
|
+
return decodeURIComponent(segment);
|
|
3099
|
+
} catch {
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
function refOf(host, rawPath, hostKinds) {
|
|
3104
|
+
const hostName = host.toLowerCase();
|
|
3105
|
+
if (hostName === "")
|
|
3106
|
+
return err({ kind: "no-host" });
|
|
3107
|
+
const trimmed = rawPath.replace(/[?#].*$/s, "").replace(/\/+$/, "");
|
|
3108
|
+
const segments = [];
|
|
3109
|
+
for (const raw of trimmed.split("/")) {
|
|
3110
|
+
if (raw === "")
|
|
3111
|
+
continue;
|
|
3112
|
+
const s = safeDecode(raw);
|
|
3113
|
+
if (s === undefined)
|
|
3114
|
+
return err({ kind: "bad-segment", reason: "a percent escape is not valid" });
|
|
3115
|
+
if (s.includes("/") || s.includes("\\"))
|
|
3116
|
+
return err({ kind: "bad-segment", reason: "a segment holds a separator" });
|
|
3117
|
+
segments.push(s);
|
|
3118
|
+
}
|
|
3119
|
+
const last = segments.at(-1);
|
|
3120
|
+
if (last === undefined)
|
|
3121
|
+
return err({ kind: "no-path" });
|
|
3122
|
+
segments[segments.length - 1] = last.replace(/\.git$/i, "");
|
|
3123
|
+
if (segments.length < 2 || segments.some((s) => s === ""))
|
|
3124
|
+
return err({ kind: "no-path" });
|
|
3125
|
+
return ok({ kind: hostKindOf(hostName, hostKinds), host: hostName, path: segments });
|
|
3126
|
+
}
|
|
3127
|
+
function hostOfAuthority(authority) {
|
|
3128
|
+
const afterAt = authority.slice(authority.lastIndexOf("@") + 1);
|
|
3129
|
+
if (afterAt.startsWith("["))
|
|
3130
|
+
return afterAt.slice(0, afterAt.indexOf("]") + 1);
|
|
3131
|
+
const colon = afterAt.indexOf(":");
|
|
3132
|
+
return colon < 0 ? afterAt : afterAt.slice(0, colon);
|
|
3133
|
+
}
|
|
3134
|
+
function parseRemote(url, hostKinds = {}) {
|
|
3135
|
+
const text = url.trim();
|
|
3136
|
+
if (text === "")
|
|
3137
|
+
return err({ kind: "empty" });
|
|
3138
|
+
const scheme = /^([A-Za-z][A-Za-z0-9+.-]*):\/\//.exec(text);
|
|
3139
|
+
if (scheme !== null) {
|
|
3140
|
+
const name = (scheme[1] ?? "").toLowerCase();
|
|
3141
|
+
if (name === "file")
|
|
3142
|
+
return err({ kind: "local-path" });
|
|
3143
|
+
if (!["https", "http", "git", "ssh", "git+ssh", "ssh+git"].includes(name))
|
|
3144
|
+
return err({ kind: "unsupported-scheme", scheme: name });
|
|
3145
|
+
const rest = text.slice(scheme[0].length);
|
|
3146
|
+
const slash = rest.indexOf("/");
|
|
3147
|
+
const authority = slash < 0 ? rest : rest.slice(0, slash);
|
|
3148
|
+
return refOf(hostOfAuthority(authority), slash < 0 ? "" : rest.slice(slash), hostKinds);
|
|
3149
|
+
}
|
|
3150
|
+
if (/^[A-Za-z]:[\\/]/.test(text) || text.startsWith("/") || text.startsWith(".") || text.startsWith("~"))
|
|
3151
|
+
return err({ kind: "local-path" });
|
|
3152
|
+
const colon = text.indexOf(":");
|
|
3153
|
+
const slash = text.indexOf("/");
|
|
3154
|
+
if (colon < 0 || slash >= 0 && slash < colon)
|
|
3155
|
+
return err({ kind: "local-path" });
|
|
3156
|
+
return refOf(hostOfAuthority(text.slice(0, colon)), text.slice(colon + 1), hostKinds);
|
|
3157
|
+
}
|
|
3158
|
+
var HOSTNAME = /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+(?::\d+)?$/;
|
|
3159
|
+
function webPage(path) {
|
|
3160
|
+
const m = /^(.*?)\/(?:-\/)?(?:tree|blob|commits|raw)\/([^/?#]+)(?:[/?#].*)?$/s.exec(path);
|
|
3161
|
+
if (m === null)
|
|
3162
|
+
return { path };
|
|
3163
|
+
const branch = safeDecode(m[2] ?? "");
|
|
3164
|
+
return branch === undefined ? { path } : { path: m[1] ?? path, branch };
|
|
3165
|
+
}
|
|
3166
|
+
function readRemote(input, hostKinds = {}) {
|
|
3167
|
+
const text = input.trim();
|
|
3168
|
+
const strict = parseRemote(text, hostKinds);
|
|
3169
|
+
const asIs = () => strict.kind === "err" ? strict : ok({ ref: strict.value });
|
|
3170
|
+
const viaPage = (origin, path) => {
|
|
3171
|
+
const page = webPage(path);
|
|
3172
|
+
const r = parseRemote(`${origin}${page.path}`, hostKinds);
|
|
3173
|
+
return r.kind === "err" ? r : ok(page.branch === undefined ? { ref: r.value } : { ref: r.value, branch: page.branch });
|
|
3174
|
+
};
|
|
3175
|
+
const scheme = /^([A-Za-z][A-Za-z0-9+.-]*):\/\/([^/]*)(\/.*)?$/.exec(text);
|
|
3176
|
+
if (scheme !== null)
|
|
3177
|
+
return strict.kind === "err" && scheme[1]?.toLowerCase() === "file" ? strict : viaPage(`${scheme[1]}://${scheme[2]}`, scheme[3] ?? "");
|
|
3178
|
+
if (text === "" || /^[A-Za-z]:[\\/]/.test(text) || /^[/.~]/.test(text))
|
|
3179
|
+
return asIs();
|
|
3180
|
+
const named = /^(github|gitlab):([^/].*)$/i.exec(text);
|
|
3181
|
+
if (named !== null)
|
|
3182
|
+
return viaPage(`https://${(named[1] ?? "").toLowerCase()}.com/`, named[2] ?? "");
|
|
3183
|
+
const colon = text.indexOf(":");
|
|
3184
|
+
const slash = text.indexOf("/");
|
|
3185
|
+
if (colon >= 0 && (slash < 0 || colon < slash))
|
|
3186
|
+
return asIs();
|
|
3187
|
+
const first = slash < 0 ? text : text.slice(0, slash);
|
|
3188
|
+
if (slash >= 0 && HOSTNAME.test(first))
|
|
3189
|
+
return viaPage(`https://${first}`, text.slice(slash));
|
|
3190
|
+
if (/^[^/\s]+\/[^/\s]+$/.test(text))
|
|
3191
|
+
return viaPage("https://github.com/", text);
|
|
3192
|
+
return asIs();
|
|
3193
|
+
}
|
|
3194
|
+
function cloneUrlOf(ref) {
|
|
3195
|
+
return `https://${ref.host}/${ref.path.map(encodeURIComponent).join("/")}.git`;
|
|
3196
|
+
}
|
|
3197
|
+
function describeRef(ref) {
|
|
3198
|
+
return `${ref.host}/${ref.path.join("/")}`;
|
|
3199
|
+
}
|
|
3200
|
+
function sameRepository(a, b) {
|
|
3201
|
+
return a.host.toLowerCase() === b.host.toLowerCase() && a.path.length === b.path.length && a.path.every((s, i) => s.toLowerCase() === (b.path[i] ?? "").toLowerCase());
|
|
3202
|
+
}
|
|
3203
|
+
|
|
3204
|
+
// ../ts/githost/github.ts
|
|
3205
|
+
var repoShape = object({ private: boolean, default_branch: string, clone_url: string, archived: boolean }, { visibility: string });
|
|
3206
|
+
var issueShape = object({ number: integer, title: string, state: literal("open", "closed"), html_url: string }, { body: nullable(string), labels: array(oneOf(string, object({ name: string }))), pull_request: unknownValue });
|
|
3207
|
+
var subIssuesShape = array(object({ number: integer }));
|
|
3208
|
+
var pullShape = object({
|
|
3209
|
+
number: integer,
|
|
3210
|
+
title: string,
|
|
3211
|
+
state: literal("open", "closed"),
|
|
3212
|
+
html_url: string,
|
|
3213
|
+
head: object({ ref: string }),
|
|
3214
|
+
base: object({ ref: string })
|
|
3215
|
+
}, { merged: boolean, merged_at: nullable(string) });
|
|
3216
|
+
var branchShape = object({ name: string, protected: boolean });
|
|
3217
|
+
var protectionShape = object({}, { required_pull_request_reviews: unknownValue, allow_force_pushes: object({ enabled: boolean }) });
|
|
3218
|
+
var commitShape = object({ commit: object({ verification: object({ verified: boolean }) }) });
|
|
3219
|
+
|
|
3220
|
+
// ../ts/githost/gitlab.ts
|
|
3221
|
+
var projectShape = object({ http_url_to_repo: string }, { archived: boolean, visibility: literal("public", "internal", "private"), default_branch: nullable(string) });
|
|
3222
|
+
var issueShape2 = object({ iid: integer, title: string, state: literal("opened", "closed"), web_url: string }, { description: nullable(string), labels: array(string) });
|
|
3223
|
+
var mergeRequestShape = object({
|
|
3224
|
+
iid: integer,
|
|
3225
|
+
title: string,
|
|
3226
|
+
state: literal("opened", "closed", "merged", "locked"),
|
|
3227
|
+
web_url: string,
|
|
3228
|
+
source_branch: string,
|
|
3229
|
+
target_branch: string
|
|
3230
|
+
});
|
|
3231
|
+
var branchShape2 = object({ name: string, protected: boolean });
|
|
3232
|
+
var protectedShape = object({ name: string, allow_force_push: boolean }, { code_owner_approval_required: boolean, push_access_levels: array(object({ access_level: integer })) });
|
|
3233
|
+
var signatureShape = object({ verification_status: string });
|
|
3234
|
+
// src/state.ts
|
|
3235
|
+
var REPO_STATE_KINDS = [
|
|
3236
|
+
"Missing",
|
|
3237
|
+
"Clean",
|
|
3238
|
+
"Dirty",
|
|
3239
|
+
"Ahead",
|
|
3240
|
+
"Behind",
|
|
3241
|
+
"Diverged",
|
|
3242
|
+
"Locked",
|
|
3243
|
+
"Detached",
|
|
3244
|
+
"WrongRemote",
|
|
3245
|
+
"NotARepo"
|
|
3246
|
+
];
|
|
3247
|
+
function defaultActionOf(kind) {
|
|
3248
|
+
switch (kind) {
|
|
3249
|
+
case "Missing":
|
|
3250
|
+
return "Clone";
|
|
3251
|
+
case "Behind":
|
|
3252
|
+
return "FastForward";
|
|
3253
|
+
case "WrongRemote":
|
|
3254
|
+
case "NotARepo":
|
|
3255
|
+
return "Refuse";
|
|
3256
|
+
case "Clean":
|
|
3257
|
+
case "Dirty":
|
|
3258
|
+
case "Ahead":
|
|
3259
|
+
case "Diverged":
|
|
3260
|
+
case "Locked":
|
|
3261
|
+
case "Detached":
|
|
3262
|
+
return "Skip";
|
|
3263
|
+
default:
|
|
3264
|
+
return absurd(kind);
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
function fastForwardable(kind) {
|
|
3268
|
+
return kind === "Behind";
|
|
3269
|
+
}
|
|
3270
|
+
function holdsLocalWork(kind) {
|
|
3271
|
+
switch (kind) {
|
|
3272
|
+
case "Missing":
|
|
3273
|
+
case "Clean":
|
|
3274
|
+
case "Behind":
|
|
3275
|
+
return false;
|
|
3276
|
+
case "Dirty":
|
|
3277
|
+
case "Ahead":
|
|
3278
|
+
case "Diverged":
|
|
3279
|
+
case "Locked":
|
|
3280
|
+
case "Detached":
|
|
3281
|
+
case "WrongRemote":
|
|
3282
|
+
case "NotARepo":
|
|
3283
|
+
return true;
|
|
3284
|
+
default:
|
|
3285
|
+
return absurd(kind);
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
function describeRemote(remote) {
|
|
3289
|
+
if (remote === undefined || remote.trim() === "")
|
|
3290
|
+
return "(none)";
|
|
3291
|
+
const r = parseRemote(remote);
|
|
3292
|
+
return r.kind === "ok" ? describeRef(r.value) : "(not a remote of a host)";
|
|
3293
|
+
}
|
|
3294
|
+
function isExpectedRemote(remote, expected) {
|
|
3295
|
+
if (remote === undefined)
|
|
3296
|
+
return false;
|
|
3297
|
+
const r = parseRemote(remote);
|
|
3298
|
+
return r.kind === "ok" && sameRepository(r.value, expected);
|
|
3299
|
+
}
|
|
3300
|
+
function classify(facts, expected) {
|
|
3301
|
+
switch (facts.kind) {
|
|
3302
|
+
case "absent":
|
|
3303
|
+
return { kind: "Missing" };
|
|
3304
|
+
case "not-a-repository":
|
|
3305
|
+
return { kind: "NotARepo" };
|
|
3306
|
+
case "repository": {
|
|
3307
|
+
if (!isExpectedRemote(facts.remote, expected))
|
|
3308
|
+
return { kind: "WrongRemote", found: describeRemote(facts.remote) };
|
|
3309
|
+
if (facts.lockFile)
|
|
3310
|
+
return { kind: "Locked" };
|
|
3311
|
+
if (facts.detached)
|
|
3312
|
+
return { kind: "Detached" };
|
|
3313
|
+
if (facts.dirty)
|
|
3314
|
+
return { kind: "Dirty" };
|
|
3315
|
+
if (facts.ahead > 0 && facts.behind > 0)
|
|
3316
|
+
return { kind: "Diverged", ahead: facts.ahead, behind: facts.behind };
|
|
3317
|
+
if (facts.ahead > 0)
|
|
3318
|
+
return { kind: "Ahead", commits: facts.ahead };
|
|
3319
|
+
if (facts.behind > 0)
|
|
3320
|
+
return { kind: "Behind", commits: facts.behind };
|
|
3321
|
+
return { kind: "Clean" };
|
|
3322
|
+
}
|
|
3323
|
+
default:
|
|
3324
|
+
return absurd(facts);
|
|
3325
|
+
}
|
|
3326
|
+
}
|
|
3327
|
+
|
|
3328
|
+
// src/plan.ts
|
|
3329
|
+
function isDestructive(action) {
|
|
3330
|
+
const kind = typeof action === "string" ? action : action.kind;
|
|
3331
|
+
switch (kind) {
|
|
3332
|
+
case "RemoveGenerated":
|
|
3333
|
+
return true;
|
|
3334
|
+
case "Clone":
|
|
3335
|
+
case "FastForward":
|
|
3336
|
+
case "CreateWorktree":
|
|
3337
|
+
case "ConfigureRepo":
|
|
3338
|
+
case "ConfigureHost":
|
|
3339
|
+
case "WriteGenerated":
|
|
3340
|
+
case "InstallTool":
|
|
3341
|
+
case "Skip":
|
|
3342
|
+
case "Refuse":
|
|
3343
|
+
return false;
|
|
3344
|
+
default:
|
|
3345
|
+
return absurd(kind);
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
function isHostChange(action) {
|
|
3349
|
+
const kind = typeof action === "string" ? action : action.kind;
|
|
3350
|
+
switch (kind) {
|
|
3351
|
+
case "ConfigureHost":
|
|
3352
|
+
case "InstallTool":
|
|
3353
|
+
return true;
|
|
3354
|
+
case "Clone":
|
|
3355
|
+
case "FastForward":
|
|
3356
|
+
case "CreateWorktree":
|
|
3357
|
+
case "ConfigureRepo":
|
|
3358
|
+
case "WriteGenerated":
|
|
3359
|
+
case "RemoveGenerated":
|
|
3360
|
+
case "Skip":
|
|
3361
|
+
case "Refuse":
|
|
3362
|
+
return false;
|
|
3363
|
+
default:
|
|
3364
|
+
return absurd(kind);
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
function mayRemove(state) {
|
|
3368
|
+
return !holdsLocalWork(state.kind);
|
|
3369
|
+
}
|
|
3370
|
+
function describeAction(action) {
|
|
3371
|
+
switch (action.kind) {
|
|
3372
|
+
case "Clone":
|
|
3373
|
+
return `clone ${action.url} into ${action.target}`;
|
|
3374
|
+
case "FastForward":
|
|
3375
|
+
return `fast-forward ${action.target} by ${action.commits} commit${action.commits === 1 ? "" : "s"}`;
|
|
3376
|
+
case "CreateWorktree":
|
|
3377
|
+
return `add a worktree of ${action.repo} for ${action.branch} at ${action.target}`;
|
|
3378
|
+
case "ConfigureRepo":
|
|
3379
|
+
return `set ${Object.keys(action.settings).sort().join(", ")} in the local Git configuration of ${action.target}`;
|
|
3380
|
+
case "ConfigureHost":
|
|
3381
|
+
return `change the host: ${action.what}`;
|
|
3382
|
+
case "WriteGenerated":
|
|
3383
|
+
return `write the generated file ${action.target}`;
|
|
3384
|
+
case "RemoveGenerated":
|
|
3385
|
+
return `remove the generated file ${action.target}`;
|
|
3386
|
+
case "InstallTool":
|
|
3387
|
+
return `install ${action.tool} ${action.version}`;
|
|
3388
|
+
case "Skip":
|
|
3389
|
+
return `skip ${action.target}: ${describeSkip(action.why)}`;
|
|
3390
|
+
case "Refuse":
|
|
3391
|
+
return `refuse ${action.target}: ${describeRefusal(action.why)}`;
|
|
3392
|
+
default:
|
|
3393
|
+
return absurd(action);
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
function describeSkip(why) {
|
|
3397
|
+
switch (why.kind) {
|
|
3398
|
+
case "in-sync":
|
|
3399
|
+
return "it is up to date";
|
|
3400
|
+
case "uncommitted-changes":
|
|
3401
|
+
return "it has uncommitted changes, which are never touched";
|
|
3402
|
+
case "ahead":
|
|
3403
|
+
return `it has ${why.commits} commit${why.commits === 1 ? "" : "s"} its remote does not`;
|
|
3404
|
+
case "diverged":
|
|
3405
|
+
return `it has ${why.ahead} commit${why.ahead === 1 ? "" : "s"} its remote does not and the remote has ${why.behind} it does not; only a person merges`;
|
|
3406
|
+
case "locked":
|
|
3407
|
+
return "a Git lock file is there, so another process may be working in it; the lock is not removed";
|
|
3408
|
+
case "detached":
|
|
3409
|
+
return "HEAD is on no branch";
|
|
3410
|
+
case "worktree-unneeded":
|
|
3411
|
+
return "nobody else is working in the repository and it has no uncommitted work, so its own checkout is the place to work";
|
|
3412
|
+
case "worktree-exists":
|
|
3413
|
+
return "the worktree is there";
|
|
3414
|
+
case "base-unavailable":
|
|
3415
|
+
return `its repository is ${why.state}`;
|
|
3416
|
+
case "file-current":
|
|
3417
|
+
return "its bytes are the generated ones";
|
|
3418
|
+
case "file-absent":
|
|
3419
|
+
return "it is not there and is not wanted";
|
|
3420
|
+
default:
|
|
3421
|
+
return absurd(why);
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
function describeRefusal(why) {
|
|
3425
|
+
switch (why.kind) {
|
|
3426
|
+
case "wrong-remote":
|
|
3427
|
+
return `a repository of ${why.found} is at that path, not the one the manifest names, and it is never overwritten`;
|
|
3428
|
+
case "not-a-repository":
|
|
3429
|
+
return "something that is not a Git repository is at that path, and it is never overwritten";
|
|
3430
|
+
case "generated-edited":
|
|
3431
|
+
return "its bytes are not the ones the state record holds, so it was edited by hand; `--force` replaces it";
|
|
3432
|
+
default:
|
|
3433
|
+
return absurd(why);
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
function forState(repo, state) {
|
|
3437
|
+
const target = repo.path;
|
|
3438
|
+
switch (state.kind) {
|
|
3439
|
+
case "Missing":
|
|
3440
|
+
return { kind: "Clone", target, url: repo.cloneUrl, branch: repo.branch };
|
|
3441
|
+
case "Behind":
|
|
3442
|
+
return { kind: "FastForward", target, commits: state.commits };
|
|
3443
|
+
case "Clean":
|
|
3444
|
+
return { kind: "Skip", target, why: { kind: "in-sync" } };
|
|
3445
|
+
case "Dirty":
|
|
3446
|
+
return { kind: "Skip", target, why: { kind: "uncommitted-changes" } };
|
|
3447
|
+
case "Ahead":
|
|
3448
|
+
return {
|
|
3449
|
+
kind: "Skip",
|
|
3450
|
+
target,
|
|
3451
|
+
why: { kind: "ahead", commits: state.commits }
|
|
3452
|
+
};
|
|
3453
|
+
case "Diverged":
|
|
3454
|
+
return {
|
|
3455
|
+
kind: "Skip",
|
|
3456
|
+
target,
|
|
3457
|
+
why: { kind: "diverged", ahead: state.ahead, behind: state.behind }
|
|
3458
|
+
};
|
|
3459
|
+
case "Locked":
|
|
3460
|
+
return { kind: "Skip", target, why: { kind: "locked" } };
|
|
3461
|
+
case "Detached":
|
|
3462
|
+
return { kind: "Skip", target, why: { kind: "detached" } };
|
|
3463
|
+
case "WrongRemote":
|
|
3464
|
+
return {
|
|
3465
|
+
kind: "Refuse",
|
|
3466
|
+
target,
|
|
3467
|
+
why: { kind: "wrong-remote", found: state.found }
|
|
3468
|
+
};
|
|
3469
|
+
case "NotARepo":
|
|
3470
|
+
return { kind: "Refuse", target, why: { kind: "not-a-repository" } };
|
|
3471
|
+
default:
|
|
3472
|
+
return absurd(state);
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
function takesConfiguration(state) {
|
|
3476
|
+
switch (state.kind) {
|
|
3477
|
+
case "Missing":
|
|
3478
|
+
case "Clean":
|
|
3479
|
+
case "Dirty":
|
|
3480
|
+
case "Ahead":
|
|
3481
|
+
case "Behind":
|
|
3482
|
+
case "Diverged":
|
|
3483
|
+
case "Detached":
|
|
3484
|
+
return true;
|
|
3485
|
+
case "Locked":
|
|
3486
|
+
case "WrongRemote":
|
|
3487
|
+
case "NotARepo":
|
|
3488
|
+
return false;
|
|
3489
|
+
default:
|
|
3490
|
+
return absurd(state);
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
var ABSENT = {
|
|
3494
|
+
state: { kind: "Missing" },
|
|
3495
|
+
config: {},
|
|
3496
|
+
busy: false
|
|
3497
|
+
};
|
|
3498
|
+
var byPath = (a, b) => foldKey(a.path) < foldKey(b.path) ? -1 : foldKey(a.path) > foldKey(b.path) ? 1 : a.path < b.path ? -1 : a.path > b.path ? 1 : 0;
|
|
3499
|
+
function settingsToSet(repo, observed) {
|
|
3500
|
+
const diff = {};
|
|
3501
|
+
for (const key of Object.keys(repo.settings).sort()) {
|
|
3502
|
+
const want = repo.settings[key];
|
|
3503
|
+
if (want !== undefined && observed.config[key] !== want)
|
|
3504
|
+
diff[key] = want;
|
|
3505
|
+
}
|
|
3506
|
+
return Object.keys(diff).length === 0 ? undefined : diff;
|
|
3507
|
+
}
|
|
3508
|
+
function plan(desired, observed, options = {}) {
|
|
3509
|
+
const actions = [];
|
|
3510
|
+
for (const repo of [...desired.repos].sort(byPath)) {
|
|
3511
|
+
const seen = Object.hasOwn(observed.repos, repo.path) ? observed.repos[repo.path] ?? ABSENT : ABSENT;
|
|
3512
|
+
actions.push(forState(repo, seen.state));
|
|
3513
|
+
const settings = takesConfiguration(seen.state) ? settingsToSet(repo, seen) : undefined;
|
|
3514
|
+
if (settings !== undefined)
|
|
3515
|
+
actions.push({ kind: "ConfigureRepo", target: repo.path, settings });
|
|
3516
|
+
}
|
|
3517
|
+
for (const wt of [...desired.worktrees].sort(byPath)) {
|
|
3518
|
+
const there = Object.hasOwn(observed.worktrees, wt.path) ? observed.worktrees[wt.path] : undefined;
|
|
3519
|
+
if (there !== undefined && there.kind !== "Missing") {
|
|
3520
|
+
actions.push({
|
|
3521
|
+
kind: "Skip",
|
|
3522
|
+
target: wt.path,
|
|
3523
|
+
why: { kind: "worktree-exists" }
|
|
3524
|
+
});
|
|
3525
|
+
continue;
|
|
3526
|
+
}
|
|
3527
|
+
const base = Object.hasOwn(observed.repos, wt.repo) ? observed.repos[wt.repo] ?? ABSENT : ABSENT;
|
|
3528
|
+
switch (base.state.kind) {
|
|
3529
|
+
case "Missing":
|
|
3530
|
+
actions.push({
|
|
3531
|
+
kind: "Skip",
|
|
3532
|
+
target: wt.path,
|
|
3533
|
+
why: { kind: "worktree-unneeded" }
|
|
3534
|
+
});
|
|
3535
|
+
break;
|
|
3536
|
+
case "Locked":
|
|
3537
|
+
case "WrongRemote":
|
|
3538
|
+
case "NotARepo":
|
|
3539
|
+
actions.push({
|
|
3540
|
+
kind: "Skip",
|
|
3541
|
+
target: wt.path,
|
|
3542
|
+
why: { kind: "base-unavailable", state: base.state.kind }
|
|
3543
|
+
});
|
|
3544
|
+
break;
|
|
3545
|
+
case "Dirty":
|
|
3546
|
+
actions.push({
|
|
3547
|
+
kind: "CreateWorktree",
|
|
3548
|
+
target: wt.path,
|
|
3549
|
+
repo: wt.repo,
|
|
3550
|
+
branch: wt.branch
|
|
3551
|
+
});
|
|
3552
|
+
break;
|
|
3553
|
+
case "Clean":
|
|
3554
|
+
case "Ahead":
|
|
3555
|
+
case "Behind":
|
|
3556
|
+
case "Diverged":
|
|
3557
|
+
case "Detached":
|
|
3558
|
+
actions.push(base.busy ? {
|
|
3559
|
+
kind: "CreateWorktree",
|
|
3560
|
+
target: wt.path,
|
|
3561
|
+
repo: wt.repo,
|
|
3562
|
+
branch: wt.branch
|
|
3563
|
+
} : {
|
|
3564
|
+
kind: "Skip",
|
|
3565
|
+
target: wt.path,
|
|
3566
|
+
why: { kind: "worktree-unneeded" }
|
|
3567
|
+
});
|
|
3568
|
+
break;
|
|
3569
|
+
default:
|
|
3570
|
+
return absurd(base.state);
|
|
3571
|
+
}
|
|
3572
|
+
}
|
|
3573
|
+
const wanted = new Set(desired.files.map((f) => f.path));
|
|
3574
|
+
const files = [];
|
|
3575
|
+
for (const f of desired.files) {
|
|
3576
|
+
const seen = Object.hasOwn(observed.files, f.path) ? observed.files[f.path] ?? {} : {};
|
|
3577
|
+
if (seen.onDisk === f.sha256)
|
|
3578
|
+
files.push({
|
|
3579
|
+
kind: "Skip",
|
|
3580
|
+
target: f.path,
|
|
3581
|
+
why: { kind: "file-current" }
|
|
3582
|
+
});
|
|
3583
|
+
else if (seen.onDisk === undefined || seen.onDisk === seen.recorded)
|
|
3584
|
+
files.push({ kind: "WriteGenerated", target: f.path, sha256: f.sha256 });
|
|
3585
|
+
else
|
|
3586
|
+
files.push({
|
|
3587
|
+
kind: "Refuse",
|
|
3588
|
+
target: f.path,
|
|
3589
|
+
why: { kind: "generated-edited" }
|
|
3590
|
+
});
|
|
3591
|
+
}
|
|
3592
|
+
for (const [path, seen] of Object.entries(observed.files)) {
|
|
3593
|
+
if (wanted.has(path) || seen.recorded === undefined)
|
|
3594
|
+
continue;
|
|
3595
|
+
const target = path;
|
|
3596
|
+
if (seen.onDisk === undefined)
|
|
3597
|
+
files.push({ kind: "Skip", target, why: { kind: "file-absent" } });
|
|
3598
|
+
else if (seen.onDisk === seen.recorded)
|
|
3599
|
+
files.push({ kind: "RemoveGenerated", target });
|
|
3600
|
+
else
|
|
3601
|
+
files.push({ kind: "Refuse", target, why: { kind: "generated-edited" } });
|
|
3602
|
+
}
|
|
3603
|
+
actions.push(...files.sort((a, b) => foldKey(a.target) < foldKey(b.target) ? -1 : foldKey(a.target) > foldKey(b.target) ? 1 : 0));
|
|
3604
|
+
if (options.setupApply === true)
|
|
3605
|
+
for (const c of desired.hostChanges)
|
|
3606
|
+
actions.push({ ...c });
|
|
3607
|
+
return { actions };
|
|
3608
|
+
}
|
|
3609
|
+
async function planDigest(p) {
|
|
3610
|
+
return await sha256(JSON.stringify(p.actions));
|
|
3611
|
+
}
|
|
3612
|
+
function isSettled(p) {
|
|
3613
|
+
return p.actions.every((a) => a.kind === "Skip");
|
|
3614
|
+
}
|
|
3615
|
+
// src/types.ts
|
|
3616
|
+
function describeBrandError(e) {
|
|
3617
|
+
switch (e.kind) {
|
|
3618
|
+
case "git-host":
|
|
3619
|
+
return `${JSON.stringify(e.text)} is not a Git host name: ${e.reason}`;
|
|
3620
|
+
case "branch-name":
|
|
3621
|
+
return `${JSON.stringify(e.text)} is not a branch name: ${e.reason}`;
|
|
3622
|
+
case "sha":
|
|
3623
|
+
return `${JSON.stringify(e.text)} is not a commit id (40 or 64 lower-case hexadecimal digits)`;
|
|
3624
|
+
case "version":
|
|
3625
|
+
return `${JSON.stringify(e.text)} is not a pinned version: ${e.reason}`;
|
|
3626
|
+
default:
|
|
3627
|
+
return absurd(e);
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
function gitHost(text) {
|
|
3631
|
+
const host = text.toLowerCase();
|
|
3632
|
+
if (host === "")
|
|
3633
|
+
return err({ kind: "git-host", text, reason: "it is empty" });
|
|
3634
|
+
if (!/^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$/.test(host)) {
|
|
3635
|
+
return err({
|
|
3636
|
+
kind: "git-host",
|
|
3637
|
+
text,
|
|
3638
|
+
reason: "it holds a character a host name does not (a port, a path, a scheme, a credential and non-ASCII letters are not part of the name)"
|
|
3639
|
+
});
|
|
3640
|
+
}
|
|
3641
|
+
if (host.includes(".."))
|
|
3642
|
+
return err({ kind: "git-host", text, reason: "it holds an empty label" });
|
|
3643
|
+
return ok(host);
|
|
3644
|
+
}
|
|
3645
|
+
function branchName(text) {
|
|
3646
|
+
const bad = (reason) => err({ kind: "branch-name", text, reason });
|
|
3647
|
+
if (text === "")
|
|
3648
|
+
return bad("it is empty");
|
|
3649
|
+
if (text === "@")
|
|
3650
|
+
return bad("it is `@`");
|
|
3651
|
+
if (text.startsWith("-"))
|
|
3652
|
+
return bad("it starts with `-`");
|
|
3653
|
+
if (text.startsWith("/") || text.endsWith("/") || text.includes("//"))
|
|
3654
|
+
return bad("it has an empty component");
|
|
3655
|
+
if (text.endsWith("."))
|
|
3656
|
+
return bad("it ends in a dot");
|
|
3657
|
+
if (text.includes(".."))
|
|
3658
|
+
return bad("it holds `..`");
|
|
3659
|
+
if (text.includes("@{"))
|
|
3660
|
+
return bad("it holds `@{`");
|
|
3661
|
+
if (/[\u0000- \u007f~^:?*[\\]/.test(text))
|
|
3662
|
+
return bad("it holds a control character, a space or one of ~ ^ : ? * [ \\");
|
|
3663
|
+
for (const part of text.split("/")) {
|
|
3664
|
+
if (part.startsWith("."))
|
|
3665
|
+
return bad("a component starts with a dot");
|
|
3666
|
+
if (part.endsWith(".lock"))
|
|
3667
|
+
return bad("a component ends in `.lock`");
|
|
3668
|
+
}
|
|
3669
|
+
return ok(text);
|
|
3670
|
+
}
|
|
3671
|
+
function sha(text) {
|
|
3672
|
+
return /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(text) ? ok(text) : err({ kind: "sha", text });
|
|
3673
|
+
}
|
|
3674
|
+
function version(text) {
|
|
3675
|
+
if (text === "latest")
|
|
3676
|
+
return err({ kind: "version", text, reason: "`latest` is not a version" });
|
|
3677
|
+
if (!/^\d/.test(text))
|
|
3678
|
+
return err({
|
|
3679
|
+
kind: "version",
|
|
3680
|
+
text,
|
|
3681
|
+
reason: "it is not major.minor.patch (a range or a tag is not a pin)"
|
|
3682
|
+
});
|
|
3683
|
+
const parsed = parseSemVer(text);
|
|
3684
|
+
if (parsed.kind === "err")
|
|
3685
|
+
return err({
|
|
3686
|
+
kind: "version",
|
|
3687
|
+
text,
|
|
3688
|
+
reason: "it is not major.minor.patch with an optional pre-release or build"
|
|
3689
|
+
});
|
|
3690
|
+
return ok(text);
|
|
3691
|
+
}
|
|
3692
|
+
|
|
3693
|
+
// src/layout.ts
|
|
3694
|
+
var WORKSPACE_FOLDERS = [".worktrees", ".ergonex", ".git"];
|
|
3695
|
+
function describeLayoutError(e) {
|
|
3696
|
+
switch (e.kind) {
|
|
3697
|
+
case "portable":
|
|
3698
|
+
return describePortableError(e.cause);
|
|
3699
|
+
case "host":
|
|
3700
|
+
return describeBrandError(e.cause);
|
|
3701
|
+
case "workspace-folder":
|
|
3702
|
+
return `${JSON.stringify(e.segment)} is named like a folder of the workspace (${WORKSPACE_FOLDERS.join(", ")}), which a remote may not be`;
|
|
3703
|
+
case "too-deep":
|
|
3704
|
+
return `${e.path.join("/")} has ${e.path.length} segments, and ${e.manager} offers no nested groups: at most ${e.limit} (an owner and a repository)`;
|
|
3705
|
+
case "too-shallow":
|
|
3706
|
+
return `${e.path.join("/")} is not an owner and a repository`;
|
|
3707
|
+
case "not-in-layout":
|
|
3708
|
+
return `${e.path} is not a repository path of the workspace`;
|
|
3709
|
+
default:
|
|
3710
|
+
return absurd(e);
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
var portable = (cause) => ({
|
|
3714
|
+
kind: "portable",
|
|
3715
|
+
cause
|
|
3716
|
+
});
|
|
3717
|
+
function notWorkspaceFolder(name) {
|
|
3718
|
+
return WORKSPACE_FOLDERS.some((f) => foldKey(f) === foldKey(name)) ? err({ kind: "workspace-folder", segment: name }) : ok(name);
|
|
3719
|
+
}
|
|
3720
|
+
function gitHostOf(ref) {
|
|
3721
|
+
const h = gitHost(ref.host);
|
|
3722
|
+
if (h.kind === "err")
|
|
3723
|
+
return err({ kind: "host", cause: h.error });
|
|
3724
|
+
const free = notWorkspaceFolder(h.value);
|
|
3725
|
+
if (free.kind === "err")
|
|
3726
|
+
return free;
|
|
3727
|
+
const s = segment(h.value);
|
|
3728
|
+
return s.kind === "err" ? err(portable(s.error)) : h;
|
|
3729
|
+
}
|
|
3730
|
+
function segmentsOfRef(ref) {
|
|
3731
|
+
if (ref.path.length < 2)
|
|
3732
|
+
return err({ kind: "too-shallow", path: ref.path });
|
|
3733
|
+
const limit = maxDepth(ref.kind);
|
|
3734
|
+
if (ref.path.length > limit)
|
|
3735
|
+
return err({ kind: "too-deep", manager: ref.kind, path: ref.path, limit });
|
|
3736
|
+
const host = gitHostOf(ref);
|
|
3737
|
+
if (host.kind === "err")
|
|
3738
|
+
return host;
|
|
3739
|
+
for (const s of ref.path) {
|
|
3740
|
+
const free = notWorkspaceFolder(s);
|
|
3741
|
+
if (free.kind === "err")
|
|
3742
|
+
return free;
|
|
3743
|
+
}
|
|
3744
|
+
return ok([host.value, ...ref.path]);
|
|
3745
|
+
}
|
|
3746
|
+
function repoPathOf(ref) {
|
|
3747
|
+
const segments = segmentsOfRef(ref);
|
|
3748
|
+
if (segments.kind === "err")
|
|
3749
|
+
return segments;
|
|
3750
|
+
const p = repoPath(segments.value);
|
|
3751
|
+
return p.kind === "err" ? err(portable(p.error)) : p;
|
|
3752
|
+
}
|
|
3753
|
+
function slugOf(text) {
|
|
3754
|
+
const replaced = text.normalize("NFC").replace(/[\u0000-\u001f\u007f<>:"/\\|?*~^[\]\s]+/g, "-");
|
|
3755
|
+
let slug = replaced.replace(/-{2,}/g, "-").replace(/[-. ]+$/, "").replace(/^[-.]+/, "");
|
|
3756
|
+
if (isDeviceName(slug)) {
|
|
3757
|
+
const dot = slug.indexOf(".");
|
|
3758
|
+
slug = dot < 0 ? `${slug}_` : `${slug.slice(0, dot)}_${slug.slice(dot)}`;
|
|
3759
|
+
}
|
|
3760
|
+
return segment(slug);
|
|
3761
|
+
}
|
|
3762
|
+
function worktreePathOf(ref, branch) {
|
|
3763
|
+
const segments = segmentsOfRef(ref);
|
|
3764
|
+
if (segments.kind === "err")
|
|
3765
|
+
return segments;
|
|
3766
|
+
const slug = slugOf(branch);
|
|
3767
|
+
if (slug.kind === "err")
|
|
3768
|
+
return err(portable(slug.error));
|
|
3769
|
+
const p = repoPath([".worktrees", ...segments.value, slug.value]);
|
|
3770
|
+
return p.kind === "err" ? err(portable(p.error)) : p;
|
|
3771
|
+
}
|
|
3772
|
+
function refOfPath(path, hostKinds = {}) {
|
|
3773
|
+
const [host, ...rest] = path.split("/");
|
|
3774
|
+
if (host === undefined || rest.length < 2 || WORKSPACE_FOLDERS.some((f) => foldKey(f) === foldKey(host)))
|
|
3775
|
+
return err({ kind: "not-in-layout", path });
|
|
3776
|
+
return ok({ kind: hostKindOf(host, hostKinds), host, path: rest });
|
|
3777
|
+
}
|
|
3778
|
+
function homeOf(facts) {
|
|
3779
|
+
const nonEmpty = (s) => s === undefined || s.trim() === "" ? undefined : s;
|
|
3780
|
+
const explicit = nonEmpty(facts.override) ?? nonEmpty(facts.workspacesHome);
|
|
3781
|
+
if (explicit !== undefined)
|
|
3782
|
+
return ok(explicit);
|
|
3783
|
+
const home = nonEmpty(facts.home);
|
|
3784
|
+
if (home === undefined)
|
|
3785
|
+
return err({ kind: "no-home" });
|
|
3786
|
+
return ok(`${home.replace(/[\\/]+$/, "")}${facts.windows ? "\\" : "/"}workspaces`);
|
|
3787
|
+
}
|
|
3788
|
+
function worktreeWanted(facts) {
|
|
3789
|
+
return facts.uncommittedWork || facts.otherWorker;
|
|
3790
|
+
}
|
|
3791
|
+
// src/tools/catalog.ts
|
|
3792
|
+
var PROBED_TOOLS = ["git", "bun", "node"];
|
|
3793
|
+
var artifactShape = object({
|
|
3794
|
+
os: literal("linux", "macos", "windows"),
|
|
3795
|
+
arch: literal("x64", "arm64"),
|
|
3796
|
+
url: string,
|
|
3797
|
+
sha256: string
|
|
3798
|
+
}, { kind: literal("file", "tar.gz", "zip"), bin: string });
|
|
3799
|
+
var toolShape = object({ name: string, version: string, artifacts: array(artifactShape) }, { provides: array(string) });
|
|
3800
|
+
var catalogShape = array(toolShape);
|
|
3801
|
+
var NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
3802
|
+
function safeInnerPath(path) {
|
|
3803
|
+
if (path === "" || path.startsWith("/") || path.includes("\\") || /^[A-Za-z]:/.test(path))
|
|
3804
|
+
return false;
|
|
3805
|
+
return path.split("/").every((p) => p !== "" && p !== "." && p !== "..");
|
|
3806
|
+
}
|
|
3807
|
+
function urlProblem(text, loopbackHttp = false) {
|
|
3808
|
+
let url;
|
|
3809
|
+
try {
|
|
3810
|
+
url = new URL(text);
|
|
3811
|
+
} catch {
|
|
3812
|
+
return "it is not a URL";
|
|
3813
|
+
}
|
|
3814
|
+
const loopback = loopbackHttp && url.protocol === "http:" && (url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname === "[::1]");
|
|
3815
|
+
if (url.protocol !== "https:" && !loopback)
|
|
3816
|
+
return "a pinned artifact is fetched over https only";
|
|
3817
|
+
if (url.username !== "" || url.password !== "")
|
|
3818
|
+
return "it holds a credential";
|
|
3819
|
+
if (/[?&](key|api_key|apikey|access_token|token|password|passwd|signature|sig)=/i.test(url.search))
|
|
3820
|
+
return "its query holds a credential";
|
|
3821
|
+
if (url.pathname.split("/").some((p) => p.toLowerCase() === "latest"))
|
|
3822
|
+
return "it names `latest`, which is not a version";
|
|
3823
|
+
return;
|
|
3824
|
+
}
|
|
3825
|
+
function decodeCatalog(input, loopbackHttp = false) {
|
|
3826
|
+
const shaped = catalogShape(input, "$.tools");
|
|
3827
|
+
if (shaped.kind === "err") {
|
|
3828
|
+
return err([
|
|
3829
|
+
{
|
|
3830
|
+
rule: "ergonex/catalog-shape",
|
|
3831
|
+
path: shaped.error.path,
|
|
3832
|
+
message: `the tool catalog is not what is expected: ${formatDecodeError(shaped.error)}`
|
|
3833
|
+
}
|
|
3834
|
+
]);
|
|
3835
|
+
}
|
|
3836
|
+
const issues = [];
|
|
3837
|
+
const tools = [];
|
|
3838
|
+
const seenTool = new Set;
|
|
3839
|
+
for (const [i, raw] of shaped.value.entries()) {
|
|
3840
|
+
const at = `$.tools[${i}]`;
|
|
3841
|
+
const bad = (path, message) => issues.push({ rule: "ergonex/catalog-tool", path, message });
|
|
3842
|
+
if (!NAME.test(raw.name))
|
|
3843
|
+
bad(`${at}.name`, `${JSON.stringify(raw.name)} is not a tool name (lower-case letters, digits, \`.\`, \`_\` and \`-\`)`);
|
|
3844
|
+
else if (PROBED_TOOLS.includes(raw.name))
|
|
3845
|
+
bad(`${at}.name`, `${raw.name} is verified by a probe of its version and never installed, so it is not a tool of the catalog (0002-workspace-model FR-038)`);
|
|
3846
|
+
else if (seenTool.has(raw.name))
|
|
3847
|
+
bad(`${at}.name`, `${raw.name} is in the catalog twice; one pinned version each`);
|
|
3848
|
+
seenTool.add(raw.name);
|
|
3849
|
+
const v = version(raw.version);
|
|
3850
|
+
if (v.kind === "err")
|
|
3851
|
+
bad(`${at}.version`, describeBrandError(v.error));
|
|
3852
|
+
const artifacts = [];
|
|
3853
|
+
const seen = new Set;
|
|
3854
|
+
for (const [j, a] of raw.artifacts.entries()) {
|
|
3855
|
+
const where = `${at}.artifacts[${j}]`;
|
|
3856
|
+
const problem = urlProblem(a.url, loopbackHttp);
|
|
3857
|
+
if (problem !== undefined)
|
|
3858
|
+
bad(`${where}.url`, `the URL is not a pin: ${problem}`);
|
|
3859
|
+
const sum = sha256Of(a.sha256);
|
|
3860
|
+
if (sum === undefined)
|
|
3861
|
+
bad(`${where}.sha256`, "the hash is not a SHA-256 (64 lower-case hexadecimal digits)");
|
|
3862
|
+
const kind = a.kind ?? "file";
|
|
3863
|
+
const bin = a.bin ?? (kind === "file" ? a.url.split("/").pop() ?? "" : "");
|
|
3864
|
+
if (!safeInnerPath(bin))
|
|
3865
|
+
bad(`${where}.bin`, `${JSON.stringify(bin)} is not a safe path to the executable (name it, relative, with \`/\` between parts and no \`..\`)`);
|
|
3866
|
+
const key = `${a.os}/${a.arch}`;
|
|
3867
|
+
if (seen.has(key))
|
|
3868
|
+
bad(`${where}`, `${raw.name} has two artifacts for ${key}`);
|
|
3869
|
+
seen.add(key);
|
|
3870
|
+
if (problem === undefined && sum !== undefined && safeInnerPath(bin))
|
|
3871
|
+
artifacts.push({
|
|
3872
|
+
os: a.os,
|
|
3873
|
+
arch: a.arch,
|
|
3874
|
+
url: a.url,
|
|
3875
|
+
sha256: sum,
|
|
3876
|
+
kind,
|
|
3877
|
+
bin
|
|
3878
|
+
});
|
|
3879
|
+
}
|
|
3880
|
+
if (v.kind === "ok")
|
|
3881
|
+
tools.push({
|
|
3882
|
+
name: raw.name,
|
|
3883
|
+
version: v.value,
|
|
3884
|
+
provides: raw.provides ?? [raw.name],
|
|
3885
|
+
artifacts
|
|
3886
|
+
});
|
|
3887
|
+
}
|
|
3888
|
+
return issues.length > 0 ? err(issues) : ok({ tools });
|
|
3889
|
+
}
|
|
3890
|
+
|
|
3891
|
+
// src/manifest.ts
|
|
3892
|
+
var repositoryShape = object({ remote: string }, { branch: string, group: string, worktrees: array(string) });
|
|
3893
|
+
var manifestShape = object({ repositories: array(repositoryShape) }, {
|
|
3894
|
+
gitlabHosts: array(string),
|
|
3895
|
+
policy: object({}, {
|
|
3896
|
+
requiresSignedCommits: boolean,
|
|
3897
|
+
regulatedDomain: array(string),
|
|
3898
|
+
unapprovedGraceDays: integer,
|
|
3899
|
+
allowedGitHost: array(string)
|
|
3900
|
+
}),
|
|
3901
|
+
identity: object({ name: string, email: string }),
|
|
3902
|
+
signing: object({ format: string }, { key: string }),
|
|
3903
|
+
direnv: boolean,
|
|
3904
|
+
tools: unknownValue,
|
|
3905
|
+
modules: array(object({ name: string, program: string }, { sha256: string, summary: string }))
|
|
3906
|
+
});
|
|
3907
|
+
function issueFinding(issue, today) {
|
|
3908
|
+
return {
|
|
3909
|
+
ruleId: issue.rule,
|
|
3910
|
+
severity: "Error",
|
|
3911
|
+
message: issue.message,
|
|
3912
|
+
subject: issue.path,
|
|
3913
|
+
firstSeen: today
|
|
3914
|
+
};
|
|
3915
|
+
}
|
|
3916
|
+
function decodeManifest(input, options = {}) {
|
|
3917
|
+
const shaped = manifestShape(input);
|
|
3918
|
+
if (shaped.kind === "err") {
|
|
3919
|
+
return err([
|
|
3920
|
+
{
|
|
3921
|
+
rule: "ergonex/manifest-shape",
|
|
3922
|
+
path: shaped.error.path,
|
|
3923
|
+
message: `the manifest is not what is expected: ${formatDecodeError(shaped.error)}`
|
|
3924
|
+
}
|
|
3925
|
+
]);
|
|
3926
|
+
}
|
|
3927
|
+
const raw = shaped.value;
|
|
3928
|
+
const issues = [];
|
|
3929
|
+
const hostKinds = {};
|
|
3930
|
+
for (const [i, h] of (raw.gitlabHosts ?? []).entries()) {
|
|
3931
|
+
const host = gitHost(h);
|
|
3932
|
+
if (host.kind === "err")
|
|
3933
|
+
issues.push({
|
|
3934
|
+
rule: "ergonex/manifest-host",
|
|
3935
|
+
path: `$.gitlabHosts[${i}]`,
|
|
3936
|
+
message: describeBrandError(host.error)
|
|
3937
|
+
});
|
|
3938
|
+
else if (host.value === "github.com")
|
|
3939
|
+
issues.push({
|
|
3940
|
+
rule: "ergonex/manifest-host",
|
|
3941
|
+
path: `$.gitlabHosts[${i}]`,
|
|
3942
|
+
message: "github.com is GitHub, not GitLab"
|
|
3943
|
+
});
|
|
3944
|
+
else
|
|
3945
|
+
hostKinds[host.value] = "Gitlab";
|
|
3946
|
+
}
|
|
3947
|
+
let signing;
|
|
3948
|
+
if (raw.signing !== undefined) {
|
|
3949
|
+
if (raw.signing.format === "openpgp" || raw.signing.format === "ssh")
|
|
3950
|
+
signing = { format: raw.signing.format, key: raw.signing.key };
|
|
3951
|
+
else
|
|
3952
|
+
issues.push({
|
|
3953
|
+
rule: "ergonex/manifest-signing",
|
|
3954
|
+
path: "$.signing.format",
|
|
3955
|
+
message: `the signing format is ${JSON.stringify(raw.signing.format)}; Git signs with openpgp or ssh`
|
|
3956
|
+
});
|
|
3957
|
+
}
|
|
3958
|
+
const modules = [];
|
|
3959
|
+
const seenModules = new Set;
|
|
3960
|
+
for (const [i, m] of (raw.modules ?? []).entries()) {
|
|
3961
|
+
const at = `$.modules[${i}]`;
|
|
3962
|
+
const name = moduleName(m.name);
|
|
3963
|
+
if (name.kind === "err")
|
|
3964
|
+
issues.push({
|
|
3965
|
+
rule: "ergonex/manifest-module",
|
|
3966
|
+
path: `${at}.name`,
|
|
3967
|
+
message: describeNameError(name.error)
|
|
3968
|
+
});
|
|
3969
|
+
else if (seenModules.has(name.value))
|
|
3970
|
+
issues.push({
|
|
3971
|
+
rule: "ergonex/manifest-module",
|
|
3972
|
+
path: `${at}.name`,
|
|
3973
|
+
message: `the module ${name.value} is registered twice`
|
|
3974
|
+
});
|
|
3975
|
+
else
|
|
3976
|
+
seenModules.add(name.value);
|
|
3977
|
+
if (m.sha256 !== undefined && sha256Of(m.sha256) === undefined)
|
|
3978
|
+
issues.push({
|
|
3979
|
+
rule: "ergonex/manifest-module",
|
|
3980
|
+
path: `${at}.sha256`,
|
|
3981
|
+
message: "the SHA-256 of a module's program is 64 hexadecimal digits"
|
|
3982
|
+
});
|
|
3983
|
+
modules.push({
|
|
3984
|
+
name: m.name,
|
|
3985
|
+
program: m.program,
|
|
3986
|
+
sha256: m.sha256,
|
|
3987
|
+
summary: m.summary
|
|
3988
|
+
});
|
|
3989
|
+
}
|
|
3990
|
+
let catalog = { tools: [] };
|
|
3991
|
+
if (raw.tools !== undefined) {
|
|
3992
|
+
const c = decodeCatalog(raw.tools, options.loopbackHttp === true);
|
|
3993
|
+
if (c.kind === "ok")
|
|
3994
|
+
catalog = c.value;
|
|
3995
|
+
else
|
|
3996
|
+
for (const i of c.error)
|
|
3997
|
+
issues.push({ rule: i.rule, path: i.path, message: i.message });
|
|
3998
|
+
}
|
|
3999
|
+
const policy = policyOf(raw.policy);
|
|
4000
|
+
const repositories = [];
|
|
4001
|
+
const named = [];
|
|
4002
|
+
for (const [i, entry] of raw.repositories.entries()) {
|
|
4003
|
+
const at = `$.repositories[${i}]`;
|
|
4004
|
+
const remote = readRemote(entry.remote, hostKinds);
|
|
4005
|
+
if (remote.kind === "err") {
|
|
4006
|
+
issues.push({
|
|
4007
|
+
rule: "ergonex/manifest-remote",
|
|
4008
|
+
path: `${at}.remote`,
|
|
4009
|
+
message: `the remote is not a repository on a Git host: ${describeRemoteError(remote.error)}`
|
|
4010
|
+
});
|
|
4011
|
+
continue;
|
|
4012
|
+
}
|
|
4013
|
+
const ref = remote.value.ref;
|
|
4014
|
+
const path = repoPathOf(ref);
|
|
4015
|
+
if (path.kind === "err") {
|
|
4016
|
+
issues.push({
|
|
4017
|
+
rule: "ergonex/manifest-path",
|
|
4018
|
+
path: `${at}.remote`,
|
|
4019
|
+
message: describeLayoutError(path.error)
|
|
4020
|
+
});
|
|
4021
|
+
continue;
|
|
4022
|
+
}
|
|
4023
|
+
if (!hostAllowed(policy, ref.host)) {
|
|
4024
|
+
issues.push({
|
|
4025
|
+
rule: "ergonex/host-not-allowed",
|
|
4026
|
+
path: `${at}.remote`,
|
|
4027
|
+
message: `the host ${ref.host} is not one of the policy's allowed Git hosts (${policy.allowedGitHost.join(", ")})`
|
|
4028
|
+
});
|
|
4029
|
+
}
|
|
4030
|
+
let branch;
|
|
4031
|
+
const wantedBranch = entry.branch ?? remote.value.branch;
|
|
4032
|
+
if (wantedBranch !== undefined) {
|
|
4033
|
+
const b = branchName(wantedBranch);
|
|
4034
|
+
if (b.kind === "err")
|
|
4035
|
+
issues.push({
|
|
4036
|
+
rule: "ergonex/manifest-branch",
|
|
4037
|
+
path: `${at}.branch`,
|
|
4038
|
+
message: describeBrandError(b.error)
|
|
4039
|
+
});
|
|
4040
|
+
else
|
|
4041
|
+
branch = b.value;
|
|
4042
|
+
}
|
|
4043
|
+
const worktrees = [];
|
|
4044
|
+
for (const [j, w] of (entry.worktrees ?? []).entries()) {
|
|
4045
|
+
const b = branchName(w);
|
|
4046
|
+
if (b.kind === "err") {
|
|
4047
|
+
issues.push({
|
|
4048
|
+
rule: "ergonex/manifest-branch",
|
|
4049
|
+
path: `${at}.worktrees[${j}]`,
|
|
4050
|
+
message: describeBrandError(b.error)
|
|
4051
|
+
});
|
|
4052
|
+
continue;
|
|
4053
|
+
}
|
|
4054
|
+
const wp = worktreePathOf(ref, b.value);
|
|
4055
|
+
if (wp.kind === "err") {
|
|
4056
|
+
issues.push({
|
|
4057
|
+
rule: "ergonex/manifest-path",
|
|
4058
|
+
path: `${at}.worktrees[${j}]`,
|
|
4059
|
+
message: describeLayoutError(wp.error)
|
|
4060
|
+
});
|
|
4061
|
+
continue;
|
|
4062
|
+
}
|
|
4063
|
+
worktrees.push({ branch: b.value, path: wp.value });
|
|
4064
|
+
named.push({ id: `${at}.worktrees[${j}]`, path: wp.value });
|
|
4065
|
+
}
|
|
4066
|
+
named.push({ id: `${at}.remote`, path: path.value });
|
|
4067
|
+
repositories.push({
|
|
4068
|
+
cloneUrl: cloneUrlOf(ref),
|
|
4069
|
+
ref,
|
|
4070
|
+
path: path.value,
|
|
4071
|
+
branch,
|
|
4072
|
+
group: entry.group,
|
|
4073
|
+
worktrees
|
|
4074
|
+
});
|
|
4075
|
+
}
|
|
4076
|
+
for (const c of collisions(named)) {
|
|
4077
|
+
for (const m of c.members) {
|
|
4078
|
+
const others = c.members.filter((o) => o.id !== m.id).map((o) => `${o.path} (${o.id})`);
|
|
4079
|
+
issues.push({
|
|
4080
|
+
rule: "ergonex/path-collision",
|
|
4081
|
+
path: m.id,
|
|
4082
|
+
message: `${m.path} is the same path as ${others.join(" and ")} after Unicode normalization and case folding, which a case-insensitive file system cannot hold apart; both are refused`
|
|
4083
|
+
});
|
|
4084
|
+
}
|
|
4085
|
+
}
|
|
4086
|
+
if (issues.length > 0)
|
|
4087
|
+
return err(issues);
|
|
4088
|
+
return ok({
|
|
4089
|
+
repositories,
|
|
4090
|
+
hostKinds,
|
|
4091
|
+
policy,
|
|
4092
|
+
...raw.identity === undefined ? {} : { identity: raw.identity },
|
|
4093
|
+
...signing === undefined ? {} : { signing },
|
|
4094
|
+
direnv: raw.direnv === true,
|
|
4095
|
+
modules,
|
|
4096
|
+
catalog
|
|
4097
|
+
});
|
|
4098
|
+
}
|
|
4099
|
+
function hostKindIn(manifest, host) {
|
|
4100
|
+
return hostKindOf(host, manifest.hostKinds);
|
|
4101
|
+
}
|
|
4102
|
+
export {
|
|
4103
|
+
BEGIN,
|
|
4104
|
+
EMPTY_RECORD,
|
|
4105
|
+
END,
|
|
4106
|
+
ERGONEX_VERSION,
|
|
4107
|
+
EXIT_STATUSES,
|
|
4108
|
+
GLOBAL_OPTIONS,
|
|
4109
|
+
HOOKS_FOLDER,
|
|
4110
|
+
MAX_SEGMENT_BYTES,
|
|
4111
|
+
MIN_NODE_MAJOR,
|
|
4112
|
+
MODULE_SOURCES,
|
|
4113
|
+
REPO_PATH_ROOM,
|
|
4114
|
+
REPO_STATE_KINDS,
|
|
4115
|
+
SEVERITIES2 as SEVERITIES,
|
|
4116
|
+
SHELLS,
|
|
4117
|
+
STATE_RECORD_PATH,
|
|
4118
|
+
TEMPLATES,
|
|
4119
|
+
UNAPPROVED_GRACE_DAYS,
|
|
4120
|
+
WINDOWS_PATH_LIMIT,
|
|
4121
|
+
WORKSPACE_FOLDERS,
|
|
4122
|
+
branchName,
|
|
4123
|
+
checkFiles,
|
|
4124
|
+
checkPathLength,
|
|
4125
|
+
child2 as child,
|
|
4126
|
+
classify,
|
|
4127
|
+
classifyFs,
|
|
4128
|
+
collisions,
|
|
4129
|
+
dayNumber,
|
|
4130
|
+
daysBetween,
|
|
4131
|
+
decodeExternalManifest,
|
|
4132
|
+
decodeManifest,
|
|
4133
|
+
decodeRegistered,
|
|
4134
|
+
decodeStateRecord,
|
|
4135
|
+
defaultActionOf,
|
|
4136
|
+
defineCommand,
|
|
4137
|
+
describeAction,
|
|
4138
|
+
describeBrandError,
|
|
4139
|
+
describeGenerateError,
|
|
4140
|
+
describeLayoutError,
|
|
4141
|
+
describeNameError,
|
|
4142
|
+
describePortableError,
|
|
4143
|
+
describeRefusal,
|
|
4144
|
+
describeRemote,
|
|
4145
|
+
describeSkip,
|
|
4146
|
+
desiredOf,
|
|
4147
|
+
digestsOf,
|
|
4148
|
+
dispatch,
|
|
4149
|
+
encodeStateRecord,
|
|
4150
|
+
exitCodeOf,
|
|
4151
|
+
exitStatusOf,
|
|
4152
|
+
externalManifestSchema,
|
|
4153
|
+
fastForwardable,
|
|
4154
|
+
fileDigest,
|
|
4155
|
+
findModule,
|
|
4156
|
+
findingsOf,
|
|
4157
|
+
firstSeenOf,
|
|
4158
|
+
foldKey,
|
|
4159
|
+
fsCheck,
|
|
4160
|
+
generated,
|
|
4161
|
+
generatedPath,
|
|
4162
|
+
gitHost,
|
|
4163
|
+
gitHostOf,
|
|
4164
|
+
headerText,
|
|
4165
|
+
hiddenFindings,
|
|
4166
|
+
holdsLocalWork,
|
|
4167
|
+
homeOf,
|
|
4168
|
+
hookChecks,
|
|
4169
|
+
hookFailsWhenUnableToCheck,
|
|
4170
|
+
hostAllowed,
|
|
4171
|
+
hostKindIn,
|
|
4172
|
+
installBlock,
|
|
4173
|
+
isAutoName,
|
|
4174
|
+
isDestructive,
|
|
4175
|
+
isExpectedRemote,
|
|
4176
|
+
isHostChange,
|
|
4177
|
+
isRegulated,
|
|
4178
|
+
isSettled,
|
|
4179
|
+
issueFinding,
|
|
4180
|
+
mayRemove,
|
|
4181
|
+
moduleName,
|
|
4182
|
+
nativePath,
|
|
4183
|
+
nearestNames,
|
|
4184
|
+
normalizeText,
|
|
4185
|
+
pathChecks,
|
|
4186
|
+
plan,
|
|
4187
|
+
planDigest,
|
|
4188
|
+
policyOf,
|
|
4189
|
+
precedenceOf,
|
|
4190
|
+
rcFileFor,
|
|
4191
|
+
refOfPath,
|
|
4192
|
+
removeBlock,
|
|
4193
|
+
renderAll,
|
|
4194
|
+
repoPath,
|
|
4195
|
+
repoPathChecks,
|
|
4196
|
+
repoPathLength,
|
|
4197
|
+
repoPathOf,
|
|
4198
|
+
resolveAll,
|
|
4199
|
+
runtimeChecks,
|
|
4200
|
+
sarifLevel,
|
|
4201
|
+
segment,
|
|
4202
|
+
segmentsOf,
|
|
4203
|
+
settingsOf,
|
|
4204
|
+
severityOf,
|
|
4205
|
+
sha,
|
|
4206
|
+
slugOf,
|
|
4207
|
+
snippetFor,
|
|
4208
|
+
toSarif,
|
|
4209
|
+
unapprovedSeverity,
|
|
4210
|
+
unifiedDiff,
|
|
4211
|
+
unsignedCommitSeverity,
|
|
4212
|
+
verifyFindings,
|
|
4213
|
+
version,
|
|
4214
|
+
withFile,
|
|
4215
|
+
withoutFile,
|
|
4216
|
+
worktreePathOf,
|
|
4217
|
+
worktreeWanted,
|
|
4218
|
+
worst
|
|
4219
|
+
};
|