Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Websocket connection undefined (#252)
Browse files Browse the repository at this point in the history
Fix _updateSubscriptions to safely access connections and add tests
  • Loading branch information
nicholasjpaterno authored and davidmurdoch committed Jan 18, 2019
1 parent 35b8c71 commit e17046e
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 1,070 deletions.
11 changes: 10 additions & 1 deletion lib/httpServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var http = require("http");
const http = require("http");
const { rpcError } = require("./utils/to");

module.exports = function(provider, logger) {
var server = http.createServer(function(request, response) {
Expand Down Expand Up @@ -50,6 +51,14 @@ module.exports = function(provider, logger) {
logger.log(payload.method);
}

// http connections do not support subscriptions
if (payload.method === "eth_subscribe" || payload.method === "eth_unsubscribe") {
headers["Content-Type"] = "application/json";
response.writeHead(400, headers);
response.end(rpcError(payload.id, -32000, "notifications not supported"));
break;
}

provider.send(payload, function(_, result) {
headers["Content-Type"] = "application/json";
response.writeHead(200, headers);
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/to.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,16 @@ module.exports = {
}
var bufVal = utils.toBuffer(val);
return utils.bufferToInt(bufVal);
},

rpcError: function(id, code, msg) {
return JSON.stringify({
jsonrpc: "2.0",
id: id,
error: {
code: code,
message: msg
}
});
}
};
6 changes: 5 additions & 1 deletion lib/webSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,9 @@ ConnectionManager.prototype._logPayload = function(payload) {
};

ConnectionManager.prototype._updateSubscriptions = function(notification) {
this.connectionsBySubscriptionId[notification.params.subscription].connection.send(JSON.stringify(notification));
const subscription = this.connectionsBySubscriptionId[notification.params.subscription];
// Safety check for subscription/connection.
if (subscription) {
subscription.connection.send(JSON.stringify(notification));
}
};
Loading

0 comments on commit e17046e

Please sign in to comment.