@actuallab/rpc 12.1.62
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.cjs +1200 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +440 -0
- package/dist/index.d.ts +440 -0
- package/dist/index.js +1133 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { EventHandlerSet, PromiseSource, RetryDelayer } from '@actuallab/core';
|
|
2
|
+
|
|
3
|
+
/** Identifies the type of an RPC call on the wire. */
|
|
4
|
+
declare const enum RpcCallTypeId {
|
|
5
|
+
Regular = 0,
|
|
6
|
+
Reliable = 1
|
|
7
|
+
}
|
|
8
|
+
/** Identifies system call methods — names include :argCount suffix for .NET interop. */
|
|
9
|
+
declare const RpcSystemCalls: {
|
|
10
|
+
readonly handshake: "$sys.Handshake:1";
|
|
11
|
+
readonly ok: "$sys.Ok:1";
|
|
12
|
+
readonly error: "$sys.Error:1";
|
|
13
|
+
readonly cancel: "$sys.Cancel:0";
|
|
14
|
+
readonly keepAlive: "$sys.KeepAlive:1";
|
|
15
|
+
readonly reconnect: "$sys.Reconnect:3";
|
|
16
|
+
};
|
|
17
|
+
/** Wire-format RPC message envelope. */
|
|
18
|
+
interface RpcMessage {
|
|
19
|
+
CallType?: number;
|
|
20
|
+
RelatedId?: number;
|
|
21
|
+
Method?: string;
|
|
22
|
+
Headers?: unknown[];
|
|
23
|
+
}
|
|
24
|
+
declare const ENVELOPE_DELIMITER = "\n";
|
|
25
|
+
declare const ARG_DELIMITER = "\u001F";
|
|
26
|
+
declare const FRAME_DELIMITER = "\n\u001E";
|
|
27
|
+
|
|
28
|
+
/** Serializes an RpcMessage + args into the json5 wire format. */
|
|
29
|
+
declare function serializeMessage(message: RpcMessage, args?: unknown[]): string;
|
|
30
|
+
/** Serializes multiple messages into a single WebSocket frame. */
|
|
31
|
+
declare function serializeFrame(messages: string[]): string;
|
|
32
|
+
/** Splits a WebSocket text frame into individual message strings. */
|
|
33
|
+
declare function splitFrame(frame: string): string[];
|
|
34
|
+
/** Deserializes a single message string into envelope + args. */
|
|
35
|
+
declare function deserializeMessage(raw: string): {
|
|
36
|
+
message: RpcMessage;
|
|
37
|
+
args: unknown[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Abstract WebSocket interface — works with both browser WebSocket and Node.js ws. */
|
|
41
|
+
interface WebSocketLike {
|
|
42
|
+
readonly readyState: number;
|
|
43
|
+
send(data: string): void;
|
|
44
|
+
close(code?: number, reason?: string): void;
|
|
45
|
+
onopen: ((ev: unknown) => void) | null;
|
|
46
|
+
onmessage: ((ev: {
|
|
47
|
+
data: unknown;
|
|
48
|
+
}) => void) | null;
|
|
49
|
+
onclose: ((ev: {
|
|
50
|
+
code: number;
|
|
51
|
+
reason: string;
|
|
52
|
+
}) => void) | null;
|
|
53
|
+
onerror: ((ev: unknown) => void) | null;
|
|
54
|
+
}
|
|
55
|
+
declare const WebSocketState: {
|
|
56
|
+
readonly CONNECTING: 0;
|
|
57
|
+
readonly OPEN: 1;
|
|
58
|
+
readonly CLOSING: 2;
|
|
59
|
+
readonly CLOSED: 3;
|
|
60
|
+
};
|
|
61
|
+
/** Abstract RPC connection — transport-agnostic interface for sending/receiving messages. */
|
|
62
|
+
interface RpcConnection {
|
|
63
|
+
readonly isOpen: boolean;
|
|
64
|
+
readonly whenConnected: Promise<void>;
|
|
65
|
+
readonly messageReceived: EventHandlerSet<string>;
|
|
66
|
+
readonly closed: EventHandlerSet<{
|
|
67
|
+
code: number;
|
|
68
|
+
reason: string;
|
|
69
|
+
}>;
|
|
70
|
+
send(serializedMessage: string): void;
|
|
71
|
+
close(code?: number, reason?: string): void;
|
|
72
|
+
}
|
|
73
|
+
/** WebSocket-based RpcConnection — handles frame splitting and message queueing. */
|
|
74
|
+
declare class RpcWebSocketConnection implements RpcConnection {
|
|
75
|
+
private _ws;
|
|
76
|
+
private _sendBuffer;
|
|
77
|
+
private _connected;
|
|
78
|
+
readonly messageReceived: EventHandlerSet<string>;
|
|
79
|
+
readonly closed: EventHandlerSet<{
|
|
80
|
+
code: number;
|
|
81
|
+
reason: string;
|
|
82
|
+
}>;
|
|
83
|
+
readonly error: EventHandlerSet<unknown>;
|
|
84
|
+
constructor(ws: WebSocketLike);
|
|
85
|
+
get isOpen(): boolean;
|
|
86
|
+
get whenConnected(): Promise<void>;
|
|
87
|
+
send(serializedMessage: string): void;
|
|
88
|
+
sendBatch(messages: string[]): void;
|
|
89
|
+
close(code?: number, reason?: string): void;
|
|
90
|
+
private _flush;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** MessagePort-based RpcConnection — for in-process testing without WebSocket mocks. */
|
|
94
|
+
declare class RpcMessageChannelConnection implements RpcConnection {
|
|
95
|
+
private _port;
|
|
96
|
+
private _open;
|
|
97
|
+
readonly messageReceived: EventHandlerSet<string>;
|
|
98
|
+
readonly closed: EventHandlerSet<{
|
|
99
|
+
code: number;
|
|
100
|
+
reason: string;
|
|
101
|
+
}>;
|
|
102
|
+
readonly whenConnected: Promise<void>;
|
|
103
|
+
constructor(port: MessagePort);
|
|
104
|
+
get isOpen(): boolean;
|
|
105
|
+
send(serializedMessage: string): void;
|
|
106
|
+
close(code?: number, reason?: string): void;
|
|
107
|
+
}
|
|
108
|
+
/** Creates a pair of connected RpcMessageChannelConnections for testing. */
|
|
109
|
+
declare function createMessageChannelPair(): [RpcMessageChannelConnection, RpcMessageChannelConnection];
|
|
110
|
+
|
|
111
|
+
/** Describes a single method on an RPC service. */
|
|
112
|
+
interface RpcMethodDef {
|
|
113
|
+
readonly name: string;
|
|
114
|
+
readonly serviceName: string;
|
|
115
|
+
readonly argCount: number;
|
|
116
|
+
readonly wireArgCount: number;
|
|
117
|
+
readonly callTypeId: number;
|
|
118
|
+
readonly stream: boolean;
|
|
119
|
+
readonly noWait: boolean;
|
|
120
|
+
}
|
|
121
|
+
/** Describes an RPC service — its name and method definitions. */
|
|
122
|
+
interface RpcServiceDef {
|
|
123
|
+
readonly name: string;
|
|
124
|
+
readonly methods: ReadonlyMap<string, RpcMethodDef>;
|
|
125
|
+
}
|
|
126
|
+
interface RpcMethodDefInput {
|
|
127
|
+
args: unknown[];
|
|
128
|
+
returns?: symbol;
|
|
129
|
+
callTypeId?: number;
|
|
130
|
+
stream?: boolean;
|
|
131
|
+
noWait?: boolean;
|
|
132
|
+
}
|
|
133
|
+
declare const RpcType: {
|
|
134
|
+
readonly object: symbol;
|
|
135
|
+
readonly stream: symbol;
|
|
136
|
+
readonly void: symbol;
|
|
137
|
+
};
|
|
138
|
+
interface RpcServiceDefOptions {
|
|
139
|
+
ctOffset?: number;
|
|
140
|
+
}
|
|
141
|
+
/** Returns the full wire method name: "ServiceName.MethodName:wireArgCount". */
|
|
142
|
+
declare function wireMethodName(def: RpcMethodDef): string;
|
|
143
|
+
declare function defineRpcService(name: string, methods: Record<string, RpcMethodDefInput>, options?: RpcServiceDefOptions): RpcServiceDef;
|
|
144
|
+
|
|
145
|
+
/** Tracks a pending outbound RPC call. */
|
|
146
|
+
declare class RpcOutboundCall {
|
|
147
|
+
readonly callId: number;
|
|
148
|
+
readonly method: string;
|
|
149
|
+
readonly result: PromiseSource<unknown>;
|
|
150
|
+
/** Whether to remove this call from the tracker on $sys.Ok. Default: true.
|
|
151
|
+
* Subclasses (e.g. compute calls) override to false to stay in tracker for invalidation. */
|
|
152
|
+
readonly removeOnOk: boolean;
|
|
153
|
+
/** The serialized wire message — stored for re-sending on reconnect. */
|
|
154
|
+
serializedMessage: string;
|
|
155
|
+
constructor(callId: number, method: string);
|
|
156
|
+
/** Called when the connection is lost. Subclasses can override to resolve invalidation promises. */
|
|
157
|
+
onDisconnect(): void;
|
|
158
|
+
}
|
|
159
|
+
/** Manages outbound calls by their RelatedId. */
|
|
160
|
+
declare class RpcOutboundCallTracker {
|
|
161
|
+
private _calls;
|
|
162
|
+
private _nextId;
|
|
163
|
+
get size(): number;
|
|
164
|
+
nextId(): number;
|
|
165
|
+
register(call: RpcOutboundCall): void;
|
|
166
|
+
get(callId: number): RpcOutboundCall | undefined;
|
|
167
|
+
remove(callId: number): RpcOutboundCall | undefined;
|
|
168
|
+
values(): IterableIterator<RpcOutboundCall>;
|
|
169
|
+
activeCallIds(): number[];
|
|
170
|
+
/** Reject all pending calls with the given error.
|
|
171
|
+
* Stage-3 compute calls (result resolved, awaiting invalidation) are kept in the tracker. */
|
|
172
|
+
rejectAll(error: Error): void;
|
|
173
|
+
/** Invalidate all remaining stage-3 calls (on reconnect or peer stop). */
|
|
174
|
+
invalidateAll(): void;
|
|
175
|
+
clear(): void;
|
|
176
|
+
}
|
|
177
|
+
/** Tracks an incoming inbound RPC call. */
|
|
178
|
+
declare class RpcInboundCall {
|
|
179
|
+
readonly callId: number;
|
|
180
|
+
readonly method: string;
|
|
181
|
+
readonly args: unknown[];
|
|
182
|
+
constructor(callId: number, method: string, args: unknown[]);
|
|
183
|
+
}
|
|
184
|
+
/** Manages inbound calls by their RelatedId. */
|
|
185
|
+
declare class RpcInboundCallTracker {
|
|
186
|
+
private _calls;
|
|
187
|
+
get size(): number;
|
|
188
|
+
register(call: RpcInboundCall): void;
|
|
189
|
+
get(callId: number): RpcInboundCall | undefined;
|
|
190
|
+
remove(callId: number): RpcInboundCall | undefined;
|
|
191
|
+
clear(): void;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Handles an incoming system call message and dispatches to the appropriate tracker. */
|
|
195
|
+
declare function handleSystemCall(message: RpcMessage, args: unknown[], outboundTracker: RpcOutboundCallTracker, inboundTracker: RpcInboundCallTracker): void;
|
|
196
|
+
|
|
197
|
+
/** Sends system RPC messages — like .NET's RpcSystemCallSender. */
|
|
198
|
+
declare class RpcSystemCallSender {
|
|
199
|
+
handshake(conn: RpcConnection, peerId: string, hubId: string, index: number): void;
|
|
200
|
+
ok(conn: RpcConnection, relatedId: number, result: unknown): void;
|
|
201
|
+
error(conn: RpcConnection, relatedId: number, error: unknown): void;
|
|
202
|
+
cancel(conn: RpcConnection, relatedId: number): void;
|
|
203
|
+
keepAlive(conn: RpcConnection, activeCallIds: number[]): void;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
declare class RpcClientPeerReconnectDelayer extends RetryDelayer {
|
|
207
|
+
constructor();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
type RpcServiceImpl = Record<string, (...args: unknown[]) => unknown>;
|
|
211
|
+
/** Context passed to service dispatch — carries callId and connection for compute tracking. */
|
|
212
|
+
interface RpcDispatchContext {
|
|
213
|
+
__rpcDispatch: true;
|
|
214
|
+
callId: number;
|
|
215
|
+
connection: RpcConnection;
|
|
216
|
+
}
|
|
217
|
+
/** Dispatches inbound RPC calls to registered service implementations. */
|
|
218
|
+
declare class RpcServiceHost {
|
|
219
|
+
private _methods;
|
|
220
|
+
register(def: RpcServiceDef, impl: RpcServiceImpl): void;
|
|
221
|
+
dispatch(wireMethod: string, args: unknown[], context?: RpcDispatchContext): Promise<unknown>;
|
|
222
|
+
getMethodDef(wireMethod: string): RpcMethodDef | undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Central RPC coordinator — manages peers, services, and configuration. */
|
|
226
|
+
declare class RpcHub {
|
|
227
|
+
readonly hubId: string;
|
|
228
|
+
readonly peers: Map<string, RpcPeer>;
|
|
229
|
+
readonly serviceHost: RpcServiceHost;
|
|
230
|
+
readonly systemCallSender: RpcSystemCallSender;
|
|
231
|
+
constructor(hubId?: string);
|
|
232
|
+
addPeer(peer: RpcPeer): void;
|
|
233
|
+
removePeer(ref: string): void;
|
|
234
|
+
/** Get or create a peer by ref. Server peers have "server://" prefix, everything else is a client peer. */
|
|
235
|
+
getPeer(ref: string): RpcPeer;
|
|
236
|
+
/** Get or create a client peer for the given URL. */
|
|
237
|
+
getClientPeer(ref: string): RpcClientPeer;
|
|
238
|
+
/** Get or create a server peer for the given ref. */
|
|
239
|
+
getServerPeer(ref: string): RpcServerPeer;
|
|
240
|
+
/** Register a service with optional server method wrapping for custom call types. */
|
|
241
|
+
addService(defOrContract: RpcServiceDef | (abstract new (...args: any[]) => any), impl: RpcServiceImpl): void;
|
|
242
|
+
/** Create a typed client proxy for a service on a remote peer. */
|
|
243
|
+
addClient<T extends object>(peer: RpcPeer, defOrContract: RpcServiceDef | (abstract new (...args: any[]) => any)): T;
|
|
244
|
+
close(): void;
|
|
245
|
+
/** Dispatch an inbound system call. Override in FusionHub to handle compute-specific system calls. */
|
|
246
|
+
handleSystemCall(message: RpcMessage, args: unknown[], outbound: RpcOutboundCallTracker, inbound: RpcInboundCallTracker): void;
|
|
247
|
+
/** Build an RpcServiceDef from decorator metadata on a contract class. Override in FusionHub for compute support. */
|
|
248
|
+
protected _buildServiceDef(cls: abstract new (...args: any[]) => any): RpcServiceDef;
|
|
249
|
+
/** Resolve a service def from either a plain RpcServiceDef or a contract class. */
|
|
250
|
+
protected _resolveServiceDef(defOrContract: RpcServiceDef | (abstract new (...args: any[]) => any)): RpcServiceDef;
|
|
251
|
+
/** Override in FusionHub — default is pass-through (for methods with callTypeId !== 0). */
|
|
252
|
+
protected _wrapServerMethod(_methodDef: RpcMethodDef, fn: (...args: unknown[]) => unknown, _impl: object): (...args: unknown[]) => unknown;
|
|
253
|
+
/** Create a client method for the given methodDef. Override in FusionHub for custom call types. */
|
|
254
|
+
protected _createClientMethod(peer: RpcPeer, methodDef: RpcMethodDef): (...args: unknown[]) => unknown;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Default serialization format for RpcClientPeer connections.
|
|
259
|
+
* TS uses plain JSON.stringify without polymorphic type wrapping,
|
|
260
|
+
* so "json5np" (System.Text.Json, no polymorphism) is the correct match.
|
|
261
|
+
*/
|
|
262
|
+
declare const DEFAULT_SERIALIZATION_FORMAT = "json5np";
|
|
263
|
+
/** Builds the WebSocket connection URL for an RpcClientPeer. */
|
|
264
|
+
type RpcConnectionUrlResolver = (peer: RpcClientPeer) => string;
|
|
265
|
+
/** Default connection URL provider — appends `clientId` and `f` query parameters. */
|
|
266
|
+
declare const defaultConnectionUrlResolver: RpcConnectionUrlResolver;
|
|
267
|
+
/** Options for RpcPeer.call() — allows custom call types and cancellation. */
|
|
268
|
+
interface RpcCallOptions {
|
|
269
|
+
/** Wire CallType field (0 = regular). */
|
|
270
|
+
callTypeId?: number;
|
|
271
|
+
/** Factory for creating custom outbound call instances (e.g. compute calls). */
|
|
272
|
+
outboundCallFactory?: (id: number, method: string) => RpcOutboundCall;
|
|
273
|
+
/** AbortSignal for caller-initiated cancellation. */
|
|
274
|
+
signal?: AbortSignal;
|
|
275
|
+
}
|
|
276
|
+
/** Data extracted from an inbound $sys.Handshake message. */
|
|
277
|
+
interface RemoteHandshake {
|
|
278
|
+
RemotePeerId?: string;
|
|
279
|
+
RemoteHubId?: string;
|
|
280
|
+
ProtocolVersion?: number;
|
|
281
|
+
Index?: number;
|
|
282
|
+
}
|
|
283
|
+
/** Base class for RPC peers — handles bidirectional message dispatch. */
|
|
284
|
+
declare abstract class RpcPeer {
|
|
285
|
+
/** Routing/addressing key — used as key in hub.peers (URL for clients, "server://{uuid}" for servers). */
|
|
286
|
+
readonly ref: string;
|
|
287
|
+
/** Auto-generated GUID — used in handshakes only. */
|
|
288
|
+
readonly id: string;
|
|
289
|
+
protected _hub: RpcHub;
|
|
290
|
+
protected _connection: RpcConnection | undefined;
|
|
291
|
+
readonly outbound: RpcOutboundCallTracker;
|
|
292
|
+
readonly inbound: RpcInboundCallTracker;
|
|
293
|
+
readonly connected: EventHandlerSet<void>;
|
|
294
|
+
readonly disconnected: EventHandlerSet<{
|
|
295
|
+
code: number;
|
|
296
|
+
reason: string;
|
|
297
|
+
}>;
|
|
298
|
+
protected _pendingSends: RpcOutboundCall[];
|
|
299
|
+
private _keepAliveTimer;
|
|
300
|
+
constructor(ref: string, hub: RpcHub);
|
|
301
|
+
get connection(): RpcConnection | undefined;
|
|
302
|
+
get isConnected(): boolean;
|
|
303
|
+
protected setupConnection(conn: RpcConnection): void;
|
|
304
|
+
call(method: string, args?: unknown[], options?: RpcCallOptions): RpcOutboundCall;
|
|
305
|
+
callNoWait(method: string, args?: unknown[]): void;
|
|
306
|
+
close(): void;
|
|
307
|
+
/** Send any messages buffered while disconnected. Call after connection + handshake are ready. */
|
|
308
|
+
protected _flushPendingSends(): void;
|
|
309
|
+
/** Override in subclasses to handle the remote peer's handshake response. */
|
|
310
|
+
protected _onHandshakeReceived(_handshake: RemoteHandshake): void;
|
|
311
|
+
private _handleMessage;
|
|
312
|
+
private _handleInbound;
|
|
313
|
+
private _startKeepAlive;
|
|
314
|
+
private _stopKeepAlive;
|
|
315
|
+
}
|
|
316
|
+
/** Connection state for RpcClientPeer. */
|
|
317
|
+
declare const enum RpcPeerConnectionKind {
|
|
318
|
+
Disconnected = 0,
|
|
319
|
+
Connecting = 1,
|
|
320
|
+
Connected = 2
|
|
321
|
+
}
|
|
322
|
+
/** Client-side RPC peer — initiates WebSocket connection. ref = URL. */
|
|
323
|
+
declare class RpcClientPeer extends RpcPeer {
|
|
324
|
+
private _disposed;
|
|
325
|
+
private _connectionKind;
|
|
326
|
+
private _tryIndex;
|
|
327
|
+
private _handshakeIndex;
|
|
328
|
+
private _lastRemoteHubId;
|
|
329
|
+
private _pendingHandshake;
|
|
330
|
+
private _reconnectsAt;
|
|
331
|
+
/** Base64url-encoded peer ID — matches .NET's RpcClientPeer.ClientId (Guid.ToBase64Url). */
|
|
332
|
+
readonly clientId: string;
|
|
333
|
+
/**
|
|
334
|
+
* Serialization format key sent to the server via `f=` query parameter.
|
|
335
|
+
* TS uses plain JSON.stringify (no polymorphic type wrapping), so `json5np`
|
|
336
|
+
* (no-polymorphism) is the correct default.
|
|
337
|
+
*/
|
|
338
|
+
readonly serializationFormat: string;
|
|
339
|
+
/** Builds the WebSocket URL for this peer. Replace to customize URL construction. */
|
|
340
|
+
connectionUrlResolver: RpcConnectionUrlResolver;
|
|
341
|
+
readonly peerChanged: EventHandlerSet<void>;
|
|
342
|
+
readonly reconnectDelayer: RpcClientPeerReconnectDelayer;
|
|
343
|
+
readonly reconnectsAtChanged: EventHandlerSet<void>;
|
|
344
|
+
get reconnectsAt(): number;
|
|
345
|
+
private _setReconnectsAt;
|
|
346
|
+
constructor(hub: RpcHub, url: string, serializationFormat?: string);
|
|
347
|
+
get connectionKind(): RpcPeerConnectionKind;
|
|
348
|
+
/** One-shot connection for tests — no reconnection loop, no handshake. */
|
|
349
|
+
connectWith(conn: RpcConnection): void;
|
|
350
|
+
/** Start the reconnection loop — runs until disposed. */
|
|
351
|
+
run(wsFactory?: (url: string) => WebSocketLike): Promise<void>;
|
|
352
|
+
/** Re-send outbound calls after reconnection + flush pending sends.
|
|
353
|
+
* Stage-3 compute calls are always self-invalidated: without $sys.Reconnect
|
|
354
|
+
* protocol, the server's invalidation tracking is lost on disconnect, and
|
|
355
|
+
* re-sending would get a duplicate $sys.Ok that is ignored (PromiseSource
|
|
356
|
+
* already resolved). Self-invalidation forces a fresh recompute that
|
|
357
|
+
* establishes new invalidation tracking on the new connection.
|
|
358
|
+
* Regular in-flight calls are re-sent transparently. */
|
|
359
|
+
private _reconnect;
|
|
360
|
+
protected _onHandshakeReceived(handshake: RemoteHandshake): void;
|
|
361
|
+
close(): void;
|
|
362
|
+
}
|
|
363
|
+
/** Server-side RPC peer — wraps an accepted connection. ref = "server://{uuid}". */
|
|
364
|
+
declare class RpcServerPeer extends RpcPeer {
|
|
365
|
+
constructor(hub: RpcHub, ref: string);
|
|
366
|
+
/** Accept an incoming connection — sets up message handling and handshake response. */
|
|
367
|
+
accept(conn: RpcConnection): void;
|
|
368
|
+
protected _onHandshakeReceived(_handshake: RemoteHandshake): void;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Creates a typed RPC client proxy — intercepts method calls and sends them over RPC. */
|
|
372
|
+
declare function createRpcClient<T extends object>(peer: RpcPeer, serviceDef: RpcServiceDef): T;
|
|
373
|
+
|
|
374
|
+
/** Abstract WebSocket server interface — implement with `ws` or other library. */
|
|
375
|
+
interface WebSocketServer {
|
|
376
|
+
onConnection(handler: (ws: WebSocketLike) => void): void;
|
|
377
|
+
close(): void;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
interface MethodMeta {
|
|
381
|
+
argCount: number;
|
|
382
|
+
stream?: boolean;
|
|
383
|
+
noWait?: boolean;
|
|
384
|
+
[key: symbol]: unknown;
|
|
385
|
+
}
|
|
386
|
+
interface ServiceMeta {
|
|
387
|
+
name: string;
|
|
388
|
+
ctOffset?: number;
|
|
389
|
+
}
|
|
390
|
+
/** Read method metadata from a decorated class. */
|
|
391
|
+
declare function getMethodsMeta(cls: abstract new (...args: any[]) => any): Record<string, MethodMeta> | undefined;
|
|
392
|
+
/** Read service metadata from a decorated class. */
|
|
393
|
+
declare function getServiceMeta(cls: abstract new (...args: any[]) => any): ServiceMeta | undefined;
|
|
394
|
+
/** Class decorator — stores the RPC service wire name and optional ctOffset. */
|
|
395
|
+
declare function rpcService(serviceName: string, options?: {
|
|
396
|
+
ctOffset?: number;
|
|
397
|
+
}): <T extends abstract new (...args: any[]) => any>(_target: T, context: ClassDecoratorContext<T>) => void;
|
|
398
|
+
/** Method decorator — stores RPC method metadata (argCount, stream, noWait). Does NOT wrap the method. */
|
|
399
|
+
declare function rpcMethod(options?: {
|
|
400
|
+
stream?: boolean;
|
|
401
|
+
noWait?: boolean;
|
|
402
|
+
}): <This, Args extends unknown[], Return>(target: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>) => (this: This, ...args: Args) => Return;
|
|
403
|
+
|
|
404
|
+
declare const enum RpcPeerStateKind {
|
|
405
|
+
Connected = 0,
|
|
406
|
+
JustDisconnected = 1,
|
|
407
|
+
Disconnected = 2,
|
|
408
|
+
JustConnected = 3
|
|
409
|
+
}
|
|
410
|
+
interface RpcPeerState {
|
|
411
|
+
readonly kind: RpcPeerStateKind;
|
|
412
|
+
readonly lastError?: Error;
|
|
413
|
+
readonly reconnectsIn: number;
|
|
414
|
+
}
|
|
415
|
+
declare function isConnected(state: RpcPeerState): boolean;
|
|
416
|
+
declare function likelyConnected(state: RpcPeerState): boolean;
|
|
417
|
+
declare function getStateDescription(state: RpcPeerState, useLastError?: boolean): string;
|
|
418
|
+
|
|
419
|
+
declare class RpcPeerStateMonitor {
|
|
420
|
+
readonly peer: RpcClientPeer;
|
|
421
|
+
readonly stateChanged: EventHandlerSet<RpcPeerState>;
|
|
422
|
+
private _rawConnected;
|
|
423
|
+
private _connectedAt;
|
|
424
|
+
private _disconnectedAt;
|
|
425
|
+
private _lastError;
|
|
426
|
+
private _state;
|
|
427
|
+
private _timer;
|
|
428
|
+
private _onConnected;
|
|
429
|
+
private _onDisconnected;
|
|
430
|
+
private _onReconnectsAtChanged;
|
|
431
|
+
private _onCancelDelaysChanged;
|
|
432
|
+
constructor(peer: RpcClientPeer);
|
|
433
|
+
get state(): RpcPeerState;
|
|
434
|
+
dispose(): void;
|
|
435
|
+
private _recompute;
|
|
436
|
+
private _setState;
|
|
437
|
+
private _scheduleRecompute;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export { ARG_DELIMITER, DEFAULT_SERIALIZATION_FORMAT, ENVELOPE_DELIMITER, FRAME_DELIMITER, type MethodMeta, type RemoteHandshake, type RpcCallOptions, RpcCallTypeId, RpcClientPeer, RpcClientPeerReconnectDelayer, type RpcConnection, type RpcConnectionUrlResolver, type RpcDispatchContext, RpcHub, RpcInboundCall, RpcInboundCallTracker, type RpcMessage, RpcMessageChannelConnection, type RpcMethodDef, type RpcMethodDefInput, RpcOutboundCall, RpcOutboundCallTracker, RpcPeer, RpcPeerConnectionKind, type RpcPeerState, RpcPeerStateKind, RpcPeerStateMonitor, RpcServerPeer, type RpcServiceDef, type RpcServiceDefOptions, RpcServiceHost, type RpcServiceImpl, RpcSystemCallSender, RpcSystemCalls, RpcType, RpcWebSocketConnection, type ServiceMeta, type WebSocketLike, type WebSocketServer, WebSocketState, createMessageChannelPair, createRpcClient, defaultConnectionUrlResolver, defineRpcService, deserializeMessage, getMethodsMeta, getServiceMeta, getStateDescription, handleSystemCall, isConnected, likelyConnected, rpcMethod, rpcService, serializeFrame, serializeMessage, splitFrame, wireMethodName };
|