@sirosfoundation/wmp-js 0.2.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/peer.d.ts ADDED
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Peer — the core WMP client/server dispatcher.
3
+ * Mirrors go-wmp/pkg/wmp/peer.go.
4
+ *
5
+ * Handles incoming messages by dispatching to handlers/profiles,
6
+ * and provides methods to send outgoing requests and notifications.
7
+ */
8
+ import { type Transport } from "./transport.js";
9
+ import { type Profile, type PeerContext } from "./profile.js";
10
+ import { type SessionCreateParams, type SessionCreateResult, type SessionCloseParams, type FlowStartParams, type FlowStartResult, type FlowProgressParams, type FlowActionParams, type FlowActionResult, type FlowCompleteParams, type FlowErrorParams, type FlowCancelParams, type FlowCancelResult, type ResolveParams, type ResolveResult, type Capabilities, type SecurityMode } from "./types.js";
11
+ /**
12
+ * Handler processes incoming WMP method calls.
13
+ * All methods are optional — unimplemented methods return "method not found".
14
+ */
15
+ export interface Handler {
16
+ onSessionCreate?(params: SessionCreateParams): Promise<SessionCreateResult>;
17
+ onSessionClose?(params: SessionCloseParams): void;
18
+ onFlowStart?(params: FlowStartParams): Promise<FlowStartResult>;
19
+ onFlowProgress?(params: FlowProgressParams): void;
20
+ onFlowAction?(params: FlowActionParams): Promise<FlowActionResult>;
21
+ onFlowComplete?(params: FlowCompleteParams): void;
22
+ onFlowError?(params: FlowErrorParams): void;
23
+ onFlowCancel?(params: FlowCancelParams): Promise<FlowCancelResult>;
24
+ onResolve?(params: ResolveParams): Promise<ResolveResult>;
25
+ }
26
+ export interface PeerOptions {
27
+ /** Default timeout for RPC calls in ms. Default: 30000 */
28
+ callTimeout?: number;
29
+ /** Handler for incoming method calls. */
30
+ handler?: Handler;
31
+ }
32
+ export declare class Peer implements PeerContext {
33
+ private transport;
34
+ private handler;
35
+ private registry;
36
+ private pending;
37
+ private callTimeout;
38
+ private sessionId?;
39
+ private closed;
40
+ constructor(transport: Transport, opts?: PeerOptions);
41
+ /** Register a profile. Call before starting to receive messages. */
42
+ use(profile: Profile): void;
43
+ /** The negotiated session ID (set after session.create). */
44
+ get session(): string | undefined;
45
+ notify(method: string, params: unknown): Promise<void>;
46
+ call<T = unknown>(method: string, params: unknown): Promise<T>;
47
+ /** Initiate a WMP session. */
48
+ createSession(opts: {
49
+ participants?: string[];
50
+ capabilities?: Capabilities;
51
+ security?: SecurityMode;
52
+ auth?: {
53
+ type: string;
54
+ token?: string;
55
+ [key: string]: unknown;
56
+ };
57
+ sender?: string;
58
+ ttl?: number;
59
+ }): Promise<SessionCreateResult>;
60
+ /**
61
+ * Bind a session ID to the underlying transport. For HttpSseTransport,
62
+ * this sets the Wmp-Session-Id header and session_id query parameter.
63
+ */
64
+ private bindSessionToTransport;
65
+ /** Close the current session. */
66
+ closeSession(reason?: string): Promise<void>;
67
+ /** Start a flow. */
68
+ startFlow(flowType: string, flowId: string, params?: unknown, timeout?: number): Promise<FlowStartResult>;
69
+ /** Send a flow progress notification. */
70
+ flowProgress(flowId: string, step: string, payload?: unknown): Promise<void>;
71
+ /** Send a flow action request and wait for result. */
72
+ flowAction(flowId: string, action: string, params?: unknown): Promise<FlowActionResult>;
73
+ /** Notify flow completion. */
74
+ flowComplete(flowId: string, result?: unknown): Promise<void>;
75
+ /** Notify flow error. */
76
+ flowError(flowId: string, code: number, message: string, data?: unknown): Promise<void>;
77
+ /** Cancel a flow. */
78
+ flowCancel(flowId: string, reason?: string): Promise<FlowCancelResult>;
79
+ resolve(type: string, uri: string, options?: unknown): Promise<ResolveResult>;
80
+ close(): void;
81
+ private dispatch;
82
+ private handleResponse;
83
+ private handleRequest;
84
+ private dispatchMethod;
85
+ private activeFlows;
86
+ /** Look up the FlowHandler for an active flow by flow_id. */
87
+ private flowHandlerForId;
88
+ /** Track a flow as active (called when flow.start succeeds). */
89
+ trackFlow(flowId: string, flowType: string): void;
90
+ /** Untrack a flow (called on flow.complete / flow.error / flow.cancel). */
91
+ untrackFlow(flowId: string): void;
92
+ private wmpMeta;
93
+ private onTransportError;
94
+ private onTransportClose;
95
+ }
package/dist/peer.js ADDED
@@ -0,0 +1,380 @@
1
+ /**
2
+ * Peer — the core WMP client/server dispatcher.
3
+ * Mirrors go-wmp/pkg/wmp/peer.go.
4
+ *
5
+ * Handles incoming messages by dispatching to handlers/profiles,
6
+ * and provides methods to send outgoing requests and notifications.
7
+ */
8
+ import { isRequest, isResponse, isNotification, createRequest, createNotification, createResponse, createErrorResponse, WMPError, } from "./jsonrpc.js";
9
+ import { Registry, } from "./profile.js";
10
+ import { ErrorCode, Method, VERSION, } from "./types.js";
11
+ // ---------------------------------------------------------------------------
12
+ // Peer
13
+ // ---------------------------------------------------------------------------
14
+ export class Peer {
15
+ transport;
16
+ handler;
17
+ registry = new Registry();
18
+ pending = new Map();
19
+ callTimeout;
20
+ sessionId;
21
+ closed = false;
22
+ constructor(transport, opts) {
23
+ this.transport = transport;
24
+ this.handler = opts?.handler ?? {};
25
+ this.callTimeout = opts?.callTimeout ?? 30_000;
26
+ // Wire up transport events.
27
+ this.transport.on("message", (msg) => this.dispatch(msg));
28
+ this.transport.on("error", (err) => this.onTransportError(err));
29
+ this.transport.on("close", () => this.onTransportClose());
30
+ }
31
+ /** Register a profile. Call before starting to receive messages. */
32
+ use(profile) {
33
+ this.registry.register(profile);
34
+ profile.init(this);
35
+ }
36
+ /** The negotiated session ID (set after session.create). */
37
+ get session() {
38
+ return this.sessionId;
39
+ }
40
+ // -------------------------------------------------------------------------
41
+ // PeerContext implementation (for profiles)
42
+ // -------------------------------------------------------------------------
43
+ async notify(method, params) {
44
+ const msg = createNotification(method, params);
45
+ await this.transport.send(msg);
46
+ }
47
+ async call(method, params) {
48
+ const req = createRequest(method, params);
49
+ const id = req.id;
50
+ return new Promise((resolve, reject) => {
51
+ const timer = setTimeout(() => {
52
+ this.pending.delete(id);
53
+ reject(new WMPError(ErrorCode.InternalError, `Call to ${method} timed out`));
54
+ }, this.callTimeout);
55
+ this.pending.set(id, {
56
+ resolve: resolve,
57
+ reject,
58
+ timer,
59
+ });
60
+ this.transport.send(req).catch((err) => {
61
+ this.pending.delete(id);
62
+ clearTimeout(timer);
63
+ reject(err);
64
+ });
65
+ });
66
+ }
67
+ // -------------------------------------------------------------------------
68
+ // Session convenience methods
69
+ // -------------------------------------------------------------------------
70
+ /** Initiate a WMP session. */
71
+ async createSession(opts) {
72
+ const wmp = { version: VERSION, sender: opts.sender };
73
+ const params = {
74
+ wmp,
75
+ participants: opts.participants,
76
+ capabilities_offered: opts.capabilities,
77
+ security: opts.security ?? { mode: "tls" },
78
+ ttl: opts.ttl,
79
+ auth: opts.auth,
80
+ };
81
+ const result = await this.call(Method.SessionCreate, params);
82
+ if (result.wmp?.session_id) {
83
+ this.sessionId = result.wmp.session_id;
84
+ // Propagate session ID to transport for header/URL binding.
85
+ this.bindSessionToTransport(result.wmp.session_id);
86
+ }
87
+ return result;
88
+ }
89
+ /**
90
+ * Bind a session ID to the underlying transport. For HttpSseTransport,
91
+ * this sets the Wmp-Session-Id header and session_id query parameter.
92
+ */
93
+ bindSessionToTransport(sessionId) {
94
+ // Duck-type check for HttpSseTransport's setSessionId method.
95
+ const t = this.transport;
96
+ if (typeof t.setSessionId === "function") {
97
+ t.setSessionId(sessionId);
98
+ }
99
+ }
100
+ /** Close the current session. */
101
+ async closeSession(reason = "complete") {
102
+ if (!this.sessionId)
103
+ return;
104
+ await this.notify(Method.SessionClose, {
105
+ wmp: { version: VERSION, session_id: this.sessionId },
106
+ reason,
107
+ });
108
+ this.sessionId = undefined;
109
+ }
110
+ // -------------------------------------------------------------------------
111
+ // Flow convenience methods
112
+ // -------------------------------------------------------------------------
113
+ /** Start a flow. */
114
+ async startFlow(flowType, flowId, params, timeout) {
115
+ return this.call(Method.FlowStart, {
116
+ wmp: this.wmpMeta(),
117
+ flow_type: flowType,
118
+ flow_id: flowId,
119
+ params,
120
+ timeout,
121
+ });
122
+ }
123
+ /** Send a flow progress notification. */
124
+ async flowProgress(flowId, step, payload) {
125
+ await this.notify(Method.FlowProgress, {
126
+ wmp: this.wmpMeta(),
127
+ flow_id: flowId,
128
+ step,
129
+ payload,
130
+ });
131
+ }
132
+ /** Send a flow action request and wait for result. */
133
+ async flowAction(flowId, action, params) {
134
+ return this.call(Method.FlowAction, {
135
+ wmp: this.wmpMeta(),
136
+ flow_id: flowId,
137
+ action,
138
+ params,
139
+ });
140
+ }
141
+ /** Notify flow completion. */
142
+ async flowComplete(flowId, result) {
143
+ await this.notify(Method.FlowComplete, {
144
+ wmp: this.wmpMeta(),
145
+ flow_id: flowId,
146
+ result,
147
+ });
148
+ }
149
+ /** Notify flow error. */
150
+ async flowError(flowId, code, message, data) {
151
+ await this.notify(Method.FlowError, {
152
+ wmp: this.wmpMeta(),
153
+ flow_id: flowId,
154
+ code,
155
+ message,
156
+ data,
157
+ });
158
+ }
159
+ /** Cancel a flow. */
160
+ async flowCancel(flowId, reason) {
161
+ return this.call(Method.FlowCancel, {
162
+ wmp: this.wmpMeta(),
163
+ flow_id: flowId,
164
+ reason,
165
+ });
166
+ }
167
+ // -------------------------------------------------------------------------
168
+ // Resolve convenience method
169
+ // -------------------------------------------------------------------------
170
+ async resolve(type, uri, options) {
171
+ return this.call(Method.Resolve, {
172
+ wmp: this.wmpMeta(),
173
+ type,
174
+ uri,
175
+ options,
176
+ });
177
+ }
178
+ // -------------------------------------------------------------------------
179
+ // Close
180
+ // -------------------------------------------------------------------------
181
+ close() {
182
+ this.closed = true;
183
+ // Reject all pending calls.
184
+ for (const [id, pending] of this.pending) {
185
+ clearTimeout(pending.timer);
186
+ pending.reject(new Error("Peer closed"));
187
+ this.pending.delete(id);
188
+ }
189
+ this.transport.close();
190
+ }
191
+ // -------------------------------------------------------------------------
192
+ // Internal dispatch
193
+ // -------------------------------------------------------------------------
194
+ dispatch(msg) {
195
+ if (isResponse(msg)) {
196
+ this.handleResponse(msg);
197
+ }
198
+ else if (isRequest(msg)) {
199
+ this.handleRequest(msg);
200
+ }
201
+ }
202
+ handleResponse(resp) {
203
+ if (resp.id == null)
204
+ return;
205
+ const pending = this.pending.get(resp.id);
206
+ if (!pending)
207
+ return;
208
+ this.pending.delete(resp.id);
209
+ clearTimeout(pending.timer);
210
+ if (resp.error) {
211
+ pending.reject(new WMPError(resp.error.code, resp.error.message, resp.error.data));
212
+ }
213
+ else {
214
+ pending.resolve(resp.result);
215
+ }
216
+ }
217
+ handleRequest(req) {
218
+ if (isNotification(req)) {
219
+ this.dispatchMethod(req.method, req.params).catch(() => {
220
+ /* notifications get no error response */
221
+ });
222
+ }
223
+ else {
224
+ this.dispatchMethod(req.method, req.params)
225
+ .then((result) => {
226
+ const resp = createResponse(req.id, result);
227
+ return this.transport.send(resp);
228
+ })
229
+ .catch((err) => {
230
+ const rpcErr = toRPCError(err);
231
+ const resp = createErrorResponse(req.id, rpcErr);
232
+ return this.transport.send(resp);
233
+ });
234
+ }
235
+ }
236
+ async dispatchMethod(method, params) {
237
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- JSON-RPC params are untyped
238
+ const p = params;
239
+ switch (method) {
240
+ // Session
241
+ case Method.SessionCreate:
242
+ if (this.handler.onSessionCreate) {
243
+ return this.handler.onSessionCreate(p);
244
+ }
245
+ break;
246
+ case Method.SessionClose:
247
+ if (this.handler.onSessionClose) {
248
+ this.handler.onSessionClose(p);
249
+ }
250
+ return undefined;
251
+ // Flows — delegate to profile FlowHandlers first, then handler.
252
+ case Method.FlowStart: {
253
+ const fh = this.registry.getFlowHandler(p.flow_type);
254
+ if (fh)
255
+ return fh.startFlow(p);
256
+ if (this.handler.onFlowStart)
257
+ return this.handler.onFlowStart(p);
258
+ break;
259
+ }
260
+ case Method.FlowProgress: {
261
+ const fh = this.flowHandlerForId(p.flow_id);
262
+ if (fh) {
263
+ fh.handleProgress(p);
264
+ return undefined;
265
+ }
266
+ if (this.handler.onFlowProgress) {
267
+ this.handler.onFlowProgress(p);
268
+ return undefined;
269
+ }
270
+ return undefined;
271
+ }
272
+ case Method.FlowAction: {
273
+ const fh = this.flowHandlerForId(p.flow_id);
274
+ if (fh)
275
+ return fh.handleAction(p);
276
+ if (this.handler.onFlowAction)
277
+ return this.handler.onFlowAction(p);
278
+ break;
279
+ }
280
+ case Method.FlowComplete: {
281
+ const fh = this.flowHandlerForId(p.flow_id);
282
+ if (fh) {
283
+ fh.handleComplete(p);
284
+ return undefined;
285
+ }
286
+ if (this.handler.onFlowComplete) {
287
+ this.handler.onFlowComplete(p);
288
+ return undefined;
289
+ }
290
+ return undefined;
291
+ }
292
+ case Method.FlowError: {
293
+ const fh = this.flowHandlerForId(p.flow_id);
294
+ if (fh) {
295
+ fh.handleError(p);
296
+ return undefined;
297
+ }
298
+ if (this.handler.onFlowError) {
299
+ this.handler.onFlowError(p);
300
+ return undefined;
301
+ }
302
+ return undefined;
303
+ }
304
+ case Method.FlowCancel:
305
+ if (this.handler.onFlowCancel) {
306
+ return this.handler.onFlowCancel(p);
307
+ }
308
+ break;
309
+ // Resolve — delegate to profile ResolveHandlers.
310
+ case Method.Resolve: {
311
+ const rh = this.registry.getResolveHandler(p.type);
312
+ if (rh)
313
+ return rh.handleResolve(p);
314
+ if (this.handler.onResolve)
315
+ return this.handler.onResolve(p);
316
+ break;
317
+ }
318
+ // Custom method — delegate to profile MethodHandlers.
319
+ default: {
320
+ const mh = this.registry.getMethodHandler(method);
321
+ if (mh)
322
+ return mh.handleMethod(method, params);
323
+ break;
324
+ }
325
+ }
326
+ throw new WMPError(ErrorCode.MethodNotFound, `Method not found: ${method}`);
327
+ }
328
+ // Track flow_id → flow_type mapping for routing to the right FlowHandler.
329
+ activeFlows = new Map();
330
+ /** Look up the FlowHandler for an active flow by flow_id. */
331
+ flowHandlerForId(flowId) {
332
+ const flowType = this.activeFlows.get(flowId);
333
+ if (!flowType)
334
+ return undefined;
335
+ return this.registry.getFlowHandler(flowType);
336
+ }
337
+ /** Track a flow as active (called when flow.start succeeds). */
338
+ trackFlow(flowId, flowType) {
339
+ this.activeFlows.set(flowId, flowType);
340
+ }
341
+ /** Untrack a flow (called on flow.complete / flow.error / flow.cancel). */
342
+ untrackFlow(flowId) {
343
+ this.activeFlows.delete(flowId);
344
+ }
345
+ // -------------------------------------------------------------------------
346
+ // Helpers
347
+ // -------------------------------------------------------------------------
348
+ wmpMeta() {
349
+ return {
350
+ version: VERSION,
351
+ session_id: this.sessionId,
352
+ timestamp: new Date().toISOString(),
353
+ };
354
+ }
355
+ onTransportError(_err) {
356
+ // Could emit events here for reconnection logic.
357
+ }
358
+ onTransportClose() {
359
+ if (!this.closed) {
360
+ // Transport closed unexpectedly — reject all pending.
361
+ for (const [id, pending] of this.pending) {
362
+ clearTimeout(pending.timer);
363
+ pending.reject(new Error("Transport closed"));
364
+ this.pending.delete(id);
365
+ }
366
+ }
367
+ }
368
+ }
369
+ // ---------------------------------------------------------------------------
370
+ // Error conversion
371
+ // ---------------------------------------------------------------------------
372
+ function toRPCError(err) {
373
+ if (err instanceof WMPError) {
374
+ return { code: err.code, message: err.message, data: err.data };
375
+ }
376
+ return {
377
+ code: ErrorCode.InternalError,
378
+ message: err instanceof Error ? err.message : String(err),
379
+ };
380
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Profile system — mirrors go-wmp/pkg/wmp/profile.go.
3
+ *
4
+ * Profiles are pluggable extensions that define flow handlers,
5
+ * custom method handlers, and resolve handlers.
6
+ */
7
+ import type { FlowStartParams, FlowStartResult, FlowActionParams, FlowActionResult, FlowProgressParams, FlowCompleteParams, FlowErrorParams, ResolveParams, ResolveResult } from "./types.js";
8
+ export interface PeerContext {
9
+ /** Send a JSON-RPC notification (no response expected). */
10
+ notify(method: string, params: unknown): Promise<void>;
11
+ /** Send a JSON-RPC request and wait for the response. */
12
+ call<T = unknown>(method: string, params: unknown): Promise<T>;
13
+ }
14
+ export interface Profile {
15
+ /** Profile identifier (e.g., "openid4x", "evidence"). */
16
+ readonly name: string;
17
+ /** Capability names this profile provides. */
18
+ capabilities(): string[];
19
+ /** Called when the profile is registered with a Peer. */
20
+ init(ctx: PeerContext): void;
21
+ }
22
+ export interface FlowHandler {
23
+ /** Flow type identifiers this handler manages. */
24
+ flowTypes(): string[];
25
+ startFlow(params: FlowStartParams): Promise<FlowStartResult>;
26
+ handleAction(params: FlowActionParams): Promise<FlowActionResult>;
27
+ handleProgress(params: FlowProgressParams): void;
28
+ handleComplete(params: FlowCompleteParams): void;
29
+ handleError(params: FlowErrorParams): void;
30
+ }
31
+ export interface MethodHandler {
32
+ /** Method names this handler supports. */
33
+ methods(): string[];
34
+ handleMethod(method: string, params: unknown): Promise<unknown>;
35
+ }
36
+ export interface ResolveHandler {
37
+ /** Resolve type identifiers this handler supports. */
38
+ resolveTypes(): string[];
39
+ handleResolve(params: ResolveParams): Promise<ResolveResult>;
40
+ }
41
+ export declare class Registry {
42
+ private profiles;
43
+ private flowHandlers;
44
+ private methodHandlers;
45
+ private resolveHandlers;
46
+ register(profile: Profile): void;
47
+ getFlowHandler(flowType: string): FlowHandler | undefined;
48
+ getMethodHandler(method: string): MethodHandler | undefined;
49
+ getResolveHandler(resolveType: string): ResolveHandler | undefined;
50
+ allCapabilities(): string[];
51
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Profile system — mirrors go-wmp/pkg/wmp/profile.go.
3
+ *
4
+ * Profiles are pluggable extensions that define flow handlers,
5
+ * custom method handlers, and resolve handlers.
6
+ */
7
+ // ---------------------------------------------------------------------------
8
+ // Registry — internal profile/handler registry
9
+ // ---------------------------------------------------------------------------
10
+ export class Registry {
11
+ profiles = [];
12
+ flowHandlers = new Map();
13
+ methodHandlers = new Map();
14
+ resolveHandlers = new Map();
15
+ register(profile) {
16
+ // Register sub-interfaces.
17
+ if (isFlowHandler(profile)) {
18
+ for (const ft of profile.flowTypes()) {
19
+ if (this.flowHandlers.has(ft)) {
20
+ throw new Error(`Flow type "${ft}" already registered by another profile`);
21
+ }
22
+ this.flowHandlers.set(ft, profile);
23
+ }
24
+ }
25
+ if (isMethodHandler(profile)) {
26
+ for (const m of profile.methods()) {
27
+ if (this.methodHandlers.has(m)) {
28
+ throw new Error(`Method "${m}" already registered by another profile`);
29
+ }
30
+ this.methodHandlers.set(m, profile);
31
+ }
32
+ }
33
+ if (isResolveHandler(profile)) {
34
+ for (const rt of profile.resolveTypes()) {
35
+ if (this.resolveHandlers.has(rt)) {
36
+ throw new Error(`Resolve type "${rt}" already registered by another profile`);
37
+ }
38
+ this.resolveHandlers.set(rt, profile);
39
+ }
40
+ }
41
+ this.profiles.push(profile);
42
+ }
43
+ getFlowHandler(flowType) {
44
+ return this.flowHandlers.get(flowType);
45
+ }
46
+ getMethodHandler(method) {
47
+ return this.methodHandlers.get(method);
48
+ }
49
+ getResolveHandler(resolveType) {
50
+ return this.resolveHandlers.get(resolveType);
51
+ }
52
+ allCapabilities() {
53
+ return this.profiles.flatMap((p) => p.capabilities());
54
+ }
55
+ }
56
+ // Type guards for profile sub-interfaces.
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ function isFlowHandler(p) {
59
+ return ("flowTypes" in p &&
60
+ typeof p.flowTypes === "function" &&
61
+ "startFlow" in p);
62
+ }
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ function isMethodHandler(p) {
65
+ return ("methods" in p &&
66
+ typeof p.methods === "function" &&
67
+ "handleMethod" in p);
68
+ }
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ function isResolveHandler(p) {
71
+ return ("resolveTypes" in p &&
72
+ typeof p.resolveTypes === "function" &&
73
+ "handleResolve" in p);
74
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * WMP Relay profile — rendezvous relay with offline message queue.
3
+ *
4
+ * Per wmp-transport.md §6.2–6.5, a relay:
5
+ * - Accepts registrations via wmp.relay.register
6
+ * - Routes incoming traffic to registered participants
7
+ * - Queues messages when a participant is offline
8
+ */
9
+ import type { MethodHandler } from "./profile.js";
10
+ export interface RelayConfig {
11
+ /** Max messages queued per participant. Default: 1000. */
12
+ maxQueueSize?: number;
13
+ /** Message TTL in milliseconds. Default: 24 hours. */
14
+ messageTTL?: number;
15
+ /** Registration TTL in milliseconds. Default: 1 hour. */
16
+ registrationTTL?: number;
17
+ /** Called when a message is forwarded to a registered peer. */
18
+ onForward?: (participant: string, msg: unknown) => Promise<void>;
19
+ }
20
+ export interface QueuedMessage {
21
+ data: unknown;
22
+ queuedAt: number;
23
+ expiresAt: number;
24
+ }
25
+ export declare class Relay implements MethodHandler {
26
+ private maxQueueSize;
27
+ private messageTTL;
28
+ private registrationTTL;
29
+ private onForward?;
30
+ private registrations;
31
+ private queues;
32
+ constructor(config?: RelayConfig);
33
+ methods(): string[];
34
+ handleMethod(method: string, params: unknown): Promise<unknown>;
35
+ /** Check if a participant is currently registered. */
36
+ isRegistered(participant: string): boolean;
37
+ /** Enqueue a message for offline delivery. */
38
+ enqueue(participant: string, data: unknown): void;
39
+ /** Drain all queued messages for a participant. */
40
+ drain(participant: string): QueuedMessage[];
41
+ /** Get the number of queued messages. */
42
+ queueLength(participant: string): number;
43
+ /** Remove a participant's registration. */
44
+ unregister(participant: string): void;
45
+ /** Purge expired registrations and messages. */
46
+ purgeExpired(): void;
47
+ }