Compare commits

..

99 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
fb99848df1 1.1.41 2019-08-12 22:45:58 +02:00
1bd39d0755 fix(core): update 2019-08-12 22:45:58 +02:00
d24c5f628c 1.1.40 2019-08-12 22:31:41 +02:00
33fbabdd72 fix(core): update 2019-08-12 22:31:40 +02:00
9d26fff986 1.1.39 2019-08-12 18:16:26 +02:00
41151e5436 fix(core): update 2019-08-12 18:16:25 +02:00
109c5197f3 1.1.38 2019-06-07 08:40:24 +02:00
c3d47c1f50 fix(core): update 2019-06-07 08:40:24 +02:00
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
180ae23c9a 1.0.7 2016-08-15 01:38:38 +02:00
7f0fff0bf4 updated tests. Data now flows correctly between socket endpoints 2016-08-15 01:38:28 +02:00
42 changed files with 4466 additions and 1250 deletions

23
.gitignore vendored
View File

@ -1,3 +1,22 @@
node_modules/ .nogit/
# artifacts
coverage/ coverage/
docs/ public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,36 +1,119 @@
image: hosttoday/ht-docker-node:npmts # gitzone ci_default
image: registry.gitlab.com/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: testBuild:
stage: test stage: test
script: script:
- npmci test stable - npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci command npm run build
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 lts
- npmci npm publish
only: only:
- tags - tags
tags: tags:
- docker - docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:18-dind
stage: metadata
script:
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
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: {}

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"request": "launch",
"args": [
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
]
}

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)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.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,52 +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(socketConnection.listenToFunctionRequests);
}
;
// communication
clientCall() {
// TODO: target specific client and initiate response
}
}
exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5z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FLENBQUMsS0FBSyxFQUFFLENBQUM7UUFDcEIsQ0FBQyxDQUFDO1FBcENFLElBQUksQ0FBQyxPQUFPLEdBQUcsVUFBVSxDQUFDO0lBQzlCLENBQUM7O0lBRUQ7O09BRUc7SUFDSyx1QkFBdUIsQ0FBQyxTQUFTO1FBQ3JDLElBQUksZ0JBQWdCLEdBQXNCLElBQUksdURBQWdCLENBQUM7WUFDM0QsS0FBSyxFQUFDLFNBQVM7WUFDZixhQUFhLEVBQUMsS0FBSztZQUNuQixJQUFJLEVBQUMsU0FBUztZQUNkLE1BQU0sRUFBQyxTQUFTO1NBQ25CLENBQUMsQ0FBQztRQUNILE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLDZDQUE2QyxDQUFDLENBQUE7UUFDcEUsSUFBSSxDQUFDLFdBQVcsQ0FBQyxHQUFHLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztRQUN2QyxnQkFBZ0IsQ0FBQyxZQUFZLEVBQUU7YUFDMUIsSUFBSSxDQUFDLGdCQUFnQixDQUFDLHdCQUF3QixDQUFDLENBQUM7SUFDekQsQ0FBQzs7SUFxQkQsZ0JBQWdCO0lBQ2hCLFVBQVU7UUFFTiw2REFBNkQ7SUFDakUsQ0FBQztBQUNMLENBQUM7QUFoRFksbUJBQVcsY0FnRHZCLENBQUEifQ==

View File

@ -1,29 +0,0 @@
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
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: ISocketFunctionCall): plugins.q.Promise<{}>;
}

View File

