@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.
- package/AGENTS.md +58 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +476 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/boot/auth-flip.saga.ts.html +271 -0
- package/coverage/lcov-report/src/boot/boot.saga.ts.html +469 -0
- package/coverage/lcov-report/src/boot/bucket-switch.saga.ts.html +430 -0
- package/coverage/lcov-report/src/boot/index.html +161 -0
- package/coverage/lcov-report/src/boot/preload.saga.ts.html +178 -0
- package/coverage/lcov-report/src/client/bucket-handle.ts.html +766 -0
- package/coverage/lcov-report/src/client/index.html +161 -0
- package/coverage/lcov-report/src/client/router.ts.html +367 -0
- package/coverage/lcov-report/src/client/runtime.ts.html +940 -0
- package/coverage/lcov-report/src/client/sp00ky-client.ts.html +1531 -0
- package/coverage/lcov-report/src/events/index.html +116 -0
- package/coverage/lcov-report/src/events/index.ts.html +877 -0
- package/coverage/lcov-report/src/index.html +146 -0
- package/coverage/lcov-report/src/index.ts.html +202 -0
- package/coverage/lcov-report/src/kernel/constants.ts.html +247 -0
- package/coverage/lcov-report/src/kernel/effects.ts.html +562 -0
- package/coverage/lcov-report/src/kernel/events.ts.html +250 -0
- package/coverage/lcov-report/src/kernel/index.html +176 -0
- package/coverage/lcov-report/src/kernel/interpreter.ts.html +514 -0
- package/coverage/lcov-report/src/kernel/saga.ts.html +304 -0
- package/coverage/lcov-report/src/modules/app-release/index.html +116 -0
- package/coverage/lcov-report/src/modules/app-release/index.ts.html +712 -0
- package/coverage/lcov-report/src/modules/auth/events/index.html +116 -0
- package/coverage/lcov-report/src/modules/auth/events/index.ts.html +142 -0
- package/coverage/lcov-report/src/modules/auth/index.html +116 -0
- package/coverage/lcov-report/src/modules/auth/index.ts.html +1225 -0
- package/coverage/lcov-report/src/modules/crdt/crdt-field.ts.html +967 -0
- package/coverage/lcov-report/src/modules/crdt/index.html +146 -0
- package/coverage/lcov-report/src/modules/crdt/index.ts.html +1474 -0
- package/coverage/lcov-report/src/modules/crdt/loro-loader.ts.html +160 -0
- package/coverage/lcov-report/src/modules/devtools/flags.ts.html +1132 -0
- package/coverage/lcov-report/src/modules/devtools/index.html +161 -0
- package/coverage/lcov-report/src/modules/devtools/index.ts.html +2413 -0
- package/coverage/lcov-report/src/modules/devtools/storage-info.ts.html +589 -0
- package/coverage/lcov-report/src/modules/devtools/versions.ts.html +415 -0
- package/coverage/lcov-report/src/modules/feature-flag/index.html +116 -0
- package/coverage/lcov-report/src/modules/feature-flag/index.ts.html +1033 -0
- package/coverage/lcov-report/src/modules/index.html +116 -0
- package/coverage/lcov-report/src/modules/ref-tables.ts.html +349 -0
- package/coverage/lcov-report/src/mutation/index.html +191 -0
- package/coverage/lcov-report/src/mutation/jobs.ts.html +208 -0
- package/coverage/lcov-report/src/mutation/mutation-id.ts.html +190 -0
- package/coverage/lcov-report/src/mutation/push.saga.ts.html +730 -0
- package/coverage/lcov-report/src/mutation/rows.ts.html +1216 -0
- package/coverage/lcov-report/src/mutation/tray.saga.ts.html +229 -0
- package/coverage/lcov-report/src/mutation/write.saga.ts.html +583 -0
- package/coverage/lcov-report/src/otel/index.html +116 -0
- package/coverage/lcov-report/src/otel/index.ts.html +466 -0
- package/coverage/lcov-report/src/query/env.ts.html +253 -0
- package/coverage/lcov-report/src/query/fetch.saga.ts.html +412 -0
- package/coverage/lcov-report/src/query/hash.ts.html +139 -0
- package/coverage/lcov-report/src/query/index.html +281 -0
- package/coverage/lcov-report/src/query/lifecycle.saga.ts.html +388 -0
- package/coverage/lcov-report/src/query/materialize.saga.ts.html +310 -0
- package/coverage/lcov-report/src/query/materialize.ts.html +217 -0
- package/coverage/lcov-report/src/query/membership.saga.ts.html +709 -0
- package/coverage/lcov-report/src/query/membership.ts.html +559 -0
- package/coverage/lcov-report/src/query/register.saga.ts.html +655 -0
- package/coverage/lcov-report/src/query/render-set.ts.html +265 -0
- package/coverage/lcov-report/src/query/sql.ts.html +322 -0
- package/coverage/lcov-report/src/query/window-query.ts.html +670 -0
- package/coverage/lcov-report/src/services/blobs/blob-cache.ts.html +1894 -0
- package/coverage/lcov-report/src/services/blobs/blob-manifest.ts.html +766 -0
- package/coverage/lcov-report/src/services/blobs/blob-store.ts.html +1162 -0
- package/coverage/lcov-report/src/services/blobs/index.html +161 -0
- package/coverage/lcov-report/src/services/blobs/index.ts.html +295 -0
- package/coverage/lcov-report/src/services/database/cache-engine.ts.html +664 -0
- package/coverage/lcov-report/src/services/database/connection-supervisor.ts.html +1330 -0
- package/coverage/lcov-report/src/services/database/database.ts.html +658 -0
- package/coverage/lcov-report/src/services/database/engine-factory.ts.html +184 -0
- package/coverage/lcov-report/src/services/database/errors.ts.html +187 -0
- package/coverage/lcov-report/src/services/database/events/index.html +116 -0
- package/coverage/lcov-report/src/services/database/events/index.ts.html +184 -0
- package/coverage/lcov-report/src/services/database/index.html +386 -0
- package/coverage/lcov-report/src/services/database/index.ts.html +121 -0
- package/coverage/lcov-report/src/services/database/local-migrator.ts.html +703 -0
- package/coverage/lcov-report/src/services/database/local.ts.html +1633 -0
- package/coverage/lcov-report/src/services/database/plan-render.ts.html +409 -0
- package/coverage/lcov-report/src/services/database/relation-resolver.ts.html +682 -0
- package/coverage/lcov-report/src/services/database/remote.ts.html +1036 -0
- package/coverage/lcov-report/src/services/database/sqlite-cache-engine.ts.html +4132 -0
- package/coverage/lcov-report/src/services/database/sqlite-lock-verify.ts.html +220 -0
- package/coverage/lcov-report/src/services/database/sqlite-open.ts.html +577 -0
- package/coverage/lcov-report/src/services/database/sqlite-plan-sql.ts.html +499 -0
- package/coverage/lcov-report/src/services/database/sqlite-select.ts.html +478 -0
- package/coverage/lcov-report/src/services/database/sqlite-transport.ts.html +757 -0
- package/coverage/lcov-report/src/services/database/surql-translate.ts.html +1333 -0
- package/coverage/lcov-report/src/services/database/surreal-cache-engine.ts.html +568 -0
- package/coverage/lcov-report/src/services/logger/index.html +116 -0
- package/coverage/lcov-report/src/services/logger/index.ts.html +154 -0
- package/coverage/lcov-report/src/services/persistence/index.html +146 -0
- package/coverage/lcov-report/src/services/persistence/localstorage.ts.html +163 -0
- package/coverage/lcov-report/src/services/persistence/resilient.ts.html +208 -0
- package/coverage/lcov-report/src/services/persistence/surrealdb.ts.html +271 -0
- package/coverage/lcov-report/src/services/stream-processor/index.html +146 -0
- package/coverage/lcov-report/src/services/stream-processor/index.ts.html +3313 -0
- package/coverage/lcov-report/src/services/stream-processor/permissions.ts.html +244 -0
- package/coverage/lcov-report/src/services/stream-processor/wasm-types.ts.html +346 -0
- package/coverage/lcov-report/src/services/tabs/broker-client.ts.html +934 -0
- package/coverage/lcov-report/src/services/tabs/coordinator.ts.html +1984 -0
- package/coverage/lcov-report/src/services/tabs/index.html +191 -0
- package/coverage/lcov-report/src/services/tabs/leader-locks.ts.html +310 -0
- package/coverage/lcov-report/src/services/tabs/protocol.ts.html +871 -0
- package/coverage/lcov-report/src/services/tabs/support.ts.html +193 -0
- package/coverage/lcov-report/src/services/tabs/tabs-broker-worker.ts.html +2005 -0
- package/coverage/lcov-report/src/sp00ky.ts.html +100 -0
- package/coverage/lcov-report/src/state/client-state.ts.html +637 -0
- package/coverage/lcov-report/src/state/index.html +161 -0
- package/coverage/lcov-report/src/state/lifecycle.ts.html +343 -0
- package/coverage/lcov-report/src/state/reducers.ts.html +1471 -0
- package/coverage/lcov-report/src/state/selectors.ts.html +637 -0
- package/coverage/lcov-report/src/sync/connection.saga.ts.html +343 -0
- package/coverage/lcov-report/src/sync/index.html +176 -0
- package/coverage/lcov-report/src/sync/live.saga.ts.html +232 -0
- package/coverage/lcov-report/src/sync/policy.ts.html +1606 -0
- package/coverage/lcov-report/src/sync/poll.saga.ts.html +193 -0
- package/coverage/lcov-report/src/sync/tabs.saga.ts.html +361 -0
- package/coverage/lcov-report/src/types.ts.html +2419 -0
- package/coverage/lcov-report/src/utils/blurhash.ts.html +355 -0
- package/coverage/lcov-report/src/utils/error-classification.ts.html +244 -0
- package/coverage/lcov-report/src/utils/index.html +191 -0
- package/coverage/lcov-report/src/utils/index.ts.html +820 -0
- package/coverage/lcov-report/src/utils/parser.ts.html +313 -0
- package/coverage/lcov-report/src/utils/semver.ts.html +175 -0
- package/coverage/lcov-report/src/utils/surql.ts.html +667 -0
- package/coverage/lcov.info +19079 -0
- package/dist/index.d.ts +2946 -140
- package/dist/index.js +13424 -2356
- package/dist/otel/index.d.ts +2 -2
- package/dist/otel/index.js +6 -6
- package/dist/sqlite-open.js +322 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +472 -0
- package/dist/types.d.ts +769 -11
- package/package.json +11 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/skills/{spooky-core → sp00ky-core}/SKILL.md +12 -12
- package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
- package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +36 -2
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +201 -0
- package/src/modules/auth/auth.local-first.test.ts +141 -0
- package/src/modules/auth/events/index.ts +2 -1
- package/src/modules/auth/index.ts +138 -25
- package/src/modules/cache/cache.relay.test.ts +95 -0
- package/src/modules/cache/index.ts +163 -43
- package/src/modules/cache/types.ts +2 -2
- package/src/modules/crdt/crdt-field.ts +294 -0
- package/src/modules/crdt/crdt-hydration.test.ts +210 -0
- package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
- package/src/modules/crdt/index.ts +463 -0
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/data/data.activity.test.ts +109 -0
- package/src/modules/data/data.authority.test.ts +122 -0
- package/src/modules/data/data.heartbeat-wake.test.ts +115 -0
- package/src/modules/data/data.hydration.test.ts +142 -0
- package/src/modules/data/data.membership.test.ts +623 -0
- package/src/modules/data/data.notify-table.test.ts +41 -0
- package/src/modules/data/data.pending-ids.test.ts +199 -0
- package/src/modules/data/data.rebind.test.ts +170 -0
- package/src/modules/data/data.rematerialize.test.ts +114 -0
- package/src/modules/data/data.run.test.ts +113 -0
- package/src/modules/data/data.settled-writes.test.ts +235 -0
- package/src/modules/data/data.status.test.ts +249 -0
- package/src/modules/data/id-set-plan.test.ts +122 -0
- package/src/modules/data/index.ts +2031 -150
- package/src/modules/data/mutation-id.test.ts +25 -0
- package/src/modules/data/mutation-id.ts +35 -0
- package/src/modules/data/window-query.test.ts +52 -0
- package/src/modules/data/window-query.ts +194 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +451 -46
- package/src/modules/devtools/notify-throttle.test.ts +154 -0
- package/src/modules/devtools/state-shape.test.ts +146 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +251 -0
- package/src/modules/feature-flag/index.ts +308 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -0
- package/src/modules/sync/engine.ts +170 -86
- package/src/modules/sync/events/index.ts +9 -2
- package/src/modules/sync/queue/queue-down.test.ts +228 -0
- package/src/modules/sync/queue/queue-down.ts +108 -13
- package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
- package/src/modules/sync/queue/queue-up.ts +241 -57
- package/src/modules/sync/scheduler.pause.test.ts +109 -0
- package/src/modules/sync/scheduler.retry.test.ts +237 -0
- package/src/modules/sync/scheduler.ts +215 -13
- package/src/modules/sync/sync.cleanup.test.ts +116 -0
- package/src/modules/sync/sync.health.test.ts +167 -0
- package/src/modules/sync/sync.heartbeat.test.ts +80 -0
- package/src/modules/sync/sync.list-ref-meta.test.ts +193 -0
- package/src/modules/sync/sync.live-removal.test.ts +289 -0
- package/src/modules/sync/sync.reconnect.test.ts +145 -0
- package/src/modules/sync/sync.rematerialize-after-fetch.test.ts +93 -0
- package/src/modules/sync/sync.settled-poll.test.ts +58 -0
- package/src/modules/sync/sync.subquery.test.ts +82 -0
- package/src/modules/sync/sync.tabs.test.ts +249 -0
- package/src/modules/sync/sync.ts +2350 -133
- package/src/modules/sync/utils.test.ts +322 -2
- package/src/modules/sync/utils.ts +286 -16
- package/src/otel/index.ts +13 -10
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +193 -0
- package/src/services/database/connection-supervisor.test.ts +289 -0
- package/src/services/database/connection-supervisor.ts +415 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.ts +130 -62
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +7 -0
- package/src/services/database/local-migrator.ts +30 -27
- package/src/services/database/local.test.ts +64 -0
- package/src/services/database/local.ts +484 -67
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +467 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +222 -15
- package/src/services/database/sqlite-cache-engine.test.ts +616 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1358 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +104 -0
- package/src/services/database/sqlite-plan-sql.ts +138 -0
- package/src/services/database/sqlite-projection.test.ts +99 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +246 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +416 -0
- package/src/services/database/surreal-cache-engine.ts +161 -0
- package/src/services/logger/index.ts +3 -2
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +11 -4
- package/src/services/persistence/surrealdb.ts +10 -10
- package/src/services/stream-processor/index.ts +796 -84
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
- package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +59 -3
- package/src/services/tabs/broker-client.ts +283 -0
- package/src/services/tabs/broker.test.ts +327 -0
- package/src/services/tabs/coordinator.test.ts +365 -0
- package/src/services/tabs/coordinator.ts +633 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +258 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +640 -0
- package/src/sp00ky.auth-order.test.ts +112 -0
- package/src/sp00ky.init-query.test.ts +183 -0
- package/src/sp00ky.local-first.test.ts +60 -0
- package/src/sp00ky.ts +1854 -0
- package/src/types.ts +548 -13
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/error-classification.test.ts +54 -0
- package/src/utils/error-classification.ts +25 -0
- package/src/utils/index.ts +79 -13
- package/src/utils/parser.test.ts +49 -120
- package/src/utils/parser.ts +32 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/surql.ts +30 -18
- package/src/utils/withRetry.test.ts +1 -1
- package/tsdown.config.ts +86 -1
- package/src/spooky.ts +0 -395
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { CacheModule } from './index';
|
|
4
|
+
|
|
5
|
+
// The ingest relay is how one tab's circuit learns what another tab wrote.
|
|
6
|
+
// The leader relays every ingest (its sync fetches are the only copy the
|
|
7
|
+
// followers get); a follower relays with `localWritesOnly`, just its mutation
|
|
8
|
+
// path, because its sync-fetched batches are the leader's data coming back.
|
|
9
|
+
|
|
10
|
+
function makeLogger(): any {
|
|
11
|
+
const logger: any = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, trace: () => {} };
|
|
12
|
+
logger.child = () => logger;
|
|
13
|
+
return logger;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function setup() {
|
|
17
|
+
const local: any = { epoch: 1, execute: vi.fn(async () => {}), query: vi.fn(async () => []) };
|
|
18
|
+
const ssp: any = {
|
|
19
|
+
addReceiver: vi.fn(),
|
|
20
|
+
ingestMany: vi.fn((records: unknown[]) => records),
|
|
21
|
+
};
|
|
22
|
+
const cache = new CacheModule(local, ssp, () => {}, makeLogger());
|
|
23
|
+
const relay = vi.fn();
|
|
24
|
+
return { cache, local, ssp, relay };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const rec = (id: string, version: number) => ({
|
|
28
|
+
table: 'thread',
|
|
29
|
+
op: 'CREATE' as const,
|
|
30
|
+
record: { id: new RecordId('thread', id), title: 't' },
|
|
31
|
+
version,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('CacheModule ingest relay', () => {
|
|
35
|
+
it('applyRelayedIngest updates the version memo and never re-relays', () => {
|
|
36
|
+
const { cache, ssp, relay } = setup();
|
|
37
|
+
cache.setIngestRelay(relay);
|
|
38
|
+
|
|
39
|
+
cache.applyRelayedIngest([
|
|
40
|
+
{ table: 'thread', op: 'CREATE', id: 'thread:a', record: { id: 'thread:a', _00_rv: 4 } },
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
expect(ssp.ingestMany).toHaveBeenCalledTimes(1);
|
|
44
|
+
expect(cache.lookup('thread:a')).toBe(4);
|
|
45
|
+
expect(relay).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('localWritesOnly relays the mutation path (save + delete)', async () => {
|
|
49
|
+
const { cache, local, relay } = setup();
|
|
50
|
+
cache.setIngestRelay(relay, { localWritesOnly: true });
|
|
51
|
+
|
|
52
|
+
await cache.save(rec('a', 1), true);
|
|
53
|
+
expect(local.execute).not.toHaveBeenCalled();
|
|
54
|
+
expect(relay).toHaveBeenCalledTimes(1);
|
|
55
|
+
expect(relay.mock.calls[0][0][0]).toMatchObject({ table: 'thread', op: 'CREATE', id: 'thread:a' });
|
|
56
|
+
|
|
57
|
+
const before = { id: 'thread:a', title: 't' };
|
|
58
|
+
await cache.delete('thread', 'thread:a', true, before);
|
|
59
|
+
expect(relay).toHaveBeenCalledTimes(2);
|
|
60
|
+
expect(relay.mock.calls[1][0]).toEqual([
|
|
61
|
+
{ table: 'thread', op: 'DELETE', id: 'thread:a', record: before },
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('localWritesOnly stays silent for sync-fetched batches', async () => {
|
|
66
|
+
const { cache, local, relay } = setup();
|
|
67
|
+
cache.setIngestRelay(relay, { localWritesOnly: true });
|
|
68
|
+
|
|
69
|
+
await cache.saveBatch([rec('a', 1), rec('b', 1)], false);
|
|
70
|
+
expect(local.execute).toHaveBeenCalledTimes(1);
|
|
71
|
+
expect(relay).not.toHaveBeenCalled();
|
|
72
|
+
|
|
73
|
+
await cache.delete('thread', 'thread:a', false, {});
|
|
74
|
+
expect(relay).not.toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('an unscoped relay fires for every ingest', async () => {
|
|
78
|
+
const { cache, relay } = setup();
|
|
79
|
+
cache.setIngestRelay(relay);
|
|
80
|
+
|
|
81
|
+
await cache.saveBatch([rec('a', 1)], false);
|
|
82
|
+
await cache.save(rec('b', 1), true);
|
|
83
|
+
await cache.delete('thread', 'thread:a', false, {});
|
|
84
|
+
|
|
85
|
+
expect(relay).toHaveBeenCalledTimes(3);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('setIngestRelay(null) stops relaying', async () => {
|
|
89
|
+
const { cache, relay } = setup();
|
|
90
|
+
cache.setIngestRelay(relay, { localWritesOnly: true });
|
|
91
|
+
cache.setIngestRelay(null);
|
|
92
|
+
await cache.save(rec('a', 1), true);
|
|
93
|
+
expect(relay).not.toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { LocalStore } from '../../services/database/index';
|
|
2
|
+
import { StaleEpochError } from '../../services/database/index';
|
|
3
|
+
import type {
|
|
3
4
|
StreamProcessorService,
|
|
4
5
|
StreamUpdate,
|
|
5
6
|
StreamUpdateReceiver,
|
|
6
7
|
} from '../../services/stream-processor/index';
|
|
7
|
-
import { Logger } from '../../services/logger/index';
|
|
8
|
+
import type { Logger } from '../../services/logger/index';
|
|
8
9
|
import { parseRecordIdString, encodeRecordId, surql } from '../../utils/index';
|
|
9
|
-
import { CacheRecord, QueryConfig } from './types';
|
|
10
|
-
import { RecordVersionArray } from '../../types';
|
|
10
|
+
import type { CacheRecord, QueryConfig } from './types';
|
|
11
|
+
import type { RecordVersionArray } from '../../types';
|
|
11
12
|
|
|
12
13
|
export * from './types';
|
|
13
14
|
|
|
@@ -17,13 +18,29 @@ export * from './types';
|
|
|
17
18
|
* Single responsibility: Handle all local storage operations and DBSP ingestion.
|
|
18
19
|
* This module acts as the bridge between data operations and persistence.
|
|
19
20
|
*/
|
|
21
|
+
/** One ingested change, in exactly the shape `ingestMany` consumes. Shared
|
|
22
|
+
* with the tabs protocol so a leader can relay its ingests to followers. */
|
|
23
|
+
export interface CacheIngestTuple {
|
|
24
|
+
table: string;
|
|
25
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
26
|
+
id: string;
|
|
27
|
+
record: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
20
30
|
export class CacheModule implements StreamUpdateReceiver {
|
|
21
31
|
private logger: Logger;
|
|
22
32
|
private streamUpdateCallback: (update: StreamUpdate) => void;
|
|
23
33
|
private versionLookups: Record<string, number> = {};
|
|
34
|
+
/** Shared-tabs leader: fan every committed ingest out to follower circuits.
|
|
35
|
+
* Fired AFTER the local tx (the rows are already in the shared store, so a
|
|
36
|
+
* follower only needs the circuit feed). A follower relays its own
|
|
37
|
+
* mutations to the leader the same way, see {@link setIngestRelay}. */
|
|
38
|
+
private ingestRelay: ((tuples: CacheIngestTuple[]) => void) | null = null;
|
|
39
|
+
/** See {@link setIngestRelay}. */
|
|
40
|
+
private relayLocalWritesOnly = false;
|
|
24
41
|
|
|
25
42
|
constructor(
|
|
26
|
-
private local:
|
|
43
|
+
private local: LocalStore,
|
|
27
44
|
private streamProcessor: StreamProcessorService,
|
|
28
45
|
streamUpdateCallback: (update: StreamUpdate) => void,
|
|
29
46
|
logger: Logger
|
|
@@ -43,17 +60,69 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
43
60
|
{
|
|
44
61
|
queryHash: update.queryHash,
|
|
45
62
|
arrayLength: update.localArray?.length,
|
|
46
|
-
Category: '
|
|
63
|
+
Category: 'sp00ky-client::CacheModule::onStreamUpdate',
|
|
47
64
|
},
|
|
48
65
|
'Stream update received'
|
|
49
66
|
);
|
|
50
67
|
this.streamUpdateCallback(update);
|
|
51
68
|
}
|
|
52
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Fan every committed ingest out to the other tabs. The leader relays
|
|
72
|
+
* everything (its sync fetches are the only copy the followers get). A
|
|
73
|
+
* follower relays with `localWritesOnly`: just the mutation path, which is
|
|
74
|
+
* the only thing it knows that the leader does not. Its sync-fetched
|
|
75
|
+
* batches are the leader's data coming back and must not be re-broadcast,
|
|
76
|
+
* or every follower registration would fan its whole working set to
|
|
77
|
+
* every tab.
|
|
78
|
+
*/
|
|
79
|
+
setIngestRelay(
|
|
80
|
+
cb: ((tuples: CacheIngestTuple[]) => void) | null,
|
|
81
|
+
opts: { localWritesOnly?: boolean } = {}
|
|
82
|
+
): void {
|
|
83
|
+
this.ingestRelay = cb;
|
|
84
|
+
this.relayLocalWritesOnly = opts.localWritesOnly === true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Shared-tabs follower: feed relayed tuples into THIS tab's circuit only.
|
|
89
|
+
* The rows are already in the shared store (the leader wrote them), so no
|
|
90
|
+
* local write happens here; the normal chain then runs: SSP -> stream update
|
|
91
|
+
* -> DataModule debounce -> materializeRecords (re-reads via the port
|
|
92
|
+
* transport) -> this tab's subscriptions fire with this tab's hashes.
|
|
93
|
+
*/
|
|
94
|
+
applyRelayedIngest(tuples: CacheIngestTuple[]): void {
|
|
95
|
+
for (const t of tuples) {
|
|
96
|
+
const rv = (t.record as { _00_rv?: number } | undefined)?._00_rv;
|
|
97
|
+
if (t.op === 'DELETE') delete this.versionLookups[t.id];
|
|
98
|
+
else if (typeof rv === 'number') this.versionLookups[t.id] = rv;
|
|
99
|
+
}
|
|
100
|
+
this.streamProcessor.ingestMany(tuples as Parameters<StreamProcessorService['ingestMany']>[0]);
|
|
101
|
+
}
|
|
102
|
+
|
|
53
103
|
public lookup(recordId: string): number {
|
|
54
104
|
return this.versionLookups[recordId] ?? 0;
|
|
55
105
|
}
|
|
56
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Seed the version memo from rows the circuit was primed with out of the
|
|
109
|
+
* local store, so the first post-reload sync diff does not re-download
|
|
110
|
+
* bodies the browser already has. Only rows the prime actually put into the
|
|
111
|
+
* circuit belong here: a memo entry with no circuit row would make the diff
|
|
112
|
+
* flag the id forever while nothing ever fetches it.
|
|
113
|
+
*/
|
|
114
|
+
public primeVersions(entries: [string, number][]): void {
|
|
115
|
+
for (const [id, rv] of entries) {
|
|
116
|
+
if (rv > (this.versionLookups[id] ?? 0)) this.versionLookups[id] = rv;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Drop the version cache on a bucket switch — a stale version would make
|
|
121
|
+
* the sync diff skip fetching a body the new bucket legitimately needs. */
|
|
122
|
+
public clearVersionLookups(): void {
|
|
123
|
+
this.versionLookups = {};
|
|
124
|
+
}
|
|
125
|
+
|
|
57
126
|
/**
|
|
58
127
|
* Save a single record to local DB and ingest into DBSP
|
|
59
128
|
* Used by mutations (create/update)
|
|
@@ -70,10 +139,17 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
70
139
|
async saveBatch(records: CacheRecord[], skipDbInsert: boolean = false): Promise<void> {
|
|
71
140
|
if (records.length === 0) return;
|
|
72
141
|
|
|
142
|
+
// Fence against bucket switches: this batch's records were derived from
|
|
143
|
+
// reads against the CURRENT store/user. If a switch lands while we await
|
|
144
|
+
// the (gated) local write, the write throws StaleEpochError and the whole
|
|
145
|
+
// batch — including the SSP ingest — is dropped: the new bucket re-syncs
|
|
146
|
+
// its own data from the server.
|
|
147
|
+
const epoch = this.local.epoch;
|
|
148
|
+
|
|
73
149
|
this.logger.debug(
|
|
74
150
|
{
|
|
75
151
|
count: records.length,
|
|
76
|
-
Category: '
|
|
152
|
+
Category: 'sp00ky-client::CacheModule::saveBatch',
|
|
77
153
|
},
|
|
78
154
|
'Saving record batch'
|
|
79
155
|
);
|
|
@@ -85,7 +161,7 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
85
161
|
...record,
|
|
86
162
|
record: {
|
|
87
163
|
...record.record,
|
|
88
|
-
|
|
164
|
+
_00_rv: record.version,
|
|
89
165
|
},
|
|
90
166
|
};
|
|
91
167
|
});
|
|
@@ -94,40 +170,69 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
94
170
|
const query = surql.seal<void>(
|
|
95
171
|
surql.tx(
|
|
96
172
|
populatedRecords.map((_, i) => {
|
|
97
|
-
|
|
173
|
+
// MERGE, not REPLACE: the remote payload omits local-only
|
|
174
|
+
// fields (`_00_crdt`, `_00_cursor`) injected by the CLI's
|
|
175
|
+
// local schema, so REPLACE would wipe the persisted CRDT
|
|
176
|
+
// snapshot on every sync-down round-trip and break offline
|
|
177
|
+
// reload of formatted text.
|
|
178
|
+
return surql.upsertMerge(`id${i}`, `content${i}`);
|
|
98
179
|
})
|
|
99
180
|
)
|
|
100
181
|
);
|
|
101
182
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
{}
|
|
112
|
-
|
|
183
|
+
// Filled in place, NOT with a spread-per-iteration reduce: spreading the
|
|
184
|
+
// accumulator copies every key written so far on each record, which is
|
|
185
|
+
// O(n^2) in the batch size. On a cold start that batches thousands of
|
|
186
|
+
// rows (a game library, a player-name registry) it was the single
|
|
187
|
+
// biggest main-thread cost of the whole boot - ~36% of samples, seconds
|
|
188
|
+
// of blocking - for a loop that does no real work.
|
|
189
|
+
const params: Record<string, any> = {};
|
|
190
|
+
for (let i = 0; i < populatedRecords.length; i++) {
|
|
191
|
+
const { id, ...content } = populatedRecords[i].record;
|
|
192
|
+
params[`id${i}`] = id;
|
|
193
|
+
params[`content${i}`] = content;
|
|
194
|
+
}
|
|
113
195
|
|
|
114
|
-
await this.local.execute(query, params);
|
|
196
|
+
await this.local.execute(query, params, { epoch });
|
|
115
197
|
}
|
|
116
198
|
|
|
117
|
-
//
|
|
118
|
-
|
|
199
|
+
// Late fence for the skipDbInsert path (no gated write above to trip on).
|
|
200
|
+
if (this.local.epoch !== epoch) throw new StaleEpochError();
|
|
201
|
+
|
|
202
|
+
// 2. Bulk ingest into DBSP (use populatedRecords which has _00_rv set).
|
|
203
|
+
// ingestMany coalesces the per-record stream updates into a single
|
|
204
|
+
// notification per affected query — the UI then updates once, after the
|
|
205
|
+
// whole batch is ingested, instead of row-by-row.
|
|
206
|
+
const versionOf = new Map<string, number>();
|
|
207
|
+
const bulk = populatedRecords.map((record) => {
|
|
119
208
|
const recordId = encodeRecordId(record.record.id);
|
|
120
|
-
|
|
121
|
-
|
|
209
|
+
versionOf.set(recordId, record.version);
|
|
210
|
+
return { table: record.table, op: record.op, id: recordId, record: record.record };
|
|
211
|
+
});
|
|
212
|
+
// Memo and relay only what the circuit actually took: a chunk that
|
|
213
|
+
// failed mid-batch is neither "known" here nor fanned out to followers.
|
|
214
|
+
const ingested = this.streamProcessor.ingestMany(bulk);
|
|
215
|
+
for (const t of ingested) this.versionLookups[t.id] = versionOf.get(t.id) ?? 0;
|
|
216
|
+
// `skipDbInsert` is exactly the mutation path (the tx already wrote the
|
|
217
|
+
// row); sync-fetched batches pass false.
|
|
218
|
+
if (ingested.length > 0 && (!this.relayLocalWritesOnly || skipDbInsert)) {
|
|
219
|
+
this.ingestRelay?.(ingested as CacheIngestTuple[]);
|
|
122
220
|
}
|
|
123
221
|
|
|
124
222
|
this.logger.debug(
|
|
125
|
-
{ count: records.length, Category: '
|
|
223
|
+
{ count: records.length, Category: 'sp00ky-client::CacheModule::saveBatch' },
|
|
126
224
|
'Batch saved successfully'
|
|
127
225
|
);
|
|
128
226
|
} catch (err) {
|
|
227
|
+
if (err instanceof StaleEpochError) {
|
|
228
|
+
this.logger.debug(
|
|
229
|
+
{ count: records.length, Category: 'sp00ky-client::CacheModule::saveBatch' },
|
|
230
|
+
'Dropped batch from before a bucket switch'
|
|
231
|
+
);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
129
234
|
this.logger.error(
|
|
130
|
-
{ err, count: records.length, Category: '
|
|
235
|
+
{ err, count: records.length, Category: 'sp00ky-client::CacheModule::saveBatch' },
|
|
131
236
|
'Failed to save batch'
|
|
132
237
|
);
|
|
133
238
|
throw err;
|
|
@@ -137,29 +242,41 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
137
242
|
/**
|
|
138
243
|
* Delete a record from local DB and ingest deletion into DBSP
|
|
139
244
|
*/
|
|
140
|
-
async delete(table: string, id: string, skipDbDelete: boolean = false): Promise<void> {
|
|
245
|
+
async delete(table: string, id: string, skipDbDelete: boolean = false, recordData: Record<string, any> = {}): Promise<void> {
|
|
141
246
|
this.logger.debug(
|
|
142
|
-
{ table, id, Category: '
|
|
247
|
+
{ table, id, Category: 'sp00ky-client::CacheModule::delete' },
|
|
143
248
|
'Deleting record'
|
|
144
249
|
);
|
|
145
250
|
|
|
251
|
+
const epoch = this.local.epoch;
|
|
146
252
|
try {
|
|
147
253
|
// 1. Delete from local database
|
|
148
254
|
if (!skipDbDelete) {
|
|
149
|
-
await this.local.query('DELETE $id', { id: parseRecordIdString(id) });
|
|
255
|
+
await this.local.query('DELETE $id', { id: parseRecordIdString(id) }, { epoch });
|
|
150
256
|
}
|
|
257
|
+
if (this.local.epoch !== epoch) throw new StaleEpochError();
|
|
151
258
|
|
|
152
|
-
// 2. Ingest deletion into DBSP
|
|
259
|
+
// 2. Ingest deletion into DBSP (pass record data so predicates can be matched)
|
|
153
260
|
delete this.versionLookups[id];
|
|
154
|
-
|
|
261
|
+
this.streamProcessor.ingestMany([{ table, op: 'DELETE', id, record: recordData }]);
|
|
262
|
+
if (!this.relayLocalWritesOnly || skipDbDelete) {
|
|
263
|
+
this.ingestRelay?.([{ table, op: 'DELETE', id, record: recordData }]);
|
|
264
|
+
}
|
|
155
265
|
|
|
156
266
|
this.logger.debug(
|
|
157
|
-
{ table, id, Category: '
|
|
267
|
+
{ table, id, Category: 'sp00ky-client::CacheModule::delete' },
|
|
158
268
|
'Record deleted successfully'
|
|
159
269
|
);
|
|
160
270
|
} catch (err) {
|
|
271
|
+
if (err instanceof StaleEpochError) {
|
|
272
|
+
this.logger.debug(
|
|
273
|
+
{ table, id, Category: 'sp00ky-client::CacheModule::delete' },
|
|
274
|
+
'Dropped delete from before a bucket switch'
|
|
275
|
+
);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
161
278
|
this.logger.error(
|
|
162
|
-
{ err, table, id, Category: '
|
|
279
|
+
{ err, table, id, Category: 'sp00ky-client::CacheModule::delete' },
|
|
163
280
|
'Failed to delete record'
|
|
164
281
|
);
|
|
165
282
|
throw err;
|
|
@@ -170,12 +287,15 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
170
287
|
* Register a query with DBSP to create a materialized view
|
|
171
288
|
* Returns the initial result array
|
|
172
289
|
*/
|
|
173
|
-
registerQuery(config: QueryConfig): {
|
|
290
|
+
registerQuery(config: QueryConfig): {
|
|
291
|
+
localArray: RecordVersionArray;
|
|
292
|
+
registrationTimings?: { parseMs: number; planMs: number; snapshotMs: number };
|
|
293
|
+
} {
|
|
174
294
|
this.logger.debug(
|
|
175
295
|
{
|
|
176
296
|
queryHash: config.queryHash,
|
|
177
297
|
surql: config.surql,
|
|
178
|
-
Category: '
|
|
298
|
+
Category: 'sp00ky-client::CacheModule::registerQuery',
|
|
179
299
|
},
|
|
180
300
|
'Registering query'
|
|
181
301
|
);
|
|
@@ -202,15 +322,15 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
202
322
|
{
|
|
203
323
|
queryHash: config.queryHash,
|
|
204
324
|
arrayLength: update.localArray?.length,
|
|
205
|
-
Category: '
|
|
325
|
+
Category: 'sp00ky-client::CacheModule::registerQuery',
|
|
206
326
|
},
|
|
207
327
|
'Query registered successfully'
|
|
208
328
|
);
|
|
209
329
|
|
|
210
|
-
return { localArray: update.localArray };
|
|
330
|
+
return { localArray: update.localArray, registrationTimings: update.registration };
|
|
211
331
|
} catch (err) {
|
|
212
332
|
this.logger.error(
|
|
213
|
-
{ err, queryHash: config.queryHash, Category: '
|
|
333
|
+
{ err, queryHash: config.queryHash, Category: 'sp00ky-client::CacheModule::registerQuery' },
|
|
214
334
|
'Failed to register query'
|
|
215
335
|
);
|
|
216
336
|
throw err;
|
|
@@ -222,18 +342,18 @@ export class CacheModule implements StreamUpdateReceiver {
|
|
|
222
342
|
*/
|
|
223
343
|
unregisterQuery(queryHash: string): void {
|
|
224
344
|
this.logger.debug(
|
|
225
|
-
{ queryHash, Category: '
|
|
345
|
+
{ queryHash, Category: 'sp00ky-client::CacheModule::unregisterQuery' },
|
|
226
346
|
'Unregistering query'
|
|
227
347
|
);
|
|
228
348
|
try {
|
|
229
349
|
this.streamProcessor.unregisterQueryPlan(queryHash);
|
|
230
350
|
this.logger.debug(
|
|
231
|
-
{ queryHash, Category: '
|
|
351
|
+
{ queryHash, Category: 'sp00ky-client::CacheModule::unregisterQuery' },
|
|
232
352
|
'Query unregistered successfully'
|
|
233
353
|
);
|
|
234
354
|
} catch (err) {
|
|
235
355
|
this.logger.error(
|
|
236
|
-
{ err, queryHash, Category: '
|
|
356
|
+
{ err, queryHash, Category: 'sp00ky-client::CacheModule::unregisterQuery' },
|
|
237
357
|
'Failed to unregister query'
|
|
238
358
|
);
|
|
239
359
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RecordId, Duration } from 'surrealdb';
|
|
2
|
-
import { QueryTimeToLive
|
|
1
|
+
import type { RecordId, Duration } from 'surrealdb';
|
|
2
|
+
import type { QueryTimeToLive } from '../../types';
|
|
3
3
|
|
|
4
4
|
export type RecordWithId = Record<string, any> & { id: RecordId<string> };
|
|
5
5
|
|