msgpack-rpc-lite
Advanced tools
Comparing version 0.0.3 to 0.0.4
107
index.js
@@ -28,38 +28,30 @@ /* | ||
function Client(port, host = 'localhost', timeout = 0, codecOptions = { encode: {}, decode: {} }) { | ||
function Client(connectOptions, codecOptions) { | ||
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; | ||
const socketEvents = [ 'connect', 'end', 'timeout', 'drain', 'error', 'close' ]; | ||
const encodeCodec = msgpack.createCodec((codecOptions || {}).encode); | ||
const decodeCodec = msgpack.createCodec((codecOptions || {}).decode); | ||
function send(message, callback = function () {}) { | ||
const socket = net.connect(port, host); | ||
socket.setTimeout(timeout); | ||
debug({ socket }); | ||
Object.defineProperties(this, { | ||
'encodeCodec': { | ||
get() { return encodeCodec; } | ||
}, | ||
'decodeCodec': { | ||
get() { return decodeCodec; } | ||
}, | ||
'connectOptions': { | ||
get() { return connectOptions; }, | ||
set(value) { connectOptions = value; } | ||
}, | ||
'close': { | ||
// It is left for compatibility with v0.0.2 or earlier. | ||
get() { return (() => { }); } | ||
} | ||
}); | ||
} | ||
socketEvents.forEach(eventName => { | ||
socket.on(eventName, (...args) => { | ||
debug(`socket event [${ eventName }]`); | ||
self.emit.apply(self, [eventName].concat(args)); | ||
}); | ||
}); | ||
if (message[0] === 0) { | ||
socket.pipe(msgpack.createDecodeStream({ codec: decodeCodec })).on('data', message => { | ||
if (debug.enabled) { debug(`received message: ${ util.inspect(message, false, null, true) }`); } | ||
socket.end(); | ||
const [ type, msgid, error, result ] = message; // Response message | ||
assert.equal(type, 1); | ||
assert.equal(msgid, message[1]); | ||
callback.call(self, error, result, msgid); | ||
}); | ||
} | ||
const encodeStream = msgpack.createEncodeStream({ codec: encodeCodec }); | ||
function send(message, callback = function () {}) { | ||
const self = this; | ||
const socket = net.createConnection(this.connectOptions, () => { | ||
const encodeStream = msgpack.createEncodeStream({ codec: this.encodeCodec }); | ||
encodeStream.pipe(socket); | ||
@@ -71,24 +63,27 @@ encodeStream.write(message, (...args) => { | ||
encodeStream.end(); | ||
} | ||
Object.defineProperty(this, 'encodeCodec', { | ||
get() { return encodeCodec; } | ||
}); | ||
debug({ socket }); | ||
Object.defineProperty(this, 'decodeCodec', { | ||
get() { return decodeCodec; } | ||
const socketEvents = [ 'connect', 'end', 'timeout', 'drain', 'error', 'close' ]; | ||
socketEvents.forEach(eventName => { | ||
socket.on(eventName, (...args) => { | ||
debug(`socket event [${ eventName }]`); | ||
self.emit.apply(self, [eventName].concat(args)); | ||
}); | ||
}); | ||
Object.defineProperty(this, 'send', { | ||
get() { return send; }, | ||
enumerable: false | ||
}); | ||
if (message[0] === 0) { | ||
socket.pipe(msgpack.createDecodeStream({ codec: this.decodeCodec })).on('data', message => { | ||
if (debug.enabled) { debug(`received message: ${ util.inspect(message, false, null, true) }`); } | ||
// It is left for compatibility with v0.6 or earlier. | ||
Object.defineProperty(this, 'close', { | ||
get() { return (() => {}); } | ||
}); | ||
socket.end(); | ||
const [ type, msgid, error, result ] = message; // Response message | ||
assert.equal(type, 1); | ||
assert.equal(msgid, message[1]); | ||
callback.call(self, error, result, msgid); | ||
}); | ||
} | ||
} | ||
function _call(type, method, ...args) { | ||
function call(type, method, ...args) { | ||
const callback = typeof args[args.length - 1] === 'function' && args.pop(); | ||
@@ -98,6 +93,6 @@ const params = args; | ||
if (callback) { | ||
this.send(message, callback); | ||
send.call(this, message, callback); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
this.send(message, (error, ...args) => { | ||
send.call(this, message, (error, ...args) => { | ||
if (error) { reject(error); } else { resolve(args); } | ||
@@ -110,11 +105,11 @@ }); | ||
function request(method, ...args) { | ||
return _call.apply(this, [ 0, method ].concat(args)); | ||
return call.apply(this, [ 0, method ].concat(args)); | ||
} | ||
function notify(method, ...args) { | ||
return _call.apply(this, [ 2, method ].concat(args)); | ||
return call.apply(this, [ 2, method ].concat(args)); | ||
} | ||
Client.prototype.request = request; | ||
Client.prototype.call = request; // It is left for compatibility with v0.6 or earlier. | ||
Client.prototype.call = request; // It is left for compatibility with v0.0.2 or earlier. | ||
Client.prototype.notify = notify; | ||
@@ -124,7 +119,13 @@ util.inherits(Client, events.EventEmitter); | ||
exports.createClient = function createClient(port, host, timeout, codecOptions) { | ||
function createClient(port, host = 'localhost', timeout = 0, codecOptions = { encode: {}, decode: {} }) { | ||
debug({ port, host, timeout, codecOptions }); | ||
return new Client(port, host, timeout, codecOptions); | ||
}; | ||
assert.equal(typeof port, 'number', 'Illegal argument: port'); | ||
assert.equal(typeof host, 'string', 'Illegal argument: host'); | ||
assert.equal(typeof timeout, 'number', 'Illegal argument: timeout'); | ||
return new Client({ port, host, timeout }, codecOptions); | ||
} | ||
exports.createClient = createClient; | ||
exports.createServer = function createServer(options, codecOptions = { encode: {}, decode: {} }) { | ||
@@ -131,0 +132,0 @@ const encodeCodec = msgpack.createCodec((codecOptions || {}).encode); |
{ | ||
"name": "msgpack-rpc-lite", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Implementation of MessagePack-RPC with msgpack-lite", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
19213
9
325