@spooky-sync/core 0.0.1-canary.33 → 0.0.1-canary.331
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/AGENTS.md +84 -0
- package/README.md +56 -16
- package/dist/index.d.ts +2080 -142
- package/dist/index.js +16009 -2480
- package/dist/live.d.ts +149 -0
- package/dist/live.js +739 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/otel/index.js +3 -3
- package/dist/pure.d.ts +373 -0
- package/dist/pure.js +24 -0
- package/dist/sqlite-open.js +392 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/sw.d.ts +207 -0
- package/dist/sw.js +393 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +477 -0
- package/dist/types.d.ts +1857 -38
- package/dist/worker-edges.js +480 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +48 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +28 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +173 -0
- package/skills/sp00ky-core/SKILL.md +32 -7
- package/skills/sp00ky-core/references/config.md +7 -1
- package/src/boot/auth-flip.saga.test.ts +135 -0
- package/src/boot/auth-flip.saga.ts +77 -0
- package/src/boot/boot.saga.test.ts +203 -0
- package/src/boot/boot.saga.ts +146 -0
- package/src/boot/bucket-switch.saga.test.ts +187 -0
- package/src/boot/bucket-switch.saga.ts +133 -0
- package/src/boot/preload.saga.test.ts +80 -0
- package/src/boot/preload.saga.ts +40 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/client/bucket-handle.test.ts +195 -0
- package/src/client/bucket-handle.ts +352 -0
- package/src/client/router.test.ts +58 -0
- package/src/client/router.ts +100 -0
- package/src/client/runtime.test.ts +195 -0
- package/src/client/runtime.ts +296 -0
- package/src/client/services.test.ts +74 -0
- package/src/client/services.ts +434 -0
- package/src/client/sp00ky-client.test.ts +341 -0
- package/src/client/sp00ky-client.ts +625 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +69 -1
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +80 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +167 -0
- package/src/kernel/events.ts +57 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +148 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/live/index.test.ts +512 -0
- package/src/live/index.ts +1059 -0
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +209 -0
- package/src/modules/auth/auth.local-first.test.ts +228 -0
- package/src/modules/auth/events/index.ts +9 -2
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.integration.test.ts +205 -0
- package/src/modules/auth/impersonation.test.ts +377 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +476 -22
- package/src/modules/crdt/acceptance.ts +75 -0
- package/src/modules/crdt/bytes.ts +50 -0
- package/src/modules/crdt/crdt-field.ts +125 -0
- package/src/modules/crdt/crdt.test.ts +469 -0
- package/src/modules/crdt/hash.ts +103 -0
- package/src/modules/crdt/index.ts +757 -0
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/crdt/mirror.ts +37 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +746 -95
- package/src/modules/devtools/mutations.test.ts +92 -0
- package/src/modules/devtools/mutations.ts +160 -0
- package/src/modules/devtools/notify-throttle.test.ts +166 -0
- package/src/modules/devtools/state-shape.test.ts +263 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +298 -0
- package/src/modules/feature-flag/index.ts +316 -0
- package/src/modules/ref-tables.test.ts +93 -0
- package/src/modules/ref-tables.ts +91 -0
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/mutation/mutation-id.test.ts +37 -0
- package/src/mutation/mutation-id.ts +35 -0
- package/src/mutation/pending-writes.integration.test.ts +175 -0
- package/src/mutation/push.saga.test.ts +301 -0
- package/src/mutation/push.saga.ts +225 -0
- package/src/mutation/rows.test.ts +231 -0
- package/src/mutation/rows.ts +427 -0
- package/src/mutation/tray.saga.test.ts +63 -0
- package/src/mutation/tray.saga.ts +48 -0
- package/src/mutation/write.saga.test.ts +316 -0
- package/src/mutation/write.saga.ts +268 -0
- package/src/otel/index.ts +10 -7
- package/src/pure.ts +66 -0
- package/src/push/types.ts +174 -0
- package/src/query/anon-query-isolation.integration.test.ts +527 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +65 -0
- package/src/query/fetch.saga.test.ts +199 -0
- package/src/query/fetch.saga.ts +120 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/query/id-set-plan.test.ts +132 -0
- package/src/query/lifecycle.saga.test.ts +274 -0
- package/src/query/lifecycle.saga.ts +186 -0
- package/src/query/list-ref-live.integration.test.ts +203 -0
- package/src/query/materialize.saga.test.ts +115 -0
- package/src/query/materialize.saga.ts +84 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +57 -0
- package/src/query/membership.saga.test.ts +407 -0
- package/src/query/membership.saga.ts +282 -0
- package/src/query/membership.test.ts +118 -0
- package/src/query/membership.ts +201 -0
- package/src/query/register.saga.test.ts +311 -0
- package/src/query/register.saga.ts +235 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/sql.test.ts +118 -0
- package/src/query/sql.ts +157 -0
- package/src/query/window-query.test.ts +70 -0
- package/src/query/window-query.ts +208 -0
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +209 -0
- package/src/services/database/connection-supervisor.test.ts +318 -0
- package/src/services/database/connection-supervisor.ts +422 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +156 -61
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +7 -0
- package/src/services/database/landed-body-clear.integration.test.ts +236 -0
- package/src/services/database/local-migrator.integration.test.ts +110 -0
- package/src/services/database/local-migrator.ts +61 -16
- package/src/services/database/local.test.ts +150 -0
- package/src/services/database/local.ts +508 -63
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +484 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +285 -6
- package/src/services/database/sqlite-cache-engine.test.ts +628 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1401 -0
- package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
- package/src/services/database/sqlite-column-reset.ts +91 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +164 -0
- package/src/services/database/sqlite-plan-sql.ts +196 -0
- package/src/services/database/sqlite-projection.test.ts +121 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +261 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +417 -0
- package/src/services/database/surreal-cache-engine.ts +163 -0
- package/src/services/logger/index.test.ts +132 -0
- package/src/services/logger/index.ts +189 -7
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +10 -3
- package/src/services/persistence/surrealdb.ts +4 -4
- package/src/services/stream-processor/index.ts +806 -67
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
- package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +58 -2
- package/src/services/tabs/broker-client.ts +284 -0
- package/src/services/tabs/broker.test.ts +381 -0
- package/src/services/tabs/coordinator.test.ts +386 -0
- package/src/services/tabs/coordinator.ts +646 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +262 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +652 -0
- package/src/sp00ky.ts +5 -395
- package/src/state/client-state.ts +208 -0
- package/src/state/lifecycle.test.ts +141 -0
- package/src/state/lifecycle.ts +99 -0
- package/src/state/reducers.test.ts +306 -0
- package/src/state/reducers.ts +531 -0
- package/src/state/selectors.test.ts +188 -0
- package/src/state/selectors.ts +258 -0
- package/src/sw/index.test.ts +582 -0
- package/src/sw/index.ts +607 -0
- package/src/sw/store.ts +96 -0
- package/src/sync/connection.saga.test.ts +97 -0
- package/src/sync/connection.saga.ts +89 -0
- package/src/sync/live.saga.test.ts +178 -0
- package/src/sync/live.saga.ts +107 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/sync/policy.test.ts +663 -0
- package/src/sync/policy.ts +515 -0
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +78 -0
- package/src/sync/tabs.saga.ts +96 -0
- package/src/testing/adapters.ts +137 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +63 -0
- package/src/testing/run-pure.test.ts +92 -0
- package/src/testing/run-pure.ts +128 -0
- package/src/testing/services.ts +17 -0
- package/src/types.ts +633 -10
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +39 -0
- package/src/utils/error-classification.test.ts +54 -0
- package/src/utils/error-classification.ts +25 -0
- package/src/utils/index.ts +89 -12
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +64 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/sha256.ts +15 -0
- package/src/utils/surql.ts +29 -18
- package/src/utils/utils.test.ts +3 -2
- package/tsdown.config.ts +117 -3
- package/vitest.config.ts +48 -0
- package/src/modules/cache/index.ts +0 -241
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -879
- package/src/modules/sync/engine.ts +0 -173
- package/src/modules/sync/events/index.ts +0 -77
- package/src/modules/sync/index.ts +0 -3
- package/src/modules/sync/queue/index.ts +0 -2
- package/src/modules/sync/queue/queue-down.ts +0 -89
- package/src/modules/sync/queue/queue-up.ts +0 -223
- package/src/modules/sync/scheduler.ts +0 -84
- package/src/modules/sync/sync.ts +0 -421
- package/src/modules/sync/utils.test.ts +0 -311
- package/src/modules/sync/utils.ts +0 -167
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { BucketHandle, bucketContentToBlob } from './bucket-handle';
|
|
3
|
+
|
|
4
|
+
vi.mock('../utils/blurhash', async (importOriginal) => {
|
|
5
|
+
const actual = await importOriginal<typeof import('../utils/blurhash')>();
|
|
6
|
+
return { ...actual, encodeImageToBlurhash: vi.fn(async () => 'LEHV6nWB2yk8pyo0adR*.7kCMdnj') };
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
function fakeRemote(answers: Record<string, unknown> = {}) {
|
|
10
|
+
const calls: Array<[string, unknown]> = [];
|
|
11
|
+
const remote = {
|
|
12
|
+
query: async (sql: string, vars?: unknown) => {
|
|
13
|
+
calls.push([sql, vars]);
|
|
14
|
+
for (const [needle, answer] of Object.entries(answers)) if (sql.includes(needle)) return [answer];
|
|
15
|
+
return [undefined];
|
|
16
|
+
},
|
|
17
|
+
} as any;
|
|
18
|
+
return { remote, calls };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function fakeBlobs() {
|
|
22
|
+
const log: string[] = [];
|
|
23
|
+
return {
|
|
24
|
+
log,
|
|
25
|
+
blobs: {
|
|
26
|
+
invalidate: async (k: { path: string }) => void log.push(`invalidate:${k.path}`),
|
|
27
|
+
read: async (k: { path: string }) => (k.path.endsWith('.bh') ? new Blob(['LEHV6nWB2yk8pyo0adR*.7kCMdnj']) : new Blob(['x'])),
|
|
28
|
+
acquireUrl: async (k: { path: string }) => ({ url: `blob:${k.path}`, release: () => undefined }),
|
|
29
|
+
setPinned: (k: { path: string }, pinned: boolean) => void log.push(`pin:${k.path}:${pinned}`),
|
|
30
|
+
prefetch: async (keys: Array<{ path: string }>) => void log.push(`prefetch:${keys.map((k) => k.path).join('+')}`),
|
|
31
|
+
} as any,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('bucketContentToBlob', () => {
|
|
36
|
+
it('coerces strings, buffers and views; passes blobs through; rejects the rest', async () => {
|
|
37
|
+
expect(bucketContentToBlob(null)).toBeNull();
|
|
38
|
+
expect(bucketContentToBlob(42)).toBeNull();
|
|
39
|
+
const b = new Blob(['a']);
|
|
40
|
+
expect(bucketContentToBlob(b)).toBe(b);
|
|
41
|
+
expect(await bucketContentToBlob('hi')!.text()).toBe('hi');
|
|
42
|
+
expect((await bucketContentToBlob(new ArrayBuffer(3))!.arrayBuffer()).byteLength).toBe(3);
|
|
43
|
+
expect((await bucketContentToBlob(new Uint8Array([1, 2]))!.arrayBuffer()).byteLength).toBe(2);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('BucketHandle', () => {
|
|
48
|
+
it('put: uploads, invalidates, writes the blurhash sidecar for images (best-effort), honours the setting', async () => {
|
|
49
|
+
const { remote, calls } = fakeRemote();
|
|
50
|
+
const { blobs, log } = fakeBlobs();
|
|
51
|
+
const warn = vi.fn();
|
|
52
|
+
const h = new BucketHandle('files', remote, blobs, { logger: { warn } });
|
|
53
|
+
const res = await h.put('a/pic.png', new Uint8Array([1]));
|
|
54
|
+
expect(res.blurhash).toBe('LEHV6nWB2yk8pyo0adR*.7kCMdnj');
|
|
55
|
+
expect(calls.map(([s]) => s)).toEqual(['RETURN f"files:/a/pic.png".put($content);', 'RETURN f"files:/a/pic.png.bh".put($content);']);
|
|
56
|
+
expect(log).toEqual(['invalidate:a/pic.png', 'invalidate:a/pic.png.bh']);
|
|
57
|
+
const off = new BucketHandle('files', remote, blobs, { blurhash: false });
|
|
58
|
+
expect((await off.put('b.png', 'x')).blurhash).toBeNull();
|
|
59
|
+
const perCall = new BucketHandle('files', remote, blobs, { blurhash: { componentX: 2 } });
|
|
60
|
+
expect((await perCall.put('c.txt', 'x', { blurhash: true })).blurhash).toBeNull();
|
|
61
|
+
expect((await perCall.put('c.png', 'x', { blurhash: { componentY: 3 } })).blurhash).toBeTruthy();
|
|
62
|
+
const failing = { query: async (sql: string) => { if (sql.includes('.bh')) throw new Error('sidecar'); return [undefined]; } } as any;
|
|
63
|
+
const sidecarFail = new BucketHandle('files', failing, blobs, { logger: { warn } });
|
|
64
|
+
expect((await sidecarFail.put('d.png', 'x')).blurhash).toBeNull();
|
|
65
|
+
expect(warn).toHaveBeenCalled();
|
|
66
|
+
const noBlobs = new BucketHandle('files', remote, null);
|
|
67
|
+
expect((await noBlobs.put('e.png', 'x')).blurhash).toBeTruthy();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('blurhash: reads the sidecar through the cache, negative-caches misses, warns on invalid or failing reads', async () => {
|
|
71
|
+
const { remote } = fakeRemote();
|
|
72
|
+
const warn = vi.fn();
|
|
73
|
+
const { blobs } = fakeBlobs();
|
|
74
|
+
const h = new BucketHandle('bucketA', remote, blobs, { logger: { warn } });
|
|
75
|
+
expect(await h.blurhash('img.png')).toBe('LEHV6nWB2yk8pyo0adR*.7kCMdnj');
|
|
76
|
+
const missing = new BucketHandle('bucketB', remote, { ...blobs, read: async () => null } as any, { logger: { warn } });
|
|
77
|
+
expect(await missing.blurhash('none.png')).toBeNull();
|
|
78
|
+
expect(await missing.blurhash('none.png')).toBeNull();
|
|
79
|
+
const invalid = new BucketHandle('bucketC', remote, { ...blobs, read: async () => new Blob(['not-a-hash']) } as any, { logger: { warn } });
|
|
80
|
+
expect(await invalid.blurhash('bad.png')).toBeNull();
|
|
81
|
+
const throwing = new BucketHandle('bucketD', remote, { ...blobs, read: async () => { throw new Error('io'); } } as any, { logger: { warn } });
|
|
82
|
+
expect(await throwing.blurhash('err.png')).toBeNull();
|
|
83
|
+
expect(warn).toHaveBeenCalledTimes(2);
|
|
84
|
+
await new BucketHandle('bucketB', remote, blobs).put('none.png', 'x');
|
|
85
|
+
expect(await new BucketHandle('bucketB', remote, blobs).blurhash('none.png')).toBeTruthy();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('get / read / url / pin / evict / prefetch / delete / exists / head / copy / rename / list', async () => {
|
|
89
|
+
const { remote, calls } = fakeRemote({ '.get()': 'content', '.exists()': true, '.head()': { size: 1 }, '.list()': ['a'] });
|
|
90
|
+
const { blobs, log } = fakeBlobs();
|
|
91
|
+
const h = new BucketHandle('files', remote, blobs);
|
|
92
|
+
expect(await h.get('p')).toBe('content');
|
|
93
|
+
expect(await (await h.read('p'))!.text()).toBe('x');
|
|
94
|
+
expect(await new BucketHandle('files', remote, null).read('p')).toBeInstanceOf(Blob);
|
|
95
|
+
expect(await h.url('p')).toEqual({ url: 'blob:p', release: expect.any(Function) });
|
|
96
|
+
expect(await new BucketHandle('files', remote, null).url('p')).toBeNull();
|
|
97
|
+
h.pin('p');
|
|
98
|
+
h.unpin('p');
|
|
99
|
+
await h.evict('p');
|
|
100
|
+
await h.prefetch(['a', 'b']);
|
|
101
|
+
await h.delete('doc.txt');
|
|
102
|
+
await h.delete('img.png');
|
|
103
|
+
const flaky = { query: async (sql: string) => { if (sql.includes('.bh".delete')) throw new Error('nope'); return [undefined]; } } as any;
|
|
104
|
+
await new BucketHandle('files', flaky, blobs).delete('img2.png');
|
|
105
|
+
expect(await h.exists('p')).toBe(true);
|
|
106
|
+
expect(await h.head('p')).toEqual({ size: 1 });
|
|
107
|
+
await h.copy('a', 'b');
|
|
108
|
+
await h.rename('a', 'b');
|
|
109
|
+
expect(await h.list()).toEqual(['a']);
|
|
110
|
+
expect(await h.list('pre/')).toEqual(['a']);
|
|
111
|
+
expect(log).toContain('pin:p:true');
|
|
112
|
+
expect(log).toContain('pin:p:false');
|
|
113
|
+
expect(log).toContain('prefetch:a+b');
|
|
114
|
+
expect(calls.some(([s]) => s === 'RETURN f"files:/img.png.bh".delete();')).toBe(true);
|
|
115
|
+
expect(calls.some(([s]) => s === 'RETURN f"files:/pre/".list();')).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('r2 buckets (files with the SSP)', () => {
|
|
120
|
+
function fakeSsp(files: Record<string, string> = {}) {
|
|
121
|
+
const requests: Array<{ method: string; url: string; auth: string | null; body: unknown }> = [];
|
|
122
|
+
const fetchMock = vi.fn(async (url: string, init: RequestInit) => {
|
|
123
|
+
const headers = (init.headers ?? {}) as Record<string, string>;
|
|
124
|
+
requests.push({ method: init.method ?? 'GET', url, auth: headers.Authorization ?? null, body: init.body });
|
|
125
|
+
const key = decodeURIComponent(new URL(url).pathname.replace(/^\/bucket\/[^/]+\//, ''));
|
|
126
|
+
const json = (status: number, value: unknown) => new Response(JSON.stringify(value), { status });
|
|
127
|
+
if (headers.Authorization !== 'Bearer tok') return json(401, { error: 'invalid or expired token' });
|
|
128
|
+
switch (init.method) {
|
|
129
|
+
case 'PUT':
|
|
130
|
+
files[key] = typeof init.body === 'string' ? init.body : 'bytes';
|
|
131
|
+
return json(200, { ok: true });
|
|
132
|
+
case 'GET':
|
|
133
|
+
return key in files ? new Response(files[key], { status: 200 }) : json(404, { error: 'no such file' });
|
|
134
|
+
case 'DELETE':
|
|
135
|
+
delete files[key];
|
|
136
|
+
return json(200, { ok: true });
|
|
137
|
+
default: {
|
|
138
|
+
const op = JSON.parse(String(init.body));
|
|
139
|
+
if (op.op === 'exists') return json(200, key in files);
|
|
140
|
+
if (op.op === 'head') return json(200, key in files ? { key: `/${key}`, size: files[key].length } : null);
|
|
141
|
+
if (!(key in files)) return json(404, { error: 'no such file' });
|
|
142
|
+
files[op.target] = files[key];
|
|
143
|
+
if (op.op === 'rename') delete files[key];
|
|
144
|
+
return json(200, { ok: true });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
149
|
+
return { requests, files };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const remote = (token: string | null = 'tok') => ({ getAuthToken: () => token, query: vi.fn() }) as any;
|
|
153
|
+
const r2 = (r = remote()) =>
|
|
154
|
+
new BucketHandle('pics', r, null, { backend: 'r2', files: { endpoint: 'https://app.db.example/' }, blurhash: false });
|
|
155
|
+
|
|
156
|
+
it('reads and writes over HTTP with the session token, never the database socket', async () => {
|
|
157
|
+
const { requests, files } = fakeSsp();
|
|
158
|
+
const db = remote();
|
|
159
|
+
const h = r2(db);
|
|
160
|
+
await h.put('u1/a b.png', 'PNG');
|
|
161
|
+
expect(files['u1/a b.png']).toBe('PNG');
|
|
162
|
+
expect(requests[0]).toMatchObject({ method: 'PUT', url: 'https://app.db.example/bucket/pics/u1/a%20b.png', auth: 'Bearer tok' });
|
|
163
|
+
expect(await ((await h.get('u1/a b.png')) as Blob).text()).toBe('PNG');
|
|
164
|
+
expect(await h.get('missing.png')).toBeNull();
|
|
165
|
+
expect(await h.exists('u1/a b.png')).toBe(true);
|
|
166
|
+
expect(await h.head('u1/a b.png')).toEqual({ key: '/u1/a b.png', size: 3 });
|
|
167
|
+
await h.copy('u1/a b.png', 'u1/c.png');
|
|
168
|
+
await h.rename('u1/c.png', 'u1/d.png');
|
|
169
|
+
expect(Object.keys(files).sort()).toEqual(['u1/a b.png', 'u1/d.png']);
|
|
170
|
+
await expect(h.copy('nope.png', 'x.png')).rejects.toThrow(/no file/);
|
|
171
|
+
await h.delete('u1/a b.png');
|
|
172
|
+
expect(await h.exists('u1/a b.png')).toBe(false);
|
|
173
|
+
await expect(h.list()).rejects.toThrow(/listing/);
|
|
174
|
+
expect(db.query).not.toHaveBeenCalled();
|
|
175
|
+
vi.unstubAllGlobals();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('surfaces refusals and a missing endpoint as errors', async () => {
|
|
179
|
+
fakeSsp({ 'a.png': 'x' });
|
|
180
|
+
await expect(r2(remote(null)).get('a.png')).rejects.toThrow(/401.*invalid or expired token/);
|
|
181
|
+
const noEndpoint = new BucketHandle('pics', remote(), null, { backend: 'r2', files: {} });
|
|
182
|
+
await expect(noEndpoint.get('a.png')).rejects.toThrow(/files\.endpoint/);
|
|
183
|
+
vi.unstubAllGlobals();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('a bucket without backend r2 keeps using the database', async () => {
|
|
187
|
+
const db = { getAuthToken: () => 'tok', query: vi.fn(async () => ['content']) } as any;
|
|
188
|
+
const fetchMock = vi.fn();
|
|
189
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
190
|
+
const h = new BucketHandle('pics', db, null, { files: { endpoint: 'https://app.db.example' } });
|
|
191
|
+
expect(await h.get('a.png')).toBe('content');
|
|
192
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
193
|
+
vi.unstubAllGlobals();
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import type { RemoteDatabaseService } from '../services/database/remote';
|
|
2
|
+
import type { BlobCache, BlobReadOptions, BlobUrlLease } from '../services/blobs/index';
|
|
3
|
+
import {
|
|
4
|
+
blurhashSidecarPath,
|
|
5
|
+
encodeImageToBlurhash,
|
|
6
|
+
isImagePath,
|
|
7
|
+
isBlurhashValid,
|
|
8
|
+
type BlurhashSetting,
|
|
9
|
+
type BlurhashEncodeOptions,
|
|
10
|
+
} from '../utils/blurhash';
|
|
11
|
+
|
|
12
|
+
/** Coerce whatever the `.get()` RPC hands back into a Blob. */
|
|
13
|
+
export function bucketContentToBlob(content: unknown): Blob | null {
|
|
14
|
+
if (content == null) return null;
|
|
15
|
+
if (content instanceof Blob) return content;
|
|
16
|
+
if (typeof content === 'string') return new Blob([content]);
|
|
17
|
+
if (content instanceof ArrayBuffer) return new Blob([content]);
|
|
18
|
+
// Uint8Array and friends. The cast sidesteps `ArrayBufferLike` vs
|
|
19
|
+
// `ArrayBuffer` (SharedArrayBuffer) in lib.dom's BlobPart.
|
|
20
|
+
if (ArrayBuffer.isView(content)) return new Blob([content as unknown as BlobPart]);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface BucketPutOptions {
|
|
25
|
+
/** Override the client-level {@link Sp00kyConfig.blurhash} setting for this put. */
|
|
26
|
+
blurhash?: BlurhashSetting;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface BucketPutResult {
|
|
30
|
+
/** The computed blurhash when the content was a hashable image; else null. */
|
|
31
|
+
blurhash: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface BucketHandleSettings {
|
|
35
|
+
blurhash?: BlurhashSetting;
|
|
36
|
+
logger?: { warn: (obj: unknown, msg?: string) => void };
|
|
37
|
+
/** The bucket's backend as generated (`'r2'`: files live with the SSP). */
|
|
38
|
+
backend?: string;
|
|
39
|
+
/** {@link Sp00kyConfig.files}: where `r2` bucket files are read and written. */
|
|
40
|
+
files?: { endpoint?: string };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A handle's settings from the client config and the bucket's generated schema. */
|
|
44
|
+
export function bucketSettings(
|
|
45
|
+
config: { blurhash?: BlurhashSetting; files?: { endpoint?: string }; schema: { buckets?: readonly { name: string; backend?: string }[] } },
|
|
46
|
+
name: string,
|
|
47
|
+
logger: { warn: (obj: unknown, msg?: string) => void }
|
|
48
|
+
): BucketHandleSettings {
|
|
49
|
+
const backend = config.schema.buckets?.find((b) => b.name === name)?.backend;
|
|
50
|
+
return { blurhash: config.blurhash, logger, backend, files: config.files };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Where a bucket's bytes go: SurrealDB itself, or the SSP for `r2` buckets. */
|
|
54
|
+
interface BucketIo {
|
|
55
|
+
put(path: string, content: string | Uint8Array | Blob): Promise<void>;
|
|
56
|
+
/** The file, or null when it does not exist. */
|
|
57
|
+
get(path: string): Promise<unknown>;
|
|
58
|
+
delete(path: string): Promise<void>;
|
|
59
|
+
exists(path: string): Promise<boolean>;
|
|
60
|
+
head(path: string): Promise<Record<string, unknown>>;
|
|
61
|
+
copy(sourcePath: string, targetPath: string): Promise<void>;
|
|
62
|
+
rename(sourcePath: string, targetPath: string): Promise<void>;
|
|
63
|
+
list(prefix: string): Promise<string[]>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `f"bucket:/path"` statements over the database connection. */
|
|
67
|
+
function surrealIo(bucket: string, remote: RemoteDatabaseService): BucketIo {
|
|
68
|
+
const file = (path: string) => `f"${bucket}:/${path}"`;
|
|
69
|
+
return {
|
|
70
|
+
async put(path, content) {
|
|
71
|
+
await remote.query(`RETURN ${file(path)}.put($content);`, { content });
|
|
72
|
+
},
|
|
73
|
+
async get(path) {
|
|
74
|
+
const [result] = await remote.query<[unknown]>(`RETURN ${file(path)}.get();`);
|
|
75
|
+
return result;
|
|
76
|
+
},
|
|
77
|
+
async delete(path) {
|
|
78
|
+
await remote.query(`RETURN ${file(path)}.delete();`);
|
|
79
|
+
},
|
|
80
|
+
async exists(path) {
|
|
81
|
+
const [result] = await remote.query<[boolean]>(`RETURN ${file(path)}.exists();`);
|
|
82
|
+
return result;
|
|
83
|
+
},
|
|
84
|
+
async head(path) {
|
|
85
|
+
const [result] = await remote.query<[Record<string, unknown>]>(`RETURN ${file(path)}.head();`);
|
|
86
|
+
return result;
|
|
87
|
+
},
|
|
88
|
+
async copy(sourcePath, targetPath) {
|
|
89
|
+
await remote.query(`RETURN ${file(sourcePath)}.copy($target);`, { target: targetPath });
|
|
90
|
+
},
|
|
91
|
+
async rename(sourcePath, targetPath) {
|
|
92
|
+
await remote.query(`RETURN ${file(sourcePath)}.rename($target);`, { target: targetPath });
|
|
93
|
+
},
|
|
94
|
+
async list(prefix) {
|
|
95
|
+
const [result] = await remote.query<[string[]]>(`RETURN ${file(prefix)}.list();`);
|
|
96
|
+
return result;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* `BACKEND "r2"` buckets: the SSP keeps the files (R2 on Cloudflare, a
|
|
103
|
+
* directory under `spky dev`) and serves them at `/bucket/<name>/<key>`,
|
|
104
|
+
* after asking SurrealDB, as the signed-in user, whether the bucket's
|
|
105
|
+
* PERMISSIONS clause allows it. The request carries the session's token.
|
|
106
|
+
*/
|
|
107
|
+
function sspIo(bucket: string, remote: RemoteDatabaseService, endpoint: string | undefined): BucketIo {
|
|
108
|
+
const base = () => {
|
|
109
|
+
if (!endpoint) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Bucket '${bucket}' is stored with the SSP (BACKEND "r2"): set \`files.endpoint\` in the client config to the SSP's URL.`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return endpoint.replace(/\/+$/, '');
|
|
115
|
+
};
|
|
116
|
+
const url = (path: string) =>
|
|
117
|
+
`${base()}/bucket/${encodeURIComponent(bucket)}/${path
|
|
118
|
+
.split('/')
|
|
119
|
+
.filter(Boolean)
|
|
120
|
+
.map(encodeURIComponent)
|
|
121
|
+
.join('/')}`;
|
|
122
|
+
const send = async (method: string, path: string, body?: BodyInit, contentType?: string): Promise<Response> => {
|
|
123
|
+
const headers: Record<string, string> = {};
|
|
124
|
+
const token = remote.getAuthToken();
|
|
125
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
126
|
+
if (contentType) headers['Content-Type'] = contentType;
|
|
127
|
+
const response = await fetch(url(path), { method, headers, body, credentials: 'omit' });
|
|
128
|
+
if (!response.ok && response.status !== 404) {
|
|
129
|
+
let message = response.statusText;
|
|
130
|
+
try {
|
|
131
|
+
message = ((await response.json()) as { error?: string }).error ?? message;
|
|
132
|
+
} catch {
|
|
133
|
+
// Not JSON: keep the status text.
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`Bucket '${bucket}' ${method} ${path} failed (${response.status}): ${message}`);
|
|
136
|
+
}
|
|
137
|
+
return response;
|
|
138
|
+
};
|
|
139
|
+
const op = async <T>(path: string, body: Record<string, unknown>): Promise<T | null> => {
|
|
140
|
+
const response = await send('POST', path, JSON.stringify(body), 'application/json');
|
|
141
|
+
if (response.status === 404) return null;
|
|
142
|
+
return (await response.json()) as T;
|
|
143
|
+
};
|
|
144
|
+
const missing = (path: string) => new Error(`Bucket '${bucket}': no file at ${path}`);
|
|
145
|
+
return {
|
|
146
|
+
async put(path, content) {
|
|
147
|
+
const type = content instanceof Blob ? content.type : undefined;
|
|
148
|
+
await send('PUT', path, content as BodyInit, type || 'application/octet-stream');
|
|
149
|
+
},
|
|
150
|
+
async get(path) {
|
|
151
|
+
const response = await send('GET', path);
|
|
152
|
+
return response.status === 404 ? null : await response.blob();
|
|
153
|
+
},
|
|
154
|
+
async delete(path) {
|
|
155
|
+
await send('DELETE', path);
|
|
156
|
+
},
|
|
157
|
+
async exists(path) {
|
|
158
|
+
return (await op<boolean>(path, { op: 'exists' })) ?? false;
|
|
159
|
+
},
|
|
160
|
+
async head(path) {
|
|
161
|
+
return (await op<Record<string, unknown>>(path, { op: 'head' })) as Record<string, unknown>;
|
|
162
|
+
},
|
|
163
|
+
async copy(sourcePath, targetPath) {
|
|
164
|
+
if ((await op(sourcePath, { op: 'copy', target: targetPath })) === null) throw missing(sourcePath);
|
|
165
|
+
},
|
|
166
|
+
async rename(sourcePath, targetPath) {
|
|
167
|
+
if ((await op(sourcePath, { op: 'rename', target: targetPath })) === null) throw missing(sourcePath);
|
|
168
|
+
},
|
|
169
|
+
async list() {
|
|
170
|
+
// SurrealDB refuses `list` to record users; the SSP does too.
|
|
171
|
+
throw new Error(`Bucket '${bucket}': listing is not available (BACKEND "r2")`);
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Paths known to have no blurhash sidecar, per tab. The blob cache has no
|
|
178
|
+
* negative caching, so without this every mount of a hashless image would pay
|
|
179
|
+
* one serialized remote read. Cleared when a put writes a sidecar or a delete
|
|
180
|
+
* removes the image. Keyed `${bucket}:${path}` (the image path, not the sidecar).
|
|
181
|
+
*/
|
|
182
|
+
const missingBlurhash = new Set<string>();
|
|
183
|
+
|
|
184
|
+
export class BucketHandle {
|
|
185
|
+
private io: BucketIo;
|
|
186
|
+
|
|
187
|
+
constructor(
|
|
188
|
+
private bucketName: string,
|
|
189
|
+
remote: RemoteDatabaseService,
|
|
190
|
+
/** Absent on the raw handle the cache itself reads through. */
|
|
191
|
+
private blobs?: BlobCache | null,
|
|
192
|
+
private settings?: BucketHandleSettings
|
|
193
|
+
) {
|
|
194
|
+
this.io =
|
|
195
|
+
settings?.backend === 'r2'
|
|
196
|
+
? sspIo(bucketName, remote, settings.files?.endpoint)
|
|
197
|
+
: surrealIo(bucketName, remote);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Effective blurhash setting: per-call option > client config > default ON. */
|
|
201
|
+
private resolveBlurhash(option?: BlurhashSetting): BlurhashEncodeOptions | null {
|
|
202
|
+
const setting = option ?? this.settings?.blurhash ?? true;
|
|
203
|
+
if (setting === false) return null;
|
|
204
|
+
return setting === true ? {} : setting;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async put(
|
|
208
|
+
path: string,
|
|
209
|
+
content: string | Uint8Array | Blob,
|
|
210
|
+
options?: BucketPutOptions
|
|
211
|
+
): Promise<BucketPutResult> {
|
|
212
|
+
// Start hashing while the upload is in flight; both browser-side costs
|
|
213
|
+
// overlap and the sidecar put only queues once the main put resolved.
|
|
214
|
+
const encodeOptions = this.resolveBlurhash(options?.blurhash);
|
|
215
|
+
const hashPromise =
|
|
216
|
+
encodeOptions && isImagePath(path)
|
|
217
|
+
? encodeImageToBlurhash(content, encodeOptions)
|
|
218
|
+
: Promise.resolve(null);
|
|
219
|
+
|
|
220
|
+
await this.io.put(path, content);
|
|
221
|
+
// A path can be overwritten, so anything cached under it is now wrong.
|
|
222
|
+
await this.blobs?.invalidate({ bucket: this.bucketName, path });
|
|
223
|
+
|
|
224
|
+
// The sidecar is best-effort: a hash or sidecar failure must never fail
|
|
225
|
+
// the image put that triggered it.
|
|
226
|
+
let hash: string | null = null;
|
|
227
|
+
try {
|
|
228
|
+
hash = await hashPromise;
|
|
229
|
+
if (hash) {
|
|
230
|
+
const sidecar = blurhashSidecarPath(path);
|
|
231
|
+
await this.io.put(sidecar, hash);
|
|
232
|
+
await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
|
|
233
|
+
missingBlurhash.delete(`${this.bucketName}:${path}`);
|
|
234
|
+
}
|
|
235
|
+
} catch (error) {
|
|
236
|
+
hash = null;
|
|
237
|
+
this.settings?.logger?.warn({ error, path }, 'blurhash sidecar put failed');
|
|
238
|
+
}
|
|
239
|
+
return { blurhash: hash };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The blurhash stored alongside an uploaded image (see
|
|
244
|
+
* {@link blurhashSidecarPath}), or null when there is none. Reads through the
|
|
245
|
+
* blob cache, so a warm client answers from OPFS without a network hop, and
|
|
246
|
+
* misses are remembered per tab so a hashless image costs at most one
|
|
247
|
+
* serialized remote read per session.
|
|
248
|
+
*/
|
|
249
|
+
async blurhash(path: string): Promise<string | null> {
|
|
250
|
+
const cacheKey = `${this.bucketName}:${path}`;
|
|
251
|
+
if (missingBlurhash.has(cacheKey)) return null;
|
|
252
|
+
try {
|
|
253
|
+
const blob = await this.read(blurhashSidecarPath(path), { persist: true });
|
|
254
|
+
if (!blob) {
|
|
255
|
+
missingBlurhash.add(cacheKey);
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
const hash = (await blob.text()).trim();
|
|
259
|
+
if (!isBlurhashValid(hash).result) {
|
|
260
|
+
this.settings?.logger?.warn({ path }, 'blurhash sidecar holds an invalid hash');
|
|
261
|
+
missingBlurhash.add(cacheKey);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
return hash;
|
|
265
|
+
} catch (error) {
|
|
266
|
+
// Transient failure: do NOT negative-cache, the next mount may succeed.
|
|
267
|
+
this.settings?.logger?.warn({ error, path }, 'blurhash sidecar read failed');
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async get(path: string): Promise<unknown> {
|
|
273
|
+
return this.io.get(path);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Read through the local blob cache: OPFS first, the bucket second. Unlike
|
|
278
|
+
* {@link get} this survives a reload and works offline. Returns null when the
|
|
279
|
+
* file exists in neither place.
|
|
280
|
+
*/
|
|
281
|
+
async read(path: string, options?: BlobReadOptions): Promise<Blob | null> {
|
|
282
|
+
if (!this.blobs) return bucketContentToBlob(await this.get(path));
|
|
283
|
+
return this.blobs.read({ bucket: this.bucketName, path }, options);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* A refcounted object URL for `path`, suitable for `<img src>`. The caller
|
|
288
|
+
* MUST call `release()` when the URL goes off screen. Returns null when the
|
|
289
|
+
* file does not exist, or when object URLs are unavailable (non-browser).
|
|
290
|
+
*/
|
|
291
|
+
async url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null> {
|
|
292
|
+
if (!this.blobs) return null;
|
|
293
|
+
return this.blobs.acquireUrl({ bucket: this.bucketName, path }, options);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Exempt `path` from pressure eviction. Pinned bytes never expire. */
|
|
297
|
+
pin(path: string): void {
|
|
298
|
+
this.blobs?.setPinned({ bucket: this.bucketName, path }, true);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
unpin(path: string): void {
|
|
302
|
+
this.blobs?.setPinned({ bucket: this.bucketName, path }, false);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Drop `path` from the local cache without touching the remote file. */
|
|
306
|
+
async evict(path: string): Promise<void> {
|
|
307
|
+
await this.blobs?.invalidate({ bucket: this.bucketName, path });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Warm the cache for offline use. Already-cached paths are skipped. */
|
|
311
|
+
async prefetch(paths: string[]): Promise<void> {
|
|
312
|
+
await this.blobs?.prefetch(paths.map((path) => ({ bucket: this.bucketName, path })));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async delete(path: string): Promise<void> {
|
|
316
|
+
await this.io.delete(path);
|
|
317
|
+
await this.blobs?.invalidate({ bucket: this.bucketName, path });
|
|
318
|
+
// Symmetry with put: an image's blurhash sidecar dies with it. Best-effort,
|
|
319
|
+
// the sidecar may simply not exist.
|
|
320
|
+
if (isImagePath(path)) {
|
|
321
|
+
const sidecar = blurhashSidecarPath(path);
|
|
322
|
+
try {
|
|
323
|
+
await this.io.delete(sidecar);
|
|
324
|
+
await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
|
|
325
|
+
} catch {
|
|
326
|
+
// Nothing to clean up.
|
|
327
|
+
}
|
|
328
|
+
missingBlurhash.delete(`${this.bucketName}:${path}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async exists(path: string): Promise<boolean> {
|
|
333
|
+
return this.io.exists(path);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async head(path: string): Promise<Record<string, unknown>> {
|
|
337
|
+
return this.io.head(path);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async copy(sourcePath: string, targetPath: string): Promise<void> {
|
|
341
|
+
await this.io.copy(sourcePath, targetPath);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async rename(sourcePath: string, targetPath: string): Promise<void> {
|
|
345
|
+
await this.io.rename(sourcePath, targetPath);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async list(prefix?: string): Promise<string[]> {
|
|
349
|
+
return this.io.list(prefix ?? '');
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import type { RuntimeEvent } from '../kernel/events';
|
|
3
|
+
import { defaultEnv } from '../query/env';
|
|
4
|
+
import { route } from './router';
|
|
5
|
+
|
|
6
|
+
const env = defaultEnv({ tables: [] } as any);
|
|
7
|
+
|
|
8
|
+
describe('route', () => {
|
|
9
|
+
it('maps every event to a saga and the documented lane', () => {
|
|
10
|
+
const cases: Array<[RuntimeEvent, string | undefined]> = [
|
|
11
|
+
[{ type: 'EnsureRegistered' }, 'serial:ensure'],
|
|
12
|
+
[{ type: 'RegisterRemote', hash: 'h' }, 'dedupe:register:h'],
|
|
13
|
+
[{ type: 'ReadDirtyMembership' }, 'serial:membership'],
|
|
14
|
+
[{ type: 'ReadMembership', hashes: ['h'] }, 'serial:membership'],
|
|
15
|
+
[{ type: 'FetchRows' }, 'serial:fetch'],
|
|
16
|
+
[{ type: 'Materialize', hash: 'h' }, 'serial:mat:h'],
|
|
17
|
+
[{ type: 'MaterializeDirty' }, undefined],
|
|
18
|
+
[{ type: 'StreamUpdate', update: { queryHash: 'h', localArray: [] } }, 'serial:stream:h'],
|
|
19
|
+
[{ type: 'LifecycleTick' }, 'dedupe:lifecycle'],
|
|
20
|
+
[{ type: 'HeartbeatNow' }, 'dedupe:lifecycle'],
|
|
21
|
+
[{ type: 'AckPrune' }, 'dedupe:ack-prune'],
|
|
22
|
+
[{ type: 'GcTick' }, 'dedupe:gc'],
|
|
23
|
+
[{ type: 'Drain' }, 'serial:outbox'],
|
|
24
|
+
[{ type: 'FlushWrite', key: 'k' }, 'serial:outbox-write'],
|
|
25
|
+
[{ type: 'FlushPendingWrites' }, undefined],
|
|
26
|
+
[{ type: 'AdoptPendingWrites' }, 'serial:outbox-write'],
|
|
27
|
+
[{ type: 'PollTick' }, 'dedupe:poll'],
|
|
28
|
+
[{ type: 'SelfHealTick' }, 'dedupe:heal'],
|
|
29
|
+
[{ type: 'SyncOutcome', ok: true }, 'serial:health'],
|
|
30
|
+
[{ type: 'ConnectionChanged', state: 'connected' }, 'serial:connection'],
|
|
31
|
+
[{ type: 'LiveStart' }, 'serial:live'],
|
|
32
|
+
[{ type: 'LiveChange', hashes: [] }, 'serial:live-change'],
|
|
33
|
+
[{ type: 'TabRole', role: 'leader' }, 'serial:tabs'],
|
|
34
|
+
[{ type: 'TabMessage', message: {} }, 'serial:tabs'],
|
|
35
|
+
[{ type: 'StartRemote' }, 'dedupe:start-remote'],
|
|
36
|
+
[{ type: 'PrimeCircuit' }, 'dedupe:prime'],
|
|
37
|
+
[{ type: 'VersionsPrimed', entries: [] }, undefined],
|
|
38
|
+
[{ type: 'WarmBlobs' }, 'dedupe:blobs'],
|
|
39
|
+
[{ type: 'AuthFlip', userId: null }, 'serial:bucket'],
|
|
40
|
+
[{ type: 'BucketSwitch', target: 'x' }, 'serial:bucket'],
|
|
41
|
+
[{ type: 'PageHide' }, undefined],
|
|
42
|
+
];
|
|
43
|
+
for (const [event, lane] of cases) {
|
|
44
|
+
const target = route(env, event);
|
|
45
|
+
expect(typeof target.saga.next).toBe('function');
|
|
46
|
+
expect(target.lane ? `${target.lane.kind}:${target.lane.key}` : undefined).toBe(lane);
|
|
47
|
+
target.saga.return(undefined);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
it('MaterializeDirty fans out one Materialize per dirty hash', async () => {
|
|
51
|
+
const { runPure } = await import('../testing/run-pure');
|
|
52
|
+
const { buildEntry, buildState } = await import('../testing/build');
|
|
53
|
+
const R = await import('../state/reducers');
|
|
54
|
+
const s = R.markDirty(['a', 'b'])(buildState([buildEntry({ def: { hash: 'a' } }), buildEntry({ def: { hash: 'b' } })]));
|
|
55
|
+
const out = await runPure(route(env, { type: 'MaterializeDirty' }).saga, { state: s });
|
|
56
|
+
expect(out.dispatched).toEqual([{ type: 'Materialize', hash: 'a' }, { type: 'Materialize', hash: 'b' }]);
|
|
57
|
+
});
|
|
58
|
+
});
|