Compare commits

..

12 Commits

Author SHA1 Message Date
75aa1f6f0d 1.1.52 2019-11-03 19:17:26 +01:00
3f073ab9b3 fix(core): update 2019-11-03 19:17:26 +01:00
08c1618ea8 1.1.51 2019-11-03 18:33:46 +01:00
eb181fa2f6 fix(core): update 2019-11-03 18:33:46 +01:00
c901ab75d3 1.1.50 2019-11-03 16:48:35 +01:00
075c59ed2c fix(core): update 2019-11-03 16:48:35 +01:00
8d358dd93d 1.1.49 2019-09-10 00:21:48 +02:00
e576d6058a fix(core): update 2019-09-10 00:21:47 +02:00
6546da2394 1.1.48 2019-09-09 23:58:32 +02:00
29597daba1 fix(core): update 2019-09-09 23:58:32 +02:00
c8b647d2fd 1.1.47 2019-09-09 16:20:43 +02:00
1fa4aca049 fix(core): update 2019-09-09 16:20:43 +02:00
11 changed files with 678 additions and 474 deletions

916
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartsocket",
"version": "1.1.46",
"version": "1.1.52",
"description": "easy and secure websocket communication",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
@ -19,26 +19,26 @@
},
"homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.7",
"@pushrocks/lik": "^3.0.11",
"@pushrocks/smartdelay": "^2.0.3",
"@pushrocks/smartexpress": "^3.0.40",
"@pushrocks/smartdelay": "^2.0.6",
"@pushrocks/smartexpress": "^3.0.52",
"@pushrocks/smarthash": "^2.0.6",
"@pushrocks/smartlog": "^2.0.19",
"@pushrocks/smartpromise": "^3.0.2",
"@types/shortid": "0.0.29",
"@types/socket.io": "^2.1.2",
"@pushrocks/smartlog": "^2.0.21",
"@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/smartunique": "^3.0.1",
"@types/socket.io": "^2.1.4",
"@types/socket.io-client": "^1.4.32",
"shortid": "^2.2.14",
"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0"
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24",
"@gitzone/tstest": "^1.0.28",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.3",
"tslint": "^5.19.0",
"@types/node": "^12.12.5",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
},
"private": false,

View File

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

View File

