msgpack-rpc-lite
Advanced tools
Comparing version 0.0.1 to 0.0.2
34
index.js
@@ -28,5 +28,9 @@ /* | ||
function Client(port = 9199, host = 'localhost', timeout = 0, codecOptions = { encode: {}, decode: {} }) { | ||
function Client(port, host = 'localhost', timeout = 0, codecOptions = { encode: {}, decode: {} }) { | ||
events.EventEmitter.call(this); | ||
assert.equal(typeof port, 'number', 'Illegal argument: port'); | ||
assert.equal(typeof host, 'string', 'Illegal argument: host'); | ||
assert.equal(typeof timeout, 'number', 'Illegal argument: timeout'); | ||
const self = this; | ||
@@ -88,4 +92,6 @@ const socketEvents = [ 'connect', 'end', 'timeout', 'drain', 'error', 'close' ]; | ||
function _call(type, method, params, callback) { | ||
const message = [ type ].concat(type === 0 ? msgidGenerator.next() : [], [ method, [].concat(params) ] ); | ||
function _call(type, method, ...args) { | ||
const callback = typeof args.slice(-1) === 'function' && args.pop(); | ||
const params = args; | ||
const message = [ type ].concat(type === 0 ? msgidGenerator.next() : [], method, [ params ] ); | ||
if (callback) { | ||
@@ -102,8 +108,8 @@ this.send(message, callback); | ||
function request(method, params, callback) { | ||
return _call.call(this, 0, method, params, callback); | ||
function request(method, ...args) { | ||
return _call.apply(this, [ 0, method ].concat(args)); | ||
} | ||
function notify(method, params, callback) { | ||
return _call.call(this, 2, method, params, callback); | ||
function notify(method, ...args) { | ||
return _call.apply(this, [ 2, method ].concat(args)); | ||
} | ||
@@ -122,7 +128,7 @@ | ||
exports.createServer = function createServer(options, connectionListener, codecOptions = { encode: {}, decode: {} }) { | ||
exports.createServer = function createServer(options, codecOptions = { encode: {}, decode: {} }) { | ||
const encodeCodec = msgpack.createCodec((codecOptions || {}).encode); | ||
const decodeCodec = msgpack.createCodec((codecOptions || {}).decode); | ||
const server = net.createServer(options, connectionListener); | ||
server.on('connection', function onConnection(socket) { | ||
const connectionListener = function onConnection(socket) { | ||
const self = this; | ||
socket.pipe(msgpack.createDecodeStream({ codec: decodeCodec })).on('data', message => { | ||
@@ -132,3 +138,3 @@ debug(message); | ||
const [ , msgid, method, params ] = message; // Request message | ||
server.emit(method, params, (error, result) => { | ||
self.emit(method, params, (error, result) => { | ||
const encodeStream = msgpack.createEncodeStream({ codec: encodeCodec }); | ||
@@ -141,7 +147,7 @@ encodeStream.pipe(socket); | ||
const [ , method, params ] = message; // Notification message | ||
server.emit(method, params); | ||
self.emit(method, params); | ||
} | ||
}); | ||
}); | ||
return server; | ||
}; | ||
return net.createServer(options, connectionListener); | ||
}; |
{ | ||
"name": "msgpack-rpc-lite", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Implementation of MessagePack-RPC with msgpack-lite", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# msgpack-rpc-lite | ||
[![npm version](https://badge.fury.io/js/msgpack-rpc-lite.svg)](https://badge.fury.io/js/msgpack-rpc-lite) | ||
[![Build Status](https://travis-ci.org/naokikimura/msgpack-rpc-lite.svg?branch=master)](https://travis-ci.org/naokikimura/msgpack-rpc-lite) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/85c5e44e31da475ebcaae8f1b79de7c8)](https://app.codacy.com/app/n.kimura.cap/msgpack-rpc-lite?utm_source=github.com&utm_medium=referral&utm_content=naokikimura/msgpack-rpc-lite&utm_campaign=badger) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/5a32022009694006ab61191e243e569f)](https://www.codacy.com/app/n.kimura.cap/msgpack-rpc-lite?utm_source=github.com&utm_medium=referral&utm_content=naokikimura/msgpack-rpc-lite&utm_campaign=Badge_Coverage) | ||
Implementation of MessagePack-RPC with msgpack-lite | ||
Implementation of MessagePack-RPC with msgpack-lite | ||
## Usage ## | ||
- __createServer([*serverOptions*][, *codecOptions*])__ | ||
Creates a new MessagePack-RPC server. | ||
- `serverOptions` <Object> See [net.createServer([options][, connectionListener])](https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) | ||
- `codecOptions` <Object> | ||
- `encode` See [Custom Codec Options][1] | ||
- `decode` See [Custom Codec Options][1] | ||
- Returns: <net.Server> | ||
- __Server event: *method*__ | ||
Emitted when a new connection is made. | ||
- <Array> request parameters. | ||
- <Function> If a request is received, a callback function is passed. | ||
- To return the results to the client, pass `null` as the first argument and response parameters as the second argument. | ||
- If an error occurs, pass it to the first argument. | ||
- __createClient(*port*[, *host*][, *timeout*][, *codecOptions*])__ | ||
Initiates a MessagePack-RPC client. | ||
- `port` <number> Port the socket should connect to. | ||
- `host` <string> Host the socket should connect to. Default: `'localhost'` | ||
- `timeout` <number> Sets the socket to timeout after timeout milliseconds of inactivity on the socket. If timeout is 0, then the existing idle timeout is disabled. Default: `0` | ||
- `codecOptions` <Object> | ||
- `encode` See [Custom Codec Options][1] | ||
- `decode` See [Custom Codec Options][1] | ||
- Return: <Client> | ||
- __client.request(*method*[, *...args*])__ | ||
- `method` <string> | ||
- Return: <Promise> | ||
- __client.notify(*method*[, *...args*])__ | ||
- `method` <string> | ||
- Return: <Promise> | ||
[1]: https://github.com/kawanet/msgpack-lite#custom-codec-options | ||
## Examples ## | ||
- Server | ||
```js | ||
const rpc = require('msgpack-rpc-lite'); | ||
const server = rpc.createServer().on('say', (params, callback) => { | ||
const [ message ] = params; | ||
callback(null, `hello ${ message }`); | ||
}); | ||
server.listen(9199); | ||
``` | ||
- Client | ||
```js | ||
const rpc = require('msgpack-rpc-lite'); | ||
const client = rpc.createClient(9199, 'localhost'); | ||
client.request('say', [ 'world' ]).then(([ response ]) => { | ||
console.log(response); // hello world | ||
}) | ||
``` | ||
## Compatibility Mode ## | ||
Set `true` to `useraw` of `encode`/`decode` of Codec-options. | ||
- Client | ||
```js | ||
const codecOptions = { encode: { useraw: true }, decode: { useraw: true } }; | ||
const client = rpc.createClient(9199, 'localhost', 0, codecOptions); | ||
``` | ||
See also: | ||
- https://github.com/kawanet/msgpack-lite#compatibility-mode | ||
- https://github.com/msgpack/msgpack/blob/master/spec.md#upgrading-messagepack-specification | ||
## See also ## | ||
- [MessagePack-RPC Specification](https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md) | ||
- http://frsyuki.hatenablog.com/entry/20100406/p1 | ||
- [MessagePack specification](https://github.com/msgpack/msgpack/blob/master/spec.md) | ||
- [msgpack-lite](https://github.com/kawanet/msgpack-lite) |
@@ -24,5 +24,5 @@ const expect = require('chai').expect; | ||
server = rpc.createServer().on('foo', (params, callback) => { | ||
server = rpc.createServer().on('foo', (params, done) => { | ||
expect(params).to.have.ordered.members([ 1, 2, 3 ]); | ||
callback(null, 'bar'); | ||
done(null, 'bar'); | ||
}); | ||
@@ -32,3 +32,3 @@ server.listen(port); | ||
client = rpc.createClient(port); | ||
return client.request('foo', [ 1, 2, 3]); | ||
return client.request('foo', 1, 2, 3); | ||
}).then(([ response ]) => { | ||
@@ -51,3 +51,3 @@ expect(response).to.have.ordered.members([ 'bar' ]); | ||
client = rpc.createClient(port); | ||
client.notify('qux', [ 1, 2, 3]); | ||
client.notify('qux', 1, 2, 3); | ||
}).catch(done); | ||
@@ -54,0 +54,0 @@ }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15845
226
97