Compare commits

..

81 Commits

Author SHA1 Message Date
1b45fb45e9 1.1.37 2019-06-06 23:11:03 +02:00
c22adc9b57 fix(core): update 2019-06-06 23:11:03 +02:00
35e87306dd 1.1.36 2019-05-02 11:46:36 +02:00
1b20aa5992 fix(core): update 2019-05-02 11:46:36 +02:00
a63f14da47 1.1.35 2019-04-26 17:35:15 +02:00
86cf94c5dc fix(core): update 2019-04-26 17:35:15 +02:00
0d39d6f237 1.1.34 2019-04-24 18:30:57 +02:00
1856ad5cfc fix(core): update 2019-04-24 18:30:56 +02:00
b5dab810e8 1.1.33 2019-04-24 17:57:17 +02:00
f145b85b1c fix(core): update 2019-04-24 17:57:16 +02:00
a8c647c5c7 1.1.32 2019-04-24 17:17:36 +02:00
22472e0adf fix(core): update 2019-04-24 17:17:36 +02:00
333ddb4d9c 1.1.31 2019-04-24 17:10:52 +02:00
126f020bf8 fix(core): update 2019-04-24 17:10:51 +02:00
7677b1bb48 1.1.30 2019-04-24 16:09:01 +02:00
114379c5b3 fix(core): update 2019-04-24 16:09:01 +02:00
79ae7d2039 1.1.29 2019-04-24 15:47:29 +02:00
d2cb6a3045 fix(core): update 2019-04-24 15:47:28 +02:00
963619b2ed 1.1.28 2019-04-23 17:28:46 +02:00
976abaaed7 fix(core): update 2019-04-23 17:28:46 +02:00
ef322c32ac 1.1.27 2019-01-31 02:32:52 +01:00
475911a968 fix(build): now building with tsbuild 2019-01-31 02:32:52 +01:00
5530e29da4 1.1.26 2019-01-31 00:54:40 +01:00
5f1d2c83c6 fix(core): update 2019-01-31 00:54:39 +01:00
b84dd36885 1.1.25 2019-01-31 00:46:42 +01:00
c2d64ba39a fix(readme): update 2019-01-31 00:46:42 +01:00
85a5d7cdaf 1.1.24 2019-01-30 10:55:52 +01:00
69380a2dbc fix(npmextra): adjust access level 2019-01-30 10:55:51 +01:00
fc292d83b9 1.1.23 2019-01-30 10:55:17 +01:00
b29961df95 fix(scope): switch to @pushrocks 2019-01-30 10:55:16 +01:00
f774a82ad5 1.1.22 2019-01-30 03:21:15 +01:00
5a45f4750b fix(package.json): private setting 2019-01-30 03:21:14 +01:00
fc6227ef96 1.1.21 2019-01-30 03:15:22 +01:00
873aba3e0b fix(snyk): add .snyk file 2019-01-30 03:15:21 +01:00
f9bed9da8a 1.1.20 2019-01-30 03:14:03 +01:00
d296064bfa fix(structure): update to latest standards 2019-01-30 03:14:02 +01:00
1b0922759a 1.1.19 2018-03-19 10:00:14 +01:00
aef4f9c591 now working as expected 2018-03-19 10:00:11 +01:00
abf546eec3 1.1.18 2018-03-18 17:50:39 +01:00
483cb38175 start transitioning to better SocketFunction handling 2018-03-18 17:50:37 +01:00
b2bd66a6a6 1.1.17 2018-03-18 16:40:42 +01:00
cfc3e8788d add @types/node 2018-03-18 16:40:40 +01:00
23c14c032c 1.1.16 2018-03-17 17:52:36 +01:00
c92dae809f format README 2018-03-17 17:52:31 +01:00
7200464e4b 1.1.15 2018-03-17 17:52:07 +01:00
eaa97f0598 update readme 2018-03-17 17:52:01 +01:00
cde8270313 1.1.14 2018-03-17 17:18:59 +01:00
1b073ac8f6 update readme 2018-03-17 17:18:53 +01:00
3f3857df1e 1.1.13 2018-03-15 02:29:44 +01:00
02e17a52c3 update to latest standards 2018-03-15 02:29:40 +01:00
287c2a73c5 1.1.12 2017-10-09 16:46:24 +02:00
dea3746bb0 allow setting of specific server 2017-10-09 10:40:59 +02:00
fc2a55bb0d fix not ending error correctly 2017-10-09 10:28:18 +02:00
99eca912d3 1.1.11 2017-10-09 03:06:14 +02:00
0b6929af8f update to newest version 2017-10-09 03:06:09 +02:00
0aa073f2a7 1.1.10 2017-07-15 23:16:26 +02:00
7170e58457 update smartsocket class to use standard class method definition 2017-07-15 23:16:22 +02:00
cccee35f2c 1.1.9 2017-07-07 22:38:14 +02:00
118ec84ec5 update docs 2017-07-07 22:38:10 +02:00
dd31eea263 update tests 2017-07-07 22:10:03 +02:00
477429143d 1.1.8 2017-07-07 22:03:22 +02:00
ea6fa14133 remove taskbuffer 2017-07-07 22:03:18 +02:00
02065e36a6 update to latest standards 2017-07-07 22:02:19 +02:00
f34358db81 update README 2016-09-26 16:59:06 +02:00
fddf54b2b1 1.1.7 2016-09-26 00:28:45 +02:00
e1dbdc1a15 improve README 2016-09-26 00:28:39 +02:00
f3031a1d4e 1.1.6 2016-09-26 00:23:28 +02:00
47ca497d48 improve README 2016-09-26 00:23:17 +02:00
c7e3c4537b 1.1.5 2016-09-05 00:34:13 +02:00
62b36ab859 fix scoping of socket roles 2016-09-05 00:34:09 +02:00
4563aade16 small syntax fix 2016-09-04 22:20:33 +02:00
47596bfa59 1.1.4 2016-09-03 16:21:00 +02:00
6543d60019 added docs 2016-09-03 16:03:57 +02:00
f0aba45cec 1.1.3 2016-09-03 00:41:52 +02:00
f327fa2784 updated dependencies and exported socketConnection 2016-09-03 00:41:49 +02:00
983b4672fd 1.1.2 2016-08-16 04:48:47 +02:00
c81a41b365 now authenticating sockets by checking the password hash 2016-08-16 04:48:42 +02:00
abe4d22226 1.1.1 2016-08-15 03:47:36 +02:00
87cc238345 update README 2016-08-15 03:47:32 +02:00
475cd12904 1.1.0 2016-08-15 02:36:24 +02:00
84f33fa447 now working in both directions so mesh setups work. 2016-08-15 02:36:17 +02:00
40 changed files with 4387 additions and 1296 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
coverage/ coverage/
docs/ pages/

View File

@ -1,36 +1,125 @@
image: hosttoday/ht-docker-node:npmts # gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages: stages:
- security
- test - test
- release - release
- metadata
testLEGACY: # ====================
stage: test # security stage
# ====================
mirror:
stage: security
script: script:
- npmci test legacy - npmci git mirror
tags: tags:
- docker - docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# test stage
# ====================
testLTS: testLTS:
stage: test stage: test
script: script:
- npmci test lts - npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
testSTABLE: testSTABLE:
stage: test stage: test
script: script:
- npmci test stable - npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- notpriv
release: release:
stage: release stage: release
environment: npmjs-com_registry
script: script:
- npmci publish - npmci node install stable
- npmci npm publish
only: only:
- tags - tags
tags: tags:
- docker - docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g typedoc typescript
- npmci npm prepare
- npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

13
.snyk Normal file
View File

@ -0,0 +1,13 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.13.5
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
ignore:
SNYK-JS-JSYAML-173999:
- '@pushrocks/smartexpress > @pushrocks/smartfile > js-yaml':
reason: None given
expires: '2019-05-24T15:16:11.291Z'
SNYK-JS-JSYAML-174129:
- '@pushrocks/smartexpress > @pushrocks/smartfile > js-yaml':
reason: None given
expires: '2019-05-24T15:16:11.291Z'
patch: {}

118
README.md
View File

