@antelopejs/tooling-configs 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -1
- package/dist/api-CCOiLN3w.mjs +185 -0
- package/dist/bin/checks.mjs +253 -27
- package/dist/bin/garden.mjs +5 -161
- package/dist/bin/lease.mjs +3 -279
- package/dist/bin/sync.mjs +4 -4
- package/dist/bin/triage.d.mts +1 -0
- package/dist/bin/triage.mjs +108 -0
- package/dist/bin/verify.mjs +155 -11
- package/dist/ports-DGP6bPJy.mjs +282 -0
- package/package.json +2 -1
package/dist/bin/verify.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { a as VERIFY_KINDS, c as isVerifyKind, l as skillFolder } from "../shared-igPpEDpS.mjs";
|
|
3
|
-
import { a as isString, f as parseJson, i as isObject, u as optStr } from "../json-CG2L7YFl.mjs";
|
|
3
|
+
import { a as isString, c as object, f as parseJson, i as isObject, n as children, p as str, s as num, u as optStr } from "../json-CG2L7YFl.mjs";
|
|
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 { appendFileSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { appendFileSync, chmodSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, rmdirSync, statSync, symlinkSync, utimesSync, writeFileSync } from "node:fs";
|
|
8
8
|
import { basename, dirname, extname, join, matchesGlob, posix, relative, resolve, sep } from "node:path";
|
|
9
9
|
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
10
10
|
import { pathToFileURL } from "node:url";
|
|
11
11
|
import ts from "typescript";
|
|
12
12
|
import { randomBytes } from "node:crypto";
|
|
13
|
-
import { homedir, tmpdir } from "node:os";
|
|
13
|
+
import { constants as constants$1, homedir, tmpdir } from "node:os";
|
|
14
14
|
import { parseArgs } from "node:util";
|
|
15
15
|
import { createServer } from "node:net";
|
|
16
16
|
//#region src/verify/doors.ts
|
|
@@ -338,6 +338,27 @@ function emitFingerprint(repo, sha, files) {
|
|
|
338
338
|
* published packages. Nothing a journey asserts on lives here.
|
|
339
339
|
*/
|
|
340
340
|
const CACHE_DIR = join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "antelopejs-verify");
|
|
341
|
+
/** The folder that holds every run, one subfolder per repository. */
|
|
342
|
+
const RUNS_DIR = join(tmpdir(), "antelopejs-verify");
|
|
343
|
+
function makeWritable(folder) {
|
|
344
|
+
chmodSync(folder, lstatSync(folder).mode | 448);
|
|
345
|
+
for (const entry of readdirSync(folder, { withFileTypes: true })) if (entry.isDirectory()) makeWritable(join(folder, entry.name));
|
|
346
|
+
}
|
|
347
|
+
/** Deletes a folder this run wrote, including folders an install left read-only. */
|
|
348
|
+
function removeTree(folder) {
|
|
349
|
+
try {
|
|
350
|
+
rmSync(folder, {
|
|
351
|
+
recursive: true,
|
|
352
|
+
force: true
|
|
353
|
+
});
|
|
354
|
+
} catch {
|
|
355
|
+
makeWritable(folder);
|
|
356
|
+
rmSync(folder, {
|
|
357
|
+
recursive: true,
|
|
358
|
+
force: true
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
}
|
|
341
362
|
/** Runs git in `cwd` and returns its trimmed output. */
|
|
342
363
|
function git(cwd, args) {
|
|
343
364
|
return execFileSync("git", args, {
|
|
@@ -375,10 +396,12 @@ async function openWorkspace(root, kind) {
|
|
|
375
396
|
function stateFile(dir) {
|
|
376
397
|
return join(dir, "run.json");
|
|
377
398
|
}
|
|
378
|
-
/** Reads a run created by `new`, failing with the reason when it is missing. */
|
|
399
|
+
/** Reads a run created by `new`, failing with the reason when it is missing, and marks it active by touching run.json. */
|
|
379
400
|
function loadRun(dir) {
|
|
380
401
|
const file = stateFile(resolve(dir));
|
|
381
402
|
if (!existsSync(file)) throw new Error(`No run at ${dir}: run.json is missing. Create one with \`new\`.`);
|
|
403
|
+
const now = /* @__PURE__ */ new Date();
|
|
404
|
+
utimesSync(file, now, now);
|
|
382
405
|
return JSON.parse(readFileSync(file, "utf8"));
|
|
383
406
|
}
|
|
384
407
|
/** Persists the run state. */
|
|
@@ -388,7 +411,7 @@ function saveRun(state) {
|
|
|
388
411
|
/** Allocates the folders of a new run under the system temp folder. */
|
|
389
412
|
function allocateRun(workspace, baseRef, title) {
|
|
390
413
|
const id = `${(/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").slice(0, 15)}-${randomBytes(3).toString("hex")}`;
|
|
391
|
-
const dir = join(
|
|
414
|
+
const dir = join(RUNS_DIR, workspace.name, id);
|
|
392
415
|
const evidence = join(dir, "evidence");
|
|
393
416
|
mkdirSync(evidence, { recursive: true });
|
|
394
417
|
return {
|
|
@@ -1858,26 +1881,65 @@ function writeCommand(folder, spec) {
|
|
|
1858
1881
|
mkdirSync(folder, { recursive: true });
|
|
1859
1882
|
writeFileSync(join(folder, "command.txt"), `cd ${shellQuote(spec.cwd)}\n${describe(spec)}\n`);
|
|
1860
1883
|
}
|
|
1884
|
+
const foreground = /* @__PURE__ */ new Set();
|
|
1885
|
+
const releases = /* @__PURE__ */ new Set();
|
|
1886
|
+
const INTERRUPTS = [
|
|
1887
|
+
"SIGINT",
|
|
1888
|
+
"SIGTERM",
|
|
1889
|
+
"SIGHUP",
|
|
1890
|
+
"SIGQUIT"
|
|
1891
|
+
];
|
|
1892
|
+
let listening = false;
|
|
1893
|
+
/** On the first interrupt, kills the process group of every foreground command, runs every registered release (one that throws is reported and the others still run), and exits with the shell's code for the signal. */
|
|
1894
|
+
function listenForInterrupts() {
|
|
1895
|
+
if (listening) return;
|
|
1896
|
+
listening = true;
|
|
1897
|
+
for (const signal of INTERRUPTS) process.once(signal, () => {
|
|
1898
|
+
for (const child of foreground) if (child.pid !== void 0) killOwnGroup(child.pid);
|
|
1899
|
+
for (const release of releases) try {
|
|
1900
|
+
release();
|
|
1901
|
+
} catch (error) {
|
|
1902
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
1903
|
+
}
|
|
1904
|
+
process.exit(128 + constants$1.signals[signal]);
|
|
1905
|
+
});
|
|
1906
|
+
}
|
|
1907
|
+
/**
|
|
1908
|
+
* Until the returned function is called, an interrupt calls `release` after
|
|
1909
|
+
* killing the process group of every command still running in the
|
|
1910
|
+
* foreground, the processes it started included.
|
|
1911
|
+
*/
|
|
1912
|
+
function onInterrupt(release) {
|
|
1913
|
+
listenForInterrupts();
|
|
1914
|
+
releases.add(release);
|
|
1915
|
+
return () => releases.delete(release);
|
|
1916
|
+
}
|
|
1861
1917
|
/** Runs a command to completion and writes command.txt, stdout.txt, stderr.txt and exit-code.txt into `folder`. */
|
|
1862
1918
|
function runRecorded(folder, spec) {
|
|
1863
1919
|
writeCommand(folder, spec);
|
|
1920
|
+
listenForInterrupts();
|
|
1864
1921
|
return new Promise((resolve) => {
|
|
1865
1922
|
const child = spawn(spec.program, spec.args, {
|
|
1866
1923
|
cwd: spec.cwd,
|
|
1867
1924
|
env: spec.env,
|
|
1925
|
+
detached: true,
|
|
1868
1926
|
stdio: [
|
|
1869
1927
|
"ignore",
|
|
1870
1928
|
"pipe",
|
|
1871
1929
|
"pipe"
|
|
1872
1930
|
]
|
|
1873
1931
|
});
|
|
1932
|
+
foreground.add(child);
|
|
1874
1933
|
const out = [];
|
|
1875
1934
|
const err = [];
|
|
1876
1935
|
child.stdout.on("data", (chunk) => out.push(chunk));
|
|
1877
1936
|
child.stderr.on("data", (chunk) => err.push(chunk));
|
|
1878
|
-
const timer = setTimeout(() =>
|
|
1937
|
+
const timer = setTimeout(() => {
|
|
1938
|
+
if (child.pid !== void 0) killOwnGroup(child.pid);
|
|
1939
|
+
}, spec.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
1879
1940
|
const finish = (code, signal, failure = "") => {
|
|
1880
1941
|
clearTimeout(timer);
|
|
1942
|
+
foreground.delete(child);
|
|
1881
1943
|
const result = {
|
|
1882
1944
|
folder,
|
|
1883
1945
|
command: describe(spec),
|
|
@@ -1930,6 +1992,14 @@ function startRecorded(folder, spec) {
|
|
|
1930
1992
|
exited
|
|
1931
1993
|
};
|
|
1932
1994
|
}
|
|
1995
|
+
/** Kills the process group a detached command leads, never a lone PID the system may have reused since the command exited. */
|
|
1996
|
+
function killOwnGroup(pid) {
|
|
1997
|
+
try {
|
|
1998
|
+
process.kill(-pid, "SIGKILL");
|
|
1999
|
+
} catch {
|
|
2000
|
+
return;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
1933
2003
|
/** Kills the process group of `pid`, or `pid` alone, ignoring a process already gone. */
|
|
1934
2004
|
function killGroup(pid) {
|
|
1935
2005
|
try {
|
|
@@ -2192,6 +2262,56 @@ async function packInterface(workspace, state) {
|
|
|
2192
2262
|
linkDependencies(root, workspace.root);
|
|
2193
2263
|
return root;
|
|
2194
2264
|
}
|
|
2265
|
+
/** How long a run may sit untouched before the next `new` deletes it. */
|
|
2266
|
+
const RUN_TTL_MS = 6048e5;
|
|
2267
|
+
function lastActivity(dir) {
|
|
2268
|
+
const file = join(dir, "run.json");
|
|
2269
|
+
return statSync(existsSync(file) ? file : dir).mtimeMs;
|
|
2270
|
+
}
|
|
2271
|
+
/** The leftovers a run recorded; none when its run.json is missing or unreadable. */
|
|
2272
|
+
function recorded(dir) {
|
|
2273
|
+
try {
|
|
2274
|
+
const state = object(parseJson(readFileSync(join(dir, "run.json"), "utf8")), "run.json");
|
|
2275
|
+
return {
|
|
2276
|
+
pids: children(state, "processes").map((entry) => num(entry, "pid")),
|
|
2277
|
+
containers: children(state, "containers").map((entry) => str(entry, "name"))
|
|
2278
|
+
};
|
|
2279
|
+
} catch {
|
|
2280
|
+
return {
|
|
2281
|
+
pids: [],
|
|
2282
|
+
containers: []
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
function entries(folder) {
|
|
2287
|
+
try {
|
|
2288
|
+
return readdirSync(folder, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(folder, entry.name));
|
|
2289
|
+
} catch {
|
|
2290
|
+
return [];
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
function pruneRun(dir, now) {
|
|
2294
|
+
try {
|
|
2295
|
+
if (now - lastActivity(dir) < RUN_TTL_MS) return;
|
|
2296
|
+
const { pids, containers } = recorded(dir);
|
|
2297
|
+
if (pids.some(alive)) return;
|
|
2298
|
+
for (const name of containers) removeContainer(name);
|
|
2299
|
+
removeTree(dir);
|
|
2300
|
+
} catch (error) {
|
|
2301
|
+
if (existsSync(dir)) process.stderr.write(`could not prune ${dir}: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
/**
|
|
2305
|
+
* Deletes every run under the runs folder, of any repository, that nothing
|
|
2306
|
+
* touched for seven days and whose recorded processes are all gone, with the
|
|
2307
|
+
* containers it recorded. A run a command loaded since then, or one with a
|
|
2308
|
+
* live process, is still in progress and stays. A run it cannot delete is
|
|
2309
|
+
* reported on stderr and never stops the new run.
|
|
2310
|
+
*/
|
|
2311
|
+
function pruneRuns() {
|
|
2312
|
+
const now = Date.now();
|
|
2313
|
+
for (const repo of entries(RUNS_DIR)) for (const dir of entries(repo)) pruneRun(dir, now);
|
|
2314
|
+
}
|
|
2195
2315
|
/**
|
|
2196
2316
|
* Creates an isolated run: runs the setup (the build by default), then
|
|
2197
2317
|
* prepares what the kind drives. An interface is packed as npm would publish
|
|
@@ -2199,6 +2319,7 @@ async function packInterface(workspace, state) {
|
|
|
2199
2319
|
* kind's backing `services` start in Docker on free ports.
|
|
2200
2320
|
*/
|
|
2201
2321
|
async function createRun(workspace, baseRef, title) {
|
|
2322
|
+
pruneRuns();
|
|
2202
2323
|
const state = allocateRun(workspace, baseRef, title);
|
|
2203
2324
|
saveRun(state);
|
|
2204
2325
|
await runSetup(workspace, state);
|
|
@@ -2303,10 +2424,7 @@ function clean(state) {
|
|
|
2303
2424
|
saveRun(state);
|
|
2304
2425
|
for (const entry of readdirSync(state.dir)) {
|
|
2305
2426
|
if (entry === "evidence" || entry === "run.json") continue;
|
|
2306
|
-
|
|
2307
|
-
recursive: true,
|
|
2308
|
-
force: true
|
|
2309
|
-
});
|
|
2427
|
+
removeTree(join(state.dir, entry));
|
|
2310
2428
|
}
|
|
2311
2429
|
return lines;
|
|
2312
2430
|
}
|
|
@@ -2324,7 +2442,7 @@ const VERIFICATION_LAYER = [
|
|
|
2324
2442
|
".claude/**",
|
|
2325
2443
|
".github/**"
|
|
2326
2444
|
];
|
|
2327
|
-
/** Tests are user-facing in a template,
|
|
2445
|
+
/** Tests are user-facing in a template, since `ajs` copies its whole tree except `.agents/` and `.claude/skills/`. */
|
|
2328
2446
|
const NOT_USER_FACING = {
|
|
2329
2447
|
interface: [
|
|
2330
2448
|
"**/*.test.ts",
|
|
@@ -3103,8 +3221,34 @@ function statusOf(context, failure) {
|
|
|
3103
3221
|
if (failure.notVerified !== null) return "NOT VERIFIED";
|
|
3104
3222
|
return failure.error !== null || context.steps.some((step) => step.checks.some((check) => !check.pass)) || context.steps.length === 0 ? "FAIL" : "PASS";
|
|
3105
3223
|
}
|
|
3224
|
+
/** Deletes a journey's scratch folder and HOME, and their parents once empty; the evidence stays. A folder it cannot delete is reported on stderr and left for the next prune. */
|
|
3225
|
+
function discard(context) {
|
|
3226
|
+
for (const folder of [context.work, context.home]) try {
|
|
3227
|
+
removeTree(folder);
|
|
3228
|
+
for (let parent = dirname(folder); parent !== context.state.dir && readdirSync(parent).length === 0; parent = dirname(parent)) rmdirSync(parent);
|
|
3229
|
+
} catch (error) {
|
|
3230
|
+
process.stderr.write(`could not delete ${folder}: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
3231
|
+
}
|
|
3232
|
+
}
|
|
3106
3233
|
async function runOne(workspace, state, feature, id, journey) {
|
|
3107
3234
|
const context = new VerifyContext(workspace, state, feature, id);
|
|
3235
|
+
const others = new Set(state.processes.map((record) => record.pid));
|
|
3236
|
+
const release = onInterrupt(() => {
|
|
3237
|
+
const own = state.processes.filter((record) => !others.has(record.pid));
|
|
3238
|
+
for (const record of own) killGroup(record.pid);
|
|
3239
|
+
discard(context);
|
|
3240
|
+
state.processes = state.processes.filter((record) => others.has(record.pid));
|
|
3241
|
+
saveRun(state);
|
|
3242
|
+
});
|
|
3243
|
+
try {
|
|
3244
|
+
return await driveJourney(context, feature, id, journey);
|
|
3245
|
+
} finally {
|
|
3246
|
+
release();
|
|
3247
|
+
discard(context);
|
|
3248
|
+
}
|
|
3249
|
+
}
|
|
3250
|
+
async function driveJourney(context, feature, id, journey) {
|
|
3251
|
+
const { state } = context;
|
|
3108
3252
|
let failure = {
|
|
3109
3253
|
error: null,
|
|
3110
3254
|
notVerified: null
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { c as object, f as parseJson, i as isObject, l as optNum, p as str, s as num, t as child } from "./json-CG2L7YFl.mjs";
|
|
2
|
+
import { readFileSync, readdirSync, readlinkSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, join } from "node:path";
|
|
4
|
+
import { execFileSync } from "node:child_process";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { connect, createServer } from "node:net";
|
|
8
|
+
//#region src/lease/protocol.ts
|
|
9
|
+
/**
|
|
10
|
+
* The port every antelopejs-lease version listens on, on 127.0.0.1, while it holds the registry lock.
|
|
11
|
+
* Each repository pins its own tooling-configs version, so this port, {@link leaseHome} and the v1
|
|
12
|
+
* record are a protocol between versions: they may be extended, never changed.
|
|
13
|
+
*/
|
|
14
|
+
const LOCK_PORT = 2e4;
|
|
15
|
+
const RECORD_VERSION = 1;
|
|
16
|
+
/** The offset of each role's port in its block of ten. Offsets 6 to 9 stay spare for later roles. */
|
|
17
|
+
const PORT_ROLES = {
|
|
18
|
+
api: 0,
|
|
19
|
+
web: 1,
|
|
20
|
+
mongo: 2,
|
|
21
|
+
smtp: 3,
|
|
22
|
+
inspect: 4,
|
|
23
|
+
hmr: 5
|
|
24
|
+
};
|
|
25
|
+
function isRole(value) {
|
|
26
|
+
return Object.hasOwn(PORT_ROLES, value);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `ANTELOPEJS_LEASE_HOME`, else `~/.antelopejs/leases`: one registry for every repository of the
|
|
30
|
+
* user. A relative `ANTELOPEJS_LEASE_HOME` throws, since each working directory would resolve it to
|
|
31
|
+
* another registry.
|
|
32
|
+
*/
|
|
33
|
+
function leaseHome(env) {
|
|
34
|
+
const home = env.ANTELOPEJS_LEASE_HOME;
|
|
35
|
+
if (!home) return join(homedir(), ".antelopejs", "leases");
|
|
36
|
+
if (!isAbsolute(home)) throw new Error(`antelopejs-lease: ANTELOPEJS_LEASE_HOME must be an absolute path, not ${home}`);
|
|
37
|
+
return home;
|
|
38
|
+
}
|
|
39
|
+
function isLeaseId(value) {
|
|
40
|
+
return /^[0-9a-f]{10}$/u.test(value);
|
|
41
|
+
}
|
|
42
|
+
/** The lease id of the worktree whose real path is `worktree`. */
|
|
43
|
+
function leaseId(worktree) {
|
|
44
|
+
const id = createHash("sha256").update(worktree).digest("hex").slice(0, 10);
|
|
45
|
+
if (!isLeaseId(id)) throw new Error(`unexpected digest ${id}`);
|
|
46
|
+
return id;
|
|
47
|
+
}
|
|
48
|
+
/** Builds a value for every role, so a new role is a compile error until each table names it. */
|
|
49
|
+
function eachRole(value) {
|
|
50
|
+
return {
|
|
51
|
+
api: value("api"),
|
|
52
|
+
web: value("web"),
|
|
53
|
+
mongo: value("mongo"),
|
|
54
|
+
smtp: value("smtp"),
|
|
55
|
+
inspect: value("inspect"),
|
|
56
|
+
hmr: value("hmr")
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function isMongoMode(value) {
|
|
60
|
+
return value === "owned" || value === "external";
|
|
61
|
+
}
|
|
62
|
+
function decodeRecord(value) {
|
|
63
|
+
const source = object(value, "lease.json");
|
|
64
|
+
if (num(source, "version") !== RECORD_VERSION) return void 0;
|
|
65
|
+
const id = str(source, "id");
|
|
66
|
+
const mongo = str(source, "mongo");
|
|
67
|
+
if (!isLeaseId(id) || !isMongoMode(mongo)) return void 0;
|
|
68
|
+
const ports = child(source, "ports");
|
|
69
|
+
return {
|
|
70
|
+
version: RECORD_VERSION,
|
|
71
|
+
id,
|
|
72
|
+
worktree: str(source, "worktree"),
|
|
73
|
+
gitInode: num(source, "gitInode"),
|
|
74
|
+
gitBirth: num(source, "gitBirth"),
|
|
75
|
+
ports: eachRole((role) => num(ports, role)),
|
|
76
|
+
createdAt: str(source, "createdAt"),
|
|
77
|
+
mongo
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function reservedPorts(value) {
|
|
81
|
+
if (!isObject(value) || !Object.hasOwn(value, "ports")) return [];
|
|
82
|
+
const ports = value.ports;
|
|
83
|
+
if (!isObject(ports)) return [];
|
|
84
|
+
return Object.keys(ports).map((key) => optNum(ports, key)).filter((port) => port !== void 0);
|
|
85
|
+
}
|
|
86
|
+
function tryJson(text) {
|
|
87
|
+
try {
|
|
88
|
+
return parseJson(text);
|
|
89
|
+
} catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function tryRecord(value) {
|
|
94
|
+
try {
|
|
95
|
+
return decodeRecord(value);
|
|
96
|
+
} catch {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Reads a `lease.json` leniently: unknown fields are ignored, anything unreadable is `foreign`. */
|
|
101
|
+
function parseRecord(text) {
|
|
102
|
+
const value = tryJson(text);
|
|
103
|
+
if (value === void 0) return {
|
|
104
|
+
kind: "foreign",
|
|
105
|
+
reserved: []
|
|
106
|
+
};
|
|
107
|
+
const record = tryRecord(value);
|
|
108
|
+
if (record !== void 0) return {
|
|
109
|
+
kind: "current",
|
|
110
|
+
record
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
kind: "foreign",
|
|
114
|
+
reserved: reservedPorts(value)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/lease/ports.ts
|
|
119
|
+
const BAND_START = 20010;
|
|
120
|
+
const BLOCK_SIZE = 10;
|
|
121
|
+
const BLOCK_COUNT = 9990 / BLOCK_SIZE;
|
|
122
|
+
const DENIED_PORTS = [
|
|
123
|
+
24678,
|
|
124
|
+
27017,
|
|
125
|
+
27018,
|
|
126
|
+
27019
|
|
127
|
+
];
|
|
128
|
+
const CONNECT_TIMEOUT_MS = 1e3;
|
|
129
|
+
const LISTEN_STATE = "0A";
|
|
130
|
+
/**
|
|
131
|
+
* The block bases a lease tries, in order: from a block picked by a hash of the id and `uid`
|
|
132
|
+
* together, forward and around the band. Each user keeps a registry of its own, so mixing in the
|
|
133
|
+
* uid usually starts two users' worktrees at one path on different blocks. The band [20010, 30000)
|
|
134
|
+
* sits below the kernel's ephemeral range (32768 and up on Linux, 49152 and up on macOS), so no
|
|
135
|
+
* `bind(:0)` caller races for these ports. Blocks holding the Vite HMR default (24678) or a system
|
|
136
|
+
* mongod (27017 to 27019) are skipped.
|
|
137
|
+
*/
|
|
138
|
+
function candidateBases(id, uid = process.getuid?.() ?? 0) {
|
|
139
|
+
const first = createHash("sha256").update(`${uid}:${id}`).digest().readUInt32BE(0) % BLOCK_COUNT;
|
|
140
|
+
return Array.from({ length: BLOCK_COUNT }, (_, step) => BAND_START + (first + step) % BLOCK_COUNT * BLOCK_SIZE).filter((base) => !DENIED_PORTS.some((port) => port >= base && port < base + BLOCK_SIZE));
|
|
141
|
+
}
|
|
142
|
+
function blockPorts(base) {
|
|
143
|
+
return Array.from({ length: BLOCK_SIZE }, (_, offset) => base + offset);
|
|
144
|
+
}
|
|
145
|
+
async function everyBindable(ports, bindable) {
|
|
146
|
+
for (const port of ports) if (!await bindable(port)) return false;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The first candidate block none of whose ten ports is in `taken` and all of whose ports bind.
|
|
151
|
+
* Throws when the band has no such block.
|
|
152
|
+
*/
|
|
153
|
+
async function chooseBlock(id, taken, bindable) {
|
|
154
|
+
for (const base of candidateBases(id)) {
|
|
155
|
+
const ports = blockPorts(base);
|
|
156
|
+
if (ports.some((port) => taken.has(port))) continue;
|
|
157
|
+
if (await everyBindable(ports, bindable)) return eachRole((role) => base + PORT_ROLES[role]);
|
|
158
|
+
}
|
|
159
|
+
throw new Error(`antelopejs-lease: no free port block in ${BAND_START}-29999; run antelopejs-lease gc, then retry`);
|
|
160
|
+
}
|
|
161
|
+
/** Listens on `host:port`, Node's dual-stack default without `host`. `undefined` when the address is in use. */
|
|
162
|
+
function claimPort(port, host) {
|
|
163
|
+
return new Promise((resolve, reject) => {
|
|
164
|
+
const server = createServer();
|
|
165
|
+
server.once("error", (error) => {
|
|
166
|
+
if (error.code === "EADDRINUSE") resolve(void 0);
|
|
167
|
+
else reject(error);
|
|
168
|
+
});
|
|
169
|
+
server.listen({
|
|
170
|
+
port,
|
|
171
|
+
host
|
|
172
|
+
}, () => resolve(server));
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function closeServer(server) {
|
|
176
|
+
return new Promise((resolve) => server.close(() => resolve()));
|
|
177
|
+
}
|
|
178
|
+
async function bindsOn(port, host) {
|
|
179
|
+
try {
|
|
180
|
+
const server = await claimPort(port, host);
|
|
181
|
+
if (server === void 0) return false;
|
|
182
|
+
await closeServer(server);
|
|
183
|
+
return true;
|
|
184
|
+
} catch {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* True when nothing holds `port`. The dual-stack wildcard bind fails on Linux for a listener on any
|
|
190
|
+
* local address; the 127.0.0.1 bind covers systems that let a wildcard sit beside a specific address.
|
|
191
|
+
*/
|
|
192
|
+
async function bindable(port) {
|
|
193
|
+
return await bindsOn(port, "127.0.0.1") && bindsOn(port);
|
|
194
|
+
}
|
|
195
|
+
function acceptsOn(port, host) {
|
|
196
|
+
return new Promise((resolve) => {
|
|
197
|
+
const socket = connect({
|
|
198
|
+
port,
|
|
199
|
+
host,
|
|
200
|
+
timeout: CONNECT_TIMEOUT_MS
|
|
201
|
+
});
|
|
202
|
+
const settle = (answer) => {
|
|
203
|
+
socket.destroy();
|
|
204
|
+
resolve(answer);
|
|
205
|
+
};
|
|
206
|
+
socket.once("connect", () => settle(true));
|
|
207
|
+
socket.once("timeout", () => settle(false));
|
|
208
|
+
socket.once("error", () => settle(false));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/** True when a server accepts connections on `port` on the IPv4 or the IPv6 loopback. */
|
|
212
|
+
async function accepting(port) {
|
|
213
|
+
return await acceptsOn(port, "127.0.0.1") || acceptsOn(port, "::1");
|
|
214
|
+
}
|
|
215
|
+
function listenInodes(table, port) {
|
|
216
|
+
let text;
|
|
217
|
+
try {
|
|
218
|
+
text = readFileSync(table, "utf8");
|
|
219
|
+
} catch {
|
|
220
|
+
return [];
|
|
221
|
+
}
|
|
222
|
+
return text.split("\n").slice(1).map((line) => line.trim().split(/\s+/u)).filter((columns) => columns[3] === LISTEN_STATE && Number.parseInt(columns[1]?.split(":")[1] ?? "", 16) === port).map((columns) => `socket:[${columns[9]}]`);
|
|
223
|
+
}
|
|
224
|
+
function holdsSocket(pid, sockets) {
|
|
225
|
+
let fds;
|
|
226
|
+
try {
|
|
227
|
+
fds = readdirSync(`/proc/${pid}/fd`);
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
return fds.some((fd) => {
|
|
232
|
+
try {
|
|
233
|
+
return sockets.has(readlinkSync(`/proc/${pid}/fd/${fd}`));
|
|
234
|
+
} catch {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function listenSockets(port) {
|
|
240
|
+
return /* @__PURE__ */ new Set([...listenInodes("/proc/net/tcp", port), ...listenInodes("/proc/net/tcp6", port)]);
|
|
241
|
+
}
|
|
242
|
+
function linuxListenerPids(port) {
|
|
243
|
+
const sockets = listenSockets(port);
|
|
244
|
+
if (sockets.size === 0) return [];
|
|
245
|
+
return readdirSync("/proc").filter((name) => /^\d+$/u.test(name) && holdsSocket(name, sockets)).map(Number);
|
|
246
|
+
}
|
|
247
|
+
function lsofListenerPids(port) {
|
|
248
|
+
try {
|
|
249
|
+
return execFileSync("lsof", [
|
|
250
|
+
"-nP",
|
|
251
|
+
"-t",
|
|
252
|
+
`-iTCP:${port}`,
|
|
253
|
+
"-sTCP:LISTEN"
|
|
254
|
+
], {
|
|
255
|
+
encoding: "utf8",
|
|
256
|
+
stdio: [
|
|
257
|
+
"ignore",
|
|
258
|
+
"pipe",
|
|
259
|
+
"ignore"
|
|
260
|
+
]
|
|
261
|
+
}).split("\n").filter((line) => /^\d+$/u.test(line)).map(Number);
|
|
262
|
+
} catch {
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** The pids listening on `port` that this user may inspect; empty when none can be read. */
|
|
267
|
+
function listenerPids(port) {
|
|
268
|
+
if (process.platform === "linux") return linuxListenerPids(port);
|
|
269
|
+
if (process.platform === "darwin") return lsofListenerPids(port);
|
|
270
|
+
return [];
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* True when process `pid` listens on `port`. Where the platform gives no way to tell, true when
|
|
274
|
+
* anything accepts on `port`.
|
|
275
|
+
*/
|
|
276
|
+
async function listensOn(pid, port) {
|
|
277
|
+
if (process.platform === "linux") return holdsSocket(String(pid), listenSockets(port));
|
|
278
|
+
if (process.platform === "darwin") return lsofListenerPids(port).includes(pid);
|
|
279
|
+
return accepting(port);
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
export { closeServer as a, LOCK_PORT as c, isRole as d, leaseHome as f, claimPort as i, PORT_ROLES as l, parseRecord as m, bindable as n, listenerPids as o, leaseId as p, chooseBlock as r, listensOn as s, accepting as t, isLeaseId as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/tooling-configs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Shared tooling presets for AntelopeJS repositories: oxlint (with the vendored anti-slop rules), oxfmt, ESLint for Nuxt layers, Knip, and an interface range check",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"antelopejs",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"antelopejs-ratchet": "./dist/bin/ratchet.mjs",
|
|
34
34
|
"antelopejs-suppressions": "./dist/bin/suppressions.mjs",
|
|
35
35
|
"antelopejs-sync": "./dist/bin/sync.mjs",
|
|
36
|
+
"antelopejs-triage": "./dist/bin/triage.mjs",
|
|
36
37
|
"antelopejs-verify": "./dist/bin/verify.mjs"
|
|
37
38
|
},
|
|
38
39
|
"files": [
|