@spooky-sync/core 0.0.1-canary.24 → 0.0.1-canary.240

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 (303) hide show
  1. package/AGENTS.md +58 -0
  2. package/coverage/lcov-report/base.css +224 -0
  3. package/coverage/lcov-report/block-navigation.js +87 -0
  4. package/coverage/lcov-report/favicon.png +0 -0
  5. package/coverage/lcov-report/index.html +476 -0
  6. package/coverage/lcov-report/prettify.css +1 -0
  7. package/coverage/lcov-report/prettify.js +2 -0
  8. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  9. package/coverage/lcov-report/sorter.js +210 -0
  10. package/coverage/lcov-report/src/boot/auth-flip.saga.ts.html +271 -0
  11. package/coverage/lcov-report/src/boot/boot.saga.ts.html +469 -0
  12. package/coverage/lcov-report/src/boot/bucket-switch.saga.ts.html +430 -0
  13. package/coverage/lcov-report/src/boot/index.html +161 -0
  14. package/coverage/lcov-report/src/boot/preload.saga.ts.html +178 -0
  15. package/coverage/lcov-report/src/client/bucket-handle.ts.html +766 -0
  16. package/coverage/lcov-report/src/client/index.html +161 -0
  17. package/coverage/lcov-report/src/client/router.ts.html +367 -0
  18. package/coverage/lcov-report/src/client/runtime.ts.html +940 -0
  19. package/coverage/lcov-report/src/client/sp00ky-client.ts.html +1531 -0
  20. package/coverage/lcov-report/src/events/index.html +116 -0
  21. package/coverage/lcov-report/src/events/index.ts.html +877 -0
  22. package/coverage/lcov-report/src/index.html +146 -0
  23. package/coverage/lcov-report/src/index.ts.html +202 -0
  24. package/coverage/lcov-report/src/kernel/constants.ts.html +247 -0
  25. package/coverage/lcov-report/src/kernel/effects.ts.html +562 -0
  26. package/coverage/lcov-report/src/kernel/events.ts.html +250 -0
  27. package/coverage/lcov-report/src/kernel/index.html +176 -0
  28. package/coverage/lcov-report/src/kernel/interpreter.ts.html +514 -0
  29. package/coverage/lcov-report/src/kernel/saga.ts.html +304 -0
  30. package/coverage/lcov-report/src/modules/app-release/index.html +116 -0
  31. package/coverage/lcov-report/src/modules/app-release/index.ts.html +712 -0
  32. package/coverage/lcov-report/src/modules/auth/events/index.html +116 -0
  33. package/coverage/lcov-report/src/modules/auth/events/index.ts.html +142 -0
  34. package/coverage/lcov-report/src/modules/auth/index.html +116 -0
  35. package/coverage/lcov-report/src/modules/auth/index.ts.html +1225 -0
  36. package/coverage/lcov-report/src/modules/crdt/crdt-field.ts.html +967 -0
  37. package/coverage/lcov-report/src/modules/crdt/index.html +146 -0
  38. package/coverage/lcov-report/src/modules/crdt/index.ts.html +1474 -0
  39. package/coverage/lcov-report/src/modules/crdt/loro-loader.ts.html +160 -0
  40. package/coverage/lcov-report/src/modules/devtools/flags.ts.html +1132 -0
  41. package/coverage/lcov-report/src/modules/devtools/index.html +161 -0
  42. package/coverage/lcov-report/src/modules/devtools/index.ts.html +2413 -0
  43. package/coverage/lcov-report/src/modules/devtools/storage-info.ts.html +589 -0
  44. package/coverage/lcov-report/src/modules/devtools/versions.ts.html +415 -0
  45. package/coverage/lcov-report/src/modules/feature-flag/index.html +116 -0
  46. package/coverage/lcov-report/src/modules/feature-flag/index.ts.html +1033 -0
  47. package/coverage/lcov-report/src/modules/index.html +116 -0
  48. package/coverage/lcov-report/src/modules/ref-tables.ts.html +349 -0
  49. package/coverage/lcov-report/src/mutation/index.html +191 -0
  50. package/coverage/lcov-report/src/mutation/jobs.ts.html +208 -0
  51. package/coverage/lcov-report/src/mutation/mutation-id.ts.html +190 -0
  52. package/coverage/lcov-report/src/mutation/push.saga.ts.html +730 -0
  53. package/coverage/lcov-report/src/mutation/rows.ts.html +1216 -0
  54. package/coverage/lcov-report/src/mutation/tray.saga.ts.html +229 -0
  55. package/coverage/lcov-report/src/mutation/write.saga.ts.html +583 -0
  56. package/coverage/lcov-report/src/otel/index.html +116 -0
  57. package/coverage/lcov-report/src/otel/index.ts.html +466 -0
  58. package/coverage/lcov-report/src/query/env.ts.html +253 -0
  59. package/coverage/lcov-report/src/query/fetch.saga.ts.html +412 -0
  60. package/coverage/lcov-report/src/query/hash.ts.html +139 -0
  61. package/coverage/lcov-report/src/query/index.html +281 -0
  62. package/coverage/lcov-report/src/query/lifecycle.saga.ts.html +388 -0
  63. package/coverage/lcov-report/src/query/materialize.saga.ts.html +310 -0
  64. package/coverage/lcov-report/src/query/materialize.ts.html +217 -0
  65. package/coverage/lcov-report/src/query/membership.saga.ts.html +709 -0
  66. package/coverage/lcov-report/src/query/membership.ts.html +559 -0
  67. package/coverage/lcov-report/src/query/register.saga.ts.html +655 -0
  68. package/coverage/lcov-report/src/query/render-set.ts.html +265 -0
  69. package/coverage/lcov-report/src/query/sql.ts.html +322 -0
  70. package/coverage/lcov-report/src/query/window-query.ts.html +670 -0
  71. package/coverage/lcov-report/src/services/blobs/blob-cache.ts.html +1894 -0
  72. package/coverage/lcov-report/src/services/blobs/blob-manifest.ts.html +766 -0
  73. package/coverage/lcov-report/src/services/blobs/blob-store.ts.html +1162 -0
  74. package/coverage/lcov-report/src/services/blobs/index.html +161 -0
  75. package/coverage/lcov-report/src/services/blobs/index.ts.html +295 -0
  76. package/coverage/lcov-report/src/services/database/cache-engine.ts.html +664 -0
  77. package/coverage/lcov-report/src/services/database/connection-supervisor.ts.html +1330 -0
  78. package/coverage/lcov-report/src/services/database/database.ts.html +658 -0
  79. package/coverage/lcov-report/src/services/database/engine-factory.ts.html +184 -0
  80. package/coverage/lcov-report/src/services/database/errors.ts.html +187 -0
  81. package/coverage/lcov-report/src/services/database/events/index.html +116 -0
  82. package/coverage/lcov-report/src/services/database/events/index.ts.html +184 -0
  83. package/coverage/lcov-report/src/services/database/index.html +386 -0
  84. package/coverage/lcov-report/src/services/database/index.ts.html +121 -0
  85. package/coverage/lcov-report/src/services/database/local-migrator.ts.html +703 -0
  86. package/coverage/lcov-report/src/services/database/local.ts.html +1633 -0
  87. package/coverage/lcov-report/src/services/database/plan-render.ts.html +409 -0
  88. package/coverage/lcov-report/src/services/database/relation-resolver.ts.html +682 -0
  89. package/coverage/lcov-report/src/services/database/remote.ts.html +1036 -0
  90. package/coverage/lcov-report/src/services/database/sqlite-cache-engine.ts.html +4132 -0
  91. package/coverage/lcov-report/src/services/database/sqlite-lock-verify.ts.html +220 -0
  92. package/coverage/lcov-report/src/services/database/sqlite-open.ts.html +577 -0
  93. package/coverage/lcov-report/src/services/database/sqlite-plan-sql.ts.html +499 -0
  94. package/coverage/lcov-report/src/services/database/sqlite-select.ts.html +478 -0
  95. package/coverage/lcov-report/src/services/database/sqlite-transport.ts.html +757 -0
  96. package/coverage/lcov-report/src/services/database/surql-translate.ts.html +1333 -0
  97. package/coverage/lcov-report/src/services/database/surreal-cache-engine.ts.html +568 -0
  98. package/coverage/lcov-report/src/services/logger/index.html +116 -0
  99. package/coverage/lcov-report/src/services/logger/index.ts.html +154 -0
  100. package/coverage/lcov-report/src/services/persistence/index.html +146 -0
  101. package/coverage/lcov-report/src/services/persistence/localstorage.ts.html +163 -0
  102. package/coverage/lcov-report/src/services/persistence/resilient.ts.html +208 -0
  103. package/coverage/lcov-report/src/services/persistence/surrealdb.ts.html +271 -0
  104. package/coverage/lcov-report/src/services/stream-processor/index.html +146 -0
  105. package/coverage/lcov-report/src/services/stream-processor/index.ts.html +3313 -0
  106. package/coverage/lcov-report/src/services/stream-processor/permissions.ts.html +244 -0
  107. package/coverage/lcov-report/src/services/stream-processor/wasm-types.ts.html +346 -0
  108. package/coverage/lcov-report/src/services/tabs/broker-client.ts.html +934 -0
  109. package/coverage/lcov-report/src/services/tabs/coordinator.ts.html +1984 -0
  110. package/coverage/lcov-report/src/services/tabs/index.html +191 -0
  111. package/coverage/lcov-report/src/services/tabs/leader-locks.ts.html +310 -0
  112. package/coverage/lcov-report/src/services/tabs/protocol.ts.html +871 -0
  113. package/coverage/lcov-report/src/services/tabs/support.ts.html +193 -0
  114. package/coverage/lcov-report/src/services/tabs/tabs-broker-worker.ts.html +2005 -0
  115. package/coverage/lcov-report/src/sp00ky.ts.html +100 -0
  116. package/coverage/lcov-report/src/state/client-state.ts.html +637 -0
  117. package/coverage/lcov-report/src/state/index.html +161 -0
  118. package/coverage/lcov-report/src/state/lifecycle.ts.html +343 -0
  119. package/coverage/lcov-report/src/state/reducers.ts.html +1471 -0
  120. package/coverage/lcov-report/src/state/selectors.ts.html +637 -0
  121. package/coverage/lcov-report/src/sync/connection.saga.ts.html +343 -0
  122. package/coverage/lcov-report/src/sync/index.html +176 -0
  123. package/coverage/lcov-report/src/sync/live.saga.ts.html +232 -0
  124. package/coverage/lcov-report/src/sync/policy.ts.html +1606 -0
  125. package/coverage/lcov-report/src/sync/poll.saga.ts.html +193 -0
  126. package/coverage/lcov-report/src/sync/tabs.saga.ts.html +361 -0
  127. package/coverage/lcov-report/src/types.ts.html +2419 -0
  128. package/coverage/lcov-report/src/utils/blurhash.ts.html +355 -0
  129. package/coverage/lcov-report/src/utils/error-classification.ts.html +244 -0
  130. package/coverage/lcov-report/src/utils/index.html +191 -0
  131. package/coverage/lcov-report/src/utils/index.ts.html +820 -0
  132. package/coverage/lcov-report/src/utils/parser.ts.html +313 -0
  133. package/coverage/lcov-report/src/utils/semver.ts.html +175 -0
  134. package/coverage/lcov-report/src/utils/surql.ts.html +667 -0
  135. package/coverage/lcov.info +19079 -0
  136. package/dist/index.d.ts +2946 -140
  137. package/dist/index.js +13424 -2356
  138. package/dist/otel/index.d.ts +2 -2
  139. package/dist/otel/index.js +6 -6
  140. package/dist/sqlite-open.js +322 -0
  141. package/dist/sqlite-worker.d.ts +1 -0
  142. package/dist/sqlite-worker.js +439 -0
  143. package/dist/tabs-broker-worker.d.ts +8 -0
  144. package/dist/tabs-broker-worker.js +472 -0
  145. package/dist/types.d.ts +769 -11
  146. package/package.json +11 -7
  147. package/scripts/check-broker-bundle.mjs +33 -0
  148. package/skills/{spooky-core → sp00ky-core}/SKILL.md +12 -12
  149. package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
  150. package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
  151. package/src/bucket-blurhash.test.ts +148 -0
  152. package/src/build-globals.d.ts +12 -0
  153. package/src/events/events.test.ts +2 -1
  154. package/src/events/index.ts +3 -0
  155. package/src/index.ts +36 -2
  156. package/src/modules/app-release/index.test.ts +125 -0
  157. package/src/modules/app-release/index.ts +201 -0
  158. package/src/modules/auth/auth.local-first.test.ts +141 -0
  159. package/src/modules/auth/events/index.ts +2 -1
  160. package/src/modules/auth/index.ts +138 -25
  161. package/src/modules/cache/cache.relay.test.ts +95 -0
  162. package/src/modules/cache/index.ts +163 -43
  163. package/src/modules/cache/types.ts +2 -2
  164. package/src/modules/crdt/crdt-field.ts +294 -0
  165. package/src/modules/crdt/crdt-hydration.test.ts +210 -0
  166. package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
  167. package/src/modules/crdt/index.ts +463 -0
  168. package/src/modules/crdt/loro-loader.ts +25 -0
  169. package/src/modules/data/data.activity.test.ts +109 -0
  170. package/src/modules/data/data.authority.test.ts +122 -0
  171. package/src/modules/data/data.heartbeat-wake.test.ts +115 -0
  172. package/src/modules/data/data.hydration.test.ts +142 -0
  173. package/src/modules/data/data.membership.test.ts +623 -0
  174. package/src/modules/data/data.notify-table.test.ts +41 -0
  175. package/src/modules/data/data.pending-ids.test.ts +199 -0
  176. package/src/modules/data/data.rebind.test.ts +170 -0
  177. package/src/modules/data/data.rematerialize.test.ts +114 -0
  178. package/src/modules/data/data.run.test.ts +113 -0
  179. package/src/modules/data/data.settled-writes.test.ts +235 -0
  180. package/src/modules/data/data.status.test.ts +249 -0
  181. package/src/modules/data/id-set-plan.test.ts +122 -0
  182. package/src/modules/data/index.ts +2031 -150
  183. package/src/modules/data/mutation-id.test.ts +25 -0
  184. package/src/modules/data/mutation-id.ts +35 -0
  185. package/src/modules/data/window-query.test.ts +52 -0
  186. package/src/modules/data/window-query.ts +194 -0
  187. package/src/modules/devtools/flags.ts +349 -0
  188. package/src/modules/devtools/index.ts +451 -46
  189. package/src/modules/devtools/notify-throttle.test.ts +154 -0
  190. package/src/modules/devtools/state-shape.test.ts +146 -0
  191. package/src/modules/devtools/storage-info.test.ts +79 -0
  192. package/src/modules/devtools/storage-info.ts +168 -0
  193. package/src/modules/devtools/versions.test.ts +74 -0
  194. package/src/modules/devtools/versions.ts +110 -0
  195. package/src/modules/feature-flag/index.test.ts +251 -0
  196. package/src/modules/feature-flag/index.ts +308 -0
  197. package/src/modules/ref-tables.test.ts +91 -0
  198. package/src/modules/ref-tables.ts +88 -0
  199. package/src/modules/sync/engine.ts +170 -86
  200. package/src/modules/sync/events/index.ts +9 -2
  201. package/src/modules/sync/queue/queue-down.test.ts +228 -0
  202. package/src/modules/sync/queue/queue-down.ts +108 -13
  203. package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
  204. package/src/modules/sync/queue/queue-up.ts +241 -57
  205. package/src/modules/sync/scheduler.pause.test.ts +109 -0
  206. package/src/modules/sync/scheduler.retry.test.ts +237 -0
  207. package/src/modules/sync/scheduler.ts +215 -13
  208. package/src/modules/sync/sync.cleanup.test.ts +116 -0
  209. package/src/modules/sync/sync.health.test.ts +167 -0
  210. package/src/modules/sync/sync.heartbeat.test.ts +80 -0
  211. package/src/modules/sync/sync.list-ref-meta.test.ts +193 -0
  212. package/src/modules/sync/sync.live-removal.test.ts +289 -0
  213. package/src/modules/sync/sync.reconnect.test.ts +145 -0
  214. package/src/modules/sync/sync.rematerialize-after-fetch.test.ts +93 -0
  215. package/src/modules/sync/sync.settled-poll.test.ts +58 -0
  216. package/src/modules/sync/sync.subquery.test.ts +82 -0
  217. package/src/modules/sync/sync.tabs.test.ts +249 -0
  218. package/src/modules/sync/sync.ts +2350 -133
  219. package/src/modules/sync/utils.test.ts +322 -2
  220. package/src/modules/sync/utils.ts +286 -16
  221. package/src/otel/index.ts +13 -10
  222. package/src/services/blobs/blob-cache.test.ts +359 -0
  223. package/src/services/blobs/blob-cache.ts +603 -0
  224. package/src/services/blobs/blob-manifest.ts +227 -0
  225. package/src/services/blobs/blob-store.test.ts +77 -0
  226. package/src/services/blobs/blob-store.ts +359 -0
  227. package/src/services/blobs/blob.fixture.ts +90 -0
  228. package/src/services/blobs/index.ts +70 -0
  229. package/src/services/database/cache-engine.ts +193 -0
  230. package/src/services/database/connection-supervisor.test.ts +289 -0
  231. package/src/services/database/connection-supervisor.ts +415 -0
  232. package/src/services/database/database.query-timeout.test.ts +140 -0
  233. package/src/services/database/database.ts +130 -62
  234. package/src/services/database/engine-factory.ts +33 -0
  235. package/src/services/database/errors.ts +34 -0
  236. package/src/services/database/events/index.ts +2 -1
  237. package/src/services/database/index.ts +7 -0
  238. package/src/services/database/local-migrator.ts +30 -27
  239. package/src/services/database/local.test.ts +64 -0
  240. package/src/services/database/local.ts +484 -67
  241. package/src/services/database/plan-render.test.ts +159 -0
  242. package/src/services/database/plan-render.ts +108 -0
  243. package/src/services/database/relation-resolver.test.ts +467 -0
  244. package/src/services/database/relation-resolver.ts +0 -0
  245. package/src/services/database/remote.ts +222 -15
  246. package/src/services/database/sqlite-cache-engine.test.ts +616 -0
  247. package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
  248. package/src/services/database/sqlite-cache-engine.ts +1358 -0
  249. package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
  250. package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
  251. package/src/services/database/sqlite-lock-verify.test.ts +33 -0
  252. package/src/services/database/sqlite-lock-verify.ts +45 -0
  253. package/src/services/database/sqlite-open.test.ts +150 -0
  254. package/src/services/database/sqlite-open.ts +164 -0
  255. package/src/services/database/sqlite-plan-sql.test.ts +104 -0
  256. package/src/services/database/sqlite-plan-sql.ts +138 -0
  257. package/src/services/database/sqlite-projection.test.ts +99 -0
  258. package/src/services/database/sqlite-select.integration.test.ts +185 -0
  259. package/src/services/database/sqlite-select.test.ts +246 -0
  260. package/src/services/database/sqlite-select.ts +131 -0
  261. package/src/services/database/sqlite-transport.fixture.ts +30 -0
  262. package/src/services/database/sqlite-transport.ts +224 -0
  263. package/src/services/database/sqlite-worker.ts +437 -0
  264. package/src/services/database/surql-translate.ts +416 -0
  265. package/src/services/database/surreal-cache-engine.ts +161 -0
  266. package/src/services/logger/index.ts +3 -2
  267. package/src/services/persistence/localstorage.ts +2 -2
  268. package/src/services/persistence/resilient.ts +11 -4
  269. package/src/services/persistence/surrealdb.ts +10 -10
  270. package/src/services/stream-processor/index.ts +796 -84
  271. package/src/services/stream-processor/permissions.test.ts +47 -0
  272. package/src/services/stream-processor/permissions.ts +53 -0
  273. package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
  274. package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
  275. package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
  276. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  277. package/src/services/stream-processor/wasm-types.ts +59 -3
  278. package/src/services/tabs/broker-client.ts +283 -0
  279. package/src/services/tabs/broker.test.ts +327 -0
  280. package/src/services/tabs/coordinator.test.ts +365 -0
  281. package/src/services/tabs/coordinator.ts +633 -0
  282. package/src/services/tabs/fake-ports.fixture.ts +112 -0
  283. package/src/services/tabs/leader-locks.ts +75 -0
  284. package/src/services/tabs/protocol.ts +258 -0
  285. package/src/services/tabs/support.ts +36 -0
  286. package/src/services/tabs/tabs-broker-worker.ts +640 -0
  287. package/src/sp00ky.auth-order.test.ts +112 -0
  288. package/src/sp00ky.init-query.test.ts +183 -0
  289. package/src/sp00ky.local-first.test.ts +60 -0
  290. package/src/sp00ky.ts +1854 -0
  291. package/src/types.ts +548 -13
  292. package/src/utils/blurhash.ts +90 -0
  293. package/src/utils/error-classification.test.ts +54 -0
  294. package/src/utils/error-classification.ts +25 -0
  295. package/src/utils/index.ts +79 -13
  296. package/src/utils/parser.test.ts +49 -120
  297. package/src/utils/parser.ts +32 -2
  298. package/src/utils/semver.test.ts +32 -0
  299. package/src/utils/semver.ts +30 -0
  300. package/src/utils/surql.ts +30 -18
  301. package/src/utils/withRetry.test.ts +1 -1
  302. package/tsdown.config.ts +86 -1
  303. package/src/spooky.ts +0 -395
