@antelopejs/tooling-configs 0.4.1 → 0.5.1
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 +68 -29
- package/dist/bin/api-diff.mjs +235 -37
- package/dist/bin/body-check.mjs +14 -11
- package/dist/bin/capture.mjs +173 -54
- package/dist/bin/check-size.mjs +1 -1
- package/dist/bin/checks.mjs +166 -11
- package/dist/bin/comment-proof.mjs +29 -7
- package/dist/bin/garden.mjs +8 -58
- package/dist/bin/lease.mjs +48 -11
- package/dist/bin/ratchet.mjs +48 -14
- package/dist/bin/suppressions.mjs +3 -3
- package/dist/bin/sync.mjs +199 -80
- package/dist/bin/verify.mjs +1163 -474
- package/dist/capture/index.d.mts +6 -1
- package/dist/capture/index.mjs +1 -1
- package/dist/{ci-workflow-37qJa9LX.mjs → ci-workflow-COJUT12b.mjs} +121 -1
- package/dist/eslint.d.mts +16 -3
- package/dist/eslint.mjs +21 -6
- package/dist/formal/index.d.mts +20 -4
- package/dist/formal/index.mjs +71 -19
- package/dist/{map-B23_WMGE.mjs → map-DRyYz_Fw.mjs} +20 -1
- package/dist/oxc/anti-slop/index.mjs +156 -421
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +789 -103
- package/dist/oxc/lint.d.mts +13 -15
- package/dist/oxc/lint.mjs +19 -29
- package/dist/{package-CqqBgE8Y.mjs → package-Dn7VTFUW.mjs} +71 -17
- package/dist/{proof-stamp-DDDC0J6l.mjs → proof-stamp-DLWpPwQE.mjs} +72 -10
- package/dist/{ratchet-Q-JXuZIX.mjs → ratchet-ClfblHNJ.mjs} +18 -15
- package/dist/require-safety-comment-for-type-assertion-mnb0CIxa.mjs +181 -0
- package/dist/{scenes-DZVXkcxS.mjs → scenes-d-VQ0LOm.mjs} +4 -1
- package/dist/semver-BDxeEcP_.mjs +57 -0
- package/dist/{suppression-base-9L8dlMQK.mjs → suppression-base-Drnl6Y7F.mjs} +2 -2
- package/dist/{suppression-budget-C0SkPO5R.mjs → suppression-budget-Dvb_IqBo.mjs} +1 -1
- package/dist/{suppressions-DM60NkvL.mjs → suppressions-BroaaGJ_.mjs} +1 -1
- package/dist/{templates-BKZv-BRw.mjs → templates-lJBZFskp.mjs} +8 -3
- package/dist/type-aware-rules-DMuN6sHb.mjs +30 -0
- package/dist/{typescript-DAgQIAAR.mjs → typescript-BBbry7uU.mjs} +123 -27
- package/dist/verify/index.d.mts +60 -15
- package/package.json +3 -1
- package/dist/require-safety-comment-for-type-assertion-BBQ669kj.mjs +0 -83
package/dist/bin/verify.mjs
CHANGED
|
@@ -4,17 +4,18 @@ import { a as isString, c as object, f as parseJson, i as isObject, n as childre
|
|
|
4
4
|
import { a as exitIs, c as interrupted, i as exists, r as equal, u as refused } from "../checks-CxkSu2E6.mjs";
|
|
5
5
|
import { n as runCliAsync } from "../cli-4Tv71vYk.mjs";
|
|
6
6
|
import { c as repositoryRoot } from "../files-DJ0TeaTU.mjs";
|
|
7
|
-
import { _ as
|
|
8
|
-
import { appendFileSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { S as matchesPath, _ as recordProcesses, a as readFeatures, b as withLocalBins, c as allocateRun, d as isolatedEnv, f as loadRun, g as ownRun, h as operatorTmpdir, i as planFor, l as commandRecords, m as operatorEnv, n as SMOKE_FEATURE, o as CACHE_DIR, p as openWorkspace, r as checkMap, s as RUNS_DIR, u as git, v as removeTree, x as writeWhole, y as saveRun } from "../map-DRyYz_Fw.mjs";
|
|
8
|
+
import { accessSync, appendFileSync, chmodSync, closeSync, constants, copyFileSync, cpSync, existsSync, linkSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, unlinkSync, utimesSync, writeFileSync } from "node:fs";
|
|
9
9
|
import { basename, dirname, extname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
10
10
|
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
11
11
|
import { pathToFileURL } from "node:url";
|
|
12
12
|
import ts from "typescript";
|
|
13
13
|
import { createHash, randomBytes } from "node:crypto";
|
|
14
14
|
import { availableParallelism, constants as constants$1 } from "node:os";
|
|
15
|
-
import { parseArgs } from "node:util";
|
|
16
|
-
import { createServer } from "node:net";
|
|
15
|
+
import { parseArgs, stripVTControlCharacters } from "node:util";
|
|
16
|
+
import { connect, createServer } from "node:net";
|
|
17
17
|
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
18
|
+
import { lookup } from "node:dns";
|
|
18
19
|
//#region src/verify/doors.ts
|
|
19
20
|
const RELEASE_FILES = [
|
|
20
21
|
"package.json",
|
|
@@ -232,8 +233,8 @@ function parseScript(path, source) {
|
|
|
232
233
|
file: ts.createSourceFile("emit.js", outputText, ts.ScriptTarget.ES2022, true, ts.ScriptKind.JS)
|
|
233
234
|
};
|
|
234
235
|
}
|
|
235
|
-
function declaredNames(name) {
|
|
236
|
-
return ts.isIdentifier(name) ? [name.text] : name.elements.flatMap((element) => ts.isOmittedExpression(element) ? [] : declaredNames(element.name));
|
|
236
|
+
function declaredNames$1(name) {
|
|
237
|
+
return ts.isIdentifier(name) ? [name.text] : name.elements.flatMap((element) => ts.isOmittedExpression(element) ? [] : declaredNames$1(element.name));
|
|
237
238
|
}
|
|
238
239
|
function isFunctionDeclaration(statement) {
|
|
239
240
|
return ts.isFunctionDeclaration(statement) && Boolean(statement.body) && statement.name !== void 0;
|
|
@@ -242,7 +243,7 @@ function isFunctionDeclaration(statement) {
|
|
|
242
243
|
function localsOf(script, covered) {
|
|
243
244
|
const file = script.path.replace(MODULE_EXTENSION, "");
|
|
244
245
|
const names = script.file.statements.flatMap((statement) => {
|
|
245
|
-
if (ts.isVariableStatement(statement)) return statement.declarationList.declarations.flatMap((declaration) => declaredNames(declaration.name));
|
|
246
|
+
if (ts.isVariableStatement(statement)) return statement.declarationList.declarations.flatMap((declaration) => declaredNames$1(declaration.name));
|
|
246
247
|
return (ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement)) && statement.name ? [statement.name.text] : [];
|
|
247
248
|
});
|
|
248
249
|
const clashing = [...covered.functions.get(file) ?? []].filter((name) => covered.duplicated.has(name));
|
|
@@ -875,6 +876,7 @@ function startRecorded(folder, spec) {
|
|
|
875
876
|
});
|
|
876
877
|
closeSync(stdout);
|
|
877
878
|
closeSync(stderr);
|
|
879
|
+
child.on("error", (error) => appendFileSync(join(folder, "stderr.txt"), `${error.message}\n`));
|
|
878
880
|
const exited = new Promise((resolve) => {
|
|
879
881
|
child.on("exit", (exitCode, signal) => {
|
|
880
882
|
writeFileSync(join(folder, "exit-code.txt"), `${exitCode ?? `signal ${signal}`}\n`);
|
|
@@ -926,13 +928,16 @@ const PORT_RANGE = {
|
|
|
926
928
|
high: 2e4
|
|
927
929
|
};
|
|
928
930
|
const PORT_ATTEMPTS = 50;
|
|
931
|
+
function isAddressInfo(address) {
|
|
932
|
+
return address instanceof Object;
|
|
933
|
+
}
|
|
929
934
|
function bindable(port) {
|
|
930
935
|
return new Promise((resolve) => {
|
|
931
936
|
const server = createServer();
|
|
932
937
|
server.once("error", () => resolve(null));
|
|
933
938
|
server.listen(port, "127.0.0.1", () => {
|
|
934
939
|
const address = server.address();
|
|
935
|
-
const bound = address
|
|
940
|
+
const bound = isAddressInfo(address) ? address.port : null;
|
|
936
941
|
server.close(() => resolve(bound));
|
|
937
942
|
});
|
|
938
943
|
});
|
|
@@ -955,15 +960,35 @@ async function freePort() {
|
|
|
955
960
|
function reason(error) {
|
|
956
961
|
return error.cause instanceof Error ? error.cause.message : error.message;
|
|
957
962
|
}
|
|
958
|
-
/**
|
|
963
|
+
/** Header names whose values http.json replaces with `<redacted>`, since `publish` pushes the evidence to the repository. */
|
|
964
|
+
const CREDENTIAL_HEADERS = /* @__PURE__ */ new Set([
|
|
965
|
+
"authorization",
|
|
966
|
+
"proxy-authorization",
|
|
967
|
+
"cookie",
|
|
968
|
+
"set-cookie"
|
|
969
|
+
]);
|
|
970
|
+
function redacted(headers) {
|
|
971
|
+
return Object.fromEntries(Object.entries(headers).map(([name, value]) => [name, CREDENTIAL_HEADERS.has(name) ? "<redacted>" : value]));
|
|
972
|
+
}
|
|
973
|
+
/** The headers a request sends: `content-type: application/json` when it has a body, then the journey's headers over it, names lower-cased. */
|
|
974
|
+
function requestHeaders(init) {
|
|
975
|
+
const headers = new Headers(init.body ? { "content-type": "application/json" } : {});
|
|
976
|
+
for (const [name, value] of Object.entries(init.headers ?? {})) headers.set(name, value);
|
|
977
|
+
return headers;
|
|
978
|
+
}
|
|
979
|
+
/** Sends one request and writes http.json with the request it sent, and the status, headers and body of the answer, credential headers redacted. */
|
|
959
980
|
async function httpRecorded(folder, url, init = {}) {
|
|
960
981
|
mkdirSync(folder, { recursive: true });
|
|
982
|
+
const method = init.method ?? "GET";
|
|
983
|
+
let sent = {};
|
|
961
984
|
let result;
|
|
962
985
|
try {
|
|
986
|
+
const headers = requestHeaders(init);
|
|
987
|
+
sent = Object.fromEntries(headers);
|
|
963
988
|
const response = await fetch(url, {
|
|
964
|
-
method
|
|
989
|
+
method,
|
|
965
990
|
body: init.body,
|
|
966
|
-
headers
|
|
991
|
+
headers,
|
|
967
992
|
signal: AbortSignal.timeout(init.timeoutMs ?? 5e3)
|
|
968
993
|
});
|
|
969
994
|
const body = await response.text();
|
|
@@ -977,6 +1002,7 @@ async function httpRecorded(folder, url, init = {}) {
|
|
|
977
1002
|
folder,
|
|
978
1003
|
url,
|
|
979
1004
|
status: response.status,
|
|
1005
|
+
headers: Object.fromEntries(response.headers),
|
|
980
1006
|
body,
|
|
981
1007
|
json,
|
|
982
1008
|
error: null
|
|
@@ -986,17 +1012,25 @@ async function httpRecorded(folder, url, init = {}) {
|
|
|
986
1012
|
folder,
|
|
987
1013
|
url,
|
|
988
1014
|
status: null,
|
|
1015
|
+
headers: {},
|
|
989
1016
|
body: "",
|
|
990
1017
|
json: void 0,
|
|
991
1018
|
error: error instanceof Error ? reason(error) : String(error)
|
|
992
1019
|
};
|
|
993
1020
|
}
|
|
1021
|
+
const request = {
|
|
1022
|
+
url,
|
|
1023
|
+
...init,
|
|
1024
|
+
method,
|
|
1025
|
+
headers: redacted(sent)
|
|
1026
|
+
};
|
|
1027
|
+
const response = {
|
|
1028
|
+
...result,
|
|
1029
|
+
headers: redacted(result.headers)
|
|
1030
|
+
};
|
|
994
1031
|
writeFileSync(join(folder, "http.json"), `${JSON.stringify({
|
|
995
|
-
request
|
|
996
|
-
|
|
997
|
-
...init
|
|
998
|
-
},
|
|
999
|
-
response: result
|
|
1032
|
+
request,
|
|
1033
|
+
response
|
|
1000
1034
|
}, null, 2)}\n`);
|
|
1001
1035
|
return result;
|
|
1002
1036
|
}
|
|
@@ -1284,7 +1318,7 @@ function publishedPort(container, port) {
|
|
|
1284
1318
|
if (!match) throw new Error(`${container} does not publish ${port}: ${mapping}`);
|
|
1285
1319
|
return Number(match[1]);
|
|
1286
1320
|
}
|
|
1287
|
-
function waitReady(container, ready, folder) {
|
|
1321
|
+
function waitReady$1(container, ready, folder) {
|
|
1288
1322
|
const deadline = Date.now() + READY_TIMEOUT_MS$1;
|
|
1289
1323
|
let last = docker([
|
|
1290
1324
|
"exec",
|
|
@@ -1347,7 +1381,7 @@ function startServices(workspace, state, prefix) {
|
|
|
1347
1381
|
saveRun(state);
|
|
1348
1382
|
container.port = publishedPort(name, definition.port);
|
|
1349
1383
|
saveRun(state);
|
|
1350
|
-
waitReady(name, definition.ready, join(folder, "02-ready"));
|
|
1384
|
+
waitReady$1(name, definition.ready, join(folder, "02-ready"));
|
|
1351
1385
|
failed = false;
|
|
1352
1386
|
} finally {
|
|
1353
1387
|
state.timings.push({
|
|
@@ -1461,7 +1495,8 @@ function serializable(value) {
|
|
|
1461
1495
|
function target(body) {
|
|
1462
1496
|
const name = body.interface || interfaces[0];
|
|
1463
1497
|
let owner;
|
|
1464
|
-
|
|
1498
|
+
const list = entries[name] || ["."];
|
|
1499
|
+
let value = load(name, body.entry || (list.length === 1 ? list[0] : "."));
|
|
1465
1500
|
for (const key of body.path) {
|
|
1466
1501
|
owner = value;
|
|
1467
1502
|
value = value[key];
|
|
@@ -1633,6 +1668,11 @@ function interfaceLinks(workspace, state) {
|
|
|
1633
1668
|
const links = /* @__PURE__ */ new Map([["@antelopejs/interface-core", core]]);
|
|
1634
1669
|
const entries = {};
|
|
1635
1670
|
for (const name of implementedInterfaces(workspace.manifest)) {
|
|
1671
|
+
if (name === workspace.manifest.name) {
|
|
1672
|
+
links.set(name, realpathSync(workspace.root));
|
|
1673
|
+
entries[name] = runtimeEntries(workspace.manifest).filter((entry) => entry !== ".");
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1636
1676
|
const folder = installedPackage(workspace.root, name);
|
|
1637
1677
|
links.set(name, folder);
|
|
1638
1678
|
entries[name] = runtimeEntries(readPackage(folder));
|
|
@@ -1647,12 +1687,15 @@ function interfaceLinks(workspace, state) {
|
|
|
1647
1687
|
* the driver module and, for the interface kind, the stub provider, or, for
|
|
1648
1688
|
* the module kind, the module under verification as a `local` source. Every
|
|
1649
1689
|
* interface the driver calls is linked to the copy the repository resolves,
|
|
1650
|
-
* so the project shares one copy per interface as core requires.
|
|
1690
|
+
* so the project shares one copy per interface as core requires. An
|
|
1691
|
+
* interface of the module's own package links the repository root, whose
|
|
1692
|
+
* non-`.` runtime subpaths are its entries. Each of `companions` is loaded
|
|
1693
|
+
* too, but never in place of the module or the driver.
|
|
1651
1694
|
*/
|
|
1652
|
-
function writeFixture(workspace, state, folder,
|
|
1695
|
+
function writeFixture(workspace, state, folder, configs) {
|
|
1653
1696
|
const project = join(folder, "project");
|
|
1654
1697
|
const { links, entries } = interfaceLinks(workspace, state);
|
|
1655
|
-
const modules = {};
|
|
1698
|
+
const modules = { ...configs.companions };
|
|
1656
1699
|
if (workspace.kind === "interface") {
|
|
1657
1700
|
writeModule(join(project, "stub"), {
|
|
1658
1701
|
name: "verify-stub",
|
|
@@ -1669,7 +1712,7 @@ function writeFixture(workspace, state, folder, moduleConfig) {
|
|
|
1669
1712
|
type: "local",
|
|
1670
1713
|
path: workspace.root
|
|
1671
1714
|
},
|
|
1672
|
-
config:
|
|
1715
|
+
config: configs.module
|
|
1673
1716
|
};
|
|
1674
1717
|
writeModule(join(project, "driver"), {
|
|
1675
1718
|
name: "verify-driver",
|
|
@@ -1711,6 +1754,8 @@ function ajsEntry(root) {
|
|
|
1711
1754
|
//#endregion
|
|
1712
1755
|
//#region src/verify/surface-types.ts
|
|
1713
1756
|
const NAMED = ts.SymbolFlags.Interface | ts.SymbolFlags.Class | ts.SymbolFlags.Enum | ts.SymbolFlags.TypeAlias;
|
|
1757
|
+
const MODULE = ts.SymbolFlags.ValueModule | ts.SymbolFlags.NamespaceModule;
|
|
1758
|
+
const INHERITING = ts.SymbolFlags.Class | ts.SymbolFlags.Interface;
|
|
1714
1759
|
function isObjectType$1(type) {
|
|
1715
1760
|
return (type.flags & ts.TypeFlags.Object) !== 0;
|
|
1716
1761
|
}
|
|
@@ -1726,72 +1771,212 @@ function publicPart(declaration) {
|
|
|
1726
1771
|
if (!ts.isClassDeclaration(declaration)) return declaration;
|
|
1727
1772
|
return ts.factory.updateClassDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, declaration.heritageClauses, declaration.members.filter(isPublicMember));
|
|
1728
1773
|
}
|
|
1729
|
-
/**
|
|
1774
|
+
/** Whether a heritage expression is a name such as `Base` or `ns.Base`, rather than a call. */
|
|
1775
|
+
function isEntityName(expression) {
|
|
1776
|
+
return ts.isIdentifier(expression) || ts.isPropertyAccessExpression(expression) && isEntityName(expression.expression);
|
|
1777
|
+
}
|
|
1778
|
+
/** The name a written type reference resolves through, when the node is one. */
|
|
1779
|
+
function referencedName(node) {
|
|
1780
|
+
if (ts.isTypeReferenceNode(node)) return node.typeName;
|
|
1781
|
+
if (ts.isExpressionWithTypeArguments(node) && isEntityName(node.expression)) return node.expression;
|
|
1782
|
+
if (ts.isTypeQueryNode(node)) return node.exprName;
|
|
1783
|
+
if (!ts.isImportTypeNode(node)) return void 0;
|
|
1784
|
+
if (node.qualifier) return node.qualifier;
|
|
1785
|
+
return ts.isLiteralTypeNode(node.argument) ? node.argument.literal : void 0;
|
|
1786
|
+
}
|
|
1787
|
+
/** Whether reading the declaration would read a whole module rather than one declaration. */
|
|
1788
|
+
function isModuleBody(declaration) {
|
|
1789
|
+
return ts.isSourceFile(declaration) || ts.isModuleDeclaration(declaration);
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Collects the package's declarations a function reaches. The written text of
|
|
1793
|
+
* every declaration it meets is read once, and each type reference in it is
|
|
1794
|
+
* resolved to its symbol without evaluating a type, so the walk is finite. The
|
|
1795
|
+
* root signatures' own types are walked once for what their text does not
|
|
1796
|
+
* write: a class instance a constructor answers and the type arguments they
|
|
1797
|
+
* were instantiated with.
|
|
1798
|
+
*/
|
|
1730
1799
|
var DeclarationWalk = class {
|
|
1731
1800
|
declarations = /* @__PURE__ */ new Map();
|
|
1801
|
+
reached = /* @__PURE__ */ new Set();
|
|
1802
|
+
read = /* @__PURE__ */ new Set();
|
|
1803
|
+
inherited = /* @__PURE__ */ new Set();
|
|
1732
1804
|
seen = /* @__PURE__ */ new Set();
|
|
1805
|
+
unread = [];
|
|
1806
|
+
types = [];
|
|
1733
1807
|
printer = ts.createPrinter({ removeComments: true });
|
|
1734
1808
|
checker;
|
|
1735
|
-
|
|
1736
|
-
constructor(checker,
|
|
1809
|
+
isLocalFile;
|
|
1810
|
+
constructor(checker, isLocalFile) {
|
|
1737
1811
|
this.checker = checker;
|
|
1738
|
-
this.
|
|
1812
|
+
this.isLocalFile = isLocalFile;
|
|
1739
1813
|
}
|
|
1740
1814
|
get found() {
|
|
1741
|
-
|
|
1815
|
+
for (;;) {
|
|
1816
|
+
const type = this.types.pop();
|
|
1817
|
+
if (type) {
|
|
1818
|
+
this.type(type);
|
|
1819
|
+
continue;
|
|
1820
|
+
}
|
|
1821
|
+
const symbol = this.unread.pop();
|
|
1822
|
+
if (!symbol) return [...this.declarations.values()];
|
|
1823
|
+
for (const declaration of this.local(symbol)) if (!isModuleBody(declaration)) this.references(declaration);
|
|
1824
|
+
}
|
|
1742
1825
|
}
|
|
1743
1826
|
signature(signature) {
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1827
|
+
this.types.push(...signature.getTypeParameters() ?? [], ...signature.getParameters().map((parameter) => this.checker.getTypeOfSymbol(parameter)), signature.getReturnType());
|
|
1828
|
+
}
|
|
1829
|
+
/** Reads what a symbol on the path to the signatures writes of their types: a class's or interface's heritage type arguments, any other symbol's declarations. */
|
|
1830
|
+
owner(symbol) {
|
|
1831
|
+
if (symbol.flags & INHERITING) this.inherit(symbol);
|
|
1832
|
+
else this.open(symbol);
|
|
1833
|
+
}
|
|
1834
|
+
/** The symbol's declarations in the package, leaving out what lib or a dependency declares of a symbol the package augments. */
|
|
1835
|
+
local(symbol) {
|
|
1836
|
+
return (symbol.declarations ?? []).filter((declaration) => this.isLocalFile(declaration.getSourceFile()));
|
|
1837
|
+
}
|
|
1838
|
+
isLocal(symbol) {
|
|
1839
|
+
return this.local(symbol).length > 0;
|
|
1840
|
+
}
|
|
1841
|
+
resolve(symbol) {
|
|
1842
|
+
return symbol.flags & ts.SymbolFlags.Alias ? this.checker.getAliasedSymbol(symbol) : symbol;
|
|
1747
1843
|
}
|
|
1748
1844
|
record(symbol) {
|
|
1749
|
-
if (
|
|
1750
|
-
const texts = (symbol
|
|
1845
|
+
if ((symbol.flags & NAMED) === 0) return;
|
|
1846
|
+
const texts = this.local(symbol).map((declaration) => this.printer.printNode(ts.EmitHint.Unspecified, publicPart(declaration), declaration.getSourceFile()).replace(/\s+/g, " ").replace(/^(?:export )?(?:declare )?/, ""));
|
|
1751
1847
|
this.declarations.set(symbol, texts.join(" "));
|
|
1752
|
-
for (const declaration of symbol.declarations ?? []) this.generics(declaration);
|
|
1753
1848
|
}
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
if (
|
|
1849
|
+
open(found) {
|
|
1850
|
+
const symbol = this.resolve(found);
|
|
1851
|
+
if (this.read.has(symbol) || !this.isLocal(symbol)) return;
|
|
1852
|
+
this.read.add(symbol);
|
|
1853
|
+
this.unread.push(symbol);
|
|
1854
|
+
}
|
|
1855
|
+
inherit(found) {
|
|
1856
|
+
const symbol = this.resolve(found);
|
|
1857
|
+
if (this.inherited.has(symbol) || !this.isLocal(symbol)) return;
|
|
1858
|
+
this.inherited.add(symbol);
|
|
1859
|
+
for (const declaration of this.local(symbol)) {
|
|
1860
|
+
if (!ts.isClassLike(declaration) && !ts.isInterfaceDeclaration(declaration)) continue;
|
|
1861
|
+
for (const clause of declaration.heritageClauses ?? []) for (const base of clause.types) {
|
|
1862
|
+
this.typeArguments(base);
|
|
1863
|
+
for (const owner of this.bases(base.expression)) this.owner(owner);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
/** Reads the type arguments a heritage expression writes, including those inside a call such as `mix<A>(Base<B>)`. */
|
|
1868
|
+
typeArguments(node) {
|
|
1869
|
+
if (ts.isExpressionWithTypeArguments(node)) {
|
|
1870
|
+
for (const argument of node.typeArguments ?? []) this.references(argument);
|
|
1871
|
+
this.typeArguments(node.expression);
|
|
1872
|
+
} else if (ts.isTypeNode(node)) this.references(node);
|
|
1873
|
+
else ts.forEachChild(node, (child) => this.typeArguments(child));
|
|
1874
|
+
}
|
|
1875
|
+
/** Records the symbol when it names an interface, class, enum or alias of the package, and queues its declarations to be read, once. */
|
|
1876
|
+
reach(found) {
|
|
1877
|
+
const symbol = this.resolve(found);
|
|
1878
|
+
if (this.reached.has(symbol) || !this.isLocal(symbol)) return;
|
|
1879
|
+
this.reached.add(symbol);
|
|
1880
|
+
for (const declaration of symbol.declarations ?? []) {
|
|
1881
|
+
const owner = ts.isEnumMember(declaration) ? this.checker.getSymbolAtLocation(declaration.parent.name) : void 0;
|
|
1882
|
+
if (owner) this.reach(owner);
|
|
1883
|
+
}
|
|
1884
|
+
const assigned = symbol.exports?.get(ts.InternalSymbolName.ExportEquals);
|
|
1885
|
+
if (assigned) this.reach(assigned);
|
|
1886
|
+
if (symbol.flags & MODULE) for (const member of this.checker.getExportsOfModule(symbol)) {
|
|
1887
|
+
const value = this.resolve(member);
|
|
1888
|
+
if (value.flags & ts.SymbolFlags.Value) this.reach(value);
|
|
1889
|
+
}
|
|
1890
|
+
this.record(symbol);
|
|
1891
|
+
this.open(symbol);
|
|
1892
|
+
}
|
|
1893
|
+
/** The symbols a heritage expression names: the entity it is, or each name in a call such as `Mixin(Base)`. */
|
|
1894
|
+
bases(expression) {
|
|
1895
|
+
const symbol = ts.isIdentifier(expression) || ts.isPropertyAccessExpression(expression) ? this.checker.getSymbolAtLocation(expression) : void 0;
|
|
1896
|
+
if (symbol) return [symbol];
|
|
1897
|
+
const found = [];
|
|
1898
|
+
ts.forEachChild(expression, (child) => {
|
|
1899
|
+
if (!ts.isTypeNode(child)) found.push(...this.bases(child));
|
|
1900
|
+
});
|
|
1901
|
+
return found;
|
|
1757
1902
|
}
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1903
|
+
references(node) {
|
|
1904
|
+
const name = referencedName(node);
|
|
1905
|
+
const symbol = name && this.checker.getSymbolAtLocation(name);
|
|
1906
|
+
if (symbol) this.reach(symbol);
|
|
1907
|
+
if (ts.isExpressionWithTypeArguments(node) && !isEntityName(node.expression)) for (const base of this.bases(node.expression)) this.reach(base);
|
|
1908
|
+
ts.forEachChild(node, (child) => this.references(child));
|
|
1761
1909
|
}
|
|
1762
1910
|
type(type) {
|
|
1763
1911
|
if (this.seen.has(type)) return;
|
|
1764
1912
|
this.seen.add(type);
|
|
1913
|
+
if (type.aliasSymbol) {
|
|
1914
|
+
this.reach(type.aliasSymbol);
|
|
1915
|
+
this.types.push(...type.aliasTypeArguments ?? []);
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1765
1918
|
if (type.isUnionOrIntersection()) {
|
|
1766
|
-
|
|
1919
|
+
this.types.push(...type.types);
|
|
1767
1920
|
return;
|
|
1768
1921
|
}
|
|
1769
1922
|
if (type.flags & ts.TypeFlags.TypeParameter) {
|
|
1770
|
-
this.
|
|
1771
|
-
this.type(type.getDefault() ?? type);
|
|
1923
|
+
this.types.push(type.getConstraint() ?? type, type.getDefault() ?? type);
|
|
1772
1924
|
return;
|
|
1773
1925
|
}
|
|
1774
|
-
if (type
|
|
1775
|
-
for (const argument of type.aliasTypeArguments ?? []) this.type(argument);
|
|
1776
|
-
if (isObjectType$1(type) && isReference(type)) for (const argument of this.checker.getTypeArguments(type)) this.type(argument);
|
|
1926
|
+
if (isObjectType$1(type) && isReference(type)) this.types.push(...this.checker.getTypeArguments(type));
|
|
1777
1927
|
const symbol = type.getSymbol();
|
|
1778
|
-
if (symbol
|
|
1779
|
-
for (const declaration of symbol.declarations ?? []) {
|
|
1780
|
-
const owner = ts.isEnumMember(declaration) ? this.checker.getSymbolAtLocation(declaration.parent.name) : void 0;
|
|
1781
|
-
if (owner) this.record(owner);
|
|
1782
|
-
}
|
|
1783
|
-
this.record(symbol);
|
|
1784
|
-
this.members(type);
|
|
1928
|
+
if (symbol) this.reach(symbol);
|
|
1785
1929
|
}
|
|
1786
1930
|
};
|
|
1931
|
+
function declaredNames(statement) {
|
|
1932
|
+
if (ts.isVariableStatement(statement)) return statement.declarationList.declarations.map((declaration) => declaration.name);
|
|
1933
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isFunctionDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || ts.isEnumDeclaration(statement) || ts.isModuleDeclaration(statement)) return statement.name ? [statement.name] : [];
|
|
1934
|
+
return [];
|
|
1935
|
+
}
|
|
1936
|
+
function moduleStatements(statement) {
|
|
1937
|
+
const body = ts.isModuleDeclaration(statement) ? statement.body : void 0;
|
|
1938
|
+
if (body && ts.isModuleBlock(body)) return body.statements;
|
|
1939
|
+
return body && ts.isModuleDeclaration(body) ? [body] : [];
|
|
1940
|
+
}
|
|
1941
|
+
/** Whether a module file's statement opens a scope outside text can see: `declare global`, or `declare module` of a module that is not a package file. */
|
|
1942
|
+
function opensOutside(checker, statement, isLocalFile) {
|
|
1943
|
+
if (!ts.isModuleDeclaration(statement)) return false;
|
|
1944
|
+
if (statement.flags & ts.NodeFlags.GlobalAugmentation) return true;
|
|
1945
|
+
if (!ts.isStringLiteral(statement.name)) return false;
|
|
1946
|
+
return !(checker.getSymbolAtLocation(statement.name)?.declarations ?? []).some((declaration) => ts.isSourceFile(declaration) && isLocalFile(declaration));
|
|
1947
|
+
}
|
|
1948
|
+
/**
|
|
1949
|
+
* The symbols the package declares where text outside the package can name
|
|
1950
|
+
* them: in a script file, in `declare global`, or in `declare module` of a
|
|
1951
|
+
* module that is not a package file. These include every augmentation of a
|
|
1952
|
+
* lib or dependency symbol. Text outside the package reaches a declaration
|
|
1953
|
+
* of the package only through one of them, so every walk reaches them all.
|
|
1954
|
+
*/
|
|
1955
|
+
function outsideVisible(checker, files, isLocalFile) {
|
|
1956
|
+
const visible = /* @__PURE__ */ new Set();
|
|
1957
|
+
const pending = files.flatMap((file) => ts.isExternalModule(file) ? file.statements.filter((statement) => opensOutside(checker, statement, isLocalFile)).flatMap(moduleStatements) : [...file.statements]);
|
|
1958
|
+
for (let statement = pending.pop(); statement; statement = pending.pop()) {
|
|
1959
|
+
pending.push(...moduleStatements(statement));
|
|
1960
|
+
for (const name of declaredNames(statement)) {
|
|
1961
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
1962
|
+
if (symbol) visible.add(symbol);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
return [...visible];
|
|
1966
|
+
}
|
|
1787
1967
|
/**
|
|
1788
1968
|
* The declarations of the package that the signatures reach through their
|
|
1789
1969
|
* parameter and answer types, transitively, each printed without comments.
|
|
1790
|
-
*
|
|
1791
|
-
*
|
|
1970
|
+
* `path` holds the symbols the surface entered to find the signatures, the
|
|
1971
|
+
* last one owning them; their declarations write the type arguments the
|
|
1972
|
+
* signatures were instantiated with. `visible` holds the
|
|
1973
|
+
* package's symbols outside text can name, from {@link outsideVisible}. A change inside a reached declaration,
|
|
1974
|
+
* such as a member's type, changes the functions that reach it.
|
|
1792
1975
|
*/
|
|
1793
|
-
function referencedDeclarations(checker,
|
|
1794
|
-
const walk = new DeclarationWalk(checker,
|
|
1976
|
+
function referencedDeclarations(checker, isLocalFile, signatures, path, visible) {
|
|
1977
|
+
const walk = new DeclarationWalk(checker, isLocalFile);
|
|
1978
|
+
for (const symbol of visible) walk.reach(symbol);
|
|
1979
|
+
for (const symbol of path) walk.owner(symbol);
|
|
1795
1980
|
for (const signature of signatures) walk.signature(signature);
|
|
1796
1981
|
return walk.found.sort((left, right) => left.localeCompare(right));
|
|
1797
1982
|
}
|
|
@@ -1837,13 +2022,13 @@ function resolveAlias(walk, symbol) {
|
|
|
1837
2022
|
function signatureTexts(walk, signatures, kind) {
|
|
1838
2023
|
return signatures.map((signature) => walk.checker.signatureToString(signature, void 0, SIGNATURE_FLAGS, kind).replace(/\s+/g, " "));
|
|
1839
2024
|
}
|
|
1840
|
-
function callableText(walk, type) {
|
|
2025
|
+
function callableText(walk, type, path) {
|
|
1841
2026
|
const callable = walk.checker.getNonNullableType(type);
|
|
1842
2027
|
const calls = callable.getCallSignatures();
|
|
1843
2028
|
const constructs = callable.getConstructSignatures();
|
|
1844
2029
|
const texts = [...signatureTexts(walk, calls, ts.SignatureKind.Call), ...signatureTexts(walk, constructs, ts.SignatureKind.Construct)];
|
|
1845
2030
|
if (texts.length === 0) return void 0;
|
|
1846
|
-
const reached = referencedDeclarations(walk.checker, (
|
|
2031
|
+
const reached = referencedDeclarations(walk.checker, (file) => inPackage(walk, file.fileName), [...calls, ...constructs], path, walk.visible);
|
|
1847
2032
|
return reached.length === 0 ? texts.join(" | ") : `${texts.join(" | ")} where ${reached.join(" ")}`;
|
|
1848
2033
|
}
|
|
1849
2034
|
function typeOf(walk, symbol) {
|
|
@@ -1853,17 +2038,17 @@ function isPublic(property) {
|
|
|
1853
2038
|
const hidden = ts.ModifierFlags.Private | ts.ModifierFlags.Protected;
|
|
1854
2039
|
return (property.declarations ?? []).every((declaration) => (ts.getCombinedModifierFlags(declaration) & hidden) === 0);
|
|
1855
2040
|
}
|
|
1856
|
-
function recordMembers(walk, type, prefix, skip) {
|
|
2041
|
+
function recordMembers(walk, type, prefix, skip, owners) {
|
|
1857
2042
|
for (const property of walk.checker.getPropertiesOfType(type)) {
|
|
1858
2043
|
if (property.name === skip || !isPublic(property)) continue;
|
|
1859
2044
|
if (!declaredHere(walk, property)) continue;
|
|
1860
|
-
const text = callableText(walk, typeOf(walk, property));
|
|
2045
|
+
const text = callableText(walk, typeOf(walk, property), [...owners, property]);
|
|
1861
2046
|
if (text !== void 0) walk.surface.set(`${prefix}${property.name}`, text);
|
|
1862
2047
|
}
|
|
1863
2048
|
}
|
|
1864
|
-
function recordClass(walk, symbol, path) {
|
|
1865
|
-
recordMembers(walk, typeOf(walk, symbol), `${path}.`, "prototype");
|
|
1866
|
-
recordMembers(walk, walk.checker.getDeclaredTypeOfSymbol(symbol), `${path}.prototype.`, "");
|
|
2049
|
+
function recordClass(walk, symbol, path, owners) {
|
|
2050
|
+
recordMembers(walk, typeOf(walk, symbol), `${path}.`, "prototype", owners);
|
|
2051
|
+
recordMembers(walk, walk.checker.getDeclaredTypeOfSymbol(symbol), `${path}.prototype.`, "", owners);
|
|
1867
2052
|
}
|
|
1868
2053
|
function isObjectType(type) {
|
|
1869
2054
|
return (type.flags & ts.TypeFlags.Object) !== 0;
|
|
@@ -1877,14 +2062,14 @@ function visit(walk, exported, path, trail) {
|
|
|
1877
2062
|
if (trail.includes(symbol)) return;
|
|
1878
2063
|
const depth = path.split(":")[1]?.split(".").length ?? MAX_DEPTH;
|
|
1879
2064
|
const type = symbol.flags & ts.SymbolFlags.Value ? typeOf(walk, symbol) : void 0;
|
|
1880
|
-
const
|
|
2065
|
+
const next = [...trail, symbol];
|
|
2066
|
+
const text = type && callableText(walk, type, next);
|
|
1881
2067
|
if (text !== void 0) walk.surface.set(path, text);
|
|
1882
2068
|
if (symbol.flags & ts.SymbolFlags.Class && declaredHere(walk, symbol)) {
|
|
1883
|
-
recordClass(walk, symbol, path);
|
|
2069
|
+
recordClass(walk, symbol, path, next);
|
|
1884
2070
|
return;
|
|
1885
2071
|
}
|
|
1886
2072
|
if (depth >= MAX_DEPTH || !declaredHere(walk, symbol)) return;
|
|
1887
|
-
const next = [...trail, symbol];
|
|
1888
2073
|
if (symbol.flags & ts.SymbolFlags.Module) for (const member of walk.checker.getExportsOfModule(symbol)) visit(walk, member, `${path}.${member.name}`, next);
|
|
1889
2074
|
else if (type && text === void 0 && isObjectLiteralType(walk, type)) for (const member of walk.checker.getPropertiesOfType(type)) visit(walk, member, `${path}.${member.name}`, next);
|
|
1890
2075
|
}
|
|
@@ -1940,10 +2125,13 @@ function declaredSurface(packageFolder) {
|
|
|
1940
2125
|
types: ["node"]
|
|
1941
2126
|
}
|
|
1942
2127
|
});
|
|
2128
|
+
const checker = program.getTypeChecker();
|
|
2129
|
+
const local = (file) => inPackage({ folder }, file.fileName);
|
|
1943
2130
|
const walk = {
|
|
1944
|
-
checker
|
|
2131
|
+
checker,
|
|
1945
2132
|
folder,
|
|
1946
|
-
surface: /* @__PURE__ */ new Map()
|
|
2133
|
+
surface: /* @__PURE__ */ new Map(),
|
|
2134
|
+
visible: outsideVisible(checker, program.getSourceFiles().filter(local), local)
|
|
1947
2135
|
};
|
|
1948
2136
|
const missing = unresolvedImports(walk, program);
|
|
1949
2137
|
if (missing.length > 0) throw new Error(`unresolved imports in declarations: ${missing.join(", ")}${nodeTypes === void 0 && missing.some((entry) => entry.includes("-> node:")) ? "; install @types/node so node: built-ins resolve" : ""}`);
|
|
@@ -2520,6 +2708,10 @@ function copyWorkingTree(root, target) {
|
|
|
2520
2708
|
function templateName(workspace) {
|
|
2521
2709
|
return workspace.config.template?.name ?? `Verify ${workspace.name}`;
|
|
2522
2710
|
}
|
|
2711
|
+
/** The `ajs` command group that creates from the template: `module` for a module template, `project` otherwise. */
|
|
2712
|
+
function templateInit(workspace) {
|
|
2713
|
+
return workspace.config.template?.module === true ? "module" : "project";
|
|
2714
|
+
}
|
|
2523
2715
|
/**
|
|
2524
2716
|
* Builds the offline template source of a run: the template's working tree,
|
|
2525
2717
|
* uncommitted changes included, as a local git repository, and an interfaces
|
|
@@ -2837,11 +3029,32 @@ const moduleDev = {
|
|
|
2837
3029
|
context.expect(after.folder, [refused(after)]);
|
|
2838
3030
|
}
|
|
2839
3031
|
};
|
|
2840
|
-
/**
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
3032
|
+
/**
|
|
3033
|
+
* What `ajs <group> init` creates, by group: the file it writes at the root,
|
|
3034
|
+
* whether `pnpm run build` must run before `ajs project dev`, and the flags
|
|
3035
|
+
* of `ajs project dev`. A module builds first and runs in its `playground`
|
|
3036
|
+
* project, which loads the module's build output.
|
|
3037
|
+
*/
|
|
3038
|
+
const CREATED = {
|
|
3039
|
+
project: {
|
|
3040
|
+
file: "antelope.config.ts",
|
|
3041
|
+
build: false,
|
|
3042
|
+
dev: ["--watch"]
|
|
3043
|
+
},
|
|
3044
|
+
module: {
|
|
3045
|
+
file: "package.json",
|
|
3046
|
+
build: true,
|
|
3047
|
+
dev: [
|
|
3048
|
+
"--watch",
|
|
3049
|
+
"--project",
|
|
3050
|
+
"playground"
|
|
3051
|
+
]
|
|
3052
|
+
}
|
|
3053
|
+
};
|
|
3054
|
+
/** Scaffolds a project, or a module for a module template, from the template into `project` with `ajs project init` or `ajs module init` and installs it. */
|
|
3055
|
+
async function scaffoldInto(context, project) {
|
|
3056
|
+
const init = await context.scaffold(`ajs ${templateInit(context.workspace)} init`, project);
|
|
3057
|
+
context.expect(init.folder, [exitIs(init, 0)], { critical: true });
|
|
2845
3058
|
context.tree("scaffolded tree", project);
|
|
2846
3059
|
const install = await context.command("pnpm install", "pnpm", [
|
|
2847
3060
|
"install",
|
|
@@ -2849,6 +3062,21 @@ async function scaffoldProject(context) {
|
|
|
2849
3062
|
"--prefer-offline"
|
|
2850
3063
|
], { cwd: project });
|
|
2851
3064
|
context.expect(install.folder, [exitIs(install, 0)], { critical: true });
|
|
3065
|
+
}
|
|
3066
|
+
/**
|
|
3067
|
+
* The journey's copy of the project scaffolded from the template and
|
|
3068
|
+
* installed, built once per run under the key `scaffolded`, which a repo
|
|
3069
|
+
* journey shares by preparing it with the same init and install. The copy is
|
|
3070
|
+
* checked for the file its init writes whichever journey built it, and the
|
|
3071
|
+
* journey's own HOME is pointed at the offline manifest again, since the
|
|
3072
|
+
* build's HOME is gone.
|
|
3073
|
+
*/
|
|
3074
|
+
async function scaffoldProject(context) {
|
|
3075
|
+
const project = await context.prepared("scaffolded", (folder) => scaffoldInto(context, folder));
|
|
3076
|
+
const tree = context.tree("scaffolded project", project);
|
|
3077
|
+
const created = CREATED[templateInit(context.workspace)];
|
|
3078
|
+
context.expect(tree.folder, [exists(join(project, created.file))], { critical: true });
|
|
3079
|
+
await context.configureGit();
|
|
2852
3080
|
return project;
|
|
2853
3081
|
}
|
|
2854
3082
|
/** The journeys each kind ships, by id; a feature file names them next to its repo-owned journeys. */
|
|
@@ -2857,17 +3085,23 @@ const BUILTIN_JOURNEYS = {
|
|
|
2857
3085
|
module: { "module-dev": moduleDev },
|
|
2858
3086
|
template: {
|
|
2859
3087
|
"scaffold-dev": {
|
|
2860
|
-
title: "a project scaffolded from the template installs, starts and answers",
|
|
2861
|
-
entryPoint: "`ajs project init --template` from the interfaces manifest, then `ajs project dev --watch`, as the template's `pnpm dev` runs it",
|
|
3088
|
+
title: "a project, or a module, scaffolded from the template installs, starts and answers",
|
|
3089
|
+
entryPoint: "`ajs project init --template` from the interfaces manifest, then `ajs project dev --watch`, as the template's `pnpm dev` runs it; for a module template `ajs module init --template`, `pnpm run build`, then `ajs project dev --watch --project playground`",
|
|
2862
3090
|
async run(context) {
|
|
2863
|
-
const
|
|
3091
|
+
const created = CREATED[templateInit(context.workspace)];
|
|
3092
|
+
const project = await scaffoldProject(context);
|
|
3093
|
+
if (created.build) {
|
|
3094
|
+
const build = await context.command("pnpm run build", "pnpm", ["run", "build"], { cwd: project });
|
|
3095
|
+
context.expect(build.folder, [exitIs(build, 0)], { critical: true });
|
|
3096
|
+
}
|
|
3097
|
+
const app = await context.dev(`ajs project dev ${created.dev.join(" ")}`, project, { args: [...created.dev] });
|
|
2864
3098
|
const stopped = await context.stop("SIGINT", app);
|
|
2865
3099
|
context.expect(stopped.folder, [interrupted(stopped)]);
|
|
2866
3100
|
}
|
|
2867
3101
|
},
|
|
2868
3102
|
"scaffold-checks": {
|
|
2869
|
-
title: "a project scaffolded from the template passes its own checks",
|
|
2870
|
-
entryPoint: "`ajs project init --template` from the interfaces manifest, then `pnpm run checks`",
|
|
3103
|
+
title: "a project, or a module, scaffolded from the template passes its own checks",
|
|
3104
|
+
entryPoint: "`ajs project init --template`, or `ajs module init --template` for a module template, from the interfaces manifest, then `pnpm run checks`",
|
|
2871
3105
|
async run(context) {
|
|
2872
3106
|
const project = await scaffoldProject(context);
|
|
2873
3107
|
const checks = await context.command("pnpm run checks", "pnpm", ["run", "checks"], { cwd: project });
|
|
@@ -2902,152 +3136,496 @@ function appFault(started) {
|
|
|
2902
3136
|
return null;
|
|
2903
3137
|
}
|
|
2904
3138
|
//#endregion
|
|
2905
|
-
//#region src/verify/
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
const CLOCK_ATTEMPTS = 100;
|
|
2913
|
-
/** A start time later than every command's, for a module the harness first sees outside a command: any lock record it has counts as one `ajs` installed it from. */
|
|
2914
|
-
const UNKNOWN_START = 2n ** 63n;
|
|
2915
|
-
/** A version `ajs` records for a registry package; a git module records `git:<branch>:<commit>`. */
|
|
2916
|
-
const REGISTRY_VERSION = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.+-]*)?$/;
|
|
2917
|
-
/** When the folder at `path` was created, which tells apart the folders created one after another there even when the file system reuses the inode; `0n` when the file system does not record it. */
|
|
2918
|
-
function bornAt(path) {
|
|
2919
|
-
try {
|
|
2920
|
-
return lstatSync(path, { bigint: true }).birthtimeNs;
|
|
2921
|
-
} catch {
|
|
2922
|
-
return 0n;
|
|
2923
|
-
}
|
|
3139
|
+
//#region src/verify/prepared.ts
|
|
3140
|
+
const KEY = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
3141
|
+
/** The path of `path` inside `root`, or `null` when it lies outside. */
|
|
3142
|
+
function inside(root, path) {
|
|
3143
|
+
const rest = relative(root, path);
|
|
3144
|
+
if (rest === ".." || rest.startsWith(`..${sep}`) || isAbsolute(rest)) return null;
|
|
3145
|
+
return rest;
|
|
2924
3146
|
}
|
|
2925
|
-
/**
|
|
2926
|
-
function
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
3147
|
+
/** Surveys `folder` in one walk that descends only into real directories, so it never follows a symlink; a target may name the folder by its real path. */
|
|
3148
|
+
function survey(folder) {
|
|
3149
|
+
const real = realpathSync(folder);
|
|
3150
|
+
const found = {
|
|
3151
|
+
links: [],
|
|
3152
|
+
special: null
|
|
3153
|
+
};
|
|
3154
|
+
const walk = (at) => {
|
|
3155
|
+
for (const entry of readdirSync(join(folder, at), { withFileTypes: true })) {
|
|
3156
|
+
const path = join(at, entry.name);
|
|
3157
|
+
if (entry.isDirectory()) walk(path);
|
|
3158
|
+
else if (entry.isSymbolicLink()) {
|
|
3159
|
+
const pointed = readlinkSync(join(folder, path));
|
|
3160
|
+
const target = isAbsolute(pointed) ? inside(folder, pointed) ?? inside(real, pointed) : null;
|
|
3161
|
+
if (target !== null) found.links.push({
|
|
3162
|
+
path,
|
|
3163
|
+
target
|
|
3164
|
+
});
|
|
3165
|
+
} else if (!entry.isFile()) found.special ??= path;
|
|
3166
|
+
}
|
|
3167
|
+
};
|
|
3168
|
+
walk("");
|
|
3169
|
+
return found;
|
|
2933
3170
|
}
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
3171
|
+
/** The files under `source` with a hard link outside it, such as package files pnpm links from its store; a file linked only within `source` is not one. */
|
|
3172
|
+
function sharedOutside(source) {
|
|
3173
|
+
const inodes = /* @__PURE__ */ new Map();
|
|
3174
|
+
const walk = (folder) => {
|
|
3175
|
+
for (const entry of readdirSync(folder, { withFileTypes: true })) {
|
|
3176
|
+
const path = join(folder, entry.name);
|
|
3177
|
+
if (entry.isDirectory()) walk(path);
|
|
3178
|
+
else if (entry.isFile()) {
|
|
3179
|
+
const { dev, ino, nlink } = lstatSync(path, { bigint: true });
|
|
3180
|
+
if (nlink < 2n) continue;
|
|
3181
|
+
const key = `${dev}:${ino}`;
|
|
3182
|
+
const inode = inodes.get(key) ?? {
|
|
3183
|
+
nlink,
|
|
3184
|
+
paths: []
|
|
3185
|
+
};
|
|
3186
|
+
inode.paths.push(path);
|
|
3187
|
+
inodes.set(key, inode);
|
|
3188
|
+
}
|
|
3189
|
+
}
|
|
3190
|
+
};
|
|
3191
|
+
walk(source);
|
|
3192
|
+
return new Set([...inodes.values()].filter((inode) => inode.nlink > BigInt(inode.paths.length)).flatMap((inode) => inode.paths));
|
|
2940
3193
|
}
|
|
2941
|
-
/**
|
|
2942
|
-
function
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
directory
|
|
2950
|
-
};
|
|
2951
|
-
if (directory) yield* entries(root, path);
|
|
3194
|
+
/** Hard-links `from` at `to` unless the filesystem allows `from` no more links; returns whether it did. */
|
|
3195
|
+
function linked(from, to) {
|
|
3196
|
+
try {
|
|
3197
|
+
linkSync(from, to);
|
|
3198
|
+
return true;
|
|
3199
|
+
} catch (error) {
|
|
3200
|
+
if (error instanceof Error && "code" in error && error.code === "EMLINK") return false;
|
|
3201
|
+
throw error;
|
|
2952
3202
|
}
|
|
2953
3203
|
}
|
|
2954
|
-
/**
|
|
2955
|
-
function
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
3204
|
+
/** Copies the tree at `source` to the new path `target` without following a symlink, linking each of `shared`; any other file is cloned where the filesystem can, else copied, with its timestamps. */
|
|
3205
|
+
function cloneTree(source, target, shared) {
|
|
3206
|
+
mkdirSync(target);
|
|
3207
|
+
for (const entry of readdirSync(source, { withFileTypes: true })) {
|
|
3208
|
+
const from = join(source, entry.name);
|
|
3209
|
+
const to = join(target, entry.name);
|
|
3210
|
+
if (entry.isDirectory()) cloneTree(from, to, shared);
|
|
3211
|
+
else if (entry.isSymbolicLink()) symlinkSync(readlinkSync(from), to);
|
|
3212
|
+
else if (!shared.has(from) || !linked(from, to)) {
|
|
3213
|
+
copyFileSync(from, to, constants.COPYFILE_FICLONE);
|
|
3214
|
+
const { atimeMs, mtimeMs } = statSync(from);
|
|
3215
|
+
utimesSync(to, atimeMs / 1e3, mtimeMs / 1e3);
|
|
3216
|
+
}
|
|
2960
3217
|
}
|
|
3218
|
+
chmodSync(target, statSync(source).mode);
|
|
2961
3219
|
}
|
|
2962
3220
|
/**
|
|
2963
|
-
* Copies `source`
|
|
2964
|
-
*
|
|
2965
|
-
*
|
|
2966
|
-
*
|
|
3221
|
+
* Copies `source` to `target`, which ends up at `root`. Each of `links`
|
|
3222
|
+
* points to its target inside `root`, so no copy reaches another journey's
|
|
3223
|
+
* folder. Every other symlink keeps its target, so pnpm's relative links in a
|
|
3224
|
+
* copy never point back into the source. A file with a hard link outside
|
|
3225
|
+
* `source` is linked, not copied.
|
|
2967
3226
|
*/
|
|
2968
|
-
function
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
verbatimSymlinks: true,
|
|
2974
|
-
preserveTimestamps: true
|
|
2975
|
-
});
|
|
2976
|
-
renameSync(staging, target);
|
|
2977
|
-
} catch {
|
|
2978
|
-
return;
|
|
2979
|
-
} finally {
|
|
2980
|
-
removeTree(staging);
|
|
3227
|
+
function copyTree(source, target, links = [], root = target) {
|
|
3228
|
+
cloneTree(source, target, sharedOutside(source));
|
|
3229
|
+
for (const link of links) {
|
|
3230
|
+
unlinkSync(join(target, link.path));
|
|
3231
|
+
symlinkSync(join(root, link.target), join(target, link.path));
|
|
2981
3232
|
}
|
|
2982
3233
|
}
|
|
2983
|
-
/**
|
|
2984
|
-
function
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
if (!isObject(manifest)) return void 0;
|
|
2989
|
-
const modules = Object.entries(manifest).map(([name, version]) => [name, String(version)]).sort(([left], [right]) => left < right ? -1 : 1);
|
|
2990
|
-
return {
|
|
2991
|
-
mtime: lstatSync(file, { bigint: true }).mtimeNs,
|
|
2992
|
-
modules
|
|
2993
|
-
};
|
|
2994
|
-
} catch {
|
|
3234
|
+
/** Writes `content` to `file`, through a new inode renamed over it when the file has other hard links, so the write reaches no other path, such as a copy `prepared` linked or pnpm's store, and a failed write leaves the file as it was. */
|
|
3235
|
+
function writeOwn(file, content) {
|
|
3236
|
+
const before = existsSync(file) ? statSync(file) : null;
|
|
3237
|
+
if (before === null || before.nlink < 2) {
|
|
3238
|
+
writeFileSync(file, content);
|
|
2995
3239
|
return;
|
|
2996
3240
|
}
|
|
3241
|
+
const real = realpathSync(file);
|
|
3242
|
+
accessSync(real, constants.W_OK);
|
|
3243
|
+
const scratch = mkdtempSync(join(dirname(real), ".edit-"));
|
|
3244
|
+
try {
|
|
3245
|
+
const fresh = join(scratch, "file");
|
|
3246
|
+
writeFileSync(fresh, content, { mode: before.mode });
|
|
3247
|
+
chmodSync(fresh, before.mode);
|
|
3248
|
+
renameSync(fresh, real);
|
|
3249
|
+
} finally {
|
|
3250
|
+
removeTree(scratch);
|
|
3251
|
+
}
|
|
3252
|
+
}
|
|
3253
|
+
/** Judges the build of `folder` by the steps it recorded. */
|
|
3254
|
+
function judge$1(steps, folder) {
|
|
3255
|
+
const checks = steps.flatMap((step) => step.checks);
|
|
3256
|
+
if (checks.length === 0) return {
|
|
3257
|
+
kept: false,
|
|
3258
|
+
reason: "the build recorded no check"
|
|
3259
|
+
};
|
|
3260
|
+
if (!checks.every((check) => check.pass)) return {
|
|
3261
|
+
kept: false,
|
|
3262
|
+
reason: "a check of the build failed"
|
|
3263
|
+
};
|
|
3264
|
+
const found = survey(folder);
|
|
3265
|
+
return found.special === null ? {
|
|
3266
|
+
kept: true,
|
|
3267
|
+
links: found.links
|
|
3268
|
+
} : {
|
|
3269
|
+
kept: false,
|
|
3270
|
+
reason: `${found.special} is not a file, directory or symlink, which a copy would drop`
|
|
3271
|
+
};
|
|
3272
|
+
}
|
|
3273
|
+
/** The links `keep` recorded, each a path then its target, joined by NUL, the one character no path holds. */
|
|
3274
|
+
function readLinks(kept) {
|
|
3275
|
+
const fields = readFileSync(join(kept, "links"), "utf8").split("\0");
|
|
3276
|
+
const links = [];
|
|
3277
|
+
for (let at = 0; at + 1 < fields.length; at += 2) links.push({
|
|
3278
|
+
path: fields[at],
|
|
3279
|
+
target: fields[at + 1]
|
|
3280
|
+
});
|
|
3281
|
+
return links;
|
|
3282
|
+
}
|
|
3283
|
+
/** The projects `keep` recorded; a build kept without the list holds none. */
|
|
3284
|
+
function readProjects(kept) {
|
|
3285
|
+
const file = join(kept, "projects.json");
|
|
3286
|
+
if (!existsSync(file)) return [];
|
|
3287
|
+
const listed = parseJson(readFileSync(file, "utf8"));
|
|
3288
|
+
return Array.isArray(listed) ? listed.filter(isObject).flatMap(({ path, cache }) => isString(path) ? [{
|
|
3289
|
+
path,
|
|
3290
|
+
cache: Array.isArray(cache) ? cache.filter(isString) : null
|
|
3291
|
+
}] : []) : [];
|
|
3292
|
+
}
|
|
3293
|
+
/** The journey that built what the run kept, as its path under the evidence. */
|
|
3294
|
+
function builder(kept) {
|
|
3295
|
+
return readFileSync(join(kept, "from.txt"), "utf8").trim();
|
|
2997
3296
|
}
|
|
2998
3297
|
/**
|
|
2999
|
-
*
|
|
3000
|
-
*
|
|
3001
|
-
*
|
|
3002
|
-
*
|
|
3298
|
+
* Copies what the run kept into the journey, with the evidence of that build
|
|
3299
|
+
* under the step `marker`, seeds each project the build seeded as
|
|
3300
|
+
* `scaffold` does, and returns `null`. When the copy fails, or what the run
|
|
3301
|
+
* kept is gone or replaced once it ends, as when the journey that built
|
|
3302
|
+
* reruns meanwhile, the partial copy is removed and this returns why.
|
|
3003
3303
|
*/
|
|
3004
|
-
function
|
|
3005
|
-
const
|
|
3006
|
-
folder: join(name),
|
|
3007
|
-
label: `${name}@${module.version}`,
|
|
3008
|
-
bound: module.bound
|
|
3009
|
-
}));
|
|
3304
|
+
function reuse$1(context, { marker, key, kept, folder }) {
|
|
3305
|
+
const evidence = join(marker, "build");
|
|
3010
3306
|
try {
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3307
|
+
const memo = statSync(kept).ino;
|
|
3308
|
+
copyTree(join(kept, "evidence"), evidence);
|
|
3309
|
+
copyTree(join(kept, "folder"), folder, readLinks(kept));
|
|
3310
|
+
const from = builder(kept);
|
|
3311
|
+
const projects = readProjects(kept);
|
|
3312
|
+
if (statSync(kept).ino !== memo) throw new Error(`${kept} was replaced while it was copied`);
|
|
3313
|
+
writeFileSync(join(marker, "prepared.txt"), `${key}: ${folder} is a copy of what ${from} prepared; build/ holds the evidence of that build\n`);
|
|
3314
|
+
for (const project of projects) context.caches.seed(join(folder, project.path), project.cache);
|
|
3315
|
+
return null;
|
|
3019
3316
|
} catch (error) {
|
|
3020
|
-
|
|
3317
|
+
removeTree(evidence);
|
|
3318
|
+
removeTree(folder);
|
|
3319
|
+
return error instanceof Error ? error.message : String(error);
|
|
3021
3320
|
}
|
|
3022
3321
|
}
|
|
3023
|
-
/**
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3322
|
+
/** Removes what the run kept by first renaming it away, so a journey copying it meanwhile fails instead of copying a half-deleted tree. */
|
|
3323
|
+
function drop(kept) {
|
|
3324
|
+
const trash = mkdtempSync(`${kept}.`);
|
|
3325
|
+
renameSync(kept, join(trash, "dropped"));
|
|
3326
|
+
removeTree(trash);
|
|
3327
|
+
}
|
|
3328
|
+
/** Keeps the built folder and the build's step evidence for the rest of the run, published by one rename; when another journey kept the key first, its copy stays and this returns `false`. */
|
|
3329
|
+
function keep(context, kept, folder, built) {
|
|
3330
|
+
mkdirSync(dirname(kept), { recursive: true });
|
|
3331
|
+
const staging = mkdtempSync(`${kept}.`);
|
|
3332
|
+
try {
|
|
3333
|
+
mkdirSync(join(staging, "evidence"));
|
|
3334
|
+
for (const step of built.steps) copyTree(join(context.folder, step), join(staging, "evidence", step));
|
|
3335
|
+
copyTree(folder, join(staging, "folder"), built.links, join(kept, "folder"));
|
|
3336
|
+
writeFileSync(join(staging, "links"), built.links.flatMap((link) => [link.path, link.target]).join("\0"));
|
|
3337
|
+
writeFileSync(join(staging, "projects.json"), JSON.stringify(built.projects));
|
|
3338
|
+
writeFileSync(join(staging, "from.txt"), `${relative(context.state.evidence, context.folder)}\n`);
|
|
3339
|
+
renameSync(staging, kept);
|
|
3340
|
+
return true;
|
|
3341
|
+
} catch (error) {
|
|
3342
|
+
if (!existsSync(kept)) throw error;
|
|
3343
|
+
return false;
|
|
3344
|
+
} finally {
|
|
3345
|
+
removeTree(staging);
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
/** Runs `build` with a HOME of its own for its commands, deleted when it ends. */
|
|
3349
|
+
async function buildAlone(context, key, folder, build) {
|
|
3350
|
+
const outer = context.buildHome;
|
|
3351
|
+
const home = join(context.home, `.prepared-${key}`);
|
|
3352
|
+
mkdirSync(home, { recursive: true });
|
|
3353
|
+
context.buildHome = home;
|
|
3354
|
+
try {
|
|
3355
|
+
await build(folder);
|
|
3356
|
+
} finally {
|
|
3357
|
+
context.buildHome = outer;
|
|
3358
|
+
removeTree(home);
|
|
3359
|
+
}
|
|
3360
|
+
}
|
|
3361
|
+
/** Builds `key` in this journey after the step `marker`, which marks the steps after it as the build and says, after `before`, whether the run kept it. */
|
|
3362
|
+
async function buildHere(context, where, build, before = "") {
|
|
3363
|
+
const { marker, key, kept, folder } = where;
|
|
3364
|
+
const note = (outcome) => writeFileSync(join(marker, "prepared.txt"), `${key}: ${before}${folder} was built here by the steps after this one; ${outcome}\n`);
|
|
3365
|
+
const earlier = new Set(readdirSync(context.folder));
|
|
3366
|
+
const checked = context.steps.length;
|
|
3367
|
+
mkdirSync(folder, { recursive: true });
|
|
3368
|
+
try {
|
|
3369
|
+
await buildAlone(context, key, folder, build);
|
|
3370
|
+
} catch (error) {
|
|
3371
|
+
note("not kept: the build threw, so the next journey builds again");
|
|
3372
|
+
throw error;
|
|
3373
|
+
}
|
|
3374
|
+
const verdict = judge$1(context.steps.slice(checked), folder);
|
|
3375
|
+
if (!verdict.kept) {
|
|
3376
|
+
note(`not kept: ${verdict.reason}, so the next journey builds again`);
|
|
3377
|
+
return;
|
|
3378
|
+
}
|
|
3379
|
+
const steps = readdirSync(context.folder).filter((entry) => !earlier.has(entry));
|
|
3380
|
+
const projects = context.caches.seeded().flatMap((project) => {
|
|
3381
|
+
const path = inside(folder, project);
|
|
3382
|
+
return path === null || !existsSync(project) ? [] : [{
|
|
3383
|
+
path,
|
|
3384
|
+
cache: context.caches.clean(project)
|
|
3385
|
+
}];
|
|
3386
|
+
});
|
|
3387
|
+
note(keep(context, kept, folder, {
|
|
3388
|
+
steps,
|
|
3389
|
+
links: verdict.links,
|
|
3390
|
+
projects
|
|
3391
|
+
}) ? "kept, so later journeys of the run copy it" : `not kept: ${builder(kept)} kept ${key} first, so later journeys copy that build`);
|
|
3392
|
+
}
|
|
3393
|
+
/** `JourneyContext.prepared` for one journey of the run. */
|
|
3394
|
+
async function prepare(context, key, build) {
|
|
3395
|
+
if (!KEY.test(key)) throw new Error(`prepared key ${JSON.stringify(key)} must be lowercase letters and digits joined by dashes`);
|
|
3396
|
+
const folder = join(context.work, key);
|
|
3397
|
+
if (existsSync(folder)) throw new Error(`${folder} already exists: a journey prepares ${key} once`);
|
|
3398
|
+
const kept = join(context.state.dir, "prepared", key);
|
|
3399
|
+
if (existsSync(kept) && builder(kept) === relative(context.state.evidence, context.folder)) drop(kept);
|
|
3400
|
+
const where = {
|
|
3401
|
+
marker: context.step(`prepared ${key}`),
|
|
3402
|
+
key,
|
|
3403
|
+
kept,
|
|
3404
|
+
folder
|
|
3405
|
+
};
|
|
3406
|
+
if (!existsSync(kept)) await buildHere(context, where, build);
|
|
3407
|
+
else {
|
|
3408
|
+
const failed = reuse$1(context, where);
|
|
3409
|
+
if (failed !== null) await buildHere(context, where, build, `copying what the run kept failed (${failed}), so `);
|
|
3410
|
+
}
|
|
3411
|
+
return folder;
|
|
3412
|
+
}
|
|
3413
|
+
//#endregion
|
|
3414
|
+
//#region src/verify/cache-copy.ts
|
|
3415
|
+
/** The file `ajs` lists a project cache's modules in, each name with its version. */
|
|
3416
|
+
const MANIFEST = "manifest.json";
|
|
3417
|
+
/** Stands in a kept cache for the path of the project cache it was kept from, which pnpm 10 and 11 write into the `NODE_PATH` of each `.bin` shim. */
|
|
3418
|
+
const KEPT_CACHE = `${sep}@antelopejs-verify-kept-cache@${sep}`;
|
|
3419
|
+
/** Whether `path` is `folder` or lies under it. */
|
|
3420
|
+
function within(folder, path) {
|
|
3421
|
+
return path === folder || path.startsWith(`${folder}${sep}`);
|
|
3422
|
+
}
|
|
3423
|
+
/** Every entry under `root` but those `skip` names and what they hold, by its path relative to `root`, with its modification time; symbolic links are listed, not followed. */
|
|
3424
|
+
function* entries(root, skip = () => false, prefix = "") {
|
|
3425
|
+
for (const entry of readdirSync(join(root, prefix), { withFileTypes: true })) {
|
|
3426
|
+
const path = join(prefix, entry.name);
|
|
3427
|
+
if (skip(path)) continue;
|
|
3428
|
+
const directory = entry.isDirectory();
|
|
3429
|
+
yield {
|
|
3430
|
+
path,
|
|
3431
|
+
mtime: lstatSync(join(root, path), { bigint: true }).mtimeNs,
|
|
3432
|
+
directory
|
|
3433
|
+
};
|
|
3434
|
+
if (directory) yield* entries(root, skip, path);
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
/** The path a module installed into `cache` finds it at, with its symbolic links resolved as the working directory of `pnpm` resolves them, ending with a separator. */
|
|
3438
|
+
function cachePath(cache) {
|
|
3439
|
+
return join(realpathSync(dirname(cache)), basename(cache)) + sep;
|
|
3440
|
+
}
|
|
3441
|
+
/** Replaces each of `from`, in order, by `to` in every `.bin` shim under `folder`, never through a hard link another path shares. */
|
|
3442
|
+
function retarget(folder, from, to) {
|
|
3443
|
+
for (const { path, directory } of entries(folder)) {
|
|
3444
|
+
if (directory || basename(dirname(path)) !== ".bin") continue;
|
|
3445
|
+
const file = join(folder, path);
|
|
3446
|
+
if (!lstatSync(file).isFile()) continue;
|
|
3447
|
+
const text = readFileSync(file, "utf8");
|
|
3448
|
+
const shim = from.reduce((current, old) => current.replaceAll(old, () => to), text);
|
|
3449
|
+
if (shim !== text) writeOwn(file, shim);
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
/**
|
|
3453
|
+
* Copies `source` into a staging entry next to `target`, replaces each of
|
|
3454
|
+
* `from` by `to` in its `.bin` shims, and renames it onto `target`, so a
|
|
3455
|
+
* reader sees all of the copy or none of it. A failed copy, a deleted source
|
|
3456
|
+
* or a `target` another writer filled first leaves `target` as it was. With
|
|
3457
|
+
* `only`, the copy leaves out every other module's folder and its manifest
|
|
3458
|
+
* lists those modules alone.
|
|
3459
|
+
*/
|
|
3460
|
+
function publish(source, target, from, to, only) {
|
|
3461
|
+
const staging = join(dirname(target), `.staging-${randomBytes(6).toString("hex")}`);
|
|
3462
|
+
try {
|
|
3463
|
+
cpSync(source, staging, {
|
|
3464
|
+
recursive: true,
|
|
3465
|
+
verbatimSymlinks: true,
|
|
3466
|
+
preserveTimestamps: true,
|
|
3467
|
+
filter: (path) => only === void 0 || !only.left.some((name) => within(join(source, name), path))
|
|
3468
|
+
});
|
|
3469
|
+
if (only !== void 0) writeFileSync(join(staging, MANIFEST), JSON.stringify(Object.fromEntries(only.modules)));
|
|
3470
|
+
retarget(staging, from, to);
|
|
3471
|
+
renameSync(staging, target);
|
|
3472
|
+
} catch {
|
|
3473
|
+
return;
|
|
3474
|
+
} finally {
|
|
3475
|
+
removeTree(staging);
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
3478
|
+
//#endregion
|
|
3479
|
+
//#region src/verify/caches.ts
|
|
3480
|
+
/** Where `ajs` installs the modules of a project that names no other `cacheFolder`. */
|
|
3481
|
+
const PROJECT_CACHE = join(".antelope", "cache");
|
|
3482
|
+
/** Where `ajs` keeps a project's lock records, one folder per `<package>@<version>`. */
|
|
3483
|
+
const LOCK_FOLDER = "antelope-lock";
|
|
3484
|
+
/** How many milliseconds the harness waits at most for the file system clock to pass an install. */
|
|
3485
|
+
const CLOCK_ATTEMPTS = 100;
|
|
3486
|
+
/** A start time later than every command's, for a module the harness first sees outside a command: any lock record it has counts as one `ajs` installed it from. */
|
|
3487
|
+
const UNKNOWN_START = 2n ** 63n;
|
|
3488
|
+
/** A version `ajs` records for a registry package; a git module records `git:<branch>:<commit>`. */
|
|
3489
|
+
const REGISTRY_VERSION = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.+-]*)?$/;
|
|
3490
|
+
/** When the folder at `path` was created, which tells apart the folders created one after another there even when the file system reuses the inode; `0n` when the file system does not record it. */
|
|
3491
|
+
function bornAt(path) {
|
|
3492
|
+
try {
|
|
3493
|
+
return lstatSync(path, { bigint: true }).birthtimeNs;
|
|
3494
|
+
} catch {
|
|
3495
|
+
return 0n;
|
|
3496
|
+
}
|
|
3497
|
+
}
|
|
3498
|
+
/** Whether `project` held the lock record of `name@version` when a command that started at `start` began; `ajs` writes a record only after an install that used none. */
|
|
3499
|
+
function pinned(project, name, version, start) {
|
|
3500
|
+
try {
|
|
3501
|
+
const record = join(project, LOCK_FOLDER, `${name}@${version}`);
|
|
3502
|
+
return lstatSync(record, { bigint: true }).mtimeNs <= start;
|
|
3503
|
+
} catch {
|
|
3504
|
+
return false;
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
function names(folder) {
|
|
3508
|
+
try {
|
|
3509
|
+
return readdirSync(folder).filter((name) => !name.startsWith("."));
|
|
3510
|
+
} catch {
|
|
3511
|
+
return [];
|
|
3512
|
+
}
|
|
3513
|
+
}
|
|
3514
|
+
/** Why a module the harness recorded as `known` is no longer that install, now that the manifest lists `version` in a folder created at `born`. */
|
|
3515
|
+
function reinstalled(name, known, version, born) {
|
|
3516
|
+
if (known.version !== version) return `${name} changed from ${known.version} to ${version} after ajs installed it`;
|
|
3517
|
+
if (born !== known.born) return `${name} was created again after ajs installed ${name}@${version}`;
|
|
3518
|
+
}
|
|
3519
|
+
/** Whether a project's antelope config (antelope.json or antelope.config.*) mentions `cacheFolder`, so `ajs` may install somewhere other than `.antelope/cache`. */
|
|
3520
|
+
function namesCacheFolder(project) {
|
|
3521
|
+
try {
|
|
3522
|
+
return readdirSync(project).filter((name) => /^antelope(\.config)?\.[cm]?[jt]s(on)?$/.test(name)).some((name) => /\bcacheFolder\b/.test(readFileSync(join(project, name), "utf8")));
|
|
3523
|
+
} catch {
|
|
3524
|
+
return false;
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
/** Why a registry module the project followed is no longer among `modules`, or `undefined` when each still is. */
|
|
3528
|
+
function departure(state, modules) {
|
|
3529
|
+
const listed = new Set(modules.map(([name]) => name));
|
|
3530
|
+
for (const [name, known] of state.installed) if (!listed.has(name)) return `${name} left ${MANIFEST} after ajs installed ${name}@${known.version}`;
|
|
3531
|
+
}
|
|
3532
|
+
/** Whether `name`, listed at `version`, is a git module `state` does not follow; one seen for the first time joins `added`. */
|
|
3533
|
+
function recordGit(state, name, version, added) {
|
|
3534
|
+
if (state.installed.has(name) || REGISTRY_VERSION.test(version)) return false;
|
|
3535
|
+
if (!state.git.has(name)) added.push(`${name}@${version}`);
|
|
3536
|
+
state.git.add(name);
|
|
3537
|
+
return true;
|
|
3538
|
+
}
|
|
3539
|
+
/** The manifest `ajs` writes each time it finishes installing a module: its modification time and each module's version, sorted by module. */
|
|
3540
|
+
function manifestOf(cache) {
|
|
3541
|
+
try {
|
|
3542
|
+
const file = join(cache, MANIFEST);
|
|
3543
|
+
const manifest = parseJson(readFileSync(file, "utf8"));
|
|
3544
|
+
if (!isObject(manifest)) return void 0;
|
|
3545
|
+
const modules = Object.entries(manifest).map(([name, version]) => [name, String(version)]).sort(([left], [right]) => left < right ? -1 : 1);
|
|
3546
|
+
return {
|
|
3547
|
+
mtime: lstatSync(file, { bigint: true }).mtimeNs,
|
|
3548
|
+
modules
|
|
3549
|
+
};
|
|
3550
|
+
} catch {
|
|
3551
|
+
return;
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
/**
|
|
3555
|
+
* Why `cache` is not exactly what `ajs` installed, or `undefined` when it is.
|
|
3556
|
+
* Each file of a module must be no newer than the module's bound. Outside the
|
|
3557
|
+
* modules, `ajs` writes only the manifest and the folders that hold the
|
|
3558
|
+
* modules, which must be no newer than `folders`: the manifest's time, or the
|
|
3559
|
+
* bound of a cache copied in whole when that is later, since a copy gives its
|
|
3560
|
+
* folders the time of the copy. Entries under the folders in `left`, which the
|
|
3561
|
+
* kept copy leaves out, are not checked.
|
|
3562
|
+
*/
|
|
3563
|
+
function changeIn(cache, installed, folders, left) {
|
|
3564
|
+
const modules = [...installed].map(([name, module]) => ({
|
|
3565
|
+
folder: join(name),
|
|
3566
|
+
label: `${name}@${module.version}`,
|
|
3567
|
+
bound: module.bound
|
|
3568
|
+
}));
|
|
3569
|
+
try {
|
|
3570
|
+
for (const { path, mtime, directory } of entries(cache, (path) => left.includes(path))) {
|
|
3571
|
+
if (path === "manifest.json") continue;
|
|
3572
|
+
const owner = modules.find(({ folder }) => within(folder, path));
|
|
3573
|
+
if (owner === void 0) {
|
|
3574
|
+
if (!directory) return `${path} belongs to no installed module`;
|
|
3575
|
+
if (mtime > folders) return `${path} changed after ajs wrote ${MANIFEST}`;
|
|
3576
|
+
} else if (mtime > owner.bound) return `${path} changed after ajs installed ${owner.label}`;
|
|
3577
|
+
}
|
|
3578
|
+
} catch (error) {
|
|
3579
|
+
return `it could not be read: ${error instanceof Error ? error.message : String(error)}`;
|
|
3580
|
+
}
|
|
3581
|
+
}
|
|
3582
|
+
/** Why the cache of a followed project is not exactly what a fresh `ajs` install gives, or `undefined` when it is. */
|
|
3583
|
+
function refusal(cache, state, manifest) {
|
|
3584
|
+
const left = manifest.modules.filter(([, version]) => !REGISTRY_VERSION.test(version)).map(([name]) => name);
|
|
3585
|
+
const folders = state.copied !== void 0 && state.copied > manifest.mtime ? state.copied : manifest.mtime;
|
|
3586
|
+
return state.refused ?? (left.length === manifest.modules.length ? `${manifest.modules[0]?.join("@")} is not a registry version, and ajs updates a git module in place` : changeIn(cache, state.installed, folders, left));
|
|
3587
|
+
}
|
|
3588
|
+
/**
|
|
3589
|
+
* The module caches of the projects one journey creates, and the run's kept
|
|
3590
|
+
* copies of them in `store`. Kept copies are never written, so journeys of
|
|
3591
|
+
* one run can copy them at once.
|
|
3592
|
+
*
|
|
3593
|
+
* Only a cache exactly as a fresh `ajs` install leaves it is kept. `ajs`
|
|
3594
|
+
* writes a registry module's files, then records its version in
|
|
3595
|
+
* `manifest.json`, so the manifest's modification time when the harness first
|
|
3596
|
+
* sees the module bounds every file of it. A file modified later, a module
|
|
3597
|
+
* whose version or folder changes after that, a module `ajs` installed from a
|
|
3598
|
+
* lock record the project held before the command, and two modules that
|
|
3599
|
+
* first appear after one command that is not `ajs`, which may have edited the
|
|
3600
|
+
* first before installing the second, each keep the cache out of the store. A
|
|
3601
|
+
* git module, which `ajs` updates in place and builds with the project's own
|
|
3602
|
+
* install command, is never followed nor kept: the copy holds the cache's
|
|
3603
|
+
* registry modules alone.
|
|
3604
|
+
*/
|
|
3605
|
+
var ProjectCaches = class {
|
|
3038
3606
|
store;
|
|
3039
3607
|
projects = /* @__PURE__ */ new Map();
|
|
3040
3608
|
constructor(store) {
|
|
3041
3609
|
this.store = store;
|
|
3042
3610
|
}
|
|
3043
|
-
/**
|
|
3044
|
-
|
|
3045
|
-
|
|
3611
|
+
/**
|
|
3612
|
+
* Gives a project that has installed no module yet a copy of the largest kept cache, so `ajs` reuses the packages an earlier journey installed instead of packing and installing them again. A project whose antelope config names its own `cacheFolder` gets no copy, since `ajs` would not read `.antelope/cache`.
|
|
3613
|
+
* A project that already holds modules is followed from what it holds. When `copiedFrom` is given, that cache is a copy of the cache `clean` accepted and named by those paths: its `.bin` shims are pointed at this project's cache and it is followed as a copy from the store is.
|
|
3614
|
+
*/
|
|
3615
|
+
seed(project, copiedFrom = null) {
|
|
3616
|
+
const state = {
|
|
3617
|
+
installed: /* @__PURE__ */ new Map(),
|
|
3618
|
+
git: /* @__PURE__ */ new Set()
|
|
3619
|
+
};
|
|
3046
3620
|
this.projects.set(project, state);
|
|
3047
3621
|
if (namesCacheFolder(project)) return;
|
|
3048
3622
|
const cache = join(project, PROJECT_CACHE);
|
|
3049
3623
|
if ((manifestOf(cache)?.modules.length ?? 0) > 0) {
|
|
3050
|
-
this.observe();
|
|
3624
|
+
if (copiedFrom === null) this.observe();
|
|
3625
|
+
else {
|
|
3626
|
+
retarget(cache, copiedFrom, cachePath(cache));
|
|
3627
|
+
this.adopt(state, cache);
|
|
3628
|
+
}
|
|
3051
3629
|
return;
|
|
3052
3630
|
}
|
|
3053
3631
|
let largest;
|
|
@@ -3061,7 +3639,11 @@ var ProjectCaches = class {
|
|
|
3061
3639
|
}
|
|
3062
3640
|
if (largest === void 0) return;
|
|
3063
3641
|
mkdirSync(dirname(cache), { recursive: true });
|
|
3064
|
-
publish(largest.folder, cache);
|
|
3642
|
+
publish(largest.folder, cache, [KEPT_CACHE], cachePath(cache));
|
|
3643
|
+
this.adopt(state, cache);
|
|
3644
|
+
}
|
|
3645
|
+
/** Follows each module of a cache copied whole from one exactly as `ajs` installed it, bounded by the newest entry of the copy, since a copy gives its folders the time of the copy. */
|
|
3646
|
+
adopt(state, cache) {
|
|
3065
3647
|
const copied = manifestOf(cache);
|
|
3066
3648
|
if (copied === void 0) return;
|
|
3067
3649
|
let bound = copied.mtime;
|
|
@@ -3075,27 +3657,45 @@ var ProjectCaches = class {
|
|
|
3075
3657
|
born: bornAt(join(cache, name)),
|
|
3076
3658
|
bound
|
|
3077
3659
|
});
|
|
3660
|
+
state.copied = bound;
|
|
3078
3661
|
this.passClock(bound);
|
|
3079
3662
|
}
|
|
3663
|
+
/** Every project seeded so far, so a copy of a folder that holds some can seed its own. */
|
|
3664
|
+
seeded() {
|
|
3665
|
+
return [...this.projects.keys()];
|
|
3666
|
+
}
|
|
3667
|
+
/** When the cache of a seeded `project` holds no module, or only registry modules exactly as `ajs` installed them, the paths its `.bin` shims may name, resolved and as given, which `seed` takes as `copiedFrom` for a copy of it; `null` otherwise. */
|
|
3668
|
+
clean(project) {
|
|
3669
|
+
this.observe();
|
|
3670
|
+
const state = this.projects.get(project);
|
|
3671
|
+
const cache = join(project, PROJECT_CACHE);
|
|
3672
|
+
const manifest = manifestOf(cache);
|
|
3673
|
+
return (manifest === void 0 || manifest.modules.length === 0 || state !== void 0 && manifest.modules.every(([, version]) => REGISTRY_VERSION.test(version)) && refusal(cache, state, manifest) === void 0) && existsSync(cache) ? [cachePath(cache), cache + sep] : null;
|
|
3674
|
+
}
|
|
3080
3675
|
/**
|
|
3081
|
-
* Records the bound of each module that appeared in a project's manifest
|
|
3676
|
+
* Records the bound of each registry module that appeared in a project's manifest
|
|
3082
3677
|
* since the last call, and refuses the cache of a project whose recorded
|
|
3083
3678
|
* module left the manifest, changed version or sits in a new folder. A
|
|
3084
3679
|
* module that appeared with a lock record no newer than `start`, the start
|
|
3085
|
-
* of the command that just ended, was installed from that record.
|
|
3680
|
+
* of the command that just ended, was installed from that record. After a
|
|
3681
|
+
* command that is not `ajs`, `ajs` may not be alone between two installs, so a
|
|
3682
|
+
* project whose manifest gained more than one module is refused.
|
|
3086
3683
|
*/
|
|
3087
|
-
observe(start = UNKNOWN_START) {
|
|
3684
|
+
observe(start = UNKNOWN_START, ajs = true) {
|
|
3088
3685
|
let latest = -1n;
|
|
3089
3686
|
for (const [project, state] of this.projects) {
|
|
3090
3687
|
const cache = join(project, PROJECT_CACHE);
|
|
3091
3688
|
const manifest = manifestOf(cache);
|
|
3092
3689
|
if (manifest === void 0) continue;
|
|
3093
|
-
const
|
|
3094
|
-
|
|
3690
|
+
const added = [];
|
|
3691
|
+
state.refused ??= departure(state, manifest.modules);
|
|
3095
3692
|
for (const [name, version] of manifest.modules) {
|
|
3096
3693
|
const born = bornAt(join(cache, name));
|
|
3097
3694
|
const known = state.installed.get(name);
|
|
3695
|
+
if (recordGit(state, name, version, added)) continue;
|
|
3098
3696
|
if (known === void 0) {
|
|
3697
|
+
added.push(`${name}@${version}`);
|
|
3698
|
+
if (state.git.has(name)) state.refused ??= `${name} changed from a git module to ${version} after ajs installed it`;
|
|
3099
3699
|
state.installed.set(name, {
|
|
3100
3700
|
version,
|
|
3101
3701
|
born,
|
|
@@ -3103,15 +3703,15 @@ var ProjectCaches = class {
|
|
|
3103
3703
|
});
|
|
3104
3704
|
if (manifest.mtime > latest) latest = manifest.mtime;
|
|
3105
3705
|
if (pinned(project, name, version, start)) state.refused ??= `ajs installed ${name}@${version} from the project's lock record`;
|
|
3106
|
-
} else
|
|
3107
|
-
else if (born !== known.born) state.refused ??= `${name} was created again after ajs installed ${name}@${version}`;
|
|
3706
|
+
} else state.refused ??= reinstalled(name, known, version, born);
|
|
3108
3707
|
}
|
|
3708
|
+
if (!ajs && added.length > 1) state.refused ??= `${added.join(" and ")} appeared in one command that is not ajs, which may have changed one of them before ajs installed the next`;
|
|
3109
3709
|
}
|
|
3110
3710
|
if (latest >= 0n) this.passClock(latest);
|
|
3111
3711
|
}
|
|
3112
|
-
/** Observes the project caches once a command or an app start has ended, and returns its result. Its `folder` is the step folder the harness created as it began, so a lock record no newer than that folder was there before the command. */
|
|
3113
|
-
observed(result) {
|
|
3114
|
-
this.observe(bornAt(result.folder) || UNKNOWN_START);
|
|
3712
|
+
/** Observes the project caches once a command or an app start has ended, and returns its result. Its `folder` is the step folder the harness created as it began, so a lock record no newer than that folder was there before the command. `ajs` tells whether only `ajs` ran, as in `ajs` commands and app starts, or a command that may also run other programs. */
|
|
3713
|
+
observed(result, ajs) {
|
|
3714
|
+
this.observe(bornAt(result.folder) || UNKNOWN_START, ajs);
|
|
3115
3715
|
return result;
|
|
3116
3716
|
}
|
|
3117
3717
|
/**
|
|
@@ -3147,220 +3747,321 @@ var ProjectCaches = class {
|
|
|
3147
3747
|
const cache = join(project, PROJECT_CACHE);
|
|
3148
3748
|
const manifest = manifestOf(cache);
|
|
3149
3749
|
if (manifest === void 0 || manifest.modules.length === 0) continue;
|
|
3150
|
-
const
|
|
3151
|
-
|
|
3152
|
-
const
|
|
3153
|
-
|
|
3750
|
+
const modules = manifest.modules.filter(([, version]) => REGISTRY_VERSION.test(version));
|
|
3751
|
+
const left = manifest.modules.filter(([, version]) => !REGISTRY_VERSION.test(version)).map(([name]) => name);
|
|
3752
|
+
const target = join(this.store, createHash("sha256").update(modules.map((entry) => entry.join("@")).join("\n")).digest("hex").slice(0, 16));
|
|
3753
|
+
if (modules.length > 0 && existsSync(target)) continue;
|
|
3754
|
+
const change = refusal(cache, state, manifest);
|
|
3154
3755
|
if (change !== void 0) {
|
|
3155
3756
|
skipped.push(`${cache}: ${change}`);
|
|
3156
3757
|
continue;
|
|
3157
3758
|
}
|
|
3158
3759
|
mkdirSync(this.store, { recursive: true });
|
|
3159
|
-
publish(cache, target)
|
|
3760
|
+
publish(cache, target, [cachePath(cache), cache + sep], KEPT_CACHE, left.length > 0 ? {
|
|
3761
|
+
modules,
|
|
3762
|
+
left
|
|
3763
|
+
} : void 0);
|
|
3160
3764
|
}
|
|
3161
3765
|
return skipped;
|
|
3162
3766
|
}
|
|
3163
3767
|
};
|
|
3164
3768
|
//#endregion
|
|
3165
|
-
//#region src/verify/
|
|
3166
|
-
const
|
|
3167
|
-
/** The
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
function
|
|
3175
|
-
const
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3769
|
+
//#region src/verify/readiness.ts
|
|
3770
|
+
const READY_TIMEOUT_MS = 12e4;
|
|
3771
|
+
/** The longest wait between readiness probes. */
|
|
3772
|
+
const POLL_MS = 250;
|
|
3773
|
+
/** The wait between readiness probes in the first `FAST_POLL_WINDOW_MS`, where most apps come up: an app ready in 300 ms is seen within 25 ms of it instead of at the 500 ms tick. */
|
|
3774
|
+
const FAST_POLL_MS = 25;
|
|
3775
|
+
/** How long readiness is probed every `FAST_POLL_MS` before falling back to `POLL_MS`. */
|
|
3776
|
+
const FAST_POLL_WINDOW_MS = 3e3;
|
|
3777
|
+
const SETTLE_MS = 3e3;
|
|
3778
|
+
async function probe(context, app, fixture, folder) {
|
|
3779
|
+
const probe = context.workspace.config.template?.probe;
|
|
3780
|
+
if (!fixture && probe?.log) {
|
|
3781
|
+
const output = readOutput(app.folder);
|
|
3782
|
+
return {
|
|
3783
|
+
name: `dev output matches ${probe.log}`,
|
|
3784
|
+
pass: probe.log.test(`${output.stdout}${output.stderr}`),
|
|
3785
|
+
detail: output.stderr.slice(-300)
|
|
3786
|
+
};
|
|
3787
|
+
}
|
|
3788
|
+
const path = fixture ? "/health" : probe?.path ?? "/";
|
|
3789
|
+
const answer = await httpRecorded(folder, `http://127.0.0.1:${app.port}${path}`);
|
|
3790
|
+
const status = fixture ? 200 : probe?.status ?? 200;
|
|
3791
|
+
const body = !fixture && probe?.body ? probe.body : /./s;
|
|
3792
|
+
return {
|
|
3793
|
+
name: `GET ${path} answers ${status} with ${body}`,
|
|
3794
|
+
pass: answer.status === status && body.test(answer.body),
|
|
3795
|
+
detail: answer.error ?? `status ${answer.status}, body ${answer.body.slice(0, 300)}`
|
|
3179
3796
|
};
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3797
|
+
}
|
|
3798
|
+
/** Probes a started app in a new `ready` step until it answers, exits or times out, and aborts the journey unless it came up and stayed up. */
|
|
3799
|
+
async function waitReady(context, app, handle, fixture) {
|
|
3800
|
+
const folder = context.step("ready");
|
|
3801
|
+
const deadline = Date.now() + READY_TIMEOUT_MS;
|
|
3802
|
+
let exited = false;
|
|
3803
|
+
handle.exited.then(() => {
|
|
3804
|
+
exited = true;
|
|
3805
|
+
});
|
|
3806
|
+
const fastUntil = Date.now() + FAST_POLL_WINDOW_MS;
|
|
3807
|
+
let check = await probe(context, app, fixture, folder);
|
|
3808
|
+
while (!check.pass && !exited && Date.now() < deadline) {
|
|
3809
|
+
await setTimeout$1(Date.now() < fastUntil ? FAST_POLL_MS : POLL_MS);
|
|
3810
|
+
check = await probe(context, app, fixture, folder);
|
|
3811
|
+
}
|
|
3812
|
+
const settle = context.workspace.config.template?.probe.log ? context.workspace.config.template.probe.settleMs ?? SETTLE_MS : 0;
|
|
3813
|
+
if (check.pass && settle > 0) await setTimeout$1(settle);
|
|
3814
|
+
const alive = {
|
|
3815
|
+
name: "still running after readiness",
|
|
3816
|
+
pass: !exited,
|
|
3817
|
+
detail: `waited ${settle} ms`
|
|
3193
3818
|
};
|
|
3194
|
-
|
|
3195
|
-
|
|
3819
|
+
writeFileSync(join(folder, "ready.txt"), `${check.pass && !exited ? "ready" : exited ? "exited before ready" : "timed out"}\n`);
|
|
3820
|
+
context.expect(folder, check.pass ? [check, alive] : [check], { critical: true });
|
|
3196
3821
|
}
|
|
3822
|
+
//#endregion
|
|
3823
|
+
//#region src/verify/registry.ts
|
|
3824
|
+
/** A longer line, counted in bytes, is not read for a failing request. */
|
|
3825
|
+
const MAX_LINE_BYTES = 8192;
|
|
3826
|
+
/** How long each half of the probe, the DNS lookup and the TCP connect, may take before it counts as failed. */
|
|
3827
|
+
const PROBE_TIMEOUT_MS = 3e3;
|
|
3828
|
+
/** The command words, after the program, that fetch packages or images. */
|
|
3829
|
+
const FETCHING = /* @__PURE__ */ new Map([
|
|
3830
|
+
["docker", [
|
|
3831
|
+
["pull"],
|
|
3832
|
+
["build"],
|
|
3833
|
+
["buildx", "build"],
|
|
3834
|
+
["compose", "pull"],
|
|
3835
|
+
["compose", "build"]
|
|
3836
|
+
]],
|
|
3837
|
+
["npm", [
|
|
3838
|
+
["install"],
|
|
3839
|
+
["ci"],
|
|
3840
|
+
["i"],
|
|
3841
|
+
["pack"],
|
|
3842
|
+
["view"]
|
|
3843
|
+
]],
|
|
3844
|
+
["pnpm", [
|
|
3845
|
+
["install"],
|
|
3846
|
+
["i"],
|
|
3847
|
+
["add"],
|
|
3848
|
+
["fetch"],
|
|
3849
|
+
["update"]
|
|
3850
|
+
]],
|
|
3851
|
+
["ajs", [["module", "install"], ["project", "init"]]]
|
|
3852
|
+
]);
|
|
3853
|
+
/** One shell word of a recorded command, quotes included. */
|
|
3854
|
+
const SHELL_WORD = /(?:'[^']*'|"(?:\\.|[^"\\])*"|\\.|[^\s'"\\])+/g;
|
|
3855
|
+
const SHELL_PIECE = /'([^']*)'|"((?:\\.|[^"\\])*)"|\\(.)/g;
|
|
3856
|
+
/** A path inside the `@antelopejs/core` package, which `node` runs as `ajs`. */
|
|
3857
|
+
const AJS_ENTRY = /(?:^|\/)@antelopejs\/core\//;
|
|
3858
|
+
function shellWords(command) {
|
|
3859
|
+
return (command.slice(0, 4096).match(SHELL_WORD) ?? []).slice(0, 16).map((word) => word.replace(SHELL_PIECE, (_whole, single, double, escaped) => single ?? double?.replace(/\\(.)/g, "$1") ?? escaped ?? ""));
|
|
3860
|
+
}
|
|
3861
|
+
/** Whether a step's `command.txt` runs a program and subcommand that fetch dependencies or images. */
|
|
3862
|
+
function fetchesDependencies(folder) {
|
|
3863
|
+
const file = join(folder, "command.txt");
|
|
3864
|
+
if (!existsSync(file)) return false;
|
|
3865
|
+
const text = readFileSync(file, "utf8");
|
|
3866
|
+
const words = shellWords((text.startsWith("cd ") ? text.slice(text.indexOf("\n") + 1) : text).split("\n")[0] ?? "");
|
|
3867
|
+
while (words.length > 0 && /^[A-Za-z_]\w*=/.test(words[0] ?? "")) words.shift();
|
|
3868
|
+
const first = words.shift();
|
|
3869
|
+
if (first === void 0) return false;
|
|
3870
|
+
let program = basename(first);
|
|
3871
|
+
if (program === "node" && AJS_ENTRY.test(words[0] ?? "")) {
|
|
3872
|
+
program = "ajs";
|
|
3873
|
+
words.shift();
|
|
3874
|
+
}
|
|
3875
|
+
return (FETCHING.get(program) ?? []).some((prefix) => prefix.every((word, index) => words[index] === word));
|
|
3876
|
+
}
|
|
3877
|
+
/** How a connection to a host fails before any answer, in any letter case. */
|
|
3878
|
+
const NETWORK_CAUSE = /\b(?:ENOTFOUND|EAI_AGAIN|ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENETUNREACH|EHOSTUNREACH|ERR_SOCKET_TIMEOUT|socket hang up|no such host|server misbehaving|i\/o timeout|TLS handshake timeout|context deadline exceeded|Temporary failure in name resolution|Client\.Timeout exceeded while awaiting headers|connection refused|network is unreachable)\b/i;
|
|
3879
|
+
/** The public npm, Docker Hub and container registries a fetch may name: fixed names only, since a name the product picks can fail DNS for the product's own reasons. */
|
|
3880
|
+
const REGISTRY_HOSTS = /* @__PURE__ */ new Set([
|
|
3881
|
+
"registry.npmjs.org",
|
|
3882
|
+
"registry.yarnpkg.com",
|
|
3883
|
+
"registry.npmmirror.com",
|
|
3884
|
+
"registry-1.docker.io",
|
|
3885
|
+
"auth.docker.io",
|
|
3886
|
+
"index.docker.io",
|
|
3887
|
+
"docker.io",
|
|
3888
|
+
"production.cloudflare.docker.com",
|
|
3889
|
+
"docker-images-prod.6aa30f8b08e16409b46e0173d6de2f56.r2.cloudflarestorage.com",
|
|
3890
|
+
"ghcr.io",
|
|
3891
|
+
"quay.io",
|
|
3892
|
+
"gcr.io",
|
|
3893
|
+
"mirror.gcr.io",
|
|
3894
|
+
"public.ecr.aws"
|
|
3895
|
+
]);
|
|
3896
|
+
/** The npm summary lines that repeat the error code with no request, such as `npm error code ENOTFOUND`, also behind a BuildKit `#8 12.31` prefix or indentation. */
|
|
3897
|
+
const NPM_SUMMARY = /^\s*(?:#\d+ [\d.]+ +)?npm (?:error|ERR!) (?:code|errno|syscall) [\w-]+\s*$/;
|
|
3898
|
+
/** A host with its optional port, in a URL or after a network cause. */
|
|
3899
|
+
const URL_HOST = String.raw`(\[[^\]\s]+\]|[^\s"/:?#\[\]]+)(?::(\d+))?`;
|
|
3900
|
+
/** The address after a network error code; with no port, it is dropped when a URL, `dial tcp` or `lookup` target on the line names the same host. */
|
|
3901
|
+
const BARE_TARGET = /\b(?:ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ETIMEDOUT|ECONNRESET)\s+(\[[^\]\s]+\]|[\w-]+(?:\.[\w-]+)+)(?::(\d+))?/gi;
|
|
3197
3902
|
/**
|
|
3198
|
-
*
|
|
3199
|
-
*
|
|
3200
|
-
*
|
|
3201
|
-
*
|
|
3903
|
+
* The places where a line names the target of a request: the URL after
|
|
3904
|
+
* `GET`, `HEAD`, `POST`, `PUT`, `DELETE` or `request to`, the `lookup <host>`
|
|
3905
|
+
* and `dial tcp <host>:<port>` of the Docker client, and the address after a
|
|
3906
|
+
* network error code.
|
|
3202
3907
|
*/
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3908
|
+
const TARGETS = [
|
|
3909
|
+
new RegExp(String.raw`(?:\b(?:get|head|post|put|delete)\b(?: request)?(?: to)?|\brequest to)[ ]+"?https?://${URL_HOST}`, "gi"),
|
|
3910
|
+
/\bdial tcp (\[[^\]\s]+\]|[\w.-]+):(\d+)/gi,
|
|
3911
|
+
/\blookup ([\w.-]+)/gi,
|
|
3912
|
+
BARE_TARGET
|
|
3913
|
+
];
|
|
3914
|
+
/** The targets of a line, or `null` when one of them is not a public registry on port 80 or 443. */
|
|
3915
|
+
function registryTargets(line) {
|
|
3916
|
+
const targets = [];
|
|
3917
|
+
for (const pattern of TARGETS) for (const match of line.matchAll(pattern)) {
|
|
3918
|
+
const host = (match[1] ?? "").toLowerCase().replace(/\.$/, "");
|
|
3919
|
+
const defaultPort = /http:\/\//i.test(match[0]) ? 80 : 443;
|
|
3920
|
+
const port = match[2] === void 0 ? defaultPort : Number(match[2]);
|
|
3921
|
+
if (!REGISTRY_HOSTS.has(host) || port !== 80 && port !== 443) return null;
|
|
3922
|
+
const named = targets.some((target) => target.host === host);
|
|
3923
|
+
if (pattern !== BARE_TARGET || match[2] !== void 0 || !named) targets.push({
|
|
3924
|
+
host,
|
|
3925
|
+
port
|
|
3926
|
+
});
|
|
3212
3927
|
}
|
|
3928
|
+
return targets;
|
|
3929
|
+
}
|
|
3930
|
+
/** The registry targets of every line with a network cause, or `null` when a failing request went anywhere else or a line names no target. */
|
|
3931
|
+
function failingTargets(texts) {
|
|
3932
|
+
const found = /* @__PURE__ */ new Map();
|
|
3933
|
+
for (const text of texts) for (const raw of text.split(/\r\n|\r|\n/)) {
|
|
3934
|
+
if (Buffer.byteLength(raw) > MAX_LINE_BYTES) continue;
|
|
3935
|
+
const line = stripVTControlCharacters(raw);
|
|
3936
|
+
if (!NETWORK_CAUSE.test(line)) continue;
|
|
3937
|
+
if (NPM_SUMMARY.test(line)) continue;
|
|
3938
|
+
const targets = registryTargets(line);
|
|
3939
|
+
if (targets === null || targets.length === 0) return null;
|
|
3940
|
+
for (const target of targets) found.set(`${target.host}:${target.port}`, target);
|
|
3941
|
+
}
|
|
3942
|
+
return [...found.values()];
|
|
3943
|
+
}
|
|
3944
|
+
function exitedNonZero(folder) {
|
|
3945
|
+
const file = join(folder, "exit-code.txt");
|
|
3946
|
+
return existsSync(file) && readFileSync(file, "utf8").trim() !== "0";
|
|
3947
|
+
}
|
|
3948
|
+
function streamsOf(folder) {
|
|
3949
|
+
return ["stdout.txt", "stderr.txt"].map((name) => join(folder, name)).filter((file) => existsSync(file)).map((file) => readFileSync(file, "utf8"));
|
|
3950
|
+
}
|
|
3951
|
+
/** The addresses a host resolves to now, or why it did not resolve within the timeout. */
|
|
3952
|
+
function resolve$1(host) {
|
|
3953
|
+
return new Promise((settle) => {
|
|
3954
|
+
const timer = setTimeout(() => settle({
|
|
3955
|
+
ok: false,
|
|
3956
|
+
failure: `no DNS answer within ${PROBE_TIMEOUT_MS} ms`
|
|
3957
|
+
}), PROBE_TIMEOUT_MS);
|
|
3958
|
+
lookup(host, { all: true }, (error, found) => {
|
|
3959
|
+
clearTimeout(timer);
|
|
3960
|
+
settle(error === null ? {
|
|
3961
|
+
ok: true,
|
|
3962
|
+
addresses: found.map((entry) => entry.address)
|
|
3963
|
+
} : {
|
|
3964
|
+
ok: false,
|
|
3965
|
+
failure: error.code ?? error.message
|
|
3966
|
+
});
|
|
3967
|
+
});
|
|
3968
|
+
});
|
|
3213
3969
|
}
|
|
3214
|
-
/**
|
|
3215
|
-
function
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
}
|
|
3234
|
-
/** The links `keep` recorded, each a path then its target, joined by NUL, the one character no path holds. */
|
|
3235
|
-
function readLinks(kept) {
|
|
3236
|
-
const fields = readFileSync(join(kept, "links"), "utf8").split("\0");
|
|
3237
|
-
const links = [];
|
|
3238
|
-
for (let at = 0; at + 1 < fields.length; at += 2) links.push({
|
|
3239
|
-
path: fields[at],
|
|
3240
|
-
target: fields[at + 1]
|
|
3970
|
+
/** `null` once a TCP connection to the address opens, or why it did not within the timeout. */
|
|
3971
|
+
function tcpConnect(address, port) {
|
|
3972
|
+
return new Promise((settle) => {
|
|
3973
|
+
const socket = connect({
|
|
3974
|
+
host: address,
|
|
3975
|
+
port
|
|
3976
|
+
});
|
|
3977
|
+
socket.setTimeout(PROBE_TIMEOUT_MS, () => {
|
|
3978
|
+
socket.destroy();
|
|
3979
|
+
settle(`no TCP connect within ${PROBE_TIMEOUT_MS} ms`);
|
|
3980
|
+
});
|
|
3981
|
+
socket.once("connect", () => {
|
|
3982
|
+
socket.destroy();
|
|
3983
|
+
settle(null);
|
|
3984
|
+
});
|
|
3985
|
+
socket.once("error", (error) => {
|
|
3986
|
+
socket.destroy();
|
|
3987
|
+
settle(error.code ?? error.message);
|
|
3988
|
+
});
|
|
3241
3989
|
});
|
|
3242
|
-
return links;
|
|
3243
3990
|
}
|
|
3244
|
-
/**
|
|
3245
|
-
function
|
|
3246
|
-
|
|
3991
|
+
/** Resolves the target's host now and opens a TCP connection to its port on every address it resolves to; one connect is enough to answer. */
|
|
3992
|
+
async function probeTarget({ host, port }) {
|
|
3993
|
+
const resolution = await resolve$1(host);
|
|
3994
|
+
if (!resolution.ok) return `DNS lookup of ${host} failed: ${resolution.failure}`;
|
|
3995
|
+
const failures = await Promise.all(resolution.addresses.map((address) => tcpConnect(address, port)));
|
|
3996
|
+
if (failures.some((failure) => failure === null)) return null;
|
|
3997
|
+
const causes = [...new Set(failures)].join(", ");
|
|
3998
|
+
return `TCP connect to ${host} port ${port} failed on all ${failures.length} addresses: ${causes}`;
|
|
3247
3999
|
}
|
|
3248
4000
|
/**
|
|
3249
|
-
*
|
|
3250
|
-
*
|
|
3251
|
-
*
|
|
3252
|
-
*
|
|
4001
|
+
* Why a failed journey ended because a container or package registry could
|
|
4002
|
+
* not be reached, or `null` when the product, not the network, explains it.
|
|
4003
|
+
*
|
|
4004
|
+
* The output decides only which registry to ask; a live probe decides the
|
|
4005
|
+
* outage. A missed outage stays FAIL, and when the registry answers the
|
|
4006
|
+
* probe the failure is the product's, whatever the output says.
|
|
4007
|
+
*
|
|
4008
|
+
* It reads only the first step with a failing check, and only when that
|
|
4009
|
+
* step's command exited non-zero or was signalled. The step's `command.txt`
|
|
4010
|
+
* must run a program and subcommand that fetch dependencies or images, after
|
|
4011
|
+
* any `NAME=value` prefix and whatever the program's path: `docker pull`,
|
|
4012
|
+
* `docker build`, `docker buildx build`, `docker compose pull` or `build`,
|
|
4013
|
+
* `npm install`, `ci`, `i`, `pack` or `view`, `pnpm install`, `i`, `add`,
|
|
4014
|
+
* `fetch` or `update`, and `ajs module install` or `ajs project init` (also
|
|
4015
|
+
* run as `node` on a script inside `@antelopejs/core`). The subcommand words
|
|
4016
|
+
* must follow the program directly, so `pnpm test`, `pnpm run`, `sh -c`,
|
|
4017
|
+
* `node`, `docker run` and `docker exec` stay FAIL.
|
|
4018
|
+
*
|
|
4019
|
+
* The failing requests are the lines of both streams, ANSI escapes stripped
|
|
4020
|
+
* and lines over 8 KiB skipped, that carry a network cause such as
|
|
4021
|
+
* `ENOTFOUND`, `EAI_AGAIN`, `ECONNREFUSED`, `ETIMEDOUT`, `no such host`, `i/o
|
|
4022
|
+
* timeout` or `Temporary failure in name resolution`. Their targets are the
|
|
4023
|
+
* URL after `GET`, `HEAD`, `POST`, `PUT`, `DELETE` or `request to`, the host
|
|
4024
|
+
* after `lookup`, the host before the port after `dial tcp`, and the address
|
|
4025
|
+
* after a network error code, on port 443, or 80 for an `http://` URL, when
|
|
4026
|
+
* no port is named. Every line with a network cause must name a target, npm's
|
|
4027
|
+
* `code`, `errno` and `syscall` summary lines aside (also indented or behind a
|
|
4028
|
+
* BuildKit `#8 12.31` prefix), and every target must be
|
|
4029
|
+
* one of these public registries on port 80 or 443: `registry.npmjs.org`,
|
|
4030
|
+
* `registry.yarnpkg.com`, `registry.npmmirror.com`, `registry-1.docker.io`,
|
|
4031
|
+
* `auth.docker.io`, `index.docker.io`, `docker.io`,
|
|
4032
|
+
* `production.cloudflare.docker.com`, Docker Hub's one blob host
|
|
4033
|
+
* `docker-images-prod.6aa30f8b08e16409b46e0173d6de2f56.r2.cloudflarestorage.com`, `ghcr.io`, `quay.io`, `gcr.io`, `mirror.gcr.io`
|
|
4034
|
+
* and `public.ecr.aws`. Only fixed names count: a name the product picks, such as
|
|
4035
|
+
* an ECR account registry or a regional `pkg.dev` host, can fail DNS for the
|
|
4036
|
+
* product's own reasons, so it stays FAIL, and so do an address, a private
|
|
4037
|
+
* mirror and `localhost`.
|
|
4038
|
+
*
|
|
4039
|
+
* Each target then gets a fresh probe: a DNS lookup and a TCP connect to
|
|
4040
|
+
* every address it resolves to, each bounded to 3 s, and one connect that
|
|
4041
|
+
* succeeds is an answer. The journey is NOT VERIFIED only when every target fails
|
|
4042
|
+
* it, and the reason names the step and each probe's failure. A registry
|
|
4043
|
+
* that answered with a 5xx or 429 passes the probe, so it stays FAIL.
|
|
3253
4044
|
*/
|
|
3254
|
-
function
|
|
3255
|
-
const evidence = join(marker, "build");
|
|
4045
|
+
async function registryFault(evidence, steps, probe = probeTarget) {
|
|
3256
4046
|
try {
|
|
3257
|
-
const
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
4047
|
+
const step = steps.find((entry) => entry.checks.some((check) => !check.pass));
|
|
4048
|
+
if (step === void 0) return null;
|
|
4049
|
+
const folder = join(evidence, step.folder);
|
|
4050
|
+
if (!exitedNonZero(folder) || !fetchesDependencies(folder)) return null;
|
|
4051
|
+
const targets = failingTargets(streamsOf(folder));
|
|
4052
|
+
if (targets === null || targets.length === 0) return null;
|
|
4053
|
+
const failures = await Promise.all(targets.map(probe));
|
|
4054
|
+
if (failures.some((failure) => failure === null)) return null;
|
|
4055
|
+
return `step ${step.name} could not reach a container or package registry, which also fails a fresh probe now, an outage outside the code under test: ${failures.join("; ")}`;
|
|
4056
|
+
} catch {
|
|
3263
4057
|
return null;
|
|
3264
|
-
} catch (error) {
|
|
3265
|
-
removeTree(evidence);
|
|
3266
|
-
removeTree(folder);
|
|
3267
|
-
return error instanceof Error ? error.message : String(error);
|
|
3268
4058
|
}
|
|
3269
4059
|
}
|
|
3270
|
-
/** Removes what the run kept by first renaming it away, so a journey copying it meanwhile fails instead of copying a half-deleted tree. */
|
|
3271
|
-
function drop(kept) {
|
|
3272
|
-
const trash = mkdtempSync(`${kept}.`);
|
|
3273
|
-
renameSync(kept, join(trash, "dropped"));
|
|
3274
|
-
removeTree(trash);
|
|
3275
|
-
}
|
|
3276
|
-
/** Keeps the built folder and the build's step evidence for the rest of the run, published by one rename; when another journey kept the key first, its copy stays and this returns `false`. */
|
|
3277
|
-
function keep(context, kept, folder, built) {
|
|
3278
|
-
mkdirSync(dirname(kept), { recursive: true });
|
|
3279
|
-
const staging = mkdtempSync(`${kept}.`);
|
|
3280
|
-
try {
|
|
3281
|
-
mkdirSync(join(staging, "evidence"));
|
|
3282
|
-
for (const step of built.steps) copyTree(join(context.folder, step), join(staging, "evidence", step));
|
|
3283
|
-
copyTree(folder, join(staging, "folder"), built.links, join(kept, "folder"));
|
|
3284
|
-
writeFileSync(join(staging, "links"), built.links.flatMap((link) => [link.path, link.target]).join("\0"));
|
|
3285
|
-
writeFileSync(join(staging, "from.txt"), `${relative(context.state.evidence, context.folder)}\n`);
|
|
3286
|
-
renameSync(staging, kept);
|
|
3287
|
-
return true;
|
|
3288
|
-
} catch (error) {
|
|
3289
|
-
if (!existsSync(kept)) throw error;
|
|
3290
|
-
return false;
|
|
3291
|
-
} finally {
|
|
3292
|
-
removeTree(staging);
|
|
3293
|
-
}
|
|
3294
|
-
}
|
|
3295
|
-
/** Runs `build` with a HOME of its own for its commands, deleted when it ends. */
|
|
3296
|
-
async function buildAlone(context, key, folder, build) {
|
|
3297
|
-
const outer = context.buildHome;
|
|
3298
|
-
const home = join(context.home, `.prepared-${key}`);
|
|
3299
|
-
mkdirSync(home, { recursive: true });
|
|
3300
|
-
context.buildHome = home;
|
|
3301
|
-
try {
|
|
3302
|
-
await build(folder);
|
|
3303
|
-
} finally {
|
|
3304
|
-
context.buildHome = outer;
|
|
3305
|
-
removeTree(home);
|
|
3306
|
-
}
|
|
3307
|
-
}
|
|
3308
|
-
/** Builds `key` in this journey after the step `marker`, which marks the steps after it as the build and says, after `before`, whether the run kept it. */
|
|
3309
|
-
async function buildHere(context, where, build, before = "") {
|
|
3310
|
-
const { marker, key, kept, folder } = where;
|
|
3311
|
-
const note = (outcome) => writeFileSync(join(marker, "prepared.txt"), `${key}: ${before}${folder} was built here by the steps after this one; ${outcome}\n`);
|
|
3312
|
-
const earlier = new Set(readdirSync(context.folder));
|
|
3313
|
-
const checked = context.steps.length;
|
|
3314
|
-
mkdirSync(folder, { recursive: true });
|
|
3315
|
-
try {
|
|
3316
|
-
await buildAlone(context, key, folder, build);
|
|
3317
|
-
} catch (error) {
|
|
3318
|
-
note("not kept: the build threw, so the next journey builds again");
|
|
3319
|
-
throw error;
|
|
3320
|
-
}
|
|
3321
|
-
const verdict = judge$1(context.steps.slice(checked), folder);
|
|
3322
|
-
if (!verdict.kept) {
|
|
3323
|
-
note(`not kept: ${verdict.reason}, so the next journey builds again`);
|
|
3324
|
-
return;
|
|
3325
|
-
}
|
|
3326
|
-
note(keep(context, kept, folder, {
|
|
3327
|
-
steps: readdirSync(context.folder).filter((entry) => !earlier.has(entry)),
|
|
3328
|
-
links: verdict.links
|
|
3329
|
-
}) ? "kept, so later journeys of the run copy it" : `not kept: ${builder(kept)} kept ${key} first, so later journeys copy that build`);
|
|
3330
|
-
}
|
|
3331
|
-
/** `JourneyContext.prepared` for one journey of the run. */
|
|
3332
|
-
async function prepare(context, key, build) {
|
|
3333
|
-
if (!KEY.test(key)) throw new Error(`prepared key ${JSON.stringify(key)} must be lowercase letters and digits joined by dashes`);
|
|
3334
|
-
const folder = join(context.work, key);
|
|
3335
|
-
if (existsSync(folder)) throw new Error(`${folder} already exists: a journey prepares ${key} once`);
|
|
3336
|
-
const kept = join(context.state.dir, "prepared", key);
|
|
3337
|
-
if (existsSync(kept) && builder(kept) === relative(context.state.evidence, context.folder)) drop(kept);
|
|
3338
|
-
const where = {
|
|
3339
|
-
marker: context.step(`prepared ${key}`),
|
|
3340
|
-
key,
|
|
3341
|
-
kept,
|
|
3342
|
-
folder
|
|
3343
|
-
};
|
|
3344
|
-
if (!existsSync(kept)) await buildHere(context, where, build);
|
|
3345
|
-
else {
|
|
3346
|
-
const failed = reuse$1(where);
|
|
3347
|
-
if (failed !== null) await buildHere(context, where, build, `copying what the run kept failed (${failed}), so `);
|
|
3348
|
-
}
|
|
3349
|
-
return folder;
|
|
3350
|
-
}
|
|
3351
4060
|
//#endregion
|
|
3352
4061
|
//#region src/verify/context.ts
|
|
3353
4062
|
var JourneyAbort = class extends Error {};
|
|
3354
4063
|
var NotVerified = class extends Error {};
|
|
3355
|
-
const READY_TIMEOUT_MS = 12e4;
|
|
3356
4064
|
const STOP_TIMEOUT_MS = 3e4;
|
|
3357
|
-
/** The longest wait between readiness probes. */
|
|
3358
|
-
const POLL_MS = 250;
|
|
3359
|
-
/** The wait between readiness probes in the first `FAST_POLL_WINDOW_MS`, where most apps come up: an app ready in 300 ms is seen within 25 ms of it instead of at the 500 ms tick. */
|
|
3360
|
-
const FAST_POLL_MS = 25;
|
|
3361
|
-
/** How long readiness is probed every `FAST_POLL_MS` before falling back to `POLL_MS`. */
|
|
3362
|
-
const FAST_POLL_WINDOW_MS = 3e3;
|
|
3363
|
-
const SETTLE_MS = 3e3;
|
|
3364
4065
|
/** The waits between the readiness reruns after a failed journey: one miss under load does not hide a catch. */
|
|
3365
4066
|
const READINESS_RETRY_MS = [
|
|
3366
4067
|
500,
|
|
@@ -3370,6 +4071,15 @@ const READINESS_RETRY_MS = [
|
|
|
3370
4071
|
function slug(text) {
|
|
3371
4072
|
return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
3372
4073
|
}
|
|
4074
|
+
/** Sends `name` to `pid`, returning whether the signal was delivered. */
|
|
4075
|
+
function signal(pid, name) {
|
|
4076
|
+
try {
|
|
4077
|
+
process.kill(pid, name);
|
|
4078
|
+
return true;
|
|
4079
|
+
} catch {
|
|
4080
|
+
return false;
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
3373
4083
|
/** The harness side of a journey: every driving call writes its evidence into a new numbered step folder. */
|
|
3374
4084
|
var VerifyContext = class {
|
|
3375
4085
|
repo;
|
|
@@ -3421,22 +4131,25 @@ var VerifyContext = class {
|
|
|
3421
4131
|
}
|
|
3422
4132
|
prepared = (key, build) => prepare(this, key, build);
|
|
3423
4133
|
command(name, program, args, options = {}) {
|
|
4134
|
+
return this.run(name, program, args, options, false);
|
|
4135
|
+
}
|
|
4136
|
+
/** Runs `ajs <args>` of the repository's `@antelopejs/core`. */
|
|
4137
|
+
ajs(name, args, options = {}) {
|
|
4138
|
+
return this.run(name, process.execPath, [ajsEntry(this.repo), ...args], options, true);
|
|
4139
|
+
}
|
|
4140
|
+
run(name, program, args, options, ajs) {
|
|
3424
4141
|
return runRecorded(this.step(name), {
|
|
3425
4142
|
program,
|
|
3426
4143
|
args,
|
|
3427
4144
|
cwd: options.cwd ?? this.work,
|
|
3428
4145
|
env: this.env(options.env),
|
|
3429
4146
|
timeoutMs: options.timeoutMs
|
|
3430
|
-
}).then((result) => this.caches.observed(result));
|
|
3431
|
-
}
|
|
3432
|
-
/** Runs `ajs <args>` of the repository's `@antelopejs/core`. */
|
|
3433
|
-
ajs(name, args, options = {}) {
|
|
3434
|
-
return this.command(name, process.execPath, [ajsEntry(this.repo), ...args], options);
|
|
4147
|
+
}).then((result) => this.caches.observed(result, ajs));
|
|
3435
4148
|
}
|
|
3436
4149
|
edit(name, file, content) {
|
|
3437
4150
|
const folder = this.step(name);
|
|
3438
4151
|
mkdirSync(join(file, ".."), { recursive: true });
|
|
3439
|
-
|
|
4152
|
+
writeOwn(file, content);
|
|
3440
4153
|
writeFileSync(join(folder, "edit.txt"), `${file}\n---\n${content}`);
|
|
3441
4154
|
return folder;
|
|
3442
4155
|
}
|
|
@@ -3488,7 +4201,10 @@ var VerifyContext = class {
|
|
|
3488
4201
|
fixture(options = {}) {
|
|
3489
4202
|
const config = options.moduleConfig ?? this.workspace.config.moduleConfig?.(this.services()) ?? {};
|
|
3490
4203
|
const folder = this.fixtures.size === 0 ? this.work : join(this.work, `fixture-${this.fixtures.size + 1}`);
|
|
3491
|
-
const fixture = writeFixture(this.workspace, this.state, folder,
|
|
4204
|
+
const fixture = writeFixture(this.workspace, this.state, folder, {
|
|
4205
|
+
module: config,
|
|
4206
|
+
companions: this.workspace.config.companions?.(this.services()) ?? {}
|
|
4207
|
+
});
|
|
3492
4208
|
this.fixtures.set(fixture.project, fixture);
|
|
3493
4209
|
this.tree("fixture project", fixture.project);
|
|
3494
4210
|
this.caches.seed(fixture.project);
|
|
@@ -3519,14 +4235,15 @@ var VerifyContext = class {
|
|
|
3519
4235
|
...options.env
|
|
3520
4236
|
};
|
|
3521
4237
|
const folder = this.step(name);
|
|
4238
|
+
const [program, ...args] = options.command ?? [
|
|
4239
|
+
process.execPath,
|
|
4240
|
+
ajsEntry(this.repo),
|
|
4241
|
+
"project",
|
|
4242
|
+
"dev"
|
|
4243
|
+
];
|
|
3522
4244
|
const handle = startRecorded(folder, {
|
|
3523
|
-
program
|
|
3524
|
-
args: [
|
|
3525
|
-
ajsEntry(this.repo),
|
|
3526
|
-
"project",
|
|
3527
|
-
"dev",
|
|
3528
|
-
...options.args ?? []
|
|
3529
|
-
],
|
|
4245
|
+
program,
|
|
4246
|
+
args: [...args, ...options.args ?? []],
|
|
3530
4247
|
cwd: project,
|
|
3531
4248
|
env: this.env(env)
|
|
3532
4249
|
});
|
|
@@ -3553,51 +4270,8 @@ var VerifyContext = class {
|
|
|
3553
4270
|
name: `${name} (${project})`,
|
|
3554
4271
|
pid: handle.pid
|
|
3555
4272
|
}] });
|
|
3556
|
-
await
|
|
3557
|
-
return this.caches.observed(app);
|
|
3558
|
-
}
|
|
3559
|
-
async probe(app, fixture, folder) {
|
|
3560
|
-
const probe = this.workspace.config.template?.probe;
|
|
3561
|
-
if (!fixture && probe?.log) {
|
|
3562
|
-
const output = readOutput(app.folder);
|
|
3563
|
-
return {
|
|
3564
|
-
name: `dev output matches ${probe.log}`,
|
|
3565
|
-
pass: probe.log.test(`${output.stdout}${output.stderr}`),
|
|
3566
|
-
detail: output.stderr.slice(-300)
|
|
3567
|
-
};
|
|
3568
|
-
}
|
|
3569
|
-
const path = fixture ? "/health" : probe?.path ?? "/";
|
|
3570
|
-
const answer = await httpRecorded(folder, `http://127.0.0.1:${app.port}${path}`);
|
|
3571
|
-
const status = fixture ? 200 : probe?.status ?? 200;
|
|
3572
|
-
const body = !fixture && probe?.body ? probe.body : /./s;
|
|
3573
|
-
return {
|
|
3574
|
-
name: `GET ${path} answers ${status} with ${body}`,
|
|
3575
|
-
pass: answer.status === status && body.test(answer.body),
|
|
3576
|
-
detail: answer.error ?? `status ${answer.status}, body ${answer.body.slice(0, 300)}`
|
|
3577
|
-
};
|
|
3578
|
-
}
|
|
3579
|
-
async waitReady(app, handle, fixture) {
|
|
3580
|
-
const folder = this.step("ready");
|
|
3581
|
-
const deadline = Date.now() + READY_TIMEOUT_MS;
|
|
3582
|
-
let exited = false;
|
|
3583
|
-
handle.exited.then(() => {
|
|
3584
|
-
exited = true;
|
|
3585
|
-
});
|
|
3586
|
-
const fastUntil = Date.now() + FAST_POLL_WINDOW_MS;
|
|
3587
|
-
let check = await this.probe(app, fixture, folder);
|
|
3588
|
-
while (!check.pass && !exited && Date.now() < deadline) {
|
|
3589
|
-
await setTimeout$1(Date.now() < fastUntil ? FAST_POLL_MS : POLL_MS);
|
|
3590
|
-
check = await this.probe(app, fixture, folder);
|
|
3591
|
-
}
|
|
3592
|
-
const settle = this.workspace.config.template?.probe.log ? this.workspace.config.template.probe.settleMs ?? SETTLE_MS : 0;
|
|
3593
|
-
if (check.pass && settle > 0) await setTimeout$1(settle);
|
|
3594
|
-
const alive = {
|
|
3595
|
-
name: "still running after readiness",
|
|
3596
|
-
pass: !exited,
|
|
3597
|
-
detail: `waited ${settle} ms`
|
|
3598
|
-
};
|
|
3599
|
-
writeFileSync(join(folder, "ready.txt"), `${check.pass && !exited ? "ready" : exited ? "exited before ready" : "timed out"}\n`);
|
|
3600
|
-
this.expect(folder, check.pass ? [check, alive] : [check], { critical: true });
|
|
4273
|
+
await waitReady(this, app, handle, fixture !== void 0);
|
|
4274
|
+
return this.caches.observed(app, true);
|
|
3601
4275
|
}
|
|
3602
4276
|
/** Posts a JSON body to a route of the driver module. */
|
|
3603
4277
|
async driver(name, app, route, body) {
|
|
@@ -3617,9 +4291,9 @@ var VerifyContext = class {
|
|
|
3617
4291
|
const body = {
|
|
3618
4292
|
path: request.path,
|
|
3619
4293
|
args: request.args ?? [],
|
|
3620
|
-
chain: request.chain ?? []
|
|
3621
|
-
entry: request.entry ?? "."
|
|
4294
|
+
chain: request.chain ?? []
|
|
3622
4295
|
};
|
|
4296
|
+
if (request.entry) body.entry = request.entry;
|
|
3623
4297
|
if (request.interface) body.interface = request.interface;
|
|
3624
4298
|
return this.driver(name, app, "/call", body);
|
|
3625
4299
|
}
|
|
@@ -3627,12 +4301,8 @@ var VerifyContext = class {
|
|
|
3627
4301
|
const folder = this.step(name);
|
|
3628
4302
|
writeFileSync(join(folder, "command.txt"), `kill -INT ${app.pid}\n`);
|
|
3629
4303
|
const running = this.running.find((entry) => entry.app.pid === app.pid);
|
|
3630
|
-
running?.sent.push("SIGINT");
|
|
3631
|
-
|
|
3632
|
-
process.kill(app.pid, "SIGINT");
|
|
3633
|
-
} catch {
|
|
3634
|
-
writeFileSync(join(folder, "note.txt"), "process was already gone\n");
|
|
3635
|
-
}
|
|
4304
|
+
if (!running?.exit && signal(app.pid, "SIGINT")) running?.sent.push("SIGINT");
|
|
4305
|
+
else writeFileSync(join(folder, "note.txt"), "process was already gone\n");
|
|
3636
4306
|
let timer;
|
|
3637
4307
|
const timeout = new Promise((resolve) => {
|
|
3638
4308
|
timer = setTimeout(() => resolve(void 0), STOP_TIMEOUT_MS);
|
|
@@ -3664,11 +4334,8 @@ var VerifyContext = class {
|
|
|
3664
4334
|
if (index !== -1) this.running.splice(index, 1);
|
|
3665
4335
|
recordProcesses(this.state, { drop: [pid] });
|
|
3666
4336
|
}
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
"pnpm",
|
|
3670
|
-
"--git-init"
|
|
3671
|
-
]) {
|
|
4337
|
+
/** Points `ajs` at the run's offline manifest in the HOME the next commands get. */
|
|
4338
|
+
async configureGit() {
|
|
3672
4339
|
const set = await this.ajs("config set git", [
|
|
3673
4340
|
"config",
|
|
3674
4341
|
"set",
|
|
@@ -3680,15 +4347,23 @@ var VerifyContext = class {
|
|
|
3680
4347
|
pass: set.exitCode === 0,
|
|
3681
4348
|
detail: set.stderr.slice(-300)
|
|
3682
4349
|
}], { critical: true });
|
|
4350
|
+
}
|
|
4351
|
+
async scaffold(name, project, flags = [
|
|
4352
|
+
"--pm",
|
|
4353
|
+
"pnpm",
|
|
4354
|
+
"--git-init"
|
|
4355
|
+
]) {
|
|
4356
|
+
await this.configureGit();
|
|
4357
|
+
const group = templateInit(this.workspace);
|
|
3683
4358
|
const init = await this.ajs(name, [
|
|
3684
|
-
|
|
4359
|
+
group,
|
|
3685
4360
|
"init",
|
|
3686
4361
|
relative(this.work, project),
|
|
3687
4362
|
"--template",
|
|
3688
4363
|
this.state.templateName,
|
|
3689
4364
|
...flags
|
|
3690
4365
|
]);
|
|
3691
|
-
if (init.exitCode === 0) this.caches.seed(project);
|
|
4366
|
+
if (init.exitCode === 0 && group === "project") this.caches.seed(project);
|
|
3692
4367
|
return init;
|
|
3693
4368
|
}
|
|
3694
4369
|
/** Stops every app the journey left running. */
|
|
@@ -3700,16 +4375,18 @@ var VerifyContext = class {
|
|
|
3700
4375
|
* or `null` when nothing points away from the product: a service that
|
|
3701
4376
|
* still fails its readiness command after a few reruns, an app killed by
|
|
3702
4377
|
* SIGKILL, SIGTERM, SIGHUP or SIGINT that the harness did not send, or the
|
|
3703
|
-
* app the harness started failing to bind the driver's port
|
|
3704
|
-
*
|
|
3705
|
-
*
|
|
4378
|
+
* app the harness started failing to bind the driver's port, or a failed
|
|
4379
|
+
* dependency or image fetch whose public registry also fails a fresh DNS
|
|
4380
|
+
* and TCP probe (see `registryFault`). A break that takes its own service
|
|
4381
|
+
* down reads as an outage. Call it after `cleanup`, when every app has
|
|
4382
|
+
* exited.
|
|
3706
4383
|
*/
|
|
3707
4384
|
async environmentFault() {
|
|
3708
4385
|
for (const [name, service] of Object.entries(this.workspace.config.services ?? {})) {
|
|
3709
4386
|
const failure = await this.unready(name, service.ready);
|
|
3710
4387
|
if (failure !== null) return `the service ${name} no longer passes its readiness command: ${failure}`;
|
|
3711
4388
|
}
|
|
3712
|
-
return appFault(this.started);
|
|
4389
|
+
return appFault(this.started) ?? await registryFault(this.state.evidence, this.steps);
|
|
3713
4390
|
}
|
|
3714
4391
|
/** The last readiness failure of a service after the reruns, or `null` once one rerun passes. */
|
|
3715
4392
|
async unready(name, ready) {
|
|
@@ -4269,7 +4946,8 @@ const USAGE = `Usage: antelopejs-verify <command>
|
|
|
4269
4946
|
formal <run> [--all] run the formal checks that cover the diff and gate them on changed behaviour
|
|
4270
4947
|
doors [patch] [--base <ref>] print the One-way-door line for a patch, or for the diff
|
|
4271
4948
|
clean <run> stop what the run started, keep only the evidence
|
|
4272
|
-
verdict <run> --pr <n>
|
|
4949
|
+
verdict <run> --pr <n> [--judgement <file>]
|
|
4950
|
+
store the judgement as evidence/judgement.json, then write evidence/verdict.md from it and the results
|
|
4273
4951
|
`;
|
|
4274
4952
|
function print(text) {
|
|
4275
4953
|
process.stdout.write(text.endsWith("\n") ? text : `${text}\n`);
|
|
@@ -4423,10 +5101,20 @@ function printDoors(workspace, files) {
|
|
|
4423
5101
|
for (const door of doors) print(` ${door.kind}: ${door.files.join(", ")}`);
|
|
4424
5102
|
return 0;
|
|
4425
5103
|
}
|
|
4426
|
-
function judge(workspace, dir, pr) {
|
|
4427
|
-
if (!Number.isInteger(pr) || pr <= 0) throw new Error("verdict needs --pr <number>");
|
|
5104
|
+
function judge(workspace, dir, pr, source) {
|
|
4428
5105
|
const state = loadRun(dir);
|
|
4429
|
-
|
|
5106
|
+
for (const output of [
|
|
5107
|
+
"verdict.md",
|
|
5108
|
+
"verdict.json",
|
|
5109
|
+
"plan.json"
|
|
5110
|
+
]) rmSync(join(state.evidence, output), { force: true });
|
|
5111
|
+
if (!Number.isInteger(pr) || pr <= 0) throw new Error("verdict needs --pr <number>");
|
|
5112
|
+
const stored = join(state.evidence, "judgement.json");
|
|
5113
|
+
if (source !== void 0) {
|
|
5114
|
+
readJudgement(source);
|
|
5115
|
+
writeFileSync(stored, readFileSync(source));
|
|
5116
|
+
}
|
|
5117
|
+
const verdict = renderVerdict(workspace, state, pr, readJudgement(stored));
|
|
4430
5118
|
print(verdict.markdown);
|
|
4431
5119
|
return verdict.result === "PASS" || verdict.result === "PASS+NOTES" ? 0 : 1;
|
|
4432
5120
|
}
|
|
@@ -4444,6 +5132,7 @@ async function main(argv) {
|
|
|
4444
5132
|
default: ""
|
|
4445
5133
|
},
|
|
4446
5134
|
pr: { type: "string" },
|
|
5135
|
+
judgement: { type: "string" },
|
|
4447
5136
|
all: { type: "boolean" },
|
|
4448
5137
|
kind: { type: "string" },
|
|
4449
5138
|
plan: { type: "boolean" },
|
|
@@ -4490,8 +5179,8 @@ async function main(argv) {
|
|
|
4490
5179
|
]);
|
|
4491
5180
|
} else if (command === "formal") return runFormal(workspace, rest[0], values.all === true);
|
|
4492
5181
|
else if (command === "doors") return printDoors(workspace, rest[0] ? patchFiles(rest[0]) : planFor(workspace, values.base).changed);
|
|
4493
|
-
else if (command === "clean") for (const line of clean(
|
|
4494
|
-
else if (command === "verdict") return judge(workspace, rest[0], Number(values.pr));
|
|
5182
|
+
else if (command === "clean") for (const line of clean(ownRun(workspace, rest[0]))) print(line);
|
|
5183
|
+
else if (command === "verdict") return judge(workspace, rest[0], Number(values.pr), values.judgement);
|
|
4495
5184
|
else {
|
|
4496
5185
|
print(USAGE);
|
|
4497
5186
|
return 2;
|