LogoPear Docs
ReferencesBareModules

bare-rpc

librpc ABI compatible RPC for Bare

stable

bare-rpc — librpc ABI compatible RPC for Bare.

npm i bare-rpc

Usage

import RPC from 'bare-rpc'

// On one end
const rpc = new RPC(stream, (req) => {
  if (req.command === 42) {
    console.log(req.data.toString()) // ping
    req.reply('pong')
  }
})

// On the other end
const req = rpc.request(42)
req.send('ping')

const replyBuffer = await req.reply()
console.log(replyBuffer.toString()) // pong

API

RPC

new RPC(stream: Duplex, onrequest: (req: RPCIncomingRequest) => void | Promise<void>)

Source

Create an RPC instance using a duplex stream. onrequest is an optional callback run when a remote request is received. This is where processing and responding to an RPC request happens. onrequest receives a RPCIncomingRequest as an argument.

Parameters

ParameterTypeDefaultDescription
streamDuplexThe duplex stream to frame RPC messages over, such as a pipe or socket.
onrequest(req: RPCIncomingRequest) => void | Promise<void>Callback run for each incoming request or event, receiving an RPCIncomingRequest (or RPCIncomingEvent) to inspect and reply() to. Defaults to a no-op.

event(command: number): RPCOutgoingEvent

Source

Create an outgoing event for command. Events are one-way: unlike request() they carry no id and expect no reply, so the remote end receives them as an RPCIncomingEvent.

Parameters

ParameterTypeDefaultDescription
commandnumberA unique number identifying the event; the remote end differentiates events by it.

Returns RPCOutgoingEvent — an RPCOutgoingEvent; call send() on it to dispatch the one-way event.

idle: boolean

Source

Whether there are no requests or responses currently in flight. Useful for determining when it is safe to tear down the underlying stream.

request(command: number): RPCOutgoingRequest

Source

Create a request for command. command is a unique number that should be used to differentiate different requests on the remote end.

Parameters

ParameterTypeDefaultDescription
commandnumberA unique number identifying the request; the remote end differentiates requests by it.

Returns RPCOutgoingRequest — an RPCOutgoingRequest; call send() on it to dispatch the request and await reply() for the response.

RPCIncomingEvent

new RPCIncomingEvent(rpc: RPC, command: number, data: Buffer)

Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance the event arrived on.
commandnumberThe command number the event was sent with.
dataBufferThe payload buffer sent with the event.

RPCIncomingEvent.command: number

Source

The command that the request was created with. A command is a unique number.

RPCIncomingEvent.data: Buffer | null

Source

The data buffer sent with the request.

RPCIncomingEvent.rpc: RPC

Source

RPCOutgoingEvent

new RPCOutgoingEvent(rpc: RPC, command: number)

Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance to send the event on.
commandnumberThe command number to send.

RPCOutgoingEvent.command: number

Source

The command that the request was created with. A command is a unique number.

RPCOutgoingEvent.rpc: RPC

Source

RPCOutgoingEvent.send(data?: Buffer | string | null, encoding?: BufferEncoding): void

Source

Send the request with the provided data. data can be a buffer or a string which will be encoded using encoding.

Parameters

ParameterTypeDefaultDescription
data?Buffer | string | nullThe payload to send: a Buffer, or a string encoded using encoding. Omit to send no data.
encoding?BufferEncodingThe encoding used when data is a string (defaults to utf8).

Throws

  • ALREADY_SENT — the event has already been sent.

RPCOutgoingEvent.sent: boolean

Source

A boolean for whether the request has been sent.

RPCIncomingRequest

new RPCIncomingRequest(rpc: RPC, id: number, command: number, data: Buffer)

Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance the request arrived on.
idnumberThe request id, used to correlate the reply with the request.
commandnumberThe command number the request was sent with.
dataBufferThe payload buffer sent with the request.

RPCIncomingRequest.command: number

Source

The command that the request was created with. A command is a unique number.

createRequestStream(opts?: ReadableOptions): RPCIncomingStream

