@spooky-sync/core 0.0.1-canary.31 → 0.0.1-canary.310

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 (257) hide show
  1. package/AGENTS.md +81 -0
  2. package/README.md +56 -16
  3. package/dist/index.d.ts +2026 -137
  4. package/dist/index.js +14956 -2434
  5. package/dist/live.d.ts +141 -0
  6. package/dist/live.js +709 -0
  7. package/dist/otel/index.d.ts +1 -1
  8. package/dist/otel/index.js +3 -3
  9. package/dist/pure.d.ts +329 -0
  10. package/dist/pure.js +24 -0
  11. package/dist/sqlite-open.js +358 -0
  12. package/dist/sqlite-worker.d.ts +1 -0
  13. package/dist/sqlite-worker.js +439 -0
  14. package/dist/sw.d.ts +207 -0
  15. package/dist/sw.js +393 -0
  16. package/dist/tabs-broker-worker.d.ts +8 -0
  17. package/dist/tabs-broker-worker.js +472 -0
  18. package/dist/types.d.ts +1765 -38
  19. package/dist/worker-edges.js +423 -0
  20. package/dist/worker-impersonation.js +60 -0
  21. package/dist/worker-ref-tables.d.ts +46 -0
  22. package/dist/worker-types.d.ts +112 -0
  23. package/dist/worker-types.js +73 -0
  24. package/package.json +27 -7
  25. package/scripts/check-broker-bundle.mjs +33 -0
  26. package/scripts/check-worker-entries.mjs +172 -0
  27. package/skills/sp00ky-core/SKILL.md +31 -6
  28. package/skills/sp00ky-core/references/config.md +6 -0
  29. package/src/boot/auth-flip.saga.test.ts +135 -0
  30. package/src/boot/auth-flip.saga.ts +77 -0
  31. package/src/boot/boot.saga.test.ts +158 -0
  32. package/src/boot/boot.saga.ts +131 -0
  33. package/src/boot/bucket-switch.saga.test.ts +187 -0
  34. package/src/boot/bucket-switch.saga.ts +133 -0
  35. package/src/boot/preload.saga.test.ts +51 -0
  36. package/src/boot/preload.saga.ts +31 -0
  37. package/src/bucket-blurhash.test.ts +148 -0
  38. package/src/build-globals.d.ts +12 -0
  39. package/src/client/bucket-handle.test.ts +117 -0
  40. package/src/client/bucket-handle.ts +227 -0
  41. package/src/client/router.test.ts +58 -0
  42. package/src/client/router.ts +100 -0
  43. package/src/client/runtime.test.ts +195 -0
  44. package/src/client/runtime.ts +296 -0
  45. package/src/client/services.ts +370 -0
  46. package/src/client/sp00ky-client.test.ts +303 -0
  47. package/src/client/sp00ky-client.ts +586 -0
  48. package/src/events/events.test.ts +2 -1
  49. package/src/events/index.ts +3 -0
  50. package/src/index.ts +69 -1
  51. package/src/kernel/constants.test.ts +19 -0
  52. package/src/kernel/constants.ts +80 -0
  53. package/src/kernel/effects.test.ts +40 -0
  54. package/src/kernel/effects.ts +161 -0
  55. package/src/kernel/events.ts +57 -0
  56. package/src/kernel/interpreter.test.ts +116 -0
  57. package/src/kernel/interpreter.ts +148 -0
  58. package/src/kernel/saga.test.ts +89 -0
  59. package/src/kernel/saga.ts +73 -0
  60. package/src/live/index.test.ts +478 -0
  61. package/src/live/index.ts +1013 -0
  62. package/src/modules/app-release/index.test.ts +125 -0
  63. package/src/modules/app-release/index.ts +209 -0
  64. package/src/modules/auth/auth.local-first.test.ts +141 -0
  65. package/src/modules/auth/events/index.ts +9 -2
  66. package/src/modules/auth/impersonation-banner.ts +443 -0
  67. package/src/modules/auth/impersonation.test.ts +355 -0
  68. package/src/modules/auth/impersonation.ts +179 -0
  69. package/src/modules/auth/index.ts +386 -17
  70. package/src/modules/crdt/crdt-field.ts +294 -0
  71. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  72. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  73. package/src/modules/crdt/index.ts +463 -0
  74. package/src/modules/crdt/loro-loader.ts +25 -0
  75. package/src/modules/devtools/flags.ts +349 -0
  76. package/src/modules/devtools/index.ts +746 -95
  77. package/src/modules/devtools/mutations.test.ts +92 -0
  78. package/src/modules/devtools/mutations.ts +160 -0
  79. package/src/modules/devtools/notify-throttle.test.ts +166 -0
  80. package/src/modules/devtools/state-shape.test.ts +263 -0
  81. package/src/modules/devtools/storage-info.test.ts +79 -0
  82. package/src/modules/devtools/storage-info.ts +168 -0
  83. package/src/modules/devtools/versions.test.ts +74 -0
  84. package/src/modules/devtools/versions.ts +110 -0
  85. package/src/modules/feature-flag/index.test.ts +251 -0
  86. package/src/modules/feature-flag/index.ts +316 -0
  87. package/src/modules/ref-tables.test.ts +91 -0
  88. package/src/modules/ref-tables.ts +88 -0
  89. package/src/modules/web-push/index.test.ts +824 -0
  90. package/src/modules/web-push/index.ts +1027 -0
  91. package/src/mutation/jobs.test.ts +25 -0
  92. package/src/mutation/jobs.ts +41 -0
  93. package/src/mutation/mutation-id.test.ts +37 -0
  94. package/src/mutation/mutation-id.ts +35 -0
  95. package/src/mutation/pending-writes.integration.test.ts +175 -0
  96. package/src/mutation/push.saga.test.ts +285 -0
  97. package/src/mutation/push.saga.ts +217 -0
  98. package/src/mutation/rows.test.ts +231 -0
  99. package/src/mutation/rows.ts +427 -0
  100. package/src/mutation/tray.saga.test.ts +63 -0
  101. package/src/mutation/tray.saga.ts +48 -0
  102. package/src/mutation/write.saga.test.ts +316 -0
  103. package/src/mutation/write.saga.ts +268 -0
  104. package/src/otel/index.ts +10 -7
  105. package/src/pure.ts +65 -0
  106. package/src/push/types.ts +174 -0
  107. package/src/query/env.test.ts +23 -0
  108. package/src/query/env.ts +56 -0
  109. package/src/query/fetch.saga.test.ts +199 -0
  110. package/src/query/fetch.saga.ts +120 -0
  111. package/src/query/hash.test.ts +14 -0
  112. package/src/query/hash.ts +18 -0
  113. package/src/query/id-set-plan.test.ts +132 -0
  114. package/src/query/lifecycle.saga.test.ts +274 -0
  115. package/src/query/lifecycle.saga.ts +159 -0
  116. package/src/query/materialize.saga.test.ts +115 -0
  117. package/src/query/materialize.saga.ts +84 -0
  118. package/src/query/materialize.test.ts +58 -0
  119. package/src/query/materialize.ts +57 -0
  120. package/src/query/membership.saga.test.ts +350 -0
  121. package/src/query/membership.saga.ts +278 -0
  122. package/src/query/membership.test.ts +118 -0
  123. package/src/query/membership.ts +201 -0
  124. package/src/query/register.saga.test.ts +293 -0
  125. package/src/query/register.saga.ts +226 -0
  126. package/src/query/render-set.test.ts +47 -0
  127. package/src/query/render-set.ts +71 -0
  128. package/src/query/sql.test.ts +63 -0
  129. package/src/query/sql.ts +93 -0
  130. package/src/query/window-query.test.ts +70 -0
  131. package/src/query/window-query.ts +195 -0
  132. package/src/services/blobs/blob-cache.test.ts +359 -0
  133. package/src/services/blobs/blob-cache.ts +603 -0
  134. package/src/services/blobs/blob-manifest.ts +227 -0
  135. package/src/services/blobs/blob-store.test.ts +77 -0
  136. package/src/services/blobs/blob-store.ts +359 -0
  137. package/src/services/blobs/blob.fixture.ts +90 -0
  138. package/src/services/blobs/index.ts +70 -0
  139. package/src/services/database/cache-engine.ts +199 -0
  140. package/src/services/database/connection-supervisor.test.ts +318 -0
  141. package/src/services/database/connection-supervisor.ts +422 -0
  142. package/src/services/database/database.query-timeout.test.ts +140 -0
  143. package/src/services/database/database.responses.test.ts +43 -0
  144. package/src/services/database/database.ts +146 -61
  145. package/src/services/database/engine-factory.ts +33 -0
  146. package/src/services/database/errors.ts +34 -0
  147. package/src/services/database/events/index.ts +2 -1
  148. package/src/services/database/index.ts +7 -0
  149. package/src/services/database/landed-body-clear.integration.test.ts +236 -0
  150. package/src/services/database/local-migrator.integration.test.ts +110 -0
  151. package/src/services/database/local-migrator.ts +59 -16
  152. package/src/services/database/local.test.ts +64 -0
  153. package/src/services/database/local.ts +487 -63
  154. package/src/services/database/plan-render.test.ts +159 -0
  155. package/src/services/database/plan-render.ts +108 -0
  156. package/src/services/database/relation-resolver.test.ts +467 -0
  157. package/src/services/database/relation-resolver.ts +0 -0
  158. package/src/services/database/remote.ts +213 -6
  159. package/src/services/database/sqlite-cache-engine.test.ts +617 -0
  160. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  161. package/src/services/database/sqlite-cache-engine.ts +1372 -0
  162. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  163. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  164. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  165. package/src/services/database/sqlite-lock-verify.ts +45 -0
  166. package/src/services/database/sqlite-open.test.ts +150 -0
  167. package/src/services/database/sqlite-open.ts +164 -0
  168. package/src/services/database/sqlite-plan-sql.test.ts +139 -0
  169. package/src/services/database/sqlite-plan-sql.ts +179 -0
  170. package/src/services/database/sqlite-projection.test.ts +121 -0
  171. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  172. package/src/services/database/sqlite-select.test.ts +246 -0
  173. package/src/services/database/sqlite-select.ts +131 -0
  174. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  175. package/src/services/database/sqlite-transport.ts +224 -0
  176. package/src/services/database/sqlite-worker.ts +437 -0
  177. package/src/services/database/surql-translate.ts +416 -0
  178. package/src/services/database/surreal-cache-engine.ts +161 -0
  179. package/src/services/logger/index.test.ts +132 -0
  180. package/src/services/logger/index.ts +189 -7
  181. package/src/services/persistence/localstorage.ts +2 -2
  182. package/src/services/persistence/resilient.ts +10 -3
  183. package/src/services/persistence/surrealdb.ts +4 -4
  184. package/src/services/stream-processor/index.ts +779 -67
  185. package/src/services/stream-processor/permissions.test.ts +47 -0
  186. package/src/services/stream-processor/permissions.ts +53 -0
  187. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  188. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  189. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  190. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  191. package/src/services/stream-processor/wasm-types.ts +58 -2
  192. package/src/services/tabs/broker-client.ts +283 -0
  193. package/src/services/tabs/broker.test.ts +327 -0
  194. package/src/services/tabs/coordinator.test.ts +386 -0
  195. package/src/services/tabs/coordinator.ts +638 -0
  196. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  197. package/src/services/tabs/leader-locks.ts +75 -0
  198. package/src/services/tabs/protocol.ts +262 -0
  199. package/src/services/tabs/support.ts +36 -0
  200. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  201. package/src/sp00ky.ts +5 -395
  202. package/src/state/client-state.ts +200 -0
  203. package/src/state/lifecycle.test.ts +118 -0
  204. package/src/state/lifecycle.ts +86 -0
  205. package/src/state/reducers.test.ts +304 -0
  206. package/src/state/reducers.ts +526 -0
  207. package/src/state/selectors.test.ts +167 -0
  208. package/src/state/selectors.ts +216 -0
  209. package/src/sw/index.test.ts +582 -0
  210. package/src/sw/index.ts +607 -0
  211. package/src/sw/store.ts +96 -0
  212. package/src/sync/connection.saga.test.ts +86 -0
  213. package/src/sync/connection.saga.ts +85 -0
  214. package/src/sync/live.saga.test.ts +178 -0
  215. package/src/sync/live.saga.ts +107 -0
  216. package/src/sync/policy.health.test.ts +56 -0
  217. package/src/sync/policy.test.ts +659 -0
  218. package/src/sync/policy.ts +502 -0
  219. package/src/sync/poll.saga.test.ts +51 -0
  220. package/src/sync/poll.saga.ts +36 -0
  221. package/src/sync/tabs.saga.test.ts +78 -0
  222. package/src/sync/tabs.saga.ts +96 -0
  223. package/src/testing/adapters.ts +133 -0
  224. package/src/testing/build.ts +63 -0
  225. package/src/testing/fake-services.ts +48 -0
  226. package/src/testing/run-pure.test.ts +92 -0
  227. package/src/testing/run-pure.ts +128 -0
  228. package/src/testing/services.ts +17 -0
  229. package/src/types.ts +624 -10
  230. package/src/utils/blurhash.ts +90 -0
  231. package/src/utils/edges.ts +39 -0
  232. package/src/utils/error-classification.test.ts +54 -0
  233. package/src/utils/error-classification.ts +25 -0
  234. package/src/utils/index.ts +78 -12
  235. package/src/utils/parser.test.ts +67 -102
  236. package/src/utils/parser.ts +64 -2
  237. package/src/utils/semver.test.ts +32 -0
  238. package/src/utils/semver.ts +30 -0
  239. package/src/utils/sha256.ts +15 -0
  240. package/src/utils/surql.ts +30 -18
  241. package/src/utils/utils.test.ts +3 -2
  242. package/tsdown.config.ts +117 -3
  243. package/vitest.config.ts +48 -0
  244. package/src/modules/cache/index.ts +0 -241
  245. package/src/modules/cache/types.ts +0 -19
  246. package/src/modules/data/data.test.ts +0 -58
  247. package/src/modules/data/index.ts +0 -879
  248. package/src/modules/sync/engine.ts +0 -173
  249. package/src/modules/sync/events/index.ts +0 -77
  250. package/src/modules/sync/index.ts +0 -3
  251. package/src/modules/sync/queue/index.ts +0 -2
  252. package/src/modules/sync/queue/queue-down.ts +0 -89
  253. package/src/modules/sync/queue/queue-up.ts +0 -223
  254. package/src/modules/sync/scheduler.ts +0 -84
  255. package/src/modules/sync/sync.ts +0 -421
  256. package/src/modules/sync/utils.test.ts +0 -311
  257. package/src/modules/sync/utils.ts +0 -167