@ -11,8 +11,28 @@ let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketConnection: smartsocket.SocketConnection;
let testSocketRole1: smartsocket.SocketRole;
let testSocketFunctionForServer: smartsocket.SocketFunction;
let testSocketFunctionClient: smartsocket.SocketFunction;
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
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 = {
port: 3000
@ -49,7 +69,7 @@ tap.test('should register a new Function', async () => {
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1'
funcName: 'testFunction2'
});
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
console.log(testSmartsocket.socketFunctions);
@ -78,15 +98,16 @@ tap.test('2 clients should connect in parallel', 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'
});
console.log(response);
expect(response.value1).to.equal('hello');
});
tap.test('should be able to make a functionCall from server to client', async () => {
const response = await testSmartsocket.clientCall(
'testFunction1',
const response = await testSmartsocket.clientCall<IReqResServer>(
'testFunction2',
{
hi: 'hi there from server'
},
@ -95,6 +116,7 @@ tap.test('should be able to make a functionCall from server to client', async ()
})
);
console.log(response);
expect(response.hi).to.equal('hi there from server');
});
tap.test('client should disconnect and reconnect', async tools => {

View File

@ -3,7 +3,7 @@ import * as plugins from './smartsocket.plugins';
// classes
import { Objectmap } from '@pushrocks/lik';
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 { SocketRole } from './smartsocket.classes.socketrole';
import { SocketServer } from './smartsocket.classes.socketserver';
@ -16,12 +16,16 @@ export interface ISmartsocketConstructorOptions {
}
export class Smartsocket {
/**
* a unique id to detect server restarts
*/
public id = 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>();
public socketRequests = new Objectmap<SocketRequest>();
public socketFunctions = new Objectmap<SocketFunction<any>>();
public socketRequests = new Objectmap<SocketRequest<any>>();
private socketServer = new SocketServer(this);
@ -69,21 +73,21 @@ export class Smartsocket {
/**
* allows call to specific client.
*/
public async clientCall(
functionNameArg: string,
dataArg: any,
public async clientCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(
functionNameArg: T['method'],
dataArg: T['request'],
targetSocketConnectionArg: SocketConnection
) {
const socketRequest = new SocketRequest(this, {
): Promise<T['response']> {
const socketRequest = new SocketRequest<T>(this, {
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
shortId: plugins.smartunique.shortId(),
side: 'requesting'
});
const response: ISocketFunctionCall = await socketRequest.dispatch();
const response: ISocketFunctionCallDataResponse<T> = await socketRequest.dispatch();
const result = response.funcDataArg;
return result;
}
@ -98,7 +102,7 @@ export class Smartsocket {
return;
}
public addSocketFunction(socketFunction: SocketFunction) {
public addSocketFunction(socketFunction: SocketFunction<any>) {
this.socketFunctions.add(socketFunction);
}

View File

@ -1,7 +1,7 @@
import * as plugins from './smartsocket.plugins';
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 { SocketRole } from './smartsocket.classes.socketrole';
@ -23,8 +23,8 @@ export class SmartsocketClient {
public serverUrl: string;
public serverPort: number;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
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) {
@ -37,7 +37,7 @@ export class SmartsocketClient {
});
}
public addSocketFunction(socketFunction: SocketFunction) {
public addSocketFunction(socketFunction: SocketFunction<any>) {
this.socketFunctions.add(socketFunction);
this.socketRole.allowedFunctions.add(socketFunction);
}
@ -70,6 +70,14 @@ export class SmartsocketClient {
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
// handle errors
this.socketConnection.socket.on('reconnect_failed', async () => {
this.disconnect();
});
this.socketConnection.socket.on('connect_error', async () => {
this.disconnect();
});
});
return done.promise;
}
@ -78,9 +86,19 @@ export class SmartsocketClient {
* disconnect from the server
*/
public async disconnect() {
this.socketConnection.socket.disconnect(true);
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
if (this.socketConnection) {
this.socketConnection.socket.disconnect(true);
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
}
}
/**
* try a reconnection
*/
public async tryDebouncedReconnect() {
await plugins.smartdelay.delayForRandom(10000, 60000);
await this.connect();
}
/**
@ -88,12 +106,12 @@ export class SmartsocketClient {
* @param functionNameArg
* @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 socketRequest = new SocketRequest(this, {
const socketRequest = new SocketRequest<T>(this, {
side: 'requesting',
originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(),
shortId: plugins.smartunique.shortId(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg

View File

@ -115,10 +115,10 @@ export class SocketConnection {
public listenToFunctionRequests() {
const done = plugins.smartpromise.defer();
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
plugins.smartlog.defaultLogger.log('info', 'function request received');
const referencedFunction: SocketFunction = this.role.allowedFunctions.find(
const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.find(
socketFunctionArg => {
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(
'info',
`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
*/
export interface ISocketFunctionConstructorOptions {
funcName: string;
funcDef: TFuncDef;
export interface ISocketFunctionConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcName: T['method'];
funcDef: TFuncDef<T>;
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
*/
export interface ISocketFunctionCall {
funcName: string;
funcDataArg: any;
export interface ISocketFunctionCallDataRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcName: T['method'];
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
*/
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
/**
* 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
public static getSocketFunctionByName(
public static getSocketFunctionByName<Q extends plugins.typedrequestInterfaces.ITypedRequest>(
smartsocketRefArg: Smartsocket | SmartsocketClient,
functionNameArg: string
): SocketFunction {
): SocketFunction<Q> {
return smartsocketRefArg.socketFunctions.find(socketFunctionArg => {
return socketFunctionArg.name === functionNameArg;
});
@ -49,13 +57,13 @@ export class SocketFunction {
// INSTANCE
public name: string;
public funcDef: TFuncDef;
public funcDef: TFuncDef<T>;
public roles: SocketRole[];
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: ISocketFunctionConstructorOptions) {
constructor(optionsArg: ISocketFunctionConstructorOptions<T>) {
this.name = optionsArg.funcName;
this.funcDef = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles;
@ -67,20 +75,16 @@ export class SocketFunction {
/**
* invokes the function of this SocketFunction
*/
public invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> {
const done = plugins.smartpromise.defer();
public async invoke(dataArg: ISocketFunctionCallDataRequest<T>, socketConnectionArg: SocketConnection): Promise<ISocketFunctionCallDataResponse<T>> {
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => {
const funcResponseData: ISocketFunctionCall = {
funcName: this.name,
funcDataArg: resultData
};
done.resolve(funcResponseData);
});
const funcResponseData: ISocketFunctionCallDataResponse<T> = {
funcName: this.name,
funcDataArg: await this.funcDef(dataArg.funcDataArg, socketConnectionArg)
};
return funcResponseData;
} else {
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 interfaces
import { SocketFunction, ISocketFunctionCall } from './smartsocket.classes.socketfunction';
import { SocketFunction, ISocketFunctionCallDataRequest, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
// import classes
import { Objectmap } from '@pushrocks/lik';
@ -17,29 +17,29 @@ export type TSocketRequestSide = 'requesting' | 'responding';
/**
* interface of constructor of class SocketRequest
*/
export interface ISocketRequestConstructorOptions {
export interface ISocketRequestConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
side: TSocketRequestSide;
originSocketConnection: SocketConnection;
shortId: string;
funcCallData?: ISocketFunctionCall;
funcCallData?: ISocketFunctionCallDataRequest<T>;
}
/**
* request object that is sent initially and may or may not receive a response
*/
export interface ISocketRequestDataObject {
funcCallData: ISocketFunctionCall;
export interface ISocketRequestDataObject<T extends plugins.typedrequestInterfaces.ITypedRequest> {
funcCallData: ISocketFunctionCallDataRequest<T> | ISocketFunctionCallDataResponse<T>;
shortId: string;
responseTimeout?: number;
}
// export classes
export class SocketRequest {
export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
// STATIC
public static getSocketRequestById(
smartsocketRef: Smartsocket | SmartsocketClient,
shortIdArg: string
): SocketRequest {
): SocketRequest<any> {
return smartsocketRef.socketRequests.find(socketRequestArg => {
return socketRequestArg.shortid === shortIdArg;
});
@ -50,14 +50,14 @@ export class SocketRequest {
public side: TSocketRequestSide;
public shortid: string;
public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall;
public done = plugins.smartpromise.defer<ISocketFunctionCall>();
public funcCallData: ISocketFunctionCallDataRequest<T>;
public done = plugins.smartpromise.defer<ISocketFunctionCallDataResponse<T>>();
public smartsocketRef: Smartsocket | SmartsocketClient;
constructor(
smartsocketRefArg: Smartsocket | SmartsocketClient,
optionsArg: ISocketRequestConstructorOptions
optionsArg: ISocketRequestConstructorOptions<T>
) {
this.smartsocketRef = smartsocketRefArg;
this.side = optionsArg.side;
@ -72,8 +72,8 @@ export class SocketRequest {
/**
* dispatches a socketrequest from the requesting to the receiving side
*/
public dispatch(): Promise<ISocketFunctionCall> {
const requestData: ISocketRequestDataObject = {
public dispatch(): Promise<ISocketFunctionCallDataResponse<T>> {
const requestData: ISocketRequestDataObject<T> = {
funcCallData: this.funcCallData,
shortId: this.shortid
};
@ -84,7 +84,7 @@ export class SocketRequest {
/**
* 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!');
this.done.resolve(responseDataArg.funcCallData);
this.smartsocketRef.socketRequests.remove(this);
@ -96,7 +96,7 @@ export class SocketRequest {
* creates the response on the responding side
*/
public async createResponse(): Promise<void> {
const targetSocketFunction: SocketFunction = SocketFunction.getSocketFunctionByName(
const targetSocketFunction: SocketFunction<T> = SocketFunction.getSocketFunctionByName(
this.smartsocketRef,
this.funcCallData.funcName
);
@ -112,11 +112,11 @@ export class SocketRequest {
plugins.smartlog.defaultLogger.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.');
const requestData: ISocketRequestDataObject = {
const responseData: ISocketRequestDataObject<T> = {
funcCallData: resultData,
shortId: this.shortid
};
this.originSocketConnection.socket.emit('functionResponse', requestData);
this.originSocketConnection.socket.emit('functionResponse', responseData);
this.smartsocketRef.socketRequests.remove(this);
});
}

View File

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

View File

@ -1,12 +1,17 @@
// apiglobal scope
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
export {typedrequestInterfaces};
// pushrocks scope
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 smartexpress from '@pushrocks/smartexpress';
import * as smartpromise from '@pushrocks/smartpromise';
import * as shortid from 'shortid';
import socketIo from 'socket.io';
import socketIoClient from 'socket.io-client';
import * as smartunique from '@pushrocks/smartunique';
export {
lik,
@ -15,7 +20,14 @@ export {
smartdelay,
smartexpress,
smartpromise,
shortid,
smartunique,
};
// third party scope
import socketIo from 'socket.io';
import socketIoClient from 'socket.io-client';
export {
socketIo,
socketIoClient
};