Skip to content

Commit 4c13718

Browse files
author
ionut.stan
committed
Server IncomingRequest did not allow a different serialization method.
1 parent 3e0f360 commit 4c13718

File tree

8 files changed

+95
-22
lines changed

8 files changed

+95
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
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": "1.4.3",
4+
"version": "1.5.0",
55
"scripts": {
66
"transpile": "babel src --out-dir src",
77
"todo": "http://jamesknelson.com/using-es6-in-the-browser-with-babel-6-and-webpack/",

src/BidirectionalWebsocketRouter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ class BidirectionalWebsocketRouter extends EventEmitter
288288
}
289289

290290

291-
const objResponse = await this._jsonrpcServer.processRequest(incomingRequest);
291+
await this._jsonrpcServer.processRequest(incomingRequest);
292292

293293
if(webSocket.readyState !== webSocket.constructor.OPEN)
294294
{
295-
console.error("webSocket.readyState: " + JSON.stringify(webSocket.readyState) + ". Request was " + strMessage + ". Attempted responding with " + JSON.stringify(objResponse, undefined, "\t") + ".");
295+
console.error("webSocket.readyState: " + JSON.stringify(webSocket.readyState) + ". Request was " + strMessage + ". Attempted responding with " + JSON.stringify(incomingRequest.callResultToBeSerialized, undefined, "\t") + ".");
296296
}
297297

298-
webSocket.send(JSON.stringify(objResponse, undefined, "\t"));
298+
webSocket.send(incomingRequest.callResultSerialized);
299299
}
300300
else if(objMessage.hasOwnProperty("result") || objMessage.hasOwnProperty("error"))
301301
{

src/IncomingRequest.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class IncomingRequest
1919
this._bidirectionalWebsocketRouter = null;
2020

2121
this._mxResult = null;
22+
this._objResponseToBeSerialized = null;
23+
this._mxResultSerialized = null;
2224
this._bMethodCalled = false;
2325

2426
this._nConnectionID = null;
@@ -243,6 +245,42 @@ class IncomingRequest
243245
}
244246

245247

248+
/**
249+
* @returns {Object}
250+
*/
251+
get callResultToBeSerialized()
252+
{
253+
return this._objResponseToBeSerialized;
254+
}
255+
256+
257+
/**
258+
* @param {Object} objResultToBeSerialized
259+
*/
260+
set callResultToBeSerialized(objResultToBeSerialized)
261+
{
262+
this._objResponseToBeSerialized = objResultToBeSerialized;
263+
}
264+
265+
266+
/**
267+
* @returns {string|Buffer}
268+
*/
269+
get callResultSerialized()
270+
{
271+
return this._mxResultSerialized;
272+
}
273+
274+
275+
/**
276+
* @param {string|Buffer} mxResultSerialized
277+
*/
278+
set callResultSerialized(mxResultSerialized)
279+
{
280+
this._mxResultSerialized = mxResultSerialized;
281+
}
282+
283+
246284
/**
247285
* @returns {Object}
248286
*/

src/Plugins/Server/DebugLogger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DebugLogger extends JSONRPC.ServerPluginBase
2626
*
2727
* @param {JSONRPC.IncomingRequest} incomingRequest
2828
*/
29-
async afterJSONEncode(incomingRequest)
29+
async afterSerialize(incomingRequest)
3030
{
3131
// @TODO: specify selected endpoint?
3232

src/Server.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Server extends EventEmitter
6262
httpResponse.statusCode = 500;
6363

6464
const incomingRequest = await this.processHTTPRequest(httpRequest, httpResponse);
65-
const objResponse = await this.processRequest(incomingRequest);
65+
await this.processRequest(incomingRequest);
6666

6767
if(incomingRequest.callResult instanceof Error)
6868
{
@@ -92,7 +92,7 @@ class Server extends EventEmitter
9292
else
9393
{
9494
httpResponse.setHeader("Content-Type", "application/json");
95-
httpResponse.write(JSON.stringify(objResponse, undefined, "\t"));
95+
httpResponse.write(incomingRequest.callResultSerialized);
9696
}
9797
}
9898
catch(error)
@@ -265,7 +265,7 @@ class Server extends EventEmitter
265265
*
266266
* @param {JSONRPC.IncomingRequest} incomingRequest
267267
*
268-
* @returns {Object|null}
268+
* @returns {null}
269269
*/
270270
async processRequest(incomingRequest)
271271
{
@@ -382,14 +382,25 @@ class Server extends EventEmitter
382382
}
383383
}
384384