@ -1,71 +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");
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(() => {
done.resolve();
});
return done.promise;
}
;
}
exports.SmartsocketClient = SmartsocketClient;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0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UNmLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3hCLENBQUM7SUFDRCxVQUFVLENBQUMsZUFBc0IsRUFBQyxPQUEyQjtRQUN6RCxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO1FBQzdCLElBQUksYUFBYSxHQUFHLElBQUksaURBQWEsQ0FBQztZQUNsQyxJQUFJLEVBQUMsWUFBWTtZQUNqQixzQkFBc0IsRUFBQyxJQUFJLENBQUMsZ0JBQWdCO1lBQzVDLE9BQU8sRUFBQyxPQUFPLENBQUMsT0FBTyxDQUFDLFFBQVEsRUFBRTtZQUNsQyxZQUFZLEVBQUM7Z0JBQ1QsUUFBUSxFQUFFLGVBQWU7Z0JBQ3pCLFdBQVcsRUFBQyxPQUFPO2FBQ3RCO1NBQ0osQ0FBQyxDQUFDO1FBQ0gsYUFBYSxDQUFDLFFBQVEsRUFBRTthQUNuQixJQUFJLENBQUM7WUFDRixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7UUFDbkIsQ0FBQyxDQUFDLENBQUM7UUFDUCxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN4QixDQUFDOztBQUVMLENBQUM7QUFwRVkseUJBQWlCLG9CQW9FN0IsQ0FBQSJ9

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,90 +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.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.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
let referencedFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.funcCallData.funcName;
});
if (referencedFunction !== undefined) {
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) => {
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId);
targetSocketRequest.handleResponse(dataArg);
});
}
else {
done.reject("socket needs to be authenticated first");
}
;
return done.promise;
}
;
}
exports.SocketConnection = SocketConnection;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRjb25uZWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSx1QkFBdUIsQ0FBQyxDQUFBO0FBQ2pELE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFJakQsb0RBQTJFLHFDQUFxQyxDQUFDLENBQUE7QUFhaEgsQ0FBQztBQVdGLGlCQUFpQjtBQUVqQjs7R0FFRztBQUNIO0lBS0ksWUFBWSxVQUErQztRQUN2RCxJQUFJLENBQUMsS0FBSyxHQUFHLFVBQVUsQ0FBQyxLQUFLLENBQUM7UUFDOUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxVQUFVLENBQUMsYUFBYSxDQUFDO1FBQzlDLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsTUFBTSxHQUFHLFVBQVUsQ0FBQyxNQUFNLENBQUM7UUFFaEMseUNBQXlDO1FBQ3pDLElBQUksQ0FBQyxNQUFNLENBQUMsRUFBRSxDQUFDLFlBQVksRUFBRTtZQUN6QixPQUFPLENBQUMsU0FBUyxDQUFDLElBQUksQ0FBQyxVQUFVLElBQUksQ0FBQyxLQUFLLGVBQWUsQ0FBQyxDQUFDO1FBQ2hFLENBQUMsQ0FBQyxDQUFDO0lBQ1AsQ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dCQUNoRyxDQUFDO2dCQUFDLElBQUksQ0FBQyxDQUFDO29CQUNKLE9BQU8sQ0FBQyxTQUFTLENBQUMsSUFBSSxDQUFDLDhDQUE4QyxDQUFDLENBQUM7Z0JBQzNFLENBQUM7Z0JBQUEsQ0FBQztZQUNOLENBQUMsQ0FBQyxDQUFDO1lBQ0gsSUFBSSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsa0JBQWtCLEVBQUUsQ0FBQyxPQUFnQztnQkFDaEUsSUFBSSxtQkFBbUIsR0FBRyxPQUFPLENBQUMsb0JBQW9CLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFDO2dCQUN4RSxtQkFBbUIsQ0FBQyxjQUFjLENBQUMsT0FBTyxDQUFDLENBQUM7WUFDaEQsQ0FBQyxDQUFDLENBQUE7UUFDTixDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDSixJQUFJLENBQUMsTUFBTSxDQUFDLHdDQUF3QyxDQUFDLENBQUM7UUFDMUQsQ0FBQztRQUFBLENBQUM7UUFDRixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN4QixDQUFDOztBQUlMLENBQUM7QUFoRlksd0JBQWdCLG1CQWdGNUIsQ0FBQTtBQUFBLENBQUMifQ==

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,53 +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) => {
done.resolve(resultData);
});
}
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRmdW5jdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc29ja2V0ZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFFakQsaUJBQWlCO0FBQ2pCLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQVk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0FBQyxPQUFPLENBQUMsVUFBVSxDQUFDLENBQUM7WUFDN0IsQ0FBQyxDQUFDLENBQUM7UUFFWCxDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDSixNQUFNLElBQUksS0FBSyxDQUFDLCtEQUErRCxDQUFDLENBQUM7UUFDckYsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3hCLENBQUM7O0FBRUwsQ0FBQztBQTFDWSxzQkFBYyxpQkEwQzFCLENBQUEifQ==

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,60 +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;
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) {
this.done.resolve(responseDataArg);
exports.allSocketRequests.remove(this);
}
// responding --------------------------
/**
* creates the response on the responding side
*/
createResponse() {
let targetSocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName);
targetSocketFunction.invoke(this.funcCallData)
.then((resultData) => {
let requestData = {
funcCallData: resultData,
shortId: this.shortid
};
this.originSocketConnection.socket.emit("functionResponse", requestData);
exports.allSocketRequests.remove(this);
});
}
}
exports.SocketRequest = SocketRequest;
;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyZXF1ZXN0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyZXF1ZXN0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSx1QkFBdUIsQ0FBQyxDQUFBO0FBQ2pELE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFLakQsaUJBQWlCO0FBQ2pCLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQWdCL0IsQ0FBQztBQVNELENBQUM7QUFFRixnQkFBZ0I7QUFDTCx5QkFBaUIsR0FBRyxJQUFJLGVBQVMsRUFBaUIsQ0FBQztBQUU5RCxpQkFBaUI7QUFDakI7SUFPSSxZQUFZLFVBQTJD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ZHLG9CQUFvQixDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDO2FBQ3pDLElBQUksQ0FBQyxDQUFDLFVBQVU7WUFDYixJQUFJLFdBQVcsR0FBNkI7Z0JBQ3hDLFlBQVksRUFBRSxVQUFVO2dCQUN4QixPQUFPLEVBQUUsSUFBSSxDQUFDLE9BQU87YUFDeEIsQ0FBQTtZQUNELElBQUksQ0FBQyxzQkFBc0IsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLGtCQUFrQixFQUFDLFdBQVcsQ0FBQyxDQUFDO1lBQ3hFLHlCQUFpQixDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUNuQyxDQUFDLENBQUMsQ0FBQztJQUNYLENBQUM7QUFDTCxDQUFDO0FBckRZLHFCQUFhLGdCQXFEekIsQ0FBQTtBQUFBLENBQUMifQ==

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"
} }
} }

