grain-rpc
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -12,3 +12,3 @@ /** | ||
RpcRespErr = 3, | ||
Custom = 4, | ||
Custom = 4 | ||
} | ||
@@ -15,0 +15,0 @@ export interface IMsgRpcCall { |
@@ -86,4 +86,5 @@ /// <reference types="node" /> | ||
export declare class Rpc extends EventEmitter implements IForwarderDest { | ||
private _sendMessage; | ||
private _inactiveQueue; | ||
private _sendMessageCB; | ||
private _inactiveRecvQueue; | ||
private _inactiveSendQueue; | ||
private _logger; | ||
@@ -110,4 +111,4 @@ private _callWrapper; | ||
/** | ||
* Until start() is called, received messages are queued. This gives you an opportunity to | ||
* register implementations and add "message" listeners without the risk of missing messages, | ||
* Until start() is called, received and sent messages are queued. This gives you an opportunity | ||
* to register implementations and add "message" listeners without the risk of missing messages, | ||
* even if receiveMessage() has already started being called. | ||
@@ -117,2 +118,7 @@ */ | ||
/** | ||
* Calling stop() resume the same state as before start was called: received and sent messages are | ||
* queued. | ||
*/ | ||
stop(): void; | ||
/** | ||
* Messaging interface: send data to the other side, to be emitted there as a "message" event. | ||
@@ -142,3 +148,3 @@ */ | ||
*/ | ||
getStub<Iface extends any>(name: string): any; | ||
getStub<Iface extends any>(name: string): Iface; | ||
getStub<Iface>(name: string, checker: tic.Checker): Iface; | ||
@@ -162,14 +168,15 @@ getStubForward<Iface extends any>(fwdDest: string, name: string): any; | ||
forwardMessage(msg: IMsgCustom): Promise<any>; | ||
private _makeCallRaw(iface, meth, args, resultChecker, fwdDest); | ||
private _makeCall(iface, meth, args, resultChecker, fwdDest); | ||
private _dispatch(msg); | ||
private _onCustomMessage(msg); | ||
private _onMessageCall(call); | ||
private _failCall(call, code, mesg, reportCode?); | ||
private _sendResponse(reqId, data); | ||
private _onMessageResp(resp); | ||
private _info(call, code, message?); | ||
private _warn(call, code, message?); | ||
private _callDesc(call); | ||
private _parseName(name); | ||
private _sendMessage; | ||
private _makeCallRaw; | ||
private _makeCall; | ||
private _dispatch; | ||
private _onCustomMessage; | ||
private _onMessageCall; | ||
private _failCall; | ||
private _sendResponse; | ||
private _onMessageResp; | ||
private _info; | ||
private _warn; | ||
private _callDesc; | ||
private _parseName; | ||
} | ||
@@ -176,0 +183,0 @@ /** |
@@ -97,2 +97,4 @@ "use strict"; | ||
super(); | ||
this._inactiveRecvQueue = []; // queue of received message | ||
this._inactiveSendQueue = []; // queue of messages to be sent | ||
this._implMap = new Map(); | ||
@@ -102,7 +104,6 @@ this._forwarders = new Map(); | ||
this._nextRequestId = 1; | ||
const { logger = console, sendMessage = inactiveSend, callWrapper = plainCall } = options; | ||
const { logger = console, sendMessage = null, callWrapper = plainCall } = options; | ||
this._logger = logger; | ||
this._sendMessage = sendMessage; | ||
this._sendMessageCB = sendMessage; | ||
this._callWrapper = callWrapper; | ||
this._inactiveQueue = (this._sendMessage === inactiveSend) ? [] : null; | ||
} | ||
@@ -113,4 +114,4 @@ /** | ||
receiveMessage(msg) { | ||
if (this._inactiveQueue) { | ||
this._inactiveQueue.push(msg); | ||
if (!this._sendMessageCB) { | ||
this._inactiveRecvQueue.push(msg); | ||
} | ||
@@ -122,16 +123,20 @@ else { | ||
/** | ||
* Until start() is called, received messages are queued. This gives you an opportunity to | ||
* register implementations and add "message" listeners without the risk of missing messages, | ||
* Until start() is called, received and sent messages are queued. This gives you an opportunity | ||
* to register implementations and add "message" listeners without the risk of missing messages, | ||
* even if receiveMessage() has already started being called. | ||
*/ | ||
start(sendMessage) { | ||
this._sendMessage = sendMessage; | ||
if (this._inactiveQueue) { | ||
for (const msg of this._inactiveQueue) { | ||
this._dispatch(msg); // We need to be careful not to throw from here. | ||
} | ||
this._inactiveQueue = null; | ||
} | ||
// Message sent by `_dispatch(...)` are appended to the send queue | ||
processQueue(this._inactiveRecvQueue, this._dispatch.bind(this)); | ||
processQueue(this._inactiveSendQueue, sendMessage); | ||
this._sendMessageCB = sendMessage; | ||
} | ||
/** | ||
* Calling stop() resume the same state as before start was called: received and sent messages are | ||
* queued. | ||
*/ | ||
stop() { | ||
this._sendMessageCB = null; | ||
} | ||
/** | ||
* Messaging interface: send data to the other side, to be emitted there as a "message" event. | ||
@@ -252,2 +257,10 @@ */ | ||
} | ||
_sendMessage(msg) { | ||
if (!this._sendMessageCB) { | ||
this._inactiveSendQueue.push(msg); | ||
} | ||
else { | ||
return this._sendMessageCB(msg); | ||
} | ||
} | ||
_makeCallRaw(iface, meth, args, resultChecker, fwdDest) { | ||
@@ -420,6 +433,2 @@ return new Promise((resolve, reject) => { | ||
exports.Rpc = Rpc; | ||
// Helper to fail if we try to call a method or post a message before start() has been called. | ||
function inactiveSend(msg) { | ||
throw new Error("Rpc cannot be used before start() has been called"); | ||
} | ||
/** | ||
@@ -443,1 +452,15 @@ * Interfaces may throw errors that include .code field, and it gets propagated to callers (e.g. | ||
const anyChecker = checkerAnyResult; | ||
/** | ||
* A little helper that processes message queues when starting an rpc instance. | ||
*/ | ||
function processQueue(queue, processFunc) { | ||
let i = 0; | ||
try { | ||
for (; i < queue.length; ++i) { | ||
processFunc(queue[i]); | ||
} | ||
} | ||
finally { | ||
queue.splice(0, i); | ||
} | ||
} |
{ | ||
"name": "grain-rpc", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Typed RPC interface on top of an arbitrary communication channel", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
33986
718