Compare commits
127 Commits
Author | SHA1 | Date | |
---|---|---|---|
c901ab75d3 | |||
075c59ed2c | |||
8d358dd93d | |||
e576d6058a | |||
6546da2394 | |||
29597daba1 | |||
c8b647d2fd | |||
1fa4aca049 | |||
2d4695de22 | |||
a3f4dcbc2a | |||
d6e81288b4 | |||
c441d89596 | |||
85b69e487a | |||
0d94ed9345 | |||
e188b18016 | |||
33c0fa3746 | |||
52be1415ee | |||
e75d5eabdb | |||
fb99848df1 | |||
1bd39d0755 | |||
d24c5f628c | |||
33fbabdd72 | |||
9d26fff986 | |||
41151e5436 | |||
109c5197f3 | |||
c3d47c1f50 | |||
1b45fb45e9 | |||
c22adc9b57 | |||
35e87306dd | |||
1b20aa5992 | |||
a63f14da47 | |||
86cf94c5dc | |||
0d39d6f237 | |||
1856ad5cfc | |||
b5dab810e8 | |||
f145b85b1c | |||
a8c647c5c7 | |||
22472e0adf | |||
333ddb4d9c | |||
126f020bf8 | |||
7677b1bb48 | |||
114379c5b3 | |||
79ae7d2039 | |||
d2cb6a3045 | |||
963619b2ed | |||
976abaaed7 | |||
ef322c32ac | |||
475911a968 | |||
5530e29da4 | |||
5f1d2c83c6 | |||
b84dd36885 | |||
c2d64ba39a | |||
85a5d7cdaf | |||
69380a2dbc | |||
fc292d83b9 | |||
b29961df95 | |||
f774a82ad5 | |||
5a45f4750b | |||
fc6227ef96 | |||
873aba3e0b | |||
f9bed9da8a | |||
d296064bfa | |||
1b0922759a | |||
aef4f9c591 | |||
abf546eec3 | |||
483cb38175 | |||
b2bd66a6a6 | |||
cfc3e8788d | |||
23c14c032c | |||
c92dae809f | |||
7200464e4b | |||
eaa97f0598 | |||
cde8270313 | |||
1b073ac8f6 | |||
3f3857df1e | |||
02e17a52c3 | |||
287c2a73c5 | |||
dea3746bb0 | |||
fc2a55bb0d | |||
99eca912d3 | |||
0b6929af8f | |||
0aa073f2a7 | |||
7170e58457 | |||
cccee35f2c | |||
118ec84ec5 | |||
dd31eea263 | |||
477429143d | |||
ea6fa14133 | |||
02065e36a6 | |||
f34358db81 | |||
fddf54b2b1 | |||
e1dbdc1a15 | |||
f3031a1d4e | |||
47ca497d48 | |||
c7e3c4537b | |||
62b36ab859 | |||
4563aade16 | |||
47596bfa59 | |||
6543d60019 | |||
f0aba45cec | |||
f327fa2784 | |||
983b4672fd | |||
c81a41b365 | |||
abe4d22226 | |||
87cc238345 | |||
475cd12904 | |||
84f33fa447 | |||
180ae23c9a | |||
7f0fff0bf4 | |||
24310b6709 | |||
bd8697ac6e | |||
7b2b2bd151 | |||
8dfc39be75 | |||
ab2196fd5f | |||
3afede95fc | |||
da510eb87a | |||
cb3d7f4d7b | |||
19a883c641 | |||
1f8e1fc7cb | |||
5109964247 | |||
f670cae1f8 | |||
fee54dfb95 | |||
8d58e0b2f3 | |||
fc530ba37e | |||
62abfda156 | |||
24a8795642 | |||
2b326937ff |
23
.gitignore
vendored
23
.gitignore
vendored
@ -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
|
111
.gitlab-ci.yml
111
.gitlab-ci.yml
@ -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:
|
||||||
|
- 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:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
artifacts:
|
||||||
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
13
.snyk
Normal file
13
.snyk
Normal 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
29
.vscode/launch.json
vendored
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
119
README.md
119
README.md
@ -1,62 +1,111 @@
|
|||||||
# smartsocket
|
# @pushrocks/smartsocket
|
||||||
easy and secure websocket communication
|
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
|
||||||
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
||||||
|
[](https://gitlab.com/pushrocks/smartsocket/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/@pushrocks/smartsocket)
|
||||||
|
[](https://snyk.io/test/npm/@pushrocks/smartsocket)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](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.
|
||||||
|
|
||||||
### Serverside
|
### Serverside
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import * as smartsocket from "smartsocket";
|
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();
|
||||||
roles: [mySocketRole] // all roles that have access to a specific function
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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:**
|
||||||
|
> 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.
|
||||||
|
> 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)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
||||||
|
2
dist/index.d.ts
vendored
2
dist/index.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
export * from "./smartsocket.classes.smartsocket";
|
|
||||||
export * from "./smartsocket.classes.smartsocketclient";
|
|
7
dist/index.js
vendored
7
dist/index.js
vendored
@ -1,7 +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"));
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsaUJBQWMsbUNBQW1DLENBQUMsRUFBQTtBQUNsRCxpQkFBYyx5Q0FBeUMsQ0FBQyxFQUFBIn0=
|
|
29
dist/smartsocket.classes.smartsocket.d.ts
vendored
29
dist/smartsocket.classes.smartsocket.d.ts
vendored
@ -1,29 +0,0 @@
|
|||||||
/// <reference types="socket.io" />
|
|
||||||
import { Objectmap } from "lik";
|
|
||||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
|
||||||
export interface ISocketObject {
|
|
||||||
alias?: string;
|
|
||||||
authenticated: boolean;
|
|
||||||
role?: string;
|
|
||||||
socket: SocketIO.Socket;
|
|
||||||
}
|
|
||||||
export interface ISmartsocketConstructorOptions {
|
|
||||||
port: number;
|
|
||||||
}
|
|
||||||
export declare class Smartsocket {
|
|
||||||
options: ISmartsocketConstructorOptions;
|
|
||||||
io: SocketIO.Server;
|
|
||||||
openSockets: Objectmap;
|
|
||||||
registeredRoles: Objectmap;
|
|
||||||
constructor(optionsArg: ISmartsocketConstructorOptions);
|
|
||||||
/**
|
|
||||||
* the standard handler for new socket connections
|
|
||||||
*/
|
|
||||||
private _handleSocket(socket);
|
|
||||||
registerFunctions(socketRoleArg: SocketRole): void;
|
|
||||||
/**
|
|
||||||
* starts listening to incling sockets:
|
|
||||||
*/
|
|
||||||
startServer: () => void;
|
|
||||||
closeServer: () => void;
|
|
||||||
}
|
|
51
dist/smartsocket.classes.smartsocket.js
vendored
51
dist/smartsocket.classes.smartsocket.js
vendored
@ -1,51 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartsocket.plugins");
|
|
||||||
const helpers = require("./smartsocket.helpers");
|
|
||||||
// classes
|
|
||||||
const lik_1 = require("lik");
|
|
||||||
;
|
|
||||||
;
|
|
||||||
class Smartsocket {
|
|
||||||
constructor(optionsArg) {
|
|
||||||
this.openSockets = new lik_1.Objectmap();
|
|
||||||
this.registeredRoles = new lik_1.Objectmap();
|
|
||||||
/**
|
|
||||||
* starts listening to incling sockets:
|
|
||||||
*/
|
|
||||||
this.startServer = () => {
|
|
||||||
this.io = plugins.socketIo(this.options.port);
|
|
||||||
this.io.on('connection', (socketArg) => {
|
|
||||||
this._handleSocket(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
|
|
||||||
*/
|
|
||||||
_handleSocket(socket) {
|
|
||||||
let socketObject = {
|
|
||||||
socket: socket,
|
|
||||||
authenticated: false
|
|
||||||
};
|
|
||||||
plugins.beautylog.log("Socket connected. Trying to authenticate...");
|
|
||||||
this.openSockets.add(socketObject);
|
|
||||||
helpers.authenticateSocket(socketObject)
|
|
||||||
.then();
|
|
||||||
}
|
|
||||||
registerFunctions(socketRoleArg) {
|
|
||||||
this.registeredRoles.add(socketRoleArg);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
}
|
|
||||||
exports.Smartsocket = Smartsocket;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFDakQsTUFBWSxPQUFPLFdBQU0sdUJBQXVCLENBQUMsQ0FBQTtBQUVqRCxVQUFVO0FBQ1Ysc0JBQTBCLEtBQUssQ0FBQyxDQUFBO0FBVS9CLENBQUM7QUFLRCxDQUFDO0FBRUY7SUFLSSxZQUFZLFVBQTBDO1FBRnRELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQUUsQ0FBQztRQUM5QixvQkFBZSxHQUFHLElBQUksZUFBUyxFQUFFLENBQUM7UUF3QmxDOztXQUVHO1FBRUgsZ0JBQVcsR0FBRztZQUNWLElBQUksQ0FBQyxFQUFFLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzlDLElBQUksQ0FBQyxFQUFFLENBQUMsRUFBRSxDQUFDLFlBQVksRUFBRSxDQUFDLFNBQVM7Z0JBQy9CLElBQUksQ0FBQyxhQUFhLENBQUMsU0FBUyxDQUFDLENBQUM7WUFDbEMsQ0FBQyxDQUFDLENBQUM7UUFDUCxDQUFDLENBQUE7UUFDRCxnQkFBVyxHQUFHO1lBQ1YsSUFBSSxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsQ0FBQyxlQUE4QjtnQkFDcEQsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsa0NBQWtDLGVBQWUsQ0FBQyxLQUFLLEVBQUUsQ0FBQyxDQUFDO2dCQUNqRixlQUFlLENBQUMsTUFBTSxDQUFDLFVBQVUsRUFBRSxDQUFDO1lBQ3hDLENBQUMsQ0FBQyxDQUFDO1lBQ0gsSUFBSSxDQUFDLFdBQVcsQ0FBQyxJQUFJLEVBQUUsQ0FBQztZQUN4QixJQUFJLENBQUMsRUFBRSxDQUFDLEtBQUssRUFBRSxDQUFDO1FBQ3BCLENBQUMsQ0FBQTtRQXZDRyxJQUFJLENBQUMsT0FBTyxHQUFHLFVBQVUsQ0FBQztJQUM5QixDQUFDOztJQUVEOztPQUVHO0lBQ0ssYUFBYSxDQUFDLE1BQU07UUFDeEIsSUFBSSxZQUFZLEdBQWtCO1lBQzlCLE1BQU0sRUFBRSxNQUFNO1lBQ2QsYUFBYSxFQUFFLEtBQUs7U0FDdkIsQ0FBQztRQUNGLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLDZDQUE2QyxDQUFDLENBQUE7UUFDcEUsSUFBSSxDQUFDLFdBQVcsQ0FBQyxHQUFHLENBQUMsWUFBWSxDQUFDLENBQUM7UUFDbkMsT0FBTyxDQUFDLGtCQUFrQixDQUFDLFlBQVksQ0FBQzthQUNuQyxJQUFJLEVBQUUsQ0FBQztJQUNoQixDQUFDO0lBRUQsaUJBQWlCLENBQUMsYUFBd0I7UUFDdEMsSUFBSSxDQUFDLGVBQWUsQ0FBQyxHQUFHLENBQUMsYUFBYSxDQUFDLENBQUM7SUFDNUMsQ0FBQzs7QUFxQkwsQ0FBQztBQTlDWSxtQkFBVyxjQThDdkIsQ0FBQSJ9
|
|
10
dist/smartsocket.classes.smartsocketclient.d.ts
vendored
10
dist/smartsocket.classes.smartsocketclient.d.ts
vendored
@ -1,10 +0,0 @@
|
|||||||
/**
|
|
||||||
* interface for class SmartsocketClient
|
|
||||||
*/
|
|
||||||
export interface ISmartsocketClientOptions {
|
|
||||||
port: number;
|
|
||||||
url: string;
|
|
||||||
}
|
|
||||||
export declare class SmartsocketClient {
|
|
||||||
constructor();
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
class SmartsocketClient {
|
|
||||||
constructor() {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.SmartsocketClient = SmartsocketClient;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldGNsaWVudC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXRjbGllbnQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQVVBO0lBQ0k7SUFFQSxDQUFDO0FBQ0wsQ0FBQztBQUpZLHlCQUFpQixvQkFJN0IsQ0FBQSJ9
|
|
12
dist/smartsocket.classes.socketfunction.d.ts
vendored
12
dist/smartsocket.classes.socketfunction.d.ts
vendored
@ -1,12 +0,0 @@
|
|||||||
import { SocketRole } from "./smartsocket.classes.socketrole";
|
|
||||||
export interface SocketFunctionOptions {
|
|
||||||
name: string;
|
|
||||||
func: any;
|
|
||||||
roles: SocketRole[];
|
|
||||||
}
|
|
||||||
export declare class SocketFunction {
|
|
||||||
name: string;
|
|
||||||
func: any;
|
|
||||||
roles: SocketRole[];
|
|
||||||
constructor(optionsArg: SocketFunctionOptions);
|
|
||||||
}
|
|
12
dist/smartsocket.classes.socketfunction.js
vendored
12
dist/smartsocket.classes.socketfunction.js
vendored
@ -1,12 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
;
|
|
||||||
class SocketFunction {
|
|
||||||
constructor(optionsArg) {
|
|
||||||
this.name = optionsArg.name;
|
|
||||||
this.func = optionsArg.func;
|
|
||||||
this.roles = optionsArg.roles;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
}
|
|
||||||
exports.SocketFunction = SocketFunction;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRmdW5jdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc29ja2V0ZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQVlDLENBQUM7QUFFRjtJQUlJLFlBQVksVUFBaUM7UUFDekMsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsSUFBSSxDQUFDO1FBQzVCLElBQUksQ0FBQyxJQUFJLEdBQUcsVUFBVSxDQUFDLElBQUksQ0FBQztRQUM1QixJQUFJLENBQUMsS0FBSyxHQUFHLFVBQVUsQ0FBQyxLQUFLLENBQUM7SUFDbEMsQ0FBQzs7QUFDTCxDQUFDO0FBVFksc0JBQWMsaUJBUzFCLENBQUEifQ==
|
|
15
dist/smartsocket.classes.socketrole.d.ts
vendored
15
dist/smartsocket.classes.socketrole.d.ts
vendored
@ -1,15 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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;
|
|
||||||
constructor(optionsArg: SocketRoleOptions);
|
|
||||||
}
|
|
12
dist/smartsocket.classes.socketrole.js
vendored
12
dist/smartsocket.classes.socketrole.js
vendored
@ -1,12 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
/**
|
|
||||||
* A socketrole defines access to certain routines.
|
|
||||||
*/
|
|
||||||
class SocketRole {
|
|
||||||
constructor(optionsArg) {
|
|
||||||
this.name = optionsArg.name;
|
|
||||||
this.passwordHash = optionsArg.passwordHash;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.SocketRole = SocketRole;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzb2NrZXQuY2xhc3Nlcy5zb2NrZXRyb2xlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFjQTs7R0FFRztBQUNIO0lBR0ksWUFBWSxVQUE0QjtRQUNwQyxJQUFJLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQyxJQUFJLENBQUM7UUFDNUIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO0lBQ2hELENBQUM7QUFDTCxDQUFDO0FBUFksa0JBQVUsYUFPdEIsQ0FBQSJ9
|
|
7
dist/smartsocket.helpers.d.ts
vendored
7
dist/smartsocket.helpers.d.ts
vendored
@ -1,7 +0,0 @@
|
|||||||
/// <reference types="q" />
|
|
||||||
import * as plugins from "./smartsocket.plugins";
|
|
||||||
import { ISocketObject } from "./smartsocket.classes.smartsocket";
|
|
||||||
/**
|
|
||||||
* authenticate a socket
|
|
||||||
*/
|
|
||||||
export declare let authenticateSocket: (socketObjectArg: ISocketObject) => plugins.q.Promise<{}>;
|
|
28
dist/smartsocket.helpers.js
vendored
28
dist/smartsocket.helpers.js
vendored
@ -1,28 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartsocket.plugins");
|
|
||||||
/**
|
|
||||||
* authenticate a socket
|
|
||||||
*/
|
|
||||||
exports.authenticateSocket = (socketObjectArg) => {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
socketObjectArg.socket.on("dataAuth", dataArg => {
|
|
||||||
plugins.beautylog.log("received authentication data. now hashing and comparing...");
|
|
||||||
socketObjectArg.socket.removeListener("dataAuth", () => { });
|
|
||||||
if ((true)) {
|
|
||||||
socketObjectArg.alias = dataArg.alias;
|
|
||||||
socketObjectArg.authenticated = true;
|
|
||||||
socketObjectArg.role = dataArg.role;
|
|
||||||
socketObjectArg.socket.emit("authenticated");
|
|
||||||
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`);
|
|
||||||
done.resolve(socketObjectArg);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
socketObjectArg.socket.disconnect();
|
|
||||||
done.reject("not authenticated");
|
|
||||||
}
|
|
||||||
;
|
|
||||||
});
|
|
||||||
socketObjectArg.socket.emit("requestAuth");
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFRakQ7O0dBRUc7QUFDUSwwQkFBa0IsR0FBRyxDQUFDLGVBQThCO0lBQzNELElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUM7SUFDN0IsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxFQUFFLE9BQU87UUFDekMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsNERBQTRELENBQUMsQ0FBQztRQUNwRixlQUFlLENBQUMsTUFBTSxDQUFDLGNBQWMsQ0FBQyxVQUFVLEVBQUUsUUFBUSxDQUFDLENBQUMsQ0FBQztRQUM3RCxFQUFFLENBQUEsQ0FBQyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUEsQ0FBQztZQUNQLGVBQWUsQ0FBQyxLQUFLLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQTtZQUNyQyxlQUFlLENBQUMsYUFBYSxHQUFHLElBQUksQ0FBQztZQUNyQyxlQUFlLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUM7WUFDcEMsZUFBZSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUM7WUFDN0MsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsdUJBQXVCLGVBQWUsQ0FBQyxLQUFLLFdBQVcsZUFBZSxDQUFDLElBQUksb0JBQW9CLENBQUMsQ0FBQTtZQUNySCxJQUFJLENBQUMsT0FBTyxDQUFDLGVBQWUsQ0FBQyxDQUFDO1FBQ2xDLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLGVBQWUsQ0FBQyxNQUFNLENBQUMsVUFBVSxFQUFFLENBQUM7WUFDcEMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxtQkFBbUIsQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFBQSxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7SUFDSCxlQUFlLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQztJQUMzQyxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUMifQ==
|
|
7
dist/smartsocket.plugins.d.ts
vendored
7
dist/smartsocket.plugins.d.ts
vendored
@ -1,7 +0,0 @@
|
|||||||
import "typings-global";
|
|
||||||
export import beautylog = require("beautylog");
|
|
||||||
export import lik = require("lik");
|
|
||||||
export import q = require("q");
|
|
||||||
export import socketIo = require("socket.io");
|
|
||||||
export import socketIoClient = require("socket.io-client");
|
|
||||||
export import taskbuffer = require("taskbuffer");
|
|
9
dist/smartsocket.plugins.js
vendored
9
dist/smartsocket.plugins.js
vendored
@ -1,9 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
exports.beautylog = require("beautylog");
|
|
||||||
exports.lik = require("lik");
|
|
||||||
exports.q = require("q");
|
|
||||||
exports.socketIo = require("socket.io");
|
|
||||||
exports.socketIoClient = require("socket.io-client");
|
|
||||||
exports.taskbuffer = require("taskbuffer");
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsV0FBRyxXQUFZLEtBQUssQ0FBQyxDQUFDO0FBQ3RCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNqQixnQkFBUSxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2hDLHNCQUFjLFdBQVcsa0JBQWtCLENBQUMsQ0FBQztBQUM3QyxrQkFBVSxXQUFXLFlBQVksQ0FBQyxDQUFDIn0=
|
|
17
npmextra.json
Normal file
17
npmextra.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"gitzone": {
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
}
|
3224
package-lock.json
generated
Normal file
3224
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
52
package.json
52
package.json
@ -1,11 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "smartsocket",
|
"name": "@pushrocks/smartsocket",
|
||||||
"version": "1.0.2",
|
"version": "1.1.50",
|
||||||
"description": "easy and secure websocket communication",
|
"description": "easy and secure websocket communication",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)"
|
"test": "tstest test/",
|
||||||
|
"build": "tsbuild"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -18,18 +19,39 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartsocket#README",
|
"homepage": "https://gitlab.com/pushrocks/smartsocket#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/q": "0.x.x",
|
"@apiglobal/typedrequest-interfaces": "^1.0.7",
|
||||||
"@types/socket.io": "^1.4.26",
|
"@pushrocks/lik": "^3.0.11",
|
||||||
"@types/socket.io-client": "^1.4.26",
|
"@pushrocks/smartdelay": "^2.0.3",
|
||||||
"beautylog": "^5.0.20",
|
"@pushrocks/smartexpress": "^3.0.40",
|
||||||
"lik": "^1.0.10",
|
"@pushrocks/smarthash": "^2.0.6",
|
||||||
"q": "^1.4.1",
|
"@pushrocks/smartlog": "^2.0.19",
|
||||||
"socket.io": "^1.4.8",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"socket.io-client": "^1.4.8",
|
"@types/shortid": "0.0.29",
|
||||||
"taskbuffer": "^1.0.7"
|
"@types/socket.io": "^2.1.2",
|
||||||
|
"@types/socket.io-client": "^1.4.32",
|
||||||
|
"shortid": "^2.2.15",
|
||||||
|
"socket.io": "^2.2.0",
|
||||||
|
"socket.io-client": "^2.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"should": "^10.0.0",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"typings-test": "^1.0.1"
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
}
|
"@gitzone/tstest": "^1.0.24",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
|
"@types/node": "^12.7.4",
|
||||||
|
"tslint": "^5.19.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
1
test/common/test.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
console.log('TODO');
|
2
test/test.d.ts
vendored
2
test/test.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
import "typings-test";
|
|
||||||
import "should";
|
|
104
test/test.expressserver.ts
Normal file
104
test/test.expressserver.ts
Normal 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<any>;
|
||||||
|
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();
|
37
test/test.js
37
test/test.js
@ -1,37 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
require("should");
|
|
||||||
const socketIoClient = require("socket.io-client");
|
|
||||||
const smartsocket = require("../dist/index");
|
|
||||||
let testSmartsocket;
|
|
||||||
describe("smartsocket", function () {
|
|
||||||
it("should create a new smartsocket", function () {
|
|
||||||
testSmartsocket = new smartsocket.Smartsocket({ port: 3000 });
|
|
||||||
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
|
|
||||||
});
|
|
||||||
it("should register a new Function", function () {
|
|
||||||
});
|
|
||||||
it("should start listening when .started is called", function () {
|
|
||||||
testSmartsocket.startServer();
|
|
||||||
});
|
|
||||||
it("should react to a new websocket connection", function (done) {
|
|
||||||
this.timeout(10000);
|
|
||||||
let socket = socketIoClient("http://localhost:3000", {});
|
|
||||||
socket.on("requestAuth", function () {
|
|
||||||
console.log("server requested authentication");
|
|
||||||
socket.emit("dataAuth", {
|
|
||||||
role: "coreflowContainer",
|
|
||||||
password: "somePassword",
|
|
||||||
alias: "coreflow1"
|
|
||||||
});
|
|
||||||
socket.on("authenticated", () => {
|
|
||||||
console.log("client is authenticated");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should close the server", function () {
|
|
||||||
testSmartsocket.closeServer();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUNoQixNQUFPLGNBQWMsV0FBVyxrQkFBa0IsQ0FBQyxDQUFDO0FBQ3BELE1BQU8sV0FBVyxXQUFXLGVBQWUsQ0FBQyxDQUFDO0FBRTlDLElBQUksZUFBd0MsQ0FBQztBQUU3QyxRQUFRLENBQUMsYUFBYSxFQUFFO0lBQ3BCLEVBQUUsQ0FBQyxpQ0FBaUMsRUFBRTtRQUNsQyxlQUFlLEdBQUcsSUFBSSxXQUFXLENBQUMsV0FBVyxDQUFDLEVBQUUsSUFBSSxFQUFFLElBQUksRUFBRSxDQUFDLENBQUM7UUFDOUQsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLFdBQVcsQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUNsRSxDQUFDLENBQUMsQ0FBQztJQUNILEVBQUUsQ0FBQyxnQ0FBZ0MsRUFBQztJQUVwQyxDQUFDLENBQUMsQ0FBQTtJQUNGLEVBQUUsQ0FBQyxnREFBZ0QsRUFBQztRQUNoRCxlQUFlLENBQUMsV0FBVyxFQUFFLENBQUM7SUFDbEMsQ0FBQyxDQUFDLENBQUE7SUFDRixFQUFFLENBQUMsNENBQTRDLEVBQUUsVUFBVSxJQUFJO1FBQzNELElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUM7UUFDcEIsSUFBSSxNQUFNLEdBQUcsY0FBYyxDQUFDLHVCQUF1QixFQUFFLEVBQUUsQ0FBQyxDQUFDO1FBQ3pELE1BQU0sQ0FBQyxFQUFFLENBQUMsYUFBYSxFQUFFO1lBQ3JCLE9BQU8sQ0FBQyxHQUFHLENBQUMsaUNBQWlDLENBQUMsQ0FBQztZQUMvQyxNQUFNLENBQUMsSUFBSSxDQUFDLFVBQVUsRUFBRTtnQkFDcEIsSUFBSSxFQUFFLG1CQUFtQjtnQkFDekIsUUFBUSxFQUFFLGNBQWM7Z0JBQ3hCLEtBQUssRUFBRSxXQUFXO2FBQ3JCLENBQUMsQ0FBQztZQUNILE1BQU0sQ0FBQyxFQUFFLENBQUMsZUFBZSxFQUFDO2dCQUN0QixPQUFPLENBQUMsR0FBRyxDQUFDLHlCQUF5QixDQUFDLENBQUM7Z0JBQ3ZDLElBQUksRUFBRSxDQUFDO1lBQ1gsQ0FBQyxDQUFDLENBQUM7UUFDUCxDQUFDLENBQUMsQ0FBQztJQUNQLENBQUMsQ0FBQyxDQUFDO0lBQ0gsRUFBRSxDQUFDLHlCQUF5QixFQUFDO1FBQ3pCLGVBQWUsQ0FBQyxXQUFXLEVBQUUsQ0FBQztJQUNsQyxDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFDIn0=
|
|
163
test/test.ts
163
test/test.ts
@ -1,38 +1,133 @@
|
|||||||
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 * 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 testSocketConnection: smartsocket.SocketConnection;
|
||||||
|
let testSocketRole1: smartsocket.SocketRole;
|
||||||
|
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
|
||||||
|
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
|
||||||
|
|
||||||
describe("smartsocket", function () {
|
export interface IReqResClient {
|
||||||
it("should create a new smartsocket", function () {
|
method: 'testFunction1';
|
||||||
testSmartsocket = new smartsocket.Smartsocket({ port: 3000 });
|
request: {
|
||||||
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
|
value1: string;
|
||||||
});
|
};
|
||||||
it("should register a new Function",function(){
|
response: {
|
||||||
|
value1: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IReqResServer {
|
||||||
|
method: 'testFunction2';
|
||||||
|
request: {
|
||||||
|
hi: string;
|
||||||
|
};
|
||||||
|
response: {
|
||||||
|
hi: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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 () => {
|
||||||
|
testSocketFunctionForServer = new smartsocket.SocketFunction({
|
||||||
|
allowedRoles: [testSocketRole1],
|
||||||
|
funcDef: async (dataArg, socketConnectionArg) => {
|
||||||
|
return dataArg;
|
||||||
|
},
|
||||||
|
funcName: 'testFunction1'
|
||||||
|
});
|
||||||
|
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||||
|
|
||||||
|
testSocketFunctionClient = new smartsocket.SocketFunction({
|
||||||
|
allowedRoles: [],
|
||||||
|
funcDef: async (dataArg, socketConnectionArg) => {
|
||||||
|
return dataArg;
|
||||||
|
},
|
||||||
|
funcName: 'testFunction2'
|
||||||
|
});
|
||||||
|
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
|
||||||
|
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(testSocketFunctionClient);
|
||||||
|
console.log(testSmartsocketClient.socketFunctions);
|
||||||
|
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<IReqResClient>('testFunction1', {
|
||||||
|
value1: 'hello'
|
||||||
|
});
|
||||||
|
console.log(response);
|
||||||
|
expect(response.value1).to.equal('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to make a functionCall from server to client', async () => {
|
||||||
|
const response = await testSmartsocket.clientCall<IReqResServer>(
|
||||||
|
'testFunction2',
|
||||||
|
{
|
||||||
|
hi: 'hi there from server'
|
||||||
|
},
|
||||||
|
testSmartsocket.socketConnections.find(socketConnection => {
|
||||||
|
return true;
|
||||||
})
|
})
|
||||||
it("should start listening when .started is called",function(){
|
);
|
||||||
testSmartsocket.startServer();
|
console.log(response);
|
||||||
})
|
expect(response.hi).to.equal('hi there from server');
|
||||||
it("should react to a new websocket connection", function (done) {
|
});
|
||||||
this.timeout(10000);
|
|
||||||
let socket = socketIoClient("http://localhost:3000", {});
|
tap.test('client should disconnect and reconnect', async tools => {
|
||||||
socket.on("requestAuth", function () {
|
await testSmartsocketClient.disconnect();
|
||||||
console.log("server requested authentication");
|
await tools.delayFor(100);
|
||||||
socket.emit("dataAuth", {
|
await testSmartsocketClient.connect();
|
||||||
role: "coreflowContainer",
|
});
|
||||||
password: "somePassword",
|
|
||||||
alias: "coreflow1"
|
// terminate
|
||||||
});
|
tap.test('should close the server', async () => {
|
||||||
socket.on("authenticated",() => {
|
await testSmartsocket.stop();
|
||||||
console.log("client is authenticated");
|
});
|
||||||
done();
|
|
||||||
});
|
tap.start();
|
||||||
});
|
|
||||||
});
|
|
||||||
it("should close the server",function(){
|
|
||||||
testSmartsocket.closeServer();
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
10
ts/index.ts
10
ts/index.ts
@ -1,4 +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.socketrole';
|
||||||
|
export * from './smartsocket.classes.socketconnection';
|
||||||
|
@ -1,68 +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 { ISocketFunctionCallDataRequest, SocketFunction, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
|
||||||
|
import { SocketRequest } from './smartsocket.classes.socketrequest';
|
||||||
|
import { SocketRole } from './smartsocket.classes.socketrole';
|
||||||
|
import { SocketServer } from './smartsocket.classes.socketserver';
|
||||||
|
|
||||||
|
// socket.io
|
||||||
export interface ISocketObject {
|
import * as SocketIO from 'socket.io';
|
||||||
alias?:string;
|
|
||||||
authenticated: boolean
|
|
||||||
role?:string,
|
|
||||||
socket: SocketIO.Socket,
|
|
||||||
};
|
|
||||||
|
|
||||||
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();
|
public socketConnections = new Objectmap<SocketConnection>();
|
||||||
registeredRoles = new Objectmap();
|
public socketRoles = new Objectmap<SocketRole>();
|
||||||
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
public socketFunctions = new Objectmap<SocketFunction<any>>();
|
||||||
this.options = optionsArg;
|
public socketRequests = new Objectmap<SocketRequest<any>>();
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
private socketServer = new SocketServer(this);
|
||||||
* the standard handler for new socket connections
|
|
||||||
*/
|
constructor(optionsArg: ISmartsocketConstructorOptions) {
|
||||||
private _handleSocket(socket) {
|
this.options = optionsArg;
|
||||||
let socketObject: ISocketObject = {
|
}
|
||||||
socket: socket,
|
|
||||||
authenticated: false
|
// tslint:disable-next-line:member-ordering
|
||||||
};
|
public async setExternalServer(serverType: 'smartexpress', serverArg: any) {
|
||||||
plugins.beautylog.log("Socket connected. Trying to authenticate...")
|
await this.socketServer.setExternalServer(serverType, serverArg);
|
||||||
this.openSockets.add(socketObject);
|
}
|
||||||
helpers.authenticateSocket(socketObject)
|
|
||||||
.then();
|
/**
|
||||||
|
* 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<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||||
|
functionNameArg: T['method'],
|
||||||
|
dataArg: T['request'],
|
||||||
|
targetSocketConnectionArg: SocketConnection
|
||||||
|
): Promise<T['response']> {
|
||||||
|
const socketRequest = new SocketRequest<T>(this, {
|
||||||
|
funcCallData: {
|
||||||
|
funcDataArg: dataArg,
|
||||||
|
funcName: functionNameArg
|
||||||
|
},
|
||||||
|
originSocketConnection: targetSocketConnectionArg,
|
||||||
|
shortId: plugins.shortid.generate(),
|
||||||
|
side: 'requesting'
|
||||||
|
});
|
||||||
|
const response: ISocketFunctionCallDataResponse<T> = 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;
|
||||||
|
}
|
||||||
|
|
||||||
registerFunctions(socketRoleArg:SocketRole){
|
public addSocketFunction(socketFunction: SocketFunction<any>) {
|
||||||
this.registeredRoles.add(socketRoleArg);
|
this.socketFunctions.add(socketFunction);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* the standard handler for new socket connections
|
||||||
* starts listening to incling sockets:
|
*/
|
||||||
*/
|
private async _handleSocketConnection(socketArg: plugins.socketIo.Socket) {
|
||||||
|
const socketConnection: SocketConnection = new SocketConnection({
|
||||||
startServer = () => {
|
alias: undefined,
|
||||||
this.io = plugins.socketIo(this.options.port);
|
authenticated: false,
|
||||||
this.io.on('connection', (socketArg) => {
|
role: undefined,
|
||||||
this._handleSocket(socketArg);
|
side: 'server',
|
||||||
});
|
smartsocketHost: this,
|
||||||
}
|
socket: socketArg
|
||||||
closeServer = () => {
|
});
|
||||||
this.openSockets.forEach((socketObjectArg: ISocketObject) => {
|
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
|
||||||
plugins.beautylog.log(`disconnect socket with >>alias ${socketObjectArg.alias}`);
|
this.socketConnections.add(socketConnection);
|
||||||
socketObjectArg.socket.disconnect();
|
await socketConnection.authenticate();
|
||||||
});
|
await socketConnection.listenToFunctionRequests();
|
||||||
this.openSockets.wipe();
|
}
|
||||||
this.io.close();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,15 +1,123 @@
|
|||||||
import * as plugins from "./smartsocket.plugins"
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
|
import { SocketConnection } from './smartsocket.classes.socketconnection';
|
||||||
|
import { ISocketFunctionCallDataRequest, SocketFunction } from './smartsocket.classes.socketfunction';
|
||||||
|
import { ISocketRequestDataObject, SocketRequest } from './smartsocket.classes.socketrequest';
|
||||||
|
import { SocketRole } from './smartsocket.classes.socketrole';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* interface for class SmartsocketClient
|
* interface for class SmartsocketClient
|
||||||
*/
|
*/
|
||||||
export interface ISmartsocketClientOptions {
|
export interface ISmartsocketClientOptions {
|
||||||
port:number;
|
port: number;
|
||||||
url:string;
|
url: string;
|
||||||
|
alias: string; // an alias makes it easier to identify this client in a multo client environment
|
||||||
|
role: string;
|
||||||
|
password: string; // by setting a password access to functions can be limited
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartsocketClient {
|
export class SmartsocketClient {
|
||||||
constructor(){
|
public alias: string;
|
||||||
|
public socketRole: SocketRole;
|
||||||
|
public socketConnection: SocketConnection;
|
||||||
|
public serverUrl: string;
|
||||||
|
public serverPort: number;
|
||||||
|
|
||||||
|
public socketFunctions = new plugins.lik.Objectmap<SocketFunction<any>>();
|
||||||
|
public socketRequests = new plugins.lik.Objectmap<SocketRequest<any>>();
|
||||||
|
public socketRoles = new plugins.lik.Objectmap<SocketRole>();
|
||||||
|
|
||||||
|
constructor(optionsArg: ISmartsocketClientOptions) {
|
||||||
|
this.alias = optionsArg.alias;
|
||||||
|
this.serverUrl = optionsArg.url;
|
||||||
|
this.serverPort = optionsArg.port;
|
||||||
|
this.socketRole = new SocketRole({
|
||||||
|
name: optionsArg.role,
|
||||||
|
passwordHash: optionsArg.password
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public addSocketFunction(socketFunction: SocketFunction<any>) {
|
||||||
|
this.socketFunctions.add(socketFunction);
|
||||||
|
this.socketRole.allowedFunctions.add(socketFunction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* connect the client to the server
|
||||||
|
*/
|
||||||
|
public connect() {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'trying to connect...');
|
||||||
|
const socketUrl = `${this.serverUrl}:${this.serverPort}`;
|
||||||
|
this.socketConnection = new SocketConnection({
|
||||||
|
alias: this.alias,
|
||||||
|
authenticated: false,
|
||||||
|
role: this.socketRole,
|
||||||
|
side: 'client',
|
||||||
|
smartsocketHost: this,
|
||||||
|
socket: plugins.socketIoClient(socketUrl, { multiplex: false })
|
||||||
|
});
|
||||||
|
this.socketConnection.socket.on('requestAuth', () => {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'server requested authentication');
|
||||||
|
this.socketConnection.socket.emit('dataAuth', {
|
||||||
|
role: this.socketRole.name,
|
||||||
|
password: this.socketRole.passwordHash,
|
||||||
|
alias: this.alias
|
||||||
|
});
|
||||||
|
this.socketConnection.socket.on('authenticated', () => {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'client is authenticated');
|
||||||
|
this.socketConnection.authenticated = true;
|
||||||
|
this.socketConnection.listenToFunctionRequests();
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
// handle errors
|
||||||
|
this.socketConnection.socket.on('reconnect_failed', async () => {
|
||||||
|
this.disconnect();
|
||||||
|
});
|
||||||
|
this.socketConnection.socket.on('connect_error', async () => {
|
||||||
|
this.disconnect();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* disconnect from the server
|
||||||
|
*/
|
||||||
|
public async disconnect() {
|
||||||
|
if (this.socketConnection) {
|
||||||
|
this.socketConnection.socket.disconnect(true);
|
||||||
|
this.socketConnection = undefined;
|
||||||
|
plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* try a reconnection
|
||||||
|
*/
|
||||||
|
public async tryReconnect() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dispatches a server call
|
||||||
|
* @param functionNameArg
|
||||||
|
* @param dataArg
|
||||||
|
*/
|
||||||
|
public async serverCall<T extends plugins.typedrequestInterfaces.ITypedRequest>(functionNameArg: T['method'], dataArg: T['request']): Promise<T['response']> {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
const socketRequest = new SocketRequest<T>(this, {
|
||||||
|
side: 'requesting',
|
||||||
|
originSocketConnection: this.socketConnection,
|
||||||
|
shortId: plugins.shortid.generate(),
|
||||||
|
funcCallData: {
|
||||||
|
funcName: functionNameArg,
|
||||||
|
funcDataArg: dataArg
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const response = await socketRequest.dispatch();
|
||||||
|
const result = response.funcDataArg;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
167
ts/smartsocket.classes.socketconnection.ts
Normal file
167
ts/smartsocket.classes.socketconnection.ts
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
|
// import classes
|
||||||
|
import { Smartsocket } from './smartsocket.classes.smartsocket';
|
||||||
|
import { SocketFunction } from './smartsocket.classes.socketfunction';
|
||||||
|
import { SocketRequest, ISocketRequestDataObject } from './smartsocket.classes.socketrequest';
|
||||||
|
import { SocketRole } from './smartsocket.classes.socketrole';
|
||||||
|
|
||||||
|
// socket.io
|
||||||
|
import * as SocketIO from 'socket.io';
|
||||||
|
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
|
||||||
|
|
||||||
|
// 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
|
||||||
|
*/
|
||||||
|
export interface ISocketConnectionConstructorOptions {
|
||||||
|
alias: string;
|
||||||
|
authenticated: boolean;
|
||||||
|
role: SocketRole;
|
||||||
|
side: TSocketConnectionSide;
|
||||||
|
smartsocketHost: Smartsocket | SmartsocketClient;
|
||||||
|
socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interface for authentication data
|
||||||
|
*/
|
||||||
|
export interface ISocketConnectionAuthenticationObject {
|
||||||
|
role: 'coreflowContainer';
|
||||||
|
password: 'somePassword';
|
||||||
|
alias: 'coreflow1';
|
||||||
|
}
|
||||||
|
|
||||||
|
// export classes
|
||||||
|
export let allSocketConnections = new Objectmap<SocketConnection>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class SocketConnection represents a websocket connection
|
||||||
|
*/
|
||||||
|
export class SocketConnection {
|
||||||
|
public alias: string;
|
||||||
|
public side: TSocketConnectionSide;
|
||||||
|
public authenticated: boolean = false;
|
||||||
|
public role: SocketRole;
|
||||||
|
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||||
|
public socket: SocketIO.Socket | SocketIOClient.Socket;
|
||||||
|
constructor(optionsArg: ISocketConnectionConstructorOptions) {
|
||||||
|
this.alias = optionsArg.alias;
|
||||||
|
this.authenticated = optionsArg.authenticated;
|
||||||
|
this.role = optionsArg.role;
|
||||||
|
this.side = optionsArg.side;
|
||||||
|
this.smartsocketRef = optionsArg.smartsocketHost;
|
||||||
|
this.socket = optionsArg.socket;
|
||||||
|
|
||||||
|
// standard behaviour that is always true
|
||||||
|
allSocketConnections.add(this);
|
||||||
|
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 --------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* authenticate the socket
|
||||||
|
*/
|
||||||
|
public authenticate() {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
this.socket.on('dataAuth', (dataArg: ISocketConnectionAuthenticationObject) => {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'info',
|
||||||
|
'received authentication data. now hashing and comparing...'
|
||||||
|
);
|
||||||
|
this.socket.removeListener('dataAuth', () => {});
|
||||||
|
if (SocketRole.checkPasswordForRole(dataArg, this.smartsocketRef)) {
|
||||||
|
// TODO: authenticate password
|
||||||
|
this.alias = dataArg.alias;
|
||||||
|
this.authenticated = true;
|
||||||
|
this.role = SocketRole.getSocketRoleByName(this.smartsocketRef, dataArg.role);
|
||||||
|
this.socket.emit('authenticated');
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'ok',
|
||||||
|
`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`
|
||||||
|
);
|
||||||
|
done.resolve(this);
|
||||||
|
} else {
|
||||||
|
this.authenticated = false;
|
||||||
|
this.socket.disconnect();
|
||||||
|
done.reject('not authenticated');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.socket.emit('requestAuth');
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
// listening -------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* listen to function requests
|
||||||
|
*/
|
||||||
|
public listenToFunctionRequests() {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
if (this.authenticated) {
|
||||||
|
this.socket.on('function', (dataArg: ISocketRequestDataObject<any>) => {
|
||||||
|
// check if requested function is available to the socket's scope
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'function request received');
|
||||||
|
const referencedFunction: SocketFunction<any> = this.role.allowedFunctions.find(
|
||||||
|
socketFunctionArg => {
|
||||||
|
return socketFunctionArg.name === dataArg.funcCallData.funcName;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (referencedFunction) {
|
||||||
|
plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
|
||||||
|
const localSocketRequest = new SocketRequest(this.smartsocketRef, {
|
||||||
|
side: 'responding',
|
||||||
|
originSocketConnection: this,
|
||||||
|
shortId: dataArg.shortId,
|
||||||
|
funcCallData: dataArg.funcCallData
|
||||||
|
});
|
||||||
|
localSocketRequest.createResponse(); // takes care of creating response and sending it back
|
||||||
|
} else {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'warn',
|
||||||
|
'function not existent or out of access scope'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.socket.on('functionResponse', (dataArg: ISocketRequestDataObject<any>) => {
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'info',
|
||||||
|
`received response for request with id ${dataArg.shortId}`
|
||||||
|
);
|
||||||
|
const targetSocketRequest = SocketRequest.getSocketRequestById(
|
||||||
|
this.smartsocketRef,
|
||||||
|
dataArg.shortId
|
||||||
|
);
|
||||||
|
targetSocketRequest.handleResponse(dataArg);
|
||||||
|
});
|
||||||
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'info',
|
||||||
|
`now listening to function requests for ${this.alias}`
|
||||||
|
);
|
||||||
|
done.resolve(this);
|
||||||
|
} else {
|
||||||
|
const errMessage = 'socket needs to be authenticated first';
|
||||||
|
plugins.smartlog.defaultLogger.log('error', errMessage);
|
||||||
|
done.reject(errMessage);
|
||||||
|
}
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sending ----------------------
|
||||||
|
}
|
@ -1,24 +1,96 @@
|
|||||||
import * as plugins from "./smartsocket.plugins";
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
// import classes
|
// import classes
|
||||||
import { Stringmap } 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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interface of the contructor options of class SocketFunction
|
||||||
|
*/
|
||||||
|
export interface ISocketFunctionConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
|
funcName: T['method'];
|
||||||
|
funcDef: TFuncDef<T>;
|
||||||
|
allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
|
||||||
|
}
|
||||||
|
|
||||||
export interface SocketFunctionOptions {
|
/**
|
||||||
name: string;
|
* interface of the Socket Function call, in other words the object that routes a call to a function
|
||||||
func: any;
|
*/
|
||||||
roles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
|
export interface ISocketFunctionCallDataRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
};
|
funcName: T['method'];
|
||||||
|
funcDataArg: T['request'];
|
||||||
|
}
|
||||||
|
|
||||||
export class SocketFunction {
|
/**
|
||||||
name: string;
|
* interface of the Socket Function call, in other words the object that routes a call to a function
|
||||||
func: any;
|
*/
|
||||||
roles: SocketRole[];
|
export interface ISocketFunctionCallDataResponse<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
constructor(optionsArg: SocketFunctionOptions) {
|
funcName: T['method'];
|
||||||
this.name = optionsArg.name;
|
funcDataArg: T['response'];
|
||||||
this.func = optionsArg.func;
|
}
|
||||||
this.roles = optionsArg.roles;
|
|
||||||
};
|
/**
|
||||||
}
|
* interface for function definition of SocketFunction
|
||||||
|
*/
|
||||||
|
export type TFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = (dataArg: T['request'], connectionArg: SocketConnection) => PromiseLike<T['response']>;
|
||||||
|
|
||||||
|
// export classes
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class that respresents a function that can be transparently called using a SocketConnection
|
||||||
|
*/
|
||||||
|
export class SocketFunction<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
|
// STATIC
|
||||||
|
public static getSocketFunctionByName<Q extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||||
|
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
||||||
|
functionNameArg: string
|
||||||
|
): SocketFunction<Q> {
|
||||||
|
return smartsocketRefArg.socketFunctions.find(socketFunctionArg => {
|
||||||
|
return socketFunctionArg.name === functionNameArg;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public name: string;
|
||||||
|
public funcDef: TFuncDef<T>;
|
||||||
|
public roles: SocketRole[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the constructor for SocketFunction
|
||||||
|
*/
|
||||||
|
constructor(optionsArg: ISocketFunctionConstructorOptions<T>) {
|
||||||
|
this.name = optionsArg.funcName;
|
||||||
|
this.funcDef = optionsArg.funcDef;
|
||||||
|
this.roles = optionsArg.allowedRoles;
|
||||||
|
for (const socketRoleArg of this.roles) {
|
||||||
|
this._notifyRole(socketRoleArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* invokes the function of this SocketFunction
|
||||||
|
*/
|
||||||
|
public async invoke(dataArg: ISocketFunctionCallDataRequest<T>, socketConnectionArg: SocketConnection): Promise<ISocketFunctionCallDataResponse<T>> {
|
||||||
|
if (dataArg.funcName === this.name) {
|
||||||
|
const funcResponseData: ISocketFunctionCallDataResponse<T> = {
|
||||||
|
funcName: this.name,
|
||||||
|
funcDataArg: await this.funcDef(dataArg.funcDataArg, socketConnectionArg)
|
||||||
|
};
|
||||||
|
return funcResponseData;
|
||||||
|
} else {
|
||||||
|
throw new Error("SocketFunction.name does not match the data argument's .name!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* notifies a role about access to this SocketFunction
|
||||||
|
*/
|
||||||
|
private _notifyRole(socketRoleArg: SocketRole) {
|
||||||
|
socketRoleArg.addSocketFunction(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
123
ts/smartsocket.classes.socketrequest.ts
Normal file
123
ts/smartsocket.classes.socketrequest.ts
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
|
// import interfaces
|
||||||
|
import { SocketFunction, ISocketFunctionCallDataRequest, ISocketFunctionCallDataResponse } from './smartsocket.classes.socketfunction';
|
||||||
|
|
||||||
|
// import classes
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
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 type TSocketRequestStatus = 'new' | 'pending' | 'finished';
|
||||||
|
export type TSocketRequestSide = 'requesting' | 'responding';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* interface of constructor of class SocketRequest
|
||||||
|
*/
|
||||||
|
export interface ISocketRequestConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
|
side: TSocketRequestSide;
|
||||||
|
originSocketConnection: SocketConnection;
|
||||||
|
shortId: string;
|
||||||
|
funcCallData?: ISocketFunctionCallDataRequest<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* request object that is sent initially and may or may not receive a response
|
||||||
|
*/
|
||||||
|
export interface ISocketRequestDataObject<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
|
funcCallData: ISocketFunctionCallDataRequest<T> | ISocketFunctionCallDataResponse<T>;
|
||||||
|
shortId: string;
|
||||||
|
responseTimeout?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// export classes
|
||||||
|
export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
||||||
|
// STATIC
|
||||||
|
public static getSocketRequestById(
|
||||||
|
smartsocketRef: Smartsocket | SmartsocketClient,
|
||||||
|
shortIdArg: string
|
||||||
|
): SocketRequest<any> {
|
||||||
|
return smartsocketRef.socketRequests.find(socketRequestArg => {
|
||||||
|
return socketRequestArg.shortid === shortIdArg;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public status: TSocketRequestStatus = 'new';
|
||||||
|
public side: TSocketRequestSide;
|
||||||
|
public shortid: string;
|
||||||
|
public originSocketConnection: SocketConnection;
|
||||||
|
public funcCallData: ISocketFunctionCallDataRequest<T>;
|
||||||
|
public done = plugins.smartpromise.defer<ISocketFunctionCallDataResponse<T>>();
|
||||||
|
|
||||||
|
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
||||||
|
optionsArg: ISocketRequestConstructorOptions<T>
|
||||||
|
) {
|
||||||
|
this.smartsocketRef = smartsocketRefArg;
|
||||||
|
this.side = optionsArg.side;
|
||||||
|
this.shortid = optionsArg.shortId;
|
||||||
|
this.funcCallData = optionsArg.funcCallData;
|
||||||
|
this.originSocketConnection = optionsArg.originSocketConnection;
|
||||||
|
this.smartsocketRef.socketRequests.add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// requesting --------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dispatches a socketrequest from the requesting to the receiving side
|
||||||
|
*/
|
||||||
|
public dispatch(): Promise<ISocketFunctionCallDataResponse<T>> {
|
||||||
|
const requestData: ISocketRequestDataObject<T> = {
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public async handleResponse(responseDataArg: ISocketRequestDataObject<T>) {
|
||||||
|
plugins.smartlog.defaultLogger.log('info', 'handling response!');
|
||||||
|
this.done.resolve(responseDataArg.funcCallData);
|
||||||
|
this.smartsocketRef.socketRequests.remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// responding --------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates the response on the responding side
|
||||||
|
*/
|
||||||
|
public async createResponse(): Promise<void> {
|
||||||
|
const targetSocketFunction: SocketFunction<T> = SocketFunction.getSocketFunctionByName(
|
||||||
|
this.smartsocketRef,
|
||||||
|
this.funcCallData.funcName
|
||||||
|
);
|
||||||
|
|
||||||
|
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 responseData: ISocketRequestDataObject<T> = {
|
||||||
|
funcCallData: resultData,
|
||||||
|
shortId: this.shortid
|
||||||
|
};
|
||||||
|
this.originSocketConnection.socket.emit('functionResponse', responseData);
|
||||||
|
this.smartsocketRef.socketRequests.remove(this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,25 +1,58 @@
|
|||||||
import * as plugins from "./smartsocket.plugins";
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
// import classes
|
// import classes
|
||||||
import { Stringmap } from "lik";
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
import { SocketFunction } from './smartsocket.classes.socketfunction';
|
||||||
|
import { Smartsocket } from './smartsocket.classes.smartsocket';
|
||||||
|
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient';
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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(
|
||||||
constructor(optionsArg:SocketRoleOptions){
|
referenceSmartsocket: Smartsocket | SmartsocketClient,
|
||||||
this.name = optionsArg.name;
|
socketRoleNameArg: string
|
||||||
this.passwordHash = optionsArg.passwordHash;
|
): 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<any>>();
|
||||||
|
constructor(optionsArg: ISocketRoleOptions) {
|
||||||
|
this.name = optionsArg.name;
|
||||||
|
this.passwordHash = optionsArg.passwordHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* adds the socketfunction to the socketrole
|
||||||
|
* @param socketFunctionArg
|
||||||
|
*/
|
||||||
|
public addSocketFunction(socketFunctionArg: SocketFunction<any>) {
|
||||||
|
this.allowedFunctions.add(socketFunctionArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
78
ts/smartsocket.classes.socketserver.ts
Normal file
78
ts/smartsocket.classes.socketserver.ts
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import * as plugins from './smartsocket.plugins';
|
||||||
|
|
||||||
|
// used in case no other server is supplied
|
||||||
|
import * as http from 'http';
|
||||||
|
import * as https from 'https';
|
||||||
|
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 | https.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() {}
|
||||||
|
}
|
@ -1,31 +0,0 @@
|
|||||||
import * as plugins from "./smartsocket.plugins";
|
|
||||||
|
|
||||||
// interfaces
|
|
||||||
import {ISocketObject} from "./smartsocket.classes.smartsocket";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* authenticate a socket
|
|
||||||
*/
|
|
||||||
export let authenticateSocket = (socketObjectArg: ISocketObject) => {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
socketObjectArg.socket.on("dataAuth", dataArg => {
|
|
||||||
plugins.beautylog.log("received authentication data. now hashing and comparing...");
|
|
||||||
socketObjectArg.socket.removeListener("dataAuth", () => { });
|
|
||||||
if((true)){ // TODO: authenticate password
|
|
||||||
socketObjectArg.alias = dataArg.alias
|
|
||||||
socketObjectArg.authenticated = true;
|
|
||||||
socketObjectArg.role = dataArg.role;
|
|
||||||
socketObjectArg.socket.emit("authenticated");
|
|
||||||
plugins.beautylog.ok(`socket with >>alias ${socketObjectArg.alias} >>role ${socketObjectArg.role} is authenticated!`)
|
|
||||||
done.resolve(socketObjectArg);
|
|
||||||
} else {
|
|
||||||
socketObjectArg.socket.disconnect();
|
|
||||||
done.reject("not authenticated");
|
|
||||||
};
|
|
||||||
});
|
|
||||||
socketObjectArg.socket.emit("requestAuth");
|
|
||||||
return done.promise;
|
|
||||||
};
|
|
@ -1,7 +1,33 @@
|
|||||||
import "typings-global";
|
// apiglobal scope
|
||||||
export import beautylog = require("beautylog");
|
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
||||||
export import lik = require("lik");
|
|
||||||
export import q = require("q");
|
export {typedrequestInterfaces};
|
||||||
export import socketIo = require("socket.io");
|
|
||||||
export import socketIoClient = require("socket.io-client");
|
// pushrocks scope
|
||||||
export import taskbuffer = require("taskbuffer");
|
import * as lik from '@pushrocks/lik';
|
||||||
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
|
import * as smarthash from '@pushrocks/smarthash';
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
|
import * as smartexpress from '@pushrocks/smartexpress';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
|
|
||||||
|
export {
|
||||||
|
lik,
|
||||||
|
smartlog,
|
||||||
|
smarthash,
|
||||||
|
smartdelay,
|
||||||
|
smartexpress,
|
||||||
|
smartpromise
|
||||||
|
};
|
||||||
|
|
||||||
|
// third party scope
|
||||||
|
import * as shortid from 'shortid';
|
||||||
|
import socketIo from 'socket.io';
|
||||||
|
import socketIoClient from 'socket.io-client';
|
||||||
|
|
||||||
|
export {
|
||||||
|
shortid,
|
||||||
|
socketIo,
|
||||||
|
socketIoClient
|
||||||
|
};
|
||||||
|
17
tslint.json
Normal file
17
tslint.json
Normal 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"
|
||||||
|
}
|
Reference in New Issue
Block a user