@spooky-sync/core 0.0.1-canary.7 → 0.0.1-canary.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/AGENTS.md +56 -0
  2. package/dist/index.d.ts +299 -369
  3. package/dist/index.js +2278 -399
  4. package/dist/otel/index.d.ts +21 -0
  5. package/dist/otel/index.js +86 -0
  6. package/dist/types.d.ts +460 -0
  7. package/package.json +37 -7
  8. package/skills/sp00ky-core/SKILL.md +258 -0
  9. package/skills/sp00ky-core/references/auth.md +98 -0
  10. package/skills/sp00ky-core/references/config.md +76 -0
  11. package/src/build-globals.d.ts +12 -0
  12. package/src/events/events.test.ts +2 -1
  13. package/src/events/index.ts +3 -0
  14. package/src/index.ts +3 -2
  15. package/src/modules/auth/events/index.ts +2 -1
  16. package/src/modules/auth/index.ts +17 -20
  17. package/src/modules/cache/index.ts +41 -29
  18. package/src/modules/cache/types.ts +2 -2
  19. package/src/modules/crdt/crdt-field.ts +281 -0
  20. package/src/modules/crdt/crdt-hydration.test.ts +206 -0
  21. package/src/modules/crdt/index.ts +352 -0
  22. package/src/modules/data/data.status.test.ts +108 -0
  23. package/src/modules/data/index.ts +662 -108
  24. package/src/modules/data/window-query.test.ts +52 -0
  25. package/src/modules/data/window-query.ts +130 -0
  26. package/src/modules/devtools/index.ts +89 -21
  27. package/src/modules/devtools/versions.test.ts +74 -0
  28. package/src/modules/devtools/versions.ts +81 -0
  29. package/src/modules/ref-tables.test.ts +56 -0
  30. package/src/modules/ref-tables.ts +57 -0
  31. package/src/modules/sync/engine.ts +97 -37
  32. package/src/modules/sync/events/index.ts +3 -2
  33. package/src/modules/sync/queue/queue-down.ts +5 -4
  34. package/src/modules/sync/queue/queue-up.ts +14 -13
  35. package/src/modules/sync/scheduler.ts +2 -2
  36. package/src/modules/sync/sync.ts +553 -58
  37. package/src/modules/sync/utils.test.ts +239 -2
  38. package/src/modules/sync/utils.ts +166 -17
  39. package/src/otel/index.ts +127 -0
  40. package/src/services/database/database.ts +11 -11
  41. package/src/services/database/events/index.ts +2 -1
  42. package/src/services/database/local-migrator.ts +21 -21
  43. package/src/services/database/local.test.ts +33 -0
  44. package/src/services/database/local.ts +192 -32
  45. package/src/services/database/remote.ts +13 -13
  46. package/src/services/logger/index.ts +6 -101
  47. package/src/services/persistence/localstorage.ts +2 -2
  48. package/src/services/persistence/resilient.ts +41 -0
  49. package/src/services/persistence/surrealdb.ts +9 -9
  50. package/src/services/stream-processor/index.ts +205 -36
  51. package/src/services/stream-processor/permissions.test.ts +47 -0
  52. package/src/services/stream-processor/permissions.ts +53 -0
  53. package/src/services/stream-processor/stream-processor.batch.test.ts +136 -0
  54. package/src/services/stream-processor/stream-processor.test.ts +1 -1
  55. package/src/services/stream-processor/wasm-types.ts +18 -2
  56. package/src/sp00ky.auth-order.test.ts +65 -0
  57. package/src/sp00ky.ts +582 -0
  58. package/src/types.ts +132 -13
  59. package/src/utils/index.ts +35 -13
  60. package/src/utils/parser.ts +3 -2
  61. package/src/utils/surql.ts +24 -15
  62. package/src/utils/withRetry.test.ts +1 -1
  63. package/tsdown.config.ts +55 -1
  64. package/src/spooky.ts +0 -392
package/dist/index.d.ts CHANGED
@@ -1,132 +1,10 @@
1
+ import { A as Logger$1, C as RunOptions, D as StoreType, E as Sp00kyQueryResultPromise, M as EventSystem, O as TimingPhase, S as RegistrationTimings, T as Sp00kyQueryResult, _ as QueryTimeToLive, a as MutationCallback, b as RecordVersionArray, c as PersistenceClient, d as QueryConfig, f as QueryConfigRecord, g as QueryStatusCallback, h as QueryStatus, i as MATERIALIZATION_SAMPLE_WINDOW, j as EventDefinition, k as UpdateOptions, l as PhaseStat, m as QueryState, n as EventSubscriptionOptions, o as MutationEvent, p as QueryHash, r as Level, s as MutationEventType, t as DebounceOptions, u as PinoTransmit, v as QueryTimings, w as Sp00kyConfig, x as RecordVersionDiff, y as QueryUpdateCallback } from "./types.js";
1
2
  import * as surrealdb0 from "surrealdb";
2
- import { Duration, RecordId, Surreal, SurrealTransaction } from "surrealdb";
3
- import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RecordId as RecordId$1, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
4
- import { Level, Level as Level$1, Logger } from "pino";
3
+ import { Duration, RecordId, Surreal as Surreal$1, SurrealTransaction } from "surrealdb";
4
+ import { AccessDefinition, BackendNames, BackendRoutes, BucketNames, ColumnSchema, GetTable, QueryBuilder, QueryOptions, RoutePayload, SchemaStructure, TableModel, TableNames, TypeNameToTypeMap } from "@spooky-sync/query-builder";
5
+ import { Logger } from "pino";
6
+ import { LoroDoc } from "loro-crdt";
5
7
 
