@korajs/server 1.0.0-beta.1 → 1.0.0-beta.2

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/dist/index.d.cts CHANGED
@@ -1,10 +1,173 @@
1
- import { SchemaDefinition, KoraEventEmitter, Operation, BlobRef, VersionVector } from '@korajs/core';
1
+ import { SchemaDefinition, Operation, BlobRef, KoraEventEmitter, VersionVector } from '@korajs/core';
2
2
  import * as _korajs_sync from '@korajs/sync';
3
- import { SyncStore, MessageSerializer, SyncMessage, AwarenessUpdateMessage, YjsDocUpdateMessage, BlobChunkRequestMessage, BlobChunkResponseMessage, ApplyResult } from '@korajs/sync';
3
+ import { SyncStore, ApplyResult, MessageSerializer, SyncMessage, AwarenessUpdateMessage, YjsDocUpdateMessage, BlobChunkRequestMessage, BlobChunkResponseMessage } from '@korajs/sync';
4
4
  import { S as ServerTransport, a as ServerMessageHandler, b as ServerCloseHandler, c as ServerErrorHandler } from './server-transport-CU1BLWgr.cjs';
5
5
  import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
6
6
  import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
7
7
 
8
+ /**
9
+ * Per-collection scope map from auth context.
10
+ */
11
+ type ScopeMap = Record<string, Record<string, unknown>>;
12
+
13
+ /**
14
+ * A materialized record reconstructed from the operation log
15
+ * or read from a materialized collection table.
16
+ */
17
+ interface MaterializedRecord {
18
+ id: string;
19
+ [key: string]: unknown;
20
+ }
21
+ /**
22
+ * Options for querying a materialized collection table.
23
+ */
24
+ interface CollectionQueryOptions {
25
+ /** Exact-match filters on field values */
26
+ where?: Record<string, unknown>;
27
+ /** Field name to order results by */
28
+ orderBy?: string;
29
+ /** Sort direction (default: 'asc') */
30
+ orderDirection?: 'asc' | 'desc';
31
+ /** Maximum number of records to return */
32
+ limit?: number;
33
+ /** Number of records to skip (for pagination) */
34
+ offset?: number;
35
+ /** Include soft-deleted records (default: false) */
36
+ includeDeleted?: boolean;
37
+ }
38
+ /**
39
+ * Server-side store interface. Extends SyncStore with lifecycle,
40
+ * introspection, and materialization methods needed by the sync server.
41
+ */
42
+ interface ServerStore extends SyncStore {
43
+ /** Close the store and release resources */
44
+ close(): Promise<void>;
45
+ /** Get the total number of stored operations */
46
+ getOperationCount(): Promise<number>;
47
+ /**
48
+ * Set the schema for materialized collection tables.
49
+ * Creates collection tables and indexes based on the schema definition.
50
+ * If operations already exist in the store, backfills the materialized
51
+ * tables from the operation log.
52
+ *
53
+ * @param schema - The schema definition describing all collections
54
+ */
55
+ setSchema(schema: SchemaDefinition): Promise<void>;
56
+ /** Schema used for materialized tables and server-side validation, if set. */
57
+ getSchema(): SchemaDefinition | null;
58
+ /**
59
+ * Get all records from a materialized collection.
60
+ * When schema is set, reads directly from the collection table (O(1) indexed).
61
+ * When schema is not set, falls back to replaying the operation log.
62
+ * Deleted records are excluded.
63
+ *
64
+ * @param collection - The collection name to query
65
+ * @returns Array of records with their current state
66
+ */
67
+ materializeCollection(collection: string): Promise<MaterializedRecord[]>;
68
+ /**
69
+ * Query records from a materialized collection with filtering, ordering,
70
+ * and pagination. Requires schema to be set via setSchema().
71
+ *
72
+ * @param collection - The collection name to query
73
+ * @param options - Query options (where, orderBy, limit, offset)
74
+ * @returns Array of matching records
75
+ */
76
+ queryCollection(collection: string, options?: CollectionQueryOptions): Promise<MaterializedRecord[]>;
77
+ /**
78
+ * Find a single record by ID from a materialized collection.
79
+ * Requires schema to be set via setSchema().
80
+ *
81
+ * @param collection - The collection name
82
+ * @param id - The record ID
83
+ * @returns The record or null if not found (or deleted)
84
+ */
85
+ findRecord(collection: string, id: string): Promise<MaterializedRecord | null>;
86
+ /**
87
+ * Count records in a materialized collection, optionally filtered.
88
+ * Requires schema to be set via setSchema().
89
+ *
90
+ * @param collection - The collection name
91
+ * @param where - Optional exact-match filters
92
+ * @returns Number of matching records
93
+ */
94
+ countCollection(collection: string, where?: Record<string, unknown>): Promise<number>;
95
+ /**
96
+ * Export all data as a portable backup binary.
97
+ * Ships operations, version vector, and metadata in a self-describing format
98
+ * with SHA-256 checksum.
99
+ */
100
+ exportBackup(): Promise<Uint8Array>;
101
+ /**
102
+ * Restore data from a portable backup binary.
103
+ * Operations are applied through applyRemoteOperation for safe merge.
104
+ *
105
+ * @param data - Backup binary
106
+ * @param merge - If true, merge with existing operations; if false, replace all
107
+ */
108
+ importBackup(data: Uint8Array, merge?: boolean): Promise<{
109
+ operationsRestored: number;
110
+ success: boolean;
111
+ }>;
112
+ }
113
+
114
+ /**
115
+ * Shared vocabulary for why the server refused an operation.
116
+ *
117
+ * The `retriable` flag answers one question for the caller: is it worth
118
+ * submitting this SAME operation again later?
119
+ *
120
+ * - `true` — the rejection is transient (for example a rate limit). The
121
+ * identical operation may succeed once the condition clears, so a
122
+ * client is right to back off and retry the same bytes.
123
+ * - `false` — the rejection is permanent for the operation as written (a
124
+ * constraint violation, a referential conflict, a malformed
125
+ * mutation, a scope violation). Resubmitting the same bytes will
126
+ * always fail; the caller must change the operation or give up.
127
+ *
128
+ * This is deliberately defined once, in one place, so the trusted server
129
+ * data-plane (route context / `server.kora`) and the untrusted client-ingestion
130
+ * path speak the same language. `retriable` (this spelling) matches the wire
131
+ * `ErrorMessage.retriable` field the sync protocol already ships, so a single
132
+ * flag flows from the pipeline out to connected clients unchanged.
133
+ */
134
+ interface OperationRejection {
135
+ /** Stable, machine-readable reason code (for example `CONSTRAINT_VIOLATION`). */
136
+ code: string;
137
+ /** Human-readable explanation with enough context to debug without reproduction. */
138
+ message: string;
139
+ /** Whether resubmitting the identical operation may later succeed. */
140
+ retriable: boolean;
141
+ }
142
+ /**
143
+ * Reason codes whose underlying condition is transient, so the identical
144
+ * operation may succeed on a later attempt.
145
+ *
146
+ * Everything NOT listed here is treated as permanent. Permanent is the safe
147
+ * default: a client should never be told to keep hammering a genuinely invalid
148
+ * operation, so a new code is non-retriable until it is deliberately added here.
149
+ *
150
+ * `RATE_LIMIT` is the code the session emits when a client exceeds its
151
+ * per-minute operation budget (see `client-session.ts`).
152
+ */
153
+ declare const RETRIABLE_REJECTION_CODES: ReadonlySet<string>;
154
+ /**
155
+ * Classify a reason code as retriable (transient) or not (permanent).
156
+ *
157
+ * @param code - A rejection reason code.
158
+ * @returns `true` when resubmitting the identical operation may later succeed.
159
+ */
160
+ declare function isRetriableRejection(code: string): boolean;
161
+
162
+ interface ApplyServerOperationResult {
163
+ /** Result of applying the primary operation */
164
+ result: ApplyResult;
165
+ /** Primary op when applied, plus any server-generated side-effect operations */
166
+ appliedOperations: Operation[];
167
+ /** Rejection reason when the operation was not applied */
168
+ rejection?: OperationRejection;
169
+ }
170
+
8
171
  /**
9
172
  * Snapshot of server-side sync metrics at a point in time.
10
173
  */
