Channels
createChannel(functions, options) makes a channel that owns functions. The side that made it is the owner. A side that got it from connect() or inside a value is a holder.
const counter = createChannel({
increment(by: number) {
return by + 1;
},
});| Member | Description |
|---|---|
run(name, ...args) | Calls name and resolves with its result. Arguments and the return type come from the function type. |
notify(name, ...args) | Calls name and does not wait. It returns nothing, and a failure is dropped. |
with({ signal }) | Returns a caller whose run and notify cancel when signal aborts. See Cancelling. |
close() | Closes the channel for the owner and every holder. |
closed | A promise that resolves once the channel closed, from either end. |
- Calls on one channel start on the owner in the order they were made.
- The owner can call its own channel. The call runs in place and does not go through the endpoint.
- Function names are strings passed to
run, so a function may be calledrun,notify,withorclose. - The functions may be a
type, aninterfaceortypeofan object. runon a generator function returns a promise of the generator. Await it before the firstnext().
options.onClose() runs once when the channel closes. Its returned promise is awaited before closed resolves. A throw is reported as an unhandled rejection and not rethrown.
Inside a function, this is a CallContext:
| Field | Description |
|---|---|
this.signal | An RpcSignal that aborts when the caller cancels, the channel closes or the endpoint closes. It stays valid across await. |
this.channel | The channel the call came in on, for example to close it. |
this is typed as CallContext in an object passed to createChannel with no annotation. In a separate type, write this: CallContext on the methods that use it, as scan and quit do above.
The endpoint
createEndpoint(port, options) ties channels to one port. options is optional.
| Method | Description |
|---|---|
serve(channel) | Makes channel the root, which the other side gets from connect(). It takes a channel made by createChannel, and only once. |
connect<T>() | Returns the root channel the other side served, as a holder. It returns at once, and its calls wait until the other side calls serve. Each call returns the same channel. |
close() | Closes every channel and the port. |
| Option | Description |
|---|---|
plugins | seroval plugins for the values in calls and results. Both sides need the same plugins. |
onCall(call, next) | Wraps every incoming call. See onCall. |
format | How values become messages. Defaults to JSON. Pass binaryFormat from @seroval/rpc/binary to use binary mode. Both sides need the same format. |
window | The most stream items the other side may send before this side has consumed them. Defaults to 16. It must be a positive integer, or createEndpoint throws a RangeError. |
Each side can serve a root and connect to the other side's root, so calls go in both directions. T in connect<T>() is not checked against what the other side serves.
servethrows aTypeErrorfor a value not made bycreateChannel, anRpcTransportErrorwhen called twice, and anRpcClosedErrorafter the endpoint closed.- Calls that arrive before
servewait for it. - After
serve, a call to a channel this side does not own fails withRpcClosedError. A notify to one is dropped.
Formats
An EndpointFormat is what format takes. binaryFormat is the only one besides the JSON default.
| Member | Description |
|---|---|
createWriter(options) | Returns a transport Writer that sends each value through options.send, and reports a value that failed to options.onError. |
createReader(options) | Returns a transport Reader for the messages of the writer. |
isStreamItem(message) | True when a data message carries one more item of a stream. The credit window counts these. |
options.plugins holds the plugins of the endpoint. With binaryFormat, each needs a binary section.
Ports
A Port is the only thing the core knows about the connection:
interface Port {
send(message: unknown): void;
onMessage(listener: (message: unknown) => void): () => void;
onError?(listener: (error: unknown) => void): () => void;
onClose?(listener: () => void): () => void;
close?(): void;
}| Member | Description |
|---|---|
send(message) | Sends one message. A throw fails the endpoint with RpcTransportError. |
onMessage(listener) | Passes on each message, and returns a function that stops listening. Buffer messages until a listener attaches, as a MessagePort does, or the first message is lost. |
onError(listener) | Optional. A failure of the connection fails the endpoint with RpcTransportError. |
onClose(listener) | Optional. The other end went away. The endpoint closes and calls in flight fail with RpcClosedError. Without it, calls in flight wait when the other end goes away. |
close() | Optional. Called when the endpoint closes. |
Messages are plain arrays of numbers, strings, null and SerovalNode objects, so postMessage sends them as is. For a WebSocket, send JSON.stringify(message) and pass on JSON.parse(event.data).
fromNodeWorker(source) from @seroval/rpc/node adapts a worker_threads Worker or parentPort:
errorandmessageerrorevents go toonError, andexitandclosetoonClose.close()closes aMessagePortsuch asparentPort. It does not callworker.terminate(), since that would drop messages still on the way. The worker closes its own port when its endpoint closes, and then exits.
fromMessagePort(source) from @seroval/rpc/post-message adapts a web Worker, a MessagePort or the worker's own scope (self):
- It calls
start()on aMessagePort, so messages reach the listener. errorandmessageerrorevents go toonError. Acloseevent goes toonClose. AMessagePorthas one, but a webWorkerdoes not.close()callssource.close()when the source has it.
Errors
| Error | When |
|---|---|
RpcClosedError | The channel or the endpoint closed before the call finished, or the port closed. It is also the reason of this.signal when the caller cancels or the endpoint closes. |
RpcTransportError | The port failed, the arguments of a call cannot be sent, or the other side sent a message rpc cannot read, including a different protocol version. |
RpcRemoteError | The function threw on the other side, or its result cannot be sent. remoteName holds the remote error's name. |
Only name and message of a remote error cross, never the stack, the cause or other fields. A remote error named RpcClosedError arrives as RpcClosedError, and any other as RpcRemoteError. A thrown value that is not an object arrives with the name Error and the value as text.
A failure of the transport fails every call in flight with the same RpcTransportError, and closes the endpoint.
Limits
Streams:
- A result stream the caller never reads stays held on the other side until the endpoint closes. Its producer stops after
windowitems. Read it, callreturn()on it, or cancel the call. - Streams get the window only inside arrays, plain objects, maps and sets. A stream anywhere else, such as inside a plugin value, stops after
windowitems. - A cancel for a call that still waits for
serveis lost, and the call runs. Anotifycannot be cancelled. - The window counts seroval's
StreamNextnodes, which seroval does not export. JSON mode reads the node type from the message.binaryFormatreads the last node of each chunk after the first.
Channels:
- A channel sent and never closed stays in the endpoint until the endpoint closes. Close it from either end.
- A channel received from the other side cannot be sent back to it. Only channels made by
createChannelcross, so the call fails. - A call to a channel ID this side does not own waits when the endpoint never called
serve. - Calls that wait for
servehave no limit on their number.
Generators:
- Every native generator and async generator in a value is two-way, with one round trip per step and no window. There is no marker to send one as a one-way stream. Wrap it in a plain async iterable instead.
- A receiver that drops a generator without
return()leaves its hidden channel in the endpoint until the endpoint closes. - Closing an endpoint sends no close message for the channels it owns. The other side learns through
Port.onClose. With a port that has noonClose, such asfromMessagePorton a webWorker, a pendingnext()keeps waiting.
Other:
- Buffers are copied into the message. There is no transfer of an
ArrayBufferto the other side, in binary mode too. endpoint.close(reason)ignores the reason. Calls in flight fail withRpcClosedError.- A channel is not closed when it is garbage collected.