fix(core): update

This commit is contained in:
2020-09-24 18:03:01 +00:00
parent 424d9cca91
commit 1fbc76ef30
34 changed files with 10256 additions and 1074 deletions

View File

@ -1,7 +1,6 @@
import * as plugins from './smartsocket.plugins';
// classes
import { Objectmap } from '@pushrocks/lik';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCallDataRequest, SocketFunction, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
import { SocketRequest } from './smartsocket.classes.socketrequest';
@ -10,6 +9,7 @@ import { SocketServer } from './smartsocket.classes.socketserver';
// socket.io
import * as SocketIO from 'socket.io';
import { logger } from './smartsocket.logging';
export interface ISmartsocketConstructorOptions {
port?: number;
@ -22,10 +22,10 @@ export class Smartsocket {
public shortId = plugins.smartunique.shortId();
public options: ISmartsocketConstructorOptions;
public io: SocketIO.Server;
public socketConnections = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>();
public socketFunctions = new Objectmap<SocketFunction<any>>();
public socketRequests = new Objectmap<SocketRequest<any>>();
public socketConnections = new plugins.lik.ObjectMap<SocketConnection>();
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
public socketFunctions = new plugins.lik.ObjectMap<SocketFunction<any>>();
public socketRequests = new plugins.lik.ObjectMap<SocketRequest<any>>();
private socketServer = new SocketServer(this);
@ -55,7 +55,7 @@ export class Smartsocket {
public async stop() {
await plugins.smartdelay.delayFor(1000);
this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log(
logger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
@ -118,7 +118,7 @@ export class Smartsocket {
smartsocketHost: this,
socket: socketArg
});
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
logger.log('info', 'Socket connected. Trying to authenticate...');
this.socketConnections.add(socketConnection);
await socketConnection.authenticate();
await socketConnection.listenToFunctionRequests();

View File

@ -5,7 +5,7 @@ import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
import { defaultLogger } from '@pushrocks/smartlog';
import { logger } from './smartsocket.logging';
/**
* interface for class SmartsocketClient
@ -37,9 +37,9 @@ export class SmartsocketClient {
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
public eventStatus: interfaces.TConnectionStatus = 'new';
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
public socketRoles = new plugins.lik.Objectmap<SocketRole>();
public socketFunctions = new plugins.lik.ObjectMap<SocketFunction<any>>();
public socketRequests = new plugins.lik.ObjectMap<SocketRequest<any>>();
public socketRoles = new plugins.lik.ObjectMap<SocketRole>();
constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias;
@ -62,7 +62,7 @@ export class SmartsocketClient {
*/
public connect() {
const done = plugins.smartpromise.defer();
plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
logger.log('info', 'trying to connect...');
const socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({
alias: this.alias,
@ -79,19 +79,19 @@ export class SmartsocketClient {
const timer = new plugins.smarttime.Timer(5000);
timer.start();
timer.completed.then(() => {
defaultLogger.log('warn', 'connection to server timed out.');
logger.log('warn', 'connection to server timed out.');
this.disconnect();
});
// authentication flow
this.socketConnection.socket.on('requestAuth', (requestAuthPayload: interfaces.IRequestAuthPayload) => {
timer.reset();
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
logger.log('info', 'server requested authentication');
// lets register the authenticated event
this.socketConnection.socket.on('authenticated', () => {
this.remoteShortId = requestAuthPayload.serverShortId;
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
logger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
@ -99,7 +99,7 @@ export class SmartsocketClient {
// lets register the forbidden event
this.socketConnection.socket.on('forbidden', async () => {
defaultLogger.log('warn', `disconnecting due to being forbidden to use the ressource`);
logger.log('warn', `disconnecting due to being forbidden to use the ressource`);
await this.disconnect();
});
@ -136,11 +136,11 @@ export class SmartsocketClient {
*/
public async disconnect() {
if (this.socketConnection) {
this.socketConnection.disconnect();
await this.socketConnection.disconnect();
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
logger.log('ok', 'disconnected!');
}
defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`);
logger.log('warn', `disconnected from server ${this.remoteShortId}`);
this.remoteShortId = null;
this.updateStatus('disconnected');

View File

@ -1,8 +1,6 @@
import * as plugins from './smartsocket.plugins';
import * as interfaces from './interfaces';
import { Objectmap } from '@pushrocks/lik';
// import classes
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SocketFunction } from './smartsocket.classes.socketfunction';
@ -12,6 +10,7 @@ import { SocketRole } from './smartsocket.classes.socketrole';
// socket.io
import * as SocketIO from 'socket.io';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
import { logger } from './smartsocket.logging';
// export interfaces
@ -42,7 +41,7 @@ export interface ISocketConnectionAuthenticationObject {
}
// export classes
export let allSocketConnections = new Objectmap<SocketConnection>();
export let allSocketConnections = new plugins.lik.ObjectMap<SocketConnection>();
/**
* class SocketConnection represents a websocket connection
@ -74,7 +73,7 @@ export class SocketConnection {
this.updateStatus('connected');
});
this.socket.on('disconnect', async () => {
plugins.smartlog.defaultLogger.log(
logger.log(
'info',
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
);
@ -91,7 +90,7 @@ export class SocketConnection {
public authenticate() {
const done = plugins.smartpromise.defer();
this.socket.on('dataAuth', async (dataArg: ISocketConnectionAuthenticationObject) => {
plugins.smartlog.defaultLogger.log(
logger.log(
'info',
'received authentication data. now hashing and comparing...'
);
@ -102,7 +101,7 @@ export class SocketConnection {
this.authenticated = true;
this.role = SocketRole.getSocketRoleByName(this.smartsocketRef, dataArg.role);
this.socket.emit('authenticated');
plugins.smartlog.defaultLogger.log(
logger.log(
'ok',
`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`
);
@ -130,14 +129,14 @@ export class SocketConnection {
if (this.authenticated) {
this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
// check if requested function is available to the socket's scope
plugins.smartlog.defaultLogger.log('info', 'function request received');
logger.log('info', 'function request received');
const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.find(
socketFunctionArg => {
return socketFunctionArg.name === dataArg.funcCallData.funcName;
}
);
if (referencedFunction) {
plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
logger.log('ok', 'function in access scope');
const localSocketRequest = new SocketRequest(this.smartsocketRef, {
side: 'responding',
originSocketConnection: this,
@ -146,14 +145,14 @@ export class SocketConnection {
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
} else {
plugins.smartlog.defaultLogger.log(
logger.log(
'warn',
'function not existent or out of access scope'
);
}
});
this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject<any>) => {
plugins.smartlog.defaultLogger.log(
logger.log(
'info',
`received response for request with id ${dataArg.shortId}`
);
@ -163,14 +162,14 @@ export class SocketConnection {
);
targetSocketRequest.handleResponse(dataArg);
});
plugins.smartlog.defaultLogger.log(
logger.log(
'info',
`now listening to function requests for ${this.alias}`
);
done.resolve(this);
} else {
const errMessage = 'socket needs to be authenticated first';
plugins.smartlog.defaultLogger.log('error', errMessage);
logger.log('error', errMessage);
done.reject(errMessage);
}
return done.promise;

View File

@ -1,7 +1,6 @@
import * as plugins from './smartsocket.plugins';
// import classes
import { Objectmap } from '@pushrocks/lik';
import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { Smartsocket } from './smartsocket.classes.smartsocket';

View File

@ -4,9 +4,8 @@ import * as plugins from './smartsocket.plugins';
import { SocketFunction, ISocketFunctionCallDataRequest, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
// import classes
import { Objectmap } from '@pushrocks/lik';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { defaultLogger } from '@pushrocks/smartlog';
import { logger } from './smartsocket.logging';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
@ -85,7 +84,7 @@ export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedReques
* handles the response that is received by the requesting side
*/
public async handleResponse(responseDataArg: ISocketRequestDataObject<T>) {
plugins.smartlog.defaultLogger.log('info', 'handling response!');
logger.log('info', 'handling response!');
this.done.resolve(responseDataArg.funcCallData);
this.smartsocketRef.socketRequests.remove(this);
}
@ -102,16 +101,16 @@ export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedReques
);
if (!targetSocketFunction) {
defaultLogger.log(
logger.log(
'warn',
`There is no SocketFunction defined for ${this.funcCallData.funcName}`
);
defaultLogger.log('warn', `So now response is being sent.`);
logger.log('warn', `So now response is being sent.`);
return;
}
plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`);
logger.log('info', `invoking ${targetSocketFunction.name}`);
targetSocketFunction.invoke(this.funcCallData, this.originSocketConnection).then(resultData => {
plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.');
logger.log('info', 'got resultData. Sending it to requesting party.');
const responseData: ISocketRequestDataObject<T> = {
funcCallData: resultData,
shortId: this.shortid

View File

@ -1,7 +1,6 @@
import * as plugins from './smartsocket.plugins';
// import classes
import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from './smartsocket.classes.socketfunction';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
@ -42,7 +41,7 @@ export class SocketRole {
// INSTANCE
public name: string;
public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction<any>>();
public allowedFunctions = new plugins.lik.ObjectMap<SocketFunction<any>>();
constructor(optionsArg: ISocketRoleOptions) {
this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash;

View File

@ -4,6 +4,7 @@ import * as plugins from './smartsocket.plugins';
import * as http from 'http';
import * as https from 'https';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { logger } from './smartsocket.logging';
/**
* class socketServer
@ -55,11 +56,11 @@ export class SocketServer {
// in case an external server has been set "this.standaloneServer" should be false
if (this.httpServer && this.standaloneServer) {
if (!this.smartsocket.options.port) {
plugins.smartlog.defaultLogger.log('error', 'there should be a port specifed for smartsocket!');
logger.log('error', 'there should be a port specifed for smartsocket!');
throw new Error('there should be a port specified for smartsocket');
}
this.httpServer.listen(this.smartsocket.options.port, () => {
plugins.smartlog.defaultLogger.log('success', `Server started in standalone mode on ${this.smartsocket.options.port}`);
logger.log('success', `Server started in standalone mode on ${this.smartsocket.options.port}`);
done.resolve();
});
} else {

View File

@ -0,0 +1,3 @@
import * as plugins from './smartsocket.plugins';
export const logger = new plugins.smartlog.ConsoleLog();