@@ -125,105 +288,257 @@ declare function createSilentLogger(): Logger;
125
288
  declare function createDefaultLogger(): Logger;
126
289
 
127
290
  /**
128
- * A materialized record reconstructed from the operation log
129
- * or read from a materialized collection table.
291
+ * Minimal interface for a ws.WebSocketServer instance.
292
+ * Allows dependency injection for testing without importing ws directly.
130
293
  */
131
- interface MaterializedRecord {
132
- id: string;
133
- [key: string]: unknown;
294
+ interface WsServerLike {
295
+ on(event: string, listener: (...args: unknown[]) => void): void;
296
+ close(callback?: (err?: Error) => void): void;
297
+ address(): {
298
+ port: number;
299
+ } | string | null;
134
300
  }
135
301
  /**
136
- * Options for querying a materialized collection table.
302
+ * Constructor type for creating a WebSocket server.
137
303
  */
138
- interface CollectionQueryOptions {
139
- /** Exact-match filters on field values */
140
- where?: Record<string, unknown>;
141
- /** Field name to order results by */
142
- orderBy?: string;
143
- /** Sort direction (default: 'asc') */
144
- orderDirection?: 'asc' | 'desc';
145
- /** Maximum number of records to return */
146
- limit?: number;
147
- /** Number of records to skip (for pagination) */
148
- offset?: number;
149
- /** Include soft-deleted records (default: false) */
150
- includeDeleted?: boolean;
151
- }
304
+ type WsServerConstructor = new (options: {
305
+ port?: number;
306
+ host?: string;
307
+ path?: string;
308
+ }) => WsServerLike;
152
309
  /**
153
- * Server-side store interface. Extends SyncStore with lifecycle,
154
- * introspection, and materialization methods needed by the sync server.
310
+ * Self-hosted sync server. Accepts WebSocket connections from clients,
311
+ * handles the sync protocol, stores operations, and relays changes
312
+ * between connected clients.
313
+ *
314
+ * Two modes of operation:
315
+ * 1. **Standalone**: Call `start()` with a port — creates its own WebSocket server.
316
+ * 2. **Attach**: Call `handleConnection(transport)` — attach to an existing HTTP server.
155
317
  */
156
- interface ServerStore extends SyncStore {
157
- /** Close the store and release resources */
158
- close(): Promise<void>;
159
- /** Get the total number of stored operations */
160
- getOperationCount(): Promise<number>;
318
+ declare class KoraSyncServer {
319
+ private readonly store;
320
+ private readonly auth;
321
+ private readonly serializer;
322
+ private readonly emitter;
323
+ private readonly maxConnections;
324
+ private readonly batchSize;
325
+ private readonly schemaVersion;
326
+ private readonly supportedSchemaVersions;
327
+ private readonly port;
328
+ private readonly host;
329
+ private readonly path;
330
+ private readonly logger;
331
+ private readonly metrics;
332
+ private readonly awarenessRelay;
333
+ private readonly yjsDocRelay;
334
+ private readonly blobChunkRelay;
335
+ private readonly persistBlobChunk;
336
+ private readonly maxOperationBytes;
337
+ private readonly maxOpsPerMinute;
338
+ private readonly validateOperation;
339
+ private readonly koraContext;
340
+ private readonly sessions;
341
+ private readonly httpClients;
342
+ private readonly httpSessionToClient;
343
+ private readonly serverVersion;
344
+ private wsServer;
345
+ private running;
346
+ constructor(config: KoraSyncServerConfig);
161
347
  /**
162
- * Set the schema for materialized collection tables.
163
- * Creates collection tables and indexes based on the schema definition.
164
- * If operations already exist in the store, backfills the materialized
165
- * tables from the operation log.
348
+ * The trusted, scoped data-plane context (`apply` / `query` / `findById`).
349
+ * Shared with the production server so HTTP routes, the operation validator,
350
+ * and `server.kora` are all the same object over the same pipeline.
351
+ */
352
+ getKoraContext(): ProductionHttpRouteContext;
353
+ /**
354
+ * Subscribe to session-level events for metrics collection and logging.
355
+ * Called when a new session is created.
356
+ */
357
+ private attachSessionEvents;
358
+ /**
359
+ * Get the metrics collector for external access (e.g., HTTP endpoints).
360
+ */
361
+ getMetricsCollector(): ServerMetricsCollector;
362
+ /**
363
+ * Get the logger for external access (e.g., event streaming).
364
+ */
365
+ getLogger(): Logger;
366
+ /**
367
+ * Start the WebSocket server in standalone mode.
166
368
  *
167
- * @param schema - The schema definition describing all collections
369
+ * @param wsServerImpl - Optional WebSocket server constructor for testing
168
370
  */
169
- setSchema(schema: SchemaDefinition): Promise<void>;
170
- /** Schema used for materialized tables and server-side validation, if set. */
171
- getSchema(): SchemaDefinition | null;
371
+ start(wsServerImpl?: WsServerConstructor): Promise<void>;
172
372
  /**
173
- * Get all records from a materialized collection.
174
- * When schema is set, reads directly from the collection table (O(1) indexed).
175
- * When schema is not set, falls back to replaying the operation log.
176
- * Deleted records are excluded.
373
+ * Stop the server. Closes all sessions and the WebSocket server.
374
+ */
375
+ stop(): Promise<void>;
376
+ /**
377
+ * Handle one HTTP sync request for a long-polling client.
177
378
  *
178
- * @param collection - The collection name to query
179
- * @returns Array of records with their current state
379
+ * A stable `clientId` identifies the logical connection across requests.
180
380
  */
181
- materializeCollection(collection: string): Promise<MaterializedRecord[]>;
381
+ handleHttpRequest(request: HttpSyncRequest): Promise<HttpSyncResponse>;
182
382
  /**
183
- * Query records from a materialized collection with filtering, ordering,
184
- * and pagination. Requires schema to be set via setSchema().
383
+ * Handle an incoming client connection (attach mode).
384
+ * Creates a new ClientSession for the transport.
185
385
  *
186
- * @param collection - The collection name to query
187
- * @param options - Query options (where, orderBy, limit, offset)
188
- * @returns Array of matching records
386
+ * @param transport - The server transport for the new connection
387
+ * @returns The session ID
189
388
  */
190
- queryCollection(collection: string, options?: CollectionQueryOptions): Promise<MaterializedRecord[]>;
389
+ handleConnection(transport: ServerTransport): string;
191
390
  /**
192
- * Find a single record by ID from a materialized collection.
193
- * Requires schema to be set via setSchema().
391
+ * Get the current server status.
392
+ */
393
+ getStatus(): Promise<ServerStatus>;
394
+ /**
395
+ * Apply a server-originated operation (for example one created by a custom
396
+ * HTTP route) through the same validated pipeline that incoming client
397
+ * operations use — Tier 2 constraints, referential integrity, and cascade
398
+ * side effects — then relay every applied operation to connected clients.
194
399
  *
195
- * @param collection - The collection name
196
- * @param id - The record ID
197
- * @returns The record or null if not found (or deleted)
400
+ * Because the operation did not come from a client session, it is relayed to
401
+ * ALL sessions (there is no source session to exclude). Each session still
402
+ * applies its own per-scope visibility filter in `relayOperations`, so a
403
+ * client only receives the operation if it falls within that client's scope.
404
+ *
405
+ * @param op - A fully-formed, server-originated operation to apply
406
+ * @returns The apply result, including any server-generated side-effect ops
198
407
  */
199
- findRecord(collection: string, id: string): Promise<MaterializedRecord | null>;
408
+ applyLocalOperation(op: Operation): Promise<ApplyServerOperationResult>;
200
409
  /**
201
- * Count records in a materialized collection, optionally filtered.
202
- * Requires schema to be set via setSchema().
410
+ * Collect every blob reference still reachable from live records on the server.
203
411
  *
204
- * @param collection - The collection name
205
- * @param where - Optional exact-match filters
206
- * @returns Number of matching records
412
+ * This is the live set for garbage-collecting the server's central blob store:
413
+ * pass the result to `collectBlobGarbage(blobStore, liveRefs)` from
414
+ * `@korajs/store` to reclaim bytes no record references any more. Only
415
+ * collections that declare a `blob` field are scanned.
416
+ *
417
+ * @returns Every live blob reference across all collections
418
+ */
419
+ getLiveBlobRefs(): Promise<BlobRef[]>;
420
+ /**
421
+ * Get the number of currently connected clients.
422
+ */
423
+ getConnectionCount(): number;
424
+ private handleRelay;
425
+ private handleSessionClose;
426
+ private handleAwarenessRelay;
427
+ private handleYjsDocRelay;
428
+ private getOrCreateHttpClient;
429
+ }
430
+
431
+ /**
432
+ * A mutation a custom HTTP route asks the server to apply on the data plane.
433
+ *
434
+ * `recordId` is optional for inserts (a UUID v7 is generated when omitted) and
435
+ * required for updates and deletes. For updates, `data` holds only the changed
436
+ * fields; the server reads the current record to capture the previous values so
437
+ * the change three-way merges correctly with concurrent client edits.
438
+ */
439
+ interface RouteMutation {
440
+ collection: string;
441
+ type: 'insert' | 'update' | 'delete';
442
+ recordId?: string;
443
+ data?: Record<string, unknown> | null;
444
+ }
445
+ /**
446
+ * Scope the route derived from the request's authenticated actor. When present,
447
+ * it is enforced exactly like a sync session's scope: `apply()` rejects a
448
+ * mutation whose resulting record falls outside the scope, and `query()` /
449
+ * `findById()` only return records inside it. Omit it for genuinely public
450
+ * routes (for example a signup endpoint) where no per-actor isolation applies.
451
+ */
452
+ interface RouteScopeOptions {
453
+ scope?: ScopeMap;
454
+ }
455
+ /** Result of {@link ProductionHttpRouteContext.apply}. */
456
+ type RouteApplyResult = {
457
+ ok: true;
458
+ operation: Operation;
459
+ record: MaterializedRecord | null;
460
+ } | {
461
+ ok: false;
462
+ code: string;
463
+ message: string;
464
+ retriable: boolean;
465
+ };
466
+ /**
467
+ * Scoped, validated data-plane access handed to custom HTTP route handlers.
468
+ *
469
+ * Every mutation goes through the same pipeline as sync (Tier 2 constraints,
470
+ * referential integrity, materialization, and fan-out to connected clients), so
471
+ * REST endpoints cannot accidentally bypass validation, constraints, or tenant
472
+ * isolation the way hand-rolled store writes can.
473
+ */
474
+ interface ProductionHttpRouteContext {
475
+ /**
476
+ * Apply a mutation through the validated server pipeline and relay the
477
+ * resulting operation(s) to connected clients.
207
478
  */
208
- countCollection(collection: string, where?: Record<string, unknown>): Promise<number>;
479
+ apply(mutation: RouteMutation, options?: RouteScopeOptions): Promise<RouteApplyResult>;
480
+ /** Read materialized records from a collection, optionally scoped. */
481
+ query(collection: string, options?: CollectionQueryOptions & RouteScopeOptions): Promise<MaterializedRecord[]>;
482
+ /** Read a single materialized record by id, optionally scoped. */
483
+ findById(collection: string, id: string, options?: RouteScopeOptions): Promise<MaterializedRecord | null>;
484
+ }
485
+
486
+ /**
487
+ * What the server hands an operation validator so it can adjudicate an untrusted
488
+ * client operation before that operation becomes authoritative.
489
+ *
490
+ * The validator is POLICY (yours); the framework owns the MECHANISM around it —
491
+ * running it at ingestion, and routing its decision deterministically.
492
+ */
493
+ interface OperationValidationContext {
209
494
  /**
210
- * Export all data as a portable backup binary.
211
- * Ships operations, version vector, and metadata in a self-describing format
212
- * with SHA-256 checksum.
495
+ * The authenticated actor for the submitting session, or `null` for an
496
+ * anonymous / unauthenticated connection. This is how a public submission
497
+ * endpoint tells "signed-in owner" from "anonymous respondent" apart.
213
498
  */
214
- exportBackup(): Promise<Uint8Array>;
499
+ auth: AuthContext | null;
215
500
  /**
216
- * Restore data from a portable backup binary.
217
- * Operations are applied through applyRemoteOperation for safe merge.
218
- *
219
- * @param data - Backup binary
220
- * @param merge - If true, merge with existing operations; if false, replace all
501
+ * Trusted, scoped data-plane access — the same context available as
502
+ * `server.kora` / `request.kora`. Use `kora.query` / `kora.findById` to read
503
+ * current authoritative state while deciding, and `kora.apply` to AUTHOR a
504
+ * derived server operation (for example promoting a validated anonymous
505
+ * submission into an owner-visible collection). Authoring a new operation is
506
+ * always preferred over mutating the incoming one, which must stay immutable
507
+ * so content-addressing and convergence hold.
221
508
  */
222
- importBackup(data: Uint8Array, merge?: boolean): Promise<{
223
- operationsRestored: number;
224
- success: boolean;
225
- }>;
509
+ kora: ProductionHttpRouteContext;
226
510
  }
511
+ /**
512
+ * The verdict a validator returns for one incoming operation.
513
+ *
514
+ * - `accept` — let the operation materialize on the server as-is and relay to
515
+ * connected clients, exactly as an untrusted op would flow without a validator.
516
+ * - `reject` — refuse it. The operation never enters the authoritative log, so no
517
+ * other replica ever sees it; a structured rejection travels back to the
518
+ * submitter tied to the operation id, and the submitter keeps the op in a
519
+ * durable rejected store (recoverable and explainable) rather than losing it or
520
+ * retrying forever. `retriable` defaults from the shared taxonomy for the code.
521
+ * - `ignore` — the server takes responsibility for the operation out of band (for
522
+ * example the validator already authored a derived op via `context.kora`) and
523
+ * does not want the raw incoming op materialized. No rejection is sent, so the
524
+ * submitter treats it as handled and drops it from its pending queue.
525
+ */
526
+ type OperationDecision = {
527
+ action: 'accept';
528
+ } | {
529
+ action: 'reject';
530
+ code: string;
531
+ message: string;
532
+ retriable?: boolean;
533
+ } | {
534
+ action: 'ignore';
535
+ };
536
+ /**
537
+ * An app-provided function that adjudicates an untrusted client operation at sync
538
+ * ingestion, after HLC ordering and the built-in guards (timestamp, rate, size),
539
+ * and before the operation is materialized into authoritative state.
540
+ */
541
+ type OperationValidator = (operation: Operation, context: OperationValidationContext) => Promise<OperationDecision> | OperationDecision;
227
542
 
228
543
  /**
229
544
  * Authenticated client context. Returned by an AuthProvider after validation.
@@ -303,6 +618,31 @@ interface KoraSyncServerConfig {
303
618
  * `@korajs/store` derives both from a `ContentAddressedBlobStore`.
304
619
  */
305
620
  persistBlobChunk?: (hash: string, bytes: Uint8Array) => Promise<void> | void;
621
+ /**
622
+ * Maximum serialized byte size of a single client operation accepted at sync
623
+ * ingest. Operations larger than this are rejected before materialization.
624
+ * Defaults to 256 KiB. Set once here to enforce one payload cap across every
625
+ * connected client instead of configuring each session.
626
+ */
627
+ maxOperationBytes?: number;
628
+ /**
629
+ * Maximum operations accepted per connected client per minute (sliding window).
630
+ * Operations beyond the limit are rejected until the window resets. Defaults to
631
+ * 600. Set once here to enforce one rate cap across every connected client.
632
+ */
633
+ maxOpsPerMinute?: number;
634
+ /**
635
+ * Adjudicate untrusted client operations before they become authoritative.
636
+ *
637
+ * Runs at sync ingestion for every incoming client operation, after HLC
638
+ * ordering and the built-in guards, and before materialization. Return
639
+ * `accept` to let it through, `reject` to refuse it (a structured rejection
640
+ * travels back to the submitter and the op never enters the authoritative
641
+ * log), or `ignore` when the server has handled it out of band. This is what
642
+ * lets Kora serve public / multi-tenant apps where the client is not trusted.
643
+ * Omit it and every operation is accepted, as before.
644
+ */
645
+ validateOperation?: OperationValidator;
306
646
  }
307
647
  /**
308
648
  * Request envelope for the server-side HTTP sync endpoint.
@@ -513,6 +853,14 @@ interface ClientSessionOptions {
513
853
  maxOperationBytes?: number;
514
854
  /** Maximum operations accepted per minute for this session. Defaults to 600. */
515
855
  maxOpsPerMinute?: number;
856
+ /**
857
+ * Adjudicate untrusted client operations before materialization. When present,
858
+ * each incoming operation is passed to this validator; a `reject` decision
859
+ * sends an operation-rejected message and skips materialization.
860
+ */
861
+ validateOperation?: OperationValidator;
862
+ /** Trusted data-plane context handed to the validator (read state, author derived ops). */
863
+ koraContext?: ProductionHttpRouteContext;
516
864
  }
