Compare commits
42 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0ce732ee1 | |||
98be0f036c | |||
54fca17142 | |||
1a7634e8db | |||
8830b825ac | |||
123324bf43 | |||
4761ff31cf | |||
430c3ea13a | |||
6dd3782b0d | |||
06aa721247 | |||
f728c56016 | |||
f512acdfaa | |||
331a098851 | |||
a8053a6e20 | |||
a62f3eb750 | |||
4b53044e8f | |||
2db2c80130 | |||
8b23eaf194 | |||
a03c54001f | |||
7bc404ba21 | |||
ac2a1559b0 | |||
57b37cb327 | |||
8b17e814cc | |||
f4b8cde347 | |||
3189d4d274 | |||
c2d134a6ea | |||
7552dc1e9f | |||
fe91459510 | |||
0e0ffb7634 | |||
37bb052774 | |||
3c7683d40e | |||
c19f27e873 | |||
c1a03fec0f | |||
8b650c5ea7 | |||
4fc6e327ec | |||
7991baf2bf | |||
d033780015 | |||
eae46e6461 | |||
785acfaba4 | |||
5a4dceb75d | |||
a17834a8f0 | |||
01765fa50f |
@ -3,93 +3,123 @@ image: hosttoday/ht-docker-node:npmci
|
|||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .yarn/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: "$CI_BUILD_STAGE"
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- trigger
|
- metadata
|
||||||
- pages
|
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command yarn global add snyk
|
- npmci npm prepare
|
||||||
- npmci command yarn install --ignore-scripts
|
- npmci command npm install -g snyk
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
testLEGACY:
|
# ====================
|
||||||
stage: test
|
# test stage
|
||||||
script:
|
# ====================
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
testSTABLE:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
image: docker:stable
|
||||||
|
allow_failure: true
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
|
script:
|
||||||
|
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||||
|
- docker run
|
||||||
|
--env SOURCE_CODE="$PWD"
|
||||||
|
--volume "$PWD":/code
|
||||||
|
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||||
|
artifacts:
|
||||||
|
paths: [codeclimate.json]
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: trigger
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: pages
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command yarn global add npmpage
|
- npmci command npm install -g typedoc typescript
|
||||||
- npmci command npmpage
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
19
license
Normal file
19
license
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2018 Lossless GmbH (hello@lossless.com)
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -1,9 +1,17 @@
|
|||||||
{
|
{
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartuniverse",
|
||||||
|
"shortDescription": "messaging service for micro services",
|
||||||
|
"npmPackagename": "@pushrocks/smartuniverse",
|
||||||
|
"license": "MIT",
|
||||||
|
"projectDomain": "push.rocks"
|
||||||
|
}
|
||||||
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"@gitzone/npmts",
|
|
||||||
"ts-node"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
}
|
}
|
||||||
}
|
}
|
3027
package-lock.json
generated
Normal file
3027
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,30 +1,38 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartuniverse",
|
"name": "@pushrocks/smartuniverse",
|
||||||
"version": "1.0.14",
|
"version": "1.0.35",
|
||||||
|
"private": false,
|
||||||
"description": "messaging service for your micro services",
|
"description": "messaging service for your micro services",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(ts-node -O '{\"lib\": [ \"es2015\" ]}' test/test.ts)",
|
"test": "(tstest test/)",
|
||||||
"build": "(npmts)",
|
"build": "(tsbuild)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^9.6.0",
|
"@gitzone/tsbuild": "^2.1.8",
|
||||||
"tapbundle": "^2.0.0"
|
"@gitzone/tstest": "^1.0.20",
|
||||||
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
|
"@types/node": "^11.13.4",
|
||||||
|
"tslint": "^5.15.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"rxjs": "*"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lik": "^2.0.5",
|
"@pushrocks/lik": "^3.0.5",
|
||||||
"rxjs": "^5.5.8",
|
"@pushrocks/smartdelay": "^2.0.3",
|
||||||
"smartcli": "^2.0.12",
|
"@pushrocks/smartexpress": "^3.0.18",
|
||||||
"smartexpress": "^1.0.21",
|
"@pushrocks/smartfile": "^7.0.2",
|
||||||
"smartfile": "^4.2.28",
|
"@pushrocks/smarthash": "^2.0.4",
|
||||||
"smartq": "^1.1.8",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"smartrequest": "^1.0.8",
|
"@pushrocks/smartrequest": "^1.1.14",
|
||||||
"smartrx": "^1.0.5",
|
"@pushrocks/smartrx": "^2.0.3",
|
||||||
"smartsocket": "^1.1.19",
|
"@pushrocks/smartsocket": "^1.1.27",
|
||||||
"smarttime": "^2.0.0"
|
"@pushrocks/smarttime": "^3.0.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
readme.md
50
readme.md
@ -1,33 +1,49 @@
|
|||||||
# smartuniverse
|
# @pushrocks/smartuniverse
|
||||||
|
|
||||||
messaging service for your micro services
|
messaging service for micro services
|
||||||
|
|
||||||
## Availabililty
|
## Availabililty and Links
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
||||||
[](https://GitLab.com/pushrocks/smartuniverse)
|
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartuniverse)
|
||||||
[](https://github.com/pushrocks/smartuniverse)
|
- [github.com (source mirror)](https://github.com/pushrocks/smartuniverse)
|
||||||
[](https://pushrocks.gitlab.io/smartuniverse/)
|
- [docs (typedoc)](https://pushrocks.gitlab.io/smartuniverse/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
|
|
||||||
[](https://GitLab.com/pushrocks/smartuniverse/commits/master)
|
[](https://gitlab.com/pushrocks/smartuniverse/commits/master)
|
||||||
[](https://GitLab.com/pushrocks/smartuniverse/commits/master)
|
[](https://gitlab.com/pushrocks/smartuniverse/commits/master)
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
[](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
||||||
[](https://www.bithound.io/github/pushrocks/smartuniverse/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartuniverse)
|
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartuniverse)
|
[](https://snyk.io/test/npm/@pushrocks/smartuniverse)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
[](http://standardjs.com/)
|
[](https://prettier.io/)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
### What is smartuniverse all about?
|
||||||
|
|
||||||
> licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
Think WhatsApp, but for your microservices architecture. It allows your services to securely talk to each other in **private, shielded channels** without having to expose anything to the outside world. This allows the use of **reactive programming across your entire stack**.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import * as smartuniverse from '@pushrocks/smartuniverse';
|
||||||
|
|
||||||
|
const myUniverse = new smartuniverse.Universe({
|
||||||
|
messageExpiryInMilliseconds: 60000 // the standard time in milliseconds until a message expires
|
||||||
|
});
|
||||||
|
|
||||||
|
// create as many channels as you like
|
||||||
|
myUniverse.addChannel('awesomeChannel', 'awesomeChannelPass');
|
||||||
|
myUniverse.addChannel('awesomeChannel2', 'jhkjhfsdf87eerkjslkfja9');
|
||||||
|
|
||||||
|
myUniverse.start(8765); // start the server and provide the port on which to listen on
|
||||||
|
```
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
[](https://push.rocks)
|
[](https://maintainedby.lossless.com)
|
||||||
|
30
test/test.ts
30
test/test.ts
@ -1,12 +1,17 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartuniverse from '../ts/index';
|
import * as smartuniverse from '../ts/index';
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
let testUniverse: smartuniverse.Universe;
|
let testUniverse: smartuniverse.Universe;
|
||||||
let testUniverseClient: smartuniverse.UniverseClient;
|
let testUniverseClient: smartuniverse.ClientUniverse;
|
||||||
let testMessageObservable: Observable<smartuniverse.UniverseMessage>;
|
let testClientChannel: smartuniverse.ClientUniverseChannel;
|
||||||
|
|
||||||
|
const testChannelData = {
|
||||||
|
channelName: 'awesomeTestChannel',
|
||||||
|
channelPass: 'awesomeChannelPAss'
|
||||||
|
}
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testUniverse = new smartuniverse.Universe({
|
testUniverse = new smartuniverse.Universe({
|
||||||
@ -15,15 +20,24 @@ tap.test('first test', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('add a message to the SmartUniverse', async () => {
|
tap.test('add a message to the SmartUniverse', async () => {
|
||||||
await testUniverse.initServer(8765);
|
await testUniverse.start(8765);
|
||||||
});
|
});
|
||||||
|
|
||||||
// testing message handling
|
// testing message handling
|
||||||
tap.test('create smartuniverse client', async () => {
|
tap.test('create smartuniverse client', async () => {
|
||||||
testUniverseClient = new smartuniverse.UniverseClient({
|
testUniverseClient = new smartuniverse.ClientUniverse({
|
||||||
serverAddress: 'http://localhost:8765'
|
serverAddress: 'http://localhost:8765'
|
||||||
});
|
});
|
||||||
expect(testUniverseClient).to.be.instanceof(smartuniverse.UniverseClient);
|
expect(testUniverseClient).to.be.instanceof(smartuniverse.ClientUniverse);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should add a channel to the universe', async () => {
|
||||||
|
await testUniverse.addChannel('testChannel', 'testPassword');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should get a observable correctly', async () => {
|
||||||
|
testClientChannel = await testUniverseClient.getChannel(testChannelData.channelName, testChannelData.channelPass);
|
||||||
|
expect(testClientChannel).to.be.instanceof(smartuniverse.ClientUniverseChannel);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should send a message correctly', async () => {
|
tap.test('should send a message correctly', async () => {
|
||||||
@ -32,10 +46,6 @@ tap.test('should send a message correctly', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should get a observable correctly', async () => {
|
|
||||||
testMessageObservable = testUniverseClient.getMessageObservable();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should receive a message correctly', async () => {});
|
tap.test('should receive a message correctly', async () => {});
|
||||||
|
|
||||||
tap.test('should disconnect the client correctly', async () => {
|
tap.test('should disconnect the client correctly', async () => {
|
||||||
|
10
ts/index.ts
10
ts/index.ts
@ -1,3 +1,11 @@
|
|||||||
|
// Client classes
|
||||||
|
export * from './smartuniverse.classes.clientuniverse';
|
||||||
|
export * from './smartuniverse.classes.clientuniversechannel';
|
||||||
|
|
||||||
|
// Server classes
|
||||||
export * from './smartuniverse.classes.universe';
|
export * from './smartuniverse.classes.universe';
|
||||||
export * from './smartuniverse.classes.universeclient';
|
export * from './smartuniverse.classes.universecache';
|
||||||
|
export * from './smartuniverse.classes.universechannel';
|
||||||
export * from './smartuniverse.classes.universemessage';
|
export * from './smartuniverse.classes.universemessage';
|
||||||
|
|
||||||
|
export * from './interfaces';
|
||||||
|
15
ts/interfaces/http.interfaces.ts
Normal file
15
ts/interfaces/http.interfaces.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export interface IServerGetMessagesRequestBody {
|
||||||
|
channel: string;
|
||||||
|
topic?: string;
|
||||||
|
youngerThan: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the interface for a standard request
|
||||||
|
*/
|
||||||
|
export interface IServerPutMessageRequestBody {
|
||||||
|
channel: string;
|
||||||
|
passphrase: string;
|
||||||
|
message: string;
|
||||||
|
payload: any;
|
||||||
|
}
|
3
ts/interfaces/index.ts
Normal file
3
ts/interfaces/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './http.interfaces';
|
||||||
|
export * from './universechannel.interfaces';
|
||||||
|
export * from './universemessage.interfaces';
|
1
ts/interfaces/universechannel.interfaces.ts
Normal file
1
ts/interfaces/universechannel.interfaces.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export interface IUniverseChannel {}
|
7
ts/interfaces/universemessage.interfaces.ts
Normal file
7
ts/interfaces/universemessage.interfaces.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface IUniverseMessage {
|
||||||
|
messageText: string;
|
||||||
|
targetChannelName: string;
|
||||||
|
passphrase: string;
|
||||||
|
payload?: string | number | any;
|
||||||
|
payloadStringType?: 'Buffer' | 'string' | 'object';
|
||||||
|
}
|
71
ts/smartuniverse.classes.clientuniverse.ts
Normal file
71
ts/smartuniverse.classes.clientuniverse.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { Smartsocket, SmartsocketClient } from '@pushrocks/smartsocket';
|
||||||
|
import * as url from 'url';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
import {
|
||||||
|
ClientUniverseChannel,
|
||||||
|
UniverseMessage
|
||||||
|
} from './';
|
||||||
|
|
||||||
|
export interface IClientOptions {
|
||||||
|
serverAddress: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this class is for client side only!!!
|
||||||
|
* allows connecting to a universe server
|
||||||
|
*/
|
||||||
|
export class ClientUniverse {
|
||||||
|
public options;
|
||||||
|
public socketClient: plugins.smartsocket.SmartsocketClient;
|
||||||
|
public observableIntake: plugins.smartrx.ObservableIntake<UniverseMessage>;
|
||||||
|
|
||||||
|
public channelCache = new Objectmap<ClientUniverseChannel>();
|
||||||
|
|
||||||
|
constructor(optionsArg: IClientOptions) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async sendMessage(messageArg: interfaces.IUniverseMessage) {
|
||||||
|
const requestBody: interfaces.IUniverseMessage = messageArg;
|
||||||
|
const requestBodyString = JSON.stringify(requestBody);
|
||||||
|
// TODO: User websocket connection if available
|
||||||
|
const response = await plugins.smartrequest.postJson(`${this.options.serverAddress}/sendmessage` , {
|
||||||
|
requestBody: requestBodyString
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getChannel(channelName: string, passphrase): Promise<ClientUniverseChannel> {
|
||||||
|
await this.checkConnection();
|
||||||
|
const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel(
|
||||||
|
this,
|
||||||
|
channelName
|
||||||
|
);
|
||||||
|
this.channelCache.add(clientUniverseChannel);
|
||||||
|
return clientUniverseChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public close() {
|
||||||
|
this.socketClient.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkConnection() {
|
||||||
|
if (!this.socketClient && !this.observableIntake) {
|
||||||
|
const parsedURL = url.parse(this.options.serverAddress);
|
||||||
|
this.socketClient = new SmartsocketClient({
|
||||||
|
alias: process.env.SOCKET_ALIAS || 'someclient',
|
||||||
|
password: 'UniverseClient',
|
||||||
|
port: parseInt(parsedURL.port, 10),
|
||||||
|
role: 'UniverseClient',
|
||||||
|
url: parsedURL.hostname
|
||||||
|
});
|
||||||
|
this.observableIntake = new plugins.smartrx.ObservableIntake();
|
||||||
|
this.socketClient.connect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
ts/smartuniverse.classes.clientuniversechannel.ts
Normal file
35
ts/smartuniverse.classes.clientuniversechannel.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
import { ClientUniverse } from './';
|
||||||
|
|
||||||
|
export class ClientUniverseChannel implements interfaces.IUniverseChannel {
|
||||||
|
// ======
|
||||||
|
// STATIC
|
||||||
|
// ======
|
||||||
|
public static async createClientUniverseChannel(
|
||||||
|
clientUniverseArg: ClientUniverse,
|
||||||
|
channelName: string
|
||||||
|
): Promise<ClientUniverseChannel> {
|
||||||
|
const clientChannel = new ClientUniverseChannel(clientUniverseArg);
|
||||||
|
await clientChannel.transmitSubscription();
|
||||||
|
return clientChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
|
|
||||||
|
public clientUniverse: ClientUniverse;
|
||||||
|
|
||||||
|
constructor(clientUniverseArg: ClientUniverse) {
|
||||||
|
this.clientUniverse = clientUniverseArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tells the universe about this instances interest into a channel
|
||||||
|
*/
|
||||||
|
public async transmitSubscription() {
|
||||||
|
this.clientUniverse.socketClient;
|
||||||
|
}
|
||||||
|
}
|
21
ts/smartuniverse.classes.clientuniversemessage.ts
Normal file
21
ts/smartuniverse.classes.clientuniversemessage.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
export class ClientUniverseMessage implements interfaces.IUniverseMessage {
|
||||||
|
// ======
|
||||||
|
// STATIC
|
||||||
|
// ======
|
||||||
|
public static createMessageFromPayload(messageArg: string, payloadArg: any) {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
|
constructor(messageArg, payloadArg) {}
|
||||||
|
|
||||||
|
getAsJsonForPayload () {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
|
||||||
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
|
||||||
|
|
||||||
export class UniverseManager {
|
|
||||||
public channelStore = new Objectmap<UniverseChannel>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* register a new member
|
|
||||||
*/
|
|
||||||
public async registerMember() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* register a new channel within the universe
|
|
||||||
* @param channelName the name of the channel
|
|
||||||
* @param authSecret the secret against which to verify members of the channel
|
|
||||||
*/
|
|
||||||
public async registerChannel(channelName: string, authSecret: string) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +1,38 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
import { Handler, Route, Server } from 'smartexpress';
|
import { Handler, Route, Server } from '@pushrocks/smartexpress';
|
||||||
|
import { UniverseCache, UniverseChannel, UniverseMessage } from './';
|
||||||
import { UniverseManager } from './smartuniverse.classes.manager';
|
|
||||||
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
|
||||||
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
||||||
import { UniverseStore } from './smartuniverse.classes.universestore';
|
|
||||||
|
|
||||||
import * as paths from './smartuniverse.paths';
|
import * as paths from './smartuniverse.paths';
|
||||||
|
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
export interface ISmartUniverseConstructorOptions {
|
export interface ISmartUniverseConstructorOptions {
|
||||||
messageExpiryInMilliseconds: number;
|
messageExpiryInMilliseconds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IServerGetMessagesRequestBody {
|
|
||||||
channel: string;
|
|
||||||
topic?: string;
|
|
||||||
youngerThan: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IServerPutMessageRequestBody {
|
|
||||||
message: string;
|
|
||||||
payload: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main class that setsup a Universe
|
||||||
|
*/
|
||||||
export class Universe {
|
export class Universe {
|
||||||
// subinstances
|
// subinstances
|
||||||
public universeStore: UniverseStore;
|
public universeCache: UniverseCache;
|
||||||
public universeManager: UniverseManager;
|
|
||||||
|
|
||||||
// options
|
// options
|
||||||
private options: ISmartUniverseConstructorOptions;
|
private options: ISmartUniverseConstructorOptions;
|
||||||
|
|
||||||
// Store version handling
|
/**
|
||||||
|
* stores the version of the universe server running
|
||||||
|
* this is done since the version is exposed through the api and multiple fs actions are avoided this way.
|
||||||
|
*/
|
||||||
private universeVersionStore: string;
|
private universeVersionStore: string;
|
||||||
private get universeVersion() {
|
|
||||||
|
/**
|
||||||
|
* get the currently running version of smartuniverse
|
||||||
|
*/
|
||||||
|
public get universeVersion() {
|
||||||
if (this.universeVersionStore) {
|
if (this.universeVersionStore) {
|
||||||
return this.universeVersionStore;
|
return this.universeVersionStore;
|
||||||
} else {
|
} else {
|
||||||
@ -44,65 +42,80 @@ export class Universe {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the smartexpress server used
|
||||||
|
*/
|
||||||
private smartexpressServer: plugins.smartexpress.Server;
|
private smartexpressServer: plugins.smartexpress.Server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the smartsocket used
|
||||||
|
*/
|
||||||
private smartsocket: plugins.smartsocket.Smartsocket;
|
private smartsocket: plugins.smartsocket.Smartsocket;
|
||||||
|
|
||||||
constructor(optionsArg: ISmartUniverseConstructorOptions) {
|
constructor(optionsArg: ISmartUniverseConstructorOptions) {
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
|
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
|
||||||
this.universeManager = new UniverseManager();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* adds a channel to the Universe
|
||||||
|
*/
|
||||||
|
public async addChannel(nameArg: string, passphraseArg: string) {
|
||||||
|
const newChannel = UniverseChannel.createChannel(this.universeCache, nameArg, passphraseArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initiates a server
|
* initiates a server
|
||||||
*/
|
*/
|
||||||
public async initServer(portArg: number | string) {
|
public async start(portArg: number | string) {
|
||||||
|
// lets create the base smartexpress server
|
||||||
this.smartexpressServer = new plugins.smartexpress.Server({
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
||||||
cors: true,
|
cors: true,
|
||||||
defaultAnswer: `smartuniverse server ${this.universeVersion}`,
|
defaultAnswer: async () => {
|
||||||
|
return `smartuniverse server ${this.universeVersion}`;
|
||||||
|
},
|
||||||
forceSsl: false,
|
forceSsl: false,
|
||||||
port: portArg
|
port: portArg
|
||||||
});
|
});
|
||||||
|
|
||||||
// message handling
|
// lets create the http request route
|
||||||
// adds messages
|
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
|
||||||
const addMessageHandler = new Handler('PUT', request => {
|
this.universeCache.addMessage(req.body);
|
||||||
const requestBody = request.body;
|
}));
|
||||||
this.universeStore.addMessage(requestBody.message, requestBody.payload);
|
|
||||||
console.log(requestBody);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
// gets messages
|
|
||||||
const readMessageHandler = new Handler('GET', request => {
|
|
||||||
const done = plugins.smartq.defer<UniverseMessage[]>();
|
|
||||||
const requestBody = request.body;
|
|
||||||
const messageObservable = this.universeStore.readMessagesYoungerThan(requestBody.since);
|
|
||||||
messageObservable.toArray().subscribe(universeMessageArrayArg => {
|
|
||||||
done.resolve(universeMessageArrayArg);
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
});
|
|
||||||
|
|
||||||
// create new Route for messages
|
|
||||||
const messageRoute = new Route(this.smartexpressServer, 'message');
|
|
||||||
messageRoute.addHandler(addMessageHandler);
|
|
||||||
messageRoute.addHandler(readMessageHandler);
|
|
||||||
|
|
||||||
const leaderElectionRoute = new Route(this.smartexpressServer, 'leadelection');
|
|
||||||
// TODO: implement Handlers for leader election
|
|
||||||
|
|
||||||
// add websocket upgrade
|
// add websocket upgrade
|
||||||
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
||||||
port: 12345 // fix this within smartsocket
|
port: 12345 // fix this within smartsocket
|
||||||
});
|
});
|
||||||
|
|
||||||
this.smartsocket.setExternalServer('express', this.smartexpressServer as any); // should work with express as well
|
// add a role for the clients
|
||||||
|
const ClientRole = new plugins.smartsocket.SocketRole({
|
||||||
|
name: 'clientuniverse',
|
||||||
|
passwordHash: 'clientuniverse' // authentication happens on another level
|
||||||
|
});
|
||||||
|
|
||||||
|
// add the role to smartsocket
|
||||||
|
this.smartsocket.addSocketRoles([ClientRole]);
|
||||||
|
|
||||||
|
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
|
||||||
|
allowedRoles: [ClientRole],
|
||||||
|
funcName: 'channelSubscription',
|
||||||
|
funcDef: () => {} // TODO: implement an action upon connection of clients
|
||||||
|
});
|
||||||
|
|
||||||
|
// add smartsocket to the running smartexpress app
|
||||||
|
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
|
||||||
|
|
||||||
|
// start the socket
|
||||||
this.smartsocket.start();
|
this.smartsocket.start();
|
||||||
|
|
||||||
|
// start the smartexpress instance
|
||||||
await this.smartexpressServer.start();
|
await this.smartexpressServer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stop everything
|
||||||
|
*/
|
||||||
public async stopServer() {
|
public async stopServer() {
|
||||||
await this.smartsocket.stop();
|
await this.smartsocket.stop();
|
||||||
await this.smartexpressServer.stop();
|
await this.smartexpressServer.stop();
|
||||||
|
63
ts/smartuniverse.classes.universecache.ts
Normal file
63
ts/smartuniverse.classes.universecache.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
||||||
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
||||||
|
|
||||||
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
|
||||||
|
import { Observable, from } from 'rxjs';
|
||||||
|
import { filter } from 'rxjs/operators';
|
||||||
|
import { rxjs } from '@pushrocks/smartrx';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* universe store handles the creation, storage and retrieval of messages.
|
||||||
|
*/
|
||||||
|
export class UniverseCache {
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
|
public standardMessageExpiry: number;
|
||||||
|
public destructionTime: number = 60000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stores messages for this instance
|
||||||
|
*/
|
||||||
|
public messageMap = new Objectmap<UniverseMessage>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stores the channels that are available within the universe
|
||||||
|
*/
|
||||||
|
public channelMap = new Objectmap<UniverseChannel>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* allows messages to be processed in a blacklist mode for further analysis
|
||||||
|
*/
|
||||||
|
public blackListChannel = new UniverseChannel(this, 'blacklist', 'nada');
|
||||||
|
|
||||||
|
constructor(standardMessageExpiryArg: number) {
|
||||||
|
this.standardMessageExpiry = standardMessageExpiryArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add a message to the UniverseCache
|
||||||
|
* @param messageArg
|
||||||
|
* @param attachedPayloadArg
|
||||||
|
*/
|
||||||
|
public async addMessage(messageArg: UniverseMessage) {
|
||||||
|
messageArg.setUniverseCache(this);
|
||||||
|
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
|
||||||
|
this.messageMap.add(messageArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a message from the UniverseCache
|
||||||
|
*/
|
||||||
|
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
|
||||||
|
const messageObservable = from(this.messageMap.getArray()).pipe(
|
||||||
|
filter(messageArg => {
|
||||||
|
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return messageObservable;
|
||||||
|
}
|
||||||
|
}
|
@ -1,32 +1,92 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
||||||
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* enables messages to stay within a certain scope.
|
* enables messages to stay within a certain scope.
|
||||||
*/
|
*/
|
||||||
export class UniverseChannel {
|
export class UniverseChannel {
|
||||||
/**
|
// ======
|
||||||
* stores the channels that are available within the universe
|
// STATIC
|
||||||
*/
|
// ======
|
||||||
public static channelStore = new Objectmap();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates new channels
|
* creates new channels
|
||||||
* @param channelArg the name of the topic
|
* @param channelArg the name of the topic
|
||||||
* @param secretArg the secret thats used for a certain topic.
|
* @param passphraseArg the secret thats used for a certain topic.
|
||||||
*/
|
*/
|
||||||
public static createChannel = (channelArg: string, secretArg: string) => {
|
public static createChannel(
|
||||||
|
universeCacheArg: UniverseCache,
|
||||||
|
channelNameArg: string,
|
||||||
|
passphraseArg: string
|
||||||
|
) {
|
||||||
|
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
|
||||||
|
universeCacheArg.channelMap.add(newChannel);
|
||||||
|
return newChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
credentials: {
|
/**
|
||||||
user: string;
|
* returns boolean wether certain channel exists
|
||||||
password: string;
|
*/
|
||||||
};
|
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
|
||||||
|
const channel = universeCacheArg.channelMap.find(channelArg => {
|
||||||
|
return channelArg.name === channelNameArg;
|
||||||
|
});
|
||||||
|
if (channel) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static authorizeAMessageForAChannel(
|
||||||
|
universeCacheArg: UniverseCache,
|
||||||
|
universeMessageArg: UniverseMessage
|
||||||
|
) {
|
||||||
|
const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
|
||||||
|
const result = universeChannel.authenticate(universeMessageArg);
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
if (foundChannel) {
|
||||||
|
universeMessageArg.authenticated = true;
|
||||||
|
universeMessageArg.universeChannelList.add(foundChannel);
|
||||||
|
return foundChannel;
|
||||||
|
} else {
|
||||||
|
universeMessageArg.authenticated = false;
|
||||||
|
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
|
/**
|
||||||
|
* the name of the channel
|
||||||
|
*/
|
||||||
|
public name: string;
|
||||||
|
public universeCacheInstance: UniverseCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the passphrase for the channel
|
||||||
|
*/
|
||||||
|
public passphrase: string;
|
||||||
|
|
||||||
|
constructor(universeCacheArg: UniverseCache, channelNameArg: string, passphraseArg: string) {
|
||||||
|
this.name = channelNameArg;
|
||||||
|
this.passphrase = passphraseArg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* authenticates a client on the server side
|
* authenticates a client on the server side
|
||||||
*/
|
*/
|
||||||
async authenticateClient() {}
|
public authenticate(universeMessageArg: UniverseMessage): boolean {
|
||||||
|
return (
|
||||||
|
this.name === universeMessageArg.requestedChannelName &&
|
||||||
|
this.passphrase === universeMessageArg.requestedChannelPassphrase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public pushToClients(messageArg: UniverseMessage) {}
|
||||||
}
|
}
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { Smartsocket, SmartsocketClient } from 'smartsocket';
|
|
||||||
import * as url from 'url';
|
|
||||||
|
|
||||||
import {
|
|
||||||
IServerGetMessagesRequestBody,
|
|
||||||
IServerPutMessageRequestBody
|
|
||||||
} from './smartuniverse.classes.universe';
|
|
||||||
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
||||||
|
|
||||||
export interface IClientOptions {
|
|
||||||
serverAddress: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* this class is for client side only!!!
|
|
||||||
* allows connecting to a universe server
|
|
||||||
*/
|
|
||||||
export class UniverseClient {
|
|
||||||
public options;
|
|
||||||
private socketClient: plugins.smartsocket.SmartsocketClient;
|
|
||||||
private observableIntake: plugins.smartrx.ObservableIntake<UniverseMessage>;
|
|
||||||
|
|
||||||
constructor(optionsArg: IClientOptions) {
|
|
||||||
this.options = optionsArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendMessage(messageArg, payloadArg) {
|
|
||||||
const requestBody = {
|
|
||||||
message: messageArg,
|
|
||||||
payload: payloadArg
|
|
||||||
};
|
|
||||||
// TODO: User websocket connection if available
|
|
||||||
await plugins.smartrequest.post(this.options.serverAddress, {
|
|
||||||
requestBody
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public getMessageObservable() {
|
|
||||||
if (!this.socketClient && !this.observableIntake) {
|
|
||||||
const parsedURL = url.parse(this.options.serverAddress);
|
|
||||||
this.socketClient = new SmartsocketClient({
|
|
||||||
alias: process.env.SOCKET_ALIAS || 'someclient',
|
|
||||||
password: 'UniverseClient',
|
|
||||||
port: parseInt(parsedURL.port, 10),
|
|
||||||
role: 'UniverseClient',
|
|
||||||
url: parsedURL.hostname
|
|
||||||
});
|
|
||||||
this.observableIntake = new plugins.smartrx.ObservableIntake();
|
|
||||||
this.socketClient.connect();
|
|
||||||
}
|
|
||||||
return this.observableIntake.observable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public close() {
|
|
||||||
this.socketClient.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +1,119 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
import { Timer, TimeStamp } from 'smarttime';
|
import { Objectmap } from '@pushrocks/lik';
|
||||||
import { UniverseStore } from './smartuniverse.classes.universestore';
|
|
||||||
|
import { Timer, TimeStamp } from '@pushrocks/smarttime';
|
||||||
|
import { Universe } from './smartuniverse.classes.universe';
|
||||||
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
||||||
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* represents a message within a universe
|
* represents a message within a universe
|
||||||
|
* acts as a container to save message states like authentication status
|
||||||
*/
|
*/
|
||||||
export class UniverseMessage {
|
export class UniverseMessage implements interfaces.IUniverseMessage {
|
||||||
/**
|
/**
|
||||||
* public and unique id
|
* public and unique id
|
||||||
* numeric ascending
|
* numeric ascending
|
||||||
* adheres to time in milliseconds
|
* adheres to time in milliseconds
|
||||||
* avoids duplications though
|
* -> meaning it describes the time of arrival
|
||||||
|
* -> two messages received at the same time will count up the second one
|
||||||
|
* -> avoids duplications of messages
|
||||||
|
* -> may be changed to nanoseconds to ensure higher throughput
|
||||||
*/
|
*/
|
||||||
public id: number;
|
public id: number;
|
||||||
public universeStore: UniverseStore;
|
|
||||||
public timestamp: TimeStamp; // when has this message been created
|
/**
|
||||||
public topic: string; // enables unprotected grouping of messages for efficiency purposes.
|
* the UniverseCache the message is attached to
|
||||||
public message: string; // the actual message
|
*/
|
||||||
public attachedPayload: any; // any attached payloads. Can be of binary format.
|
public universeCache: UniverseCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* requestedChannelName
|
||||||
|
*/
|
||||||
|
public requestedChannelName: string;
|
||||||
|
public requestedChannelPassphrase: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* enables unprotected grouping of messages for efficiency purposes.
|
||||||
|
*/
|
||||||
|
public universeChannelList = new Objectmap<UniverseChannel>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wether the message is authenticated
|
||||||
|
*/
|
||||||
|
public authenticated: boolean = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* time of creation
|
||||||
|
*/
|
||||||
|
public timestamp: TimeStamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the actual message
|
||||||
|
*/
|
||||||
|
public message: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* any attached payloads. Can be of binary format.
|
||||||
|
*/
|
||||||
|
public attachedPayload: any;
|
||||||
public destructionTimer: Timer; // a timer to take care of message destruction
|
public destructionTimer: Timer; // a timer to take care of message destruction
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the constructor to create a universe message
|
* the constructor to create a universe message
|
||||||
* @param parentUniverseStore
|
|
||||||
* @param messageArg
|
* @param messageArg
|
||||||
* @param attachedPayloadArg
|
* @param attachedPayloadArg
|
||||||
* @param selfdestructAfterArg
|
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
parentUniverseStore: UniverseStore,
|
|
||||||
messageArg: string,
|
messageArg: string,
|
||||||
attachedPayloadArg: any,
|
requestedChannelNameArg: string,
|
||||||
selfdestructAfterArg: number
|
passphraseArg: string,
|
||||||
|
attachedPayloadArg: any
|
||||||
) {
|
) {
|
||||||
this.universeStore = parentUniverseStore;
|
|
||||||
this.timestamp = new TimeStamp();
|
this.timestamp = new TimeStamp();
|
||||||
this.message = messageArg;
|
this.message = messageArg;
|
||||||
|
this.requestedChannelName = requestedChannelNameArg;
|
||||||
|
this.requestedChannelPassphrase = passphraseArg;
|
||||||
this.attachedPayload = attachedPayloadArg;
|
this.attachedPayload = attachedPayloadArg;
|
||||||
this.destructionTimer = new Timer(selfdestructAfterArg);
|
// prevent memory issues
|
||||||
this.destructionTimer.start();
|
this.fallBackDestruction();
|
||||||
|
}
|
||||||
|
|
||||||
// set up self destruction by removing this from the parent messageStore
|
public setUniverseCache(universeCacheArg: UniverseCache) {
|
||||||
this.destructionTimer.completed.then(async () => {
|
this.universeCache = universeCacheArg;
|
||||||
this.universeStore.messageStore.remove(this);
|
}
|
||||||
|
|
||||||
|
public setDestructionTimer(selfdestructAfterArg: number) {
|
||||||
|
if (selfdestructAfterArg) {
|
||||||
|
this.destructionTimer = new Timer(selfdestructAfterArg);
|
||||||
|
this.destructionTimer.start();
|
||||||
|
|
||||||
|
// set up self destruction by removing this from the parent messageCache
|
||||||
|
this.destructionTimer.completed.then(async () => {
|
||||||
|
this.universeCache.messageMap.remove(this);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.fallBackDestruction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles bad messages for further analysis
|
||||||
|
*/
|
||||||
|
handleAsBadMessage() {
|
||||||
|
console.log('received a bad message');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prevents memory leaks if channels have no default
|
||||||
|
*/
|
||||||
|
private fallBackDestruction() {
|
||||||
|
plugins.smartdelay.delayFor(1000).then(() => {
|
||||||
|
if (!this.destructionTimer) {
|
||||||
|
this.setDestructionTimer(6000);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
|
||||||
|
|
||||||
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { rxjs } from 'smartrx';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* universe store handles the creation, storage and retrieval of messages.
|
|
||||||
*/
|
|
||||||
export class UniverseStore {
|
|
||||||
public standardMessageExpiry: number;
|
|
||||||
public destructionTime: number = 60000;
|
|
||||||
public messageStore = new Objectmap<UniverseMessage>();
|
|
||||||
private lastId: number = 0; // stores the last id
|
|
||||||
|
|
||||||
constructor(standardMessageExpiryArg: number) {
|
|
||||||
this.standardMessageExpiry = standardMessageExpiryArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* add a message to the UniverseStore
|
|
||||||
* @param messageArg
|
|
||||||
* @param attachedPayloadArg
|
|
||||||
*/
|
|
||||||
public addMessage(messageArg, attachedPayloadArg) {
|
|
||||||
this.messageStore.add(
|
|
||||||
new UniverseMessage(this, messageArg, attachedPayloadArg, this.destructionTime)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read a message from the UniverseStore
|
|
||||||
*/
|
|
||||||
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
|
|
||||||
const messageObservable = rxjs.Observable.from(this.messageStore.getArray()).filter(
|
|
||||||
messageArg => {
|
|
||||||
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return messageObservable;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
|
||||||
|
|
||||||
import { Universe } from './index';
|
|
||||||
|
|
||||||
process.env.CLI = 'true';
|
|
||||||
|
|
||||||
const universeCli = new plugins.smartcli.Smartcli();
|
|
||||||
|
|
||||||
universeCli.standardTask().then(async argvArg => {
|
|
||||||
const standardUniverse = new Universe({
|
|
||||||
messageExpiryInMilliseconds: 60000
|
|
||||||
});
|
|
||||||
await standardUniverse.initServer(8765);
|
|
||||||
});
|
|
@ -1,21 +1,27 @@
|
|||||||
import * as lik from 'lik';
|
// node native
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as smartcli from 'smartcli';
|
|
||||||
import * as smartexpress from 'smartexpress';
|
export { path };
|
||||||
import * as smartfile from 'smartfile';
|
|
||||||
import * as smartq from 'smartq';
|
// pushrocks scope
|
||||||
import * as smartrequest from 'smartrequest';
|
import * as lik from '@pushrocks/lik';
|
||||||
import * as smartrx from 'smartrx';
|
import * as smarthash from '@pushrocks/smarthash';
|
||||||
import * as smartsocket from 'smartsocket';
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smarttime from 'smarttime';
|
import * as smartexpress from '@pushrocks/smartexpress';
|
||||||
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
import * as smartrx from '@pushrocks/smartrx';
|
||||||
|
import * as smartsocket from '@pushrocks/smartsocket';
|
||||||
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
lik,
|
lik,
|
||||||
path,
|
smarthash,
|
||||||
smartcli,
|
smartdelay,
|
||||||
smartexpress,
|
smartexpress,
|
||||||
smartfile,
|
smartfile,
|
||||||
smartq,
|
smartpromise,
|
||||||
smartrx,
|
smartrx,
|
||||||
smartrequest,
|
smartrequest,
|
||||||
smartsocket,
|
smartsocket,
|
||||||
|
23
tslint.json
23
tslint.json
@ -1,12 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": [
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
"tslint:latest",
|
|
||||||
"tslint-config-prettier"
|
|
||||||
],
|
|
||||||
"rules": {
|
"rules": {
|
||||||
"semicolon": [
|
"semicolon": [true, "always"],
|
||||||
true,
|
"no-console": false,
|
||||||
"always"
|
"ordered-imports": false,
|
||||||
]
|
"object-literal-sort-keys": false,
|
||||||
}
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user