msgpack-rpc-lite
Advanced tools
Comparing version 0.1.2 to 0.1.3-0
@@ -16,5 +16,5 @@ "use strict"; | ||
*/ | ||
const msgpack_lite_1 = __importDefault(require("msgpack-lite")); | ||
const assert_1 = __importDefault(require("assert")); | ||
const events_1 = __importDefault(require("events")); | ||
const msgpack_lite_1 = __importDefault(require("msgpack-lite")); | ||
const net_1 = __importDefault(require("net")); | ||
@@ -28,10 +28,10 @@ const util_1 = __importDefault(require("util")); | ||
Object.defineProperty(debug, 'enabled', { get() { return enabled; } }); | ||
const msgidGenerator = (function () { | ||
const msgidGenerator = (() => { | ||
const MAX = Math.pow(2, 32) - 1; | ||
let msgid = 0; | ||
return { next() { return (msgid = (msgid < MAX ? msgid + 1 : 0)); } }; | ||
}()); | ||
})(); | ||
/** | ||
* MessagePack-RPC client | ||
*/ | ||
*/ | ||
class Client extends events_1.default.EventEmitter { | ||
@@ -46,8 +46,8 @@ constructor(connectOptions, codecOptions = {}) { | ||
Object.defineProperties(this, { | ||
'encodeCodec': { | ||
get() { return encodeCodec; } | ||
decodeCodec: { | ||
get() { return decodeCodec; }, | ||
}, | ||
'decodeCodec': { | ||
get() { return decodeCodec; } | ||
} | ||
encodeCodec: { | ||
get() { return encodeCodec; }, | ||
}, | ||
}); | ||
@@ -88,8 +88,7 @@ } | ||
* @deprecated This method does nothing. It is left for compatibility with v0.0.2 or earlier. | ||
*/ | ||
*/ | ||
close() { } | ||
; | ||
} | ||
exports.Client = Client; | ||
function send(message, callback = function () { }) { | ||
function send(message, callback = () => { }) { | ||
const self = this; | ||
@@ -111,3 +110,3 @@ const socket = net_1.default.createConnection(this.connectOptions, () => { | ||
const socketEvents = ['connect', 'end', 'timeout', 'drain', 'error', 'close']; | ||
socketEvents.reduce((socket, eventName) => socket.on(eventName, (...args) => { | ||
socketEvents.reduce((accumulator, eventName) => accumulator.on(eventName, (...args) => { | ||
debug(`socket event [${eventName}]`); | ||
@@ -117,9 +116,9 @@ self.emit.apply(self, [eventName].concat(args)); | ||
if (message[0] === 0) { | ||
socket.pipe(msgpack_lite_1.default.createDecodeStream({ codec: this.decodeCodec })).once('data', message => { | ||
socket.pipe(msgpack_lite_1.default.createDecodeStream({ codec: this.decodeCodec })).once('data', response => { | ||
if (debug.enabled) { | ||
debug(`received message: ${util_1.default.inspect(message, false, null, true)}`); | ||
debug(`received message: ${util_1.default.inspect(response, false, null, true)}`); | ||
} | ||
const [type, msgid, error, result] = message; // Response message | ||
const [type, msgid, error, result] = response; // Response message | ||
assert_1.default.equal(type, 1); | ||
assert_1.default.equal(msgid, message[1]); | ||
assert_1.default.equal(msgid, response[1]); | ||
callback.call(self, error, result, msgid); | ||
@@ -129,5 +128,5 @@ }); | ||
} | ||
function call(type, method, ...args) { | ||
const callback = typeof args[args.length - 1] === 'function' && args.pop(); | ||
const message = [type].concat(type === 0 ? msgidGenerator.next() : [], method, [args]); | ||
function call(type, method, ...params) { | ||
const callback = typeof params[params.length - 1] === 'function' && params.pop(); | ||
const message = [type].concat(type === 0 ? msgidGenerator.next() : [], method, [params]); | ||
if (callback) { | ||
@@ -154,3 +153,4 @@ send.call(this, message, callback); | ||
* @param host { string } Host the socket should connect to. | ||
* @param 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. | ||
* @param 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. | ||
* @param codecOptions { CodecOptions } | ||
@@ -170,7 +170,7 @@ * @returns { Client } | ||
* | ||
* @param option | ||
* @param options | ||
* @param codecOptions { CodecOptions } | ||
* @returns { net.Server } | ||
*/ | ||
function createServer(options, codecOptions = { encode: {}, decode: {} }) { | ||
function createServer(options, codecOptions) { | ||
const encodeCodec = msgpack_lite_1.default.createCodec((codecOptions || {}).encode); | ||
@@ -177,0 +177,0 @@ const decodeCodec = msgpack_lite_1.default.createCodec((codecOptions || {}).decode); |
{ | ||
"name": "msgpack-rpc-lite", | ||
"version": "0.1.2", | ||
"version": "0.1.3-0", | ||
"description": "Implementation of MessagePack-RPC with msgpack-lite", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
/* | ||
* MessagePack-RPC Implementation | ||
* ============================== | ||
* | ||
* | ||
* ## MessagePack-RPC Specification ## | ||
* | ||
* See also: | ||
* | ||
* See also: | ||
* - https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md | ||
* - http://frsyuki.hatenablog.com/entry/20100406/p1 | ||
*/ | ||
import msgpack from 'msgpack-lite'; | ||
import assert from 'assert'; | ||
import events from 'events'; | ||
import msgpack from 'msgpack-lite'; | ||
import net from 'net'; | ||
@@ -18,3 +18,3 @@ import util from 'util'; | ||
interface DebugLog extends Function { | ||
(message: any, ...params: any[]): void | ||
(message: any, ...params: any[]): void; | ||
enabled: boolean; | ||
@@ -26,3 +26,3 @@ } | ||
const equalsIgnoreSpace = (a: any, b: any): boolean => tr(a) === tr(b); | ||
const isDoNothingFunction = (fn: Function) => equalsIgnoreSpace(fn, function () { }); | ||
const isDoNothingFunction = (fn: DebugLog) => equalsIgnoreSpace(fn, function () { }); | ||
const enabled = !isDoNothingFunction(debug); | ||
@@ -36,15 +36,15 @@ Object.defineProperty(debug, 'enabled', { get() { return enabled; } }); | ||
const msgidGenerator = (function () { | ||
const msgidGenerator = (() => { | ||
const MAX = Math.pow(2, 32) - 1; | ||
let msgid = 0; | ||
return { next() { return (msgid = (msgid < MAX ? msgid + 1 : 0)); } }; | ||
}()); | ||
})(); | ||
/** | ||
* MessagePack-RPC client | ||
*/ | ||
*/ | ||
export class Client extends events.EventEmitter { | ||
connectOptions: net.NetConnectOpts; | ||
readonly encodeCodec: msgpack.Codec; | ||
readonly decodeCodec: msgpack.Codec; | ||
public connectOptions: net.NetConnectOpts; | ||
public readonly encodeCodec: msgpack.Codec; | ||
public readonly decodeCodec: msgpack.Codec; | ||
constructor(connectOptions: net.NetConnectOpts, codecOptions: CodecOptions = {}) { | ||
@@ -58,8 +58,8 @@ super(); | ||
Object.defineProperties(this, { | ||
'encodeCodec': { | ||
get() { return encodeCodec; } | ||
decodeCodec: { | ||
get() { return decodeCodec; }, | ||
}, | ||
'decodeCodec': { | ||
get() { return decodeCodec; } | ||
} | ||
encodeCodec: { | ||
get() { return encodeCodec; }, | ||
}, | ||
}); | ||
@@ -70,8 +70,8 @@ } | ||
* Send a request message to the server | ||
* | ||
* @param method { string } | ||
* @param args | ||
* | ||
* @param method { string } | ||
* @param args | ||
* @returns {Promise<[any, number]>} | ||
*/ | ||
request(method: string, ...args: any[]): Promise<[any, number]> { | ||
public request(method: string, ...args: any[]): Promise<[any, number]> { | ||
return call.apply(this, [0, method].concat(args)); | ||
@@ -82,9 +82,9 @@ } | ||
* Send a request message to the server | ||
* | ||
* | ||
* @deprecated Please use the request method. It is left for compatibility with v0.0.2 or earlier. | ||
* @param method { string } | ||
* @param args | ||
* @param method { string } | ||
* @param args | ||
* @returns {Promise<[any, number]>} | ||
*/ | ||
call(method: string, ...args: any[]): Promise<[any, number]> { | ||
public call(method: string, ...args: any[]): Promise<[any, number]> { | ||
return call.apply(this, [0, method].concat(args)); | ||
@@ -95,8 +95,8 @@ } | ||
* Send a notification message to the server | ||
* | ||
* @param method { string } | ||
* @param args | ||
* | ||
* @param method { string } | ||
* @param args | ||
* @returns {Promise<any[]>} | ||
*/ | ||
notify(method: string, ...args: any[]): Promise<any[]> { | ||
public notify(method: string, ...args: any[]): Promise<any[]> { | ||
return call.apply(this, [2, method].concat(args)); | ||
@@ -107,7 +107,7 @@ } | ||
* @deprecated This method does nothing. It is left for compatibility with v0.0.2 or earlier. | ||
*/ | ||
close(): void { }; | ||
*/ | ||
public close(): void { } | ||
} | ||
function send(this: Client, message: any[], callback = function () { }) { | ||
function send(this: Client, message: any[], callback = () => { }) { | ||
const self = this; | ||
@@ -126,3 +126,3 @@ const socket = net.createConnection(this.connectOptions, () => { | ||
const socketEvents = ['connect', 'end', 'timeout', 'drain', 'error', 'close']; | ||
socketEvents.reduce((socket, eventName) => socket.on(eventName, (...args) => { | ||
socketEvents.reduce((accumulator, eventName) => accumulator.on(eventName, (...args) => { | ||
debug(`socket event [${eventName}]`); | ||
@@ -133,8 +133,8 @@ self.emit.apply(self, [eventName].concat(args)); | ||
if (message[0] === 0) { | ||
socket.pipe(msgpack.createDecodeStream({ codec: this.decodeCodec })).once('data', message => { | ||
if (debug.enabled) { debug(`received message: ${util.inspect(message, false, null, true)}`); } | ||
socket.pipe(msgpack.createDecodeStream({ codec: this.decodeCodec })).once('data', response => { | ||
if (debug.enabled) { debug(`received message: ${util.inspect(response, false, null, true)}`); } | ||
const [type, msgid, error, result] = message; // Response message | ||
const [type, msgid, error, result] = response; // Response message | ||
assert.equal(type, 1); | ||
assert.equal(msgid, message[1]); | ||
assert.equal(msgid, response[1]); | ||
callback.call(self, error, result, msgid); | ||
@@ -145,5 +145,5 @@ }); | ||
function call(this: Client, type: number, method: string, ...args: any[]) { | ||
const callback = typeof args[args.length - 1] === 'function' && args.pop(); | ||
const message = ([type] as any[]).concat(type === 0 ? msgidGenerator.next() : [], method, [args]); | ||
function call(this: Client, type: number, method: string, ...params: any[]) { | ||
const callback = typeof params[params.length - 1] === 'function' && params.pop(); | ||
const message = ([type] as any[]).concat(type === 0 ? msgidGenerator.next() : [], method, [params]); | ||
if (callback) { | ||
@@ -162,6 +162,7 @@ send.call(this, message, callback); | ||
* Initiates a MessagePack-RPC client. | ||
* | ||
* | ||
* @param port { number } Port the socket should connect to. | ||
* @param host { string } Host the socket should connect to. | ||
* @param 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. | ||
* @param 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. | ||
* @param codecOptions { CodecOptions } | ||
@@ -181,8 +182,8 @@ * @returns { Client } | ||
* Creates a new MessagePack-RPC server. | ||
* | ||
* @param option | ||
* | ||
* @param options | ||
* @param codecOptions { CodecOptions } | ||
* @returns { net.Server } | ||
*/ | ||
export function createServer(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }, codecOptions: CodecOptions = { encode: {}, decode: {} }) { | ||
export function createServer(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }, codecOptions?: CodecOptions) { | ||
const encodeCodec = msgpack.createCodec((codecOptions || {}).encode); | ||
@@ -209,2 +210,2 @@ const decodeCodec = msgpack.createCodec((codecOptions || {}).decode); | ||
return net.createServer(options, connectionListener); | ||
} | ||
} |
@@ -0,7 +1,9 @@ | ||
import { expect } from 'chai'; | ||
import debuglog from 'debug'; | ||
import net from 'net'; | ||
import { expect } from 'chai'; | ||
import portfinder from 'portfinder'; | ||
const debug = require('debug')('msgpack-rpc-lite:test'); | ||
import * as rpc from '../src/'; | ||
const debug = debuglog('msgpack-rpc-lite:test'); | ||
describe('msgpack-rpc#request', () => { | ||
@@ -21,5 +23,5 @@ | ||
server = rpc.createServer().on('foo', (params, done) => { | ||
server = rpc.createServer().on('foo', (params, callback) => { | ||
expect(params).to.have.ordered.members([1, 2, 3]); | ||
done(null, 'bar'); | ||
callback(null, 'bar'); | ||
}); | ||
@@ -26,0 +28,0 @@ server.listen(port); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
321060
14
8667
2