Small abstraction to help build out user defined extension messages in an RPC system.
npm install abstract-extension
See hypercore and hypercore-protocol for a full example on how to use this
Imports the AbstractExtension class. You should extend this and add the functionality you need.
Detroy an extension instance. Removes the message from the local pairing instance.
The local id of the message. Send this over the wire instead of the message name after exchanging the initial message names.
True if the remote also supports this message. Note that nothing bad will having from sending a message the remote does not support.
Encode a message to a buffer based on the message encoding.
Create a local message pairing instance.
Whenever the messages are updated local.onextensionupdate()
will be called if provided.
Add a new message. name
should be the string name of a message.
handlers.encoding
is an optional encoding for the message payload. Can be eitherjson
,utf-8
,binary
or any abstract encoding.handlers.onmessage(message, context)
is called when a message has been received and pairing.handlers.onerror(error, context)
is called when a message fails to decode.
Returns a sorted list of message names. You need to pass this to another remote pairing instance somehow.
Call this to setup remote pairing.
Pass the names of another instance to setup the pairing
Pair the remote id with the corresponding local message and call the onmessage handler.
Optionally pass a context object that is simply passed along to the message.onmessage
function
MIT