Compare commits

..

6 Commits

Author SHA1 Message Date
0e0ffb7634 1.0.21 2018-05-26 13:44:32 +02:00
37bb052774 fix(UniverseMessage / UniverseChannel): better authentication for of messages for channels 2018-05-26 13:44:32 +02:00
3c7683d40e 1.0.20 2018-05-24 17:08:29 +02:00
c19f27e873 fix(dependencies): update 2018-05-24 17:08:28 +02:00
c1a03fec0f 1.0.19 2018-05-24 16:55:25 +02:00
8b650c5ea7 fix(UniverseChannel): improve channel handling 2018-05-24 16:55:24 +02:00
8 changed files with 162 additions and 69 deletions

52
package-lock.json generated
View File

@ -1,9 +1,33 @@
{
"name": "@pushrocks/smartuniverse",
"version": "1.0.18",
"version": "1.0.21",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@pushrocks/smartcli": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/@pushrocks/smartcli/-/smartcli-3.0.1.tgz",
"integrity": "sha512-NeLzHOAf2nCdgZq5QUNmiDE4xVo/HQktqxXmNGAf4kt6FnXlHe3pHVMjAB6mU7DmuoN0uY4qnkQAQYgFJ0MZ4A==",
"requires": {
"@types/yargs": "^11.0.0",
"beautylog": "^6.1.10",
"lik": "^2.0.5",
"rxjs": "^6.0.0",
"smartparam": "1.0.2",
"smartq": "^1.1.8",
"yargs": "^11.0.0"
},
"dependencies": {
"rxjs": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.2.0.tgz",
"integrity": "sha512-qBzf5uu6eOKiCZuAE0SgZ0/Qp+l54oeVxFfC2t+mJ2SFI6IB8gmMdJHs5DUMu5kqifqcCtsKS2XHjhZu6RKvAw==",
"requires": {
"tslib": "^1.9.0"
}
}
}
},
"@types/body-parser": {
"version": "1.17.0",
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.0.tgz",
@ -204,9 +228,9 @@
}
},
"@types/yargs": {
"version": "10.0.2",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-10.0.2.tgz",
"integrity": "sha512-VbsIazac1gy20qTjEZVgDUhs8uuVmGbFkSGcdHpcMoXSC4+0vn/PRHz9YBqpgxKwUi8qoxf3eHff07w7aKNBOg=="
"version": "11.0.0",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-11.0.0.tgz",
"integrity": "sha512-vFql3tOxs6clgh+WVoLW3nOkNGCdeKsMU6mQZkOerJpV/CR9Xc1c1lZ+kYU+hNSobrQIOcNovWfPFDJIhcG5Pw=="
},
"accepts": {
"version": "1.3.5",
@ -1885,21 +1909,6 @@
"chai-string": "^1.4.0"
}
},
"smartcli": {
"version": "2.0.12",
"resolved": "https://registry.npmjs.org/smartcli/-/smartcli-2.0.12.tgz",
"integrity": "sha512-oeHfzFJ6RRz7yNQbwNIXfnXMq71XIzuD8pc0aURxA883ocsPLtyLSfYkhZk7DjzIFb8LpZRge0cFO3Dk7frGDw==",
"requires": {
"@types/yargs": "^10.0.1",
"beautylog": "^6.1.10",
"lik": "^2.0.2",
"rxjs": "^5.4.3",
"smartparam": "1.0.2",
"smartq": "^1.1.6",
"typings-global": "^1.0.20",
"yargs": "^11.0.0"
}
},
"smartdelay": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/smartdelay/-/smartdelay-1.0.4.tgz",
@ -2264,6 +2273,11 @@
"yn": "^2.0.0"
}
},
"tslib": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.1.tgz",
"integrity": "sha512-avfPS28HmGLLc2o4elcc2EIq2FcH++Yo5YxpBZi9Yw93BCTGFthI4HPE4Rpep6vSYQaK8e69PelM44tPj+RaQg=="
},
"type-detect": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartuniverse",
"version": "1.0.18",
"version": "1.0.21",
"private": true,
"description": "messaging service for your micro services",
"main": "dist/index.js",
@ -19,10 +19,10 @@
"typescript": "^2.8.3"
},
"dependencies": {
"@pushrocks/smartcli": "^3.0.1",
"lik": "^2.0.5",
"nodehash": "^1.0.4",
"rxjs": "^5.5.8",
"smartcli": "^2.0.12",
"smartdelay": "^1.0.4",
"smartexpress": "^1.0.21",
"smartfile": "^4.2.28",

View File

@ -3,7 +3,7 @@ import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { UniverseStore } from './smartuniverse.classes.universestore';
import { UniverseCache } from './smartuniverse.classes.universecache';
import * as paths from './smartuniverse.paths';
@ -32,7 +32,7 @@ export interface IServerPutMessageRequestBody {
*/
export class Universe {
// subinstances
public universeStore: UniverseStore;
public universeCache: UniverseCache;
// options
private options: ISmartUniverseConstructorOptions;
@ -54,7 +54,7 @@ export class Universe {
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
}
/**
@ -70,10 +70,15 @@ export class Universe {
// message handling
// adds messages
const addMessageHandler = new Handler('PUT', request => {
const addMessageHandler = new Handler('PUT', async request => {
const requestBody: IServerPutMessageRequestBody = request.body;
const message = new UniverseMessage(requestBody.message, requestBody.channel, requestBody.passphrase, requestBody.payload);
this.universeStore.addMessage(message);
const message = new UniverseMessage(
requestBody.message,
requestBody.channel,
requestBody.passphrase,
requestBody.payload
);
this.universeCache.addMessage(message);
console.log(requestBody);
return true;
});
@ -82,7 +87,7 @@ export class Universe {
const readMessageHandler = new Handler('GET', request => {
const done = plugins.smartq.defer<UniverseMessage[]>();
const requestBody = request.body;
const messageObservable = this.universeStore.readMessagesYoungerThan(requestBody.since);
const messageObservable = this.universeCache.readMessagesYoungerThan(requestBody.since);
messageObservable.toArray().subscribe(universeMessageArrayArg => {
done.resolve(universeMessageArrayArg);
});

View File

@ -1,5 +1,6 @@
import * as plugins from './smartuniverse.plugins';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { Objectmap } from 'lik';
@ -10,10 +11,25 @@ import { rxjs } from 'smartrx';
/**
* universe store handles the creation, storage and retrieval of messages.
*/
export class UniverseStore {
export class UniverseCache {
public standardMessageExpiry: number;
public destructionTime: number = 60000;
public messageStore = new Objectmap<UniverseMessage>();
/**
* stores messages for this instance
*/
public messageCache = new Objectmap<UniverseMessage>();
/**
* stores the channels that are available within the universe
*/
public channelCache = new Objectmap<UniverseChannel>();
/**
* allows messages to be processed in a blacklist mode for further analysis
*/
public blackListChannel = new UniverseChannel(this, 'blacklist', 'nada');
private lastId: number = 0; // stores the last id
constructor(standardMessageExpiryArg: number) {
@ -25,15 +41,17 @@ export class UniverseStore {
* @param messageArg
* @param attachedPayloadArg
*/
public addMessage(messageArg: UniverseMessage) {
this.messageStore.add(messageArg);
public async addMessage(messageArg: UniverseMessage) {
messageArg.setUniverseCache(this);
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
this.messageCache.add(messageArg);
}
/**
* Read a message from the UniverseStore
*/
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
const messageObservable = rxjs.Observable.from(this.messageStore.getArray()).filter(
const messageObservable = rxjs.Observable.from(this.messageCache.getArray()).filter(
messageArg => {
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
}

View File

@ -1,63 +1,89 @@
import * as plugins from './smartuniverse.plugins';
import { Objectmap } from 'lik';
import { UniverseCache } from './smartuniverse.classes.universecache';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
/**
* enables messages to stay within a certain scope.
*/
export class UniverseChannel {
/**
* stores the channels that are available within the universe
*/
public static channelStore = new Objectmap<UniverseChannel>();
/**
* allows messages to be processed in a blacklist mode for further analysis
*/
public static blackListChannel = new UniverseChannel('blacklist', 'nada');
// ======
// STATIC
// ======
/**
* creates new channels
* @param channelArg the name of the topic
* @param passphraseArg the secret thats used for a certain topic.
*/
public static createChannel (channelNameArg: string, passphraseArg: string) {
const newChannel = new UniverseChannel(channelNameArg, passphraseArg);
public static createChannel(
universeCacheArg: UniverseCache,
channelNameArg: string,
passphraseArg: string
) {
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
return newChannel;
};
}
public static authorizeForChannel (channelNameArg: string, passphraseArg: string) {
const foundChannel = this.channelStore.find(universeChannel => {
const result = universeChannel.authenticate(channelNameArg, passphraseArg);
return result;
/**
* returns boolean wether certain channel exists
*/
public static async doesChannelExists(universeCacheArg: UniverseCache, channelNameArg: string) {
const channel = universeCacheArg.channelCache.find(channelArg => {
return channelArg.name === channelNameArg;
});
if(foundChannel) {
if (channel) {
return true;
} else {
return false;
}
}
public static authorizeAMessageForAChannel(
universeCacheArg: UniverseCache,
universeMessageArg: UniverseMessage
) {
const foundChannel = universeCacheArg.channelCache.find(universeChannel => {
const result = universeChannel.authenticate(universeMessageArg);
return result;
});
if (foundChannel) {
universeMessageArg.authenticated = true;
universeMessageArg.universeChannelList.add(foundChannel);
return foundChannel;
} else {
return this.blackListChannel;
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(channelNameArg: string, passphraseArg: string) {
constructor(universeCacheArg: UniverseCache, channelNameArg: string, passphraseArg: string) {
this.name = channelNameArg;
this.passphrase = passphraseArg;
UniverseChannel.channelStore.add(this);
}
/**
* authenticates a client on the server side
*/
public authenticate(channelNameArg: string, passphraseArg: string): boolean {
return (this.name === channelNameArg && this.passphrase === passphraseArg);
public authenticate(universeMessageArg: UniverseMessage): boolean {
return (
this.name === universeMessageArg.requestedChannelName &&
this.passphrase === universeMessageArg.requestedChannelPassphrase
);
}
}

View File

@ -1,12 +1,16 @@
import * as plugins from './smartuniverse.plugins';
import { Objectmap } from 'lik';
import { Timer, TimeStamp } from 'smarttime';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseStore } from './smartuniverse.classes.universestore';
import { Universe } from './smartuniverse.classes.universe';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseCache } from './smartuniverse.classes.universecache';
/**
* represents a message within a universe
* acts as a container to save message states like authentication status
*/
export class UniverseMessage {
/**
@ -18,13 +22,25 @@ export class UniverseMessage {
public id: number;
/**
* the universe store the message is attached to
* the UniverseCache the message is attached to
*/
public universeStore: UniverseStore;
public universeCache: UniverseCache;
/**
* requestedChannelName
*/
public requestedChannelName: string;
public requestedChannelPassphrase: string;
/**
* enables unprotected grouping of messages for efficiency purposes.
*/
public universeChannel: UniverseChannel;
public universeChannelList = new Objectmap<UniverseChannel>();
/**
* wether the message is authenticated
*/
public authenticated: boolean = null;
/**
* time of creation
@ -47,16 +63,23 @@ export class UniverseMessage {
* @param messageArg
* @param attachedPayloadArg
*/
constructor(messageArg: string, channelNameArg: string, passphraseArg: string, attachedPayloadArg: any) {
constructor(
messageArg: string,
requestedChannelNameArg: string,
passphraseArg: string,
attachedPayloadArg: any
) {
this.timestamp = new TimeStamp();
this.message = messageArg;
this.universeChannel = UniverseChannel.authorizeForChannel(channelNameArg, passphraseArg);
this.requestedChannelName = requestedChannelNameArg;
this.requestedChannelPassphrase = passphraseArg;
this.attachedPayload = attachedPayloadArg;
// prevent memory issues
this.fallBackDestruction();
}
public setUniverseStore(universeStoreArg: UniverseStore) {
this.universeStore = universeStoreArg;
public setUniverseCache(universeCacheArg: UniverseCache) {
this.universeCache = universeCacheArg;
}
public setDestructionTimer(selfdestructAfterArg: number) {
@ -64,15 +87,22 @@ export class UniverseMessage {
this.destructionTimer = new Timer(selfdestructAfterArg);
this.destructionTimer.start();
// set up self destruction by removing this from the parent messageStore
// set up self destruction by removing this from the parent messageCache
this.destructionTimer.completed.then(async () => {
this.universeStore.messageStore.remove(this);
this.universeCache.messageCache.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
*/

View File

@ -6,7 +6,7 @@ process.env.CLI = 'true';
const universeCli = new plugins.smartcli.Smartcli();
universeCli.standardTask().then(async argvArg => {
universeCli.standardTask().subscribe(async argvArg => {
const standardUniverse = new Universe({
messageExpiryInMilliseconds: 60000
});

View File

@ -1,7 +1,7 @@
import * as smartcli from '@pushrocks/smartcli';
import * as lik from 'lik';
import * as nodehash from 'nodehash';
import * as path from 'path';
import * as smartcli from 'smartcli';
import * as smartdelay from 'smartdelay';
import * as smartexpress from 'smartexpress';
import * as smartfile from 'smartfile';