@ -1,67 +1,111 @@
# smartsocket # @pushrocks/smartsocket
easy and secure websocket communication, Typescript ready easy and secure websocket communication
## Status ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartsocket)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartsocket)
* [github.com (source mirror)](https://github.com/pushrocks/smartsocket)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartsocket/)
## Status for master
[![build status](https://gitlab.com/pushrocks/smartsocket/badges/master/build.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master) [![build status](https://gitlab.com/pushrocks/smartsocket/badges/master/build.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartsocket/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartsocket/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartsocket.svg)](https://www.npmjs.com/package/@pushrocks/smartsocket)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartsocket/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartsocket)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage ## Usage
We recommend the use of typescript.
Use TypeScript for best in class instellisense.
Under the hood we use socket.io and shortid for managed data exchange. Under the hood we use socket.io and shortid for managed data exchange.
### Serverside ### Serverside
```typescript
import * as smartsocket from "smartsocket";
```typescript
import * as smartsocket from 'smartsocket';
import * as q from q; // q is a promise library
// The "Smartsocket" listens on a port and can receive new "SocketConnection" requests.
let mySmartsocket = new smartsocket.Smartsocket({ let mySmartsocket = new smartsocket.Smartsocket({
port: 3000 // the port smartsocket will listen on port: 3000 // the port smartsocket will listen on
}); });
// optional:
// run this with anothoer existing server like express
declare var someExpressServer; // read the express docs about how express actually works
mySmartsocket.setServer(someExpressServer);
// A "SocketRole" can be referenced by "SocketFunction"s.
// All "SocketRequest"s carry authentication data for a specific "SocketRole".
// "SocketFunction"s know which "SocketRole"s are allowed to execute them
let mySocketRole = new smartsocket.SocketRole({ let mySocketRole = new smartsocket.SocketRole({
name: "someRoleName", name: 'someRoleName',
passwordHash: "someHashedString" passwordHash: 'someHashedString'
}); });
let mySocketFunction = new smartsocket.SocketFunction({ // A "SocketFunction" executes a referenced function and passes in any data of the corresponding "SocketRequest".
name:"newService", // The referenced function must return a promise and resolve with data of type any.
func:(data) => { // Any "SocketRequest" carries a unique identifier. If the referenced function's promise resolved any passed on argument will be returned to the requesting party
let testSocketFunction1 = new smartsocket.SocketFunction({
}, the function to execute funcName: 'testSocketFunction1',
roles:[mySocketRole] // all roles that have access to a specific function funcDef: data => {
console.log('testSocketFunction1 executed successfully!');
},
allowedRoles: [mySocketRole] // all roles that have access to a specific function
}); });
mySmartsocket.registerRole(mySocketRole); // A "Smartsocket" exposes a .clientCall() that gets
mySmartsocket.clientCall.select("client1","restart",data) // 1. the name of the "SocketFunction" on the client side
.then((responseData) => { // 2. the data to pass in
// 3. And a target "SocketConnection" (there can be multiple connections at once)
}); // any unique id association is done internally
mySmartsocket.clientCall('restart', data, someTargetConnection).then(responseData => {});
``` ```
#### Client side #### Client side
```typescript ```typescript
import * as smartsocket from "smartsocket"; import * as smartsocket from 'smartsocket';
let mySmartsocketClient = new smartsocket.SmartsocketClient({ // A "SmartsocketClient" is different from a "Smartsocket" in that it doesn't expose any public address.
url: "somedomain.com", // url, note: will only work over https, no http supported. // Thus any new "SocketConnection"s must be innitiated from a "SmartsocketClient".
port: 3000 let testSmartsocketClient = new smartsocket.SmartsocketClient({
role:"dockerhost", // some role, in this example a dockerhost vm, port: testConfig.port,
password:"somePassword", url: 'http://localhost',
alias:"client1" password: 'testPassword',
alias: 'testClient1',
role: 'testRole1'
}); });
let mySocketFunction2 = new smartsocket.SocketFunction({ // You can .connect() and .disconnect() from a "Smartsocket"
name:"restart", testSmartsocketClient.connect().then(() => {
func:(data) => {}, the function to execute done();
}); });
mySmartsocketClient.registerFunction(mySocketFunction2); // The client can also specify "SocketFunction"s. It can also specify "SocketRole"s in case a client connects to multiple servers at once
let testSocketFunction2 = new smartsocket.SocketFunction({
funcName: 'testSocketFunction2',
funcDef: data => {}, // the function to execute, has to return promise
allowedRoles: []
});
mySmartsocketClient.serverCall("newService",data) // A "SmartsocketClient" can call functions on the serverside using .serverCall() analog to the "Smartsocket"'s .clientCall method.
.then((responseData) => { mySmartsocketClient.serverCall('function', functionCallData).then(functionResponseData => {
// the functionResponseData comes from the server... awesome, right?
});; });
``` ```
> **NOTE:** > **NOTE:**
you can easily chain dependent requests on eiter the server or client side with promises. > you can easily chain dependent requests on either the server or client side with promises.
`data` is always a js object that you can design for your specific needs. > `data` is always a js object that you can design for your specific needs.
It supports buffers for large binary data network exchange. > It supports buffers for large binary data network exchange.
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

4
dist/index.d.ts vendored
View File

@ -1,4 +0,0 @@
export * from "./smartsocket.classes.smartsocket";
export * from "./smartsocket.classes.smartsocketclient";
export * from "./smartsocket.classes.socketfunction";
export * from "./smartsocket.classes.socketrole";

10
dist/index.js vendored
View File

@ -1,10 +0,0 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./smartsocket.classes.smartsocket"));
__export(require("./smartsocket.classes.smartsocketclient"));
__export(require("./smartsocket.classes.socketfunction"));
__export(require("./smartsocket.classes.socketrole"));
// need something more exposed? Create an issue on GitLab!
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsaUJBQWMsbUNBQW1DLENBQUMsRUFBQTtBQUNsRCxpQkFBYyx5Q0FBeUMsQ0FBQyxFQUFBO0FBQ3hELGlCQUFjLHNDQUFzQyxDQUFDLEVBQUE7QUFDckQsaUJBQWMsa0NBQWtDLENBQUMsRUFBQTtBQUVqRCwwREFBMEQifQ==

View File

@ -1,22 +0,0 @@
/// <reference types="socket.io" />
import { Objectmap } from "lik";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
export interface ISmartsocketConstructorOptions {
port: number;
}
export declare class Smartsocket {
options: ISmartsocketConstructorOptions;
io: SocketIO.Server;
openSockets: Objectmap<SocketConnection>;
constructor(optionsArg: ISmartsocketConstructorOptions);
/**
* the standard handler for new socket connections
*/
private _handleSocketConnection(socketArg);
/**
* starts listening to incling sockets:
*/
startServer: () => void;
closeServer: () => void;
clientCall(): void;
}

View File

@ -1,57 +0,0 @@
"use strict";
const plugins = require("./smartsocket.plugins");
// classes
const lik_1 = require("lik");
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
;
class Smartsocket {
constructor(optionsArg) {
this.openSockets = new lik_1.Objectmap();
/**
* starts listening to incling sockets:
*/
this.startServer = () => {
this.io = plugins.socketIo(this.options.port);
this.io.on("connection", (socketArg) => {
this._handleSocketConnection(socketArg);
});
};
this.closeServer = () => {
this.openSockets.forEach((socketObjectArg) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
this.io.close();
};
this.options = optionsArg;
}
;
/**
* the standard handler for new socket connections
*/
_handleSocketConnection(socketArg) {
let socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
alias: undefined,
authenticated: false,
role: undefined,
socket: socketArg
});
plugins.beautylog.log("Socket connected. Trying to authenticate...");
this.openSockets.add(socketConnection);
socketConnection.authenticate()
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch((err) => {
console.log(err);
});
}
;
// communication
clientCall() {
// TODO: target specific client and initiate response
}
}
exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFHakQsVUFBVTtBQUNWLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQUdoQyx1REFBaUMsd0NBQXdDLENBQUMsQ0FBQTtBQUt6RSxDQUFDO0FBRUY7SUFJSSxZQUFZLFVBQTBDO1FBRHRELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQW9CLENBQUM7UUEwQmhEOztXQUVHO1FBRUgsZ0JBQVcsR0FBRztZQUNWLElBQUksQ0FBQyxFQUFFLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzlDLElBQUksQ0FBQyxFQUFFLENBQUMsRUFBRSxDQUFDLFlBQVksRUFBRSxDQUFDLFNBQVM7Z0JBQy9CLElBQUksQ0FBQyx1QkFBdUIsQ0FBQyxTQUFTLENBQUMsQ0FBQztZQUM1QyxDQUFDLENBQUMsQ0FBQztRQUNQLENBQUMsQ0FBQTtRQUNELGdCQUFXLEdBQUc7WUFDVixJQUFJLENBQUMsV0FBVyxDQUFDLE9BQU8sQ0FBQyxDQUFDLGVBQWlDO2dCQUN2RCxPQUFPLENBQUMsU0FBUyxDQUFDLE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Q0wsQ0FBQztBQXJEWSxtQkFBVyxjQXFEdkIsQ0FBQSJ9

View File

@ -1,28 +0,0 @@
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
port: number;
url: string;
alias: string;
role: string;
password: string;
}
export declare class SmartsocketClient {
alias: string;
role: string;
socketConnection: SocketConnection;
serverUrl: string;
serverPort: number;
serverPassword: string;
constructor(optionsArg: ISmartsocketClientOptions);
/**
* connect the client to the server
*/
connect(): plugins.q.Promise<{}>;
disconnect(): plugins.q.Promise<{}>;
serverCall(functionNameArg: string, dataArg: any): plugins.q.Promise<{}>;
}

View File

@ -1,73 +0,0 @@
"use strict";
const plugins = require("./smartsocket.plugins");
// import classes
const smartsocket_classes_socketconnection_1 = require("./smartsocket.classes.socketconnection");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
class SmartsocketClient {
constructor(optionsArg) {
this.alias = optionsArg.alias;
this.role = optionsArg.role;
this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password;
}
;
/**
* connect the client to the server
*/
connect() {
let done = plugins.q.defer();
plugins.beautylog.log("trying to connect...");
let socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new smartsocket_classes_socketconnection_1.SocketConnection({
alias: this.alias,
authenticated: false,
role: undefined,
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
});
this.socketConnection.socket.on("requestAuth", () => {
console.log("server requested authentication");
this.socketConnection.socket.emit("dataAuth", {
role: this.role,
password: this.serverPassword,
alias: this.alias
});
this.socketConnection.socket.on("authenticated", () => {
console.log("client is authenticated");
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve();
});
});
return done.promise;
}
;
disconnect() {
let done = plugins.q.defer();
this.socketConnection.socket.disconnect();
this.socketConnection = undefined;
plugins.beautylog.ok("disconnected!");
done.resolve();
return done.promise;
}
serverCall(functionNameArg, dataArg) {
let done = plugins.q.defer();
let socketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
side: "requesting",
originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(),
funcCallData: {
funcName: functionNameArg,
funcDataArg: dataArg
}
});
socketRequest.dispatch()
.then((dataArg) => {
done.resolve(dataArg.funcDataArg);
});
return done.promise;
}
;
}
exports.SmartsocketClient = SmartsocketClient;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUl6QixDQUFDLENBSitDO0FBT2hELGlCQUFpQjtBQUNqQix1REFBaUMsd0NBQXdDLENBQUMsQ0FBQTtBQUUxRSxvREFBOEIscUNBQXFDLENBQUMsQ0FBQTtBQVlwRTtJQU9JLFlBQVksVUFBb0M7UUFDNUMsSUFBSSxDQUFDLEtBQUssR0FBRyxVQUFVLENBQUMsS0FBSyxDQUFDO1FBQzlCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsU0FBUyxHQUFHLFVBQVUsQ0FBQyxHQUFHLENBQUE7UUFDL0IsSUFBSSxDQUFDLFVBQVUsR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQ2xDLElBQUksQ0FBQyxjQUFjLEdBQUcsVUFBVSxDQUFDLFFBQVEsQ0FBQTtJQUM3QyxDQUFDOztJQUVEOztPQUVHO0lBQ0gsT0FBTztRQUNILElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FB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QUMsT0FBMkI7WUFDOUIsSUFBSSxDQUFDLE9BQU8sQ0FBQyxPQUFPLENBQUMsV0FBVyxDQUFDLENBQUM7UUFDdEMsQ0FBQyxDQUFDLENBQUM7UUFDUCxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN4QixDQUFDOztBQUVMLENBQUM7QUF0RVkseUJBQWlCLG9CQXNFN0IsQ0FBQSJ9

