Skip to content

Commit

Permalink
Merge d254c44 into dc8a3b1
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacgr committed Oct 4, 2020
2 parents dc8a3b1 + d254c44 commit 44ec12e
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 98 deletions.
322 changes: 245 additions & 77 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
"intercept-stdout": "^0.1.2",
"jsdom": "15.1.1",
"jsdom-global": "3.0.2",
"mocha": "^6.1.4"
"mocha": "^6.1.4",
"nyc": "^15.1.0"
},
"dependencies": {
"nyc": "^15.1.0",
"ws": "^7.1.0"
}
}
29 changes: 13 additions & 16 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ const formatRequest = ({
request.jsonrpc = "2.0";
}

if (params) {
if (
(!(params === Object(params)) && !Array.isArray(params))
|| typeof params === "function"
) {
throw new TypeError(`${params} must be an object or array`);
}
if (
(params && !(params === Object(params)) && !Array.isArray(params))
|| typeof params === "function"
) {
throw new TypeError(`${params} must be an object or array`);
} else if (params) {
request.params = params;
}

Expand All @@ -29,8 +28,7 @@ const formatRequest = ({
request.id = id;
}

const messageString = JSON.stringify(request) + options.delimiter;
return messageString;
return JSON.stringify(request) + options.delimiter;
};

const formatResponse = ({
Expand All @@ -49,13 +47,12 @@ const formatResponse = ({
throw new TypeError(`${method} must be a string`);
}

if (params) {
if (
(!(params === Object(params)) && !Array.isArray(params))
|| typeof params === "function"
) {
throw new TypeError(`${params} must be an object or array`);
}
if (
(params && !(params === Object(params)) && !Array.isArray(params))
|| typeof params === "function"
) {
throw new TypeError(`${params} must be an object or array`);
} else if (params) {
response.params = params;
}

Expand Down
60 changes: 60 additions & 0 deletions src/protocol/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { MessageBuffer } = require("../buffer");

class JsonRpcServerProtocol {
// base protocol class for servers
constructor(factory, client, delimiter) {
this.client = client;
this.factory = factory;
this.delimiter = delimiter;
this.messageBuffer = new MessageBuffer(delimiter);
this.event = "data";
}

writeToClient(message) {
this.client.write(message);
}

clientConnected() {
this.client.on(this.event, (data) => {
this.messageBuffer.push(data);
while (!this.messageBuffer.isFinished()) {
const chunk = this.messageBuffer.handleData();
this.factory
.handleValidation(chunk)
.then((message) => {
if (message.batch) {
if (message.batch.empty) {
return;
}
this.writeToClient(
JSON.stringify(message.batch) + this.delimiter
);
} else if (message.notification) {
this.factory.emit(
message.notification.method,
message.notification
);
} else {
this.factory
.getResult(message)
.then(result => this.writeToClient(result))
.catch((error) => {
this.writeToClient(error);
});
}
})
.catch((error) => {
this.writeToClient(error.message);
});
}
});
this.client.on("close", () => {
this.factory.emit("clientDisconnected", this.client);
});
this.client.on("end", () => {
this.factory.emit("clientDisconnected", this.client);
});
}
}

module.exports = JsonRpcServerProtocol;
72 changes: 72 additions & 0 deletions src/protocol/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const JsonRpcServerProtocol = require("./base");

class HttpServerProtocol extends JsonRpcServerProtocol {
constructor(factory, client, response, delimiter) {
super(factory, client, delimiter);
this.response = response;
}

writeToClient(message, notification) {
if (notification) {
this.factory.setResponseHeader({
response: this.response,
notification: true
});
this.response.end();
return;
}
this.factory.setResponseHeader({ response: this.response });
this.response.write(message, () => {
this.response.end();
});
}

clientConnected() {
this.client.on(this.event, (data) => {
this.messageBuffer.push(data);
});
this.client.on("end", () => {
while (!this.messageBuffer.isFinished()) {
const chunk = this.messageBuffer.handleData();
this.factory
.handleValidation(chunk)
.then((message) => {
if (message.batch) {
if (message.batch.empty) {
return;
}
this.writeToClient(
JSON.stringify(message.batch) + this.delimiter
);
} else if (message.notification) {
this.writeToClient(undefined, message.notification);
} else {
this.factory
.getResult(message)
.then((result) => {
this.writeToClient(result);
})
.catch((error) => {
this.sendError(error);
});
}
})
.catch((error) => {
this.sendError(error.message);
});
}
});
}

sendError(error) {
this.factory.setResponseHeader({
response: this.response,
errorCode: JSON.parse(error).error.code || 500
});
this.response.write(error, () => {
this.response.end();
});
}
}

module.exports = { HttpServerProtocol };
5 changes: 5 additions & 0 deletions src/protocol/tcp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const JsonRpcServerProtocol = require("./base");

class TCPServerProtocol extends JsonRpcServerProtocol {}

module.exports = { TCPServerProtocol };
14 changes: 14 additions & 0 deletions src/protocol/ws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const JsonRpcServerProtocol = require("./base");

class WSServerProtocol extends JsonRpcServerProtocol {
constructor(factory, client, delimiter) {
super(factory, client, delimiter);
this.event = "message";
}

writeToClient(message) {
this.client.send(message);
}
}

module.exports = { WSServerProtocol };
2 changes: 1 addition & 1 deletion src/server/http.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const http = require("http");
const Server = require(".");
const { errorToStatus } = require("../constants");
const { HttpServerProtocol } = require("../ServerProtocol");
const { HttpServerProtocol } = require("../protocol/http");

/**
* Constructor for Jsonic HTTP server
Expand Down
2 changes: 1 addition & 1 deletion src/server/tcp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const net = require("net");
const Server = require(".");
const { formatResponse } = require("../functions");
const { TCPServerProtocol } = require("../ServerProtocol");
const { TCPServerProtocol } = require("../protocol/tcp");

/**
* Constructor for Jsonic TCP client
Expand Down
2 changes: 1 addition & 1 deletion src/server/ws.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const WebSocket = require("ws");
const Server = require(".");
const { formatResponse } = require("../functions");
const { WSServerProtocol } = require("../ServerProtocol");
const { WSServerProtocol } = require("../protocol/ws");

/**
* Constructor for Jsonic WS client
Expand Down

0 comments on commit 44ec12e

Please sign in to comment.