3290
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.6", "version": "1.1.45",
"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,38 @@
}, },
"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.10",
"@types/shortid": "0.0.27", "@pushrocks/smartdelay": "^2.0.3",
"@types/socket.io": "^1.4.26", "@pushrocks/smartexpress": "^3.0.38",
"@types/socket.io-client": "^1.4.26", "@pushrocks/smarthash": "^2.0.6",
"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.8",
"typings-test": "^1.0.1" "@gitzone/tstest": "^1.0.24",
} "@pushrocks/tapbundle": "^3.0.11",
"@types/node": "^12.7.1",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0"
},
"private": false,
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

1
test/common/test.ts Normal file
View File

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

2
test/test.d.ts vendored
View File

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

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

@ -0,0 +1,104 @@
// 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();
});
// 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, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1'
});
testSmartsocket.addSocketFunction(testSocketFunction1);
console.log(testSmartsocket.socketFunctions);
});
tap.test('should start listening when .started is called', async () => {
await testSmartsocket.start();
});
// class SmartsocketClient
tap.test('should react to a new websocket connection from client', async () => {
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: 'http://localhost',
password: 'testPassword',
alias: 'testClient1',
role: 'testRole1'
});
testSmartsocketClient.addSocketFunction(testSocketFunction1);
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
});
tap.test('should be able to make a functionCall from client to server', async () => {
const response = await testSmartsocketClient.serverCall('testFunction1', {
value1: 'hello'
});
console.log(response);
});
tap.test('should be able to make a functionCall from server to client', async () => {});
// terminate
tap.test('should close the server', async () => {
await testSmartsocket.stop();
await myseServer.stop();
});
tap.start();

