smartsocket/dist/smartsocket.classes.socketconnection.js

99 lines
8.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers");
const lik_1 = require("lik");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
// export classes
exports.allSocketConnections = new lik_1.Objectmap();
/**
* class SocketConnection represents a websocket connection
*/
class SocketConnection {
constructor(optionsArg) {
this.authenticated = false;
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.side = optionsArg.side;
this.smartsocketHost = optionsArg.smartsocketHost;
this.socket = optionsArg.socket;
// standard behaviour that is always true
exports.allSocketConnections.add(this);
this.socket.on('disconnect', () => {
plugins.beautylog.info(`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`);
this.socket.disconnect();
exports.allSocketConnections.remove(this);
});
}
// authenticating --------------------------
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.smartq.defer();
this.socket.on('dataAuth', (dataArg) => {
plugins.beautylog.log('received authentication data. now hashing and comparing...');
this.socket.removeListener('dataAuth', () => { });
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
this.alias = dataArg.alias;
this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
this.socket.emit('authenticated');
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
}
else {
this.authenticated = false;
this.socket.disconnect();
done.reject('not authenticated');
}
});
this.socket.emit('requestAuth');
return done.promise;
}
// listening -------------------------------
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.smartq.defer();
if (this.authenticated) {
this.socket.on('function', (dataArg) => {
// check if requested function is available to the socket's scope
plugins.beautylog.log('function request received');
let referencedFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.funcCallData.funcName;
});
if (referencedFunction !== undefined) {
plugins.beautylog.ok('function in access scope');
let localSocketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
side: 'responding',
originSocketConnection: this,
shortId: dataArg.shortId,
funcCallData: dataArg.funcCallData
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
}
else {
plugins.beautylog.warn('function not existent or out of access scope');
}
});
this.socket.on('functionResponse', (dataArg) => {
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg);
});
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
done.resolve(this);
}
else {
let errMessage;
plugins.beautylog.error(errMessage);
done.reject(errMessage);
}
return done.promise;
}
}
exports.SocketConnection = SocketConnection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsaURBQWdEO0FBQ2hELGlEQUFnRDtBQUVoRCw2QkFBK0I7QUFLL0IsMkZBQWdIO0FBK0JoSCxpQkFBaUI7QUFDTixRQUFBLG9CQUFvQixHQUFHLElBQUksZUFBUyxFQUFvQixDQUFBO0FBRW5FOztHQUVHO0FBQ0g7SUFPRSxZQUFhLFVBQStDO1FBSjVELGtCQUFhLEdBQVksS0FBSyxDQUFBO1FBSzVCLElBQUksQ0FBQyxLQUFLLEdBQUcsVUFBVSxDQUFDLEtBQUssQ0FBQTtRQUM3QixJQUFJLENBQUMsYUFBYSxHQUFHLFVBQVUsQ0FBQyxhQUFhLENBQUE7UUFDN0MsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFBO1FBQzNCLElBQUksQ0FBQyxJQUFJLEdB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