Skip to content

Commit

Permalink
Updated builds
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed May 3, 2016
1 parent 533fe74 commit 3405f18
Show file tree
Hide file tree
Showing 14 changed files with 1,000 additions and 1,061 deletions.
206 changes: 106 additions & 100 deletions dist/bucket-manager.js

Large diffs are not rendered by default.

170 changes: 88 additions & 82 deletions dist/controllers/bucket-controller.js

Large diffs are not rendered by default.

75 changes: 33 additions & 42 deletions dist/controllers/comms-controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ws = require("ws");
var events = require("events");
var https = require("https");
Expand All @@ -17,21 +12,20 @@ var socket_api_1 = require("../socket-api");
* This wraps data around events sent via the web socket to the users server. Optionally
* these events can respond to the client who initiated the event as well as to all listeners.
*/
var ClientEvent = (function () {
function ClientEvent(event, client) {
class ClientEvent {
constructor(event, client) {
this.client = client;
this.error = null;
this.clientEvent = event;
this.responseType = socket_event_types_1.EventResponseType.NoResponse;
}
return ClientEvent;
})();
}
exports.ClientEvent = ClientEvent;
/**
* A wrapper class for client connections made to the CommsController
*/
var ClientConnection = (function () {
function ClientConnection(ws, domain, controller) {
class ClientConnection {
constructor(ws, domain, controller) {
var that = this;
this.domain = domain;
this._controller = controller;
Expand All @@ -48,47 +42,45 @@ var ClientConnection = (function () {
* Called whenever we recieve a message from a client
* @param {string|any} message
*/
ClientConnection.prototype.onMessage = function (message) {
winston.info("Received message from client: '" + message + "'", { process: process.pid });
onMessage(message) {
winston.info(`Received message from client: '${message}'`, { process: process.pid });
try {
var event = JSON.parse(message);
this._controller.alertMessage(new ClientEvent(event, this));
}
catch (err) {
winston.error("Could not parse socket message: '" + err + "'", { process: process.pid });
winston.error(`Could not parse socket message: '${err}'`, { process: process.pid });
}
};
}
/**
* Called whenever a client disconnnects
*/
ClientConnection.prototype.onClose = function () {
onClose() {
this.ws.removeAllListeners("message");
this.ws.removeAllListeners("close");
this.ws.removeAllListeners("error");
this.ws.clientConnection = null;
this.ws = null;
this._controller = null;
};
}
/**
* Called whenever an error has occurred
* @param {Error} err
*/
ClientConnection.prototype.onError = function (err) {
winston.error("An error has occurred for web socket : '" + err.message + "'", { process: process.pid });
};
return ClientConnection;
})();
onError(err) {
winston.error(`An error has occurred for web socket : '${err.message}'`, { process: process.pid });
}
}
/**
* A controller that deals with any any IPC or web socket communications
*/
var CommsController = (function (_super) {
__extends(CommsController, _super);
class CommsController extends events.EventEmitter {
/**
* Creates an instance of the Communication server
* @param {IConfig} cfg
*/
function CommsController(cfg) {
_super.call(this);
constructor(cfg) {
super();
var that = this;
CommsController.singleton = this;
// dummy request processing - this is not actually called as its handed off to the socket api
Expand All @@ -102,7 +94,7 @@ var CommsController = (function (_super) {
var caChain = [fs.readFileSync(cfg.sslIntermediate), fs.readFileSync(cfg.sslRoot)];
var privkey = cfg.sslKey ? fs.readFileSync(cfg.sslKey) : null;
var theCert = cfg.sslCert ? fs.readFileSync(cfg.sslCert) : null;
winston.info("Attempting to start Websocket server with SSL...", { process: process.pid });
winston.info(`Attempting to start Websocket server with SSL...`, { process: process.pid });
httpsServer = https.createServer({ key: privkey, cert: theCert, passphrase: cfg.sslPassPhrase, ca: caChain }, processRequest);
httpsServer.listen(cfg.websocket.port);
this._server = new ws.Server({ server: httpsServer });
Expand All @@ -126,7 +118,7 @@ var CommsController = (function (_super) {
}
}
if (!clientApproved) {
winston.error("A connection was made by " + (headers.host || headers.origin) + " but it is not on the approved domain list");
winston.error(`A connection was made by ${headers.host || headers.origin} but it is not on the approved domain list`);
ws.terminate();
ws.close();
}
Expand All @@ -138,38 +130,38 @@ var CommsController = (function (_super) {
* Sends an event to all connected clients of this server listening for a specific event
* @param {ClientEvent<def.SocketEvents.IEvent>} event The event to alert the server of
*/
CommsController.prototype.alertMessage = function (event) {
alertMessage(event) {
if (!event.clientEvent)
return winston.error("Websocket alert error: No ClientEvent set", { process: process.pid });
return winston.error(`Websocket alert error: No ClientEvent set`, { process: process.pid });
this.emit(socket_event_types_1.EventType[event.clientEvent.eventType], event);
if (event.responseType != socket_event_types_1.EventResponseType.NoResponse && !event.responseEvent)
return winston.error("Websocket alert error: The response type is expecting a responseEvent but one is not created", { process: process.pid });
return winston.error(`Websocket alert error: The response type is expecting a responseEvent but one is not created`, { process: process.pid });
if (event.responseType == socket_event_types_1.EventResponseType.RespondClient)
this.broadcastEventToClient(event.responseEvent, event.client);
else if (event.responseType == socket_event_types_1.EventResponseType.ReBroadcast)
this.broadcastEventToAll(event.responseEvent);
};
}
/**
* Sends an event to the client specified
* @param {IEvent} event The event to broadcast
*/
CommsController.prototype.broadcastEventToClient = function (event, client) {
broadcastEventToClient(event, client) {
var that = this;
return new Promise(function (resolve, reject) {
client.ws.send(JSON.stringify(event), undefined, function (error) {
if (error) {
winston.error("Websocket broadcase error: '" + error + "'", { process: process.pid });
winston.error(`Websocket broadcase error: '${error}'`, { process: process.pid });
return reject();
}
return resolve();
});
});
};
}
/**
* Sends an event to all connected clients of this server listening for a specific event
* @param {IEvent} event The event to broadcast
*/
CommsController.prototype.broadcastEventToAll = function (event) {
broadcastEventToAll(event) {
var that = this;
return new Promise(function (resolve, reject) {
var numResponded = 0, errorOccurred = false, releventClients = [];
Expand All @@ -186,7 +178,7 @@ var CommsController = (function (_super) {
if (errorOccurred)
return;
if (error) {
winston.error("Websocket broadcase error: '" + error + "'", { process: process.pid });
winston.error(`Websocket broadcase error: '${error}'`, { process: process.pid });
errorOccurred = true;
return reject();
}
Expand All @@ -200,14 +192,13 @@ var CommsController = (function (_super) {
if (clientLength == 0)
return resolve();
});
};
}
/**
* Called to initialize this controller and its related database objects
* @returns {Promise<Controller>}
*/
CommsController.prototype.initialize = function (db) {
initialize(db) {
return Promise.resolve(null);
};
return CommsController;
})(events.EventEmitter);
}
}
exports.CommsController = CommsController;
32 changes: 22 additions & 10 deletions dist/controllers/controller.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
}
step("next", void 0);
});
};
/**
* Base class for all controllers
*/
var Controller = (function () {
function Controller() {
class Controller {
constructor() {
}
/**
* All controllers must successfully return a promise for its initialization phase.
*/
Controller.prototype.initialize = function (db) {
initialize(db) {
return null;
};
}
/**
* Ensures the index of a collection
*/
Controller.prototype.ensureIndex = function (collection, name) {
ensureIndex(collection, name) {
return new Promise(function (resolve, reject) {
collection.createIndex(name, function (err, indexName) {
if (err)
Expand All @@ -23,14 +36,14 @@ var Controller = (function () {
return resolve();
});
});
};
}
/**
* Creates a new mongodb collection
* @param {string} name The name of the collection to create
* @param {mongodb.Db} db The database to use
* @param {Promise<mongodb.Collection>}
*/
Controller.prototype.createCollection = function (name, db) {
createCollection(name, db) {
return new Promise(function (resolve, reject) {
db.createCollection(name, function (err, collection) {
if (err || !collection)
Expand All @@ -39,7 +52,6 @@ var Controller = (function () {
return resolve(collection);
});
});
};
return Controller;
})();
}
}
exports.Controller = Controller;
32 changes: 19 additions & 13 deletions dist/controllers/cors-controller.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
}
step("next", void 0);
});
};
var controller_1 = require("./controller");
/**
* Checks all incomming requests to see if they are CORS approved
*/
var CORSController = (function (_super) {
__extends(CORSController, _super);
class CORSController extends controller_1.Controller {
/**
* Creates an instance of the user manager
* @param {mongodb.Collection} userCollection The mongo collection that stores the users
* @param {mongodb.Collection} sessionCollection The mongo collection that stores the session data
* @param {def.IConfig} The config options of this manager
*/
function CORSController(e, config) {
_super.call(this);
constructor(e, config) {
super();
var matches = [];
for (var i = 0, l = config.approvedDomains.length; i < l; i++)
matches.push(new RegExp(config.approvedDomains[i]));
Expand All @@ -35,7 +42,7 @@ var CORSController = (function (_super) {
break;
}
if (!matched)
console.log(req.headers.origin + " Does not have permission. Add it to the allowed ");
console.log(`${req.headers.origin} Does not have permission. Add it to the allowed `);
}
if (req.method === 'OPTIONS') {
res.status(200);
Expand All @@ -48,9 +55,8 @@ var CORSController = (function (_super) {
/**
* All controllers must successfully return a promise for its initialization phase.
*/
CORSController.prototype.initialize = function (db) {
initialize(db) {
return Promise.resolve(null);
};
return CORSController;
})(controller_1.Controller);
}
}
exports.CORSController = CORSController;
30 changes: 18 additions & 12 deletions dist/controllers/error-controller.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
}
step("next", void 0);
});
};
var controller_1 = require("./controller");
/**
* Handles express errors
*/
var ErrorController = (function (_super) {
__extends(ErrorController, _super);
class ErrorController extends controller_1.Controller {
/**
* Creates an instance
*/
function ErrorController(e, config) {
_super.call(this);
constructor(e, config) {
super();
// Handle all errors the same way
e.use(function (err, req, res, next) {
res.setHeader('Content-Type', 'application/json');
Expand All @@ -24,9 +31,8 @@ var ErrorController = (function (_super) {
/**
* All controllers must successfully return a promise for its initialization phase.
*/
ErrorController.prototype.initialize = function (db) {
initialize(db) {
return Promise.resolve(null);
};
return ErrorController;
})(controller_1.Controller);
}
}
exports.ErrorController = ErrorController;
Loading

0 comments on commit 3405f18

Please sign in to comment.