517
865
  /**
518
866
  * Handles the sync protocol for a single connected client.
@@ -553,6 +901,8 @@ declare class ClientSession {
553
901
  private readonly maxOperationBytes;
554
902
  private readonly maxOpsPerMinute;
555
903
  private readonly rateLimiter;
904
+ private readonly validateOperation;
905
+ private readonly koraContext;
556
906
  constructor(options: ClientSessionOptions);
557
907
  /**
558
908
  * Start handling messages from the client transport.
@@ -596,210 +946,15 @@ declare class ClientSession {
596
946
  private handleAwarenessUpdate;
597
947
  private handleYjsDocUpdate;
598
948
  private sendError;
599
- private setSerializerWireFormat;
600
- private handleTransportClose;
601
- }
602
-
603
- interface ApplyServerOperationResult {
604
- /** Result of applying the primary operation */
605
- result: ApplyResult;
606
- /** Primary op when applied, plus any server-generated side-effect operations */
607
- appliedOperations: Operation[];
608
- /** Rejection reason when the operation was not applied */
609
- rejection?: {
610
- code: string;
611
- message: string;
612
- };
613
- }
614
-
615
- /**
616
- * Minimal interface for a ws.WebSocketServer instance.
617
- * Allows dependency injection for testing without importing ws directly.
618
- */
619
- interface WsServerLike {
620
- on(event: string, listener: (...args: unknown[]) => void): void;
621
- close(callback?: (err?: Error) => void): void;
622
- address(): {
623
- port: number;
624
- } | string | null;
625
- }
626
- /**
627
- * Constructor type for creating a WebSocket server.
628
- */
629
- type WsServerConstructor = new (options: {
630
- port?: number;
631
- host?: string;
632
- path?: string;
633
- }) => WsServerLike;
634
- /**
635
- * Self-hosted sync server. Accepts WebSocket connections from clients,
636
- * handles the sync protocol, stores operations, and relays changes
637
- * between connected clients.
638
- *
639
- * Two modes of operation:
640
- * 1. **Standalone**: Call `start()` with a port — creates its own WebSocket server.
641
- * 2. **Attach**: Call `handleConnection(transport)` — attach to an existing HTTP server.
642
- */
643
- declare class KoraSyncServer {
644
- private readonly store;
645
- private readonly auth;
646
- private readonly serializer;
647
- private readonly emitter;
648
- private readonly maxConnections;
649
- private readonly batchSize;
650
- private readonly schemaVersion;
651
- private readonly supportedSchemaVersions;
652
- private readonly port;
653
- private readonly host;
654
- private readonly path;
655
- private readonly logger;
656
- private readonly metrics;
657
- private readonly awarenessRelay;
658
- private readonly yjsDocRelay;
659
- private readonly blobChunkRelay;
660
- private readonly persistBlobChunk;
661
- private readonly sessions;
662
- private readonly httpClients;
663
- private readonly httpSessionToClient;
664
- private readonly serverVersion;
665
- private wsServer;
666
- private running;
667
- constructor(config: KoraSyncServerConfig);
668
- /**
669
- * Subscribe to session-level events for metrics collection and logging.
670
- * Called when a new session is created.
671
- */
672
- private attachSessionEvents;
673
- /**
674
- * Get the metrics collector for external access (e.g., HTTP endpoints).
675
- */
676
- getMetricsCollector(): ServerMetricsCollector;
677
- /**
678
- * Get the logger for external access (e.g., event streaming).
679
- */
680
- getLogger(): Logger;
681
- /**
682
- * Start the WebSocket server in standalone mode.
683
- *
684
- * @param wsServerImpl - Optional WebSocket server constructor for testing
685
- */
686
- start(wsServerImpl?: WsServerConstructor): Promise<void>;
687
- /**
688
- * Stop the server. Closes all sessions and the WebSocket server.
689
- */
690
- stop(): Promise<void>;
691
- /**
692
- * Handle one HTTP sync request for a long-polling client.
693
- *
694
- * A stable `clientId` identifies the logical connection across requests.
695
- */
696
- handleHttpRequest(request: HttpSyncRequest): Promise<HttpSyncResponse>;
697
- /**
698
- * Handle an incoming client connection (attach mode).
699
- * Creates a new ClientSession for the transport.
700
- *
701
- * @param transport - The server transport for the new connection
702
- * @returns The session ID
703
- */
704
- handleConnection(transport: ServerTransport): string;
705
- /**
706
- * Get the current server status.
707
- */
708
- getStatus(): Promise<ServerStatus>;
709
- /**
710
- * Apply a server-originated operation (for example one created by a custom
711
- * HTTP route) through the same validated pipeline that incoming client
712
- * operations use — Tier 2 constraints, referential integrity, and cascade
713
- * side effects — then relay every applied operation to connected clients.
714
- *
715
- * Because the operation did not come from a client session, it is relayed to
716
- * ALL sessions (there is no source session to exclude). Each session still
717
- * applies its own per-scope visibility filter in `relayOperations`, so a
718
- * client only receives the operation if it falls within that client's scope.
719
- *
720
- * @param op - A fully-formed, server-originated operation to apply
721
- * @returns The apply result, including any server-generated side-effect ops
722
- */
723
- applyLocalOperation(op: Operation): Promise<ApplyServerOperationResult>;
724
- /**
725
- * Collect every blob reference still reachable from live records on the server.
726
- *
727
- * This is the live set for garbage-collecting the server's central blob store:
728
- * pass the result to `collectBlobGarbage(blobStore, liveRefs)` from
729
- * `@korajs/store` to reclaim bytes no record references any more. Only
730
- * collections that declare a `blob` field are scanned.
731
- *
732
- * @returns Every live blob reference across all collections
733
- */
734
- getLiveBlobRefs(): Promise<BlobRef[]>;
735
- /**
736
- * Get the number of currently connected clients.
737
- */
738
- getConnectionCount(): number;
739
- private handleRelay;
740
- private handleSessionClose;
741
- private handleAwarenessRelay;
742
- private handleYjsDocRelay;
743
- private getOrCreateHttpClient;
744
- }
745
-
746
- /**
747
- * Per-collection scope map from auth context.
748
- */
749
- type ScopeMap = Record<string, Record<string, unknown>>;
750
-
751
- /**
752
- * A mutation a custom HTTP route asks the server to apply on the data plane.
753
- *
754
- * `recordId` is optional for inserts (a UUID v7 is generated when omitted) and
755
- * required for updates and deletes. For updates, `data` holds only the changed
756
- * fields; the server reads the current record to capture the previous values so
757
- * the change three-way merges correctly with concurrent client edits.
758
- */
759
- interface RouteMutation {
760
- collection: string;
761
- type: 'insert' | 'update' | 'delete';
762
- recordId?: string;
763
- data?: Record<string, unknown> | null;
764
- }
765
- /**
766
- * Scope the route derived from the request's authenticated actor. When present,
767
- * it is enforced exactly like a sync session's scope: `apply()` rejects a
768
- * mutation whose resulting record falls outside the scope, and `query()` /
769
- * `findById()` only return records inside it. Omit it for genuinely public
770
- * routes (for example a signup endpoint) where no per-actor isolation applies.
771
- */
772
- interface RouteScopeOptions {
773
- scope?: ScopeMap;
774
- }
775
- /** Result of {@link ProductionHttpRouteContext.apply}. */
776
- type RouteApplyResult = {
777
- ok: true;
778
- operation: Operation;
779
- record: MaterializedRecord | null;
780
- } | {
781
- ok: false;
782
- code: string;
783
- message: string;
784
- };
785
- /**
786
- * Scoped, validated data-plane access handed to custom HTTP route handlers.
787
- *
788
- * Every mutation goes through the same pipeline as sync (Tier 2 constraints,
789
- * referential integrity, materialization, and fan-out to connected clients), so
790
- * REST endpoints cannot accidentally bypass validation, constraints, or tenant
791
- * isolation the way hand-rolled store writes can.
792
- */
793
- interface ProductionHttpRouteContext {
794
949
  /**
795
- * Apply a mutation through the validated server pipeline and relay the
796
- * resulting operation(s) to connected clients.
950
+ * Reject one specific client operation, tied to its id, so the submitter can
951
+ * divert it out of its pending queue into a durable rejected store rather than
952
+ * losing it or retrying forever. The op is NOT materialized, so no other
953
+ * replica ever sees it.
797
954
  */
798
- apply(mutation: RouteMutation, options?: RouteScopeOptions): Promise<RouteApplyResult>;
799
- /** Read materialized records from a collection, optionally scoped. */
800
- query(collection: string, options?: CollectionQueryOptions & RouteScopeOptions): Promise<MaterializedRecord[]>;
801
- /** Read a single materialized record by id, optionally scoped. */
802
- findById(collection: string, id: string, options?: RouteScopeOptions): Promise<MaterializedRecord | null>;
955
+ private sendOperationRejected;
956
+ private setSerializerWireFormat;
957
+ private handleTransportClose;
803
958
  }