View File

@ -1,40 +0,0 @@
/// <reference types="socket.io" />
/// <reference types="socket.io-client" />
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { SocketRole } from "./smartsocket.classes.socketrole";
/**
* interface for constructor of class SocketConnection
*/
export interface ISocketConnectionConstructorOptions {
alias: string;
authenticated: boolean;
role: SocketRole;
socket: SocketIO.Socket | SocketIOClient.Socket;
}
/**
* interface for authentication data
*/
export interface ISocketConnectionAuthenticationObject {
role: "coreflowContainer";
password: "somePassword";
alias: "coreflow1";
}
/**
* class SocketConnection represents a websocket connection
*/
export declare class SocketConnection {
alias: string;
authenticated: boolean;
role: SocketRole;
socket: SocketIO.Socket | SocketIOClient.Socket;
constructor(optionsArg: ISocketConnectionConstructorOptions);
/**
* authenticate the socket
*/
authenticate(): plugins.q.Promise<{}>;
/**
* listen to function requests
*/
listenToFunctionRequests(): plugins.q.Promise<{}>;
}

View File

@ -1,99 +0,0 @@
"use strict";
const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
;
// export classes
/**
* class SocketConnection represents a websocket connection
*/
class SocketConnection {
constructor(optionsArg) {
this.authenticated = false;
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
// standard behaviour that is always true
this.socket.on("disconnect", () => {
plugins.beautylog.info(`Client ${this.alias} disconnected`);
});
}
;
// authenticating --------------------------
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
if ((true)) {
this.alias = dataArg.alias;
this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role);
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
}
else {
this.authenticated = false;
this.socket.disconnect();
done.reject("not authenticated");
}
;
});
this.socket.emit("requestAuth");
return done.promise;
}
;
// listening -------------------------------
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.q.defer();
if (this.authenticated) {
this.socket.on("function", (dataArg) => {
// check if requested function is available to the socket's scope
plugins.beautylog.log("function request received");
let referencedFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.funcCallData.funcName;
});
if (referencedFunction !== undefined) {
plugins.beautylog.ok("function in access scope");
let localSocketRequest = new smartsocket_classes_socketrequest_1.SocketRequest({
side: "responding",
originSocketConnection: this,
shortId: dataArg.shortId,
funcCallData: dataArg.funcCallData
});
localSocketRequest.createResponse(); // takes care of creating response and sending it back
}
else {
plugins.beautylog.warn("function not existent or out of access scope");
}
;
});
this.socket.on("functionResponse", (dataArg) => {
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`);
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg);
});
plugins.beautylog.log(`now listening to function requests for ${this.alias}`);
done.resolve(this);
}
else {
let errMessage;
plugins.beautylog.error(errMessage);
done.reject(errMessage);
}
;
return done.promise;
}
;
}
exports.SocketConnection = SocketConnection;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW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BOEMsQ0FBQyxDQUFDO2dCQUMzRSxDQUFDO2dCQUFBLENBQUM7WUFDTixDQUFDLENBQUMsQ0FBQztZQUNILElBQUksQ0FBQyxNQUFNLENBQUMsRUFBRSxDQUFDLGtCQUFrQixFQUFFLENBQUMsT0FBZ0M7Z0JBQ2hFLE9BQU8sQ0FBQyxTQUFTLENBQUMsSUFBSSxDQUFDLHlDQUF5QyxPQUFPLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FBQztnQkFDbkYsSUFBSSxtQkFBbUIsR0FBRyxPQUFPLENBQUMsb0JBQW9CLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFDO2dCQUN4RSxtQkFBbUIsQ0FBQyxjQUFjLENBQUMsT0FBTyxDQUFDLENBQUM7WUFDaEQsQ0FBQyxDQUFDLENBQUM7WUFDSCxPQUFPLENBQUMsU0FBUyxDQUFDLEdBQUcsQ0FBQywwQ0FBMEMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUM7WUFDOUUsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUN2QixDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDSixJQUFJLFVBQW9ELENBQUM7WUFDekQsT0FBTyxDQUFDLFNBQVMsQ0FBQyxLQUFLLENBQUMsVUFBVSxDQUFDLENBQUM7WUFDcEMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxVQUFVLENBQUMsQ0FBQztRQUM1QixDQUFDO1FBQUEsQ0FBQztRQUNGLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3hCLENBQUM7O0FBSUwsQ0FBQztBQXhGWSx3QkFBZ0IsbUJBd0Y1QixDQUFBO0FBQUEsQ0FBQyJ9

View File

@ -1,46 +0,0 @@
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { Objectmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole";
/**
* interface of the contructor options of class SocketFunction
*/
export interface ISocketFunctionConstructorOptions {
funcName: string;
funcDef: any;
allowedRoles: SocketRole[];
}
/**
* 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;
}
/**
* interface for function definition of SocketFunction
*/
export interface IFuncDef {
(dataArg: any): PromiseLike<any>;
}
export declare let allSocketFunctions: Objectmap<SocketFunction>;
/**
* class that respresents a function that can be transparently called using a SocketConnection
*/
export declare class SocketFunction {
name: string;
funcDef: IFuncDef;
roles: SocketRole[];
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: ISocketFunctionConstructorOptions);
/**
* notifies a role about access to this SocketFunction
*/
private _notifyRole(socketRoleArg);
/**
* invokes the function of this SocketFunction
*/
invoke(dataArg: ISocketFunctionCall): plugins.q.Promise<any>;
}

View File

@ -1,57 +0,0 @@
"use strict";
const plugins = require("./smartsocket.plugins");
// import classes
const lik_1 = require("lik");
;
;
// export objects
exports.allSocketFunctions = new lik_1.Objectmap();
// export classes
/**
* class that respresents a function that can be transparently called using a SocketConnection
*/
class SocketFunction {
/**
* the constructor for SocketFunction
*/
constructor(optionsArg) {
this.name = optionsArg.funcName;
this.funcDef = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles) {
this._notifyRole(socketRoleArg);
}
;
exports.allSocketFunctions.add(this); // map instance with Objectmap
}
;
/**
* notifies a role about access to this SocketFunction
*/
_notifyRole(socketRoleArg) {
socketRoleArg.addSocketFunction(this);
}
/**
* invokes the function of this SocketFunction
*/
invoke(dataArg) {
let done = plugins.q.defer();
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg)
.then((resultData) => {
let funcResponseData = {
funcName: this.name,
funcDataArg: resultData
};
done.resolve(funcResponseData);
});
}
else {
throw new Error("SocketFunction.name does not match the data argument's .name!");
}
return done.promise;
}
;
}
exports.SocketFunction = SocketFunction;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIj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LE9BQTJCO1FBQzlCLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7UUFDN0IsRUFBRSxDQUFBLENBQUMsT0FBTyxDQUFDLFFBQVEsS0FBSyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUEsQ0FBQztZQUMvQixJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxXQUFXLENBQUM7aUJBQzVCLElBQUksQ0FBQyxDQUFDLFVBQWM7Z0JBQ2pCLElBQUksZ0JBQWdCLEdBQXVCO29CQUN2QyxRQUFRLEVBQUMsSUFBSSxDQUFDLElBQUk7b0JBQ2xCLFdBQVcsRUFBQyxVQUFVO2lCQUN6QixDQUFBO2dCQUNELElBQUksQ0FBQyxPQUFPLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztZQUNuQyxDQUFDLENBQUMsQ0FBQztRQUVYLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLE1BQU0sSUFBSSxLQUFLLENBQUMsK0RBQStELENBQUMsQ0FBQztRQUNyRixDQUFDO1FBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUM7SUFDeEIsQ0FBQzs7QUFFTCxDQUFDO0FBOUNZLHNCQUFjLGlCQThDMUIsQ0FBQSJ9

View File

@ -1,46 +0,0 @@
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { Objectmap } from "lik";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
export declare type TSocketRequestStatus = "new" | "pending" | "finished";
export declare type TSocketRequestSide = "requesting" | "responding";
/**
* interface of constructor of class SocketRequest
*/
export interface SocketRequestConstructorOptions {
side: TSocketRequestSide;
originSocketConnection: SocketConnection;
shortId: string;
funcCallData?: ISocketFunctionCall;
}
/**
* request object that is sent initially and may or may not receive a response
*/
export interface ISocketRequestDataObject {
funcCallData: ISocketFunctionCall;
shortId: string;
responseTimeout?: number;
}
export declare let allSocketRequests: Objectmap<SocketRequest>;
export declare class SocketRequest {
status: TSocketRequestStatus;
side: TSocketRequestSide;
shortid: string;
originSocketConnection: SocketConnection;
funcCallData: ISocketFunctionCall;
done: plugins.q.Deferred<{}>;
constructor(optionsArg: SocketRequestConstructorOptions);
/**
* dispatches a socketrequest from the requesting to the receiving side
*/
dispatch(): plugins.q.Promise<{}>;
/**
* handles the response that is received by the requesting side
*/
handleResponse(responseDataArg: ISocketRequestDataObject): void;
/**
* creates the response on the responding side
*/
createResponse(): void;
}

View File

@ -1,64 +0,0 @@
"use strict";
const plugins = require("./smartsocket.plugins");
const helpers = require("./smartsocket.helpers");
// import classes
const lik_1 = require("lik");
;
;
//export objects
exports.allSocketRequests = new lik_1.Objectmap();
// export classes
class SocketRequest {
constructor(optionsArg) {
this.status = "new";
this.done = plugins.q.defer();
this.side = optionsArg.side;
this.shortid = optionsArg.shortId;
this.funcCallData = optionsArg.funcCallData;
this.originSocketConnection = optionsArg.originSocketConnection;
exports.allSocketRequests.add(this);
}
;
// requesting --------------------------
/**
* dispatches a socketrequest from the requesting to the receiving side
*/
dispatch() {
let requestData = {
funcCallData: this.funcCallData,
shortId: this.shortid
};
this.originSocketConnection.socket.emit("function", requestData);
return this.done.promise;
}
;
/**
* handles the response that is received by the requesting side
*/
handleResponse(responseDataArg) {
plugins.beautylog.log("handling response!");
this.done.resolve(responseDataArg.funcCallData);
exports.allSocketRequests.remove(this);
}
// responding --------------------------
/**
* creates the response on the responding side
*/
createResponse() {
let targetSocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName);
plugins.beautylog.info(`invoking ${targetSocketFunction.name}`);
targetSocketFunction.invoke(this.funcCallData)
.then((resultData) => {
plugins.beautylog.log("got resultData. Sending it to requesting party.");
let requestData = {
funcCallData: resultData,
shortId: this.shortid
};
this.originSocketConnection.socket.emit("functionResponse", requestData);
exports.allSocketRequests.remove(this);
});
}
}
exports.SocketRequest = SocketRequest;
;
//# sour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Q0YsSUFBSSxDQUFDLHNCQUFzQixDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsa0JBQWtCLEVBQUMsV0FBVyxDQUFDLENBQUM7WUFDeEUseUJBQWlCLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ25DLENBQUMsQ0FBQyxDQUFDO0lBQ1gsQ0FBQztBQUNMLENBQUM7QUF6RFkscUJBQWEsZ0JBeUR6QixDQUFBO0FBQUEsQ0FBQyJ9

View File

@ -1,20 +0,0 @@
import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
export declare let allSocketRoles: Objectmap<SocketRole>;
/**
* interface for class SocketRole
*/
export interface SocketRoleOptions {
name: string;
passwordHash: string;
}
/**
* A socketrole defines access to certain routines.
*/
export declare class SocketRole {
name: string;
passwordHash: string;
allowedFunctions: Objectmap<SocketFunction>;
constructor(optionsArg: SocketRoleOptions);
addSocketFunction(socketFunctionArg: SocketFunction): void;
}

View File

@ -1,21 +0,0 @@
"use strict";
// import classes
const lik_1 = require("lik");
exports.allSocketRoles = new lik_1.Objectmap();
/**
* A socketrole defines access to certain routines.
*/
class SocketRole {
constructor(optionsArg) {
this.allowedFunctions = new lik_1.Objectmap();
this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash;
exports.allSocketRoles.add(this);
}
;
addSocketFunction(socketFunctionArg) {
this.allowedFunctions.add(socketFunctionArg);
}
}
exports.SocketRole = SocketRole;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFFQSxpQkFBaUI7QUFDakIsc0JBQTBCLEtBQUssQ0FBQyxDQUFBO0FBSXJCLHNCQUFjLEdBQUcsSUFBSSxlQUFTLEVBQWMsQ0FBQztBQVd4RDs7R0FFRztBQUNIO0lBSUksWUFBWSxVQUE2QjtRQUR6QyxxQkFBZ0IsR0FBRyxJQUFJLGVBQVMsRUFBa0IsQ0FBQztRQUUvQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO1FBQzVDLHNCQUFjLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQzdCLENBQUM7O0lBQ0QsaUJBQWlCLENBQUMsaUJBQWdDO1FBQzlDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxHQUFHLENBQUMsaUJBQWlCLENBQUMsQ0FBQztJQUNqRCxDQUFDO0FBQ0wsQ0FBQztBQVpZLGtCQUFVLGFBWXRCLENBQUEifQ==

View File

@ -1,12 +0,0 @@
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest, TSocketRequestSide } from "./smartsocket.classes.socketrequest";
import { SocketRole } from "./smartsocket.classes.socketrole";
export declare let getSocketFunctionByName: (functionNameArg: string) => SocketFunction;
/**
* get corresponding Socketrequest instance by shortId
*/
export declare let getSocketRequestById: (shortIdArg: string, requestSide?: TSocketRequestSide) => SocketRequest;
/**
* get corresponding SocketRole instance by name
*/
export declare let getSocketRoleByName: (socketRoleNameArg: string) => SocketRole;

View File

@ -1,23 +0,0 @@
"use strict";
const smartsocket_classes_socketfunction_1 = require("./smartsocket.classes.socketfunction");
const smartsocket_classes_socketrequest_1 = require("./smartsocket.classes.socketrequest");
const smartsocket_classes_socketrole_1 = require("./smartsocket.classes.socketrole");
// SocketFunction helpers
exports.getSocketFunctionByName = (functionNameArg) => {
return smartsocket_classes_socketfunction_1.allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg; });
};
// SocketRequest helpers
/**
* get corresponding Socketrequest instance by shortId
*/
exports.getSocketRequestById = (shortIdArg, requestSide) => {
return smartsocket_classes_socketrequest_1.allSocketRequests.find((socketRequestArg) => { return socketRequestArg.shortid === shortIdArg; });
};
// SocketRole helpers
/**
* get corresponding SocketRole instance by name
*/
exports.getSocketRoleByName = (socketRoleNameArg) => {
return smartsocket_classes_socketrole_1.allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg; });
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUlBLHFEQUFtRCxzQ0FBc0MsQ0FBQyxDQUFBO0FBRTFGLG9EQUFxRSxxQ0FBcUMsQ0FBQyxDQUFBO0FBQzNHLGlEQUEyQyxrQ0FBa0MsQ0FBQyxDQUFBO0FBRTlFLHlCQUF5QjtBQUNkLCtCQUF1QixHQUFHLENBQUMsZUFBdUI7SUFDekQsTUFBTSxDQUFDLHVEQUFrQixDQUFDLElBQUksQ0FBQyxDQUFDLGlCQUFpQixPQUFPLE1BQU0sQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLEtBQUssZUFBZSxDQUFBLENBQUMsQ0FBQyxDQUFDLENBQUM7QUFDakgsQ0FBQyxDQUFBO0FBRUQsd0JBQXdCO0FBRXhCOztHQUVHO0FBQ1EsNEJBQW9CLEdBQUcsQ0FBQyxVQUFpQixFQUFDLFdBQStCO0lBQ2hGLE1BQU0sQ0FBQyxxREFBaUIsQ0FBQyxJQUFJLENBQUMsQ0FBQyxnQkFBZ0IsT0FBTSxNQUFNLENBQUMsZ0JBQWdCLENBQUMsT0FBTyxLQUFLLFVBQVUsQ0FBQSxDQUFBLENBQUMsQ0FBQyxDQUFBO0FBQ3pHLENBQUMsQ0FBQTtBQUVELHFCQUFxQjtBQUVyQjs7R0FFRztBQUNRLDJCQUFtQixHQUFHLENBQUMsaUJBQXlCO0lBQ3ZELE1BQU0sQ0FBQywrQ0FBYyxDQUFDLElBQUksQ0FBQyxDQUFDLGFBQWEsT0FBTyxNQUFNLENBQUMsYUFBYSxDQUFDLElBQUksS0FBSyxpQkFBaUIsQ0FBQSxDQUFDLENBQUMsQ0FBQyxDQUFBO0FBQ3RHLENBQUMsQ0FBQyJ9

View File

@ -1,8 +0,0 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import shortid = require("shortid");
export import socketIo = require("socket.io");
export import socketIoClient = require("socket.io-client");
export import taskbuffer = require("taskbuffer");

View File

@ -1,10 +0,0 @@
"use strict";
require("typings-global");
exports.beautylog = require("beautylog");
exports.lik = require("lik");
exports.q = require("q");
exports.shortid = require("shortid");
exports.socketIo = require("socket.io");
exports.socketIoClient = require("socket.io-client");
exports.taskbuffer = require("taskbuffer");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsV0FBRyxXQUFZLEtBQUssQ0FBQyxDQUFDO0FBQ3RCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNqQixlQUFPLFdBQVcsU0FBUyxDQUFDLENBQUM7QUFDN0IsZ0JBQVEsV0FBVyxXQUFXLENBQUMsQ0FBQztBQUNoQyxzQkFBYyxXQUFXLGtCQUFrQixDQUFDLENBQUM7QUFDN0Msa0JBQVUsV0FBVyxZQUFZLENBQUMsQ0FBQyJ9

View File

@ -1,5 +1,17 @@
{ {
"npmts":{ "gitzone": {
"coverageTreshold":50 "module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartsocket",
"shortDescription": "easy and secure websocket communication",
"npmPackagename": "@pushrocks/smartsocket",
"license": "MIT",
"projectDomain": "push.rocks"
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
} }
} }

3249
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,12 @@
{ {
"name": "smartsocket", "name": "@pushrocks/smartsocket",
"version": "1.0.7", "version": "1.1.37",
"description": "easy and secure websocket communication, Typescript ready", "description": "easy and secure websocket communication",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"scripts": { "scripts": {
"test": "(npmts)" "test": "tstest test/",
"build": "tsbuild"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -18,21 +19,27 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartsocket#README", "homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": { "dependencies": {
"@types/q": "0.x.x", "@pushrocks/lik": "^3.0.5",
"@types/shortid": "0.0.27", "@pushrocks/smartdelay": "^2.0.3",
"@types/socket.io": "^1.4.26", "@pushrocks/smartexpress": "^3.0.21",
"@types/socket.io-client": "^1.4.26", "@pushrocks/smarthash": "^2.0.4",
"beautylog": "^5.0.20", "@pushrocks/smartlog": "^2.0.19",
"lik": "^1.0.15", "@pushrocks/smartpromise": "^3.0.2",
"q": "^1.4.1", "@types/shortid": "0.0.29",
"shortid": "^2.2.6", "@types/socket.io": "^2.1.2",
"socket.io": "^1.4.8", "@types/socket.io-client": "^1.4.32",
"socket.io-client": "^1.4.8", "shortid": "^2.2.14",
"taskbuffer": "^1.0.7" "socket.io": "^2.2.0",
"socket.io-client": "^2.2.0"
}, },
"devDependencies": { "devDependencies": {
"npmts-g": "^5.2.8", "@gitzone/tsbuild": "^2.1.11",
"should": "^11.0.0", "@gitzone/tsrun": "^1.2.6",
"typings-test": "^1.0.1" "@gitzone/tstest": "^1.0.24",
} "@pushrocks/tapbundle": "^3.0.9",
"@types/node": "^12.0.6",
"tslint": "^5.17.0",
"tslint-config-prettier": "^1.18.0"
},
"private": false
} }

2
test/test.d.ts vendored
View File

@ -1,2 +0,0 @@
import "typings-test";
import "should";

139
test/test.expressserver.ts Normal file
View File

@ -0,0 +1,139 @@
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@pushrocks/tapbundle';
import * as smarthash from '@pushrocks/smarthash';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartexpress from '@pushrocks/smartexpress';
import socketIoClient = require('socket.io-client');
import smartsocket = require('../ts/index');
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketRole1: smartsocket.SocketRole;
let testSocketFunction1: smartsocket.SocketFunction;
let myseServer: smartexpress.Server;
const testConfig = {
port: 3000
};
// class smartsocket
tap.test('should create a new smartsocket', async () => {
testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port });
expect(testSmartsocket).be.instanceOf(smartsocket.Smartsocket);
});
tap.test('Should accept an smartExpressServer as server', async () => {
myseServer = new smartexpress.Server({
cors: true,
forceSsl: false,
port: testConfig.port
});
testSmartsocket.setExternalServer('smartexpress', myseServer);
await myseServer.start();
});
tap.test('should start listening when .started is called', async () => {
await testSmartsocket.start();
});
// class socketrole
tap.test('should add a socketrole', async () => {
testSocketRole1 = new smartsocket.SocketRole({
name: 'testRole1',
passwordHash: smarthash.sha256FromStringSync('testPassword')
});
testSmartsocket.addSocketRoles([testSocketRole1]);
});
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({
allowedRoles: [testSocketRole1],
funcDef: async dataArg => {
return dataArg;
},
funcName: 'testFunction1'
});
});
// class SmartsocketClient
tap.test('should react to a new websocket connection from client', async () => {
const done = smartpromise.defer();
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: 'http://localhost',
password: 'testPassword',
alias: 'testClient1',
role: 'testRole1'
});
testSmartsocketClient.connect().then(() => {
done.resolve();
});
await done.promise;
});
tap.test('client should disconnect and reconnect', async () => {
let done = smartpromise.defer();
testSmartsocketClient
.disconnect()
.then(() => {
let done = smartpromise.defer();
setTimeout(() => {
testSmartsocketClient.connect().then(done.resolve);
}, 0);
return done.promise;
})
.then(() => {
done.resolve();
});
await done.promise;
});
tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test
});
tap.test('should be able to make a functionCall from client to server', async () => {
let done = smartpromise.defer();
testSmartsocketClient
.serverCall('testFunction1', {
value1: 'hello'
})
.then(dataArg => {
console.log(dataArg);
done.resolve();
});
await done.promise;
});
tap.test('should be able to make a functionCall from server to client', async () => {
let done = smartpromise.defer();
let targetSocket = (() => {
return smartsocket.allSocketConnections.find(socketConnectionArg => {
return socketConnectionArg.alias === 'testClient1';
});
})();
testSmartsocket
.clientCall(
'testFunction1',
{
value1: 'helloFromServer'
},
targetSocket
)
.then(dataArg => {
console.log(dataArg);
done.resolve();
});
});
// terminate
tap.test('should close the server', async () => {
await testSmartsocket.stop();
await myseServer.stop();
});
tap.start();

View File

@ -1,90 +0,0 @@
"use strict";
require("typings-test");
require("should");
const smartsocket = require("../dist/index");
const q = require("q");
let testSmartsocket;
let testSmartsocketClient;
let testSocketRole1;
let testSocketFunction1;
let testConfig = {
port: 3000
};
describe("smartsocket", function () {
describe("class Smartsocket", function () {
it("should create a new smartsocket", function () {
testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port });
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
});
it("should start listening when .started is called", function () {
testSmartsocket.startServer();
});
});
describe("class SocketRole", function () {
testSocketRole1 = new smartsocket.SocketRole({
name: "testRole1",
passwordHash: "somehash"
});
});
describe("class SocketFunction", function () {
it("should register a new Function", function () {
testSocketFunction1 = new smartsocket.SocketFunction({
funcName: "testFunction1",
funcDef: (dataArg) => {
let done = q.defer();
done.resolve(dataArg);
return done.promise;
},
allowedRoles: [testSocketRole1]
});
});
});
describe("class SmartsocketClient", function () {
it("should react to a new websocket connection from client", function (done) {
this.timeout(10000);
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: "http://localhost",
password: "testPassword",
alias: "testClient1",
role: "testRole1"
});
testSmartsocketClient.connect()
.then(() => {
done();
});
});
it("client should disconnect and reconnect", function (done) {
this.timeout(10000);
testSmartsocketClient.disconnect()
.then(() => {
let done = q.defer();
setTimeout(() => {
testSmartsocketClient.connect()
.then(done.resolve);
}, 0);
return done.promise;
})
.then(() => {
done();
});
});
it("2 clients should connect in parallel", function () {
});
it("should be able to make a functionCall", function (done) {
this.timeout(5000);
testSmartsocketClient.serverCall("testFunction1", {
value1: "hello"
}).then((dataArg) => {
console.log(dataArg);
done();
});
});
});
describe("terminating smartsocket", function () {
it("should close the server", function () {
testSmartsocket.closeServer();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQixNQUFPLFdBQVcsV0FBVyxlQUFlLENBQUMsQ0FBQztBQUM5QyxNQUFPLENBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUV4QixJQUFJLGVBQXdDLENBQUM7QUFDN0MsSUFBSSxxQkFBb0QsQ0FBQztBQUN6RCxJQUFJLGVBQXVDLENBQUM7QUFDNUMsSUFBSSxtQkFBOEMsQ0FBQztBQUVuRCxJQUFJLFVBQVUsR0FBRztJQUNiLElBQUksRUFBRSxJQUFJO0NBQ2IsQ0FBQTtBQUVELFFBQVEsQ0FBQyxhQUFhLEVBQUU7SUFDcEIsU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UN4QixDQUFDLENBQUM7aUJBQ0QsSUFBSSxDQUFDO2dCQUNGLElBQUksRUFBRSxDQUFDO1lBQ1gsQ0FBQyxDQUFDLENBQUM7UUFDWCxDQUFDLENBQUMsQ0FBQztRQUNILEVBQUUsQ0FBQyxzQ0FBc0MsRUFBRTtRQUUzQyxDQUFDLENBQUMsQ0FBQztRQUNILEVBQUUsQ0FBQyx1Q0FBdUMsRUFBQyxVQUFTLElBQUk7WUFDcEQsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztZQUNuQixxQkFBcUIsQ0FBQyxVQUFVLENBQUMsZUFBZSxFQUFDO2dCQUM3QyxNQUFNLEVBQUMsT0FBTzthQUNqQixDQUFDLENBQUMsSUFBSSxDQUFDLENBQUMsT0FBTztnQkFDWixPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFDO2dCQUNyQixJQUFJLEVBQUUsQ0FBQztZQUNYLENBQUMsQ0FBQyxDQUFDO1FBQ1AsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQztJQUNILFFBQVEsQ0FBQyx5QkFBeUIsRUFBRTtRQUNoQyxFQUFFLENBQUMseUJBQXlCLEVBQUU7WUFDMUIsZUFBZSxDQUFDLFdBQVcsRUFBRSxDQUFDO1FBQ2xDLENBQUMsQ0FBQyxDQUFDO0lBQ1AsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQyJ9

View File

@ -1,93 +1,124 @@
import "typings-test"; // tslint:disable-next-line:no-implicit-dependencies
import "should"; import { expect, tap } from '@pushrocks/tapbundle';
import socketIoClient = require("socket.io-client");
import smartsocket = require("../dist/index"); import * as smarthash from '@pushrocks/smarthash';
import q = require("q"); import * as smartpromise from '@pushrocks/smartpromise';
import socketIoClient = require('socket.io-client');
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;
let testConfig = { const testConfig = {
port: 3000 port: 3000
} };
describe("smartsocket", function () { // class smartsocket
describe("class Smartsocket", function () { tap.test('should create a new smartsocket', async () => {
it("should create a new smartsocket", function () {
testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port }); testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port });
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket); expect(testSmartsocket).be.instanceOf(smartsocket.Smartsocket);
}); });
it("should start listening when .started is called", function () {
testSmartsocket.startServer(); tap.test('should start listening when .started is called', async () => {
await testSmartsocket.start();
}); });
});
describe("class SocketRole", function(){ // class socketrole
tap.test('should add a socketrole', async () => {
testSocketRole1 = new smartsocket.SocketRole({ testSocketRole1 = new smartsocket.SocketRole({
name:"testRole1", name: 'testRole1',
passwordHash:"somehash" passwordHash: smarthash.sha256FromStringSync('testPassword')
}) });
}) testSmartsocket.addSocketRoles([testSocketRole1]);
describe("class SocketFunction", function () { });
it("should register a new Function", function () {
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunction1 = new smartsocket.SocketFunction({ testSocketFunction1 = new smartsocket.SocketFunction({
funcName:"testFunction1", allowedRoles: [testSocketRole1],
funcDef: (dataArg) => { funcDef: async (dataArg, socketConnectionArg) => {
let done = q.defer(); return dataArg;
done.resolve(dataArg);
return done.promise;
}, },
allowedRoles:[testSocketRole1] funcName: 'testFunction1'
}); });
}); });
});
describe("class SmartsocketClient", function () { // class SmartsocketClient
it("should react to a new websocket connection from client", function (done) { tap.test('should react to a new websocket connection from client', async () => {
this.timeout(10000); const done = smartpromise.defer();
testSmartsocketClient = new smartsocket.SmartsocketClient({ testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port, port: testConfig.port,
url: "http://localhost", url: 'http://localhost',
password: "testPassword", password: 'testPassword',
alias: "testClient1", alias: 'testClient1',
role: "testRole1" role: 'testRole1'
}); });
testSmartsocketClient.connect() testSmartsocketClient.connect().then(() => {
done.resolve();
});
await done.promise;
});
tap.test('client should disconnect and reconnect', async () => {
let done = smartpromise.defer();
testSmartsocketClient
.disconnect()
.then(() => { .then(() => {
done(); let done = smartpromise.defer();
});
});
it("client should disconnect and reconnect", function (done) {
this.timeout(10000);
testSmartsocketClient.disconnect()
.then(() => {
let done = q.defer();
setTimeout(() => { setTimeout(() => {
testSmartsocketClient.connect() testSmartsocketClient.connect().then(done.resolve);
.then(done.resolve) }, 0);
}, 0)
return done.promise; return done.promise;
}) })
.then(() => { .then(() => {
done(); done.resolve();
}); });
await done.promise;
}); });
it("2 clients should connect in parallel", function () {
tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test
}); });
it("should be able to make a functionCall",function(done){
this.timeout(5000); tap.test('should be able to make a functionCall from client to server', async () => {
testSmartsocketClient.serverCall("testFunction1",{ let done = smartpromise.defer();
value1:"hello" testSmartsocketClient
}).then((dataArg) => { .serverCall('testFunction1', {
value1: 'hello'
})
.then(dataArg => {
console.log(dataArg); console.log(dataArg);
done(); done.resolve();
}); });
}) await done.promise;
}); });
describe("terminating smartsocket", function () {
it("should close the server", function () { tap.test('should be able to make a functionCall from server to client', async () => {
testSmartsocket.closeServer(); let done = smartpromise.defer();
let targetSocket = (() => {
return smartsocket.allSocketConnections.find(socketConnectionArg => {
return socketConnectionArg.alias === 'testClient1';
}); });
}) })();
testSmartsocket
.clientCall(
'testFunction1',
{
value1: 'helloFromServer'
},
targetSocket
)
.then(dataArg => {
console.log(dataArg);
done.resolve();
}); });
});
// terminate
tap.test('should close the server', async () => {
await testSmartsocket.stop();
});
tap.start();

View File

@ -1,8 +1,10 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
export * from "./smartsocket.classes.smartsocket"; // export main classes
export * from "./smartsocket.classes.smartsocketclient"; export * from './smartsocket.classes.smartsocket';
export * from "./smartsocket.classes.socketfunction"; export * from './smartsocket.classes.smartsocketclient';
export * from "./smartsocket.classes.socketrole";
// need something more exposed? Create an issue on GitLab! // export further classes and objects
export * from './smartsocket.classes.socketfunction';
export * from './smartsocket.classes.socketrole';
export * from './smartsocket.classes.socketconnection';

View File

@ -1,68 +1,126 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
import * as helpers from "./smartsocket.helpers"; import * as helpers from './smartsocket.helpers';
// classes // classes
import { Objectmap } from "lik"; import { Objectmap } from '@pushrocks/lik';
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketConnection } from "./smartsocket.classes.socketconnection"; import { SocketRequest } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketServer } from './smartsocket.classes.socketserver';
// socket.io
import * as SocketIO from 'socket.io';
export interface ISmartsocketConstructorOptions { export interface ISmartsocketConstructorOptions {
port: number; port?: number;
}
};
export class Smartsocket { export class Smartsocket {
options: ISmartsocketConstructorOptions public options: ISmartsocketConstructorOptions;
io: SocketIO.Server; public io: SocketIO.Server;
openSockets = new Objectmap<SocketConnection>(); public openSockets = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>();
private socketServer = new SocketServer(this);
constructor(optionsArg: ISmartsocketConstructorOptions) { constructor(optionsArg: ISmartsocketConstructorOptions) {
this.options = optionsArg; this.options = optionsArg;
}; }
// tslint:disable-next-line:member-ordering
public async setExternalServer(serverType: 'smartexpress', serverArg: any) {
await this.socketServer.setExternalServer(serverType, serverArg);
}
/**
* starts smartsocket
*/
public async start() {
this.io = plugins.socketIo(this.socketServer.getServerForSocketIo());
await this.socketServer.start();
this.io.on('connection', socketArg => {
this._handleSocketConnection(socketArg);
});
}
/**
* stops smartsocket
*/
public async stop() {
await plugins.smartdelay.delayFor(1000);
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
this.io.close();
// stop the corresponging server
this.socketServer.stop();
}
// communication
/**
* allows call to specific client.
*/
public async clientCall(
functionNameArg: string,
dataArg: any,
targetSocketConnectionArg: SocketConnection
) {
const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest({
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
side: 'requesting'
});
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => {
done.resolve(dataArg2.funcDataArg);
});
const result = await done.promise;
return result;
}
/**
* adds socketRoles
*/
public addSocketRoles(socketRolesArray: SocketRole[]): void {
for (const socketRole of socketRolesArray) {
this.socketRoles.add(socketRole);
}
return;
}
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocketConnection(socketArg) { private _handleSocketConnection(socketArg) {
let socketConnection: SocketConnection = new SocketConnection({ const socketConnection: SocketConnection = new SocketConnection({
alias: undefined, alias: undefined,
authenticated: false, authenticated: false,
role: undefined, role: undefined,
side: 'server',
smartsocketHost: this,
socket: socketArg socket: socketArg
}); });
plugins.beautylog.log("Socket connected. Trying to authenticate...") plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
this.openSockets.add(socketConnection); this.openSockets.add(socketConnection);
socketConnection.authenticate() socketConnection
.authenticate()
.then(() => { .then(() => {
return socketConnection.listenToFunctionRequests(); return socketConnection.listenToFunctionRequests();
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
}); });
};
/**
* starts listening to incling sockets:
*/
startServer = () => {
this.io = plugins.socketIo(this.options.port);
this.io.on("connection", (socketArg) => {
this._handleSocketConnection(socketArg);
});
}
closeServer = () => {
this.openSockets.forEach((socketObjectArg: SocketConnection) => {
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
this.io.close();
};
// communication
clientCall(){
// TODO: target specific client and initiate response
} }
} }

View File

@ -1,14 +1,9 @@
import * as plugins from "./smartsocket.plugins" import * as plugins from './smartsocket.plugins';
import { SocketConnection } from './smartsocket.classes.socketconnection';
import { ISocketFunctionCall, SocketFunction } from './smartsocket.classes.socketfunction';
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
// import interfaces
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"
// import classes
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest } from "./smartsocket.classes.socketrequest";
/** /**
* interface for class SmartsocketClient * interface for class SmartsocketClient
*/ */
@ -30,52 +25,56 @@ export class SmartsocketClient {
constructor(optionsArg: ISmartsocketClientOptions) { constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
this.role = optionsArg.role; 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.serverPassword = optionsArg.password;
}; }
/** /**
* connect the client to the server * connect the client to the server
*/ */
connect() { connect() {
let done = plugins.q.defer(); let done = plugins.smartpromise.defer();
plugins.beautylog.log("trying to connect..."); plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
let socketUrl = `${this.serverUrl}:${this.serverPort}`; let socketUrl = `${this.serverUrl}:${this.serverPort}`;
this.socketConnection = new SocketConnection({ this.socketConnection = new SocketConnection({
alias: this.alias, alias: this.alias,
authenticated: false, authenticated: false,
role: undefined, role: undefined,
side: 'client',
smartsocketHost: null,
socket: plugins.socketIoClient(socketUrl, { multiplex: false }) socket: plugins.socketIoClient(socketUrl, { multiplex: false })
}); });
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.role,
password: this.serverPassword, password: this.serverPassword,
alias: this.alias alias: this.alias
}); });
this.socketConnection.socket.on("authenticated",() => { this.socketConnection.socket.on('authenticated', () => {
console.log("client is authenticated"); console.log('client is authenticated');
this.socketConnection.authenticated = true; this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests(); this.socketConnection.listenToFunctionRequests();
done.resolve(); done.resolve();
}); });
}); });
return done.promise; return done.promise;
}; }
disconnect() { disconnect() {
let done = plugins.q.defer(); let done = plugins.smartpromise.defer();
this.socketConnection.socket.disconnect(); this.socketConnection.socket.disconnect();
this.socketConnection = undefined; this.socketConnection = undefined;
plugins.beautylog.ok("disconnected!"); plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
done.resolve(); done.resolve();
return done.promise; return done.promise;
} }
serverCall(functionNameArg: string, dataArg: any) { serverCall(functionNameArg: string, dataArg: any) {
let done = plugins.q.defer(); let done = plugins.smartpromise.defer();
let socketRequest = new SocketRequest({ let socketRequest = new SocketRequest({
side:"requesting", side: 'requesting',
originSocketConnection: this.socketConnection, originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
funcCallData: { funcCallData: {
@ -83,11 +82,9 @@ export class SmartsocketClient {
funcDataArg: dataArg funcDataArg: dataArg
} }
}); });
socketRequest.dispatch() socketRequest.dispatch().then((dataArg: ISocketFunctionCall) => {
.then((dataArg:ISocketFunctionCall) => {
done.resolve(dataArg.funcDataArg); done.resolve(dataArg.funcDataArg);
}); });
return done.promise; return done.promise;
}; }
} }

View File

@ -1,13 +1,28 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
import * as helpers from "./smartsocket.helpers"; import * as helpers from './smartsocket.helpers';
import { Objectmap } from '@pushrocks/lik';
// import classes // import classes
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SocketRequest, ISocketRequestDataObject, allSocketRequests } from "./smartsocket.classes.socketrequest"; import { SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketRole } from "./smartsocket.classes.socketrole"; import {
SocketRequest,
ISocketRequestDataObject,
allSocketRequests
} from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
// socket.io
import * as SocketIO from 'socket.io';
// export interfaces // export interfaces
/**
* defines is a SocketConnection is server or client side. Important for mesh setups.
*/
export type TSocketConnectionSide = 'server' | 'client';
/** /**
* interface for constructor of class SocketConnection * interface for constructor of class SocketConnection
*/ */
@ -15,39 +30,52 @@ export interface ISocketConnectionConstructorOptions {
alias: string; alias: string;
authenticated: boolean; authenticated: boolean;
role: SocketRole; role: SocketRole;
side: TSocketConnectionSide;
smartsocketHost: Smartsocket;
socket: SocketIO.Socket | SocketIOClient.Socket; socket: SocketIO.Socket | SocketIOClient.Socket;
}; }
/** /**
* interface for authentication data * interface for authentication data
*/ */
export interface ISocketConnectionAuthenticationObject { export interface ISocketConnectionAuthenticationObject {
role: "coreflowContainer", role: 'coreflowContainer';
password: "somePassword", password: 'somePassword';
alias: "coreflow1" alias: 'coreflow1';
} }
// export classes // export classes
export let allSocketConnections = new Objectmap<SocketConnection>();
/** /**
* class SocketConnection represents a websocket connection * class SocketConnection represents a websocket connection
*/ */
export class SocketConnection { export class SocketConnection {
alias: string; alias: string;
side: TSocketConnectionSide;
authenticated: boolean = false; authenticated: boolean = false;
role: SocketRole; role: SocketRole;
socket: SocketIO.Socket | SocketIOClient.Socket; smartsocketHost: Smartsocket;
socket: any; // SocketIO.Socket | SocketIOClient.Socket
constructor(optionsArg: ISocketConnectionConstructorOptions) { constructor(optionsArg: ISocketConnectionConstructorOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated; this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role; this.role = optionsArg.role;
this.side = optionsArg.side;
this.smartsocketHost = optionsArg.smartsocketHost;
this.socket = optionsArg.socket; this.socket = optionsArg.socket;
// standard behaviour that is always true // standard behaviour that is always true
this.socket.on("disconnect", () => { allSocketConnections.add(this);
plugins.beautylog.info(`Client ${this.alias} disconnected`); this.socket.on('disconnect', () => {
plugins.smartlog.defaultLogger.log(
'info',
`SocketConnection with >alias ${this.alias} on >side ${this.side} disconnected`
);
this.socket.disconnect();
allSocketConnections.remove(this);
}); });
}; }
// authenticating -------------------------- // authenticating --------------------------
@ -55,26 +83,33 @@ export class SocketConnection {
* authenticate the socket * authenticate the socket
*/ */
authenticate() { authenticate() {
let done = plugins.q.defer(); let done = plugins.smartpromise.defer();
this.socket.on("dataAuth", dataArg => { this.socket.on('dataAuth', (dataArg: ISocketConnectionAuthenticationObject) => {
plugins.beautylog.log("received authentication data. now hashing and comparing..."); plugins.smartlog.defaultLogger.log(
this.socket.removeListener("dataAuth", () => { }); 'info',
if ((true)) { // TODO: authenticate password 'received authentication data. now hashing and comparing...'
this.alias = dataArg.alias );
this.socket.removeListener('dataAuth', () => {});
if (helpers.checkPasswordForRole(dataArg, this.smartsocketHost)) {
// TODO: authenticate password
this.alias = dataArg.alias;
this.authenticated = true; this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role); this.role = helpers.getSocketRoleByName(dataArg.role, this.smartsocketHost);
this.socket.emit("authenticated"); this.socket.emit('authenticated');
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`); plugins.smartlog.defaultLogger.log(
'ok',
`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`
);
done.resolve(this); done.resolve(this);
} else { } else {
this.authenticated = false; this.authenticated = false;
this.socket.disconnect(); this.socket.disconnect();
done.reject("not authenticated"); done.reject('not authenticated');
}; }
}); });
this.socket.emit("requestAuth"); this.socket.emit('requestAuth');
return done.promise; return done.promise;
}; }
// listening ------------------------------- // listening -------------------------------
@ -82,42 +117,52 @@ export class SocketConnection {
* listen to function requests * listen to function requests
*/ */
listenToFunctionRequests() { listenToFunctionRequests() {
let done = plugins.q.defer(); let done = plugins.smartpromise.defer();
if (this.authenticated) { if (this.authenticated) {
this.socket.on("function", (dataArg:ISocketRequestDataObject) => { this.socket.on('function', (dataArg: ISocketRequestDataObject) => {
// check if requested function is available to the socket's scope // check if requested function is available to the socket's scope
plugins.beautylog.log("function request received"); plugins.smartlog.defaultLogger.log('info', 'function request received');
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => { let referencedFunction: SocketFunction = this.role.allowedFunctions.find(
socketFunctionArg => {
return socketFunctionArg.name === dataArg.funcCallData.funcName; return socketFunctionArg.name === dataArg.funcCallData.funcName;
}); }
);
if (referencedFunction !== undefined) { if (referencedFunction !== undefined) {
plugins.beautylog.ok!("function in access scope"); plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
let localSocketRequest = new SocketRequest({ let localSocketRequest = new SocketRequest({
side:"responding", side: 'responding',
originSocketConnection: this, originSocketConnection: this,
shortId: dataArg.shortId, shortId: dataArg.shortId,
funcCallData: dataArg.funcCallData funcCallData: dataArg.funcCallData
}); });
localSocketRequest.createResponse(); // takes care of creating response and sending it back localSocketRequest.createResponse(); // takes care of creating response and sending it back
} else { } else {
plugins.beautylog.warn("function not existent or out of access scope"); plugins.smartlog.defaultLogger.log(
}; 'warn',
'function not existent or out of access scope'
);
}
}); });
this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => { this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject) => {
plugins.beautylog.info(`received response for request with id ${dataArg.shortId}`); plugins.smartlog.defaultLogger.log(
'info',
`received response for request with id ${dataArg.shortId}`
);
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId); let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg); targetSocketRequest.handleResponse(dataArg);
}); });
plugins.beautylog.log(`now listening to function requests for ${this.alias}`); plugins.smartlog.defaultLogger.log(
'info',
`now listening to function requests for ${this.alias}`
);
done.resolve(this); done.resolve(this);
} else { } else {
let errMessage: "socket needs to be authenticated first"; let errMessage: 'socket needs to be authenticated first';
plugins.beautylog.error(errMessage); plugins.smartlog.defaultLogger.log('error', errMessage);
done.reject(errMessage); done.reject(errMessage);
}; }
return done.promise; return done.promise;
}; }
// sending ---------------------- // sending ----------------------
}
};

