Skip to content

fundamentool


rpc ​

Enumerations ​

TRpcAgent ​

Defined in: src/rpc/types.ts:6

Identifies which side of the channel sent a message.

Enumeration Members ​

Enumeration MemberValueDescriptionDefined in
Client1Message originated from the client (RpcClient side).src/rpc/types.ts:10
Server0Message 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 MemberValueDefined in
Array6src/rpc/types.ts:134
BigInt2src/rpc/types.ts:130
Date7src/rpc/types.ts:135
Error10src/rpc/types.ts:138
Function4src/rpc/types.ts:132
Object5src/rpc/types.ts:133
Other0src/rpc/types.ts:128
Promise9src/rpc/types.ts:137
RegExp8src/rpc/types.ts:136
String3src/rpc/types.ts:131
Symbol1src/rpc/types.ts:129

TRpcEncodedValueOther ​

Defined in: src/rpc/types.ts:142

Sub-tag for TRpcEncodedType.Other — non-JSON-serializable primitives.

Enumeration Members ​

Enumeration MemberValueDefined in
Infinity2src/rpc/types.ts:145
NaN1src/rpc/types.ts:144
NegativeInfinity3src/rpc/types.ts:146
Undefined0src/rpc/types.ts:143

TRpcType ​

Defined in: src/rpc/types.ts:16

All possible RPC message types used on the wire.

Enumeration Members ​

Enumeration MemberValueDescriptionDefined in
Call0Regular method call from client to server.src/rpc/types.ts:18
Dispatch5Server pushes data to the client without a request.src/rpc/types.ts:28
Event4Event emitted by the client side.src/rpc/types.ts:26
MetaCall7Meta call (introspection, e.g. proxy()).src/rpc/types.ts:32
Result1Response from server to client.src/rpc/types.ts:20
SubCall2Cross-thread sub-call (server calling a function passed as an argument).src/rpc/types.ts:22
SubResult3Response to a sub-call.src/rpc/types.ts:24
Subscribe6Subscribe 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 ​
ParameterType
options| TRpcClientOptions | TRpcClientOptionsInit
Returns ​

RpcClient

Overrides ​

EventEmitter.constructor

Properties ​

PropertyModifierTypeDefined in
DEFAULT_EVENTSstaticstring[]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 ParameterDefault type
Rany
Parameters ​
ParameterTypeDescription
methodstringMethod name exported by RpcConnect.
args?any[]Arguments to pass to the method.
options?TRpcClientRequestOptionsOptional timeout / abort signal / event names.
Returns ​

Promise<R>

Implementation of ​

IRpcClient.call

clear() ​
ts
clear(): void;

Defined in: src/EventEmitter/index.ts:53

Removes all listeners without destroying the emitter.

Returns ​

void

Implementation of ​

IRpcClient.clear

Inherited from ​

EventEmitter.clear

destroy() ​
ts
destroy(): void;

Defined in: src/rpc/RpcClient.ts:63

Destroys the client and releases resources.

Returns ​

void

Implementation of ​

IRpcClient.destroy

Overrides ​

EventEmitter.destroy

emit() ​
ts
protected emit(data): void;

Defined in: src/EventEmitter/index.ts:27

Parameters ​
ParameterType
dataany
Returns ​

void

Inherited from ​

EventEmitter.emit

on() ​
ts
on(
   event, 
   args?, 
   options?): TRpcUnsubscribe;

Defined in: src/rpc/RpcClient.ts:72

Subscribes to a server-side event stream.

Parameters ​
ParameterTypeDescription
eventstringEvent name.
args?any[]Arguments forwarded to the server subscription handler.
options?TRpcClientRequestOptions-
Returns ​

TRpcUnsubscribe

An unsubscribe function.

Implementation of ​

IRpcClient.on