804
959
 
805
960
  /**
@@ -875,6 +1030,29 @@ interface ProductionServer {
875
1030
  start(): Promise<string>;
876
1031
  /** Stop the server gracefully. */
877
1032
  stop(): Promise<void>;
1033
+ /**
1034
+ * Trusted, scoped data-plane access for server-side callers that have no HTTP
1035
+ * request — background jobs, scheduled tasks, seeding scripts.
1036
+ *
1037
+ * `kora.apply()` runs the mutation through the exact same validated pipeline as
1038
+ * sync (Tier 2 constraints, referential integrity, materialization, and fan-out
1039
+ * to connected clients), and `kora.query()` / `kora.findById()` read
1040
+ * materialized state. This is the same context handed to custom HTTP routes as
1041
+ * `request.kora`, so a job and a request share one code path and one set of
1042
+ * guarantees instead of the job writing to the store directly and silently
1043
+ * bypassing validation.
1044
+ */
1045
+ kora: ProductionHttpRouteContext;
1046
+ /**
1047
+ * Every blob reference still reachable from a live record across all
1048
+ * collections that declare a `blob` field. This is the live set for
1049
+ * garbage-collecting the server's central blob store: pass it to
1050
+ * `collectBlobGarbage(blobStore, refs)` from `@korajs/store` on a schedule to
1051
+ * reclaim bytes no record points at any more.
1052
+ *
1053
+ * @returns Every live blob reference the server can currently see.
1054
+ */
1055
+ getLiveBlobRefs(): Promise<BlobRef[]>;
878
1056
  }
