@kamilio/wire 0.1.56 → 0.1.57

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.
Files changed (124) hide show
  1. package/README.md +44 -1
  2. package/dist/adapters/document-shell.d.ts +7 -0
  3. package/dist/adapters/document-shell.d.ts.map +1 -0
  4. package/dist/adapters/document-shell.js +51 -0
  5. package/dist/adapters/document-shell.js.map +1 -0
  6. package/dist/adapters/root.d.ts.map +1 -1
  7. package/dist/adapters/root.js +69 -4
  8. package/dist/adapters/root.js.map +1 -1
  9. package/dist/cli.d.ts.map +1 -1
  10. package/dist/cli.js +8 -2
  11. package/dist/cli.js.map +1 -1
  12. package/dist/document-shell-engine.mjs +1880 -0
  13. package/dist/executable.js +1 -1
  14. package/dist/executable.js.map +1 -1
  15. package/dist/hooks.d.ts.map +1 -1
  16. package/dist/hooks.js +19 -19
  17. package/dist/hooks.js.map +1 -1
  18. package/node_modules/provider-asana/dist/asana-project.d.ts +7 -1
  19. package/node_modules/provider-asana/dist/asana-project.d.ts.map +1 -1
  20. package/node_modules/provider-asana/dist/asana-task.d.ts +7 -1
  21. package/node_modules/provider-asana/dist/asana-task.d.ts.map +1 -1
  22. package/node_modules/provider-asana/dist/index.d.ts +14 -2
  23. package/node_modules/provider-asana/dist/index.d.ts.map +1 -1
  24. package/node_modules/provider-chatgpt/dist/chatgpt.d.ts +7 -1
  25. package/node_modules/provider-chatgpt/dist/chatgpt.d.ts.map +1 -1
  26. package/node_modules/provider-chatgpt/dist/index.d.ts +7 -1
  27. package/node_modules/provider-chatgpt/dist/index.d.ts.map +1 -1
  28. package/node_modules/provider-gmail/dist/gmail.d.ts +7 -1
  29. package/node_modules/provider-gmail/dist/gmail.d.ts.map +1 -1
  30. package/node_modules/provider-gmail/dist/index.d.ts +7 -1
  31. package/node_modules/provider-gmail/dist/index.d.ts.map +1 -1
  32. package/node_modules/provider-google-docs/dist/doc-blocks.d.ts.map +1 -1
  33. package/node_modules/provider-google-docs/dist/doc-blocks.js +6 -2
  34. package/node_modules/provider-google-docs/dist/doc-blocks.js.map +1 -1
  35. package/node_modules/provider-google-docs/dist/google-docs.d.ts +7 -1
  36. package/node_modules/provider-google-docs/dist/google-docs.d.ts.map +1 -1
  37. package/node_modules/provider-google-docs/dist/google-docs.js +31 -10
  38. package/node_modules/provider-google-docs/dist/google-docs.js.map +1 -1
  39. package/node_modules/provider-google-docs/dist/google-forms.d.ts +7 -1
  40. package/node_modules/provider-google-docs/dist/google-forms.d.ts.map +1 -1
  41. package/node_modules/provider-google-docs/dist/index.d.ts +14 -2
  42. package/node_modules/provider-google-docs/dist/index.d.ts.map +1 -1
  43. package/node_modules/provider-google-docs/src/doc-blocks.ts +8 -2
  44. package/node_modules/provider-google-docs/src/google-docs.ts +26 -10
  45. package/node_modules/provider-lattice/dist/index.d.ts +14 -2
  46. package/node_modules/provider-lattice/dist/index.d.ts.map +1 -1
  47. package/node_modules/provider-notion/dist/index.d.ts +7 -1
  48. package/node_modules/provider-notion/dist/index.d.ts.map +1 -1
  49. package/node_modules/provider-notion/dist/notion-sync.d.ts +2 -2
  50. package/node_modules/provider-notion/dist/notion-sync.d.ts.map +1 -1
  51. package/node_modules/provider-notion/dist/notion-sync.js +24 -15
  52. package/node_modules/provider-notion/dist/notion-sync.js.map +1 -1
  53. package/node_modules/provider-notion/dist/notion.d.ts +7 -1
  54. package/node_modules/provider-notion/dist/notion.d.ts.map +1 -1
  55. package/node_modules/provider-notion/dist/notion.js +1 -1
  56. package/node_modules/provider-notion/dist/notion.js.map +1 -1
  57. package/node_modules/provider-notion/src/notion-sync.ts +23 -15
  58. package/node_modules/provider-notion/src/notion.ts +1 -1
  59. package/node_modules/provider-slack/dist/index.d.ts +7 -1
  60. package/node_modules/provider-slack/dist/index.d.ts.map +1 -1
  61. package/node_modules/provider-slack/dist/slack.d.ts +7 -1
  62. package/node_modules/provider-slack/dist/slack.d.ts.map +1 -1
  63. package/node_modules/provider-zoom/dist/index.d.ts +7 -1
  64. package/node_modules/provider-zoom/dist/index.d.ts.map +1 -1
  65. package/node_modules/provider-zoom/dist/zoom-hub.d.ts +7 -1
  66. package/node_modules/provider-zoom/dist/zoom-hub.d.ts.map +1 -1
  67. package/node_modules/wire-core/dist/core/editor-receipts.d.ts +5 -0
  68. package/node_modules/wire-core/dist/core/editor-receipts.d.ts.map +1 -0
  69. package/node_modules/wire-core/dist/core/editor-receipts.js +9 -0
  70. package/node_modules/wire-core/dist/core/editor-receipts.js.map +1 -0
  71. package/node_modules/wire-core/dist/core/model.d.ts +4 -1
  72. package/node_modules/wire-core/dist/core/model.d.ts.map +1 -1
  73. package/node_modules/wire-core/dist/core/owner.d.ts +3 -0
  74. package/node_modules/wire-core/dist/core/owner.d.ts.map +1 -0
  75. package/node_modules/wire-core/dist/core/owner.js +16 -0
  76. package/node_modules/wire-core/dist/core/owner.js.map +1 -0
  77. package/node_modules/wire-core/dist/core/source.d.ts +4 -1
  78. package/node_modules/wire-core/dist/core/source.d.ts.map +1 -1
  79. package/node_modules/wire-core/dist/core/source.js +12 -2
  80. package/node_modules/wire-core/dist/core/source.js.map +1 -1
  81. package/node_modules/wire-core/dist/index.d.ts +1 -1
  82. package/node_modules/wire-core/dist/index.d.ts.map +1 -1
  83. package/node_modules/wire-core/dist/index.js +1 -1
  84. package/node_modules/wire-core/dist/index.js.map +1 -1
  85. package/node_modules/wire-core/dist/operations.d.ts +11 -2
  86. package/node_modules/wire-core/dist/operations.d.ts.map +1 -1
  87. package/node_modules/wire-core/dist/operations.js +399 -31
  88. package/node_modules/wire-core/dist/operations.js.map +1 -1
  89. package/node_modules/wire-core/dist/storage/editor-receipts.d.ts +17 -0
  90. package/node_modules/wire-core/dist/storage/editor-receipts.d.ts.map +1 -0
  91. package/node_modules/wire-core/dist/storage/editor-receipts.js +27 -0
  92. package/node_modules/wire-core/dist/storage/editor-receipts.js.map +1 -0
  93. package/node_modules/wire-core/dist/storage/history-integrity.d.ts +4 -0
  94. package/node_modules/wire-core/dist/storage/history-integrity.d.ts.map +1 -0
  95. package/node_modules/wire-core/dist/storage/history-integrity.js +32 -0
  96. package/node_modules/wire-core/dist/storage/history-integrity.js.map +1 -0
  97. package/node_modules/wire-core/dist/storage/history.d.ts +93 -0
  98. package/node_modules/wire-core/dist/storage/history.d.ts.map +1 -0
  99. package/node_modules/wire-core/dist/storage/history.js +450 -0
  100. package/node_modules/wire-core/dist/storage/history.js.map +1 -0
  101. package/node_modules/wire-core/dist/storage/registry.d.ts +2 -2
  102. package/node_modules/wire-core/dist/storage/registry.d.ts.map +1 -1
  103. package/node_modules/wire-core/dist/storage/registry.js +34 -14
  104. package/node_modules/wire-core/dist/storage/registry.js.map +1 -1
  105. package/node_modules/wire-core/dist/storage/workspace.d.ts.map +1 -1
  106. package/node_modules/wire-core/dist/storage/workspace.js +183 -40
  107. package/node_modules/wire-core/dist/storage/workspace.js.map +1 -1
  108. package/node_modules/wire-core/src/core/editor-receipts.ts +8 -0
  109. package/node_modules/wire-core/src/core/model.ts +4 -1
  110. package/node_modules/wire-core/src/core/owner.ts +16 -0
  111. package/node_modules/wire-core/src/core/source.ts +13 -1
  112. package/node_modules/wire-core/src/index.ts +1 -1
  113. package/node_modules/wire-core/src/operations.ts +339 -33
  114. package/node_modules/wire-core/src/storage/editor-receipts.ts +27 -0
  115. package/node_modules/wire-core/src/storage/history-integrity.ts +25 -0
  116. package/node_modules/wire-core/src/storage/history.ts +331 -0
  117. package/node_modules/wire-core/src/storage/registry.ts +22 -15
  118. package/node_modules/wire-core/src/storage/workspace.ts +129 -34
  119. package/package.json +6 -2
  120. package/src/adapters/document-shell.ts +43 -0
  121. package/src/adapters/root.ts +71 -4
  122. package/src/cli.ts +8 -2
  123. package/src/executable.ts +1 -1
  124. package/src/hooks.ts +19 -18