6
- //#region src/events/index.d.ts
7
- /**
8
- * Utility type to define the payload structure of an event.
9
- * If the payload type P is never, it defines payload as undefined.
10
- */
11
- type EventPayloadDefinition<P> = [P] extends [never] ? {
12
- payload: undefined;
13
- } : {
14
- payload: P;
15
- };
16
- /**
17
- * Defines the structure of an event with a specific type and payload.
18
- * @template T The string literal type of the event.
19
- * @template P The type of the event payload.
20
- */
21
- type EventDefinition<T extends string, P> = {
22
- type: T;
23
- } & EventPayloadDefinition<P>;
24
- /**
25
- * A map of event types to their definitions.
26
- * Keys are event names, values are EventDefinitions.
27
- */
28
- type EventTypeMap = Record<string, EventDefinition<any, unknown> | EventDefinition<any, never>>;
29
- /**
30
- * Options for pushing/emitting events.
31
- */
32
- interface PushEventOptions {
33
- /** Configuration for debouncing the event. */
34
- debounced?: {
35
- key: string;
36
- delay: number;
37
- };
38
- }
39
- /**
40
- * Extracts the full Event object type from the map for a given key.
41
- */
42
- type Event<E extends EventTypeMap, T extends EventType<E>> = E[T];
43
- /**
44
- * Extracts the payload type from the map for a given key.
45
- */
46
- type EventPayload<E extends EventTypeMap, T extends EventType<E>> = E[T]['payload'];
47
- /**
48
- * Array of available event type keys.
49
- */
50
- type EventTypes<E extends EventTypeMap> = (keyof E)[];
51
- /**
52
- * Represents a valid key (event name) from the EventTypeMap.
53
- */
54
- type EventType<E extends EventTypeMap> = keyof E;
55
- /**
56
- * Function signature for an event handler.
57
- */
58
- type EventHandler<E extends EventTypeMap, T extends EventType<E>> = (event: Event<E, T>) => void;
59
- /**
60
- * Options when subscribing to an event.
61
- */
62
- type EventSubscriptionOptions$1 = {
63
- /** If true, the handler will be called immediately with the last emitted event of this type (if any). */
64
- immediately?: boolean;
65
- /** If true, the subscription will be automatically removed after the first event is handled. */
66
- once?: boolean;
67
- };
68
- /**
69
- * A type-safe event system that handles subscription, emission (including debouncing), and buffering of events.
70
- * @template E The EventTypeMap defining all supported events.
71
- */
72
- declare class EventSystem<E extends EventTypeMap> {
73
- private _eventTypes;
74
- private subscriberId;
75
- private isProcessing;
76
- private buffer;
77
- private subscribers;
78
- private subscribersTypeMap;
79
- private lastEvents;
80
- private debouncedEvents;
81
- constructor(_eventTypes: EventTypes<E>);
82
- get eventTypes(): EventTypes<E>;
83
- /**
84
- * Subscribes a handler to a specific event type.
85
- * @param type The event type to subscribe to.
86
- * @param handler The function to call when the event occurs.
87
- * @param options Subscription options (once, immediately).
88
- * @returns A subscription ID that can be used to unsubscribe.
89
- */
90
- subscribe<T extends EventType<E>>(type: T, handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number;
91
- /**
92
- * Subscribes a handler to multiple event types.
93
- * @param types An array of event types to subscribe to.
94
- * @param handler The function to call when any of the events occur.
95
- * @param options Subscription options.
96
- * @returns An array of subscription IDs.
97
- */
98
- subscribeMany<T extends EventType<E>>(types: T[], handler: EventHandler<E, T>, options?: EventSubscriptionOptions$1): number[];
99
- /**
100
- * Unsubscribes a specific subscription by ID.
101
- * @param id The subscription ID returned by subscribe().
102
- * @returns True if the subscription was found and removed, false otherwise.
103
- */
104
- unsubscribe(id: number): boolean;
105
- /**
106
- * Emits an event with the given type and payload.
107
- * @param type The type of event to emit.
108
- * @param payload The data associated with the event.
109
- */
110
- emit<T extends EventType<E>, P extends EventPayload<E, T>>(type: T, payload: P): void;
111
- /**
112
- * Adds a fully constructed event object to the system.
113
- * Similar to emit, but takes the full event object directly.
114
- * Supports debouncing if options are provided.
115
- * @param event The event object.
116
- * @param options Options for the event push (e.g., debouncing).
117
- */
118
- addEvent<T extends EventType<E>>(event: Event<E, T>, options?: PushEventOptions): void;
119
- private handleDebouncedEvent;
120
- private scheduleProcessing;
121
- private processEvents;
122
- private dequeue;
123
- private setLastEvent;
124
- private broadcastEvent;
125
- }
126
- //#endregion
127
- //#region src/services/logger/index.d.ts
128
- type Logger$1 = Logger;
129
- //#endregion
130
8
  //#region src/services/database/events/index.d.ts
131
9
  declare const DatabaseEventTypes: {
132
10
  readonly LocalQuery: "DATABASE_LOCAL_QUERY";
@@ -154,13 +32,13 @@ interface SealedQuery<T = void> {
154
32
  //#endregion
155
33
  //#region src/services/database/database.d.ts
156
34
  declare abstract class AbstractDatabaseService {
157
- protected client: Surreal;
35
+ protected client: Surreal$1;
158
36
  protected logger: Logger$1;
159
37
  protected events: DatabaseEventSystem;
160
38
  protected abstract eventType: typeof DatabaseEventTypes.LocalQuery | typeof DatabaseEventTypes.RemoteQuery;
161
- constructor(client: Surreal, logger: Logger$1, events: DatabaseEventSystem);
39
+ constructor(client: Surreal$1, logger: Logger$1, events: DatabaseEventSystem);
162
40
  abstract connect(): Promise<void>;
163
- getClient(): Surreal;
41
+ getClient(): Surreal$1;
164
42
  getEvents(): DatabaseEventSystem;
165
43
  tx(): Promise<SurrealTransaction>;
166
44
  private queryQueue;
@@ -176,17 +54,33 @@ declare abstract class AbstractDatabaseService {
176
54
  declare class LocalDatabaseService extends AbstractDatabaseService {
177
55
  private config;
178
56
  protected eventType: "DATABASE_LOCAL_QUERY";
179
- constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
180
- getConfig(): SpookyConfig<any>['database'];
57
+ constructor(config: Sp00kyConfig<any>['database'], logger: Logger$1);
58
+ getConfig(): Sp00kyConfig<any>['database'];
181
59
  connect(): Promise<void>;
60
+ private unloadCloseRegistered;
61
+ /**
62
+ * Close the local DB on page unload so the SurrealDB-WASM worker releases its
63
+ * IndexedDB connection cleanly. Without this, the previous page's connection
64
+ * lingers; the next load's `client.connect` opens the store but the first
65
+ * write transaction in `client.use` hits an "IndexedDB error" — which then
66
+ * (mis)triggered the corrupt-store recovery and WIPED the cache on every
67
+ * reload, making warm loads as slow as cold ones. `pagehide` is the reliable
68
+ * unload signal (fires on bfcache + normal navigation); `close()` is async but
69
+ * the WASM worker initiates the IndexedDB connection teardown synchronously.
70
+ */
71
+ private registerUnloadClose;
72
+ private openStore;
182
73
  }
74
+ /** True for the SurrealDB-WASM error raised when its IndexedDB-backed key-value
75
+ * store can't be opened (corrupt / version-incompatible / blocked). Exported
76
+ * for unit testing the error-message match. */
183
77
  //#endregion
184
78
  //#region src/services/database/remote.d.ts
185
79
  declare class RemoteDatabaseService extends AbstractDatabaseService {
186
80
  private config;
187
81
  protected eventType: "DATABASE_REMOTE_QUERY";
188
- constructor(config: SpookyConfig<any>['database'], logger: Logger$1);
189
- getConfig(): SpookyConfig<any>['database'];
82
+ constructor(config: Sp00kyConfig<any>['database'], logger: Logger$1);
83
+ getConfig(): Sp00kyConfig<any>['database'];
190
84
  connect(): Promise<void>;
191
85
  signin(params: any): Promise<any>;
192
86
  signup(params: any): Promise<any>;
@@ -194,38 +88,17 @@ declare class RemoteDatabaseService extends AbstractDatabaseService {
194
88
  invalidate(): Promise<void>;
195
89
  }
196
90
  //#endregion
197
- //#region src/modules/sync/queue/queue-up.d.ts
198
- type CreateEvent = {
199
- type: 'create';
200
- mutation_id: RecordId;
201
- record_id: RecordId;
202
- data: Record<string, unknown>;
203
- record?: Record<string, unknown>;
204
- tableName?: string;
205
- options?: PushEventOptions;
206
- };
207
- type UpdateEvent = {
208
- type: 'update';
209
- mutation_id: RecordId;
210
- record_id: RecordId;
211
- data: Record<string, unknown>;
212
- record?: Record<string, unknown>;
213
- beforeRecord?: Record<string, unknown>;
214
- options?: PushEventOptions;
215
- };
216
- type DeleteEvent = {
217
- type: 'delete';
218
- mutation_id: RecordId;
219
- record_id: RecordId;
220
- options?: PushEventOptions;
221
- };
222
- type UpEvent = CreateEvent | UpdateEvent | DeleteEvent;
223
- //#endregion
224
91
  //#region src/services/stream-processor/wasm-types.d.ts
225
92
  interface WasmStreamUpdate {
226
93
  query_id: string;
227
94
  result_hash: string;
228
95
  result_data: RecordVersionArray;
96
+ timing_store_apply_ms?: number;
97
+ timing_circuit_step_ms?: number;
98
+ timing_transform_ms?: number;
99
+ timing_parse_ms?: number;
100
+ timing_plan_ms?: number;
101
+ timing_snapshot_ms?: number;
229
102
  }
230
103
  //#endregion
231
104
  //#region src/services/stream-processor/index.d.ts
@@ -246,6 +119,25 @@ interface StreamUpdate {
246
119
  queryHash: string;
247
120
  localArray: RecordVersionArray;
248
121
  op?: 'CREATE' | 'UPDATE' | 'DELETE';
122
+ /**
123
+ * End-to-end ingest latency for the WASM call that produced this update,
124
+ * in milliseconds. Populated by StreamProcessorService.ingest. Undefined
125
+ * for the initial register_view snapshot.
126
+ */
127
+ materializationTimeMs?: number;
128
+ /** SSP internal sub-phase timings (ms) for this ingest, from the WASM binding. */
129
+ storeApplyMs?: number;
130
+ circuitStepMs?: number;
131
+ transformMs?: number;
132
+ /**
133
+ * One-shot registration timings (ms). Only set on the StreamUpdate returned
134
+ * by `registerQueryPlan` (the register_view snapshot), not on ingest updates.
135
+ */
136
+ registration?: {
137
+ parseMs: number;
138
+ planMs: number;
139
+ snapshotMs: number;
140
+ };
249
141
  }
250
142
  type StreamProcessorEvents = {
251
143
  stream_update: EventDefinition<'stream_update', StreamUpdate[]>;
@@ -265,6 +157,8 @@ declare class StreamProcessorService {
265
157
  private processor;
266
158
  private isInitialized;
267
159
  private receivers;
160
+ private batching;
161
+ private batchBuffer;
268
162
  constructor(events: EventSystem<StreamProcessorEvents>, db: LocalDatabaseService, persistenceClient: PersistenceClient, logger: Logger);
269
163
  /**
270
164
  * Add a receiver for stream updates.
@@ -272,12 +166,53 @@ declare class StreamProcessorService {
272
166
  */
273
167
  addReceiver(receiver: StreamUpdateReceiver): void;
274
168
  private notifyUpdates;
169
+ private dispatchUpdates;
170
+ /**
171
+ * Ingest a batch of record changes as a single bulk operation, firing only
172
+ * one coalesced `StreamUpdate` per affected query once every record has been
173
+ * ingested (instead of one update per record). Use this whenever multiple
174
+ * records land at once — e.g. sync fetching N missing rows — so a list query
175
+ * re-runs and the UI re-renders once for the whole batch rather than
176
+ * row-by-row.
177
+ *
178
+ * Internally opens a coalescing window, ingests each record, then flushes;
179
+ * processor state is persisted once for the whole batch. No-op for an empty
180
+ * batch.
181
+ */
182
+ ingestMany(records: Array<{
183
+ table: string;
184
+ op: 'CREATE' | 'UPDATE' | 'DELETE';
185
+ id: string;
186
+ record: any;
187
+ }>): void;
188
+ /**
189
+ * Open a coalescing window. While open, the per-record stream updates
190
+ * emitted by `ingest` are buffered (one entry per queryHash) instead of
191
+ * dispatched. Always paired with `flushCoalescing()` in a try/finally by
192
+ * `ingestMany` so the window always closes — otherwise the processor stays
193
+ * stuck buffering forever.
194
+ *
195
+ * No-op if a window is already open (nested batches aren't expected here).
196
+ */
197
+ private beginCoalescing;
198
+ /**
199
+ * Close the coalescing window and flush: dispatch one coalesced
200
+ * `StreamUpdate` per buffered queryHash, then persist processor state once
201
+ * for the whole batch (instead of once per ingest).
202
+ */
203
+ private flushCoalescing;
275
204
  /**
276
205
  * Initialize the WASM module and processor.
277
206
  * This must be called before using other methods.
278
207
  */
279
208
  init(): Promise<void>;
280
209
  loadState(): Promise<void>;
210
+ /**
211
+ * Seed per-table `select` permission predicates ({ [table]: whereText }).
212
+ * Must run after the processor exists and before any `register_view`, else
213
+ * non-`_00_` tables are default-denied and registration fails.
214
+ */
215
+ setPermissions(permissions: Record<string, string>): void;
281
216
  saveState(): Promise<void>;
282
217
  /**
283
218
  * Ingest a record change into the processor.
@@ -297,205 +232,6 @@ declare class StreamProcessorService {
297
232
  private normalizeValue;
298
233
  }
299
234
  //#endregion
300
- //#region src/types.d.ts
301
- /**
302
- * The type of storage backend to use for the local database.
303
- * - 'memory': In-memory storage (transient).
304
- * - 'indexeddb': IndexedDB storage (persistent).
305
- */
306
- type StoreType = 'memory' | 'indexeddb';
307
- /**
308
- * Interface for a custom persistence client.
309
- * Allows providing a custom storage mechanism for the local database.
310
- */
311
- interface PersistenceClient {
312
- /**
313
- * Sets a value in the storage.
314
- * @param key The key to set.
315
- * @param value The value to store.
316
- */
317
- set<T>(key: string, value: T): Promise<void>;
318
- /**
319
- * Gets a value from the storage.
320
- * @param key The key to retrieve.
321
- * @returns The stored value or null if not found.
322
- */
323
- get<T>(key: string): Promise<T | null>;
324
- /**
325
- * Removes a value from the storage.
326
- * @param key The key to remove.
327
- */
328
- remove(key: string): Promise<void>;
329
- }
330
- /**
331
- * Supported Time-To-Live (TTL) values for cached queries.
332
- * Format: number + unit (m=minutes, h=hours, d=days).
333
- */
334
- type QueryTimeToLive = '1m' | '5m' | '10m' | '15m' | '20m' | '25m' | '30m' | '1h' | '2h' | '3h' | '4h' | '5h' | '6h' | '7h' | '8h' | '9h' | '10h' | '11h' | '12h' | '1d';
335
- /**
336
- * Result object returned when a query is registered or executed.
337
- */
338
- interface SpookyQueryResult {
339
- /** The unique hash identifier for the query. */
340
- hash: string;
341
- }
342
- type SpookyQueryResultPromise = Promise<SpookyQueryResult>;
343
- interface EventSubscriptionOptions {
344
- priority?: number;
345
- }
346
- /**
347
- * Configuration options for the Spooky client.
348
- * @template S The schema structure type.
349
- */
350
- interface SpookyConfig<S extends SchemaStructure> {
351
- /** Database connection configuration. */
352
- database: {
353
- /** The SurrealDB endpoint URL. */
354
- endpoint?: string;
355
- /** The namespace to use. */
356
- namespace: string;
357
- /** The database name. */
358
- database: string;
359
- /** The local store type implementation. */
360
- store?: StoreType;
361
- /** Authentication token. */
362
- token?: string;
363
- };
364
- /** Unique client identifier. If not provided, one will be generated. */
365
- clientId?: string;
366
- /** The schema definition. */
367
- schema: S;
368
- /** The compiled SURQL schema string. */
369
- schemaSurql: string;
370
- /** Logging level. */
371
- logLevel: Level$1;
372
- /**
373
- * Persistence client to use.
374
- * Can be a custom implementation, 'surrealdb' (default), or 'localstorage'.
375
- */
376
- persistenceClient?: PersistenceClient | 'surrealdb' | 'localstorage';
377
- /** OpenTelemetry collector endpoint for telemetry data. */
378
- otelEndpoint?: string;
379
- /**
380
- * Debounce time in milliseconds for stream updates.
381
- * Defaults to 100ms.
382
- */
383
- streamDebounceTime?: number;
384
- }
385
- type QueryHash = string;
386
- type RecordVersionArray = Array<[string, number]>;
387
- /**
388
- * Represents the difference between two record version sets.
389
- * Used for synchronizing local and remote states.
390
- */
391
- interface RecordVersionDiff {
392
- /** List of records added. */
393
- added: Array<{
394
- id: RecordId$1<string>;
395
- version: number;
396
- }>;
397
- /** List of records updated. */
398
- updated: Array<{
399
- id: RecordId$1<string>;
400
- version: number;
401
- }>;
402
- /** List of record IDs removed. */
403
- removed: RecordId$1<string>[];
404
- }
405
- /**
406
- * Configuration for a specific query instance.
407
- * Stores metadata about the query's state, parameters, and versioning.
408
- */
409
- interface QueryConfig {
410
- /** The unique ID of the query config record. */
411
- id: RecordId$1<string>;
412
- /** The SURQL query string. */
413
- surql: string;
414
- /** Parameters used in the query. */
415
- params: Record<string, any>;
416
- /** The version array representing the local state of results. */
417
- localArray: RecordVersionArray;
418
- /** The version array representing the remote (server) state of results. */
419
- remoteArray: RecordVersionArray;
420
- /** Time-To-Live for this query. */
421
- ttl: QueryTimeToLive;
422
- /** Timestamp when the query was last accessed/active. */
423
- lastActiveAt: Date;
424
- /** The name of the table this query targets (if applicable). */
425
- tableName: string;
426
- }
427
- type QueryConfigRecord = QueryConfig & {
428
- id: string;
429
- };
430
- /**
431
- * Internal state of a live query.
432
- */
433
- interface QueryState {
434
- /** The configuration for this query. */
435
- config: QueryConfig;
436
- /** The current cached records for this query. */
437
- records: Record<string, any>[];
438
- /** Timer for TTL expiration. */
439
- ttlTimer: NodeJS.Timeout | null;
440
- /** TTL duration in milliseconds. */
441
- ttlDurationMs: number;
442
- /** Number of times the query has been updated. */
443
- updateCount: number;
444
- }
445
- type QueryUpdateCallback = (records: Record<string, any>[]) => void;
446
- type MutationCallback = (mutations: UpEvent[]) => void;
447
- type MutationEventType = 'create' | 'update' | 'delete';
448
- /**
449
- * Represents a mutation event (create, update, delete) to be synchronized.
450
- */
451
- interface MutationEvent {
452
- /** Example: 'create', 'update', or 'delete'. */
453
- type: MutationEventType;
454
- /** unique id of the mutation */
455
- mutation_id: RecordId$1<string>;
456
- /** The ID of the record being mutated. */
457
- record_id: RecordId$1<string>;
458
- /** The data payload for create/update operations. */
459
- data?: any;
460
- /** The full record data (optional context). */
461
- record?: any;
462
- /** Options for the mutation event (e.g., debounce settings). */
463
- options?: PushEventOptions;
464
- /** Timestamp when the event was created. */
465
- createdAt: Date;
466
- }
467
- /**
468
- * Options for run operations.
469
- */
470
- interface RunOptions {
471
- assignedTo?: string;
472
- max_retries?: number;
473
- retry_strategy?: 'linear' | 'exponential';
474
- }
475
- /**
476
- * Options for update operations.
477
- */
478
- interface UpdateOptions {
479
- /**
480
- * Debounce configuration for the update.
481
- * If boolean, enables default debounce behavior.
482
- */
483
- debounced?: boolean | DebounceOptions;
484
- }
485
- /**
486
- * Configuration options for debouncing updates.
487
- */
488
- interface DebounceOptions {
489
- /**
490
- * The key to use for debouncing.
491
- * - 'recordId': Debounce based on the specific record ID. WARNING: IT WILL ONLY ACCEPT THE LATEST CHANGE AND DOES *NOT* MERGE THE PREVIOUS ONCES. IF YOU ARE UNSURE JUST USE 'recordId_x_fields'.
492
- * - 'recordId_x_fields': Debounce based on record ID and specific fields.
493
- */
494
- key?: 'recordId' | 'recordId_x_fields';
495
- /** The debounce delay in milliseconds. */
496
- delay?: number;
497
- }
498
- //#endregion
499
235
  //#region src/modules/auth/events/index.d.ts
500
236
  declare const AuthEventTypes: {
501
237
  readonly AuthStateChanged: "AUTH_STATE_CHANGED";
@@ -543,7 +279,151 @@ declare class AuthService<S extends SchemaStructure> {
543
279
  signIn<Name extends keyof S['access'] & string>(accessName: Name, params: ExtractAccessParams<S, Name, 'signIn'>): Promise<void>;
544
280
  }
545
281
  //#endregion
546
- //#region src/spooky.d.ts
282
+ //#region src/modules/crdt/crdt-field.d.ts
283
+ declare const CURSOR_COLORS: string[];
284
+ declare function cursorColorFromName(name: string): string;
285
+ declare class CrdtField {
286
+ private fieldName;
287
+ private doc;
288
+ private pushTimer;
289
+ private local;
290
+ private remote;
291
+ private recordId;
292
+ private sessionId;
293
+ private unsubscribe;
294
+ private lastPushTime;
295
+ private lastCursorPushTime;
296
+ private loadedFromCrdt;
297
+ private pushRetryCount;
298
+ private logger;
299
+ private cursorsEnabled;
300
+ /** Remote-push debounce. Local writes happen immediately on every Loro
301
+ * update; the remote UPSERT is coalesced over this window. Configured
302
+ * via `Sp00kyConfig.crdtDebounceMs`, default 500. */
303
+ private remoteDebounceMs;
304
+ private _onCursorUpdate;
305
+ private pendingCursorUpdate;
306
+ /** Callback set by the editor to receive remote cursor updates.
307
+ * Any cursor data that arrived before this callback was set will be replayed. */
308
+ set onCursorUpdate(cb: ((data: Uint8Array) => void) | null);
309
+ get onCursorUpdate(): ((data: Uint8Array) => void) | null;
310
+ constructor(fieldName: string, cursorsEnabled: boolean, initialState?: Uint8Array, logger?: Logger$1 | null);
311
+ getDoc(): LoroDoc;
312
+ /** Whether the LoroDoc was loaded from saved CRDT state */
313
+ hasContent(): boolean;
314
+ startSync(local: LocalDatabaseService, remote: RemoteDatabaseService, recordId: string, sessionId: string, debounceMs: number): void;
315
+ stopSync(): void;
316
+ importRemote(state: Uint8Array): void;
317
+ exportSnapshot(): Uint8Array;
318
+ /** Push this session's cursor blob into the parent row at
319
+ * `<field>.cursors[$sid]`. No-op when cursors aren't enabled on this
320
+ * field — the editor still calls this method optimistically, but
321
+ * without `@cursor` on the schema there's nowhere to store the blob.
322
+ * The UPDATE itself fires the parent table's LIVE feed, so other
323
+ * browsers receive the cursor change without a separate `_00_rv` bump. */
324
+ pushCursorState(encoded: Uint8Array): Promise<void>;
325
+ /** Import remote cursor state (called by CrdtManager from LIVE SELECT) */
326
+ importRemoteCursor(base64State: string): void;
327
+ private scheduleRemotePush;
328
+ /** SET path inside a parent row for the current snapshot. `@crdt`-only
329
+ * fields hold the snapshot directly (`<field>`); `@crdt @cursor`
330
+ * fields hold a `{ state, cursors }` object so the snapshot lives at
331
+ * `<field>.state` next to per-session cursor blobs. */
332
+ private statePath;
333
+ /** Mirror the LoroDoc snapshot into the parent row locally. Runs on
334
+ * every local update and every remote import so reloads (online or
335
+ * offline) see the freshest content immediately. Failures are
336
+ * swallowed — a stale local write must never block user input. */
337
+ private persistLocal;
338
+ private pushToRemote;
339
+ }
340
+ //#endregion
341
+ //#region src/modules/crdt/index.d.ts
342
+ /**
343
+ * CrdtManager manages active CrdtField instances and their sync channels.
344
+ *
345
+ * Collaborative state lives in two dedicated tables (defined in
346
+ * `apps/cli/src/meta_tables_remote.surql`):
347
+ * - `_00_crdt` { record_id, field, state } — one row per (record, field)
348
+ * - `_00_cursor` { record_id, session_id, field, state } — one row per
349
+ * (record, session, field)
350
+ *
351
+ * Splitting them off the parent row is what makes offline edits mergeable:
352
+ * each (record, field) gets its own row, so concurrent offline writes don't
353
+ * collide on the parent's last-write-wins semantics.
354
+ *
355
+ * Cross-browser delivery still rides the parent table's existing LIVE feed
356
+ * to avoid SurrealDB v3 LIVE bugs around dereference-based permission rules
357
+ * (issues 3602, 4026). On every meta UPSERT the writer also bumps the
358
+ * parent's `_00_rv` (a no-op assignment); that fires the parent's LIVE
359
+ * feed, and the receiver pulls the matching `_00_crdt` / `_00_cursor` rows
360
+ * via subquery. Permission inheritance happens server-side via
361
+ * `record_id.id != NONE` (SELECT) and `fn::can_update_record` (UPDATE).
362
+ */
363
+ declare class CrdtManager {
364
+ private schema;
365
+ private local;
366
+ private remote;
367
+ private debounceMs;
368
+ private fields;
369
+ private liveByTable;
370
+ private pendingLive;
371
+ private logger;
372
+ private sessionId;
373
+ constructor(schema: SchemaStructure, local: LocalDatabaseService, remote: RemoteDatabaseService, logger: Logger$1, debounceMs?: number);
374
+ /** Set the session id that scopes this client's cursor entries. Must be
375
+ * called before `open()` for cursors to be pushed under a stable key.
376
+ * Passed in from `sp00ky.ts` at boot (it already fetches `session::id()`
377
+ * for the data-module salt). */
378
+ setSessionId(sessionId: string): void;
379
+ /**
380
+ * Open a CRDT field for collaborative editing.
381
+ *
382
+ * @param table - Table name
383
+ * @param recordId - Full record ID (e.g., "thread:abc")
384
+ * @param field - Field name (e.g., "title", "content")
385
+ * @param fallbackText - Current plain text from the record, used to seed the
386
+ * LoroDoc if no CRDT state exists yet (migration path)
387
+ */
388
+ open(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
389
+ close(table: string, recordId: string, field: string): void;
390
+ closeAll(): void;
391
+ /** Ensure a single `LIVE SELECT * FROM <table>` is running, shared across
392
+ * every open CrdtField on `table`. */
393
+ private ensureTableSubscription;
394
+ /** Apply a parent-row payload from a non-LIVE source (e.g. the
395
+ * list_ref-driven sync engine, when the cross-user LIVE on the
396
+ * parent table is filtered out by the SurrealDB cross-session
397
+ * permission gap). Same semantics as the internal `dispatchRow`. */
398
+ applyRow(table: string, row: Record<string, unknown>): void;
399
+ /** Dispatch a parent-row LIVE event to every open CrdtField on that
400
+ * record. Each open field reads its slice of the row directly — the
401
+ * CRDT snapshot is a column on the parent now, so there is no
402
+ * follow-up subquery. */
403
+ private dispatchRow;
404
+ /** One-shot remote fetch for a row whose CRDT field hasn't synced
405
+ * locally yet (fresh device, memory-backed local DB after reload, …).
406
+ * Used by `open()` when the local read came up empty. Subsequent
407
+ * cross-browser updates ride `dispatchRow` via the parent LIVE feed. */
408
+ private fetchAndDispatchRow;
409
+ /** Schema lookup: does `<table>.<field>` carry a `@cursor` annotation?
410
+ * Determines the on-disk shape (plain snapshot vs. `{ state, cursors }`). */
411
+ private fieldHasCursor;
412
+ /** Pull the LoroDoc snapshot bytes out of a row slice. For `@crdt`-only
413
+ * the slice IS the snapshot (Uint8Array); for `@crdt @cursor` it's
414
+ * `{ state, cursors }` where `state` carries the snapshot bytes. */
415
+ private extractSnapshot;
416
+ private killTableSubscription;
417
+ private makeKey;
418
+ /**
419
+ * Throws if `<table>.<field>` is not annotated `@crdt` in the schema. Catches
420
+ * typos, removed annotations, and stale schema codegen at the call site instead
421
+ * of silently producing a non-CRDT writer.
422
+ */
423
+ private assertCrdtField;
424
+ }
425
+ //#endregion
426
+ //#region src/sp00ky.d.ts
547
427
  declare class BucketHandle {
548
428
  private bucketName;
549
429
  private remote;
@@ -557,7 +437,7 @@ declare class BucketHandle {
557
437
  rename(sourcePath: string, targetPath: string): Promise<void>;
558
438
  list(prefix?: string): Promise<string[]>;
559
439
  }
560
- declare class SpookyClient<S extends SchemaStructure> {
440
+ declare class Sp00kyClient<S extends SchemaStructure> {
561
441
  private config;
562
442
  private local;
563
443
  private remote;
@@ -567,14 +447,21 @@ declare class SpookyClient<S extends SchemaStructure> {
567
447
  private dataModule;
568
448
  private sync;
569
449
  private devTools;
450
+ private crdtManager;
570
451
  private logger;
571
452
  auth: AuthService<S>;
572
453
  streamProcessor: StreamProcessorService;
573
- get remoteClient(): Surreal;
574
- get localClient(): Surreal;
454
+ get remoteClient(): surrealdb0.Surreal;
455
+ get localClient(): surrealdb0.Surreal;
575
456
  get pendingMutationCount(): number;
457
+ /** Number of times the initial list_ref LIVE subscription retried on
458
+ * the most recent `setCurrentUserId` call. 0 when the SSP's
459
+ * pre-emptive user-table creation got there first; >0 when LIVE
460
+ * registration hit a "table not found" race. Exposed so the e2e
461
+ * suite can guard the pre-emptive path against regression. */
462
+ get liveRetryCount(): number;
576
463
  subscribeToPendingMutations(cb: (count: number) => void): () => void;
577
- constructor(config: SpookyConfig<S>);
464
+ constructor(config: Sp00kyConfig<S>);
578
465
  /**
579
466
  * Setup direct callbacks instead of event subscriptions
580
467
  */
@@ -582,13 +469,46 @@ declare class SpookyClient<S extends SchemaStructure> {
582
469
  init(): Promise<void>;
583
470
  close(): Promise<void>;
584
471
  authenticate(token: string): Promise<surrealdb0.Tokens>;
472
+ /**
473
+ * Open a CRDT field for collaborative editing.
474
+ * Returns a CrdtField with a LoroDoc that can be bound to any editor.
475
+ * Also starts a LIVE SELECT on the parent table for real-time sync;
476
+ * incoming events trigger a subquery fetch of `_00_crdt` / `_00_cursor`.
477
+ */
478
+ openCrdtField(table: string, recordId: string, field: string, fallbackText?: string): Promise<CrdtField>;
479
+ /**
480
+ * Close a CRDT field when editing is done.
481
+ */
482
+ closeCrdtField(table: string, recordId: string, field: string): void;
585
483
  deauthenticate(): Promise<void>;
586
- query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, SpookyQueryResultPromise>;
484
+ query<Table extends TableNames<S>>(table: Table, options: QueryOptions<TableModel<GetTable<S, Table>>, false>, ttl?: QueryTimeToLive): QueryBuilder<S, Table, Sp00kyQueryResultPromise>;
587
485
  private initQuery;
588
486
  queryRaw(sql: string, params: Record<string, any>, ttl: QueryTimeToLive): Promise<string>;
589
487
  subscribe(queryHash: string, callback: (records: Record<string, any>[]) => void, options?: {
590
488
  immediate?: boolean;
591
489
  }): Promise<() => void>;
490
+ /**
491
+ * Opt-in eager teardown for a query whose last subscriber has gone away
492
+ * (e.g. a viewport-windowed list cancelling an off-screen window). No-op
493
+ * while any subscriber remains. Tears down the remote `_00_query` view +
494
+ * local WASM view instead of waiting for the TTL sweep. Default behavior
495
+ * (no call here) keeps the view resident for cheap re-subscription.
496
+ */
497
+ deregisterQuery(queryHash: string): void;
498
+ /**
499
+ * Subscribe to a query's fetch-status changes (idle/fetching). With
500
+ * `{ immediate: true }` the callback fires synchronously with the current
501
+ * status. Powers the `useQuery` hook's `isFetching()` accessor.
502
+ */
503
+ subscribeQueryStatus(queryHash: string, callback: QueryStatusCallback, options?: {
504
+ immediate?: boolean;
505
+ }): () => void;
506
+ /**
507
+ * Report the frontend processing time (ms) a client framework spent applying
508
+ * an update for a query (e.g. `useQuery`'s `reconcile()`), so DevTools/MCP can
509
+ * surface the "frontend" phase of the per-query timing breakdown.
510
+ */
511
+ reportFrontendTiming(queryHash: string, ms: number): void;
592
512
  run<B extends BackendNames<S>, R extends BackendRoutes<S, B>>(backend: B, path: R, payload: RoutePayload<S, B, R>, options?: RunOptions): Promise<void>;
593
513
  bucket<B extends BucketNames<S>>(name: B): BucketHandle;
594
514
  create(id: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
@@ -597,15 +517,25 @@ declare class SpookyClient<S extends SchemaStructure> {
597
517
  }>;
598
518
  delete(table: string, id: string): Promise<void>;
599
519
  useRemote<T>(fn: (client: Surreal) => Promise<T> | T): Promise<T>;
600
- private persistClientId;
601
- private loadOrGenerateClientId;
520
+ /**
521
+ * Fetch SurrealDB's `session::id()` as a string. Used as a salt for
522
+ * query-id hashing so two sessions for the same user get distinct
523
+ * `_00_query` rows. Returns empty string if the query fails (we still
524
+ * boot, just without session scoping for IDs).
525
+ */
526
+ private fetchSessionId;
602
527
  }
603
528
  //#endregion
604
529
  //#region src/utils/index.d.ts
605
530
  declare function fileToUint8Array(file: File | Blob): Promise<Uint8Array>;
531
+ /**
532
+ * Convert plain text to simple HTML paragraphs.
533
+ * Useful for seeding a rich-text editor (e.g. TipTap/ProseMirror) with fallback content.
534
+ */
535
+ declare function textToHtml(text: string): string;
606
536
  /**
607
537
  * Helper for retrying DB operations with exponential backoff
608
538
  */
609
539
 
610
540
  //#endregion
611
- export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, DebounceOptions, EventSubscriptionOptions, type Level, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryTimeToLive, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RunOptions, SpookyClient, SpookyConfig, SpookyQueryResult, SpookyQueryResultPromise, StoreType, UpdateOptions, createAuthEventSystem, fileToUint8Array };
541
+ export { AuthEventSystem, AuthEventTypeMap, AuthEventTypes, AuthService, BucketHandle, CURSOR_COLORS, CrdtField, CrdtManager, DebounceOptions, EventSubscriptionOptions, Level, MATERIALIZATION_SAMPLE_WINDOW, MutationCallback, MutationEvent, MutationEventType, PersistenceClient, PhaseStat, PinoTransmit, QueryConfig, QueryConfigRecord, QueryHash, QueryState, QueryStatus, QueryStatusCallback, QueryTimeToLive, QueryTimings, QueryUpdateCallback, RecordVersionArray, RecordVersionDiff, RegistrationTimings, RunOptions, Sp00kyClient, Sp00kyConfig, Sp00kyQueryResult, Sp00kyQueryResultPromise, StoreType, TimingPhase, UpdateOptions, createAuthEventSystem, cursorColorFromName, fileToUint8Array, textToHtml };