@spooky-sync/core 0.0.1-canary.24 → 0.0.1-canary.241
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 +77 -0
- package/README.md +56 -16
- package/dist/index.d.ts +2176 -80
- package/dist/index.js +12557 -2588
- 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 +747 -46
- package/package.json +16 -8
- package/scripts/check-broker-bundle.mjs +33 -0
- package/skills/{spooky-core → sp00ky-core}/SKILL.md +30 -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/boot/auth-flip.saga.test.ts +82 -0
- package/src/boot/auth-flip.saga.ts +62 -0
- package/src/boot/boot.saga.test.ts +151 -0
- package/src/boot/boot.saga.ts +128 -0
- package/src/boot/bucket-switch.saga.test.ts +158 -0
- package/src/boot/bucket-switch.saga.ts +115 -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 +56 -0
- package/src/client/router.ts +94 -0
- package/src/client/runtime.test.ts +192 -0
- package/src/client/runtime.ts +296 -0
- package/src/client/services.ts +349 -0
- package/src/client/sp00ky-client.test.ts +241 -0
- package/src/client/sp00ky-client.ts +482 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +37 -2
- package/src/kernel/constants.test.ts +19 -0
- package/src/kernel/constants.ts +54 -0
- package/src/kernel/effects.test.ts +40 -0
- package/src/kernel/effects.ts +159 -0
- package/src/kernel/events.ts +55 -0
- package/src/kernel/interpreter.test.ts +116 -0
- package/src/kernel/interpreter.ts +143 -0
- package/src/kernel/saga.test.ts +89 -0
- package/src/kernel/saga.ts +73 -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 +141 -0
- package/src/modules/auth/events/index.ts +2 -1
- package/src/modules/auth/index.ts +138 -25
- 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 +459 -47
- package/src/modules/devtools/notify-throttle.test.ts +154 -0
- package/src/modules/devtools/state-shape.test.ts +149 -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 +316 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -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/push.saga.test.ts +285 -0
- package/src/mutation/push.saga.ts +215 -0
- package/src/mutation/rows.test.ts +201 -0
- package/src/mutation/rows.ts +377 -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 +163 -0
- package/src/mutation/write.saga.ts +166 -0
- package/src/otel/index.ts +13 -10
- package/src/query/env.test.ts +23 -0
- package/src/query/env.ts +56 -0
- package/src/query/fetch.saga.test.ts +156 -0
- package/src/query/fetch.saga.ts +109 -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 +122 -0
- package/src/query/lifecycle.saga.ts +101 -0
- package/src/query/materialize.saga.test.ts +82 -0
- package/src/query/materialize.saga.ts +75 -0
- package/src/query/materialize.test.ts +50 -0
- package/src/query/materialize.ts +44 -0
- package/src/query/membership.saga.test.ts +246 -0
- package/src/query/membership.saga.ts +208 -0
- package/src/query/membership.test.ts +118 -0
- package/src/query/membership.ts +158 -0
- package/src/query/register.saga.test.ts +251 -0
- package/src/query/register.saga.ts +190 -0
- package/src/query/render-set.test.ts +36 -0
- package/src/query/render-set.ts +60 -0
- package/src/query/sql.test.ts +62 -0
- package/src/query/sql.ts +79 -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 +193 -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 +145 -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 +617 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1349 -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 +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 -0
- package/src/state/client-state.ts +184 -0
- package/src/state/lifecycle.test.ts +118 -0
- package/src/state/lifecycle.ts +86 -0
- package/src/state/reducers.test.ts +280 -0
- package/src/state/reducers.ts +476 -0
- package/src/state/selectors.test.ts +149 -0
- package/src/state/selectors.ts +184 -0
- package/src/sync/connection.saga.test.ts +86 -0
- package/src/sync/connection.saga.ts +85 -0
- package/src/sync/live.saga.test.ts +69 -0
- package/src/sync/live.saga.ts +49 -0
- package/src/sync/policy.health.test.ts +56 -0
- package/src/sync/policy.test.ts +659 -0
- package/src/sync/policy.ts +502 -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 +74 -0
- package/src/sync/tabs.saga.ts +92 -0
- package/src/testing/adapters.ts +133 -0
- package/src/testing/build.ts +61 -0
- package/src/testing/fake-services.ts +34 -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 +550 -14
- 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/utils.test.ts +2 -1
- package/src/utils/withRetry.test.ts +1 -1
- package/tsdown.config.ts +86 -1
- 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 -864
- package/src/modules/sync/engine.ts +0 -166
- 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 -409
- package/src/modules/sync/utils.test.ts +0 -311
- package/src/modules/sync/utils.ts +0 -170
- package/src/spooky.ts +0 -395
package/dist/index.d.ts
CHANGED
|
@@ -1,81 +1,411 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { A as ServerViewMeta, B as TimingPhase, C as QueryTimings, D as RecordVersionDiff, E as RecordVersionArray, F as StorageHealthStatus, G as SealedQuery, H as LocalStore, I as StoreType, K as EventDefinition, L as SyncHealth, M as Sp00kyQueryResult, N as Sp00kyQueryResultPromise, O as RegistrationTimings, P as StorageHealth, R as SyncHealthConfig, S as QueryTimeToLive, T as ReconnectConfig, U as DatabaseEventSystem, V as UpdateOptions, W as DatabaseEventTypes, _ as QueryConfigRecord, a as MATERIALIZATION_SAMPLE_WINDOW, b as QueryStatus, c as MutationEvent, d as PhaseStat, f as PinoTransmit, g as QueryConfig, h as QueryAuthorityCallback, i as Level, j as Sp00kyConfig, k as RunOptions, l as MutationEventType, m as PreloadRefresh, n as DebounceOptions, o as MembershipOutcome, p as PreloadOptions, q as EventSystem, r as EventSubscriptionOptions, s as MutationCallback, t as ConnectionState, u as PersistenceClient, v as QueryHash, w as QueryUpdateCallback, x as QueryStatusCallback, y as QueryState, z as SyncHealthStatus } from "./types.js";
|
|
2
|
+
import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, FinalQuery, GetTable, QueryBuilder, QueryOptions, QueryPlan, RecordId, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
|
|
2
3
|
import * as surrealdb0 from "surrealdb";
|
|
3
|
-
import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { Duration, RecordId as RecordId$1, Surreal, SurrealEvents, SurrealTransaction } from "surrealdb";
|
|
5
|
+
import { Level as Level$1, Logger } from "pino";
|
|
6
|
+
import { decode, encode, isBlurhashValid } from "blurhash";
|
|
7
|
+
import { LoroDoc } from "loro-crdt";
|
|
6
8
|
|
|
7
|
-
//#region src/services/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
//#region src/services/tabs/protocol.d.ts
|
|
10
|
+
|
|
11
|
+
type TabId = string;
|
|
12
|
+
type TabRole$1 = 'solo' | 'leader' | 'follower';
|
|
13
|
+
/** Broker pings every tab at this cadence. */
|
|
14
|
+
|
|
15
|
+
/** Matches `CacheIngestTuple` (modules/cache): exactly what `ingestMany`
|
|
16
|
+
* consumes, so relayed batches feed follower circuits without reshaping. */
|
|
17
|
+
interface IngestTuple {
|
|
18
|
+
table: string;
|
|
19
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
20
|
+
id: string;
|
|
21
|
+
record: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
type FollowerToLeaderMessage = {
|
|
24
|
+
type: 'sync-hello';
|
|
25
|
+
tabId: TabId;
|
|
26
|
+
}
|
|
27
|
+
/** The follower committed an outbox row (through the shared store) and the
|
|
28
|
+
* leader should drain it. Idempotent; a new leader's loadFromDatabase is
|
|
29
|
+
* the backstop for a notify lost in a failover window. */ | {
|
|
30
|
+
type: 'mutation-enqueued';
|
|
31
|
+
mutationId: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'request-poll';
|
|
34
|
+
}
|
|
35
|
+
/** An optimistic write this follower committed to the SHARED store and
|
|
36
|
+
* ingested into its own circuit. The leader ingests it (no DB write, the
|
|
37
|
+
* row is already there) and fans it out to every OTHER follower as
|
|
38
|
+
* `ingest-relay`, so a follower's write lands in every tab in one hop
|
|
39
|
+
* instead of after the server round-trip. */ | {
|
|
40
|
+
type: 'ingest';
|
|
41
|
+
tuples: IngestTuple[];
|
|
11
42
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
type
|
|
21
|
-
[
|
|
22
|
-
|
|
43
|
+
type LeaderToFollowerMessage = {
|
|
44
|
+
type: 'db-ready';
|
|
45
|
+
leadershipId: number;
|
|
46
|
+
bucketId: string;
|
|
47
|
+
storageHealth: StorageHealth;
|
|
48
|
+
}
|
|
49
|
+
/** Every ingest the leader's CacheModule committed, so follower circuits
|
|
50
|
+
* stay live without their own fetch. seq detects gaps. */ | {
|
|
51
|
+
type: 'ingest-relay';
|
|
52
|
+
tuples: IngestTuple[];
|
|
53
|
+
leadershipId: number;
|
|
54
|
+
seq: number;
|
|
55
|
+
}
|
|
56
|
+
/** A `_00_list_ref` LIVE event, relayed verbatim. Each follower resolves the
|
|
57
|
+
* queryId against its own DataModule and ignores foreign queries. */ | {
|
|
58
|
+
type: 'list-ref-change';
|
|
59
|
+
action: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
60
|
+
queryId: string;
|
|
61
|
+
recordId: string;
|
|
62
|
+
version: number;
|
|
63
|
+
parent: boolean;
|
|
64
|
+
}
|
|
65
|
+
/** The leader's drain rolled back a mutation owned by this tab. */ | {
|
|
66
|
+
type: 'mutation-rolled-back';
|
|
67
|
+
mutationId: string;
|
|
68
|
+
recordId: string;
|
|
69
|
+
eventType: 'create' | 'update' | 'delete';
|
|
70
|
+
error: string;
|
|
71
|
+
}
|
|
72
|
+
/** The leader's drain pushed a mutation and deleted its outbox row from the
|
|
73
|
+
* SHARED store. Every follower keeps the row in its overlay until its
|
|
74
|
+
* `_00_list_ref` membership arrives. */ | {
|
|
75
|
+
type: 'mutation-settled';
|
|
76
|
+
mutationId: string;
|
|
77
|
+
recordId: string;
|
|
78
|
+
eventType: 'create' | 'update' | 'delete';
|
|
79
|
+
}
|
|
80
|
+
/** The leader's LIVE saw edges of these queries change; followers re-read
|
|
81
|
+
* membership for the ones they hold. */ | {
|
|
82
|
+
type: 'membership-dirty';
|
|
83
|
+
hashes: string[];
|
|
84
|
+
}
|
|
85
|
+
/** The failed-writes tray changed size on the shared store. */ | {
|
|
86
|
+
type: 'failed-mutations-changed';
|
|
87
|
+
count: number;
|
|
23
88
|
};
|
|
24
|
-
type DatabaseEventSystem = EventSystem<DatabaseEventTypeMap>;
|
|
25
89
|
//#endregion
|
|
26
|
-
//#region src/
|
|
27
|
-
|
|
28
|
-
readonly sql: string;
|
|
29
|
-
readonly extract: (results: unknown[]) => T;
|
|
30
|
-
}
|
|
90
|
+
//#region src/services/logger/index.d.ts
|
|
91
|
+
type Logger$1 = Logger;
|
|
31
92
|
//#endregion
|
|
32
93
|
//#region src/services/database/database.d.ts
|
|
33
94
|
declare abstract class AbstractDatabaseService {
|
|
34
95
|
protected client: Surreal;
|
|
35
96
|
protected logger: Logger$1;
|
|
36
97
|
protected events: DatabaseEventSystem;
|
|
98
|
+
/**
|
|
99
|
+
* Per-query deadline in ms; `0` disables. The remote service sets it from
|
|
100
|
+
* `queryTimeoutMs` (see `RemoteDatabaseService`), the local one from
|
|
101
|
+
* `localOpTimeoutMs` (see `LocalDatabaseService`): a local query can be
|
|
102
|
+
* legitimately slow, but it must never be endless - every query waits on the
|
|
103
|
+
* previous link of {@link query}'s chain, and one that never settled wedged
|
|
104
|
+
* every later local op behind it.
|
|
105
|
+
*/
|
|
106
|
+
protected queryTimeoutMs: number;
|
|
107
|
+
/** The error a deadline expiry rejects with; the local service substitutes
|
|
108
|
+
* its typed `LocalOpTimeoutError`. "timed out" in the message is
|
|
109
|
+
* load-bearing either way: `classifySyncError` keys off it. */
|
|
110
|
+
protected timeoutError(_query: string): Error;
|
|
37
111
|
protected abstract eventType: typeof DatabaseEventTypes.LocalQuery | typeof DatabaseEventTypes.RemoteQuery;
|
|
38
112
|
constructor(client: Surreal, logger: Logger$1, events: DatabaseEventSystem);
|
|
39
113
|
abstract connect(): Promise<void>;
|
|
40
114
|
getClient(): Surreal;
|
|
41
115
|
getEvents(): DatabaseEventSystem;
|
|
42
116
|
tx(): Promise<SurrealTransaction>;
|
|
43
|
-
private queryQueue;
|
|
44
117
|
/**
|
|
45
|
-
*
|
|
118
|
+
* How many statements may be in flight at once.
|
|
119
|
+
*
|
|
120
|
+
* `1` serializes every statement behind the previous one. The local engines
|
|
121
|
+
* need that: overlapping ops trip the WASM / SQLite transaction layer. The
|
|
122
|
+
* remote service raises it (`RemoteDatabaseService`): a SurrealDB socket
|
|
123
|
+
* multiplexes requests by id, and serializing them capped the client at one
|
|
124
|
+
* statement per round trip (~5/s at 170ms RTT), so the `_00_list_ref` poll
|
|
125
|
+
* alone could hold every registration behind it and one slow one-shot read
|
|
126
|
+
* stalled the whole client. The scheduler's `MAX_CONCURRENT_DOWN` only means
|
|
127
|
+
* something when this is above 1.
|
|
128
|
+
*/
|
|
129
|
+
protected maxConcurrentQueries: number;
|
|
130
|
+
private inFlightQueries;
|
|
131
|
+
private queryWaiters;
|
|
132
|
+
/** FIFO slot acquisition, so `maxConcurrentQueries = 1` keeps strict order. */
|
|
133
|
+
private acquireQuerySlot;
|
|
134
|
+
private releaseQuerySlot;
|
|
135
|
+
/**
|
|
136
|
+
* Hook run right before a statement is sent, inside its slot. The remote
|
|
137
|
+
* service uses it to hold statements while a connect is in flight, so a
|
|
138
|
+
* query never runs on a socket that is open but has not had its namespace
|
|
139
|
+
* and token applied yet. The default does nothing.
|
|
140
|
+
*/
|
|
141
|
+
protected beforeQuery(): Promise<void>;
|
|
142
|
+
/**
|
|
143
|
+
* Execute a statement under {@link maxConcurrentQueries}.
|
|
144
|
+
*
|
|
145
|
+
* A statement that never settles would hold its slot forever, and with a
|
|
146
|
+
* limit of 1 that blocks every later statement behind it. {@link
|
|
147
|
+
* queryTimeoutMs} bounds each one: on expiry this promise rejects and the
|
|
148
|
+
* slot is released, even though the underlying RPC is still parked in the
|
|
149
|
+
* SDK's pending map.
|
|
46
150
|
*/
|
|
47
151
|
query<T extends unknown[]>(query: string, vars?: Record<string, unknown>): Promise<T>;
|
|
152
|
+
/**
|
|
153
|
+
* Like {@link query} but never rejects on a statement error: every
|
|
154
|
+
* statement answers on its own as `{ status: 'OK', result }` or
|
|
155
|
+
* `{ status: 'ERR', error }`. This is what lets a batched outbox push judge
|
|
156
|
+
* each mutation separately.
|
|
157
|
+
*/
|
|
158
|
+
queryResponses(query: string, vars?: Record<string, unknown>): Promise<Array<{
|
|
159
|
+
status: 'OK';
|
|
160
|
+
result: unknown;
|
|
161
|
+
} | {
|
|
162
|
+
status: 'ERR';
|
|
163
|
+
error: string;
|
|
164
|
+
}>>;
|
|
165
|
+
private runStatement;
|
|
48
166
|
execute<T>(query: SealedQuery<T>, vars?: Record<string, unknown>): Promise<T>;
|
|
49
167
|
close(): Promise<void>;
|
|
50
168
|
}
|
|
51
169
|
//#endregion
|
|
52
|
-
//#region src/services/database/local.d.ts
|
|
53
|
-
declare class LocalDatabaseService extends AbstractDatabaseService {
|
|
54
|
-
private config;
|
|
55
|
-
protected eventType: "DATABASE_LOCAL_QUERY";
|
|
56
|
-
constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
|
|
57
|
-
getConfig(): SpookyConfig<any>['database'];
|
|
58
|
-
connect(): Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
//#endregion
|
|
61
170
|
//#region src/services/database/remote.d.ts
|
|
171
|
+
/** Transport events the SDK publishes, mapped 1:1 to {@link ConnectionState}. */
|
|
172
|
+
type RemoteConnectionEvent = ConnectionState | 'error';
|
|
62
173
|
declare class RemoteDatabaseService extends AbstractDatabaseService {
|
|
63
174
|
private config;
|
|
64
175
|
protected eventType: "DATABASE_REMOTE_QUERY";
|
|
65
|
-
|
|
66
|
-
|
|
176
|
+
private readonly reconnectConfig;
|
|
177
|
+
/**
|
|
178
|
+
* In-flight `connect()`, so concurrent callers (boot + supervisor revive +
|
|
179
|
+
* an `online` event landing at the same moment) share one attempt instead of
|
|
180
|
+
* racing two sockets. Cleared on settle, so a later call always reconnects.
|
|
181
|
+
*/
|
|
182
|
+
private connecting;
|
|
183
|
+
/**
|
|
184
|
+
* The token to re-authenticate a freshly opened socket with, kept current by
|
|
185
|
+
* {@link setAuthToken}.
|
|
186
|
+
*
|
|
187
|
+
* `config.token` is fixed at construction and most apps never set it: they
|
|
188
|
+
* sign in later, which authenticates the socket that happens to be open at
|
|
189
|
+
* the time. That is enough for the SDK's OWN reconnects (it replays
|
|
190
|
+
* `version`/`use`/`authenticate` itself), but not for the supervisor's revive
|
|
191
|
+
* loop, which builds a socket from scratch. Without this the revived socket
|
|
192
|
+
* came back UNAUTHENTICATED and stayed that way for the life of the page,
|
|
193
|
+
* while the client's `currentUserId` — restored from local storage — kept
|
|
194
|
+
* reporting the user as signed in.
|
|
195
|
+
*
|
|
196
|
+
* The visible damage is silent and total: `fn::query::register` sends
|
|
197
|
+
* `<string>($auth.id OR '')`, so every view registered afterwards is stamped
|
|
198
|
+
* with an empty identity, and every `$auth.id` predicate in it resolves
|
|
199
|
+
* false. Public tables keep returning rows while everything owned by the user
|
|
200
|
+
* returns nothing, on a page that still looks signed in.
|
|
201
|
+
*/
|
|
202
|
+
private authToken;
|
|
203
|
+
constructor(config: Sp00kyConfig<any>['database'], logger: Logger$1);
|
|
204
|
+
getConfig(): Sp00kyConfig<any>['database'];
|
|
205
|
+
/**
|
|
206
|
+
* Send one SurrealQL statement so that it survives the page going away.
|
|
207
|
+
*
|
|
208
|
+
* A WebSocket `send()` during `pagehide` is not guaranteed to flush — the
|
|
209
|
+
* browser may tear the socket down first, and the frame is simply lost.
|
|
210
|
+
* Measured: an unload-time release over the live socket reached the server
|
|
211
|
+
* zero times out of one. `fetch` with `keepalive` is the primitive the
|
|
212
|
+
* platform actually guarantees here, so this goes over SurrealDB's HTTP
|
|
213
|
+
* `/sql` endpoint instead of the RPC socket.
|
|
214
|
+
*
|
|
215
|
+
* Best-effort by design: no await, no retry, errors swallowed. Every caller
|
|
216
|
+
* must have a server-side fallback that makes a lost beacon a non-event.
|
|
217
|
+
*/
|
|
218
|
+
beaconSql(sql: string): void;
|
|
219
|
+
/**
|
|
220
|
+
* Record the token every future connect should authenticate with, or `null`
|
|
221
|
+
* on sign-out. See {@link authToken}.
|
|
222
|
+
*
|
|
223
|
+
* Does not touch the CURRENT socket: callers authenticate that themselves
|
|
224
|
+
* (sign-in and session restore both already do). This only makes the next
|
|
225
|
+
* from-scratch connect reproduce that state.
|
|
226
|
+
*/
|
|
227
|
+
setAuthToken(token: string | null): void;
|
|
228
|
+
/** Resolved reconnect tunables; the supervisor reads its own knobs here. */
|
|
229
|
+
getReconnectConfig(): Required<ReconnectConfig>;
|
|
230
|
+
/** Current transport state as reported by the SDK. */
|
|
231
|
+
getStatus(): ConnectionState;
|
|
232
|
+
/**
|
|
233
|
+
* Observe transport events. Thin passthrough so callers (the supervisor,
|
|
234
|
+
* sync, CRDT) don't have to reach through `getClient()`.
|
|
235
|
+
*/
|
|
236
|
+
subscribeConnection<K extends RemoteConnectionEvent>(event: K, cb: (...payload: SurrealEvents[K]) => void): () => void;
|
|
237
|
+
/**
|
|
238
|
+
* Tear the socket down on purpose. Used by the heartbeat watchdog when a
|
|
239
|
+
* socket stops answering but never closes: `close()` makes the SDK publish
|
|
240
|
+
* `disconnected`, which is what drives the supervisor's revive loop.
|
|
241
|
+
*/
|
|
242
|
+
forceClose(): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* Hold statements while a connect is in flight.
|
|
245
|
+
*
|
|
246
|
+
* `connect()` opens the socket, then selects the namespace/database, then
|
|
247
|
+
* authenticates - three round trips. The SDK considers the connection ready
|
|
248
|
+
* after the first, so a query queued during the other two ran on a socket
|
|
249
|
+
* with no namespace ("Specify a namespace to use") or, worse, no identity.
|
|
250
|
+
* Seen 2026-09-06 after the supervisor rebuilt a socket: a collection
|
|
251
|
+
* recount failed with exactly that error. Waiting on the in-flight connect
|
|
252
|
+
* costs nothing when no connect is running; a failed connect is not
|
|
253
|
+
* swallowed here - the query then fails on its own, as before.
|
|
254
|
+
*/
|
|
255
|
+
protected beforeQuery(): Promise<void>;
|
|
256
|
+
/**
|
|
257
|
+
* Open (or re-open) the remote connection.
|
|
258
|
+
*
|
|
259
|
+
* Safe to call repeatedly: concurrent calls share the in-flight attempt, and
|
|
260
|
+
* a call after a `disconnected` builds a fresh socket. `use()` and
|
|
261
|
+
* `authenticate()` are re-applied here for the cold path; the SDK also
|
|
262
|
+
* replays them itself on its own internal reconnects.
|
|
263
|
+
*/
|
|
67
264
|
connect(): Promise<void>;
|
|
265
|
+
private doConnect;
|
|
68
266
|
signin(params: any): Promise<any>;
|
|
69
267
|
signup(params: any): Promise<any>;
|
|
70
268
|
authenticate(token: string): Promise<any>;
|
|
71
269
|
invalidate(): Promise<void>;
|
|
72
270
|
}
|
|
73
271
|
//#endregion
|
|
272
|
+
//#region src/services/database/connection-supervisor.d.ts
|
|
273
|
+
/**
|
|
274
|
+
* Keeps the remote WebSocket alive for the whole life of the page.
|
|
275
|
+
*
|
|
276
|
+
* The SurrealDB SDK reconnects on its own after a socket `close`, but that
|
|
277
|
+
* covers only one of three ways the connection dies:
|
|
278
|
+
*
|
|
279
|
+
* 1. **Socket closes, SDK recovers.** Handled entirely by the SDK. This
|
|
280
|
+
* supervisor only observes it (to report `reconnecting` upward).
|
|
281
|
+
* 2. **Socket closes, SDK gives up.** With `attempts: -1` this shouldn't happen
|
|
282
|
+
* from exhaustion — but the SDK also terminates the engine permanently when
|
|
283
|
+
* its post-reconnect handshake throws (it re-runs `version()`, `use()`,
|
|
284
|
+
* `authenticate()` on every reconnect and closes the engine on any error).
|
|
285
|
+
* One transient hiccup there would otherwise kill the page's connection for
|
|
286
|
+
* good. The revive loop re-opens from scratch.
|
|
287
|
+
* 3. **Socket never closes at all.** A half-open connection: the peer is gone
|
|
288
|
+
* (NAT timeout, wifi switch, laptop sleep) but no FIN ever arrives, so
|
|
289
|
+
* `readyState` stays OPEN and the SDK's own 30s ping — fire-and-forget, no
|
|
290
|
+
* response deadline — never notices. Nothing ever fires a `close` event, so
|
|
291
|
+
* nothing ever triggers a reconnect. The heartbeat detects this and forces
|
|
292
|
+
* the teardown that case 2's loop then repairs.
|
|
293
|
+
*
|
|
294
|
+
* Plus wake triggers: coming back `online` or un-hiding the tab probes
|
|
295
|
+
* immediately rather than waiting out a backoff that was scheduled while the
|
|
296
|
+
* network was known-down.
|
|
297
|
+
*/
|
|
298
|
+
declare class ConnectionSupervisor {
|
|
299
|
+
private readonly remote;
|
|
300
|
+
private readonly logger;
|
|
301
|
+
private readonly config;
|
|
302
|
+
private state;
|
|
303
|
+
private subscribers;
|
|
304
|
+
private started;
|
|
305
|
+
private disposed;
|
|
306
|
+
private heartbeatTimer;
|
|
307
|
+
private heartbeatInFlight;
|
|
308
|
+
/** Consecutive failed probes. See {@link FAILURES_BEFORE_TEARDOWN}. */
|
|
309
|
+
private heartbeatFailures;
|
|
310
|
+
private reviveTimer;
|
|
311
|
+
private reviveAttempts;
|
|
312
|
+
/** Timestamp of the last wake-triggered probe, for rate limiting. */
|
|
313
|
+
private lastWakeProbeAt;
|
|
314
|
+
private reviving;
|
|
315
|
+
/**
|
|
316
|
+
* Set while the browser reports itself offline. Retrying a socket against a
|
|
317
|
+
* down interface only burns backoff, so the loop parks until `online` fires.
|
|
318
|
+
*/
|
|
319
|
+
private suspended;
|
|
320
|
+
private teardown;
|
|
321
|
+
private static readonly REVIVE_BASE_MS;
|
|
322
|
+
/**
|
|
323
|
+
* How many consecutive heartbeat failures it takes to tear the socket down.
|
|
324
|
+
*
|
|
325
|
+
* The probe rides the same serialized queue as every other RPC (deliberately
|
|
326
|
+
* — see {@link beat}), which means it cannot distinguish a WEDGED queue from
|
|
327
|
+
* a merely BUSY one. A single slow window (a large sync burst, one heavy
|
|
328
|
+
* app query) used to be enough to force-close a perfectly healthy socket,
|
|
329
|
+
* and the resulting reconnect re-registered every active query about a
|
|
330
|
+
* second later. That self-inflicted teardown manufactured the very reconnect
|
|
331
|
+
* storms this class exists to survive. A genuinely dead socket still fails
|
|
332
|
+
* every probe, so it is torn down one interval later than before.
|
|
333
|
+
*/
|
|
334
|
+
private static readonly FAILURES_BEFORE_TEARDOWN;
|
|
335
|
+
/** Retry delay after an inconclusive (first) heartbeat failure. */
|
|
336
|
+
private static readonly HEARTBEAT_RETRY_MS;
|
|
337
|
+
/** Floor between probes triggered by wake events (tab focus, pageshow). */
|
|
338
|
+
private static readonly WAKE_PROBE_MIN_INTERVAL_MS;
|
|
339
|
+
constructor(remote: RemoteDatabaseService, logger: Logger$1, config?: Required<ReconnectConfig>);
|
|
340
|
+
/** Latest observed transport state. */
|
|
341
|
+
get connection(): ConnectionState;
|
|
342
|
+
/**
|
|
343
|
+
* Observe transport state. Fires immediately with the current value and again
|
|
344
|
+
* on every change. Returns an unsubscribe.
|
|
345
|
+
*/
|
|
346
|
+
subscribe(cb: (state: ConnectionState) => void): () => void;
|
|
347
|
+
/**
|
|
348
|
+
* Begin supervising. Call once, after the initial {@link
|
|
349
|
+
* RemoteDatabaseService.connect}. Idempotent.
|
|
350
|
+
*/
|
|
351
|
+
start(): void;
|
|
352
|
+
/** Stop all timers and listeners. Safe to call more than once. */
|
|
353
|
+
dispose(): void;
|
|
354
|
+
private setState;
|
|
355
|
+
private clearReviveTimer;
|
|
356
|
+
/**
|
|
357
|
+
* Queue the next `connect()` attempt on exponential backoff, capped at
|
|
358
|
+
* `superviseRetryDelayMaxMs`. Never gives up — the page is expected to
|
|
359
|
+
* outlive any outage.
|
|
360
|
+
*/
|
|
361
|
+
private scheduleRevive;
|
|
362
|
+
private revive;
|
|
363
|
+
private stopHeartbeat;
|
|
364
|
+
private startHeartbeat;
|
|
365
|
+
/**
|
|
366
|
+
* Probe the server end-to-end. Deliberately goes through
|
|
367
|
+
* `remote.query` — the same serialized queue every other remote call uses —
|
|
368
|
+
* so a queue wedged behind a stuck RPC also fails the heartbeat instead of
|
|
369
|
+
* being invisible to it.
|
|
370
|
+
*/
|
|
371
|
+
private beat;
|
|
372
|
+
/**
|
|
373
|
+
* A restored network or an un-hidden tab is the strongest available hint that
|
|
374
|
+
* a reconnect will now succeed, so probe immediately instead of waiting out a
|
|
375
|
+
* backoff scheduled under worse conditions.
|
|
376
|
+
*/
|
|
377
|
+
private installWakeTriggers;
|
|
378
|
+
/**
|
|
379
|
+
* Reset the backoff and act on whichever problem is present: reconnect if the
|
|
380
|
+
* socket is gone, otherwise probe it (it may be half-open — which is exactly
|
|
381
|
+
* what a sleep/wake cycle produces).
|
|
382
|
+
*/
|
|
383
|
+
private wake;
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region src/services/database/local-migrator.d.ts
|
|
387
|
+
declare class LocalMigrator {
|
|
388
|
+
private localDb;
|
|
389
|
+
private logger;
|
|
390
|
+
constructor(localDb: LocalStore, logger: Logger$1);
|
|
391
|
+
provision(schemaSurql: string): Promise<void>;
|
|
392
|
+
private isSchemaUpToDate;
|
|
393
|
+
private recreateDatabase;
|
|
394
|
+
private splitStatements;
|
|
395
|
+
private createHashRecord;
|
|
396
|
+
}
|
|
397
|
+
//#endregion
|
|
74
398
|
//#region src/services/stream-processor/wasm-types.d.ts
|
|
75
399
|
interface WasmStreamUpdate {
|
|
76
400
|
query_id: string;
|
|
77
401
|
result_hash: string;
|
|
78
402
|
result_data: RecordVersionArray;
|
|
403
|
+
timing_store_apply_ms?: number;
|
|
404
|
+
timing_circuit_step_ms?: number;
|
|
405
|
+
timing_transform_ms?: number;
|
|
406
|
+
timing_parse_ms?: number;
|
|
407
|
+
timing_plan_ms?: number;
|
|
408
|
+
timing_snapshot_ms?: number;
|
|
79
409
|
}
|
|
80
410
|
//#endregion
|
|
81
411
|
//#region src/services/stream-processor/index.d.ts
|
|
@@ -96,6 +426,33 @@ interface StreamUpdate {
|
|
|
96
426
|
queryHash: string;
|
|
97
427
|
localArray: RecordVersionArray;
|
|
98
428
|
op?: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
429
|
+
/**
|
|
430
|
+
* Client-internal: not from the circuit. A membership-only change that
|
|
431
|
+
* needed no fetch is re-materialized through this same path so it cannot
|
|
432
|
+
* race a real update (DataModule.scheduleRematerialize). Carries the last
|
|
433
|
+
* known `localArray`; consumers that describe an INGEST (persist, metrics,
|
|
434
|
+
* devtools events) skip it.
|
|
435
|
+
*/
|
|
436
|
+
synthetic?: boolean;
|
|
437
|
+
/**
|
|
438
|
+
* End-to-end ingest latency for the WASM call that produced this update,
|
|
439
|
+
* in milliseconds. Populated by StreamProcessorService.ingest. Undefined
|
|
440
|
+
* for the initial register_view snapshot.
|
|
441
|
+
*/
|
|
442
|
+
materializationTimeMs?: number;
|
|
443
|
+
/** SSP internal sub-phase timings (ms) for this ingest, from the WASM binding. */
|
|
444
|
+
storeApplyMs?: number;
|
|
445
|
+
circuitStepMs?: number;
|
|
446
|
+
transformMs?: number;
|
|
447
|
+
/**
|
|
448
|
+
* One-shot registration timings (ms). Only set on the StreamUpdate returned
|
|
449
|
+
* by `registerQueryPlan` (the register_view snapshot), not on ingest updates.
|
|
450
|
+
*/
|
|
451
|
+
registration?: {
|
|
452
|
+
parseMs: number;
|
|
453
|
+
planMs: number;
|
|
454
|
+
snapshotMs: number;
|
|
455
|
+
};
|
|
99
456
|
}
|
|
100
457
|
type StreamProcessorEvents = {
|
|
101
458
|
stream_update: EventDefinition<'stream_update', StreamUpdate[]>;
|
|
@@ -107,34 +464,204 @@ type StreamProcessorEvents = {
|
|
|
107
464
|
interface StreamUpdateReceiver {
|
|
108
465
|
onStreamUpdate(update: StreamUpdate): void;
|
|
109
466
|
}
|
|
467
|
+
/** One row change in the shape `ingestMany` consumes. */
|
|
468
|
+
interface IngestRecord {
|
|
469
|
+
table: string;
|
|
470
|
+
/** `MERGE` overlays the given fields on the stored row (projection widening). */
|
|
471
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE' | 'MERGE';
|
|
472
|
+
id: string;
|
|
473
|
+
record: any;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* What the boot-time prime needs from the client: which tables to walk, how
|
|
477
|
+
* to recognise a snapshot written under a different schema, and which rows'
|
|
478
|
+
* local `_00_rv` must not be reported as the server's.
|
|
479
|
+
*/
|
|
480
|
+
interface CircuitPrimeContext {
|
|
481
|
+
tables: string[];
|
|
482
|
+
schemaHash: string;
|
|
483
|
+
/** Encoded ids with an unsettled local mutation (their `_00_rv` was bumped
|
|
484
|
+
* locally and may exceed the server's next version). */
|
|
485
|
+
pendingIds: Set<string>;
|
|
486
|
+
/** Receives every `(id, rv)` the prime put into the circuit, per table, so
|
|
487
|
+
* the sync layer can skip re-downloading bodies it already has. */
|
|
488
|
+
onVersions?: (table: string, entries: [string, number][]) => void;
|
|
489
|
+
}
|
|
490
|
+
/** Storage key of the circuit snapshot inside the local store. */
|
|
491
|
+
|
|
110
492
|
declare class StreamProcessorService {
|
|
111
493
|
events: EventSystem<StreamProcessorEvents>;
|
|
112
494
|
private db;
|
|
113
|
-
private persistenceClient;
|
|
114
495
|
private logger;
|
|
115
496
|
private processor;
|
|
116
497
|
private isInitialized;
|
|
117
498
|
private receivers;
|
|
118
|
-
|
|
499
|
+
private batching;
|
|
500
|
+
private batchBuffer;
|
|
501
|
+
private sessionAuth;
|
|
502
|
+
private stateGeneration;
|
|
503
|
+
private persistState;
|
|
504
|
+
private persistCircuit;
|
|
505
|
+
private checkpointMs;
|
|
506
|
+
private checkpointTimer;
|
|
507
|
+
private snapshotDirty;
|
|
508
|
+
private dirtyRows;
|
|
509
|
+
private hideHandler;
|
|
510
|
+
private checkpointInFlight;
|
|
511
|
+
private projection;
|
|
512
|
+
private primed;
|
|
513
|
+
private schemaHash;
|
|
514
|
+
private widenQueue;
|
|
515
|
+
private widenPending;
|
|
516
|
+
constructor(events: EventSystem<StreamProcessorEvents>, db: LocalStore, logger: Logger);
|
|
119
517
|
/**
|
|
120
518
|
* Add a receiver for stream updates.
|
|
121
519
|
* Multiple receivers can be registered (DataManager, DevTools, etc.)
|
|
122
520
|
*/
|
|
123
521
|
addReceiver(receiver: StreamUpdateReceiver): void;
|
|
124
522
|
private notifyUpdates;
|
|
523
|
+
private dispatchUpdates;
|
|
524
|
+
/**
|
|
525
|
+
* Ingest a batch of record changes, firing one coalesced `StreamUpdate` per
|
|
526
|
+
* affected query once every record has been ingested. Use this whenever
|
|
527
|
+
* multiple records land at once (sync fetching N rows, the boot prime).
|
|
528
|
+
*
|
|
529
|
+
* The batch is fed to the wasm side in chunks of {@link INGEST_CHUNK}: one
|
|
530
|
+
* circuit step per chunk (a step walks every registered view, so per-record
|
|
531
|
+
* ingest paid that fixed cost N times), but never the whole batch at once,
|
|
532
|
+
* because the wasm side has to hold every parsed row of a call at the same
|
|
533
|
+
* time and wasm32 dlmalloc never returns that peak.
|
|
534
|
+
*
|
|
535
|
+
* Returns the records that were ingested. A chunk that fails is reported
|
|
536
|
+
* and skipped, not retried (a retry would double-apply whatever the failed
|
|
537
|
+
* step already committed), and the remaining chunks still run.
|
|
538
|
+
*/
|
|
539
|
+
ingestMany(records: IngestRecord[]): IngestRecord[];
|
|
540
|
+
/**
|
|
541
|
+
* Open a coalescing window. While open, the per-record stream updates
|
|
542
|
+
* emitted by `ingest` are buffered (one entry per queryHash) instead of
|
|
543
|
+
* dispatched. Always paired with `flushCoalescing()` in a try/finally by
|
|
544
|
+
* `ingestMany` so the window always closes — otherwise the processor stays
|
|
545
|
+
* stuck buffering forever.
|
|
546
|
+
*
|
|
547
|
+
* No-op if a window is already open (nested batches aren't expected here).
|
|
548
|
+
*/
|
|
549
|
+
private beginCoalescing;
|
|
550
|
+
/**
|
|
551
|
+
* Close the coalescing window and flush: dispatch one coalesced
|
|
552
|
+
* `StreamUpdate` per buffered queryHash, then persist processor state once
|
|
553
|
+
* for the whole batch (instead of once per ingest).
|
|
554
|
+
*/
|
|
555
|
+
private flushCoalescing;
|
|
125
556
|
/**
|
|
126
557
|
* Initialize the WASM module and processor.
|
|
127
558
|
* This must be called before using other methods.
|
|
128
559
|
*/
|
|
129
560
|
init(): Promise<void>;
|
|
130
|
-
|
|
131
|
-
|
|
561
|
+
/**
|
|
562
|
+
* Drop the current WASM processor and start a fresh, empty circuit. Used on
|
|
563
|
+
* local-bucket switches: the old circuit holds the previous user's rows AND
|
|
564
|
+
* views registered with the previous `$auth` context, so neither may survive.
|
|
565
|
+
* Deliberately loads nothing: the snapshot in the store being swapped away
|
|
566
|
+
* from belongs to the previous bucket; the caller primes the new bucket's
|
|
567
|
+
* circuit (`primeFromLocal`) once its store is open, and the DataModule
|
|
568
|
+
* rebind re-registers every live view against this fresh processor. Caller
|
|
569
|
+
* must re-seed `setPermissions` afterwards (a fresh circuit default-denies
|
|
570
|
+
* every table).
|
|
571
|
+
*/
|
|
572
|
+
reset(): Promise<void>;
|
|
573
|
+
/**
|
|
574
|
+
* Release the wasm circuit and stop checkpointing. Call when the client is
|
|
575
|
+
* torn down; a recreated client (provider remount, HMR) would otherwise stack
|
|
576
|
+
* one full circuit per instance.
|
|
577
|
+
*/
|
|
578
|
+
dispose(): void;
|
|
579
|
+
/**
|
|
580
|
+
* Explicitly run the wasm-bindgen destructor. Guarded: stale wasm builds may
|
|
581
|
+
* not expose `free`, and a double free must not take the app down.
|
|
582
|
+
*/
|
|
583
|
+
private freeProcessor;
|
|
584
|
+
/** Toggle circuit-state persistence (shared-tabs follower/leader role). */
|
|
585
|
+
setPersistenceEnabled(enabled: boolean): void;
|
|
586
|
+
/**
|
|
587
|
+
* Snapshot persistence (`persistCircuit`). When on, the circuit's store is
|
|
588
|
+
* written to the local store on a checkpoint interval and when the page
|
|
589
|
+
* goes hidden, and restored by {@link primeFromLocal} on the next boot.
|
|
590
|
+
*/
|
|
591
|
+
configureCircuitPersistence(enabled: boolean, checkpointMs?: number): void;
|
|
592
|
+
/**
|
|
593
|
+
* Field projection (`circuitProjection`, default on). Takes effect on the
|
|
594
|
+
* next processor (`init`/`reset`) and on rows written after that.
|
|
595
|
+
*/
|
|
596
|
+
configureProjection(enabled: boolean): void;
|
|
597
|
+
private applyProjection;
|
|
598
|
+
/** Resolves once the boot-time prime has finished (or was skipped). */
|
|
599
|
+
whenPrimed(): Promise<void>;
|
|
600
|
+
/**
|
|
601
|
+
* Fill the circuit from the LOCAL store, in the background.
|
|
602
|
+
*
|
|
603
|
+
* With a usable snapshot: install it under whatever views have registered
|
|
604
|
+
* meanwhile (`load_store_state` re-primes them), then `reconcile` each table
|
|
605
|
+
* against the store's `(id, rv)` list so rows deleted since the checkpoint
|
|
606
|
+
* are stepped out and only rows added or changed since are read back and
|
|
607
|
+
* ingested. Without one: read every row and ingest it, chunked.
|
|
608
|
+
*
|
|
609
|
+
* Either way the circuit ends up equal to the local store without touching
|
|
610
|
+
* the network, so the first sync diff is a real delta rather than "fetch
|
|
611
|
+
* everything". The returned promise never rejects; `whenPrimed` gates on it.
|
|
612
|
+
*/
|
|
613
|
+
primeFromLocal(ctx: CircuitPrimeContext): Promise<void>;
|
|
614
|
+
private runPrime;
|
|
615
|
+
/** Publish wasm updates produced outside an ingest (restore, reconcile). */
|
|
616
|
+
private dispatchWasmUpdates;
|
|
617
|
+
/**
|
|
618
|
+
* Record that the circuit changed by `rows` rows. Cheap; the snapshot is
|
|
619
|
+
* deferred to the checkpoint timer and skipped entirely when `persistCircuit`
|
|
620
|
+
* is off.
|
|
621
|
+
*/
|
|
622
|
+
private markSnapshotDirty;
|
|
623
|
+
private startCheckpoints;
|
|
624
|
+
/** Stop checkpointing and drop the visibility listeners. */
|
|
625
|
+
stopCheckpoints(): void;
|
|
626
|
+
/**
|
|
627
|
+
* Write the circuit's store to the local store as a snapshot. Compacts the
|
|
628
|
+
* row arena first when dead bytes outweigh live ones. Serialised: a second
|
|
629
|
+
* call while one is in flight joins it. No-op unless persistence is on, this
|
|
630
|
+
* tab owns the store, and the engine can hold a snapshot.
|
|
631
|
+
*/
|
|
632
|
+
checkpoint(reason?: string): Promise<void>;
|
|
633
|
+
private runCheckpoint;
|
|
634
|
+
/**
|
|
635
|
+
* Projection widening: a newly registered view evaluates fields that rows
|
|
636
|
+
* already in the circuit were stored without. Merge just those fields in,
|
|
637
|
+
* table by table, from the local store. The view registered against what
|
|
638
|
+
* was present and converges as the merges step through.
|
|
639
|
+
*/
|
|
640
|
+
private scheduleWiden;
|
|
641
|
+
private runWiden;
|
|
642
|
+
/**
|
|
643
|
+
* Seed per-table `select` permission predicates ({ [table]: whereText }).
|
|
644
|
+
* Must run after the processor exists and before any `register_view`, else
|
|
645
|
+
* non-`_00_` tables are default-denied and registration fails.
|
|
646
|
+
*/
|
|
647
|
+
setPermissions(permissions: Record<string, string>): void;
|
|
648
|
+
/**
|
|
649
|
+
* Set the current session's auth identity for permission injection,
|
|
650
|
+
* mirroring the server's `fn::query::register`
|
|
651
|
+
* (`object::extend(params, { auth: { id: $auth.id }, access: $access })`).
|
|
652
|
+
* Stored as strings (empty when logged out) and applied to every
|
|
653
|
+
* `register_view` in {@link registerQueryPlan}. Must be set before a
|
|
654
|
+
* `$auth`-gated query registers (and re-set on auth state changes), or the
|
|
655
|
+
* in-browser SSP's `permission_inject` rejects it with
|
|
656
|
+
* "requires $auth but registration params lack it".
|
|
657
|
+
*/
|
|
658
|
+
setSessionAuth(authId: string | null, access: string | null): void;
|
|
132
659
|
/**
|
|
133
660
|
* Ingest a record change into the processor.
|
|
134
661
|
* Emits 'stream_update' event if materialized views are affected.
|
|
135
662
|
* @param isOptimistic true = local mutation (increment versions), false = remote sync (keep versions)
|
|
136
663
|
*/
|
|
137
|
-
ingest(table: string, op: '
|
|
664
|
+
ingest(table: string, op: IngestRecord['op'], id: string, record: any): WasmStreamUpdate[];
|
|
138
665
|
/**
|
|
139
666
|
* Register a new query plan.
|
|
140
667
|
* Emits 'stream_update' with the initial result.
|
|
@@ -169,6 +696,13 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
169
696
|
token: string | null;
|
|
170
697
|
currentUser: any | null;
|
|
171
698
|
isAuthenticated: boolean;
|
|
699
|
+
/**
|
|
700
|
+
* The record-access method name for the current session (e.g. `"account"`),
|
|
701
|
+
* derived from the token's `AC` claim. Consumed by the in-browser SSP's
|
|
702
|
+
* permission injection so `$access`-gated table predicates resolve locally,
|
|
703
|
+
* mirroring the server's `$access`. Null when logged out.
|
|
704
|
+
*/
|
|
705
|
+
access: string | null;
|
|
172
706
|
isLoading: boolean;
|
|
173
707
|
private events;
|
|
174
708
|
get eventSystem(): AuthEventSystem;
|
|
@@ -181,6 +715,24 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
181
715
|
*/
|
|
182
716
|
subscribe(cb: (userId: string | null) => void): () => void;
|
|
183
717
|
private notifyListeners;
|
|
718
|
+
/**
|
|
719
|
+
* Restore a session from the locally cached JWT, with NO network.
|
|
720
|
+
*
|
|
721
|
+
* This is what makes a warm boot paint instantly and what makes an offline
|
|
722
|
+
* boot possible at all: the token is in local storage, and it already carries
|
|
723
|
+
* both the access method and the `$auth.id` record id. Everything the client
|
|
724
|
+
* needs to route queries (`setCurrentUserId`) and to satisfy `$auth`-gated
|
|
725
|
+
* permission predicates in the in-browser SSP (`setSessionAuth`) is therefore
|
|
726
|
+
* available before a socket exists.
|
|
727
|
+
*
|
|
728
|
+
* The session is OPTIMISTIC: the token is unverified here. `check()` runs
|
|
729
|
+
* afterwards in the background and downgrades to a real sign-out if the
|
|
730
|
+
* server rejects it. Nothing is trusted that the server has not also seen -
|
|
731
|
+
* the local store only ever holds rows the server previously sent.
|
|
732
|
+
*
|
|
733
|
+
* Returns the restored user id, or null when there is no usable token.
|
|
734
|
+
*/
|
|
735
|
+
restoreSessionFromToken(): Promise<string | null>;
|
|
184
736
|
/**
|
|
185
737
|
* Check for existing session and validate
|
|
186
738
|
*/
|
|
@@ -190,17 +742,1471 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
190
742
|
*/
|
|
191
743
|
signOut(): Promise<void>;
|
|
192
744
|
private setSession;
|
|
745
|
+
/** Fallback when the token carries no `AC` claim: if the schema defines
|
|
746
|
+
* exactly one record-access method, assume the session used it. */
|
|
747
|
+
private defaultAccessName;
|
|
193
748
|
signUp<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signup'>): Promise<void>;
|
|
194
749
|
signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
|
|
195
750
|
}
|
|
196
751
|
//#endregion
|
|
197
|
-
//#region src/
|
|
752
|
+
//#region src/modules/crdt/crdt-field.d.ts
|
|
753
|
+
declare const CURSOR_COLORS: string[];
|
|
754
|
+
declare function cursorColorFromName(name: string): string;
|
|
755
|
+
declare class CrdtField {
|
|
756
|
+
private fieldName;
|
|
757
|
+
private doc;
|
|
758
|
+
private pushTimer;
|
|
759
|
+
private local;
|
|
760
|
+
private remote;
|
|
761
|
+
private recordId;
|
|
762
|
+
private sessionId;
|
|
763
|
+
private unsubscribe;
|
|
764
|
+
private lastPushTime;
|
|
765
|
+
private lastCursorPushTime;
|
|
766
|
+
private loadedFromCrdt;
|
|
767
|
+
private pushRetryCount;
|
|
768
|
+
private logger;
|
|
769
|
+
private cursorsEnabled;
|
|
770
|
+
/** Remote-push debounce. Local writes happen immediately on every Loro
|
|
771
|
+
* update; the remote UPSERT is coalesced over this window. Configured
|
|
772
|
+
* via `Sp00kyConfig.crdtDebounceMs`, default 500. */
|
|
773
|
+
private remoteDebounceMs;
|
|
774
|
+
private _onCursorUpdate;
|
|
775
|
+
private pendingCursorUpdate;
|
|
776
|
+
/** Callback set by the editor to receive remote cursor updates.
|
|
777
|
+
* Any cursor data that arrived before this callback was set will be replayed. */
|
|
778
|
+
set onCursorUpdate(cb: ((data: Uint8Array) => void) | null);
|
|
779
|
+
get onCursorUpdate(): ((data: Uint8Array) => void) | null;
|
|
780
|
+
/**
|
|
781
|
+
* @param LoroDocClass the `LoroDoc` constructor, injected by the caller after
|
|
782
|
+
* awaiting {@link loadLoro} — keeps `loro-crdt` out of this module's static
|
|
783
|
+
* import graph so it only ships to apps that use CRDT fields.
|
|
784
|
+
*/
|
|
785
|
+
constructor(fieldName: string, cursorsEnabled: boolean, LoroDocClass: typeof LoroDoc, initialState?: Uint8Array, logger?: Logger$1 | null);
|
|
786
|
+
getDoc(): LoroDoc;
|
|
787
|
+
/** Whether the LoroDoc was loaded from saved CRDT state */
|
|
788
|
+
hasContent(): boolean;
|
|
789
|
+
startSync(local: LocalStore, remote: RemoteDatabaseService, recordId: string, sessionId: string, debounceMs: number): void;
|
|
790
|
+
/**
|
|
791
|
+
* Stop syncing this field. Flushes one final remote push by default so the
|
|
792
|
+
* last keystrokes aren't lost. Pass `{ flush: false }` on a bucket switch —
|
|
793
|
+
* the remote session already belongs to the NEXT user, and pushing this
|
|
794
|
+
* (previous user's) snapshot under it would clobber the record remotely.
|
|
795
|
+
*/
|
|
796
|
+
stopSync(options?: {
|
|
797
|
+
flush?: boolean;
|
|
798
|
+
}): void;
|
|
799
|
+
importRemote(state: Uint8Array): void;
|
|
800
|
+
exportSnapshot(): Uint8Array;
|
|
801
|
+
/** Push this session's cursor blob into the parent row at
|
|
802
|
+
* `<field>.cursors[$sid]`. No-op when cursors aren't enabled on this
|
|
803
|
+
* field — the editor still calls this method optimistically, but
|
|
804
|
+
* without `@cursor` on the schema there's nowhere to store the blob.
|
|
805
|
+
* The UPDATE itself fires the parent table's LIVE feed, so other
|
|
806
|
+
* browsers receive the cursor change without a separate `_00_rv` bump. */
|
|
807
|
+
pushCursorState(encoded: Uint8Array): Promise<void>;
|
|
808
|
+
/** Import remote cursor state (called by CrdtManager from LIVE SELECT) */
|
|
809
|
+
importRemoteCursor(base64State: string): void;
|
|
810
|
+
private scheduleRemotePush;
|
|
811
|
+
/** SET path inside a parent row for the current snapshot. `@crdt`-only
|
|
812
|
+
* fields hold the snapshot directly (`<field>`); `@crdt @cursor`
|
|
813
|
+
* fields hold a `{ state, cursors }` object so the snapshot lives at
|
|
814
|
+
* `<field>.state` next to per-session cursor blobs. */
|
|
815
|
+
private statePath;
|
|
816
|
+
/** Mirror the LoroDoc snapshot into the parent row locally. Runs on
|
|
817
|
+
* every local update and every remote import so reloads (online or
|
|
818
|
+
* offline) see the freshest content immediately. Failures are
|
|
819
|
+
* swallowed — a stale local write must never block user input. */
|
|
820
|
+
private persistLocal;
|
|
821
|
+
private pushToRemote;
|
|
822
|
+
}
|
|
823
|
+
//#endregion
|
|
824
|
+
//#region src/modules/crdt/index.d.ts
|
|
825
|
+
/**
|
|
826
|
+
* CrdtManager manages active CrdtField instances and their sync channels.
|
|
827
|
+
*
|
|
828
|
+
* Collaborative state lives in two dedicated tables (defined in
|
|
829
|
+
* `apps/cli/src/meta_tables_remote.surql`):
|
|
830
|
+
* - `_00_crdt` { record_id, field, state } — one row per (record, field)
|
|
831
|
+
* - `_00_cursor` { record_id, session_id, field, state } — one row per
|
|
832
|
+
* (record, session, field)
|
|
833
|
+
*
|
|
834
|
+
* Splitting them off the parent row is what makes offline edits mergeable:
|
|
835
|
+
* each (record, field) gets its own row, so concurrent offline writes don't
|
|
836
|
+
* collide on the parent's last-write-wins semantics.
|
|
837
|
+
*
|
|
838
|
+
* Cross-browser delivery still rides the parent table's existing LIVE feed
|
|
839
|
+
* to avoid SurrealDB v3 LIVE bugs around dereference-based permission rules
|
|
840
|
+
* (issues 3602, 4026). On every meta UPSERT the writer also bumps the
|
|
841
|
+
* parent's `_00_rv` (a no-op assignment); that fires the parent's LIVE
|
|
842
|
+
* feed, and the receiver pulls the matching `_00_crdt` / `_00_cursor` rows
|
|
843
|
+
* via subquery. Permission inheritance happens server-side via
|
|
844
|
+
* `record_id.id != NONE` (SELECT) and `fn::can_update_record` (UPDATE).
|
|
845
|
+
*/
|
|
846
|
+
declare class CrdtManager {
|
|
847
|
+
private schema;
|
|
848
|
+
private local;
|
|
849
|
+
private remote;
|
|
850
|
+
private debounceMs;
|
|
851
|
+
private fields;
|
|
852
|
+
private liveByTable;
|
|
853
|
+
private pendingLive;
|
|
854
|
+
private staleTables;
|
|
855
|
+
private connectionGeneration;
|
|
856
|
+
private connectionUnsubscribes;
|
|
857
|
+
private logger;
|
|
858
|
+
private sessionId;
|
|
859
|
+
constructor(schema: SchemaStructure, local: LocalStore, remote: RemoteDatabaseService, logger: Logger$1, debounceMs?: number);
|
|
860
|
+
/**
|
|
861
|
+
* Re-establish table LIVEs after a socket drop.
|
|
862
|
+
*
|
|
863
|
+
* A LIVE subscription lives and dies with its WebSocket session, and
|
|
864
|
+
* `ensureTableSubscription` is memoized on `liveByTable` — so without this,
|
|
865
|
+
* the first reconnect leaves CRDT realtime permanently dead: the map still
|
|
866
|
+
* holds a uuid for a subscription the server has forgotten, so every later
|
|
867
|
+
* `open()` short-circuits and no LIVE is ever re-issued.
|
|
868
|
+
*
|
|
869
|
+
* Both drop events matter: the SDK publishes `reconnecting` (not
|
|
870
|
+
* `disconnected`) when it intends to recover on its own, and `disconnected`
|
|
871
|
+
* only once it has given up.
|
|
872
|
+
*/
|
|
873
|
+
private subscribeToReconnect;
|
|
874
|
+
/** Stop observing transport events. Separate from {@link closeAll}, which also
|
|
875
|
+
* runs on a bucket switch where the manager keeps being used. */
|
|
876
|
+
dispose(): void;
|
|
877
|
+
private hasOpenFieldFor;
|
|
878
|
+
/** Set the session id that scopes this client's cursor entries. Must be
|
|
879
|
+
* called before `open()` for cursors to be pushed under a stable key.
|
|
880
|
+
* Passed in from `sp00ky.ts` at boot (it already fetches `session::id()`
|
|
881
|
+
* for the data-module salt). */
|
|
882
|
+
setSessionId(sessionId: string): void;
|
|
883
|
+
/**
|
|
884
|
+
* Open a CRDT field for collaborative editing.
|
|
885
|
+
*
|
|
886
|
+
* @param table - Table name
|
|
887
|
+
* @param recordId - Full record ID (e.g., "thread:abc")
|
|
888
|
+
* @param field - Field name (e.g., "title", "content")
|
|
889
|
+
* @param fallbackText - Current plain text from the record, used to seed the
|
|
890
|
+
* LoroDoc if no CRDT state exists yet (migration path)
|
|
891
|
+
*/
|
|
892
|
+
open(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
|
|
893
|
+
close(table: string, recordId: string, field: string): void;
|
|
894
|
+
/**
|
|
895
|
+
* Close every open field + table LIVE. Fields flush a final remote push by
|
|
896
|
+
* default; pass `{ flush: false }` on a bucket switch, where that flush
|
|
897
|
+
* would push the previous user's snapshot under the next user's session.
|
|
898
|
+
*/
|
|
899
|
+
closeAll(options?: {
|
|
900
|
+
flush?: boolean;
|
|
901
|
+
}): void;
|
|
902
|
+
/** Ensure a single `LIVE SELECT * FROM <table>` is running, shared across
|
|
903
|
+
* every open CrdtField on `table`. */
|
|
904
|
+
private ensureTableSubscription;
|
|
905
|
+
/** Apply a parent-row payload from a non-LIVE source (e.g. the
|
|
906
|
+
* list_ref-driven sync engine, when the cross-user LIVE on the
|
|
907
|
+
* parent table is filtered out by the SurrealDB cross-session
|
|
908
|
+
* permission gap). Same semantics as the internal `dispatchRow`. */
|
|
909
|
+
applyRow(table: string, row: Record<string, unknown>): void;
|
|
910
|
+
/** Dispatch a parent-row LIVE event to every open CrdtField on that
|
|
911
|
+
* record. Each open field reads its slice of the row directly — the
|
|
912
|
+
* CRDT snapshot is a column on the parent now, so there is no
|
|
913
|
+
* follow-up subquery. */
|
|
914
|
+
private dispatchRow;
|
|
915
|
+
/** One-shot remote fetch for a row whose CRDT field hasn't synced
|
|
916
|
+
* locally yet (fresh device, memory-backed local DB after reload, …).
|
|
917
|
+
* Used by `open()` when the local read came up empty. Subsequent
|
|
918
|
+
* cross-browser updates ride `dispatchRow` via the parent LIVE feed. */
|
|
919
|
+
private fetchAndDispatchRow;
|
|
920
|
+
/** Schema lookup: does `<table>.<field>` carry a `@cursor` annotation?
|
|
921
|
+
* Determines the on-disk shape (plain snapshot vs. `{ state, cursors }`). */
|
|
922
|
+
private fieldHasCursor;
|
|
923
|
+
/** Pull the LoroDoc snapshot bytes out of a row slice. For `@crdt`-only
|
|
924
|
+
* the slice IS the snapshot (Uint8Array); for `@crdt @cursor` it's
|
|
925
|
+
* `{ state, cursors }` where `state` carries the snapshot bytes. */
|
|
926
|
+
private extractSnapshot;
|
|
927
|
+
private killTableSubscription;
|
|
928
|
+
private makeKey;
|
|
929
|
+
/**
|
|
930
|
+
* Throws if `<table>.<field>` is not annotated `@crdt` in the schema. Catches
|
|
931
|
+
* typos, removed annotations, and stale schema codegen at the call site instead
|
|
932
|
+
* of silently producing a non-CRDT writer.
|
|
933
|
+
*/
|
|
934
|
+
private assertCrdtField;
|
|
935
|
+
}
|
|
936
|
+
//#endregion
|
|
937
|
+
//#region src/modules/feature-flag/index.d.ts
|
|
938
|
+
interface FeatureFlagSnapshot {
|
|
939
|
+
variant: string | undefined;
|
|
940
|
+
payload: unknown | undefined;
|
|
941
|
+
}
|
|
942
|
+
interface FeatureFlagOptions {
|
|
943
|
+
fallback?: string;
|
|
944
|
+
ttl?: QueryTimeToLive;
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* A locally forced variant. Applies to THIS browser only and is never sent to
|
|
948
|
+
* the server — the assignment in `_00_user_feature` is untouched, so clearing
|
|
949
|
+
* the override restores whatever the server says.
|
|
950
|
+
*/
|
|
951
|
+
interface FeatureFlagOverride {
|
|
952
|
+
variant: string;
|
|
953
|
+
payload?: unknown;
|
|
954
|
+
}
|
|
955
|
+
declare class FeatureFlagHandle {
|
|
956
|
+
readonly key: string;
|
|
957
|
+
readonly fallback: string | undefined;
|
|
958
|
+
private latest;
|
|
959
|
+
private listeners;
|
|
960
|
+
private unsubscribeFn;
|
|
961
|
+
private onCloseFn;
|
|
962
|
+
private closed;
|
|
963
|
+
constructor(key: string, fallback: string | undefined);
|
|
964
|
+
attach(unsubscribe: () => void): void;
|
|
965
|
+
detach(): void;
|
|
966
|
+
set(snapshot: FeatureFlagSnapshot): void;
|
|
967
|
+
variant(): string | undefined;
|
|
968
|
+
payload<T = unknown>(): T | undefined;
|
|
969
|
+
enabled(): boolean;
|
|
970
|
+
subscribe(cb: (s: FeatureFlagSnapshot) => void): () => void;
|
|
971
|
+
onClose(cb: () => void): void;
|
|
972
|
+
close(): void;
|
|
973
|
+
}
|
|
974
|
+
/** The slice of the query engine this module drives: register a live query
|
|
975
|
+
* locally, ask for its remote registration, subscribe to its rows. */
|
|
976
|
+
interface LiveQueryPort$1 {
|
|
977
|
+
query(table: string, surql: string, params: Record<string, unknown>, ttl: QueryTimeToLive): Promise<string>;
|
|
978
|
+
subscribe(hash: string, cb: (records: Record<string, any>[]) => void, options?: {
|
|
979
|
+
immediate?: boolean;
|
|
980
|
+
}): () => void;
|
|
981
|
+
}
|
|
982
|
+
interface RemoteRegisterPort$1 {
|
|
983
|
+
enqueueDownEvent(event: {
|
|
984
|
+
type: 'register';
|
|
985
|
+
payload: {
|
|
986
|
+
hash: string;
|
|
987
|
+
};
|
|
988
|
+
}): void;
|
|
989
|
+
}
|
|
990
|
+
interface FeatureFlagModuleDeps<S extends SchemaStructure> {
|
|
991
|
+
dataModule: LiveQueryPort$1;
|
|
992
|
+
sync: RemoteRegisterPort$1;
|
|
993
|
+
auth: Pick<AuthService<S>, 'subscribe'>;
|
|
994
|
+
logger: Logger$1;
|
|
995
|
+
}
|
|
996
|
+
declare class FeatureFlagModule<S extends SchemaStructure> {
|
|
997
|
+
private deps;
|
|
998
|
+
private logger;
|
|
999
|
+
private handles;
|
|
1000
|
+
private authUnsubscribe;
|
|
1001
|
+
private lastUserId;
|
|
1002
|
+
private querySubscription;
|
|
1003
|
+
private starting;
|
|
1004
|
+
private ttl;
|
|
1005
|
+
private snapshots;
|
|
1006
|
+
private loaded;
|
|
1007
|
+
private overrides;
|
|
1008
|
+
constructor(deps: FeatureFlagModuleDeps<S>);
|
|
1009
|
+
init(): void;
|
|
1010
|
+
feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
|
|
1011
|
+
closeAll(): Promise<void>;
|
|
1012
|
+
/** Auth changed: drop the old user's query/snapshots and re-observe. */
|
|
1013
|
+
private refresh;
|
|
1014
|
+
private teardownQuery;
|
|
1015
|
+
/** Start the single shared live query (idempotent; no-op with no handles). */
|
|
1016
|
+
private ensureStarted;
|
|
1017
|
+
/** Live query result → per-key snapshots → push to every active handle. */
|
|
1018
|
+
private applyRecords;
|
|
1019
|
+
/**
|
|
1020
|
+
* Force `key` to `variant` in THIS browser. Pass `null` to clear.
|
|
1021
|
+
*
|
|
1022
|
+
* Nothing is written to the server: the `_00_user_feature` assignment is
|
|
1023
|
+
* untouched, so clearing restores whatever the server says. Persisted to
|
|
1024
|
+
* localStorage on the page origin, so it survives a reload.
|
|
1025
|
+
*/
|
|
1026
|
+
setLocalOverride(key: string, variant: string | null, payload?: unknown): void;
|
|
1027
|
+
clearLocalOverrides(): void;
|
|
1028
|
+
getLocalOverrides(): Record<string, FeatureFlagOverride>;
|
|
1029
|
+
/** The assignment for `key`, with any local override taking precedence. */
|
|
1030
|
+
private resolve;
|
|
1031
|
+
private pushAll;
|
|
1032
|
+
private loadOverrides;
|
|
1033
|
+
private persistOverrides;
|
|
1034
|
+
}
|
|
1035
|
+
//#endregion
|
|
1036
|
+
//#region src/modules/app-release/index.d.ts
|
|
1037
|
+
interface AppReleaseSnapshot {
|
|
1038
|
+
/** Latest announced version for the app, or undefined when no row exists. */
|
|
1039
|
+
version: string | undefined;
|
|
1040
|
+
/** Clients should clear SW/caches when reloading onto this version. */
|
|
1041
|
+
cacheBust: boolean;
|
|
1042
|
+
/** Clients should reload/update immediately instead of asking. */
|
|
1043
|
+
mandatory: boolean;
|
|
1044
|
+
releasedAt: string | undefined;
|
|
1045
|
+
}
|
|
1046
|
+
interface AppReleaseOptions {
|
|
1047
|
+
ttl?: QueryTimeToLive;
|
|
1048
|
+
}
|
|
1049
|
+
declare class AppReleaseHandle {
|
|
1050
|
+
readonly app: string;
|
|
1051
|
+
private latest;
|
|
1052
|
+
private listeners;
|
|
1053
|
+
private onCloseFn;
|
|
1054
|
+
private closed;
|
|
1055
|
+
constructor(app: string);
|
|
1056
|
+
set(snapshot: AppReleaseSnapshot): void;
|
|
1057
|
+
snapshot(): AppReleaseSnapshot;
|
|
1058
|
+
version(): string | undefined;
|
|
1059
|
+
/** True when the announced version is semver-newer than `currentVersion`. */
|
|
1060
|
+
updateAvailable(currentVersion: string): boolean;
|
|
1061
|
+
subscribe(cb: (s: AppReleaseSnapshot) => void): () => void;
|
|
1062
|
+
onClose(cb: () => void): void;
|
|
1063
|
+
close(): void;
|
|
1064
|
+
}
|
|
1065
|
+
/** The slice of the query engine this module drives: register a live query
|
|
1066
|
+
* locally, ask for its remote registration, subscribe to its rows. */
|
|
1067
|
+
interface LiveQueryPort {
|
|
1068
|
+
query(table: string, surql: string, params: Record<string, unknown>, ttl: QueryTimeToLive): Promise<string>;
|
|
1069
|
+
subscribe(hash: string, cb: (records: Record<string, any>[]) => void, options?: {
|
|
1070
|
+
immediate?: boolean;
|
|
1071
|
+
}): () => void;
|
|
1072
|
+
}
|
|
1073
|
+
interface RemoteRegisterPort {
|
|
1074
|
+
enqueueDownEvent(event: {
|
|
1075
|
+
type: 'register';
|
|
1076
|
+
payload: {
|
|
1077
|
+
hash: string;
|
|
1078
|
+
};
|
|
1079
|
+
}): void;
|
|
1080
|
+
}
|
|
1081
|
+
interface AppReleaseModuleDeps<S extends SchemaStructure> {
|
|
1082
|
+
dataModule: LiveQueryPort;
|
|
1083
|
+
sync: RemoteRegisterPort;
|
|
1084
|
+
auth: Pick<AuthService<S>, 'subscribe'>;
|
|
1085
|
+
logger: Logger$1;
|
|
1086
|
+
}
|
|
1087
|
+
declare class AppReleaseModule<S extends SchemaStructure> {
|
|
1088
|
+
private deps;
|
|
1089
|
+
private logger;
|
|
1090
|
+
private handles;
|
|
1091
|
+
private authUnsubscribe;
|
|
1092
|
+
private lastUserId;
|
|
1093
|
+
private querySubscription;
|
|
1094
|
+
private starting;
|
|
1095
|
+
private ttl;
|
|
1096
|
+
private snapshots;
|
|
1097
|
+
private loaded;
|
|
1098
|
+
constructor(deps: AppReleaseModuleDeps<S>);
|
|
1099
|
+
init(): void;
|
|
1100
|
+
release(app: string, options?: AppReleaseOptions): AppReleaseHandle;
|
|
1101
|
+
closeAll(): Promise<void>;
|
|
1102
|
+
private refresh;
|
|
1103
|
+
private teardownQuery;
|
|
1104
|
+
private ensureStarted;
|
|
1105
|
+
private applyRecords;
|
|
1106
|
+
}
|
|
1107
|
+
//#endregion
|
|
1108
|
+
//#region src/kernel/events.d.ts
|
|
1109
|
+
/**
|
|
1110
|
+
* Inbound events. Everything that can start a saga arrives as one of these
|
|
1111
|
+
* through the runtime router: public API calls, adapter callbacks, timers,
|
|
1112
|
+
* and `dispatch` effects from other sagas.
|
|
1113
|
+
*/
|
|
1114
|
+
type RuntimeEvent = {
|
|
1115
|
+
type: 'EnsureRegistered';
|
|
1116
|
+
requireAuth?: boolean;
|
|
1117
|
+
attempt?: number;
|
|
1118
|
+
} | {
|
|
1119
|
+
type: 'RegisterRemote';
|
|
1120
|
+
hash: QueryHash;
|
|
1121
|
+
retry?: boolean;
|
|
1122
|
+
} | {
|
|
1123
|
+
type: 'SyncOutcome';
|
|
1124
|
+
ok: boolean;
|
|
1125
|
+
error?: unknown;
|
|
1126
|
+
} | {
|
|
1127
|
+
type: 'AckPrune';
|
|
1128
|
+
} | {
|
|
1129
|
+
type: 'ReadDirtyMembership';
|
|
1130
|
+
} | {
|
|
1131
|
+
type: 'ReadMembership';
|
|
1132
|
+
hashes: QueryHash[];
|
|
1133
|
+
} | {
|
|
1134
|
+
type: 'FetchRows';
|
|
1135
|
+
} | {
|
|
1136
|
+
type: 'Materialize';
|
|
1137
|
+
hash: QueryHash;
|
|
1138
|
+
} | {
|
|
1139
|
+
type: 'MaterializeDirty';
|
|
1140
|
+
} | {
|
|
1141
|
+
type: 'LifecycleTick';
|
|
1142
|
+
} | {
|
|
1143
|
+
type: 'GcTick';
|
|
1144
|
+
} | {
|
|
1145
|
+
type: 'Drain';
|
|
1146
|
+
} | {
|
|
1147
|
+
type: 'FlushWrite';
|
|
1148
|
+
key: string;
|
|
1149
|
+
} | {
|
|
1150
|
+
type: 'PollTick';
|
|
1151
|
+
} | {
|
|
1152
|
+
type: 'SelfHealTick';
|
|
1153
|
+
} | {
|
|
1154
|
+
type: 'HeartbeatNow';
|
|
1155
|
+
} | {
|
|
1156
|
+
type: 'StartRemote';
|
|
1157
|
+
} | {
|
|
1158
|
+
type: 'PrimeCircuit';
|
|
1159
|
+
} | {
|
|
1160
|
+
type: 'VersionsPrimed';
|
|
1161
|
+
entries: Array<readonly [string, number]>;
|
|
1162
|
+
} | {
|
|
1163
|
+
type: 'WarmBlobs';
|
|
1164
|
+
} | {
|
|
1165
|
+
type: 'AuthFlip';
|
|
1166
|
+
userId: string | null;
|
|
1167
|
+
} | {
|
|
1168
|
+
type: 'BucketSwitch';
|
|
1169
|
+
target: string;
|
|
1170
|
+
} | {
|
|
1171
|
+
type: 'PageHide';
|
|
1172
|
+
} | {
|
|
1173
|
+
type: 'ConnectionChanged';
|
|
1174
|
+
state: ConnectionState;
|
|
1175
|
+
} | {
|
|
1176
|
+
type: 'StreamUpdate';
|
|
1177
|
+
update: StreamUpdate;
|
|
1178
|
+
} | {
|
|
1179
|
+
type: 'LiveStart';
|
|
1180
|
+
} | {
|
|
1181
|
+
type: 'LiveChange';
|
|
1182
|
+
hashes: QueryHash[];
|
|
1183
|
+
} | {
|
|
1184
|
+
type: 'TabRole';
|
|
1185
|
+
role: 'solo' | 'leader' | 'follower';
|
|
1186
|
+
} | {
|
|
1187
|
+
type: 'TabMessage';
|
|
1188
|
+
message: unknown;
|
|
1189
|
+
};
|
|
1190
|
+
type OutEvent = {
|
|
1191
|
+
type: 'query:records';
|
|
1192
|
+
hash: QueryHash;
|
|
1193
|
+
records: ReadonlyArray<Record<string, unknown>>;
|
|
1194
|
+
} | {
|
|
1195
|
+
type: 'query:status';
|
|
1196
|
+
hash: QueryHash;
|
|
1197
|
+
status: QueryStatus;
|
|
1198
|
+
} | {
|
|
1199
|
+
type: 'query:authority';
|
|
1200
|
+
hash: QueryHash;
|
|
1201
|
+
known: boolean;
|
|
1202
|
+
} | {
|
|
1203
|
+
type: 'query:view-lost';
|
|
1204
|
+
hash: QueryHash;
|
|
1205
|
+
} | {
|
|
1206
|
+
type: 'query:evicted';
|
|
1207
|
+
hash: QueryHash;
|
|
1208
|
+
} | {
|
|
1209
|
+
type: 'mutation:event';
|
|
1210
|
+
event: unknown;
|
|
1211
|
+
} | {
|
|
1212
|
+
type: 'mutation:settled';
|
|
1213
|
+
mutationId: string;
|
|
1214
|
+
recordId: string;
|
|
1215
|
+
eventType: string;
|
|
1216
|
+
} | {
|
|
1217
|
+
type: 'mutation:rolled-back';
|
|
1218
|
+
mutationId: string;
|
|
1219
|
+
recordId: string;
|
|
1220
|
+
eventType: string;
|
|
1221
|
+
error: string;
|
|
1222
|
+
} | {
|
|
1223
|
+
type: 'tray:changed';
|
|
1224
|
+
count: number;
|
|
1225
|
+
} | {
|
|
1226
|
+
type: 'health:changed';
|
|
1227
|
+
health: SyncHealth;
|
|
1228
|
+
} | {
|
|
1229
|
+
type: 'activity:changed';
|
|
1230
|
+
fetching: number;
|
|
1231
|
+
pending: number;
|
|
1232
|
+
} | {
|
|
1233
|
+
type: 'tabs:broadcast';
|
|
1234
|
+
message: unknown;
|
|
1235
|
+
} | {
|
|
1236
|
+
type: 'tabs:sendTo';
|
|
1237
|
+
tabId: string;
|
|
1238
|
+
message: unknown;
|
|
1239
|
+
} | {
|
|
1240
|
+
type: 'devtools';
|
|
1241
|
+
name: string;
|
|
1242
|
+
data?: unknown;
|
|
1243
|
+
} | {
|
|
1244
|
+
type: 'log';
|
|
1245
|
+
level: 'debug' | 'info' | 'warn' | 'error';
|
|
1246
|
+
message: string;
|
|
1247
|
+
data?: unknown;
|
|
1248
|
+
};
|
|
1249
|
+
//#endregion
|
|
1250
|
+
//#region src/state/lifecycle.d.ts
|
|
1251
|
+
/**
|
|
1252
|
+
* The query lifecycle as one explicit machine.
|
|
1253
|
+
*
|
|
1254
|
+
* `phase` answers "where do this query's rows come from":
|
|
1255
|
+
* - `cold` never resolved on this device: rows come from a local
|
|
1256
|
+
* predicate scan (or the SSP local window), bindings show a loader.
|
|
1257
|
+
* - `cached` a durable `_00_view` row was found: rows come from that id-set.
|
|
1258
|
+
* - `live` a server membership set was accepted this session.
|
|
1259
|
+
* - `view-lost` the server's `_00_query` row vanished while we held
|
|
1260
|
+
* membership; rows are kept, a re-registration is under way.
|
|
1261
|
+
*
|
|
1262
|
+
* `remote` tracks the server-side registration, `fetchDepth` is the refcount
|
|
1263
|
+
* behind `status: 'fetching'`, `notified` says whether subscribers received
|
|
1264
|
+
* at least one materialization this registration.
|
|
1265
|
+
*/
|
|
1266
|
+
type QueryPhase = 'cold' | 'cached' | 'live' | 'view-lost';
|
|
1267
|
+
type RemotePhase = 'unregistered' | 'registering' | 'registered' | 'failed';
|
|
1268
|
+
interface QueryLifecycle {
|
|
1269
|
+
readonly phase: QueryPhase;
|
|
1270
|
+
readonly remote: RemotePhase;
|
|
1271
|
+
readonly fetchDepth: number;
|
|
1272
|
+
readonly notified: boolean;
|
|
1273
|
+
}
|
|
1274
|
+
//#endregion
|
|
1275
|
+
//#region src/kernel/effects.d.ts
|
|
1276
|
+
type Vars = Record<string, unknown>;
|
|
1277
|
+
/** What the in-browser SSP needs to build a local view. */
|
|
1278
|
+
interface RegisterPlan {
|
|
1279
|
+
queryHash: string;
|
|
1280
|
+
surql: string;
|
|
1281
|
+
params: Vars;
|
|
1282
|
+
ttl: string;
|
|
1283
|
+
tableName: string;
|
|
1284
|
+
involvedTables?: string[];
|
|
1285
|
+
}
|
|
1286
|
+
/** One statement's outcome of a multi-statement remote request. */
|
|
1287
|
+
type StatementResult = {
|
|
1288
|
+
status: 'OK';
|
|
1289
|
+
result: unknown;
|
|
1290
|
+
} | {
|
|
1291
|
+
status: 'ERR';
|
|
1292
|
+
error: string;
|
|
1293
|
+
};
|
|
1294
|
+
/** One entry of an `all` fan-out result (allSettled semantics). */
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* Effects are data. A saga yields one and gets its result back on the next
|
|
1298
|
+
* `next()`; the interpreter (client/runtime) is the only place they execute.
|
|
1299
|
+
*/
|
|
1300
|
+
type Effect = {
|
|
1301
|
+
kind: 'local.query';
|
|
1302
|
+
sql: string;
|
|
1303
|
+
vars?: Vars;
|
|
1304
|
+
epoch?: number;
|
|
1305
|
+
} | {
|
|
1306
|
+
kind: 'local.select';
|
|
1307
|
+
plan: QueryPlan;
|
|
1308
|
+
params?: Vars;
|
|
1309
|
+
} | {
|
|
1310
|
+
kind: 'local.execute';
|
|
1311
|
+
query: SealedQuery<unknown>;
|
|
1312
|
+
vars?: Vars;
|
|
1313
|
+
epoch?: number;
|
|
1314
|
+
} | {
|
|
1315
|
+
kind: 'local.getById';
|
|
1316
|
+
table: string;
|
|
1317
|
+
id: unknown;
|
|
1318
|
+
} | {
|
|
1319
|
+
kind: 'local.upsert';
|
|
1320
|
+
table: string;
|
|
1321
|
+
id: unknown;
|
|
1322
|
+
data: Record<string, unknown>;
|
|
1323
|
+
mode: 'replace' | 'merge';
|
|
1324
|
+
} | {
|
|
1325
|
+
kind: 'local.delete';
|
|
1326
|
+
table: string;
|
|
1327
|
+
id: unknown;
|
|
1328
|
+
} | {
|
|
1329
|
+
kind: 'local.epoch';
|
|
1330
|
+
} | {
|
|
1331
|
+
kind: 'remote.query';
|
|
1332
|
+
sql: string;
|
|
1333
|
+
vars?: Vars;
|
|
1334
|
+
timeoutMs?: number;
|
|
1335
|
+
} | {
|
|
1336
|
+
kind: 'remote.live';
|
|
1337
|
+
table: string;
|
|
1338
|
+
} | {
|
|
1339
|
+
kind: 'remote.kill';
|
|
1340
|
+
uuid: string;
|
|
1341
|
+
} | {
|
|
1342
|
+
kind: 'ssp.register';
|
|
1343
|
+
plan: RegisterPlan;
|
|
1344
|
+
} | {
|
|
1345
|
+
kind: 'ssp.unregister';
|
|
1346
|
+
hash: string;
|
|
1347
|
+
} | {
|
|
1348
|
+
kind: 'ssp.ingest';
|
|
1349
|
+
records: IngestRecord[];
|
|
1350
|
+
} | {
|
|
1351
|
+
kind: 'timer.set';
|
|
1352
|
+
key: string;
|
|
1353
|
+
ms: number;
|
|
1354
|
+
event: RuntimeEvent;
|
|
1355
|
+
} | {
|
|
1356
|
+
kind: 'timer.clear';
|
|
1357
|
+
key: string;
|
|
1358
|
+
} | {
|
|
1359
|
+
kind: 'state.read';
|
|
1360
|
+
select: (s: ClientState) => unknown;
|
|
1361
|
+
} | {
|
|
1362
|
+
kind: 'state.update';
|
|
1363
|
+
fn: (s: ClientState) => ClientState;
|
|
1364
|
+
} | {
|
|
1365
|
+
kind: 'state.wait';
|
|
1366
|
+
until: (s: ClientState) => boolean;
|
|
1367
|
+
} | {
|
|
1368
|
+
kind: 'now';
|
|
1369
|
+
} | {
|
|
1370
|
+
kind: 'id';
|
|
1371
|
+
scope: 'mutation' | 'salt';
|
|
1372
|
+
} | {
|
|
1373
|
+
kind: 'hash';
|
|
1374
|
+
input: string;
|
|
1375
|
+
} | {
|
|
1376
|
+
kind: 'emit';
|
|
1377
|
+
event: OutEvent;
|
|
1378
|
+
} | {
|
|
1379
|
+
kind: 'dispatch';
|
|
1380
|
+
event: RuntimeEvent;
|
|
1381
|
+
} | {
|
|
1382
|
+
kind: 'all';
|
|
1383
|
+
effects: Effect[];
|
|
1384
|
+
} | {
|
|
1385
|
+
kind: 'service';
|
|
1386
|
+
name: ServiceName;
|
|
1387
|
+
args: unknown[];
|
|
1388
|
+
};
|
|
1389
|
+
/**
|
|
1390
|
+
* Calls into the legacy services that are adapters, not state: auth, tabs,
|
|
1391
|
+
* blobs, migrator, crdt, persistence, the SSP lifecycle, the remote socket.
|
|
1392
|
+
* A saga names the call; the runtime binds it to the real service.
|
|
1393
|
+
*/
|
|
1394
|
+
interface ServiceCalls {
|
|
1395
|
+
'hint.read': () => string | null;
|
|
1396
|
+
'hint.write': (bucketId: string) => void;
|
|
1397
|
+
'local.connect': (bucketId: string) => Promise<void>;
|
|
1398
|
+
'local.switchStore': (bucketId: string) => Promise<void>;
|
|
1399
|
+
'local.beginSwitch': () => () => void;
|
|
1400
|
+
'local.currentBucketId': () => string;
|
|
1401
|
+
'local.usesSurqlSchema': () => boolean;
|
|
1402
|
+
'migrator.provision': () => Promise<void>;
|
|
1403
|
+
'blobs.start': (bucketId: string) => Promise<void>;
|
|
1404
|
+
'blobs.setNamespace': (bucketId: string) => Promise<void>;
|
|
1405
|
+
'blobs.clear': () => Promise<void>;
|
|
1406
|
+
'ssp.init': () => Promise<void>;
|
|
1407
|
+
'ssp.setPermissions': () => void;
|
|
1408
|
+
'ssp.setSessionAuth': (authId: string | null, access: string | null) => void;
|
|
1409
|
+
'ssp.prime': (pendingIds: string[]) => Promise<void>;
|
|
1410
|
+
'ssp.reset': () => Promise<void>;
|
|
1411
|
+
'ssp.setPersistence': (enabled: boolean) => void;
|
|
1412
|
+
'auth.restoreSession': () => Promise<string | null>;
|
|
1413
|
+
'auth.init': () => Promise<void>;
|
|
1414
|
+
'auth.sessionAuthId': () => string | null;
|
|
1415
|
+
'auth.access': () => string | null;
|
|
1416
|
+
'auth.token': () => string | null;
|
|
1417
|
+
'auth.currentUser': () => Record<string, unknown> | null;
|
|
1418
|
+
'remote.connect': () => Promise<void>;
|
|
1419
|
+
'remote.releaseViews': (ids: unknown[]) => void;
|
|
1420
|
+
'supervisor.start': () => void;
|
|
1421
|
+
'tabs.start': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
|
|
1422
|
+
'tabs.moveToBucket': (bucketId: string) => Promise<'solo' | 'leader' | 'follower'>;
|
|
1423
|
+
'crdt.setSessionId': (sessionId: string) => void;
|
|
1424
|
+
'crdt.closeAll': (flush: boolean) => void;
|
|
1425
|
+
'persistence.set': (key: string, value: unknown) => Promise<void>;
|
|
1426
|
+
'features.init': () => void;
|
|
1427
|
+
'releases.init': () => void;
|
|
1428
|
+
'window.attach': () => void;
|
|
1429
|
+
}
|
|
1430
|
+
type ServiceName = keyof ServiceCalls;
|
|
1431
|
+
/** Typed constructors. Each is one line so the saga reads like the plan. */
|
|
1432
|
+
//#endregion
|
|
1433
|
+
//#region src/kernel/saga.d.ts
|
|
1434
|
+
/**
|
|
1435
|
+
* A saga is a generator that yields effects and receives their results. It
|
|
1436
|
+
* holds no references to adapters or state, so it can be driven by the real
|
|
1437
|
+
* interpreter or by `testing/run-pure` with canned results.
|
|
1438
|
+
*/
|
|
1439
|
+
type Saga<R> = Generator<Effect, R, any>;
|
|
1440
|
+
/**
|
|
1441
|
+
* Lanes bound concurrency without a queue object per lane:
|
|
1442
|
+
* - `serial`: runs strictly one at a time per key, in arrival order.
|
|
1443
|
+
* - `dedupe`: a request while one run is active joins that run instead of
|
|
1444
|
+
* starting another.
|
|
1445
|
+
* The bookkeeping is a pure value so the policy is unit-testable; the runtime
|
|
1446
|
+
* owns the promises.
|
|
1447
|
+
*/
|
|
1448
|
+
type Lane = {
|
|
1449
|
+
kind: 'serial';
|
|
1450
|
+
key: string;
|
|
1451
|
+
} | {
|
|
1452
|
+
kind: 'dedupe';
|
|
1453
|
+
key: string;
|
|
1454
|
+
};
|
|
1455
|
+
interface LaneState {
|
|
1456
|
+
readonly running: ReadonlySet<string>;
|
|
1457
|
+
readonly waiting: ReadonlyMap<string, number>;
|
|
1458
|
+
}
|
|
1459
|
+
//#endregion
|
|
1460
|
+
//#region src/state/client-state.d.ts
|
|
1461
|
+
type Row = Record<string, unknown>;
|
|
1462
|
+
/** Everything a query is, minus anything that changes after registration. */
|
|
1463
|
+
interface QueryDefinition {
|
|
1464
|
+
/** Session-salted `_00_query:<hash>` record id: the remote view id. */
|
|
1465
|
+
readonly id: RecordId<string>;
|
|
1466
|
+
readonly hash: QueryHash;
|
|
1467
|
+
/** Unsalted `sha256({surql, params})`: key of the durable `_00_view` row. */
|
|
1468
|
+
readonly viewKey: string;
|
|
1469
|
+
readonly surql: string;
|
|
1470
|
+
readonly params: Readonly<Record<string, unknown>>;
|
|
1471
|
+
readonly plan?: QueryPlan;
|
|
1472
|
+
readonly ttl: QueryTimeToLive;
|
|
1473
|
+
readonly ttlMs: number;
|
|
1474
|
+
readonly tableName: string;
|
|
1475
|
+
readonly createdAt: number;
|
|
1476
|
+
}
|
|
1477
|
+
type ServerViewState = 'materializing' | 'ready' | null;
|
|
1478
|
+
interface QueryTelemetry {
|
|
1479
|
+
readonly updateCount: number;
|
|
1480
|
+
readonly errorCount: number;
|
|
1481
|
+
readonly lastUpdatedAt: number | null;
|
|
1482
|
+
readonly materializationSamples: ReadonlyArray<number>;
|
|
1483
|
+
readonly lastIngestLatencyMs: number | null;
|
|
1484
|
+
readonly phaseSamples: Readonly<Record<string, ReadonlyArray<number>>>;
|
|
1485
|
+
readonly phaseLast: Readonly<Record<string, number | null>>;
|
|
1486
|
+
readonly registrationTimings: RegistrationTimings;
|
|
1487
|
+
}
|
|
1488
|
+
interface QueryEntry {
|
|
1489
|
+
readonly def: QueryDefinition;
|
|
1490
|
+
readonly lifecycle: QueryLifecycle;
|
|
1491
|
+
/** Server membership (ids + versions) or the durable seed. */
|
|
1492
|
+
readonly remoteArray: RecordVersionArray;
|
|
1493
|
+
/** The in-browser SSP view's id-set: local truth for "does this row match". */
|
|
1494
|
+
readonly localArray: RecordVersionArray;
|
|
1495
|
+
readonly subqueryRemoteArray: RecordVersionArray;
|
|
1496
|
+
readonly records: ReadonlyArray<Row>;
|
|
1497
|
+
readonly serverState: ServerViewState;
|
|
1498
|
+
readonly subscribers: number;
|
|
1499
|
+
readonly lastSubscriberLeftAt: number | null;
|
|
1500
|
+
readonly lastHeartbeatAt: number | null;
|
|
1501
|
+
readonly lastPolledAt: number | null;
|
|
1502
|
+
readonly registerAttempts: number;
|
|
1503
|
+
readonly telemetry: QueryTelemetry;
|
|
1504
|
+
}
|
|
1505
|
+
type OutboxStatus = 'pending' | 'acked';
|
|
1506
|
+
/** In-memory mirror of one `_00_pending_mutations` row plus its push progress. */
|
|
1507
|
+
interface OutboxItem {
|
|
1508
|
+
readonly id: string;
|
|
1509
|
+
readonly type: MutationEventType;
|
|
1510
|
+
readonly recordId: string;
|
|
1511
|
+
readonly table: string;
|
|
1512
|
+
readonly status: OutboxStatus;
|
|
1513
|
+
readonly ackedAt: number | null;
|
|
1514
|
+
readonly attempts: number;
|
|
1515
|
+
}
|
|
1516
|
+
type TabRole = 'solo' | 'leader' | 'follower';
|
|
1517
|
+
/** A debounced update accumulating locally until its flush writes the outbox row. */
|
|
1518
|
+
interface PendingWrite {
|
|
1519
|
+
readonly key: string;
|
|
1520
|
+
readonly table: string;
|
|
1521
|
+
readonly recordId: string;
|
|
1522
|
+
readonly data: Readonly<Record<string, unknown>>;
|
|
1523
|
+
readonly before: Readonly<Record<string, unknown>> | null;
|
|
1524
|
+
readonly firstAt: number;
|
|
1525
|
+
}
|
|
1526
|
+
interface SyncSlice {
|
|
1527
|
+
readonly health: SyncHealth;
|
|
1528
|
+
readonly consecutiveFailures: number;
|
|
1529
|
+
readonly hasSyncedOnce: boolean;
|
|
1530
|
+
readonly selfHealAttempts: number;
|
|
1531
|
+
readonly pollIdleStreak: number;
|
|
1532
|
+
readonly lastReconnectRefetchAt: number | null;
|
|
1533
|
+
readonly needsResubscribe: boolean;
|
|
1534
|
+
readonly fetchAttempts: number;
|
|
1535
|
+
readonly liveUuid: string | null;
|
|
1536
|
+
readonly liveTable: string | null;
|
|
1537
|
+
readonly lanes: LaneState;
|
|
1538
|
+
}
|
|
1539
|
+
interface ClientState {
|
|
1540
|
+
readonly sessionId: string | null;
|
|
1541
|
+
readonly userId: string | null;
|
|
1542
|
+
/** The principal the current session salt was minted for. */
|
|
1543
|
+
readonly saltUserId: string | null;
|
|
1544
|
+
/** Latest bucket-switch target; an older switch that wakes up to a newer target skips. */
|
|
1545
|
+
readonly pendingBucket: string | null;
|
|
1546
|
+
readonly tabId: string;
|
|
1547
|
+
readonly tabRole: TabRole;
|
|
1548
|
+
readonly bucketId: string | null;
|
|
1549
|
+
readonly localReady: boolean;
|
|
1550
|
+
readonly primed: boolean;
|
|
1551
|
+
readonly queries: ReadonlyMap<QueryHash, QueryEntry>;
|
|
1552
|
+
/** Hashes whose local registration is in flight (dedupes concurrent `query()` calls). */
|
|
1553
|
+
readonly registering: ReadonlySet<QueryHash>;
|
|
1554
|
+
/** Re-read attempts per hash while the server reports a view as `materializing`. */
|
|
1555
|
+
readonly membershipReread: ReadonlyMap<QueryHash, number>;
|
|
1556
|
+
/** Local body versions (`_00_rv`) by encoded record id. */
|
|
1557
|
+
readonly versions: ReadonlyMap<string, number>;
|
|
1558
|
+
readonly outbox: ReadonlyArray<OutboxItem>;
|
|
1559
|
+
readonly pendingWrites: ReadonlyMap<string, PendingWrite>;
|
|
1560
|
+
readonly failedCount: number;
|
|
1561
|
+
/** Queries whose render inputs changed since their last materialization. */
|
|
1562
|
+
readonly dirty: ReadonlySet<QueryHash>;
|
|
1563
|
+
/** Queries whose server membership must be re-read. */
|
|
1564
|
+
readonly membershipDirty: ReadonlySet<QueryHash>;
|
|
1565
|
+
readonly sync: SyncSlice;
|
|
1566
|
+
}
|
|
1567
|
+
//#endregion
|
|
1568
|
+
//#region src/modules/ref-tables.d.ts
|
|
1569
|
+
type RefMode = 'single' | 'dedicated';
|
|
1570
|
+
/**
|
|
1571
|
+
* Sentinel user id for unauthenticated clients when anonymous live queries are
|
|
1572
|
+
* enabled. Mirrors `ssp_protocol::ANON_AUTH_ID`. It carries no `user:` prefix
|
|
1573
|
+
* so it can never collide with a real user id (those arrive as `user:<id>`);
|
|
1574
|
+
* both sides resolve it to the shared `_00_list_ref_anon` table.
|
|
1575
|
+
*/
|
|
1576
|
+
//#endregion
|
|
1577
|
+
//#region src/query/env.d.ts
|
|
1578
|
+
/**
|
|
1579
|
+
* Static configuration the sagas read. Plain data, built once by the runtime
|
|
1580
|
+
* from `Sp00kyConfig`; sagas never see the config object itself.
|
|
1581
|
+
*/
|
|
1582
|
+
interface SagaEnv {
|
|
1583
|
+
readonly schema: SchemaStructure;
|
|
1584
|
+
readonly refMode: RefMode;
|
|
1585
|
+
readonly anonLive: boolean;
|
|
1586
|
+
readonly remoteTimeoutMs: number;
|
|
1587
|
+
readonly pushTimeoutMs: number;
|
|
1588
|
+
readonly outboxBatchSize: number;
|
|
1589
|
+
readonly degradeAfter: number;
|
|
1590
|
+
readonly materializeDebounceMs: number;
|
|
1591
|
+
readonly pollBaseMs: number;
|
|
1592
|
+
readonly defaultTtlMs: number;
|
|
1593
|
+
}
|
|
1594
|
+
//#endregion
|
|
1595
|
+
//#region src/mutation/rows.d.ts
|
|
1596
|
+
interface FailedMutationRow {
|
|
1597
|
+
id: string;
|
|
1598
|
+
mutationType: MutationEventType;
|
|
1599
|
+
recordId: string;
|
|
1600
|
+
tableName: string;
|
|
1601
|
+
data?: Record<string, unknown>;
|
|
1602
|
+
beforeRecord?: Record<string, unknown> | null;
|
|
1603
|
+
error: {
|
|
1604
|
+
message: string;
|
|
1605
|
+
kind: 'application' | 'unreplayable';
|
|
1606
|
+
};
|
|
1607
|
+
attempts: number;
|
|
1608
|
+
createdAt: number;
|
|
1609
|
+
failedAt: number;
|
|
1610
|
+
revert: 'full' | 'partial' | 'none';
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Parse a record id READ BACK FROM THE STORE, stripping SurrealDB's `⟨⟩`
|
|
1614
|
+
* escaping (outbox ids contain `_`, so they come back escaped).
|
|
1615
|
+
*/
|
|
1616
|
+
//#endregion
|
|
1617
|
+
//#region src/kernel/interpreter.d.ts
|
|
1618
|
+
/** The five adapters plus the pure ports, as the interpreter sees them. */
|
|
1619
|
+
interface Adapters {
|
|
1620
|
+
local: Pick<LocalStore, 'query' | 'execute' | 'select' | 'getById' | 'upsert' | 'delete'> & {
|
|
1621
|
+
readonly epoch: number;
|
|
1622
|
+
};
|
|
1623
|
+
remote: {
|
|
1624
|
+
queryResponses(sql: string, vars?: Record<string, unknown>): Promise<StatementResult[]>;
|
|
1625
|
+
/** Subscribe to `LIVE SELECT * FROM <table>`; the callback receives the changed query hashes. */
|
|
1626
|
+
live(table: string, onChange: (hashes: string[]) => void): Promise<string>;
|
|
1627
|
+
kill(uuid: string): Promise<void>;
|
|
1628
|
+
};
|
|
1629
|
+
ssp: {
|
|
1630
|
+
registerQueryPlan(plan: {
|
|
1631
|
+
queryHash: string;
|
|
1632
|
+
surql: string;
|
|
1633
|
+
params: Record<string, unknown>;
|
|
1634
|
+
ttl: Duration;
|
|
1635
|
+
lastActiveAt: Date;
|
|
1636
|
+
localArray: [];
|
|
1637
|
+
remoteArray: [];
|
|
1638
|
+
meta: {
|
|
1639
|
+
tableName: string;
|
|
1640
|
+
involvedTables?: string[];
|
|
1641
|
+
};
|
|
1642
|
+
}): StreamUpdate | undefined;
|
|
1643
|
+
unregisterQueryPlan(hash: string): void;
|
|
1644
|
+
ingestMany(records: IngestRecord[]): unknown;
|
|
1645
|
+
};
|
|
1646
|
+
timers: {
|
|
1647
|
+
set(key: string, ms: number, fire: () => void): void;
|
|
1648
|
+
clear(key: string): void;
|
|
1649
|
+
};
|
|
1650
|
+
clock: {
|
|
1651
|
+
now(): number;
|
|
1652
|
+
};
|
|
1653
|
+
ids: {
|
|
1654
|
+
mutation(): string;
|
|
1655
|
+
salt(): string;
|
|
1656
|
+
};
|
|
1657
|
+
hash(input: string): Promise<string>;
|
|
1658
|
+
services: ServiceCalls;
|
|
1659
|
+
}
|
|
1660
|
+
/** What the interpreter needs from the runtime: state, waiting, emitting, dispatching. */
|
|
1661
|
+
//#endregion
|
|
1662
|
+
//#region src/client/runtime.d.ts
|
|
1663
|
+
interface RuntimeOptions {
|
|
1664
|
+
env: SagaEnv;
|
|
1665
|
+
adapters: Adapters;
|
|
1666
|
+
logger: Logger$1;
|
|
1667
|
+
tabId: string;
|
|
1668
|
+
initialState?: ClientState;
|
|
1669
|
+
}
|
|
1670
|
+
/**
|
|
1671
|
+
* The one effectful object of the engine: holds the state, runs sagas on
|
|
1672
|
+
* lanes, fires timers, fans events out to subscribers and schedules
|
|
1673
|
+
* materialization for dirty queries. No decision logic lives here.
|
|
1674
|
+
*/
|
|
1675
|
+
declare class Runtime {
|
|
1676
|
+
private stateValue;
|
|
1677
|
+
private readonly interpret;
|
|
1678
|
+
private readonly env;
|
|
1679
|
+
private readonly adapters;
|
|
1680
|
+
private readonly logger;
|
|
1681
|
+
private readonly serialLanes;
|
|
1682
|
+
private readonly dedupeLanes;
|
|
1683
|
+
private readonly waiters;
|
|
1684
|
+
private readonly recordSubs;
|
|
1685
|
+
private readonly statusSubs;
|
|
1686
|
+
private readonly authoritySubs;
|
|
1687
|
+
private readonly lastStatus;
|
|
1688
|
+
private readonly listeners;
|
|
1689
|
+
private readonly scheduledMaterialize;
|
|
1690
|
+
private readonly timerKeys;
|
|
1691
|
+
private lastActivity;
|
|
1692
|
+
private lastHealth;
|
|
1693
|
+
private disposed;
|
|
1694
|
+
constructor(opts: RuntimeOptions);
|
|
1695
|
+
get state(): ClientState;
|
|
1696
|
+
/** Run a saga, optionally on a lane. Rejections propagate to the caller. */
|
|
1697
|
+
run<R>(saga: Saga<R>, opts?: {
|
|
1698
|
+
lane?: Lane;
|
|
1699
|
+
signal?: AbortSignal;
|
|
1700
|
+
}): Promise<R>;
|
|
1701
|
+
/** Route an event to its saga. Never rejects: failures are logged. */
|
|
1702
|
+
dispatch(event: RuntimeEvent): Promise<void>;
|
|
1703
|
+
private setState;
|
|
1704
|
+
waitFor(until: (s: ClientState) => boolean, signal?: AbortSignal): Promise<void>;
|
|
1705
|
+
/** Deliver an outbound event to its subscribers and listeners. */
|
|
1706
|
+
emit(event: OutEvent): void;
|
|
1707
|
+
private notify;
|
|
1708
|
+
private safely;
|
|
1709
|
+
/** Observe outbound events by type (`'*'` for all). */
|
|
1710
|
+
on(type: OutEvent['type'] | '*', cb: (event: OutEvent) => void): () => void;
|
|
1711
|
+
subscribe(hash: QueryHash, cb: QueryUpdateCallback, options?: {
|
|
1712
|
+
immediate?: boolean;
|
|
1713
|
+
}): () => void;
|
|
1714
|
+
subscribeStatus(hash: QueryHash, cb: QueryStatusCallback, options?: {
|
|
1715
|
+
immediate?: boolean;
|
|
1716
|
+
}): () => void;
|
|
1717
|
+
subscribeAuthority(hash: QueryHash, cb: QueryAuthorityCallback, options?: {
|
|
1718
|
+
immediate?: boolean;
|
|
1719
|
+
}): () => void;
|
|
1720
|
+
/** Apply a reducer outside a saga (facade conveniences such as timing reports). */
|
|
1721
|
+
update(reducer: (s: ClientState) => ClientState): void;
|
|
1722
|
+
dispose(): void;
|
|
1723
|
+
}
|
|
1724
|
+
//#endregion
|
|
1725
|
+
//#region src/services/tabs/coordinator.d.ts
|
|
1726
|
+
interface CoordinatorHooks {
|
|
1727
|
+
/** Become the store owner: spawn the worker, open the pool under the given
|
|
1728
|
+
* per-leadership lock. Returns the resulting health for db-ready relays.
|
|
1729
|
+
* `resumeHeld` = keep the existing live worker (broker-restart path). */
|
|
1730
|
+
adoptOwner(bucketId: string, opts: {
|
|
1731
|
+
workerLockName: string;
|
|
1732
|
+
allowMemoryFallback: boolean;
|
|
1733
|
+
forceTakeover: boolean;
|
|
1734
|
+
resumeHeld: boolean;
|
|
1735
|
+
}): Promise<StorageHealth>;
|
|
1736
|
+
/** Attach to a leader's worker through `dbPort`. */
|
|
1737
|
+
adoptAttached(dbPort: MessagePort, snapshot: {
|
|
1738
|
+
bucketId: string;
|
|
1739
|
+
storageHealth: StorageHealth;
|
|
1740
|
+
leadershipId: number;
|
|
1741
|
+
}): Promise<void>;
|
|
1742
|
+
/** We were leader and got demoted (zombie thaw / stale promotion): tear the
|
|
1743
|
+
* owned worker down so its SAH handles free up. */
|
|
1744
|
+
releaseOwnership(): Promise<void>;
|
|
1745
|
+
/** The leader (or its port) is gone; queue/park ops until a new role lands. */
|
|
1746
|
+
onLeaderLost(reason: string): void;
|
|
1747
|
+
/** Leader side: forward a follower's dbPort into the owned worker. */
|
|
1748
|
+
exposeClientPort(clientId: string, port: MessagePort): Promise<void>;
|
|
1749
|
+
removeClientPort(clientId: string): Promise<void>;
|
|
1750
|
+
/** Sync layer role changes (implemented in the sync module). */
|
|
1751
|
+
/** Synchronous leader WIRING. Must not block: the role is published and
|
|
1752
|
+
* `leader-ready` is sent right after it returns. */
|
|
1753
|
+
becomeSyncLeader(hub: LeaderSyncHub): void;
|
|
1754
|
+
/** The blocking half of the leader handover (outbox drain, LIVE restart),
|
|
1755
|
+
* run in the background once the tab is already a working leader. */
|
|
1756
|
+
resumeSyncLeaderDuties(): Promise<void>;
|
|
1757
|
+
becomeSyncFollower(forwarder: SyncForwarder): void;
|
|
1758
|
+
becomeSyncSolo(): void;
|
|
1759
|
+
/** Current storage health, for db-ready sent to late-joining followers. */
|
|
1760
|
+
currentStorageHealth(): StorageHealth;
|
|
1761
|
+
}
|
|
1762
|
+
/** Leader-side fan-out surface handed to the sync layer. The sync router
|
|
1763
|
+
* (modules/sync/tab-router.ts) registers itself as the message handler. */
|
|
1764
|
+
declare class LeaderSyncHub {
|
|
1765
|
+
readonly leadershipId: number;
|
|
1766
|
+
private logger;
|
|
1767
|
+
private followers;
|
|
1768
|
+
private seq;
|
|
1769
|
+
onFollowerMessage: ((tabId: TabId, msg: FollowerToLeaderMessage) => void) | null;
|
|
1770
|
+
onFollowerDetached: ((tabId: TabId) => void) | null;
|
|
1771
|
+
constructor(leadershipId: number, logger: Logger$1);
|
|
1772
|
+
attach(tabId: TabId, port: MessagePort): void;
|
|
1773
|
+
detach(tabId: TabId): void;
|
|
1774
|
+
detachAll(): void;
|
|
1775
|
+
sendTo(tabId: TabId, msg: LeaderToFollowerMessage): void;
|
|
1776
|
+
broadcast(msg: LeaderToFollowerMessage, exceptTabId?: TabId): void;
|
|
1777
|
+
/** Stamped ingest relay; seq lets followers detect gaps. */
|
|
1778
|
+
relayIngest(tuples: IngestTuple[], exceptTabId?: TabId): void;
|
|
1779
|
+
get followerCount(): number;
|
|
1780
|
+
get relayedBatches(): number;
|
|
1781
|
+
}
|
|
1782
|
+
/** Follower half of the syncPort. Queues while detached (leaderless window)
|
|
1783
|
+
* and flushes on rebind; a lost-in-flight mutation notify is additionally
|
|
1784
|
+
* backstopped by the new leader reloading the shared outbox from the store. */
|
|
1785
|
+
declare class SyncForwarder {
|
|
1786
|
+
private tabId;
|
|
1787
|
+
private port;
|
|
1788
|
+
private queued;
|
|
1789
|
+
onLeaderMessage: ((msg: LeaderToFollowerMessage) => void) | null;
|
|
1790
|
+
constructor(tabId: TabId);
|
|
1791
|
+
rebind(port: MessagePort): void;
|
|
1792
|
+
unbind(): void;
|
|
1793
|
+
private post;
|
|
1794
|
+
mutationEnqueued(mutationId: string): void;
|
|
1795
|
+
/** An optimistic write this tab just ingested. Deliberately NOT queued while
|
|
1796
|
+
* detached: a new leader primes its circuit from the shared store, which
|
|
1797
|
+
* already holds the row, and replaying a stale tuple at it later would put
|
|
1798
|
+
* an older `_00_rv` in its version memo. */
|
|
1799
|
+
ingest(tuples: IngestTuple[]): void;
|
|
1800
|
+
requestPoll(): void;
|
|
1801
|
+
}
|
|
1802
|
+
declare class TabsCoordinator {
|
|
1803
|
+
private deps;
|
|
1804
|
+
role: TabRole$1;
|
|
1805
|
+
leadershipId: number;
|
|
1806
|
+
leaderTabId: TabId | null;
|
|
1807
|
+
brokerRole: 'pending' | TabRole$1;
|
|
1808
|
+
/** Wall time from `become-leader` to `leader-ready` on the last promotion.
|
|
1809
|
+
* Reported in the DevTools role-change event: a slow store adopt (OPFS pool
|
|
1810
|
+
* contention after the previous leader died) shows up here as seconds. */
|
|
1811
|
+
lastPromotionMs: number | null;
|
|
1812
|
+
private fingerprint;
|
|
1813
|
+
private bucketId;
|
|
1814
|
+
private broker;
|
|
1815
|
+
private hub;
|
|
1816
|
+
private forwarder;
|
|
1817
|
+
private roleListeners;
|
|
1818
|
+
private startResolve;
|
|
1819
|
+
private startReject;
|
|
1820
|
+
/** Ports received before start() resolves or between roles. */
|
|
1821
|
+
private promotionChain;
|
|
1822
|
+
private closed;
|
|
1823
|
+
constructor(deps: {
|
|
1824
|
+
tabId: TabId;
|
|
1825
|
+
fingerprint: string;
|
|
1826
|
+
hooks: CoordinatorHooks;
|
|
1827
|
+
logger: Logger$1;
|
|
1828
|
+
/** Fired on pagehide while this tab leads: last-chance OPFS release. */
|
|
1829
|
+
onLeaderPageHide?: () => void;
|
|
1830
|
+
});
|
|
1831
|
+
/** Serialize role transitions; each is small but async (worker opens). */
|
|
1832
|
+
private enqueue;
|
|
1833
|
+
onRoleChange(cb: (role: TabRole$1) => void): () => void;
|
|
1834
|
+
private setRole;
|
|
1835
|
+
/** Connect the broker and resolve once this tab has a usable store role.
|
|
1836
|
+
* Rejects when no role lands in time; the caller then boots solo. */
|
|
1837
|
+
start(bucketId: string): Promise<TabRole$1>;
|
|
1838
|
+
/** Bucket switch: leave the old namespace, join the new one. Resolves when
|
|
1839
|
+
* a role lands in the new namespace. */
|
|
1840
|
+
moveToBucket(bucketId: string): Promise<TabRole$1>;
|
|
1841
|
+
private promote;
|
|
1842
|
+
private demote;
|
|
1843
|
+
private tabLock;
|
|
1844
|
+
private teardownLeader;
|
|
1845
|
+
private teardownFollower;
|
|
1846
|
+
private serveFollower;
|
|
1847
|
+
private attachToLeader;
|
|
1848
|
+
private handleLeaderGone;
|
|
1849
|
+
private fallbackToSolo;
|
|
1850
|
+
get syncHub(): LeaderSyncHub | null;
|
|
1851
|
+
get syncForwarder(): SyncForwarder | null;
|
|
1852
|
+
get tabId(): TabId;
|
|
1853
|
+
stop(): Promise<void>;
|
|
1854
|
+
}
|
|
1855
|
+
//#endregion
|
|
1856
|
+
//#region src/services/blobs/blob-store.d.ts
|
|
1857
|
+
/**
|
|
1858
|
+
* Byte storage for cached bucket files.
|
|
1859
|
+
*
|
|
1860
|
+
* The default implementation is OPFS. Bucket files never arrive over HTTP in
|
|
1861
|
+
* this client — `BucketHandle.get()` is a SurrealQL RPC on the sync socket — so
|
|
1862
|
+
* neither the browser's HTTP cache nor the Cache API can hold them. We persist
|
|
1863
|
+
* the bytes ourselves, and OPFS is the cheapest place to put them: a read is
|
|
1864
|
+
* `getFile()` → a disk-backed lazy `File` that `URL.createObjectURL` can serve
|
|
1865
|
+
* without ever moving the bytes through the JS heap.
|
|
1866
|
+
*
|
|
1867
|
+
* Layout is real nested directories rather than one hashed filename:
|
|
1868
|
+
*
|
|
1869
|
+
* sp00ky-blobs/<namespace>/<bucket>/<...path segments>
|
|
1870
|
+
*
|
|
1871
|
+
* That costs a `getDirectoryHandle` per segment on write, and buys the property
|
|
1872
|
+
* the whole orphan story rests on: the full `(bucket, path)` key is recoverable
|
|
1873
|
+
* from a directory walk alone. The `_00_blob` manifest can therefore be wiped
|
|
1874
|
+
* (memory fallback, SQLite pool wipe, IndexedDB corruption recovery) and be
|
|
1875
|
+
* rebuilt from disk instead of taking the cached bytes down with it.
|
|
1876
|
+
*/
|
|
1877
|
+
/** Identifies one cached file: the bucket it lives in and its path within. */
|
|
1878
|
+
interface BlobKey {
|
|
1879
|
+
bucket: string;
|
|
1880
|
+
path: string;
|
|
1881
|
+
}
|
|
1882
|
+
/** What a directory walk can tell us about a stored file, with no manifest. */
|
|
1883
|
+
interface BlobStat {
|
|
1884
|
+
key: BlobKey;
|
|
1885
|
+
size: number;
|
|
1886
|
+
/** File mtime. Seeds `lastAccess` when a manifest row has to be rebuilt. */
|
|
1887
|
+
mtime: number;
|
|
1888
|
+
}
|
|
1889
|
+
interface BlobStore {
|
|
1890
|
+
/** False for {@link MemoryBlobStore} and for OPFS-less environments: the
|
|
1891
|
+
* cache still dedupes and serves within a tab, but nothing survives reload. */
|
|
1892
|
+
readonly persistent: boolean;
|
|
1893
|
+
/** Namespace (the local bucketId) all keys are resolved under. */
|
|
1894
|
+
readonly namespace: string;
|
|
1895
|
+
read(key: BlobKey): Promise<Blob | null>;
|
|
1896
|
+
/** Returns the number of bytes written. Throws on quota exhaustion. */
|
|
1897
|
+
write(key: BlobKey, bytes: Blob): Promise<number>;
|
|
1898
|
+
remove(key: BlobKey): Promise<void>;
|
|
1899
|
+
/** Every committed file under the current namespace. Sweeps torn writes. */
|
|
1900
|
+
list(): Promise<BlobStat[]>;
|
|
1901
|
+
/** Drop the whole namespace (sign-out with `clearOnSignOut`, or a reset). */
|
|
1902
|
+
clear(): Promise<void>;
|
|
1903
|
+
/** Point at another namespace. Does not touch the bytes of the old one. */
|
|
1904
|
+
setNamespace(namespace: string): void;
|
|
1905
|
+
}
|
|
1906
|
+
//#endregion
|
|
1907
|
+
//#region src/services/blobs/blob-manifest.d.ts
|
|
1908
|
+
interface BlobEntry {
|
|
1909
|
+
/** `${bucket}/${path}` — also the `_00_blob` row id. */
|
|
1910
|
+
id: string;
|
|
1911
|
+
bucket: string;
|
|
1912
|
+
path: string;
|
|
1913
|
+
size: number;
|
|
1914
|
+
contentType: string;
|
|
1915
|
+
createdAt: number;
|
|
1916
|
+
lastAccess: number;
|
|
1917
|
+
hits: number;
|
|
1918
|
+
/** Exempt from pressure eviction. Never expires on its own. */
|
|
1919
|
+
pinned: boolean;
|
|
1920
|
+
}
|
|
1921
|
+
declare class BlobManifest {
|
|
1922
|
+
private local;
|
|
1923
|
+
private entries;
|
|
1924
|
+
/** Ids whose in-memory state has not been written back yet. */
|
|
1925
|
+
private dirty;
|
|
1926
|
+
private removed;
|
|
1927
|
+
private flushing;
|
|
1928
|
+
constructor(local: LocalStore);
|
|
1929
|
+
/**
|
|
1930
|
+
* Hydrate from the rows matching `keys`. Ids come from the OPFS listing, so
|
|
1931
|
+
* this never needs a full-table scan (and therefore never needs a QueryPlan).
|
|
1932
|
+
* Any read failure yields an empty manifest: reconcile then rebuilds every
|
|
1933
|
+
* row from disk, which is exactly the desired degradation.
|
|
1934
|
+
*/
|
|
1935
|
+
load(ids: string[]): Promise<void>;
|
|
1936
|
+
get(key: BlobKey): BlobEntry | undefined;
|
|
1937
|
+
getById(id: string): BlobEntry | undefined;
|
|
1938
|
+
all(): BlobEntry[];
|
|
1939
|
+
totalBytes(): number;
|
|
1940
|
+
pinnedBytes(): number;
|
|
1941
|
+
put(entry: BlobEntry): void;
|
|
1942
|
+
touch(id: string, now: number): void;
|
|
1943
|
+
setPinned(id: string, pinned: boolean): boolean;
|
|
1944
|
+
remove(id: string): void;
|
|
1945
|
+
/** Forget everything without scheduling deletes — for a bucket switch, where
|
|
1946
|
+
* the rows belong to the store we are leaving and must stay put. */
|
|
1947
|
+
reset(): void;
|
|
1948
|
+
hasPendingWrites(): boolean;
|
|
1949
|
+
/**
|
|
1950
|
+
* Write back pending changes. Serialized: a second concurrent flush awaits
|
|
1951
|
+
* the first rather than racing it into the same rows. Failures are swallowed
|
|
1952
|
+
* on purpose — a lost metadata write costs an LRU timestamp, and the entry is
|
|
1953
|
+
* rebuilt from disk on the next reconcile.
|
|
1954
|
+
*/
|
|
1955
|
+
flush(): Promise<void>;
|
|
1956
|
+
private doFlush;
|
|
1957
|
+
}
|
|
1958
|
+
//#endregion
|
|
1959
|
+
//#region src/services/blobs/blob-cache.d.ts
|
|
1960
|
+
interface BlobUrlLease {
|
|
1961
|
+
url: string;
|
|
1962
|
+
release(): void;
|
|
1963
|
+
}
|
|
1964
|
+
interface BlobReadOptions {
|
|
1965
|
+
/** Write through to L1 on a miss. Default true. */
|
|
1966
|
+
persist?: boolean;
|
|
1967
|
+
/** Mark the entry exempt from pressure eviction. */
|
|
1968
|
+
pin?: boolean;
|
|
1969
|
+
/**
|
|
1970
|
+
* Default `'never'`: a bucket path is treated as immutable, which is how the
|
|
1971
|
+
* client writes them (`crypto.randomUUID() + ext`). `'head'` spends a remote
|
|
1972
|
+
* `head()` to compare sizes before trusting L1.
|
|
1973
|
+
*/
|
|
1974
|
+
revalidate?: 'never' | 'head';
|
|
1975
|
+
/** Skip L0/L1 entirely and refill from remote. Backs `refetch()`. */
|
|
1976
|
+
reload?: boolean;
|
|
1977
|
+
}
|
|
1978
|
+
interface BlobCacheStats {
|
|
1979
|
+
entries: number;
|
|
1980
|
+
totalBytes: number;
|
|
1981
|
+
budgetBytes: number;
|
|
1982
|
+
pinnedBytes: number;
|
|
1983
|
+
evictedEntries: number;
|
|
1984
|
+
evictedBytes: number;
|
|
1985
|
+
reconciledEntries: number;
|
|
1986
|
+
hits: number;
|
|
1987
|
+
misses: number;
|
|
1988
|
+
persistent: boolean;
|
|
1989
|
+
/** True when pinned bytes alone exceed the budget: new entries stop being
|
|
1990
|
+
* written rather than pinned ones being thrown away. */
|
|
1991
|
+
persistPaused: boolean;
|
|
1992
|
+
}
|
|
1993
|
+
interface BlobCacheOptions {
|
|
1994
|
+
store: BlobStore;
|
|
1995
|
+
manifest: BlobManifest;
|
|
1996
|
+
/** L2 read. Resolves to null when the file does not exist remotely. */
|
|
1997
|
+
fetchRemote(key: BlobKey): Promise<Blob | null>;
|
|
1998
|
+
/** L2 metadata, for `revalidate: 'head'`. */
|
|
1999
|
+
headRemote?(key: BlobKey): Promise<Record<string, unknown> | null>;
|
|
2000
|
+
logger: Logger$1;
|
|
2001
|
+
maxBytes: number;
|
|
2002
|
+
now?: () => number;
|
|
2003
|
+
/** Injected so the URL layer is exercisable off a DOM (node tests). */
|
|
2004
|
+
urls?: {
|
|
2005
|
+
create(blob: Blob): string;
|
|
2006
|
+
revoke(url: string): void;
|
|
2007
|
+
};
|
|
2008
|
+
}
|
|
2009
|
+
declare class BlobCache {
|
|
2010
|
+
private readonly store;
|
|
2011
|
+
private readonly manifest;
|
|
2012
|
+
private readonly fetchRemote;
|
|
2013
|
+
private readonly headRemote?;
|
|
2014
|
+
private readonly logger;
|
|
2015
|
+
private readonly now;
|
|
2016
|
+
private readonly urlFactory;
|
|
2017
|
+
private maxBytes;
|
|
2018
|
+
private persistPaused;
|
|
2019
|
+
/** Set after a quota failure survives one forced eviction. */
|
|
2020
|
+
private persistDisabled;
|
|
2021
|
+
private readonly urls;
|
|
2022
|
+
/** Ids at zero references, oldest first — the hot-URL window. */
|
|
2023
|
+
private idleUrls;
|
|
2024
|
+
private readonly inflight;
|
|
2025
|
+
private flushTimer;
|
|
2026
|
+
private readonly onPageHide;
|
|
2027
|
+
/**
|
|
2028
|
+
* Resolves once the manifest has been reconciled against disk. Reads await
|
|
2029
|
+
* it, so `start()` does NOT have to be awaited on the boot path — blocking
|
|
2030
|
+
* boot on an OPFS directory walk delayed the WebSocket connect (and with it
|
|
2031
|
+
* the connection supervisor) for no benefit.
|
|
2032
|
+
*/
|
|
2033
|
+
private ready;
|
|
2034
|
+
private hits;
|
|
2035
|
+
private misses;
|
|
2036
|
+
private evictedEntries;
|
|
2037
|
+
private evictedBytes;
|
|
2038
|
+
private reconciledEntries;
|
|
2039
|
+
constructor(opts: BlobCacheOptions);
|
|
2040
|
+
/** Coalesce manifest write-back. Metadata only, so losing the tail costs an
|
|
2041
|
+
* LRU timestamp that reconcile reseeds from the file mtime. */
|
|
2042
|
+
private scheduleFlush;
|
|
2043
|
+
/**
|
|
2044
|
+
* Resolve the bytes for `key`, filling L1 on the way when `persist` is on.
|
|
2045
|
+
* Returns null when the file does not exist remotely and is not cached.
|
|
2046
|
+
*/
|
|
2047
|
+
read(key: BlobKey, options?: BlobReadOptions): Promise<Blob | null>;
|
|
2048
|
+
/**
|
|
2049
|
+
* An object URL for `key`, refcounted. Callers MUST `release()`; the URL is
|
|
2050
|
+
* revoked once the last holder lets go and it falls out of the hot window.
|
|
2051
|
+
*/
|
|
2052
|
+
acquireUrl(key: BlobKey, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
|
|
2053
|
+
private lease;
|
|
2054
|
+
private releaseUrl;
|
|
2055
|
+
private revokeUrl;
|
|
2056
|
+
/** L1 lookup with the size check that catches torn and cross-tab writes. */
|
|
2057
|
+
private readLocal;
|
|
2058
|
+
/** True when the remote agrees with the cached size, or cannot be reached. */
|
|
2059
|
+
private headMatches;
|
|
2060
|
+
private fetchDeduped;
|
|
2061
|
+
private persist;
|
|
2062
|
+
private writeThrough;
|
|
2063
|
+
/** Forget one path everywhere. Called on `bucket.put()`/`bucket.delete()`. */
|
|
2064
|
+
invalidate(key: BlobKey): Promise<void>;
|
|
2065
|
+
private dropLocal;
|
|
2066
|
+
setPinned(key: BlobKey, pinned: boolean): void;
|
|
2067
|
+
/**
|
|
2068
|
+
* Bring total bytes under budget by dropping the least recently used
|
|
2069
|
+
* entries. Pinned entries and anything with a live object URL are skipped —
|
|
2070
|
+
* evicting bytes that a mounted `<img>` is displaying would blank it.
|
|
2071
|
+
*/
|
|
2072
|
+
private enforceBudget;
|
|
2073
|
+
/** Evict LRU-first until at or below `target`. Returns the resulting total. */
|
|
2074
|
+
private evictTo;
|
|
2075
|
+
/**
|
|
2076
|
+
* Rebuild the manifest from what is actually on disk. OPFS wins on existence
|
|
2077
|
+
* in both directions: files with no row get a row (seeded from mtime), rows
|
|
2078
|
+
* are only loaded for files that exist, and torn `.part-` writes are swept by
|
|
2079
|
+
* the walk itself.
|
|
2080
|
+
*
|
|
2081
|
+
* Rows whose file vanished outside our control (a browser origin eviction)
|
|
2082
|
+
* are left in `_00_blob`. They are inert — `load()` only ever asks for ids it
|
|
2083
|
+
* found on disk — and are overwritten if that path is cached again.
|
|
2084
|
+
*/
|
|
2085
|
+
reconcile(): Promise<void>;
|
|
2086
|
+
/** Warm the cache for offline use. Skips anything already cached. */
|
|
2087
|
+
prefetch(keys: BlobKey[]): Promise<void>;
|
|
2088
|
+
/** Bind to the boot bucket and hydrate the manifest from disk. Separate from
|
|
2089
|
+
* {@link setNamespace} because boot must reconcile even when the namespace
|
|
2090
|
+
* it lands on is the one the store was constructed with. */
|
|
2091
|
+
start(namespace: string): Promise<void>;
|
|
2092
|
+
/** Repoint at another local bucket. The bytes of the old one stay on disk so
|
|
2093
|
+
* switching back (or signing back in) is still warm. */
|
|
2094
|
+
setNamespace(namespace: string): Promise<void>;
|
|
2095
|
+
setMaxBytes(maxBytes: number): void;
|
|
2096
|
+
/** Delete every cached byte in the current namespace. */
|
|
2097
|
+
clear(): Promise<void>;
|
|
2098
|
+
flush(): Promise<void>;
|
|
2099
|
+
/** Flush metadata and drop every object URL. Must run before the local store
|
|
2100
|
+
* closes — the flush writes through it. */
|
|
2101
|
+
close(): Promise<void>;
|
|
2102
|
+
stats(): BlobCacheStats;
|
|
2103
|
+
}
|
|
2104
|
+
//#endregion
|
|
2105
|
+
//#region src/client/services.d.ts
|
|
2106
|
+
/** Everything the facade talks to besides the runtime. */
|
|
2107
|
+
interface Services<S extends SchemaStructure> {
|
|
2108
|
+
logger: Logger$1;
|
|
2109
|
+
local: LocalStore;
|
|
2110
|
+
remote: RemoteDatabaseService;
|
|
2111
|
+
connectionSupervisor: ConnectionSupervisor;
|
|
2112
|
+
blobs: BlobCache;
|
|
2113
|
+
persistence: PersistenceClient;
|
|
2114
|
+
streamProcessor: StreamProcessorService;
|
|
2115
|
+
migrator: LocalMigrator;
|
|
2116
|
+
crdt: CrdtManager;
|
|
2117
|
+
auth: AuthService<S>;
|
|
2118
|
+
tabs: TabsCoordinator | null;
|
|
2119
|
+
tabsUnsupportedReason: string | undefined;
|
|
2120
|
+
tabId: string;
|
|
2121
|
+
}
|
|
2122
|
+
/** What the services need back from the runtime once it exists. */
|
|
2123
|
+
//#endregion
|
|
2124
|
+
//#region src/utils/blurhash.d.ts
|
|
2125
|
+
/**
|
|
2126
|
+
* Blurhash generation settings. `true` enables with the defaults below, `false`
|
|
2127
|
+
* disables. Resolution order for a put: per-call option > client config >
|
|
2128
|
+
* default ON. See {@link Sp00kyConfig.blurhash}.
|
|
2129
|
+
*/
|
|
2130
|
+
type BlurhashSetting = boolean | BlurhashEncodeOptions;
|
|
2131
|
+
interface BlurhashEncodeOptions {
|
|
2132
|
+
/** Horizontal detail components, 1-9. Defaults to 4. */
|
|
2133
|
+
componentX?: number;
|
|
2134
|
+
/** Vertical detail components, 1-9. Defaults to 3. */
|
|
2135
|
+
componentY?: number;
|
|
2136
|
+
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Where an image's blurhash lives: a tiny sidecar object in the same bucket.
|
|
2139
|
+
* Buckets have no per-object metadata channel (`put` is just `.put($content)`),
|
|
2140
|
+
* so the hash for `covers/x_t.webp` is the text object `covers/x_t.webp.bh`.
|
|
2141
|
+
*/
|
|
2142
|
+
declare function blurhashSidecarPath(path: string): string;
|
|
2143
|
+
/** Extensions `bucket.put` treats as images worth hashing. */
|
|
2144
|
+
declare const BLURHASH_IMAGE_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif", "avif", "bmp"];
|
|
2145
|
+
declare function isImagePath(path: string): boolean;
|
|
2146
|
+
/**
|
|
2147
|
+
* Decode `content` as an image and compute its blurhash. Browser-only: returns
|
|
2148
|
+
* null (never throws) when image decoding is unavailable (node, workers without
|
|
2149
|
+
* canvas), when the bytes are not a decodable image, or on any other failure —
|
|
2150
|
+
* a missing hash must never break the upload that triggered it.
|
|
2151
|
+
*/
|
|
2152
|
+
declare function encodeImageToBlurhash(content: string | Uint8Array | Blob, options?: BlurhashEncodeOptions): Promise<string | null>;
|
|
2153
|
+
//#endregion
|
|
2154
|
+
//#region src/client/bucket-handle.d.ts
|
|
2155
|
+
/** Coerce whatever the `.get()` RPC hands back into a Blob. */
|
|
2156
|
+
declare function bucketContentToBlob(content: unknown): Blob | null;
|
|
2157
|
+
interface BucketPutOptions {
|
|
2158
|
+
/** Override the client-level {@link Sp00kyConfig.blurhash} setting for this put. */
|
|
2159
|
+
blurhash?: BlurhashSetting;
|
|
2160
|
+
}
|
|
2161
|
+
interface BucketPutResult {
|
|
2162
|
+
/** The computed blurhash when the content was a hashable image; else null. */
|
|
2163
|
+
blurhash: string | null;
|
|
2164
|
+
}
|
|
2165
|
+
interface BucketHandleSettings {
|
|
2166
|
+
blurhash?: BlurhashSetting;
|
|
2167
|
+
logger?: {
|
|
2168
|
+
warn: (obj: unknown, msg?: string) => void;
|
|
2169
|
+
};
|
|
2170
|
+
}
|
|
198
2171
|
declare class BucketHandle {
|
|
199
2172
|
private bucketName;
|
|
200
2173
|
private remote;
|
|
201
|
-
|
|
202
|
-
|
|
2174
|
+
/** Absent on the raw handle the cache itself reads through. */
|
|
2175
|
+
private blobs?;
|
|
2176
|
+
private settings?;
|
|
2177
|
+
constructor(bucketName: string, remote: RemoteDatabaseService, /** Absent on the raw handle the cache itself reads through. */
|
|
2178
|
+
blobs?: (BlobCache | null) | undefined, settings?: BucketHandleSettings | undefined);
|
|
2179
|
+
/** Effective blurhash setting: per-call option > client config > default ON. */
|
|
2180
|
+
private resolveBlurhash;
|
|
2181
|
+
put(path: string, content: string | Uint8Array | Blob, options?: BucketPutOptions): Promise<BucketPutResult>;
|
|
2182
|
+
/**
|
|
2183
|
+
* The blurhash stored alongside an uploaded image (see
|
|
2184
|
+
* {@link blurhashSidecarPath}), or null when there is none. Reads through the
|
|
2185
|
+
* blob cache, so a warm client answers from OPFS without a network hop, and
|
|
2186
|
+
* misses are remembered per tab so a hashless image costs at most one
|
|
2187
|
+
* serialized remote read per session.
|
|
2188
|
+
*/
|
|
2189
|
+
blurhash(path: string): Promise<string | null>;
|
|
203
2190
|
get(path: string): Promise<unknown>;
|
|
2191
|
+
/**
|
|
2192
|
+
* Read through the local blob cache: OPFS first, the bucket second. Unlike
|
|
2193
|
+
* {@link get} this survives a reload and works offline. Returns null when the
|
|
2194
|
+
* file exists in neither place.
|
|
2195
|
+
*/
|
|
2196
|
+
read(path: string, options?: BlobReadOptions): Promise<Blob | null>;
|
|
2197
|
+
/**
|
|
2198
|
+
* A refcounted object URL for `path`, suitable for `<img src>`. The caller
|
|
2199
|
+
* MUST call `release()` when the URL goes off screen. Returns null when the
|
|
2200
|
+
* file does not exist, or when object URLs are unavailable (non-browser).
|
|
2201
|
+
*/
|
|
2202
|
+
url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
|
|
2203
|
+
/** Exempt `path` from pressure eviction. Pinned bytes never expire. */
|
|
2204
|
+
pin(path: string): void;
|
|
2205
|
+
unpin(path: string): void;
|
|
2206
|
+
/** Drop `path` from the local cache without touching the remote file. */
|
|
2207
|
+
evict(path: string): Promise<void>;
|
|
2208
|
+
/** Warm the cache for offline use. Already-cached paths are skipped. */
|
|
2209
|
+
prefetch(paths: string[]): Promise<void>;
|
|
204
2210
|
delete(path: string): Promise<void>;
|
|
205
2211
|
exists(path: string): Promise<boolean>;
|
|
206
2212
|
head(path: string): Promise<Record<string, unknown>>;
|
|
@@ -208,55 +2214,145 @@ declare class BucketHandle {
|
|
|
208
2214
|
rename(sourcePath: string, targetPath: string): Promise<void>;
|
|
209
2215
|
list(prefix?: string): Promise<string[]>;
|
|
210
2216
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
private
|
|
2217
|
+
//#endregion
|
|
2218
|
+
//#region src/client/sp00ky-client.d.ts
|
|
2219
|
+
interface Sp00kyClientDeps<S extends SchemaStructure> {
|
|
2220
|
+
services?: Services<S>;
|
|
2221
|
+
runtime?: Runtime;
|
|
2222
|
+
env?: Partial<SagaEnv>;
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* The public client. Every method is a thin translation into a saga run, a
|
|
2226
|
+
* state read, or a subscription on the runtime; the engine's behaviour lives
|
|
2227
|
+
* in the sagas.
|
|
2228
|
+
*/
|
|
2229
|
+
declare class Sp00kyClient<S extends SchemaStructure> {
|
|
2230
|
+
private readonly config;
|
|
2231
|
+
readonly auth: AuthService<S>;
|
|
2232
|
+
readonly streamProcessor: StreamProcessorService;
|
|
2233
|
+
private readonly services;
|
|
2234
|
+
private readonly env;
|
|
2235
|
+
private readonly runtime;
|
|
2236
|
+
private readonly featureFlags;
|
|
2237
|
+
private readonly appReleases;
|
|
2238
|
+
private readonly devTools;
|
|
2239
|
+
private hub;
|
|
2240
|
+
private forwarder;
|
|
2241
|
+
private detachWindow;
|
|
2242
|
+
private sharedActive;
|
|
2243
|
+
constructor(config: Sp00kyConfig<S>, deps?: Sp00kyClientDeps<S>);
|
|
2244
|
+
/** Adapter callbacks become runtime events; runtime events reach the legacy services. */
|
|
2245
|
+
private wireAdapters;
|
|
2246
|
+
private forwardToDevTools;
|
|
233
2247
|
init(): Promise<void>;
|
|
234
2248
|
close(): Promise<void>;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
2249
|
+
isLocalReady(): boolean;
|
|
2250
|
+
query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, Sp00kyQueryResultPromise>;
|
|
2251
|
+
private registerInput;
|
|
238
2252
|
private initQuery;
|
|
239
2253
|
queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<string>;
|
|
2254
|
+
/**
|
|
2255
|
+
* Preload = register a query nobody subscribes to. Resolved before on this
|
|
2256
|
+
* device: returns at once. Never resolved: resolves when the server's
|
|
2257
|
+
* membership and every body are local. `signal` aborts the wait.
|
|
2258
|
+
*/
|
|
2259
|
+
preload(finalQuery: FinalQuery<S, any, any, any, any, any>, options?: PreloadOptions): Promise<void>;
|
|
240
2260
|
subscribe(queryHash: string, callback: (records: Record<string, any>[]) => void, options?: {
|
|
241
2261
|
immediate?: boolean;
|
|
242
2262
|
}): Promise<() => void>;
|
|
2263
|
+
deregisterQuery(queryHash: string): void;
|
|
2264
|
+
subscribeQueryStatus(queryHash: string, callback: QueryStatusCallback, options?: {
|
|
2265
|
+
immediate?: boolean;
|
|
2266
|
+
}): () => void;
|
|
2267
|
+
subscribeQueryAuthority(queryHash: string, callback: QueryAuthorityCallback, options?: {
|
|
2268
|
+
immediate?: boolean;
|
|
2269
|
+
}): () => void;
|
|
2270
|
+
reportFrontendTiming(queryHash: string, ms: number): void;
|
|
2271
|
+
create<T extends Record<string, unknown>>(id: string, data: T): Promise<T>;
|
|
2272
|
+
update<T extends Record<string, unknown>>(_table: string, id: string, data: Partial<T>, options?: UpdateOptions): Promise<T>;
|
|
2273
|
+
delete(_table: string, id: string): Promise<void>;
|
|
243
2274
|
run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, payload: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
2275
|
+
get failedMutationCount(): number;
|
|
2276
|
+
subscribeToFailedMutations(cb: (count: number) => void): () => void;
|
|
2277
|
+
listFailedMutations(): Promise<FailedMutationRow[]>;
|
|
2278
|
+
retryFailedMutation(mutationId: string): Promise<boolean>;
|
|
2279
|
+
discardFailedMutation(mutationId: string): Promise<boolean>;
|
|
2280
|
+
get tabRole(): TabRole$1 | null;
|
|
2281
|
+
get remoteClient(): Surreal;
|
|
2282
|
+
get localClient(): unknown;
|
|
2283
|
+
get pendingMutationCount(): number;
|
|
2284
|
+
/** Kept for the e2e suite; the saga core has no LIVE retry ladder. */
|
|
2285
|
+
get liveRetryCount(): number;
|
|
2286
|
+
subscribeToPendingMutations(cb: (count: number) => void): () => void;
|
|
2287
|
+
get fetchingQueryCount(): number;
|
|
2288
|
+
subscribeToFetchActivity(cb: (fetching: number) => void): () => void;
|
|
2289
|
+
get syncHealth(): SyncHealth;
|
|
2290
|
+
subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void;
|
|
2291
|
+
get storageHealth(): StorageHealth;
|
|
2292
|
+
subscribeToStorageHealth(cb: (health: StorageHealth) => void): () => void;
|
|
2293
|
+
/** The engine state, read-only. For tests and DevTools. */
|
|
2294
|
+
get state(): ClientState;
|
|
2295
|
+
/** Feed an event into the engine. For adapters, tests and DevTools. */
|
|
2296
|
+
dispatch(event: RuntimeEvent): Promise<void>;
|
|
2297
|
+
authenticate(token: string): Promise<surrealdb0.Tokens>;
|
|
2298
|
+
deauthenticate(): Promise<void>;
|
|
250
2299
|
useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T>;
|
|
251
|
-
|
|
252
|
-
|
|
2300
|
+
remoteQuery<T extends unknown[]>(sql: string, vars?: Record<string, unknown>): Promise<T>;
|
|
2301
|
+
feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
|
|
2302
|
+
setFeatureOverride(key: string, variant: string | null, payload?: unknown): void;
|
|
2303
|
+
clearFeatureOverrides(): void;
|
|
2304
|
+
getFeatureOverrides(): Record<string, FeatureFlagOverride>;
|
|
2305
|
+
appRelease(app: string, options?: AppReleaseOptions): AppReleaseHandle;
|
|
2306
|
+
openCrdtField(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
|
|
2307
|
+
closeCrdtField(table: string, recordId: string, field: string): void;
|
|
2308
|
+
bucket<B extends BucketNames<S>>(name: B): BucketHandle;
|
|
2309
|
+
getBlobCacheStats(): BlobCacheStats;
|
|
2310
|
+
}
|
|
2311
|
+
//#endregion
|
|
2312
|
+
//#region src/utils/semver.d.ts
|
|
2313
|
+
/** True when `a` is a valid version strictly greater than valid version `b`. */
|
|
2314
|
+
declare function semverGt(a: unknown, b: unknown): boolean;
|
|
2315
|
+
//#endregion
|
|
2316
|
+
//#region src/services/database/errors.d.ts
|
|
2317
|
+
/**
|
|
2318
|
+
* A local-store operation that did not answer within its deadline.
|
|
2319
|
+
*
|
|
2320
|
+
* The local write path (`db.create` / `db.update` / `db.delete`, every local
|
|
2321
|
+
* query behind them) used to have no deadline anywhere: the SQLite worker
|
|
2322
|
+
* transport parks a call until the worker replies, the surrealdb engine's
|
|
2323
|
+
* query chain waits on the previous link, and `withRetry` retries without a
|
|
2324
|
+
* clock. One op that never settled (a worker starved behind a long select, a
|
|
2325
|
+
* lock verification awaiting `navigator.locks.query()` forever) left the
|
|
2326
|
+
* caller's promise pending for the tab's lifetime - a chat composer that never
|
|
2327
|
+
* re-enabled, a call that never got past "Connecting".
|
|
2328
|
+
*
|
|
2329
|
+
* The message says "timed out" on purpose: `classifySyncError` keys off it and
|
|
2330
|
+
* treats the failure as transient (re-queue), never as an application error
|
|
2331
|
+
* that rolls the mutation back. `retryable: false` keeps `withRetry` from
|
|
2332
|
+
* spinning on it: the op is still running in the engine, retrying queues a
|
|
2333
|
+
* second copy behind it.
|
|
2334
|
+
*/
|
|
2335
|
+
declare class LocalOpTimeoutError extends Error {
|
|
2336
|
+
readonly name = "LocalOpTimeoutError";
|
|
2337
|
+
readonly retryable = false;
|
|
2338
|
+
readonly op: string;
|
|
2339
|
+
readonly timeoutMs: number;
|
|
2340
|
+
constructor(op: string, timeoutMs: number);
|
|
253
2341
|
}
|
|
2342
|
+
/** Default deadline for one local-store operation. Generous: a cold 4k-row
|
|
2343
|
+
* select on a throttled tab is seconds, not tens of seconds. */
|
|
2344
|
+
declare const DEFAULT_LOCAL_OP_TIMEOUT_MS = 30000;
|
|
254
2345
|
//#endregion
|
|
255
2346
|
//#region src/utils/index.d.ts
|
|
256
2347
|
declare function fileToUint8Array(file: File | Blob): Promise<Uint8Array>;
|
|
2348
|
+
/**
|
|
2349
|
+
* Convert plain text to simple HTML paragraphs.
|
|
2350
|
+
* Useful for seeding a rich-text editor (e.g. TipTap/ProseMirror) with fallback content.
|
|
2351
|
+
*/
|
|
2352
|
+
declare function textToHtml(text: string): string;
|
|
257
2353
|
/**
|
|
258
2354
|
* Helper for retrying DB operations with exponential backoff
|
|
259
2355
|
*/
|
|
260
2356
|
|
|
261
2357
|
//#endregion
|
|
262
|
-
export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, DebounceOptions, EventSubscriptionOptions, Level, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PinoTransmit, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryTimeToLive, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RunOptions,
|
|
2358
|
+
export { AppReleaseHandle, AppReleaseModule, type AppReleaseOptions, type AppReleaseSnapshot, AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BLURHASH_IMAGE_EXTENSIONS, type BlobCacheStats, type BlobEntry, type BlobKey, type BlobReadOptions, type BlobUrlLease, type BlurhashEncodeOptions, type BlurhashSetting, BucketHandle, BucketPutOptions, BucketPutResult, CURSOR_COLORS, ConnectionState, CrdtField, CrdtManager, DEFAULT_LOCAL_OP_TIMEOUT_MS, DebounceOptions, EventSubscriptionOptions, type FailedMutationRow as FailedMutation, FeatureFlagHandle, FeatureFlagModule, type FeatureFlagOptions, type FeatureFlagOverride, type FeatureFlagSnapshot, Level, LocalOpTimeoutError, MATERIALIZATION_SAMPLE_WINDOW, MembershipOutcome, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, PreloadOptions, PreloadRefresh, QueryAuthorityCallback, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, ReconnectConfig, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, ServerViewMeta, Sp00kyClient, Sp00kyClientDeps, Sp00kyConfig, Sp00kyQueryResult, Sp00kyQueryResultPromise, StorageHealth, StorageHealthStatus, StoreType, SyncHealth, SyncHealthConfig, SyncHealthStatus, TimingPhase, UpdateOptions, blurhashSidecarPath, bucketContentToBlob, createAuthEventSystem, cursorColorFromName, decode as decodeBlurhash, encode as encodeBlurhash, encodeImageToBlurhash, fileToUint8Array, isBlurhashValid, isImagePath, semverGt, textToHtml };
|