@celljs/ta-changes 0.0.0-stage → 4.8.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/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@celljs/ta-changes",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.8.0",
4
+ "description": "A pi package for Ta: the files each reply changed, with their diffs, and a button that takes the changes of a reply back",
5
+ "keywords": [
6
+ "pi-package"
7
+ ],
8
+ "homepage": "https://github.com/cellbang/cell",
9
+ "bugs": {
10
+ "url": "https://github.com/cellbang/cell/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/cellbang/cell.git"
15
+ },
16
+ "license": "MIT",
17
+ "files": [
18
+ "web",
19
+ "host",
20
+ "extension",
21
+ "web-src"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc -b && bun build web-src/index.tsx --format esm --target browser --outfile web/index.js && bun build web-src/host.ts --format esm --target node --outfile host/index.mjs && bun build web-src/extension.ts --format esm --target node --outfile extension/index.mjs",
25
+ "clean": "rm -rf lib web host extension *.tsbuildinfo",
26
+ "test": "bun test ./lib",
27
+ "watch": "tsc -b -w --preserveWatchOutput"
28
+ },
29
+ "devDependencies": {
30
+ "@celljs/ta-protocol": "4.8.0",
31
+ "@celljs/ta-web": "4.8.0",
32
+ "@types/react": "^19.3.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "pi": {
38
+ "web": [
39
+ "./web/index.js"
40
+ ],
41
+ "host": "./host/index.mjs",
42
+ "extensions": [
43
+ "./extension/index.mjs"
44
+ ]
45
+ }
46
+ }
package/web/index.js ADDED
@@ -0,0 +1,460 @@
1
+ // web-src/changes-util.ts
2
+ var ChangesUtil;
3
+ ((ChangesUtil) => {
4
+ function isText(data) {
5
+ return !data?.subarray(0, 8000).includes(0);
6
+ }
7
+ ChangesUtil.isText = isText;
8
+ function totals(files) {
9
+ return files.reduce((sum, file) => ({ added: sum.added + file.added, removed: sum.removed + file.removed }), { added: 0, removed: 0 });
10
+ }
11
+ ChangesUtil.totals = totals;
12
+ function relative(path, root) {
13
+ const base = root.replace(/[\\/]+$/, "");
14
+ if (base && path.length > base.length && path.startsWith(base) && /[\\/]/.test(path[base.length])) {
15
+ return path.slice(base.length + 1);
16
+ }
17
+ return path;
18
+ }
19
+ ChangesUtil.relative = relative;
20
+ function place(path, root) {
21
+ const shown = relative(path, root);
22
+ const index = Math.max(shown.lastIndexOf("/"), shown.lastIndexOf("\\"));
23
+ return index < 0 ? { name: shown, dir: "" } : { name: shown.slice(index + 1), dir: shown.slice(0, index) || shown.slice(0, 1) };
24
+ }
25
+ ChangesUtil.place = place;
26
+ function summary(set) {
27
+ const files = set.files.map((file) => `${relative(file.path, set.root)} (${file.before === undefined && !file.large ? "new, " : ""}+${file.added} -${file.removed})`);
28
+ return `Changed ${set.files.length === 1 ? "1 file" : `${set.files.length} files`}: ${files.join(", ")}`;
29
+ }
30
+ ChangesUtil.summary = summary;
31
+ function revertNote(paths, root) {
32
+ return "The user took back the changes you made to these files in one of your earlier replies; they are as they were before that reply: " + `${paths.map((path) => relative(path, root)).join(", ")}. Read them again before you change them.`;
33
+ }
34
+ ChangesUtil.revertNote = revertNote;
35
+ })(ChangesUtil ||= {});
36
+
37
+ // web-src/changes-protocol.ts
38
+ var CHANGES_TYPE = "ta-changes";
39
+ var REVERT_TYPE = "ta-changes-revert";
40
+ var REVERTED_KEY = "reverted";
41
+ var MAX_KEPT_BYTES = 4 * 1024 * 1024;
42
+
43
+ // web-src/i18n.ts
44
+ var EN = {
45
+ filesChanged: "{count} files changed",
46
+ fileChanged: "1 file changed",
47
+ undo: "Undo the changes of this reply",
48
+ undoing: "Taking back…",
49
+ created: "new",
50
+ conflictTitle: "Files changed again since",
51
+ conflictMessage: "{files} changed after this reply. Undoing writes them back as they were before it, and the later changes are lost.",
52
+ overwrite: "Undo anyway",
53
+ skipped: "Not taken back, as their contents were not kept: {files}",
54
+ nothing: "These files are already as they were before this reply.",
55
+ busy: "Ta is still working in this conversation; undo once it stops.",
56
+ closed: "This conversation is not open in the Ta service; open it again and retry.",
57
+ missing: "This reply is not in the conversation any more.",
58
+ failed: "Could not undo: {message}",
59
+ revertNote: "Took back the changes to {count} files",
60
+ revertNoteOne: "Took back the changes to 1 file",
61
+ diffTitle: "{name} (changes)",
62
+ binary: "A binary file: no diff to show.",
63
+ large: "The file is too large: its contents before and after were not kept.",
64
+ gone: "The kept contents were cleaned up: no diff to show.",
65
+ loading: "Loading…"
66
+ };
67
+ var ZH = {
68
+ filesChanged: "{count} 个文件已更改",
69
+ fileChanged: "1 个文件已更改",
70
+ undo: "撤销本轮改动",
71
+ undoing: "正在撤销…",
72
+ created: "新建",
73
+ conflictTitle: "有文件在这之后又改过",
74
+ conflictMessage: "{files} 在这一轮之后又被修改过。撤销会把它们恢复成这一轮之前的样子,之后的修改会丢失。",
75
+ overwrite: "仍然撤销",
76
+ skipped: "这些文件没有保存内容,未能撤销:{files}",
77
+ nothing: "这些文件已经是这一轮之前的样子了。",
78
+ busy: "Ta 还在这个对话里工作,等它停下再撤销。",
79
+ closed: "Ta 服务里没有打开这个对话,重新打开后再试。",
80
+ missing: "这一轮已经不在对话里了。",
81
+ failed: "撤销失败:{message}",
82
+ revertNote: "已撤销 {count} 个文件的改动",
83
+ revertNoteOne: "已撤销 1 个文件的改动",
84
+ diffTitle: "{name}(本轮改动)",
85
+ binary: "二进制文件,不显示差异。",
86
+ large: "文件太大,没有保存改动前后的内容。",
87
+ gone: "保存的内容已被清理,无法显示差异。",
88
+ loading: "加载中…"
89
+ };
90
+ var ChangesWords;
91
+ ((ChangesWords) => {
92
+ function of(language) {
93
+ return language === "zh-CN" ? ZH : EN;
94
+ }
95
+ ChangesWords.of = of;
96
+ function format(text, values) {
97
+ return text.replace(/\{(\w+)\}/g, (whole, key) => (key in values) ? String(values[key]) : whole);
98
+ }
99
+ ChangesWords.format = format;
100
+ })(ChangesWords ||= {});
101
+
102
+ // web-src/react.ts
103
+ var React;
104
+ function bind(react) {
105
+ React = react;
106
+ }
107
+
108
+ // web-src/page.ts
109
+ var pi;
110
+ function bindPage(api) {
111
+ pi = api;
112
+ }
113
+ function t(key, values = {}) {
114
+ return ChangesWords.format(ChangesWords.of(pi.ui.language)[key], values);
115
+ }
116
+ function icon(name) {
117
+ return pi.ui.icons[name] ?? (() => null);
118
+ }
119
+ function message(error) {
120
+ return error instanceof Error ? error.message : String(error);
121
+ }
122
+ var Reverted;
123
+ ((Reverted) => {
124
+ let ids = new Set;
125
+ let loading = false;
126
+ const listeners = new Set;
127
+ function load() {
128
+ return pi.storage.get(REVERTED_KEY).then((value) => {
129
+ ids = new Set(Array.isArray(value) ? value : []);
130
+ for (const listener of listeners) {
131
+ listener();
132
+ }
133
+ });
134
+ }
135
+ function watch() {
136
+ return pi.storage.onChange((keys) => {
137
+ if (keys.includes(REVERTED_KEY)) {
138
+ load();
139
+ }
140
+ });
141
+ }
142
+ Reverted.watch = watch;
143
+ function use(id) {
144
+ const [, setVersion] = React.useState(0);
145
+ React.useEffect(() => {
146
+ const listener = () => setVersion((version) => version + 1);
147
+ listeners.add(listener);
148
+ if (!loading) {
149
+ loading = true;
150
+ load().catch(() => {
151
+ loading = false;
152
+ });
153
+ }
154
+ return () => {
155
+ listeners.delete(listener);
156
+ };
157
+ }, []);
158
+ return ids.has(id);
159
+ }
160
+ Reverted.use = use;
161
+ function add(id) {
162
+ ids = new Set([...ids, id]);
163
+ for (const listener of listeners) {
164
+ listener();
165
+ }
166
+ }
167
+ Reverted.add = add;
168
+ })(Reverted ||= {});
169
+ var Latest;
170
+ ((Latest) => {
171
+ const listeners = new Set;
172
+ function tell(value) {
173
+ for (const listener of listeners) {
174
+ listener(value);
175
+ }
176
+ }
177
+ function watch() {
178
+ pi.on("message_end", (event) => {
179
+ const message2 = event.message;
180
+ if (message2.role === "custom" && message2.customType === CHANGES_TYPE && message2.details?.files) {
181
+ tell(message2.details);
182
+ }
183
+ });
184
+ pi.on("session_replaced", () => tell("replaced"));
185
+ }
186
+ Latest.watch = watch;
187
+ function subscribe(listener) {
188
+ listeners.add(listener);
189
+ return () => {
190
+ listeners.delete(listener);
191
+ };
192
+ }
193
+ Latest.subscribe = subscribe;
194
+ })(Latest ||= {});
195
+
196
+ // web-src/diff-tab.tsx
197
+ function openDiff(set, file) {
198
+ const { name } = ChangesUtil.place(file.path, set.root);
199
+ pi.ui.openEditor({
200
+ id: `ta-changes:${set.id}:${file.path}`,
201
+ title: () => t("diffTitle", { name }),
202
+ icon: icon("FileDiff"),
203
+ render: () => /* @__PURE__ */ React.createElement(DiffTab, {
204
+ set,
205
+ file
206
+ })
207
+ }, { preview: true });
208
+ }
209
+ async function text(hash) {
210
+ return hash ? pi.callHost("read", hash) : "";
211
+ }
212
+ function DiffTab({ set, file }) {
213
+ const [texts, setTexts] = React.useState();
214
+ React.useEffect(() => {
215
+ if (file.binary || file.large) {
216
+ return;
217
+ }
218
+ let live = true;
219
+ Promise.all([text(file.before), text(file.after)]).then(([before, after]) => live && setTexts(before === null || after === null ? { error: t("gone") } : { before, after }), (error) => live && setTexts({ error: message(error) }));
220
+ return () => {
221
+ live = false;
222
+ };
223
+ }, [file]);
224
+ const empty = file.binary ? t("binary") : file.large ? t("large") : !texts ? t("loading") : ("error" in texts) ? texts.error : undefined;
225
+ return /* @__PURE__ */ React.createElement("div", {
226
+ className: "tcg-diff"
227
+ }, /* @__PURE__ */ React.createElement("div", {
228
+ className: "tcg-diff-head"
229
+ }, /* @__PURE__ */ React.createElement("span", {
230
+ className: "tcg-diff-path",
231
+ title: file.path
232
+ }, ChangesUtil.relative(file.path, set.root)), /* @__PURE__ */ React.createElement("span", {
233
+ className: "tcg-stat"
234
+ }, /* @__PURE__ */ React.createElement("span", {
235
+ className: "tcg-add"
236
+ }, "+", file.added), /* @__PURE__ */ React.createElement("span", {
237
+ className: "tcg-del"
238
+ }, "−", file.removed))), /* @__PURE__ */ React.createElement("div", {
239
+ className: "tcg-diff-body"
240
+ }, empty !== undefined || !texts || "error" in texts ? /* @__PURE__ */ React.createElement("div", {
241
+ className: "tcg-diff-empty"
242
+ }, empty) : /* @__PURE__ */ React.createElement(pi.ui.DiffEditor, {
243
+ path: file.path,
244
+ original: texts.before,
245
+ modified: texts.after
246
+ })));
247
+ }
248
+
249
+ // web-src/card.tsx
250
+ function Stat({ added, removed }) {
251
+ return /* @__PURE__ */ React.createElement("span", {
252
+ className: "tcg-stat"
253
+ }, /* @__PURE__ */ React.createElement("span", {
254
+ className: "tcg-add"
255
+ }, "+", added), /* @__PURE__ */ React.createElement("span", {
256
+ className: "tcg-del"
257
+ }, "−", removed));
258
+ }
259
+ function names(paths, root) {
260
+ return paths.map((path) => ChangesUtil.relative(path, root)).join(", ");
261
+ }
262
+ async function undo(sessionId, set) {
263
+ let result = await pi.callHost("revert", sessionId, set.id, false);
264
+ if (result.conflicts.length && !result.refused) {
265
+ const go = await pi.ui.confirm(t("conflictTitle"), t("conflictMessage", { files: names(result.conflicts, set.root) }), {
266
+ confirmLabel: t("overwrite"),
267
+ danger: true
268
+ });
269
+ if (!go) {
270
+ return false;
271
+ }
272
+ result = await pi.callHost("revert", sessionId, set.id, true);
273
+ }
274
+ if (result.refused) {
275
+ pi.ui.notify(t(result.refused), "warning");
276
+ return false;
277
+ }
278
+ if (result.skipped.length) {
279
+ pi.ui.notify(t("skipped", { files: names(result.skipped, set.root) }), "warning");
280
+ } else if (!result.reverted.length) {
281
+ pi.ui.notify(t("nothing"));
282
+ }
283
+ return true;
284
+ }
285
+ function useLatest(sessionId) {
286
+ const [latest, setLatest] = React.useState();
287
+ React.useEffect(() => {
288
+ if (!sessionId) {
289
+ return;
290
+ }
291
+ let live = true;
292
+ const ask = () => pi.callHost("latest", sessionId).then((set) => live && setLatest({ sessionId, set: set ?? undefined }), () => live && setLatest({ sessionId }));
293
+ const stop = Latest.subscribe((value) => value === "replaced" ? void ask() : setLatest({ sessionId, set: value }));
294
+ ask();
295
+ return () => {
296
+ live = false;
297
+ stop();
298
+ };
299
+ }, [sessionId]);
300
+ return latest && latest.sessionId === sessionId ? latest.set : undefined;
301
+ }
302
+ function ChangesBar() {
303
+ const sessionId = pi.ui.useSessionId();
304
+ const set = useLatest(sessionId);
305
+ const reverted = Reverted.use(set?.id ?? "");
306
+ const [open, setOpen] = React.useState(false);
307
+ const [busy, setBusy] = React.useState(false);
308
+ const root = React.useRef(null);
309
+ React.useEffect(() => setOpen(false), [set?.id]);
310
+ React.useEffect(() => {
311
+ if (!open) {
312
+ return;
313
+ }
314
+ const close = (event) => {
315
+ if (event instanceof KeyboardEvent ? event.key === "Escape" : !root.current?.contains(event.target)) {
316
+ setOpen(false);
317
+ }
318
+ };
319
+ document.addEventListener("pointerdown", close, true);
320
+ document.addEventListener("keydown", close);
321
+ return () => {
322
+ document.removeEventListener("pointerdown", close, true);
323
+ document.removeEventListener("keydown", close);
324
+ };
325
+ }, [open]);
326
+ if (!sessionId || !set?.files?.length || reverted) {
327
+ return null;
328
+ }
329
+ const totals = ChangesUtil.totals(set.files);
330
+ const Undo = icon("Undo2");
331
+ const run = async () => {
332
+ if (busy) {
333
+ return;
334
+ }
335
+ setBusy(true);
336
+ try {
337
+ if (await undo(sessionId, set)) {
338
+ Reverted.add(set.id);
339
+ setOpen(false);
340
+ }
341
+ } catch (error) {
342
+ pi.ui.notify(t("failed", { message: message(error) }), "error");
343
+ } finally {
344
+ setBusy(false);
345
+ }
346
+ };
347
+ return /* @__PURE__ */ React.createElement("div", {
348
+ className: "tcg",
349
+ ref: root
350
+ }, open && /* @__PURE__ */ React.createElement("div", {
351
+ className: "tcg-card",
352
+ role: "dialog"
353
+ }, /* @__PURE__ */ React.createElement("div", {
354
+ className: "tcg-files"
355
+ }, set.files.map((file) => {
356
+ const { name, dir } = ChangesUtil.place(file.path, set.root);
357
+ return /* @__PURE__ */ React.createElement("button", {
358
+ key: file.path,
359
+ type: "button",
360
+ className: "tcg-file",
361
+ title: file.path,
362
+ onClick: () => openDiff(set, file)
363
+ }, /* @__PURE__ */ React.createElement("span", {
364
+ className: "tcg-name"
365
+ }, name), dir && /* @__PURE__ */ React.createElement("span", {
366
+ className: "tcg-dir"
367
+ }, dir), file.before === undefined && !file.large && /* @__PURE__ */ React.createElement("span", {
368
+ className: "tcg-tag"
369
+ }, t("created")), /* @__PURE__ */ React.createElement(Stat, {
370
+ added: file.added,
371
+ removed: file.removed
372
+ }));
373
+ })), /* @__PURE__ */ React.createElement("div", {
374
+ className: "tcg-foot"
375
+ }, /* @__PURE__ */ React.createElement("button", {
376
+ type: "button",
377
+ className: "tcg-undo",
378
+ disabled: busy,
379
+ onClick: () => void run()
380
+ }, /* @__PURE__ */ React.createElement(Undo, {
381
+ size: 15
382
+ }), busy ? t("undoing") : t("undo")))), /* @__PURE__ */ React.createElement("button", {
383
+ type: "button",
384
+ className: "tcg-pill",
385
+ "aria-expanded": open,
386
+ onClick: () => setOpen(!open)
387
+ }, /* @__PURE__ */ React.createElement("span", null, set.files.length === 1 ? t("fileChanged") : t("filesChanged", { count: set.files.length })), /* @__PURE__ */ React.createElement(Stat, {
388
+ added: totals.added,
389
+ removed: totals.removed
390
+ })));
391
+ }
392
+ function RevertNote({ message: message2 }) {
393
+ const details = message2.details;
394
+ const count = details?.files?.length ?? 0;
395
+ const Undo = icon("Undo2");
396
+ return /* @__PURE__ */ React.createElement("div", {
397
+ className: "tcg-note"
398
+ }, /* @__PURE__ */ React.createElement(Undo, {
399
+ size: 13
400
+ }), count === 1 ? t("revertNoteOne") : t("revertNote", { count }));
401
+ }
402
+
403
+ // web-src/style.ts
404
+ var STYLE = `
405
+ .tcg { position: relative; display: flex; justify-content: center; }
406
+ .tcg-pill { display: inline-flex; align-items: center; gap: 8px; height: 32px; padding: 0 14px; border: 1px solid var(--ta-border-muted); border-radius: 999px; background: var(--ta-raised); color: var(--ta-text); font: inherit; font-size: 13px; cursor: pointer; transition: background 120ms, border-color 120ms, transform 120ms var(--ta-ease); }
407
+ .tcg-pill:hover { background: var(--ta-hover); border-color: var(--ta-border); }
408
+ .tcg-pill:active { transform: scale(.98); }
409
+ .tcg-pill[aria-expanded='true'] { border-color: var(--ta-border); }
410
+ .tcg-stat { display: inline-flex; gap: 6px; font-variant-numeric: tabular-nums; font-size: 12.5px; }
411
+ .tcg-add { color: var(--ta-green); }
412
+ .tcg-del { color: var(--ta-red); }
413
+ .tcg-card { position: absolute; bottom: calc(100% + 8px); left: 50%; z-index: 20; width: min(100%, 560px); transform: translateX(-50%); border: 1px solid var(--ta-border-muted); border-radius: 14px; background: var(--ta-card); box-shadow: var(--ta-shadow-pop); overflow: hidden; animation: tcg-in 160ms var(--ta-ease); }
414
+ @keyframes tcg-in { from { opacity: 0; transform: translate(-50%, 4px); } }
415
+ .tcg-files { max-height: 280px; overflow: auto; padding: 4px 0; }
416
+ .tcg-file { display: flex; align-items: baseline; gap: 8px; width: 100%; padding: 8px 16px; border: 0; background: none; color: inherit; font: inherit; font-size: 13.5px; text-align: left; cursor: pointer; }
417
+ .tcg-file:hover { background: var(--ta-hover); }
418
+ .tcg-file:disabled { cursor: default; }
419
+ .tcg-file:disabled:hover { background: none; }
420
+ .tcg-name { flex: none; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
421
+ .tcg-dir { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
422
+ .tcg-tag { flex: none; padding: 0 6px; border-radius: 6px; background: var(--ta-chip); color: var(--ta-muted); font-size: 11px; line-height: 17px; }
423
+ .tcg-file .tcg-stat { flex: none; margin-left: auto; }
424
+ .tcg-foot { border-top: 1px solid var(--ta-border-muted); padding: 4px; }
425
+ .tcg-undo { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 12px; border: 0; border-radius: 10px; background: none; color: var(--ta-secondary); font: inherit; font-size: 13.5px; text-align: left; cursor: pointer; }
426
+ .tcg-undo:hover { background: var(--ta-hover); color: var(--ta-text); }
427
+ .tcg-undo:disabled { color: var(--ta-muted); cursor: default; background: none; }
428
+ .tcg-note { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 2px 0; color: var(--ta-muted); font-size: 12.5px; }
429
+ .tcg-diff { display: flex; flex-direction: column; height: 100%; min-height: 0; }
430
+ .tcg-diff-head { display: flex; align-items: center; gap: 10px; flex: none; height: 32px; padding: 0 12px; border-bottom: 1px solid var(--ta-border-muted); color: var(--ta-muted); font-size: 12px; }
431
+ .tcg-diff-path { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
432
+ .tcg-diff-body { flex: 1; min-height: 0; }
433
+ .tcg-diff-empty { padding: 24px; color: var(--ta-muted); font-size: 13px; text-align: center; }
434
+ `;
435
+ function injectStyle() {
436
+ const element = document.createElement("style");
437
+ element.dataset.taPackage = "ta-changes";
438
+ element.textContent = STYLE;
439
+ document.head.appendChild(element);
440
+ return () => element.remove();
441
+ }
442
+
443
+ // web-src/index.tsx
444
+ function activate(pi2) {
445
+ bind(pi2.React);
446
+ bindPage(pi2);
447
+ const removeStyle = injectStyle();
448
+ const stopWatching = Reverted.watch();
449
+ Latest.watch();
450
+ pi2.ui.setWidget("changes", ChangesBar);
451
+ pi2.registerMessageRenderer(REVERT_TYPE, RevertNote);
452
+ return () => {
453
+ stopWatching();
454
+ pi2.ui.setWidget("changes", undefined);
455
+ removeStyle();
456
+ };
457
+ }
458
+ export {
459
+ activate as default
460
+ };
@@ -0,0 +1,33 @@
1
+ import { afterAll, describe, expect, it } from 'bun:test';
2
+ import { mkdtempSync, readdirSync, rmSync, utimesSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { BlobStore } from './blob-store';
6
+
7
+ describe('BlobStore', () => {
8
+ const dir = mkdtempSync(join(tmpdir(), 'ta-changes-blobs-'));
9
+ afterAll(() => rmSync(dir, { recursive: true, force: true }));
10
+
11
+ it('keeps the same contents once, by their hash', async () => {
12
+ const store = new BlobStore(dir);
13
+ const data = new TextEncoder().encode('hello');
14
+ const name = await store.store(data);
15
+ expect(name).toBe('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824');
16
+ expect(await store.store(data)).toBe(name);
17
+ expect(readdirSync(join(dir, '2c'))).toEqual([name]);
18
+ expect(new TextDecoder().decode(await store.load(name))).toBe('hello');
19
+ expect(await store.load('0'.repeat(64))).toBeUndefined();
20
+ expect(await store.load('../../etc/passwd')).toBeUndefined();
21
+ });
22
+
23
+ it('removes what was not kept again for long', async () => {
24
+ const store = new BlobStore(dir);
25
+ const old = await store.store(new TextEncoder().encode('old'));
26
+ const fresh = await store.store(new TextEncoder().encode('fresh'));
27
+ const longAgo = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000);
28
+ utimesSync(join(dir, old.slice(0, 2), old), longAgo, longAgo);
29
+ expect(await store.prune()).toBe(1);
30
+ expect(await store.load(old)).toBeUndefined();
31
+ expect(await store.load(fresh)).toBeDefined();
32
+ });
33
+ });
@@ -0,0 +1,68 @@
1
+ import { mkdir, readdir, readFile, rename, rm, stat, utimes, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { hash } from './recorder';
4
+
5
+ const HASH = /^[0-9a-f]{64}$/;
6
+
7
+ /**
8
+ * The contents of changed files, by their hash, on the machine of ta serve: the same contents are kept
9
+ * once, however many replies and sessions have them. What nobody kept again for {@link KEEP_DAYS} days goes.
10
+ */
11
+ export class BlobStore {
12
+ static readonly KEEP_DAYS = 30;
13
+
14
+ constructor(protected readonly dir: string) {}
15
+
16
+ static isHash(value: unknown): value is string {
17
+ return typeof value === 'string' && HASH.test(value);
18
+ }
19
+
20
+ async store(data: Uint8Array): Promise<string> {
21
+ const name = hash(data);
22
+ const file = this.file(name);
23
+ const now = new Date();
24
+ try {
25
+ // Kept again: it stays for as long again.
26
+ await utimes(file, now, now);
27
+ } catch {
28
+ await mkdir(join(this.dir, name.slice(0, 2)), { recursive: true });
29
+ const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
30
+ await writeFile(temporary, data);
31
+ await rename(temporary, file);
32
+ }
33
+ return name;
34
+ }
35
+
36
+ async load(name: string): Promise<Uint8Array | undefined> {
37
+ if (!BlobStore.isHash(name)) {
38
+ return undefined;
39
+ }
40
+ try {
41
+ return await readFile(this.file(name));
42
+ } catch {
43
+ return undefined;
44
+ }
45
+ }
46
+
47
+ /** Removes what was last kept more than `days` ago. */
48
+ async prune(days = BlobStore.KEEP_DAYS, now = Date.now()): Promise<number> {
49
+ const before = now - days * 24 * 60 * 60 * 1000;
50
+ let removed = 0;
51
+ for (const group of await readdir(this.dir).catch(() => [])) {
52
+ const dir = join(this.dir, group);
53
+ for (const name of await readdir(dir).catch(() => [])) {
54
+ const file = join(dir, name);
55
+ const info = await stat(file).catch(() => undefined);
56
+ if (info && info.mtimeMs < before) {
57
+ await rm(file, { force: true });
58
+ removed++;
59
+ }
60
+ }
61
+ }
62
+ return removed;
63
+ }
64
+
65
+ protected file(name: string): string {
66
+ return join(this.dir, name.slice(0, 2), name);
67
+ }
68
+ }