VSOA app framework like Express.js
const vppjs = require('vppjs');
const vpp = vppjs.vpp();
const router = vppjs.router();
const devices = {};
router.dgram('/ping', (req, res) => {
return res.datagram('pong');
});
router.get('/file/', (req, res) => {
const fileId = req.payload.param;
fs.createReadStream('./files/' + fileId).pipe(res.createStream());
});
router.set('/user/', (req, res) => {
const userInfo = req.payload.param;
devices[userInfo.id] = userInfo;
res.reply('ok');
});
vpp.use('/foo', router);
Create a new instance of Vpp class.
Create a new instance of VppRouter class.
These events are emitted when client is connected or disconnected:
- cli: VSOA remote client object
- server: VSOA Server object
vpp.on('connect', (cli, server) => {
// do something
})
In addition to standard VSOA Remote Client events, the cli object of vppjs will also emit 'disconnect' event on disconnection.
options
: {VppOptions}info
: {Object}, for VSOA server info, default{ name: 'Vpp.js' }
passwd
: {String}, for VSOA server authenticationtlsOpt
: {Object}, Node.js TLS options object, for VSOA TLS server setupdefaultErrorCode
: {Number}, range from 128 to 255, default 199. This is used by the default error handler to send reply for unhandled server error.
const { Vpp } = require('vppjs')
const vpp = new Vpp()
port
: {Number}, port number the VSOA server to listenhost
: {String}, host name the VSOA server to listenbacklog
: {Number}, TCP connection backlog, check Node.js document for more informationcallback
: {Function}, callback function when the server socket is listening
callback
: {Function}, callback function when the server is stopped
payload
: {VsoaPayload}, the raw VSOA request payload object orundefined
param
: {String|Object}, payload param datadata
: {Buffer}, optional payload dataoffset
: {Number}, optional offset of payload data inside the given bufferlength
: {Number}, optional length of actual payload data inside the buffer
quick
: {Boolean}, optional flag to incide normal or quick VSOA data channelurlpath
: {String}, optional url path of publishement, defaults '/'
url
: {String}, url to be checked if has been subscribed by a client
Note: unlike Expres.js, the Vpp or VppRouter instance for now, cannot use
arbitrary (req, res, nex) => {}
middleware function, but router intances only!
subPath
: {String}, the mounting url sub path (relative to the parent router/vpp) for the given sub routersrouters
: {VppRouter}, the sub routers to mount
subPath
: {String}, the mounting url sub path for the given RPC handlers.getHandlers
: {VppRpcHandler}, handlers that will be called once there are GET RPC calls to the joined URL (parent url path + subPath).
subPath
: {String}, the mounting url sub path for the given RPC handlers.setHandlers
: {VppRpcHandler}, handlers that will be called once there are SET RPC calls to the joined URL.
subPath
: {String}, the mounting url sub path for the given Datagram handlers.dgramHandlers
: {DgramHandler}, handlers that will be called once there are DGRAM messages sent to the joined URL.
payload
: VppPayload orundefined
quick
: {Boolean}, optional flag to incide normal or quick VSOA data channelsubPath
: {String}, optional url path of publishement, defaults '/'.
When working with VppRouter object, APIs that accept VppPayload can help automatically convert it into the raw VsoaPayload:
- If input is an object and
- a. it contains 'param' field then raw VSOA payload is assumed;
- b. it contains 'data' field of Buffer then raw VSOA payload is assumed;
- c. OTHERWISE plain object is assumed, send it as 'param'.
- if input is string, send it as 'param'.
- if input is Buffer, send it as 'data'.
- if input is number, convert it to string then send it as 'param'. This is
because VSOA IGNORES number in
payload.param
. - otherwise, input will be IGNORED.
If this function returns a Promise, routers mounted after this one will be called once it is resolved, otherwise the router chain will break if the promise is rejected.
If this function returns anything other than a promise object. Developer can
still use the last next
callback to trigger following routers. If next
is
omitted, Vpp will break the router chain right after currrent handler call.
req
: VppRpcRequestres
: VppRpcResponsenext
: optional Node.js style callback function
VSOA RemoteClient object, which could be used to call original VSOA client API.
A string of the targeting url of the incoming RPC call.
Number of the raw RPC call seqno
.
Enum value of VsoaRpcMethod
, an integer number of VSOA RPC call method: 0 = GET, 1 = SET.
Incoming raw {VsoaPayload} object, see publish section
Raw VSOA Server object, which might be used for advanced usecases.
payload
: {VppPayload}, see VppPayload section.code
: {Number}, optional VSOA status code, default 0.seqno
: {Number}, sequence number to respond, default to the one fromreq
.
payload
: {VppPayload} orundefined
, see VppPayload section.url
: {String}, optional url to publish, default to the incoming RPC call url.
payload
: {VppPayload}, see VppPayload section.url
: {String}, optional url to send the datagram, default to the incoming RPC call url.
payload
: {VppPayload|undefined}, see VppPayload section.timeout
: {Number}, optional timeout in milliseconds, use default VSOA value.code
: {Number}, optional RPC reponse code, default 0.seqno
: {Number}, optional RPC sequence number, default to the one fromreq
.
Router handling logic is same as VppRpcHandler.
req
: {VppDgramRequest}res
: {VppDgramResponse}next
: Node.js style callback function
A string of the targeting url of this incoming datagram.
VSOA RemoteClient object, which could be used to call original VSOA client API.
Incoming raw {VsoaPayload} object, see publish section
Boolean flag indicating if this datagram was sent via the quick data channel.
Raw VSOA Server object, which might be used for advanced usecases.
payload
: {VppPayload}, see VppPayload section.url
: {String}, optional url to publish, default to the incoming datagram's url.
payload
: {VppPayload}, see VppPayload section.quick
: {Boolean}, optional flag to incide normal or quick VSOA data channelurl
: {String}, optional targeting url of this replying datagram, default to the incoming datagram's url.
TODO
- more unit tests
- vpp middleware can intercept all sub route requests
- accept error handlers
- simple request logging middleware
- scaffolding scripts