879
1057
  /**
880
1058
  * Creates a production server that serves both static files and WebSocket sync
@@ -1384,4 +1562,4 @@ declare class AwarenessRelay {
1384
1562
  */
1385
1563
  declare function createKoraServer(config: KoraSyncServerConfig): KoraSyncServer;
1386
1564
 
1387
- export { type AuthContext, type AuthProvider, AwarenessRelay, type AwarenessRelayCallback, type ClientMetrics, ClientSession, type ClientSessionOptions, type CollectionQueryOptions, type HttpPollResponse, HttpServerTransport, type HttpSyncRequest, type HttpSyncResponse, KoraAuthProvider, type KoraAuthProviderOptions, KoraSyncServer, type KoraSyncServerConfig, type LogEntry, type LogLevel, type Logger, type MaterializedRecord, MemoryServerStore, MixedAuthProvider, type MixedAuthProviderOptions, NoAuthProvider, PostgresServerStore, type ProductionHttpRoute, type ProductionHttpRouteContext, type ProductionHttpRouteRequest, type ProductionHttpRouteResponse, type ProductionServer, type ProductionServerConfig, type RelayCallback, type RouteApplyResult, type RouteMutation, type RouteScopeOptions, ServerCloseHandler, ServerErrorHandler, ServerMessageHandler, ServerMetricsCollector, type ServerMetricsSnapshot, type ServerStatus, type ServerStore, ServerTransport, type SessionState, SqliteServerStore, TokenAuthProvider, type TokenAuthProviderOptions, type WsServerConstructor, type WsServerLike, WsServerTransport, type WsServerTransportOptions, type WsWebSocket, createDefaultLogger, createJsonLogger, createKoraServer, createPostgresServerStore, createPrettyLogger, createProductionServer, createSilentLogger, createSqliteServerStore };
1565
+ export { type AuthContext, type AuthProvider, AwarenessRelay, type AwarenessRelayCallback, type ClientMetrics, ClientSession, type ClientSessionOptions, type CollectionQueryOptions, type HttpPollResponse, HttpServerTransport, type HttpSyncRequest, type HttpSyncResponse, KoraAuthProvider, type KoraAuthProviderOptions, KoraSyncServer, type KoraSyncServerConfig, type LogEntry, type LogLevel, type Logger, type MaterializedRecord, MemoryServerStore, MixedAuthProvider, type MixedAuthProviderOptions, NoAuthProvider, type OperationDecision, type OperationRejection, type OperationValidationContext, type OperationValidator, PostgresServerStore, type ProductionHttpRoute, type ProductionHttpRouteContext, type ProductionHttpRouteRequest, type ProductionHttpRouteResponse, type ProductionServer, type ProductionServerConfig, RETRIABLE_REJECTION_CODES, type RelayCallback, type RouteApplyResult, type RouteMutation, type RouteScopeOptions, ServerCloseHandler, ServerErrorHandler, ServerMessageHandler, ServerMetricsCollector, type ServerMetricsSnapshot, type ServerStatus, type ServerStore, ServerTransport, type SessionState, SqliteServerStore, TokenAuthProvider, type TokenAuthProviderOptions, type WsServerConstructor, type WsServerLike, WsServerTransport, type WsServerTransportOptions, type WsWebSocket, createDefaultLogger, createJsonLogger, createKoraServer, createPostgresServerStore, createPrettyLogger, createProductionServer, createSilentLogger, createSqliteServerStore, isRetriableRejection };