@@ -0,0 +1,31 @@
1
+ import type { QueryHash } from '../types';
2
+ import type { Saga } from '../kernel/saga';
3
+ import { fx } from '../kernel/effects';
4
+ import { settleFailed, settled } from '../state/selectors';
5
+ import type { SagaEnv } from '../query/env';
6
+ import type { RegisterInput } from '../query/register.saga';
7
+ import { registerLocal } from '../query/register.saga';
8
+
9
+ export class PreloadFailedError extends Error {
10
+ constructor(public readonly hash: QueryHash) {
11
+ super(`Preload could not settle: the registration of ${hash} failed`);
12
+ this.name = 'PreloadFailedError';
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Preload = a registered query nobody subscribes to. Resolved before on this
18
+ * device: returns as soon as the entry exists (its rows paint from cache).
19
+ * Never resolved: blocks until the server's membership and every body are
20
+ * local and the first materialization ran. The entry is evicted like any
21
+ * other query a ttl after it was last watched (or registered).
22
+ */
23
+ export function* preload(env: SagaEnv, input: RegisterInput): Saga<{ hash: QueryHash; waited: boolean }> {
24
+ const hash = yield* registerLocal(env, input);
25
+ const phase = (yield fx.state.read((s) => s.queries.get(hash)?.lifecycle.phase)) as string | undefined;
26
+ if (phase !== 'cold') return { hash, waited: false };
27
+ yield fx.state.wait((s) => settled(s, hash) || settleFailed(s, hash));
28
+ const failed = (yield fx.state.read((s) => settleFailed(s, hash))) as boolean;
29
+ if (failed) throw new PreloadFailedError(hash);
30
+ return { hash, waited: true };
31
+ }
@@ -0,0 +1,148 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { BucketHandle } from './sp00ky';
3
+ import type { RemoteDatabaseService } from './services/database/index';
4
+ import {
5
+ blurhashSidecarPath,
6
+ isImagePath,
7
+ encodeImageToBlurhash,
8
+ encodeBlurhash,
9
+ } from './utils/blurhash';
10
+
11
+ // A real 1x1 hash so isBlurhashValid passes in the read-path tests.
12
+ const VALID_HASH = encodeBlurhash(new Uint8ClampedArray([12, 34, 56, 255]), 1, 1, 1, 1);
13
+
14
+ function makeRemote(queries: string[]) {
15
+ return {
16
+ query: vi.fn(async (surql: string) => {
17
+ queries.push(surql);
18
+ return [null];
19
+ }),
20
+ } as unknown as RemoteDatabaseService;
21
+ }
22
+
23
+ describe('blurhash path helpers', () => {
24
+ it('appends .bh for the sidecar', () => {
25
+ expect(blurhashSidecarPath('a/b_t.webp')).toBe('a/b_t.webp.bh');
26
+ });
27
+
28
+ it('recognizes image extensions case-insensitively', () => {
29
+ expect(isImagePath('x/y.webp')).toBe(true);
30
+ expect(isImagePath('x/y.PNG')).toBe(true);
31
+ expect(isImagePath('x/y.webp.bh')).toBe(false);
32
+ expect(isImagePath('x/notes.txt')).toBe(false);
33
+ });
34
+ });
35
+
36
+ describe('encodeImageToBlurhash outside a browser', () => {
37
+ it('returns null instead of throwing (no createImageBitmap in node)', async () => {
38
+ await expect(encodeImageToBlurhash(new Uint8Array([1, 2, 3]))).resolves.toBeNull();
39
+ });
40
+ });
41
+
42
+ describe('BucketHandle.put', () => {
43
+ let queries: string[];
44
+
45
+ beforeEach(() => {
46
+ queries = [];
47
+ });
48
+
49
+ it('image put succeeds with blurhash null when encoding is unavailable', async () => {
50
+ const handle = new BucketHandle('covers', makeRemote(queries), null);
51
+ const result = await handle.put('a/b_t.webp', new Uint8Array([1]));
52
+ expect(result).toEqual({ blurhash: null });
53
+ // Only the image put went out; no sidecar for a failed/unavailable encode.
54
+ expect(queries).toHaveLength(1);
55
+ expect(queries[0]).toContain('covers:/a/b_t.webp".put');
56
+ });
57
+
58
+ it('skips hashing entirely for non-image paths and when disabled', async () => {
59
+ const handle = new BucketHandle('covers', makeRemote(queries), null, { blurhash: false });
60
+ await handle.put('a/b_t.webp', new Uint8Array([1]));
61
+ await handle.put('a/readme.txt', 'hello');
62
+ expect(queries).toHaveLength(2);
63
+ expect(queries.some((q) => q.includes('.bh'))).toBe(false);
64
+ });
65
+
66
+ it('writes the sidecar when a hash is produced', async () => {
67
+ // Simulate a browser: a createImageBitmap whose bitmap a canvas can read.
68
+ const pixels = { width: 2, height: 2 };
69
+ vi.stubGlobal(
70
+ 'createImageBitmap',
71
+ vi.fn(async () => ({ ...pixels, close: vi.fn() }))
72
+ );
73
+ vi.stubGlobal(
74
+ 'OffscreenCanvas',
75
+ class {
76
+ width: number;
77
+ height: number;
78
+ constructor(w: number, h: number) {
79
+ this.width = w;
80
+ this.height = h;
81
+ }
82
+ getContext() {
83
+ return {
84
+ drawImage: () => {},
85
+ getImageData: (_x: number, _y: number, w: number, h: number) => ({
86
+ data: new Uint8ClampedArray(w * h * 4).fill(128),
87
+ }),
88
+ };
89
+ }
90
+ }
91
+ );
92
+ try {
93
+ const handle = new BucketHandle('covers', makeRemote(queries), null);
94
+ const result = await handle.put('a/b_t.webp', new Uint8Array([1]));
95
+ expect(result.blurhash).toBeTruthy();
96
+ expect(queries).toHaveLength(2);
97
+ expect(queries[1]).toContain('covers:/a/b_t.webp.bh".put');
98
+ } finally {
99
+ vi.unstubAllGlobals();
100
+ }
101
+ });
102
+ });
103
+
104
+ describe('BucketHandle.blurhash', () => {
105
+ it('returns a valid stored hash and negative-caches misses per path', async () => {
106
+ const reads: string[] = [];
107
+ let payload: unknown = VALID_HASH;
108
+ const remote = {
109
+ query: vi.fn(async (surql: string) => {
110
+ reads.push(surql);
111
+ return [payload];
112
+ }),
113
+ } as unknown as RemoteDatabaseService;
114
+ const handle = new BucketHandle('covers', remote, null);
115
+
116
+ await expect(handle.blurhash('hit/x.webp')).resolves.toBe(VALID_HASH);
117
+
118
+ payload = null;
119
+ await expect(handle.blurhash('miss/x.webp')).resolves.toBeNull();
120
+ const readsAfterMiss = reads.length;
121
+ // The miss is remembered: no second remote read for the same path.
122
+ await expect(handle.blurhash('miss/x.webp')).resolves.toBeNull();
123
+ expect(reads).toHaveLength(readsAfterMiss);
124
+ });
125
+
126
+ it('treats invalid sidecar content as missing', async () => {
127
+ const remote = {
128
+ query: vi.fn(async () => ['not a blurhash \\ at all']),
129
+ } as unknown as RemoteDatabaseService;
130
+ const handle = new BucketHandle('covers', remote, null);
131
+ await expect(handle.blurhash('bad/x.webp')).resolves.toBeNull();
132
+ });
133
+ });
134
+
135
+ describe('BucketHandle.delete', () => {
136
+ it('also deletes the sidecar for image paths, best-effort', async () => {
137
+ const queries: string[] = [];
138
+ const handle = new BucketHandle('covers', makeRemote(queries), null);
139
+ await handle.delete('a/b_t.webp');
140
+ expect(queries).toHaveLength(2);
141
+ expect(queries[0]).toContain('covers:/a/b_t.webp".delete');
142
+ expect(queries[1]).toContain('covers:/a/b_t.webp.bh".delete');
143
+
144
+ queries.length = 0;
145
+ await handle.delete('a/notes.txt');
146
+ expect(queries).toHaveLength(1);
147
+ });
148
+ });
@@ -0,0 +1,12 @@
1
+ // Build-time string constants injected by tsdown's `define` (see
2
+ // tsdown.config.ts). They carry the real bundled frontend package versions so
3
+ // the DevTools can surface frontend/backend version drift.
4
+ //
5
+ // Typed as `string | undefined` on purpose: the substitution only happens when
6
+ // `@spooky-sync/core` is built with our tsdown plugin. A downstream app that
7
+ // bundles core from source never runs that plugin, so these identifiers must be
8
+ // guarded with `typeof` (see modules/devtools/index.ts) to avoid a runtime
9
+ // ReferenceError that would break DB initialization.
10
+ declare const __SP00KY_CORE_VERSION__: string | undefined;
11
+ declare const __SP00KY_WASM_VERSION__: string | undefined;
12
+ declare const __SP00KY_SURREAL_VERSION__: string | undefined;
@@ -0,0 +1,117 @@
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
+ });
@@ -0,0 +1,227 @@
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
+ }
38
+
39
+ /**
40
+ * Paths known to have no blurhash sidecar, per tab. The blob cache has no
41
+ * negative caching, so without this every mount of a hashless image would pay
42
+ * one serialized remote read. Cleared when a put writes a sidecar or a delete
43
+ * removes the image. Keyed `${bucket}:${path}` (the image path, not the sidecar).
44
+ */
45
+ const missingBlurhash = new Set<string>();
46
+
47
+ export class BucketHandle {
48
+ constructor(
49
+ private bucketName: string,
50
+ private remote: RemoteDatabaseService,
51
+ /** Absent on the raw handle the cache itself reads through. */
52
+ private blobs?: BlobCache | null,
53
+ private settings?: BucketHandleSettings
54
+ ) {}
55
+
56
+ /** Effective blurhash setting: per-call option > client config > default ON. */
57
+ private resolveBlurhash(option?: BlurhashSetting): BlurhashEncodeOptions | null {
58
+ const setting = option ?? this.settings?.blurhash ?? true;
59
+ if (setting === false) return null;
60
+ return setting === true ? {} : setting;
61
+ }
62
+
63
+ async put(
64
+ path: string,
65
+ content: string | Uint8Array | Blob,
66
+ options?: BucketPutOptions
67
+ ): Promise<BucketPutResult> {
68
+ // Start hashing while the upload is in flight; both browser-side costs
69
+ // overlap and the sidecar put only queues once the main put resolved.
70
+ const encodeOptions = this.resolveBlurhash(options?.blurhash);
71
+ const hashPromise =
72
+ encodeOptions && isImagePath(path)
73
+ ? encodeImageToBlurhash(content, encodeOptions)
74
+ : Promise.resolve(null);
75
+
76
+ await this.remote.query(`RETURN f"${this.bucketName}:/${path}".put($content);`, { content });
77
+ // A path can be overwritten, so anything cached under it is now wrong.
78
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
79
+
80
+ // The sidecar is best-effort: a hash or sidecar failure must never fail
81
+ // the image put that triggered it.
82
+ let hash: string | null = null;
83
+ try {
84
+ hash = await hashPromise;
85
+ if (hash) {
86
+ const sidecar = blurhashSidecarPath(path);
87
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sidecar}".put($content);`, {
88
+ content: hash,
89
+ });
90
+ await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
91
+ missingBlurhash.delete(`${this.bucketName}:${path}`);
92
+ }
93
+ } catch (error) {
94
+ hash = null;
95
+ this.settings?.logger?.warn({ error, path }, 'blurhash sidecar put failed');
96
+ }
97
+ return { blurhash: hash };
98
+ }
99
+
100
+ /**
101
+ * The blurhash stored alongside an uploaded image (see
102
+ * {@link blurhashSidecarPath}), or null when there is none. Reads through the
103
+ * blob cache, so a warm client answers from OPFS without a network hop, and
104
+ * misses are remembered per tab so a hashless image costs at most one
105
+ * serialized remote read per session.
106
+ */
107
+ async blurhash(path: string): Promise<string | null> {
108
+ const cacheKey = `${this.bucketName}:${path}`;
109
+ if (missingBlurhash.has(cacheKey)) return null;
110
+ try {
111
+ const blob = await this.read(blurhashSidecarPath(path), { persist: true });
112
+ if (!blob) {
113
+ missingBlurhash.add(cacheKey);
114
+ return null;
115
+ }
116
+ const hash = (await blob.text()).trim();
117
+ if (!isBlurhashValid(hash).result) {
118
+ this.settings?.logger?.warn({ path }, 'blurhash sidecar holds an invalid hash');
119
+ missingBlurhash.add(cacheKey);
120
+ return null;
121
+ }
122
+ return hash;
123
+ } catch (error) {
124
+ // Transient failure: do NOT negative-cache, the next mount may succeed.
125
+ this.settings?.logger?.warn({ error, path }, 'blurhash sidecar read failed');
126
+ return null;
127
+ }
128
+ }
129
+
130
+ async get(path: string): Promise<unknown> {
131
+ const [result] = await this.remote.query<[unknown]>(
132
+ `RETURN f"${this.bucketName}:/${path}".get();`
133
+ );
134
+ return result;
135
+ }
136
+
137
+ /**
138
+ * Read through the local blob cache: OPFS first, the bucket second. Unlike
139
+ * {@link get} this survives a reload and works offline. Returns null when the
140
+ * file exists in neither place.
141
+ */
142
+ async read(path: string, options?: BlobReadOptions): Promise<Blob | null> {
143
+ if (!this.blobs) return bucketContentToBlob(await this.get(path));
144
+ return this.blobs.read({ bucket: this.bucketName, path }, options);
145
+ }
146
+
147
+ /**
148
+ * A refcounted object URL for `path`, suitable for `<img src>`. The caller
149
+ * MUST call `release()` when the URL goes off screen. Returns null when the
150
+ * file does not exist, or when object URLs are unavailable (non-browser).
151
+ */
152
+ async url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null> {
153
+ if (!this.blobs) return null;
154
+ return this.blobs.acquireUrl({ bucket: this.bucketName, path }, options);
155
+ }
156
+
157
+ /** Exempt `path` from pressure eviction. Pinned bytes never expire. */
158
+ pin(path: string): void {
159
+ this.blobs?.setPinned({ bucket: this.bucketName, path }, true);
160
+ }
161
+
162
+ unpin(path: string): void {
163
+ this.blobs?.setPinned({ bucket: this.bucketName, path }, false);
164
+ }
165
+
166
+ /** Drop `path` from the local cache without touching the remote file. */
167
+ async evict(path: string): Promise<void> {
168
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
169
+ }
170
+
171
+ /** Warm the cache for offline use. Already-cached paths are skipped. */
172
+ async prefetch(paths: string[]): Promise<void> {
173
+ await this.blobs?.prefetch(paths.map((path) => ({ bucket: this.bucketName, path })));
174
+ }
175
+
176
+ async delete(path: string): Promise<void> {
177
+ await this.remote.query(`RETURN f"${this.bucketName}:/${path}".delete();`);
178
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
179
+ // Symmetry with put: an image's blurhash sidecar dies with it. Best-effort,
180
+ // the sidecar may simply not exist.
181
+ if (isImagePath(path)) {
182
+ const sidecar = blurhashSidecarPath(path);
183
+ try {
184
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sidecar}".delete();`);
185
+ await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
186
+ } catch {
187
+ // Nothing to clean up.
188
+ }
189
+ missingBlurhash.delete(`${this.bucketName}:${path}`);
190
+ }
191
+ }
192
+
193
+ async exists(path: string): Promise<boolean> {
194
+ const [result] = await this.remote.query<[boolean]>(
195
+ `RETURN f"${this.bucketName}:/${path}".exists();`
196
+ );
197
+ return result;
198
+ }
199
+
200
+ async head(path: string): Promise<Record<string, unknown>> {
201
+ const [result] = await this.remote.query<[Record<string, unknown>]>(
202
+ `RETURN f"${this.bucketName}:/${path}".head();`
203
+ );
204
+ return result;
205
+ }
206
+
207
+ async copy(sourcePath: string, targetPath: string): Promise<void> {
208
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sourcePath}".copy($target);`, {
209
+ target: targetPath,
210
+ });
211
+ }
212
+
213
+ async rename(sourcePath: string, targetPath: string): Promise<void> {
214
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sourcePath}".rename($target);`, {
215
+ target: targetPath,
216
+ });
217
+ }
218
+
219
+ async list(prefix?: string): Promise<string[]> {
220
+ const p = prefix ?? '';
221
+ const [result] = await this.remote.query<[string[]]>(
222
+ `RETURN f"${this.bucketName}:/${p}".list();`
223
+ );
224
+ return result;
225
+ }
226
+ }
227
+
@@ -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
+ });