Compare commits

..

6 Commits

Author SHA1 Message Date
85b69e487a 1.1.44 2019-08-13 11:36:32 +02:00
0d94ed9345 fix(core): update 2019-08-13 11:36:31 +02:00
e188b18016 1.1.43 2019-08-13 09:37:23 +02:00
33c0fa3746 fix(core): update 2019-08-13 09:37:23 +02:00
52be1415ee 1.1.42 2019-08-12 22:46:57 +02:00
e75d5eabdb fix(core): update 2019-08-12 22:46:57 +02:00
12 changed files with 95 additions and 75 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.1.41", "version": "1.1.44",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.1.41", "version": "1.1.44",
"description": "easy and secure websocket communication", "description": "easy and secure websocket communication",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",

View File

@ -93,9 +93,7 @@ tap.test('should be able to make a functionCall from client to server', async ()
console.log(response); console.log(response);
}); });
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 () => {});
});
// terminate // terminate
tap.test('should close the server', async () => { tap.test('should close the server', async () => {

View File

@ -9,8 +9,10 @@ import smartsocket = require('../ts/index');
let testSmartsocket: smartsocket.Smartsocket; let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketConnection: smartsocket.SocketConnection;
let testSocketRole1: smartsocket.SocketRole; let testSocketRole1: smartsocket.SocketRole;
let testSocketFunction1: smartsocket.SocketFunction; let testSocketFunctionForServer: smartsocket.SocketFunction;
let testSocketFunctionClient: smartsocket.SocketFunction;
const testConfig = { const testConfig = {
port: 3000 port: 3000
@ -33,14 +35,23 @@ tap.test('should add a socketrole', async () => {
// class SocketFunction // class SocketFunction
tap.test('should register a new Function', async () => { tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({ testSocketFunctionForServer = new smartsocket.SocketFunction({
allowedRoles: [testSocketRole1], allowedRoles: [testSocketRole1],
funcDef: async (dataArg, socketConnectionArg) => { funcDef: async (dataArg, socketConnectionArg) => {
return dataArg; return dataArg;
}, },
funcName: 'testFunction1' funcName: 'testFunction1'
}); });
testSmartsocket.addSocketFunction(testSocketFunction1); testSmartsocket.addSocketFunction(testSocketFunctionForServer);
testSocketFunctionClient = new smartsocket.SocketFunction({
allowedRoles: [],
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1'
});
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
console.log(testSmartsocket.socketFunctions); console.log(testSmartsocket.socketFunctions);
}); });
@ -57,17 +68,11 @@ tap.test('should react to a new websocket connection from client', async () => {
alias: 'testClient1', alias: 'testClient1',
role: 'testRole1' role: 'testRole1'
}); });
testSmartsocketClient.addSocketFunction(testSocketFunction1); testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
console.log(testSmartsocketClient.socketFunctions); console.log(testSmartsocketClient.socketFunctions);
await testSmartsocketClient.connect(); await testSmartsocketClient.connect();
}); });
tap.test('client should disconnect and reconnect', async tools => {
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
});
tap.test('2 clients should connect in parallel', async () => { tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test // TODO: implement parallel test
}); });
@ -80,12 +85,24 @@ 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 testSmartsocketClient.serverCall('testFunction1', { const response = await testSmartsocket.clientCall(
hi: 'hi there from client' 'testFunction1',
}); {
hi: 'hi there from server'
},
testSmartsocket.socketConnections.find(socketConnection => {
return true;
})
);
console.log(response); console.log(response);
}); });
tap.test('client should disconnect and reconnect', async tools => {
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
});
// terminate // terminate
tap.test('should close the server', async () => { tap.test('should close the server', async () => {
await testSmartsocket.stop(); await testSmartsocket.stop();

View File

@ -1,5 +1,3 @@
import * as plugins from './smartsocket.plugins';
// export main classes // export main classes
export * from './smartsocket.classes.smartsocket'; export * from './smartsocket.classes.smartsocket';
export * from './smartsocket.classes.smartsocketclient'; export * from './smartsocket.classes.smartsocketclient';

View File

@ -18,7 +18,7 @@ export interface ISmartsocketConstructorOptions {
export class Smartsocket { export class Smartsocket {
public options: ISmartsocketConstructorOptions; public options: ISmartsocketConstructorOptions;
public io: SocketIO.Server; public io: SocketIO.Server;
public openSockets = 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>();
public socketRequests = new Objectmap<SocketRequest>(); public socketRequests = new Objectmap<SocketRequest>();
@ -50,14 +50,14 @@ export class Smartsocket {
*/ */
public async stop() { public async stop() {
await plugins.smartdelay.delayFor(1000); await plugins.smartdelay.delayFor(1000);
this.openSockets.forEach((socketObjectArg: SocketConnection) => { this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
`disconnect socket with >>alias ${socketObjectArg.alias}` `disconnect socket with >>alias ${socketObjectArg.alias}`
); );
socketObjectArg.socket.disconnect(); socketObjectArg.socket.disconnect();
}); });
this.openSockets.wipe(); this.socketConnections.wipe();
this.io.close(); this.io.close();
// stop the corresponging server // stop the corresponging server
@ -74,7 +74,6 @@ export class Smartsocket {
dataArg: any, dataArg: any,
targetSocketConnectionArg: SocketConnection targetSocketConnectionArg: SocketConnection
) { ) {
const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest(this, {
funcCallData: { funcCallData: {
funcDataArg: dataArg, funcDataArg: dataArg,
@ -84,10 +83,8 @@ export class Smartsocket {
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
side: 'requesting' side: 'requesting'
}); });
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => { const response: ISocketFunctionCall = await socketRequest.dispatch();
done.resolve(dataArg2.funcDataArg); const result = response.funcDataArg;
});
const result = await done.promise;
return result; return result;
} }
@ -108,7 +105,7 @@ export class Smartsocket {
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocketConnection(socketArg) { private async _handleSocketConnection(socketArg: plugins.socketIo.Socket) {
const socketConnection: SocketConnection = new SocketConnection({ const socketConnection: SocketConnection = new SocketConnection({
alias: undefined, alias: undefined,
authenticated: false, authenticated: false,
@ -118,14 +115,8 @@ export class Smartsocket {
socket: socketArg socket: socketArg
}); });
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...'); plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
this.openSockets.add(socketConnection); this.socketConnections.add(socketConnection);
socketConnection await socketConnection.authenticate();
.authenticate() await socketConnection.listenToFunctionRequests();
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch(err => {
console.log(err);
});
} }
} }

View File

@ -18,11 +18,10 @@ export interface ISmartsocketClientOptions {
export class SmartsocketClient { export class SmartsocketClient {
public alias: string; public alias: string;
public role: string; public socketRole: SocketRole;
public socketConnection: SocketConnection; public socketConnection: SocketConnection;
public serverUrl: string; public serverUrl: string;
public serverPort: number; public serverPort: number;
public serverPassword: string;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>(); public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>(); public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
@ -30,14 +29,17 @@ export class SmartsocketClient {
constructor(optionsArg: ISmartsocketClientOptions) { constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
this.role = optionsArg.role;
this.serverUrl = optionsArg.url; this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port; this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password; this.socketRole = new SocketRole({
name: optionsArg.role,
passwordHash: optionsArg.password
});
} }
public addSocketFunction(socketFunction: SocketFunction) { public addSocketFunction(socketFunction: SocketFunction) {
this.socketFunctions.add(socketFunction); this.socketFunctions.add(socketFunction);
this.socketRole.allowedFunctions.add(socketFunction);
} }
/** /**
@ -50,7 +52,7 @@ export class SmartsocketClient {
this.socketConnection = new SocketConnection({ this.socketConnection = new SocketConnection({
alias: this.alias, alias: this.alias,
authenticated: false, authenticated: false,
role: undefined, role: this.socketRole,
side: 'client', side: 'client',
smartsocketHost: this, smartsocketHost: this,
socket: plugins.socketIoClient(socketUrl, { multiplex: false }) socket: plugins.socketIoClient(socketUrl, { multiplex: false })
@ -58,8 +60,8 @@ export class SmartsocketClient {
this.socketConnection.socket.on('requestAuth', () => { this.socketConnection.socket.on('requestAuth', () => {
console.log('server requested authentication'); console.log('server requested authentication');
this.socketConnection.socket.emit('dataAuth', { this.socketConnection.socket.emit('dataAuth', {
role: this.role, role: this.socketRole.name,
password: this.serverPassword, password: this.socketRole.passwordHash,
alias: this.alias alias: this.alias
}); });
this.socketConnection.socket.on('authenticated', () => { this.socketConnection.socket.on('authenticated', () => {
@ -72,16 +74,21 @@ export class SmartsocketClient {
return done.promise; return done.promise;
} }
public disconnect() { /**
const done = plugins.smartpromise.defer(); * disconnect from the server
this.socketConnection.socket.disconnect(); */
public async disconnect() {
this.socketConnection.socket.disconnect(true);
this.socketConnection = undefined; this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!'); plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
done.resolve();
return done.promise;
} }
public serverCall(functionNameArg: string, dataArg: any) { /**
* dispatches a server call
* @param functionNameArg
* @param dataArg
*/
public async serverCall(functionNameArg: string, dataArg: any): Promise<any> {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest(this, {
side: 'requesting', side: 'requesting',
@ -92,9 +99,8 @@ export class SmartsocketClient {
funcDataArg: dataArg funcDataArg: dataArg
} }
}); });
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => { const response = await socketRequest.dispatch();
done.resolve(dataArg2.funcDataArg); const result = response.funcDataArg;
}); return result;
return done.promise;
} }
} }

View File

@ -5,10 +5,7 @@ import { Objectmap } from '@pushrocks/lik';
// import classes // import classes
import { Smartsocket } from './smartsocket.classes.smartsocket'; import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SocketFunction } from './smartsocket.classes.socketfunction'; import { SocketFunction } from './smartsocket.classes.socketfunction';
import { import { SocketRequest, ISocketRequestDataObject } from './smartsocket.classes.socketrequest';
SocketRequest,
ISocketRequestDataObject,
} from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole'; import { SocketRole } from './smartsocket.classes.socketrole';
// socket.io // socket.io
@ -126,7 +123,7 @@ export class SocketConnection {
return socketFunctionArg.name === dataArg.funcCallData.funcName; return socketFunctionArg.name === dataArg.funcCallData.funcName;
} }
); );
if (referencedFunction !== undefined) { if (referencedFunction) {
plugins.smartlog.defaultLogger.log('ok', 'function in access scope'); plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
const localSocketRequest = new SocketRequest(this.smartsocketRef, { const localSocketRequest = new SocketRequest(this.smartsocketRef, {
side: 'responding', side: 'responding',
@ -147,7 +144,10 @@ export class SocketConnection {
'info', 'info',
`received response for request with id ${dataArg.shortId}` `received response for request with id ${dataArg.shortId}`
); );
const targetSocketRequest = SocketRequest.getSocketRequestById(this.smartsocketRef, dataArg.shortId); const targetSocketRequest = SocketRequest.getSocketRequestById(
this.smartsocketRef,
dataArg.shortId
);
targetSocketRequest.handleResponse(dataArg); targetSocketRequest.handleResponse(dataArg);
}); });
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(

View File

@ -38,7 +38,10 @@ export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => Promis
*/ */
export class SocketFunction { export class SocketFunction {
// STATIC // STATIC
public static getSocketFunctionByName (smartsocketRefArg: Smartsocket | SmartsocketClient, functionNameArg: string): SocketFunction { public static getSocketFunctionByName(
smartsocketRefArg: Smartsocket | SmartsocketClient,
functionNameArg: string
): SocketFunction {
console.log(smartsocketRefArg.socketFunctions); console.log(smartsocketRefArg.socketFunctions);
return smartsocketRefArg.socketFunctions.find(socketFunctionArg => { return smartsocketRefArg.socketFunctions.find(socketFunctionArg => {
return socketFunctionArg.name === functionNameArg; return socketFunctionArg.name === functionNameArg;

View File

@ -17,7 +17,7 @@ export type TSocketRequestSide = 'requesting' | 'responding';
/** /**
* interface of constructor of class SocketRequest * interface of constructor of class SocketRequest
*/ */
export interface SocketRequestConstructorOptions { export interface ISocketRequestConstructorOptions {
side: TSocketRequestSide; side: TSocketRequestSide;
originSocketConnection: SocketConnection; originSocketConnection: SocketConnection;
shortId: string; shortId: string;
@ -51,12 +51,14 @@ export class SocketRequest {
public shortid: string; public shortid: string;
public originSocketConnection: SocketConnection; public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall; public funcCallData: ISocketFunctionCall;
public done = plugins.smartpromise.defer(); public done = plugins.smartpromise.defer<ISocketFunctionCall>();
public smartsocketRef: Smartsocket | SmartsocketClient; public smartsocketRef: Smartsocket | SmartsocketClient;
constructor(
constructor(smartsocketRefArg: Smartsocket | SmartsocketClient, optionsArg: SocketRequestConstructorOptions) { smartsocketRefArg: Smartsocket | SmartsocketClient,
optionsArg: ISocketRequestConstructorOptions
) {
this.smartsocketRef = smartsocketRefArg; this.smartsocketRef = smartsocketRefArg;
this.side = optionsArg.side; this.side = optionsArg.side;
this.shortid = optionsArg.shortId; this.shortid = optionsArg.shortId;
@ -70,7 +72,7 @@ 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() { public dispatch(): Promise<ISocketFunctionCall> {
const requestData: ISocketRequestDataObject = { const requestData: ISocketRequestDataObject = {
funcCallData: this.funcCallData, funcCallData: this.funcCallData,
shortId: this.shortid shortId: this.shortid

View File

@ -1,6 +1,5 @@
import * as plugins from './smartsocket.plugins'; import * as plugins from './smartsocket.plugins';
// import classes // import classes
import { Objectmap } from '@pushrocks/lik'; import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from './smartsocket.classes.socketfunction'; import { SocketFunction } from './smartsocket.classes.socketfunction';
@ -11,7 +10,7 @@ import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.soc
/** /**
* interface for class SocketRole * interface for class SocketRole
*/ */
export interface SocketRoleOptions { export interface ISocketRoleOptions {
name: string; name: string;
passwordHash: string; passwordHash: string;
} }
@ -23,7 +22,7 @@ export class SocketRole {
// STATIC // STATIC
public static getSocketRoleByName( public static getSocketRoleByName(
referenceSmartsocket: Smartsocket | SmartsocketClient, referenceSmartsocket: Smartsocket | SmartsocketClient,
socketRoleNameArg: string, socketRoleNameArg: string
): SocketRole { ): SocketRole {
return referenceSmartsocket.socketRoles.find(socketRoleArg => { return referenceSmartsocket.socketRoles.find(socketRoleArg => {
return socketRoleArg.name === socketRoleNameArg; return socketRoleArg.name === socketRoleNameArg;
@ -34,7 +33,8 @@ export class SocketRole {
dataArg: ISocketConnectionAuthenticationObject, dataArg: ISocketConnectionAuthenticationObject,
referenceSmartsocket: Smartsocket | SmartsocketClient referenceSmartsocket: Smartsocket | SmartsocketClient
): boolean { ): boolean {
const targetPasswordHash = SocketRole.getSocketRoleByName(referenceSmartsocket, dataArg.role).passwordHash; const targetPasswordHash = SocketRole.getSocketRoleByName(referenceSmartsocket, dataArg.role)
.passwordHash;
const computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password); const computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password);
return targetPasswordHash === computedCompareHash; return targetPasswordHash === computedCompareHash;
} }
@ -43,10 +43,15 @@ export class SocketRole {
public name: string; public name: string;
public passwordHash: string; public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction>(); public allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: SocketRoleOptions) { constructor(optionsArg: ISocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
} }
/**
* adds the socketfunction to the socketrole
* @param socketFunctionArg
*/
public addSocketFunction(socketFunctionArg: SocketFunction) { public addSocketFunction(socketFunctionArg: SocketFunction) {
this.allowedFunctions.add(socketFunctionArg); this.allowedFunctions.add(socketFunctionArg);
} }