View File

@ -1,8 +1,9 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
// import classes // import classes
import { Objectmap } from "lik"; import { Objectmap } from '@pushrocks/lik';
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from './smartsocket.classes.socketrole';
import { SocketConnection } from './smartsocket.classes.socketconnection';
// export interfaces // export interfaces
@ -11,9 +12,9 @@ import { SocketRole } from "./smartsocket.classes.socketrole";
*/ */
export interface ISocketFunctionConstructorOptions { export interface ISocketFunctionConstructorOptions {
funcName: string; funcName: string;
funcDef: any; funcDef: TFuncDef;
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
@ -21,14 +22,12 @@ export interface ISocketFunctionConstructorOptions {
export interface ISocketFunctionCall { export interface ISocketFunctionCall {
funcName: string; funcName: string;
funcDataArg: any; funcDataArg: any;
}; }
/** /**
* interface for function definition of SocketFunction * interface for function definition of SocketFunction
*/ */
export interface IFuncDef { export type TFuncDef = (dataArg: any, connectionArg: SocketConnection) => PromiseLike<any>;
(dataArg:any):PromiseLike<any>
}
// export objects // export objects
export let allSocketFunctions = new Objectmap<SocketFunction>(); export let allSocketFunctions = new Objectmap<SocketFunction>();
@ -40,7 +39,7 @@ export let allSocketFunctions = new Objectmap<SocketFunction>();
*/ */
export class SocketFunction { export class SocketFunction {
name: string; name: string;
funcDef: IFuncDef; funcDef: TFuncDef;
roles: SocketRole[]; roles: SocketRole[];
/** /**
@ -52,9 +51,28 @@ export class SocketFunction {
this.roles = optionsArg.allowedRoles; this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles) { for (let socketRoleArg of this.roles) {
this._notifyRole(socketRoleArg); this._notifyRole(socketRoleArg);
}; }
allSocketFunctions.add(this); // map instance with Objectmap allSocketFunctions.add(this); // map instance with Objectmap
}
/**
* invokes the function of this SocketFunction
*/
invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> {
let done = plugins.smartpromise.defer();
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => {
let funcResponseData: ISocketFunctionCall = {
funcName: this.name,
funcDataArg: resultData
}; };
done.resolve(funcResponseData);
});
} else {
throw new Error("SocketFunction.name does not match the data argument's .name!");
}
return done.promise;
}
/** /**
* notifies a role about access to this SocketFunction * notifies a role about access to this SocketFunction
@ -62,26 +80,4 @@ export class SocketFunction {
private _notifyRole(socketRoleArg: SocketRole) { private _notifyRole(socketRoleArg: SocketRole) {
socketRoleArg.addSocketFunction(this); socketRoleArg.addSocketFunction(this);
} }
/**
* invokes the function of this SocketFunction
*/
invoke(dataArg:ISocketFunctionCall):plugins.q.Promise<any> {
let done = plugins.q.defer();
if(dataArg.funcName === this.name){
this.funcDef(dataArg.funcDataArg)
.then((resultData:any) => {
let funcResponseData:ISocketFunctionCall = {
funcName:this.name,
funcDataArg:resultData
}
done.resolve(funcResponseData);
});
} else {
throw new Error("SocketFunction.name does not match the data argument's .name!");
}
return done.promise;
};
} }

