Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
7693b52066 | |||
30a02ae48b | |||
241182ed2e | |||
3d82038ec3 | |||
300d62ed12 | |||
a5e849aa17 | |||
83807d7c5c | |||
39d3a0f2f8 | |||
904a48d414 | |||
e2acb28756 | |||
92e4379bd2 | |||
e0ce732ee1 | |||
98be0f036c | |||
54fca17142 | |||
1a7634e8db | |||
8830b825ac | |||
123324bf43 | |||
4761ff31cf | |||
430c3ea13a | |||
6dd3782b0d | |||
06aa721247 | |||
f728c56016 | |||
f512acdfaa | |||
331a098851 |
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.
|
21
package-lock.json
generated
21
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartuniverse",
|
||||
"version": "1.0.28",
|
||||
"version": "1.0.41",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -386,6 +386,17 @@
|
||||
"luxon": "^1.12.1"
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartunique": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@pushrocks/smartunique/-/smartunique-3.0.1.tgz",
|
||||
"integrity": "sha512-xBu9ZB4C0BA0S/pbFFZn2ItPfnodPKpzrYIq1yN5XDs6OaookwcDF/iBwfS9+EYMSPENC9wAsOxg2RGMm4Qicw==",
|
||||
"requires": {
|
||||
"@types/shortid": "^0.0.29",
|
||||
"@types/uuid": "^3.0.0",
|
||||
"shortid": "^2.2.8",
|
||||
"uuid": "^3.1.0"
|
||||
}
|
||||
},
|
||||
"@pushrocks/tapbundle": {
|
||||
"version": "3.0.9",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.0.9.tgz",
|
||||
@ -566,6 +577,14 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/uuid": {
|
||||
"version": "3.4.4",
|
||||
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-3.4.4.tgz",
|
||||
"integrity": "sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==",
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/vinyl": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fvinyl/-/vinyl-2.0.3.tgz",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartuniverse",
|
||||
"version": "1.0.28",
|
||||
"version": "1.0.41",
|
||||
"private": false,
|
||||
"description": "messaging service for your micro services",
|
||||
"main": "dist/index.js",
|
||||
@ -33,6 +33,7 @@
|
||||
"@pushrocks/smartrequest": "^1.1.14",
|
||||
"@pushrocks/smartrx": "^2.0.3",
|
||||
"@pushrocks/smartsocket": "^1.1.27",
|
||||
"@pushrocks/smarttime": "^3.0.7"
|
||||
"@pushrocks/smarttime": "^3.0.7",
|
||||
"@pushrocks/smartunique": "^3.0.1"
|
||||
}
|
||||
}
|
||||
|
38
readme.md
38
readme.md
@ -1,13 +1,16 @@
|
||||
# @pushrocks/smartuniverse
|
||||
|
||||
messaging service for micro services
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartuniverse)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartuniverse)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartuniverse/)
|
||||
|
||||
- [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
||||
- [gitlab.com (source)](https://gitlab.com/pushrocks/smartuniverse)
|
||||
- [github.com (source mirror)](https://github.com/pushrocks/smartuniverse)
|
||||
- [docs (typedoc)](https://pushrocks.gitlab.io/smartuniverse/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://gitlab.com/pushrocks/smartuniverse/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartuniverse/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartuniverse)
|
||||
@ -20,9 +23,34 @@ messaging service for micro services
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
### What is smartuniverse all about?
|
||||
|
||||
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**.
|
||||
|
||||
### Server side
|
||||
every universe has a server that manages messages.
|
||||
Think Kafka, but without Kafka.
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
### Client side
|
||||
All your microservices represents clients in the universe that may talk to each other using the universe server.
|
||||
|
||||
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://maintainedby.lossless.com)
|
||||
|
22
test/test.ts
22
test/test.ts
@ -5,9 +5,14 @@ import * as smartuniverse from '../ts/index';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
let testUniverse: smartuniverse.Universe;
|
||||
let testUniverseClient: smartuniverse.ClientUniverse;
|
||||
let testClientUniverse: smartuniverse.ClientUniverse;
|
||||
let testClientChannel: smartuniverse.ClientUniverseChannel;
|
||||
|
||||
const testChannelData = {
|
||||
channelName: 'awesomeTestChannel',
|
||||
channelPass: 'awesomeChannelPAss'
|
||||
};
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testUniverse = new smartuniverse.Universe({
|
||||
messageExpiryInMilliseconds: 1000
|
||||
@ -15,15 +20,15 @@ tap.test('first test', async () => {
|
||||
});
|
||||
|
||||
tap.test('add a message to the SmartUniverse', async () => {
|
||||
await testUniverse.initServer(8765);
|
||||
await testUniverse.start(8765);
|
||||
});
|
||||
|
||||
// testing message handling
|
||||
tap.test('create smartuniverse client', async () => {
|
||||
testUniverseClient = new smartuniverse.ClientUniverse({
|
||||
testClientUniverse = new smartuniverse.ClientUniverse({
|
||||
serverAddress: 'http://localhost:8765'
|
||||
});
|
||||
expect(testUniverseClient).to.be.instanceof(smartuniverse.ClientUniverse);
|
||||
expect(testClientUniverse).to.be.instanceof(smartuniverse.ClientUniverse);
|
||||
});
|
||||
|
||||
tap.test('should add a channel to the universe', async () => {
|
||||
@ -31,20 +36,21 @@ tap.test('should add a channel to the universe', async () => {
|
||||
});
|
||||
|
||||
tap.test('should get a observable correctly', async () => {
|
||||
testClientChannel = await testUniverseClient.getChannel('testChannel');
|
||||
testClientChannel = await testClientUniverse.getChannel(testChannelData.channelName);
|
||||
expect(testClientChannel).to.be.instanceof(smartuniverse.ClientUniverseChannel);
|
||||
});
|
||||
|
||||
tap.test('should send a message correctly', async () => {
|
||||
await testUniverseClient.sendMessage('greeting', {
|
||||
anyBool: true
|
||||
await testClientUniverse.sendMessage({
|
||||
messageText: 'hello',
|
||||
targetChannelName: 'channel1'
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should receive a message correctly', async () => {});
|
||||
|
||||
tap.test('should disconnect the client correctly', async () => {
|
||||
testUniverseClient.close();
|
||||
testClientUniverse.close();
|
||||
});
|
||||
|
||||
tap.test('should end the server correctly', async tools => {
|
||||
|
@ -7,4 +7,5 @@ export * from './smartuniverse.classes.universe';
|
||||
export * from './smartuniverse.classes.universecache';
|
||||
export * from './smartuniverse.classes.universechannel';
|
||||
export * from './smartuniverse.classes.universemessage';
|
||||
export * from './smartuniverse.interfaces';
|
||||
|
||||
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 {}
|
15
ts/interfaces/universemessage.interfaces.ts
Normal file
15
ts/interfaces/universemessage.interfaces.ts
Normal file
@ -0,0 +1,15 @@
|
||||
export interface IMessageCreator {
|
||||
messageText: string;
|
||||
payload?: string | number | any;
|
||||
payloadStringType?: 'Buffer' | 'string' | 'object';
|
||||
targetChannelName: string;
|
||||
}
|
||||
|
||||
export interface IUniverseMessage extends IMessageCreator {
|
||||
id: string;
|
||||
/**
|
||||
* time of creation
|
||||
*/
|
||||
timestamp: number;
|
||||
passphrase: string;
|
||||
}
|
@ -5,10 +5,10 @@ import { Observable } from 'rxjs';
|
||||
import { Smartsocket, SmartsocketClient } from '@pushrocks/smartsocket';
|
||||
import * as url from 'url';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import {
|
||||
ClientUniverseChannel,
|
||||
IServerGetMessagesRequestBody,
|
||||
IServerPutMessageRequestBody,
|
||||
UniverseMessage
|
||||
} from './';
|
||||
|
||||
@ -31,32 +31,62 @@ export class ClientUniverse {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
public async sendMessage(messageArg, payloadArg) {
|
||||
const requestBody = {
|
||||
message: messageArg,
|
||||
payload: payloadArg
|
||||
};
|
||||
// TODO: User websocket connection if available
|
||||
await plugins.smartrequest.postJson(this.options.serverAddress, {
|
||||
requestBody
|
||||
});
|
||||
/**
|
||||
* adds a channel to the channelcache
|
||||
* TODO: verify channel before adding it to the channel cache
|
||||
*/
|
||||
public async addChannel (channelNameArg: string, passphraseArg: string) {
|
||||
const existingChannel = this.getChannel(channelNameArg);
|
||||
|
||||
if (existingChannel) {
|
||||
throw new Error('channel exists');
|
||||
}
|
||||
|
||||
// lets create the channel
|
||||
ClientUniverseChannel.createClientUniverseChannel(
|
||||
this,
|
||||
channelNameArg,
|
||||
passphraseArg
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a channel from the channelcache
|
||||
* @param channelName
|
||||
* @param passphraseArg
|
||||
*/
|
||||
public async getChannel(channelName: string): Promise<ClientUniverseChannel> {
|
||||
await this.checkConnection();
|
||||
const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel(
|
||||
this,
|
||||
channelName
|
||||
);
|
||||
this.channelCache.add(clientUniverseChannel);
|
||||
const clientUniverseChannel = this.channelCache.find(channel => {
|
||||
return channel.name === channelName;
|
||||
})
|
||||
return clientUniverseChannel;
|
||||
}
|
||||
|
||||
public async sendMessage(messageArg: interfaces.IMessageCreator) {
|
||||
const requestBody: interfaces.IUniverseMessage = {
|
||||
id: plugins.smartunique.shortId(),
|
||||
timestamp: Date.now(),
|
||||
passphrase: (await this.getChannel(messageArg.targetChannelName)).passphrase,
|
||||
...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 close() {
|
||||
this.socketClient.disconnect();
|
||||
}
|
||||
|
||||
private async checkConnection() {
|
||||
/**
|
||||
* checks the connection towards a universe server
|
||||
* since password validation is done through other means, a connection should always be possible
|
||||
*/
|
||||
private async checkConnection(): Promise<void> {
|
||||
if (!this.socketClient && !this.observableIntake) {
|
||||
const parsedURL = url.parse(this.options.serverAddress);
|
||||
this.socketClient = new SmartsocketClient({
|
||||
|
@ -1,17 +1,26 @@
|
||||
import * as plugins from './smartuniverse.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { ClientUniverse, IUniverseChannel } from './';
|
||||
import { ClientUniverse } from './';
|
||||
|
||||
export class ClientUniverseChannel implements IUniverseChannel {
|
||||
export class ClientUniverseChannel implements interfaces.IUniverseChannel {
|
||||
// ======
|
||||
// STATIC
|
||||
// ======
|
||||
/**
|
||||
* creates a channel and adds it to the cache of clientUniverseArg
|
||||
* @param clientUniverseArg
|
||||
* @param channelNameArg
|
||||
* @param passphraseArg
|
||||
*/
|
||||
public static async createClientUniverseChannel(
|
||||
clientUniverseArg: ClientUniverse,
|
||||
channelName: string
|
||||
channelNameArg: string,
|
||||
passphraseArg: string
|
||||
): Promise<ClientUniverseChannel> {
|
||||
const clientChannel = new ClientUniverseChannel(clientUniverseArg);
|
||||
await clientChannel.transmitSubscription();
|
||||
const clientChannel = new ClientUniverseChannel(clientUniverseArg, channelNameArg, passphraseArg);
|
||||
clientUniverseArg.channelCache.add(clientChannel);
|
||||
await clientChannel.subscribe();
|
||||
return clientChannel;
|
||||
}
|
||||
|
||||
@ -19,16 +28,24 @@ export class ClientUniverseChannel implements IUniverseChannel {
|
||||
// INSTANCE
|
||||
// ========
|
||||
|
||||
// properties
|
||||
public name: string;
|
||||
public passphrase: string;
|
||||
|
||||
// refs
|
||||
public clientUniverse: ClientUniverse;
|
||||
|
||||
constructor(clientUniverseArg: ClientUniverse) {
|
||||
constructor(clientUniverseArg: ClientUniverse, nameArg: string, passphraseArg: string) {
|
||||
this.clientUniverse = clientUniverseArg;
|
||||
this.name = nameArg;
|
||||
this.passphrase = passphraseArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* subscribes to a channel
|
||||
* tells the universe about this instances interest into a channel
|
||||
*/
|
||||
public async transmitSubscription() {
|
||||
public async subscribe() {
|
||||
this.clientUniverse.socketClient;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,32 @@
|
||||
import * as plugins from './smartuniverse.plugins';
|
||||
|
||||
import { IUniverseMessage } from './';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
export class ClientUniverseMessage implements IUniverseMessage {
|
||||
export class ClientUniverseMessage implements interfaces.IUniverseMessage {
|
||||
// ======
|
||||
// STATIC
|
||||
// ======
|
||||
createMessage(messageArg: string, payloadArg: any) {}
|
||||
public static createMessageFromPayload(messageDescriptor: interfaces.IUniverseMessage) {
|
||||
|
||||
};
|
||||
|
||||
// ========
|
||||
// INSTANCE
|
||||
// ========
|
||||
|
||||
public id: string;
|
||||
|
||||
public timestamp: number;
|
||||
public smartTimestamp: plugins.smarttime.TimeStamp;
|
||||
|
||||
public messageText: string;
|
||||
public passphrase: string;
|
||||
public payload: any;
|
||||
public payloadStringType;
|
||||
public targetChannelName: string;
|
||||
constructor(messageArg, payloadArg) {}
|
||||
|
||||
getAsJsonForPayload () {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -5,25 +5,13 @@ import { UniverseCache, UniverseChannel, UniverseMessage } from './';
|
||||
|
||||
import * as paths from './smartuniverse.paths';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
export interface ISmartUniverseConstructorOptions {
|
||||
messageExpiryInMilliseconds: number;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* main class that setsup a Universe
|
||||
@ -35,9 +23,16 @@ export class Universe {
|
||||
// options
|
||||
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 get universeVersion() {
|
||||
|
||||
/**
|
||||
* get the currently running version of smartuniverse
|
||||
*/
|
||||
public get universeVersion() {
|
||||
if (this.universeVersionStore) {
|
||||
return this.universeVersionStore;
|
||||
} else {
|
||||
@ -47,7 +42,14 @@ export class Universe {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* the smartexpress server used
|
||||
*/
|
||||
private smartexpressServer: plugins.smartexpress.Server;
|
||||
|
||||
/**
|
||||
* the smartsocket used
|
||||
*/
|
||||
private smartsocket: plugins.smartsocket.Smartsocket;
|
||||
|
||||
constructor(optionsArg: ISmartUniverseConstructorOptions) {
|
||||
@ -59,14 +61,14 @@ export class Universe {
|
||||
* adds a channel to the Universe
|
||||
*/
|
||||
public async addChannel(nameArg: string, passphraseArg: string) {
|
||||
const newChannel = new UniverseChannel(this.universeCache, nameArg, passphraseArg);
|
||||
this.universeCache.channelMap.add(newChannel);
|
||||
const newChannel = UniverseChannel.createChannel(this.universeCache, nameArg, passphraseArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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({
|
||||
cors: true,
|
||||
defaultAnswer: async () => {
|
||||
@ -76,31 +78,47 @@ export class Universe {
|
||||
port: portArg
|
||||
});
|
||||
|
||||
// lets create the http request route
|
||||
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
|
||||
const universeMessageInstance = new UniverseMessage(req.body);
|
||||
this.universeCache.addMessage(universeMessageInstance);
|
||||
}));
|
||||
|
||||
// add websocket upgrade
|
||||
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
||||
port: 12345 // fix this within smartsocket
|
||||
});
|
||||
|
||||
// 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: () => {}
|
||||
funcDef: () => {
|
||||
console.log('a client connected');
|
||||
} // TODO: implement an action upon connection of clients
|
||||
});
|
||||
|
||||
// add smartsocket to the running smartexpress app
|
||||
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
|
||||
// should work with express as well
|
||||
|
||||
// start the socket
|
||||
this.smartsocket.start();
|
||||
|
||||
// start the smartexpress instance
|
||||
await this.smartexpressServer.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* stop everything
|
||||
*/
|
||||
public async stopServer() {
|
||||
await this.smartsocket.stop();
|
||||
await this.smartexpressServer.stop();
|
||||
|
@ -39,7 +39,7 @@ export class UniverseCache {
|
||||
}
|
||||
|
||||
/**
|
||||
* add a message to the UniverseStore
|
||||
* add a message to the UniverseCache
|
||||
* @param messageArg
|
||||
* @param attachedPayloadArg
|
||||
*/
|
||||
@ -50,12 +50,12 @@ export class UniverseCache {
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a message from the UniverseStore
|
||||
* 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 messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
||||
})
|
||||
);
|
||||
return messageObservable;
|
||||
|
@ -23,6 +23,7 @@ export class UniverseChannel {
|
||||
passphraseArg: string
|
||||
) {
|
||||
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
|
||||
universeCacheArg.channelMap.add(newChannel);
|
||||
return newChannel;
|
||||
}
|
||||
|
||||
@ -82,8 +83,8 @@ export class UniverseChannel {
|
||||
*/
|
||||
public authenticate(universeMessageArg: UniverseMessage): boolean {
|
||||
return (
|
||||
this.name === universeMessageArg.requestedChannelName &&
|
||||
this.passphrase === universeMessageArg.requestedChannelPassphrase
|
||||
this.name === universeMessageArg.targetChannelName &&
|
||||
this.passphrase === universeMessageArg.passphrase
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartuniverse.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { Objectmap } from '@pushrocks/lik';
|
||||
|
||||
@ -6,31 +7,30 @@ import { Timer, TimeStamp } from '@pushrocks/smarttime';
|
||||
import { Universe } from './smartuniverse.classes.universe';
|
||||
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
||||
import { UniverseCache } from './smartuniverse.classes.universecache';
|
||||
import { IUniverseMessage } from './interfaces';
|
||||
|
||||
/**
|
||||
* represents a message within a universe
|
||||
* acts as a container to save message states like authentication status
|
||||
*/
|
||||
export class UniverseMessage {
|
||||
/**
|
||||
* public and unique id
|
||||
* numeric ascending
|
||||
* adheres to time in milliseconds
|
||||
* avoids duplications though
|
||||
*/
|
||||
public id: number;
|
||||
export class UniverseMessage implements interfaces.IUniverseMessage {
|
||||
|
||||
public id: string;
|
||||
|
||||
public timestamp: number;
|
||||
public smartTimestamp: TimeStamp;
|
||||
|
||||
public messageText: string;
|
||||
public passphrase: string;
|
||||
public payload: any;
|
||||
public payloadStringType;
|
||||
public targetChannelName: string;
|
||||
|
||||
/**
|
||||
* the UniverseCache the message is attached to
|
||||
*/
|
||||
public universeCache: UniverseCache;
|
||||
|
||||
/**
|
||||
* requestedChannelName
|
||||
*/
|
||||
public requestedChannelName: string;
|
||||
public requestedChannelPassphrase: string;
|
||||
|
||||
/**
|
||||
* enables unprotected grouping of messages for efficiency purposes.
|
||||
*/
|
||||
@ -42,19 +42,8 @@ export class UniverseMessage {
|
||||
public authenticated: boolean = null;
|
||||
|
||||
/**
|
||||
* time of creation
|
||||
* a destruction timer for this message
|
||||
*/
|
||||
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
|
||||
|
||||
/**
|
||||
@ -62,17 +51,12 @@ export class UniverseMessage {
|
||||
* @param messageArg
|
||||
* @param attachedPayloadArg
|
||||
*/
|
||||
constructor(
|
||||
messageArg: string,
|
||||
requestedChannelNameArg: string,
|
||||
passphraseArg: string,
|
||||
attachedPayloadArg: any
|
||||
) {
|
||||
this.timestamp = new TimeStamp();
|
||||
this.message = messageArg;
|
||||
this.requestedChannelName = requestedChannelNameArg;
|
||||
this.requestedChannelPassphrase = passphraseArg;
|
||||
this.attachedPayload = attachedPayloadArg;
|
||||
constructor(messageDescriptor: IUniverseMessage) {
|
||||
this.smartTimestamp = new TimeStamp(this.timestamp);
|
||||
this.messageText = messageDescriptor.messageText;
|
||||
this.targetChannelName = messageDescriptor.targetChannelName;
|
||||
this.passphrase = messageDescriptor.passphrase;
|
||||
this.payload = messageDescriptor.payload;
|
||||
// prevent memory issues
|
||||
this.fallBackDestruction();
|
||||
}
|
||||
@ -98,7 +82,7 @@ export class UniverseMessage {
|
||||
/**
|
||||
* handles bad messages for further analysis
|
||||
*/
|
||||
handleAsBadMessage() {
|
||||
public handleAsBadMessage() {
|
||||
console.log('received a bad message');
|
||||
}
|
||||
|
||||
|
@ -1,3 +0,0 @@
|
||||
export interface IUniverseChannel {}
|
||||
|
||||
export interface IUniverseMessage {}
|
@ -14,6 +14,7 @@ import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
import * as smartsocket from '@pushrocks/smartsocket';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
|
||||
export {
|
||||
lik,
|
||||
@ -25,5 +26,6 @@ export {
|
||||
smartrx,
|
||||
smartrequest,
|
||||
smartsocket,
|
||||
smarttime
|
||||
smarttime,
|
||||
smartunique
|
||||
};
|
||||
|
Reference in New Issue
Block a user