385-
let objResponse = incomingRequest.toResponseObject();
385+
incomingRequest.callResultToBeSerialized = incomingRequest.toResponseObject();
386386

387-
this.emit("result", objResponse);
387+
this.emit("response", incomingRequest);
388388
for(let plugin of this._arrPlugins)
389389
{
390-
await plugin.response(objResponse);
390+
await plugin.response(incomingRequest);
391391
}
392392

393-
return objResponse;
393+
394+
if(incomingRequest.callResultSerialized === null)
395+
{
396+
incomingRequest.callResultSerialized = JSON.stringify(incomingRequest.callResultToBeSerialized, undefined, "\t");
397+
}
398+
399+
400+
this.emit("afterSerialize", incomingRequest);
401+
for(let plugin of this._arrPlugins)
402+
{
403+
await plugin.afterSerialize(incomingRequest);
404+
}
394405
}
395406
};

src/ServerPluginBase.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,31 @@ class ServerPluginBase
6666
*
6767
* objResponse is a standard JSONRPC 2.0 response object.
6868
*
69-
* @param {Object} objResponse
69+
* @param {JSONRPC.IncomingRequest} incomingRequest
7070
*/
71-
async response(objResponse)
71+
async response(incomingRequest)
7272
{
7373
// Gives a chance to modify the server response object before sending it out.
7474

75+
// incomingRequest.callResultToBeSerialized is available here.
76+
77+
// Normally, this allows extending the protocol.
78+
}
79+
80+
81+
/**
82+
* This is called with the actual response object.
83+
*
84+
* objResponse is a standard JSONRPC 2.0 response object.
85+
*
86+
* @param {JSONRPC.IncomingRequest} incomingRequest
87+
*/
88+
async afterSerialize(incomingRequest)
89+
{
90+
// Gives a chance to modify the serialized server response string (or something else) before sending it out.
91+
92+
// incomingRequest.callResultSerialized is available here.
93+
7594
// Normally, this allows extending the protocol.
7695
}
7796
};

tests/ServerDebugMarkerPlugin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class ServerDebugMarkerPlugin extends JSONRPC.ServerPluginBase
2121
*
2222
* objResponse is a standard JSONRPC 2.0 response object.
2323
*
24-
* @param {Object} objResponse
24+
* @param {JSONRPC.IncomingRequest} incomingRequest
2525
*/
26-
async response(objResponse)
26+
async response(incomingRequest)
2727
{
2828
// Gives a chance to modify the server response object before sending it out.
2929

30-
// Normally, this allows extending the protocol.
30+
incomingRequest.callResultToBeSerialized.from = this._strSite;
3131

32-
objResponse.from = this._strSite;
32+
// Normally, this allows extending the protocol.
3333
}
3434
};

tests/ServerPluginInvalidResponseJSON.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ JSONRPC.Exception = require("../src/Exception");
55
module.exports =
66
class ServerPluginInvalidResponseJSON extends JSONRPC.ServerPluginBase
77
{
8-
async response(objResponse)
8+
/**
9+
* @param {JSONRPC.IncomingRequest} incomingRequest
10+
*/
11+
async response(incomingRequest)
912
{
10-
for(let strKey in objResponse)
13+
for(let strKey in incomingRequest.callResultToBeSerialized)
1114
{
12-
delete objResponse[strKey];
15+
delete incomingRequest.callResultToBeSerialized[strKey];
1316
}
1417

15-
objResponse.helloFromMars = ".... . .-.. .-.. --- / ..-. .-. --- -- / -- .- .-. ...";
18+
incomingRequest.callResultToBeSerialized.helloFromMars = ".... . .-.. .-.. --- / ..-. .-. --- -- / -- .- .-. ...";
19+
20+
console.log(incomingRequest.callResultToBeSerialized);
1621
}
1722
};

0 commit comments

Comments
 (0)