Compare commits

..

8 Commits

Author SHA1 Message Date
d6e81288b4 1.1.45 2019-08-13 15:58:08 +02:00
c441d89596 fix(core): update 2019-08-13 15:58:08 +02:00
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
13 changed files with 99 additions and 80 deletions

2
package-lock.json generated
View File

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

View File

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

View File

@ -1 +1 @@
console.log('TODO');
console.log('TODO');

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -54,11 +54,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) {
console.log('there should be a port specifed for smartsocket!');
plugins.smartlog.defaultLogger.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, () => {
console.log(`Server started in standalone mode on ${this.smartsocket.options.port}`);
plugins.smartlog.defaultLogger.log('success', `Server started in standalone mode on ${this.smartsocket.options.port}`);
done.resolve();
});
} else {