once() ​
ts
once(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:46

Parameters ​
ParameterType
...listenersIEventEmitterListener<any>[]
Returns ​

() => void

Inherited from ​

EventEmitter.once

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 ​

IRpcClient.proxy

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 ​
ParameterType
...listenersIEventEmitterListener<any>[]
Returns ​

() => void

Implementation of ​

IRpcClient.subscribe

Inherited from ​

EventEmitter.subscribe

taskCount() ​
ts
taskCount(): number;

Defined in: src/rpc/RpcClient.ts:59

Returns the number of currently pending calls.

Returns ​

number

Implementation of ​

IRpcClient.taskCount


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 ParameterDefault type
Client extends IRpcClientIRpcClient

Implements ​

Constructors ​

Constructor ​
ts
new RpcClientPool<Client>(options): RpcClientPool<Client>;

Defined in: src/rpc/RpcClientPool.ts:101

Parameters ​
ParameterType
options| TRpcClientWorkerPoolOptionsInit<Client> | TRpcClientPoolOptions<Client>
Returns ​

RpcClientPool<Client>

Overrides ​

EventEmitter.constructor

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 ParameterDefault type
A extends any[]any[]
Rany
Parameters ​
ParameterType
...args[string, A, TRpcClientRequestOptions]
Returns ​

Promise<R>

Implementation of ​

IRpcClient.call

clear() ​
ts
clear(): void;

Defined in: src/EventEmitter/index.ts:53

Removes all listeners without destroying the emitter.

Returns ​

void

Implementation of ​

IRpcClient.clear

Inherited from ​

EventEmitter.clear

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 ​

IRpcClient.destroy

Overrides ​

EventEmitter.destroy

emit() ​
ts
protected emit(data): void;

Defined in: src/EventEmitter/index.ts:27

Parameters ​
ParameterType
dataany
Returns ​

void

Inherited from ​

EventEmitter.emit

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 ​
ParameterTypeDefault valueDescription
countnumber0Number 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 ​
ParameterType
...args[string, any[], TRpcClientRequestOptions]
Returns ​

TRpcUnsubscribe

Implementation of ​

IRpcClient.on

once() ​
ts
once(...listeners): () => void;

Defined in: src/EventEmitter/index.ts:46

Parameters ​
ParameterType
...listenersIEventEmitterListener<any>[]
Returns ​

() => void

Inherited from ​

EventEmitter.once

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 ​

IRpcClient.proxy

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 ​
ParameterType
...listenersIEventEmitterListener<any>[]
Returns ​

() => void

Implementation of ​

IRpcClient.subscribe

Inherited from ​

EventEmitter.subscribe

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 ​

IRpcClient.taskCount


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 ​
ParameterType
extrenalFnProvider?TRpcExtrernalFnProvider
options?{ useSymols?: boolean; }
options.useSymols?boolean
Returns ​

RpcCoder

Properties ​

PropertyModifierTypeDefault valueDefined in
fnCoderpublicRpcFnCodernullsrc/rpc/RpcCoder/index.ts:65
NON_ENCODABLE_TYPESstaticstring[]NON_ENCODABLE_TYPESsrc/rpc/RpcCoder/index.ts:63

Methods ​

decode() ​
ts
decode(encodedData, withExternalFns?): any;

Defined in: src/rpc/RpcCoder/index.ts:246

Parameters ​
ParameterTypeDefault value
encodedDataTRpcEncodedDataundefined
withExternalFnsbooleantrue
Returns ​

any

encode() ​
ts
encode(value, withInternalFns?): TRpcEncodedData;

Defined in: src/rpc/RpcCoder/index.ts:83

Parameters ​
ParameterTypeDefault value
valueanyundefined
withInternalFnsbooleantrue
Returns ​

TRpcEncodedData

invoke() ​
ts
invoke(index, args): any;

Defined in: src/rpc/RpcCoder/index.ts:79

Parameters ​
ParameterType
indexnumber
argsany[]
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 ​
ParameterType
options| TRpcConnectOptions | TRpcConnectOptionsInit
Returns ​

RpcConnect

Properties ​

PropertyModifierTypeDefault valueDefined in
interruptstatic<A>(millis?, params?) => Promise<A>waitsrc/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 ​
ParameterType
dataany
Returns ​

void

dispatchEncoded() ​
ts
dispatchEncoded(encodedData): void;

Defined in: src/rpc/RpcConnect.ts:65

Parameters ​
ParameterType
encodedDataTRpcEncodedData
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 ​
ParameterType
providerTRpcExtrernalFnProvider
Returns ​

RpcFnCoder

Properties ​

PropertyTypeDefault valueDefined in
externalsTRpcFn[][]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 ​
ParameterType
fnTRpcFn
ctx?any
Returns ​

number

decode() ​
ts
decode(encodedFnValue, withExternalFns?): TRpcFn;

Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:63

Parameters ​
ParameterTypeDefault value
encodedFnValueTRpcEncodedValueFnundefined
withExternalFnsbooleantrue
Returns ​

TRpcFn

encode() ​
ts
encode(
   fn, 
   context?, 
   withInternalFns?): TRpcEncodedValueFn;

Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:40

Parameters ​
ParameterTypeDefault value
fnTRpcFnundefined
context?anyundefined
withInternalFns?booleantrue
Returns ​

TRpcEncodedValueFn

invoke() ​
ts
invoke(index, args): any;

Defined in: src/rpc/RpcCoder/RpcFnCoder.ts:35

Parameters ​
ParameterType
indexnumber
argsany[]
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 ​

RpcSymbolCoder

Methods ​

decode() ​
ts
decode(encodedSymbol, strings): symbol;

Defined in: src/rpc/RpcCoder/RpcSymbolCoder.ts:38

Parameters ​
ParameterType
encodedSymbolTRpcEncodedValueSymbol
stringsstring[]
Returns ​

symbol

encode() ​
ts
encode(symbolValue, stringIndex): TRpcEncodedValueSymbol;

Defined in: src/rpc/RpcCoder/RpcSymbolCoder.ts:19

Parameters ​
ParameterType
symbolValuesymbol
stringIndexnumber
Returns ​

TRpcEncodedValueSymbol

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 ​

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 ParameterDefault type
A extends any[]any[]
Rany
Parameters ​
ParameterTypeDescription
methodstringMethod name exported by RpcConnect.
args?AArguments to pass to the method.
options?TRpcClientRequestOptionsOptional 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 ​

IEventEmitter.clear

destroy() ​
ts
destroy(): void;

Defined in: src/rpc/types.ts:330

Destroys the client and releases resources.

Returns ​

void

Overrides ​

IEventEmitter.destroy

on() ​
ts
on(
   event, 
   args?, 
   options?): TRpcUnsubscribe;

Defined in: src/rpc/types.ts:348

Subscribes to a server-side event stream.

Parameters ​
ParameterTypeDescription
eventstringEvent name.
args?any[]Arguments forwarded to the server subscription handler.
options?TRpcClientRequestOptions-
Returns ​

TRpcUnsubscribe

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 ​
ParameterType
...listenersIEventEmitterListener<any>[]
Returns ​

IEventEmitterUnsubscribe

Inherited from ​

IEventEmitter.subscribe

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 ​
ParameterType
dataany
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 ​
ParameterType
encodedDataany
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 ​

NameTypeDescriptionDefined in
onMessageTRpcClientOptionsOnMessageRegisters a listener for incoming server messages.src/rpc/types.ts:311
postMessageTRpcClientOptionsPostMessageSends a message to the server.src/rpc/types.ts:309
timeout?numberDefault 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 ​

TRpcClientOptions


TRpcClientOptionsOnMessage ​

ts
type TRpcClientOptionsOnMessage = (callback) => TRpcUnsubscribe;

Defined in: src/rpc/types.ts:296

Registers a listener for incoming messages; returns an unsubscribe fn.

Parameters ​

ParameterType
callbackTRpcClientOptionsOnMessageCallback

Returns ​

TRpcUnsubscribe


TRpcClientOptionsOnMessageCallback ​

ts
type TRpcClientOptionsOnMessageCallback = (responseData) => void;

Defined in: src/rpc/types.ts:293

Callback type for incoming server messages.

Parameters ​

ParameterType
responseDataTRpcConnectMessage

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 ​

ParameterType
requestDataTRpcClientMessage

Returns ​

TRpcUnsubscribe


TRpcClientPoolOptions ​

ts
type TRpcClientPoolOptions<Client> = {
  maxWorkers?: number;
  workerProvider: () => Client;
};

Defined in: src/rpc/RpcClientPool.ts:9

Options for RpcClientPool.

Type Parameters ​

Type ParameterDefault type
Client extends IRpcClientIRpcClient

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 call

Properties ​

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 ParameterDefault type
Client extends IRpcClientIRpcClient

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 ​
ParameterType
encodedDataTRpcEncodedData | 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 ​
ParameterType
valueany
withInternalFns?boolean
Returns ​

TRpcEncodedData

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 ​
ParameterType
fnIndexnumber
argsany[]
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 ​

NameTypeDescriptionDefined in
exportsTRpcConnectOptionsExportsMethods exposed to the client.src/rpc/types.ts:401
getConnections?TRpcConnectOptionsGetConnectionsOptional: returns all active connections (used for dispatchToAll).src/rpc/types.ts:403
onMessageTRpcConnectOptionsOnMessageRegisters a listener for incoming client messages.src/rpc/types.ts:399
postMessageTRpcConnectOptionsPostMessageSends 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 ​

ParameterType
thisTRpcTaskContext
...argsany[]

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 ​

IRpcConnect[]


TRpcConnectOptionsInit ​

ts
type TRpcConnectOptionsInit = () => TRpcConnectOptions;

Defined in: src/rpc/types.ts:407

Factory function that returns TRpcConnectOptions (used for lazy init).

Returns ​

TRpcConnectOptions


TRpcConnectOptionsOnMessage ​

ts
type TRpcConnectOptionsOnMessage = (callback) => TRpcUnsubscribe;

Defined in: src/rpc/types.ts:416

Registers a listener for incoming messages; returns an unsubscribe fn.

Parameters ​

ParameterType
callbackTRpcConnectOptionsOnMessageCallback

Returns ​

TRpcUnsubscribe


TRpcConnectOptionsOnMessageCallback ​

ts
type TRpcConnectOptionsOnMessageCallback = (responseData) => void;

Defined in: src/rpc/types.ts:413

Callback type for incoming client messages.

Parameters ​

ParameterType
responseDataMessageEvent<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 ​

ParameterType
requestDataTRpcConnectMessage

Returns ​

TRpcUnsubscribe


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 ​

NameTypeDefined in
abortedbooleansrc/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 ​

ParameterType
fnIndexnumber

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 ​

ParameterType
...argsany[]

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 ​

ParameterType
valueTRpcEncodedValueFn

Returns ​

TRpcFn


TRpcFnEncode ​

ts
type TRpcFnEncode = (value) => TRpcEncodedValueFn;

Defined in: src/rpc/types.ts:159

Encodes a function reference into its wire representation.

Parameters ​

ParameterType
valueTRpcFn

Returns ​

TRpcEncodedValueFn


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 ​
ParameterType
dataany
Returns ​

void

dispatchToAll ​
ts
dispatchToAll: (data) => void;

Defined in: src/rpc/types.ts:382

Sends data to all connected clients.

Parameters ​
ParameterType
dataany
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 ParameterDefault type
Aany
Parameters ​
ParameterType
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 ​

ParameterType
resultany
isError0 | 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

MIT License