View File

@ -1,75 +0,0 @@
"use strict";
require("typings-test");
require("should");
const smartsocket = require("../dist/index");
const q = require("q");
let testSmartsocket;
let testSmartsocketClient;
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 () {
});
describe("class SocketFunction", function () {
it("should register a new Function", function () {
testSocketFunction1 = new smartsocket.SocketFunction({
funcName: "testFunction1",
funcDef: () => {
let done = q.defer();
return done.promise;
},
allowedRoles: []
});
});
});
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: undefined
});
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 () {
});
});
describe("terminating smartsocket", function () {
it("should close the server", function () {
testSmartsocket.closeServer();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQixNQUFPLFdBQVcsV0FBVyxlQUFlLENBQUMsQ0FBQztBQUM5QyxNQUFPLENBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUV4QixJQUFJLGVBQXdDLENBQUM7QUFDN0MsSUFBSSxxQkFBb0QsQ0FBQztBQUN6RCxJQUFJLG1CQUE4QyxDQUFDO0FBRW5ELElBQUksVUFBVSxHQUFHO0lBQ2IsSUFBSSxFQUFFLElBQUk7Q0FDYixDQUFBO0FBRUQsUUFBUSxDQUFDLGFBQWEsRUFBRTtJQUNwQixRQUFRLENBQUMsbUJBQW1CLEVBQUU7UUFDMUIsRUFBRSxDQUFDLGlDQUFpQyxFQUFFO1lBQ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DMUIsZUFBZSxDQUFDLFdBQVcsRUFBRSxDQUFDO1FBQ2xDLENBQUMsQ0FBQyxDQUFDO0lBQ1AsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQyJ9

View File

@ -1,79 +1,111 @@
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 testSocketFunction1:smartsocket.SocketFunction; let testSocketConnection: smartsocket.SocketConnection;
let testSocketRole1: smartsocket.SocketRole;
let testSocketFunctionForServer: smartsocket.SocketFunction;
let testSocketFunctionClient: 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();
});
});
describe("class SocketRole", function(){
}) // class socketrole
describe("class SocketFunction", function () { tap.test('should add a socketrole', async () => {
it("should register a new Function", function () { testSocketRole1 = new smartsocket.SocketRole({
testSocketFunction1 = new smartsocket.SocketFunction({ name: 'testRole1',
funcName:"testFunction1", passwordHash: smarthash.sha256FromStringSync('testPassword')
funcDef: () => { });
let done = q.defer(); testSmartsocket.addSocketRoles([testSocketRole1]);
return done.promise; });
// class SocketFunction
tap.test('should register a new Function', async () => {
testSocketFunctionForServer = new smartsocket.SocketFunction({
allowedRoles: [testSocketRole1],
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
}, },
allowedRoles:[] funcName: 'testFunction1'
}); });
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
testSocketFunctionClient = new smartsocket.SocketFunction({
allowedRoles: [],
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
funcName: 'testFunction1'
}); });
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
console.log(testSmartsocket.socketFunctions);
}); });
describe("class SmartsocketClient", function () {
it("should react to a new websocket connection from client", function (done) { tap.test('should start listening when .started is called', async () => {
this.timeout(10000); await testSmartsocket.start();
});
// class SmartsocketClient
tap.test('should react to a new websocket connection from client', async () => {
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: undefined role: 'testRole1'
}); });
testSmartsocketClient.connect() testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
.then(() => { console.log(testSmartsocketClient.socketFunctions);
done(); await testSmartsocketClient.connect();
}); });
});
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 () {
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 () => {
const response = await testSmartsocketClient.serverCall('testFunction1', {
value1: 'hello'
}); });
describe("terminating smartsocket", function () { console.log(response);
it("should close the server", function () {
testSmartsocket.closeServer();
}); });
tap.test('should be able to make a functionCall from server to client', async () => {
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();
});
tap.start();

