fix(core): update

This commit is contained in:
Philipp Kunz 2019-09-09 23:58:32 +02:00
parent c8b647d2fd
commit 29597daba1
11 changed files with 109 additions and 67 deletions

5
package-lock.json generated
View File

@ -4,6 +4,11 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@apiglobal/typedrequest-interfaces": {
"version": "1.0.7",
"resolved": "https://verdaccio.lossless.one/@apiglobal%2ftypedrequest-interfaces/-/typedrequest-interfaces-1.0.7.tgz",
"integrity": "sha512-yPl0UcLFMwSQL7bK52wVjkgvadC+x2YS3+7T15V1A1dXNxa96yd4WX1fqcKqwnBrvYexq/8FaxWGi98tZ0oNwg=="
},
"@babel/code-frame": { "@babel/code-frame": {
"version": "7.5.5", "version": "7.5.5",
"resolved": "https://verdaccio.lossless.one/@babel%2fcode-frame/-/code-frame-7.5.5.tgz", "resolved": "https://verdaccio.lossless.one/@babel%2fcode-frame/-/code-frame-7.5.5.tgz",

View File

@ -19,6 +19,7 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartsocket#README", "homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": { "dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.7",
"@pushrocks/lik": "^3.0.11", "@pushrocks/lik": "^3.0.11",
"@pushrocks/smartdelay": "^2.0.3", "@pushrocks/smartdelay": "^2.0.3",
"@pushrocks/smartexpress": "^3.0.40", "@pushrocks/smartexpress": "^3.0.40",

View File

@ -11,7 +11,7 @@ import smartsocket = require('../ts/index');
let testSmartsocket: smartsocket.Smartsocket; let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketRole1: smartsocket.SocketRole; let testSocketRole1: smartsocket.SocketRole;
let testSocketFunction1: smartsocket.SocketFunction; let testSocketFunction1: smartsocket.SocketFunction<any>;
let myseServer: smartexpress.Server; let myseServer: smartexpress.Server;
const testConfig = { const testConfig = {

View File

@ -11,8 +11,28 @@ let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketConnection: smartsocket.SocketConnection; let testSocketConnection: smartsocket.SocketConnection;
let testSocketRole1: smartsocket.SocketRole; let testSocketRole1: smartsocket.SocketRole;
let testSocketFunctionForServer: smartsocket.SocketFunction; let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
let testSocketFunctionClient: smartsocket.SocketFunction; let testSocketFunctionClient: smartsocket.SocketFunction<any>;
export interface IReqResClient {
method: 'testFunction1';
request: {
value1: string;
};
response: {
value1: string;
}
}
export interface IReqResServer {
method: 'testFunction2';
request: {
hi: string;
};
response: {
hi: string
}
}
const testConfig = { const testConfig = {
port: 3000 port: 3000
@ -78,7 +98,7 @@ tap.test('2 clients should connect in parallel', async () => {
}); });
tap.test('should be able to make a functionCall from client to server', async () => { tap.test('should be able to make a functionCall from client to server', async () => {
const response = await testSmartsocketClient.serverCall('testFunction1', { const response = await testSmartsocketClient.serverCall<IReqResClient>('testFunction1', {
value1: 'hello' value1: 'hello'
}); });
console.log(response); console.log(response);
@ -86,7 +106,7 @@ tap.test('should be able to make a functionCall from client to server', async ()
}); });
tap.test('should be able to make a functionCall from server to client', async () => { tap.test('should be able to make a functionCall from server to client', async () => {
const response = await testSmartsocket.clientCall( const response = await testSmartsocket.clientCall<IReqResServer>(
'testFunction2', 'testFunction2',
{ {
hi: 'hi there from server' hi: 'hi there from server'

View File

@ -3,7 +3,7 @@ import * as plugins from './smartsocket.plugins';
// classes // classes
import { Objectmap } from '@pushrocks/lik'; import { Objectmap } from '@pushrocks/lik';
import { SocketConnection } from './smartsocket.classes.socketconnection'; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction'; import { ISocketFunctionCallDataRequest, SocketFunction, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
import { SocketRequest } from './smartsocket.classes.socketrequest'; import { SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole'; import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketServer } from './smartsocket.classes.socketserver'; import { SocketServer } from './smartsocket.classes.socketserver';
@ -20,8 +20,8 @@ export class Smartsocket {
public io: SocketIO.Server; public io: SocketIO.Server;
public socketConnections = new Objectmap<SocketConnection>(); public socketConnections = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>(); public socketRoles = new Objectmap<SocketRole>();
public socketFunctions = new Objectmap<SocketFunction>(); public socketFunctions = new Objectmap<SocketFunction<any>>();
public socketRequests = new Objectmap<SocketRequest>(); public socketRequests = new Objectmap<SocketRequest<any>>();
private socketServer = new SocketServer(this); private socketServer = new SocketServer(this);
@ -69,12 +69,12 @@ export class Smartsocket {
/** /**
* allows call to specific client. * allows call to specific client.
*/ */
public async clientCall( public async clientCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(
functionNameArg: string, functionNameArg: T['method'],
dataArg: any, dataArg: T['request'],
targetSocketConnectionArg: SocketConnection targetSocketConnectionArg: SocketConnection
) { ): Promise<T['response']> {
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest<T>(this, {
funcCallData: { funcCallData: {
funcDataArg: dataArg, funcDataArg: dataArg,
funcName: functionNameArg funcName: functionNameArg
@ -83,7 +83,7 @@ export class Smartsocket {
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
side: 'requesting' side: 'requesting'
}); });
const response: ISocketFunctionCall = await socketRequest.dispatch(); const response: ISocketFunctionCallDataResponse<T> = await socketRequest.dispatch();
const result = response.funcDataArg; const result = response.funcDataArg;
return result; return result;
} }
@ -98,7 +98,7 @@ export class Smartsocket {
return; return;
} }
public addSocketFunction(socketFunction: SocketFunction) { public addSocketFunction(socketFunction: SocketFunction<any>) {
this.socketFunctions.add(socketFunction); this.socketFunctions.add(socketFunction);
} }

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
import { SocketConnection } from './smartsocket.classes.socketconnection'; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction'; import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest'; import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole'; import { SocketRole } from './smartsocket.classes.socketrole';
@ -23,8 +23,8 @@ export class SmartsocketClient {
public serverUrl: string; public serverUrl: string;
public serverPort: number; public serverPort: number;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>(); public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>(); public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
public socketRoles = new plugins.lik.Objectmap<SocketRole>(); public socketRoles = new plugins.lik.Objectmap<SocketRole>();
constructor(optionsArg: ISmartsocketClientOptions) { constructor(optionsArg: ISmartsocketClientOptions) {
@ -37,7 +37,7 @@ export class SmartsocketClient {
}); });
} }
public addSocketFunction(socketFunction: SocketFunction) { public addSocketFunction(socketFunction: SocketFunction<any>) {
this.socketFunctions.add(socketFunction); this.socketFunctions.add(socketFunction);
this.socketRole.allowedFunctions.add(socketFunction); this.socketRole.allowedFunctions.add(socketFunction);
} }
@ -88,9 +88,9 @@ export class SmartsocketClient {
* @param functionNameArg * @param functionNameArg
* @param dataArg * @param dataArg
*/ */
public async serverCall(functionNameArg: string, dataArg: any): Promise<any> { public async serverCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(functionNameArg: T['method'], dataArg: T['request']): Promise<T['response']> {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest<T>(this, {
side: 'requesting', side: 'requesting',
originSocketConnection: this.socketConnection, originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),

View File

@ -115,10 +115,10 @@ export class SocketConnection {
public listenToFunctionRequests() { public listenToFunctionRequests() {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
if (this.authenticated) { if (this.authenticated) {
this.socket.on('function', (dataArg: ISocketRequestDataObject) => { this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
// check if requested function is available to the socket's scope // check if requested function is available to the socket's scope
plugins.smartlog.defaultLogger.log('info', 'function request received'); plugins.smartlog.defaultLogger.log('info', 'function request received');
const referencedFunction: SocketFunction = this.role.allowedFunctions.find( const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.find(
socketFunctionArg => { socketFunctionArg => {
return socketFunctionArg.name === dataArg.funcCallData.funcName; return socketFunctionArg.name === dataArg.funcCallData.funcName;
} }
@ -139,7 +139,7 @@ export class SocketConnection {
); );
} }
}); });
this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject) => { this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject<any>) => {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
`received response for request with id ${dataArg.shortId}` `received response for request with id ${dataArg.shortId}`

View File

@ -12,36 +12,44 @@ import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
/** /**
* interface of the contructor options of class SocketFunction * interface of the contructor options of class SocketFunction
*/ */
export interface ISocketFunctionConstructorOptions { export interface ISocketFunctionConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcName: string; funcName: string;
funcDef: TFuncDef; funcDef: TFuncDef<T>;
allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
} }
/** /**
* interface of the Socket Function call, in other words the object that routes a call to a function * interface of the Socket Function call, in other words the object that routes a call to a function
*/ */
export interface ISocketFunctionCall { export interface ISocketFunctionCallDataRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcName: string; funcName: T['method'];
funcDataArg: any; funcDataArg: T['request'];
}
/**
* interface of the Socket Function call, in other words the object that routes a call to a function
*/
export interface ISocketFunctionCallDataResponse<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcName: T['method'];
funcDataArg: T['response'];
} }
/** /**
* interface for function definition of SocketFunction * interface for function definition of SocketFunction
*/ */
export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => PromiseLike<any>; export type TFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = (dataArg: T['request'], connectionArg: SocketConnection) => PromiseLike<T['response']>;
// export classes // export classes
/** /**
* class that respresents a function that can be transparently called using a SocketConnection * class that respresents a function that can be transparently called using a SocketConnection
*/ */
export class SocketFunction { export class SocketFunction<T extends plugins.typedrequestInterfaces.ITypedRequest> {
// STATIC // STATIC
public static getSocketFunctionByName( public static getSocketFunctionByName<Q extends plugins.typedrequestInterfaces.ITypedRequest>(
smartsocketRefArg: Smartsocket | SmartsocketClient, smartsocketRefArg: Smartsocket | SmartsocketClient,
functionNameArg: string functionNameArg: string
): SocketFunction { ): SocketFunction<Q> {
return smartsocketRefArg.socketFunctions.find(socketFunctionArg => { return smartsocketRefArg.socketFunctions.find(socketFunctionArg => {
return socketFunctionArg.name === functionNameArg; return socketFunctionArg.name === functionNameArg;
}); });
@ -49,13 +57,13 @@ export class SocketFunction {
// INSTANCE // INSTANCE
public name: string; public name: string;
public funcDef: TFuncDef; public funcDef: TFuncDef<T>;
public roles: SocketRole[]; public roles: SocketRole[];
/** /**
* the constructor for SocketFunction * the constructor for SocketFunction
*/ */
constructor(optionsArg: ISocketFunctionConstructorOptions) { constructor(optionsArg: ISocketFunctionConstructorOptions<T>) {
this.name = optionsArg.funcName; this.name = optionsArg.funcName;
this.funcDef = optionsArg.funcDef; this.funcDef = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles; this.roles = optionsArg.allowedRoles;
@ -67,20 +75,16 @@ export class SocketFunction {
/** /**
* invokes the function of this SocketFunction * invokes the function of this SocketFunction
*/ */
public invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> { public async invoke(dataArg: ISocketFunctionCallDataRequest<T>, socketConnectionArg: SocketConnection): Promise<ISocketFunctionCallDataResponse<T>> {
const done = plugins.smartpromise.defer();
if (dataArg.funcName === this.name) { if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => { const funcResponseData: ISocketFunctionCallDataResponse<T> = {
const funcResponseData: ISocketFunctionCall = { funcName: this.name,
funcName: this.name, funcDataArg: await this.funcDef(dataArg.funcDataArg, socketConnectionArg)
funcDataArg: resultData };
}; return funcResponseData;
done.resolve(funcResponseData);
});
} else { } else {
throw new Error("SocketFunction.name does not match the data argument's .name!"); throw new Error("SocketFunction.name does not match the data argument's .name!");
} }
return done.promise;
} }
/** /**

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
// import interfaces // import interfaces
import { SocketFunction, ISocketFunctionCall } from './smartsocket.classes.socketfunction'; import { SocketFunction, ISocketFunctionCallDataRequest, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
// import classes // import classes
import { Objectmap } from '@pushrocks/lik'; import { Objectmap } from '@pushrocks/lik';
@ -17,29 +17,29 @@ export type TSocketRequestSide = 'requesting' | 'responding';
/** /**
* interface of constructor of class SocketRequest * interface of constructor of class SocketRequest
*/ */
export interface ISocketRequestConstructorOptions { export interface ISocketRequestConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
side: TSocketRequestSide; side: TSocketRequestSide;
originSocketConnection: SocketConnection; originSocketConnection: SocketConnection;
shortId: string; shortId: string;
funcCallData?: ISocketFunctionCall; funcCallData?: ISocketFunctionCallDataRequest<T>;
} }
/** /**
* request object that is sent initially and may or may not receive a response * request object that is sent initially and may or may not receive a response
*/ */
export interface ISocketRequestDataObject { export interface ISocketRequestDataObject<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcCallData: ISocketFunctionCall; funcCallData: ISocketFunctionCallDataRequest<T> | ISocketFunctionCallDataResponse<T>;
shortId: string; shortId: string;
responseTimeout?: number; responseTimeout?: number;
} }
// export classes // export classes
export class SocketRequest { export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
// STATIC // STATIC
public static getSocketRequestById( public static getSocketRequestById(
smartsocketRef: Smartsocket | SmartsocketClient, smartsocketRef: Smartsocket | SmartsocketClient,
shortIdArg: string shortIdArg: string
): SocketRequest { ): SocketRequest<any> {
return smartsocketRef.socketRequests.find(socketRequestArg => { return smartsocketRef.socketRequests.find(socketRequestArg => {
return socketRequestArg.shortid === shortIdArg; return socketRequestArg.shortid === shortIdArg;
}); });
@ -50,14 +50,14 @@ export class SocketRequest {
public side: TSocketRequestSide; public side: TSocketRequestSide;
public shortid: string; public shortid: string;
public originSocketConnection: SocketConnection; public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall; public funcCallData: ISocketFunctionCallDataRequest<T>;
public done = plugins.smartpromise.defer<ISocketFunctionCall>(); public done = plugins.smartpromise.defer<ISocketFunctionCallDataResponse<T>>();
public smartsocketRef: Smartsocket | SmartsocketClient; public smartsocketRef: Smartsocket | SmartsocketClient;
constructor( constructor(
smartsocketRefArg: Smartsocket | SmartsocketClient, smartsocketRefArg: Smartsocket | SmartsocketClient,
optionsArg: ISocketRequestConstructorOptions optionsArg: ISocketRequestConstructorOptions<T>
) { ) {
this.smartsocketRef = smartsocketRefArg; this.smartsocketRef = smartsocketRefArg;
this.side = optionsArg.side; this.side = optionsArg.side;
@ -72,8 +72,8 @@ export class SocketRequest {
/** /**
* dispatches a socketrequest from the requesting to the receiving side * dispatches a socketrequest from the requesting to the receiving side
*/ */
public dispatch(): Promise<ISocketFunctionCall> { public dispatch(): Promise<ISocketFunctionCallDataResponse<T>> {
const requestData: ISocketRequestDataObject = { const requestData: ISocketRequestDataObject<T> = {
funcCallData: this.funcCallData, funcCallData: this.funcCallData,
shortId: this.shortid shortId: this.shortid
}; };
@ -84,7 +84,7 @@ export class SocketRequest {
/** /**
* handles the response that is received by the requesting side * handles the response that is received by the requesting side
*/ */
public handleResponse(responseDataArg: ISocketRequestDataObject) { public async handleResponse(responseDataArg: ISocketRequestDataObject<T>) {
plugins.smartlog.defaultLogger.log('info', 'handling response!'); plugins.smartlog.defaultLogger.log('info', 'handling response!');
this.done.resolve(responseDataArg.funcCallData); this.done.resolve(responseDataArg.funcCallData);
this.smartsocketRef.socketRequests.remove(this); this.smartsocketRef.socketRequests.remove(this);
@ -96,7 +96,7 @@ export class SocketRequest {
* creates the response on the responding side * creates the response on the responding side
*/ */
public async createResponse(): Promise<void> { public async createResponse(): Promise<void> {
const targetSocketFunction: SocketFunction = SocketFunction.getSocketFunctionByName( const targetSocketFunction: SocketFunction<T> = SocketFunction.getSocketFunctionByName(
this.smartsocketRef, this.smartsocketRef,
this.funcCallData.funcName this.funcCallData.funcName
); );
@ -112,11 +112,11 @@ export class SocketRequest {
plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`); plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`);
targetSocketFunction.invoke(this.funcCallData, this.originSocketConnection).then(resultData => { targetSocketFunction.invoke(this.funcCallData, this.originSocketConnection).then(resultData => {
plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.'); plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.');
const requestData: ISocketRequestDataObject = { const responseData: ISocketRequestDataObject<T> = {
funcCallData: resultData, funcCallData: resultData,
shortId: this.shortid shortId: this.shortid
}; };
this.originSocketConnection.socket.emit('functionResponse', requestData); this.originSocketConnection.socket.emit('functionResponse', responseData);
this.smartsocketRef.socketRequests.remove(this); this.smartsocketRef.socketRequests.remove(this);
}); });
} }

View File

@ -42,7 +42,7 @@ export class SocketRole {
// INSTANCE // INSTANCE
public name: string; public name: string;
public passwordHash: string; public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction>(); public allowedFunctions = new Objectmap<SocketFunction<any>>();
constructor(optionsArg: ISocketRoleOptions) { constructor(optionsArg: ISocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
@ -52,7 +52,7 @@ export class SocketRole {
* adds the socketfunction to the socketrole * adds the socketfunction to the socketrole
* @param socketFunctionArg * @param socketFunctionArg
*/ */
public addSocketFunction(socketFunctionArg: SocketFunction) { public addSocketFunction(socketFunctionArg: SocketFunction<any>) {
this.allowedFunctions.add(socketFunctionArg); this.allowedFunctions.add(socketFunctionArg);
} }
} }

View File

@ -1,12 +1,16 @@
// apiglobal scope
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
export {typedrequestInterfaces};
// pushrocks scope
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smartlog from '@pushrocks/smartlog'; import * as smartlog from '@pushrocks/smartlog';
import * as smarthash from '@pushrocks/smarthash'; import * as smarthash from '@pushrocks/smarthash';
import * as smartdelay from '@pushrocks/smartdelay'; import * as smartdelay from '@pushrocks/smartdelay';
import * as smartexpress from '@pushrocks/smartexpress'; import * as smartexpress from '@pushrocks/smartexpress';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as shortid from 'shortid';
import socketIo from 'socket.io';
import socketIoClient from 'socket.io-client';
export { export {
lik, lik,
@ -14,7 +18,15 @@ export {
smarthash, smarthash,
smartdelay, smartdelay,
smartexpress, smartexpress,
smartpromise, smartpromise
};
// third party scope
import * as shortid from 'shortid';
import socketIo from 'socket.io';
import socketIoClient from 'socket.io-client';
export {
shortid, shortid,
socketIo, socketIo,
socketIoClient socketIoClient