fix(structure): update to latest standards

This commit is contained in:
2019-01-30 03:14:02 +01:00
parent 1b0922759a
commit d296064bfa
17 changed files with 1997 additions and 1615 deletions

View File

@ -2,7 +2,7 @@ import * as plugins from './smartsocket.plugins';
import * as helpers from './smartsocket.helpers';
// classes
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketRequest } from './smartsocket.classes.socketrequest';
@ -48,7 +48,10 @@ export class Smartsocket {
public async stop() {
await plugins.smartdelay.delayFor(1000);
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
plugins.smartlog.defaultLogger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
@ -68,7 +71,7 @@ export class Smartsocket {
dataArg: any,
targetSocketConnectionArg: SocketConnection
) {
const done = plugins.smartq.defer();
const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest({
funcCallData: {
funcDataArg: dataArg,
@ -107,7 +110,7 @@ export class Smartsocket {
smartsocketHost: this,
socket: socketArg
});
plugins.beautylog.log('Socket connected. Trying to authenticate...');
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
this.openSockets.add(socketConnection);
socketConnection
.authenticate()

View File

@ -34,8 +34,8 @@ export class SmartsocketClient {
* connect the client to the server
*/
connect() {
let done = plugins.smartq.defer();
plugins.beautylog.log('trying to connect...');
let done = plugins.smartpromise.defer();
plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
let socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({
alias: this.alias,
@ -63,16 +63,16 @@ export class SmartsocketClient {
}
disconnect() {
let done = plugins.smartq.defer();
let done = plugins.smartpromise.defer();
this.socketConnection.socket.disconnect();
this.socketConnection = undefined;
plugins.beautylog.ok('disconnected!');
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
done.resolve();
return done.promise;
}
serverCall(functionNameArg: string, dataArg: any) {
let done = plugins.smartq.defer();
let done = plugins.smartpromise.defer();
let socketRequest = new SocketRequest({
side: 'requesting',
originSocketConnection: this.socketConnection,

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins';
import * as helpers from './smartsocket.helpers';
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
// import classes
import { Smartsocket } from './smartsocket.classes.smartsocket';
@ -68,7 +68,8 @@ export class SocketConnection {
// standard behaviour that is always true
allSocketConnections.add(this);
this.socket.on('disconnect', () => {
plugins.beautylog.info(
plugins.smartlog.defaultLogger.log(
'info',
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
);
this.socket.disconnect();
@ -82,9 +83,12 @@ export class SocketConnection {
* authenticate the socket
*/
authenticate() {
let done = plugins.smartq.defer();
let done = plugins.smartpromise.defer();
this.socket.on('dataAuth', (dataArg: ISocketConnectionAuthenticationObject) => {
plugins.beautylog.log('received authentication data. now hashing and comparing...');
plugins.smartlog.defaultLogger.log(
'info',
'received authentication data. now hashing and comparing...'
);
this.socket.removeListener('dataAuth', () => {});
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
// TODO: authenticate password
@ -92,7 +96,8 @@ export class SocketConnection {
this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
this.socket.emit('authenticated');
plugins.beautylog.ok(
plugins.smartlog.defaultLogger.log(
'ok',
`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`
);
done.resolve(this);
@ -112,18 +117,18 @@ export class SocketConnection {
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.smartq.defer();
let done = plugins.smartpromise.defer();
if (this.authenticated) {
this.socket.on('function', (dataArg: ISocketRequestDataObject) => {
// check if requested function is available to the socket's scope
plugins.beautylog.log('function request received');
plugins.smartlog.defaultLogger.log('info', 'function request received');
let referencedFunction: SocketFunction = this.role.allowedFunctions.find(
socketFunctionArg => {
return socketFunctionArg.name === dataArg.funcCallData.funcName;
}
);
if (referencedFunction !== undefined) {
plugins.beautylog.ok!('function in access scope');
plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
let localSocketRequest = new SocketRequest({
side: 'responding',
originSocketConnection: this,
@ -132,19 +137,28 @@ export class SocketConnection {
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
} else {
plugins.beautylog.warn('function not existent or out of access scope');
plugins.smartlog.defaultLogger.log(
'warn',
'function not existent or out of access scope'
);
}
});
this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject) => {
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
plugins.smartlog.defaultLogger.log(
'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}`);
plugins.smartlog.defaultLogger.log(
'info',
`now listening to function requests for ${this.alias}`
);
done.resolve(this);
} else {
let errMessage: 'socket needs to be authenticated first';
plugins.beautylog.error(errMessage);
plugins.smartlog.defaultLogger.log('error', errMessage);
done.reject(errMessage);
}
return done.promise;

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins';
// import classes
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
import { SocketRole } from './smartsocket.classes.socketrole';
// export interfaces
@ -60,7 +60,7 @@ export class SocketFunction {
* invokes the function of this SocketFunction
*/
invoke(dataArg: ISocketFunctionCall): Promise<any> {
let done = plugins.smartq.defer();
let done = plugins.smartpromise.defer();
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg).then((resultData: any) => {
let funcResponseData: ISocketFunctionCall = {

View File

@ -5,7 +5,7 @@ import * as helpers from './smartsocket.helpers';
import { ISocketFunctionCall } from './smartsocket.classes.socketfunction';
// import classes
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketConnection } from './smartsocket.classes.socketconnection';
@ -42,7 +42,7 @@ export class SocketRequest {
shortid: string;
originSocketConnection: SocketConnection;
funcCallData: ISocketFunctionCall;
done = plugins.smartq.defer();
done = plugins.smartpromise.defer();
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
this.shortid = optionsArg.shortId;
@ -69,7 +69,7 @@ export class SocketRequest {
* handles the response that is received by the requesting side
*/
handleResponse(responseDataArg: ISocketRequestDataObject) {
plugins.beautylog.log('handling response!');
plugins.smartlog.defaultLogger.log('info', 'handling response!');
this.done.resolve(responseDataArg.funcCallData);
allSocketRequests.remove(this);
}
@ -83,9 +83,9 @@ export class SocketRequest {
let targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(
this.funcCallData.funcName
);
plugins.beautylog.info(`invoking ${targetSocketFunction.name}`);
plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`);
targetSocketFunction.invoke(this.funcCallData).then(resultData => {
plugins.beautylog.log('got resultData. Sending it to requesting party.');
plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.');
let requestData: ISocketRequestDataObject = {
funcCallData: resultData,
shortId: this.shortid

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins';
// import classes
import { Objectmap } from 'lik';
import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from './smartsocket.classes.socketfunction';
/**

View File

@ -52,7 +52,7 @@ export class SocketServer {
* starts listening to incoming sockets:
*/
public async start() {
const done = plugins.smartq.defer();
const done = plugins.smartpromise.defer();
// handle http servers
if (this.httpServer && this.standaloneServer) {

View File

@ -20,7 +20,7 @@ export let checkPasswordForRole = (
referenceSmartsocket: Smartsocket
): boolean => {
let targetPasswordHash = getSocketRoleByName(dataArg.role, referenceSmartsocket).passwordHash;
let computedCompareHash = plugins.nodehash.sha256FromStringSync(dataArg.password);
let computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password);
return targetPasswordHash === computedCompareHash;
};

View File

@ -1,8 +1,10 @@
export import beautylog = require('beautylog');
export import lik = require('lik');
export import nodehash = require('nodehash');
export import smartdelay = require('smartdelay');
export import smartq = require('smartq');
export import shortid = require('shortid');
export import socketIo = require('socket.io');
export import socketIoClient = require('socket.io-client');
import * as lik from '@pushrocks/lik';
import * as smartlog from '@pushrocks/smartlog';
import * as smarthash from '@pushrocks/smarthash';
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise';
import * as shortid from 'shortid';
import * as socketIo from 'socket.io';
import * as socketIoClient from 'socket.io-client';
export { lik, smartlog, smarthash, smartdelay, smartpromise, shortid, socketIo, socketIoClient };