Skip to content

Commit

Permalink
Fleshing out the WebSocket integration
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 21, 2016
1 parent 158028d commit 018cd48
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"override": null
},
"websocket": {
"enabled": true,
"enabled": false,
"port": 3000
},
"title": "Tensō Browsable API"
Expand Down
1 change: 1 addition & 0 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var Tenso = function () {
_this.error(req, res, status, arg);
});
this.server.tenso = this;
this.websocket = null;
this.version = "3.2.0";
}

Expand Down
60 changes: 36 additions & 24 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var path = require("path"),
OAuth2Strategy = require("passport-oauth2").Strategy,
SAMLStrategy = require("passport-saml").Strategy,
TwitterStrategy = require("passport-twitter").Strategy,
RedisStore = require("connect-redis")(session);
RedisStore = require("connect-redis")(session),
lws = require("lws");

function trim(obj) {
return obj.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
Expand Down Expand Up @@ -505,40 +506,55 @@ function auth(obj, config) {
}

function bootstrap(obj, config) {
var notify = false;

// Bootstrapping configuration
auth(obj, config);

// Setting headers
config.headers = config.headers || {};
if (!config.headers) {
config.headers = {};
}

config.headers.server = "tenso/3.2.0";

// Starting WebSocket server
if (config.websocket.enabled) {
obj.websocket = new lws.Server({ port: config.websocket.port });
obj.server.log("Started WebSocket server on port " + config.websocket.port, "debug");
}

// Setting routes
iterate(config.routes, function (routes, method) {
iterate(routes, function (arg, route) {
if (typeof arg === "function") {
obj.server[method](route, function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

arg.apply(obj, args);
});
} else {
obj.server[method](route, function (req, res) {
if (!res._header) {
res.send(arg);
}
if (method === "socket") {
if (obj.websocket) {
iterate(routes, function (arg, event) {
obj.websocket.on(event, arg);
});
}
});
} else {
iterate(routes, function (arg, route) {
if (typeof arg === "function") {
obj.server[method](route, function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

arg.apply(obj, args);
});
} else {
obj.server[method](route, function (req, res) {
if (!res._header) {
res.send(arg);
}
});
}
});
}
});

// Disabling compression over SSL due to BREACH
if (config.ssl.cert && config.ssl.key) {
config.compress = false;
notify = true;
obj.server.log("Compression over SSL is disabled for your protection", "debug");
}

// Starting API server
Expand All @@ -549,10 +565,6 @@ function bootstrap(obj, config) {
obj.respond(req, res, err, status);
});

if (notify) {
obj.server.log("Compression over SSL is disabled for your protection", "debug");
}

return obj;
}

Expand Down
1 change: 1 addition & 0 deletions src/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Tenso {
this.error(req, res, status, arg);
});
this.server.tenso = this;
this.websocket = null;
this.version = "{{VERSION}}";
}

Expand Down
52 changes: 32 additions & 20 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const path = require("path"),
OAuth2Strategy = require("passport-oauth2").Strategy,
SAMLStrategy = require("passport-saml").Strategy,
TwitterStrategy = require("passport-twitter").Strategy,
RedisStore = require("connect-redis")(session);
RedisStore = require("connect-redis")(session),
lws = require("lws");

function trim (obj) {
return obj.replace(/^(\s+|\t+|\n+)|(\s+|\t+|\n+)$/g, "");
Expand Down Expand Up @@ -488,36 +489,51 @@ function auth (obj, config) {
}

function bootstrap (obj, config) {
let notify = false;

// Bootstrapping configuration
auth(obj, config);

// Setting headers
config.headers = config.headers || {};
if (!config.headers) {
config.headers = {};
}

config.headers.server = "tenso/{{VERSION}}";

// Starting WebSocket server
if (config.websocket.enabled) {
obj.websocket = new lws.Server({port: config.websocket.port});
obj.server.log("Started WebSocket server on port " + config.websocket.port, "debug");
}

// Setting routes
iterate(config.routes, function (routes, method) {
iterate(routes, function (arg, route) {
if (typeof arg === "function") {
obj.server[method](route, function (...args) {
arg.apply(obj, args);
});
} else {
obj.server[method](route, function (req, res) {
if (!res._header) {
res.send(arg);
}
if (method === "socket") {
if (obj.websocket) {
iterate(routes, function (arg, event) {
obj.websocket.on(event, arg);
});
}
});
} else {
iterate(routes, function (arg, route) {
if (typeof arg === "function") {
obj.server[method](route, function (...args) {
arg.apply(obj, args);
});
} else {
obj.server[method](route, function (req, res) {
if (!res._header) {
res.send(arg);
}
});
}
});
}
});

// Disabling compression over SSL due to BREACH
if (config.ssl.cert && config.ssl.key) {
config.compress = false;
notify = true;
obj.server.log("Compression over SSL is disabled for your protection", "debug");
}

// Starting API server
Expand All @@ -528,10 +544,6 @@ function bootstrap (obj, config) {
obj.respond(req, res, err, status);
});

if (notify) {
obj.server.log("Compression over SSL is disabled for your protection", "debug");
}

return obj;
}

Expand Down

0 comments on commit 018cd48

Please sign in to comment.