@foldrun/core 17.2.0 → 17.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/approvals.js +3 -3
- package/dist/src/blob-cache.d.ts +58 -0
- package/dist/src/blob-cache.js +309 -0
- package/dist/src/blob-pins.d.ts +44 -0
- package/dist/src/blob-pins.js +61 -0
- package/dist/src/blob-store.d.ts +47 -0
- package/dist/src/blob-store.js +357 -0
- package/dist/src/blob-sweeper.d.ts +70 -0
- package/dist/src/blob-sweeper.js +254 -0
- package/dist/src/blob-types.d.ts +145 -0
- package/dist/src/blob-types.js +115 -0
- package/dist/src/browse-egress.d.ts +31 -0
- package/dist/src/browse-egress.js +43 -0
- package/dist/src/egress.d.ts +9 -0
- package/dist/src/evals.js +1 -1
- package/dist/src/history.d.ts +86 -9
- package/dist/src/history.js +132 -16
- package/dist/src/journal-store.d.ts +58 -0
- package/dist/src/journal-store.js +117 -0
- package/dist/src/manifest.d.ts +92 -0
- package/dist/src/manifest.js +239 -0
- package/dist/src/ops-log.js +26 -3
- package/dist/src/platform.d.ts +7 -0
- package/dist/src/platform.js +1 -0
- package/dist/src/publish.d.ts +134 -0
- package/dist/src/publish.js +434 -0
- package/dist/src/records.d.ts +72 -0
- package/dist/src/records.js +24 -0
- package/dist/src/revision-store.d.ts +161 -0
- package/dist/src/revision-store.js +378 -0
- package/dist/src/run-container.d.ts +20 -0
- package/dist/src/run-container.js +61 -0
- package/dist/src/runner.js +22 -4
- package/dist/src/s3-blob-store.d.ts +29 -0
- package/dist/src/s3-blob-store.js +458 -0
- package/dist/src/state-retention.d.ts +15 -0
- package/dist/src/state-retention.js +65 -0
- package/dist/src/storage.d.ts +29 -1
- package/dist/src/storage.js +331 -81
- package/dist/src/store.d.ts +41 -0
- package/dist/src/store.js +188 -5
- package/dist/src/trigger-log.js +17 -0
- package/dist/src/workspace-revisions.d.ts +259 -0
- package/dist/src/workspace-revisions.js +1274 -0
- package/package.json +1 -1
- package/src/approvals.ts +3 -3
- package/src/blob-cache.ts +354 -0
- package/src/blob-pins.ts +99 -0
- package/src/blob-store.ts +385 -0
- package/src/blob-sweeper.ts +308 -0
- package/src/blob-types.ts +216 -0
- package/src/browse-egress.ts +49 -1
- package/src/egress.ts +6 -0
- package/src/evals.ts +1 -1
- package/src/history.ts +182 -16
- package/src/journal-store.ts +152 -0
- package/src/manifest.ts +274 -0
- package/src/ops-log.ts +25 -3
- package/src/platform.ts +8 -0
- package/src/publish.ts +520 -0
- package/src/records.ts +105 -0
- package/src/revision-store.ts +513 -0
- package/src/run-container.ts +62 -0
- package/src/runner.ts +21 -4
- package/src/s3-blob-store.ts +489 -0
- package/src/state-retention.ts +83 -0
- package/src/storage.ts +335 -81
- package/src/store.ts +178 -5
- package/src/trigger-log.ts +15 -0
- package/src/workspace-revisions.ts +1324 -0
package/dist/src/history.js
CHANGED
|
@@ -42,6 +42,42 @@ export function registerTreeReader(fn) {
|
|
|
42
42
|
const prev = readTree;
|
|
43
43
|
readTree = (tenant, scope) => fn(tenant, scope) ?? prev(tenant, scope);
|
|
44
44
|
}
|
|
45
|
+
/** The scope's tree as the registered readers see it (text files only), or null. */
|
|
46
|
+
export function currentTree(tenant, scope) {
|
|
47
|
+
return readTree(tenant, scope);
|
|
48
|
+
}
|
|
49
|
+
let journal = null;
|
|
50
|
+
export function registerJournal(fn) {
|
|
51
|
+
journal = fn;
|
|
52
|
+
}
|
|
53
|
+
/** A workspace was deleted: whatever a registered journal still holds for it
|
|
54
|
+
* (queued changes, their files) goes too (FOL-126 phase 4). */
|
|
55
|
+
let onGone = null;
|
|
56
|
+
export function registerWorkspaceGone(fn) {
|
|
57
|
+
onGone = fn;
|
|
58
|
+
}
|
|
59
|
+
export function workspaceGone(tenant, workspace) {
|
|
60
|
+
try {
|
|
61
|
+
onGone?.(tenant, workspace);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// a journal that cannot be cleared now is cleared at the next boot (the replay discards a missing workspace)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let lifecycleHooks = null;
|
|
68
|
+
export function registerWorkspaceLifecycle(l) {
|
|
69
|
+
lifecycleHooks = l;
|
|
70
|
+
}
|
|
71
|
+
export const workspaceLifecycle = () => lifecycleHooks;
|
|
72
|
+
/** A workspace's folder was renamed (sync path): see WorkspaceLifecycle.moved. */
|
|
73
|
+
export function workspaceMoved(tenant, from, to) {
|
|
74
|
+
try {
|
|
75
|
+
lifecycleHooks?.moved(tenant, from, to);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// finished by the next reconcile
|
|
79
|
+
}
|
|
80
|
+
}
|
|
45
81
|
function assertSafeName(v, what) {
|
|
46
82
|
if (!/^[A-Za-z0-9][A-Za-z0-9._@-]{0,120}$/.test(v))
|
|
47
83
|
throw new Error(`illegal ${what}: ${v}`);
|
|
@@ -117,6 +153,12 @@ export function recordRevision(tenant, scope, files, meta = {}) {
|
|
|
117
153
|
// outside this call, such as a bundle's generated index.
|
|
118
154
|
snapshot(tenant, scope, rev.id);
|
|
119
155
|
fs.appendFileSync(path.join(dir, "index.jsonl"), `${JSON.stringify(line)}\n`);
|
|
156
|
+
try {
|
|
157
|
+
journal?.(tenant, scope, changed, { by: rev.by, message: rev.message, at: rev.at });
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// the revision store is not allowed to break a save; workspace-revisions logs and retries
|
|
161
|
+
}
|
|
120
162
|
return rev;
|
|
121
163
|
}
|
|
122
164
|
// ----------------------------------------------------------------- snapshots
|
|
@@ -158,12 +200,12 @@ function snapshot(tenant, scope, id) {
|
|
|
158
200
|
return true;
|
|
159
201
|
}
|
|
160
202
|
/** Whether a revision can be restored to: it has a snapshot. */
|
|
161
|
-
export function
|
|
203
|
+
export function hasSnapshotLocal(tenant, scope, id) {
|
|
162
204
|
assertSafeName(id, "revision id");
|
|
163
205
|
return fs.existsSync(treePath(tenant, scope, id));
|
|
164
206
|
}
|
|
165
207
|
/** The whole tree at revision `id`, or null when it has no snapshot. */
|
|
166
|
-
export function
|
|
208
|
+
export function snapshotAtLocal(tenant, scope, id) {
|
|
167
209
|
assertSafeName(id, "revision id");
|
|
168
210
|
let manifest;
|
|
169
211
|
try {
|
|
@@ -187,7 +229,7 @@ function snapshotIds(tenant, scope) {
|
|
|
187
229
|
}
|
|
188
230
|
}
|
|
189
231
|
/** The newest revision that can be restored to — what "now" is in history. */
|
|
190
|
-
export function
|
|
232
|
+
export function headRevisionLocal(tenant, scope) {
|
|
191
233
|
const have = snapshotIds(tenant, scope);
|
|
192
234
|
return journalRevisions(tenant, scope, { limit: Infinity }).find((r) => have.has(r.id)) ?? null;
|
|
193
235
|
}
|
|
@@ -198,8 +240,8 @@ export function headRevision(tenant, scope) {
|
|
|
198
240
|
* by a path that never recorded a change. Returns the head's id, or null
|
|
199
241
|
* when there is nothing to snapshot.
|
|
200
242
|
*/
|
|
201
|
-
export function
|
|
202
|
-
const head =
|
|
243
|
+
export function ensureBaselineLocal(tenant, scope, by = "foldrun") {
|
|
244
|
+
const head = headRevisionLocal(tenant, scope);
|
|
203
245
|
if (head)
|
|
204
246
|
return head.id;
|
|
205
247
|
const tree = readTree(tenant, scope);
|
|
@@ -215,7 +257,7 @@ export function ensureBaseline(tenant, scope, by = "foldrun") {
|
|
|
215
257
|
fs.writeFileSync(marker, id, { flag: "wx" });
|
|
216
258
|
}
|
|
217
259
|
catch {
|
|
218
|
-
const held =
|
|
260
|
+
const held = headRevisionLocal(tenant, scope)?.id ?? null;
|
|
219
261
|
// A marker with no baseline behind it, minutes old, is a process that
|
|
220
262
|
// died writing one: clear it, and the next call writes the baseline.
|
|
221
263
|
try {
|
|
@@ -249,12 +291,15 @@ export function ensureBaseline(tenant, scope, by = "foldrun") {
|
|
|
249
291
|
* moment. Null when history holds nothing that old. Throws (status 400) on
|
|
250
292
|
* something that is none of those.
|
|
251
293
|
*/
|
|
252
|
-
export function
|
|
294
|
+
export function resolveRevisionLocal(tenant, scope, to, now = Date.now()) {
|
|
295
|
+
const have = snapshotIds(tenant, scope);
|
|
296
|
+
return resolveIn(journalRevisions(tenant, scope, { limit: Infinity }).filter((r) => have.has(r.id)), to, now);
|
|
297
|
+
}
|
|
298
|
+
/** The pick of resolveRevision over a newest-first list of restorable revisions. */
|
|
299
|
+
function resolveIn(all, to, now) {
|
|
253
300
|
const t = to.trim();
|
|
254
301
|
if (!t || t.startsWith("-"))
|
|
255
302
|
throw Object.assign(new Error(`not a revision or a time: ${to}`), { status: 400 });
|
|
256
|
-
const have = snapshotIds(tenant, scope);
|
|
257
|
-
const all = journalRevisions(tenant, scope, { limit: Infinity }).filter((r) => have.has(r.id));
|
|
258
303
|
const rel = /^(\d+)\s*(m|h|d|w)$/.exec(t);
|
|
259
304
|
const moment = rel
|
|
260
305
|
? now - Number(rel[1]) * { m: 60_000, h: 3_600_000, d: 86_400_000, w: 604_800_000 }[rel[2]]
|
|
@@ -294,7 +339,7 @@ function describe(files) {
|
|
|
294
339
|
.join(", ") + ` file${files.length === 1 ? "" : "s"}`;
|
|
295
340
|
}
|
|
296
341
|
/** Newest first. `forPath` narrows to revisions that touched one file. */
|
|
297
|
-
export function
|
|
342
|
+
export function listRevisionsLocal(tenant, scope, opts = {}) {
|
|
298
343
|
return journalRevisions(tenant, scope, opts);
|
|
299
344
|
}
|
|
300
345
|
function journalRevisions(tenant, scope, opts = {}) {
|
|
@@ -358,7 +403,7 @@ function rebuildIndex(dir) {
|
|
|
358
403
|
}
|
|
359
404
|
return rows.map(({ written: _w, ...r }) => r).reverse();
|
|
360
405
|
}
|
|
361
|
-
export function
|
|
406
|
+
export function readRevisionLocal(tenant, scope, id, forPath) {
|
|
362
407
|
assertSafeName(id, "revision id");
|
|
363
408
|
const file = path.join(historyDir(tenant, scope), `${id}.json`);
|
|
364
409
|
if (!fs.existsSync(file))
|
|
@@ -369,13 +414,15 @@ export function readRevision(tenant, scope, id, forPath) {
|
|
|
369
414
|
/** The files changed after revision `from` up to and including `to`, in a
|
|
370
415
|
* scope — what differs between the versions two runs ran (FOL-72). Null
|
|
371
416
|
* when either is unknown or `to` is not after `from`. */
|
|
372
|
-
export function
|
|
417
|
+
export function changedPathsBetweenLocal(tenant, scope, from, to) {
|
|
373
418
|
if (!from || !to)
|
|
374
419
|
return null;
|
|
375
420
|
if (from === to)
|
|
376
421
|
return [];
|
|
377
|
-
|
|
378
|
-
|
|
422
|
+
return changedIn(listRevisionsLocal(tenant, scope, { limit: 500 }), from, to);
|
|
423
|
+
}
|
|
424
|
+
function changedIn(revs, from, to) {
|
|
425
|
+
// newest first. An exact id before a commit, as resolveRevision.
|
|
379
426
|
const at = (x) => {
|
|
380
427
|
const byId = revs.findIndex((r) => r.id === x);
|
|
381
428
|
return byId >= 0 ? byId : revs.findIndex((r) => r.commit === x);
|
|
@@ -388,8 +435,77 @@ export function changedPathsBetween(tenant, scope, from, to) {
|
|
|
388
435
|
}
|
|
389
436
|
/** The most recent revision id for a scope — what a change is attributed
|
|
390
437
|
* to when nothing more specific (a deployed commit) is on record. */
|
|
391
|
-
export function
|
|
392
|
-
return
|
|
438
|
+
export function latestRevisionIdLocal(tenant, scope) {
|
|
439
|
+
return listRevisionsLocal(tenant, scope, { limit: 1 })[0]?.id ?? null;
|
|
440
|
+
}
|
|
441
|
+
let view = null;
|
|
442
|
+
export function registerRevisionView(v) {
|
|
443
|
+
view = v;
|
|
444
|
+
}
|
|
445
|
+
/** Revisions first, then the local entries older than the oldest of them. */
|
|
446
|
+
async function combined(tenant, scope, opts, restorableOnly = false) {
|
|
447
|
+
const limit = opts.limit ?? 200;
|
|
448
|
+
const mine = view && scope !== "@library" ? await view.list(tenant, scope, { ...opts, limit: Number.isFinite(limit) ? limit : 500 }) : null;
|
|
449
|
+
let local = listRevisionsLocal(tenant, scope, { forPath: opts.forPath, limit: Infinity });
|
|
450
|
+
if (restorableOnly) {
|
|
451
|
+
const have = snapshotIds(tenant, scope);
|
|
452
|
+
local = local.filter((r) => have.has(r.id));
|
|
453
|
+
}
|
|
454
|
+
if (!mine || !mine.length)
|
|
455
|
+
return local.slice(0, limit);
|
|
456
|
+
const oldest = mine[mine.length - 1].at;
|
|
457
|
+
const seen = new Set(mine.map((r) => r.id));
|
|
458
|
+
return [...mine, ...local.filter((r) => !seen.has(r.id) && r.at < oldest)].slice(0, limit);
|
|
459
|
+
}
|
|
460
|
+
export async function listRevisions(tenant, scope, opts = {}) {
|
|
461
|
+
return combined(tenant, scope, opts);
|
|
462
|
+
}
|
|
463
|
+
/** The list the history page shows: each row with whether it can be restored
|
|
464
|
+
* to. One listing answers both — every revision in the store is restorable;
|
|
465
|
+
* a local entry is when it has a snapshot — where asking per row would list
|
|
466
|
+
* the store once per row. */
|
|
467
|
+
export async function listRevisionsRestorable(tenant, scope, opts = {}) {
|
|
468
|
+
const rows = await combined(tenant, scope, opts);
|
|
469
|
+
const have = snapshotIds(tenant, scope);
|
|
470
|
+
const stored = view && scope !== "@library" ? new Set(((await view.list(tenant, scope, { limit: 500 })) ?? []).map((r) => r.id)) : new Set();
|
|
471
|
+
return rows.map((r) => ({ ...r, restorable: have.has(r.id) || stored.has(r.id) }));
|
|
472
|
+
}
|
|
473
|
+
export async function readRevision(tenant, scope, id, forPath) {
|
|
474
|
+
assertSafeName(id, "revision id");
|
|
475
|
+
const fromStore = view && scope !== "@library" ? await view.read(tenant, scope, id, forPath) : null;
|
|
476
|
+
return fromStore ?? readRevisionLocal(tenant, scope, id, forPath);
|
|
477
|
+
}
|
|
478
|
+
export async function snapshotAt(tenant, scope, id) {
|
|
479
|
+
assertSafeName(id, "revision id");
|
|
480
|
+
const fromStore = view && scope !== "@library" ? await view.snapshot(tenant, scope, id) : null;
|
|
481
|
+
return fromStore ?? snapshotAtLocal(tenant, scope, id);
|
|
482
|
+
}
|
|
483
|
+
export async function hasSnapshot(tenant, scope, id) {
|
|
484
|
+
assertSafeName(id, "revision id");
|
|
485
|
+
if (hasSnapshotLocal(tenant, scope, id))
|
|
486
|
+
return true;
|
|
487
|
+
return Boolean(view && scope !== "@library" && (await view.list(tenant, scope, { limit: 500 }))?.some((r) => r.id === id));
|
|
488
|
+
}
|
|
489
|
+
export async function headRevision(tenant, scope) {
|
|
490
|
+
return (await combined(tenant, scope, { limit: 1 }, true))[0] ?? null;
|
|
491
|
+
}
|
|
492
|
+
export async function resolveRevision(tenant, scope, to, now = Date.now()) {
|
|
493
|
+
return resolveIn(await combined(tenant, scope, { limit: Infinity }, true), to, now);
|
|
494
|
+
}
|
|
495
|
+
export async function changedPathsBetween(tenant, scope, from, to) {
|
|
496
|
+
if (!from || !to)
|
|
497
|
+
return null;
|
|
498
|
+
if (from === to)
|
|
499
|
+
return [];
|
|
500
|
+
return changedIn(await combined(tenant, scope, { limit: 500 }), from, to);
|
|
501
|
+
}
|
|
502
|
+
export async function latestRevisionId(tenant, scope) {
|
|
503
|
+
return (await combined(tenant, scope, { limit: 1 }))[0]?.id ?? null;
|
|
504
|
+
}
|
|
505
|
+
/** A scope with no revision at all gets a local baseline, as before. */
|
|
506
|
+
export async function ensureBaseline(tenant, scope, by = "foldrun") {
|
|
507
|
+
const head = await headRevision(tenant, scope);
|
|
508
|
+
return head ? head.id : ensureBaselineLocal(tenant, scope, by);
|
|
393
509
|
}
|
|
394
510
|
/**
|
|
395
511
|
* A line diff, for showing a revision. Plain LCS — quadratic, and fine: the
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** What is stored: the whole entry (new content only, never the old text). */
|
|
2
|
+
export interface JournalRecord {
|
|
3
|
+
v: 1;
|
|
4
|
+
tenant: string;
|
|
5
|
+
workspace: string;
|
|
6
|
+
id: string;
|
|
7
|
+
files: {
|
|
8
|
+
path: string;
|
|
9
|
+
before: null;
|
|
10
|
+
after: string | null;
|
|
11
|
+
base?: string | null;
|
|
12
|
+
handed?: string | null;
|
|
13
|
+
}[];
|
|
14
|
+
by: string;
|
|
15
|
+
message: string;
|
|
16
|
+
at: string;
|
|
17
|
+
restoredFrom?: string;
|
|
18
|
+
/** A run's copy-back: the revisions (workspace head, state head) its stage-in
|
|
19
|
+
* started from. Each file's base is read from these, not from the folder. */
|
|
20
|
+
baseRev?: {
|
|
21
|
+
workspace: string | null;
|
|
22
|
+
state: string | null;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface JournalStore {
|
|
26
|
+
/** Which store this is, for logs ("files", "database"). */
|
|
27
|
+
readonly kind: string;
|
|
28
|
+
/** Write the entry down; throws if it cannot. Returns its handle. Putting an
|
|
29
|
+
* id that is already there answers the existing handle. */
|
|
30
|
+
put(rec: JournalRecord): string;
|
|
31
|
+
/** Forget one entry. Safe to repeat. */
|
|
32
|
+
remove(tenant: string, workspace: string, handle: string): void;
|
|
33
|
+
/** Every workspace that has entries. */
|
|
34
|
+
workspaces(): {
|
|
35
|
+
tenant: string;
|
|
36
|
+
workspace: string;
|
|
37
|
+
}[];
|
|
38
|
+
/** One workspace's entries, oldest first, with how long each has been there. */
|
|
39
|
+
entries(tenant: string, workspace: string): {
|
|
40
|
+
handle: string;
|
|
41
|
+
ageMs: number;
|
|
42
|
+
}[];
|
|
43
|
+
/** Read one entry; throws for one that is torn or foreign. */
|
|
44
|
+
read(tenant: string, workspace: string, handle: string): JournalRecord;
|
|
45
|
+
/** Keep an unreadable entry out of every later replay, without destroying it. */
|
|
46
|
+
setAside(tenant: string, workspace: string, handle: string): void;
|
|
47
|
+
/** Housekeeping before a replay: debris of a write that died half way (files only). */
|
|
48
|
+
sweep(tenant: string, workspace: string, olderThanMs: number): void;
|
|
49
|
+
/** Remove everything of one workspace, or of the whole account. Safe to repeat. */
|
|
50
|
+
drop(tenant: string, workspace?: string): void;
|
|
51
|
+
/** A workspace was renamed: its entries not held by a live queue (a dead
|
|
52
|
+
* process's) now belong to the new name. Handles of entries that were
|
|
53
|
+
* moved are NOT promised to stay valid: a caller holding some forgets them
|
|
54
|
+
* first and puts them again under the new name. */
|
|
55
|
+
move(tenant: string, from: string, to: string): void;
|
|
56
|
+
}
|
|
57
|
+
/** The files: <root>/<tenant>/<workspace>/<time>-<seq>-<id>.json, tmp + rename. */
|
|
58
|
+
export declare function fsJournalStore(root: () => string): JournalStore;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Where the revision journal's entries are kept (FOL-126 phase 4, item 3).
|
|
2
|
+
//
|
|
3
|
+
// The journal (workspace-revisions.ts) writes every change down BEFORE it is
|
|
4
|
+
// queued for a commit, and removes it once committed. Until now "written down"
|
|
5
|
+
// meant a file under <data>/.revision-journal/ — on the shared volume, which
|
|
6
|
+
// the storage refactor retires. So the place is a seam:
|
|
7
|
+
//
|
|
8
|
+
// fsJournalStore the files, as before: the default, and the only one a
|
|
9
|
+
// laptop or a Compose install without a database ever has.
|
|
10
|
+
// A hosted install keeps it as a LEGACY source, replayed at
|
|
11
|
+
// boot and drained, so switching loses nothing.
|
|
12
|
+
// (platform) pgJournalStore (src/journal-pg.ts): a table, behind the
|
|
13
|
+
// same calls, registered where there is a database.
|
|
14
|
+
//
|
|
15
|
+
// The calls are synchronous (the writers that journal are), and an entry's
|
|
16
|
+
// `handle` is whatever the store needs to find it again.
|
|
17
|
+
import fs from "node:fs";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
/** The files: <root>/<tenant>/<workspace>/<time>-<seq>-<id>.json, tmp + rename. */
|
|
20
|
+
export function fsJournalStore(root) {
|
|
21
|
+
let seq = 0;
|
|
22
|
+
const dirOf = (tenant, workspace) => path.join(root(), tenant, workspace);
|
|
23
|
+
return {
|
|
24
|
+
kind: "files",
|
|
25
|
+
put(rec) {
|
|
26
|
+
const dir = dirOf(rec.tenant, rec.workspace);
|
|
27
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
28
|
+
const name = `${Date.now().toString().padStart(14, "0")}-${String(seq++ % 1_000_000).padStart(6, "0")}-${rec.id}.json`;
|
|
29
|
+
const file = path.join(dir, name);
|
|
30
|
+
fs.writeFileSync(`${file}.tmp`, JSON.stringify(rec));
|
|
31
|
+
fs.renameSync(`${file}.tmp`, file);
|
|
32
|
+
return file;
|
|
33
|
+
},
|
|
34
|
+
remove(tenant, workspace, handle) {
|
|
35
|
+
try {
|
|
36
|
+
fs.rmSync(handle, { force: true });
|
|
37
|
+
fs.rmdirSync(dirOf(tenant, workspace)); // only when empty
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// not empty, or already gone
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
workspaces() {
|
|
44
|
+
const out = [];
|
|
45
|
+
let tenants = [];
|
|
46
|
+
try {
|
|
47
|
+
tenants = fs.readdirSync(root());
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
for (const tenant of tenants) {
|
|
53
|
+
try {
|
|
54
|
+
for (const workspace of fs.readdirSync(path.join(root(), tenant)))
|
|
55
|
+
out.push({ tenant, workspace });
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// not a directory
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
},
|
|
63
|
+
entries(tenant, workspace) {
|
|
64
|
+
const dir = dirOf(tenant, workspace);
|
|
65
|
+
let names = [];
|
|
66
|
+
try {
|
|
67
|
+
names = fs.readdirSync(dir).filter((n) => n.endsWith(".json")).sort();
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
const out = [];
|
|
73
|
+
for (const n of names) {
|
|
74
|
+
const file = path.join(dir, n);
|
|
75
|
+
try {
|
|
76
|
+
out.push({ handle: file, ageMs: Date.now() - fs.statSync(file).mtimeMs });
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// gone meanwhile
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
},
|
|
84
|
+
read(_t, _w, handle) {
|
|
85
|
+
return JSON.parse(fs.readFileSync(handle, "utf8"));
|
|
86
|
+
},
|
|
87
|
+
setAside(_t, _w, handle) {
|
|
88
|
+
fs.renameSync(handle, `${handle}.unreadable`);
|
|
89
|
+
},
|
|
90
|
+
sweep(tenant, workspace, olderThanMs) {
|
|
91
|
+
const dir = dirOf(tenant, workspace);
|
|
92
|
+
try {
|
|
93
|
+
for (const n of fs.readdirSync(dir)) {
|
|
94
|
+
// a write that died before its rename leaves a .tmp: nothing to replay
|
|
95
|
+
if (n.endsWith(".json.tmp") && olderThanMs > 0 && Date.now() - fs.statSync(path.join(dir, n)).mtimeMs > olderThanMs)
|
|
96
|
+
fs.rmSync(path.join(dir, n), { force: true });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// nothing there
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
drop(tenant, workspace) {
|
|
104
|
+
fs.rmSync(workspace === undefined ? path.join(root(), tenant) : dirOf(tenant, workspace), { recursive: true, force: true });
|
|
105
|
+
},
|
|
106
|
+
move(tenant, from, to) {
|
|
107
|
+
const src = dirOf(tenant, from);
|
|
108
|
+
if (!fs.existsSync(src))
|
|
109
|
+
return;
|
|
110
|
+
const dest = dirOf(tenant, to);
|
|
111
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
112
|
+
for (const n of fs.readdirSync(src))
|
|
113
|
+
fs.renameSync(path.join(src, n), path.join(dest, n));
|
|
114
|
+
fs.rmSync(src, { recursive: true, force: true });
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export interface ManifestEntry {
|
|
2
|
+
/** sha256 of the bytes, lower-case hex. The blob's name. */
|
|
3
|
+
sha: string;
|
|
4
|
+
size: number;
|
|
5
|
+
/** 0o644, or 0o755 when the file is executable (store.ts marks scripts so).
|
|
6
|
+
* Stored as the number, so JSON carries 420 or 493. Nothing else is kept:
|
|
7
|
+
* owner, times and extended attributes are not part of a workspace. */
|
|
8
|
+
mode: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Manifest {
|
|
11
|
+
v: 1;
|
|
12
|
+
/** Forward-slashed paths, relative to the workspace root. */
|
|
13
|
+
files: Record<string, ManifestEntry>;
|
|
14
|
+
}
|
|
15
|
+
export declare const MODE_FILE = 420;
|
|
16
|
+
export declare const MODE_EXEC = 493;
|
|
17
|
+
/** A manifest id: the sha256 of its canonical JSON. */
|
|
18
|
+
export type ManifestId = string;
|
|
19
|
+
export declare const AUTHORED_DIRS: readonly string[];
|
|
20
|
+
export declare const STATE_DIRS: readonly string[];
|
|
21
|
+
export declare const AUTHORED_ROOT_FILES: readonly string[];
|
|
22
|
+
export type Side = "authored" | "state";
|
|
23
|
+
/** Which manifest a path belongs in, or null when it belongs in neither. */
|
|
24
|
+
export declare function sideOf(rel: string): Side | null;
|
|
25
|
+
/**
|
|
26
|
+
* A manifest path: relative, forward-slashed, no empty / `.` / `..` segment,
|
|
27
|
+
* no control characters, at most 400 characters (the same bounds as a storage
|
|
28
|
+
* path), and on one of the two sides. Throws; returns the path unchanged when
|
|
29
|
+
* it is fine. Backslashes are REFUSED rather than converted: a manifest must
|
|
30
|
+
* round-trip byte for byte, and `a\b` on one machine is a different name from
|
|
31
|
+
* `a/b` on another.
|
|
32
|
+
*/
|
|
33
|
+
export declare const MAX_PATH_BYTES = 400;
|
|
34
|
+
/** Files per manifest (authored and state each). A manifest is read and
|
|
35
|
+
* rewritten whole on every publish; this bounds that cost until manifests
|
|
36
|
+
* are sharded. 50k files is ~5 MB of canonical JSON. */
|
|
37
|
+
export declare const MAX_MANIFEST_FILES = 50000;
|
|
38
|
+
export declare function assertManifestPath(rel: string): string;
|
|
39
|
+
/** Two paths that differ only by case cannot both be extracted on a
|
|
40
|
+
* case-insensitive disk (macOS and Windows laptops, Docker Desktop volumes).
|
|
41
|
+
* Display casing is kept; a collision inside one manifest is refused. */
|
|
42
|
+
export declare function assertNoCaseCollisions(m: Manifest): void;
|
|
43
|
+
export declare const sha256Hex: (b: string | Uint8Array) => string;
|
|
44
|
+
/** What "the same entry" means: bytes AND mode. A chmod +x is a change even
|
|
45
|
+
* though the sha is the same. Changes record this as their `base`. */
|
|
46
|
+
export declare const entryId: (e: Pick<ManifestEntry, "sha" | "mode">) => string;
|
|
47
|
+
export declare function assertEntry(p: string, e: ManifestEntry): void;
|
|
48
|
+
/** Everything parseManifest would refuse, checked on a manifest we are about
|
|
49
|
+
* to WRITE — a revision must never point at a manifest it cannot read back. */
|
|
50
|
+
export declare function assertManifest(m: Manifest): void;
|
|
51
|
+
export declare function emptyManifest(): Manifest;
|
|
52
|
+
/** Bytes → the mode a manifest records: executable stays executable. */
|
|
53
|
+
export declare function modeFor(executable: boolean): number;
|
|
54
|
+
/** Sorted keys at every level, no whitespace: one tree, one byte string, one
|
|
55
|
+
* id — whichever machine wrote it. */
|
|
56
|
+
export declare function canonicalManifest(m: Manifest): string;
|
|
57
|
+
export declare function manifestId(m: Manifest): ManifestId;
|
|
58
|
+
/** The id of a manifest with no files. A revision on one head names this for
|
|
59
|
+
* the side that head does not own (the workspace head carries no state, the
|
|
60
|
+
* state head carries no authored files); it is never stored, loadManifest
|
|
61
|
+
* answers it. */
|
|
62
|
+
export declare const EMPTY_MANIFEST_ID: ManifestId;
|
|
63
|
+
/** Parse and validate a manifest read from a store. A manifest that does not
|
|
64
|
+
* say so cleanly is a broken revision, not an empty one. */
|
|
65
|
+
export declare function parseManifest(text: string): Manifest;
|
|
66
|
+
export declare const blobObjectKey: (tenant: string, sha: string) => string;
|
|
67
|
+
export declare const manifestObjectKey: (tenant: string, id: ManifestId) => string;
|
|
68
|
+
/** One path's change, as a writer saw it. `base` is the entryId (sha + mode)
|
|
69
|
+
* the writer STARTED from (null: the path did not exist) — the fact that makes "disjoint paths
|
|
70
|
+
* replay, the same path conflicts" decidable without a merge. */
|
|
71
|
+
export type PathChange = {
|
|
72
|
+
op: "put";
|
|
73
|
+
base: string | null;
|
|
74
|
+
sha: string;
|
|
75
|
+
size: number;
|
|
76
|
+
mode: number;
|
|
77
|
+
} | {
|
|
78
|
+
op: "delete";
|
|
79
|
+
base: string;
|
|
80
|
+
};
|
|
81
|
+
export type Changes = Record<string, PathChange>;
|
|
82
|
+
/** Apply changes to a manifest, ignoring `base` (the caller decided). */
|
|
83
|
+
export declare function applyChanges(m: Manifest, changes: Changes): Manifest;
|
|
84
|
+
/** The changes that turn `from` into `to`, with `from`'s shas as the bases. */
|
|
85
|
+
export declare function diffManifests(from: Manifest, to: Manifest): Changes;
|
|
86
|
+
/** The copy a conflicting writer's version is kept as. Deterministic in the
|
|
87
|
+
* operation id — a retried publish names the same file, so a retry cannot
|
|
88
|
+
* leave two copies — and next to the original, like today's
|
|
89
|
+
* `.conflict-<stamp>` copy in applyContainerChanges. 48 bits of the op id's
|
|
90
|
+
* hash; `attempt` > 0 is the caller stepping aside when that name is already
|
|
91
|
+
* taken by something else (publish never overwrites an occupied name). */
|
|
92
|
+
export declare function conflictPath(rel: string, opId: string, attempt?: number): string;
|