@spooky-sync/core 0.0.1-canary.21 → 0.0.1-canary.211
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 +57 -0
- package/dist/index.d.ts +2514 -58
- package/dist/index.js +12561 -2449
- package/dist/otel/index.d.ts +2 -2
- package/dist/otel/index.js +6 -6
- package/dist/sqlite-open.js +303 -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 +751 -11
- package/package.json +11 -7
- package/scripts/check-broker-bundle.mjs +33 -0
- package/skills/{spooky-core → sp00ky-core}/SKILL.md +12 -12
- package/skills/{spooky-core → sp00ky-core}/references/auth.md +1 -1
- package/skills/{spooky-core → sp00ky-core}/references/config.md +2 -2
- package/src/bucket-blurhash.test.ts +148 -0
- package/src/build-globals.d.ts +12 -0
- package/src/events/events.test.ts +2 -1
- package/src/events/index.ts +3 -0
- package/src/index.ts +36 -2
- package/src/modules/app-release/index.test.ts +125 -0
- package/src/modules/app-release/index.ts +201 -0
- package/src/modules/auth/auth.local-first.test.ts +101 -0
- package/src/modules/auth/events/index.ts +2 -1
- package/src/modules/auth/index.ts +127 -24
- package/src/modules/cache/cache.relay.test.ts +95 -0
- package/src/modules/cache/index.ts +163 -43
- package/src/modules/cache/types.ts +2 -2
- package/src/modules/crdt/crdt-field.ts +294 -0
- package/src/modules/crdt/crdt-hydration.test.ts +210 -0
- package/src/modules/crdt/crdt-reconnect.test.ts +195 -0
- package/src/modules/crdt/index.ts +463 -0
- package/src/modules/crdt/loro-loader.ts +25 -0
- package/src/modules/data/data.hydration.test.ts +142 -0
- package/src/modules/data/data.membership.test.ts +523 -0
- package/src/modules/data/data.notify-table.test.ts +41 -0
- package/src/modules/data/data.pending-ids.test.ts +199 -0
- package/src/modules/data/data.rebind.test.ts +170 -0
- package/src/modules/data/data.rematerialize.test.ts +114 -0
- package/src/modules/data/data.run.test.ts +113 -0
- package/src/modules/data/data.settled-writes.test.ts +206 -0
- package/src/modules/data/data.status.test.ts +249 -0
- package/src/modules/data/id-set-plan.test.ts +122 -0
- package/src/modules/data/index.ts +1815 -151
- package/src/modules/data/mutation-id.test.ts +25 -0
- package/src/modules/data/mutation-id.ts +35 -0
- package/src/modules/data/window-query.test.ts +52 -0
- package/src/modules/data/window-query.ts +194 -0
- package/src/modules/devtools/flags.ts +349 -0
- package/src/modules/devtools/index.ts +450 -46
- package/src/modules/devtools/notify-throttle.test.ts +154 -0
- package/src/modules/devtools/state-shape.test.ts +146 -0
- package/src/modules/devtools/storage-info.test.ts +79 -0
- package/src/modules/devtools/storage-info.ts +168 -0
- package/src/modules/devtools/versions.test.ts +74 -0
- package/src/modules/devtools/versions.ts +110 -0
- package/src/modules/feature-flag/index.test.ts +251 -0
- package/src/modules/feature-flag/index.ts +308 -0
- package/src/modules/ref-tables.test.ts +91 -0
- package/src/modules/ref-tables.ts +88 -0
- package/src/modules/sync/engine.ts +164 -82
- package/src/modules/sync/events/index.ts +9 -2
- package/src/modules/sync/queue/queue-down.test.ts +180 -0
- package/src/modules/sync/queue/queue-down.ts +80 -13
- package/src/modules/sync/queue/queue-up.forwarded.test.ts +164 -0
- package/src/modules/sync/queue/queue-up.ts +241 -57
- package/src/modules/sync/scheduler.pause.test.ts +109 -0
- package/src/modules/sync/scheduler.retry.test.ts +237 -0
- package/src/modules/sync/scheduler.ts +215 -13
- package/src/modules/sync/sync.cleanup.test.ts +116 -0
- package/src/modules/sync/sync.health.test.ts +149 -0
- package/src/modules/sync/sync.heartbeat.test.ts +80 -0
- package/src/modules/sync/sync.live-removal.test.ts +175 -0
- package/src/modules/sync/sync.reconnect.test.ts +145 -0
- package/src/modules/sync/sync.subquery.test.ts +82 -0
- package/src/modules/sync/sync.tabs.test.ts +249 -0
- package/src/modules/sync/sync.ts +1726 -99
- package/src/modules/sync/utils.test.ts +269 -2
- package/src/modules/sync/utils.ts +201 -17
- package/src/otel/index.ts +13 -10
- package/src/services/blobs/blob-cache.test.ts +359 -0
- package/src/services/blobs/blob-cache.ts +603 -0
- package/src/services/blobs/blob-manifest.ts +227 -0
- package/src/services/blobs/blob-store.test.ts +77 -0
- package/src/services/blobs/blob-store.ts +359 -0
- package/src/services/blobs/blob.fixture.ts +90 -0
- package/src/services/blobs/index.ts +70 -0
- package/src/services/database/cache-engine.ts +193 -0
- package/src/services/database/connection-supervisor.test.ts +289 -0
- package/src/services/database/connection-supervisor.ts +415 -0
- package/src/services/database/database.query-timeout.test.ts +83 -0
- package/src/services/database/database.ts +41 -12
- 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 +413 -0
- package/src/services/database/relation-resolver.ts +0 -0
- package/src/services/database/remote.ts +110 -14
- package/src/services/database/sqlite-cache-engine.test.ts +616 -0
- package/src/services/database/sqlite-cache-engine.timeout.test.ts +61 -0
- package/src/services/database/sqlite-cache-engine.ts +1358 -0
- package/src/services/database/sqlite-devtools-queries.integration.test.ts +143 -0
- package/src/services/database/sqlite-devtools-queries.test.ts +154 -0
- package/src/services/database/sqlite-lock-verify.test.ts +33 -0
- package/src/services/database/sqlite-lock-verify.ts +45 -0
- package/src/services/database/sqlite-open.test.ts +150 -0
- package/src/services/database/sqlite-open.ts +164 -0
- package/src/services/database/sqlite-plan-sql.test.ts +104 -0
- package/src/services/database/sqlite-plan-sql.ts +138 -0
- package/src/services/database/sqlite-projection.test.ts +99 -0
- package/src/services/database/sqlite-select.integration.test.ts +185 -0
- package/src/services/database/sqlite-select.test.ts +246 -0
- package/src/services/database/sqlite-select.ts +131 -0
- package/src/services/database/sqlite-transport.fixture.ts +30 -0
- package/src/services/database/sqlite-transport.ts +224 -0
- package/src/services/database/sqlite-worker.ts +437 -0
- package/src/services/database/surql-translate.ts +416 -0
- package/src/services/database/surreal-cache-engine.ts +161 -0
- package/src/services/logger/index.ts +3 -2
- package/src/services/persistence/localstorage.ts +2 -2
- package/src/services/persistence/resilient.ts +11 -4
- package/src/services/persistence/surrealdb.ts +10 -10
- package/src/services/stream-processor/index.ts +796 -84
- package/src/services/stream-processor/permissions.test.ts +47 -0
- package/src/services/stream-processor/permissions.ts +53 -0
- package/src/services/stream-processor/stream-processor.batch.test.ts +186 -0
- package/src/services/stream-processor/stream-processor.prime.test.ts +198 -0
- package/src/services/stream-processor/stream-processor.reset.test.ts +226 -0
- package/src/services/stream-processor/stream-processor.test.ts +1 -1
- package/src/services/stream-processor/wasm-types.ts +59 -3
- package/src/services/tabs/broker-client.ts +283 -0
- package/src/services/tabs/broker.test.ts +327 -0
- package/src/services/tabs/coordinator.test.ts +365 -0
- package/src/services/tabs/coordinator.ts +633 -0
- package/src/services/tabs/fake-ports.fixture.ts +112 -0
- package/src/services/tabs/leader-locks.ts +75 -0
- package/src/services/tabs/protocol.ts +258 -0
- package/src/services/tabs/support.ts +36 -0
- package/src/services/tabs/tabs-broker-worker.ts +640 -0
- package/src/sp00ky.auth-order.test.ts +92 -0
- package/src/sp00ky.init-query.test.ts +183 -0
- package/src/sp00ky.local-first.test.ts +60 -0
- package/src/sp00ky.ts +1693 -0
- package/src/types.ts +528 -13
- package/src/utils/blurhash.ts +90 -0
- package/src/utils/error-classification.test.ts +44 -0
- package/src/utils/error-classification.ts +7 -0
- package/src/utils/index.ts +79 -13
- package/src/utils/parser.test.ts +49 -120
- package/src/utils/parser.ts +32 -2
- package/src/utils/semver.test.ts +32 -0
- package/src/utils/semver.ts +30 -0
- package/src/utils/surql.ts +30 -18
- package/src/utils/withRetry.test.ts +1 -1
- package/tsdown.config.ts +86 -1
- package/src/spooky.ts +0 -395
package/dist/index.d.ts
CHANGED
|
@@ -1,81 +1,247 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { A as Sp00kyQueryResultPromise, B as LocalStore, C as ReconnectConfig, D as RunOptions, E as RegistrationTimings, F as SyncHealthConfig, G as SyncEventSystem, H as DatabaseEventSystem, I as SyncHealthStatus, K as EventDefinition, L as TimingPhase, M as StorageHealthStatus, N as StoreType, O as Sp00kyConfig, P as SyncHealth, R as UpdateOptions, S as QueryUpdateCallback, T as RecordVersionDiff, U as DatabaseEventTypes, V as SealedQuery, W as Logger$1, _ as QueryState, a as MATERIALIZATION_SAMPLE_WINDOW, b as QueryTimeToLive, c as MutationEventType, d as PinoTransmit, f as PreloadOptions, g as QueryHash, h as QueryConfigRecord, i as Level, j as StorageHealth, k as Sp00kyQueryResult, l as PersistenceClient, m as QueryConfig, n as DebounceOptions, o as MutationCallback, p as PreloadRefresh, q as EventSystem, r as EventSubscriptionOptions, s as MutationEvent, t as ConnectionState, u as PhaseStat, v as QueryStatus, w as RecordVersionArray, x as QueryTimings, y as QueryStatusCallback, z as UpEvent } from "./types.js";
|
|
2
2
|
import * as surrealdb0 from "surrealdb";
|
|
3
|
-
import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
|
|
4
|
-
import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
|
|
3
|
+
import { Duration, RecordId, Surreal as Surreal$1, SurrealEvents, SurrealTransaction } from "surrealdb";
|
|
4
|
+
import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, FinalQuery, GetTable, QueryBuilder, QueryOptions, QueryPlan, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
|
|
5
5
|
import { Logger } from "pino";
|
|
6
|
+
import { decode, encode, isBlurhashValid } from "blurhash";
|
|
7
|
+
import { LoroDoc } from "loro-crdt";
|
|
6
8
|
|
|
7
|
-
//#region src/services/database/events/index.d.ts
|
|
8
|
-
declare const DatabaseEventTypes: {
|
|
9
|
-
readonly LocalQuery: "DATABASE_LOCAL_QUERY";
|
|
10
|
-
readonly RemoteQuery: "DATABASE_REMOTE_QUERY";
|
|
11
|
-
};
|
|
12
|
-
interface DatabaseQueryEventPayload {
|
|
13
|
-
query: string;
|
|
14
|
-
vars?: Record<string, unknown>;
|
|
15
|
-
duration: number;
|
|
16
|
-
success: boolean;
|
|
17
|
-
error?: string;
|
|
18
|
-
timestamp: number;
|
|
19
|
-
}
|
|
20
|
-
type DatabaseEventTypeMap = {
|
|
21
|
-
[DatabaseEventTypes.LocalQuery]: EventDefinition<typeof DatabaseEventTypes.LocalQuery, DatabaseQueryEventPayload>;
|
|
22
|
-
[DatabaseEventTypes.RemoteQuery]: EventDefinition<typeof DatabaseEventTypes.RemoteQuery, DatabaseQueryEventPayload>;
|
|
23
|
-
};
|
|
24
|
-
type DatabaseEventSystem = EventSystem<DatabaseEventTypeMap>;
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/utils/surql.d.ts
|
|
27
|
-
interface SealedQuery<T = void> {
|
|
28
|
-
readonly sql: string;
|
|
29
|
-
readonly extract: (results: unknown[]) => T;
|
|
30
|
-
}
|
|
31
|
-
//#endregion
|
|
32
9
|
//#region src/services/database/database.d.ts
|
|
33
10
|
declare abstract class AbstractDatabaseService {
|
|
34
|
-
protected client: Surreal;
|
|
11
|
+
protected client: Surreal$1;
|
|
35
12
|
protected logger: Logger$1;
|
|
36
13
|
protected events: DatabaseEventSystem;
|
|
14
|
+
/**
|
|
15
|
+
* Per-query deadline in ms; `0` disables. The remote service sets it from
|
|
16
|
+
* `queryTimeoutMs` (see `RemoteDatabaseService`), the local one from
|
|
17
|
+
* `localOpTimeoutMs` (see `LocalDatabaseService`): a local query can be
|
|
18
|
+
* legitimately slow, but it must never be endless - every query waits on the
|
|
19
|
+
* previous link of {@link query}'s chain, and one that never settled wedged
|
|
20
|
+
* every later local op behind it.
|
|
21
|
+
*/
|
|
22
|
+
protected queryTimeoutMs: number;
|
|
23
|
+
/** The error a deadline expiry rejects with; the local service substitutes
|
|
24
|
+
* its typed `LocalOpTimeoutError`. "timed out" in the message is
|
|
25
|
+
* load-bearing either way: `classifySyncError` keys off it. */
|
|
26
|
+
protected timeoutError(_query: string): Error;
|
|
37
27
|
protected abstract eventType: typeof DatabaseEventTypes.LocalQuery | typeof DatabaseEventTypes.RemoteQuery;
|
|
38
|
-
constructor(client: Surreal, logger: Logger$1, events: DatabaseEventSystem);
|
|
28
|
+
constructor(client: Surreal$1, logger: Logger$1, events: DatabaseEventSystem);
|
|
39
29
|
abstract connect(): Promise<void>;
|
|
40
|
-
getClient(): Surreal;
|
|
30
|
+
getClient(): Surreal$1;
|
|
41
31
|
getEvents(): DatabaseEventSystem;
|
|
42
32
|
tx(): Promise<SurrealTransaction>;
|
|
43
33
|
private queryQueue;
|
|
44
34
|
/**
|
|
45
35
|
* Execute a query with serialized execution to prevent WASM transaction issues.
|
|
36
|
+
*
|
|
37
|
+
* Serialization means every query waits on the previous one, so a call that
|
|
38
|
+
* never settles blocks the whole chain forever. {@link queryTimeoutMs} bounds
|
|
39
|
+
* each link: on expiry this promise rejects and the chain moves on, even
|
|
40
|
+
* though the underlying RPC is still parked in the SDK's pending map.
|
|
46
41
|
*/
|
|
47
42
|
query<T extends unknown[]>(query: string, vars?: Record<string, unknown>): Promise<T>;
|
|
48
43
|
execute<T>(query: SealedQuery<T>, vars?: Record<string, unknown>): Promise<T>;
|
|
49
44
|
close(): Promise<void>;
|
|
50
45
|
}
|
|
51
46
|
//#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
47
|
//#region src/services/database/remote.d.ts
|
|
48
|
+
/** Transport events the SDK publishes, mapped 1:1 to {@link ConnectionState}. */
|
|
49
|
+
type RemoteConnectionEvent = ConnectionState | 'error';
|
|
62
50
|
declare class RemoteDatabaseService extends AbstractDatabaseService {
|
|
63
51
|
private config;
|
|
64
52
|
protected eventType: "DATABASE_REMOTE_QUERY";
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
private readonly reconnectConfig;
|
|
54
|
+
/**
|
|
55
|
+
* In-flight `connect()`, so concurrent callers (boot + supervisor revive +
|
|
56
|
+
* an `online` event landing at the same moment) share one attempt instead of
|
|
57
|
+
* racing two sockets. Cleared on settle, so a later call always reconnects.
|
|
58
|
+
*/
|
|
59
|
+
private connecting;
|
|
60
|
+
constructor(config: Sp00kyConfig<any>['database'], logger: Logger$1);
|
|
61
|
+
getConfig(): Sp00kyConfig<any>['database'];
|
|
62
|
+
/** Resolved reconnect tunables; the supervisor reads its own knobs here. */
|
|
63
|
+
getReconnectConfig(): Required<ReconnectConfig>;
|
|
64
|
+
/** Current transport state as reported by the SDK. */
|
|
65
|
+
getStatus(): ConnectionState;
|
|
66
|
+
/**
|
|
67
|
+
* Observe transport events. Thin passthrough so callers (the supervisor,
|
|
68
|
+
* sync, CRDT) don't have to reach through `getClient()`.
|
|
69
|
+
*/
|
|
70
|
+
subscribeConnection<K extends RemoteConnectionEvent>(event: K, cb: (...payload: SurrealEvents[K]) => void): () => void;
|
|
71
|
+
/**
|
|
72
|
+
* Tear the socket down on purpose. Used by the heartbeat watchdog when a
|
|
73
|
+
* socket stops answering but never closes: `close()` makes the SDK publish
|
|
74
|
+
* `disconnected`, which is what drives the supervisor's revive loop.
|
|
75
|
+
*/
|
|
76
|
+
forceClose(): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Open (or re-open) the remote connection.
|
|
79
|
+
*
|
|
80
|
+
* Safe to call repeatedly: concurrent calls share the in-flight attempt, and
|
|
81
|
+
* a call after a `disconnected` builds a fresh socket. `use()` and
|
|
82
|
+
* `authenticate()` are re-applied here for the cold path; the SDK also
|
|
83
|
+
* replays them itself on its own internal reconnects.
|
|
84
|
+
*/
|
|
67
85
|
connect(): Promise<void>;
|
|
86
|
+
private doConnect;
|
|
68
87
|
signin(params: any): Promise<any>;
|
|
69
88
|
signup(params: any): Promise<any>;
|
|
70
89
|
authenticate(token: string): Promise<any>;
|
|
71
90
|
invalidate(): Promise<void>;
|
|
72
91
|
}
|
|
73
92
|
//#endregion
|
|
93
|
+
//#region src/services/database/connection-supervisor.d.ts
|
|
94
|
+
/**
|
|
95
|
+
* Keeps the remote WebSocket alive for the whole life of the page.
|
|
96
|
+
*
|
|
97
|
+
* The SurrealDB SDK reconnects on its own after a socket `close`, but that
|
|
98
|
+
* covers only one of three ways the connection dies:
|
|
99
|
+
*
|
|
100
|
+
* 1. **Socket closes, SDK recovers.** Handled entirely by the SDK. This
|
|
101
|
+
* supervisor only observes it (to report `reconnecting` upward).
|
|
102
|
+
* 2. **Socket closes, SDK gives up.** With `attempts: -1` this shouldn't happen
|
|
103
|
+
* from exhaustion — but the SDK also terminates the engine permanently when
|
|
104
|
+
* its post-reconnect handshake throws (it re-runs `version()`, `use()`,
|
|
105
|
+
* `authenticate()` on every reconnect and closes the engine on any error).
|
|
106
|
+
* One transient hiccup there would otherwise kill the page's connection for
|
|
107
|
+
* good. The revive loop re-opens from scratch.
|
|
108
|
+
* 3. **Socket never closes at all.** A half-open connection: the peer is gone
|
|
109
|
+
* (NAT timeout, wifi switch, laptop sleep) but no FIN ever arrives, so
|
|
110
|
+
* `readyState` stays OPEN and the SDK's own 30s ping — fire-and-forget, no
|
|
111
|
+
* response deadline — never notices. Nothing ever fires a `close` event, so
|
|
112
|
+
* nothing ever triggers a reconnect. The heartbeat detects this and forces
|
|
113
|
+
* the teardown that case 2's loop then repairs.
|
|
114
|
+
*
|
|
115
|
+
* Plus wake triggers: coming back `online` or un-hiding the tab probes
|
|
116
|
+
* immediately rather than waiting out a backoff that was scheduled while the
|
|
117
|
+
* network was known-down.
|
|
118
|
+
*/
|
|
119
|
+
declare class ConnectionSupervisor {
|
|
120
|
+
private readonly remote;
|
|
121
|
+
private readonly logger;
|
|
122
|
+
private readonly config;
|
|
123
|
+
private state;
|
|
124
|
+
private subscribers;
|
|
125
|
+
private started;
|
|
126
|
+
private disposed;
|
|
127
|
+
private heartbeatTimer;
|
|
128
|
+
private heartbeatInFlight;
|
|
129
|
+
/** Consecutive failed probes. See {@link FAILURES_BEFORE_TEARDOWN}. */
|
|
130
|
+
private heartbeatFailures;
|
|
131
|
+
private reviveTimer;
|
|
132
|
+
private reviveAttempts;
|
|
133
|
+
/** Timestamp of the last wake-triggered probe, for rate limiting. */
|
|
134
|
+
private lastWakeProbeAt;
|
|
135
|
+
private reviving;
|
|
136
|
+
/**
|
|
137
|
+
* Set while the browser reports itself offline. Retrying a socket against a
|
|
138
|
+
* down interface only burns backoff, so the loop parks until `online` fires.
|
|
139
|
+
*/
|
|
140
|
+
private suspended;
|
|
141
|
+
private teardown;
|
|
142
|
+
private static readonly REVIVE_BASE_MS;
|
|
143
|
+
/**
|
|
144
|
+
* How many consecutive heartbeat failures it takes to tear the socket down.
|
|
145
|
+
*
|
|
146
|
+
* The probe rides the same serialized queue as every other RPC (deliberately
|
|
147
|
+
* — see {@link beat}), which means it cannot distinguish a WEDGED queue from
|
|
148
|
+
* a merely BUSY one. A single slow window (a large sync burst, one heavy
|
|
149
|
+
* app query) used to be enough to force-close a perfectly healthy socket,
|
|
150
|
+
* and the resulting reconnect re-registered every active query about a
|
|
151
|
+
* second later. That self-inflicted teardown manufactured the very reconnect
|
|
152
|
+
* storms this class exists to survive. A genuinely dead socket still fails
|
|
153
|
+
* every probe, so it is torn down one interval later than before.
|
|
154
|
+
*/
|
|
155
|
+
private static readonly FAILURES_BEFORE_TEARDOWN;
|
|
156
|
+
/** Retry delay after an inconclusive (first) heartbeat failure. */
|
|
157
|
+
private static readonly HEARTBEAT_RETRY_MS;
|
|
158
|
+
/** Floor between probes triggered by wake events (tab focus, pageshow). */
|
|
159
|
+
private static readonly WAKE_PROBE_MIN_INTERVAL_MS;
|
|
160
|
+
constructor(remote: RemoteDatabaseService, logger: Logger$1, config?: Required<ReconnectConfig>);
|
|
161
|
+
/** Latest observed transport state. */
|
|
162
|
+
get connection(): ConnectionState;
|
|
163
|
+
/**
|
|
164
|
+
* Observe transport state. Fires immediately with the current value and again
|
|
165
|
+
* on every change. Returns an unsubscribe.
|
|
166
|
+
*/
|
|
167
|
+
subscribe(cb: (state: ConnectionState) => void): () => void;
|
|
168
|
+
/**
|
|
169
|
+
* Begin supervising. Call once, after the initial {@link
|
|
170
|
+
* RemoteDatabaseService.connect}. Idempotent.
|
|
171
|
+
*/
|
|
172
|
+
start(): void;
|
|
173
|
+
/** Stop all timers and listeners. Safe to call more than once. */
|
|
174
|
+
dispose(): void;
|
|
175
|
+
private setState;
|
|
176
|
+
private clearReviveTimer;
|
|
177
|
+
/**
|
|
178
|
+
* Queue the next `connect()` attempt on exponential backoff, capped at
|
|
179
|
+
* `superviseRetryDelayMaxMs`. Never gives up — the page is expected to
|
|
180
|
+
* outlive any outage.
|
|
181
|
+
*/
|
|
182
|
+
private scheduleRevive;
|
|
183
|
+
private revive;
|
|
184
|
+
private stopHeartbeat;
|
|
185
|
+
private startHeartbeat;
|
|
186
|
+
/**
|
|
187
|
+
* Probe the server end-to-end. Deliberately goes through
|
|
188
|
+
* `remote.query` — the same serialized queue every other remote call uses —
|
|
189
|
+
* so a queue wedged behind a stuck RPC also fails the heartbeat instead of
|
|
190
|
+
* being invisible to it.
|
|
191
|
+
*/
|
|
192
|
+
private beat;
|
|
193
|
+
/**
|
|
194
|
+
* A restored network or an un-hidden tab is the strongest available hint that
|
|
195
|
+
* a reconnect will now succeed, so probe immediately instead of waiting out a
|
|
196
|
+
* backoff scheduled under worse conditions.
|
|
197
|
+
*/
|
|
198
|
+
private installWakeTriggers;
|
|
199
|
+
/**
|
|
200
|
+
* Reset the backoff and act on whichever problem is present: reconnect if the
|
|
201
|
+
* socket is gone, otherwise probe it (it may be half-open — which is exactly
|
|
202
|
+
* what a sleep/wake cycle produces).
|
|
203
|
+
*/
|
|
204
|
+
private wake;
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/modules/sync/queue/queue-down.d.ts
|
|
208
|
+
type RegisterEvent = {
|
|
209
|
+
type: 'register';
|
|
210
|
+
payload: {
|
|
211
|
+
hash: string;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
type SyncEvent = {
|
|
215
|
+
type: 'sync';
|
|
216
|
+
payload: {
|
|
217
|
+
hash: string;
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
type HeartbeatEvent = {
|
|
221
|
+
type: 'heartbeat';
|
|
222
|
+
payload: {
|
|
223
|
+
hash: string;
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
type CleanupEvent = {
|
|
227
|
+
type: 'cleanup';
|
|
228
|
+
payload: {
|
|
229
|
+
hash: string;
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
type DownEvent = RegisterEvent | SyncEvent | HeartbeatEvent | CleanupEvent;
|
|
233
|
+
//#endregion
|
|
74
234
|
//#region src/services/stream-processor/wasm-types.d.ts
|
|
75
235
|
interface WasmStreamUpdate {
|
|
76
236
|
query_id: string;
|
|
77
237
|
result_hash: string;
|
|
78
238
|
result_data: RecordVersionArray;
|
|
239
|
+
timing_store_apply_ms?: number;
|
|
240
|
+
timing_circuit_step_ms?: number;
|
|
241
|
+
timing_transform_ms?: number;
|
|
242
|
+
timing_parse_ms?: number;
|
|
243
|
+
timing_plan_ms?: number;
|
|
244
|
+
timing_snapshot_ms?: number;
|
|
79
245
|
}
|
|
80
246
|
//#endregion
|
|
81
247
|
//#region src/services/stream-processor/index.d.ts
|
|
@@ -96,6 +262,33 @@ interface StreamUpdate {
|
|
|
96
262
|
queryHash: string;
|
|
97
263
|
localArray: RecordVersionArray;
|
|
98
264
|
op?: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
265
|
+
/**
|
|
266
|
+
* Client-internal: not from the circuit. A membership-only change that
|
|
267
|
+
* needed no fetch is re-materialized through this same path so it cannot
|
|
268
|
+
* race a real update (DataModule.scheduleRematerialize). Carries the last
|
|
269
|
+
* known `localArray`; consumers that describe an INGEST (persist, metrics,
|
|
270
|
+
* devtools events) skip it.
|
|
271
|
+
*/
|
|
272
|
+
synthetic?: boolean;
|
|
273
|
+
/**
|
|
274
|
+
* End-to-end ingest latency for the WASM call that produced this update,
|
|
275
|
+
* in milliseconds. Populated by StreamProcessorService.ingest. Undefined
|
|
276
|
+
* for the initial register_view snapshot.
|
|
277
|
+
*/
|
|
278
|
+
materializationTimeMs?: number;
|
|
279
|
+
/** SSP internal sub-phase timings (ms) for this ingest, from the WASM binding. */
|
|
280
|
+
storeApplyMs?: number;
|
|
281
|
+
circuitStepMs?: number;
|
|
282
|
+
transformMs?: number;
|
|
283
|
+
/**
|
|
284
|
+
* One-shot registration timings (ms). Only set on the StreamUpdate returned
|
|
285
|
+
* by `registerQueryPlan` (the register_view snapshot), not on ingest updates.
|
|
286
|
+
*/
|
|
287
|
+
registration?: {
|
|
288
|
+
parseMs: number;
|
|
289
|
+
planMs: number;
|
|
290
|
+
snapshotMs: number;
|
|
291
|
+
};
|
|
99
292
|
}
|
|
100
293
|
type StreamProcessorEvents = {
|
|
101
294
|
stream_update: EventDefinition<'stream_update', StreamUpdate[]>;
|
|
@@ -107,34 +300,204 @@ type StreamProcessorEvents = {
|
|
|
107
300
|
interface StreamUpdateReceiver {
|
|
108
301
|
onStreamUpdate(update: StreamUpdate): void;
|
|
109
302
|
}
|
|
303
|
+
/** One row change in the shape `ingestMany` consumes. */
|
|
304
|
+
interface IngestRecord {
|
|
305
|
+
table: string;
|
|
306
|
+
/** `MERGE` overlays the given fields on the stored row (projection widening). */
|
|
307
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE' | 'MERGE';
|
|
308
|
+
id: string;
|
|
309
|
+
record: any;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* What the boot-time prime needs from the client: which tables to walk, how
|
|
313
|
+
* to recognise a snapshot written under a different schema, and which rows'
|
|
314
|
+
* local `_00_rv` must not be reported as the server's.
|
|
315
|
+
*/
|
|
316
|
+
interface CircuitPrimeContext {
|
|
317
|
+
tables: string[];
|
|
318
|
+
schemaHash: string;
|
|
319
|
+
/** Encoded ids with an unsettled local mutation (their `_00_rv` was bumped
|
|
320
|
+
* locally and may exceed the server's next version). */
|
|
321
|
+
pendingIds: Set<string>;
|
|
322
|
+
/** Receives every `(id, rv)` the prime put into the circuit, per table, so
|
|
323
|
+
* the sync layer can skip re-downloading bodies it already has. */
|
|
324
|
+
onVersions?: (table: string, entries: [string, number][]) => void;
|
|
325
|
+
}
|
|
326
|
+
/** Storage key of the circuit snapshot inside the local store. */
|
|
327
|
+
|
|
110
328
|
declare class StreamProcessorService {
|
|
111
329
|
events: EventSystem<StreamProcessorEvents>;
|
|
112
330
|
private db;
|
|
113
|
-
private persistenceClient;
|
|
114
331
|
private logger;
|
|
115
332
|
private processor;
|
|
116
333
|
private isInitialized;
|
|
117
334
|
private receivers;
|
|
118
|
-
|
|
335
|
+
private batching;
|
|
336
|
+
private batchBuffer;
|
|
337
|
+
private sessionAuth;
|
|
338
|
+
private stateGeneration;
|
|
339
|
+
private persistState;
|
|
340
|
+
private persistCircuit;
|
|
341
|
+
private checkpointMs;
|
|
342
|
+
private checkpointTimer;
|
|
343
|
+
private snapshotDirty;
|
|
344
|
+
private dirtyRows;
|
|
345
|
+
private hideHandler;
|
|
346
|
+
private checkpointInFlight;
|
|
347
|
+
private projection;
|
|
348
|
+
private primed;
|
|
349
|
+
private schemaHash;
|
|
350
|
+
private widenQueue;
|
|
351
|
+
private widenPending;
|
|
352
|
+
constructor(events: EventSystem<StreamProcessorEvents>, db: LocalStore, logger: Logger);
|
|
119
353
|
/**
|
|
120
354
|
* Add a receiver for stream updates.
|
|
121
355
|
* Multiple receivers can be registered (DataManager, DevTools, etc.)
|
|
122
356
|
*/
|
|
123
357
|
addReceiver(receiver: StreamUpdateReceiver): void;
|
|
124
358
|
private notifyUpdates;
|
|
359
|
+
private dispatchUpdates;
|
|
360
|
+
/**
|
|
361
|
+
* Ingest a batch of record changes, firing one coalesced `StreamUpdate` per
|
|
362
|
+
* affected query once every record has been ingested. Use this whenever
|
|
363
|
+
* multiple records land at once (sync fetching N rows, the boot prime).
|
|
364
|
+
*
|
|
365
|
+
* The batch is fed to the wasm side in chunks of {@link INGEST_CHUNK}: one
|
|
366
|
+
* circuit step per chunk (a step walks every registered view, so per-record
|
|
367
|
+
* ingest paid that fixed cost N times), but never the whole batch at once,
|
|
368
|
+
* because the wasm side has to hold every parsed row of a call at the same
|
|
369
|
+
* time and wasm32 dlmalloc never returns that peak.
|
|
370
|
+
*
|
|
371
|
+
* Returns the records that were ingested. A chunk that fails is reported
|
|
372
|
+
* and skipped, not retried (a retry would double-apply whatever the failed
|
|
373
|
+
* step already committed), and the remaining chunks still run.
|
|
374
|
+
*/
|
|
375
|
+
ingestMany(records: IngestRecord[]): IngestRecord[];
|
|
376
|
+
/**
|
|
377
|
+
* Open a coalescing window. While open, the per-record stream updates
|
|
378
|
+
* emitted by `ingest` are buffered (one entry per queryHash) instead of
|
|
379
|
+
* dispatched. Always paired with `flushCoalescing()` in a try/finally by
|
|
380
|
+
* `ingestMany` so the window always closes — otherwise the processor stays
|
|
381
|
+
* stuck buffering forever.
|
|
382
|
+
*
|
|
383
|
+
* No-op if a window is already open (nested batches aren't expected here).
|
|
384
|
+
*/
|
|
385
|
+
private beginCoalescing;
|
|
386
|
+
/**
|
|
387
|
+
* Close the coalescing window and flush: dispatch one coalesced
|
|
388
|
+
* `StreamUpdate` per buffered queryHash, then persist processor state once
|
|
389
|
+
* for the whole batch (instead of once per ingest).
|
|
390
|
+
*/
|
|
391
|
+
private flushCoalescing;
|
|
125
392
|
/**
|
|
126
393
|
* Initialize the WASM module and processor.
|
|
127
394
|
* This must be called before using other methods.
|
|
128
395
|
*/
|
|
129
396
|
init(): Promise<void>;
|
|
130
|
-
|
|
131
|
-
|
|
397
|
+
/**
|
|
398
|
+
* Drop the current WASM processor and start a fresh, empty circuit. Used on
|
|
399
|
+
* local-bucket switches: the old circuit holds the previous user's rows AND
|
|
400
|
+
* views registered with the previous `$auth` context, so neither may survive.
|
|
401
|
+
* Deliberately loads nothing: the snapshot in the store being swapped away
|
|
402
|
+
* from belongs to the previous bucket; the caller primes the new bucket's
|
|
403
|
+
* circuit (`primeFromLocal`) once its store is open, and the DataModule
|
|
404
|
+
* rebind re-registers every live view against this fresh processor. Caller
|
|
405
|
+
* must re-seed `setPermissions` afterwards (a fresh circuit default-denies
|
|
406
|
+
* every table).
|
|
407
|
+
*/
|
|
408
|
+
reset(): Promise<void>;
|
|
409
|
+
/**
|
|
410
|
+
* Release the wasm circuit and stop checkpointing. Call when the client is
|
|
411
|
+
* torn down; a recreated client (provider remount, HMR) would otherwise stack
|
|
412
|
+
* one full circuit per instance.
|
|
413
|
+
*/
|
|
414
|
+
dispose(): void;
|
|
415
|
+
/**
|
|
416
|
+
* Explicitly run the wasm-bindgen destructor. Guarded: stale wasm builds may
|
|
417
|
+
* not expose `free`, and a double free must not take the app down.
|
|
418
|
+
*/
|
|
419
|
+
private freeProcessor;
|
|
420
|
+
/** Toggle circuit-state persistence (shared-tabs follower/leader role). */
|
|
421
|
+
setPersistenceEnabled(enabled: boolean): void;
|
|
422
|
+
/**
|
|
423
|
+
* Snapshot persistence (`persistCircuit`). When on, the circuit's store is
|
|
424
|
+
* written to the local store on a checkpoint interval and when the page
|
|
425
|
+
* goes hidden, and restored by {@link primeFromLocal} on the next boot.
|
|
426
|
+
*/
|
|
427
|
+
configureCircuitPersistence(enabled: boolean, checkpointMs?: number): void;
|
|
428
|
+
/**
|
|
429
|
+
* Field projection (`circuitProjection`, default on). Takes effect on the
|
|
430
|
+
* next processor (`init`/`reset`) and on rows written after that.
|
|
431
|
+
*/
|
|
432
|
+
configureProjection(enabled: boolean): void;
|
|
433
|
+
private applyProjection;
|
|
434
|
+
/** Resolves once the boot-time prime has finished (or was skipped). */
|
|
435
|
+
whenPrimed(): Promise<void>;
|
|
436
|
+
/**
|
|
437
|
+
* Fill the circuit from the LOCAL store, in the background.
|
|
438
|
+
*
|
|
439
|
+
* With a usable snapshot: install it under whatever views have registered
|
|
440
|
+
* meanwhile (`load_store_state` re-primes them), then `reconcile` each table
|
|
441
|
+
* against the store's `(id, rv)` list so rows deleted since the checkpoint
|
|
442
|
+
* are stepped out and only rows added or changed since are read back and
|
|
443
|
+
* ingested. Without one: read every row and ingest it, chunked.
|
|
444
|
+
*
|
|
445
|
+
* Either way the circuit ends up equal to the local store without touching
|
|
446
|
+
* the network, so the first sync diff is a real delta rather than "fetch
|
|
447
|
+
* everything". The returned promise never rejects; `whenPrimed` gates on it.
|
|
448
|
+
*/
|
|
449
|
+
primeFromLocal(ctx: CircuitPrimeContext): Promise<void>;
|
|
450
|
+
private runPrime;
|
|
451
|
+
/** Publish wasm updates produced outside an ingest (restore, reconcile). */
|
|
452
|
+
private dispatchWasmUpdates;
|
|
453
|
+
/**
|
|
454
|
+
* Record that the circuit changed by `rows` rows. Cheap; the snapshot is
|
|
455
|
+
* deferred to the checkpoint timer and skipped entirely when `persistCircuit`
|
|
456
|
+
* is off.
|
|
457
|
+
*/
|
|
458
|
+
private markSnapshotDirty;
|
|
459
|
+
private startCheckpoints;
|
|
460
|
+
/** Stop checkpointing and drop the visibility listeners. */
|
|
461
|
+
stopCheckpoints(): void;
|
|
462
|
+
/**
|
|
463
|
+
* Write the circuit's store to the local store as a snapshot. Compacts the
|
|
464
|
+
* row arena first when dead bytes outweigh live ones. Serialised: a second
|
|
465
|
+
* call while one is in flight joins it. No-op unless persistence is on, this
|
|
466
|
+
* tab owns the store, and the engine can hold a snapshot.
|
|
467
|
+
*/
|
|
468
|
+
checkpoint(reason?: string): Promise<void>;
|
|
469
|
+
private runCheckpoint;
|
|
470
|
+
/**
|
|
471
|
+
* Projection widening: a newly registered view evaluates fields that rows
|
|
472
|
+
* already in the circuit were stored without. Merge just those fields in,
|
|
473
|
+
* table by table, from the local store. The view registered against what
|
|
474
|
+
* was present and converges as the merges step through.
|
|
475
|
+
*/
|
|
476
|
+
private scheduleWiden;
|
|
477
|
+
private runWiden;
|
|
478
|
+
/**
|
|
479
|
+
* Seed per-table `select` permission predicates ({ [table]: whereText }).
|
|
480
|
+
* Must run after the processor exists and before any `register_view`, else
|
|
481
|
+
* non-`_00_` tables are default-denied and registration fails.
|
|
482
|
+
*/
|
|
483
|
+
setPermissions(permissions: Record<string, string>): void;
|
|
484
|
+
/**
|
|
485
|
+
* Set the current session's auth identity for permission injection,
|
|
486
|
+
* mirroring the server's `fn::query::register`
|
|
487
|
+
* (`object::extend(params, { auth: { id: $auth.id }, access: $access })`).
|
|
488
|
+
* Stored as strings (empty when logged out) and applied to every
|
|
489
|
+
* `register_view` in {@link registerQueryPlan}. Must be set before a
|
|
490
|
+
* `$auth`-gated query registers (and re-set on auth state changes), or the
|
|
491
|
+
* in-browser SSP's `permission_inject` rejects it with
|
|
492
|
+
* "requires $auth but registration params lack it".
|
|
493
|
+
*/
|
|
494
|
+
setSessionAuth(authId: string | null, access: string | null): void;
|
|
132
495
|
/**
|
|
133
496
|
* Ingest a record change into the processor.
|
|
134
497
|
* Emits 'stream_update' event if materialized views are affected.
|
|
135
498
|
* @param isOptimistic true = local mutation (increment versions), false = remote sync (keep versions)
|
|
136
499
|
*/
|
|
137
|
-
ingest(table: string, op: '
|
|
500
|
+
ingest(table: string, op: IngestRecord['op'], id: string, record: any): WasmStreamUpdate[];
|
|
138
501
|
/**
|
|
139
502
|
* Register a new query plan.
|
|
140
503
|
* Emits 'stream_update' with the initial result.
|
|
@@ -147,6 +510,1153 @@ declare class StreamProcessorService {
|
|
|
147
510
|
private normalizeValue;
|
|
148
511
|
}
|
|
149
512
|
//#endregion
|
|
513
|
+
//#region src/modules/cache/types.d.ts
|
|
514
|
+
type RecordWithId = Record<string, any> & {
|
|
515
|
+
id: RecordId<string>;
|
|
516
|
+
};
|
|
517
|
+
interface QueryConfig$1 {
|
|
518
|
+
queryHash: string;
|
|
519
|
+
surql: string;
|
|
520
|
+
params: Record<string, any>;
|
|
521
|
+
ttl: QueryTimeToLive | Duration;
|
|
522
|
+
lastActiveAt: Date;
|
|
523
|
+
}
|
|
524
|
+
interface CacheRecord {
|
|
525
|
+
table: string;
|
|
526
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
527
|
+
record: RecordWithId;
|
|
528
|
+
version: number;
|
|
529
|
+
}
|
|
530
|
+
//#endregion
|
|
531
|
+
//#region src/modules/cache/index.d.ts
|
|
532
|
+
/**
|
|
533
|
+
* CacheModule - Centralized storage and DBSP ingestion
|
|
534
|
+
*
|
|
535
|
+
* Single responsibility: Handle all local storage operations and DBSP ingestion.
|
|
536
|
+
* This module acts as the bridge between data operations and persistence.
|
|
537
|
+
*/
|
|
538
|
+
/** One ingested change, in exactly the shape `ingestMany` consumes. Shared
|
|
539
|
+
* with the tabs protocol so a leader can relay its ingests to followers. */
|
|
540
|
+
interface CacheIngestTuple {
|
|
541
|
+
table: string;
|
|
542
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
543
|
+
id: string;
|
|
544
|
+
record: Record<string, unknown>;
|
|
545
|
+
}
|
|
546
|
+
declare class CacheModule implements StreamUpdateReceiver {
|
|
547
|
+
private local;
|
|
548
|
+
private streamProcessor;
|
|
549
|
+
private logger;
|
|
550
|
+
private streamUpdateCallback;
|
|
551
|
+
private versionLookups;
|
|
552
|
+
/** Shared-tabs leader: fan every committed ingest out to follower circuits.
|
|
553
|
+
* Fired AFTER the local tx (the rows are already in the shared store, so a
|
|
554
|
+
* follower only needs the circuit feed). A follower relays its own
|
|
555
|
+
* mutations to the leader the same way, see {@link setIngestRelay}. */
|
|
556
|
+
private ingestRelay;
|
|
557
|
+
/** See {@link setIngestRelay}. */
|
|
558
|
+
private relayLocalWritesOnly;
|
|
559
|
+
constructor(local: LocalStore, streamProcessor: StreamProcessorService, streamUpdateCallback: (update: StreamUpdate) => void, logger: Logger$1);
|
|
560
|
+
/**
|
|
561
|
+
* Implements StreamUpdateReceiver interface
|
|
562
|
+
* Called directly by StreamProcessor when views change
|
|
563
|
+
*/
|
|
564
|
+
onStreamUpdate(update: StreamUpdate): void;
|
|
565
|
+
/**
|
|
566
|
+
* Fan every committed ingest out to the other tabs. The leader relays
|
|
567
|
+
* everything (its sync fetches are the only copy the followers get). A
|
|
568
|
+
* follower relays with `localWritesOnly`: just the mutation path, which is
|
|
569
|
+
* the only thing it knows that the leader does not. Its sync-fetched
|
|
570
|
+
* batches are the leader's data coming back and must not be re-broadcast,
|
|
571
|
+
* or every follower registration would fan its whole working set to
|
|
572
|
+
* every tab.
|
|
573
|
+
*/
|
|
574
|
+
setIngestRelay(cb: ((tuples: CacheIngestTuple[]) => void) | null, opts?: {
|
|
575
|
+
localWritesOnly?: boolean;
|
|
576
|
+
}): void;
|
|
577
|
+
/**
|
|
578
|
+
* Shared-tabs follower: feed relayed tuples into THIS tab's circuit only.
|
|
579
|
+
* The rows are already in the shared store (the leader wrote them), so no
|
|
580
|
+
* local write happens here; the normal chain then runs: SSP -> stream update
|
|
581
|
+
* -> DataModule debounce -> materializeRecords (re-reads via the port
|
|
582
|
+
* transport) -> this tab's subscriptions fire with this tab's hashes.
|
|
583
|
+
*/
|
|
584
|
+
applyRelayedIngest(tuples: CacheIngestTuple[]): void;
|
|
585
|
+
lookup(recordId: string): number;
|
|
586
|
+
/**
|
|
587
|
+
* Seed the version memo from rows the circuit was primed with out of the
|
|
588
|
+
* local store, so the first post-reload sync diff does not re-download
|
|
589
|
+
* bodies the browser already has. Only rows the prime actually put into the
|
|
590
|
+
* circuit belong here: a memo entry with no circuit row would make the diff
|
|
591
|
+
* flag the id forever while nothing ever fetches it.
|
|
592
|
+
*/
|
|
593
|
+
primeVersions(entries: [string, number][]): void;
|
|
594
|
+
/** Drop the version cache on a bucket switch — a stale version would make
|
|
595
|
+
* the sync diff skip fetching a body the new bucket legitimately needs. */
|
|
596
|
+
clearVersionLookups(): void;
|
|
597
|
+
/**
|
|
598
|
+
* Save a single record to local DB and ingest into DBSP
|
|
599
|
+
* Used by mutations (create/update)
|
|
600
|
+
*/
|
|
601
|
+
save(cacheRecord: CacheRecord, skipDbInsert?: boolean): Promise<void>;
|
|
602
|
+
/**
|
|
603
|
+
* Save multiple records in a batch
|
|
604
|
+
* More efficient than calling save() multiple times
|
|
605
|
+
* Used by sync operations
|
|
606
|
+
*/
|
|
607
|
+
saveBatch(records: CacheRecord[], skipDbInsert?: boolean): Promise<void>;
|
|
608
|
+
/**
|
|
609
|
+
* Delete a record from local DB and ingest deletion into DBSP
|
|
610
|
+
*/
|
|
611
|
+
delete(table: string, id: string, skipDbDelete?: boolean, recordData?: Record<string, any>): Promise<void>;
|
|
612
|
+
/**
|
|
613
|
+
* Register a query with DBSP to create a materialized view
|
|
614
|
+
* Returns the initial result array
|
|
615
|
+
*/
|
|
616
|
+
registerQuery(config: QueryConfig$1): {
|
|
617
|
+
localArray: RecordVersionArray;
|
|
618
|
+
registrationTimings?: {
|
|
619
|
+
parseMs: number;
|
|
620
|
+
planMs: number;
|
|
621
|
+
snapshotMs: number;
|
|
622
|
+
};
|
|
623
|
+
};
|
|
624
|
+
/**
|
|
625
|
+
* Unregister a query from DBSP
|
|
626
|
+
*/
|
|
627
|
+
unregisterQuery(queryHash: string): void;
|
|
628
|
+
}
|
|
629
|
+
//#endregion
|
|
630
|
+
//#region src/modules/data/index.d.ts
|
|
631
|
+
/**
|
|
632
|
+
* DataModule - Unified query and mutation management
|
|
633
|
+
*
|
|
634
|
+
* Merges the functionality of QueryManager and MutationManager.
|
|
635
|
+
* Uses CacheModule for all storage operations.
|
|
636
|
+
*/
|
|
637
|
+
/** A `_00_window` row as read back: the id-set and whether the server vouched
|
|
638
|
+
* for it (which is what allows an empty set to count as known membership). */
|
|
639
|
+
interface DurableMembership {
|
|
640
|
+
ids: RecordVersionArray;
|
|
641
|
+
confirmed: boolean;
|
|
642
|
+
}
|
|
643
|
+
declare class DataModule<S extends SchemaStructure> {
|
|
644
|
+
private cache;
|
|
645
|
+
private local;
|
|
646
|
+
private schema;
|
|
647
|
+
private streamDebounceTime;
|
|
648
|
+
/** Tab identity baked into mutation ids (shared-tabs rollback routing);
|
|
649
|
+
* undefined in solo mode, where mutation-id falls back to a session id. */
|
|
650
|
+
private tabId;
|
|
651
|
+
private activeQueries;
|
|
652
|
+
private pendingQueries;
|
|
653
|
+
private subscriptions;
|
|
654
|
+
private statusSubscriptions;
|
|
655
|
+
private mutationCallbacks;
|
|
656
|
+
private debounceTimers;
|
|
657
|
+
private pendingStreamUpdates;
|
|
658
|
+
private fetchDepth;
|
|
659
|
+
private logger;
|
|
660
|
+
/**
|
|
661
|
+
* Optional observer notified whenever a query's fetch status changes.
|
|
662
|
+
* Wired by Sp00kyClient to push status changes into DevTools. Kept as a
|
|
663
|
+
* settable field (rather than a constructor arg) because DevTools is
|
|
664
|
+
* constructed after DataModule.
|
|
665
|
+
*/
|
|
666
|
+
onQueryStatusChange?: (hash: QueryHash, status: QueryStatus) => void;
|
|
667
|
+
/**
|
|
668
|
+
* Optional observer invoked when a still-subscribed query's TTL heartbeat
|
|
669
|
+
* fires (~90% of the TTL). Wired by Sp00kyClient to
|
|
670
|
+
* `Sp00kySync.heartbeatQuery`, which refreshes the remote `_00_query`
|
|
671
|
+
* row's `lastActiveAt` so an actively-watched query never expires. Settable
|
|
672
|
+
* field (not a constructor arg) because the sync engine is wired after
|
|
673
|
+
* DataModule is constructed — mirrors `onQueryStatusChange`.
|
|
674
|
+
*/
|
|
675
|
+
onHeartbeat?: (hash: QueryHash) => void;
|
|
676
|
+
/**
|
|
677
|
+
* Optional hook fired by {@link deregisterQuery} when an opt-in query (e.g. a
|
|
678
|
+
* viewport-windowed list cancelling an off-screen window) loses its last
|
|
679
|
+
* subscriber. Wired by Sp00kyClient to enqueue a `cleanup` down-event, which
|
|
680
|
+
* tears the remote `_00_query` view down (releasing its `_00_list_ref` edges)
|
|
681
|
+
* instead of leaving it for the TTL sweep. The local view + state are freed in
|
|
682
|
+
* {@link finalizeDeregister} only after that remote delete, so a fast
|
|
683
|
+
* re-subscribe (scroll back) can abort/heal the teardown — see `cleanupQuery`.
|
|
684
|
+
*/
|
|
685
|
+
onDeregister?: (hash: QueryHash) => void;
|
|
686
|
+
private sessionId;
|
|
687
|
+
private currentUserId;
|
|
688
|
+
constructor(cache: CacheModule, local: LocalStore, schema: S, logger: Logger$1, streamDebounceTime?: number);
|
|
689
|
+
init(sessionId: string): Promise<void>;
|
|
690
|
+
/**
|
|
691
|
+
* Update the session salt used in query-id hashing. Call this when the
|
|
692
|
+
* SurrealDB session changes (sign-in, sign-out, reconnect). Subsequently
|
|
693
|
+
* registered queries will get fresh, session-scoped IDs.
|
|
694
|
+
*/
|
|
695
|
+
setSessionId(sessionId: string): void;
|
|
696
|
+
/** Shared-tabs: bake this tab's identity into mutation ids so a rollback of
|
|
697
|
+
* a follower's mutation routes back to the tab that made it. */
|
|
698
|
+
setTabId(tabId: string): void;
|
|
699
|
+
/**
|
|
700
|
+
* Update the authenticated user record id. Pass `null` on sign-out.
|
|
701
|
+
* Read by `Sp00kySync.listRefTable()` so the LIVE subscription and
|
|
702
|
+
* the poll route to the same per-user `_00_list_ref_user_<id>` the
|
|
703
|
+
* SSP writes to.
|
|
704
|
+
*/
|
|
705
|
+
setCurrentUserId(userId: string | null): void;
|
|
706
|
+
/** Read-only view of the authenticated user id used for per-user
|
|
707
|
+
* `_00_list_ref` routing. Other modules consult this so they pick the
|
|
708
|
+
* same table name DataModule does. */
|
|
709
|
+
getCurrentUserId(): string | null;
|
|
710
|
+
/**
|
|
711
|
+
* Register a query and return its hash for subscriptions
|
|
712
|
+
*/
|
|
713
|
+
query<T extends TableNames<S>>(tableName: T, surqlString: string, params: Record<string, any>, ttl: QueryTimeToLive, plan?: QueryPlan): Promise<QueryHash>;
|
|
714
|
+
/**
|
|
715
|
+
* Subscribe to query updates
|
|
716
|
+
*/
|
|
717
|
+
subscribe(queryHash: string, callback: QueryUpdateCallback, options?: {
|
|
718
|
+
immediate?: boolean;
|
|
719
|
+
}): () => void;
|
|
720
|
+
/**
|
|
721
|
+
* Subscribe to a query's fetch-status changes (idle/fetching).
|
|
722
|
+
* With `{ immediate: true }` the callback fires synchronously with the
|
|
723
|
+
* current status (defaults to `idle` if the query isn't registered yet).
|
|
724
|
+
*/
|
|
725
|
+
subscribeStatus(queryHash: string, callback: QueryStatusCallback, options?: {
|
|
726
|
+
immediate?: boolean;
|
|
727
|
+
}): () => void;
|
|
728
|
+
/**
|
|
729
|
+
* Set a query's fetch status and notify status observers (DevTools +
|
|
730
|
+
* `subscribeStatus` listeners). No-op when the status is unchanged or the
|
|
731
|
+
* query is unknown.
|
|
732
|
+
*/
|
|
733
|
+
setQueryStatus(queryHash: string, status: QueryStatus): void;
|
|
734
|
+
/**
|
|
735
|
+
* Enter a fetch cycle for a query. Refcounted: registration and concurrent
|
|
736
|
+
* poll/LIVE sync rounds can overlap on the same hash, and only the OUTERMOST
|
|
737
|
+
* cycle may flip the status — 0→1 emits `fetching`, and `endFetching`'s 1→0
|
|
738
|
+
* emits `idle`. Always pair with `endFetching` in a `finally`.
|
|
739
|
+
*/
|
|
740
|
+
beginFetching(queryHash: string): void;
|
|
741
|
+
/** Leave a fetch cycle started with {@link beginFetching}; emits `idle` on the last exit. */
|
|
742
|
+
endFetching(queryHash: string): void;
|
|
743
|
+
/**
|
|
744
|
+
* Subscribe to mutations (for sync)
|
|
745
|
+
*/
|
|
746
|
+
onMutation(callback: MutationCallback): () => void;
|
|
747
|
+
/**
|
|
748
|
+
* Handle stream updates from DBSP (via CacheModule)
|
|
749
|
+
*/
|
|
750
|
+
onStreamUpdate(update: StreamUpdate): Promise<void>;
|
|
751
|
+
/** Coalesce `update` onto the query's trailing timer (see onStreamUpdate). */
|
|
752
|
+
private queueStreamUpdate;
|
|
753
|
+
/**
|
|
754
|
+
* Re-materialize + notify a query whose MEMBERSHIP changed without any row
|
|
755
|
+
* needing to be fetched, i.e. without the SSP stream update that normally
|
|
756
|
+
* carries the notify. That is every row this client wrote itself: the local
|
|
757
|
+
* CREATE memoized it at `_00_rv = 1`, the server publishes it at 1, so the
|
|
758
|
+
* sync engine rightly fetches nothing - and then nobody told the subscribers
|
|
759
|
+
* that `remoteArray` now holds the id. The row appeared on reload only.
|
|
760
|
+
*
|
|
761
|
+
* Routed through the same per-query debounce as a real stream update, so it
|
|
762
|
+
* cannot race one: a pending real update already materializes against the
|
|
763
|
+
* current `remoteArray` and wins. The synthetic update re-uses the circuit's
|
|
764
|
+
* last `localArray` and skips the persist/metrics that describe an ingest.
|
|
765
|
+
*/
|
|
766
|
+
scheduleRematerialize(queryHash: string): void;
|
|
767
|
+
/**
|
|
768
|
+
* Process a query's pending (debounced) stream update NOW instead of on the
|
|
769
|
+
* trailing edge. Called by the sync engine before it flips a query back to
|
|
770
|
+
* `idle`, so the status change never races ahead of the rows it fetched.
|
|
771
|
+
* No-op when nothing is pending. The pending entry is removed before the
|
|
772
|
+
* await so a concurrently-firing timer can't process it twice.
|
|
773
|
+
*/
|
|
774
|
+
flushPendingStreamUpdate(queryHash: string): Promise<void>;
|
|
775
|
+
/**
|
|
776
|
+
* Materialize a query's result rows from the local store.
|
|
777
|
+
*
|
|
778
|
+
* A query's rows are its MEMBERSHIP — the id-set the server put in
|
|
779
|
+
* `_00_list_ref` (`remoteArray`) — not "every local body that matches the
|
|
780
|
+
* WHERE". Those two disagree, and the disagreement was the bug: when a row
|
|
781
|
+
* leaves a query's window but still exists upstream, `handleRemovedRecords`
|
|
782
|
+
* keeps its local body and never re-fetches it, so a predicate re-scan finds
|
|
783
|
+
* that stale body still matching and keeps rendering the row. Selecting the
|
|
784
|
+
* id-set directly is also the only correct thing for a windowed query, where
|
|
785
|
+
* re-applying `START m` against the shared local store skips the window's own
|
|
786
|
+
* rows entirely (sparse windowing) and returns nothing.
|
|
787
|
+
*
|
|
788
|
+
* The rendered set is:
|
|
789
|
+
*
|
|
790
|
+
* (membership ∪ (pendingWrites ∩ localArray)) − pendingDeletes
|
|
791
|
+
*
|
|
792
|
+
* The middle term keeps optimistic writes visible without re-admitting stale
|
|
793
|
+
* rows. Every local write is fed to the SSP (`cache.saveBatch` →
|
|
794
|
+
* `ingestMany`), so `localArray` answers "does this row match the predicate
|
|
795
|
+
* per LOCAL truth". A pending write that moves a row into the window is in
|
|
796
|
+
* `localArray` and shows; one that moves a row out is absent and does not; a
|
|
797
|
+
* stale body the server dropped has no pending write at all, so it stays out.
|
|
798
|
+
* `pendingDeletes` covers the reverse lag — the server still lists a row whose
|
|
799
|
+
* DELETE is sitting in our outbox.
|
|
800
|
+
*
|
|
801
|
+
* Falls back to the predicate scan only when membership has never been
|
|
802
|
+
* established (a query first run on this device), so an offline first paint
|
|
803
|
+
* still shows something.
|
|
804
|
+
*/
|
|
805
|
+
private materializeRecords;
|
|
806
|
+
/**
|
|
807
|
+
* The materialization itself, without the DevTools timing wrapper. Split out so
|
|
808
|
+
* cold-start seeding can use it before a `QueryState` exists.
|
|
809
|
+
*/
|
|
810
|
+
private materializeFromConfig;
|
|
811
|
+
/**
|
|
812
|
+
* The authoritative membership list to render from, or `null` when membership
|
|
813
|
+
* has never been established and the caller must fall back to a scan.
|
|
814
|
+
*
|
|
815
|
+
* A windowed query has no usable fallback — re-running its `START m` locally
|
|
816
|
+
* returns the wrong rows — so it renders from whatever id-set is on hand
|
|
817
|
+
* (SSP's included) rather than degrading to a scan. That is the pre-existing
|
|
818
|
+
* behavior for windows and is preserved.
|
|
819
|
+
*/
|
|
820
|
+
private resolveMembership;
|
|
821
|
+
private readonly settledWrites;
|
|
822
|
+
private readonly settledDeletes;
|
|
823
|
+
private pendingIds;
|
|
824
|
+
private pendingIdsAt;
|
|
825
|
+
private pendingIdsInflight;
|
|
826
|
+
private static readonly PENDING_IDS_TTL_MS;
|
|
827
|
+
private pendingIdsGen;
|
|
828
|
+
private static readonly PENDING_IDS_MAX_REREADS;
|
|
829
|
+
/** Drop the cached outbox ids. Cheap; call it on anything that could change
|
|
830
|
+
* `_00_pending_mutations`. */
|
|
831
|
+
private invalidatePendingIds;
|
|
832
|
+
/**
|
|
833
|
+
* Grace period for a settled write. Long enough to cover an SSP round trip
|
|
834
|
+
* that is running slowly (seconds, not milliseconds, when the edge path is
|
|
835
|
+
* backed up), short enough that a write the server silently dropped cannot
|
|
836
|
+
* linger misleadingly.
|
|
837
|
+
*
|
|
838
|
+
* The rejection case does NOT rely on this expiring: an application error
|
|
839
|
+
* rolls the mutation back and never reports it settled, so it vanishes at
|
|
840
|
+
* once. This deadline only bounds the case where the write succeeded and its
|
|
841
|
+
* membership never arrived at all.
|
|
842
|
+
*/
|
|
843
|
+
private static readonly SETTLED_WRITE_GRACE_MS;
|
|
844
|
+
/**
|
|
845
|
+
* Report that a mutation was accepted by the server and its outbox row
|
|
846
|
+
* removed. Called only on the SUCCESS path — a rolled-back mutation must
|
|
847
|
+
* disappear immediately, which is what makes this safe.
|
|
848
|
+
*/
|
|
849
|
+
noteWriteSettled(recordId: string, mutationType: string): void;
|
|
850
|
+
/** Drop entries past their deadline. */
|
|
851
|
+
private pruneSettled;
|
|
852
|
+
/** Apply the pending-write union and pending-delete subtraction, and map to
|
|
853
|
+
* RecordIds for the engines' id-set path. */
|
|
854
|
+
private buildRenderIds;
|
|
855
|
+
private processStreamUpdate;
|
|
856
|
+
/**
|
|
857
|
+
* Compute p55/p90/p99 from a rolling window of materialization samples.
|
|
858
|
+
* Returns nulls for any percentile that has no samples yet so SurrealDB
|
|
859
|
+
* `option<float>` columns stay NONE rather than 0 before the first ingest.
|
|
860
|
+
*/
|
|
861
|
+
private computeMaterializationPercentiles;
|
|
862
|
+
/** Record a per-phase timing sample (ms) on a query's rolling window. */
|
|
863
|
+
private recordPhase;
|
|
864
|
+
/** Record the remote record-fetch time (ms) for a query. Called by the sync engine. */
|
|
865
|
+
recordRemoteFetch(hash: string, ms: number): void;
|
|
866
|
+
/**
|
|
867
|
+
* Record the frontend reconcile time (ms) for a query. Called from `useQuery`
|
|
868
|
+
* via `Sp00kyClient.reportFrontendTiming` after it applies an update to its store.
|
|
869
|
+
*/
|
|
870
|
+
recordFrontendTiming(hash: string, ms: number): void;
|
|
871
|
+
/**
|
|
872
|
+
* Build the per-query processing-time breakdown surfaced to the DevTools panel
|
|
873
|
+
* and the MCP. `ssp` is the WASM-ingest wall time (from `materializationSamples`);
|
|
874
|
+
* the rest come from the per-phase rolling windows + one-shot registration timings.
|
|
875
|
+
*/
|
|
876
|
+
phaseTimings(q: QueryState): QueryTimings;
|
|
877
|
+
/**
|
|
878
|
+
* Get query state (for sync and devtools)
|
|
879
|
+
*/
|
|
880
|
+
getQueryByHash(hash: string): QueryState | undefined;
|
|
881
|
+
/**
|
|
882
|
+
* Cold-query guard for instant-hydrate: true when the query exists, hasn't been
|
|
883
|
+
* hydrated, and has NOT yet fetched its server result (`remoteArray` empty).
|
|
884
|
+
* We gate on `remoteArray`, not local `records`: a windowed query is often
|
|
885
|
+
* partially pre-seeded from the circuit (e.g. the dashboard's 5-row preview),
|
|
886
|
+
* but it still hasn't loaded its own full window from the server — so it should
|
|
887
|
+
* still hydrate. A warm re-subscribe (remoteArray already populated) is skipped.
|
|
888
|
+
*/
|
|
889
|
+
isCold(hash: string): boolean;
|
|
890
|
+
/**
|
|
891
|
+
* Walk a hydrated record's fields and append any EMBEDDED child records to
|
|
892
|
+
* `batch` (recursing for nested related fields). An embedded child is a
|
|
893
|
+
* value that is itself a record — a non-null object whose `id` is a
|
|
894
|
+
* `RecordId` — or an array of such records (one-to-many vs one-to-one). A
|
|
895
|
+
* bare `RecordId` (a foreign-key reference) or any other value is skipped,
|
|
896
|
+
* so this never mistakes a FK column for an embedded body. Children are
|
|
897
|
+
* keyed by their own `record.id.table`, versioned by `_00_rv`, and cleaned
|
|
898
|
+
* to their table's real columns (which strips the alias/related fields).
|
|
899
|
+
* `seen` dedupes within the batch.
|
|
900
|
+
*/
|
|
901
|
+
private collectEmbeddedChildren;
|
|
902
|
+
/**
|
|
903
|
+
* Prepare a subquery-bearing row (preload / hydration) for the schemafull
|
|
904
|
+
* local store: replace an embedded FORWARD-relation object (`author = { id, … }`)
|
|
905
|
+
* with its RecordId so a `record<…>` field coerces, and DROP reverse-subquery
|
|
906
|
+
* ARRAYS (`comments = [ … ]`) since their rows are cached separately as their
|
|
907
|
+
* own bodies. A flat record — as the live `SELECT * FROM $ids` sync returns,
|
|
908
|
+
* with relations already RecordIds — passes through unchanged.
|
|
909
|
+
*/
|
|
910
|
+
private flattenRelationsForStorage;
|
|
911
|
+
/**
|
|
912
|
+
* Instant-hydrate: ingest rows fetched one-shot from the remote (the query's own
|
|
913
|
+
* surql run directly) so the query DISPLAYS immediately, while the full realtime
|
|
914
|
+
* registration proceeds in the background. Ingests with versions (`_00_rv`) so the
|
|
915
|
+
* later `syncRecords` dedup skips re-pulling unchanged bodies, and seeds
|
|
916
|
+
* `remoteArray` so windowed queries materialize the correct window (no sparse
|
|
917
|
+
* local-circuit issue). Runs at most once per query (the `hydrated` flag).
|
|
918
|
+
*/
|
|
919
|
+
applyHydration(hash: string, rows: RecordWithId[]): Promise<void>;
|
|
920
|
+
/**
|
|
921
|
+
* Build the cache batch for a set of one-shot rows and persist it to the
|
|
922
|
+
* local DB + in-browser SSP. Maps each row to a `CREATE` op on its own table
|
|
923
|
+
* and extracts EMBEDDED related children (any nesting depth) as their own
|
|
924
|
+
* records — a `.related()` query returns its children embedded, and a later
|
|
925
|
+
* correlated re-materialization needs them present as standalone rows.
|
|
926
|
+
* Shared by `applyHydration` (live registration) and `persistSnapshot`
|
|
927
|
+
* (preload).
|
|
928
|
+
*/
|
|
929
|
+
private buildAndSaveCacheBatch;
|
|
930
|
+
/**
|
|
931
|
+
* Preload/prewarm: persist one-shot rows (and their embedded related children)
|
|
932
|
+
* into the local cache WITHOUT registering a query — no `activeQueries` entry,
|
|
933
|
+
* no `_00_query` view, no TTL heartbeat. The rows live in the local DB as
|
|
934
|
+
* ordinary bodies (never GC'd on their own) so a later `useQuery` seeds its
|
|
935
|
+
* first paint from them instantly, then registers a live view to freshen.
|
|
936
|
+
*/
|
|
937
|
+
persistSnapshot(tableName: string, rows: RecordWithId[]): Promise<void>;
|
|
938
|
+
/**
|
|
939
|
+
* Read the durable preload freshness marker for a query hash, or null if this
|
|
940
|
+
* query was never preloaded in the current bucket. Co-located with the cached
|
|
941
|
+
* rows (per-bucket `_00_preload` table) so a bucket switch that clears the
|
|
942
|
+
* data also clears the marker — a stale marker can't claim "warm" when the
|
|
943
|
+
* rows are gone. Any read error is treated as cold.
|
|
944
|
+
*/
|
|
945
|
+
getPreloadMarker(hash: string): Promise<{
|
|
946
|
+
fetchedAt: number;
|
|
947
|
+
rowCount: number;
|
|
948
|
+
} | null>;
|
|
949
|
+
/** Stamp the preload freshness marker after a successful snapshot fetch. */
|
|
950
|
+
writePreloadMarker(hash: string, rowCount: number): Promise<void>;
|
|
951
|
+
/**
|
|
952
|
+
* Read the durable membership row, or `null` if this query has never had
|
|
953
|
+
* authoritative membership on this device. Any read error is treated as
|
|
954
|
+
* "unknown" so a broken row degrades to the predicate scan rather than
|
|
955
|
+
* rendering an empty list.
|
|
956
|
+
*
|
|
957
|
+
* `confirmed` is true only for rows written after the server itself vouched
|
|
958
|
+
* for the set (a non-empty id-set, or an empty one it reported a row count of
|
|
959
|
+
* zero for, or an empty one that followed a non-empty one in the same
|
|
960
|
+
* session). Rows written before the marker existed, including the `[]` rows a
|
|
961
|
+
* pre-`ea56f50e` client mirrored from an unflushed read, read as unconfirmed.
|
|
962
|
+
*/
|
|
963
|
+
getWindowMembership(key: string): Promise<DurableMembership | null>;
|
|
964
|
+
/**
|
|
965
|
+
* Persist the durable membership row. Best-effort: callers must not fail a
|
|
966
|
+
* sync round because the mirror write failed.
|
|
967
|
+
*
|
|
968
|
+
* `confirmed` says whether a cold start may trust this row even when it is
|
|
969
|
+
* empty. A confirmed empty is a real answer ("the server says this query has
|
|
970
|
+
* no rows") and stays empty across a reload; an unconfirmed empty is the
|
|
971
|
+
* retry budget's guess and falls back to the predicate scan on the next boot,
|
|
972
|
+
* exactly as every empty row did before the marker existed.
|
|
973
|
+
*/
|
|
974
|
+
writeWindowMembership(key: string, ids: RecordVersionArray, confirmed: boolean): Promise<void>;
|
|
975
|
+
/**
|
|
976
|
+
* Record ids with a mutation still in the outbox, split by direction.
|
|
977
|
+
*
|
|
978
|
+
* Both halves feed {@link materializeRecords}: `writes` keeps optimistic
|
|
979
|
+
* creates/updates visible before the server has acknowledged them, and
|
|
980
|
+
* `deletes` suppresses rows the server still lists because our DELETE hasn't
|
|
981
|
+
* been processed yet. Reading `_00_pending_mutations` (rather than tracking
|
|
982
|
+
* ids in memory) is what makes both survive a reload.
|
|
983
|
+
*
|
|
984
|
+
* On failure returns empty sets: membership alone then decides, which can
|
|
985
|
+
* briefly hide an optimistic write but never resurrects a deleted row.
|
|
986
|
+
*/
|
|
987
|
+
getPendingRecordIds(): Promise<{
|
|
988
|
+
writes: Set<string>;
|
|
989
|
+
deletes: Set<string>;
|
|
990
|
+
}>;
|
|
991
|
+
/** The uncached read. Also the reload path after an invalidation, so the ids
|
|
992
|
+
* still survive a reload exactly as before. `gen` is the generation the read
|
|
993
|
+
* was issued under; the result is cached only if it is still current. */
|
|
994
|
+
private readPendingRecordIds;
|
|
995
|
+
/** True while ≥1 live subscriber is watching this query (refcount guard). */
|
|
996
|
+
hasSubscribers(hash: string): boolean;
|
|
997
|
+
/**
|
|
998
|
+
* Opt-in eager teardown for a query whose LAST subscriber just left — used by
|
|
999
|
+
* viewport-windowed lists to cancel off-screen windows instead of leaving
|
|
1000
|
+
* their remote views to expire on the TTL sweep. No-op while any subscriber
|
|
1001
|
+
* remains (refcount). Only enqueues the remote cleanup here; the local WASM
|
|
1002
|
+
* view + in-memory state are freed in {@link finalizeDeregister} after the
|
|
1003
|
+
* remote delete completes, so a re-subscribe in between aborts/heals it.
|
|
1004
|
+
*
|
|
1005
|
+
* NOTE: most queries should NOT use this — the default keep-alive on
|
|
1006
|
+
* unsubscribe avoids re-registration churn on navigation.
|
|
1007
|
+
*/
|
|
1008
|
+
deregisterQuery(hash: string): void;
|
|
1009
|
+
/**
|
|
1010
|
+
* Final local teardown after the remote `_00_query` row was deleted: free the
|
|
1011
|
+
* WASM view, heartbeat timer, debounce timer, and in-memory state. Caller
|
|
1012
|
+
* (`cleanupQuery`) guarantees no subscriber remains.
|
|
1013
|
+
*/
|
|
1014
|
+
finalizeDeregister(hash: string): void;
|
|
1015
|
+
/**
|
|
1016
|
+
* Get query state by id (for sync and devtools)
|
|
1017
|
+
*/
|
|
1018
|
+
getQueryById(id: RecordId<string>): QueryState | undefined;
|
|
1019
|
+
/**
|
|
1020
|
+
* Get all active queries (for devtools)
|
|
1021
|
+
*/
|
|
1022
|
+
getActiveQueries(): QueryState[];
|
|
1023
|
+
getActiveQueryHashes(): QueryHash[];
|
|
1024
|
+
updateQueryLocalArray(id: string, localArray: RecordVersionArray): Promise<void>;
|
|
1025
|
+
updateQueryRemoteArray(hash: string, remoteArray: RecordVersionArray, opts?: {
|
|
1026
|
+
/** `_00_query.rowCount` read in the same round trip; `null` = unknown. */
|
|
1027
|
+
serverRowCount?: number | null;
|
|
1028
|
+
}): Promise<void>;
|
|
1029
|
+
/**
|
|
1030
|
+
* Cancel every armed timer ahead of a local-bucket switch: stream-update
|
|
1031
|
+
* debounce timers (their pending updates carry the OLD bucket's id-sets) and
|
|
1032
|
+
* per-query TTL heartbeats (they'd refresh the previous user's remote
|
|
1033
|
+
* `_00_query` rows under the new session). The rebind re-arms heartbeats.
|
|
1034
|
+
*/
|
|
1035
|
+
quiesce(): void;
|
|
1036
|
+
/**
|
|
1037
|
+
* Re-home every active query in a freshly-opened bucket, KEEPING its hash —
|
|
1038
|
+
* `useQuery` subscriptions are keyed by hash and don't re-register on auth
|
|
1039
|
+
* changes, so the hooks must stay attached. Per query:
|
|
1040
|
+
* 1. reset the sync arrays + hydration flag and drop the previous user's
|
|
1041
|
+
* records, notifying subscribers with the new-bucket materialization
|
|
1042
|
+
* (usually empty) so their rows leave the UI immediately;
|
|
1043
|
+
* 2. recreate the `_00_query` row in the new bucket;
|
|
1044
|
+
* 3. re-register the SSP view on the (fresh, post-reset) processor — this
|
|
1045
|
+
* also rebinds the view to the NEW `$auth` context;
|
|
1046
|
+
* 4. restart the TTL heartbeat.
|
|
1047
|
+
* Returns the hashes so the caller can enqueue remote re-registration, which
|
|
1048
|
+
* refills records from the server via the normal register→sync→notify path.
|
|
1049
|
+
*/
|
|
1050
|
+
rebindAfterBucketSwitch(): Promise<QueryHash[]>;
|
|
1051
|
+
/**
|
|
1052
|
+
* Called after a query's initial sync completes.
|
|
1053
|
+
* Ensures subscribers are notified even if no stream updates fired (e.g. empty result set).
|
|
1054
|
+
*/
|
|
1055
|
+
notifyQuerySynced(queryHash: string): Promise<void>;
|
|
1056
|
+
run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, data: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
|
|
1057
|
+
/**
|
|
1058
|
+
* Build the outbox job record + resolve its table for a backend route.
|
|
1059
|
+
*
|
|
1060
|
+
* Every job is a single execution. Recurring work is declared server-side
|
|
1061
|
+
* (`schedules:` in sp00ky.yml) and the scheduler creates a fresh row per cycle,
|
|
1062
|
+
* so nothing here needs to know about schedules.
|
|
1063
|
+
*/
|
|
1064
|
+
private buildJobRecord;
|
|
1065
|
+
/**
|
|
1066
|
+
* Create a new record
|
|
1067
|
+
*/
|
|
1068
|
+
create<T extends Record<string, unknown>>(id: string, data: T): Promise<T>;
|
|
1069
|
+
/**
|
|
1070
|
+
* Update an existing record
|
|
1071
|
+
*/
|
|
1072
|
+
update<T extends Record<string, unknown>>(table: string, id: string, data: Partial<T>, options?: UpdateOptions): Promise<T>;
|
|
1073
|
+
/**
|
|
1074
|
+
* Delete a record
|
|
1075
|
+
*/
|
|
1076
|
+
delete(table: string, id: string): Promise<void>;
|
|
1077
|
+
/**
|
|
1078
|
+
* Rollback a failed optimistic create by deleting the record locally
|
|
1079
|
+
*/
|
|
1080
|
+
rollbackCreate(recordId: RecordId, tableName: string): Promise<void>;
|
|
1081
|
+
/**
|
|
1082
|
+
* Rollback a failed optimistic update by restoring the previous record state
|
|
1083
|
+
*/
|
|
1084
|
+
rollbackUpdate(recordId: RecordId, tableName: string, beforeRecord: Record<string, unknown>): Promise<void>;
|
|
1085
|
+
/**
|
|
1086
|
+
* Force a re-materialize + notify of every active query on `tableName`.
|
|
1087
|
+
* Used after a DELETE landed in the local store (this tab's own, or one
|
|
1088
|
+
* relayed from another tab): the SSP may not emit a view update for a
|
|
1089
|
+
* DELETE ingest, and the re-materialize reads the store, which already
|
|
1090
|
+
* excludes the row. Each query is isolated so one failing re-materialize
|
|
1091
|
+
* can't stop the others.
|
|
1092
|
+
*/
|
|
1093
|
+
notifyTableQueries(tableName: string): Promise<void>;
|
|
1094
|
+
/**
|
|
1095
|
+
* Remove a record from all active query states and notify subscribers
|
|
1096
|
+
*/
|
|
1097
|
+
private removeRecordFromQueries;
|
|
1098
|
+
private createAndRegisterQuery;
|
|
1099
|
+
private createNewQuery;
|
|
1100
|
+
private calculateHash;
|
|
1101
|
+
/**
|
|
1102
|
+
* Session-independent counterpart of {@link calculateHash}: the key for a
|
|
1103
|
+
* query's durable `_00_window` membership row.
|
|
1104
|
+
*
|
|
1105
|
+
* Deliberately the SAME inputs minus the `session::id()` salt, so the two keys
|
|
1106
|
+
* can never drift apart. The salt is right for `_00_query` (two tabs must not
|
|
1107
|
+
* fight over one row) and wrong for membership, which has to be recognizable
|
|
1108
|
+
* after a reload — a reload mints a new session id, and offline the salt is
|
|
1109
|
+
* `''`, so a salted key can never match what the previous session wrote.
|
|
1110
|
+
*/
|
|
1111
|
+
private calculateMembershipKey;
|
|
1112
|
+
private sha256;
|
|
1113
|
+
private startTTLHeartbeat;
|
|
1114
|
+
private replaceRecordInQueries;
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Parse update options to generate push event options
|
|
1118
|
+
*/
|
|
1119
|
+
//#endregion
|
|
1120
|
+
//#region src/services/tabs/protocol.d.ts
|
|
1121
|
+
type TabId = string;
|
|
1122
|
+
type TabRole = 'solo' | 'leader' | 'follower';
|
|
1123
|
+
/** Broker pings every tab at this cadence. */
|
|
1124
|
+
|
|
1125
|
+
/** Matches `CacheIngestTuple` (modules/cache): exactly what `ingestMany`
|
|
1126
|
+
* consumes, so relayed batches feed follower circuits without reshaping. */
|
|
1127
|
+
interface IngestTuple {
|
|
1128
|
+
table: string;
|
|
1129
|
+
op: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
1130
|
+
id: string;
|
|
1131
|
+
record: Record<string, unknown>;
|
|
1132
|
+
}
|
|
1133
|
+
type FollowerToLeaderMessage = {
|
|
1134
|
+
type: 'sync-hello';
|
|
1135
|
+
tabId: TabId;
|
|
1136
|
+
}
|
|
1137
|
+
/** The follower committed an outbox row (through the shared store) and the
|
|
1138
|
+
* leader should drain it. Idempotent; a new leader's loadFromDatabase is
|
|
1139
|
+
* the backstop for a notify lost in a failover window. */ | {
|
|
1140
|
+
type: 'mutation-enqueued';
|
|
1141
|
+
mutationId: string;
|
|
1142
|
+
} | {
|
|
1143
|
+
type: 'request-poll';
|
|
1144
|
+
}
|
|
1145
|
+
/** An optimistic write this follower committed to the SHARED store and
|
|
1146
|
+
* ingested into its own circuit. The leader ingests it (no DB write, the
|
|
1147
|
+
* row is already there) and fans it out to every OTHER follower as
|
|
1148
|
+
* `ingest-relay`, so a follower's write lands in every tab in one hop
|
|
1149
|
+
* instead of after the server round-trip. */ | {
|
|
1150
|
+
type: 'ingest';
|
|
1151
|
+
tuples: IngestTuple[];
|
|
1152
|
+
};
|
|
1153
|
+
type LeaderToFollowerMessage = {
|
|
1154
|
+
type: 'db-ready';
|
|
1155
|
+
leadershipId: number;
|
|
1156
|
+
bucketId: string;
|
|
1157
|
+
storageHealth: StorageHealth;
|
|
1158
|
+
}
|
|
1159
|
+
/** Every ingest the leader's CacheModule committed, so follower circuits
|
|
1160
|
+
* stay live without their own fetch. seq detects gaps. */ | {
|
|
1161
|
+
type: 'ingest-relay';
|
|
1162
|
+
tuples: IngestTuple[];
|
|
1163
|
+
leadershipId: number;
|
|
1164
|
+
seq: number;
|
|
1165
|
+
}
|
|
1166
|
+
/** A `_00_list_ref` LIVE event, relayed verbatim. Each follower resolves the
|
|
1167
|
+
* queryId against its own DataModule and ignores foreign queries. */ | {
|
|
1168
|
+
type: 'list-ref-change';
|
|
1169
|
+
action: 'CREATE' | 'UPDATE' | 'DELETE';
|
|
1170
|
+
queryId: string;
|
|
1171
|
+
recordId: string;
|
|
1172
|
+
version: number;
|
|
1173
|
+
parent: boolean;
|
|
1174
|
+
}
|
|
1175
|
+
/** The leader's drain rolled back a mutation owned by this tab. */ | {
|
|
1176
|
+
type: 'mutation-rolled-back';
|
|
1177
|
+
mutationId: string;
|
|
1178
|
+
recordId: string;
|
|
1179
|
+
eventType: 'create' | 'update' | 'delete';
|
|
1180
|
+
error: string;
|
|
1181
|
+
}
|
|
1182
|
+
/** The leader's drain pushed a mutation and deleted its outbox row from the
|
|
1183
|
+
* SHARED store. Every follower starts its settled-write grace so a row it
|
|
1184
|
+
* was rendering as a pending write does not blink out before its
|
|
1185
|
+
* `_00_list_ref` membership arrives. */ | {
|
|
1186
|
+
type: 'mutation-settled';
|
|
1187
|
+
mutationId: string;
|
|
1188
|
+
recordId: string;
|
|
1189
|
+
eventType: 'create' | 'update' | 'delete';
|
|
1190
|
+
};
|
|
1191
|
+
//#endregion
|
|
1192
|
+
//#region src/services/tabs/coordinator.d.ts
|
|
1193
|
+
/** Leader-side fan-out surface handed to the sync layer. The sync router
|
|
1194
|
+
* (modules/sync/tab-router.ts) registers itself as the message handler. */
|
|
1195
|
+
declare class LeaderSyncHub {
|
|
1196
|
+
readonly leadershipId: number;
|
|
1197
|
+
private logger;
|
|
1198
|
+
private followers;
|
|
1199
|
+
private seq;
|
|
1200
|
+
onFollowerMessage: ((tabId: TabId, msg: FollowerToLeaderMessage) => void) | null;
|
|
1201
|
+
onFollowerDetached: ((tabId: TabId) => void) | null;
|
|
1202
|
+
constructor(leadershipId: number, logger: Logger$1);
|
|
1203
|
+
attach(tabId: TabId, port: MessagePort): void;
|
|
1204
|
+
detach(tabId: TabId): void;
|
|
1205
|
+
detachAll(): void;
|
|
1206
|
+
sendTo(tabId: TabId, msg: LeaderToFollowerMessage): void;
|
|
1207
|
+
broadcast(msg: LeaderToFollowerMessage, exceptTabId?: TabId): void;
|
|
1208
|
+
/** Stamped ingest relay; seq lets followers detect gaps. */
|
|
1209
|
+
relayIngest(tuples: IngestTuple[], exceptTabId?: TabId): void;
|
|
1210
|
+
get followerCount(): number;
|
|
1211
|
+
get relayedBatches(): number;
|
|
1212
|
+
}
|
|
1213
|
+
/** Follower half of the syncPort. Queues while detached (leaderless window)
|
|
1214
|
+
* and flushes on rebind; a lost-in-flight mutation notify is additionally
|
|
1215
|
+
* backstopped by the new leader reloading the shared outbox from the store. */
|
|
1216
|
+
declare class SyncForwarder {
|
|
1217
|
+
private tabId;
|
|
1218
|
+
private port;
|
|
1219
|
+
private queued;
|
|
1220
|
+
onLeaderMessage: ((msg: LeaderToFollowerMessage) => void) | null;
|
|
1221
|
+
constructor(tabId: TabId);
|
|
1222
|
+
rebind(port: MessagePort): void;
|
|
1223
|
+
unbind(): void;
|
|
1224
|
+
private post;
|
|
1225
|
+
mutationEnqueued(mutationId: string): void;
|
|
1226
|
+
/** An optimistic write this tab just ingested. Deliberately NOT queued while
|
|
1227
|
+
* detached: a new leader primes its circuit from the shared store, which
|
|
1228
|
+
* already holds the row, and replaying a stale tuple at it later would put
|
|
1229
|
+
* an older `_00_rv` in its version memo. */
|
|
1230
|
+
ingest(tuples: IngestTuple[]): void;
|
|
1231
|
+
requestPoll(): void;
|
|
1232
|
+
}
|
|
1233
|
+
//#endregion
|
|
1234
|
+
//#region src/modules/sync/sync.d.ts
|
|
1235
|
+
/**
|
|
1236
|
+
* Tunables for `Sp00kySync` construction.
|
|
1237
|
+
*/
|
|
1238
|
+
interface Sp00kySyncOptions {
|
|
1239
|
+
/**
|
|
1240
|
+
* Cadence (ms) for the `_00_list_ref` poll fallback that catches
|
|
1241
|
+
* cross-session UPDATEs the LIVE-permission gap drops. Non-positive
|
|
1242
|
+
* values fall back to the default; see
|
|
1243
|
+
* {@link resolveListRefPollInterval}.
|
|
1244
|
+
*/
|
|
1245
|
+
refSyncIntervalMs?: number;
|
|
1246
|
+
/**
|
|
1247
|
+
* Enable realtime sync for unauthenticated clients against the shared
|
|
1248
|
+
* `_00_list_ref_anon` table. See {@link Sp00kyConfig.enableAnonymousLiveQueries}.
|
|
1249
|
+
* Defaults to `false`.
|
|
1250
|
+
*/
|
|
1251
|
+
anonymousLiveQueries?: boolean;
|
|
1252
|
+
/**
|
|
1253
|
+
* Consecutive failed sync rounds before sync health flips to `degraded`.
|
|
1254
|
+
* `0` disables degraded reporting. See {@link Sp00kyConfig.syncHealth}.
|
|
1255
|
+
* Defaults to `3`.
|
|
1256
|
+
*/
|
|
1257
|
+
degradeAfterConsecutiveFailures?: number;
|
|
1258
|
+
/**
|
|
1259
|
+
* Max time a single mutation push may take before it is treated as a network
|
|
1260
|
+
* failure and retried. Guards against an RPC that never settles wedging the
|
|
1261
|
+
* up-queue for the session. Defaults to 30000; `0` disables the timeout.
|
|
1262
|
+
*/
|
|
1263
|
+
pushTimeoutMs?: number;
|
|
1264
|
+
/**
|
|
1265
|
+
* Max time a single down event (`register`/`sync`/`cleanup`) may take before
|
|
1266
|
+
* it is treated as a network failure and retried. The mirror of
|
|
1267
|
+
* {@link pushTimeoutMs} for the read side, which had no such guard: a
|
|
1268
|
+
* `fn::query::register` that never settled held its slot in the down drain,
|
|
1269
|
+
* and every later registration behind it, for the rest of the session.
|
|
1270
|
+
* Defaults to 30000; `0` disables the timeout.
|
|
1271
|
+
*/
|
|
1272
|
+
downTimeoutMs?: number;
|
|
1273
|
+
/**
|
|
1274
|
+
* Transport supervisor. Sync reads its state to report `connection` in
|
|
1275
|
+
* {@link SyncHealth} so a UI can show "reconnecting…" the instant the socket
|
|
1276
|
+
* drops, without waiting for the degrade threshold. Optional: omitted in
|
|
1277
|
+
* tests, where `connection` then reports `connected`.
|
|
1278
|
+
*/
|
|
1279
|
+
connectionSupervisor?: ConnectionSupervisor;
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* The main synchronization engine for Sp00ky.
|
|
1283
|
+
* Handles the bidirectional synchronization between the local database and the remote backend.
|
|
1284
|
+
* Uses a queue-based architecture with 'up' (local to remote) and 'down' (remote to local) queues.
|
|
1285
|
+
* @template S The schema structure type.
|
|
1286
|
+
*/
|
|
1287
|
+
declare class Sp00kySync<S extends SchemaStructure> {
|
|
1288
|
+
private local;
|
|
1289
|
+
private remote;
|
|
1290
|
+
private cache;
|
|
1291
|
+
private dataModule;
|
|
1292
|
+
private schema;
|
|
1293
|
+
private upQueue;
|
|
1294
|
+
private downQueue;
|
|
1295
|
+
private isInit;
|
|
1296
|
+
private logger;
|
|
1297
|
+
private syncEngine;
|
|
1298
|
+
/** Engine-level events (e.g. `SYNC_REMOTE_DATA_INGESTED`). Distinct
|
|
1299
|
+
* from `this.events`, which carries Sp00kySync-level events like
|
|
1300
|
+
* `SYNC_QUERY_UPDATED` and `SYNC_MUTATION_ROLLED_BACK`. */
|
|
1301
|
+
get engineEvents(): SyncEventSystem;
|
|
1302
|
+
private scheduler;
|
|
1303
|
+
/**
|
|
1304
|
+
* Set by any event that means the socket we registered on is gone, so the
|
|
1305
|
+
* next `connected` knows it must re-subscribe rather than treat itself as the
|
|
1306
|
+
* initial connect. See {@link subscribeToReconnect}.
|
|
1307
|
+
*/
|
|
1308
|
+
private needsResubscribe;
|
|
1309
|
+
/** When the last reconnect-driven full refetch ran, for burst coalescing. */
|
|
1310
|
+
private lastReconnectRefetchAt;
|
|
1311
|
+
/**
|
|
1312
|
+
* Minimum gap between reconnect-driven full refetches. Long enough to absorb
|
|
1313
|
+
* a flapping socket (the SDK reconnect ladder starts at 1s), short enough
|
|
1314
|
+
* that a genuine drop minutes later still refetches.
|
|
1315
|
+
*/
|
|
1316
|
+
private static readonly RECONNECT_REFETCH_COOLDOWN_MS;
|
|
1317
|
+
events: SyncEventSystem;
|
|
1318
|
+
private currentUserId;
|
|
1319
|
+
private tabRole;
|
|
1320
|
+
private tabId;
|
|
1321
|
+
private hub;
|
|
1322
|
+
private forwarder;
|
|
1323
|
+
private refMode;
|
|
1324
|
+
private readonly anonLiveEnabled;
|
|
1325
|
+
private currentLiveQueryUuid;
|
|
1326
|
+
private liveQueryUnsubscribe;
|
|
1327
|
+
private listRefPollTimer;
|
|
1328
|
+
private listRefPollRunning;
|
|
1329
|
+
private listRefPollInFlight;
|
|
1330
|
+
readonly refSyncIntervalMs: number;
|
|
1331
|
+
private listRefIdleStreak;
|
|
1332
|
+
private stillRemoteStreaks;
|
|
1333
|
+
private lastLiveEventAt;
|
|
1334
|
+
private _liveRetryCount;
|
|
1335
|
+
get liveRetryCount(): number;
|
|
1336
|
+
get isSyncing(): boolean;
|
|
1337
|
+
get pendingMutationCount(): number;
|
|
1338
|
+
subscribeToPendingMutations(cb: (count: number) => void): () => void;
|
|
1339
|
+
private readonly degradeAfterFailures;
|
|
1340
|
+
/** Per-push RPC deadline; see {@link withPushTimeout}. */
|
|
1341
|
+
private readonly pushTimeoutMs;
|
|
1342
|
+
private readonly downTimeoutMs;
|
|
1343
|
+
private consecutiveSyncFailures;
|
|
1344
|
+
private syncHealthStatus;
|
|
1345
|
+
private lastSyncErrorKind;
|
|
1346
|
+
private lastSyncErrorMessage;
|
|
1347
|
+
private hasSyncedOnce;
|
|
1348
|
+
private selfHealTimer;
|
|
1349
|
+
private selfHealAttempts;
|
|
1350
|
+
private static readonly SELF_HEAL_BASE_MS;
|
|
1351
|
+
private static readonly SELF_HEAL_MAX_MS;
|
|
1352
|
+
/**
|
|
1353
|
+
* Transport supervisor, when one was supplied. Sync only reads state from it;
|
|
1354
|
+
* it never drives reconnects itself.
|
|
1355
|
+
*/
|
|
1356
|
+
private readonly connectionSupervisor?;
|
|
1357
|
+
/**
|
|
1358
|
+
* Mirror of the supervisor's state. Defaults to `connected` so a client
|
|
1359
|
+
* constructed without a supervisor (tests, embedders) reports the same health
|
|
1360
|
+
* shape it always has rather than a permanent false "disconnected".
|
|
1361
|
+
*/
|
|
1362
|
+
private connectionState;
|
|
1363
|
+
/** Current sync-health snapshot. */
|
|
1364
|
+
get syncHealth(): SyncHealth;
|
|
1365
|
+
/**
|
|
1366
|
+
* Observe sync health. The callback fires immediately with the current
|
|
1367
|
+
* status and again on every healthy↔degraded transition. Returns an
|
|
1368
|
+
* unsubscribe. Mirrors {@link subscribeToPendingMutations}.
|
|
1369
|
+
*/
|
|
1370
|
+
subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void;
|
|
1371
|
+
private emitSyncHealth;
|
|
1372
|
+
/**
|
|
1373
|
+
* Mirror the supervisor's transport state into {@link SyncHealth} and emit on
|
|
1374
|
+
* every change, so a UI can react to a dropped socket immediately instead of
|
|
1375
|
+
* waiting for `degradeAfterFailures` failed rounds. `status` is untouched:
|
|
1376
|
+
* a brief reconnect is not a degradation.
|
|
1377
|
+
*
|
|
1378
|
+
* No explicit unsubscribe: the supervisor is owned by the same client and
|
|
1379
|
+
* drops all subscribers in its own `dispose()`, which `Sp00kyClient.close()`
|
|
1380
|
+
* calls first.
|
|
1381
|
+
*/
|
|
1382
|
+
private subscribeToConnectionState;
|
|
1383
|
+
/**
|
|
1384
|
+
* Fed by the scheduler once per drained sync round. Individual failures are
|
|
1385
|
+
* absorbed by the queue's retry; only a run of `degradeAfterFailures`
|
|
1386
|
+
* consecutive failures flips the status to `degraded`, and the next clean
|
|
1387
|
+
* round flips it back. No-op when reporting is disabled (`degradeAfterFailures`
|
|
1388
|
+
* is 0).
|
|
1389
|
+
*/
|
|
1390
|
+
private recordSyncOutcome;
|
|
1391
|
+
/**
|
|
1392
|
+
* Begin self-heal retries (no-op if already running). Started on the
|
|
1393
|
+
* healthy→degraded transition; {@link recordSyncOutcome} stops it on recovery.
|
|
1394
|
+
*/
|
|
1395
|
+
private startSelfHeal;
|
|
1396
|
+
private scheduleSelfHeal;
|
|
1397
|
+
private stopSelfHeal;
|
|
1398
|
+
/**
|
|
1399
|
+
* Release a deregistered query's remote view immediately instead of leaving
|
|
1400
|
+
* it to the TTL sweep. Off by default; see the reasoning in
|
|
1401
|
+
* {@link cleanupQuery}. Kept as a field rather than deleted so the eager path
|
|
1402
|
+
* can be re-enabled in a test once the subquery-body repair path exists.
|
|
1403
|
+
*/
|
|
1404
|
+
private readonly releaseQueriesEagerly;
|
|
1405
|
+
constructor(local: LocalStore, remote: RemoteDatabaseService, cache: CacheModule, dataModule: DataModule<S>, schema: S, logger: Logger$1, options?: Sp00kySyncOptions);
|
|
1406
|
+
/**
|
|
1407
|
+
* Initializes the synchronization system.
|
|
1408
|
+
* Starts the scheduler and initiates the initial sync cycles.
|
|
1409
|
+
* @throws Error if already initialized.
|
|
1410
|
+
*/
|
|
1411
|
+
init(): Promise<void>;
|
|
1412
|
+
/** Set BEFORE init(): shapes what init boots (a follower loads no outbox and
|
|
1413
|
+
* never starts LIVE; its own registration/poll paths stay untouched). */
|
|
1414
|
+
setTabContext(role: 'solo' | 'leader' | 'follower', tabId: string | null): void;
|
|
1415
|
+
/** In-flight {@link resumeLeaderDuties}, so a second call joins the first
|
|
1416
|
+
* instead of double-draining the outbox. */
|
|
1417
|
+
private leaderDutiesInFlight;
|
|
1418
|
+
/** Resolves once the in-browser circuit has been primed from the local
|
|
1419
|
+
* store. Every sync diff waits on it: diffing against an empty circuit
|
|
1420
|
+
* classifies the whole working set as missing and re-downloads it. */
|
|
1421
|
+
private primeGate;
|
|
1422
|
+
/** The prime we last waited on. `whenPrimed` hands out one promise per
|
|
1423
|
+
* prime, so a new identity means a new prime (boot, bucket switch) ran. */
|
|
1424
|
+
private settledPrime;
|
|
1425
|
+
setPrimeGate(gate: () => Promise<void>): void;
|
|
1426
|
+
/**
|
|
1427
|
+
* Leader WIRING only, and deliberately synchronous.
|
|
1428
|
+
*
|
|
1429
|
+
* The coordinator publishes the leader role and tells the broker
|
|
1430
|
+
* `leader-ready` the moment the store is adopted, and the broker can mint a
|
|
1431
|
+
* follower's ports on the very next tick. So the follower-message handler
|
|
1432
|
+
* has to be live before this returns, or a mutation forwarded in that window
|
|
1433
|
+
* is dropped. Everything that can block (outbox reload, LIVE restart) moved
|
|
1434
|
+
* to {@link resumeLeaderDuties}: a promotion that waits on the network holds
|
|
1435
|
+
* `leader-ready` back, and a broker whose leader never reports ready serves
|
|
1436
|
+
* no follower ports and re-elects no one, which wedges the whole namespace.
|
|
1437
|
+
*/
|
|
1438
|
+
promoteToLeader(hub: LeaderSyncHub): void;
|
|
1439
|
+
/** Leader duties: drain the shared outbox, own the single list_ref LIVE,
|
|
1440
|
+
* relay LIVE events and rollbacks to followers. Idempotent for a boot-time
|
|
1441
|
+
* leader; a runtime promotion (failover) reloads the outbox, which now
|
|
1442
|
+
* holds EVERY tab's rows, and restarts LIVE under this session. Runs in the
|
|
1443
|
+
* background off the promotion path, so however long it takes (or if it
|
|
1444
|
+
* never finishes) the tab is already a working leader. */
|
|
1445
|
+
resumeLeaderDuties(): Promise<void>;
|
|
1446
|
+
/** Follower duties: no outbox drain, no LIVE. Mutations forward to the
|
|
1447
|
+
* leader; everything else (registration, per-query sync, poll) runs
|
|
1448
|
+
* against this tab's own remote session as usual. */
|
|
1449
|
+
demoteToFollower(forwarder: SyncForwarder): void;
|
|
1450
|
+
/**
|
|
1451
|
+
* A pending mutation was discarded because it can never be sent.
|
|
1452
|
+
*
|
|
1453
|
+
* This is a lost write, so it must not stay invisible. Every failure in this
|
|
1454
|
+
* chain used to be a `logger.error` an app running `logLevel: 'fatal'` never
|
|
1455
|
+
* shows, which is how an outbox could sit undrained for hours with the UI
|
|
1456
|
+
* reporting nothing. Surfaces as a rollback event (the mutation will never
|
|
1457
|
+
* apply, which is what a subscriber needs to know) and degrades sync health.
|
|
1458
|
+
*/
|
|
1459
|
+
private onMutationDropped;
|
|
1460
|
+
/** A forwarded outbox row from a follower: load + drain it. Idempotent. */
|
|
1461
|
+
enqueueForwardedMutation(mutationId: string): Promise<void>;
|
|
1462
|
+
/**
|
|
1463
|
+
* Tuples another tab already committed to the shared store: feed them to
|
|
1464
|
+
* THIS tab's circuit (no local write). A DELETE additionally forces a
|
|
1465
|
+
* re-materialize of the table's queries, exactly as the writing tab does
|
|
1466
|
+
* for itself, because the SSP may not emit a view update for it.
|
|
1467
|
+
*/
|
|
1468
|
+
private applyRelayedIngest;
|
|
1469
|
+
/** A relayed `_00_list_ref` LIVE event: resolve against THIS tab's queries
|
|
1470
|
+
* and run the exact same handling the LIVE subscription would have. */
|
|
1471
|
+
private applyRelayedListRefChange;
|
|
1472
|
+
/** One immediate poll cycle (failover convergence). */
|
|
1473
|
+
forcePollRound(): Promise<void>;
|
|
1474
|
+
/**
|
|
1475
|
+
* Quiesce all sync activity ahead of a local-bucket switch. After this
|
|
1476
|
+
* resolves, nothing in the sync module writes to the local store: the poll
|
|
1477
|
+
* loop is stopped AND its in-flight tick awaited, LIVE is killed, debounce
|
|
1478
|
+
* timers are cancelled (their outbox rows are already persisted), and the
|
|
1479
|
+
* scheduler has drained its in-flight queue item — including that item's
|
|
1480
|
+
* outbox-row delete, which must land in the OLD bucket. Queued down-events
|
|
1481
|
+
* are dropped (they reference old-bucket query rows; the post-switch rebind
|
|
1482
|
+
* re-enqueues registrations). The old user's un-pushed outbox is deliberately
|
|
1483
|
+
* NOT drained: the remote session already belongs to the next user.
|
|
1484
|
+
*/
|
|
1485
|
+
prepareBucketSwitch(): Promise<void>;
|
|
1486
|
+
/**
|
|
1487
|
+
* Resume syncing against the freshly-opened bucket: reload the mutation
|
|
1488
|
+
* outbox from ITS `_00_pending_mutations` (the new user's own un-pushed
|
|
1489
|
+
* offline work) and restart the scheduler. LIVE + the list_ref poll restart
|
|
1490
|
+
* via the `setCurrentUserId` call that follows in the auth listener.
|
|
1491
|
+
*/
|
|
1492
|
+
completeBucketSwitch(): Promise<void>;
|
|
1493
|
+
/**
|
|
1494
|
+
* Push the authenticated user's record id from the parent client's
|
|
1495
|
+
* auth subscription. Tears down the existing `_00_list_ref` LIVE (if
|
|
1496
|
+
* any) and re-registers it under the new user's dedicated table so
|
|
1497
|
+
* SurrealDB binds the permission rule under the post-flip auth
|
|
1498
|
+
* context. Pass `null` on sign-out.
|
|
1499
|
+
*
|
|
1500
|
+
* The dedicated `_00_list_ref_user_<id>` table is created lazily by
|
|
1501
|
+
* the SSP when the first query registration arrives, which may be
|
|
1502
|
+
* concurrent with this call. We retry the LIVE registration with a
|
|
1503
|
+
* short backoff so a "table not found" race resolves without
|
|
1504
|
+
* surfacing as a permanent auth-loading hang.
|
|
1505
|
+
*/
|
|
1506
|
+
setCurrentUserId(userId: string | null): Promise<void>;
|
|
1507
|
+
private startListRefPoll;
|
|
1508
|
+
private stopListRefPoll;
|
|
1509
|
+
/**
|
|
1510
|
+
* One poll cycle: refetch `_00_list_ref` for every active query. Returns
|
|
1511
|
+
* whether ANY query's remoteArray actually changed — the scheduler uses this
|
|
1512
|
+
* to drive the adaptive idle backoff.
|
|
1513
|
+
*
|
|
1514
|
+
* Also the ONLY health signal that runs while the page is idle. Sync health is
|
|
1515
|
+
* otherwise activity-driven (mutations/registrations via the scheduler,
|
|
1516
|
+
* reconnect re-registration, self-heal), so on a quiet page a stale `degraded`
|
|
1517
|
+
* would linger until the next mutation and a genuine idle drop would be
|
|
1518
|
+
* invisible. We fold the cycle's aggregate reachability into `recordSyncOutcome`
|
|
1519
|
+
* so idle health self-recovers (and self-degrades) with no user action. A clean
|
|
1520
|
+
* cycle is idempotent when already healthy (`recordSyncOutcome` early-returns at
|
|
1521
|
+
* `consecutiveSyncFailures === 0`), so a healthy idle page pays nothing.
|
|
1522
|
+
*/
|
|
1523
|
+
private pollListRefForActiveQueries;
|
|
1524
|
+
/**
|
|
1525
|
+
* Pull the upstream list_ref entries for `queryHash`, diff them
|
|
1526
|
+
* against the local `remoteArray` cache, sync any added/updated rows
|
|
1527
|
+
* through the SyncEngine, then persist the new remoteArray. This is
|
|
1528
|
+
* the same shape `createRemoteQuery` does for its initial fetch and
|
|
1529
|
+
* what `handleRemoteListRefChange` does per-LIVE-event — we reuse
|
|
1530
|
+
* it on a timer as a fallback for missed LIVE notifications.
|
|
1531
|
+
*/
|
|
1532
|
+
private refetchListRefForQuery;
|
|
1533
|
+
/**
|
|
1534
|
+
* Resolve the current `_00_list_ref` table name for the active auth
|
|
1535
|
+
* context. Public so the `createRemoteQuery` initial-fetch path can
|
|
1536
|
+
* read from the right per-user table.
|
|
1537
|
+
*
|
|
1538
|
+
* Reads the user id from `DataModule` rather than the local mirror,
|
|
1539
|
+
* because `DataModule.setCurrentUserId` runs synchronously from the
|
|
1540
|
+
* auth callback (before any `await`), whereas `sync.setCurrentUserId`
|
|
1541
|
+
* is async — the userQuery's initial fetch can fire between those
|
|
1542
|
+
* two points and we need the correct table name immediately.
|
|
1543
|
+
*/
|
|
1544
|
+
listRefTable(): string;
|
|
1545
|
+
private killRefLiveQuery;
|
|
1546
|
+
private restartRefLiveQuery;
|
|
1547
|
+
/**
|
|
1548
|
+
* Drop local LIVE bookkeeping without issuing a `KILL`.
|
|
1549
|
+
*
|
|
1550
|
+
* Called when the socket dies. The server-side subscription is scoped to that
|
|
1551
|
+
* WebSocket session and died with it, so there is nothing left to kill — and
|
|
1552
|
+
* by the time the reconnect handler runs, the client reports `connected`
|
|
1553
|
+
* again, which would otherwise send a `KILL` for a stale uuid on the *new*
|
|
1554
|
+
* session and hold up the restart queued behind it.
|
|
1555
|
+
*/
|
|
1556
|
+
private invalidateRefLiveQuery;
|
|
1557
|
+
private subscribeToReconnect;
|
|
1558
|
+
private startRefLiveQueries;
|
|
1559
|
+
private handleRemoteListRefChange;
|
|
1560
|
+
/**
|
|
1561
|
+
* Handle a LIVE change to a SUBQUERY child edge (a `_00_list_ref` row with
|
|
1562
|
+
* `parent` set) for a `.related()` query. Unlike primary rows, child rows
|
|
1563
|
+
* must NOT touch the query's `localArray`/`remoteArray`/`rowCount`; we only
|
|
1564
|
+
* keep the child BODY fresh in the local cache so the in-browser SSP's
|
|
1565
|
+
* subquery-table dependency re-materializes the parent view.
|
|
1566
|
+
*
|
|
1567
|
+
* CREATE/UPDATE fetch+upsert the child body. DELETE is intentionally a
|
|
1568
|
+
* no-op: a child leaving this query's set must not delete a body another
|
|
1569
|
+
* query may still show (see `syncSubqueryChildren` deletion-safety note);
|
|
1570
|
+
* a genuine record delete propagates via the normal delete path.
|
|
1571
|
+
*/
|
|
1572
|
+
private handleRemoteSubqueryChange;
|
|
1573
|
+
/**
|
|
1574
|
+
* Enqueues a 'down' event (from remote to local) for processing.
|
|
1575
|
+
* @param event The DownEvent to enqueue.
|
|
1576
|
+
*/
|
|
1577
|
+
enqueueDownEvent(event: DownEvent): void;
|
|
1578
|
+
/**
|
|
1579
|
+
* Bound a mutation push so it always settles.
|
|
1580
|
+
*
|
|
1581
|
+
* `SyncScheduler.syncUp` early-returns while `isSyncingUp` is true, and that
|
|
1582
|
+
* flag only clears in the `finally` of the drain loop. A push whose RPC never
|
|
1583
|
+
* settles (socket dropped mid-flight, response lost) therefore wedges the
|
|
1584
|
+
* up-queue for the rest of the session: no retry, no error, no further
|
|
1585
|
+
* mutation ever sent. A timeout turns that into an ordinary network failure,
|
|
1586
|
+
* which `UpQueue.next` re-queues for the next trigger. The message deliberately
|
|
1587
|
+
* contains "timed out" so `classifySyncError` treats it as `network` and
|
|
1588
|
+
* retries rather than rolling the mutation back.
|
|
1589
|
+
*/
|
|
1590
|
+
private withPushTimeout;
|
|
1591
|
+
private processUpEvent;
|
|
1592
|
+
/**
|
|
1593
|
+
* A mutation the server accepted, reported once its outbox row is gone.
|
|
1594
|
+
*
|
|
1595
|
+
* Keeps the written row in the render set until its membership arrives.
|
|
1596
|
+
* Without this the row is briefly in neither term of
|
|
1597
|
+
* `(membership ∪ pendingWrites) − pendingDeletes` — the outbox delete is
|
|
1598
|
+
* tied to the push, while membership waits on the SSP ingesting the row,
|
|
1599
|
+
* materializing the view, writing the `_00_list_ref` edge and this client
|
|
1600
|
+
* reading it back. The writer therefore watched its own comment appear,
|
|
1601
|
+
* vanish, and return, while every other client showed it throughout.
|
|
1602
|
+
*/
|
|
1603
|
+
private handleMutationSettled;
|
|
1604
|
+
private handleRollback;
|
|
1605
|
+
private processDownEvent;
|
|
1606
|
+
private withDownTimeout;
|
|
1607
|
+
private runDownEvent;
|
|
1608
|
+
/**
|
|
1609
|
+
* Synchronizes a specific query by hash.
|
|
1610
|
+
* Compares local and remote version arrays and fetches differences.
|
|
1611
|
+
* @param hash The hash of the query to sync.
|
|
1612
|
+
*/
|
|
1613
|
+
syncQuery(hash: string): Promise<void>;
|
|
1614
|
+
/**
|
|
1615
|
+
* Run a sync for a single query while reflecting its fetch status. Marks the
|
|
1616
|
+
* query `fetching` for the duration when the diff actually pulls records
|
|
1617
|
+
* (added/updated), then resets to `idle` in a `finally` so a failed sync
|
|
1618
|
+
* never leaves a query stuck `fetching`. Part A's notification coalescing
|
|
1619
|
+
* means the single resulting UI update lands after this completes.
|
|
1620
|
+
*/
|
|
1621
|
+
private runSyncForQuery;
|
|
1622
|
+
/**
|
|
1623
|
+
* Record ids with a pending local DELETE in the outbox (`_00_pending_mutations`).
|
|
1624
|
+
* Sync must not re-fetch/re-insert these — the remote delete is async, so the
|
|
1625
|
+
* server's `_00_list_ref` still lists them until it's processed, and the diff
|
|
1626
|
+
* would otherwise resurrect a just-deleted record.
|
|
1627
|
+
*/
|
|
1628
|
+
private getPendingDeleteIds;
|
|
1629
|
+
/**
|
|
1630
|
+
* Enqueues a list of mutations (up events) to be sent to the remote.
|
|
1631
|
+
* @param mutations Array of UpEvents (create/update/delete) to enqueue.
|
|
1632
|
+
*/
|
|
1633
|
+
enqueueMutation(mutations: UpEvent[]): Promise<void>;
|
|
1634
|
+
private registerQuery;
|
|
1635
|
+
private createRemoteQuery;
|
|
1636
|
+
/**
|
|
1637
|
+
* Sync the BODIES of a `.related()` query's subquery child rows into the
|
|
1638
|
+
* local cache, separately from the primary window array. The SSP writes
|
|
1639
|
+
* each matched child as a `_00_list_ref` edge tagged `parent`/`parent_rel`;
|
|
1640
|
+
* `buildSubqueryListRefSelect` pulls those `out`+`version` pairs (any
|
|
1641
|
+
* nesting depth). We diff against the in-memory `subqueryRemoteArray` and
|
|
1642
|
+
* fetch added/updated bodies through the SyncEngine — which `saveBatch`s
|
|
1643
|
+
* them into the local DB AND the in-browser SSP, whose subquery-table
|
|
1644
|
+
* dependency then re-materializes the parent view (no explicit notify).
|
|
1645
|
+
*
|
|
1646
|
+
* Deletion safety: we pass `removed: []` deliberately. A child body can be
|
|
1647
|
+
* shared by other queries; letting `handleRemovedRecords` delete one that
|
|
1648
|
+
* merely left THIS query's child set would clobber data another query still
|
|
1649
|
+
* shows. Genuine record deletes flow through the normal delete path; a
|
|
1650
|
+
* lingering orphan body is invisible (the correlated WHERE stops matching).
|
|
1651
|
+
*
|
|
1652
|
+
* Kept off `runSyncForQuery` on purpose so child fetches never flip the
|
|
1653
|
+
* query to `fetching` or skew its DevTools timings.
|
|
1654
|
+
*/
|
|
1655
|
+
private syncSubqueryChildren;
|
|
1656
|
+
heartbeatQuery(queryHash: string): Promise<void>;
|
|
1657
|
+
private cleanupQuery;
|
|
1658
|
+
}
|
|
1659
|
+
//#endregion
|
|
150
1660
|
//#region src/modules/auth/events/index.d.ts
|
|
151
1661
|
declare const AuthEventTypes: {
|
|
152
1662
|
readonly AuthStateChanged: "AUTH_STATE_CHANGED";
|
|
@@ -169,6 +1679,13 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
169
1679
|
token: string | null;
|
|
170
1680
|
currentUser: any | null;
|
|
171
1681
|
isAuthenticated: boolean;
|
|
1682
|
+
/**
|
|
1683
|
+
* The record-access method name for the current session (e.g. `"account"`),
|
|
1684
|
+
* derived from the token's `AC` claim. Consumed by the in-browser SSP's
|
|
1685
|
+
* permission injection so `$access`-gated table predicates resolve locally,
|
|
1686
|
+
* mirroring the server's `$access`. Null when logged out.
|
|
1687
|
+
*/
|
|
1688
|
+
access: string | null;
|
|
172
1689
|
isLoading: boolean;
|
|
173
1690
|
private events;
|
|
174
1691
|
get eventSystem(): AuthEventSystem;
|
|
@@ -181,6 +1698,24 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
181
1698
|
*/
|
|
182
1699
|
subscribe(cb: (userId: string | null) => void): () => void;
|
|
183
1700
|
private notifyListeners;
|
|
1701
|
+
/**
|
|
1702
|
+
* Restore a session from the locally cached JWT, with NO network.
|
|
1703
|
+
*
|
|
1704
|
+
* This is what makes a warm boot paint instantly and what makes an offline
|
|
1705
|
+
* boot possible at all: the token is in local storage, and it already carries
|
|
1706
|
+
* both the access method and the `$auth.id` record id. Everything the client
|
|
1707
|
+
* needs to route queries (`setCurrentUserId`) and to satisfy `$auth`-gated
|
|
1708
|
+
* permission predicates in the in-browser SSP (`setSessionAuth`) is therefore
|
|
1709
|
+
* available before a socket exists.
|
|
1710
|
+
*
|
|
1711
|
+
* The session is OPTIMISTIC: the token is unverified here. `check()` runs
|
|
1712
|
+
* afterwards in the background and downgrades to a real sign-out if the
|
|
1713
|
+
* server rejects it. Nothing is trusted that the server has not also seen -
|
|
1714
|
+
* the local store only ever holds rows the server previously sent.
|
|
1715
|
+
*
|
|
1716
|
+
* Returns the restored user id, or null when there is no usable token.
|
|
1717
|
+
*/
|
|
1718
|
+
restoreSessionFromToken(): Promise<string | null>;
|
|
184
1719
|
/**
|
|
185
1720
|
* Check for existing session and validate
|
|
186
1721
|
*/
|
|
@@ -190,17 +1725,672 @@ declare class AuthService<S extends SchemaStructure> {
|
|
|
190
1725
|
*/
|
|
191
1726
|
signOut(): Promise<void>;
|
|
192
1727
|
private setSession;
|
|
1728
|
+
/** Fallback when the token carries no `AC` claim: if the schema defines
|
|
1729
|
+
* exactly one record-access method, assume the session used it. */
|
|
1730
|
+
private defaultAccessName;
|
|
193
1731
|
signUp<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signup'>): Promise<void>;
|
|
194
1732
|
signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
|
|
195
1733
|
}
|
|
196
1734
|
//#endregion
|
|
197
|
-
//#region src/
|
|
1735
|
+
//#region src/modules/crdt/crdt-field.d.ts
|
|
1736
|
+
declare const CURSOR_COLORS: string[];
|
|
1737
|
+
declare function cursorColorFromName(name: string): string;
|
|
1738
|
+
declare class CrdtField {
|
|
1739
|
+
private fieldName;
|
|
1740
|
+
private doc;
|
|
1741
|
+
private pushTimer;
|
|
1742
|
+
private local;
|
|
1743
|
+
private remote;
|
|
1744
|
+
private recordId;
|
|
1745
|
+
private sessionId;
|
|
1746
|
+
private unsubscribe;
|
|
1747
|
+
private lastPushTime;
|
|
1748
|
+
private lastCursorPushTime;
|
|
1749
|
+
private loadedFromCrdt;
|
|
1750
|
+
private pushRetryCount;
|
|
1751
|
+
private logger;
|
|
1752
|
+
private cursorsEnabled;
|
|
1753
|
+
/** Remote-push debounce. Local writes happen immediately on every Loro
|
|
1754
|
+
* update; the remote UPSERT is coalesced over this window. Configured
|
|
1755
|
+
* via `Sp00kyConfig.crdtDebounceMs`, default 500. */
|
|
1756
|
+
private remoteDebounceMs;
|
|
1757
|
+
private _onCursorUpdate;
|
|
1758
|
+
private pendingCursorUpdate;
|
|
1759
|
+
/** Callback set by the editor to receive remote cursor updates.
|
|
1760
|
+
* Any cursor data that arrived before this callback was set will be replayed. */
|
|
1761
|
+
set onCursorUpdate(cb: ((data: Uint8Array) => void) | null);
|
|
1762
|
+
get onCursorUpdate(): ((data: Uint8Array) => void) | null;
|
|
1763
|
+
/**
|
|
1764
|
+
* @param LoroDocClass the `LoroDoc` constructor, injected by the caller after
|
|
1765
|
+
* awaiting {@link loadLoro} — keeps `loro-crdt` out of this module's static
|
|
1766
|
+
* import graph so it only ships to apps that use CRDT fields.
|
|
1767
|
+
*/
|
|
1768
|
+
constructor(fieldName: string, cursorsEnabled: boolean, LoroDocClass: typeof LoroDoc, initialState?: Uint8Array, logger?: Logger$1 | null);
|
|
1769
|
+
getDoc(): LoroDoc;
|
|
1770
|
+
/** Whether the LoroDoc was loaded from saved CRDT state */
|
|
1771
|
+
hasContent(): boolean;
|
|
1772
|
+
startSync(local: LocalStore, remote: RemoteDatabaseService, recordId: string, sessionId: string, debounceMs: number): void;
|
|
1773
|
+
/**
|
|
1774
|
+
* Stop syncing this field. Flushes one final remote push by default so the
|
|
1775
|
+
* last keystrokes aren't lost. Pass `{ flush: false }` on a bucket switch —
|
|
1776
|
+
* the remote session already belongs to the NEXT user, and pushing this
|
|
1777
|
+
* (previous user's) snapshot under it would clobber the record remotely.
|
|
1778
|
+
*/
|
|
1779
|
+
stopSync(options?: {
|
|
1780
|
+
flush?: boolean;
|
|
1781
|
+
}): void;
|
|
1782
|
+
importRemote(state: Uint8Array): void;
|
|
1783
|
+
exportSnapshot(): Uint8Array;
|
|
1784
|
+
/** Push this session's cursor blob into the parent row at
|
|
1785
|
+
* `<field>.cursors[$sid]`. No-op when cursors aren't enabled on this
|
|
1786
|
+
* field — the editor still calls this method optimistically, but
|
|
1787
|
+
* without `@cursor` on the schema there's nowhere to store the blob.
|
|
1788
|
+
* The UPDATE itself fires the parent table's LIVE feed, so other
|
|
1789
|
+
* browsers receive the cursor change without a separate `_00_rv` bump. */
|
|
1790
|
+
pushCursorState(encoded: Uint8Array): Promise<void>;
|
|
1791
|
+
/** Import remote cursor state (called by CrdtManager from LIVE SELECT) */
|
|
1792
|
+
importRemoteCursor(base64State: string): void;
|
|
1793
|
+
private scheduleRemotePush;
|
|
1794
|
+
/** SET path inside a parent row for the current snapshot. `@crdt`-only
|
|
1795
|
+
* fields hold the snapshot directly (`<field>`); `@crdt @cursor`
|
|
1796
|
+
* fields hold a `{ state, cursors }` object so the snapshot lives at
|
|
1797
|
+
* `<field>.state` next to per-session cursor blobs. */
|
|
1798
|
+
private statePath;
|
|
1799
|
+
/** Mirror the LoroDoc snapshot into the parent row locally. Runs on
|
|
1800
|
+
* every local update and every remote import so reloads (online or
|
|
1801
|
+
* offline) see the freshest content immediately. Failures are
|
|
1802
|
+
* swallowed — a stale local write must never block user input. */
|
|
1803
|
+
private persistLocal;
|
|
1804
|
+
private pushToRemote;
|
|
1805
|
+
}
|
|
1806
|
+
//#endregion
|
|
1807
|
+
//#region src/modules/crdt/index.d.ts
|
|
1808
|
+
/**
|
|
1809
|
+
* CrdtManager manages active CrdtField instances and their sync channels.
|
|
1810
|
+
*
|
|
1811
|
+
* Collaborative state lives in two dedicated tables (defined in
|
|
1812
|
+
* `apps/cli/src/meta_tables_remote.surql`):
|
|
1813
|
+
* - `_00_crdt` { record_id, field, state } — one row per (record, field)
|
|
1814
|
+
* - `_00_cursor` { record_id, session_id, field, state } — one row per
|
|
1815
|
+
* (record, session, field)
|
|
1816
|
+
*
|
|
1817
|
+
* Splitting them off the parent row is what makes offline edits mergeable:
|
|
1818
|
+
* each (record, field) gets its own row, so concurrent offline writes don't
|
|
1819
|
+
* collide on the parent's last-write-wins semantics.
|
|
1820
|
+
*
|
|
1821
|
+
* Cross-browser delivery still rides the parent table's existing LIVE feed
|
|
1822
|
+
* to avoid SurrealDB v3 LIVE bugs around dereference-based permission rules
|
|
1823
|
+
* (issues 3602, 4026). On every meta UPSERT the writer also bumps the
|
|
1824
|
+
* parent's `_00_rv` (a no-op assignment); that fires the parent's LIVE
|
|
1825
|
+
* feed, and the receiver pulls the matching `_00_crdt` / `_00_cursor` rows
|
|
1826
|
+
* via subquery. Permission inheritance happens server-side via
|
|
1827
|
+
* `record_id.id != NONE` (SELECT) and `fn::can_update_record` (UPDATE).
|
|
1828
|
+
*/
|
|
1829
|
+
declare class CrdtManager {
|
|
1830
|
+
private schema;
|
|
1831
|
+
private local;
|
|
1832
|
+
private remote;
|
|
1833
|
+
private debounceMs;
|
|
1834
|
+
private fields;
|
|
1835
|
+
private liveByTable;
|
|
1836
|
+
private pendingLive;
|
|
1837
|
+
private staleTables;
|
|
1838
|
+
private connectionGeneration;
|
|
1839
|
+
private connectionUnsubscribes;
|
|
1840
|
+
private logger;
|
|
1841
|
+
private sessionId;
|
|
1842
|
+
constructor(schema: SchemaStructure, local: LocalStore, remote: RemoteDatabaseService, logger: Logger$1, debounceMs?: number);
|
|
1843
|
+
/**
|
|
1844
|
+
* Re-establish table LIVEs after a socket drop.
|
|
1845
|
+
*
|
|
1846
|
+
* A LIVE subscription lives and dies with its WebSocket session, and
|
|
1847
|
+
* `ensureTableSubscription` is memoized on `liveByTable` — so without this,
|
|
1848
|
+
* the first reconnect leaves CRDT realtime permanently dead: the map still
|
|
1849
|
+
* holds a uuid for a subscription the server has forgotten, so every later
|
|
1850
|
+
* `open()` short-circuits and no LIVE is ever re-issued.
|
|
1851
|
+
*
|
|
1852
|
+
* Both drop events matter: the SDK publishes `reconnecting` (not
|
|
1853
|
+
* `disconnected`) when it intends to recover on its own, and `disconnected`
|
|
1854
|
+
* only once it has given up.
|
|
1855
|
+
*/
|
|
1856
|
+
private subscribeToReconnect;
|
|
1857
|
+
/** Stop observing transport events. Separate from {@link closeAll}, which also
|
|
1858
|
+
* runs on a bucket switch where the manager keeps being used. */
|
|
1859
|
+
dispose(): void;
|
|
1860
|
+
private hasOpenFieldFor;
|
|
1861
|
+
/** Set the session id that scopes this client's cursor entries. Must be
|
|
1862
|
+
* called before `open()` for cursors to be pushed under a stable key.
|
|
1863
|
+
* Passed in from `sp00ky.ts` at boot (it already fetches `session::id()`
|
|
1864
|
+
* for the data-module salt). */
|
|
1865
|
+
setSessionId(sessionId: string): void;
|
|
1866
|
+
/**
|
|
1867
|
+
* Open a CRDT field for collaborative editing.
|
|
1868
|
+
*
|
|
1869
|
+
* @param table - Table name
|
|
1870
|
+
* @param recordId - Full record ID (e.g., "thread:abc")
|
|
1871
|
+
* @param field - Field name (e.g., "title", "content")
|
|
1872
|
+
* @param fallbackText - Current plain text from the record, used to seed the
|
|
1873
|
+
* LoroDoc if no CRDT state exists yet (migration path)
|
|
1874
|
+
*/
|
|
1875
|
+
open(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
|
|
1876
|
+
close(table: string, recordId: string, field: string): void;
|
|
1877
|
+
/**
|
|
1878
|
+
* Close every open field + table LIVE. Fields flush a final remote push by
|
|
1879
|
+
* default; pass `{ flush: false }` on a bucket switch, where that flush
|
|
1880
|
+
* would push the previous user's snapshot under the next user's session.
|
|
1881
|
+
*/
|
|
1882
|
+
closeAll(options?: {
|
|
1883
|
+
flush?: boolean;
|
|
1884
|
+
}): void;
|
|
1885
|
+
/** Ensure a single `LIVE SELECT * FROM <table>` is running, shared across
|
|
1886
|
+
* every open CrdtField on `table`. */
|
|
1887
|
+
private ensureTableSubscription;
|
|
1888
|
+
/** Apply a parent-row payload from a non-LIVE source (e.g. the
|
|
1889
|
+
* list_ref-driven sync engine, when the cross-user LIVE on the
|
|
1890
|
+
* parent table is filtered out by the SurrealDB cross-session
|
|
1891
|
+
* permission gap). Same semantics as the internal `dispatchRow`. */
|
|
1892
|
+
applyRow(table: string, row: Record<string, unknown>): void;
|
|
1893
|
+
/** Dispatch a parent-row LIVE event to every open CrdtField on that
|
|
1894
|
+
* record. Each open field reads its slice of the row directly — the
|
|
1895
|
+
* CRDT snapshot is a column on the parent now, so there is no
|
|
1896
|
+
* follow-up subquery. */
|
|
1897
|
+
private dispatchRow;
|
|
1898
|
+
/** One-shot remote fetch for a row whose CRDT field hasn't synced
|
|
1899
|
+
* locally yet (fresh device, memory-backed local DB after reload, …).
|
|
1900
|
+
* Used by `open()` when the local read came up empty. Subsequent
|
|
1901
|
+
* cross-browser updates ride `dispatchRow` via the parent LIVE feed. */
|
|
1902
|
+
private fetchAndDispatchRow;
|
|
1903
|
+
/** Schema lookup: does `<table>.<field>` carry a `@cursor` annotation?
|
|
1904
|
+
* Determines the on-disk shape (plain snapshot vs. `{ state, cursors }`). */
|
|
1905
|
+
private fieldHasCursor;
|
|
1906
|
+
/** Pull the LoroDoc snapshot bytes out of a row slice. For `@crdt`-only
|
|
1907
|
+
* the slice IS the snapshot (Uint8Array); for `@crdt @cursor` it's
|
|
1908
|
+
* `{ state, cursors }` where `state` carries the snapshot bytes. */
|
|
1909
|
+
private extractSnapshot;
|
|
1910
|
+
private killTableSubscription;
|
|
1911
|
+
private makeKey;
|
|
1912
|
+
/**
|
|
1913
|
+
* Throws if `<table>.<field>` is not annotated `@crdt` in the schema. Catches
|
|
1914
|
+
* typos, removed annotations, and stale schema codegen at the call site instead
|
|
1915
|
+
* of silently producing a non-CRDT writer.
|
|
1916
|
+
*/
|
|
1917
|
+
private assertCrdtField;
|
|
1918
|
+
}
|
|
1919
|
+
//#endregion
|
|
1920
|
+
//#region src/modules/feature-flag/index.d.ts
|
|
1921
|
+
interface FeatureFlagSnapshot {
|
|
1922
|
+
variant: string | undefined;
|
|
1923
|
+
payload: unknown | undefined;
|
|
1924
|
+
}
|
|
1925
|
+
interface FeatureFlagOptions {
|
|
1926
|
+
fallback?: string;
|
|
1927
|
+
ttl?: QueryTimeToLive;
|
|
1928
|
+
}
|
|
1929
|
+
/**
|
|
1930
|
+
* A locally forced variant. Applies to THIS browser only and is never sent to
|
|
1931
|
+
* the server — the assignment in `_00_user_feature` is untouched, so clearing
|
|
1932
|
+
* the override restores whatever the server says.
|
|
1933
|
+
*/
|
|
1934
|
+
interface FeatureFlagOverride {
|
|
1935
|
+
variant: string;
|
|
1936
|
+
payload?: unknown;
|
|
1937
|
+
}
|
|
1938
|
+
declare class FeatureFlagHandle {
|
|
1939
|
+
readonly key: string;
|
|
1940
|
+
readonly fallback: string | undefined;
|
|
1941
|
+
private latest;
|
|
1942
|
+
private listeners;
|
|
1943
|
+
private unsubscribeFn;
|
|
1944
|
+
private onCloseFn;
|
|
1945
|
+
private closed;
|
|
1946
|
+
constructor(key: string, fallback: string | undefined);
|
|
1947
|
+
attach(unsubscribe: () => void): void;
|
|
1948
|
+
detach(): void;
|
|
1949
|
+
set(snapshot: FeatureFlagSnapshot): void;
|
|
1950
|
+
variant(): string | undefined;
|
|
1951
|
+
payload<T = unknown>(): T | undefined;
|
|
1952
|
+
enabled(): boolean;
|
|
1953
|
+
subscribe(cb: (s: FeatureFlagSnapshot) => void): () => void;
|
|
1954
|
+
onClose(cb: () => void): void;
|
|
1955
|
+
close(): void;
|
|
1956
|
+
}
|
|
1957
|
+
interface FeatureFlagModuleDeps<S extends SchemaStructure> {
|
|
1958
|
+
dataModule: DataModule<S>;
|
|
1959
|
+
sync: Sp00kySync<S>;
|
|
1960
|
+
auth: AuthService<S>;
|
|
1961
|
+
logger: Logger$1;
|
|
1962
|
+
}
|
|
1963
|
+
declare class FeatureFlagModule<S extends SchemaStructure> {
|
|
1964
|
+
private deps;
|
|
1965
|
+
private logger;
|
|
1966
|
+
private handles;
|
|
1967
|
+
private authUnsubscribe;
|
|
1968
|
+
private lastUserId;
|
|
1969
|
+
private querySubscription;
|
|
1970
|
+
private starting;
|
|
1971
|
+
private ttl;
|
|
1972
|
+
private snapshots;
|
|
1973
|
+
private loaded;
|
|
1974
|
+
private overrides;
|
|
1975
|
+
constructor(deps: FeatureFlagModuleDeps<S>);
|
|
1976
|
+
init(): void;
|
|
1977
|
+
feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
|
|
1978
|
+
closeAll(): Promise<void>;
|
|
1979
|
+
/** Auth changed: drop the old user's query/snapshots and re-observe. */
|
|
1980
|
+
private refresh;
|
|
1981
|
+
private teardownQuery;
|
|
1982
|
+
/** Start the single shared live query (idempotent; no-op with no handles). */
|
|
1983
|
+
private ensureStarted;
|
|
1984
|
+
/** Live query result → per-key snapshots → push to every active handle. */
|
|
1985
|
+
private applyRecords;
|
|
1986
|
+
/**
|
|
1987
|
+
* Force `key` to `variant` in THIS browser. Pass `null` to clear.
|
|
1988
|
+
*
|
|
1989
|
+
* Nothing is written to the server: the `_00_user_feature` assignment is
|
|
1990
|
+
* untouched, so clearing restores whatever the server says. Persisted to
|
|
1991
|
+
* localStorage on the page origin, so it survives a reload.
|
|
1992
|
+
*/
|
|
1993
|
+
setLocalOverride(key: string, variant: string | null, payload?: unknown): void;
|
|
1994
|
+
clearLocalOverrides(): void;
|
|
1995
|
+
getLocalOverrides(): Record<string, FeatureFlagOverride>;
|
|
1996
|
+
/** The assignment for `key`, with any local override taking precedence. */
|
|
1997
|
+
private resolve;
|
|
1998
|
+
private pushAll;
|
|
1999
|
+
private loadOverrides;
|
|
2000
|
+
private persistOverrides;
|
|
2001
|
+
}
|
|
2002
|
+
//#endregion
|
|
2003
|
+
//#region src/modules/app-release/index.d.ts
|
|
2004
|
+
interface AppReleaseSnapshot {
|
|
2005
|
+
/** Latest announced version for the app, or undefined when no row exists. */
|
|
2006
|
+
version: string | undefined;
|
|
2007
|
+
/** Clients should clear SW/caches when reloading onto this version. */
|
|
2008
|
+
cacheBust: boolean;
|
|
2009
|
+
/** Clients should reload/update immediately instead of asking. */
|
|
2010
|
+
mandatory: boolean;
|
|
2011
|
+
releasedAt: string | undefined;
|
|
2012
|
+
}
|
|
2013
|
+
interface AppReleaseOptions {
|
|
2014
|
+
ttl?: QueryTimeToLive;
|
|
2015
|
+
}
|
|
2016
|
+
declare class AppReleaseHandle {
|
|
2017
|
+
readonly app: string;
|
|
2018
|
+
private latest;
|
|
2019
|
+
private listeners;
|
|
2020
|
+
private onCloseFn;
|
|
2021
|
+
private closed;
|
|
2022
|
+
constructor(app: string);
|
|
2023
|
+
set(snapshot: AppReleaseSnapshot): void;
|
|
2024
|
+
snapshot(): AppReleaseSnapshot;
|
|
2025
|
+
version(): string | undefined;
|
|
2026
|
+
/** True when the announced version is semver-newer than `currentVersion`. */
|
|
2027
|
+
updateAvailable(currentVersion: string): boolean;
|
|
2028
|
+
subscribe(cb: (s: AppReleaseSnapshot) => void): () => void;
|
|
2029
|
+
onClose(cb: () => void): void;
|
|
2030
|
+
close(): void;
|
|
2031
|
+
}
|
|
2032
|
+
interface AppReleaseModuleDeps<S extends SchemaStructure> {
|
|
2033
|
+
dataModule: DataModule<S>;
|
|
2034
|
+
sync: Sp00kySync<S>;
|
|
2035
|
+
auth: AuthService<S>;
|
|
2036
|
+
logger: Logger$1;
|
|
2037
|
+
}
|
|
2038
|
+
declare class AppReleaseModule<S extends SchemaStructure> {
|
|
2039
|
+
private deps;
|
|
2040
|
+
private logger;
|
|
2041
|
+
private handles;
|
|
2042
|
+
private authUnsubscribe;
|
|
2043
|
+
private lastUserId;
|
|
2044
|
+
private querySubscription;
|
|
2045
|
+
private starting;
|
|
2046
|
+
private ttl;
|
|
2047
|
+
private snapshots;
|
|
2048
|
+
private loaded;
|
|
2049
|
+
constructor(deps: AppReleaseModuleDeps<S>);
|
|
2050
|
+
init(): void;
|
|
2051
|
+
release(app: string, options?: AppReleaseOptions): AppReleaseHandle;
|
|
2052
|
+
closeAll(): Promise<void>;
|
|
2053
|
+
private refresh;
|
|
2054
|
+
private teardownQuery;
|
|
2055
|
+
private ensureStarted;
|
|
2056
|
+
private applyRecords;
|
|
2057
|
+
}
|
|
2058
|
+
//#endregion
|
|
2059
|
+
//#region src/services/blobs/blob-store.d.ts
|
|
2060
|
+
/**
|
|
2061
|
+
* Byte storage for cached bucket files.
|
|
2062
|
+
*
|
|
2063
|
+
* The default implementation is OPFS. Bucket files never arrive over HTTP in
|
|
2064
|
+
* this client — `BucketHandle.get()` is a SurrealQL RPC on the sync socket — so
|
|
2065
|
+
* neither the browser's HTTP cache nor the Cache API can hold them. We persist
|
|
2066
|
+
* the bytes ourselves, and OPFS is the cheapest place to put them: a read is
|
|
2067
|
+
* `getFile()` → a disk-backed lazy `File` that `URL.createObjectURL` can serve
|
|
2068
|
+
* without ever moving the bytes through the JS heap.
|
|
2069
|
+
*
|
|
2070
|
+
* Layout is real nested directories rather than one hashed filename:
|
|
2071
|
+
*
|
|
2072
|
+
* sp00ky-blobs/<namespace>/<bucket>/<...path segments>
|
|
2073
|
+
*
|
|
2074
|
+
* That costs a `getDirectoryHandle` per segment on write, and buys the property
|
|
2075
|
+
* the whole orphan story rests on: the full `(bucket, path)` key is recoverable
|
|
2076
|
+
* from a directory walk alone. The `_00_blob` manifest can therefore be wiped
|
|
2077
|
+
* (memory fallback, SQLite pool wipe, IndexedDB corruption recovery) and be
|
|
2078
|
+
* rebuilt from disk instead of taking the cached bytes down with it.
|
|
2079
|
+
*/
|
|
2080
|
+
/** Identifies one cached file: the bucket it lives in and its path within. */
|
|
2081
|
+
interface BlobKey {
|
|
2082
|
+
bucket: string;
|
|
2083
|
+
path: string;
|
|
2084
|
+
}
|
|
2085
|
+
/** What a directory walk can tell us about a stored file, with no manifest. */
|
|
2086
|
+
interface BlobStat {
|
|
2087
|
+
key: BlobKey;
|
|
2088
|
+
size: number;
|
|
2089
|
+
/** File mtime. Seeds `lastAccess` when a manifest row has to be rebuilt. */
|
|
2090
|
+
mtime: number;
|
|
2091
|
+
}
|
|
2092
|
+
interface BlobStore {
|
|
2093
|
+
/** False for {@link MemoryBlobStore} and for OPFS-less environments: the
|
|
2094
|
+
* cache still dedupes and serves within a tab, but nothing survives reload. */
|
|
2095
|
+
readonly persistent: boolean;
|
|
2096
|
+
/** Namespace (the local bucketId) all keys are resolved under. */
|
|
2097
|
+
readonly namespace: string;
|
|
2098
|
+
read(key: BlobKey): Promise<Blob | null>;
|
|
2099
|
+
/** Returns the number of bytes written. Throws on quota exhaustion. */
|
|
2100
|
+
write(key: BlobKey, bytes: Blob): Promise<number>;
|
|
2101
|
+
remove(key: BlobKey): Promise<void>;
|
|
2102
|
+
/** Every committed file under the current namespace. Sweeps torn writes. */
|
|
2103
|
+
list(): Promise<BlobStat[]>;
|
|
2104
|
+
/** Drop the whole namespace (sign-out with `clearOnSignOut`, or a reset). */
|
|
2105
|
+
clear(): Promise<void>;
|
|
2106
|
+
/** Point at another namespace. Does not touch the bytes of the old one. */
|
|
2107
|
+
setNamespace(namespace: string): void;
|
|
2108
|
+
}
|
|
2109
|
+
//#endregion
|
|
2110
|
+
//#region src/services/blobs/blob-manifest.d.ts
|
|
2111
|
+
interface BlobEntry {
|
|
2112
|
+
/** `${bucket}/${path}` — also the `_00_blob` row id. */
|
|
2113
|
+
id: string;
|
|
2114
|
+
bucket: string;
|
|
2115
|
+
path: string;
|
|
2116
|
+
size: number;
|
|
2117
|
+
contentType: string;
|
|
2118
|
+
createdAt: number;
|
|
2119
|
+
lastAccess: number;
|
|
2120
|
+
hits: number;
|
|
2121
|
+
/** Exempt from pressure eviction. Never expires on its own. */
|
|
2122
|
+
pinned: boolean;
|
|
2123
|
+
}
|
|
2124
|
+
declare class BlobManifest {
|
|
2125
|
+
private local;
|
|
2126
|
+
private entries;
|
|
2127
|
+
/** Ids whose in-memory state has not been written back yet. */
|
|
2128
|
+
private dirty;
|
|
2129
|
+
private removed;
|
|
2130
|
+
private flushing;
|
|
2131
|
+
constructor(local: LocalStore);
|
|
2132
|
+
/**
|
|
2133
|
+
* Hydrate from the rows matching `keys`. Ids come from the OPFS listing, so
|
|
2134
|
+
* this never needs a full-table scan (and therefore never needs a QueryPlan).
|
|
2135
|
+
* Any read failure yields an empty manifest: reconcile then rebuilds every
|
|
2136
|
+
* row from disk, which is exactly the desired degradation.
|
|
2137
|
+
*/
|
|
2138
|
+
load(ids: string[]): Promise<void>;
|
|
2139
|
+
get(key: BlobKey): BlobEntry | undefined;
|
|
2140
|
+
getById(id: string): BlobEntry | undefined;
|
|
2141
|
+
all(): BlobEntry[];
|
|
2142
|
+
totalBytes(): number;
|
|
2143
|
+
pinnedBytes(): number;
|
|
2144
|
+
put(entry: BlobEntry): void;
|
|
2145
|
+
touch(id: string, now: number): void;
|
|
2146
|
+
setPinned(id: string, pinned: boolean): boolean;
|
|
2147
|
+
remove(id: string): void;
|
|
2148
|
+
/** Forget everything without scheduling deletes — for a bucket switch, where
|
|
2149
|
+
* the rows belong to the store we are leaving and must stay put. */
|
|
2150
|
+
reset(): void;
|
|
2151
|
+
hasPendingWrites(): boolean;
|
|
2152
|
+
/**
|
|
2153
|
+
* Write back pending changes. Serialized: a second concurrent flush awaits
|
|
2154
|
+
* the first rather than racing it into the same rows. Failures are swallowed
|
|
2155
|
+
* on purpose — a lost metadata write costs an LRU timestamp, and the entry is
|
|
2156
|
+
* rebuilt from disk on the next reconcile.
|
|
2157
|
+
*/
|
|
2158
|
+
flush(): Promise<void>;
|
|
2159
|
+
private doFlush;
|
|
2160
|
+
}
|
|
2161
|
+
//#endregion
|
|
2162
|
+
//#region src/services/blobs/blob-cache.d.ts
|
|
2163
|
+
interface BlobUrlLease {
|
|
2164
|
+
url: string;
|
|
2165
|
+
release(): void;
|
|
2166
|
+
}
|
|
2167
|
+
interface BlobReadOptions {
|
|
2168
|
+
/** Write through to L1 on a miss. Default true. */
|
|
2169
|
+
persist?: boolean;
|
|
2170
|
+
/** Mark the entry exempt from pressure eviction. */
|
|
2171
|
+
pin?: boolean;
|
|
2172
|
+
/**
|
|
2173
|
+
* Default `'never'`: a bucket path is treated as immutable, which is how the
|
|
2174
|
+
* client writes them (`crypto.randomUUID() + ext`). `'head'` spends a remote
|
|
2175
|
+
* `head()` to compare sizes before trusting L1.
|
|
2176
|
+
*/
|
|
2177
|
+
revalidate?: 'never' | 'head';
|
|
2178
|
+
/** Skip L0/L1 entirely and refill from remote. Backs `refetch()`. */
|
|
2179
|
+
reload?: boolean;
|
|
2180
|
+
}
|
|
2181
|
+
interface BlobCacheStats {
|
|
2182
|
+
entries: number;
|
|
2183
|
+
totalBytes: number;
|
|
2184
|
+
budgetBytes: number;
|
|
2185
|
+
pinnedBytes: number;
|
|
2186
|
+
evictedEntries: number;
|
|
2187
|
+
evictedBytes: number;
|
|
2188
|
+
reconciledEntries: number;
|
|
2189
|
+
hits: number;
|
|
2190
|
+
misses: number;
|
|
2191
|
+
persistent: boolean;
|
|
2192
|
+
/** True when pinned bytes alone exceed the budget: new entries stop being
|
|
2193
|
+
* written rather than pinned ones being thrown away. */
|
|
2194
|
+
persistPaused: boolean;
|
|
2195
|
+
}
|
|
2196
|
+
interface BlobCacheOptions {
|
|
2197
|
+
store: BlobStore;
|
|
2198
|
+
manifest: BlobManifest;
|
|
2199
|
+
/** L2 read. Resolves to null when the file does not exist remotely. */
|
|
2200
|
+
fetchRemote(key: BlobKey): Promise<Blob | null>;
|
|
2201
|
+
/** L2 metadata, for `revalidate: 'head'`. */
|
|
2202
|
+
headRemote?(key: BlobKey): Promise<Record<string, unknown> | null>;
|
|
2203
|
+
logger: Logger$1;
|
|
2204
|
+
maxBytes: number;
|
|
2205
|
+
now?: () => number;
|
|
2206
|
+
/** Injected so the URL layer is exercisable off a DOM (node tests). */
|
|
2207
|
+
urls?: {
|
|
2208
|
+
create(blob: Blob): string;
|
|
2209
|
+
revoke(url: string): void;
|
|
2210
|
+
};
|
|
2211
|
+
}
|
|
2212
|
+
declare class BlobCache {
|
|
2213
|
+
private readonly store;
|
|
2214
|
+
private readonly manifest;
|
|
2215
|
+
private readonly fetchRemote;
|
|
2216
|
+
private readonly headRemote?;
|
|
2217
|
+
private readonly logger;
|
|
2218
|
+
private readonly now;
|
|
2219
|
+
private readonly urlFactory;
|
|
2220
|
+
private maxBytes;
|
|
2221
|
+
private persistPaused;
|
|
2222
|
+
/** Set after a quota failure survives one forced eviction. */
|
|
2223
|
+
private persistDisabled;
|
|
2224
|
+
private readonly urls;
|
|
2225
|
+
/** Ids at zero references, oldest first — the hot-URL window. */
|
|
2226
|
+
private idleUrls;
|
|
2227
|
+
private readonly inflight;
|
|
2228
|
+
private flushTimer;
|
|
2229
|
+
private readonly onPageHide;
|
|
2230
|
+
/**
|
|
2231
|
+
* Resolves once the manifest has been reconciled against disk. Reads await
|
|
2232
|
+
* it, so `start()` does NOT have to be awaited on the boot path — blocking
|
|
2233
|
+
* boot on an OPFS directory walk delayed the WebSocket connect (and with it
|
|
2234
|
+
* the connection supervisor) for no benefit.
|
|
2235
|
+
*/
|
|
2236
|
+
private ready;
|
|
2237
|
+
private hits;
|
|
2238
|
+
private misses;
|
|
2239
|
+
private evictedEntries;
|
|
2240
|
+
private evictedBytes;
|
|
2241
|
+
private reconciledEntries;
|
|
2242
|
+
constructor(opts: BlobCacheOptions);
|
|
2243
|
+
/** Coalesce manifest write-back. Metadata only, so losing the tail costs an
|
|
2244
|
+
* LRU timestamp that reconcile reseeds from the file mtime. */
|
|
2245
|
+
private scheduleFlush;
|
|
2246
|
+
/**
|
|
2247
|
+
* Resolve the bytes for `key`, filling L1 on the way when `persist` is on.
|
|
2248
|
+
* Returns null when the file does not exist remotely and is not cached.
|
|
2249
|
+
*/
|
|
2250
|
+
read(key: BlobKey, options?: BlobReadOptions): Promise<Blob | null>;
|
|
2251
|
+
/**
|
|
2252
|
+
* An object URL for `key`, refcounted. Callers MUST `release()`; the URL is
|
|
2253
|
+
* revoked once the last holder lets go and it falls out of the hot window.
|
|
2254
|
+
*/
|
|
2255
|
+
acquireUrl(key: BlobKey, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
|
|
2256
|
+
private lease;
|
|
2257
|
+
private releaseUrl;
|
|
2258
|
+
private revokeUrl;
|
|
2259
|
+
/** L1 lookup with the size check that catches torn and cross-tab writes. */
|
|
2260
|
+
private readLocal;
|
|
2261
|
+
/** True when the remote agrees with the cached size, or cannot be reached. */
|
|
2262
|
+
private headMatches;
|
|
2263
|
+
private fetchDeduped;
|
|
2264
|
+
private persist;
|
|
2265
|
+
private writeThrough;
|
|
2266
|
+
/** Forget one path everywhere. Called on `bucket.put()`/`bucket.delete()`. */
|
|
2267
|
+
invalidate(key: BlobKey): Promise<void>;
|
|
2268
|
+
private dropLocal;
|
|
2269
|
+
setPinned(key: BlobKey, pinned: boolean): void;
|
|
2270
|
+
/**
|
|
2271
|
+
* Bring total bytes under budget by dropping the least recently used
|
|
2272
|
+
* entries. Pinned entries and anything with a live object URL are skipped —
|
|
2273
|
+
* evicting bytes that a mounted `<img>` is displaying would blank it.
|
|
2274
|
+
*/
|
|
2275
|
+
private enforceBudget;
|
|
2276
|
+
/** Evict LRU-first until at or below `target`. Returns the resulting total. */
|
|
2277
|
+
private evictTo;
|
|
2278
|
+
/**
|
|
2279
|
+
* Rebuild the manifest from what is actually on disk. OPFS wins on existence
|
|
2280
|
+
* in both directions: files with no row get a row (seeded from mtime), rows
|
|
2281
|
+
* are only loaded for files that exist, and torn `.part-` writes are swept by
|
|
2282
|
+
* the walk itself.
|
|
2283
|
+
*
|
|
2284
|
+
* Rows whose file vanished outside our control (a browser origin eviction)
|
|
2285
|
+
* are left in `_00_blob`. They are inert — `load()` only ever asks for ids it
|
|
2286
|
+
* found on disk — and are overwritten if that path is cached again.
|
|
2287
|
+
*/
|
|
2288
|
+
reconcile(): Promise<void>;
|
|
2289
|
+
/** Warm the cache for offline use. Skips anything already cached. */
|
|
2290
|
+
prefetch(keys: BlobKey[]): Promise<void>;
|
|
2291
|
+
/** Bind to the boot bucket and hydrate the manifest from disk. Separate from
|
|
2292
|
+
* {@link setNamespace} because boot must reconcile even when the namespace
|
|
2293
|
+
* it lands on is the one the store was constructed with. */
|
|
2294
|
+
start(namespace: string): Promise<void>;
|
|
2295
|
+
/** Repoint at another local bucket. The bytes of the old one stay on disk so
|
|
2296
|
+
* switching back (or signing back in) is still warm. */
|
|
2297
|
+
setNamespace(namespace: string): Promise<void>;
|
|
2298
|
+
setMaxBytes(maxBytes: number): void;
|
|
2299
|
+
/** Delete every cached byte in the current namespace. */
|
|
2300
|
+
clear(): Promise<void>;
|
|
2301
|
+
flush(): Promise<void>;
|
|
2302
|
+
/** Flush metadata and drop every object URL. Must run before the local store
|
|
2303
|
+
* closes — the flush writes through it. */
|
|
2304
|
+
close(): Promise<void>;
|
|
2305
|
+
stats(): BlobCacheStats;
|
|
2306
|
+
}
|
|
2307
|
+
//#endregion
|
|
2308
|
+
//#region src/utils/blurhash.d.ts
|
|
2309
|
+
/**
|
|
2310
|
+
* Blurhash generation settings. `true` enables with the defaults below, `false`
|
|
2311
|
+
* disables. Resolution order for a put: per-call option > client config >
|
|
2312
|
+
* default ON. See {@link Sp00kyConfig.blurhash}.
|
|
2313
|
+
*/
|
|
2314
|
+
type BlurhashSetting = boolean | BlurhashEncodeOptions;
|
|
2315
|
+
interface BlurhashEncodeOptions {
|
|
2316
|
+
/** Horizontal detail components, 1-9. Defaults to 4. */
|
|
2317
|
+
componentX?: number;
|
|
2318
|
+
/** Vertical detail components, 1-9. Defaults to 3. */
|
|
2319
|
+
componentY?: number;
|
|
2320
|
+
}
|
|
2321
|
+
/**
|
|
2322
|
+
* Where an image's blurhash lives: a tiny sidecar object in the same bucket.
|
|
2323
|
+
* Buckets have no per-object metadata channel (`put` is just `.put($content)`),
|
|
2324
|
+
* so the hash for `covers/x_t.webp` is the text object `covers/x_t.webp.bh`.
|
|
2325
|
+
*/
|
|
2326
|
+
declare function blurhashSidecarPath(path: string): string;
|
|
2327
|
+
/** Extensions `bucket.put` treats as images worth hashing. */
|
|
2328
|
+
declare const BLURHASH_IMAGE_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif", "avif", "bmp"];
|
|
2329
|
+
declare function isImagePath(path: string): boolean;
|
|
2330
|
+
/**
|
|
2331
|
+
* Decode `content` as an image and compute its blurhash. Browser-only: returns
|
|
2332
|
+
* null (never throws) when image decoding is unavailable (node, workers without
|
|
2333
|
+
* canvas), when the bytes are not a decodable image, or on any other failure —
|
|
2334
|
+
* a missing hash must never break the upload that triggered it.
|
|
2335
|
+
*/
|
|
2336
|
+
declare function encodeImageToBlurhash(content: string | Uint8Array | Blob, options?: BlurhashEncodeOptions): Promise<string | null>;
|
|
2337
|
+
//#endregion
|
|
2338
|
+
//#region src/sp00ky.d.ts
|
|
2339
|
+
/** Coerce whatever the `.get()` RPC hands back into a Blob. */
|
|
2340
|
+
declare function bucketContentToBlob(content: unknown): Blob | null;
|
|
2341
|
+
interface BucketPutOptions {
|
|
2342
|
+
/** Override the client-level {@link Sp00kyConfig.blurhash} setting for this put. */
|
|
2343
|
+
blurhash?: BlurhashSetting;
|
|
2344
|
+
}
|
|
2345
|
+
interface BucketPutResult {
|
|
2346
|
+
/** The computed blurhash when the content was a hashable image; else null. */
|
|
2347
|
+
blurhash: string | null;
|
|
2348
|
+
}
|
|
2349
|
+
interface BucketHandleSettings {
|
|
2350
|
+
blurhash?: BlurhashSetting;
|
|
2351
|
+
logger?: {
|
|
2352
|
+
warn: (obj: unknown, msg?: string) => void;
|
|
2353
|
+
};
|
|
2354
|
+
}
|
|
198
2355
|
declare class BucketHandle {
|
|
199
2356
|
private bucketName;
|
|
200
2357
|
private remote;
|
|
201
|
-
|
|
202
|
-
|
|
2358
|
+
/** Absent on the raw handle the cache itself reads through. */
|
|
2359
|
+
private blobs?;
|
|
2360
|
+
private settings?;
|
|
2361
|
+
constructor(bucketName: string, remote: RemoteDatabaseService, /** Absent on the raw handle the cache itself reads through. */
|
|
2362
|
+
blobs?: (BlobCache | null) | undefined, settings?: BucketHandleSettings | undefined);
|
|
2363
|
+
/** Effective blurhash setting: per-call option > client config > default ON. */
|
|
2364
|
+
private resolveBlurhash;
|
|
2365
|
+
put(path: string, content: string | Uint8Array | Blob, options?: BucketPutOptions): Promise<BucketPutResult>;
|
|
2366
|
+
/**
|
|
2367
|
+
* The blurhash stored alongside an uploaded image (see
|
|
2368
|
+
* {@link blurhashSidecarPath}), or null when there is none. Reads through the
|
|
2369
|
+
* blob cache, so a warm client answers from OPFS without a network hop, and
|
|
2370
|
+
* misses are remembered per tab so a hashless image costs at most one
|
|
2371
|
+
* serialized remote read per session.
|
|
2372
|
+
*/
|
|
2373
|
+
blurhash(path: string): Promise<string | null>;
|
|
203
2374
|
get(path: string): Promise<unknown>;
|
|
2375
|
+
/**
|
|
2376
|
+
* Read through the local blob cache: OPFS first, the bucket second. Unlike
|
|
2377
|
+
* {@link get} this survives a reload and works offline. Returns null when the
|
|
2378
|
+
* file exists in neither place.
|
|
2379
|
+
*/
|
|
2380
|
+
read(path: string, options?: BlobReadOptions): Promise<Blob | null>;
|
|
2381
|
+
/**
|
|
2382
|
+
* A refcounted object URL for `path`, suitable for `<img src>`. The caller
|
|
2383
|
+
* MUST call `release()` when the URL goes off screen. Returns null when the
|
|
2384
|
+
* file does not exist, or when object URLs are unavailable (non-browser).
|
|
2385
|
+
*/
|
|
2386
|
+
url(path: string, options?: BlobReadOptions): Promise<BlobUrlLease | null>;
|
|
2387
|
+
/** Exempt `path` from pressure eviction. Pinned bytes never expire. */
|
|
2388
|
+
pin(path: string): void;
|
|
2389
|
+
unpin(path: string): void;
|
|
2390
|
+
/** Drop `path` from the local cache without touching the remote file. */
|
|
2391
|
+
evict(path: string): Promise<void>;
|
|
2392
|
+
/** Warm the cache for offline use. Already-cached paths are skipped. */
|
|
2393
|
+
prefetch(paths: string[]): Promise<void>;
|
|
204
2394
|
delete(path: string): Promise<void>;
|
|
205
2395
|
exists(path: string): Promise<boolean>;
|
|
206
2396
|
head(path: string): Promise<Record<string, unknown>>;
|
|
@@ -208,55 +2398,321 @@ declare class BucketHandle {
|
|
|
208
2398
|
rename(sourcePath: string, targetPath: string): Promise<void>;
|
|
209
2399
|
list(prefix?: string): Promise<string[]>;
|
|
210
2400
|
}
|
|
211
|
-
declare class
|
|
2401
|
+
declare class Sp00kyClient<S extends SchemaStructure> {
|
|
212
2402
|
private config;
|
|
213
2403
|
private local;
|
|
214
2404
|
private remote;
|
|
2405
|
+
private blobs;
|
|
2406
|
+
private connectionSupervisor;
|
|
215
2407
|
private persistenceClient;
|
|
216
2408
|
private migrator;
|
|
217
2409
|
private cache;
|
|
218
2410
|
private dataModule;
|
|
219
2411
|
private sync;
|
|
220
2412
|
private devTools;
|
|
2413
|
+
private crdtManager;
|
|
2414
|
+
/**
|
|
2415
|
+
* True once the LOCAL half of boot is done and the client can serve reads
|
|
2416
|
+
* from the local store. Distinct from being connected: `syncHealth` covers
|
|
2417
|
+
* reaching the server and `storageHealth` covers whether the local store is
|
|
2418
|
+
* durable, but neither says "usable". Consumers gate their first paint on
|
|
2419
|
+
* this, which is what makes a warm boot instant and an offline boot possible.
|
|
2420
|
+
*/
|
|
2421
|
+
private localReady;
|
|
2422
|
+
private saltUserId;
|
|
2423
|
+
private featureFlags;
|
|
2424
|
+
private appReleases;
|
|
2425
|
+
private preloadedHashes;
|
|
2426
|
+
private pendingQueryInits;
|
|
221
2427
|
private logger;
|
|
222
2428
|
auth: AuthService<S>;
|
|
223
2429
|
streamProcessor: StreamProcessorService;
|
|
224
|
-
|
|
225
|
-
|
|
2430
|
+
private tabsCoordinator;
|
|
2431
|
+
private sharedActive;
|
|
2432
|
+
/** Current shared-tabs role, or null when the feature is off/fell back. */
|
|
2433
|
+
get tabRole(): TabRole | null;
|
|
2434
|
+
get remoteClient(): surrealdb0.Surreal;
|
|
2435
|
+
get localClient(): unknown;
|
|
226
2436
|
get pendingMutationCount(): number;
|
|
2437
|
+
/** Number of times the initial list_ref LIVE subscription retried on
|
|
2438
|
+
* the most recent `setCurrentUserId` call. 0 when the SSP's
|
|
2439
|
+
* pre-emptive user-table creation got there first; >0 when LIVE
|
|
2440
|
+
* registration hit a "table not found" race. Exposed so the e2e
|
|
2441
|
+
* suite can guard the pre-emptive path against regression. */
|
|
2442
|
+
get liveRetryCount(): number;
|
|
227
2443
|
subscribeToPendingMutations(cb: (count: number) => void): () => void;
|
|
228
|
-
|
|
2444
|
+
/** Current sync-health snapshot. See {@link Sp00kyConfig.syncHealth}. */
|
|
2445
|
+
get syncHealth(): SyncHealth;
|
|
2446
|
+
/**
|
|
2447
|
+
* Observe sync health. Fires immediately with the current status and again
|
|
2448
|
+
* on every healthy↔degraded transition. Returns an unsubscribe.
|
|
2449
|
+
*/
|
|
2450
|
+
subscribeToSyncHealth(cb: (health: SyncHealth) => void): () => void;
|
|
2451
|
+
/** Durability of the local cache. See {@link StorageHealth}. `'unknown'` for
|
|
2452
|
+
* engines that don't report it. */
|
|
2453
|
+
get storageHealth(): StorageHealth;
|
|
2454
|
+
/**
|
|
2455
|
+
* Observe local-store durability. Fires immediately with the current snapshot
|
|
2456
|
+
* and again on every change (at most once per bucket open in practice).
|
|
2457
|
+
* Returns an unsubscribe.
|
|
2458
|
+
*/
|
|
2459
|
+
subscribeToStorageHealth(cb: (health: StorageHealth) => void): () => void;
|
|
2460
|
+
constructor(config: Sp00kyConfig<S>);
|
|
2461
|
+
/** The shared-tabs role machinery, wired to this client's modules. */
|
|
2462
|
+
private buildTabsCoordinator;
|
|
229
2463
|
/**
|
|
230
2464
|
* Setup direct callbacks instead of event subscriptions
|
|
231
2465
|
*/
|
|
232
2466
|
private setupCallbacks;
|
|
233
2467
|
init(): Promise<void>;
|
|
2468
|
+
/**
|
|
2469
|
+
* The network half of boot: connect, verify the restored session, and let the
|
|
2470
|
+
* sync engine catch up. Runs in the background after `init()` has already
|
|
2471
|
+
* resolved, so nothing here is on the paint path.
|
|
2472
|
+
*
|
|
2473
|
+
* Every step is best-effort. A failure leaves the client in exactly the state
|
|
2474
|
+
* a warm offline boot is in - local reads working, writes queued in the
|
|
2475
|
+
* outbox - and the connection supervisor keeps retrying underneath.
|
|
2476
|
+
*/
|
|
2477
|
+
private initRemote;
|
|
2478
|
+
private bucketSwitchChain;
|
|
2479
|
+
private pendingBucketTarget;
|
|
2480
|
+
/**
|
|
2481
|
+
* Ensure the local store is this user's bucket, switching if needed. Called
|
|
2482
|
+
* from the auth listener on every auth flip; concurrent calls are chained
|
|
2483
|
+
* and superseded intermediates are skipped (latest target wins).
|
|
2484
|
+
*/
|
|
2485
|
+
private ensureLocalBucket;
|
|
2486
|
+
/**
|
|
2487
|
+
* The bucket-switch choreography: drain → swap → rebind.
|
|
2488
|
+
*
|
|
2489
|
+
* Drain: sync quiesced (poll/LIVE stopped, in-flight round awaited so its
|
|
2490
|
+
* outbox delete lands in the OLD bucket, debounce timers cancelled),
|
|
2491
|
+
* DataModule timers cleared, CRDT fields closed WITHOUT their final flush
|
|
2492
|
+
* (the remote session already belongs to the next user).
|
|
2493
|
+
*
|
|
2494
|
+
* Swap: gate closes so any local query issued mid-switch (sibling auth
|
|
2495
|
+
* subscribers, FeatureFlagModule) waits and then runs against the NEW
|
|
2496
|
+
* bucket; store swaps open-new-before-close-old; schema provisions
|
|
2497
|
+
* (no-op for a returning bucket); stale `_00_query` rows are wiped (dead
|
|
2498
|
+
* sessionId-salted hashes with stale arrays — record bodies stay warm);
|
|
2499
|
+
* SSP resets to a fresh circuit with re-seeded permissions.
|
|
2500
|
+
*
|
|
2501
|
+
* Rebind: auth token re-persisted (the surrealdb persistence client wrote it
|
|
2502
|
+
* into the OLD bucket's `_00_kv` before this listener ran), active queries
|
|
2503
|
+
* re-homed keeping their hashes, sync resumed on the new bucket's own
|
|
2504
|
+
* outbox, and every query re-registered remotely to refill from the server.
|
|
2505
|
+
*/
|
|
2506
|
+
private doSwitchBucket;
|
|
234
2507
|
close(): Promise<void>;
|
|
2508
|
+
/**
|
|
2509
|
+
* Subscribe to a feature flag for the current user. Returns a
|
|
2510
|
+
* `FeatureFlagHandle` whose `variant()`, `payload()` and `enabled()`
|
|
2511
|
+
* accessors reflect the latest assignment from `_00_user_feature`,
|
|
2512
|
+
* and whose `subscribe(cb)` fires whenever that assignment changes.
|
|
2513
|
+
*
|
|
2514
|
+
* Permissions are enforced by SurrealDB: a client can only ever see
|
|
2515
|
+
* its own row, and cannot create or modify assignments.
|
|
2516
|
+
*/
|
|
2517
|
+
feature(key: string, options?: FeatureFlagOptions): FeatureFlagHandle;
|
|
2518
|
+
/**
|
|
2519
|
+
* Force a feature flag to `variant` in THIS browser only; `null` clears it.
|
|
2520
|
+
*
|
|
2521
|
+
* Nothing is sent to the server — the `_00_user_feature` assignment is
|
|
2522
|
+
* untouched, so clearing restores whatever the server says. Persisted to
|
|
2523
|
+
* localStorage, survives reloads, and applies while signed out. Backs the
|
|
2524
|
+
* DevTools Access tab, and is a convenient hook for tests.
|
|
2525
|
+
*
|
|
2526
|
+
* To change a flag for OTHER users you need admin rights (`spky admin add`)
|
|
2527
|
+
* and the DevTools Access tab, or `spky flag`.
|
|
2528
|
+
*/
|
|
2529
|
+
setFeatureOverride(key: string, variant: string | null, payload?: unknown): void;
|
|
2530
|
+
/** Drop every local feature flag override set via `setFeatureOverride`. */
|
|
2531
|
+
clearFeatureOverrides(): void;
|
|
2532
|
+
/** The local feature flag overrides currently in effect, keyed by flag. */
|
|
2533
|
+
getFeatureOverrides(): Record<string, FeatureFlagOverride>;
|
|
2534
|
+
/**
|
|
2535
|
+
* Observe the announced release of an app (`_00_app_release:<app>`, written
|
|
2536
|
+
* by `spky deploy` / `spky release`). The handle's `snapshot()` carries the
|
|
2537
|
+
* announced version plus the cache-bust/mandatory flags, and
|
|
2538
|
+
* `updateAvailable(currentVersion)` compares it semver-wise against the
|
|
2539
|
+
* running build. World-readable; writes are root-only.
|
|
2540
|
+
*/
|
|
2541
|
+
appRelease(app: string, options?: AppReleaseOptions): AppReleaseHandle;
|
|
235
2542
|
authenticate(token: string): Promise<surrealdb0.Tokens>;
|
|
2543
|
+
/**
|
|
2544
|
+
* Open a CRDT field for collaborative editing.
|
|
2545
|
+
* Returns a CrdtField with a LoroDoc that can be bound to any editor.
|
|
2546
|
+
* Also starts a LIVE SELECT on the parent table for real-time sync;
|
|
2547
|
+
* incoming events trigger a subquery fetch of `_00_crdt` / `_00_cursor`.
|
|
2548
|
+
*/
|
|
2549
|
+
openCrdtField(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
|
|
2550
|
+
/**
|
|
2551
|
+
* Close a CRDT field when editing is done.
|
|
2552
|
+
*/
|
|
2553
|
+
closeCrdtField(table: string, recordId: string, field: string): void;
|
|
236
2554
|
deauthenticate(): Promise<void>;
|
|
237
|
-
query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table,
|
|
2555
|
+
query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, Sp00kyQueryResultPromise>;
|
|
238
2556
|
private initQuery;
|
|
2557
|
+
/**
|
|
2558
|
+
* Background tail of {@link initQuery}: instant-hydrate (opt-in via
|
|
2559
|
+
* `config.instantHydrate`, and only when the query is cold) followed by
|
|
2560
|
+
* enqueuing the `register` down-event. Never rejects — both halves catch and
|
|
2561
|
+
* log, so `void`-ing the returned promise can't produce an unhandled
|
|
2562
|
+
* rejection. By default (hydrate off) the register lifecycle is the single
|
|
2563
|
+
* freshness path; the one-shot fetch is an optimization apps enable
|
|
2564
|
+
* explicitly, and it runs regardless of preload state — cache-first delivery
|
|
2565
|
+
* never depends on WHY rows are cached.
|
|
2566
|
+
*/
|
|
2567
|
+
private finishQueryInit;
|
|
2568
|
+
/**
|
|
2569
|
+
* Smart, awaitable preload/prewarm into the LOCAL cache — without registering a
|
|
2570
|
+
* live view (NO `_00_query`, NO subscription, NO TTL heartbeat).
|
|
2571
|
+
*
|
|
2572
|
+
* Cache-aware via a durable per-bucket freshness marker (`_00_preload`):
|
|
2573
|
+
* - COLD (never preloaded in this bucket): fetch the query one-shot from the
|
|
2574
|
+
* remote, persist the rows (+ embedded `.related()` children), stamp the
|
|
2575
|
+
* marker — and AWAIT it. This is the "smart waiting" first load: callers can
|
|
2576
|
+
* `await db.preload(...)` to hold the UI until the data is ready.
|
|
2577
|
+
* - WARM (marker present): return instantly — NEVER blocks. `refresh` decides
|
|
2578
|
+
* whether to also kick a one-time silent refetch (see {@link PreloadOptions}).
|
|
2579
|
+
* Default `onUse` does nothing; the data freshens when the real `useQuery`
|
|
2580
|
+
* mounts and registers its live view.
|
|
2581
|
+
*
|
|
2582
|
+
* Best-effort: any fetch failure (offline, etc.) is a no-op warn (no marker
|
|
2583
|
+
* written, so it's retried next load). Deduped per session by query hash.
|
|
2584
|
+
*/
|
|
2585
|
+
preload(finalQuery: FinalQuery<S, any, any, any, any, any>, options?: PreloadOptions): Promise<void>;
|
|
2586
|
+
/**
|
|
2587
|
+
* One-shot remote fetch + local persist for a preload query. Returns the row
|
|
2588
|
+
* count on success, or -1 on failure (best-effort: logged, never thrown) so
|
|
2589
|
+
* the caller skips stamping the freshness marker and retries next load.
|
|
2590
|
+
*/
|
|
2591
|
+
private fetchAndPersist;
|
|
239
2592
|
queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<string>;
|
|
240
2593
|
subscribe(queryHash: string, callback: (records: Record<string, any>[]) => void, options?: {
|
|
241
2594
|
immediate?: boolean;
|
|
242
2595
|
}): Promise<() => void>;
|
|
2596
|
+
/**
|
|
2597
|
+
* Opt-in eager teardown for a query whose last subscriber has gone away
|
|
2598
|
+
* (e.g. a viewport-windowed list cancelling an off-screen window). No-op
|
|
2599
|
+
* while any subscriber remains. Tears down the remote `_00_query` view +
|
|
2600
|
+
* local WASM view instead of waiting for the TTL sweep. Default behavior
|
|
2601
|
+
* (no call here) keeps the view resident for cheap re-subscription.
|
|
2602
|
+
*/
|
|
2603
|
+
deregisterQuery(queryHash: string): void;
|
|
2604
|
+
/**
|
|
2605
|
+
* Subscribe to a query's fetch-status changes (idle/fetching). With
|
|
2606
|
+
* `{ immediate: true }` the callback fires synchronously with the current
|
|
2607
|
+
* status. Powers the `useQuery` hook's `isFetching()` accessor.
|
|
2608
|
+
*/
|
|
2609
|
+
subscribeQueryStatus(queryHash: string, callback: QueryStatusCallback, options?: {
|
|
2610
|
+
immediate?: boolean;
|
|
2611
|
+
}): () => void;
|
|
2612
|
+
/**
|
|
2613
|
+
* Report the frontend processing time (ms) a client framework spent applying
|
|
2614
|
+
* an update for a query (e.g. `useQuery`'s `reconcile()`), so DevTools/MCP can
|
|
2615
|
+
* surface the "frontend" phase of the per-query timing breakdown.
|
|
2616
|
+
*/
|
|
2617
|
+
reportFrontendTiming(queryHash: string, ms: number): void;
|
|
243
2618
|
run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, payload: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
|
|
244
2619
|
bucket<B extends BucketNames<S>>(name: B): BucketHandle;
|
|
2620
|
+
/** Cache-free handle. The blob cache reads the remote through this, so a
|
|
2621
|
+
* cache miss can't loop back into the cache. */
|
|
2622
|
+
private rawBucket;
|
|
2623
|
+
/** Blob cache counters for DevTools. */
|
|
2624
|
+
getBlobCacheStats(): BlobCacheStats;
|
|
245
2625
|
create(id: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
246
2626
|
update(table: string, id: string, data: Record<string, unknown>, options?: UpdateOptions): Promise<{
|
|
247
2627
|
[x: string]: /*elided*/any;
|
|
248
2628
|
}>;
|
|
249
2629
|
delete(table: string, id: string): Promise<void>;
|
|
2630
|
+
/**
|
|
2631
|
+
* Whether the local store is initialized and reads can be served. See the
|
|
2632
|
+
* `localReady` field: this is deliberately independent of connectivity.
|
|
2633
|
+
*/
|
|
2634
|
+
isLocalReady(): boolean;
|
|
250
2635
|
useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T>;
|
|
251
|
-
|
|
252
|
-
|
|
2636
|
+
/**
|
|
2637
|
+
* Mint the salt used for query-id hashing, so two sessions registering the
|
|
2638
|
+
* same logical query get distinct `_00_query` rows.
|
|
2639
|
+
*
|
|
2640
|
+
* Generated LOCALLY, deliberately. This used to be `RETURN <string>session::id()`,
|
|
2641
|
+
* which cost a serial round trip on the critical boot path and resolved to
|
|
2642
|
+
* `''` offline. The value never needed to come from the server: the server
|
|
2643
|
+
* derives its own `clientId` inside `fn::query::register` and *ignores*
|
|
2644
|
+
* whatever the caller passed, and the permission rules that matter gate on
|
|
2645
|
+
* `auth_id = $auth.id` rather than the session (`_00_list_ref`). Session
|
|
2646
|
+
* scoping via `clientId = session::id()` was in fact removed upstream because
|
|
2647
|
+
* it broke a user with two tabs open. All this value has to be is unique per
|
|
2648
|
+
* browser session, which `randomUUID` gives us for free and offline.
|
|
2649
|
+
*/
|
|
2650
|
+
/**
|
|
2651
|
+
* The current principal as the `"table:id"` string the in-browser SSP wants
|
|
2652
|
+
* for `$auth.id`, or null when signed out.
|
|
2653
|
+
*
|
|
2654
|
+
* Tolerates BOTH shapes `currentUser.id` can take, which is the point:
|
|
2655
|
+
* a session restored from the cached token carries a plain string (the JWT's
|
|
2656
|
+
* `ID` claim), while one verified by the server carries a RecordId. Passing
|
|
2657
|
+
* the former to `encodeRecordId` reads `.table` off a string and throws
|
|
2658
|
+
* during boot.
|
|
2659
|
+
*/
|
|
2660
|
+
/**
|
|
2661
|
+
* Prime the in-browser circuit from the local store. Builds the context the
|
|
2662
|
+
* stream processor needs: every synced table (the app schema plus the
|
|
2663
|
+
* server-written meta tables that sync down), a schema hash so a snapshot
|
|
2664
|
+
* projected under another schema is not trusted, and the ids whose local
|
|
2665
|
+
* `_00_rv` was bumped by an unsettled mutation.
|
|
2666
|
+
*/
|
|
2667
|
+
private primeCircuit;
|
|
2668
|
+
private sessionAuthId;
|
|
2669
|
+
private mintSessionSalt;
|
|
253
2670
|
}
|
|
254
2671
|
//#endregion
|
|
2672
|
+
//#region src/utils/semver.d.ts
|
|
2673
|
+
/** True when `a` is a valid version strictly greater than valid version `b`. */
|
|
2674
|
+
declare function semverGt(a: unknown, b: unknown): boolean;
|
|
2675
|
+
//#endregion
|
|
2676
|
+
//#region src/services/database/errors.d.ts
|
|
2677
|
+
/**
|
|
2678
|
+
* A local-store operation that did not answer within its deadline.
|
|
2679
|
+
*
|
|
2680
|
+
* The local write path (`db.create` / `db.update` / `db.delete`, every local
|
|
2681
|
+
* query behind them) used to have no deadline anywhere: the SQLite worker
|
|
2682
|
+
* transport parks a call until the worker replies, the surrealdb engine's
|
|
2683
|
+
* query chain waits on the previous link, and `withRetry` retries without a
|
|
2684
|
+
* clock. One op that never settled (a worker starved behind a long select, a
|
|
2685
|
+
* lock verification awaiting `navigator.locks.query()` forever) left the
|
|
2686
|
+
* caller's promise pending for the tab's lifetime - a chat composer that never
|
|
2687
|
+
* re-enabled, a call that never got past "Connecting".
|
|
2688
|
+
*
|
|
2689
|
+
* The message says "timed out" on purpose: `classifySyncError` keys off it and
|
|
2690
|
+
* treats the failure as transient (re-queue), never as an application error
|
|
2691
|
+
* that rolls the mutation back. `retryable: false` keeps `withRetry` from
|
|
2692
|
+
* spinning on it: the op is still running in the engine, retrying queues a
|
|
2693
|
+
* second copy behind it.
|
|
2694
|
+
*/
|
|
2695
|
+
declare class LocalOpTimeoutError extends Error {
|
|
2696
|
+
readonly name = "LocalOpTimeoutError";
|
|
2697
|
+
readonly retryable = false;
|
|
2698
|
+
readonly op: string;
|
|
2699
|
+
readonly timeoutMs: number;
|
|
2700
|
+
constructor(op: string, timeoutMs: number);
|
|
2701
|
+
}
|
|
2702
|
+
/** Default deadline for one local-store operation. Generous: a cold 4k-row
|
|
2703
|
+
* select on a throttled tab is seconds, not tens of seconds. */
|
|
2704
|
+
declare const DEFAULT_LOCAL_OP_TIMEOUT_MS = 30000;
|
|
2705
|
+
//#endregion
|
|
255
2706
|
//#region src/utils/index.d.ts
|
|
256
2707
|
declare function fileToUint8Array(file: File | Blob): Promise<Uint8Array>;
|
|
2708
|
+
/**
|
|
2709
|
+
* Convert plain text to simple HTML paragraphs.
|
|
2710
|
+
* Useful for seeding a rich-text editor (e.g. TipTap/ProseMirror) with fallback content.
|
|
2711
|
+
*/
|
|
2712
|
+
declare function textToHtml(text: string): string;
|
|
257
2713
|
/**
|
|
258
2714
|
* Helper for retrying DB operations with exponential backoff
|
|
259
2715
|
*/
|
|
260
2716
|
|
|
261
2717
|
//#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,
|
|
2718
|
+
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, FeatureFlagHandle, FeatureFlagModule, type FeatureFlagOptions, type FeatureFlagOverride, type FeatureFlagSnapshot, Level, LocalOpTimeoutError, MATERIALIZATION_SAMPLE_WINDOW, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, PreloadOptions, PreloadRefresh, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, ReconnectConfig, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, Sp00kyClient, 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 };
|