@quereus/sync-coordinator 0.3.4 → 0.3.6
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/README.md +7 -1
- package/package.json +3 -3
- package/dist/src/bin/sync-coordinator.d.ts +0 -6
- package/dist/src/bin/sync-coordinator.d.ts.map +0 -1
- package/dist/src/bin/sync-coordinator.js +0 -85
- package/dist/src/bin/sync-coordinator.js.map +0 -1
- package/dist/src/common/index.d.ts +0 -5
- package/dist/src/common/index.d.ts.map +0 -1
- package/dist/src/common/index.js +0 -5
- package/dist/src/common/index.js.map +0 -1
- package/dist/src/common/logger.d.ts +0 -20
- package/dist/src/common/logger.d.ts.map +0 -1
- package/dist/src/common/logger.js +0 -24
- package/dist/src/common/logger.js.map +0 -1
- package/dist/src/config/index.d.ts +0 -6
- package/dist/src/config/index.d.ts.map +0 -1
- package/dist/src/config/index.js +0 -6
- package/dist/src/config/index.js.map +0 -1
- package/dist/src/config/loader.d.ts +0 -27
- package/dist/src/config/loader.d.ts.map +0 -1
- package/dist/src/config/loader.js +0 -144
- package/dist/src/config/loader.js.map +0 -1
- package/dist/src/config/types.d.ts +0 -74
- package/dist/src/config/types.d.ts.map +0 -1
- package/dist/src/config/types.js +0 -27
- package/dist/src/config/types.js.map +0 -1
- package/dist/src/index.d.ts +0 -21
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -26
- package/dist/src/index.js.map +0 -1
- package/dist/src/metrics/coordinator-metrics.d.ts +0 -27
- package/dist/src/metrics/coordinator-metrics.d.ts.map +0 -1
- package/dist/src/metrics/coordinator-metrics.js +0 -57
- package/dist/src/metrics/coordinator-metrics.js.map +0 -1
- package/dist/src/metrics/index.d.ts +0 -7
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -7
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/registry.d.ts +0 -53
- package/dist/src/metrics/registry.d.ts.map +0 -1
- package/dist/src/metrics/registry.js +0 -158
- package/dist/src/metrics/registry.js.map +0 -1
- package/dist/src/metrics/types.d.ts +0 -62
- package/dist/src/metrics/types.d.ts.map +0 -1
- package/dist/src/metrics/types.js +0 -16
- package/dist/src/metrics/types.js.map +0 -1
- package/dist/src/server/index.d.ts +0 -7
- package/dist/src/server/index.d.ts.map +0 -1
- package/dist/src/server/index.js +0 -7
- package/dist/src/server/index.js.map +0 -1
- package/dist/src/server/routes.d.ts +0 -10
- package/dist/src/server/routes.d.ts.map +0 -1
- package/dist/src/server/routes.js +0 -153
- package/dist/src/server/routes.js.map +0 -1
- package/dist/src/server/server.d.ts +0 -34
- package/dist/src/server/server.d.ts.map +0 -1
- package/dist/src/server/server.js +0 -57
- package/dist/src/server/server.js.map +0 -1
- package/dist/src/server/websocket.d.ts +0 -10
- package/dist/src/server/websocket.d.ts.map +0 -1
- package/dist/src/server/websocket.js +0 -159
- package/dist/src/server/websocket.js.map +0 -1
- package/dist/src/service/coordinator-service.d.ts +0 -114
- package/dist/src/service/coordinator-service.d.ts.map +0 -1
- package/dist/src/service/coordinator-service.js +0 -373
- package/dist/src/service/coordinator-service.js.map +0 -1
- package/dist/src/service/index.d.ts +0 -6
- package/dist/src/service/index.d.ts.map +0 -1
- package/dist/src/service/index.js +0 -5
- package/dist/src/service/index.js.map +0 -1
- package/dist/src/service/types.d.ts +0 -142
- package/dist/src/service/types.d.ts.map +0 -1
- package/dist/src/service/types.js +0 -5
- package/dist/src/service/types.js.map +0 -1
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fastify server setup for sync-coordinator.
|
|
3
|
-
*/
|
|
4
|
-
import Fastify from 'fastify';
|
|
5
|
-
import fastifyCors from '@fastify/cors';
|
|
6
|
-
import fastifyWebsocket from '@fastify/websocket';
|
|
7
|
-
import { CoordinatorService } from '../service/coordinator-service.js';
|
|
8
|
-
import { registerRoutes } from './routes.js';
|
|
9
|
-
import { registerWebSocket } from './websocket.js';
|
|
10
|
-
import { serverLog } from '../common/logger.js';
|
|
11
|
-
/**
|
|
12
|
-
* Create a coordinator server.
|
|
13
|
-
*/
|
|
14
|
-
export async function createCoordinatorServer(options) {
|
|
15
|
-
const { config, hooks } = options;
|
|
16
|
-
serverLog('Creating coordinator server');
|
|
17
|
-
// Create Fastify instance
|
|
18
|
-
const app = Fastify({
|
|
19
|
-
logger: config.logging.level === 'debug',
|
|
20
|
-
});
|
|
21
|
-
// Register CORS
|
|
22
|
-
await app.register(fastifyCors, {
|
|
23
|
-
origin: config.cors.origin,
|
|
24
|
-
credentials: config.cors.credentials,
|
|
25
|
-
});
|
|
26
|
-
// Register WebSocket support
|
|
27
|
-
await app.register(fastifyWebsocket);
|
|
28
|
-
// Create service
|
|
29
|
-
const serviceOptions = {
|
|
30
|
-
config,
|
|
31
|
-
hooks,
|
|
32
|
-
};
|
|
33
|
-
const service = new CoordinatorService(serviceOptions);
|
|
34
|
-
// Initialize service
|
|
35
|
-
await service.initialize();
|
|
36
|
-
// Register routes
|
|
37
|
-
registerRoutes(app, service, config.basePath);
|
|
38
|
-
registerWebSocket(app, service, config.basePath);
|
|
39
|
-
serverLog('Routes registered at %s', config.basePath);
|
|
40
|
-
// Server control
|
|
41
|
-
const start = async () => {
|
|
42
|
-
const address = await app.listen({
|
|
43
|
-
host: config.host,
|
|
44
|
-
port: config.port,
|
|
45
|
-
});
|
|
46
|
-
serverLog('Server listening at %s', address);
|
|
47
|
-
console.log(`Sync coordinator listening at ${address}${config.basePath}`);
|
|
48
|
-
};
|
|
49
|
-
const stop = async () => {
|
|
50
|
-
serverLog('Stopping server');
|
|
51
|
-
await app.close();
|
|
52
|
-
await service.shutdown();
|
|
53
|
-
serverLog('Server stopped');
|
|
54
|
-
};
|
|
55
|
-
return { app, service, start, stop };
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=server.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],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M,CAAC,QAAQ,CAAC,CAAC;IAEjD,SAAS,CAAC,yBAAyB,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEtD,iBAAiB;IACjB,MAAM,KAAK,GAAG,KAAK,IAAI,EAAE;QACvB,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC;YAC/B,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,IAAI,EAAE,MAAM,CAAC,IAAI;SAClB,CAAC,CAAC;QACH,SAAS,CAAC,wBAAwB,EAAE,OAAO,CAAC,CAAC;QAC7C,OAAO,CAAC,GAAG,CAAC,iCAAiC,OAAO,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5E,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE;QACtB,SAAS,CAAC,iBAAiB,CAAC,CAAC;QAC7B,MAAM,GAAG,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC;QACzB,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAC9B,CAAC,CAAC;IAEF,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebSocket handler for real-time sync.
|
|
3
|
-
*/
|
|
4
|
-
import type { FastifyInstance } from 'fastify';
|
|
5
|
-
import type { CoordinatorService } from '../service/coordinator-service.js';
|
|
6
|
-
/**
|
|
7
|
-
* Register WebSocket handler.
|
|
8
|
-
*/
|
|
9
|
-
export declare function registerWebSocket(app: FastifyInstance, service: CoordinatorService, basePath: string): void;
|
|
10
|
-
//# sourceMappingURL=websocket.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/server/websocket.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,SAAS,CAAC;AAW/D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAiD5E;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,eAAe,EACpB,OAAO,EAAE,kBAAkB,EAC3B,QAAQ,EAAE,MAAM,GACf,IAAI,CAsIN"}
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WebSocket handler for real-time sync.
|
|
3
|
-
*/
|
|
4
|
-
import { siteIdFromBase64, siteIdToBase64, deserializeHLC, serializeHLC, } from '@quereus/plugin-sync';
|
|
5
|
-
import { wsLog } from '../common/logger.js';
|
|
6
|
-
// ============================================================================
|
|
7
|
-
// WebSocket Handler
|
|
8
|
-
// ============================================================================
|
|
9
|
-
/**
|
|
10
|
-
* Register WebSocket handler.
|
|
11
|
-
*/
|
|
12
|
-
export function registerWebSocket(app, service, basePath) {
|
|
13
|
-
app.get(`${basePath}/ws`, { websocket: true }, (socket, request) => {
|
|
14
|
-
wsLog('New WebSocket connection from %s', request.ip);
|
|
15
|
-
let session = null;
|
|
16
|
-
const sendError = (code, message) => {
|
|
17
|
-
socket.send(JSON.stringify({ type: 'error', code, message }));
|
|
18
|
-
};
|
|
19
|
-
const sendMessage = (msg) => {
|
|
20
|
-
socket.send(JSON.stringify(msg));
|
|
21
|
-
};
|
|
22
|
-
socket.on('message', async (data) => {
|
|
23
|
-
try {
|
|
24
|
-
const message = JSON.parse(data.toString());
|
|
25
|
-
wsLog('Received message: %s', message.type);
|
|
26
|
-
switch (message.type) {
|
|
27
|
-
case 'handshake':
|
|
28
|
-
await handleHandshake(message);
|
|
29
|
-
break;
|
|
30
|
-
case 'get_changes':
|
|
31
|
-
await handleGetChanges(message);
|
|
32
|
-
break;
|
|
33
|
-
case 'apply_changes':
|
|
34
|
-
await handleApplyChanges(message);
|
|
35
|
-
break;
|
|
36
|
-
case 'get_snapshot':
|
|
37
|
-
await handleGetSnapshot();
|
|
38
|
-
break;
|
|
39
|
-
case 'ping':
|
|
40
|
-
sendMessage({ type: 'pong' });
|
|
41
|
-
break;
|
|
42
|
-
default:
|
|
43
|
-
sendError('UNKNOWN_MESSAGE', `Unknown message type: ${message.type}`);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
const msg = err instanceof Error ? err.message : 'Message processing failed';
|
|
48
|
-
wsLog('Message error: %s', msg);
|
|
49
|
-
sendError('MESSAGE_ERROR', msg);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
socket.on('close', () => {
|
|
53
|
-
wsLog('WebSocket closed: %s', session?.connectionId?.slice(0, 8) || 'no-session');
|
|
54
|
-
if (session) {
|
|
55
|
-
service.unregisterSession(session.connectionId);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
socket.on('error', (err) => {
|
|
59
|
-
wsLog('WebSocket error: %O', err);
|
|
60
|
-
});
|
|
61
|
-
// Handler functions
|
|
62
|
-
async function handleHandshake(msg) {
|
|
63
|
-
if (session) {
|
|
64
|
-
sendError('ALREADY_AUTHENTICATED', 'Already authenticated');
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
try {
|
|
68
|
-
const identity = await service.authenticate({
|
|
69
|
-
token: msg.token,
|
|
70
|
-
siteIdRaw: msg.siteId,
|
|
71
|
-
siteId: siteIdFromBase64(msg.siteId),
|
|
72
|
-
socket,
|
|
73
|
-
});
|
|
74
|
-
session = await service.registerSession(socket, identity);
|
|
75
|
-
sendMessage({
|
|
76
|
-
type: 'handshake_ack',
|
|
77
|
-
serverSiteId: siteIdToBase64(service.getSiteId()),
|
|
78
|
-
connectionId: session.connectionId,
|
|
79
|
-
});
|
|
80
|
-
wsLog('Handshake complete: %s', session.connectionId.slice(0, 8));
|
|
81
|
-
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
const msg = err instanceof Error ? err.message : 'Authentication failed';
|
|
84
|
-
sendError('AUTH_FAILED', msg);
|
|
85
|
-
socket.close(4001, 'Authentication failed');
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
async function handleGetChanges(msg) {
|
|
89
|
-
if (!session) {
|
|
90
|
-
sendError('NOT_AUTHENTICATED', 'Must handshake first');
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
let sinceHLC;
|
|
94
|
-
if (msg.sinceHLC) {
|
|
95
|
-
sinceHLC = deserializeHLC(Buffer.from(msg.sinceHLC, 'base64'));
|
|
96
|
-
}
|
|
97
|
-
const changes = await service.getChangesSince(session.identity, sinceHLC);
|
|
98
|
-
// Serialize for JSON transport
|
|
99
|
-
const serializedChanges = changes.map(cs => serializeChangeSet(cs));
|
|
100
|
-
sendMessage({ type: 'changes', changeSets: serializedChanges });
|
|
101
|
-
}
|
|
102
|
-
async function handleApplyChanges(msg) {
|
|
103
|
-
if (!session) {
|
|
104
|
-
sendError('NOT_AUTHENTICATED', 'Must handshake first');
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
// Deserialize from JSON transport
|
|
108
|
-
const changes = msg.changes.map(cs => deserializeChangeSet(cs));
|
|
109
|
-
const result = await service.applyChanges(session.identity, changes);
|
|
110
|
-
sendMessage({ type: 'apply_result', ...result });
|
|
111
|
-
}
|
|
112
|
-
async function handleGetSnapshot() {
|
|
113
|
-
if (!session) {
|
|
114
|
-
sendError('NOT_AUTHENTICATED', 'Must handshake first');
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
// Stream snapshot chunks
|
|
118
|
-
for await (const chunk of service.getSnapshotStream(session.identity)) {
|
|
119
|
-
sendMessage({ ...chunk, type: 'snapshot_chunk' });
|
|
120
|
-
}
|
|
121
|
-
sendMessage({ type: 'snapshot_complete' });
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// Serialization Helpers
|
|
127
|
-
// ============================================================================
|
|
128
|
-
function serializeChangeSet(cs) {
|
|
129
|
-
return {
|
|
130
|
-
siteId: siteIdToBase64(cs.siteId),
|
|
131
|
-
transactionId: cs.transactionId,
|
|
132
|
-
hlc: Buffer.from(serializeHLC(cs.hlc)).toString('base64'),
|
|
133
|
-
changes: cs.changes.map(c => ({
|
|
134
|
-
...c,
|
|
135
|
-
hlc: Buffer.from(serializeHLC(c.hlc)).toString('base64'),
|
|
136
|
-
})),
|
|
137
|
-
schemaMigrations: cs.schemaMigrations.map(m => ({
|
|
138
|
-
...m,
|
|
139
|
-
hlc: Buffer.from(serializeHLC(m.hlc)).toString('base64'),
|
|
140
|
-
})),
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
function deserializeChangeSet(cs) {
|
|
144
|
-
const obj = cs;
|
|
145
|
-
return {
|
|
146
|
-
siteId: siteIdFromBase64(obj.siteId),
|
|
147
|
-
transactionId: obj.transactionId,
|
|
148
|
-
hlc: deserializeHLC(Buffer.from(obj.hlc, 'base64')),
|
|
149
|
-
changes: obj.changes.map(c => ({
|
|
150
|
-
...c,
|
|
151
|
-
hlc: deserializeHLC(Buffer.from(c.hlc, 'base64')),
|
|
152
|
-
})),
|
|
153
|
-
schemaMigrations: (obj.schemaMigrations || []).map(m => ({
|
|
154
|
-
...m,
|
|
155
|
-
hlc: deserializeHLC(Buffer.from(m.hlc, 'base64')),
|
|
156
|
-
})),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
//# sourceMappingURL=websocket.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../../../src/server/websocket.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,cAAc,EACd,YAAY,GAIb,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AA2C5C,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,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AAE,CAAC,CAAC;YACtF,GAAG,CAAC;YACJ,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,GAAa,EAAE,QAAQ,CAAC,CAAC;SAC5D,CAAC,CAAC;KACS,CAAC;AACjB,CAAC"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CoordinatorService - Main service layer for sync coordination.
|
|
3
|
-
*
|
|
4
|
-
* Wraps SyncManager with validation hooks and client session management.
|
|
5
|
-
*/
|
|
6
|
-
import type { WebSocket } from 'ws';
|
|
7
|
-
import { type HLC, type SiteId, type ChangeSet, type ApplyResult, type SnapshotChunk } from '@quereus/plugin-sync';
|
|
8
|
-
import type { CoordinatorConfig } from '../config/types.js';
|
|
9
|
-
import { type CoordinatorMetrics } from '../metrics/index.js';
|
|
10
|
-
import type { ClientIdentity, ClientSession, AuthContext, SyncOperation, CoordinatorHooks } from './types.js';
|
|
11
|
-
/**
|
|
12
|
-
* Options for creating a CoordinatorService.
|
|
13
|
-
*/
|
|
14
|
-
export interface CoordinatorServiceOptions {
|
|
15
|
-
/** Full configuration */
|
|
16
|
-
config: CoordinatorConfig;
|
|
17
|
-
/** Custom hooks for validation/auth */
|
|
18
|
-
hooks?: CoordinatorHooks;
|
|
19
|
-
/** Custom metrics (uses global registry if not provided) */
|
|
20
|
-
metrics?: CoordinatorMetrics;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Coordinator service that manages sync operations with hooks.
|
|
24
|
-
*/
|
|
25
|
-
export declare class CoordinatorService {
|
|
26
|
-
private readonly config;
|
|
27
|
-
private readonly hooks;
|
|
28
|
-
private readonly metrics;
|
|
29
|
-
private syncManager;
|
|
30
|
-
private kvStore;
|
|
31
|
-
/** Active WebSocket sessions by connection ID */
|
|
32
|
-
private readonly sessions;
|
|
33
|
-
/** Connection IDs by site ID for broadcasting */
|
|
34
|
-
private readonly siteIdToConnections;
|
|
35
|
-
private initialized;
|
|
36
|
-
constructor(options: CoordinatorServiceOptions);
|
|
37
|
-
/**
|
|
38
|
-
* Initialize the service (open store, create sync manager).
|
|
39
|
-
*/
|
|
40
|
-
initialize(): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* Shutdown the service.
|
|
43
|
-
*/
|
|
44
|
-
shutdown(): Promise<void>;
|
|
45
|
-
/**
|
|
46
|
-
* Authenticate a request/connection.
|
|
47
|
-
*/
|
|
48
|
-
authenticate(context: AuthContext): Promise<ClientIdentity>;
|
|
49
|
-
/**
|
|
50
|
-
* Authorize an operation for a client.
|
|
51
|
-
*/
|
|
52
|
-
authorize(client: ClientIdentity, operation: SyncOperation): Promise<boolean>;
|
|
53
|
-
/**
|
|
54
|
-
* Get this coordinator's site ID.
|
|
55
|
-
*/
|
|
56
|
-
getSiteId(): SiteId;
|
|
57
|
-
/**
|
|
58
|
-
* Get current HLC.
|
|
59
|
-
*/
|
|
60
|
-
getCurrentHLC(): HLC;
|
|
61
|
-
/**
|
|
62
|
-
* Get changes since a given HLC for a client.
|
|
63
|
-
*/
|
|
64
|
-
getChangesSince(client: ClientIdentity, sinceHLC?: HLC): Promise<ChangeSet[]>;
|
|
65
|
-
/**
|
|
66
|
-
* Apply changes from a client.
|
|
67
|
-
*/
|
|
68
|
-
applyChanges(client: ClientIdentity, changes: ChangeSet[]): Promise<ApplyResult>;
|
|
69
|
-
/**
|
|
70
|
-
* Stream a full snapshot.
|
|
71
|
-
*/
|
|
72
|
-
getSnapshotStream(client: ClientIdentity, chunkSize?: number): AsyncIterable<SnapshotChunk>;
|
|
73
|
-
/**
|
|
74
|
-
* Check if delta sync is possible.
|
|
75
|
-
*/
|
|
76
|
-
canDeltaSync(client: ClientIdentity, sinceHLC: HLC): Promise<boolean>;
|
|
77
|
-
/**
|
|
78
|
-
* Register a new WebSocket client session.
|
|
79
|
-
*/
|
|
80
|
-
registerSession(socket: WebSocket, identity: ClientIdentity): Promise<ClientSession>;
|
|
81
|
-
/**
|
|
82
|
-
* Unregister a WebSocket client session.
|
|
83
|
-
*/
|
|
84
|
-
unregisterSession(connectionId: string): void;
|
|
85
|
-
/**
|
|
86
|
-
* Get a session by connection ID.
|
|
87
|
-
*/
|
|
88
|
-
getSession(connectionId: string): ClientSession | undefined;
|
|
89
|
-
/**
|
|
90
|
-
* Update the last sync HLC for a session.
|
|
91
|
-
*/
|
|
92
|
-
updateSessionSyncState(connectionId: string, hlc: HLC): void;
|
|
93
|
-
/**
|
|
94
|
-
* Broadcast changes to all connected clients except the sender.
|
|
95
|
-
*/
|
|
96
|
-
private broadcastChanges;
|
|
97
|
-
/**
|
|
98
|
-
* Serialize a ChangeSet for JSON transport.
|
|
99
|
-
*/
|
|
100
|
-
private serializeChangeSet;
|
|
101
|
-
/**
|
|
102
|
-
* Get server status and stats.
|
|
103
|
-
*/
|
|
104
|
-
getStatus(): {
|
|
105
|
-
siteId: string;
|
|
106
|
-
connectedClients: number;
|
|
107
|
-
uptime: number;
|
|
108
|
-
};
|
|
109
|
-
/**
|
|
110
|
-
* Get the metrics registry for this service.
|
|
111
|
-
*/
|
|
112
|
-
getMetrics(): CoordinatorMetrics;
|
|
113
|
-
}
|
|
114
|
-
//# sourceMappingURL=coordinator-service.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"coordinator-service.d.ts","sourceRoot":"","sources":["../../../src/service/coordinator-service.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAEpC,OAAO,EAGL,KAAK,GAAG,EACR,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,aAAa,EAKnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,WA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