@korajs/server 0.1.0
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/chunk-DGUM43GV.js +11 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/index.cjs +1201 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +513 -0
- package/dist/index.d.ts +513 -0
- package/dist/index.js +1161 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.cjs +176 -0
- package/dist/internal.cjs.map +1 -0
- package/dist/internal.d.cts +82 -0
- package/dist/internal.d.ts +82 -0
- package/dist/internal.js +150 -0
- package/dist/internal.js.map +1 -0
- package/dist/server-transport-CU1BLWgr.d.cts +35 -0
- package/dist/server-transport-CU1BLWgr.d.ts +35 -0
- package/package.json +67 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
import { KoraEventEmitter, Operation, VersionVector } from '@korajs/core';
|
|
2
|
+
import * as _korajs_sync from '@korajs/sync';
|
|
3
|
+
import { SyncStore, MessageSerializer, SyncMessage, ApplyResult } from '@korajs/sync';
|
|
4
|
+
import { S as ServerTransport, a as ServerMessageHandler, b as ServerCloseHandler, c as ServerErrorHandler } from './server-transport-CU1BLWgr.cjs';
|
|
5
|
+
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
6
|
+
import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Server-side store interface. Extends SyncStore with lifecycle
|
|
10
|
+
* and introspection methods needed by the sync server.
|
|
11
|
+
*/
|
|
12
|
+
interface ServerStore extends SyncStore {
|
|
13
|
+
/** Close the store and release resources */
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
/** Get the total number of stored operations */
|
|
16
|
+
getOperationCount(): Promise<number>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Authenticated client context. Returned by an AuthProvider after validation.
|
|
21
|
+
*/
|
|
22
|
+
interface AuthContext {
|
|
23
|
+
/** Unique user identifier */
|
|
24
|
+
userId: string;
|
|
25
|
+
/** Per-collection sync scopes (optional) */
|
|
26
|
+
scopes?: Record<string, Record<string, unknown>>;
|
|
27
|
+
/** Arbitrary metadata about the authenticated user */
|
|
28
|
+
metadata?: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface for authenticating incoming client connections.
|
|
32
|
+
* Implementations validate tokens and return an AuthContext on success.
|
|
33
|
+
*/
|
|
34
|
+
interface AuthProvider {
|
|
35
|
+
/**
|
|
36
|
+
* Validate an authentication token.
|
|
37
|
+
* @param token - The token to validate
|
|
38
|
+
* @returns AuthContext if valid, null if rejected
|
|
39
|
+
*/
|
|
40
|
+
authenticate(token: string): Promise<AuthContext | null>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration for creating a KoraSyncServer.
|
|
44
|
+
*/
|
|
45
|
+
interface KoraSyncServerConfig {
|
|
46
|
+
/** Server-side operation store */
|
|
47
|
+
store: ServerStore;
|
|
48
|
+
/** WebSocket server port (standalone mode) */
|
|
49
|
+
port?: number;
|
|
50
|
+
/** Host to bind to (standalone mode). Defaults to '0.0.0.0'. */
|
|
51
|
+
host?: string;
|
|
52
|
+
/** Authentication provider. If omitted, all connections are accepted. */
|
|
53
|
+
auth?: AuthProvider;
|
|
54
|
+
/** Message serializer. Defaults to JsonMessageSerializer. */
|
|
55
|
+
serializer?: MessageSerializer;
|
|
56
|
+
/** Event emitter for DevTools integration */
|
|
57
|
+
emitter?: KoraEventEmitter;
|
|
58
|
+
/** Maximum concurrent client connections. 0 = unlimited. Defaults to 0. */
|
|
59
|
+
maxConnections?: number;
|
|
60
|
+
/** Maximum operations per sync batch. Defaults to 100. */
|
|
61
|
+
batchSize?: number;
|
|
62
|
+
/** Schema version the server expects. Defaults to 1. */
|
|
63
|
+
schemaVersion?: number;
|
|
64
|
+
/** WebSocket path (standalone mode). Defaults to '/'. */
|
|
65
|
+
path?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Request envelope for the server-side HTTP sync endpoint.
|
|
69
|
+
*/
|
|
70
|
+
interface HttpSyncRequest {
|
|
71
|
+
/** Stable client identifier for binding HTTP requests to a server session */
|
|
72
|
+
clientId: string;
|
|
73
|
+
/** HTTP method */
|
|
74
|
+
method: 'GET' | 'POST';
|
|
75
|
+
/** Optional raw request payload for POST */
|
|
76
|
+
body?: string | Uint8Array;
|
|
77
|
+
/** Value of the Content-Type header for POST payloads */
|
|
78
|
+
contentType?: string;
|
|
79
|
+
/** Value of the If-None-Match header for GET polling */
|
|
80
|
+
ifNoneMatch?: string;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Response envelope for the server-side HTTP sync endpoint.
|
|
84
|
+
*/
|
|
85
|
+
interface HttpSyncResponse {
|
|
86
|
+
/** HTTP status code */
|
|
87
|
+
status: 200 | 202 | 204 | 304 | 400 | 405 | 410;
|
|
88
|
+
/** Optional raw response payload */
|
|
89
|
+
body?: string | Uint8Array;
|
|
90
|
+
/** Optional response headers */
|
|
91
|
+
headers?: Record<string, string>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Runtime status of a KoraSyncServer.
|
|
95
|
+
*/
|
|
96
|
+
interface ServerStatus {
|
|
97
|
+
/** Whether the server is running */
|
|
98
|
+
running: boolean;
|
|
99
|
+
/** Number of currently connected clients */
|
|
100
|
+
connectedClients: number;
|
|
101
|
+
/** Port the server is listening on (null if attach mode) */
|
|
102
|
+
port: number | null;
|
|
103
|
+
/** Total operations stored */
|
|
104
|
+
totalOperations: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface HttpPollResponse {
|
|
108
|
+
status: 200 | 204 | 304 | 410;
|
|
109
|
+
body?: string | Uint8Array;
|
|
110
|
+
headers?: Record<string, string>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Server-side transport for HTTP long-polling clients.
|
|
114
|
+
*
|
|
115
|
+
* Incoming client messages are pushed via POST, while outbound server
|
|
116
|
+
* messages are pulled via GET long-poll requests.
|
|
117
|
+
*/
|
|
118
|
+
declare class HttpServerTransport implements ServerTransport {
|
|
119
|
+
private readonly serializer;
|
|
120
|
+
private messageHandler;
|
|
121
|
+
private closeHandler;
|
|
122
|
+
private errorHandler;
|
|
123
|
+
private connected;
|
|
124
|
+
private nextSequence;
|
|
125
|
+
private readonly queue;
|
|
126
|
+
constructor(serializer: MessageSerializer);
|
|
127
|
+
send(message: _korajs_sync.SyncMessage): void;
|
|
128
|
+
onMessage(handler: ServerMessageHandler): void;
|
|
129
|
+
onClose(handler: ServerCloseHandler): void;
|
|
130
|
+
onError(handler: ServerErrorHandler): void;
|
|
131
|
+
isConnected(): boolean;
|
|
132
|
+
close(code?: number, reason?: string): void;
|
|
133
|
+
receive(payload: string | Uint8Array): void;
|
|
134
|
+
poll(ifNoneMatch?: string): HttpPollResponse;
|
|
135
|
+
private makeEtag;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Minimal interface for a ws.WebSocket instance.
|
|
140
|
+
* Allows dependency injection for testing without importing ws directly.
|
|
141
|
+
*/
|
|
142
|
+
interface WsWebSocket {
|
|
143
|
+
readyState: number;
|
|
144
|
+
send(data: string | Uint8Array, callback?: (err?: Error) => void): void;
|
|
145
|
+
close(code?: number, reason?: string): void;
|
|
146
|
+
on(event: string, listener: (...args: unknown[]) => void): void;
|
|
147
|
+
removeAllListeners(): void;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Options for WsServerTransport.
|
|
151
|
+
*/
|
|
152
|
+
interface WsServerTransportOptions {
|
|
153
|
+
/** Message serializer. Defaults to JsonMessageSerializer. */
|
|
154
|
+
serializer?: MessageSerializer;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Server-side transport wrapping a ws.WebSocket connection.
|
|
158
|
+
* Created for each incoming client connection.
|
|
159
|
+
*/
|
|
160
|
+
declare class WsServerTransport implements ServerTransport {
|
|
161
|
+
private readonly ws;
|
|
162
|
+
private readonly serializer;
|
|
163
|
+
private messageHandler;
|
|
164
|
+
private closeHandler;
|
|
165
|
+
private errorHandler;
|
|
166
|
+
constructor(ws: WsWebSocket, options?: WsServerTransportOptions);
|
|
167
|
+
send(message: SyncMessage): void;
|
|
168
|
+
onMessage(handler: ServerMessageHandler): void;
|
|
169
|
+
onClose(handler: ServerCloseHandler): void;
|
|
170
|
+
onError(handler: ServerErrorHandler): void;
|
|
171
|
+
isConnected(): boolean;
|
|
172
|
+
close(code?: number, reason?: string): void;
|
|
173
|
+
private setupListeners;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Possible states for a client session.
|
|
178
|
+
*/
|
|
179
|
+
type SessionState = 'connected' | 'authenticated' | 'syncing' | 'streaming' | 'closed';
|
|
180
|
+
/**
|
|
181
|
+
* Callback invoked when a session has new operations to relay to other sessions.
|
|
182
|
+
*/
|
|
183
|
+
type RelayCallback = (sourceSessionId: string, operations: Operation[]) => void;
|
|
184
|
+
/**
|
|
185
|
+
* Options for creating a ClientSession.
|
|
186
|
+
*/
|
|
187
|
+
interface ClientSessionOptions {
|
|
188
|
+
/** Unique session identifier */
|
|
189
|
+
sessionId: string;
|
|
190
|
+
/** Transport for this client connection */
|
|
191
|
+
transport: ServerTransport;
|
|
192
|
+
/** Server-side operation store */
|
|
193
|
+
store: ServerStore;
|
|
194
|
+
/** Authentication provider (optional) */
|
|
195
|
+
auth?: AuthProvider;
|
|
196
|
+
/** Message serializer */
|
|
197
|
+
serializer?: MessageSerializer;
|
|
198
|
+
/** Event emitter for DevTools integration */
|
|
199
|
+
emitter?: KoraEventEmitter;
|
|
200
|
+
/** Max operations per sync batch */
|
|
201
|
+
batchSize?: number;
|
|
202
|
+
/** Schema version the server expects */
|
|
203
|
+
schemaVersion?: number;
|
|
204
|
+
/** Called when this session has operations to relay to other sessions */
|
|
205
|
+
onRelay?: RelayCallback;
|
|
206
|
+
/** Called when this session closes */
|
|
207
|
+
onClose?: (sessionId: string) => void;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Handles the sync protocol for a single connected client.
|
|
211
|
+
*
|
|
212
|
+
* Lifecycle: connected → (authenticated) → syncing → streaming → closed
|
|
213
|
+
*
|
|
214
|
+
* The session:
|
|
215
|
+
* 1. Receives a handshake from the client
|
|
216
|
+
* 2. Authenticates if an AuthProvider is configured
|
|
217
|
+
* 3. Sends back a HandshakeResponse with the server's version vector
|
|
218
|
+
* 4. Computes and sends the server's delta to the client (paginated)
|
|
219
|
+
* 5. Processes incoming operation batches from the client
|
|
220
|
+
* 6. Transitions to streaming for real-time bidirectional sync
|
|
221
|
+
* 7. Relays new operations to other sessions via the RelayCallback
|
|
222
|
+
*/
|
|
223
|
+
declare class ClientSession {
|
|
224
|
+
private state;
|
|
225
|
+
private clientNodeId;
|
|
226
|
+
private authContext;
|
|
227
|
+
private readonly sessionId;
|
|
228
|
+
private readonly transport;
|
|
229
|
+
private readonly store;
|
|
230
|
+
private readonly auth;
|
|
231
|
+
private readonly serializer;
|
|
232
|
+
private readonly emitter;
|
|
233
|
+
private readonly batchSize;
|
|
234
|
+
private readonly schemaVersion;
|
|
235
|
+
private readonly onRelay;
|
|
236
|
+
private readonly onClose;
|
|
237
|
+
constructor(options: ClientSessionOptions);
|
|
238
|
+
/**
|
|
239
|
+
* Start handling messages from the client transport.
|
|
240
|
+
*/
|
|
241
|
+
start(): void;
|
|
242
|
+
/**
|
|
243
|
+
* Relay operations from another session to this client.
|
|
244
|
+
* Only relays if the session is in streaming state and transport is connected.
|
|
245
|
+
*/
|
|
246
|
+
relayOperations(operations: Operation[]): void;
|
|
247
|
+
/**
|
|
248
|
+
* Close this session.
|
|
249
|
+
*/
|
|
250
|
+
close(reason?: string): void;
|
|
251
|
+
getState(): SessionState;
|
|
252
|
+
getSessionId(): string;
|
|
253
|
+
getClientNodeId(): string | null;
|
|
254
|
+
getAuthContext(): AuthContext | null;
|
|
255
|
+
isStreaming(): boolean;
|
|
256
|
+
private handleMessage;
|
|
257
|
+
private handleHandshake;
|
|
258
|
+
private handleOperationBatch;
|
|
259
|
+
private sendDelta;
|
|
260
|
+
private sendError;
|
|
261
|
+
private setSerializerWireFormat;
|
|
262
|
+
private handleTransportClose;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Minimal interface for a ws.WebSocketServer instance.
|
|
267
|
+
* Allows dependency injection for testing without importing ws directly.
|
|
268
|
+
*/
|
|
269
|
+
interface WsServerLike {
|
|
270
|
+
on(event: string, listener: (...args: unknown[]) => void): void;
|
|
271
|
+
close(callback?: (err?: Error) => void): void;
|
|
272
|
+
address(): {
|
|
273
|
+
port: number;
|
|
274
|
+
} | string | null;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Constructor type for creating a WebSocket server.
|
|
278
|
+
*/
|
|
279
|
+
type WsServerConstructor = new (options: {
|
|
280
|
+
port?: number;
|
|
281
|
+
host?: string;
|
|
282
|
+
path?: string;
|
|
283
|
+
}) => WsServerLike;
|
|
284
|
+
/**
|
|
285
|
+
* Self-hosted sync server. Accepts WebSocket connections from clients,
|
|
286
|
+
* handles the sync protocol, stores operations, and relays changes
|
|
287
|
+
* between connected clients.
|
|
288
|
+
*
|
|
289
|
+
* Two modes of operation:
|
|
290
|
+
* 1. **Standalone**: Call `start()` with a port — creates its own WebSocket server.
|
|
291
|
+
* 2. **Attach**: Call `handleConnection(transport)` — attach to an existing HTTP server.
|
|
292
|
+
*/
|
|
293
|
+
declare class KoraSyncServer {
|
|
294
|
+
private readonly store;
|
|
295
|
+
private readonly auth;
|
|
296
|
+
private readonly serializer;
|
|
297
|
+
private readonly emitter;
|
|
298
|
+
private readonly maxConnections;
|
|
299
|
+
private readonly batchSize;
|
|
300
|
+
private readonly schemaVersion;
|
|
301
|
+
private readonly port;
|
|
302
|
+
private readonly host;
|
|
303
|
+
private readonly path;
|
|
304
|
+
private readonly sessions;
|
|
305
|
+
private readonly httpClients;
|
|
306
|
+
private readonly httpSessionToClient;
|
|
307
|
+
private wsServer;
|
|
308
|
+
private running;
|
|
309
|
+
constructor(config: KoraSyncServerConfig);
|
|
310
|
+
/**
|
|
311
|
+
* Start the WebSocket server in standalone mode.
|
|
312
|
+
*
|
|
313
|
+
* @param wsServerImpl - Optional WebSocket server constructor for testing
|
|
314
|
+
*/
|
|
315
|
+
start(wsServerImpl?: WsServerConstructor): Promise<void>;
|
|
316
|
+
/**
|
|
317
|
+
* Stop the server. Closes all sessions and the WebSocket server.
|
|
318
|
+
*/
|
|
319
|
+
stop(): Promise<void>;
|
|
320
|
+
/**
|
|
321
|
+
* Handle one HTTP sync request for a long-polling client.
|
|
322
|
+
*
|
|
323
|
+
* A stable `clientId` identifies the logical connection across requests.
|
|
324
|
+
*/
|
|
325
|
+
handleHttpRequest(request: HttpSyncRequest): Promise<HttpSyncResponse>;
|
|
326
|
+
/**
|
|
327
|
+
* Handle an incoming client connection (attach mode).
|
|
328
|
+
* Creates a new ClientSession for the transport.
|
|
329
|
+
*
|
|
330
|
+
* @param transport - The server transport for the new connection
|
|
331
|
+
* @returns The session ID
|
|
332
|
+
*/
|
|
333
|
+
handleConnection(transport: ServerTransport): string;
|
|
334
|
+
/**
|
|
335
|
+
* Get the current server status.
|
|
336
|
+
*/
|
|
337
|
+
getStatus(): Promise<ServerStatus>;
|
|
338
|
+
/**
|
|
339
|
+
* Get the number of currently connected clients.
|
|
340
|
+
*/
|
|
341
|
+
getConnectionCount(): number;
|
|
342
|
+
private handleRelay;
|
|
343
|
+
private handleSessionClose;
|
|
344
|
+
private getOrCreateHttpClient;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Options for creating a TokenAuthProvider.
|
|
349
|
+
*/
|
|
350
|
+
interface TokenAuthProviderOptions {
|
|
351
|
+
/**
|
|
352
|
+
* Validate a token and return an AuthContext if valid, or null if rejected.
|
|
353
|
+
* This is where you implement your auth logic (JWT verification, database lookup, etc.).
|
|
354
|
+
*/
|
|
355
|
+
validate: (token: string) => Promise<AuthContext | null>;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Token-based auth provider that delegates validation to a user-provided function.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const auth = new TokenAuthProvider({
|
|
363
|
+
* validate: async (token) => {
|
|
364
|
+
* const user = await verifyJWT(token)
|
|
365
|
+
* return user ? { userId: user.id } : null
|
|
366
|
+
* }
|
|
367
|
+
* })
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
declare class TokenAuthProvider implements AuthProvider {
|
|
371
|
+
private readonly validate;
|
|
372
|
+
constructor(options: TokenAuthProviderOptions);
|
|
373
|
+
authenticate(token: string): Promise<AuthContext | null>;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* In-memory server store for testing and quick prototyping.
|
|
378
|
+
* Not suitable for production — data does not survive process restart.
|
|
379
|
+
*/
|
|
380
|
+
declare class MemoryServerStore implements ServerStore {
|
|
381
|
+
private readonly nodeId;
|
|
382
|
+
private readonly operations;
|
|
383
|
+
private readonly operationIndex;
|
|
384
|
+
private readonly versionVector;
|
|
385
|
+
private closed;
|
|
386
|
+
constructor(nodeId?: string);
|
|
387
|
+
getVersionVector(): VersionVector;
|
|
388
|
+
getNodeId(): string;
|
|
389
|
+
applyRemoteOperation(op: Operation): Promise<ApplyResult>;
|
|
390
|
+
getOperationRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]>;
|
|
391
|
+
getOperationCount(): Promise<number>;
|
|
392
|
+
close(): Promise<void>;
|
|
393
|
+
/**
|
|
394
|
+
* Get all stored operations (for test assertions).
|
|
395
|
+
*/
|
|
396
|
+
getAllOperations(): Operation[];
|
|
397
|
+
private assertOpen;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* PostgreSQL-backed server store using Drizzle ORM.
|
|
402
|
+
* All reads and writes go through Drizzle's typed query builder.
|
|
403
|
+
* DDL stays as raw SQL via Drizzle's sql template (standard practice without drizzle-kit).
|
|
404
|
+
*/
|
|
405
|
+
declare class PostgresServerStore implements ServerStore {
|
|
406
|
+
private readonly nodeId;
|
|
407
|
+
private readonly db;
|
|
408
|
+
private readonly versionVector;
|
|
409
|
+
private readonly ready;
|
|
410
|
+
private closed;
|
|
411
|
+
constructor(db: PostgresJsDatabase, nodeId?: string);
|
|
412
|
+
getVersionVector(): VersionVector;
|
|
413
|
+
getNodeId(): string;
|
|
414
|
+
applyRemoteOperation(op: Operation): Promise<ApplyResult>;
|
|
415
|
+
getOperationRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]>;
|
|
416
|
+
getOperationCount(): Promise<number>;
|
|
417
|
+
close(): Promise<void>;
|
|
418
|
+
private initialize;
|
|
419
|
+
/**
|
|
420
|
+
* Create tables if they don't exist.
|
|
421
|
+
* Uses raw SQL via Drizzle's sql template — standard DDL practice without drizzle-kit.
|
|
422
|
+
*/
|
|
423
|
+
private ensureTables;
|
|
424
|
+
private serializeOperation;
|
|
425
|
+
private deserializeOperation;
|
|
426
|
+
private assertOpen;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Creates a PostgresServerStore from a PostgreSQL connection string.
|
|
430
|
+
*
|
|
431
|
+
* Uses runtime dynamic imports so projects that do not use PostgreSQL
|
|
432
|
+
* do not need to install `postgres`. Wraps the postgres client with
|
|
433
|
+
* Drizzle ORM for typed query building.
|
|
434
|
+
*/
|
|
435
|
+
declare function createPostgresServerStore(options: {
|
|
436
|
+
connectionString: string;
|
|
437
|
+
nodeId?: string;
|
|
438
|
+
}): Promise<PostgresServerStore>;
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* SQLite-backed server store using Drizzle ORM.
|
|
442
|
+
* Persists operations and version vectors to a real database file,
|
|
443
|
+
* surviving process restarts.
|
|
444
|
+
*/
|
|
445
|
+
declare class SqliteServerStore implements ServerStore {
|
|
446
|
+
private readonly nodeId;
|
|
447
|
+
private readonly db;
|
|
448
|
+
private closed;
|
|
449
|
+
constructor(db: BetterSQLite3Database, nodeId?: string);
|
|
450
|
+
getVersionVector(): VersionVector;
|
|
451
|
+
getNodeId(): string;
|
|
452
|
+
applyRemoteOperation(op: Operation): Promise<ApplyResult>;
|
|
453
|
+
getOperationRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]>;
|
|
454
|
+
getOperationCount(): Promise<number>;
|
|
455
|
+
close(): Promise<void>;
|
|
456
|
+
/**
|
|
457
|
+
* Create the operations and sync_state tables if they don't exist.
|
|
458
|
+
* Uses raw SQL via Drizzle's sql template — standard practice for DDL without drizzle-kit.
|
|
459
|
+
*/
|
|
460
|
+
private ensureTables;
|
|
461
|
+
private serializeOperation;
|
|
462
|
+
private deserializeOperation;
|
|
463
|
+
private assertOpen;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Creates a SqliteServerStore with a file-backed or in-memory database.
|
|
467
|
+
* Handles database creation, Drizzle wrapping, and table setup.
|
|
468
|
+
*
|
|
469
|
+
* @param options - Configuration options
|
|
470
|
+
* @param options.filename - Path to SQLite database file. Defaults to ':memory:' for testing.
|
|
471
|
+
* @param options.nodeId - Server node ID. Auto-generated if not provided.
|
|
472
|
+
* @returns A ready-to-use SqliteServerStore
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* import { createSqliteServerStore } from '@korajs/server'
|
|
477
|
+
*
|
|
478
|
+
* const store = createSqliteServerStore({ filename: './kora-server.db' })
|
|
479
|
+
* const server = createKoraServer({ store, port: 3001 })
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
declare function createSqliteServerStore(options: {
|
|
483
|
+
filename?: string;
|
|
484
|
+
nodeId?: string;
|
|
485
|
+
}): SqliteServerStore;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Auth provider that accepts all connections.
|
|
489
|
+
* Returns a default anonymous context for any token.
|
|
490
|
+
* Useful for development and testing.
|
|
491
|
+
*/
|
|
492
|
+
declare class NoAuthProvider implements AuthProvider {
|
|
493
|
+
authenticate(_token: string): Promise<AuthContext>;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Factory function to create a KoraSyncServer.
|
|
498
|
+
*
|
|
499
|
+
* @param config - Server configuration
|
|
500
|
+
* @returns A new KoraSyncServer instance
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```typescript
|
|
504
|
+
* const server = createKoraServer({
|
|
505
|
+
* store: new MemoryServerStore(),
|
|
506
|
+
* port: 3000,
|
|
507
|
+
* })
|
|
508
|
+
* await server.start()
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
declare function createKoraServer(config: KoraSyncServerConfig): KoraSyncServer;
|
|
512
|
+
|
|
513
|
+
export { type AuthContext, type AuthProvider, ClientSession, type ClientSessionOptions, type HttpPollResponse, HttpServerTransport, type HttpSyncRequest, type HttpSyncResponse, KoraSyncServer, type KoraSyncServerConfig, MemoryServerStore, NoAuthProvider, PostgresServerStore, type RelayCallback, ServerCloseHandler, ServerErrorHandler, ServerMessageHandler, type ServerStatus, type ServerStore, ServerTransport, type SessionState, SqliteServerStore, TokenAuthProvider, type TokenAuthProviderOptions, type WsServerConstructor, type WsServerLike, WsServerTransport, type WsServerTransportOptions, type WsWebSocket, createKoraServer, createPostgresServerStore, createSqliteServerStore };
|