@@ -0,0 +1,450 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { constants, linkSync, readFileSync, renameSync, lstatSync } from "node:fs";
3
+ import { hostname } from "node:os";
4
+ import { basename, dirname, join, resolve } from "node:path";
5
+ import { link, lstat, mkdir, open, readdir, readlink, rename, rm, stat, symlink, unlink } from "node:fs/promises";
6
+ import { editorView, hasEditorReceipt, plainMarkdown } from "./editor-receipts.js";
7
+ import { transactionId, validateRefs } from "./history-integrity.js";
8
+ import { stableJsonCompact } from "../core/json.js";
9
+ const hash = (value) => createHash("sha256").update(value).digest("hex");
10
+ const code = (error) => error.code;
11
+ const missing = (error) => code(error) === "ENOENT";
12
+ async function exists(path) { return lstat(path).then(() => true, e => { if (missing(e))
13
+ return false; throw e; }); }
14
+ async function privateDirectory(path) {
15
+ try {
16
+ await mkdir(path, { mode: 0o700 });
17
+ }
18
+ catch (e) {
19
+ if (code(e) !== "EEXIST")
20
+ throw e;
21
+ }
22
+ const info = await lstat(path);
23
+ if (!info.isDirectory() || (info.mode & 0o077) !== 0)
24
+ throw new Error('Unsafe Wire history directory: ' + path);
25
+ }
26
+ export async function requireHistoryVolume(historyDirectory, markdownDirectory) {
27
+ const [history, markdown] = await Promise.all([stat(historyDirectory), stat(markdownDirectory)]);
28
+ if (history.dev !== markdown.dev)
29
+ throw new Error("Wire history and tracked Markdown must be on the same filesystem. Run wire init in the document folder on that volume, then attach the source URL there. Keep any existing edits until you have reconciled the new attachment.");
30
+ }
31
+ async function syncDir(path) {
32
+ const fd = await open(path, "r");
33
+ try {
34
+ await fd.sync();
35
+ }
36
+ finally {
37
+ await fd.close();
38
+ }
39
+ }
40
+ async function write(path, value, mode = 0o600) {
41
+ const fd = await open(path, "wx", mode);
42
+ try {
43
+ await fd.writeFile(value);
44
+ await fd.sync();
45
+ }
46
+ finally {
47
+ await fd.close();
48
+ }
49
+ }
50
+ async function json(path, value) {
51
+ const tmp = join(dirname(path), randomUUID());
52
+ try {
53
+ await write(tmp, JSON.stringify(value));
54
+ await rename(tmp, path);
55
+ await syncDir(dirname(path));
56
+ }
57
+ finally {
58
+ await rm(tmp, { force: true });
59
+ }
60
+ }
61
+ export async function working(path) {
62
+ let fd;
63
+ try {
64
+ fd = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
65
+ }
66
+ catch (e) {
67
+ if (missing(e))
68
+ return null;
69
+ throw e;
70
+ }
71
+ try {
72
+ if (!(await fd.stat()).isFile())
73
+ throw new Error("Expected a regular Markdown file: " + path);
74
+ return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(await fd.readFile());
75
+ }
76
+ finally {
77
+ await fd.close();
78
+ }
79
+ }
80
+ const empty = () => ({ base: null, draft: null, remote: null, conflict: null, strict: false, revisions: [], resolution: null, resolutionParent: null });
81
+ // Process-owned exclusive claims. A crash leaves its immutable symlink in place.
82
+ // A contender skips only a confirmed dead, still-identical claim on this host.
83
+ // Never unlink another owner's claim: two independent recoverers cannot steal a lock.
84
+ async function claimState(path) {
85
+ let owner;
86
+ try {
87
+ owner = await readlink(path);
88
+ }
89
+ catch (e) {
90
+ if (missing(e))
91
+ return "missing";
92
+ throw e;
93
+ }
94
+ const [host, pid] = owner.split(":");
95
+ if (host === hostname() && /^\d+$/.test(pid ?? "")) {
96
+ try {
97
+ process.kill(Number(pid), 0);
98
+ }
99
+ catch (e) {
100
+ if (code(e) === "ESRCH" && await readlink(path).catch(() => "") === owner)
101
+ return "dead";
102
+ }
103
+ }
104
+ return "active";
105
+ }
106
+ async function claim(dir, name, run) {
107
+ await privateDirectory(dir);
108
+ const owner = hostname() + ":" + process.pid + ":" + randomUUID();
109
+ for (let n = 0;; n++) {
110
+ const path = join(dir, name + (n ? "." + n : ""));
111
+ try {
112
+ await symlink(owner, path);
113
+ }
114
+ catch (e) {
115
+ if (code(e) !== "EEXIST")
116
+ throw e;
117
+ const state = await claimState(path);
118
+ if (state !== "active") {
119
+ if (state === "missing")
120
+ n--;
121
+ continue;
122
+ }
123
+ throw new Error("Another Wire operation owns this document. Retry when it finishes.");
124
+ }
125
+ try {
126
+ return await run();
127
+ }
128
+ finally {
129
+ await unlink(path);
130
+ }
131
+ }
132
+ }
133
+ async function excludeClaims(dir, names) {
134
+ for (const name of await readdir(dir)) {
135
+ if (names.test(name) && await claimState(join(dir, name)) === "active")
136
+ throw new Error("Wire storage is busy. Retry the backend switch or document operation when it finishes.");
137
+ }
138
+ }
139
+ // Each side posts its claim before inspecting the other side. An operation
140
+ // that has passed its check remains visible until its last durable write.
141
+ export async function withHistoryLock(root, key, run) {
142
+ const dir = join(root, "history");
143
+ return claim(dir, "lock-" + hash(key), async () => {
144
+ await excludeClaims(dir, /^switch(?:\.\d+)?$/);
145
+ return run();
146
+ });
147
+ }
148
+ export async function withBackendSwitch(root, run) {
149
+ const dir = join(root, "history");
150
+ return claim(dir, "switch", async () => {
151
+ await excludeClaims(dir, /^lock-[a-f0-9]{64}(?:\.\d+)?$/);
152
+ return run();
153
+ });
154
+ }
155
+ export class DocumentHistory {
156
+ id;
157
+ now;
158
+ dir;
159
+ refs = empty();
160
+ constructor(root, id, now) {
161
+ this.id = id;
162
+ this.now = now;
163
+ this.dir = join(root, "history", hash(id));
164
+ }
165
+ static async load(root, id, now) {
166
+ const history = new DocumentHistory(root, id, now);
167
+ await privateDirectory(join(root, "history"));
168
+ await privateDirectory(history.dir);
169
+ const raw = await working(join(history.dir, "refs.json"));
170
+ if (raw !== null) {
171
+ history.refs = JSON.parse(raw);
172
+ validateRefs(history.refs);
173
+ }
174
+ return history;
175
+ }
176
+ async save() { await json(join(this.dir, "refs.json"), this.refs); }
177
+ async revision(kind, markdown, parents) {
178
+ plainMarkdown(markdown);
179
+ const blob = hash(markdown), ancestry = [...new Set(parents.filter((p) => p !== null))];
180
+ if (ancestry.length === 1 && this.refs.revisions.find(r => r.id === ancestry[0])?.blob === blob)
181
+ return ancestry[0];
182
+ const id = hash(JSON.stringify([kind, blob, ancestry]));
183
+ if (!this.refs.revisions.some(r => r.id === id)) {
184
+ const path = join(this.dir, blob + ".md");
185
+ if (!await exists(path)) {
186
+ const tmp = join(this.dir, randomUUID());
187
+ try {
188
+ await write(tmp, markdown);
189
+ await rename(tmp, path);
190
+ }
191
+ finally {
192
+ await rm(tmp, { force: true });
193
+ }
194
+ }
195
+ this.refs.revisions.push({ id, kind, at: this.now().toISOString(), blob, parents: ancestry });
196
+ }
197
+ return id;
198
+ }
199
+ token(raw = null) { return hash(stableJsonCompact([this.refs, raw])); }
200
+ view(raw) {
201
+ const view = editorView(raw, this.refs.editorReceipts);
202
+ for (const ref of [view.shown, view.base])
203
+ if (ref && !this.refs.revisions.some(r => r.id === ref))
204
+ throw new Error("Orphaned Wire editor receipt. The file was kept.");
205
+ return view;
206
+ }
207
+ async text(ref) {
208
+ const r = this.refs.revisions.find(r => r.id === ref);
209
+ if (!r)
210
+ throw new Error("Unknown document revision: " + ref);
211
+ const value = await working(join(this.dir, r.blob + ".md"));
212
+ if (value === null || hash(value) !== r.blob)
213
+ throw new Error("Corrupt Wire history snapshot: " + ref);
214
+ return value;
215
+ }
216
+ async log() {
217
+ // An old editor can still write through an open file descriptor after
218
+ // publication. Only an explicit history request scans these retained inodes.
219
+ const known = new Set(this.refs.revisions.map(r => r.blob));
220
+ const count = this.refs.revisions.length;
221
+ for (const name of await readdir(this.dir)) {
222
+ if (!name.endsWith(".old") || !transactionId.test(name.slice(0, -4)))
223
+ continue;
224
+ const path = join(this.dir, name);
225
+ let view;
226
+ try {
227
+ const raw = await working(path);
228
+ if (raw === null)
229
+ continue;
230
+ // A pre-enrollment displaced file has no receipt. Its parent is unknown.
231
+ view = editorView(raw, hasEditorReceipt(raw) ? { ...this.refs.retiredEditorReceipts, ...this.refs.editorReceipts } : undefined);
232
+ if (view.shown && !this.refs.revisions.some(r => r.id === view.shown))
233
+ throw new Error("Orphaned receipt");
234
+ }
235
+ catch (cause) {
236
+ throw new Error("A retained editor save is unreadable. Inspect the private copy: " + path, { cause });
237
+ }
238
+ const blob = hash(view.markdown);
239
+ if (!known.has(blob)) {
240
+ await this.revision("recovered", view.markdown, [view.shown]);
241
+ known.add(blob);
242
+ }
243
+ }
244
+ if (count !== this.refs.revisions.length)
245
+ await this.save();
246
+ return this.refs.revisions.map(({ blob: _, ...entry }) => entry).reverse();
247
+ }
248
+ async seed(markdown) {
249
+ if (this.refs.base === null && markdown !== null) {
250
+ this.refs.base = this.refs.remote = await this.revision("attach", markdown, []);
251
+ await this.save();
252
+ }
253
+ }
254
+ async checkPendingDraft(view) {
255
+ const shell = this.refs.editorDraft;
256
+ if (!shell || !view.shown || !view.base || shell === view.shown || this.refs.conflict)
257
+ return;
258
+ const basis = Object.values(this.refs.editorReceipts ?? {}).find(r => r.shown === shell)?.base;
259
+ if (!basis)
260
+ throw new Error("Missing receipt for the saved shell draft. Both files were kept.");
261
+ const editor = await this.revision("local", view.markdown, [view.shown]);
262
+ this.refs.conflict = { editor, shell, editorBase: view.base, shellBase: basis };
263
+ await this.save();
264
+ }
265
+ commonAncestor(left, right) {
266
+ const graph = new Map(this.refs.revisions.map(r => [r.id, r.parents]));
267
+ const ancestors = (start) => {
268
+ const seen = new Set(), pending = [start];
269
+ while (pending.length) {
270
+ const id = pending.pop();
271
+ if (seen.has(id))
272
+ continue;
273
+ seen.add(id);
274
+ for (const parent of graph.get(id) ?? [])
275
+ pending.push(parent);
276
+ }
277
+ return seen;
278
+ };
279
+ const a = ancestors(left), b = ancestors(right);
280
+ const common = new Set([...a].filter(id => b.has(id)));
281
+ const tips = new Set(common);
282
+ for (const id of common)
283
+ for (const parent of graph.get(id))
284
+ tips.delete(parent);
285
+ return tips.size === 1 ? [...tips][0] : null;
286
+ }
287
+ async captureLocal(markdown, parent = this.refs.draft ?? this.refs.base) {
288
+ this.refs.draft = await this.revision("local", markdown, [parent]);
289
+ await this.save();
290
+ return this.refs.draft;
291
+ }
292
+ async observe(remote) {
293
+ this.refs.remote = await this.revision("remote", remote.markdown, [this.refs.remote ?? this.refs.base]);
294
+ return this.refs.remote;
295
+ }
296
+ async status(path) {
297
+ const base = this.refs.base;
298
+ const raw = await working(path);
299
+ return { path, base, modified: base === null || raw === null || this.view(raw).markdown !== await this.text(base), conflict: this.refs.conflict };
300
+ }
301
+ // Private write-ahead intent spans the worktree, registry, and revision pointers.
302
+ // Moving the actual old inode out first lets us verify what we displaced. link()
303
+ // publishes only into a vacant name; it cannot overwrite a concurrent editor save.
304
+ async publish(path, expected, next, refs, resource, put, acknowledged = false) {
305
+ await requireHistoryVolume(this.dir, dirname(path));
306
+ const token = randomUUID();
307
+ const pending = { path, expected, next, refs, resource, token, acknowledged };
308
+ await json(join(this.dir, "pending.json"), pending);
309
+ await this.finish(pending, put);
310
+ }
311
+ async detach(path, expected, next, refs, detached, remove) {
312
+ await requireHistoryVolume(this.dir, dirname(path));
313
+ const intent = { path, expected, next, refs, detached, resource: null, token: randomUUID(), acknowledged: false };
314
+ await json(join(this.dir, "pending.json"), intent);
315
+ await this.finish(intent, undefined, remove);
316
+ }
317
+ async recover(path, put, remove) {
318
+ const file = join(this.dir, "pending.json");
319
+ const raw = await working(file);
320
+ if (raw === null)
321
+ return;
322
+ const pending = JSON.parse(raw);
323
+ if (!transactionId.test(pending.token) || typeof pending.next !== "string" || (pending.expected !== null && typeof pending.expected !== "string") || typeof pending.acknowledged !== "boolean")
324
+ throw new Error("Corrupt Wire publication journal.");
325
+ validateRefs(pending.refs);
326
+ if (path !== undefined && resolve(path) !== pending.path)
327
+ throw new Error("Wire publication pending at another path: " + pending.path);
328
+ if (pending.detached && (pending.detached.id !== this.id || pending.resource || pending.acknowledged || !remove))
329
+ throw new Error("Corrupt Wire detach journal.");
330
+ await this.finish(pending, put, remove);
331
+ return pending.detached;
332
+ }
333
+ async finish(p, put, remove) {
334
+ const pending = join(this.dir, "pending.json"), stage = join(this.dir, p.token + ".new"), old = join(this.dir, p.token + ".old"), delivered = join(this.dir, p.token + ".ok");
335
+ const cleanup = async () => { await unlink(pending); await syncDir(this.dir); await rm(stage, { force: true }); await rm(delivered, { force: true }); };
336
+ const retry = async () => {
337
+ if (await exists(old)) {
338
+ try {
339
+ await link(old, p.path);
340
+ }
341
+ catch (e) {
342
+ if (code(e) !== "EEXIST")
343
+ throw e;
344
+ }
345
+ if ((await lstat(old)).isFile()) {
346
+ const saved = await working(old).catch((e) => { if (e instanceof TypeError)
347
+ return null; throw e; });
348
+ if (saved !== null && saved !== p.expected) {
349
+ // The displaced inode remains private even when its editor envelope
350
+ // cannot be decoded. Never publish or put envelope bytes in history.
351
+ let view;
352
+ try {
353
+ view = this.view(saved);
354
+ }
355
+ catch { /* retain raw .old */ }
356
+ if (view)
357
+ await this.captureLocal(view.markdown, view.shown ?? undefined);
358
+ }
359
+ }
360
+ }
361
+ await cleanup();
362
+ throw new Error("Local file changed during sync: " + basename(p.path) + ". Local changes were preserved. Run wire sync again.");
363
+ };
364
+ // A private hard link to the displaced inode is retained, even if another
365
+ // application still has a writable file descriptor open on it.
366
+ if (!p.acknowledged && !await exists(delivered)) {
367
+ let current = await working(p.path);
368
+ if (p.expected === null && current !== null) {
369
+ // In a recovered first attachment, only our own hard link proves delivery.
370
+ if (!await exists(stage))
371
+ return retry();
372
+ const staged = await stat(stage), installed = await stat(p.path);
373
+ if (staged.dev !== installed.dev || staged.ino !== installed.ino)
374
+ return retry();
375
+ }
376
+ if (current !== p.next) {
377
+ if (!await exists(stage)) {
378
+ const mode = current === null ? 0o600 : (await stat(p.path)).mode & 0o777;
379
+ const tmp = join(this.dir, randomUUID());
380
+ try {
381
+ await write(tmp, p.next, mode);
382
+ await rename(tmp, stage);
383
+ await syncDir(this.dir);
384
+ }
385
+ finally {
386
+ await rm(tmp, { force: true });
387
+ }
388
+ }
389
+ else if (await working(stage) !== p.next) {
390
+ throw new Error("Corrupt prepared Markdown. No file was published.");
391
+ }
392
+ if (p.expected !== null && !await exists(old)) {
393
+ if (current !== p.expected)
394
+ return retry();
395
+ // Keep the name-vacant interval to the displaced-file check and
396
+ // exclusive link; no event-loop yield or fsync while the name is absent.
397
+ try {
398
+ renameSync(p.path, old);
399
+ }
400
+ catch (e) {
401
+ if (missing(e))
402
+ return retry();
403
+ throw e;
404
+ }
405
+ if (!lstatSync(old).isFile() || !readFileSync(old).equals(Buffer.from(p.expected)))
406
+ return retry();
407
+ try {
408
+ linkSync(stage, p.path);
409
+ }
410
+ catch (e) {
411
+ if (code(e) === "EEXIST")
412
+ return retry();
413
+ throw e;
414
+ }
415
+ }
416
+ else {
417
+ if (await exists(old) && await working(old) !== p.expected)
418
+ return retry();
419
+ current = await working(p.path);
420
+ if (current !== null && current !== p.next)
421
+ return retry();
422
+ if (current === null) {
423
+ try {
424
+ await link(stage, p.path);
425
+ }
426
+ catch (e) {
427
+ if (code(e) === "EEXIST")
428
+ return retry();
429
+ throw e;
430
+ }
431
+ }
432
+ }
433
+ }
434
+ await syncDir(dirname(p.path));
435
+ await write(delivered, "");
436
+ await syncDir(this.dir);
437
+ }
438
+ if (p.resource)
439
+ await put(p.resource);
440
+ this.refs = p.refs;
441
+ await this.save();
442
+ // An unregistered path can be uploaded as a new document. Make refs and
443
+ // plain file durable before removing its registration; retain intent until
444
+ // deletion succeeds so an interrupted detach can finish under the lock.
445
+ if (p.detached)
446
+ await remove(p.detached);
447
+ await cleanup();
448
+ }
449
+ }
450
+ //# sourceMappingURL=history.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"history.js","sourceRoot":"","sources":["../../src/storage/history.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACnF,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB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AAC,CAAC;YAC/B,MAAM,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YAAC,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACtD,CAAC;QACD,IAAI,CAAC,CAAC,QAAQ;YAAE,MAAM,GAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC;QAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QACtC,wEAAwE;QACxE,2EAA2E;QAC3E,wEAAwE;QACxE,IAAI,CAAC,CAAC,QAAQ;YAAE,MAAM,MAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QAC1C,MAAM,OAAO,EAAE,CAAC;IAClB,CAAC;CACF"}
@@ -1,7 +1,7 @@
1
1
  import type { Identifier, Registry, Resource } from "../core/model.js";