package/src/sp00ky.ts ADDED
@@ -0,0 +1,1854 @@
1
+ import { DataModule } from './modules/data/index';
2
+ import type {
3
+ Sp00kyConfig,
4
+ QueryTimeToLive,
5
+ QueryStatusCallback,
6
+ Sp00kyQueryResultPromise,
7
+ PersistenceClient,
8
+ PreloadOptions,
9
+ UpdateOptions,
10
+ RunOptions,
11
+ SyncHealth,
12
+ StorageHealth,
13
+ QueryAuthorityCallback,
14
+ } from './types';
15
+ import {
16
+ ConnectionSupervisor,
17
+ LocalMigrator,
18
+ RemoteDatabaseService,
19
+ createLocalEngine,
20
+ } from './services/database/index';
21
+ import type { LocalStore } from './services/database/index';
22
+ import { StaleEpochError } from './services/database/index';
23
+ import type { UpEvent } from './modules/sync/index';
24
+ import { Sp00kySync } from './modules/sync/index';
25
+ import type {
26
+ FinalQuery,
27
+ GetTable,
28
+ InnerQuery,
29
+ QueryOptions,
30
+ SchemaStructure,
31
+ TableModel,
32
+ TableNames,
33
+ BucketNames,
34
+ BackendNames,
35
+ BackendRoutes,
36
+ RoutePayload,
37
+ } from '@spooky-sync/query-builder';
38
+ import { QueryBuilder } from '@spooky-sync/query-builder';
39
+
40
+ import { DevToolsService } from './modules/devtools/index';
41
+ import { createLogger } from './services/logger/index';
42
+ import { AuthService } from './modules/auth/index';
43
+ import { StreamProcessorService } from './services/stream-processor/index';
44
+ import { extractSelectPermissions } from './services/stream-processor/permissions';
45
+ import { EventSystem } from './events/index';
46
+ import { CacheModule } from './modules/cache/index';
47
+ import type { RecordWithId } from './modules/cache/index';
48
+ import { CrdtManager, CrdtField } from './modules/crdt/index';
49
+ import { preloadLoro } from './modules/crdt/loro-loader';
50
+ import { FeatureFlagModule, FeatureFlagHandle } from './modules/feature-flag/index';
51
+ import type { FeatureFlagOptions, FeatureFlagOverride } from './modules/feature-flag/index';
52
+ import { AppReleaseModule, AppReleaseHandle } from './modules/app-release/index';
53
+ import type { AppReleaseOptions } from './modules/app-release/index';
54
+ import { LocalStoragePersistenceClient } from './services/persistence/localstorage';
55
+ import { ANON_USER_ID, bucketIdForUser } from './modules/ref-tables';
56
+ import { parseQueryParams, encodeRecordId, parseDuration } from './utils/index';
57
+ import { cleanRecord } from './utils/parser';
58
+ import { RecordId } from 'surrealdb';
59
+ import { SurrealDBPersistenceClient } from './services/persistence/surrealdb';
60
+ import { ResilientPersistenceClient } from './services/persistence/resilient';
61
+ import { detectSharedTabsSupport } from './services/tabs/support';
62
+ import { TabsCoordinator, type CoordinatorHooks } from './services/tabs/coordinator';
63
+ import { computeTabsFingerprint, hash53, type TabRole } from './services/tabs/protocol';
64
+ import type { SqliteCacheEngine } from './services/database/sqlite-cache-engine';
65
+ import type { BlobCache, BlobReadOptions, BlobUrlLease } from './services/blobs/index';
66
+ import { MemoryBlobStore, createBlobCache, resolveBlobBudget } from './services/blobs/index';
67
+
68
+ import {
69
+ blurhashSidecarPath,
70
+ encodeImageToBlurhash,
71
+ isImagePath,
72
+ isBlurhashValid,
73
+ type BlurhashSetting,
74
+ type BlurhashEncodeOptions,
75
+ } from './utils/blurhash';
76
+
77
+ /** Coerce whatever the `.get()` RPC hands back into a Blob. */
78
+ export function bucketContentToBlob(content: unknown): Blob | null {
79
+ if (content == null) return null;
80
+ if (content instanceof Blob) return content;
81
+ if (typeof content === 'string') return new Blob([content]);
82
+ if (content instanceof ArrayBuffer) return new Blob([content]);
83
+ // Uint8Array and friends. The cast sidesteps `ArrayBufferLike` vs
84
+ // `ArrayBuffer` (SharedArrayBuffer) in lib.dom's BlobPart.
85
+ if (ArrayBuffer.isView(content)) return new Blob([content as unknown as BlobPart]);
86
+ return null;
87
+ }
88
+
89
+ export interface BucketPutOptions {
90
+ /** Override the client-level {@link Sp00kyConfig.blurhash} setting for this put. */
91
+ blurhash?: BlurhashSetting;
92
+ }
93
+
94
+ export interface BucketPutResult {
95
+ /** The computed blurhash when the content was a hashable image; else null. */
96
+ blurhash: string | null;
97
+ }
98
+
99
+ interface BucketHandleSettings {
100
+ blurhash?: BlurhashSetting;
101
+ logger?: { warn: (obj: unknown, msg?: string) => void };
102
+ }
103
+
104
+ /**
105
+ * Paths known to have no blurhash sidecar, per tab. The blob cache has no
106
+ * negative caching, so without this every mount of a hashless image would pay
107
+ * one serialized remote read. Cleared when a put writes a sidecar or a delete
108
+ * removes the image. Keyed `${bucket}:${path}` (the image path, not the sidecar).
109
+ */
110
+ const missingBlurhash = new Set<string>();
111
+
112
+ export class BucketHandle {
113
+ constructor(
114
+ private bucketName: string,
115
+ private remote: RemoteDatabaseService,
116
+ /** Absent on the raw handle the cache itself reads through. */
117
+ private blobs?: BlobCache | null,
118
+ private settings?: BucketHandleSettings
119
+ ) {}
120
+
121
+ /** Effective blurhash setting: per-call option > client config > default ON. */
122
+ private resolveBlurhash(option?: BlurhashSetting): BlurhashEncodeOptions | null {
123
+ const setting = option ?? this.settings?.blurhash ?? true;
124
+ if (setting === false) return null;
125
+ return setting === true ? {} : setting;
126
+ }
127
+
128
+ async put(
129
+ path: string,
130
+ content: string | Uint8Array | Blob,
131
+ options?: BucketPutOptions
132
+ ): Promise<BucketPutResult> {
133
+ // Start hashing while the upload is in flight; both browser-side costs
134
+ // overlap and the sidecar put only queues once the main put resolved.
135
+ const encodeOptions = this.resolveBlurhash(options?.blurhash);
136
+ const hashPromise =
137
+ encodeOptions && isImagePath(path)
138
+ ? encodeImageToBlurhash(content, encodeOptions)
139
+ : Promise.resolve(null);
140
+
141
+ await this.remote.query(`RETURN f"${this.bucketName}:/${path}".put($content);`, { content });
142
+ // A path can be overwritten, so anything cached under it is now wrong.
143
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
144
+
145
+ // The sidecar is best-effort: a hash or sidecar failure must never fail
146
+ // the image put that triggered it.
147
+ let hash: string | null = null;
148
+ try {
149
+ hash = await hashPromise;
150
+ if (hash) {
151
+ const sidecar = blurhashSidecarPath(path);
152
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sidecar}".put($content);`, {
153
+ content: hash,
154
+ });
155
+ await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
156
+ missingBlurhash.delete(`${this.bucketName}:${path}`);
157
+ }
158
+ } catch (error) {
159
+ hash = null;
160
+ this.settings?.logger?.warn({ error, path }, 'blurhash sidecar put failed');
161
+ }
162
+ return { blurhash: hash };
163
+ }
164
+
165
+ /**
166
+ * The blurhash stored alongside an uploaded image (see
167
+ * {@link blurhashSidecarPath}), or null when there is none. Reads through the
168
+ * blob cache, so a warm client answers from OPFS without a network hop, and
169
+ * misses are remembered per tab so a hashless image costs at most one
170
+ * serialized remote read per session.
171
+ */
172
+ async blurhash(path: string): Promise<string | null> {
173
+ const cacheKey = `${this.bucketName}:${path}`;
174
+ if (missingBlurhash.has(cacheKey)) return null;
175
+ try {
176
+ const blob = await this.read(blurhashSidecarPath(path), { persist: true });
177
+ if (!blob) {
178
+ missingBlurhash.add(cacheKey);
179
+ return null;
180
+ }
181
+ const hash = (await blob.text()).trim();
182
+ if (!isBlurhashValid(hash).result) {
183
+ this.settings?.logger?.warn({ path }, 'blurhash sidecar holds an invalid hash');
184
+ missingBlurhash.add(cacheKey);
185
+ return null;
186
+ }
187
+ return hash;
188
+ } catch (error) {
189
+ // Transient failure: do NOT negative-cache, the next mount may succeed.
190
+ this.settings?.logger?.warn({ error, path }, 'blurhash sidecar read failed');
191
+ return null;
192
+ }
193
+ }
194
+
195
+ async get(path: string): Promise<unknown> {
196
+ const [result] = await this.remote.query<[unknown]>(
197
+ `RETURN f"${this.bucketName}:/${path}".get();`
198
+ );
199
+ return result;
200
+ }
201
+
202
+ /**
203
+ * Read through the local blob cache: OPFS first, the bucket second. Unlike
204
+ * {@link get} this survives a reload and works offline. Returns null when the
205
+ * file exists in neither place.
206
+ */
207
+ async read(path: string, options?: BlobReadOptions): Promise<Blob | null> {
208
+ if (!this.blobs) return bucketContentToBlob(await this.get(path));
209
+ return this.blobs.read({ bucket: this.bucketName, path }, options);
210
+ }
211
+
212
+ /**
213
+ * A refcounted object URL for `path`, suitable for `<img src>`. The caller
214
+ * MUST call `release()` when the URL goes off screen. Returns null when the
215
+ * file does not exist, or when object URLs are unavailable (non-browser).
216
+ */
217
+ async url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null> {
218
+ if (!this.blobs) return null;
219
+ return this.blobs.acquireUrl({ bucket: this.bucketName, path }, options);
220
+ }
221
+
222
+ /** Exempt `path` from pressure eviction. Pinned bytes never expire. */
223
+ pin(path: string): void {
224
+ this.blobs?.setPinned({ bucket: this.bucketName, path }, true);
225
+ }
226
+
227
+ unpin(path: string): void {
228
+ this.blobs?.setPinned({ bucket: this.bucketName, path }, false);
229
+ }
230
+
231
+ /** Drop `path` from the local cache without touching the remote file. */
232
+ async evict(path: string): Promise<void> {
233
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
234
+ }
235
+
236
+ /** Warm the cache for offline use. Already-cached paths are skipped. */
237
+ async prefetch(paths: string[]): Promise<void> {
238
+ await this.blobs?.prefetch(paths.map((path) => ({ bucket: this.bucketName, path })));
239
+ }
240
+
241
+ async delete(path: string): Promise<void> {
242
+ await this.remote.query(`RETURN f"${this.bucketName}:/${path}".delete();`);
243
+ await this.blobs?.invalidate({ bucket: this.bucketName, path });
244
+ // Symmetry with put: an image's blurhash sidecar dies with it. Best-effort,
245
+ // the sidecar may simply not exist.
246
+ if (isImagePath(path)) {
247
+ const sidecar = blurhashSidecarPath(path);
248
+ try {
249
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sidecar}".delete();`);
250
+ await this.blobs?.invalidate({ bucket: this.bucketName, path: sidecar });
251
+ } catch {
252
+ // Nothing to clean up.
253
+ }
254
+ missingBlurhash.delete(`${this.bucketName}:${path}`);
255
+ }
256
+ }
257
+
258
+ async exists(path: string): Promise<boolean> {
259
+ const [result] = await this.remote.query<[boolean]>(
260
+ `RETURN f"${this.bucketName}:/${path}".exists();`
261
+ );
262
+ return result;
263
+ }
264
+
265
+ async head(path: string): Promise<Record<string, unknown>> {
266
+ const [result] = await this.remote.query<[Record<string, unknown>]>(
267
+ `RETURN f"${this.bucketName}:/${path}".head();`
268
+ );
269
+ return result;
270
+ }
271
+
272
+ async copy(sourcePath: string, targetPath: string): Promise<void> {
273
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sourcePath}".copy($target);`, {
274
+ target: targetPath,
275
+ });
276
+ }
277
+
278
+ async rename(sourcePath: string, targetPath: string): Promise<void> {
279
+ await this.remote.query(`RETURN f"${this.bucketName}:/${sourcePath}".rename($target);`, {
280
+ target: targetPath,
281
+ });
282
+ }
283
+
284
+ async list(prefix?: string): Promise<string[]> {
285
+ const p = prefix ?? '';
286
+ const [result] = await this.remote.query<[string[]]>(
287
+ `RETURN f"${this.bucketName}:/${p}".list();`
288
+ );
289
+ return result;
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Boot hint for which local bucket to open before auth resolves. Written to
295
+ * PLAIN localStorage (never the configured persistenceClient): the surrealdb
296
+ * persistence client stores its keys INSIDE a bucket, and the whole point of
297
+ * the hint is to pick the bucket before any bucket is open. A warm reload of a
298
+ * signed-in user thus opens their own bucket immediately — zero switches.
299
+ * Losing the hint is fail-closed: boot lands on the anon bucket and the auth
300
+ * callback switches to the user's bucket (cache + outbox intact).
301
+ */
302
+ const LAST_BUCKET_KEY = 'sp00ky:last_bucket';
303
+
304
+ /** Reported for engines that don't track local-store durability. Frozen so a
305
+ * subscriber can't mutate the shared snapshot. */
306
+ const UNKNOWN_STORAGE_HEALTH: StorageHealth = Object.freeze({
307
+ status: 'unknown',
308
+ fallback: false,
309
+ });
310
+
311
+ function readBootBucketHint(): string | null {
312
+ try {
313
+ return typeof localStorage !== 'undefined' ? localStorage.getItem(LAST_BUCKET_KEY) : null;
314
+ } catch {
315
+ return null;
316
+ }
317
+ }
318
+
319
+ function writeBootBucketHint(bucketId: string): void {
320
+ try {
321
+ if (typeof localStorage !== 'undefined') localStorage.setItem(LAST_BUCKET_KEY, bucketId);
322
+ } catch {
323
+ /* private-mode storage errors: boot just falls back to the anon bucket */
324
+ }
325
+ }
326
+
327
+ export class Sp00kyClient<S extends SchemaStructure> {
328
+ private local: LocalStore;
329
+ private remote: RemoteDatabaseService;
330
+ private blobs: BlobCache;
331
+ private connectionSupervisor: ConnectionSupervisor;
332
+ private persistenceClient: PersistenceClient;
333
+
334
+ private migrator: LocalMigrator;
335
+ private cache: CacheModule;
336
+ private dataModule: DataModule<S>;
337
+ private sync: Sp00kySync<S>;
338
+ private devTools: DevToolsService;
339
+ private crdtManager: CrdtManager;
340
+ /**
341
+ * True once the LOCAL half of boot is done and the client can serve reads
342
+ * from the local store. Distinct from being connected: `syncHealth` covers
343
+ * reaching the server and `storageHealth` covers whether the local store is
344
+ * durable, but neither says "usable". Consumers gate their first paint on
345
+ * this, which is what makes a warm boot instant and an offline boot possible.
346
+ */
347
+ private localReady = false;
348
+ // Principal the current query-id salt was minted for (`null` = signed out).
349
+ // Rotated only on a real auth flip: see the call sites in `init`.
350
+ private saltUserId: string | null = null;
351
+ private featureFlags!: FeatureFlagModule<S>;
352
+ private appReleases!: AppReleaseModule<S>;
353
+ // Query hashes already preloaded this session — skip redundant one-shot
354
+ // fetches when the same preload query is requested again (e.g. a list row
355
+ // re-rendering). Cleared on process/session end only.
356
+ private preloadedHashes = new Set<number>();
357
+ // In-flight background init chains (instant-hydrate + register enqueue) keyed
358
+ // by registration hash. Concurrent mounts of the same query reuse the one
359
+ // chain instead of double-hydrating and double-enqueuing `register`.
360
+ // Sequential re-mounts intentionally start a fresh chain — the unconditional
361
+ // `register` re-enqueue is what freshens a warm preload on use.
362
+ private pendingQueryInits = new Map<string, Promise<void>>();
363
+
364
+ private logger: ReturnType<typeof createLogger>;
365
+ public auth: AuthService<S>;
366
+ public streamProcessor: StreamProcessorService;
367
+
368
+ // Shared-tabs: non-null when the capability gate passed at construction.
369
+ // The coordinator owns role state; `sharedActive` flips false if the broker
370
+ // rejects/times out and this tab permanently falls back to solo.
371
+ private tabsCoordinator: TabsCoordinator | null = null;
372
+ private sharedActive = false;
373
+
374
+ /** Current shared-tabs role, or null when the feature is off/fell back. */
375
+ get tabRole(): TabRole | null {
376
+ return this.sharedActive ? this.tabsCoordinator!.role : null;
377
+ }
378
+
379
+ get remoteClient() {
380
+ return this.remote.getClient();
381
+ }
382
+
383
+ get localClient() {
384
+ return this.local.getClient();
385
+ }
386
+
387
+ get pendingMutationCount(): number {
388
+ return this.sync.pendingMutationCount;
389
+ }
390
+
391
+ /** Number of times the initial list_ref LIVE subscription retried on
392
+ * the most recent `setCurrentUserId` call. 0 when the SSP's
393
+ * pre-emptive user-table creation got there first; >0 when LIVE
394
+ * registration hit a "table not found" race. Exposed so the e2e
395
+ * suite can guard the pre-emptive path against regression. */
396
+ get liveRetryCount(): number {
397
+ return this.sync.liveRetryCount;
398
+ }
399
+
400
+ subscribeToPendingMutations(cb: (count: number) => void): () => void {
401
+ return this.sync.subscribeToPendingMutations(cb);
402
+ }
403
+
404
+ /** Number of queries mid-fetch right now. See {@link subscribeToFetchActivity}. */
405
+ get fetchingQueryCount(): number {
406
+ return this.dataModule.fetchingQueryCount;
407
+ }
408
+
409
+ /**
410
+ * Observe how many queries are fetching from the server. Fires immediately
411
+ * with the current count and again on every change, so one subscription can
412
+ * drive a "downloading" indicator for the whole app. Pair with
413
+ * {@link subscribeToPendingMutations} for the "uploading" half.
414
+ */
415
+ subscribeToFetchActivity(cb: (fetching: number) => void): () => void {
416
+ return this.dataModule.subscribeActivity(cb, { immediate: true });
417
+ }
418
+
419
+ /** Current sync-health snapshot. See {@link Sp00kyConfig.syncHealth}. */
420
+ get syncHealth(): SyncHealth {
421
+ return this.sync.syncHealth;
422
+ }
423
+
424
+ /**
425
+ * Observe sync health. Fires immediately with the current status and again
426
+ * on every healthy↔degraded transition. Returns an unsubscribe.
427
+ */
428
+ subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void {
429
+ return this.sync.subscribeToSyncHealth(cb);
430
+ }
431
+
432
+ /** Durability of the local cache. See {@link StorageHealth}. `'unknown'` for
433
+ * engines that don't report it. */
434
+ get storageHealth(): StorageHealth {
435
+ return this.local.storageHealth ?? UNKNOWN_STORAGE_HEALTH;
436
+ }
437
+
438
+ /**
439
+ * Observe local-store durability. Fires immediately with the current snapshot
440
+ * and again on every change (at most once per bucket open in practice).
441
+ * Returns an unsubscribe.
442
+ */
443
+ subscribeToStorageHealth(cb: (health: StorageHealth) => void): () => void {
444
+ if (this.local.subscribeToStorageHealth) {
445
+ return this.local.subscribeToStorageHealth(cb);
446
+ }
447
+ cb(UNKNOWN_STORAGE_HEALTH);
448
+ return () => {};
449
+ }
450
+
451
+ constructor(private config: Sp00kyConfig<S>) {
452
+ const logger = createLogger(config.logLevel ?? 'info', config.otelTransmit);
453
+ this.logger = logger.child({ service: 'Sp00kyClient' });
454
+
455
+ this.logger.info(
456
+ {
457
+ config: { ...config, schema: '[SchemaStructure]' },
458
+ Category: 'sp00ky-client::Sp00kyClient::constructor',
459
+ },
460
+ 'Sp00kyClient initialized'
461
+ );
462
+
463
+ // Preload the loro CRDT engine at startup (fetches the chunk on page load)
464
+ // so the first `openCrdtField` doesn't block on a network round-trip. Left
465
+ // off, loro is never loaded unless a CRDT field is explicitly opened.
466
+ if (config.crdt) void preloadLoro();
467
+
468
+ // The default ('surrealdb') engine is a SurrealCacheEngine — a drop-in
469
+ // subclass of LocalDatabaseService that adds the engine-neutral verb surface
470
+ // with zero behavior change. Alternate engines (e.g. 'sqlite') require the
471
+ // raw-SurrealQL call-site migration before they can back `this.local`.
472
+ const tabsSupport = detectSharedTabsSupport(this.config);
473
+ this.local = createLocalEngine(this.config.localEngine, this.config.database, logger, {
474
+ shared: tabsSupport.supported,
475
+ });
476
+ this.remote = new RemoteDatabaseService(this.config.database, logger);
477
+ // Owns socket liveness for the life of the client: re-opens the connection
478
+ // when the SDK's own reconnect gives up, and heartbeats to catch a socket
479
+ // that died without ever firing a `close` event.
480
+ this.connectionSupervisor = new ConnectionSupervisor(this.remote, logger);
481
+
482
+ // Durable cache for bucket file bytes. Reads go through a raw (uncached)
483
+ // handle so the cache's own L2 fetch can't recurse back into itself. The
484
+ // namespace is provisional: `init()` rebinds it to the boot bucket and
485
+ // reconciles against what is actually on disk.
486
+ this.blobs = createBlobCache({
487
+ local: this.local,
488
+ namespace: ANON_USER_ID,
489
+ logger,
490
+ fetchRemote: async (key) =>
491
+ bucketContentToBlob(await this.rawBucket(key.bucket).get(key.path)),
492
+ headRemote: (key) => this.rawBucket(key.bucket).head(key.path),
493
+ maxBytes: this.config.blobCache?.maxBytes,
494
+ // Opting out keeps the in-tab dedupe and object-URL refcounting that
495
+ // existed before this cache, and persists nothing.
496
+ store:
497
+ this.config.blobCache?.enabled === false ? new MemoryBlobStore(ANON_USER_ID) : undefined,
498
+ });
499
+
500
+ if (config.persistenceClient === 'surrealdb') {
501
+ this.persistenceClient = new SurrealDBPersistenceClient(this.local, logger);
502
+ } else if (config.persistenceClient === 'localstorage' || !config.persistenceClient) {
503
+ this.persistenceClient = new LocalStoragePersistenceClient(logger);
504
+ } else {
505
+ this.persistenceClient = config.persistenceClient;
506
+ }
507
+
508
+ this.persistenceClient = new ResilientPersistenceClient(this.persistenceClient, logger);
509
+
510
+ this.streamProcessor = new StreamProcessorService(
511
+ new EventSystem(['stream_update']),
512
+ this.local,
513
+ logger
514
+ );
515
+ // Circuit snapshots are checkpointed, never per-ingest, and on by default
516
+ // where the store can hold them (the OPFS SQLite engine). See
517
+ // `persistCircuit` / `circuitProjection` in types.ts.
518
+ this.streamProcessor.configureCircuitPersistence(
519
+ config.persistCircuit ?? config.localEngine === 'sqlite',
520
+ config.circuitCheckpointMs
521
+ );
522
+ this.streamProcessor.configureProjection(config.circuitProjection ?? true);
523
+ this.migrator = new LocalMigrator(this.local, logger);
524
+
525
+ this.cache = new CacheModule(
526
+ this.local,
527
+ this.streamProcessor,
528
+ (update) => {
529
+ // Direct callback from cache to data module
530
+ this.dataModule.onStreamUpdate(update);
531
+ },
532
+ logger
533
+ );
534
+
535
+ // Initialize CRDT Manager. `local` is used to read the initial
536
+ // `_00_crdt` snapshot when a field opens AND to mirror every local
537
+ // edit (so reload/offline see the freshest state); `remote` is used
538
+ // for the debounced outgoing UPSERTs and the parent-table LIVE feed.
539
+ // The debounce window is configurable via `crdtDebounceMs`.
540
+ this.crdtManager = new CrdtManager(
541
+ this.config.schema,
542
+ this.local,
543
+ this.remote,
544
+ logger,
545
+ config.crdtDebounceMs ?? 500
546
+ );
547
+
548
+ this.dataModule = new DataModule(
549
+ this.cache,
550
+ this.local,
551
+ this.config.schema,
552
+ logger,
553
+ this.config.streamDebounceTime
554
+ );
555
+
556
+ // Initialize Auth
557
+ this.auth = new AuthService(this.config.schema, this.remote, this.persistenceClient, logger);
558
+
559
+ // Initialize Sync
560
+ this.sync = new Sp00kySync(
561
+ this.local,
562
+ this.remote,
563
+ this.cache,
564
+ this.dataModule,
565
+ this.config.schema,
566
+ this.logger,
567
+ {
568
+ refSyncIntervalMs: this.config.refSyncIntervalMs,
569
+ anonymousLiveQueries: this.config.enableAnonymousLiveQueries,
570
+ // `syncHealth: false` (or `{ degradeAfterConsecutiveFailures: 0 }`)
571
+ // disables degraded reporting; otherwise default to 3.
572
+ degradeAfterConsecutiveFailures:
573
+ this.config.syncHealth === false
574
+ ? 0
575
+ : (this.config.syncHealth?.degradeAfterConsecutiveFailures ?? 3),
576
+ pushTimeoutMs: this.config.pushTimeoutMs,
577
+ downTimeoutMs: this.config.downTimeoutMs,
578
+ // Read-only: sync mirrors the transport state into `SyncHealth`.
579
+ connectionSupervisor: this.connectionSupervisor,
580
+ }
581
+ );
582
+ this.sync.setPrimeGate(() => this.streamProcessor.whenPrimed());
583
+
584
+ // Initialize feature flags. Reuses the down-queue to register SSP plans
585
+ // on `_00_user_feature` and the auth subscription to re-register handles
586
+ // when the signed-in user changes.
587
+ this.featureFlags = new FeatureFlagModule({
588
+ dataModule: this.dataModule,
589
+ sync: this.sync,
590
+ auth: this.auth,
591
+ logger,
592
+ });
593
+
594
+ // App release announcements (world-readable `_00_app_release`, written by
595
+ // spky deploy/release). Same shared-live-query design as feature flags.
596
+ this.appReleases = new AppReleaseModule({
597
+ dataModule: this.dataModule,
598
+ sync: this.sync,
599
+ auth: this.auth,
600
+ logger,
601
+ });
602
+
603
+ // Initialize DevTools
604
+ this.devTools = new DevToolsService(
605
+ this.local,
606
+ this.remote,
607
+ logger,
608
+ this.config.schema,
609
+ this.auth,
610
+ this.dataModule
611
+ );
612
+
613
+ // Let the DevTools Access tab read and write local flag overrides. Done
614
+ // here rather than via the constructor because FeatureFlagModule is built
615
+ // above and DevToolsService takes its deps positionally.
616
+ this.devTools.setFeatureFlagOverrides(this.featureFlags);
617
+
618
+ // Register DevTools as a receiver for stream updates
619
+ this.streamProcessor.addReceiver(this.devTools);
620
+
621
+ // Wire up callbacks instead of events
622
+ this.setupCallbacks();
623
+
624
+ // Shared-tabs: construct the coordinator last so its hooks can close over
625
+ // every module. Nothing starts until init() calls coordinator.start().
626
+ if (tabsSupport.supported) {
627
+ this.tabsCoordinator = this.buildTabsCoordinator();
628
+ } else if (this.config.sharedTabs) {
629
+ this.logger.info(
630
+ {
631
+ reason: (tabsSupport as { reason: string }).reason,
632
+ Category: 'sp00ky-client::Sp00kyClient::tabs',
633
+ },
634
+ 'sharedTabs requested but unsupported here; running solo'
635
+ );
636
+ }
637
+ // Report shared-tabs state whenever the feature was REQUESTED, active or
638
+ // not: "asked to share one store, running alone because X" is exactly the
639
+ // thing you need to see in the panel. Apps that never set the flag report
640
+ // null and the panel shows no section at all.
641
+ if (this.config.sharedTabs) {
642
+ const unsupportedReason = tabsSupport.supported
643
+ ? undefined
644
+ : (tabsSupport as { reason: string }).reason;
645
+ this.devTools.setTabsInfoProvider(() => {
646
+ const c = this.tabsCoordinator;
647
+ if (!this.sharedActive || !c) {
648
+ return { active: false, reason: unsupportedReason ?? 'fell-back' };
649
+ }
650
+ const hub = c.syncHub;
651
+ return {
652
+ active: true,
653
+ role: c.role,
654
+ tabId: c.tabId,
655
+ leadershipId: c.leadershipId,
656
+ leaderTabId: c.role === 'leader' ? c.tabId : c.leaderTabId,
657
+ ...(hub ? { followers: hub.followerCount, relayedBatches: hub.relayedBatches } : {}),
658
+ };
659
+ });
660
+ }
661
+ this.devTools.setBlobInfoProvider(() => this.blobs.stats());
662
+ }
663
+
664
+ /** The shared-tabs role machinery, wired to this client's modules. */
665
+ private buildTabsCoordinator(): TabsCoordinator {
666
+ const engine = this.local as SqliteCacheEngine;
667
+ const tabId =
668
+ typeof crypto !== 'undefined' && crypto.randomUUID
669
+ ? crypto.randomUUID()
670
+ : `tab_${Math.random().toString(36).slice(2)}`;
671
+ const hooks: CoordinatorHooks = {
672
+ adoptOwner: (bucketId, opts) =>
673
+ engine.adoptOwner(bucketId, {
674
+ workerLockName: opts.workerLockName,
675
+ allowMemoryFallback: opts.allowMemoryFallback,
676
+ resumeHeld: opts.resumeHeld,
677
+ }),
678
+ adoptAttached: (dbPort, snapshot) =>
679
+ engine.adoptAttached(dbPort, snapshot, (reason) => engine.onLeaderLost(reason)),
680
+ releaseOwnership: () => engine.releaseOwnership(),
681
+ onLeaderLost: (reason) => engine.onLeaderLost(reason),
682
+ exposeClientPort: (clientId, port) => engine.exposeClientPort(clientId, port),
683
+ removeClientPort: (clientId) => engine.removeClientPort(clientId),
684
+ becomeSyncLeader: (hub) => {
685
+ this.streamProcessor.setPersistenceEnabled(true);
686
+ this.cache.setIngestRelay((tuples) => hub.relayIngest(tuples));
687
+ this.sync.setTabContext('leader', tabId);
688
+ this.dataModule.setTabId(tabId);
689
+ this.sync.promoteToLeader(hub);
690
+ },
691
+ resumeSyncLeaderDuties: () => this.sync.resumeLeaderDuties(),
692
+ becomeSyncFollower: (forwarder) => {
693
+ this.streamProcessor.setPersistenceEnabled(false);
694
+ // A follower's own mutations go to the leader, which ingests them and
695
+ // fans them out to the other followers: one hop, no server round-trip.
696
+ // Only the mutation path relays; this tab's sync fetches are the
697
+ // leader's data coming back and must not be re-broadcast.
698
+ this.cache.setIngestRelay((tuples) => forwarder.ingest(tuples), {
699
+ localWritesOnly: true,
700
+ });
701
+ this.sync.setTabContext('follower', tabId);
702
+ this.dataModule.setTabId(tabId);
703
+ // Installs the whole syncPort handler: ingest-relay, list_ref relay,
704
+ // settled writes, rollbacks.
705
+ this.sync.demoteToFollower(forwarder);
706
+ },
707
+ becomeSyncSolo: () => {
708
+ this.streamProcessor.setPersistenceEnabled(true);
709
+ this.cache.setIngestRelay(null);
710
+ this.sync.setTabContext('solo', tabId);
711
+ },
712
+ currentStorageHealth: () =>
713
+ this.local.storageHealth ?? { status: 'unknown', fallback: false },
714
+ };
715
+ return new TabsCoordinator({
716
+ tabId,
717
+ fingerprint: computeTabsFingerprint({
718
+ coreVersion:
719
+ typeof __SP00KY_CORE_VERSION__ !== 'undefined' ? __SP00KY_CORE_VERSION__ : 'unknown',
720
+ schemaHash: hash53(this.config.schemaSurql),
721
+ endpoint: this.config.database.endpoint ?? '',
722
+ namespace: this.config.database.namespace,
723
+ database: this.config.database.database,
724
+ }),
725
+ hooks,
726
+ logger: this.logger,
727
+ onLeaderPageHide: () => {
728
+ void engine.shutdownOwnedWorker();
729
+ },
730
+ });
731
+ }
732
+
733
+ /**
734
+ * Setup direct callbacks instead of event subscriptions
735
+ */
736
+ private setupCallbacks() {
737
+ // Surface query fetch-status changes (idle/fetching) in DevTools. Logs a
738
+ // discrete event and triggers a state push so the active-queries panel
739
+ // reflects the flip immediately.
740
+ this.dataModule.onQueryStatusChange = (queryHash, status) => {
741
+ this.devTools.logEvent('QUERY_STATUS_CHANGED', { queryHash, status });
742
+ };
743
+
744
+ // Keep an actively-watched query's remote `_00_query.lastActiveAt` fresh so
745
+ // the server TTL sweep doesn't expire it out from under live subscribers.
746
+ // DataModule fires this only while the query still has ≥1 subscriber.
747
+ this.dataModule.onHeartbeat = (queryHash) => {
748
+ void this.sync.heartbeatQuery(queryHash).catch((err) => {
749
+ this.logger.warn(
750
+ { err, queryHash, Category: 'sp00ky-client::Sp00kyClient::onHeartbeat' },
751
+ 'TTL heartbeat failed'
752
+ );
753
+ });
754
+ };
755
+
756
+ // The server no longer has the `_00_query` row of a query we hold
757
+ // membership for (TTL sweep, SSP reset, scheduler wipe). Rebuild the view
758
+ // through the ordinary register path; `register` events coalesce in the
759
+ // down queue, so a lost view costs one registration however many reads
760
+ // noticed it. Local state is untouched until the new set lands.
761
+ this.dataModule.onViewLost = (queryHash) => {
762
+ this.devTools.logEvent('QUERY_VIEW_LOST', { queryHash });
763
+ this.sync.enqueueDownEvent({ type: 'register', payload: { hash: queryHash } });
764
+ };
765
+
766
+ this.dataModule.onQueryAuthorityChange = (queryHash, known) => {
767
+ this.devTools.logEvent('QUERY_AUTHORITY_CHANGED', { queryHash, known });
768
+ };
769
+
770
+ // Eager teardown of an opt-in deregistered query: enqueue a `cleanup`
771
+ // down-event so it's serialized after any in-flight register/sync for the
772
+ // same query (avoids out-of-order delete-before-create).
773
+ this.dataModule.onDeregister = (queryHash) => {
774
+ this.sync.enqueueDownEvent({ type: 'cleanup', payload: { hash: queryHash } });
775
+ };
776
+
777
+ // Mutation callback for sync
778
+ this.dataModule.onMutation((mutations: UpEvent[]) => {
779
+ // Notify DevTools
780
+ this.devTools.onMutation(mutations);
781
+
782
+ // Enqueue in Sync
783
+ if (mutations.length > 0) {
784
+ this.sync.enqueueMutation(mutations);
785
+ }
786
+ });
787
+
788
+ // Sync events for incoming updates
789
+ this.sync.events.subscribe('SYNC_QUERY_UPDATED', (event: any) => {
790
+ this.devTools.logEvent('SYNC_QUERY_UPDATED', event.payload);
791
+ });
792
+
793
+ // Hand list_ref-driven row ingests to the CrdtManager so CRDT body
794
+ // / cursor updates reach the receiver even when the cross-session
795
+ // LIVE on the parent table is filtered out by the SurrealDB
796
+ // permission-LIVE gap. Same-user clients receive these rows via
797
+ // CrdtManager's own `LIVE SELECT * FROM <table>`; this hook is the
798
+ // redundant path that fires when only the list_ref bumped.
799
+ this.sync.engineEvents.subscribe('SYNC_REMOTE_DATA_INGESTED', (event: any) => {
800
+ try {
801
+ const records: Array<Record<string, any>> = event.payload?.records ?? [];
802
+ for (const row of records) {
803
+ const id = row?.id;
804
+ const table =
805
+ id && typeof id === 'object' && id.table !== undefined ? String(id.table) : undefined;
806
+ if (!table) continue;
807
+ this.crdtManager.applyRow(table, row);
808
+ }
809
+ } catch (err) {
810
+ this.logger.debug(
811
+ { err, Category: 'sp00ky-client::engineEvents::ingested' },
812
+ 'applyRow forwarding from sync ingest failed'
813
+ );
814
+ }
815
+ });
816
+
817
+ // Database events for DevTools
818
+ this.local.getEvents().subscribe('DATABASE_LOCAL_QUERY', (event: any) => {
819
+ this.devTools.logEvent('LOCAL_QUERY', event.payload);
820
+ });
821
+
822
+ this.remote.getEvents().subscribe('DATABASE_REMOTE_QUERY', (event: any) => {
823
+ this.devTools.logEvent('REMOTE_QUERY', event.payload);
824
+ });
825
+ }
826
+
827
+ async init() {
828
+ this.logger.info(
829
+ { Category: 'sp00ky-client::Sp00kyClient::init' },
830
+ 'Sp00kyClient initialization started'
831
+ );
832
+ try {
833
+ // Open the bucket the last session used (per-user local stores). If auth
834
+ // resolves to a different user below, the auth callback switches buckets.
835
+ const bootBucket = readBootBucketHint() ?? ANON_USER_ID;
836
+ if (this.tabsCoordinator) {
837
+ // Shared-tabs: the broker assigns this tab's role; the coordinator's
838
+ // hooks open the store (leader) or attach to the leader's (follower).
839
+ // Any failure here (no SharedWorker start, election timeout, rejected
840
+ // fingerprint) falls back to plain solo boot: exactly the flag-off
841
+ // path, including the second-tab memory fallback + its warning.
842
+ // A role can still land AFTER start() gave up (the election that timed
843
+ // out here keeps running), and when it does this tab really is sharing
844
+ // the leader's store. Track every transition so the reported state is
845
+ // the current one instead of frozen at whatever boot saw.
846
+ this.tabsCoordinator.onRoleChange((role) => {
847
+ this.sharedActive = role !== 'solo';
848
+ // Surface the transition in DevTools. Reconstructing a failover from
849
+ // the `database.tabs` snapshot alone is guesswork after the fact; a
850
+ // discrete event gives `get_events` the leader handover directly.
851
+ this.devTools.logEvent('TABS_ROLE_CHANGED', {
852
+ role,
853
+ tabId: this.tabsCoordinator?.tabId,
854
+ leadershipId: this.tabsCoordinator?.leadershipId,
855
+ leaderTabId: this.tabsCoordinator?.leaderTabId,
856
+ promotionMs: this.tabsCoordinator?.lastPromotionMs,
857
+ });
858
+ });
859
+ try {
860
+ const role = await this.tabsCoordinator.start(bootBucket);
861
+ this.sharedActive = true;
862
+ this.logger.info(
863
+ { role, bootBucket, Category: 'sp00ky-client::Sp00kyClient::init' },
864
+ 'Shared-tabs role assigned'
865
+ );
866
+ } catch (e) {
867
+ this.logger.warn(
868
+ { err: e, Category: 'sp00ky-client::Sp00kyClient::init' },
869
+ 'Shared-tabs unavailable; booting solo'
870
+ );
871
+ this.sharedActive = false;
872
+ await this.local.connect(bootBucket);
873
+ }
874
+ } else {
875
+ await this.local.connect(bootBucket);
876
+ }
877
+ this.logger.debug(
878
+ { bootBucket, Category: 'sp00ky-client::Sp00kyClient::init' },
879
+ 'Local database connected'
880
+ );
881
+
882
+ // Schemaless local engines (SQLite) create tables lazily and need no
883
+ // SurrealQL DDL provisioning.
884
+ if (this.local.usesSurqlSchema) {
885
+ await this.migrator.provision(this.config.schemaSurql);
886
+ this.logger.debug({ Category: 'sp00ky-client::Sp00kyClient::init' }, 'Schema provisioned');
887
+ }
888
+
889
+ // Warm the blob cache in the background. Deliberately NOT awaited, and
890
+ // deliberately after `remote.connect()`: this walks the OPFS directory to
891
+ // rebuild the manifest, and awaiting it ahead of the socket delayed the
892
+ // connect (and the connection supervisor with it) for no benefit. Reads
893
+ // await `BlobCache.ready` internally, so a bucket read that lands mid-walk
894
+ // still sees a reconciled manifest. Best-effort: a cold blob cache is a
895
+ // slow first image, not a broken client.
896
+ void (async () => {
897
+ try {
898
+ this.blobs.setMaxBytes(await resolveBlobBudget(this.config.blobCache?.maxBytes));
899
+ await this.blobs.start(bootBucket);
900
+ } catch (e) {
901
+ this.logger.warn(
902
+ { err: e, Category: 'sp00ky-client::Sp00kyClient::init' },
903
+ 'Blob cache failed to start; bucket files will not be cached locally'
904
+ );
905
+ }
906
+ })();
907
+
908
+ await this.streamProcessor.init();
909
+ // Seed table `select` permissions from the schema before any query is
910
+ // registered — otherwise the SSP default-denies every non-`_00_` table.
911
+ this.streamProcessor.setPermissions(extractSelectPermissions(this.config.schemaSurql));
912
+ // Fill the circuit from the local store in the background (snapshot +
913
+ // reconcile, or every cached row). Not awaited: first paint reads the
914
+ // store directly; sync waits on `whenPrimed` before its first diff.
915
+ void this.primeCircuit();
916
+ this.logger.debug(
917
+ { Category: 'sp00ky-client::Sp00kyClient::init' },
918
+ 'StreamProcessor initialized'
919
+ );
920
+
921
+
922
+ // Restore the session from the cached JWT, with no network. This is what
923
+ // lets the rest of the boot - and the app on top of it - proceed as a
924
+ // signed-in user before a socket exists. `initRemote()` verifies the
925
+ // token afterwards and signs out for real if the server rejects it.
926
+ const restoredUserId = await this.auth.restoreSessionFromToken();
927
+
928
+ // Salt for query-id hashing, minted locally (see `mintSessionSalt`). It
929
+ // is stable for the life of this client: the salt keys `_00_query` rows
930
+ // and local cache entries, so rotating it would invalidate every query
931
+ // hash and force a full re-register. Auth flips are the one case that
932
+ // must rotate it - a sign-in is a different principal.
933
+ const sessionId = this.mintSessionSalt();
934
+ this.saltUserId = restoredUserId;
935
+ await this.dataModule.init(sessionId);
936
+ this.crdtManager.setSessionId(sessionId);
937
+
938
+ // Route queries and satisfy `$auth`-gated permission predicates from the
939
+ // restored identity, BEFORE anything can register. Without this a query
940
+ // registering pre-verification would target the wrong
941
+ // `_00_query_user_<id>` table and register a permission-dead SSP view.
942
+ if (restoredUserId) {
943
+ this.dataModule.setCurrentUserId(restoredUserId);
944
+ this.streamProcessor.setSessionAuth(this.sessionAuthId(), this.auth.access);
945
+ }
946
+ this.logger.debug(
947
+ { sessionId, Category: 'sp00ky-client::Sp00kyClient::init' },
948
+ 'DataModule initialized'
949
+ );
950
+
951
+ // Refresh the salt whenever auth state flips (sign-in, sign-out).
952
+ // session::id() changes per WebSocket session, and a sign-in spawns
953
+ // a new authenticated session, so the salt must follow. Also
954
+ // forward the user id into `DataModule` and `Sp00kySync` so they
955
+ // can route to per-user `_00_query_user_<id>` /
956
+ // `_00_list_ref_user_<id>` tables in `RefMode.Dedicated` — the
957
+ // LIVE subscription on `_00_list_ref_user_<id>` is restarted
958
+ // under the new auth context inside `Sp00kySync.setCurrentUserId`
959
+ // since SurrealDB binds the LIVE permission at registration time.
960
+ //
961
+ // Sync prefix BEFORE the first `await`: setting `currentUserId`
962
+ // synchronously here is critical because the AuthProvider's own
963
+ // subscribe callback runs right after ours and immediately enables
964
+ // queries that depend on the user id. Any `await` before
965
+ // `setCurrentUserId` would let those queries register against the
966
+ // stale (null) user id and hit the wrong `_00_query[_user_*]`
967
+ // table.
968
+ this.auth.subscribe(async (userId) => {
969
+ this.dataModule.setCurrentUserId(userId);
970
+ // Mirror the server's `fn::query::register` auth injection for the
971
+ // in-browser SSP: feed the current user's full record id + access
972
+ // method so `$auth`-gated table permissions (e.g. `thread`) resolve
973
+ // locally instead of being rejected. Set synchronously BEFORE the
974
+ // first `await` (like `setCurrentUserId` above) so queries that
975
+ // re-register on this auth flip see the fresh context, not a stale one.
976
+ this.streamProcessor.setSessionAuth(this.sessionAuthId(), this.auth.access);
977
+ // Record the target bucket synchronously (still before the first
978
+ // `await`) so a reload mid-switch boots straight into the right store.
979
+ writeBootBucketHint(bucketIdForUser(userId));
980
+ // FIRST await: swap the local store to this user's bucket. Serialized
981
+ // + latest-target-wins internally; no-op when the bucket already
982
+ // matches (the boot-hint warm path).
983
+ await this.ensureLocalBucket(userId);
984
+ // Only rotate the salt when the PRINCIPAL actually changed: a sign-in or
985
+ // sign-out is a different principal and must not keep the old
986
+ // principal's query ids, but the first fire of this callback after boot
987
+ // carries the same user the salt was already minted for, and rotating
988
+ // there would invalidate every query hash for no change in value.
989
+ // Canonicalize with the SAME encoding the restore path used, NOT
990
+ // String(): after background verification this callback carries a
991
+ // RecordId, while a session restored from the token carries the plain
992
+ // "table:id" string. Comparing their raw stringifications made every
993
+ // warm boot look like a principal change, which rotated the salt and
994
+ // re-registered every mounted query - the list painted from cache and
995
+ // then emptied a second later.
996
+ const saltFor = this.sessionAuthId();
997
+ if (saltFor !== this.saltUserId) {
998
+ this.saltUserId = saltFor;
999
+ const next = this.mintSessionSalt();
1000
+ this.dataModule.setSessionId(next);
1001
+ this.crdtManager.setSessionId(next);
1002
+ }
1003
+ try {
1004
+ await this.sync.setCurrentUserId(userId);
1005
+ } catch (e) {
1006
+ this.logger.error(
1007
+ { error: e, Category: 'sp00ky-client::Sp00kyClient::authChange' },
1008
+ 'sync.setCurrentUserId failed'
1009
+ );
1010
+ }
1011
+ // After the bucket switch, so the row lands in THIS user's store.
1012
+ await this.persistVerifiedUser();
1013
+ });
1014
+
1015
+ await this.sync.init();
1016
+ this.logger.debug({ Category: 'sp00ky-client::Sp00kyClient::init' }, 'Sync initialized');
1017
+
1018
+ // Hand this tab's views back when the page goes away. Without it they
1019
+ // stay materialized on the SSP for a whole TTL after the tab is gone —
1020
+ // see `Sp00kySync.releaseViewsOnUnload`.
1021
+ //
1022
+ // `pagehide` rather than `beforeunload`: it is the one teardown event
1023
+ // that also fires on mobile, where tabs are discarded rather than
1024
+ // closed. `persisted` means the page went into the bfcache and can come
1025
+ // BACK, so releasing there would tear down views a live page still
1026
+ // needs.
1027
+ if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') {
1028
+ const onPageHide = (event: PageTransitionEvent) => {
1029
+ if (event.persisted) return;
1030
+ this.sync.releaseViewsOnUnload();
1031
+ };
1032
+ window.addEventListener('pagehide', onPageHide);
1033
+ // Re-assert liveness the moment a throttled tab is back in front, or
1034
+ // the network is back: the sweep may have taken views while the TTL
1035
+ // timers were frozen, and `heartbeatQuery` rebuilds a swept view. Only
1036
+ // watched queries beat, and never twice within 30s.
1037
+ const onWake = () => {
1038
+ if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return;
1039
+ this.dataModule.heartbeatOnWake();
1040
+ };
1041
+ if (typeof document !== 'undefined' && typeof document.addEventListener === 'function') {
1042
+ document.addEventListener('visibilitychange', onWake);
1043
+ }
1044
+ window.addEventListener('online', onWake);
1045
+ this.detachUnloadRelease = () => {
1046
+ window.removeEventListener('pagehide', onPageHide);
1047
+ window.removeEventListener('online', onWake);
1048
+ if (typeof document !== 'undefined' && typeof document.removeEventListener === 'function') {
1049
+ document.removeEventListener('visibilitychange', onWake);
1050
+ }
1051
+ };
1052
+ }
1053
+
1054
+ this.featureFlags.init();
1055
+ this.logger.debug(
1056
+ { Category: 'sp00ky-client::Sp00kyClient::init' },
1057
+ 'FeatureFlagModule initialized'
1058
+ );
1059
+
1060
+ this.appReleases.init();
1061
+ this.logger.debug(
1062
+ { Category: 'sp00ky-client::Sp00kyClient::init' },
1063
+ 'AppReleaseModule initialized'
1064
+ );
1065
+
1066
+ // LOCAL BOOT IS DONE — the client can serve reads. Consumers gate their
1067
+ // UI on this resolving, so everything above must stay network-free.
1068
+ this.localReady = true;
1069
+ this.logger.info(
1070
+ { Category: 'sp00ky-client::Sp00kyClient::init' },
1071
+ 'Sp00kyClient local initialization completed; connecting in the background'
1072
+ );
1073
+
1074
+ // The network half, deliberately NOT awaited. Nothing above needed it:
1075
+ // queries paint from the local store, `sync.init()` tolerates a closed
1076
+ // socket, and the session was restored from the cached token. Awaiting
1077
+ // this was the entire reason a warm reload sat on a loading screen for
1078
+ // seconds over data the browser already had on disk - and the reason an
1079
+ // offline boot never completed at all.
1080
+ void this.initRemote();
1081
+ } catch (e) {
1082
+ this.logger.error(
1083
+ { error: e, Category: 'sp00ky-client::Sp00kyClient::init' },
1084
+ 'Sp00kyClient initialization failed'
1085
+ );
1086
+ throw e;
1087
+ }
1088
+ }
1089
+
1090
+ /**
1091
+ * The network half of boot: connect, verify the restored session, and let the
1092
+ * sync engine catch up. Runs in the background after `init()` has already
1093
+ * resolved, so nothing here is on the paint path.
1094
+ *
1095
+ * Every step is best-effort. A failure leaves the client in exactly the state
1096
+ * a warm offline boot is in - local reads working, writes queued in the
1097
+ * outbox - and the connection supervisor keeps retrying underneath.
1098
+ */
1099
+ private async initRemote(): Promise<void> {
1100
+ try {
1101
+ await this.remote.connect();
1102
+ this.logger.debug(
1103
+ { Category: 'sp00ky-client::Sp00kyClient::initRemote' },
1104
+ 'Remote database connected'
1105
+ );
1106
+ } catch (e) {
1107
+ // NOT fatal. This used to throw out of init() and leave the consuming app
1108
+ // on its loading screen forever with no network. The supervisor (started
1109
+ // unconditionally below) owns the retry from here.
1110
+ this.logger.warn(
1111
+ { err: e, Category: 'sp00ky-client::Sp00kyClient::initRemote' },
1112
+ 'Remote connect failed; running from the local store and retrying in the background'
1113
+ );
1114
+ }
1115
+
1116
+ // Started whether or not the first connect succeeded - it is the thing that
1117
+ // revives the socket, so gating it on a successful connect would mean a
1118
+ // boot-time failure never recovered.
1119
+ this.connectionSupervisor.start();
1120
+
1121
+ try {
1122
+ // Verifies the optimistically restored token against the server. On a
1123
+ // rejected token this signs out for real; on an unreachable server it
1124
+ // keeps the cached session (see AuthService.check).
1125
+ await this.auth.init();
1126
+ this.logger.debug(
1127
+ { Category: 'sp00ky-client::Sp00kyClient::initRemote' },
1128
+ 'Auth verified'
1129
+ );
1130
+ } catch (e) {
1131
+ this.logger.warn(
1132
+ { err: e, Category: 'sp00ky-client::Sp00kyClient::initRemote' },
1133
+ 'Auth verification failed; keeping the restored session'
1134
+ );
1135
+ }
1136
+ }
1137
+
1138
+ /**
1139
+ * Write the signed-in user's own row, as the SERVER just returned it, into
1140
+ * the local store.
1141
+ *
1142
+ * `AuthService.check()` (boot verification, sign-in, an app-driven refresh)
1143
+ * runs `SELECT * FROM ONLY $auth.id` and keeps the answer on
1144
+ * `auth.currentUser`; nothing consumed it beyond the id. Meanwhile the app's
1145
+ * own `user` query paints local-first from whatever body the store holds,
1146
+ * and that body is only refreshed when the server's row version reaches
1147
+ * this client through membership (`_00_list_ref`). While the SSP is down or
1148
+ * bootstrapping that never happens, so a device that registered before the
1149
+ * account was verified kept rendering a row without `email_verified` and
1150
+ * the app parked the user on "Verify your email" over a server that said
1151
+ * "already verified". The authoritative row was already in hand.
1152
+ *
1153
+ * MERGE semantics (`CacheModule.saveBatch`) keep local-only fields, and
1154
+ * re-using the memoized version keeps the sync dedup honest: this is a
1155
+ * field refresh, not a version claim. `notifyTableQueries` re-materializes
1156
+ * the `user` queries so the gate flips without waiting for a stream update.
1157
+ * Best-effort: a failure here leaves things exactly as they were.
1158
+ */
1159
+ private async persistVerifiedUser(): Promise<void> {
1160
+ const row = this.auth.currentUser as Record<string, unknown> | null;
1161
+ if (!row || !(row.id instanceof RecordId) || Object.keys(row).length <= 1) return;
1162
+ const rid = row.id as RecordId;
1163
+ const table = rid.table.toString();
1164
+ const tableSchema = this.config.schema.tables.find((t) => t.name === table);
1165
+ if (!tableSchema) return;
1166
+ try {
1167
+ const encoded = encodeRecordId(rid);
1168
+ const version = this.cache.lookup(encoded) || 1;
1169
+ await this.cache.saveBatch([
1170
+ {
1171
+ table,
1172
+ op: 'UPDATE',
1173
+ record: cleanRecord(tableSchema.columns, row) as RecordWithId,
1174
+ version,
1175
+ },
1176
+ ]);
1177
+ await this.dataModule.notifyTableQueries(table);
1178
+ } catch (e) {
1179
+ this.logger.warn(
1180
+ { err: e, Category: 'sp00ky-client::Sp00kyClient::persistVerifiedUser' },
1181
+ 'Could not persist the verified user row locally'
1182
+ );
1183
+ }
1184
+ }
1185
+
1186
+ // Serializes bucket switches from rapid auth flips; `pendingBucketTarget`
1187
+ // makes intermediate targets collapse (A→anon→B never opens the anon bucket).
1188
+ private bucketSwitchChain: Promise<void> = Promise.resolve();
1189
+ private pendingBucketTarget: string | null = null;
1190
+
1191
+ /**
1192
+ * Ensure the local store is this user's bucket, switching if needed. Called
1193
+ * from the auth listener on every auth flip; concurrent calls are chained
1194
+ * and superseded intermediates are skipped (latest target wins).
1195
+ */
1196
+ private ensureLocalBucket(userId: string | null): Promise<void> {
1197
+ const target = bucketIdForUser(userId);
1198
+ this.pendingBucketTarget = target;
1199
+ // Close the query gate SYNCHRONOUSLY the instant a switch is pending — the
1200
+ // AuthProvider's own auth subscriber fires right after this (same tick) and
1201
+ // enables queries, and `doSwitchBucket` only runs a microtask later on the
1202
+ // chain. Without closing the gate here, that query is issued through the
1203
+ // still-open gate and is in-flight on the local wasm engine when
1204
+ // `switchStore` closes the client — which wedges the engine (every
1205
+ // subsequent query, including provisioning, hangs → no view ever registers).
1206
+ // No-op when already on the target bucket.
1207
+ const needsSwitch = this.local.currentBucketId !== target;
1208
+ const release = needsSwitch ? this.local.beginSwitch() : null;
1209
+ this.bucketSwitchChain = this.bucketSwitchChain.then(async () => {
1210
+ // Superseded by a newer flip, or already on target: reopen the gate we
1211
+ // closed above and skip the switch.
1212
+ if (this.pendingBucketTarget !== target || this.local.currentBucketId === target) {
1213
+ release?.();
1214
+ return;
1215
+ }
1216
+ await this.doSwitchBucket(target, release);
1217
+ });
1218
+ // Isolate chain failures per-caller: a failed switch must not poison every
1219
+ // future switch. The caller (auth listener) logs it. Reopen the gate on
1220
+ // failure so the client never gets stuck closed.
1221
+ const result = this.bucketSwitchChain;
1222
+ this.bucketSwitchChain = this.bucketSwitchChain.catch(() => {
1223
+ release?.();
1224
+ });
1225
+ return result;
1226
+ }
1227
+
1228
+ /**
1229
+ * The bucket-switch choreography: drain → swap → rebind.
1230
+ *
1231
+ * Drain: sync quiesced (poll/LIVE stopped, in-flight round awaited so its
1232
+ * outbox delete lands in the OLD bucket, debounce timers cancelled),
1233
+ * DataModule timers cleared, CRDT fields closed WITHOUT their final flush
1234
+ * (the remote session already belongs to the next user).
1235
+ *
1236
+ * Swap: gate closes so any local query issued mid-switch (sibling auth
1237
+ * subscribers, FeatureFlagModule) waits and then runs against the NEW
1238
+ * bucket; store swaps open-new-before-close-old; schema provisions
1239
+ * (no-op for a returning bucket); stale `_00_query` rows are wiped (dead
1240
+ * sessionId-salted hashes with stale arrays — record bodies stay warm);
1241
+ * SSP resets to a fresh circuit with re-seeded permissions.
1242
+ *
1243
+ * Rebind: auth token re-persisted (the surrealdb persistence client wrote it
1244
+ * into the OLD bucket's `_00_kv` before this listener ran), active queries
1245
+ * re-homed keeping their hashes, sync resumed on the new bucket's own
1246
+ * outbox, and every query re-registered remotely to refill from the server.
1247
+ */
1248
+ private async doSwitchBucket(target: string, gateRelease?: (() => void) | null): Promise<void> {
1249
+ this.logger.info(
1250
+ {
1251
+ target,
1252
+ from: this.local.currentBucketId,
1253
+ Category: 'sp00ky-client::Sp00kyClient::doSwitchBucket',
1254
+ },
1255
+ 'Switching local bucket'
1256
+ );
1257
+
1258
+ const leavingBucket = this.local.currentBucketId;
1259
+
1260
+ await this.sync.prepareBucketSwitch();
1261
+ this.dataModule.quiesce();
1262
+ this.crdtManager.closeAll({ flush: false });
1263
+
1264
+ // Reuse the gate the caller (`ensureLocalBucket`) closed synchronously; only
1265
+ // open our own if called without one (keeps the gate continuously closed
1266
+ // from the auth flip through the swap — no window for a racing query).
1267
+ const reopen = gateRelease ?? this.local.beginSwitch();
1268
+ try {
1269
+ if (this.sharedActive && this.tabsCoordinator) {
1270
+ // Shared-tabs: a bucket switch is a namespace move. Leaving the old
1271
+ // namespace re-elects it (if this tab led it); joining the new one
1272
+ // assigns a fresh role, whose hooks open or attach the store. The
1273
+ // leader wipe-on-pool-open replaces the DELETE _00_query below, and a
1274
+ // joining follower must NOT wipe: other tabs' rows there are live.
1275
+ try {
1276
+ await this.tabsCoordinator.moveToBucket(target);
1277
+ } catch (e) {
1278
+ this.logger.warn(
1279
+ { err: e, target, Category: 'sp00ky-client::Sp00kyClient::doSwitchBucket' },
1280
+ 'Shared-tabs bucket move failed; switching solo'
1281
+ );
1282
+ this.sharedActive = false;
1283
+ this.sync.setTabContext('solo', null);
1284
+ this.cache.setIngestRelay(null);
1285
+ this.streamProcessor.setPersistenceEnabled(true);
1286
+ await this.local.switchStore(target);
1287
+ await this.local.queryUngated('DELETE _00_query;');
1288
+ }
1289
+ } else {
1290
+ await this.local.switchStore(target);
1291
+ if (this.local.usesSurqlSchema) {
1292
+ await this.migrator.provision(this.config.schemaSurql);
1293
+ }
1294
+ await this.local.queryUngated('DELETE _00_query;');
1295
+ }
1296
+ // Cached bytes are namespaced per local bucket, so the switch is a
1297
+ // repoint, not a wipe: signing back in finds the cache warm. Apps that
1298
+ // want the signed-out user's files gone opt in with `clearOnSignOut`.
1299
+ try {
1300
+ if (
1301
+ this.config.blobCache?.clearOnSignOut &&
1302
+ target === ANON_USER_ID &&
1303
+ leavingBucket !== ANON_USER_ID
1304
+ ) {
1305
+ await this.blobs.clear();
1306
+ }
1307
+ await this.blobs.setNamespace(target);
1308
+ } catch (e) {
1309
+ this.logger.warn(
1310
+ { err: e, target, Category: 'sp00ky-client::Sp00kyClient::doSwitchBucket' },
1311
+ 'Blob cache bucket switch failed'
1312
+ );
1313
+ }
1314
+ await this.streamProcessor.reset();
1315
+ this.streamProcessor.setPermissions(extractSelectPermissions(this.config.schemaSurql));
1316
+ this.cache.clearVersionLookups();
1317
+ void this.primeCircuit();
1318
+ // Preload dedup is per-bucket: the `_00_preload` markers + cached rows it
1319
+ // guards live in the local store we just swapped away from. Keeping the
1320
+ // hashes would make `preload()` skip warming the NEW bucket (its store is
1321
+ // empty), so every thread/comment prewarm silently no-ops after login.
1322
+ this.preloadedHashes.clear();
1323
+ } finally {
1324
+ reopen();
1325
+ }
1326
+
1327
+ if (this.auth.token) {
1328
+ try {
1329
+ await this.persistenceClient.set('sp00ky_auth_token', this.auth.token);
1330
+ } catch (e) {
1331
+ this.logger.warn(
1332
+ { error: e, Category: 'sp00ky-client::Sp00kyClient::doSwitchBucket' },
1333
+ 'Failed to re-persist auth token into the new bucket'
1334
+ );
1335
+ }
1336
+ }
1337
+
1338
+ const hashes = await this.dataModule.rebindAfterBucketSwitch();
1339
+ await this.sync.completeBucketSwitch();
1340
+ for (const hash of hashes) {
1341
+ this.sync.enqueueDownEvent({ type: 'register', payload: { hash } });
1342
+ }
1343
+
1344
+ this.logger.info(
1345
+ { target, queries: hashes.length, Category: 'sp00ky-client::Sp00kyClient::doSwitchBucket' },
1346
+ 'Local bucket switch complete'
1347
+ );
1348
+ }
1349
+
1350
+ /** Removes the `pagehide` listener installed in `init`; see `close`. */
1351
+ private detachUnloadRelease: (() => void) | null = null;
1352
+
1353
+ async close() {
1354
+ // Before anything else: a live supervisor would read the intentional
1355
+ // `remote.close()` below as a failure and immediately reconnect.
1356
+ this.connectionSupervisor.dispose();
1357
+ await this.featureFlags.closeAll();
1358
+ await this.appReleases.closeAll();
1359
+ this.crdtManager.closeAll();
1360
+ this.crdtManager.dispose();
1361
+ // Flush the blob manifest while the local store is still open — the flush
1362
+ // writes `_00_blob` rows through it — and revoke every object URL.
1363
+ await this.blobs.close().catch(() => {});
1364
+ // Last chance to persist the circuit while the store is still ours.
1365
+ await this.streamProcessor.checkpoint('close').catch(() => {});
1366
+ // Leaving the broker first hands leadership to another tab (and releases
1367
+ // the OPFS handles via the worker shutdown) before the store closes.
1368
+ if (this.tabsCoordinator) await this.tabsCoordinator.stop();
1369
+ this.detachUnloadRelease?.();
1370
+ this.detachUnloadRelease = null;
1371
+ await this.local.close();
1372
+ await this.remote.close();
1373
+ // Free the wasm circuit explicitly. V8 cannot see wasm-internal bytes, so
1374
+ // relying on the wasm-bindgen FinalizationRegistry leaves the whole store
1375
+ // resident until a GC that may never come, and a client that is recreated
1376
+ // (provider remount, HMR) would stack circuits.
1377
+ this.streamProcessor.dispose();
1378
+ }
1379
+
1380
+ /**
1381
+ * Subscribe to a feature flag for the current user. Returns a
1382
+ * `FeatureFlagHandle` whose `variant()`, `payload()` and `enabled()`
1383
+ * accessors reflect the latest assignment from `_00_user_feature`,
1384
+ * and whose `subscribe(cb)` fires whenever that assignment changes.
1385
+ *
1386
+ * Permissions are enforced by SurrealDB: a client can only ever see
1387
+ * its own row, and cannot create or modify assignments.
1388
+ */
1389
+ feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle {
1390
+ return this.featureFlags.feature(key, options);
1391
+ }
1392
+
1393
+ /**
1394
+ * Force a feature flag to `variant` in THIS browser only; `null` clears it.
1395
+ *
1396
+ * Nothing is sent to the server — the `_00_user_feature` assignment is
1397
+ * untouched, so clearing restores whatever the server says. Persisted to
1398
+ * localStorage, survives reloads, and applies while signed out. Backs the
1399
+ * DevTools Access tab, and is a convenient hook for tests.
1400
+ *
1401
+ * To change a flag for OTHER users you need admin rights (`spky admin add`)
1402
+ * and the DevTools Access tab, or `spky flag`.
1403
+ */
1404
+ setFeatureOverride(key: string, variant: string | null, payload?: unknown): void {
1405
+ this.featureFlags.setLocalOverride(key, variant, payload);
1406
+ }
1407
+
1408
+ /** Drop every local feature flag override set via `setFeatureOverride`. */
1409
+ clearFeatureOverrides(): void {
1410
+ this.featureFlags.clearLocalOverrides();
1411
+ }
1412
+
1413
+ /** The local feature flag overrides currently in effect, keyed by flag. */
1414
+ getFeatureOverrides(): Record<string, FeatureFlagOverride> {
1415
+ return this.featureFlags.getLocalOverrides();
1416
+ }
1417
+
1418
+ /**
1419
+ * Observe the announced release of an app (`_00_app_release:<app>`, written
1420
+ * by `spky deploy` / `spky release`). The handle's `snapshot()` carries the
1421
+ * announced version plus the cache-bust/mandatory flags, and
1422
+ * `updateAvailable(currentVersion)` compares it semver-wise against the
1423
+ * running build. World-readable; writes are root-only.
1424
+ */
1425
+ appRelease(app: string, options?: AppReleaseOptions): AppReleaseHandle {
1426
+ return this.appReleases.release(app, options);
1427
+ }
1428
+
1429
+ authenticate(token: string) {
1430
+ // Record it before authenticating, so a socket the supervisor rebuilds
1431
+ // later comes back as this identity rather than anonymous. See
1432
+ // `RemoteDatabaseService.setAuthToken`.
1433
+ this.remote.setAuthToken(token);
1434
+ return this.remote.getClient().authenticate(token);
1435
+ }
1436
+
1437
+ /**
1438
+ * Open a CRDT field for collaborative editing.
1439
+ * Returns a CrdtField with a LoroDoc that can be bound to any editor.
1440
+ * Also starts a LIVE SELECT on the parent table for real-time sync;
1441
+ * incoming events trigger a subquery fetch of `_00_crdt` / `_00_cursor`.
1442
+ */
1443
+ async openCrdtField(
1444
+ table: string,
1445
+ recordId: string,
1446
+ field: string,
1447
+ fallbackText?: string
1448
+ ): Promise<CrdtField> {
1449
+ return this.crdtManager.open(table, recordId, field, fallbackText);
1450
+ }
1451
+
1452
+ /**
1453
+ * Close a CRDT field when editing is done.
1454
+ */
1455
+ closeCrdtField(table: string, recordId: string, field: string): void {
1456
+ this.crdtManager.close(table, recordId, field);
1457
+ }
1458
+
1459
+ deauthenticate() {
1460
+ return this.remote.getClient().invalidate();
1461
+ }
1462
+
1463
+ query<Table extends TableNames<S>>(
1464
+ table: Table,
1465
+ options: QueryOptions<TableModel<GetTable<S, Table>>, false>,
1466
+ ttl: QueryTimeToLive = '10m'
1467
+ ): QueryBuilder<S, Table, Sp00kyQueryResultPromise> {
1468
+ return new QueryBuilder<S, Table, Sp00kyQueryResultPromise>(
1469
+ this.config.schema,
1470
+ table,
1471
+ async (q) => ({
1472
+ hash: await this.initQuery(table, q, ttl),
1473
+ }),
1474
+ options
1475
+ );
1476
+ }
1477
+
1478
+ private async initQuery<Table extends TableNames<S>>(
1479
+ table: Table,
1480
+ q: InnerQuery<any, any, any>,
1481
+ ttl: QueryTimeToLive
1482
+ ) {
1483
+ const tableSchema = this.config.schema.tables.find((t) => t.name === table);
1484
+ if (!tableSchema) {
1485
+ throw new Error(`Table ${table} not found`);
1486
+ }
1487
+
1488
+ const params = parseQueryParams(tableSchema.columns, q.selectQuery.vars ?? {});
1489
+ const hash = await this.dataModule.query(
1490
+ table,
1491
+ q.selectQuery.query,
1492
+ params,
1493
+ ttl,
1494
+ q.selectQuery.plan
1495
+ );
1496
+
1497
+ // Local-first paint: the hash is returned as soon as the LOCAL registration
1498
+ // above completes — `queryState.records` is already seeded from the local
1499
+ // cache/SSP snapshot, so `useQuery` subscribes and paints from memory with
1500
+ // zero network on the paint path. The `register` down-event and the
1501
+ // instant-hydrate continue in a background chain: register is enqueued
1502
+ // first (the authoritative path never waits on a one-shot read), and a
1503
+ // hydrate that lands after the registration is dropped by
1504
+ // `applyHydration`. Concurrent mounts of the same query share one chain; a
1505
+ // sequential re-mount starts a fresh one so its `register` re-enqueue
1506
+ // keeps freshening warm data on use.
1507
+ if (!this.pendingQueryInits.has(hash)) {
1508
+ const chain = this.finishQueryInit(hash, q, params).finally(() => {
1509
+ this.pendingQueryInits.delete(hash);
1510
+ });
1511
+ this.pendingQueryInits.set(hash, chain);
1512
+ }
1513
+
1514
+ return hash;
1515
+ }
1516
+
1517
+ /**
1518
+ * Background tail of {@link initQuery}: enqueue the `register` down-event,
1519
+ * then instant-hydrate (opt-in via `config.instantHydrate`, and only when
1520
+ * the query is cold). Never rejects — both halves catch and log, so
1521
+ * `void`-ing the returned promise can't produce an unhandled rejection. By
1522
+ * default (hydrate off) the register lifecycle is the single freshness path;
1523
+ * the one-shot fetch is an optimization apps enable explicitly, and it runs
1524
+ * regardless of preload state — cache-first delivery never depends on WHY
1525
+ * rows are cached.
1526
+ *
1527
+ * Register goes first on purpose. The hydrate used to run strictly before
1528
+ * the enqueue, so a windowed list's one-shot read (a full-table scan when
1529
+ * the app has no matching index) held its own registration back by seconds
1530
+ * per window. Now whichever answer lands first paints; a hydrate that
1531
+ * arrives after the registration is dropped (`applyHydration`).
1532
+ */
1533
+ private async finishQueryInit(
1534
+ hash: string,
1535
+ q: InnerQuery<any, any, any>,
1536
+ params: Record<string, any>
1537
+ ): Promise<void> {
1538
+ try {
1539
+ await this.sync.enqueueDownEvent({
1540
+ type: 'register',
1541
+ payload: {
1542
+ hash,
1543
+ },
1544
+ });
1545
+ } catch (err) {
1546
+ this.logger.error(
1547
+ { err, hash, Category: 'sp00ky-client::Sp00kyClient::initQuery' },
1548
+ 'Failed to enqueue register down-event'
1549
+ );
1550
+ }
1551
+
1552
+ if (this.config.instantHydrate === true && this.dataModule.isCold(hash)) {
1553
+ try {
1554
+ // Fence against bucket switches: rows fetched under the previous
1555
+ // auth context must not hydrate the new bucket's query state — the
1556
+ // rebind's re-registration refills it from the right context.
1557
+ const epoch = this.local.epoch;
1558
+ const [rows] = await this.remote.query<[RecordWithId[]]>(q.selectQuery.query, params);
1559
+ if (epoch === this.local.epoch) {
1560
+ await this.dataModule.applyHydration(hash, rows ?? []);
1561
+ }
1562
+ } catch (err) {
1563
+ if (err instanceof StaleEpochError) {
1564
+ this.logger.debug(
1565
+ { hash, Category: 'sp00ky-client::Sp00kyClient::instantHydrate' },
1566
+ 'Dropped instant hydrate from before a bucket switch'
1567
+ );
1568
+ } else {
1569
+ this.logger.warn(
1570
+ { err, hash, Category: 'sp00ky-client::Sp00kyClient::instantHydrate' },
1571
+ 'Instant hydrate failed; proceeding with registration'
1572
+ );
1573
+ }
1574
+ }
1575
+ }
1576
+ }
1577
+
1578
+ /**
1579
+ * Smart, awaitable preload/prewarm into the LOCAL cache — without registering a
1580
+ * live view (NO `_00_query`, NO subscription, NO TTL heartbeat).
1581
+ *
1582
+ * Cache-aware via a durable per-bucket freshness marker (`_00_preload`):
1583
+ * - COLD (never preloaded in this bucket): fetch the query one-shot from the
1584
+ * remote, persist the rows (+ embedded `.related()` children), stamp the
1585
+ * marker — and AWAIT it. This is the "smart waiting" first load: callers can
1586
+ * `await db.preload(...)` to hold the UI until the data is ready.
1587
+ * - WARM (marker present): return instantly — NEVER blocks. `refresh` decides
1588
+ * whether to also kick a one-time silent refetch (see {@link PreloadOptions}).
1589
+ * Default `onUse` does nothing; the data freshens when the real `useQuery`
1590
+ * mounts and registers its live view.
1591
+ *
1592
+ * Best-effort: any fetch failure (offline, etc.) is a no-op warn (no marker
1593
+ * written, so it's retried next load). Deduped per session by query hash.
1594
+ */
1595
+ async preload(
1596
+ finalQuery: FinalQuery<S, any, any, any, any, any>,
1597
+ options?: PreloadOptions
1598
+ ): Promise<void> {
1599
+ const q = finalQuery.innerQuery;
1600
+ if (this.preloadedHashes.has(q.hash)) return;
1601
+
1602
+ const tableName = q.tableName;
1603
+ const tableSchema = this.config.schema.tables.find((t) => t.name === tableName);
1604
+ if (!tableSchema) {
1605
+ throw new Error(`Table ${tableName} not found`);
1606
+ }
1607
+ const params = parseQueryParams(tableSchema.columns, q.selectQuery.vars ?? {});
1608
+ const hashKey = String(q.hash);
1609
+
1610
+ const marker = await this.dataModule.getPreloadMarker(hashKey);
1611
+
1612
+ // COLD → fetch + persist + stamp, awaited so the caller can block on it.
1613
+ if (!marker) {
1614
+ const rowCount = await this.fetchAndPersist(q, tableName, params);
1615
+ if (rowCount >= 0) {
1616
+ await this.dataModule.writePreloadMarker(hashKey, rowCount);
1617
+ this.preloadedHashes.add(q.hash);
1618
+ }
1619
+ return;
1620
+ }
1621
+
1622
+ // WARM → never block. Mark handled for this session, then optionally refresh.
1623
+ this.preloadedHashes.add(q.hash);
1624
+ const refresh = options?.refresh ?? 'onUse';
1625
+ if (refresh === 'onUse') return;
1626
+
1627
+ if (refresh === 'stale') {
1628
+ const maxAgeMs = parseDuration(options?.staleTime ?? '1h');
1629
+ if (Date.now() - marker.fetchedAt <= maxAgeMs) return; // still fresh
1630
+ }
1631
+
1632
+ // `background`, or `stale` past its staleTime → one-time silent refetch.
1633
+ void this.fetchAndPersist(q, tableName, params).then((rowCount) => {
1634
+ if (rowCount >= 0) return this.dataModule.writePreloadMarker(hashKey, rowCount);
1635
+ });
1636
+ }
1637
+
1638
+ /**
1639
+ * One-shot remote fetch + local persist for a preload query. Returns the row
1640
+ * count on success, or -1 on failure (best-effort: logged, never thrown) so
1641
+ * the caller skips stamping the freshness marker and retries next load.
1642
+ */
1643
+ private async fetchAndPersist(
1644
+ q: InnerQuery<any, any, any>,
1645
+ tableName: string,
1646
+ params: Record<string, any>
1647
+ ): Promise<number> {
1648
+ try {
1649
+ const [rows] = await this.remote.query<[RecordWithId[]]>(q.selectQuery.query, params);
1650
+ const list = rows ?? [];
1651
+ await this.dataModule.persistSnapshot(tableName, list);
1652
+ return list.length;
1653
+ } catch (err) {
1654
+ this.logger.warn(
1655
+ { err, hash: q.hash, Category: 'sp00ky-client::Sp00kyClient::preload' },
1656
+ 'Preload fetch failed; data will be fetched on demand'
1657
+ );
1658
+ return -1;
1659
+ }
1660
+ }
1661
+
1662
+ async queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive) {
1663
+ const tableName = sql.split('FROM ')[1].split(' ')[0];
1664
+ return this.dataModule.query(tableName, sql, params, ttl);
1665
+ }
1666
+
1667
+ async subscribe(
1668
+ queryHash: string,
1669
+ callback: (records: Record<string, any>[]) => void,
1670
+ options?: { immediate?: boolean }
1671
+ ): Promise<() => void> {
1672
+ return this.dataModule.subscribe(queryHash, callback, options);
1673
+ }
1674
+
1675
+ /**
1676
+ * Opt-in eager teardown for a query whose last subscriber has gone away
1677
+ * (e.g. a viewport-windowed list cancelling an off-screen window). No-op
1678
+ * while any subscriber remains. Tears down the remote `_00_query` view +
1679
+ * local WASM view instead of waiting for the TTL sweep. Default behavior
1680
+ * (no call here) keeps the view resident for cheap re-subscription.
1681
+ */
1682
+ deregisterQuery(queryHash: string): void {
1683
+ this.dataModule.deregisterQuery(queryHash);
1684
+ }
1685
+
1686
+ /**
1687
+ * Subscribe to a query's fetch-status changes (idle/fetching). With
1688
+ * `{ immediate: true }` the callback fires synchronously with the current
1689
+ * status. Powers the `useQuery` hook's `isFetching()` accessor.
1690
+ */
1691
+ subscribeQueryStatus(
1692
+ queryHash: string,
1693
+ callback: QueryStatusCallback,
1694
+ options?: { immediate?: boolean }
1695
+ ): () => void {
1696
+ return this.dataModule.subscribeStatus(queryHash, callback, options);
1697
+ }
1698
+
1699
+ /**
1700
+ * Subscribe to a query's authority flips: `true` once server membership is
1701
+ * known for it (a registration or poll landed, or a durable `_00_window`
1702
+ * seed was read on boot), `false` when it is reset (bucket switch). With
1703
+ * `{ immediate: true }` the callback fires synchronously with the current
1704
+ * value. Powers the bindings' `isAuthoritative()` / cold-only `isLoading()`.
1705
+ */
1706
+ subscribeQueryAuthority(
1707
+ queryHash: string,
1708
+ callback: QueryAuthorityCallback,
1709
+ options?: { immediate?: boolean }
1710
+ ): () => void {
1711
+ return this.dataModule.subscribeAuthority(queryHash, callback, options);
1712
+ }
1713
+
1714
+ /**
1715
+ * Report the frontend processing time (ms) a client framework spent applying
1716
+ * an update for a query (e.g. `useQuery`'s `reconcile()`), so DevTools/MCP can
1717
+ * surface the "frontend" phase of the per-query timing breakdown.
1718
+ */
1719
+ reportFrontendTiming(queryHash: string, ms: number): void {
1720
+ this.dataModule.recordFrontendTiming(queryHash, ms);
1721
+ }
1722
+
1723
+ run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(
1724
+ backend: B,
1725
+ path: R,
1726
+ payload: RoutePayload<S, B, R>,
1727
+ options?: RunOptions
1728
+ ) {
1729
+ return this.dataModule.run(backend, path, payload, options);
1730
+ }
1731
+
1732
+ bucket<B extends BucketNames<S>>(name: B): BucketHandle {
1733
+ return new BucketHandle(name, this.remote, this.blobs, {
1734
+ blurhash: this.config.blurhash,
1735
+ logger: this.logger,
1736
+ });
1737
+ }
1738
+
1739
+ /** Cache-free handle. The blob cache reads the remote through this, so a
1740
+ * cache miss can't loop back into the cache. */
1741
+ private rawBucket(name: string): BucketHandle {
1742
+ return new BucketHandle(name, this.remote, null, {
1743
+ blurhash: this.config.blurhash,
1744
+ logger: this.logger,
1745
+ });
1746
+ }
1747
+
1748
+ /** Blob cache counters for DevTools. */
1749
+ getBlobCacheStats() {
1750
+ return this.blobs.stats();
1751
+ }
1752
+
1753
+ create(id: string, data: Record<string, unknown>) {
1754
+ return this.dataModule.create(id, data);
1755
+ }
1756
+
1757
+ update(table: string, id: string, data: Record<string, unknown>, options?: UpdateOptions) {
1758
+ return this.dataModule.update(table, id, data, options);
1759
+ }
1760
+
1761
+ delete(table: string, id: string) {
1762
+ return this.dataModule.delete(table, id);
1763
+ }
1764
+
1765
+ /**
1766
+ * Whether the local store is initialized and reads can be served. See the
1767
+ * `localReady` field: this is deliberately independent of connectivity.
1768
+ */
1769
+ isLocalReady(): boolean {
1770
+ return this.localReady;
1771
+ }
1772
+
1773
+ async useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T> {
1774
+ return fn(this.remote.getClient());
1775
+ }
1776
+
1777
+ /**
1778
+ * Run one raw SurrealQL statement on the remote through the client's own
1779
+ * query path: the connect gate (it never runs on a socket whose namespace
1780
+ * and token are not applied yet), the per-statement timeout and the
1781
+ * concurrency limit. Prefer this over {@link useRemote} for one-shot reads
1782
+ * such as counts; `useRemote` hands out the bare SDK client and raced a
1783
+ * reconnect with "Specify a namespace to use".
1784
+ */
1785
+ async remoteQuery<T extends unknown[]>(sql: string, vars?: Record<string, unknown>): Promise<T> {
1786
+ return this.remote.query<T>(sql, vars);
1787
+ }
1788
+
1789
+ /**
1790
+ * Mint the salt used for query-id hashing, so two sessions registering the
1791
+ * same logical query get distinct `_00_query` rows.
1792
+ *
1793
+ * Generated LOCALLY, deliberately. This used to be `RETURN <string>session::id()`,
1794
+ * which cost a serial round trip on the critical boot path and resolved to
1795
+ * `''` offline. The value never needed to come from the server: the server
1796
+ * derives its own `clientId` inside `fn::query::register` and *ignores*
1797
+ * whatever the caller passed, and the permission rules that matter gate on
1798
+ * `auth_id = $auth.id` rather than the session (`_00_list_ref`). Session
1799
+ * scoping via `clientId = session::id()` was in fact removed upstream because
1800
+ * it broke a user with two tabs open. All this value has to be is unique per
1801
+ * browser session, which `randomUUID` gives us for free and offline.
1802
+ */
1803
+ /**
1804
+ * The current principal as the `"table:id"` string the in-browser SSP wants
1805
+ * for `$auth.id`, or null when signed out.
1806
+ *
1807
+ * Tolerates BOTH shapes `currentUser.id` can take, which is the point:
1808
+ * a session restored from the cached token carries a plain string (the JWT's
1809
+ * `ID` claim), while one verified by the server carries a RecordId. Passing
1810
+ * the former to `encodeRecordId` reads `.table` off a string and throws
1811
+ * during boot.
1812
+ */
1813
+ /**
1814
+ * Prime the in-browser circuit from the local store. Builds the context the
1815
+ * stream processor needs: every synced table (the app schema plus the
1816
+ * server-written meta tables that sync down), a schema hash so a snapshot
1817
+ * projected under another schema is not trusted, and the ids whose local
1818
+ * `_00_rv` was bumped by an unsettled mutation.
1819
+ */
1820
+ private async primeCircuit(): Promise<void> {
1821
+ const tables = [
1822
+ ...this.config.schema.tables.map((t) => t.name),
1823
+ '_00_user_feature',
1824
+ '_00_app_release',
1825
+ ];
1826
+ let pendingIds = new Set<string>();
1827
+ try {
1828
+ const pending = await this.dataModule.getPendingRecordIds();
1829
+ pendingIds = new Set([...pending.writes, ...pending.deletes]);
1830
+ } catch {
1831
+ /* no outbox yet: nothing is pending */
1832
+ }
1833
+ await this.streamProcessor.primeFromLocal({
1834
+ tables,
1835
+ schemaHash: String(hash53(this.config.schemaSurql)),
1836
+ pendingIds,
1837
+ onVersions: (_table, entries) => this.cache.primeVersions(entries),
1838
+ });
1839
+ }
1840
+
1841
+ private sessionAuthId(): string | null {
1842
+ const id = this.auth.currentUser?.id;
1843
+ if (!id) return null;
1844
+ return typeof id === 'string' ? id : encodeRecordId(id);
1845
+ }
1846
+
1847
+ private mintSessionSalt(): string {
1848
+ const c: Crypto | undefined =
1849
+ typeof globalThis !== 'undefined' ? (globalThis as { crypto?: Crypto }).crypto : undefined;
1850
+ if (c?.randomUUID) return c.randomUUID();
1851
+ // Older browsers / non-secure contexts: uniqueness is all that is required.
1852
+ return `s${Date.now().toString(36)}${Math.random().toString(36).slice(2, 12)}`;
1853
+ }
1854
+ }