@rljson/fs-agent 0.0.85 → 0.2.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/README.api.md +383 -0
- package/README.architecture.md +483 -288
- package/README.contributors.md +101 -15
- package/README.md +41 -11
- package/README.public.md +409 -796
- package/README.tests.md +460 -0
- package/README.trouble.md +70 -8
- package/dist/README.api.md +383 -0
- package/dist/README.architecture.md +483 -288
- package/dist/README.contributors.md +101 -15
- package/dist/README.md +41 -11
- package/dist/README.public.md +409 -796
- package/dist/README.tests.md +460 -0
- package/dist/README.trouble.md +70 -8
- package/dist/fs-agent.d.ts +703 -426
- package/dist/fs-agent.js +2801 -1584
- package/dist/fs-atomic-write.d.ts +46 -0
- package/dist/fs-edit-chain.d.ts +368 -0
- package/dist/fs-ignore.d.ts +32 -0
- package/dist/fs-merge.d.ts +109 -0
- package/dist/fs-receive.d.ts +83 -0
- package/dist/fs-scanner.d.ts +169 -6
- package/dist/fs-send.d.ts +53 -0
- package/dist/fs-signals.d.ts +169 -0
- package/dist/fs-stamps.d.ts +94 -0
- package/dist/index.d.ts +5 -2
- package/package.json +11 -11
- package/dist/fs-anti-entropy.d.ts +0 -146
- package/dist/fs-conflict-resolver.d.ts +0 -181
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filename prefix for the staging files used by atomic writes.
|
|
3
|
+
*
|
|
4
|
+
* The scanner ignores anything starting with this, so the transient
|
|
5
|
+
* temp-and-rename never pollutes a tree or churns the watcher. See
|
|
6
|
+
* `fs-ignore.ts`, where the prefix-matching rule exists partly to keep this
|
|
7
|
+
* working.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ATOMIC_TMP_PREFIX = ".fsagent-tmp-";
|
|
10
|
+
/**
|
|
11
|
+
* A staging path beside `filePath`, in the same directory.
|
|
12
|
+
*
|
|
13
|
+
* Same directory so the rename stays within one filesystem and is therefore
|
|
14
|
+
* atomic. The random suffix is what keeps two concurrent writers of one path
|
|
15
|
+
* out of each other's staging file — without it they would trample the temp
|
|
16
|
+
* instead of the target, which is the same bug one step removed.
|
|
17
|
+
* @param filePath - The eventual destination.
|
|
18
|
+
* @returns A path to stage into.
|
|
19
|
+
*/
|
|
20
|
+
export declare const atomicTmpPath: (filePath: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Writes bytes to `filePath` through a staging file.
|
|
23
|
+
*
|
|
24
|
+
* We do not `fsync` the temp: it adds significant per-file latency under
|
|
25
|
+
* bursty restores, and durability-on-power-loss is secondary here since the
|
|
26
|
+
* content is replicated and re-synced.
|
|
27
|
+
* @param filePath - Destination path.
|
|
28
|
+
* @param content - Bytes to write.
|
|
29
|
+
*/
|
|
30
|
+
export declare const atomicWriteFile: (filePath: string, content: Buffer | string) => Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Writes a stream to `filePath` through a staging file, one chunk at a time.
|
|
33
|
+
*
|
|
34
|
+
* Never materialising the whole file is the point: a 500 MB document used to
|
|
35
|
+
* cost 500 MB of Buffer on the receiving agent, another copy in the socket
|
|
36
|
+
* parser, and the same again on the serving hub — memory that is work in
|
|
37
|
+
* flight rather than garbage, so no collection can reclaim any of it. That is
|
|
38
|
+
* the shape that exhausted a cloud relay.
|
|
39
|
+
* @param filePath - Destination path.
|
|
40
|
+
* @param stream - The bytes.
|
|
41
|
+
* @param wrapReadError - Optional: given a failure that came from READING the
|
|
42
|
+
* stream rather than writing the file, returns the error to throw instead.
|
|
43
|
+
* Callers that must tell those two apart downstream pass a tagger here;
|
|
44
|
+
* without one the original error is thrown unchanged.
|
|
45
|
+
*/
|
|
46
|
+
export declare const atomicWriteStream: (filePath: string, stream: ReadableStream<Uint8Array>, wrapReadError?: (error: unknown) => Error) => Promise<void>;
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { Db } from '@rljson/db';
|
|
2
|
+
import { Json } from '@rljson/json';
|
|
3
|
+
/** The `EditAction.type` every fs chain entry carries. */
|
|
4
|
+
export declare const FS_EDIT_ACTION = "putFsTree";
|
|
5
|
+
/**
|
|
6
|
+
* How far back a walk goes before it gives up.
|
|
7
|
+
*
|
|
8
|
+
* A walk this deep is a cold replay of a whole lineage rather than a
|
|
9
|
+
* catch-up, and left unbounded it pins a core on a long chain.
|
|
10
|
+
*
|
|
11
|
+
* Exhausting it is NOT the same as failing to read a row — see
|
|
12
|
+
* {@link FsEditChain.relate}. Conflating the two made any node more than this
|
|
13
|
+
* many pushes into its own history stop repairing, permanently and silently.
|
|
14
|
+
*/
|
|
15
|
+
export declare const DEFAULT_MAX_WALK = 500;
|
|
16
|
+
/** What one chain entry says happened. */
|
|
17
|
+
export interface FsEditData extends Json {
|
|
18
|
+
/** The tree ref the folder ended up at. */
|
|
19
|
+
treeRef: string;
|
|
20
|
+
/** Relative paths added or modified, `/`-separated, sorted. */
|
|
21
|
+
changed: string[];
|
|
22
|
+
/**
|
|
23
|
+
* Relative paths REMOVED, `/`-separated, sorted.
|
|
24
|
+
*
|
|
25
|
+
* The whole reason the chain carries more than a ref. A tree records what a
|
|
26
|
+
* folder holds; only this records what it deliberately stopped holding.
|
|
27
|
+
*/
|
|
28
|
+
removed: string[];
|
|
29
|
+
}
|
|
30
|
+
/** What {@link FsEditChain.append} records. */
|
|
31
|
+
export interface FsAppendOptions {
|
|
32
|
+
/** The state the folder ended up at. */
|
|
33
|
+
treeRef: string;
|
|
34
|
+
/** Relative paths added or modified. Default: none. */
|
|
35
|
+
changed?: readonly string[];
|
|
36
|
+
/** Relative paths removed. Default: none. */
|
|
37
|
+
removed?: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* The entry's `timeId`, when it has to be a function of its content: two
|
|
40
|
+
* nodes that merge the same two heads into the same tree write the same
|
|
41
|
+
* entry. Minted otherwise; a root is identified by its tree.
|
|
42
|
+
*/
|
|
43
|
+
timeId?: string;
|
|
44
|
+
/**
|
|
45
|
+
* What this entry was made from: none for a root, the own head for an edit,
|
|
46
|
+
* both heads for a merge — the one shape `MultiEditManager` refuses and the
|
|
47
|
+
* reason these rows are written by hand.
|
|
48
|
+
*/
|
|
49
|
+
previous: readonly string[];
|
|
50
|
+
}
|
|
51
|
+
/** One entry of the chain, as read back. */
|
|
52
|
+
export interface FsChainEntry {
|
|
53
|
+
/** The `EditHistory` ref — this entry's identity, and the head to announce. */
|
|
54
|
+
head: string;
|
|
55
|
+
/** Fleet-wide order. Minted once, by the node that made the change. */
|
|
56
|
+
timeId: string;
|
|
57
|
+
/** The tree ref the folder ended up at. */
|
|
58
|
+
treeRef: string;
|
|
59
|
+
/** The entries this one was made from. Empty for a lineage root. */
|
|
60
|
+
previous: string[];
|
|
61
|
+
/** Relative paths added or modified. */
|
|
62
|
+
changed: string[];
|
|
63
|
+
/** Relative paths removed. */
|
|
64
|
+
removed: string[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Creates the three tables an fs chain needs, idempotently.
|
|
68
|
+
*
|
|
69
|
+
* **fs-agent creates its own tables.** The host application creates the trees
|
|
70
|
+
* table
|
|
71
|
+
* (in its own server and client entry points), and an agent that expected
|
|
72
|
+
* tables its host had never created would fail at runtime on any node whose
|
|
73
|
+
* host is one release behind — exactly the mixed-version case a rollout
|
|
74
|
+
* guarantees. `@rljson/db`'s `createTable` is `createOrExtendTable`, so calling
|
|
75
|
+
* this on every init costs nothing and removes the coupling rather than
|
|
76
|
+
* versioning it.
|
|
77
|
+
* @param db - The route's database.
|
|
78
|
+
* @param treeKey - The trees table key, e.g. `fileTree`.
|
|
79
|
+
*/
|
|
80
|
+
export declare const createFsChainTables: (db: Db, treeKey: string) => Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* The append-only chain for one folder.
|
|
83
|
+
*
|
|
84
|
+
* Append-only and never merged: each node keeps its own lineage, exactly as
|
|
85
|
+
* mongo does. Nothing here rewrites or prunes an entry, and nothing collects
|
|
86
|
+
* one either — the chain grows for the life of the folder. See
|
|
87
|
+
* `README.public.md`, "Known constraints".
|
|
88
|
+
*/
|
|
89
|
+
export declare class FsEditChain {
|
|
90
|
+
private readonly _db;
|
|
91
|
+
private readonly _treeKey;
|
|
92
|
+
/** The last own-lineage walk, keyed by the head it was computed for. */
|
|
93
|
+
private _lineageCache;
|
|
94
|
+
constructor(_db: Db, _treeKey: string);
|
|
95
|
+
/**
|
|
96
|
+
* Creates the chain's tables if absent. Where this node stands is not the
|
|
97
|
+
* chain's business: a table several nodes write has no tip of its own, and
|
|
98
|
+
* the agent keeps its head in its state file.
|
|
99
|
+
*/
|
|
100
|
+
init(): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Appends one entry.
|
|
103
|
+
* @param opts - What the change was; see {@link FsAppendOptions}.
|
|
104
|
+
* @returns The entry that was written.
|
|
105
|
+
*/
|
|
106
|
+
append(opts: FsAppendOptions): Promise<FsChainEntry>;
|
|
107
|
+
/**
|
|
108
|
+
* Reads one entry back.
|
|
109
|
+
* @param head - The `EditHistory` ref to read.
|
|
110
|
+
* @returns The entry, or `undefined` when this node cannot resolve it — a
|
|
111
|
+
* ref a peer holds and we do not is the ordinary case, not an error.
|
|
112
|
+
*/
|
|
113
|
+
entry(head: string): Promise<FsChainEntry | undefined>;
|
|
114
|
+
/**
|
|
115
|
+
* How two heads stand, and for a fork the ancestor to merge against.
|
|
116
|
+
*
|
|
117
|
+
* `same` — the one head. `behind` — theirs descends from ours. `ahead` —
|
|
118
|
+
* ours descends from theirs. A descent found is proof, whatever else in
|
|
119
|
+
* either history cannot be read. `fork` — neither descends from the other:
|
|
120
|
+
* `base` is a common ancestor no other common ancestor descends from, the
|
|
121
|
+
* smallest ref of them when there are several, so every node merging the
|
|
122
|
+
* same two heads merges against the same base. `undefined` when the two
|
|
123
|
+
* share no history within the walk. `incomplete` — no descent was found and
|
|
124
|
+
* an entry could not be read, so a fork cannot be told from a lag.
|
|
125
|
+
*
|
|
126
|
+
* A walk that exhausts `maxWalk` with everything readable answers `fork`:
|
|
127
|
+
* less precise than the truth, never destructive.
|
|
128
|
+
* @param ours - This node's head.
|
|
129
|
+
* @param theirs - The head that arrived.
|
|
130
|
+
* @param maxWalk - Give up past this many entries per side.
|
|
131
|
+
* @returns The relation, and the merge base of a fork.
|
|
132
|
+
*/
|
|
133
|
+
relate(ours: string, theirs: string, maxWalk?: number): Promise<{
|
|
134
|
+
verdict: 'same' | 'behind' | 'ahead' | 'incomplete';
|
|
135
|
+
} | {
|
|
136
|
+
verdict: 'fork';
|
|
137
|
+
base: string | undefined;
|
|
138
|
+
}>;
|
|
139
|
+
/**
|
|
140
|
+
* The `previous` of every entry reachable from `head`, by generation.
|
|
141
|
+
* @param head - Where to start.
|
|
142
|
+
* @param maxWalk - Give up past this many entries.
|
|
143
|
+
* @returns Each entry read, mapped to its parents, and whether an entry
|
|
144
|
+
* could not be read.
|
|
145
|
+
*/
|
|
146
|
+
private _parentsOf;
|
|
147
|
+
/**
|
|
148
|
+
* Every entry reachable from `head`, itself included.
|
|
149
|
+
*
|
|
150
|
+
* `holed` — an entry could not be RESOLVED. The caller must conclude
|
|
151
|
+
* nothing: the unreachable part may contain the very ref it was looking for,
|
|
152
|
+
* and a walk that answers "not an ancestor" on that basis is how a node
|
|
153
|
+
* decides it is ahead of a peer it is actually behind.
|
|
154
|
+
*
|
|
155
|
+
* `bounded` — the walk ran out of budget with everything it asked for
|
|
156
|
+
* readable. A different statement, and a usable one: no relation within this
|
|
157
|
+
* many entries of history. See {@link relate}.
|
|
158
|
+
* @param head - Where to start.
|
|
159
|
+
* @param maxWalk - Give up past this many entries.
|
|
160
|
+
* @returns The refs reached, whether anything was unreadable, and whether
|
|
161
|
+
* the budget ran out.
|
|
162
|
+
*/
|
|
163
|
+
private _ancestorsOf;
|
|
164
|
+
/**
|
|
165
|
+
* Every entry this node's own head descends from, itself included.
|
|
166
|
+
*
|
|
167
|
+
* Cached on the head it was computed for. Announcements arrive in bursts and
|
|
168
|
+
* this node's head changes only when it records an entry, so the walk is
|
|
169
|
+
* done once per state rather than once per announcement.
|
|
170
|
+
* @param ourHead - This node's head, or `undefined` when it has none.
|
|
171
|
+
* @param maxWalk - Give up past this many entries.
|
|
172
|
+
* @returns The refs reached, or `undefined` when the walk could not be
|
|
173
|
+
* trusted — an unreadable entry means the stop set is unknown, not empty.
|
|
174
|
+
*/
|
|
175
|
+
private _ourLineage;
|
|
176
|
+
/**
|
|
177
|
+
* The newest edit on this lineage that CHANGED or REMOVED a given path.
|
|
178
|
+
*
|
|
179
|
+
* **The per-path question, and the one the conflict resolver could not ask.**
|
|
180
|
+
* `compareTips` orders a BRANCH by its tip, and the caller then uses that one
|
|
181
|
+
* verdict for every path the two branches disagree about — so a tip wins
|
|
182
|
+
* paths it never touched. Measured: a node that had been offline came back,
|
|
183
|
+
* wrote one file of its own, and that newer tip took `doc.txt` as well,
|
|
184
|
+
* rolling the writer's own folder from v8 back to v5.
|
|
185
|
+
*
|
|
186
|
+
* Asking per path answers it exactly: the last edit naming `doc.txt` is the
|
|
187
|
+
* writer's, and the returning node's entry does not name it at all, so it
|
|
188
|
+
* does not count for it. No clock is involved — the answer is read out of the
|
|
189
|
+
* shared history, so every node computes the same winner on its own.
|
|
190
|
+
*
|
|
191
|
+
* **A removal counts as touching the path.** An edit/delete conflict is still
|
|
192
|
+
* two people acting on one file, and the one who acted later decides it.
|
|
193
|
+
*
|
|
194
|
+
* `undefined` means "cannot say": either nothing on this lineage ever named
|
|
195
|
+
* the path, or the walk could not be read to the end. The caller must not
|
|
196
|
+
* read that as "nobody edited it" — it falls back to the branch-level order.
|
|
197
|
+
* @param head - The lineage's tip.
|
|
198
|
+
* @param path - The relative path being decided.
|
|
199
|
+
* @param maxWalk - Give up past this many entries.
|
|
200
|
+
* @returns The newest entry naming `path`, or `undefined`.
|
|
201
|
+
*/
|
|
202
|
+
lastEditOf(head: string, path: string, maxWalk?: number): Promise<FsChainEntry | undefined>;
|
|
203
|
+
/**
|
|
204
|
+
* The NET removals between a peer's head and a state this node knows.
|
|
205
|
+
*
|
|
206
|
+
* **Why a walk is needed at all, and it cost a red run to see.** A removal is
|
|
207
|
+
* stated ONCE, in the entry that made it. A node that was partitioned when it
|
|
208
|
+
* deleted a file states the removal in an entry nobody received; its next
|
|
209
|
+
* entry, after it rejoins, computes its `changed`/`removed` against its own
|
|
210
|
+
* last announcement and therefore says nothing about that deletion. So a peer
|
|
211
|
+
* reading only the head learns nothing, and the file survives on every node
|
|
212
|
+
* except the one that deleted it — which is the measured failure.
|
|
213
|
+
*
|
|
214
|
+
* Walking `previous` back to a state the receiver recognises closes exactly
|
|
215
|
+
* that gap, and the ancestry is reachable by hash alone (proven in
|
|
216
|
+
* `test/fs-chain-crosses-the-wire.spec.ts`).
|
|
217
|
+
*
|
|
218
|
+
* **Replayed oldest-first, so a re-add cancels a removal.** A path deleted
|
|
219
|
+
* and then created again inside the walked range is not a removal at all,
|
|
220
|
+
* and taking the union of `removed` would delete it. Order is the whole
|
|
221
|
+
* correctness of this function.
|
|
222
|
+
*
|
|
223
|
+
* **`complete` is mongo's contract, and it matters more than the result.**
|
|
224
|
+
* When an entry cannot be resolved the walk is truncated, and the caller MUST
|
|
225
|
+
* NOT treat the answer as authoritative — ancestors beyond the missing row
|
|
226
|
+
* are unknown, and one of them may be the re-add that cancels a removal we
|
|
227
|
+
* did collect. `@rljson/mongo-agent` states it as: *"a caller must not latch
|
|
228
|
+
* the head; ancestors beyond the missing row would otherwise be lost
|
|
229
|
+
* forever"*.
|
|
230
|
+
* **Where the walk stops is THIS NODE'S OWN LINEAGE, asked of the chain.**
|
|
231
|
+
* It used to be a set of content refs: the current one, the last applied one,
|
|
232
|
+
* and a thousand remembered past states. That set approximated "everywhere I
|
|
233
|
+
* have been" — capped, so a long-lived node forgot its oldest states, and
|
|
234
|
+
* matched BY CONTENT, so an entry on a lineage this node never travelled
|
|
235
|
+
* stopped the walk whenever it happened to produce bytes this node also
|
|
236
|
+
* holds. Identical content across nodes was rare while mtime was in the
|
|
237
|
+
* identity; now it is the norm.
|
|
238
|
+
*
|
|
239
|
+
* Neither is a measured data loss — stopping early collects FEWER removals,
|
|
240
|
+
* which is the safe direction, and content equality does mean the folder was
|
|
241
|
+
* in that state. It is a heuristic standing in for a question the chain can
|
|
242
|
+
* answer exactly, which is the whole reason the chain exists.
|
|
243
|
+
* @param head - The peer's head.
|
|
244
|
+
* @param ourHead - This node's own head, or `undefined` when it has none —
|
|
245
|
+
* a node with no history of its own recognises no state, so nothing stops
|
|
246
|
+
* the walk short of `maxWalk`.
|
|
247
|
+
* @param maxWalk - Give up past this many entries. A walk this deep is a
|
|
248
|
+
* cold replay rather than a catch-up, and left unbounded it pins a core on
|
|
249
|
+
* a long chain.
|
|
250
|
+
* **Both halves of the net delta are returned**: a path deleted and created
|
|
251
|
+
* again inside the walked range is a change, not a removal, and only the
|
|
252
|
+
* second half can say so.
|
|
253
|
+
* @returns The net removals, the net changes, and whether the walk resolved
|
|
254
|
+
* completely.
|
|
255
|
+
*/
|
|
256
|
+
collectRemovals(head: string, ourHead: string | undefined, maxWalk?: number): Promise<{
|
|
257
|
+
removed: string[];
|
|
258
|
+
changed: string[];
|
|
259
|
+
complete: boolean;
|
|
260
|
+
}>;
|
|
261
|
+
/**
|
|
262
|
+
* Pulls the generated ref out of an insert result.
|
|
263
|
+
* @param result - What `db.add*` returned.
|
|
264
|
+
* @param suffix - The table suffix, e.g. `Edits`.
|
|
265
|
+
* @returns The ref.
|
|
266
|
+
*/
|
|
267
|
+
private _refOf;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Above this many files, a removal covering the WHOLE folder is refused.
|
|
271
|
+
*
|
|
272
|
+
* The user's rule — *protect whenever ALL files would vanish* — against the
|
|
273
|
+
* reason the ratio guard has a floor at all: a folder of one or four files
|
|
274
|
+
* loses all of them as ordinary work, and so does a rename, which removes
|
|
275
|
+
* every path it holds and adds them back under new names.
|
|
276
|
+
*
|
|
277
|
+
* Ten is the line between those two facts. Below it, "the whole folder" is a
|
|
278
|
+
* handful of files and emptying it is an edit; above it, one peer's loss
|
|
279
|
+
* taking everybody's copy is the measured failure
|
|
280
|
+
* (`a small folder survives a wiped peer too`, 40 files).
|
|
281
|
+
*
|
|
282
|
+
* It is a refusal and not a question, because the agent has no one to ask.
|
|
283
|
+
* The decision called for *"protect, and ask"*; the asking belongs to a host
|
|
284
|
+
* that can show a user a dialogue, and until one does this errs towards
|
|
285
|
+
* keeping files.
|
|
286
|
+
*/
|
|
287
|
+
export declare const ALL_GONE_MIN_FILES = 10;
|
|
288
|
+
/** What {@link planJoin} is asked. */
|
|
289
|
+
export interface JoinQuestion {
|
|
290
|
+
/**
|
|
291
|
+
* The hub head's tree, `path → content hash`.
|
|
292
|
+
*
|
|
293
|
+
* Materialised VIRTUALLY — nothing is written until the plan says so. The
|
|
294
|
+
* point of the whole exercise is to decide before touching the folder.
|
|
295
|
+
*/
|
|
296
|
+
head: ReadonlyMap<string, string>;
|
|
297
|
+
/** This folder as it stands, `path → content hash`. */
|
|
298
|
+
folder: ReadonlyMap<string, string>;
|
|
299
|
+
/**
|
|
300
|
+
* Paths the history states were REMOVED and never re-added.
|
|
301
|
+
*
|
|
302
|
+
* The net removals over the WHOLE history — `collectRemovals` with no stop
|
|
303
|
+
* state, which walks to the root and lets a re-add cancel a removal in
|
|
304
|
+
* order. This is the one fact that separates a stale copy from new work, and
|
|
305
|
+
* no filesystem can supply it.
|
|
306
|
+
*/
|
|
307
|
+
removedEver: ReadonlySet<string>;
|
|
308
|
+
/**
|
|
309
|
+
* Whether a head was found at all.
|
|
310
|
+
*
|
|
311
|
+
* No head means no history exists anywhere, so this folder IS the origin and
|
|
312
|
+
* its contents are its first state. Absence of a head is NOT the same as an
|
|
313
|
+
* empty head: an empty head is a fleet that has agreed the folder is empty,
|
|
314
|
+
* and its emptiness is a fact to be applied.
|
|
315
|
+
*/
|
|
316
|
+
haveHead: boolean;
|
|
317
|
+
}
|
|
318
|
+
/** What {@link planJoin} concluded, per path. */
|
|
319
|
+
export interface JoinPlan {
|
|
320
|
+
/** In the head and missing or differing here — write the head's bytes. */
|
|
321
|
+
write: string[];
|
|
322
|
+
/**
|
|
323
|
+
* Here, unknown to the history — new local work. Announce it.
|
|
324
|
+
*
|
|
325
|
+
* A file nobody has ever mentioned cannot be a deletion anybody made, and
|
|
326
|
+
* dropping it is how a node loses its own work on joining.
|
|
327
|
+
*/
|
|
328
|
+
announce: string[];
|
|
329
|
+
/**
|
|
330
|
+
* Here, and REMOVED by the history — a stale copy. Rename aside; do NOT
|
|
331
|
+
* announce.
|
|
332
|
+
*
|
|
333
|
+
* The restored-backup case. Announcing these would push content the fleet
|
|
334
|
+
* deliberately deleted back to every node, one file at a time, under names
|
|
335
|
+
* nobody deleted — for a folder restored from last month's backup that is
|
|
336
|
+
* thousands of files. So the bytes are kept where their owner can see them
|
|
337
|
+
* and nothing is said about them. **That is a deliberate, visible local
|
|
338
|
+
* divergence**, chosen over resurrecting a deletion or destroying a file.
|
|
339
|
+
*/
|
|
340
|
+
recover: string[];
|
|
341
|
+
/**
|
|
342
|
+
* In the head AND here with different bytes — a real conflict.
|
|
343
|
+
*
|
|
344
|
+
* The case the stated algorithm does not name, because it is neither
|
|
345
|
+
* "missing here" nor "additional here": the path is live on both sides and
|
|
346
|
+
* was edited while this node was away. The head's bytes are written (the
|
|
347
|
+
* chain wins on what it states) and the local bytes are kept aside as an
|
|
348
|
+
* ordinary conflict copy, which IS announced — an edit to a live path is
|
|
349
|
+
* exactly what conflict copies exist for.
|
|
350
|
+
*/
|
|
351
|
+
conflict: string[];
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Decides what joining a network does to a folder, before anything is written.
|
|
355
|
+
*
|
|
356
|
+
* **The chain applies first; the filesystem only then.** A joining node used to
|
|
357
|
+
* author a lineage root from whatever it happened to hold and push it as the
|
|
358
|
+
* network's newest claim. That is one defect wearing two faces: every node got
|
|
359
|
+
* its own lineage root, so `relate` answered `fork` to every announcement
|
|
360
|
+
* ever made; and a node restored from a backup pushed deleted files back to the
|
|
361
|
+
* whole fleet.
|
|
362
|
+
*
|
|
363
|
+
* Every question here is answered against the chain. The folder is consulted
|
|
364
|
+
* only for what it holds — never for what that means.
|
|
365
|
+
* @param q - The head, the folder, and what the history says was removed.
|
|
366
|
+
* @returns The per-path plan; every path appears in at most one list.
|
|
367
|
+
*/
|
|
368
|
+
export declare const planJoin: (q: JoinQuestion) => JoinPlan;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translates a glob into an anchored, case-insensitive regular expression.
|
|
3
|
+
*
|
|
4
|
+
* `*` and `?` stop at a separator, `**` crosses them. A `**` followed by `/`
|
|
5
|
+
* also matches ZERO directories, so `**\/x` matches `x` at the root as well as
|
|
6
|
+
* at any depth — the one piece of gitignore's behaviour that is not obvious
|
|
7
|
+
* from the characters.
|
|
8
|
+
* @param glob - The pattern, separators already normalised to `/`.
|
|
9
|
+
* @returns A regular expression matching the whole string.
|
|
10
|
+
*/
|
|
11
|
+
export declare const globToRegExp: (glob: string) => RegExp;
|
|
12
|
+
/** Decides whether a path is ignored. Compiled once, asked many times. */
|
|
13
|
+
export interface IgnoreMatcher {
|
|
14
|
+
/**
|
|
15
|
+
* @param relativePath - Path relative to the sync root, either separator.
|
|
16
|
+
* @param isDirectory - Pass it when known; a directory-only pattern needs it.
|
|
17
|
+
* @returns True when the sync must not see this path.
|
|
18
|
+
*/
|
|
19
|
+
ignores(relativePath: string, isDirectory?: boolean): boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Compiles an ignore list into a matcher.
|
|
23
|
+
*
|
|
24
|
+
* **Order matters, and the LAST match decides** — the gitignore rule, and the
|
|
25
|
+
* only one under which `!` can mean anything: `['*.log', '!keep.log']` keeps
|
|
26
|
+
* `keep.log` and `['!keep.log', '*.log']` does not. One caveat inherited with
|
|
27
|
+
* the semantics: the scanner does not descend into an ignored directory, so a
|
|
28
|
+
* `!` line cannot rescue a file inside one.
|
|
29
|
+
* @param patterns - The ignore list, in the order it was written.
|
|
30
|
+
* @returns A matcher over those patterns.
|
|
31
|
+
*/
|
|
32
|
+
export declare const compileIgnore: (patterns: readonly string[] | undefined) => IgnoreMatcher;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { RefStamp } from '@rljson/rljson';
|
|
2
|
+
/** `path → blobId`, with {@link DIR_MARKER} for a directory. */
|
|
3
|
+
export type ContentMap = ReadonlyMap<string, string>;
|
|
4
|
+
/** The value a directory has in a {@link ContentMap}. */
|
|
5
|
+
export declare const DIR_MARKER = "<dir>";
|
|
6
|
+
/** Who introduced one side's version of a path, and when it reached the fleet. */
|
|
7
|
+
export interface Introducer {
|
|
8
|
+
/** The chain entry that introduced the version. */
|
|
9
|
+
entry: string;
|
|
10
|
+
/** Its stamp, or `undefined` while nobody has told this node. */
|
|
11
|
+
stamp: RefStamp | undefined;
|
|
12
|
+
/** The node that authored the entry. Names a conflicted copy. */
|
|
13
|
+
nodeId: string;
|
|
14
|
+
}
|
|
15
|
+
/** One side of a merge. */
|
|
16
|
+
export interface MergeSide {
|
|
17
|
+
/** The side's tree. */
|
|
18
|
+
content: ContentMap;
|
|
19
|
+
/** Who introduced this side's version of `path`. */
|
|
20
|
+
introducer: (path: string) => Introducer;
|
|
21
|
+
}
|
|
22
|
+
/** A conflict the merge settled, and the signal it owes. */
|
|
23
|
+
export interface MergeConflict {
|
|
24
|
+
/** `conflict/arbitrary` for C3 and C4, `collision/type` for C5. */
|
|
25
|
+
kind: 'conflict/arbitrary' | 'collision/type';
|
|
26
|
+
/** The contested path. */
|
|
27
|
+
path: string;
|
|
28
|
+
/** Where the version that lost the name was kept, when one was. */
|
|
29
|
+
copyPath?: string;
|
|
30
|
+
}
|
|
31
|
+
/** What {@link merge3} decided. */
|
|
32
|
+
export interface MergeOutcome {
|
|
33
|
+
/** The merged tree, conflicted copies included. */
|
|
34
|
+
merged: Map<string, string>;
|
|
35
|
+
/** Every conflict the merge settled — one signal each. */
|
|
36
|
+
conflicts: MergeConflict[];
|
|
37
|
+
/**
|
|
38
|
+
* Paths whose C3 verdict needs a stamp this node does not know yet. When
|
|
39
|
+
* non-empty the caller must not apply `merged`: it defers and signals
|
|
40
|
+
* `repair/blocked`, and merges again once the stamp has arrived.
|
|
41
|
+
*/
|
|
42
|
+
unknownStamps: string[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Merges two concurrent versions of a folder against their common ancestor.
|
|
46
|
+
*
|
|
47
|
+
* Per path, with `a` the ancestor's value and `b`/`c` the two sides':
|
|
48
|
+
*
|
|
49
|
+
* | rows | situation | outcome |
|
|
50
|
+
* | --- | --- | --- |
|
|
51
|
+
* | C1/C2 | one side changed, or both changed it the same way | that value |
|
|
52
|
+
* | C3 | both changed one file differently | both kept; the version whose introducing entry has the higher stamp keeps the name |
|
|
53
|
+
* | C4 | one side deleted, the other edited | the edit stays; the deletion is reported, not applied |
|
|
54
|
+
* | C5 | a directory on one side, a file on the other | both kept: the directory keeps the path, the file becomes a conflicted copy |
|
|
55
|
+
*
|
|
56
|
+
* Nothing compares two machines' clocks, and no version is ever dropped.
|
|
57
|
+
* @param base - The common ancestor's tree.
|
|
58
|
+
* @param ours - This node's side.
|
|
59
|
+
* @param theirs - The other side.
|
|
60
|
+
* @returns The merged tree, the conflicts it settled, and the paths it could
|
|
61
|
+
* not decide yet.
|
|
62
|
+
*/
|
|
63
|
+
export declare const merge3: (base: ContentMap, ours: MergeSide, theirs: MergeSide) => MergeOutcome;
|
|
64
|
+
/**
|
|
65
|
+
* The name a version that lost a contested path is kept under:
|
|
66
|
+
* `<stem> (conflicted copy <hub>-<n> <nodeId>)<ext>`, leaving out what is not
|
|
67
|
+
* known — the stamp, the node — and numbered when taken.
|
|
68
|
+
*
|
|
69
|
+
* A function of the loser's author and stamp only, so every node that settles
|
|
70
|
+
* the same conflict picks the same name.
|
|
71
|
+
* @param path - The contested path.
|
|
72
|
+
* @param nodeId - The node that introduced the losing version.
|
|
73
|
+
* @param stamp - Its stamp, when the name should carry it.
|
|
74
|
+
* @param taken - Paths already in use; the chosen name is added.
|
|
75
|
+
* @returns The copy's path.
|
|
76
|
+
*/
|
|
77
|
+
export declare const copyName: (path: string, nodeId: string, stamp: RefStamp | undefined, taken: Set<string>) => string;
|
|
78
|
+
/**
|
|
79
|
+
* The name a joining folder's own version of a path is set aside under when
|
|
80
|
+
* the network holds a different one, numbered when taken:
|
|
81
|
+
*
|
|
82
|
+
* `<stem> (conflicted copy <UTC time> local)<ext>`
|
|
83
|
+
*
|
|
84
|
+
* No stamp can order it — the version never reached the fleet — so the name
|
|
85
|
+
* says when it was set aside and that it is this machine's.
|
|
86
|
+
* @param path - The path.
|
|
87
|
+
* @param at - When it was set aside, epoch ms.
|
|
88
|
+
* @param taken - Paths already in use; the chosen name is added.
|
|
89
|
+
* @returns The copy's path.
|
|
90
|
+
*/
|
|
91
|
+
export declare const joinCopyName: (path: string, at: number, taken: Set<string>) => string;
|
|
92
|
+
/**
|
|
93
|
+
* The name a file is set aside under when the history DELETED its path:
|
|
94
|
+
* `<stem> (recovered)<ext>`, numbered when taken.
|
|
95
|
+
*
|
|
96
|
+
* **Not a conflict copy, and the difference is who is told.** A conflict copy
|
|
97
|
+
* is live work on a live path and is announced, because the network needs it.
|
|
98
|
+
* A recovered file is content the fleet deliberately removed — a folder
|
|
99
|
+
* restored from last month's backup, or a client that was away while a
|
|
100
|
+
* directory was deleted — and announcing it would push every one of those
|
|
101
|
+
* deletions back to every node, under names nobody deleted. So it is kept
|
|
102
|
+
* where its owner can see it and nothing is said about it. No time and no
|
|
103
|
+
* identity: the path is simply gone from the history, and one name per path is
|
|
104
|
+
* what a user can act on.
|
|
105
|
+
* @param path - The path the history removed.
|
|
106
|
+
* @param taken - Paths already in use; the chosen name is added.
|
|
107
|
+
* @returns The set-aside path.
|
|
108
|
+
*/
|
|
109
|
+
export declare const recoveredName: (path: string, taken: Set<string>) => string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** How two heads stand — see `FsEditChain.relate`. */
|
|
2
|
+
export type Relation = {
|
|
3
|
+
verdict: 'same' | 'behind' | 'ahead' | 'incomplete';
|
|
4
|
+
} | {
|
|
5
|
+
verdict: 'fork';
|
|
6
|
+
base: string | undefined;
|
|
7
|
+
};
|
|
8
|
+
/** What {@link receive} needs from the agent. */
|
|
9
|
+
export interface ReceiveDeps {
|
|
10
|
+
/** This node's head, or `undefined` before it has one. */
|
|
11
|
+
ownHead(): string | undefined;
|
|
12
|
+
/** Authors whatever the folder holds beyond the own head, first. */
|
|
13
|
+
flushLocal(): Promise<void>;
|
|
14
|
+
/** How the own head and an arrived head stand. */
|
|
15
|
+
relate(ours: string, theirs: string): Promise<Relation>;
|
|
16
|
+
/** The tree ref a head names, or `undefined` when it cannot be read. */
|
|
17
|
+
treeRefOf(head: string): Promise<string | undefined>;
|
|
18
|
+
/**
|
|
19
|
+
* Writes a head's tree into the folder, deleting what it lacks, and makes it
|
|
20
|
+
* this node's head. `refused` when it could not — nothing was adopted then.
|
|
21
|
+
*/
|
|
22
|
+
applyBehind(head: string): Promise<'applied' | 'refused'>;
|
|
23
|
+
/** Makes `head` — one state under another name — this node's head. */
|
|
24
|
+
adopt(head: string): Promise<void>;
|
|
25
|
+
/** Announces this node's head again. */
|
|
26
|
+
announceOwn(): void;
|
|
27
|
+
/** Merges a forked head into this node's; see `merge3`. */
|
|
28
|
+
merge(theirs: string, base: string | undefined): Promise<'merged' | 'deferred' | 'refused'>;
|
|
29
|
+
/** Says that nothing could be decided about `head`. */
|
|
30
|
+
blocked(head: string, reason: string): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether this node agrees with the head the hub said last, and what receiving
|
|
34
|
+
* it made this node do — `FsAgent.antiEntropyStatus`.
|
|
35
|
+
*/
|
|
36
|
+
export interface AntiEntropyStatus {
|
|
37
|
+
/** Whether this node's head was not the hub's after receiving it. */
|
|
38
|
+
diverged: boolean;
|
|
39
|
+
/** Since when, epoch ms by this node's clock. */
|
|
40
|
+
divergedSince: number | null;
|
|
41
|
+
/** The head the hub said last. */
|
|
42
|
+
hubRef: string | null;
|
|
43
|
+
/** This node's head now. */
|
|
44
|
+
localRef: string | null;
|
|
45
|
+
/** How often the hub's head made this node act. */
|
|
46
|
+
repairs: number;
|
|
47
|
+
/**
|
|
48
|
+
* What it did last: `pull` applied or adopted it, `push` announced this
|
|
49
|
+
* node's own, `merge` merged the two, `blocked` could decide nothing.
|
|
50
|
+
*/
|
|
51
|
+
lastRepair: {
|
|
52
|
+
action: 'pull' | 'push' | 'merge' | 'blocked';
|
|
53
|
+
at: number;
|
|
54
|
+
hubRef: string;
|
|
55
|
+
localRef: string;
|
|
56
|
+
/** How often in a row the same hub head made it act. */
|
|
57
|
+
attempt: number;
|
|
58
|
+
} | null;
|
|
59
|
+
}
|
|
60
|
+
/** What {@link receive} did. */
|
|
61
|
+
export type ReceiveOutcome = 'no-head' | 'in-sync' | 'applied' | 'refused' | 'announced' | 'adopted' | 'merged' | 'deferred' | 'blocked';
|
|
62
|
+
/**
|
|
63
|
+
* What a node does with a head that arrived.
|
|
64
|
+
*
|
|
65
|
+
* The chain is the only order:
|
|
66
|
+
*
|
|
67
|
+
* | relation | action |
|
|
68
|
+
* | --- | --- |
|
|
69
|
+
* | `behind` — theirs descends from ours | apply it, deleting what it lacks, and adopt it |
|
|
70
|
+
* | `ahead` — ours descends from theirs | announce ours |
|
|
71
|
+
* | `fork`, same content | converge on the smaller ref, on every node |
|
|
72
|
+
* | `fork` | merge against the base |
|
|
73
|
+
* | `incomplete` | nothing; say so |
|
|
74
|
+
*
|
|
75
|
+
* Deleting on `behind` is correct by construction: whoever made the head held
|
|
76
|
+
* this node's state and chose not to have the file. Work this node has not
|
|
77
|
+
* authored yet is authored first, so it can never be overwritten by a head
|
|
78
|
+
* that never saw it — it turns a `behind` into a `fork`.
|
|
79
|
+
* @param head - The head that arrived.
|
|
80
|
+
* @param deps - The agent.
|
|
81
|
+
* @returns What was done.
|
|
82
|
+
*/
|
|
83
|
+
export declare const receive: (head: string, deps: ReceiveDeps) => Promise<ReceiveOutcome>;
|