2
2
  export declare class SqliteRegistry implements Registry {
3
3
  readonly path: string;
4
- constructor(path: string);
4
+ constructor(path: string, create?: boolean);
5
5
  put(resource: Resource): Promise<Resource>;
6
6
  rekey(previous: Resource, identifier: Identifier): Promise<Resource>;
7
7
  get(resourceId: string): Promise<Resource>;
@@ -15,7 +15,7 @@ export declare class SqliteRegistry implements Registry {
15
15
  }
16
16
  export declare class FileRegistry implements Registry {
17
17
  readonly path: string;
18
- constructor(path: string);
18
+ constructor(path: string, create?: boolean);
19
19
  put(resource: Resource): Promise<Resource>;
20
20
  rekey(previous: Resource, identifier: Identifier): Promise<Resource>;
21
21
  get(resourceId: string): Promise<Resource>;
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/storage/registry.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAyB,QAAQ,EAAE,QAAQ,EAAgB,MAAM,kBAAkB,CAAC;AAkE5G,qBAAa,cAAe,YAAW,QAAQ;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EA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E,MAAM,UAAO;IAIjC,GAAG,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IA2B1C,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC;IAqBpE,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAK1C,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAMxE,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAMzC,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,QAAQ,EAAE,CAAC;IAKtD,aAAa,IAAI,OAAO,CAAC,SAAS,QAAQ,EAAE,CAAC;IAM7C,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/C,OAAO,CAAC,YAAY;CAGrB"}
@@ -53,8 +53,10 @@ function missingUrl(url) {
53
53
  }
54
54
  export class SqliteRegistry {
55
55
  path;
56
- constructor(path) {
57
- this.path = realpathSyncParent(path);
56
+ constructor(path, create = true) {
57
+ this.path = create ? realpathSyncParent(path) : realpathSync(path);
58
+ if (!create)
59
+ return;
58
60
  const database = this.connect();
59
61
  database.exec(`
60
62
  PRAGMA journal_mode=DELETE;
@@ -166,7 +168,7 @@ export class SqliteRegistry {
166
168
  }
167
169
  }
168
170
  async get(resourceId) {
169
- const database = this.connect();
171
+ const database = this.connect(true);
170
172
  const row = database.prepare("SELECT id FROM resources WHERE id = ?").get(resourceId);
171
173
  if (row === undefined) {
172
174
  database.close();
@@ -179,7 +181,7 @@ export class SqliteRegistry {
179
181
  return resource;
180
182
  }
181
183
  async findByIdentifier(service, identifier) {
182
- const database = this.connect();
184
+ const database = this.connect(true);
183
185
  database.exec("BEGIN");
184
186
  const row = database.prepare("SELECT resource_id FROM resource_identifiers WHERE service = ? AND identifier = ?").get(service, identifier);
185
187
  if (row === undefined) {
@@ -192,7 +194,7 @@ export class SqliteRegistry {
192
194
  return resource;
193
195
  }
194
196
  async findByUrl(url) {
195
- const database = this.connect();
197
+ const database = this.connect(true);
196
198
  database.exec("BEGIN");
197
199
  const row = database.prepare("SELECT resource_id FROM resource_urls WHERE url = ?").get(url);
198
200
  if (row === undefined) {
@@ -205,7 +207,7 @@ export class SqliteRegistry {
205
207
  return resource;
206
208
  }
207
209
  async findByPath(path) {
208
- const database = this.connect();
210
+ const database = this.connect(true);
209
211
  database.exec("BEGIN");
210
212
  const rows = database.prepare("SELECT DISTINCT resource_id FROM filesystem_links WHERE path = ? ORDER BY resource_id").all(path.replaceAll(sep, "/"));
211
213
  const resources = rows.map((row) => this.getResource(database, row.resource_id));
@@ -214,7 +216,7 @@ export class SqliteRegistry {
214
216
  return resources;
215
217
  }
216
218
  async listResources() {
217
- const database = this.connect();
219
+ const database = this.connect(true);
218
220
  database.exec("BEGIN");
219
221
  const rows = database.prepare("SELECT id FROM resources ORDER BY id").all();
220
222
  const resources = rows.map((row) => this.getResource(database, row.id));
@@ -246,16 +248,16 @@ export class SqliteRegistry {
246
248
  relationships: relationships.map((relationship) => ({ target_id: relationship.target_id, type: relationship.type, data: JSON.parse(relationship.data_json) })),
247
249
  };
248
250
  }
249
- connect() {
250
- const database = new (require("node:sqlite").DatabaseSync)(this.path);
251
+ connect(readOnly = false) {
252
+ const database = new (require("node:sqlite").DatabaseSync)(this.path, { readOnly });
251
253
  database.exec("PRAGMA busy_timeout=5000; PRAGMA foreign_keys=ON");
252
254
  return database;
253
255
  }
254
256
  }
255
257
  export class FileRegistry {
256
258
  path;
257
- constructor(path) {
258
- this.path = realpathSyncDirectory(path);
259
+ constructor(path, create = true) {
260
+ this.path = create ? realpathSyncDirectory(path) : realpathSync(path);
259
261
  }
260
262
  async put(resource) {
261
263
  const normalized = storageResource(resource);
@@ -273,9 +275,27 @@ export class FileRegistry {
273
275
  }
274
276
  const tempPath = join(this.path, randomUUID());
275
277
  const handle = await open(tempPath, "wx", 0o600);
276
- await handle.writeFile(`${stableJsonPretty(normalized)}\n`, "utf8");
277
- await handle.close();
278
- await rename(tempPath, this.resourcePath(normalized.id));
278
+ try {
279
+ try {
280
+ await handle.writeFile(`${stableJsonPretty(normalized)}\n`, "utf8");
281
+ await handle.sync();
282
+ }
283
+ finally {
284
+ await handle.close();
285
+ }
286
+ await rename(tempPath, this.resourcePath(normalized.id));
287
+ const directory = await open(this.path, "r");
288
+ try {
289
+ await directory.sync();
290
+ }
291
+ finally {
292
+ await directory.close();
293
+ }
294
+ }
295
+ finally {
296
+ await unlink(tempPath).catch((error) => { if (error.code !== "ENOENT")
297
+ throw error; });
298
+ }
279
299
  return normalized;
280
300
  }
281
301
  async rekey(previous, identifier) {