@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.
Files changed (266) hide show
  1. package/AGENTS.md +84 -0
  2. package/README.md +56 -16
  3. package/dist/index.d.ts +2080 -142
  4. package/dist/index.js +16009 -2480
  5. package/dist/live.d.ts +149 -0
  6. package/dist/live.js +739 -0
  7. package/dist/otel/index.d.ts +1 -1
  8. package/dist/otel/index.js +3 -3
  9. package/dist/pure.d.ts +373 -0
  10. package/dist/pure.js +24 -0
  11. package/dist/sqlite-open.js +392 -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 +477 -0
  18. package/dist/types.d.ts +1857 -38
  19. package/dist/worker-edges.js +480 -0
  20. package/dist/worker-impersonation.js +60 -0
  21. package/dist/worker-ref-tables.d.ts +48 -0
  22. package/dist/worker-types.d.ts +112 -0
  23. package/dist/worker-types.js +73 -0
  24. package/package.json +28 -7
  25. package/scripts/check-broker-bundle.mjs +33 -0
  26. package/scripts/check-worker-entries.mjs +173 -0
  27. package/skills/sp00ky-core/SKILL.md +32 -7
  28. package/skills/sp00ky-core/references/config.md +7 -1
  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 +203 -0
  32. package/src/boot/boot.saga.ts +146 -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 +80 -0
  36. package/src/boot/preload.saga.ts +40 -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 +195 -0
  40. package/src/client/bucket-handle.ts +352 -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.test.ts +74 -0
  46. package/src/client/services.ts +434 -0
  47. package/src/client/sp00ky-client.test.ts +341 -0
  48. package/src/client/sp00ky-client.ts +625 -0
  49. package/src/events/events.test.ts +2 -1
  50. package/src/events/index.ts +3 -0
  51. package/src/index.ts +69 -1
  52. package/src/kernel/constants.test.ts +19 -0
  53. package/src/kernel/constants.ts +80 -0
  54. package/src/kernel/effects.test.ts +40 -0
  55. package/src/kernel/effects.ts +167 -0
  56. package/src/kernel/events.ts +57 -0
  57. package/src/kernel/interpreter.test.ts +116 -0
  58. package/src/kernel/interpreter.ts +148 -0
  59. package/src/kernel/saga.test.ts +89 -0
  60. package/src/kernel/saga.ts +73 -0
  61. package/src/live/index.test.ts +512 -0
  62. package/src/live/index.ts +1059 -0
  63. package/src/modules/app-release/index.test.ts +125 -0
  64. package/src/modules/app-release/index.ts +209 -0
  65. package/src/modules/auth/auth.local-first.test.ts +228 -0
  66. package/src/modules/auth/events/index.ts +9 -2
  67. package/src/modules/auth/impersonation-banner.ts +443 -0
  68. package/src/modules/auth/impersonation.integration.test.ts +205 -0
  69. package/src/modules/auth/impersonation.test.ts +377 -0
  70. package/src/modules/auth/impersonation.ts +179 -0
  71. package/src/modules/auth/index.ts +476 -22
  72. package/src/modules/crdt/acceptance.ts +75 -0
  73. package/src/modules/crdt/bytes.ts +50 -0
  74. package/src/modules/crdt/crdt-field.ts +125 -0
  75. package/src/modules/crdt/crdt.test.ts +469 -0
  76. package/src/modules/crdt/hash.ts +103 -0
  77. package/src/modules/crdt/index.ts +757 -0
  78. package/src/modules/crdt/loro-loader.ts +25 -0
  79. package/src/modules/crdt/mirror.ts +37 -0
  80. package/src/modules/devtools/flags.ts +349 -0
  81. package/src/modules/devtools/index.ts +746 -95
  82. package/src/modules/devtools/mutations.test.ts +92 -0
  83. package/src/modules/devtools/mutations.ts +160 -0
  84. package/src/modules/devtools/notify-throttle.test.ts +166 -0
  85. package/src/modules/devtools/state-shape.test.ts +263 -0
  86. package/src/modules/devtools/storage-info.test.ts +79 -0
  87. package/src/modules/devtools/storage-info.ts +168 -0
  88. package/src/modules/devtools/versions.test.ts +74 -0
  89. package/src/modules/devtools/versions.ts +110 -0
  90. package/src/modules/feature-flag/index.test.ts +298 -0
  91. package/src/modules/feature-flag/index.ts +316 -0
  92. package/src/modules/ref-tables.test.ts +93 -0
  93. package/src/modules/ref-tables.ts +91 -0
  94. package/src/modules/web-push/index.test.ts +824 -0
  95. package/src/modules/web-push/index.ts +1027 -0
  96. package/src/mutation/jobs.test.ts +25 -0
  97. package/src/mutation/jobs.ts +41 -0
  98. package/src/mutation/mutation-id.test.ts +37 -0
  99. package/src/mutation/mutation-id.ts +35 -0
  100. package/src/mutation/pending-writes.integration.test.ts +175 -0
  101. package/src/mutation/push.saga.test.ts +301 -0
  102. package/src/mutation/push.saga.ts +225 -0
  103. package/src/mutation/rows.test.ts +231 -0
  104. package/src/mutation/rows.ts +427 -0
  105. package/src/mutation/tray.saga.test.ts +63 -0
  106. package/src/mutation/tray.saga.ts +48 -0
  107. package/src/mutation/write.saga.test.ts +316 -0
  108. package/src/mutation/write.saga.ts +268 -0
  109. package/src/otel/index.ts +10 -7
  110. package/src/pure.ts +66 -0
  111. package/src/push/types.ts +174 -0
  112. package/src/query/anon-query-isolation.integration.test.ts +527 -0
  113. package/src/query/env.test.ts +23 -0
  114. package/src/query/env.ts +65 -0
  115. package/src/query/fetch.saga.test.ts +199 -0
  116. package/src/query/fetch.saga.ts +120 -0
  117. package/src/query/hash.test.ts +14 -0
  118. package/src/query/hash.ts +18 -0
  119. package/src/query/id-set-plan.test.ts +132 -0
  120. package/src/query/lifecycle.saga.test.ts +274 -0
  121. package/src/query/lifecycle.saga.ts +186 -0
  122. package/src/query/list-ref-live.integration.test.ts +203 -0
  123. package/src/query/materialize.saga.test.ts +115 -0
  124. package/src/query/materialize.saga.ts +84 -0
  125. package/src/query/materialize.test.ts +58 -0
  126. package/src/query/materialize.ts +57 -0
  127. package/src/query/membership.saga.test.ts +407 -0
  128. package/src/query/membership.saga.ts +282 -0
  129. package/src/query/membership.test.ts +118 -0
  130. package/src/query/membership.ts +201 -0
  131. package/src/query/register.saga.test.ts +311 -0
  132. package/src/query/register.saga.ts +235 -0
  133. package/src/query/render-set.test.ts +47 -0
  134. package/src/query/render-set.ts +71 -0
  135. package/src/query/sql.test.ts +118 -0
  136. package/src/query/sql.ts +157 -0
  137. package/src/query/window-query.test.ts +70 -0
  138. package/src/query/window-query.ts +208 -0
  139. package/src/services/blobs/blob-cache.test.ts +359 -0
  140. package/src/services/blobs/blob-cache.ts +603 -0
  141. package/src/services/blobs/blob-manifest.ts +227 -0
  142. package/src/services/blobs/blob-store.test.ts +77 -0
  143. package/src/services/blobs/blob-store.ts +359 -0
  144. package/src/services/blobs/blob.fixture.ts +90 -0
  145. package/src/services/blobs/index.ts +70 -0
  146. package/src/services/database/cache-engine.ts +209 -0
  147. package/src/services/database/connection-supervisor.test.ts +318 -0
  148. package/src/services/database/connection-supervisor.ts +422 -0
  149. package/src/services/database/database.query-timeout.test.ts +140 -0
  150. package/src/services/database/database.responses.test.ts +43 -0
  151. package/src/services/database/database.ts +156 -61
  152. package/src/services/database/engine-factory.ts +33 -0
  153. package/src/services/database/errors.ts +34 -0
  154. package/src/services/database/events/index.ts +2 -1
  155. package/src/services/database/index.ts +7 -0
  156. package/src/services/database/landed-body-clear.integration.test.ts +236 -0
  157. package/src/services/database/local-migrator.integration.test.ts +110 -0
  158. package/src/services/database/local-migrator.ts +61 -16
  159. package/src/services/database/local.test.ts +150 -0
  160. package/src/services/database/local.ts +508 -63
  161. package/src/services/database/plan-render.test.ts +159 -0
  162. package/src/services/database/plan-render.ts +108 -0
  163. package/src/services/database/relation-resolver.test.ts +484 -0
  164. package/src/services/database/relation-resolver.ts +0 -0
  165. package/src/services/database/remote.ts +285 -6
  166. package/src/services/database/sqlite-cache-engine.test.ts +628 -0
  167. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  168. package/src/services/database/sqlite-cache-engine.ts +1401 -0
  169. package/src/services/database/sqlite-column-reset.integration.test.ts +100 -0
  170. package/src/services/database/sqlite-column-reset.ts +91 -0
  171. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  172. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  173. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  174. package/src/services/database/sqlite-lock-verify.ts +45 -0
  175. package/src/services/database/sqlite-open.test.ts +150 -0
  176. package/src/services/database/sqlite-open.ts +164 -0
  177. package/src/services/database/sqlite-plan-sql.test.ts +164 -0
  178. package/src/services/database/sqlite-plan-sql.ts +196 -0
  179. package/src/services/database/sqlite-projection.test.ts +121 -0
  180. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  181. package/src/services/database/sqlite-select.test.ts +261 -0
  182. package/src/services/database/sqlite-select.ts +131 -0
  183. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  184. package/src/services/database/sqlite-transport.ts +224 -0
  185. package/src/services/database/sqlite-worker.ts +437 -0
  186. package/src/services/database/surql-translate.ts +417 -0
  187. package/src/services/database/surreal-cache-engine.ts +163 -0
  188. package/src/services/logger/index.test.ts +132 -0
  189. package/src/services/logger/index.ts +189 -7
  190. package/src/services/persistence/localstorage.ts +2 -2
  191. package/src/services/persistence/resilient.ts +10 -3
  192. package/src/services/persistence/surrealdb.ts +4 -4
  193. package/src/services/stream-processor/index.ts +806 -67
  194. package/src/services/stream-processor/permissions.test.ts +47 -0
  195. package/src/services/stream-processor/permissions.ts +53 -0
  196. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  197. package/src/services/stream-processor/stream-processor.prime.test.ts +234 -0
  198. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  199. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  200. package/src/services/stream-processor/wasm-types.ts +58 -2
  201. package/src/services/tabs/broker-client.ts +284 -0
  202. package/src/services/tabs/broker.test.ts +381 -0
  203. package/src/services/tabs/coordinator.test.ts +386 -0
  204. package/src/services/tabs/coordinator.ts +646 -0
  205. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  206. package/src/services/tabs/leader-locks.ts +75 -0
  207. package/src/services/tabs/protocol.ts +262 -0
  208. package/src/services/tabs/support.ts +36 -0
  209. package/src/services/tabs/tabs-broker-worker.ts +652 -0
  210. package/src/sp00ky.ts +5 -395
  211. package/src/state/client-state.ts +208 -0
  212. package/src/state/lifecycle.test.ts +141 -0
  213. package/src/state/lifecycle.ts +99 -0
  214. package/src/state/reducers.test.ts +306 -0
  215. package/src/state/reducers.ts +531 -0
  216. package/src/state/selectors.test.ts +188 -0
  217. package/src/state/selectors.ts +258 -0
  218. package/src/sw/index.test.ts +582 -0
  219. package/src/sw/index.ts +607 -0
  220. package/src/sw/store.ts +96 -0
  221. package/src/sync/connection.saga.test.ts +97 -0
  222. package/src/sync/connection.saga.ts +89 -0
  223. package/src/sync/live.saga.test.ts +178 -0
  224. package/src/sync/live.saga.ts +107 -0
  225. package/src/sync/policy.health.test.ts +56 -0
  226. package/src/sync/policy.test.ts +663 -0
  227. package/src/sync/policy.ts +515 -0
  228. package/src/sync/poll.saga.test.ts +51 -0
  229. package/src/sync/poll.saga.ts +36 -0
  230. package/src/sync/tabs.saga.test.ts +78 -0
  231. package/src/sync/tabs.saga.ts +96 -0
  232. package/src/testing/adapters.ts +137 -0
  233. package/src/testing/build.ts +63 -0
  234. package/src/testing/fake-services.ts +63 -0
  235. package/src/testing/run-pure.test.ts +92 -0
  236. package/src/testing/run-pure.ts +128 -0
  237. package/src/testing/services.ts +17 -0
  238. package/src/types.ts +633 -10
  239. package/src/utils/blurhash.ts +90 -0
  240. package/src/utils/edges.ts +39 -0
  241. package/src/utils/error-classification.test.ts +54 -0
  242. package/src/utils/error-classification.ts +25 -0
  243. package/src/utils/index.ts +89 -12
  244. package/src/utils/parser.test.ts +67 -102
  245. package/src/utils/parser.ts +64 -2
  246. package/src/utils/semver.test.ts +32 -0
  247. package/src/utils/semver.ts +30 -0
  248. package/src/utils/sha256.ts +15 -0
  249. package/src/utils/surql.ts +29 -18
  250. package/src/utils/utils.test.ts +3 -2
  251. package/tsdown.config.ts +117 -3
  252. package/vitest.config.ts +48 -0
  253. package/src/modules/cache/index.ts +0 -241
  254. package/src/modules/cache/types.ts +0 -19
  255. package/src/modules/data/data.test.ts +0 -58
  256. package/src/modules/data/index.ts +0 -879
  257. package/src/modules/sync/engine.ts +0 -173
  258. package/src/modules/sync/events/index.ts +0 -77
  259. package/src/modules/sync/index.ts +0 -3
  260. package/src/modules/sync/queue/index.ts +0 -2
  261. package/src/modules/sync/queue/queue-down.ts +0 -89
  262. package/src/modules/sync/queue/queue-up.ts +0 -223
  263. package/src/modules/sync/scheduler.ts +0 -84
  264. package/src/modules/sync/sync.ts +0 -421
  265. package/src/modules/sync/utils.test.ts +0 -311
  266. package/src/modules/sync/utils.ts +0 -167