Source

Create a Readable stream for receiving the request's streamed data.

Parameters

ParameterTypeDefaultDescription
opts?ReadableOptionsOptions for the returned Readable stream.

Throws

  • ALREADY_RECEIVED — the request has already been received.

createResponseStream(opts?: WritableOptions): RPCOutgoingStream

Source

Create a Writable stream for sending the streamed reply to the request.

Parameters

ParameterTypeDefaultDescription
opts?WritableOptionsOptions for the returned Writable stream.

Throws

  • ALREADY_SENT — a response has already been sent for this request.

RPCIncomingRequest.data: Buffer | null

Source

The data buffer sent with the request.

RPCIncomingRequest.id: number

Source

RPCIncomingRequest.received: boolean

Source

A boolean for whether the request has received a reply.

reply(data?: Buffer | string | null, encoding?: BufferEncoding): void

Source

Send a reply to the request. data may be a buffer or a string encoded using encoding.

Parameters

ParameterTypeDefaultDescription
data?Buffer | string | nullThe payload to send: a Buffer, or a string encoded using encoding. Omit to send no data.
encoding?BufferEncodingThe encoding used when data is a string (defaults to utf8).

Throws

  • ALREADY_SENT — a response has already been sent for this request.

RPCIncomingRequest.rpc: RPC

Source

RPCIncomingRequest.sent: boolean

Source

A boolean for whether the request has been sent.

RPCOutgoingRequest

new RPCOutgoingRequest(rpc: RPC, id: number, command: number)

Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance to send the request on.
idnumberThe request id, used to correlate the reply with the request.
commandnumberThe command number to send.

RPCOutgoingRequest.command: number

Source

The command that the request was created with. A command is a unique number.

createRequestStream(opts?: WritableOptions): RPCOutgoingStream

Source

Create a Writable stream for sending data with the request.

Parameters

ParameterTypeDefaultDescription
opts?WritableOptionsOptions for the returned Writable stream.

Throws

  • ALREADY_SENT — the request has already been sent.

createResponseStream(opts?: ReadableOptions): RPCIncomingStream

Source

Create a Readable stream for receiving data in reply to the request.

Parameters

ParameterTypeDefaultDescription
opts?ReadableOptionsOptions for the returned Readable stream.

Throws

  • ALREADY_RECEIVED — the response has already been received.

RPCOutgoingRequest.id: number

Source

RPCOutgoingRequest.received: boolean

Source

A boolean for whether the request has received a reply.

reply(encoding?: BufferEncoding): Promise<Buffer | string | null>

Source

Await the reply from the remote end to the request. encoding can be defined for decoding the response data buffer back into a string.

Parameters

ParameterTypeDefaultDescription
encoding?BufferEncodingIf given, decodes the reply payload to a string using this encoding; omit to receive the raw Buffer.

Returns Promise<Buffer | string | null> — a promise that resolves with the remote end's reply payload, or rejects with the channel's teardown error if it closes before a reply arrives.

Throws

  • ALREADY_RECEIVED — a reply is already being received for this request.

RPCOutgoingRequest.rpc: RPC

Source

RPCOutgoingRequest.send(data?: Buffer | string | null, encoding?: BufferEncoding): void

Source

Send the request with the provided data. data can be a buffer or a string which will be encoded using encoding.

Parameters

ParameterTypeDefaultDescription
data?Buffer | string | nullThe payload to send: a Buffer, or a string encoded using encoding. Omit to send no data.
encoding?BufferEncodingThe encoding used when data is a string (defaults to utf8).

Throws

  • ALREADY_SENT — the request has already been sent.

RPCOutgoingRequest.sent: boolean

Source

A boolean for whether the request has been sent.

RPCIncomingStream

RPCIncomingStream

