@parcha/agentrun-computer 0.1.0-beta.43 → 0.1.0-beta.45
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 +98 -12
- package/box/Dockerfile +2 -1
- package/box/build-daytona.py +12 -2
- package/box/install.sh +19 -5
- package/box/recipe.mjs +1 -1
- package/dist/attach.d.ts +13 -3
- package/dist/attach.js +182 -15
- package/dist/cloudflare.d.ts +4 -2
- package/dist/cloudflare.js +7 -3
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/conformance/cases/files.js +7 -1
- package/dist/conformance/cases/isolation.js +4 -0
- package/dist/conformance/cases/mount.js +155 -2
- package/dist/conformance/cases/provider.js +3 -1
- package/dist/conformance/daytona.d.ts +15 -1
- package/dist/conformance/daytona.js +19 -4
- package/dist/conformance/types.d.ts +1 -1
- package/dist/daytona.d.ts +11 -2
- package/dist/daytona.js +47 -15
- package/dist/descriptor.d.ts +34 -4
- package/dist/descriptor.js +40 -6
- package/dist/descriptors/cloudflare.d.ts +14 -1
- package/dist/descriptors/cloudflare.js +7 -3
- package/dist/descriptors/daytona.d.ts +21 -3
- package/dist/descriptors/daytona.js +15 -8
- package/dist/descriptors/e2b.d.ts +1 -0
- package/dist/descriptors/e2b.js +2 -1
- package/dist/descriptors/fly.d.ts +14 -1
- package/dist/descriptors/fly.js +6 -3
- package/dist/descriptors/index.d.ts +64 -2
- package/dist/descriptors/modal.d.ts +16 -2
- package/dist/descriptors/modal.js +10 -6
- package/dist/descriptors/orgo.d.ts +3 -0
- package/dist/descriptors/orgo.js +3 -1
- package/dist/e2b.d.ts +9 -4
- package/dist/e2b.js +8 -8
- package/dist/env-proxy.d.ts +26 -0
- package/dist/env-proxy.js +97 -0
- package/dist/fly.d.ts +4 -3
- package/dist/fly.js +7 -4
- package/dist/hosted/provider.d.ts +2 -1
- package/dist/hosted/provider.js +133 -54
- package/dist/hosted/scripts.d.ts +3 -2
- package/dist/hosted/scripts.js +12 -7
- package/dist/hosted/values.d.ts +2 -5
- package/dist/hosted/values.js +2 -9
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/dist/kinds.d.ts +4 -2
- package/dist/kinds.js +10 -1
- package/dist/modal.d.ts +5 -2
- package/dist/modal.js +8 -2
- package/dist/morph.js +15 -7
- package/dist/mount-archil.d.ts +33 -17
- package/dist/mount-archil.js +83 -36
- package/dist/mount.d.ts +16 -7
- package/dist/mount.js +13 -8
- package/dist/orgo.js +1 -1
- package/dist/provider.d.ts +1 -1
- package/dist/sync-folder.d.ts +82 -0
- package/dist/sync-folder.js +494 -0
- package/dist/tar.d.ts +34 -0
- package/dist/tar.js +203 -0
- package/package.json +1 -1
- package/dist/conformance/tar.d.ts +0 -13
- package/dist/conformance/tar.js +0 -105
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type ComputerClient } from "./protocol.js";
|
|
2
|
+
import { type Result } from "./result.js";
|
|
3
|
+
/** The most entries (files, directories and links) a synced folder holds: over it, a sync fails, kind limit. */
|
|
4
|
+
export declare const SYNC_MAX_ENTRIES = 20000;
|
|
5
|
+
/** The most bytes a synced folder's files hold together: over it, a sync fails, kind limit. */
|
|
6
|
+
export declare const SYNC_MAX_BYTES: number;
|
|
7
|
+
/**
|
|
8
|
+
* The folder's modes and links: what an object store keeps no record of. A mode is listed where it is not the default
|
|
9
|
+
* (0644 for a file, 0755 for a directory); a link by its target, as written, never followed.
|
|
10
|
+
*/
|
|
11
|
+
export interface FolderMeta {
|
|
12
|
+
modes: Record<string, number>;
|
|
13
|
+
links: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Where the delta lands: the runner's copy of the run's folder. Every path is relative to the folder and passed the
|
|
17
|
+
* relative-path rule (no absolute path, no "." or ".." or empty segment, no backslash, no NUL). Each answers once what
|
|
18
|
+
* it wrote is durable; a failure is retried at the next sync, so each must be idempotent.
|
|
19
|
+
*/
|
|
20
|
+
export interface SyncSink {
|
|
21
|
+
/** A file's bytes at path, replacing what was there. */
|
|
22
|
+
put(path: string, body: Uint8Array): Promise<Result<void>>;
|
|
23
|
+
/** An empty directory at path, which no file under it records. */
|
|
24
|
+
mkdir(path: string): Promise<Result<void>>;
|
|
25
|
+
/** What path held: a file, or a directory's own record. What is under a directory comes first, each on its own. */
|
|
26
|
+
remove(path: string, type: "file" | "dir"): Promise<Result<void>>;
|
|
27
|
+
/** The folder's modes and links, whole, written only when they changed. */
|
|
28
|
+
meta(meta: FolderMeta): Promise<Result<void>>;
|
|
29
|
+
}
|
|
30
|
+
/** What one sync wrote. */
|
|
31
|
+
export interface SyncCounts {
|
|
32
|
+
put: number;
|
|
33
|
+
mkdir: number;
|
|
34
|
+
removed: number;
|
|
35
|
+
meta: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** Why a path in the folder is no relative path a sink takes, in words, or undefined when it is one. */
|
|
38
|
+
export declare function folderPathProblem(path: string): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* One synced folder: the computer's /workspace (root) through its computerd client, and the sink it lands in. baseline()
|
|
41
|
+
* reads what a push left as the manifest; each sync() then writes what changed since. One sync at a time (syncingClient
|
|
42
|
+
* runs one); `now` is the runner's clock, for the racy rule.
|
|
43
|
+
*/
|
|
44
|
+
export declare class FolderSync {
|
|
45
|
+
#private;
|
|
46
|
+
constructor(client: ComputerClient, sink: SyncSink, { root, now }?: {
|
|
47
|
+
root?: string;
|
|
48
|
+
now?: () => number;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* The manifest of a folder just pushed from the sink, read, never guessed (extraction sets no mtime): what it lists is
|
|
52
|
+
* what the sink holds, so nothing is pulled. Over a cap, it fails, kind limit.
|
|
53
|
+
*/
|
|
54
|
+
baseline(options?: {
|
|
55
|
+
signal?: AbortSignal;
|
|
56
|
+
}): Promise<Result<void>>;
|
|
57
|
+
/**
|
|
58
|
+
* The manifest of a folder whose copy in the computer may be newer than the sink's, with no manifest of its own (a
|
|
59
|
+
* kept computer attached again by a new runner): the sink holds `held` (each path's type), and nothing of the
|
|
60
|
+
* computer's copy is known, so the next sync() puts every file the computer holds, removes from the sink what it holds
|
|
61
|
+
* beyond them, and writes the modes and links.
|
|
62
|
+
*/
|
|
63
|
+
adopt(held: Iterable<readonly [string, "file" | "dir"]>): void;
|
|
64
|
+
/**
|
|
65
|
+
* The folder's modes and links as the computer lists them now, written to the sink whole, and nothing else: for a
|
|
66
|
+
* folder whose mount keeps them itself (an Archil one), so a later claim that holds it as a synced folder has them.
|
|
67
|
+
* Over a cap, it fails, kind limit, and writes nothing.
|
|
68
|
+
*/
|
|
69
|
+
recordMeta(options?: {
|
|
70
|
+
signal?: AbortSignal;
|
|
71
|
+
}): Promise<Result<void>>;
|
|
72
|
+
/**
|
|
73
|
+
* What changed since the last sync, written to the sink: removes for paths whose type changed (deepest first), then
|
|
74
|
+
* puts (a new directory's whole tree as one tar.pull, other files by files.read up to MAX_CHUNK_BYTES, a tar.pull of the
|
|
75
|
+
* file above it), then empty directories, then, once every put has landed, deletes (deepest first), then the meta when
|
|
76
|
+
* it differs from the last written. An entry of the manifest advances only once its write answered; what failed is
|
|
77
|
+
* sent again at the next sync, and the sync fails in words naming it.
|
|
78
|
+
*/
|
|
79
|
+
sync(options?: {
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
}): Promise<Result<SyncCounts>>;
|
|
82
|
+
}
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
// The synced folder's delta: what changed in a computer's /workspace since the last sync, written to the runner's copy
|
|
2
|
+
// of the run's folder (a SyncSink) before the call that changed it answers. It needs no protocol of its own: one
|
|
3
|
+
// files.list of the folder (paged, pipelined) against the manifest of the last, then files.read or tar.pull for the
|
|
4
|
+
// bytes that changed. Directories, modes and links come from the listing alone, with no bytes.
|
|
5
|
+
//
|
|
6
|
+
// Known miss: a write that leaves a file's size and mtime both as they were (`touch -d`, `cp -p` onto a file of the same
|
|
7
|
+
// size) is not seen. A same-size rewrite within the same millisecond is, by the racy rule (git's): a file whose mtime is
|
|
8
|
+
// within 1 s of the newest in its listing is pulled again at the first sync at least 1 s later. Bytes read that are the
|
|
9
|
+
// ones last put there (that re-read, a touch) are not put again. While a directory the computer's user can't read hides
|
|
10
|
+
// what the sink holds under it (files.list lists one without what it holds), a sync writes nothing and fails in words.
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { pullTarStream } from "./files.js";
|
|
13
|
+
import { WORKSPACE } from "./mount.js";
|
|
14
|
+
import { MAX_CHUNK_BYTES, MAX_LIST_DEPTH, MAX_LIST_ENTRIES } from "./protocol.js";
|
|
15
|
+
import { fail, ok } from "./result.js";
|
|
16
|
+
import { untarStream } from "./tar.js";
|
|
17
|
+
/** The most entries (files, directories and links) a synced folder holds: over it, a sync fails, kind limit. */
|
|
18
|
+
export const SYNC_MAX_ENTRIES = 20_000;
|
|
19
|
+
/** The most bytes a synced folder's files hold together: over it, a sync fails, kind limit. */
|
|
20
|
+
export const SYNC_MAX_BYTES = 2 * 1024 ** 3;
|
|
21
|
+
/** How many pulls (files.read or tar.pull) are in flight at once, and how many puts: computerd's MaxPulls is 8. */
|
|
22
|
+
const IN_FLIGHT = 8;
|
|
23
|
+
/** A file whose mtime is within this of the newest in its listing may be rewritten unseen in the same millisecond. */
|
|
24
|
+
const RACY_MS = 1000;
|
|
25
|
+
const TOO_MANY = `the run's folder holds more than ${SYNC_MAX_ENTRIES} entries, the most a synced folder holds`;
|
|
26
|
+
const FILE_MODE = 0o644;
|
|
27
|
+
const DIR_MODE = 0o755;
|
|
28
|
+
/** Why a path in the folder is no relative path a sink takes, in words, or undefined when it is one. */
|
|
29
|
+
export function folderPathProblem(path) {
|
|
30
|
+
if (path === "")
|
|
31
|
+
return "is empty";
|
|
32
|
+
if (path.includes("\0"))
|
|
33
|
+
return "holds a NUL";
|
|
34
|
+
if (path.includes("\\"))
|
|
35
|
+
return "holds a backslash";
|
|
36
|
+
if (path.startsWith("/"))
|
|
37
|
+
return "is absolute";
|
|
38
|
+
for (const segment of path.split("/")) {
|
|
39
|
+
if (segment === "")
|
|
40
|
+
return "has an empty segment";
|
|
41
|
+
if (segment === "." || segment === "..")
|
|
42
|
+
return `has a "${segment}" segment`;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const depthOf = (path) => path.split("/").length;
|
|
47
|
+
const parentOf = (path) => path.slice(0, Math.max(0, path.lastIndexOf("/")));
|
|
48
|
+
const under = (path, dir) => path.startsWith(`${dir}/`);
|
|
49
|
+
const deepestFirst = (a, b) => depthOf(b) - depthOf(a) || (a < b ? -1 : 1);
|
|
50
|
+
/** At most n at once: acquire() answers once a slot is free, with its release. */
|
|
51
|
+
class Slots {
|
|
52
|
+
#free;
|
|
53
|
+
#waiting = [];
|
|
54
|
+
constructor(n) {
|
|
55
|
+
this.#free = n;
|
|
56
|
+
}
|
|
57
|
+
async acquire() {
|
|
58
|
+
if (this.#free > 0)
|
|
59
|
+
this.#free--;
|
|
60
|
+
else
|
|
61
|
+
await new Promise((resolve) => this.#waiting.push(resolve));
|
|
62
|
+
let released = false;
|
|
63
|
+
return () => {
|
|
64
|
+
if (released)
|
|
65
|
+
return;
|
|
66
|
+
released = true;
|
|
67
|
+
const next = this.#waiting.shift();
|
|
68
|
+
if (next)
|
|
69
|
+
next();
|
|
70
|
+
else
|
|
71
|
+
this.#free++;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
async run(work) {
|
|
75
|
+
const release = await this.acquire();
|
|
76
|
+
try {
|
|
77
|
+
return await work();
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
release();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** The folder's modes and links, from a listing; built by fromEntries, so a path such as `__proto__` is a key like any. */
|
|
85
|
+
function metaOf(listing) {
|
|
86
|
+
const modes = [];
|
|
87
|
+
const links = [];
|
|
88
|
+
for (const [path, entry] of [...listing].sort(([a], [b]) => (a < b ? -1 : 1))) {
|
|
89
|
+
if (entry.type === "symlink")
|
|
90
|
+
links.push([path, entry.target ?? ""]);
|
|
91
|
+
else if (entry.mode !== (entry.type === "dir" ? DIR_MODE : FILE_MODE))
|
|
92
|
+
modes.push([path, entry.mode]);
|
|
93
|
+
}
|
|
94
|
+
return { modes: Object.fromEntries(modes), links: Object.fromEntries(links) };
|
|
95
|
+
}
|
|
96
|
+
const knownOf = (entry, settled) => ({
|
|
97
|
+
type: entry.type,
|
|
98
|
+
size: entry.size,
|
|
99
|
+
mtimeMs: entry.mtimeMs,
|
|
100
|
+
mode: entry.mode,
|
|
101
|
+
...(entry.target === undefined ? {} : { target: entry.target }),
|
|
102
|
+
settled,
|
|
103
|
+
});
|
|
104
|
+
/**
|
|
105
|
+
* One synced folder: the computer's /workspace (root) through its computerd client, and the sink it lands in. baseline()
|
|
106
|
+
* reads what a push left as the manifest; each sync() then writes what changed since. One sync at a time (syncingClient
|
|
107
|
+
* runs one); `now` is the runner's clock, for the racy rule.
|
|
108
|
+
*/
|
|
109
|
+
export class FolderSync {
|
|
110
|
+
#client;
|
|
111
|
+
#sink;
|
|
112
|
+
#root;
|
|
113
|
+
#now;
|
|
114
|
+
#manifest = new Map();
|
|
115
|
+
/** Files found racy, by the runner's time of the listing that found them. */
|
|
116
|
+
#racy = new Map();
|
|
117
|
+
/** The meta last written, as JSON; undefined before any. */
|
|
118
|
+
#meta;
|
|
119
|
+
constructor(client, sink, { root = WORKSPACE, now = Date.now } = {}) {
|
|
120
|
+
this.#client = client;
|
|
121
|
+
this.#sink = sink;
|
|
122
|
+
this.#root = root;
|
|
123
|
+
this.#now = now;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The manifest of a folder just pushed from the sink, read, never guessed (extraction sets no mtime): what it lists is
|
|
127
|
+
* what the sink holds, so nothing is pulled. Over a cap, it fails, kind limit.
|
|
128
|
+
*/
|
|
129
|
+
async baseline(options = {}) {
|
|
130
|
+
const listed = await this.#list(options.signal);
|
|
131
|
+
if (!listed.ok)
|
|
132
|
+
return listed;
|
|
133
|
+
this.#manifest = new Map([...listed.value].map(([path, entry]) => [path, knownOf(entry, true)]));
|
|
134
|
+
this.#racy.clear();
|
|
135
|
+
this.#meta = JSON.stringify(metaOf(listed.value));
|
|
136
|
+
return ok(undefined);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The manifest of a folder whose copy in the computer may be newer than the sink's, with no manifest of its own (a
|
|
140
|
+
* kept computer attached again by a new runner): the sink holds `held` (each path's type), and nothing of the
|
|
141
|
+
* computer's copy is known, so the next sync() puts every file the computer holds, removes from the sink what it holds
|
|
142
|
+
* beyond them, and writes the modes and links.
|
|
143
|
+
*/
|
|
144
|
+
adopt(held) {
|
|
145
|
+
this.#manifest = new Map([...held].map(([path, type]) => [path, { type, size: -1, mtimeMs: -1, mode: -1, settled: true }]));
|
|
146
|
+
this.#racy.clear();
|
|
147
|
+
this.#meta = undefined;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The folder's modes and links as the computer lists them now, written to the sink whole, and nothing else: for a
|
|
151
|
+
* folder whose mount keeps them itself (an Archil one), so a later claim that holds it as a synced folder has them.
|
|
152
|
+
* Over a cap, it fails, kind limit, and writes nothing.
|
|
153
|
+
*/
|
|
154
|
+
async recordMeta(options = {}) {
|
|
155
|
+
const listed = await this.#list(options.signal);
|
|
156
|
+
if (!listed.ok)
|
|
157
|
+
return listed;
|
|
158
|
+
const meta = metaOf(listed.value);
|
|
159
|
+
const wrote = await this.#sink.meta(meta).catch((error) => fail("refused", error instanceof Error ? error.message : String(error)));
|
|
160
|
+
if (!wrote.ok)
|
|
161
|
+
return fail(wrote.error.code, `the folder's modes and links: ${wrote.error.message}`);
|
|
162
|
+
this.#meta = JSON.stringify(meta);
|
|
163
|
+
return ok(undefined);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* What changed since the last sync, written to the sink: removes for paths whose type changed (deepest first), then
|
|
167
|
+
* puts (a new directory's whole tree as one tar.pull, other files by files.read up to MAX_CHUNK_BYTES, a tar.pull of the
|
|
168
|
+
* file above it), then empty directories, then, once every put has landed, deletes (deepest first), then the meta when
|
|
169
|
+
* it differs from the last written. An entry of the manifest advances only once its write answered; what failed is
|
|
170
|
+
* sent again at the next sync, and the sync fails in words naming it.
|
|
171
|
+
*/
|
|
172
|
+
async sync(options = {}) {
|
|
173
|
+
const { signal } = options;
|
|
174
|
+
const at = this.#now();
|
|
175
|
+
const listed = await this.#list(signal);
|
|
176
|
+
if (!listed.ok)
|
|
177
|
+
return listed;
|
|
178
|
+
const listing = listed.value;
|
|
179
|
+
const manifest = this.#manifest;
|
|
180
|
+
const counts = { put: 0, mkdir: 0, removed: 0, meta: false };
|
|
181
|
+
const failures = [];
|
|
182
|
+
const failed = (code, words) => failures.push({ code, words });
|
|
183
|
+
// A directory listed with nothing under it where the manifest holds something may be one its user can't read:
|
|
184
|
+
// files.list keeps such a directory but not what it holds. Then nothing is written: what is under it is unknown.
|
|
185
|
+
const parents = (entries) => new Set([...entries].filter(([, entry]) => entry.type !== "symlink").map(([path]) => parentOf(path)));
|
|
186
|
+
const [holdsNow, heldBefore] = [parents(listing), parents(manifest)];
|
|
187
|
+
const unreadable = await this.#unreadable([...listing].filter(([path, entry]) => entry.type === "dir" && heldBefore.has(path) && !holdsNow.has(path)).map(([path]) => path), signal);
|
|
188
|
+
if (unreadable.length > 0) {
|
|
189
|
+
const shown = unreadable.slice(0, 3).map((dir) => `${dir}/`);
|
|
190
|
+
return fail("refused", `the run's folder holds ${shown.join(", ")}${unreadable.length > shown.length ? ", ..." : ""}, which the computer's user can't read, so nothing is synced until it can (chmod u+rx)`);
|
|
191
|
+
}
|
|
192
|
+
// What changed: a type that differs is removed first, with what the manifest holds under it.
|
|
193
|
+
const retyped = [...listing].filter(([path, entry]) => manifest.has(path) && manifest.get(path)?.type !== entry.type).map(([path]) => path);
|
|
194
|
+
const early = new Set([...manifest.keys()].filter((path) => retyped.some((changed) => path === changed || under(path, changed))));
|
|
195
|
+
const removed = [...manifest.keys()].filter((path) => !listing.has(path) && !early.has(path)).sort(deepestFirst);
|
|
196
|
+
for (const path of [...early].sort(deepestFirst)) {
|
|
197
|
+
const known = manifest.get(path);
|
|
198
|
+
if (known.type === "symlink")
|
|
199
|
+
manifest.delete(path);
|
|
200
|
+
else
|
|
201
|
+
await this.#remove(path, known.type, counts, failed);
|
|
202
|
+
}
|
|
203
|
+
// A path whose old entry is still there keeps it in the manifest, and nothing is written there or under it, so the
|
|
204
|
+
// next sync removes it again first.
|
|
205
|
+
const stuck = retyped.filter((path) => manifest.has(path));
|
|
206
|
+
const open = (path) => !stuck.some((dir) => path === dir || under(path, dir));
|
|
207
|
+
const newDirs = new Set([...listing].filter(([path, entry]) => entry.type === "dir" && manifest.get(path)?.type !== "dir" && open(path)).map(([path]) => path));
|
|
208
|
+
const due = (path) => manifest.get(path)?.settled === false && at - (this.#racy.get(path) ?? Number.POSITIVE_INFINITY) >= RACY_MS;
|
|
209
|
+
const files = [...listing].filter(([path, entry]) => {
|
|
210
|
+
if (entry.type !== "file" || !open(path))
|
|
211
|
+
return false;
|
|
212
|
+
const known = manifest.get(path);
|
|
213
|
+
return known?.type !== "file" || known.size !== entry.size || known.mtimeMs !== entry.mtimeMs || due(path);
|
|
214
|
+
});
|
|
215
|
+
// A new directory whose parent is no new one is pulled whole, as one tar, where it holds a file.
|
|
216
|
+
const trees = [...newDirs].filter((dir) => !newDirs.has(parentOf(dir)) && files.some(([path]) => under(path, dir)));
|
|
217
|
+
const inTree = (path) => trees.some((dir) => under(path, dir));
|
|
218
|
+
// The bytes this sync may still read: each body takes its size before it is held, so a file grown since the listing
|
|
219
|
+
// never makes a sync hold more than SYNC_MAX_BYTES.
|
|
220
|
+
let left = SYNC_MAX_BYTES;
|
|
221
|
+
const take = (bytes) => {
|
|
222
|
+
if (bytes > left)
|
|
223
|
+
return false;
|
|
224
|
+
left -= bytes;
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
// The puts, IN_FLIGHT pulls at once and IN_FLIGHT puts at once; each advances its entry once it answered.
|
|
228
|
+
const pulls = new Slots(IN_FLIGHT);
|
|
229
|
+
const puts = new Slots(IN_FLIGHT);
|
|
230
|
+
// Settled: read again at least RACY_MS after the listing that found it racy, as it was then. Bytes that are the ones
|
|
231
|
+
// last put there are not put again.
|
|
232
|
+
const landed = (path, entry, sha256) => {
|
|
233
|
+
const known = manifest.get(path);
|
|
234
|
+
const same = known?.type === "file" && known.size === entry.size && known.mtimeMs === entry.mtimeMs;
|
|
235
|
+
manifest.set(path, { ...knownOf(entry, same && due(path)), sha256 });
|
|
236
|
+
this.#racy.delete(path);
|
|
237
|
+
};
|
|
238
|
+
const put = async (path, body, entry) => {
|
|
239
|
+
const sha256 = createHash("sha256").update(body).digest("hex");
|
|
240
|
+
if (manifest.get(path)?.sha256 === sha256)
|
|
241
|
+
return landed(path, entry, sha256);
|
|
242
|
+
const wrote = await this.#sink.put(path, body).catch((error) => fail("refused", error instanceof Error ? error.message : String(error)));
|
|
243
|
+
if (!wrote.ok)
|
|
244
|
+
return void failed(wrote.error.code, `${path}: ${wrote.error.message}`);
|
|
245
|
+
counts.put++;
|
|
246
|
+
landed(path, entry, sha256);
|
|
247
|
+
};
|
|
248
|
+
const tasks = [];
|
|
249
|
+
for (const dir of trees) {
|
|
250
|
+
const wanted = new Map(files.filter(([path]) => under(path, dir)));
|
|
251
|
+
tasks.push(pulls.run(() => this.#pullTree(dir, wanted, puts, put, failed, take, signal)));
|
|
252
|
+
}
|
|
253
|
+
for (const [path, entry] of files) {
|
|
254
|
+
if (inTree(path))
|
|
255
|
+
continue;
|
|
256
|
+
tasks.push(pulls.run(() => this.#pullFile(path, entry, put, failed, take, signal)));
|
|
257
|
+
}
|
|
258
|
+
await Promise.all(tasks);
|
|
259
|
+
// Directories and links carry no bytes. An empty directory is recorded (new, or emptied since: a file or directory
|
|
260
|
+
// under it in the manifest and none in the listing). A new one advances once something records it: its own mkdir,
|
|
261
|
+
// or a file or directory under it that advanced (deepest first, so a directory's are known before it). A file of a
|
|
262
|
+
// new tree that did not land is read on its own at the next sync, and its directory is new again until then.
|
|
263
|
+
const ancestors = (path) => {
|
|
264
|
+
const all = [];
|
|
265
|
+
for (let dir = parentOf(path); dir !== ""; dir = parentOf(dir))
|
|
266
|
+
all.push(dir);
|
|
267
|
+
return all;
|
|
268
|
+
};
|
|
269
|
+
const recorded = new Set([...manifest].filter(([, known]) => known.type !== "symlink").flatMap(([path]) => ancestors(path)));
|
|
270
|
+
for (const [path, entry] of listing)
|
|
271
|
+
if (entry.type === "symlink" && open(path))
|
|
272
|
+
manifest.set(path, knownOf(entry, true));
|
|
273
|
+
const dirs = [...listing].filter(([path, entry]) => entry.type === "dir" && open(path)).sort(([a], [b]) => deepestFirst(a, b));
|
|
274
|
+
for (const [path, entry] of dirs) {
|
|
275
|
+
const isNew = newDirs.has(path);
|
|
276
|
+
let made = false;
|
|
277
|
+
if (!holdsNow.has(path) && (isNew || heldBefore.has(path))) {
|
|
278
|
+
const wrote = await this.#sink.mkdir(path).catch((error) => fail("refused", error instanceof Error ? error.message : String(error)));
|
|
279
|
+
if (!wrote.ok) {
|
|
280
|
+
failed(wrote.error.code, `${path}/: ${wrote.error.message}`);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
counts.mkdir++;
|
|
284
|
+
made = true;
|
|
285
|
+
}
|
|
286
|
+
if (isNew && (made || recorded.has(path))) {
|
|
287
|
+
manifest.set(path, knownOf(entry, true));
|
|
288
|
+
for (const dir of [path, ...ancestors(path)])
|
|
289
|
+
recorded.add(dir);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Deletes, once every put landed: a moved file is never missing from both places.
|
|
293
|
+
if (failures.length === 0) {
|
|
294
|
+
for (const path of removed) {
|
|
295
|
+
const known = manifest.get(path);
|
|
296
|
+
if (known.type === "symlink")
|
|
297
|
+
manifest.delete(path);
|
|
298
|
+
else
|
|
299
|
+
await this.#remove(path, known.type, counts, failed);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// The modes and links last, once everything else landed, where they differ from the last written.
|
|
303
|
+
const meta = metaOf(listing);
|
|
304
|
+
const json = JSON.stringify(meta);
|
|
305
|
+
if (failures.length === 0 && json !== this.#meta) {
|
|
306
|
+
const wrote = await this.#sink.meta(meta).catch((error) => fail("refused", error instanceof Error ? error.message : String(error)));
|
|
307
|
+
if (wrote.ok) {
|
|
308
|
+
this.#meta = json;
|
|
309
|
+
counts.meta = true;
|
|
310
|
+
}
|
|
311
|
+
else
|
|
312
|
+
failed(wrote.error.code, `the folder's modes and links: ${wrote.error.message}`);
|
|
313
|
+
}
|
|
314
|
+
// The racy rule: a file within RACY_MS of the newest listed, not yet read that long after, is pulled again later.
|
|
315
|
+
const newest = Math.max(0, ...[...listing.values()].filter((entry) => entry.type === "file").map((entry) => entry.mtimeMs));
|
|
316
|
+
for (const [path, entry] of listing) {
|
|
317
|
+
const known = manifest.get(path);
|
|
318
|
+
if (entry.type === "file" && entry.mtimeMs >= newest - RACY_MS && known?.settled === false && !this.#racy.has(path))
|
|
319
|
+
this.#racy.set(path, at);
|
|
320
|
+
}
|
|
321
|
+
for (const path of this.#racy.keys())
|
|
322
|
+
if (!listing.has(path))
|
|
323
|
+
this.#racy.delete(path);
|
|
324
|
+
if (failures.length === 0)
|
|
325
|
+
return ok(counts);
|
|
326
|
+
const [first] = failures;
|
|
327
|
+
const shown = failures.slice(0, 3).map(({ words }) => words);
|
|
328
|
+
return fail(first.code, `${failures.length} of the folder's changes did not land, and are sent again at the next sync: ${shown.join("; ")}${failures.length > shown.length ? "; ..." : ""}`);
|
|
329
|
+
}
|
|
330
|
+
async #remove(path, type, counts, failed) {
|
|
331
|
+
const gone = await this.#sink.remove(path, type).catch((error) => fail("refused", error instanceof Error ? error.message : String(error)));
|
|
332
|
+
if (!gone.ok) {
|
|
333
|
+
failed(gone.error.code, `${path}${type === "dir" ? "/" : ""}: ${gone.error.message}`);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
counts.removed++;
|
|
337
|
+
this.#manifest.delete(path);
|
|
338
|
+
}
|
|
339
|
+
/** Of `dirs`, the ones a files.list of their own refuses: unreadable, so what the listing left out of them is unknown. */
|
|
340
|
+
async #unreadable(dirs, signal) {
|
|
341
|
+
const slots = new Slots(IN_FLIGHT);
|
|
342
|
+
const listed = await Promise.all(dirs.map((dir) => slots.run(() => this.#client.call("files.list", { path: `${this.#root}/${dir}`, depth: 1, offset: 0 }, signal ? { signal } : {}).then((page) => page.ok, () => false))));
|
|
343
|
+
return dirs.filter((_, index) => !listed[index]);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* One changed file: one files.read where it was listed at MAX_CHUNK_BYTES or less and reads whole in it, else a
|
|
347
|
+
* tar.pull of it, whose header gives its size before its bytes come. Each body takes its bytes (`take`) before it is
|
|
348
|
+
* kept; one that would pass what is left of the sync's bytes fails, kind limit.
|
|
349
|
+
*/
|
|
350
|
+
async #pullFile(path, entry, put, failed, take, signal) {
|
|
351
|
+
const at = `${this.#root}/${path}`;
|
|
352
|
+
const over = `${path}: it grew since the listing past what is left of the ${SYNC_MAX_BYTES} bytes a sync reads`;
|
|
353
|
+
if (entry.size <= MAX_CHUNK_BYTES) {
|
|
354
|
+
const read = await this.#client.call("files.read", { path: at, offset: 0, length: MAX_CHUNK_BYTES }, signal ? { signal } : {});
|
|
355
|
+
if (!read.ok)
|
|
356
|
+
return void failed(read.error.code, `${path}: ${read.error.message}`);
|
|
357
|
+
const body = Buffer.from(read.value.data, "base64");
|
|
358
|
+
// One that grew past a chunk since the listing is read as a tar, below.
|
|
359
|
+
if (read.value.eof || body.length === 0)
|
|
360
|
+
return take(body.length) ? put(path, body, entry) : void failed("limit", over);
|
|
361
|
+
}
|
|
362
|
+
const pulled = pullTarStream(this.#client, at, signal ? { signal } : {});
|
|
363
|
+
let body;
|
|
364
|
+
let refused = false;
|
|
365
|
+
const taking = (bytes) => take(bytes) || ((refused = true), false);
|
|
366
|
+
try {
|
|
367
|
+
for await (const one of untarStream(pulled.chunks, { take: taking }))
|
|
368
|
+
if (one.type === "file" && body === undefined)
|
|
369
|
+
body = one.body ?? new Uint8Array();
|
|
370
|
+
}
|
|
371
|
+
catch (error) {
|
|
372
|
+
const done = await pulled.done;
|
|
373
|
+
if (refused)
|
|
374
|
+
return void failed("limit", over);
|
|
375
|
+
return void failed(done.ok ? "refused" : done.error.code, `${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
376
|
+
}
|
|
377
|
+
if (body === undefined)
|
|
378
|
+
return void failed("refused", `${path}: its tar held no file`);
|
|
379
|
+
return put(path, body, entry);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* A new directory's tree as one tar.pull: each file of it the listing has (`wanted`) put as it comes. Each entry takes
|
|
383
|
+
* its bytes (`take`) at its header, before they are held; one past what is left of the sync's bytes fails it, kind limit.
|
|
384
|
+
*/
|
|
385
|
+
async #pullTree(dir, wanted, puts, put, failed, take, signal) {
|
|
386
|
+
const pulled = pullTarStream(this.#client, `${this.#root}/${dir}`, signal ? { signal } : {});
|
|
387
|
+
const writing = [];
|
|
388
|
+
let refused = false;
|
|
389
|
+
const taking = (bytes) => take(bytes) || ((refused = true), false);
|
|
390
|
+
try {
|
|
391
|
+
for await (const one of untarStream(pulled.chunks, { take: taking })) {
|
|
392
|
+
const name = one.name.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "");
|
|
393
|
+
if (one.type !== "file" || name === "")
|
|
394
|
+
continue;
|
|
395
|
+
const path = `${dir}/${name}`;
|
|
396
|
+
const entry = wanted.get(path);
|
|
397
|
+
// Only a file the listing has, of a confined path: one made since is the next sync's.
|
|
398
|
+
if (entry === undefined || folderPathProblem(path) !== undefined)
|
|
399
|
+
continue;
|
|
400
|
+
const release = await puts.acquire();
|
|
401
|
+
writing.push(put(path, one.body ?? new Uint8Array(), entry).finally(release));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
await Promise.all(writing);
|
|
406
|
+
const done = await pulled.done;
|
|
407
|
+
if (refused)
|
|
408
|
+
return void failed("limit", `${dir}/: its files grew since the listing past what is left of the ${SYNC_MAX_BYTES} bytes a sync reads`);
|
|
409
|
+
return void failed(done.ok ? "refused" : done.error.code, `${dir}/: ${error instanceof Error ? error.message : String(error)}`);
|
|
410
|
+
}
|
|
411
|
+
// A file listed but not in the tar was removed or replaced since: the next sync sees what it is now.
|
|
412
|
+
await Promise.all(writing);
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* The folder's entries by their path relative to it: files.list from the root, MAX_LIST_DEPTH deep, its pages
|
|
416
|
+
* pipelined (the next asked for at the last page's count, and asked again where the daemon cut a page short), and
|
|
417
|
+
* listed again from each directory at the depth limit. A path the relative-path rule refuses fails the sync in words;
|
|
418
|
+
* an entry neither file, directory nor link (a socket, a fifo) is not the folder's. Over a cap, it fails, kind limit.
|
|
419
|
+
*/
|
|
420
|
+
async #list(signal) {
|
|
421
|
+
const listing = new Map();
|
|
422
|
+
let bytes = 0;
|
|
423
|
+
const queue = [this.#root];
|
|
424
|
+
for (let dir = queue.shift(); dir !== undefined; dir = queue.shift()) {
|
|
425
|
+
const base = dir === this.#root ? 0 : depthOf(dir.slice(this.#root.length + 1));
|
|
426
|
+
const pages = await this.#pages(dir, SYNC_MAX_ENTRIES - listing.size, signal);
|
|
427
|
+
if (!pages.ok)
|
|
428
|
+
return pages;
|
|
429
|
+
for (const entry of pages.value) {
|
|
430
|
+
if (!entry.path.startsWith(`${this.#root}/`))
|
|
431
|
+
return fail("refused", `files.list of ${dir} gave ${JSON.stringify(entry.path)}, which is not in ${this.#root}`);
|
|
432
|
+
const path = entry.path.slice(this.#root.length + 1);
|
|
433
|
+
const problem = folderPathProblem(path);
|
|
434
|
+
if (problem !== undefined)
|
|
435
|
+
return fail("refused", `the run's folder holds ${JSON.stringify(path)}, which ${problem}, so no sink can keep it: rename or remove it`);
|
|
436
|
+
if (entry.type === "other")
|
|
437
|
+
continue;
|
|
438
|
+
listing.set(path, entry);
|
|
439
|
+
if (entry.type === "file")
|
|
440
|
+
bytes += entry.size;
|
|
441
|
+
if (entry.type === "dir" && depthOf(path) - base === MAX_LIST_DEPTH)
|
|
442
|
+
queue.push(entry.path);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (bytes > SYNC_MAX_BYTES)
|
|
446
|
+
return fail("limit", `the run's folder holds ${bytes} bytes, more than ${SYNC_MAX_BYTES}, the most a synced folder holds`);
|
|
447
|
+
return ok(listing);
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Every entry files.list gives below dir, MAX_LIST_DEPTH deep. Past a truncated page, the next is asked for while the
|
|
451
|
+
* one before is in flight, at the last page's count (the daemon may cut a page short of MAX_LIST_ENTRIES by bytes):
|
|
452
|
+
* one asked for where the count read so far is not is dropped, and asked for again there. More than `room` entries
|
|
453
|
+
* (what the folder's cap leaves) fails at once, kind limit, so no listing past the cap is held.
|
|
454
|
+
*/
|
|
455
|
+
async #pages(dir, room, signal) {
|
|
456
|
+
const entries = [];
|
|
457
|
+
const ask = (offset) => this.#client.call("files.list", { path: dir, depth: MAX_LIST_DEPTH, offset }, signal ? { signal } : {});
|
|
458
|
+
let current = ask(0);
|
|
459
|
+
let next;
|
|
460
|
+
const drop = async () => {
|
|
461
|
+
await next?.page.catch(() => undefined);
|
|
462
|
+
next = undefined;
|
|
463
|
+
};
|
|
464
|
+
for (;;) {
|
|
465
|
+
const page = await current;
|
|
466
|
+
if (!page.ok) {
|
|
467
|
+
await drop();
|
|
468
|
+
return fail(page.error.code, `files.list of ${dir} at ${entries.length}: ${page.error.message}`);
|
|
469
|
+
}
|
|
470
|
+
entries.push(...page.value.entries);
|
|
471
|
+
if (entries.length > room) {
|
|
472
|
+
await drop();
|
|
473
|
+
return fail("limit", TOO_MANY);
|
|
474
|
+
}
|
|
475
|
+
if (!page.value.truncated) {
|
|
476
|
+
await drop();
|
|
477
|
+
return ok(entries);
|
|
478
|
+
}
|
|
479
|
+
// Each truncated page holds an entry at least, so the room bounds how many pages there are.
|
|
480
|
+
if (page.value.entries.length === 0) {
|
|
481
|
+
await drop();
|
|
482
|
+
return fail("refused", `files.list of ${dir} at ${entries.length} gave a truncated page with no entries`);
|
|
483
|
+
}
|
|
484
|
+
if (next !== undefined && next.offset === entries.length)
|
|
485
|
+
current = next.page;
|
|
486
|
+
else {
|
|
487
|
+
await drop();
|
|
488
|
+
current = ask(entries.length);
|
|
489
|
+
}
|
|
490
|
+
const guess = Math.min(page.value.entries.length, MAX_LIST_ENTRIES);
|
|
491
|
+
next = { offset: entries.length + guess, page: ask(entries.length + guess) };
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
package/dist/tar.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface TarEntry {
|
|
2
|
+
name: string;
|
|
3
|
+
type: "file" | "dir" | "symlink" | "other";
|
|
4
|
+
mode: number;
|
|
5
|
+
body?: Uint8Array;
|
|
6
|
+
link?: string;
|
|
7
|
+
}
|
|
8
|
+
/** One entry's blocks: a pax header first where its name or link does not fit ustar's 100 bytes, then its own, its body and the padding. */
|
|
9
|
+
export declare function tarEntry(entry: TarEntry): Buffer[];
|
|
10
|
+
/** A whole archive of these entries. */
|
|
11
|
+
export declare function tarOf(entries: readonly TarEntry[]): Buffer;
|
|
12
|
+
/** An archive of these entries as it is made: each entry's blocks as it comes, then the end. */
|
|
13
|
+
export declare function tarStream(entries: AsyncIterable<TarEntry> | Iterable<TarEntry>): AsyncGenerator<Uint8Array>;
|
|
14
|
+
/**
|
|
15
|
+
* An archive read as its bytes come: push() takes a chunk and answers the entries it finished, each whole. Bytes are
|
|
16
|
+
* joined only once an entry's are all here, so a large body is copied once. Throws in words on a header whose checksum
|
|
17
|
+
* is wrong; finish() throws where the bytes end inside an entry. `take`, where given, is asked for each entry's size at
|
|
18
|
+
* its header, before its bytes are held: false throws in words, so a reader never holds more than `take` allows.
|
|
19
|
+
*/
|
|
20
|
+
export declare class TarReader {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(options?: {
|
|
23
|
+
take?: (bytes: number) => boolean;
|
|
24
|
+
});
|
|
25
|
+
push(chunk: Uint8Array): TarEntry[];
|
|
26
|
+
/** Throws in words where the bytes ended inside an entry; a short tail of no whole header, or none, is the end. */
|
|
27
|
+
finish(): void;
|
|
28
|
+
}
|
|
29
|
+
/** Every entry of an archive, in order. Throws in words on an archive it cannot read. */
|
|
30
|
+
export declare function untar(bytes: Uint8Array): TarEntry[];
|
|
31
|
+
/** The entries of an archive as its chunks come, each whole (`take` as TarReader's). Throws in words on an archive it cannot read. */
|
|
32
|
+
export declare function untarStream(chunks: AsyncIterable<Uint8Array>, options?: {
|
|
33
|
+
take?: (bytes: number) => boolean;
|
|
34
|
+
}): AsyncGenerator<TarEntry>;
|