@spooky-sync/core 0.0.1-canary.32 → 0.0.1-canary.321
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 +81 -0
- package/README.md +56 -16
- package/dist/index.d.ts +2034 -137
- package/dist/index.js +15236 -2467
- package/dist/live.d.ts +141 -0
- package/dist/live.js +733 -0
- package/dist/otel/index.d.ts +1 -1
- package/dist/otel/index.js +3 -3
- package/dist/pure.d.ts +365 -0
- package/dist/pure.js +24 -0
- package/dist/sqlite-open.js +379 -0
- package/dist/sqlite-worker.d.ts +1 -0
- package/dist/sqlite-worker.js +439 -0
- package/dist/sw.d.ts +207 -0
- package/dist/sw.js +393 -0
- package/dist/tabs-broker-worker.d.ts +8 -0
- package/dist/tabs-broker-worker.js +472 -0
- package/dist/types.d.ts +1821 -38
- package/dist/worker-edges.js +470 -0
- package/dist/worker-impersonation.js +60 -0
- package/dist/worker-ref-tables.d.ts +48 -0
- package/dist/worker-types.d.ts +112 -0
- package/dist/worker-types.js +73 -0
- package/package.json +27 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/scripts/check-worker-entries.mjs +172 -0
- package/skills/sp00ky-core/SKILL.md +31 -6
- package/skills/sp00ky-core/references/config.md +6 -0
- package/src/boot/auth-flip.saga.test.ts +135 -0
- package/src/boot/auth-flip.saga.ts +77 -0
- package/src/boot/boot.saga.test.ts +180 -0
- package/src/boot/boot.saga.ts +142 -0
- package/src/boot/bucket-switch.saga.test.ts +187 -0
- package/src/boot/bucket-switch.saga.ts +133 -0
- package/src/boot/preload.saga.test.ts +51 -0
- package/src/boot/preload.saga.ts +31 -0
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/client/bucket-handle.test.ts +117 -0
- package/src/client/bucket-handle.ts +227 -0
- package/src/client/router.test.ts +58 -0
- package/src/client/router.ts +100 -0
- package/src/client/runtime.test.ts +195 -0
- package/src/client/runtime.ts +296 -0
- package/src/client/services.test.ts +60 -0
- package/src/client/services.ts +418 -0
- package/src/client/sp00ky-client.test.ts +303 -0
- package/src/client/sp00ky-client.ts +586 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +69 -1
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +80 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +163 -0
- package/src/kernel/events.ts +57 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +148 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -0
- package/src/live/index.test.ts +501 -0
- package/src/live/index.ts +1039 -0
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +209 -0
- package/src/modules/auth/auth.local-first.test.ts +169 -0
- package/src/modules/auth/events/index.ts +9 -2
- package/src/modules/auth/impersonation-banner.ts +443 -0
- package/src/modules/auth/impersonation.integration.test.ts +205 -0
- package/src/modules/auth/impersonation.test.ts +377 -0
- package/src/modules/auth/impersonation.ts +179 -0
- package/src/modules/auth/index.ts +466 -19
- 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/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +746 -95
- package/src/modules/devtools/mutations.test.ts +92 -0
- package/src/modules/devtools/mutations.ts +160 -0
- package/src/modules/devtools/notify-throttle.test.ts +166 -0
- package/src/modules/devtools/state-shape.test.ts +263 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +298 -0
- package/src/modules/feature-flag/index.ts +316 -0
- package/src/modules/ref-tables.test.ts +93 -0
- package/src/modules/ref-tables.ts +91 -0
- package/src/modules/web-push/index.test.ts +824 -0
- package/src/modules/web-push/index.ts +1027 -0
- package/src/mutation/jobs.test.ts +25 -0
- package/src/mutation/jobs.ts +41 -0
- package/src/mutation/mutation-id.test.ts +37 -0
- package/src/mutation/mutation-id.ts +35 -0
- package/src/mutation/pending-writes.integration.test.ts +175 -0
- package/src/mutation/push.saga.test.ts +285 -0
- package/src/mutation/push.saga.ts +217 -0
- package/src/mutation/rows.test.ts +231 -0
- package/src/mutation/rows.ts +427 -0
- package/src/mutation/tray.saga.test.ts +63 -0
- package/src/mutation/tray.saga.ts +48 -0
- package/src/mutation/write.saga.test.ts +316 -0
- package/src/mutation/write.saga.ts +268 -0
- package/src/otel/index.ts +10 -7
- package/src/pure.ts +66 -0
- package/src/push/types.ts +174 -0
- package/src/query/anon-query-isolation.integration.test.ts +337 -0
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +56 -0
- package/src/query/fetch.saga.test.ts +199 -0
- package/src/query/fetch.saga.ts +120 -0
- package/src/query/hash.test.ts +14 -0
- package/src/query/hash.ts +18 -0
- package/src/query/id-set-plan.test.ts +132 -0
- package/src/query/lifecycle.saga.test.ts +274 -0
- package/src/query/lifecycle.saga.ts +159 -0
- package/src/query/list-ref-live.integration.test.ts +203 -0
- package/src/query/materialize.saga.test.ts +115 -0
- package/src/query/materialize.saga.ts +84 -0
- package/src/query/materialize.test.ts +58 -0
- package/src/query/materialize.ts +57 -0
- package/src/query/membership.saga.test.ts +350 -0
- package/src/query/membership.saga.ts +278 -0
- package/src/query/membership.test.ts +118 -0
- package/src/query/membership.ts +201 -0
- package/src/query/register.saga.test.ts +311 -0
- package/src/query/register.saga.ts +229 -0
- package/src/query/render-set.test.ts +47 -0
- package/src/query/render-set.ts +71 -0
- package/src/query/sql.test.ts +112 -0
- package/src/query/sql.ts +149 -0
- package/src/query/window-query.test.ts +70 -0
- package/src/query/window-query.ts +195 -0
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +199 -0
- package/src/services/database/connection-supervisor.test.ts +318 -0
- package/src/services/database/connection-supervisor.ts +422 -0
- package/src/services/database/database.query-timeout.test.ts +140 -0
- package/src/services/database/database.responses.test.ts +43 -0
- package/src/services/database/database.ts +146 -61
- package/src/services/database/engine-factory.ts +33 -0
- package/src/services/database/errors.ts +34 -0
- package/src/services/database/events/index.ts +2 -1
- package/src/services/database/index.ts +7 -0
- package/src/services/database/landed-body-clear.integration.test.ts +236 -0
- package/src/services/database/local-migrator.integration.test.ts +110 -0
- package/src/services/database/local-migrator.ts +59 -16
- package/src/services/database/local.test.ts +64 -0
- package/src/services/database/local.ts +487 -63
- package/src/services/database/plan-render.test.ts +159 -0
- package/src/services/database/plan-render.ts +108 -0
- package/src/services/database/relation-resolver.test.ts +484 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +273 -6
- package/src/services/database/sqlite-cache-engine.test.ts +617 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1372 -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 +145 -0
- package/src/services/database/sqlite-plan-sql.ts +182 -0
- package/src/services/database/sqlite-projection.test.ts +121 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +261 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +416 -0
- package/src/services/database/surreal-cache-engine.ts +161 -0
- package/src/services/logger/index.test.ts +132 -0
- package/src/services/logger/index.ts +189 -7
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +10 -3
- package/src/services/persistence/surrealdb.ts +4 -4
- package/src/services/stream-processor/index.ts +783 -68
- 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 +58 -2
- 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 +386 -0
- package/src/services/tabs/coordinator.ts +638 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +262 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +640 -0
- package/src/sp00ky.ts +5 -395
- package/src/state/client-state.ts +200 -0
- package/src/state/lifecycle.test.ts +141 -0
- package/src/state/lifecycle.ts +99 -0
- package/src/state/reducers.test.ts +304 -0
- package/src/state/reducers.ts +526 -0
- package/src/state/selectors.test.ts +175 -0
- package/src/state/selectors.ts +222 -0
- package/src/sw/index.test.ts +582 -0
- package/src/sw/index.ts +607 -0
- package/src/sw/store.ts +96 -0
- package/src/sync/connection.saga.test.ts +97 -0
- package/src/sync/connection.saga.ts +89 -0
- package/src/sync/live.saga.test.ts +178 -0
- package/src/sync/live.saga.ts +107 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/sync/policy.test.ts +663 -0
- package/src/sync/policy.ts +515 -0
- package/src/sync/poll.saga.test.ts +51 -0
- package/src/sync/poll.saga.ts +36 -0
- package/src/sync/tabs.saga.test.ts +78 -0
- package/src/sync/tabs.saga.ts +96 -0
- package/src/testing/adapters.ts +133 -0
- package/src/testing/build.ts +63 -0
- package/src/testing/fake-services.ts +48 -0
- package/src/testing/run-pure.test.ts +92 -0
- package/src/testing/run-pure.ts +128 -0
- package/src/testing/services.ts +17 -0
- package/src/types.ts +624 -10
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/edges.ts +39 -0
- package/src/utils/error-classification.test.ts +54 -0
- package/src/utils/error-classification.ts +25 -0
- package/src/utils/index.ts +78 -12
- package/src/utils/parser.test.ts +67 -102
- package/src/utils/parser.ts +64 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/sha256.ts +15 -0
- package/src/utils/surql.ts +30 -18
- package/src/utils/utils.test.ts +3 -2
- package/tsdown.config.ts +117 -3
- package/vitest.config.ts +48 -0
- package/src/modules/cache/index.ts +0 -241
- package/src/modules/cache/types.ts +0 -19
- package/src/modules/data/data.test.ts +0 -58
- package/src/modules/data/index.ts +0 -879
- package/src/modules/sync/engine.ts +0 -173
- package/src/modules/sync/events/index.ts +0 -77
- package/src/modules/sync/index.ts +0 -3
- package/src/modules/sync/queue/index.ts +0 -2
- package/src/modules/sync/queue/queue-down.ts +0 -89
- package/src/modules/sync/queue/queue-up.ts +0 -223
- package/src/modules/sync/scheduler.ts +0 -84
- package/src/modules/sync/sync.ts +0 -421
- package/src/modules/sync/utils.test.ts +0 -311
- package/src/modules/sync/utils.ts +0 -167
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { backoffMs, RETRY_BASE_MS, RETRY_MAX_MS } from './constants';
|
|
3
|
+
|
|
4
|
+
describe('backoffMs', () => {
|
|
5
|
+
it('doubles from the base and caps at the max', () => {
|
|
6
|
+
expect(backoffMs(0)).toBe(RETRY_BASE_MS);
|
|
7
|
+
expect(backoffMs(1)).toBe(RETRY_BASE_MS * 2);
|
|
8
|
+
expect(backoffMs(3)).toBe(RETRY_BASE_MS * 8);
|
|
9
|
+
expect(backoffMs(20)).toBe(RETRY_MAX_MS);
|
|
10
|
+
});
|
|
11
|
+
it('clamps negative and huge attempts', () => {
|
|
12
|
+
expect(backoffMs(-5)).toBe(RETRY_BASE_MS);
|
|
13
|
+
expect(backoffMs(10_000)).toBe(RETRY_MAX_MS);
|
|
14
|
+
});
|
|
15
|
+
it('honours custom base and max', () => {
|
|
16
|
+
expect(backoffMs(2, 100, 250)).toBe(250);
|
|
17
|
+
expect(backoffMs(1, 100, 250)).toBe(200);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every timing constant of the saga core in one place. Sagas never call
|
|
3
|
+
* `setTimeout`; they yield `timer.set` effects with one of these delays, so a
|
|
4
|
+
* test can pin the schedule by asserting the effect log.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Trailing coalesce for materialization after a state change. */
|
|
8
|
+
export const MATERIALIZE_DEBOUNCE_MS = 50;
|
|
9
|
+
/** Coalesce window for LIVE membership dirt before the batched edge re-read. */
|
|
10
|
+
export const MEMBERSHIP_COALESCE_MS = 50;
|
|
11
|
+
/** Re-read ladder while the server reports a view as `materializing`. */
|
|
12
|
+
export const MATERIALIZING_REREAD_LADDER_MS: readonly number[] = [150, 400, 1000];
|
|
13
|
+
/** How long an acked outbox item stays in the overlay when membership never names it. */
|
|
14
|
+
export const ACK_GRACE_MS = 30_000;
|
|
15
|
+
/** Ids per body-fetch statement. */
|
|
16
|
+
export const FETCH_CHUNK = 500;
|
|
17
|
+
/** Outbox statements per push request. */
|
|
18
|
+
export const OUTBOX_BATCH_SIZE = 50;
|
|
19
|
+
/** Push deadline per outbox batch. */
|
|
20
|
+
export const PUSH_TIMEOUT_MS = 30_000;
|
|
21
|
+
/** Exponential backoff for outbox / fetch / registration retries. */
|
|
22
|
+
export const RETRY_BASE_MS = 500;
|
|
23
|
+
export const RETRY_MAX_MS = 15_000;
|
|
24
|
+
/** Registration attempts before a query is reported as failed to settle. */
|
|
25
|
+
export const REGISTER_MAX_RETRIES = 3;
|
|
26
|
+
/**
|
|
27
|
+
* Pacing of repeated view-lost recovery. The first re-registration after a
|
|
28
|
+
* `_00_query` row reads as gone is immediate; each further one for the same
|
|
29
|
+
* query, before the row has been seen again, waits on this backoff.
|
|
30
|
+
*/
|
|
31
|
+
export const VIEW_LOST_RETRY_BASE_MS = 1_000;
|
|
32
|
+
export const VIEW_LOST_RETRY_MAX_MS = 30_000;
|
|
33
|
+
/** `_00_list_ref` poll cadence (fallback when LIVE is quiet). */
|
|
34
|
+
export const LIST_REF_POLL_BASE_MS = 500;
|
|
35
|
+
export const LIST_REF_POLL_MAX_MS = 5_000;
|
|
36
|
+
export const LIST_REF_ROW_BUDGET = 1_500;
|
|
37
|
+
export const LIST_REF_LARGE_VIEW_EDGES = 1_000;
|
|
38
|
+
export const LIST_REF_LARGE_VIEW_POLL_MS = 15_000;
|
|
39
|
+
/** Self-heal cadence while sync health is degraded. */
|
|
40
|
+
export const SELF_HEAL_BASE_MS = 2_000;
|
|
41
|
+
export const SELF_HEAL_MAX_MS = 30_000;
|
|
42
|
+
/** Consecutive failed rounds before health flips to degraded. */
|
|
43
|
+
export const DEGRADE_AFTER_FAILURES = 3;
|
|
44
|
+
/** Burst coalescing of reconnects. */
|
|
45
|
+
export const RECONNECT_REFETCH_COOLDOWN_MS = 10_000;
|
|
46
|
+
/** Waiting for `$auth.id` to be visible after a reconnect. */
|
|
47
|
+
export const AUTH_READY_RETRY_MS = 500;
|
|
48
|
+
export const AUTH_READY_MAX_ATTEMPTS = 10;
|
|
49
|
+
/** Heartbeat at this fraction of the shortest ttl in state. */
|
|
50
|
+
export const TTL_HEARTBEAT_FRACTION = 0.5;
|
|
51
|
+
/**
|
|
52
|
+
* How long past its due flush another tab's `_00_pending_writes` row may sit
|
|
53
|
+
* before it counts as abandoned (its tab died before the flush) and is moved
|
|
54
|
+
* into the outbox here.
|
|
55
|
+
*/
|
|
56
|
+
export const PENDING_WRITE_ORPHAN_MS = 3_000;
|
|
57
|
+
/**
|
|
58
|
+
* Orphan body garbage collection: the first sweep waits this long after boot
|
|
59
|
+
* (off the first paint and the first sync round), then repeats on the interval.
|
|
60
|
+
*/
|
|
61
|
+
export const GC_BOOT_DELAY_MS = 30_000;
|
|
62
|
+
export const GC_INTERVAL_MS = 60 * 60 * 1000;
|
|
63
|
+
/** Ids deleted per GC step. */
|
|
64
|
+
export const GC_CHUNK = 200;
|
|
65
|
+
/**
|
|
66
|
+
* A `_00_view` row no query has resolved for this long stops vouching for its
|
|
67
|
+
* ids (the sweep deletes it, so its bodies can be collected). Each session
|
|
68
|
+
* that resolves a query rewrites its row, so this only retires query shapes
|
|
69
|
+
* the app no longer runs.
|
|
70
|
+
*/
|
|
71
|
+
export const VIEW_RETENTION_MS = 14 * 24 * 60 * 60 * 1000;
|
|
72
|
+
/** Remote adapter slot limit. */
|
|
73
|
+
export const REMOTE_CONCURRENCY = 16;
|
|
74
|
+
/** Rolling telemetry sample window per query. */
|
|
75
|
+
export const TELEMETRY_SAMPLE_WINDOW = 100;
|
|
76
|
+
|
|
77
|
+
export function backoffMs(attempt: number, base = RETRY_BASE_MS, max = RETRY_MAX_MS): number {
|
|
78
|
+
const n = Math.max(0, Math.min(attempt, 30));
|
|
79
|
+
return Math.min(max, base * 2 ** n);
|
|
80
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { fx } from './effects';
|
|
3
|
+
|
|
4
|
+
describe('effect constructors', () => {
|
|
5
|
+
it('build every effect kind as plain data', () => {
|
|
6
|
+
const sealed = { sql: 'x;', extract: (r: unknown[]) => r[0] };
|
|
7
|
+
const plan = { table: 't' } as any;
|
|
8
|
+
const cases: Array<[ReturnType<typeof fx.now>, string]> = [
|
|
9
|
+
[fx.local.query('SELECT 1', { a: 1 }, 3), 'local.query'],
|
|
10
|
+
[fx.local.select(plan, { b: 2 }), 'local.select'],
|
|
11
|
+
[fx.local.execute(sealed, {}, 1), 'local.execute'],
|
|
12
|
+
[fx.local.getById('t', 'x'), 'local.getById'],
|
|
13
|
+
[fx.local.upsert('t', 'x', { a: 1 }, 'merge'), 'local.upsert'],
|
|
14
|
+
[fx.local.delete('t', 'x'), 'local.delete'],
|
|
15
|
+
[fx.remote.query('RETURN true', undefined, 10), 'remote.query'],
|
|
16
|
+
[fx.remote.live('_00_list_ref'), 'remote.live'],
|
|
17
|
+
[fx.remote.kill('u'), 'remote.kill'],
|
|
18
|
+
[fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '10m', tableName: 't' }), 'ssp.register'],
|
|
19
|
+
[fx.ssp.unregister('h'), 'ssp.unregister'],
|
|
20
|
+
[fx.ssp.ingest([]), 'ssp.ingest'],
|
|
21
|
+
[fx.timer.set('k', 5, { type: 'Drain' }), 'timer.set'],
|
|
22
|
+
[fx.timer.clear('k'), 'timer.clear'],
|
|
23
|
+
[fx.state.read((s) => s.tabId), 'state.read'],
|
|
24
|
+
[fx.state.update((s) => s), 'state.update'],
|
|
25
|
+
[fx.state.wait((s) => s.primed), 'state.wait'],
|
|
26
|
+
[fx.now(), 'now'],
|
|
27
|
+
[fx.id('mutation'), 'id'],
|
|
28
|
+
[fx.hash('abc'), 'hash'],
|
|
29
|
+
[fx.emit({ type: 'tray:changed', count: 1 }), 'emit'],
|
|
30
|
+
[fx.dispatch({ type: 'FetchRows' }), 'dispatch'],
|
|
31
|
+
[fx.all([fx.now()]), 'all'],
|
|
32
|
+
[fx.service('local.connect', 'anon'), 'service'],
|
|
33
|
+
];
|
|
34
|
+
for (const [effect, kind] of cases) expect(effect.kind).toBe(kind);
|
|
35
|
+
expect(fx.local.query('SELECT 1', { a: 1 }, 3)).toEqual({ kind: 'local.query', sql: 'SELECT 1', vars: { a: 1 }, epoch: 3 });
|
|
36
|
+
expect(fx.local.epoch()).toEqual({ kind: 'local.epoch' });
|
|
37
|
+
expect(fx.timer.set('k', 5, { type: 'Drain' })).toEqual({ kind: 'timer.set', key: 'k', ms: 5, event: { type: 'Drain' } });
|
|
38
|
+
expect(fx.service('hint.write', 'u1')).toEqual({ kind: 'service', name: 'hint.write', args: ['u1'] });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { QueryPlan } from '@spooky-sync/query-builder';
|
|
2
|
+
import type { SealedQuery } from '../utils/surql';
|
|
3
|
+
import type { ClientState } from '../state/client-state';
|
|
4
|
+
import type { IngestRecord } from '../services/stream-processor/index';
|
|
5
|
+
import type { RecordVersionArray, RegistrationTimings } from '../types';
|
|
6
|
+
import type { OutEvent, RuntimeEvent } from './events';
|
|
7
|
+
|
|
8
|
+
export type Vars = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
/** What the in-browser SSP needs to build a local view. */
|
|
11
|
+
export interface RegisterPlan {
|
|
12
|
+
queryHash: string;
|
|
13
|
+
surql: string;
|
|
14
|
+
params: Vars;
|
|
15
|
+
ttl: string;
|
|
16
|
+
tableName: string;
|
|
17
|
+
involvedTables?: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface RegisterResult {
|
|
20
|
+
localArray: RecordVersionArray;
|
|
21
|
+
timings: RegistrationTimings;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** One statement's outcome of a multi-statement remote request. */
|
|
25
|
+
export type StatementResult =
|
|
26
|
+
| { status: 'OK'; result: unknown }
|
|
27
|
+
| { status: 'ERR'; error: string };
|
|
28
|
+
|
|
29
|
+
/** One entry of an `all` fan-out result (allSettled semantics). */
|
|
30
|
+
export type Settled<T = unknown> = { ok: true; value: T } | { ok: false; error: unknown };
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Effects are data. A saga yields one and gets its result back on the next
|
|
34
|
+
* `next()`; the interpreter (client/runtime) is the only place they execute.
|
|
35
|
+
*/
|
|
36
|
+
export type Effect =
|
|
37
|
+
| { kind: 'local.query'; sql: string; vars?: Vars; epoch?: number }
|
|
38
|
+
| { kind: 'local.select'; plan: QueryPlan; params?: Vars }
|
|
39
|
+
| { kind: 'local.execute'; query: SealedQuery<unknown>; vars?: Vars; epoch?: number }
|
|
40
|
+
| { kind: 'local.getById'; table: string; id: unknown }
|
|
41
|
+
| { kind: 'local.upsert'; table: string; id: unknown; data: Record<string, unknown>; mode: 'replace' | 'merge' }
|
|
42
|
+
| { kind: 'local.delete'; table: string; id: unknown }
|
|
43
|
+
| { kind: 'local.epoch' }
|
|
44
|
+
| { kind: 'remote.query'; sql: string; vars?: Vars; timeoutMs?: number }
|
|
45
|
+
| { kind: 'remote.live'; table: string }
|
|
46
|
+
| { kind: 'remote.kill'; uuid: string }
|
|
47
|
+
| { kind: 'ssp.register'; plan: RegisterPlan }
|
|
48
|
+
| { kind: 'ssp.unregister'; hash: string }
|
|
49
|
+
| { kind: 'ssp.ingest'; records: IngestRecord[] }
|
|
50
|
+
| { kind: 'timer.set'; key: string; ms: number; event: RuntimeEvent }
|
|
51
|
+
| { kind: 'timer.clear'; key: string }
|
|
52
|
+
| { kind: 'state.read'; select: (s: ClientState) => unknown }
|
|
53
|
+
| { kind: 'state.update'; fn: (s: ClientState) => ClientState }
|
|
54
|
+
| { kind: 'state.wait'; until: (s: ClientState) => boolean }
|
|
55
|
+
| { kind: 'now' }
|
|
56
|
+
| { kind: 'id'; scope: 'mutation' | 'salt' }
|
|
57
|
+
| { kind: 'hash'; input: string }
|
|
58
|
+
| { kind: 'emit'; event: OutEvent }
|
|
59
|
+
| { kind: 'dispatch'; event: RuntimeEvent }
|
|
60
|
+
| { kind: 'all'; effects: Effect[] }
|
|
61
|
+
| { kind: 'service'; name: ServiceName; args: unknown[] };
|
|
62
|
+
|
|
63
|
+
export type EffectKind = Effect['kind'];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Calls into the legacy services that are adapters, not state: auth, tabs,
|
|
67
|
+
* blobs, migrator, crdt, persistence, the SSP lifecycle, the remote socket.
|
|
68
|
+
* A saga names the call; the runtime binds it to the real service.
|
|
69
|
+
*/
|
|
70
|
+
export interface ServiceCalls {
|
|
71
|
+
'hint.read': () => string | null;
|
|
72
|
+
'hint.write': (bucketId: string) => void;
|
|
73
|
+
'salt.take': (authId: string | null) => string | null;
|
|
74
|
+
'salt.leave': (salt: string, authId: string | null) => void;
|
|
75
|
+
'local.connect': (bucketId: string) => Promise<void>;
|
|
76
|
+
'local.switchStore': (bucketId: string) => Promise<void>;
|
|
77
|
+
'local.beginSwitch': () => () => void;
|
|
78
|
+
'local.currentBucketId': () => string;
|
|
79
|
+
'local.usesSurqlSchema': () => boolean;
|
|
80
|
+
'local.dropBucket': (bucketId: string) => Promise<void>;
|
|
81
|
+
'migrator.provision': () => Promise<void>;
|
|
82
|
+
'blobs.start': (bucketId: string) => Promise<void>;
|
|
83
|
+
'blobs.setNamespace': (bucketId: string) => Promise<void>;
|
|
84
|
+
'blobs.clear': () => Promise<void>;
|
|
85
|
+
'ssp.init': () => Promise<void>;
|
|
86
|
+
'ssp.setPermissions': () => void;
|
|
87
|
+
'ssp.setSessionAuth': (authId: string | null, access: string | null) => void;
|
|
88
|
+
'ssp.prime': (pendingIds: string[]) => Promise<void>;
|
|
89
|
+
'ssp.reset': () => Promise<void>;
|
|
90
|
+
'ssp.setPersistence': (enabled: boolean) => void;
|
|
91
|
+
'auth.restoreSession': () => Promise<string | null>;
|
|
92
|
+
'auth.init': () => Promise<void>;
|
|
93
|
+
'auth.sessionAuthId': () => string | null;
|
|
94
|
+
'auth.access': () => string | null;
|
|
95
|
+
'auth.token': () => string | null;
|
|
96
|
+
'auth.currentUser': () => Record<string, unknown> | null;
|
|
97
|
+
'auth.consumeEndedImpersonation': () => string | null;
|
|
98
|
+
'remote.connect': () => Promise<void>;
|
|
99
|
+
'remote.releaseViews': (ids: unknown[]) => void;
|
|
100
|
+
'supervisor.start': () => void;
|
|
101
|
+
'tabs.start': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
|
|
102
|
+
'tabs.moveToBucket': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
|
|
103
|
+
'crdt.setSessionId': (sessionId: string) => void;
|
|
104
|
+
'crdt.closeAll': (flush: boolean) => void;
|
|
105
|
+
'persistence.set': (key: string, value: unknown) => Promise<void>;
|
|
106
|
+
'features.init': () => void;
|
|
107
|
+
'releases.init': () => void;
|
|
108
|
+
'window.attach': () => void;
|
|
109
|
+
}
|
|
110
|
+
export type ServiceName = keyof ServiceCalls;
|
|
111
|
+
|
|
112
|
+
/** Typed constructors. Each is one line so the saga reads like the plan. */
|
|
113
|
+
export const fx = {
|
|
114
|
+
local: {
|
|
115
|
+
query: (sql: string, vars?: Vars, epoch?: number): Effect => ({ kind: 'local.query', sql, vars, epoch }),
|
|
116
|
+
select: (plan: QueryPlan, params?: Vars): Effect => ({ kind: 'local.select', plan, params }),
|
|
117
|
+
execute: (query: SealedQuery<unknown>, vars?: Vars, epoch?: number): Effect => ({
|
|
118
|
+
kind: 'local.execute',
|
|
119
|
+
query,
|
|
120
|
+
vars,
|
|
121
|
+
epoch,
|
|
122
|
+
}),
|
|
123
|
+
getById: (table: string, id: unknown): Effect => ({ kind: 'local.getById', table, id }),
|
|
124
|
+
upsert: (table: string, id: unknown, data: Record<string, unknown>, mode: 'replace' | 'merge'): Effect => ({
|
|
125
|
+
kind: 'local.upsert',
|
|
126
|
+
table,
|
|
127
|
+
id,
|
|
128
|
+
data,
|
|
129
|
+
mode,
|
|
130
|
+
}),
|
|
131
|
+
delete: (table: string, id: unknown): Effect => ({ kind: 'local.delete', table, id }),
|
|
132
|
+
/** The store's current epoch; a write fenced with it is dropped after a bucket switch or transport failover. */
|
|
133
|
+
epoch: (): Effect => ({ kind: 'local.epoch' }),
|
|
134
|
+
},
|
|
135
|
+
remote: {
|
|
136
|
+
query: (sql: string, vars?: Vars, timeoutMs?: number): Effect => ({ kind: 'remote.query', sql, vars, timeoutMs }),
|
|
137
|
+
/** Subscribe to `LIVE SELECT * FROM <table>`; resolves to the live uuid. */
|
|
138
|
+
live: (table: string): Effect => ({ kind: 'remote.live', table }),
|
|
139
|
+
kill: (uuid: string): Effect => ({ kind: 'remote.kill', uuid }),
|
|
140
|
+
},
|
|
141
|
+
ssp: {
|
|
142
|
+
register: (plan: RegisterPlan): Effect => ({ kind: 'ssp.register', plan }),
|
|
143
|
+
unregister: (hash: string): Effect => ({ kind: 'ssp.unregister', hash }),
|
|
144
|
+
ingest: (records: IngestRecord[]): Effect => ({ kind: 'ssp.ingest', records }),
|
|
145
|
+
},
|
|
146
|
+
timer: {
|
|
147
|
+
set: (key: string, ms: number, event: RuntimeEvent): Effect => ({ kind: 'timer.set', key, ms, event }),
|
|
148
|
+
clear: (key: string): Effect => ({ kind: 'timer.clear', key }),
|
|
149
|
+
},
|
|
150
|
+
state: {
|
|
151
|
+
read: <T>(select: (s: ClientState) => T): Effect => ({ kind: 'state.read', select }),
|
|
152
|
+
update: (fn: (s: ClientState) => ClientState): Effect => ({ kind: 'state.update', fn }),
|
|
153
|
+
/** Suspend until `until(state)` holds (checked after every state.update). */
|
|
154
|
+
wait: (until: (s: ClientState) => boolean): Effect => ({ kind: 'state.wait', until }),
|
|
155
|
+
},
|
|
156
|
+
now: (): Effect => ({ kind: 'now' }),
|
|
157
|
+
id: (scope: 'mutation' | 'salt'): Effect => ({ kind: 'id', scope }),
|
|
158
|
+
hash: (input: string): Effect => ({ kind: 'hash', input }),
|
|
159
|
+
emit: (event: OutEvent): Effect => ({ kind: 'emit', event }),
|
|
160
|
+
dispatch: (event: RuntimeEvent): Effect => ({ kind: 'dispatch', event }),
|
|
161
|
+
all: (effects: Effect[]): Effect => ({ kind: 'all', effects }),
|
|
162
|
+
service: <N extends ServiceName>(name: N, ...args: Parameters<ServiceCalls[N]>): Effect => ({ kind: 'service', name, args }),
|
|
163
|
+
} as const;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { ConnectionState, InlineRow, QueryHash, QueryStatus, SyncHealth } from '../types';
|
|
2
|
+
import type { StreamUpdate } from '../services/stream-processor/index';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Inbound events. Everything that can start a saga arrives as one of these
|
|
6
|
+
* through the runtime router: public API calls, adapter callbacks, timers,
|
|
7
|
+
* and `dispatch` effects from other sagas.
|
|
8
|
+
*/
|
|
9
|
+
export type RuntimeEvent =
|
|
10
|
+
| { type: 'EnsureRegistered'; requireAuth?: boolean; attempt?: number }
|
|
11
|
+
| { type: 'RegisterRemote'; hash: QueryHash; retry?: boolean }
|
|
12
|
+
| { type: 'SyncOutcome'; ok: boolean; error?: unknown }
|
|
13
|
+
| { type: 'AckPrune' }
|
|
14
|
+
| { type: 'ReadDirtyMembership' }
|
|
15
|
+
| { type: 'RecoverLostView'; hash: QueryHash }
|
|
16
|
+
| { type: 'ReadMembership'; hashes: QueryHash[] }
|
|
17
|
+
| { type: 'FetchRows' }
|
|
18
|
+
| { type: 'Materialize'; hash: QueryHash }
|
|
19
|
+
| { type: 'MaterializeDirty' }
|
|
20
|
+
| { type: 'LifecycleTick' }
|
|
21
|
+
| { type: 'GcTick' }
|
|
22
|
+
| { type: 'Drain' }
|
|
23
|
+
| { type: 'FlushWrite'; key: string }
|
|
24
|
+
| { type: 'FlushPendingWrites' }
|
|
25
|
+
| { type: 'AdoptPendingWrites' }
|
|
26
|
+
| { type: 'PollTick' }
|
|
27
|
+
| { type: 'SelfHealTick' }
|
|
28
|
+
| { type: 'HeartbeatNow' }
|
|
29
|
+
| { type: 'StartRemote' }
|
|
30
|
+
| { type: 'PrimeCircuit' }
|
|
31
|
+
| { type: 'VersionsPrimed'; entries: Array<readonly [string, number]> }
|
|
32
|
+
| { type: 'WarmBlobs' }
|
|
33
|
+
| { type: 'AuthFlip'; userId: string | null }
|
|
34
|
+
| { type: 'BucketSwitch'; target: string }
|
|
35
|
+
| { type: 'PageHide' }
|
|
36
|
+
| { type: 'ConnectionChanged'; state: ConnectionState }
|
|
37
|
+
| { type: 'StreamUpdate'; update: StreamUpdate }
|
|
38
|
+
| { type: 'LiveStart' }
|
|
39
|
+
| { type: 'LiveChange'; hashes: QueryHash[]; rows?: InlineRow[] }
|
|
40
|
+
| { type: 'TabRole'; role: 'solo' | 'leader' | 'follower' }
|
|
41
|
+
| { type: 'TabMessage'; message: unknown };
|
|
42
|
+
|
|
43
|
+
export type OutEvent =
|
|
44
|
+
| { type: 'query:records'; hash: QueryHash; records: ReadonlyArray<Record<string, unknown>> }
|
|
45
|
+
| { type: 'query:status'; hash: QueryHash; status: QueryStatus }
|
|
46
|
+
| { type: 'query:authority'; hash: QueryHash; known: boolean }
|
|
47
|
+
| { type: 'query:view-lost'; hash: QueryHash }
|
|
48
|
+
| { type: 'query:evicted'; hash: QueryHash }
|
|
49
|
+
| { type: 'mutation:event'; event: unknown }
|
|
50
|
+
| { type: 'mutation:settled'; mutationId: string; recordId: string; eventType: string }
|
|
51
|
+
| { type: 'mutation:rolled-back'; mutationId: string; recordId: string; eventType: string; error: string }
|
|
52
|
+
| { type: 'tray:changed'; count: number }
|
|
53
|
+
| { type: 'health:changed'; health: SyncHealth }
|
|
54
|
+
| { type: 'activity:changed'; fetching: number; pending: number }
|
|
55
|
+
| { type: 'tabs:broadcast'; message: unknown }
|
|
56
|
+
| { type: 'tabs:sendTo'; tabId: string; message: unknown }
|
|
57
|
+
| { type: 'log'; level: 'debug' | 'info' | 'warn' | 'error'; message: string; data?: unknown };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RecordId } from 'surrealdb';
|
|
3
|
+
import { fakeAdapters, fakeHost } from '../testing/adapters';
|
|
4
|
+
import { createInterpreter } from './interpreter';
|
|
5
|
+
import { fx } from './effects';
|
|
6
|
+
import { setFailedCount } from '../state/reducers';
|
|
7
|
+
|
|
8
|
+
describe('interpreter', () => {
|
|
9
|
+
it('routes store, remote, ssp and service effects to the adapters', async () => {
|
|
10
|
+
const a = fakeAdapters({
|
|
11
|
+
remote: { queryResponses: async () => [{ status: 'OK', result: 1 }] },
|
|
12
|
+
ssp: {
|
|
13
|
+
registerQueryPlan: (plan) => ({ queryHash: plan.queryHash, localArray: [['t:1', 1]], registration: { parseMs: 1, planMs: 2, snapshotMs: 3 } }),
|
|
14
|
+
},
|
|
15
|
+
services: { 'hint.read': () => 'u1' },
|
|
16
|
+
});
|
|
17
|
+
const h = fakeHost();
|
|
18
|
+
const run = createInterpreter(a.adapters, h.host);
|
|
19
|
+
const sealed = { sql: 'SELECT 1;', extract: (r: unknown[]) => r };
|
|
20
|
+
await run(fx.local.query('SELECT 1', { a: 1 }, 2));
|
|
21
|
+
await run(fx.local.query('SELECT 2'));
|
|
22
|
+
await run(fx.local.execute(sealed, {}, 1));
|
|
23
|
+
await run(fx.local.select({ table: 't' } as any, {}));
|
|
24
|
+
await run(fx.local.getById('t', 'x'));
|
|
25
|
+
await run(fx.local.upsert('t', 'x', { a: 1 }, 'merge'));
|
|
26
|
+
await run(fx.local.delete('t', 'x'));
|
|
27
|
+
expect(await run(fx.local.epoch())).toBe(0);
|
|
28
|
+
expect(await run(fx.remote.query('RETURN 1'))).toEqual([{ status: 'OK', result: 1 }]);
|
|
29
|
+
expect(await run(fx.remote.live('_00_list_ref'))).toBe('live-uuid');
|
|
30
|
+
await run(fx.remote.kill('live-uuid'));
|
|
31
|
+
const reg = await run(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '10m', tableName: 't' }));
|
|
32
|
+
expect(reg).toEqual({ localArray: [['t:1', 1]], timings: { parseMs: 1, planMs: 2, snapshotMs: 3, wallMs: null } });
|
|
33
|
+
await run(fx.ssp.unregister('h'));
|
|
34
|
+
await run(fx.ssp.ingest([]));
|
|
35
|
+
expect(await run(fx.service('hint.read'))).toBe('u1');
|
|
36
|
+
expect(a.names()).toEqual([
|
|
37
|
+
'local.query', 'local.query', 'local.execute', 'local.select', 'local.getById', 'local.upsert', 'local.delete',
|
|
38
|
+
'remote.live', 'remote.kill', 'ssp.unregister', 'ssp.ingest', 'service.hint.read',
|
|
39
|
+
]);
|
|
40
|
+
expect(a.calls[0]).toEqual(['local.query', ['SELECT 1', { a: 1 }]]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('ssp.register without a result throws; ssp.register uses default timings when the SSP reports none', async () => {
|
|
44
|
+
const empty = fakeAdapters({ ssp: { registerQueryPlan: () => undefined } });
|
|
45
|
+
const h = fakeHost();
|
|
46
|
+
await expect(createInterpreter(empty.adapters, h.host)(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '1m', tableName: 't' }))).rejects.toThrow(/not initialized/);
|
|
47
|
+
const bare = fakeAdapters();
|
|
48
|
+
const reg = await createInterpreter(bare.adapters, h.host)(fx.ssp.register({ queryHash: 'h', surql: 's', params: {}, ttl: '1m', tableName: 't' }));
|
|
49
|
+
expect(reg).toEqual({ localArray: [], timings: { parseMs: null, planMs: null, snapshotMs: null, wallMs: null } });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('remote.query honours the per-effect timeout', async () => {
|
|
53
|
+
const slow = fakeAdapters({ remote: { queryResponses: () => new Promise(() => {}) } });
|
|
54
|
+
const h = fakeHost();
|
|
55
|
+
await expect(createInterpreter(slow.adapters, h.host)(fx.remote.query('SLOW', undefined, 5))).rejects.toThrow(/timed out/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('state, wait, clock, ids, hash, timers, emit, dispatch, all', async () => {
|
|
59
|
+
const a = fakeAdapters({ now: 42 });
|
|
60
|
+
const h = fakeHost();
|
|
61
|
+
const run = createInterpreter(a.adapters, h.host);
|
|
62
|
+
expect(await run(fx.state.read((s) => s.tabId))).toBe('tab-a');
|
|
63
|
+
await run(fx.state.update(setFailedCount(2)));
|
|
64
|
+
expect(h.state.failedCount).toBe(2);
|
|
65
|
+
await run(fx.state.wait((s) => s.failedCount === 2));
|
|
66
|
+
const waiting = run(fx.state.wait((s) => s.failedCount === 3));
|
|
67
|
+
h.host.setState(setFailedCount(3)(h.state));
|
|
68
|
+
await waiting;
|
|
69
|
+
const ctrl = new AbortController();
|
|
70
|
+
const aborted = run(fx.state.wait(() => false), { signal: ctrl.signal });
|
|
71
|
+
ctrl.abort();
|
|
72
|
+
await expect(aborted).rejects.toThrow('aborted');
|
|
73
|
+
expect(await run(fx.now())).toBe(42);
|
|
74
|
+
expect(String(await run(fx.id('mutation')))).toMatch(/^_00_pending_mutations:/);
|
|
75
|
+
expect(String(await run(fx.id('salt')))).toMatch(/^salt-/);
|
|
76
|
+
expect(await run(fx.hash('abc'))).toMatch(/^ba7816bf/);
|
|
77
|
+
await run(fx.timer.set('k', 10, { type: 'Drain' }));
|
|
78
|
+
expect(a.timers.pending.has('k')).toBe(true);
|
|
79
|
+
a.timers.fire('k');
|
|
80
|
+
expect(h.dispatched).toEqual([{ type: 'Drain' }]);
|
|
81
|
+
await run(fx.timer.set('j', 1, { type: 'FetchRows' }));
|
|
82
|
+
await run(fx.timer.clear('j'));
|
|
83
|
+
expect(a.timers.pending.has('j')).toBe(false);
|
|
84
|
+
await run(fx.emit({ type: 'tray:changed', count: 1 }));
|
|
85
|
+
expect(h.emitted).toEqual([{ type: 'tray:changed', count: 1 }]);
|
|
86
|
+
await run(fx.dispatch({ type: 'PollTick' }));
|
|
87
|
+
expect(h.dispatched.at(-1)).toEqual({ type: 'PollTick' });
|
|
88
|
+
const settled = await run(fx.all([fx.now(), fx.remote.query('x'), fx.service('hint.read')]));
|
|
89
|
+
expect(settled).toEqual([{ ok: true, value: 42 }, { ok: true, value: [] }, { ok: true, value: undefined }]);
|
|
90
|
+
const failing = fakeAdapters({
|
|
91
|
+
remote: {
|
|
92
|
+
queryResponses: async () => {
|
|
93
|
+
throw new Error('boom');
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
const mixed = await createInterpreter(failing.adapters, h.host)(fx.all([fx.remote.query('x'), fx.now()]));
|
|
98
|
+
expect(mixed).toEqual([{ ok: false, error: new Error('boom') }, { ok: true, value: 1_700_000_000_000 }]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('live events reach the host as LiveChange dispatches', async () => {
|
|
102
|
+
let onChange: ((h: string[]) => void) | null = null;
|
|
103
|
+
const a = fakeAdapters({
|
|
104
|
+
remote: {
|
|
105
|
+
live: async (_t, cb) => {
|
|
106
|
+
onChange = cb;
|
|
107
|
+
return 'u';
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
const h = fakeHost();
|
|
112
|
+
await createInterpreter(a.adapters, h.host)(fx.remote.live('_00_list_ref'));
|
|
113
|
+
onChange!(['a', 'b']);
|
|
114
|
+
expect(h.dispatched).toEqual([{ type: 'LiveChange', hashes: ['a', 'b'] }]);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Duration } from 'surrealdb';
|
|
2
|
+
import type { LocalStore } from '../services/database/cache-engine';
|
|
3
|
+
import type { IngestRecord, StreamUpdate } from '../services/stream-processor/index';
|
|
4
|
+
import type { ClientState } from '../state/client-state';
|
|
5
|
+
import type { InlineRow } from '../types';
|
|
6
|
+
import { withTimeout } from '../utils/index';
|
|
7
|
+
import type { Effect, RegisterResult, ServiceCalls, Settled, StatementResult } from './effects';
|
|
8
|
+
import type { OutEvent, RuntimeEvent } from './events';
|
|
9
|
+
|
|
10
|
+
/** The five adapters plus the pure ports, as the interpreter sees them. */
|
|
11
|
+
export interface Adapters {
|
|
12
|
+
local: Pick<LocalStore, 'query' | 'execute' | 'select' | 'getById' | 'upsert' | 'delete'> & { readonly epoch: number };
|
|
13
|
+
remote: {
|
|
14
|
+
queryResponses(sql: string, vars?: Record<string, unknown>): Promise<StatementResult[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Subscribe to `LIVE SELECT * FROM <table>`; the callback receives the
|
|
17
|
+
* changed query hashes, plus the changed rows themselves when the
|
|
18
|
+
* subscription was opened with the body joined on (`liveInlineBodies`).
|
|
19
|
+
*/
|
|
20
|
+
live(table: string, onChange: (hashes: string[], rows?: InlineRow[]) => void): Promise<string>;
|
|
21
|
+
kill(uuid: string): Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
ssp: {
|
|
24
|
+
registerQueryPlan(plan: {
|
|
25
|
+
queryHash: string;
|
|
26
|
+
surql: string;
|
|
27
|
+
params: Record<string, unknown>;
|
|
28
|
+
ttl: Duration;
|
|
29
|
+
lastActiveAt: Date;
|
|
30
|
+
localArray: [];
|
|
31
|
+
remoteArray: [];
|
|
32
|
+
meta: { tableName: string; involvedTables?: string[] };
|
|
33
|
+
}): StreamUpdate | undefined;
|
|
34
|
+
unregisterQueryPlan(hash: string): void;
|
|
35
|
+
ingestMany(records: IngestRecord[]): unknown;
|
|
36
|
+
};
|
|
37
|
+
timers: { set(key: string, ms: number, fire: () => void): void; clear(key: string): void };
|
|
38
|
+
clock: { now(): number };
|
|
39
|
+
ids: { mutation(): string; salt(): string };
|
|
40
|
+
hash(input: string): Promise<string>;
|
|
41
|
+
services: ServiceCalls;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What the interpreter needs from the runtime: state, waiting, emitting, dispatching. */
|
|
45
|
+
export interface InterpreterHost {
|
|
46
|
+
getState(): ClientState;
|
|
47
|
+
setState(next: ClientState): void;
|
|
48
|
+
waitFor(until: (s: ClientState) => boolean, signal?: AbortSignal): Promise<void>;
|
|
49
|
+
emit(event: OutEvent): void;
|
|
50
|
+
dispatch(event: RuntimeEvent): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface RunContext {
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type Interpreter = (effect: Effect, ctx?: RunContext) => Promise<unknown>;
|
|
58
|
+
|
|
59
|
+
/** Effects become adapter calls here and nowhere else. */
|
|
60
|
+
export function createInterpreter(adapters: Adapters, host: InterpreterHost): Interpreter {
|
|
61
|
+
const interpret: Interpreter = async (effect, ctx) => {
|
|
62
|
+
switch (effect.kind) {
|
|
63
|
+
case 'local.query':
|
|
64
|
+
return adapters.local.query(effect.sql, effect.vars, effect.epoch === undefined ? undefined : { epoch: effect.epoch });
|
|
65
|
+
case 'local.select':
|
|
66
|
+
return adapters.local.select(effect.plan, effect.params);
|
|
67
|
+
case 'local.execute':
|
|
68
|
+
return adapters.local.execute(effect.query, effect.vars, effect.epoch === undefined ? undefined : { epoch: effect.epoch });
|
|
69
|
+
case 'local.getById':
|
|
70
|
+
return adapters.local.getById(effect.table, effect.id);
|
|
71
|
+
case 'local.upsert':
|
|
72
|
+
return adapters.local.upsert(effect.table, effect.id, effect.data, effect.mode);
|
|
73
|
+
case 'local.delete':
|
|
74
|
+
return adapters.local.delete(effect.table, effect.id);
|
|
75
|
+
case 'local.epoch':
|
|
76
|
+
return adapters.local.epoch;
|
|
77
|
+
case 'remote.query': {
|
|
78
|
+
const pending = adapters.remote.queryResponses(effect.sql, effect.vars);
|
|
79
|
+
return effect.timeoutMs
|
|
80
|
+
? withTimeout(pending, effect.timeoutMs, () => new Error(`Remote request timed out after ${effect.timeoutMs}ms`))
|
|
81
|
+
: pending;
|
|
82
|
+
}
|
|
83
|
+
case 'remote.live':
|
|
84
|
+
return adapters.remote.live(effect.table, (hashes, rows) => host.dispatch({ type: 'LiveChange', hashes, rows }));
|
|
85
|
+
case 'remote.kill':
|
|
86
|
+
return adapters.remote.kill(effect.uuid);
|
|
87
|
+
case 'ssp.register': {
|
|
88
|
+
const update = adapters.ssp.registerQueryPlan({
|
|
89
|
+
queryHash: effect.plan.queryHash,
|
|
90
|
+
surql: effect.plan.surql,
|
|
91
|
+
params: effect.plan.params,
|
|
92
|
+
ttl: new Duration(effect.plan.ttl),
|
|
93
|
+
lastActiveAt: new Date(adapters.clock.now()),
|
|
94
|
+
localArray: [],
|
|
95
|
+
remoteArray: [],
|
|
96
|
+
meta: { tableName: effect.plan.tableName, involvedTables: effect.plan.involvedTables },
|
|
97
|
+
});
|
|
98
|
+
if (!update) throw new Error('Stream processor is not initialized');
|
|
99
|
+
const result: RegisterResult = {
|
|
100
|
+
localArray: update.localArray,
|
|
101
|
+
timings: {
|
|
102
|
+
parseMs: update.registration?.parseMs ?? null,
|
|
103
|
+
planMs: update.registration?.planMs ?? null,
|
|
104
|
+
snapshotMs: update.registration?.snapshotMs ?? null,
|
|
105
|
+
wallMs: null,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
case 'ssp.unregister':
|
|
111
|
+
return adapters.ssp.unregisterQueryPlan(effect.hash);
|
|
112
|
+
case 'ssp.ingest':
|
|
113
|
+
return adapters.ssp.ingestMany(effect.records);
|
|
114
|
+
case 'timer.set':
|
|
115
|
+
return adapters.timers.set(effect.key, effect.ms, () => host.dispatch(effect.event));
|
|
116
|
+
case 'timer.clear':
|
|
117
|
+
return adapters.timers.clear(effect.key);
|
|
118
|
+
case 'state.read':
|
|
119
|
+
return effect.select(host.getState());
|
|
120
|
+
case 'state.update': {
|
|
121
|
+
const next = effect.fn(host.getState());
|
|
122
|
+
host.setState(next);
|
|
123
|
+
return next;
|
|
124
|
+
}
|
|
125
|
+
case 'state.wait':
|
|
126
|
+
return host.waitFor(effect.until, ctx?.signal);
|
|
127
|
+
case 'now':
|
|
128
|
+
return adapters.clock.now();
|
|
129
|
+
case 'id':
|
|
130
|
+
return effect.scope === 'mutation' ? adapters.ids.mutation() : adapters.ids.salt();
|
|
131
|
+
case 'hash':
|
|
132
|
+
return adapters.hash(effect.input);
|
|
133
|
+
case 'emit':
|
|
134
|
+
return host.emit(effect.event);
|
|
135
|
+
case 'dispatch':
|
|
136
|
+
return host.dispatch(effect.event);
|
|
137
|
+
case 'all': {
|
|
138
|
+
const settled = await Promise.allSettled(effect.effects.map((e) => interpret(e, ctx)));
|
|
139
|
+
return settled.map((r): Settled => (r.status === 'fulfilled' ? { ok: true, value: r.value } : { ok: false, error: r.reason }));
|
|
140
|
+
}
|
|
141
|
+
case 'service': {
|
|
142
|
+
const fn = adapters.services[effect.name] as (...args: unknown[]) => unknown;
|
|
143
|
+
return fn(...effect.args);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return interpret;
|
|
148
|
+
}
|