View File

@ -1,17 +1,18 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
import * as helpers from "./smartsocket.helpers"; import * as helpers from './smartsocket.helpers';
// import interfaces // import interfaces
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction"; import { ISocketFunctionCall } from './smartsocket.classes.socketfunction';
// import classes // import classes
import { Objectmap } from "lik"; import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from './smartsocket.classes.socketfunction';
import { SocketConnection } from "./smartsocket.classes.socketconnection"; import { SocketConnection } from './smartsocket.classes.socketconnection';
import { defaultLogger } from '@pushrocks/smartlog';
// export interfaces // export interfaces
export type TSocketRequestStatus = "new" | "pending" | "finished"; export type TSocketRequestStatus = 'new' | 'pending' | 'finished';
export type TSocketRequestSide = "requesting" | "responding"; export type TSocketRequestSide = 'requesting' | 'responding';
/** /**
* interface of constructor of class SocketRequest * interface of constructor of class SocketRequest
@ -21,7 +22,7 @@ export interface SocketRequestConstructorOptions {
originSocketConnection: SocketConnection; originSocketConnection: SocketConnection;
shortId: string; shortId: string;
funcCallData?: ISocketFunctionCall; funcCallData?: ISocketFunctionCall;
}; }
/** /**
* 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
@ -30,46 +31,46 @@ export interface ISocketRequestDataObject {
funcCallData: ISocketFunctionCall; funcCallData: ISocketFunctionCall;
shortId: string; shortId: string;
responseTimeout?: number; responseTimeout?: number;
}; }
// export objects // export objects
export let allSocketRequests = new Objectmap<SocketRequest>(); export let allSocketRequests = new Objectmap<SocketRequest>();
// export classes // export classes
export class SocketRequest { export class SocketRequest {
status: TSocketRequestStatus = "new"; public status: TSocketRequestStatus = 'new';
side: TSocketRequestSide; public side: TSocketRequestSide;
shortid: string; public shortid: string;
originSocketConnection: SocketConnection; public originSocketConnection: SocketConnection;
funcCallData: ISocketFunctionCall public funcCallData: ISocketFunctionCall;
done = plugins.q.defer(); public done = plugins.smartpromise.defer();
constructor(optionsArg: SocketRequestConstructorOptions) { constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side; this.side = optionsArg.side;
this.shortid = optionsArg.shortId; this.shortid = optionsArg.shortId;
this.funcCallData = optionsArg.funcCallData; this.funcCallData = optionsArg.funcCallData;
this.originSocketConnection = optionsArg.originSocketConnection; this.originSocketConnection = optionsArg.originSocketConnection;
allSocketRequests.add(this); allSocketRequests.add(this);
}; }
// requesting -------------------------- // requesting --------------------------
/** /**
* dispatches a socketrequest from the requesting to the receiving side * dispatches a socketrequest from the requesting to the receiving side
*/ */
dispatch() { public dispatch() {
let requestData: ISocketRequestDataObject = { let requestData: ISocketRequestDataObject = {
funcCallData: this.funcCallData, funcCallData: this.funcCallData,
shortId: this.shortid shortId: this.shortid
}
this.originSocketConnection.socket.emit("function", requestData);
return this.done.promise;
}; };
this.originSocketConnection.socket.emit('function', requestData);
return this.done.promise;
}
/** /**
* handles the response that is received by the requesting side * handles the response that is received by the requesting side
*/ */
handleResponse(responseDataArg: ISocketRequestDataObject) { public handleResponse(responseDataArg: ISocketRequestDataObject) {
plugins.beautylog.log("handling response!"); plugins.smartlog.defaultLogger.log('info', 'handling response!');
this.done.resolve(responseDataArg.funcCallData); this.done.resolve(responseDataArg.funcCallData);
allSocketRequests.remove(this); allSocketRequests.remove(this);
} }
@ -79,18 +80,27 @@ export class SocketRequest {
/** /**
* creates the response on the responding side * creates the response on the responding side
*/ */
createResponse() { public async createResponse(): Promise<void> {
let targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName); const targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(
plugins.beautylog.info(`invoking ${targetSocketFunction.name}`); this.funcCallData.funcName
targetSocketFunction.invoke(this.funcCallData) );
.then((resultData) => { if (!targetSocketFunction) {
plugins.beautylog.log("got resultData. Sending it to requesting party.") defaultLogger.log(
'warn',
`There is no SocketFunction defined for ${this.funcCallData.funcName}`
);
defaultLogger.log('warn', `So now response is being sent.`);
return;
}
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.');
let requestData: ISocketRequestDataObject = { let requestData: ISocketRequestDataObject = {
funcCallData: resultData, funcCallData: resultData,
shortId: this.shortid shortId: this.shortid
}; };
this.originSocketConnection.socket.emit("functionResponse",requestData); this.originSocketConnection.socket.emit('functionResponse', requestData);
allSocketRequests.remove(this); allSocketRequests.remove(this);
}); });
} }
}; }

