@lariocpt/crust-site 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +99 -2
- package/docs/USAGE.md +1128 -0
- package/package.json +33 -4
- package/src/assets.ts +875 -0
- package/src/build-lock.ts +214 -0
- package/src/build.ts +755 -0
- package/src/cli.ts +269 -0
- package/src/config.ts +538 -0
- package/src/content.ts +267 -0
- package/src/dev.ts +821 -0
- package/src/diagnostics.ts +382 -0
- package/src/emit.ts +260 -0
- package/src/fence.ts +593 -0
- package/src/frontmatter.ts +240 -0
- package/src/fs-path.ts +54 -0
- package/src/html.ts +400 -0
- package/src/index.ts +128 -0
- package/src/markdown.ts +444 -0
- package/src/pages.ts +169 -0
- package/src/publication.ts +245 -0
- package/src/render.ts +940 -0
- package/src/routes.ts +363 -0
- package/src/types.ts +250 -0
- package/src/url.ts +273 -0
- package/src/validate.ts +367 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { link, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { BuildError } from "./diagnostics";
|
|
4
|
+
import { isWindowsPath, filesystemPath as path } from "./fs-path";
|
|
5
|
+
|
|
6
|
+
interface Owner {
|
|
7
|
+
readonly pid: number;
|
|
8
|
+
readonly token: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const heldHashes = new Set<string>();
|
|
12
|
+
const MAX_ATTEMPTS = 25;
|
|
13
|
+
const LEGACY_LOCK = /^\.crust-out-lock-([0-9a-f]{16})-(\d+)-([0-9a-f]+)$/;
|
|
14
|
+
const RECLAIM_SLOT = /^\.crust-out-reclaim-[0-9a-f]{16}-[0-9a-f]{24}$/;
|
|
15
|
+
|
|
16
|
+
/** Only ESRCH proves death. Permission/unknown probe failures are not a lease. */
|
|
17
|
+
export function pidAlive(pid: number): boolean {
|
|
18
|
+
try {
|
|
19
|
+
process.kill(pid, 0);
|
|
20
|
+
return true;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return (error as NodeJS.ErrnoException).code !== "ESRCH";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function busy(output: string, pid?: number): BuildError {
|
|
27
|
+
return new BuildError([
|
|
28
|
+
{
|
|
29
|
+
code: "build.busy",
|
|
30
|
+
severity: "error",
|
|
31
|
+
message:
|
|
32
|
+
`another build holds the lock for output ${JSON.stringify(output)} ` +
|
|
33
|
+
(pid === undefined
|
|
34
|
+
? "(owner metadata is unreadable or initializing)"
|
|
35
|
+
: `(pid ${pid} is still alive)`) +
|
|
36
|
+
"; wait for it to finish — dead owners are reclaimed automatically",
|
|
37
|
+
},
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function sameOwner(a: Owner | null | undefined, b: Owner): boolean {
|
|
42
|
+
return a?.pid === b.pid && a.token === b.token;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** null = absent; undefined = not demonstrably a dead, fully initialized owner. */
|
|
46
|
+
async function readOwner(slot: string): Promise<Owner | null | undefined> {
|
|
47
|
+
try {
|
|
48
|
+
const value: unknown = JSON.parse(await readFile(slot, "utf8"));
|
|
49
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
50
|
+
const owner = value as Partial<Owner>;
|
|
51
|
+
if (
|
|
52
|
+
typeof owner.pid !== "number" ||
|
|
53
|
+
!Number.isSafeInteger(owner.pid) ||
|
|
54
|
+
owner.pid <= 0 ||
|
|
55
|
+
typeof owner.token !== "string" ||
|
|
56
|
+
!/^[0-9a-f]{24}$/.test(owner.token)
|
|
57
|
+
) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return owner as Owner;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
return (error as NodeJS.ErrnoException).code === "ENOENT" ? null : undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function reclaimSlot(slot: string, owner: Owner): string {
|
|
67
|
+
// Fixed-length names even if a reclaimer itself dies and needs recovery.
|
|
68
|
+
const identity = isWindowsPath(slot) ? slot.toLowerCase() : slot;
|
|
69
|
+
const key = createHash("sha256").update(identity).digest("hex").slice(0, 16);
|
|
70
|
+
return path.join(path.dirname(slot), `.crust-out-reclaim-${key}-${owner.token}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function releaseSlot(slot: string, owner: Owner): () => Promise<void> {
|
|
74
|
+
let releasing: Promise<void> | undefined;
|
|
75
|
+
return () => {
|
|
76
|
+
releasing ??= (async () => {
|
|
77
|
+
// Live owners are never evicted by this protocol. A successor's token
|
|
78
|
+
// cannot be removed by a delayed/double release from its predecessor.
|
|
79
|
+
if (sameOwner(await readOwner(slot), owner)) await rm(slot, { force: true });
|
|
80
|
+
})();
|
|
81
|
+
return releasing;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function claimSlot(
|
|
86
|
+
slot: string,
|
|
87
|
+
candidate: string,
|
|
88
|
+
owner: Owner,
|
|
89
|
+
output: string,
|
|
90
|
+
depth = 0,
|
|
91
|
+
): Promise<() => Promise<void>> {
|
|
92
|
+
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
|
|
93
|
+
try {
|
|
94
|
+
// link is an atomic create-if-absent of one SHARED name, not a scan +
|
|
95
|
+
// election among private names. Metadata is complete before it appears.
|
|
96
|
+
await link(candidate, slot);
|
|
97
|
+
return releaseSlot(slot, owner);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
100
|
+
}
|
|
101
|
+
const previous = await readOwner(slot);
|
|
102
|
+
if (previous === null) continue;
|
|
103
|
+
if (previous === undefined || pidAlive(previous.pid)) throw busy(output, previous?.pid);
|
|
104
|
+
if (depth >= MAX_ATTEMPTS) break;
|
|
105
|
+
|
|
106
|
+
// Serialize reclaimers of this EXACT dead identity. Without this gate,
|
|
107
|
+
// a delayed check-then-unlink could delete a new, already-held claim.
|
|
108
|
+
// Gates use the same protocol, so a killed reclaimer is recoverable too.
|
|
109
|
+
const releaseGate = await claimSlot(
|
|
110
|
+
reclaimSlot(slot, previous),
|
|
111
|
+
candidate,
|
|
112
|
+
owner,
|
|
113
|
+
output,
|
|
114
|
+
depth + 1,
|
|
115
|
+
);
|
|
116
|
+
try {
|
|
117
|
+
if (sameOwner(await readOwner(slot), previous) && !pidAlive(previous.pid)) {
|
|
118
|
+
await rm(slot, { force: true });
|
|
119
|
+
}
|
|
120
|
+
} finally {
|
|
121
|
+
await releaseGate();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
throw new Error(
|
|
125
|
+
`could not acquire the build lock for ${JSON.stringify(output)} after bounded recovery`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function sweepDeadGates(
|
|
130
|
+
parent: string,
|
|
131
|
+
candidate: string,
|
|
132
|
+
owner: Owner,
|
|
133
|
+
output: string,
|
|
134
|
+
): Promise<void> {
|
|
135
|
+
// A kill after unlinking a slot can detach its gate from the recovery chain.
|
|
136
|
+
// Scan the whole parent: nested gates have opaque keys, and their immediate
|
|
137
|
+
// parent gate may already be gone. Never sweep canonical output slots here.
|
|
138
|
+
for (const name of await readdir(parent)) {
|
|
139
|
+
if (!RECLAIM_SLOT.test(name)) continue;
|
|
140
|
+
const slot = path.join(parent, name);
|
|
141
|
+
const previous = await readOwner(slot);
|
|
142
|
+
if (!previous || pidAlive(previous.pid)) continue;
|
|
143
|
+
let release: () => Promise<void>;
|
|
144
|
+
try {
|
|
145
|
+
// A check-then-unlink could delete a replacement live gate. Claim it
|
|
146
|
+
// through the SAME protocol, including recovery of dead nested gates.
|
|
147
|
+
release = await claimSlot(slot, candidate, owner, output);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
// Another reclaimer may have replaced the dead owner since our read.
|
|
150
|
+
// Its live gate must not block a build for an unrelated output.
|
|
151
|
+
if (error instanceof BuildError && error.diagnostics[0]?.code === "build.busy") continue;
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
await release();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** One claim per output; caller supplies the already-normalized output identity. */
|
|
159
|
+
export async function acquireOutputLock(
|
|
160
|
+
output: string,
|
|
161
|
+
hash: string,
|
|
162
|
+
): Promise<() => Promise<void>> {
|
|
163
|
+
if (heldHashes.has(hash)) throw busy(output, process.pid);
|
|
164
|
+
heldHashes.add(hash);
|
|
165
|
+
const parent = path.dirname(output);
|
|
166
|
+
const owner: Owner = { pid: process.pid, token: randomBytes(12).toString("hex") };
|
|
167
|
+
const candidate = path.join(parent, `.crust-out-owner-${hash}-${owner.pid}-${owner.token}`);
|
|
168
|
+
try {
|
|
169
|
+
await mkdir(parent, { recursive: true });
|
|
170
|
+
// Respect older on-disk live claims and reclaim their unique dead files.
|
|
171
|
+
for (const name of await readdir(parent)) {
|
|
172
|
+
const match = LEGACY_LOCK.exec(name);
|
|
173
|
+
if (match?.[1] !== hash) continue;
|
|
174
|
+
const pid = Number(match[2]);
|
|
175
|
+
if (pidAlive(pid)) throw busy(output, pid);
|
|
176
|
+
await rm(path.join(parent, name), { force: true });
|
|
177
|
+
}
|
|
178
|
+
await writeFile(candidate, JSON.stringify(owner), { flag: "wx" });
|
|
179
|
+
const release = await claimSlot(
|
|
180
|
+
path.join(parent, `.crust-out-lock-${hash}`),
|
|
181
|
+
candidate,
|
|
182
|
+
owner,
|
|
183
|
+
output,
|
|
184
|
+
);
|
|
185
|
+
try {
|
|
186
|
+
await sweepDeadGates(parent, candidate, owner, output);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
await release();
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
let releasing: Promise<void> | undefined;
|
|
192
|
+
return () => {
|
|
193
|
+
releasing ??= (async () => {
|
|
194
|
+
try {
|
|
195
|
+
await release();
|
|
196
|
+
} finally {
|
|
197
|
+
try {
|
|
198
|
+
await rm(candidate, { force: true });
|
|
199
|
+
} finally {
|
|
200
|
+
heldHashes.delete(hash);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
})();
|
|
204
|
+
return releasing;
|
|
205
|
+
};
|
|
206
|
+
} catch (error) {
|
|
207
|
+
try {
|
|
208
|
+
await rm(candidate, { force: true });
|
|
209
|
+
} finally {
|
|
210
|
+
heldHashes.delete(hash);
|
|
211
|
+
}
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
}
|