Appearance
rpc
Enumerations
TRpcAgent
Defined in: src/rpc/types.ts:6
Identifies which side of the channel sent a message.
Enumeration Members
| Enumeration Member | Value | Description | Defined in |
|---|---|---|---|
Client | 1 | Message originated from the client (RpcClient side). | src/rpc/types.ts:10 |
Server | 0 | Message originated from the server (RpcConnect side). | src/rpc/types.ts:8 |
TRpcEncodedType
Defined in: src/rpc/types.ts:127
Discriminant tag for the encoded-value union.
Enumeration Members
| Enumeration Member | Value | Defined in |
|---|---|---|
Array | 6 | src/rpc/types.ts:134 |
BigInt | 2 | src/rpc/types.ts:130 |
Date | 7 | src/rpc/types.ts:135 |
Error | 10 | src/rpc/types.ts:138 |
Function | 4 | src/rpc/types.ts:132 |
Object | 5 | src/rpc/types.ts:133 |
Other | 0 | src/rpc/types.ts:128 |
Promise | 9 | src/rpc/types.ts:137 |
RegExp | 8 | src/rpc/types.ts:136 |
String | 3 | src/rpc/types.ts:131 |
Symbol | 1 | src/rpc/types.ts:129 |
TRpcEncodedValueOther
Defined in: src/rpc/types.ts:142
Sub-tag for TRpcEncodedType.Other — non-JSON-serializable primitives.
Enumeration Members
| Enumeration Member | Value | Defined in |
|---|---|---|
Infinity | 2 | src/rpc/types.ts:145 |
NaN | 1 | src/rpc/types.ts:144 |
NegativeInfinity | 3 | src/rpc/types.ts:146 |
Undefined | 0 | src/rpc/types.ts:143 |
TRpcType
Defined in: src/rpc/types.ts:16
All possible RPC message types used on the wire.
Enumeration Members
| Enumeration Member | Value | Description | Defined in |
|---|---|---|---|
Call | 0 | Regular method call from client to server. | src/rpc/types.ts:18 |
Dispatch | 5 | Server pushes data to the client without a request. | src/rpc/types.ts:28 |
Event | 4 | Event emitted by the client side. | src/rpc/types.ts:26 |
MetaCall | 7 | Meta call (introspection, e.g. proxy()). | src/rpc/types.ts:32 |
Result | 1 | Response from server to client. | src/rpc/types.ts:20 |
SubCall | 2 | Cross-thread sub-call (server calling a function passed as an argument). | src/rpc/types.ts:22 |
SubResult | 3 | Response to a sub-call. | src/rpc/types.ts:24 |
Subscribe | 6 | Subscribe to server-side events. | src/rpc/types.ts:30 |
Classes
RpcClient
Defined in: src/rpc/RpcClient.ts:32
RPC client that sends typed method calls over a message channel and resolves responses by idempotency key. Supports subscriptions, callbacks, proxy generation, and abort signals.
Example
ts
const client = new RpcClient({ postMessage: send, onMessage: listen });
const result = await client.call('greet', ['world']); // => 'Hello, world!'Extends
Extended by
Implements
Constructors
Constructor
ts
new RpcClient(options): RpcClient;Defined in: src/rpc/RpcClient.ts:47
Parameters
| Parameter | Type |
|---|---|
options | | TRpcClientOptions | TRpcClientOptionsInit |
Returns
Overrides
Properties
| Property | Modifier | Type | Defined in |
|---|---|---|---|
DEFAULT_EVENTS | static | string[] | src/rpc/RpcClient.ts:33 |
Methods
call()
ts
call<R>(
method,
args?,
options?): Promise<R>;Defined in: src/rpc/RpcClient.ts:68
Calls a remote method and returns a Promise of its result.
Type Parameters
| Type Parameter | Default type |
|---|---|
R | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
method | string | Method name exported by RpcConnect. |
args? | any[] | Arguments to pass to the method. |
options? | TRpcClientRequestOptions | Optional timeout / abort signal / event names. |
Returns
Promise<R>
Implementation of
clear()
ts
clear(): void;Defined in: src/EventEmitter/index.ts:53
Removes all listeners without destroying the emitter.
Returns
void
Implementation of
Inherited from
destroy()
ts
destroy(): void;Defined in: src/rpc/RpcClient.ts:63
Destroys the client and releases resources.
Returns
void
Implementation of
Overrides
emit()
ts
protected emit(data): void;Defined in: src/EventEmitter/index.ts:27
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
Inherited from
on()
ts
on(
event,
args?,
options?): TRpcUnsubscribe;Defined in: src/rpc/RpcClient.ts:72
Subscribes to a server-side event stream.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Event name. |
args? | any[] | Arguments forwarded to the server subscription handler. |
options? | TRpcClientRequestOptions | - |
Returns
An unsubscribe function.
Implementation of
once()
ts
once(...listeners): () => void;Defined in: src/EventEmitter/index.ts:46
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
proxy()
ts
proxy(): Promise<any>;Defined in: src/rpc/RpcClient.ts:83
Returns a proxy object where every property is an async method call.
Returns
Promise<any>
Implementation of
subscribe()
ts
subscribe(...listeners): () => void;Defined in: src/EventEmitter/index.ts:42
Registers one or more listeners; returns a function that removes them all.
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Implementation of
Inherited from
taskCount()
ts
taskCount(): number;Defined in: src/rpc/RpcClient.ts:59
Returns the number of currently pending calls.
Returns
number
Implementation of
RpcClientPool
Defined in: src/rpc/RpcClientPool.ts:28
Pool of RpcClient instances that distributes calls to the least-busy worker.
Example
ts
const pool = new RpcClientPool({ maxWorkers: 4, workerProvider: () => new RpcClient(opts) });
const result = await pool.call('compute', [data]);Extends
Extended by
Type Parameters
| Type Parameter | Default type |
|---|---|
Client extends IRpcClient | IRpcClient |
Implements
Constructors
Constructor
ts
new RpcClientPool<Client>(options): RpcClientPool<Client>;Defined in: src/rpc/RpcClientPool.ts:101
Parameters
| Parameter | Type |
|---|---|
options | | TRpcClientWorkerPoolOptionsInit<Client> | TRpcClientPoolOptions<Client> |
Returns
RpcClientPool<Client>
Overrides
Methods
call()
ts
call<A, R>(...args): Promise<R>;Defined in: src/rpc/RpcClientPool.ts:118
Routes the call to the least-busy worker.
Type Parameters
| Type Parameter | Default type |
|---|---|
A extends any[] | any[] |
R | any |
Parameters
| Parameter | Type |
|---|---|
...args | [string, A, TRpcClientRequestOptions] |
Returns
Promise<R>
Implementation of
clear()
ts
clear(): void;Defined in: src/EventEmitter/index.ts:53
Removes all listeners without destroying the emitter.
Returns
void
Implementation of
Inherited from
destroy()
ts
destroy(): void;Defined in: src/rpc/RpcClientPool.ts:110
Destroys all workers in the pool and the pool itself.
Returns
void
Implementation of
Overrides
emit()
ts
protected emit(data): void;Defined in: src/EventEmitter/index.ts:27
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
Inherited from
getWorker()
ts
getWorker(): Client;Defined in: src/rpc/RpcClientPool.ts:64
Returns the least-busy worker, creating a new one if a slot is free. If all slots are busy, returns the worker with the fewest active tasks.
Returns
Client
The selected Client instance.
getWorkers()
ts
getWorkers(count?): Client[];Defined in: src/rpc/RpcClientPool.ts:40
Pre-creates exactly count workers (cycling through slots up to maxWorkers). Useful in tests to ensure workers exist before attaching servers.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
count | number | 0 | Number of worker instances to pre-create. |
Returns
Client[]
Array of the created/existing worker instances.
on()
ts
on(...args): TRpcUnsubscribe;Defined in: src/rpc/RpcClientPool.ts:127
Routes the subscription to the least-busy worker.
Parameters
| Parameter | Type |
|---|---|
...args | [string, any[], TRpcClientRequestOptions] |
Returns
Implementation of
once()
ts
once(...listeners): () => void;Defined in: src/EventEmitter/index.ts:46
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Inherited from
proxy()
ts
proxy(): Promise<Record<string, any>>;Defined in: src/rpc/RpcClientPool.ts:136
Returns a proxy from the least-busy worker.
Returns
Promise<Record<string, any>>
Implementation of
subscribe()
ts
subscribe(...listeners): () => void;Defined in: src/EventEmitter/index.ts:42
Registers one or more listeners; returns a function that removes them all.
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
() => void
Implementation of
Inherited from
taskCount()
ts
taskCount(): number;Defined in: src/rpc/RpcClientPool.ts:141
Returns the total number of pending tasks across all workers.
Returns
number
Implementation of
RpcCoder
Defined in: src/rpc/RpcCoder/index.ts:62
Constructors
Constructor
ts
new RpcCoder(extrenalFnProvider?, options?): RpcCoder;Defined in: src/rpc/RpcCoder/index.ts:71
Parameters
| Parameter | Type |
|---|---|
extrenalFnProvider? | TRpcExtrernalFnProvider |
options? | { useSymols?: boolean; } |
options.useSymols? | boolean |
Returns
Properties
| Property | Modifier | Type | Default value | Defined in |
|---|---|---|---|---|
fnCoder | public | RpcFnCoder | null | src/rpc/RpcCoder/index.ts:65 |
NON_ENCODABLE_TYPES | static | string[] | NON_ENCODABLE_TYPES | src/rpc/RpcCoder/index.ts:63 |
Methods
decode()
ts
decode(encodedData, withExternalFns?): any;Defined in: src/rpc/RpcCoder/index.ts:246
Parameters
| Parameter | Type | Default value |
|---|---|---|
encodedData | TRpcEncodedData | undefined |
withExternalFns | boolean | true |
Returns
any
encode()
ts
encode(value, withInternalFns?): TRpcEncodedData;Defined in: src/rpc/RpcCoder/index.ts:83
Parameters
| Parameter | Type | Default value |
|---|---|---|
value | any | undefined |
withInternalFns | boolean | true |
Returns
invoke()
ts
invoke(index, args): any;Defined in: src/rpc/RpcCoder/index.ts:79
Parameters
| Parameter | Type |
|---|---|
index | number |
args | any[] |
Returns
any
RpcConnect
Defined in: src/rpc/RpcConnect.ts:34
Server-side RPC connection handler. Receives client calls, invokes exported methods, and sends back results. Supports subscriptions, callback roundtrips, and broadcast dispatch.
Example
ts
const connect = new RpcConnect({
exports: { greet: (name: string) => `Hello, ${name}!` },
postMessage: send,
onMessage: listen,
});Extended by
Constructors
Constructor
ts
new RpcConnect(options): RpcConnect;Defined in: src/rpc/RpcConnect.ts:45
Parameters
| Parameter | Type |
|---|---|
options | | TRpcConnectOptions | TRpcConnectOptionsInit |
Returns
Properties
| Property | Modifier | Type | Default value | Defined in |
|---|---|---|---|---|
interrupt | static | <A>(millis?, params?) => Promise<A> | wait | src/rpc/RpcConnect.ts:35 |
Methods
destroy()
ts
destroy(): void;Defined in: src/rpc/RpcConnect.ts:57
Returns
void
dispatch()
ts
dispatch(data): void;Defined in: src/rpc/RpcConnect.ts:61
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
dispatchEncoded()
ts
dispatchEncoded(encodedData): void;Defined in: src/rpc/RpcConnect.ts:65
Parameters
| Parameter | Type |
|---|---|
encodedData | TRpcEncodedData |
Returns
void
RpcFnCoder
Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:17
Tracks and encodes/decodes functions across an RPC boundary. Internal functions (passed as arguments) are stored by index; external functions (received from the remote side) are lazily created via provider.
Example
ts
const coder = new RpcFnCoder((index) => (...args) => client.callFn(index, args));
const encoded = coder.encode(myFn); // => [0, 0]
const decoded = coder.decode(encoded); // => myFn (reconstructed)Constructors
Constructor
ts
new RpcFnCoder(provider): RpcFnCoder;Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:23
Parameters
| Parameter | Type |
|---|---|
provider | TRpcExtrernalFnProvider |
Returns
Properties
| Property | Type | Default value | Defined in |
|---|---|---|---|
externals | TRpcFn[] | [] | src/rpc/RpcCoder/RpcFnCoder.ts:19 |
internals | [TRpcFn, any][] | [] | src/rpc/RpcCoder/RpcFnCoder.ts:18 |
Methods
addInternal()
ts
addInternal(fn, ctx?): number;Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:27
Parameters
| Parameter | Type |
|---|---|
fn | TRpcFn |
ctx? | any |
Returns
number
decode()
ts
decode(encodedFnValue, withExternalFns?): TRpcFn;Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:63
Parameters
| Parameter | Type | Default value |
|---|---|---|
encodedFnValue | TRpcEncodedValueFn | undefined |
withExternalFns | boolean | true |
Returns
encode()
ts
encode(
fn,
context?,
withInternalFns?): TRpcEncodedValueFn;Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:40
Parameters
| Parameter | Type | Default value |
|---|---|---|
fn | TRpcFn | undefined |
context? | any | undefined |
withInternalFns? | boolean | true |
Returns
invoke()
ts
invoke(index, args): any;Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:35
Parameters
| Parameter | Type |
|---|---|
index | number |
args | any[] |
Returns
any
RpcSymbolCoder
Defined in: src/rpc/RpcCoder/RpcSymbolCoder.ts:15
Tracks and encodes/decodes symbol values across an RPC boundary. Internal symbols are indexed by position; external symbols are recreated from their string name.
Example
ts
const coder = new RpcSymbolCoder();
const encoded = coder.encode(Symbol('myKey'), 0); // => [0, 0, 0]
const decoded = coder.decode(encoded, ['myKey']); // => Symbol('myKey')Constructors
Constructor
ts
new RpcSymbolCoder(): RpcSymbolCoder;Returns
Methods
decode()
ts
decode(encodedSymbol, strings): symbol;Defined in: src/rpc/RpcCoder/RpcSymbolCoder.ts:38
Parameters
| Parameter | Type |
|---|---|
encodedSymbol | TRpcEncodedValueSymbol |
strings | string[] |
Returns
symbol
encode()
ts
encode(symbolValue, stringIndex): TRpcEncodedValueSymbol;Defined in: src/rpc/RpcCoder/RpcSymbolCoder.ts:19
Parameters
| Parameter | Type |
|---|---|
symbolValue | symbol |
stringIndex | number |
Returns
Interfaces
IRpcClient
Defined in: src/rpc/types.ts:326
Public interface implemented by RpcClient and RpcClientPool.
Example
ts
const client: IRpcClient = new RpcClient(opts);
const result = await client.call('add', [1, 2]);Extends
IEventEmitter<any>
Methods
call()
ts
call<A, R>(
method,
args?,
options?): Promise<R>;Defined in: src/rpc/types.ts:337
Calls a remote method and returns a Promise of its result.
Type Parameters
| Type Parameter | Default type |
|---|---|
A extends any[] | any[] |
R | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
method | string | Method name exported by RpcConnect. |
args? | A | Arguments to pass to the method. |
options? | TRpcClientRequestOptions | Optional timeout / abort signal / event names. |
Returns
Promise<R>
clear()
ts
clear(): void;Defined in: src/EventEmitter/types.ts:26
Removes all listeners without destroying the emitter.
Returns
void
Inherited from
destroy()
ts
destroy(): void;Defined in: src/rpc/types.ts:330
Destroys the client and releases resources.
Returns
void
Overrides
on()
ts
on(
event,
args?,
options?): TRpcUnsubscribe;Defined in: src/rpc/types.ts:348
Subscribes to a server-side event stream.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Event name. |
args? | any[] | Arguments forwarded to the server subscription handler. |
options? | TRpcClientRequestOptions | - |
Returns
An unsubscribe function.
proxy()
ts
proxy(): Promise<Record<string, any>>;Defined in: src/rpc/types.ts:350
Returns a proxy object where every property is an async method call.
Returns
Promise<Record<string, any>>
subscribe()
ts
subscribe(...listeners): IEventEmitterUnsubscribe;Defined in: src/EventEmitter/types.ts:22
Registers one or more listeners; returns a function that removes them all.
Parameters
| Parameter | Type |
|---|---|
...listeners | IEventEmitterListener<any>[] |
Returns
Inherited from
taskCount()
ts
taskCount(): number;Defined in: src/rpc/types.ts:328
Returns the number of currently pending calls.
Returns
number
IRpcConnect
Defined in: src/rpc/types.ts:432
Public interface implemented by RpcConnect.
Example
ts
const connect: IRpcConnect = new RpcConnect(opts);
connect.dispatch({ type: 'update', data });
connect.destroy();Methods
destroy()
ts
destroy(): void;Defined in: src/rpc/types.ts:434
Destroys the connection and releases resources.
Returns
void
dispatch()
ts
dispatch(data): void;Defined in: src/rpc/types.ts:436
Pushes arbitrary data to the connected client (decoded value).
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
dispatchEncoded()
ts
dispatchEncoded(encodedData): void;Defined in: src/rpc/types.ts:438
Pushes already-encoded data to the connected client (wire format).
Parameters
| Parameter | Type |
|---|---|
encodedData | any |
Returns
void
Type Aliases
TRpcClientMessage
ts
type TRpcClientMessage =
| TRpcClientMessageCall
| TRpcClientMessageSubCall
| TRpcClientMessageEvent
| TRpcClientMessageSubCallResult;Defined in: src/rpc/types.ts:86
Union of all messages the client can send.
TRpcClientMessageCall
ts
type TRpcClientMessageCall = TRpcMessage<Client,
| Call
| Subscribe
| MetaCall, [string, any]>;Defined in: src/rpc/types.ts:62
Client → server: regular call / subscribe / meta-call.
TRpcClientMessageEvent
ts
type TRpcClientMessageEvent = TRpcMessage<Client, Event, [string, any]>;Defined in: src/rpc/types.ts:80
Client → server: event emission.
TRpcClientMessageSubCall
ts
type TRpcClientMessageSubCall = TRpcMessageSubCall<Client, SubCall, [number, any]>;Defined in: src/rpc/types.ts:68
Client → server: invoke a function that was passed as an argument.
TRpcClientMessageSubCallResult
ts
type TRpcClientMessageSubCallResult = TRpcMessageSubCall<Client, SubResult, [0 | 1, any]>;Defined in: src/rpc/types.ts:74
Client → server: result of a server-initiated sub-call.
TRpcClientOptions
ts
type TRpcClientOptions = TRpcOptions & {
onMessage: TRpcClientOptionsOnMessage;
postMessage: TRpcClientOptionsPostMessage;
timeout?: number;
};Defined in: src/rpc/types.ts:307
Options passed to new RpcClient(options).
Type Declaration
| Name | Type | Description | Defined in |
|---|---|---|---|
onMessage | TRpcClientOptionsOnMessage | Registers a listener for incoming server messages. | src/rpc/types.ts:311 |
postMessage | TRpcClientOptionsPostMessage | Sends a message to the server. | src/rpc/types.ts:309 |
timeout? | number | Default call timeout in ms applied to every call() unless overridden. | src/rpc/types.ts:313 |
Example
ts
new RpcClient({
postMessage: (data) => worker.postMessage(data),
onMessage: (cb) => { worker.on('message', cb); return () => worker.off('message', cb); },
});TRpcClientOptionsInit
ts
type TRpcClientOptionsInit = () => TRpcClientOptions;Defined in: src/rpc/types.ts:317
Factory function that returns TRpcClientOptions (used for lazy init).
Returns
TRpcClientOptionsOnMessage
ts
type TRpcClientOptionsOnMessage = (callback) => TRpcUnsubscribe;Defined in: src/rpc/types.ts:296
Registers a listener for incoming messages; returns an unsubscribe fn.
Parameters
| Parameter | Type |
|---|---|
callback | TRpcClientOptionsOnMessageCallback |
Returns
TRpcClientOptionsOnMessageCallback
ts
type TRpcClientOptionsOnMessageCallback = (responseData) => void;Defined in: src/rpc/types.ts:293
Callback type for incoming server messages.
Parameters
| Parameter | Type |
|---|---|
responseData | TRpcConnectMessage |
Returns
void
TRpcClientOptionsPostMessage
ts
type TRpcClientOptionsPostMessage = (requestData) => TRpcUnsubscribe;Defined in: src/rpc/types.ts:290
Function that sends a message to the remote end; returns an unsubscribe fn.
Parameters
| Parameter | Type |
|---|---|
requestData | TRpcClientMessage |
Returns
TRpcClientPoolOptions
ts
type TRpcClientPoolOptions<Client> = {
maxWorkers?: number;
workerProvider: () => Client;
};Defined in: src/rpc/RpcClientPool.ts:9
Options for RpcClientPool.
Type Parameters
| Type Parameter | Default type |
|---|---|
Client extends IRpcClient | IRpcClient |
Properties
maxWorkers?
ts
optional maxWorkers?: number;Defined in: src/rpc/RpcClientPool.ts:11
Maximum number of workers to create (default: 1).
workerProvider
ts
workerProvider: () => Client;Defined in: src/rpc/RpcClientPool.ts:13
Factory that creates a new RpcClient instance.
Returns
Client
TRpcClientRequestOptions
ts
type TRpcClientRequestOptions = {
events?: string[];
signal?: EventTarget;
timeout?: number;
};Defined in: src/rpc/types.ts:274
Per-call options for RpcClient.call().
Example
ts
const ctrl = new AbortController();
client.call('longTask', [], { signal: ctrl.signal, timeout: 5000 });
ctrl.abort(); // cancels the callProperties
events?
ts
optional events?: string[];Defined in: src/rpc/types.ts:280
List of event names the server may emit during this call.
signal?
ts
optional signal?: EventTarget;Defined in: src/rpc/types.ts:278
AbortSignal-compatible object — abort it to cancel the call.
timeout?
ts
optional timeout?: number;Defined in: src/rpc/types.ts:276
Maximum ms to wait before rejecting with a timeout error.
TRpcClientTask
ts
type TRpcClientTask = [TRpcTaskResolve, TRpcCoder | null, Map<number, TRpcTaskResolve>];Defined in: src/rpc/types.ts:248
Internal state for a pending client-side call.
TRpcClientWorkerPoolOptionsInit
ts
type TRpcClientWorkerPoolOptionsInit<Client> = () => TRpcClientPoolOptions<Client>;Defined in: src/rpc/RpcClientPool.ts:17
Factory function that returns TRpcClientPoolOptions (used for lazy init).
Type Parameters
| Type Parameter | Default type |
|---|---|
Client extends IRpcClient | IRpcClient |
Returns
TRpcClientPoolOptions<Client>
TRpcCoder
ts
type TRpcCoder = {
decode: (encodedData, withInternalFns?) => any;
encode: (value, withInternalFns?) => TRpcEncodedData;
invoke: (fnIndex, args) => any | Promise<any>;
};Defined in: src/rpc/types.ts:230
Interface for the argument encoder/decoder used by RpcClient and RpcConnect.
Properties
decode
ts
decode: (encodedData, withInternalFns?) => any;Defined in: src/rpc/types.ts:234
Decodes a wire-safe TRpcEncodedData back to a JavaScript value.
Parameters
| Parameter | Type |
|---|---|
encodedData | TRpcEncodedData | undefined | null |
withInternalFns? | boolean |
Returns
any
encode
ts
encode: (value, withInternalFns?) => TRpcEncodedData;Defined in: src/rpc/types.ts:232
Encodes any JavaScript value into a wire-safe TRpcEncodedData.
Parameters
| Parameter | Type |
|---|---|
value | any |
withInternalFns? | boolean |
Returns
invoke
ts
invoke: (fnIndex, args) => any | Promise<any>;Defined in: src/rpc/types.ts:239
Invokes a locally-registered function by its index with the given args.
Parameters
| Parameter | Type |
|---|---|
fnIndex | number |
args | any[] |
Returns
any | Promise<any>
TRpcConnectMessage
ts
type TRpcConnectMessage =
| TRpcConnectMessageSubCall
| TRpcConnectMessageResult
| TRpcConnectMessageSubCallResult
| TRpcConnectMessageDispatch;Defined in: src/rpc/types.ts:119
Union of all messages the server can send.
TRpcConnectMessageDispatch
ts
type TRpcConnectMessageDispatch = [Server, [Dispatch, any]];Defined in: src/rpc/types.ts:98
Server → client: server-push without a prior request.
TRpcConnectMessageResult
ts
type TRpcConnectMessageResult = TRpcMessage<Server, Result, [0 | 1, any]>;Defined in: src/rpc/types.ts:92
Server → client: result of a call (success or error).
TRpcConnectMessageSubCall
ts
type TRpcConnectMessageSubCall = TRpcMessageSubCall<Server, SubCall, [number, any]>;Defined in: src/rpc/types.ts:107
Server → client: invoke a function passed by the client as an argument.
TRpcConnectMessageSubCallResult
ts
type TRpcConnectMessageSubCallResult = TRpcMessageSubCall<Server, SubResult, [0 | 1, any]>;Defined in: src/rpc/types.ts:113
Server → client: result of a client-initiated sub-call.
TRpcConnectOptions
ts
type TRpcConnectOptions = TRpcOptions & {
exports: TRpcConnectOptionsExports;
getConnections?: TRpcConnectOptionsGetConnections;
onMessage: TRpcConnectOptionsOnMessage;
postMessage: TRpcConnectOptionsPostMessage;
};Defined in: src/rpc/types.ts:395
Options passed to new RpcConnect(options).
Type Declaration
| Name | Type | Description | Defined in |
|---|---|---|---|
exports | TRpcConnectOptionsExports | Methods exposed to the client. | src/rpc/types.ts:401 |
getConnections? | TRpcConnectOptionsGetConnections | Optional: returns all active connections (used for dispatchToAll). | src/rpc/types.ts:403 |
onMessage | TRpcConnectOptionsOnMessage | Registers a listener for incoming client messages. | src/rpc/types.ts:399 |
postMessage | TRpcConnectOptionsPostMessage | Sends a message to the client. | src/rpc/types.ts:397 |
Example
ts
new RpcConnect({
exports: { add: (a, b) => a + b },
postMessage: (data) => client.handleMessage(data),
onMessage: (cb) => { ... return unsubscribe; },
});TRpcConnectOptionsExportFn
ts
type TRpcConnectOptionsExportFn = (this, ...args) => any | Promise<any>;Defined in: src/rpc/types.ts:419
A single exported method on the server side. Receives TRpcTaskContext as this.
Parameters
| Parameter | Type |
|---|---|
this | TRpcTaskContext |
...args | any[] |
Returns
any | Promise<any>
TRpcConnectOptionsExports
ts
type TRpcConnectOptionsExports = Record<string, TRpcConnectOptionsExportFn>;Defined in: src/rpc/types.ts:422
Map of method names to their implementations, passed to RpcConnect.
TRpcConnectOptionsGetConnections
ts
type TRpcConnectOptionsGetConnections = () => IRpcConnect[];Defined in: src/rpc/types.ts:356
Returns all active RpcConnect instances in a pool.
Returns
TRpcConnectOptionsInit
ts
type TRpcConnectOptionsInit = () => TRpcConnectOptions;Defined in: src/rpc/types.ts:407
Factory function that returns TRpcConnectOptions (used for lazy init).
Returns
TRpcConnectOptionsOnMessage
ts
type TRpcConnectOptionsOnMessage = (callback) => TRpcUnsubscribe;Defined in: src/rpc/types.ts:416
Registers a listener for incoming messages; returns an unsubscribe fn.
Parameters
| Parameter | Type |
|---|---|
callback | TRpcConnectOptionsOnMessageCallback |
Returns
TRpcConnectOptionsOnMessageCallback
ts
type TRpcConnectOptionsOnMessageCallback = (responseData) => void;Defined in: src/rpc/types.ts:413
Callback type for incoming client messages.
Parameters
| Parameter | Type |
|---|---|
responseData | MessageEvent<TRpcClientMessage> |
Returns
void
TRpcConnectOptionsPostMessage
ts
type TRpcConnectOptionsPostMessage = (requestData) => TRpcUnsubscribe;Defined in: src/rpc/types.ts:410
Sends a message to the client; returns an unsubscribe fn.
Parameters
| Parameter | Type |
|---|---|
requestData | TRpcConnectMessage |
Returns
TRpcConnectTask
ts
type TRpcConnectTask = [TRpcTaskContext, TRpcCoder | null, Map<number, TRpcTaskResolve>];Defined in: src/rpc/types.ts:255
Internal state for an active server-side call.
TRpcEncodedData
ts
type TRpcEncodedData =
| [TRpcEncodedValue, TRpcEncodedDataObject[], TRpcEncodedDataArray[], string[]]
| null
| undefined
| number
| string;Defined in: src/rpc/types.ts:214
Full encoded payload transmitted on the wire. Contains a root value plus interned string/object/array pools, or a primitive shorthand when the value is a simple JSON-safe type.
TRpcEncodedDataArray
ts
type TRpcEncodedDataArray = TRpcEncodedValue[];Defined in: src/rpc/types.ts:204
Encoded array: flat list of encoded values.
TRpcEncodedDataObject
ts
type TRpcEncodedDataObject = TRpcEncodedKeyValue[];Defined in: src/rpc/types.ts:207
Encoded object: list of key-value pairs.
TRpcEncodedKeyValue
ts
type TRpcEncodedKeyValue = [number, TRpcEncodedValue];Defined in: src/rpc/types.ts:201
Encoded object key-value pair: [stringIndex, encodedValue].
TRpcEncodedValue
ts
type TRpcEncodedValue =
| number
| null
| boolean
| [Other, TRpcEncodedValueOther]
| [Symbol, TRpcEncodedValueSymbol]
| [BigInt, number]
| [String, number]
| [Function, TRpcEncodedValueFn]
| [Array, number]
| [Object, number]
| [Date, number]
| [RegExp, number]
| [Promise, TRpcEncodedValueFn]
| [Error, number];Defined in: src/rpc/types.ts:165
Tagged-union type for a single encoded value on the wire.
TRpcEncodedValueFn
ts
type TRpcEncodedValueFn = [0 | 1, number];Defined in: src/rpc/types.ts:153
Encoded representation of a function: [external, fnIndex].
TRpcEncodedValueSymbol
ts
type TRpcEncodedValueSymbol = [0 | 1, number, number];Defined in: src/rpc/types.ts:150
Encoded representation of a Symbol: [external, symbolIndex, stringIndex].
TRpcEventTarget
ts
type TRpcEventTarget = EventTarget & {
aborted: boolean;
};Defined in: src/rpc/types.ts:225
AbortSignal-compatible object used to cancel in-flight RPC calls.
Type Declaration
| Name | Type | Defined in |
|---|---|---|
aborted | boolean | src/rpc/types.ts:226 |
TRpcExtrernalFnProvider
ts
type TRpcExtrernalFnProvider = (fnIndex) => (...args) => Promise<any>;Defined in: src/rpc/types.ts:222
Resolves an external function index to a callable that returns a Promise.
Parameters
| Parameter | Type |
|---|---|
fnIndex | number |
Returns
(...args) => Promise<any>
TRpcFn
ts
type TRpcFn = (...args) => any | Promise<any>;Defined in: src/rpc/types.ts:156
Any function that can be used as an RPC argument or return value.
Parameters
| Parameter | Type |
|---|---|
...args | any[] |
Returns
any | Promise<any>
TRpcFnDecode
ts
type TRpcFnDecode = (value) => TRpcFn;Defined in: src/rpc/types.ts:162
Decodes a wire function reference back into a callable.
Parameters
| Parameter | Type |
|---|---|
value | TRpcEncodedValueFn |
Returns
TRpcFnEncode
ts
type TRpcFnEncode = (value) => TRpcEncodedValueFn;Defined in: src/rpc/types.ts:159
Encodes a function reference into its wire representation.
Parameters
| Parameter | Type |
|---|---|
value | TRpcFn |
Returns
TRpcMessage
ts
type TRpcMessage<Agent, Type, Data> = [Agent, [Type, string, ...Data]];Defined in: src/rpc/types.ts:38
Base wire-format envelope for any RPC message.
Type Parameters
| Type Parameter |
|---|
Agent extends TRpcAgent |
Type extends TRpcType |
Data extends any[] |
TRpcMessageSubCall
ts
type TRpcMessageSubCall<Agent, Type, Data> = TRpcMessage<Agent, Type, [number, ...Data]>;Defined in: src/rpc/types.ts:52
Wire-format envelope for sub-call messages (both directions).
Type Parameters
| Type Parameter |
|---|
Agent extends TRpcAgent |
Type extends | SubCall | SubResult |
Data extends any[] |
TRpcOptions
ts
type TRpcOptions = {
serializable?: boolean;
};Defined in: src/rpc/types.ts:284
Shared base options for both RpcClient and RpcConnect.
Properties
serializable?
ts
optional serializable?: boolean;Defined in: src/rpc/types.ts:286
When true, arguments are serialized before sending (required across Worker boundaries).
TRpcTaskContext
ts
type TRpcTaskContext = {
dispatch: (data) => void;
dispatchToAll: (data) => void;
getConnections: TRpcConnectOptionsGetConnections;
interrupt: <A>(time?, params?) => Promise<A>;
signal: TRpcEventTarget;
};Defined in: src/rpc/types.ts:372
Context object injected as this into every exported server method.
Example
ts
const connect = new RpcConnect({
exports: {
async heavyTask(this: TRpcTaskContext, data: any) {
await this.interrupt(5000); // pause for up to 5 s
this.dispatch({ progress: 50 });
},
},
...transport,
});Properties
dispatch
ts
dispatch: (data) => void;Defined in: src/rpc/types.ts:380
Sends data to the calling client.
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
dispatchToAll
ts
dispatchToAll: (data) => void;Defined in: src/rpc/types.ts:382
Sends data to all connected clients.
Parameters
| Parameter | Type |
|---|---|
data | any |
Returns
void
getConnections
ts
getConnections: TRpcConnectOptionsGetConnections;Defined in: src/rpc/types.ts:374
Returns all active connections in the pool.
interrupt
ts
interrupt: <A>(time?, params?) => Promise<A>;Defined in: src/rpc/types.ts:378
Pauses execution for up to time ms; resolves with params or rejects on abort.
Type Parameters
| Type Parameter | Default type |
|---|---|
A | any |
Parameters
| Parameter | Type |
|---|---|
time? | number |
params? | A |
Returns
Promise<A>
signal
ts
signal: TRpcEventTarget;Defined in: src/rpc/types.ts:376
Abort signal — aborted when the client cancels the call.
TRpcTaskResolve
ts
type TRpcTaskResolve = (result, isError) => void;Defined in: src/rpc/types.ts:245
Resolves a pending RPC task with a result or error.
Parameters
| Parameter | Type |
|---|---|
result | any |
isError | 0 | 1 |
Returns
void
TRpcUnsubscribe
ts
type TRpcUnsubscribe = () => void;Defined in: src/rpc/types.ts:264
Function returned by subscriptions and on() — call it to unsubscribe.
Returns
void