new RPCIncomingStream(rpc: RPC, request: RPCIncomingRequest | RPCOutgoingRequest, type: typeof constants.type.REQUEST | typeof constants.type.RESPONSE, opts?: ReadableOptions)
Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance the stream belongs to.
requestRPCIncomingRequest | RPCOutgoingRequestThe request the stream carries data for.
typetypeof constants.type.REQUEST | typeof constants.type.RESPONSEWhether the stream carries the request body (constants.type.REQUEST) or the response body (constants.type.RESPONSE).
opts?ReadableOptionsOptions for the underlying Readable stream.

RPCOutgoingStream

RPCOutgoingStream

new RPCOutgoingStream(rpc: RPC, request: RPCIncomingRequest | RPCOutgoingRequest, type: typeof constants.type.REQUEST | typeof constants.type.RESPONSE, opts?: WritableOptions)
Source

Parameters

ParameterTypeDefaultDescription
rpcRPCThe RPC instance the stream belongs to.
requestRPCIncomingRequest | RPCOutgoingRequestThe request the stream carries data for.
typetypeof constants.type.REQUEST | typeof constants.type.RESPONSEWhether the stream carries the request body (constants.type.REQUEST) or the response body (constants.type.RESPONSE).
opts?WritableOptionsOptions for the underlying Writable stream.

bare-rpc/errors

RPCError

RPCError.ALREADY_RECEIVED(msg: string): RPCError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

RPCError.ALREADY_SENT(msg: string): RPCError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

RPCError.CHANNEL_CLOSED(msg: string): RPCError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

RPCError.UNKNOWN_MESSAGE(msg: string): RPCError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

See also

On this page

Usage
API
RPC
new RPC(stream: Duplex, onrequest: (req: RPCIncomingRequest) => void | Promise<void>)
event(command: number): RPCOutgoingEvent
idle: boolean
request(command: number): RPCOutgoingRequest
RPCIncomingEvent
new RPCIncomingEvent(rpc: RPC, command: number, data: Buffer)
RPCIncomingEvent.command: number
RPCIncomingEvent.data: Buffer | null
RPCIncomingEvent.rpc: RPC
RPCOutgoingEvent
new RPCOutgoingEvent(rpc: RPC, command: number)
RPCOutgoingEvent.command: number
RPCOutgoingEvent.rpc: RPC
RPCOutgoingEvent.send(data?: Buffer | string | null, encoding?: BufferEncoding): void
RPCOutgoingEvent.sent: boolean
RPCIncomingRequest
new RPCIncomingRequest(rpc: RPC, id: number, command: number, data: Buffer)
RPCIncomingRequest.command: number
createRequestStream(opts?: ReadableOptions): RPCIncomingStream
createResponseStream(opts?: WritableOptions): RPCOutgoingStream
RPCIncomingRequest.data: Buffer | null
RPCIncomingRequest.id: number
RPCIncomingRequest.received: boolean
reply(data?: Buffer | string | null, encoding?: BufferEncoding): void
RPCIncomingRequest.rpc: RPC
RPCIncomingRequest.sent: boolean
RPCOutgoingRequest
new RPCOutgoingRequest(rpc: RPC, id: number, command: number)
RPCOutgoingRequest.command: number
createRequestStream(opts?: WritableOptions): RPCOutgoingStream
createResponseStream(opts?: ReadableOptions): RPCIncomingStream
RPCOutgoingRequest.id: number
RPCOutgoingRequest.received: boolean
reply(encoding?: BufferEncoding): Promise<Buffer | string | null>
RPCOutgoingRequest.rpc: RPC
RPCOutgoingRequest.send(data?: Buffer | string | null, encoding?: BufferEncoding): void
RPCOutgoingRequest.sent: boolean
RPCIncomingStream
RPCIncomingStream
RPCOutgoingStream
RPCOutgoingStream
bare-rpc/errors
RPCError
RPCError.ALREADY_RECEIVED(msg: string): RPCError
RPCError.ALREADY_SENT(msg: string): RPCError
RPCError.CHANNEL_CLOSED(msg: string): RPCError
RPCError.UNKNOWN_MESSAGE(msg: string): RPCError
See also