RpcSerialization.ts
RpcSerialization.ts overview
Section titled “RpcSerialization.ts overview”Serializes RPC protocol messages for transports.
RpcSerialization is the boundary between RpcMessage envelopes and the
bytes or strings carried by a transport. This module provides built-in
serializers for JSON, newline-delimited JSON, JSON-RPC 2.0, and MessagePack,
including framed formats that can decode multiple messages from streaming
chunks.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”serialization
Section titled “serialization”Parser (interface)
Section titled “Parser (interface)”A stateful parser for an RPC serialization format, able to decode input chunks into protocol messages and encode messages for transport.
Signature
export interface Parser { readonly decode: (data: Uint8Array | string) => ReadonlyArray<unknown> readonly encode: (response: unknown) => Uint8Array | string | undefined}Since v4.0.0
RpcSerialization (class)
Section titled “RpcSerialization (class)”Service that describes how RPC protocol messages are encoded and decoded, including the content type and whether the serialization format provides message framing.
When to use
Use to provide the serialization boundary shared by RPC clients and servers for a chosen wire format.
Signature
declare class RpcSerializationSince v4.0.0
JSON RPC serialization for whole message payloads. It does not include message framing, so it is intended for transports that frame responses themselves.
Signature
declare const json: { makeUnsafe(): Parser; readonly contentType: string; readonly includesFraming: boolean }Since v4.0.0
jsonRpc
Section titled “jsonRpc”Creates a JSON-RPC 2.0 serialization for RPC protocol messages without additional message framing.
Signature
declare const jsonRpc: (options?: { readonly contentType?: string | undefined }) => RpcSerialization["Service"]Since v4.0.0
layerJson
Section titled “layerJson”RPC serialization layer that uses JSON for serialization.
When to use
Use when you have a transport protocol that already provides message framing.
See
layerNdjsonfor transports that need newline-delimited framing
Signature
declare const layerJson: Layer.Layer<RpcSerialization, never, never>Since v4.0.0
layerJsonRpc
Section titled “layerJsonRpc”RPC serialization layer that uses JSON-RPC for serialization.
Signature
declare const layerJsonRpc: (options?: { readonly contentType?: string | undefined }) => Layer.Layer<RpcSerialization>Since v4.0.0
layerMsgPack
Section titled “layerMsgPack”RPC serialization layer that uses MessagePack for serialization.
Details
MessagePack has a more compact binary format compared to JSON and NDJSON. It also has better support for binary data.
Signature
declare const layerMsgPack: Layer.Layer<RpcSerialization, never, never>Since v4.0.0
layerNdJsonRpc
Section titled “layerNdJsonRpc”RPC serialization layer that uses newline-delimited JSON-RPC for serialization.
Signature
declare const layerNdJsonRpc: (options?: { readonly contentType?: string | undefined }) => Layer.Layer<RpcSerialization>Since v4.0.0
layerNdjson
Section titled “layerNdjson”RPC serialization layer that uses NDJSON for serialization.
When to use
Use when you have a transport protocol that does not provide message framing.
See
layerJsonfor transports that already provide message framing
Signature
declare const layerNdjson: Layer.Layer<RpcSerialization, never, never>Since v4.0.0
makeMsgPack
Section titled “makeMsgPack”Create a MessagePack serialization with custom msgpackr options.
Signature
declare const makeMsgPack: (options?: Msgpackr.Options | undefined) => RpcSerialization["Service"]Since v4.0.0
msgPack
Section titled “msgPack”Default MessagePack RPC serialization using record support and built-in message framing.
Signature
declare const msgPack: { makeUnsafe(): Parser; readonly contentType: string; readonly includesFraming: boolean }Since v4.0.0
ndJsonRpc
Section titled “ndJsonRpc”Creates a newline-delimited JSON-RPC 2.0 serialization for RPC protocol messages.
Signature
declare const ndJsonRpc: (options?: { readonly contentType?: string | undefined }) => RpcSerialization["Service"]Since v4.0.0
ndjson
Section titled “ndjson”Serializes RPC protocol messages as newline-delimited JSON, framing each message with a trailing newline.
Signature
declare const ndjson: { makeUnsafe(): Parser; readonly contentType: string; readonly includesFraming: boolean }Since v4.0.0