View File

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

View File

@ -1,63 +1,122 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
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 socketConnections = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>();
public socketFunctions = new Objectmap<SocketFunction>();
public socketRequests = new Objectmap<SocketRequest>();
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.socketConnections.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log(
'info',
`disconnect socket with >>alias ${socketObjectArg.alias}`
);
socketObjectArg.socket.disconnect();
});
this.socketConnections.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 socketRequest = new SocketRequest(this, {
funcCallData: {
funcDataArg: dataArg,
funcName: functionNameArg
},
originSocketConnection: targetSocketConnectionArg,
shortId: plugins.shortid.generate(),
side: 'requesting'
});
const response: ISocketFunctionCall = await socketRequest.dispatch();
const result = response.funcDataArg;
return result;
}
/**
* adds socketRoles
*/
public addSocketRoles(socketRolesArray: SocketRole[]): void {
for (const socketRole of socketRolesArray) {
this.socketRoles.add(socketRole);
}
return;
}
public addSocketFunction(socketFunction: SocketFunction) {
this.socketFunctions.add(socketFunction);
}
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocketConnection(socketArg) { private async _handleSocketConnection(socketArg: plugins.socketIo.Socket) {
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.socketConnections.add(socketConnection);
socketConnection.authenticate() await socketConnection.authenticate();
.then(socketConnection.listenToFunctionRequests); await socketConnection.listenToFunctionRequests();
};
/**
* 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,10 @@
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 { SocketRole } from './smartsocket.classes.socketrole';
// 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
*/ */
@ -21,59 +17,81 @@ export interface ISmartsocketClientOptions {
} }
export class SmartsocketClient { export class SmartsocketClient {
alias:string; public alias: string;
role:string; public socketRole: SocketRole;
socketConnection:SocketConnection; public socketConnection: SocketConnection;
serverUrl:string; public serverUrl: string;
serverPort:number; public serverPort: number;
serverPassword:string;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
public socketRoles = new plugins.lik.Objectmap<SocketRole>();
constructor(optionsArg: ISmartsocketClientOptions) { constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
this.role = optionsArg.role; this.serverUrl = optionsArg.url;
this.serverUrl = optionsArg.url
this.serverPort = optionsArg.port; this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password this.socketRole = new SocketRole({
}; name: optionsArg.role,
passwordHash: optionsArg.password
});
}
public addSocketFunction(socketFunction: SocketFunction) {
this.socketFunctions.add(socketFunction);
this.socketRole.allowedFunctions.add(socketFunction);
}
/** /**
* connect the client to the server * connect the client to the server
*/ */
connect(){ public connect() {
let done = plugins.q.defer(); const done = plugins.smartpromise.defer();
plugins.beautylog.log("trying to connect..."); plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
let socketUrl = `${this.serverUrl}:${this.serverPort}`; const 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: this.socketRole,
side: 'client',
smartsocketHost: this,
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"); plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
this.socketConnection.socket.emit("dataAuth", { this.socketConnection.socket.emit('dataAuth', {
role: this.role, role: this.socketRole.name,
password: this.serverPassword, password: this.socketRole.passwordHash,
alias: this.alias alias: this.alias
}); });
this.socketConnection.socket.on("authenticated",() => { this.socketConnection.socket.on('authenticated', () => {
console.log("client is authenticated"); plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
this.socketConnection.authenticated = true;
this.socketConnection.listenToFunctionRequests();
done.resolve(); done.resolve();
}); });
}); });
return done.promise; 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:string,dataArg:ISocketFunctionCall){
let done = plugins.q.defer(); /**
let socketRequest = new SocketRequest({ * disconnect from the server
side:"requesting", */
public async disconnect() {
this.socketConnection.socket.disconnect(true);
this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
}
/**
* 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',
originSocketConnection: this.socketConnection, originSocketConnection: this.socketConnection,
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
funcCallData: { funcCallData: {
@ -81,11 +99,8 @@ export class SmartsocketClient {
funcDataArg: dataArg funcDataArg: dataArg
} }
}); });
socketRequest.dispatch() const response = await socketRequest.dispatch();
.then(() => { const result = response.funcDataArg;
done.resolve(); return result;
}); }
return done.promise;
};
} }

View File

@ -1,13 +1,24 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
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 } from './smartsocket.classes.socketrequest';
import { SocketRole } from './smartsocket.classes.socketrole';
// socket.io
import * as SocketIO from 'socket.io';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
// 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,101 +26,142 @@ export interface ISocketConnectionConstructorOptions {
alias: string; alias: string;
authenticated: boolean; authenticated: boolean;
role: SocketRole; role: SocketRole;
side: TSocketConnectionSide;
smartsocketHost: Smartsocket | SmartsocketClient;
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; public alias: string;
authenticated: boolean; public side: TSocketConnectionSide;
role: SocketRole; public authenticated: boolean = false;
socket: SocketIO.Socket | SocketIOClient.Socket; public role: SocketRole;
public smartsocketRef: Smartsocket | SmartsocketClient;
public socket: 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.smartsocketRef = 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 --------------------------
/** /**
* authenticate the socket * authenticate the socket
*/ */
authenticate() { public authenticate() {
let done = plugins.q.defer(); const 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 (SocketRole.checkPasswordForRole(dataArg, this.smartsocketRef)) {
// TODO: authenticate password
this.alias = dataArg.alias;
this.authenticated = true; this.authenticated = true;
this.role = helpers.getSocketRoleByName(dataArg.role); this.role = SocketRole.getSocketRoleByName(this.smartsocketRef, dataArg.role);
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.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 -------------------------------
/** /**
* listen to function requests * listen to function requests
*/ */
listenToFunctionRequests() { public listenToFunctionRequests() {
let done = plugins.q.defer(); const 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
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => { plugins.smartlog.defaultLogger.log('info', 'function request received');
const referencedFunction: SocketFunction = this.role.allowedFunctions.find(
socketFunctionArg => {
return socketFunctionArg.name === dataArg.funcCallData.funcName; return socketFunctionArg.name === dataArg.funcCallData.funcName;
}); }
if(referencedFunction !== undefined){ );
let localSocketRequest = new SocketRequest({ if (referencedFunction) {
side:"responding", plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
const localSocketRequest = new SocketRequest(this.smartsocketRef, {
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) => {
let targetSocketRequest = helpers.getSocketRequestById(dataArg.shortId); plugins.smartlog.defaultLogger.log(
'info',
`received response for request with id ${dataArg.shortId}`
);
const targetSocketRequest = SocketRequest.getSocketRequestById(
this.smartsocketRef,
dataArg.shortId
);
targetSocketRequest.handleResponse(dataArg); targetSocketRequest.handleResponse(dataArg);
}) });
plugins.smartlog.defaultLogger.log(
'info',
`now listening to function requests for ${this.alias}`
);
done.resolve(this);
} else { } else {
done.reject("socket needs to be authenticated first"); const errMessage = 'socket needs to be authenticated first';
}; plugins.smartlog.defaultLogger.log('error', errMessage);
done.reject(errMessage);
}
return done.promise; return done.promise;
}; }
// sending ---------------------- // sending ----------------------
}
};

View File

@ -1,8 +1,11 @@
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';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
// export interfaces // export interfaces
@ -11,9 +14,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,17 +24,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 let allSocketFunctions = new Objectmap<SocketFunction>();
// export classes // export classes
@ -39,9 +37,20 @@ export let allSocketFunctions = new Objectmap<SocketFunction>();
* class that respresents a function that can be transparently called using a SocketConnection * class that respresents a function that can be transparently called using a SocketConnection
*/ */
export class SocketFunction { export class SocketFunction {
name: string; // STATIC
funcDef: IFuncDef; public static getSocketFunctionByName(
roles: SocketRole[]; smartsocketRefArg: Smartsocket | SmartsocketClient,
functionNameArg: string
): SocketFunction {
return smartsocketRefArg.socketFunctions.find(socketFunctionArg => {
return socketFunctionArg.name === functionNameArg;
});
}
// INSTANCE
public name: string;
public funcDef: TFuncDef;
public roles: SocketRole[];
/** /**
* the constructor for SocketFunction * the constructor for SocketFunction
@ -50,11 +59,29 @@ export class SocketFunction {
this.name = optionsArg.funcName; this.name = optionsArg.funcName;
this.funcDef = optionsArg.funcDef; this.funcDef = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles; this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles){ for (const socketRoleArg of this.roles) {
this._notifyRole(socketRoleArg); this._notifyRole(socketRoleArg);
}
}
/**
* invokes the function of this SocketFunction
*/
public invoke(dataArg: ISocketFunctionCall, socketConnectionArg: SocketConnection): Promise<any> {
const done = plugins.smartpromise.defer();
if (dataArg.funcName === this.name) {
this.funcDef(dataArg.funcDataArg, socketConnectionArg).then((resultData: any) => {
const funcResponseData: ISocketFunctionCall = {
funcName: this.name,
funcDataArg: resultData
}; };
allSocketFunctions.add(this); // map instance with Objectmap 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,22 +89,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) => {
done.resolve(resultData);
});
} else {
throw new Error("SocketFunction.name does not match the data argument's .name!");
}
return done.promise;
};
} }

