rpc-protocol
Create and run commands over a RPC protocol stream
Installation
$ npm install rpc-protocol
Usage
Echo server over web sockets:
const WebSocketServer = const protocol = const server = port: 3000 server { const rpc = rpc}
Echo client over web sockets
const WebSocket = const protocol = const socket = 'ws://localhost:3000' socket { const rpc = rpc}
API
rpc = require('rpc-protocol')(opts)
Creates a new RPC protocol Duplex stream where opts
can be:
opts.encoding
is an optional encoding object that contains anencode(value)
anddecode(buffer)
functions for converting values to and from buffers that can be sent over a binary streamopts.stream
is an optional stream to pump data in and out of into the protocol stream
rpc.command(commandName, callback)
Create a callback for a command given by the string commandName
that
is called when rpc.call(commandName, ...)
is called from the client
and where callback
contains a command request object. Results can be
returned to the caller by returning a value, which can be a Promise
.
Example
Below is a simple example of a command that proxies a fetch()
call over
the stream. The caller would simply need to call rpc.call('fetch', ['https://github.com', { mode: 'cors' }, ], callback)
.
rpc
Command Streams
Commands can stream responses by to the client by making use of the
second argument given to the command callback called reply(err, results)
.
It is a function that accepts an optional error and an array of results to
send back to the client.
Below is an example of a simple counter stream. Given a start
and
end
range with an interval
the command will reply with an
incremented i
value at some interval
.
rpc
didWrite = reply(err, response)
The reply()
function replies to the caller with an error err
and an
array of results as a response
. The command can continue to call this
function with more results or errors. The return value of the reply
function will indicate if the stream is still open to write to.
command = rpc.call(commandName, arguments, callback)
A command can be invoked by calling rpc.call()
with a command name
and an optional array of arguments along with a callback(err, res)
function that will be called when the command responds with a reply.
Response results are always given as an array as the command may
return more than value. A command can only throw one error at a time.
rpc
stream = rpc.createReadStream(command)
Read streams can be created from an existing command. They are useful if the command can reply with multiple values over a period of time.
rpc const command = rpcconst stream = rpcconst chunks streamstream
rpc.send(...)
rpc.send()
is the low level function that makes a request for an
existing command, an extension, or an arbitrary buffer.
Sending An Extension
Sending an extension message requires you use the extension type and some value that the extension encodes.
rpc
Sending A Command
Sending a command requires you create an instance of Command
with an
encoding, command name, array of arguments, and a callback that is
called when the command replies with a response
const command = rpcencoding 'echo' 'hello' { // called when the command replies with a response} rpc
Sending An Arbitrary Message
Sending an arbitrary message is possible but replies cannot be linked to the sent message and there is no guarantee that the message was read.
rpc
rpc.cancel(command)
An alias to rpc.fin(command)
.
rpc.fin(command)
Send a Fin
packet to the stream tied to the command request.
const command = rpcrpc
rpc.extension(extensionType, encoding)
Extensions provide a way to extend the protocol with user supplied
binary encodings. The rpc.extension(extensionType, encoding)
function
accepts an integer extensionType
and an encoding
object that
contains encode(value)
and decode(buffer)
functions for encoding
values to and from buffers.
Extensions can be used by making use of the rpc.send()
function that
expects an extensionType
, an array of arguments that will be encoded
by the extension encoding and a callback that will be called when the
extension replies with a response.
Extension Wire Interface
Extensions must provide a way of encoding an id
into the extension
encoding which must be available after decode(buffer)
is called on
the return value. The id
is used internally to track requests and
response from callers.
If an extension provides a way to encode and decode Error
properties,
then they will be propagated to the err
argument in the reply(err)
function.
Example Extension
const keyPair = const pbs = const KEY_PAIR_EXTENSION = 0xfedconst KeyPair = const server = const client = serverclient server client
License
MIT