View File

@ -1,12 +1,8 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
// import classes // import classes
import { Objectmap } from "lik"; import { Objectmap } from '@pushrocks/lik';
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from './smartsocket.classes.socketfunction';
export let allSocketRoles = new Objectmap<SocketRole>();
/** /**
* interface for class SocketRole * interface for class SocketRole
@ -26,8 +22,7 @@ export class SocketRole {
constructor(optionsArg: SocketRoleOptions) { constructor(optionsArg: SocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
allSocketRoles.add(this); }
};
addSocketFunction(socketFunctionArg: SocketFunction) { addSocketFunction(socketFunctionArg: SocketFunction) {
this.allowedFunctions.add(socketFunctionArg); this.allowedFunctions.add(socketFunctionArg);
} }

View File

@ -0,0 +1,77 @@
import * as plugins from './smartsocket.plugins';
// used in case no other server is supplied
import * as http from 'http';
import { Smartsocket } from './smartsocket.classes.smartsocket';
/**
* class socketServer
* handles the attachment of socketIo to whatever server is in play
*/
export class SocketServer {
private smartsocket: Smartsocket;
private httpServer: http.Server;
// wether httpServer is standalone
private standaloneServer = false;
private expressServer: any;
constructor(smartSocketInstance: Smartsocket) {
this.smartsocket = smartSocketInstance;
}
/**
* starts the server with another server
* also works with an express style server
*/
public async setExternalServer(
serverType: 'smartexpress',
serverArg: plugins.smartexpress.Server
) {
await serverArg.startedPromise;
this.httpServer = serverArg.httpServer;
}
/**
* gets the server for socket.io
*/
public getServerForSocketIo() {
if (this.httpServer) {
return this.httpServer;
} else {
this.httpServer = new http.Server();
this.standaloneServer = true;
return this.httpServer;
}
}
/**
* starts listening to incoming sockets:
*/
public async start() {
const done = plugins.smartpromise.defer();
// handle http servers
// 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!');
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}`);
done.resolve();
});
} else {
done.resolve();
}
// nothing else to do if express server is set
await done.promise;
return;
}
/**
* closes the server
*/
public async stop() {}
}

View File

@ -1,32 +1,60 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
// classes // classes
import { Smartsocket } from "./smartsocket.classes.smartsocket"; import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction"; import { SocketFunction, allSocketFunctions } from './smartsocket.classes.socketfunction';
import { SocketConnection } from "./smartsocket.classes.socketconnection"; import {
import { SocketRequest, allSocketRequests, TSocketRequestSide } from "./smartsocket.classes.socketrequest"; SocketConnection,
import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; ISocketConnectionAuthenticationObject
} from './smartsocket.classes.socketconnection';
import {
SocketRequest,
allSocketRequests,
TSocketRequestSide
} from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
// SocketConnection helpers
export let checkPasswordForRole = (
dataArg: ISocketConnectionAuthenticationObject,
referenceSmartsocket: Smartsocket
): boolean => {
let targetPasswordHash = getSocketRoleByName(dataArg.role, referenceSmartsocket).passwordHash;
let computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password);
return targetPasswordHash === computedCompareHash;
};
// SocketFunction helpers // SocketFunction helpers
export let getSocketFunctionByName = (functionNameArg: string): SocketFunction => { export let getSocketFunctionByName = (functionNameArg: string): SocketFunction => {
return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg }); return allSocketFunctions.find(socketFunctionArg => {
} return socketFunctionArg.name === functionNameArg;
});
};
// SocketRequest helpers // SocketRequest helpers
/** /**
* get corresponding Socketrequest instance by shortId * get corresponding Socketrequest instance by shortId
*/ */
export let getSocketRequestById = (shortIdArg:string,requestSide?:TSocketRequestSide):SocketRequest => { export let getSocketRequestById = (
return allSocketRequests.find((socketRequestArg) => {return socketRequestArg.shortid === shortIdArg}) shortIdArg: string,
} requestSide?: TSocketRequestSide
): SocketRequest => {
return allSocketRequests.find(socketRequestArg => {
return socketRequestArg.shortid === shortIdArg;
});
};
// SocketRole helpers // SocketRole helpers
/** /**
* get corresponding SocketRole instance by name * get corresponding SocketRole instance by name
*/ */
export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => { export let getSocketRoleByName = (
return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg }) socketRoleNameArg: string,
referenceSmartsocket: Smartsocket
): SocketRole => {
return referenceSmartsocket.socketRoles.find(socketRoleArg => {
return socketRoleArg.name === socketRoleNameArg;
});
}; };

View File

@ -1,8 +1,21 @@
import "typings-global"; import * as lik from '@pushrocks/lik';
export import beautylog = require("beautylog"); import * as smartlog from '@pushrocks/smartlog';
export import lik = require("lik"); import * as smarthash from '@pushrocks/smarthash';
export import q = require("q"); import * as smartdelay from '@pushrocks/smartdelay';
export import shortid = require("shortid"); import * as smartexpress from '@pushrocks/smartexpress';
export import socketIo = require("socket.io"); import * as smartpromise from '@pushrocks/smartpromise';
export import socketIoClient = require("socket.io-client"); import * as shortid from 'shortid';
export import taskbuffer = require("taskbuffer"); import socketIo from 'socket.io';
import socketIoClient from 'socket.io-client';
export {
lik,
smartlog,
smarthash,
smartdelay,
smartexpress,
smartpromise,
shortid,
socketIo,
socketIoClient
};

17
tslint.json Normal file
View File

@ -0,0 +1,17 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}