View File

@ -1,27 +1,28 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from './smartsocket.plugins';
import * as helpers from "./smartsocket.helpers";
// import interfaces // import interfaces
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction"; import { SocketFunction, 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 { SocketConnection } from './smartsocket.classes.socketconnection';
import { SocketConnection } from "./smartsocket.classes.socketconnection"; import { defaultLogger } from '@pushrocks/smartlog';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
// 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
*/ */
export interface SocketRequestConstructorOptions { export interface ISocketRequestConstructorOptions {
side: TSocketRequestSide; side: TSocketRequestSide;
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,63 @@ export interface ISocketRequestDataObject {
funcCallData: ISocketFunctionCall; funcCallData: ISocketFunctionCall;
shortId: string; shortId: string;
responseTimeout?: number; responseTimeout?: number;
}; }
//export objects
export let allSocketRequests = new Objectmap<SocketRequest>();
// export classes // export classes
export class SocketRequest { export class SocketRequest {
status: TSocketRequestStatus = "new"; // STATIC
side: TSocketRequestSide; public static getSocketRequestById(
shortid: string; smartsocketRef: Smartsocket | SmartsocketClient,
originSocketConnection: SocketConnection; shortIdArg: string
funcCallData: ISocketFunctionCall ): SocketRequest {
done = plugins.q.defer(); return smartsocketRef.socketRequests.find(socketRequestArg => {
constructor(optionsArg: SocketRequestConstructorOptions) { return socketRequestArg.shortid === shortIdArg;
});
}
// INSTANCE
public status: TSocketRequestStatus = 'new';
public side: TSocketRequestSide;
public shortid: string;
public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall;
public done = plugins.smartpromise.defer<ISocketFunctionCall>();
public smartsocketRef: Smartsocket | SmartsocketClient;
constructor(
smartsocketRefArg: Smartsocket | SmartsocketClient,
optionsArg: ISocketRequestConstructorOptions
) {
this.smartsocketRef = smartsocketRefArg;
this.side = optionsArg.side; this.side = optionsArg.side;
this.shortid = optionsArg.shortId; this.shortid = optionsArg.shortId;
this.funcCallData = optionsArg.funcCallData; this.funcCallData = optionsArg.funcCallData;
allSocketRequests.add(this); this.originSocketConnection = optionsArg.originSocketConnection;
}; this.smartsocketRef.socketRequests.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(): Promise<ISocketFunctionCall> {
let requestData: ISocketRequestDataObject = { const 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) {
this.done.resolve(responseDataArg); plugins.smartlog.defaultLogger.log('info', 'handling response!');
allSocketRequests.remove(this); this.done.resolve(responseDataArg.funcCallData);
this.smartsocketRef.socketRequests.remove(this);
} }
// responding -------------------------- // responding --------------------------
@ -77,16 +95,29 @@ 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 = SocketFunction.getSocketFunctionByName(
targetSocketFunction.invoke(this.funcCallData) this.smartsocketRef,
.then((resultData) => { this.funcCallData.funcName
let requestData: ISocketRequestDataObject = { );
if (!targetSocketFunction) {
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.');
const 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); this.smartsocketRef.socketRequests.remove(this);
}); });
} }
}; }