@@ -0,0 +1,187 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { RecordId } from 'surrealdb';
3
+ import { runPure } from '../testing/run-pure';
4
+ import { fakeServices } from '../testing/services';
5
+ import { buildEntry, buildOutboxItem, buildState } from '../testing/build';
6
+ import * as R from '../state/reducers';
7
+ import { defaultEnv } from '../query/env';
8
+ import { bucketSwitch, dropImpersonatedBucket, rebindQueries } from './bucket-switch.saga';
9
+
10
+ const env = defaultEnv({ tables: [] } as any);
11
+ const base = (over: Partial<ReturnType<typeof buildState>> = {}) => ({ ...buildState(), pendingBucket: 'u2', bucketId: 'u1', ...over });
12
+
13
+ describe('bucketSwitch', () => {
14
+ it('steps aside when superseded or already on target (releasing the gate)', async () => {
15
+ const released: number[] = [];
16
+ const svc = fakeServices({ 'local.currentBucketId': () => 'u1' });
17
+ await runPure(bucketSwitch(env, 'u2', () => void released.push(1)), { state: { ...base(), pendingBucket: 'u3' }, handlers: { service: svc.handler } });
18
+ await runPure(bucketSwitch(env, 'u1', () => void released.push(2)), { state: base({ pendingBucket: 'u1' }), handlers: { service: svc.handler } });
19
+ expect(released).toEqual([1, 2]);
20
+ expect(svc.names()).toEqual(['local.currentBucketId', 'local.currentBucketId']);
21
+ });
22
+ it('solo: drain, swap, provision, blobs, SSP reset, prime, token, rebind, outbox, re-register', async () => {
23
+ const svc = fakeServices({
24
+ 'local.currentBucketId': () => 'u1',
25
+ 'local.beginSwitch': () => () => undefined,
26
+ 'local.usesSurqlSchema': () => true,
27
+ 'auth.sessionAuthId': () => 'user:u2',
28
+ 'auth.access': () => 'account',
29
+ 'auth.token': () => 'jwt',
30
+ });
31
+ const s = base();
32
+ const withQ = R.compose(
33
+ R.putQuery(buildEntry({ def: { hash: 'a', viewKey: 'vk', surql: 'SELECT * FROM thing' }, lifecycle: { phase: 'live', remote: 'registered' }, remoteArray: [['t:1', 1]] })),
34
+ R.outboxReplace([buildOutboxItem()]),
35
+ R.setVersions([['t:1', 1]]),
36
+ R.markMembershipDirty(['a'])
37
+ )({ ...s, sessionId: 'sess2' });
38
+ const out = await runPure(bucketSwitch(env, 'u2'), {
39
+ state: withQ,
40
+ handlers: {
41
+ service: svc.handler,
42
+ 'local.getById': () => ({ ids: [['t:9', 1]], confirmed: true }),
43
+ 'ssp.register': () => ({ localArray: [['t:9', 1]], timings: {} }),
44
+ 'local.query': () => [],
45
+ },
46
+ });
47
+ expect(svc.names()).toEqual([
48
+ 'local.currentBucketId',
49
+ 'local.beginSwitch',
50
+ 'crdt.closeAll',
51
+ 'local.switchStore',
52
+ 'local.usesSurqlSchema',
53
+ 'migrator.provision',
54
+ 'blobs.setNamespace',
55
+ 'ssp.reset',
56
+ 'ssp.setPermissions',
57
+ 'auth.sessionAuthId',
58
+ 'auth.access',
59
+ 'ssp.setSessionAuth',
60
+ 'auth.consumeEndedImpersonation',
61
+ 'auth.token',
62
+ 'persistence.set',
63
+ ]);
64
+ expect(out.log.filter((e) => e.kind === 'timer.clear').map((e) => (e as any).key)).toEqual(['poll', 'outbox', 'membership', 'fetch', 'ack-prune']);
65
+ expect(out.state).toMatchObject({ bucketId: 'u2', primed: false, outbox: [] });
66
+ expect(out.state.versions.size).toBe(0);
67
+ expect(out.state.membershipDirty.size).toBe(0);
68
+ const e = out.state.queries.get('a')!;
69
+ expect(e.lifecycle).toMatchObject({ phase: 'cached', remote: 'unregistered' });
70
+ expect(e.remoteArray).toEqual([['t:9', 1]]);
71
+ expect(e.localArray).toEqual([['t:9', 1]]);
72
+ expect(String(e.def.id.table)).toBe('_00_query');
73
+ // PollTick is load-bearing: the switch cleared the poll timer, and only the
74
+ // tick re-arms it, so a sign-in without it leaves the client on LIVE alone.
75
+ expect(out.dispatched.map((d) => d.type)).toEqual(['PrimeCircuit', 'AdoptPendingWrites', 'EnsureRegistered', 'LiveStart', 'PollTick', 'Drain']);
76
+ });
77
+ it('shared tabs: moves namespaces, falls back to solo on failure; blob clear on sign-out; failures are logged and the gate always reopens', async () => {
78
+ let gateReleased = false;
79
+ const moved = fakeServices({
80
+ 'local.currentBucketId': () => 'u1',
81
+ 'tabs.moveToBucket': async () => 'follower',
82
+ 'auth.sessionAuthId': () => null,
83
+ 'auth.access': () => null,
84
+ 'auth.token': () => null,
85
+ });
86
+ const out = await runPure(bucketSwitch(env, 'u2', () => void (gateReleased = true)), {
87
+ state: { ...base(), tabRole: 'leader' },
88
+ handlers: { service: moved.handler, 'local.query': () => [] },
89
+ });
90
+ expect(out.state.tabRole).toBe('follower');
91
+ expect(gateReleased).toBe(true);
92
+ expect(moved.names()).not.toContain('local.switchStore');
93
+ const failing = fakeServices({
94
+ 'local.currentBucketId': () => 'u1',
95
+ 'local.beginSwitch': () => () => undefined,
96
+ 'tabs.moveToBucket': async () => {
97
+ throw new Error('broker');
98
+ },
99
+ 'auth.sessionAuthId': () => null,
100
+ 'auth.access': () => null,
101
+ 'auth.token': () => 'jwt',
102
+ 'persistence.set': async () => {
103
+ throw new Error('kv');
104
+ },
105
+ 'blobs.setNamespace': async () => {
106
+ throw new Error('opfs');
107
+ },
108
+ });
109
+ const fb = await runPure(bucketSwitch(env, 'anon', null, { clearBlobsOnSignOut: true }), {
110
+ state: { ...base({ pendingBucket: 'anon' }), tabRole: 'leader' },
111
+ handlers: { service: failing.handler, 'local.query': () => [] },
112
+ });
113
+ expect(fb.state.tabRole).toBe('solo');
114
+ expect(failing.names()).toContain('local.beginSwitch');
115
+ expect(failing.names()).toContain('ssp.setPersistence');
116
+ expect(failing.names()).toContain('blobs.clear');
117
+ expect(fb.emitted.filter((e) => e.type === 'log')).toHaveLength(3);
118
+ });
119
+ it('a failing swap still reopens the gate and rethrows', async () => {
120
+ let released = false;
121
+ const svc = fakeServices({
122
+ 'local.currentBucketId': () => 'u1',
123
+ 'local.switchStore': async () => {
124
+ throw new Error('disk');
125
+ },
126
+ });
127
+ await expect(runPure(bucketSwitch(env, 'u2', () => void (released = true)), { state: base(), handlers: { service: svc.handler } })).rejects.toThrow('disk');
128
+ expect(released).toBe(true);
129
+ });
130
+ });
131
+
132
+ describe('dropImpersonatedBucket', () => {
133
+ it('drops the bucket it just left when that was the impersonated user', async () => {
134
+ const svc = fakeServices({ 'auth.consumeEndedImpersonation': () => 'user:bob' });
135
+ await runPure(dropImpersonatedBucket('bob'), { state: buildState(), handlers: { service: svc.handler } });
136
+ expect(svc.calls).toEqual([
137
+ ['auth.consumeEndedImpersonation', []],
138
+ ['local.dropBucket', ['bob']],
139
+ ]);
140
+ });
141
+ it('leaves every other bucket alone', async () => {
142
+ for (const [ended, left] of [[null, 'bob'], ['user:bob', 'alice'], ['user:bob', 'anon']] as const) {
143
+ const svc = fakeServices({ 'auth.consumeEndedImpersonation': () => ended });
144
+ await runPure(dropImpersonatedBucket(left), { state: buildState(), handlers: { service: svc.handler } });
145
+ expect(svc.names()).toEqual(['auth.consumeEndedImpersonation']);
146
+ }
147
+ });
148
+ it('logs a failed delete instead of failing the switch', async () => {
149
+ const svc = fakeServices({
150
+ 'auth.consumeEndedImpersonation': () => 'user:bob',
151
+ 'local.dropBucket': async () => {
152
+ throw new Error('handles still open');
153
+ },
154
+ });
155
+ const out = await runPure(dropImpersonatedBucket('bob'), { state: buildState(), handlers: { service: svc.handler } });
156
+ expect(out.emitted.filter((e) => e.type === 'log')).toHaveLength(1);
157
+ });
158
+ });
159
+
160
+ describe('rebindQueries', () => {
161
+ it('re-seeds from the new store, rebuilds the local view (tolerating failures), emits authority flips', async () => {
162
+ const s = R.compose(
163
+ R.putQuery(buildEntry({ def: { hash: 'a' }, lifecycle: { phase: 'live' } })),
164
+ R.putQuery(buildEntry({ def: { hash: 'b' }, lifecycle: { phase: 'cold' } }))
165
+ )({ ...buildState(), sessionId: 's' });
166
+ const out = await runPure(rebindQueries(), {
167
+ state: s,
168
+ handlers: {
169
+ 'local.getById': (e: any) => {
170
+ if (String((e.id as RecordId).id) === 'view-b') return { ids: [['t:1', 1]], confirmed: true };
171
+ throw new Error('missing');
172
+ },
173
+ 'ssp.register': (e: any) => {
174
+ if (e.plan.queryHash === 'a') throw new Error('wasm');
175
+ return { localArray: [['t:1', 1]], timings: {} };
176
+ },
177
+ },
178
+ });
179
+ expect(out.state.queries.get('a')!.lifecycle.phase).toBe('cold');
180
+ expect(out.state.queries.get('b')!.lifecycle.phase).toBe('cached');
181
+ expect(out.emitted.filter((e) => e.type === 'query:authority')).toEqual([
182
+ { type: 'query:authority', hash: 'a', known: false },
183
+ { type: 'query:authority', hash: 'b', known: true },
184
+ ]);
185
+ expect(out.emitted.some((e) => e.type === 'log')).toBe(true);
186
+ });
187
+ });
@@ -0,0 +1,133 @@
1
+ import { RecordId } from 'surrealdb';
2
+ import type { Saga } from '../kernel/saga';
3
+ import type { RegisterResult } from '../kernel/effects';
4
+ import { fx } from '../kernel/effects';
5
+ import type { ClientState, QueryEntry } from '../state/client-state';
6
+ import { seedLifecycle, isAuthoritative } from '../state/lifecycle';
7
+ import * as R from '../state/reducers';
8
+ import { ANON_USER_ID, bucketIdForUser } from '../modules/ref-tables';
9
+ import type { SagaEnv } from '../query/env';
10
+ import { queryHashInput } from '../query/hash';
11
+ import { isResolvedBefore, parseViewRow } from '../query/membership';
12
+ import * as sql from '../query/sql';
13
+ import { loadViews } from '../query/lifecycle.saga';
14
+ import { loadOutbox } from '../mutation/push.saga';
15
+
16
+ export interface BucketSwitchOptions {
17
+ clearBlobsOnSignOut: boolean;
18
+ }
19
+
20
+ /**
21
+ * Move the client to another user's local store: drain, swap, rebind.
22
+ * Runs on the serial `bucket` lane; a switch that wakes up to a newer
23
+ * `pendingBucket` steps aside. Every active query keeps its hash and is
24
+ * re-seeded from the new store's `_00_view` rows, then re-registered.
25
+ */
26
+ export function* bucketSwitch(env: SagaEnv, target: string, release: (() => void) | null = null, opts: BucketSwitchOptions = { clearBlobsOnSignOut: false }): Saga<void> {
27
+ const state = (yield fx.state.read((s) => s)) as ClientState;
28
+ const current = (yield fx.service('local.currentBucketId')) as string;
29
+ if (state.pendingBucket !== target || current === target) {
30
+ release?.();
31
+ return;
32
+ }
33
+ const gate = release ?? ((yield fx.service('local.beginSwitch')) as () => void);
34
+ for (const key of ['poll', 'outbox', 'membership', 'fetch', 'ack-prune']) yield fx.timer.clear(key);
35
+ yield fx.state.update(R.clearBucketState());
36
+ yield fx.service('crdt.closeAll', false);
37
+ try {
38
+ if (state.tabRole !== 'solo') {
39
+ try {
40
+ const role = (yield fx.service('tabs.moveToBucket', target)) as ClientState['tabRole'];
41
+ yield fx.state.update(R.setTabRole(role));
42
+ } catch (error) {
43
+ yield fx.emit({ type: 'log', level: 'warn', message: 'shared-tabs bucket move failed; switching solo', data: { error } });
44
+ yield fx.state.update(R.setTabRole('solo'));
45
+ yield fx.service('ssp.setPersistence', true);
46
+ yield fx.service('local.switchStore', target);
47
+ }
48
+ } else {
49
+ yield fx.service('local.switchStore', target);
50
+ if ((yield fx.service('local.usesSurqlSchema')) as boolean) yield fx.service('migrator.provision');
51
+ }
52
+ try {
53
+ if (opts.clearBlobsOnSignOut && target === ANON_USER_ID && current !== ANON_USER_ID) yield fx.service('blobs.clear');
54
+ yield fx.service('blobs.setNamespace', target);
55
+ } catch (error) {
56
+ yield fx.emit({ type: 'log', level: 'warn', message: 'blob cache bucket switch failed', data: { error } });
57
+ }
58
+ yield fx.service('ssp.reset');
59
+ yield fx.service('ssp.setPermissions');
60
+ yield fx.service('ssp.setSessionAuth', (yield fx.service('auth.sessionAuthId')) as string | null, (yield fx.service('auth.access')) as string | null);
61
+ yield fx.state.update(R.setIdentity({ bucketId: target }));
62
+ yield fx.dispatch({ type: 'PrimeCircuit' });
63
+ } finally {
64
+ gate();
65
+ }
66
+ yield* dropImpersonatedBucket(current);
67
+ const token = (yield fx.service('auth.token')) as string | null;
68
+ if (token) {
69
+ try {
70
+ yield fx.service('persistence.set', 'sp00ky_auth_token', token);
71
+ } catch (error) {
72
+ yield fx.emit({ type: 'log', level: 'warn', message: 'failed to re-persist the auth token', data: { error } });
73
+ }
74
+ }
75
+ yield* loadViews();
76
+ yield* rebindQueries();
77
+ yield* loadOutbox(env);
78
+ yield fx.dispatch({ type: 'EnsureRegistered' });
79
+ yield fx.dispatch({ type: 'LiveStart' });
80
+ // The switch cleared the poll timer, and only the tick itself re-arms it: a
81
+ // sign-in (auth flip -> bucket switch) otherwise left the client running on
82
+ // LIVE alone for the rest of the session, so any membership change LIVE did
83
+ // not deliver was never noticed.
84
+ yield fx.dispatch({ type: 'PollTick' });
85
+ yield fx.dispatch({ type: 'Drain' });
86
+ }
87
+
88
+ /**
89
+ * An impersonation just ended and this switch left its bucket: delete that
90
+ * user's local data from the admin's device. Best-effort; a failure only
91
+ * leaves rows the server already let this admin read.
92
+ */
93
+ export function* dropImpersonatedBucket(left: string): Saga<void> {
94
+ const ended = (yield fx.service('auth.consumeEndedImpersonation')) as string | null;
95
+ if (!ended || left === ANON_USER_ID || bucketIdForUser(ended) !== left) return;
96
+ try {
97
+ yield fx.service('local.dropBucket', left);
98
+ } catch (error) {
99
+ yield fx.emit({ type: 'log', level: 'warn', message: 'could not delete the impersonated bucket', data: { bucket: left, error } });
100
+ }
101
+ }
102
+
103
+ /** Re-seed every active query from the new store and rebuild its SSP view. */
104
+ export function* rebindQueries(): Saga<void> {
105
+ const [entries, sessionId] = (yield fx.state.read((s) => [[...s.queries.values()], s.sessionId])) as [QueryEntry[], string | null];
106
+ for (const entry of entries) {
107
+ let view: ReturnType<typeof parseViewRow> = null;
108
+ try {
109
+ view = parseViewRow(yield fx.local.getById(sql.VIEW_TABLE, sql.viewRecordId(entry.def.viewKey)));
110
+ } catch {
111
+ view = null;
112
+ }
113
+ const hash = (yield fx.hash(queryHashInput({ surql: entry.def.surql, params: entry.def.params as Record<string, unknown> }, sessionId))) as string;
114
+ let localArray: QueryEntry['localArray'] = [];
115
+ try {
116
+ const reg = (yield fx.ssp.register({
117
+ queryHash: entry.def.hash,
118
+ surql: entry.def.surql,
119
+ params: entry.def.params as Record<string, unknown>,
120
+ ttl: entry.def.ttl,
121
+ tableName: entry.def.tableName,
122
+ })) as RegisterResult;
123
+ localArray = reg.localArray;
124
+ } catch (error) {
125
+ yield fx.emit({ type: 'log', level: 'warn', message: 'local view rebuild failed after bucket switch', data: { hash: entry.def.hash, error } });
126
+ }
127
+ const lifecycle = seedLifecycle(isResolvedBefore(view));
128
+ yield fx.state.update(R.rebindQuery(entry.def.hash, { id: new RecordId('_00_query', hash), lifecycle, remoteArray: view?.ids ?? [], localArray }));
129
+ if (isAuthoritative(entry.lifecycle) !== isAuthoritative(lifecycle)) {
130
+ yield fx.emit({ type: 'query:authority', hash: entry.def.hash, known: isAuthoritative(lifecycle) });
131
+ }
132
+ }
133
+ }
@@ -0,0 +1,80 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { runPure, sha256Hex } from '../testing/run-pure';
3
+ import { buildEntry, buildState } from '../testing/build';
4
+ import * as R from '../state/reducers';
5
+ import { defaultEnv } from '../query/env';
6
+ import { queryHashInput } from '../query/hash';
7
+ import { preload, PreloadFailedError } from './preload.saga';
8
+
9
+ const env = defaultEnv({ tables: [{ name: 'thing', columns: {} }] } as any);
10
+ const input = { tableName: 'thing', surql: 'SELECT * FROM thing', params: {}, ttl: '10m' as const };
11
+ const sspOk = () => ({ localArray: [], timings: { parseMs: 0, planMs: 0, snapshotMs: 0, wallMs: 0 } });
12
+
13
+ describe('preload', () => {
14
+ it('resolved before: returns without waiting and with no remote effect', async () => {
15
+ const out = await runPure(preload(env, input), {
16
+ handlers: { 'local.getById': () => ({ ids: [['thing:1', 1]], confirmed: true }), 'ssp.register': sspOk },
17
+ });
18
+ expect(out.result.waited).toBe(false);
19
+ expect(out.log.filter((e) => e.kind === 'remote.query')).toHaveLength(0);
20
+ expect(out.log.filter((e) => e.kind === 'state.wait')).toHaveLength(0);
21
+ });
22
+ it('never resolved: waits for settled (membership + bodies + first render), or fails when the registration failed', async () => {
23
+ const hash = await sha256Hex(queryHashInput(input, null));
24
+ const signedIn = buildState([], R.setIdentity({ userId: 'user:alice' }));
25
+ const settleIt = (ctx: any) => {
26
+ ctx.state = R.compose(
27
+ R.commitMembership(hash, [['thing:1', 1]], true),
28
+ R.setVersions([['thing:1', 1]]),
29
+ R.setRecords(hash, [{ id: 'thing:1' }], true, 1)
30
+ )(ctx.state);
31
+ };
32
+ const out = await runPure(preload(env, input), {
33
+ state: signedIn,
34
+ handlers: { 'local.getById': () => null, 'ssp.register': sspOk, 'state.wait': (_e, ctx) => settleIt(ctx) },
35
+ });
36
+ expect(out.result).toEqual({ hash, waited: true });
37
+ const failed = runPure(preload(env, input), {
38
+ state: signedIn,
39
+ handlers: {
40
+ 'local.getById': () => null,
41
+ 'ssp.register': sspOk,
42
+ 'state.wait': (_e, ctx) => void (ctx.state = R.applyLifecycle(hash, { type: 'remote-failed' })(ctx.state)),
43
+ },
44
+ });
45
+ await expect(failed).rejects.toBeInstanceOf(PreloadFailedError);
46
+ const gone = runPure(preload(env, input), {
47
+ state: signedIn,
48
+ handlers: { 'local.getById': () => null, 'ssp.register': sspOk, 'state.wait': (_e, ctx) => void (ctx.state = R.removeQuery(hash)(ctx.state)) },
49
+ });
50
+ await expect(gone).rejects.toThrow(/could not settle/);
51
+ const already = buildState([buildEntry({ def: { hash }, lifecycle: { phase: 'cached' } })]);
52
+ const cached = await runPure(preload(env, input), { state: already });
53
+ expect(cached.result.waited).toBe(false);
54
+ });
55
+ it('signed out without anonymous live queries: registers but never waits, since no membership can reach this session', async () => {
56
+ const out = await runPure(preload(env, input), {
57
+ handlers: { 'local.getById': () => null, 'ssp.register': sspOk },
58
+ });
59
+ expect(out.result.waited).toBe(false);
60
+ expect(out.log.filter((e) => e.kind === 'state.wait')).toHaveLength(0);
61
+ expect(out.log.some((e) => e.kind === 'dispatch' && (e as any).event?.type === 'EnsureRegistered')).toBe(true);
62
+
63
+ // With anonymous live queries a signed-out session is sent its membership
64
+ // on `_00_list_ref_anon`, so it waits like a signed-in one.
65
+ const hash = await sha256Hex(queryHashInput(input, null));
66
+ const anon = await runPure(preload(defaultEnv(env.schema, { anonLive: true }), input), {
67
+ handlers: {
68
+ 'local.getById': () => null,
69
+ 'ssp.register': sspOk,
70
+ 'state.wait': (_e, ctx) =>
71
+ void (ctx.state = R.compose(
72
+ R.commitMembership(hash, [['thing:1', 1]], true),
73
+ R.setVersions([['thing:1', 1]]),
74
+ R.setRecords(hash, [{ id: 'thing:1' }], true, 1)
75
+ )(ctx.state)),
76
+ },
77
+ });
78
+ expect(anon.result).toEqual({ hash, waited: true });
79
+ });
80
+ });
@@ -0,0 +1,40 @@
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 { receivesMembership } from '../query/env';
7
+ import type { RegisterInput } from '../query/register.saga';
8
+ import { registerLocal } from '../query/register.saga';
9
+
10
+ export class PreloadFailedError extends Error {
11
+ constructor(public readonly hash: QueryHash) {
12
+ super(`Preload could not settle: the registration of ${hash} failed`);
13
+ this.name = 'PreloadFailedError';
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Preload = a registered query nobody subscribes to. Resolved before on this
19
+ * device: returns as soon as the entry exists (its rows paint from cache).
20
+ * Never resolved: blocks until the server's membership and every body are
21
+ * local and the first materialization ran. The entry is evicted like any
22
+ * other query a ttl after it was last watched (or registered).
23
+ *
24
+ * A session that cannot be sent membership (signed out, without anonymous
25
+ * live queries) does not wait either: a view with rows would never settle,
26
+ * and an app that gates its first paint on a preload showed its loading
27
+ * screen forever, signed-out landing page included.
28
+ */
29
+ export function* preload(env: SagaEnv, input: RegisterInput): Saga<{ hash: QueryHash; waited: boolean }> {
30
+ const hash = yield* registerLocal(env, input);
31
+ const [phase, reachable] = (yield fx.state.read((s) => [s.queries.get(hash)?.lifecycle.phase, receivesMembership(env, s)])) as [
32
+ string | undefined,
33
+ boolean,
34
+ ];
35
+ if (phase !== 'cold' || !reachable) return { hash, waited: false };
36
+ yield fx.state.wait((s) => settled(s, hash) || settleFailed(s, hash));
37
+ const failed = (yield fx.state.read((s) => settleFailed(s, hash))) as boolean;
38
+ if (failed) throw new PreloadFailedError(hash);
39
+ return { hash, waited: true };
40
+ }
@@ -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;