Skip to content

Commit 492d606

Browse files
authored
Merge pull request #20 from oxygen/master
Fixed transpiled JSONRPC.Exception to keep type information.
2 parents 7862cd3 + d8e61b4 commit 492d606

File tree

12 files changed

+231
-148
lines changed

12 files changed

+231
-148
lines changed

README.MD

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This library is tested in __browsers__ (>= IE10) and in __Node.js__ (>=7.8).
1111
Very simple usage allows for very easy application development:
1212
* export methods in an endpoint (which extends JSONRPC.EndpointBase), and call those method remotely through a client (which extends JSONRPC.Client);
1313
* on bidirectional transports, there may be endpoints at both ends of a connection, or reversed roles (TCP client is a JSONRPC server, while TCP server is JSONRPC client).
14+
* all remote invocations are asynchronous (multiple responses on the same connection are matched to the right waiting promises)
1415

1516
## Transports
1617

@@ -269,7 +270,7 @@ webSocketServer.on(
269270
"connection",
270271
async (webSocket) =>
271272
{
272-
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(webSocket);
273+
const nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(webSocket);
273274
// Do something with nWebSocketConnectionID and webSocket here, like register them as a pair with an authorization plugin.
274275

275276
// const clientForThisConnection = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
@@ -299,7 +300,7 @@ await new Promise((fnResolve, fnReject) => {
299300
const wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
300301

301302

302-
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(webSocket);
303+
const nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(webSocket);
303304

304305

305306
// Obtain single client. See above section "Extending the client" for the TestClient class (subclass of JSONRPC.Client).
@@ -388,15 +389,12 @@ __Site B (ES5 browser). WebSocket client (connects to the above WebSocket TCP se
388389
ws.addEventListener("open", function(event){
389390
var wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
390391

391-
wsJSONRPCRouter.addWebSocket(ws)
392-
.then(function(nWebSocketConnectionID){
393-
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
392+
var nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(ws);
394393

395-
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, websocket transport with bidirectional JSONRPC."])
396-
.then(console.log)
397-
.catch(console.log)
398-
;
399-
})
394+
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
395+
396+
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, websocket transport with bidirectional JSONRPC."])
397+
.then(console.log)
400398
.catch(console.log)
401399
;
402400
});

builds/browser/es5/jsonrpc.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es5/jsonrpc.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es7/jsonrpc.min.js

Lines changed: 161 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es7/jsonrpc.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jsonrpc-bidirectional",
33
"description": "Bidirectional JSONRPC over web sockets or HTTP with extensive plugin support.",
4-
"version": "4.3.1",
4+
"version": "5.1.1",
55
"scripts": {
66
"build": "node build.js",
77
"test": "node --expose-gc --max-old-space-size=1024 tests/main.js",
@@ -11,9 +11,9 @@
1111
},
1212
"repository": {
1313
"type": "git",
14-
"url": "git+https://github.com/oxygens/jsonrpc-bidirectional.git"
14+
"url": "git+https://github.com/oxygen/jsonrpc-bidirectional.git"
1515
},
16-
"homepage": "https://github.com/oxygens/jsonrpc-bidirectional",
16+
"homepage": "https://github.com/oxygen/jsonrpc-bidirectional",
1717
"author": "Ionut Stan <[email protected]>",
1818
"license": "MIT",
1919
"contributors": [
@@ -34,6 +34,7 @@
3434
"child_process": false
3535
},
3636
"dependencies": {
37+
"extendable-error-class": "^0.1.1",
3738
"node-fetch": "^1.6.3"
3839
},
3940
"optionalDependencies": {
@@ -55,6 +56,7 @@
5556
"eslint": "^3.19.0",
5657
"eslint-plugin-jsdoc": "^3.0.2",
5758
"phantom": "^4.0.2",
59+
"recursive-keys": "^0.9.0",
5860
"sleep-promise": "^2.0.0",
5961
"uglify-js": "^2.8.22",
6062
"uws": "^0.14.1",

src/BidirectionalWebRTCRouter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports =
1717
class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
1818
{
1919
/**
20+
* This function must be synchronous, otherwise it will allow of race conditions where critical plugins (if any) haven't been initialized yet.
21+
*
2022
* Returns the connection ID.
2123
*
2224
* RTCDataChannel instances which will emit an error or close event will get automatically removed.
@@ -27,7 +29,7 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
2729
*
2830
* @returns {number}
2931
*/
30-
async addRTCDataChannel(dataChannel)
32+
addRTCDataChannelSync(dataChannel)
3133
{
3234
if(dataChannel.readyState === "closed")
3335
{
@@ -37,7 +39,7 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
3739
// @TODO: test cases for the above, somehow.
3840

3941
// "closed" would not recover and should never be added, because it would not get cleaned up.
40-
console.log("[" + process.pid + "] addRTCDataChannel ignoring closed dataChannel.");
42+
console.log("[" + process.pid + "] addRTCDataChannelSync ignoring closed dataChannel.");
4143

4244
return;
4345
}
@@ -74,6 +76,8 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
7476
dataChannel.addEventListener(
7577
"error",
7678
(error) => {
79+
console.error(error);
80+
7781
this.onConnectionEnded(nConnectionID);
7882

7983
if(dataChannel.readyState === "open")

src/BidirectionalWebsocketRouter.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports =
2121
class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
2222
{
2323
/**
24+
* This function must be synchronous, otherwise it will allow of race conditions where critical plugins (if any) haven't been initialized yet.
25+
*
2426
* Returns the connection ID.
2527
*
2628
* WebSocket instances which will emit an error or close event will get automatically removed.
@@ -31,7 +33,7 @@ class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
3133
*
3234
* @returns {number}
3335
*/
34-
async addWebSocket(webSocket)
36+
addWebSocketSync(webSocket)
3537
{
3638
if(webSocket.readyState === JSONRPC.WebSocketAdapters.WebSocketWrapperBase.CLOSED)
3739
{
@@ -41,7 +43,7 @@ class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
4143
// @TODO: test cases for the above, somehow.
4244

4345
// WebSocket.CLOSED would not recover and should never be added, because it would not get cleaned up.
44-
console.log("[" + process.pid + "] addWebSocket ignoring closed webSocket.");
46+
console.log("[" + process.pid + "] addWebSocketSync ignoring closed webSocket.");
4547
return;
4648
}
4749

@@ -73,45 +75,49 @@ class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
7375
};
7476

7577

76-
if(webSocket.addEventListener)
78+
if(webSocket.on)
7779
{
78-
webSocket.addEventListener(
80+
webSocket.on(
7981
"message",
80-
async (messageEvent) => {
81-
await this._routeMessage(messageEvent.data, objSession);
82+
(strData, objFlags) => {
83+
this._routeMessage(strData, objSession);
8284
}
8385
);
8486

85-
webSocket.addEventListener(
87+
webSocket.on(
8688
"close",
87-
(closeEvent) => {
88-
//closeEvent.code;
89-
//closeEvent.reason;
90-
//closeEvent.wasClean;
91-
89+
(nCode, strReason) => {
9290
this.onConnectionEnded(nConnectionID);
9391
}
9492
);
9593

96-
webSocket.addEventListener("error", fnOnError);
94+
webSocket.on("error", fnOnError);
9795
}
98-
else
96+
else if(webSocket.addEventListener)
9997
{
100-
webSocket.on(
98+
webSocket.addEventListener(
10199
"message",
102-
async (strData, objFlags) => {
103-
await this._routeMessage(strData, objSession);
100+
(messageEvent) => {
101+
this._routeMessage(messageEvent.data, objSession);
104102
}
105103
);
106104

107-
webSocket.on(
105+
webSocket.addEventListener(
108106
"close",
109-
(nCode, strReason) => {
107+
(closeEvent) => {
108+
//closeEvent.code;
109+
//closeEvent.reason;
110+
//closeEvent.wasClean;
111+
110112
this.onConnectionEnded(nConnectionID);
111113
}
112114
);
113115

114-
webSocket.on("error", fnOnError);
116+
webSocket.addEventListener("error", fnOnError);
117+
}
118+
else
119+
{
120+
throw new Error("Not a supported WebSocket interface.");
115121
}
116122

117123

src/Exception.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
const ExtendableError = require("extendable-error-class");
2+
3+
14
/**
25
* Class representing the JSONRPC Exceptions.
36
* @class
47
* @extends Error
58
*/
69
module.exports =
7-
class Exception extends Error
10+
class Exception extends ExtendableError
811
{
912
/**
1013
* @param {string} strMessage

tests/Browser/index.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,13 @@
115115
ws.addEventListener("open", function(event){
116116
var wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
117117

118-
wsJSONRPCRouter.addWebSocket(ws)
119-
.then(function(nWebSocketConnectionID){
120-
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
121-
122-
clientOfBidirectionalWS.rpc("ImHereForTheParty", ["Lynch", "Lynch does the harlem shake", /*bDoNotAuthorizeMe*/ false])
123-
.then(function(objTeamMember){
124-
// When calling with this exact string, the node's ping() method will call back into the JSONRPC server hosted on this page (node becomes client, browser becomes server).
125-
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, bidirectional websocket mode."]).then(genericTestsPromiseCatch).catch(genericTestsPromiseCatch);
126-
})
127-
.catch(genericTestsPromiseCatch)
128-
;
118+
var nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(ws);
119+
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
120+
121+
clientOfBidirectionalWS.rpc("ImHereForTheParty", ["Lynch", "Lynch does the harlem shake", /*bDoNotAuthorizeMe*/ false])
122+
.then(function(objTeamMember){
123+
// When calling with this exact string, the node's ping() method will call back into the JSONRPC server hosted on this page (node becomes client, browser becomes server).
124+
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, bidirectional websocket mode."]).then(genericTestsPromiseCatch).catch(genericTestsPromiseCatch);
129125
})
130126
.catch(genericTestsPromiseCatch)
131127
;

0 commit comments

Comments
 (0)