View File

@ -1,17 +1,16 @@
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';
import { Smartsocket } from './smartsocket.classes.smartsocket';
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
export let allSocketRoles = new Objectmap<SocketRole>(); import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.socketconnection';
/** /**
* interface for class SocketRole * interface for class SocketRole
*/ */
export interface SocketRoleOptions { export interface ISocketRoleOptions {
name: string; name: string;
passwordHash: string; passwordHash: string;
} }
@ -20,15 +19,40 @@ export interface SocketRoleOptions {
* A socketrole defines access to certain routines. * A socketrole defines access to certain routines.
*/ */
export class SocketRole { export class SocketRole {
name: string; // STATIC
passwordHash: string; public static getSocketRoleByName(
allowedFunctions = new Objectmap<SocketFunction>(); referenceSmartsocket: Smartsocket | SmartsocketClient,
constructor(optionsArg: SocketRoleOptions) { socketRoleNameArg: string
): SocketRole {
return referenceSmartsocket.socketRoles.find(socketRoleArg => {
return socketRoleArg.name === socketRoleNameArg;
});
}
public static checkPasswordForRole(
dataArg: ISocketConnectionAuthenticationObject,
referenceSmartsocket: Smartsocket | SmartsocketClient
): boolean {
const targetPasswordHash = SocketRole.getSocketRoleByName(referenceSmartsocket, dataArg.role)
.passwordHash;
const computedCompareHash = plugins.smarthash.sha256FromStringSync(dataArg.password);
return targetPasswordHash === computedCompareHash;
}
// INSTANCE
public name: string;
public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: ISocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
allSocketRoles.add(this); }
};
addSocketFunction(socketFunctionArg:SocketFunction){ /**
* adds the socketfunction to the socketrole
* @param socketFunctionArg
*/
public 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) {
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, () => {
plugins.smartlog.defaultLogger.log('success', `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 +0,0 @@
import * as plugins from "./smartsocket.plugins";
// classes
import { Smartsocket } from "./smartsocket.classes.smartsocket";
import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketRequest, allSocketRequests, TSocketRequestSide } from "./smartsocket.classes.socketrequest";
import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole";
// SocketFunction helpers
export let getSocketFunctionByName = (functionNameArg: string):SocketFunction => {
return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg });
}
// SocketRequest helpers
/**
* get corresponding Socketrequest instance by shortId
*/
export let getSocketRequestById = (shortIdArg:string,requestSide?:TSocketRequestSide):SocketRequest => {
return allSocketRequests.find((socketRequestArg) => {return socketRequestArg.shortid === shortIdArg})
}
// SocketRole helpers
/**
* get corresponding SocketRole instance by name
*/
export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => {
return allSocketRoles.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"
}