Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
0e0ffb7634 | |||
37bb052774 | |||
3c7683d40e | |||
c19f27e873 | |||
c1a03fec0f | |||
8b650c5ea7 | |||
4fc6e327ec | |||
7991baf2bf |
2505
package-lock.json
generated
Normal file
2505
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartuniverse",
|
"name": "@pushrocks/smartuniverse",
|
||||||
"version": "1.0.17",
|
"version": "1.0.21",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "messaging service for your micro services",
|
"description": "messaging service for your micro services",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -14,13 +14,15 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^9.6.0",
|
"@types/node": "^9.6.0",
|
||||||
"tapbundle": "^2.0.0"
|
"tapbundle": "^2.0.0",
|
||||||
|
"ts-node": "^6.0.3",
|
||||||
|
"typescript": "^2.8.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartcli": "^3.0.1",
|
||||||
"lik": "^2.0.5",
|
"lik": "^2.0.5",
|
||||||
"nodehash": "^1.0.4",
|
"nodehash": "^1.0.4",
|
||||||
"rxjs": "^5.5.8",
|
"rxjs": "^5.5.8",
|
||||||
"smartcli": "^2.0.12",
|
|
||||||
"smartdelay": "^1.0.4",
|
"smartdelay": "^1.0.4",
|
||||||
"smartexpress": "^1.0.21",
|
"smartexpress": "^1.0.21",
|
||||||
"smartfile": "^4.2.28",
|
"smartfile": "^4.2.28",
|
||||||
|
@ -3,7 +3,7 @@ import * as plugins from './smartuniverse.plugins';
|
|||||||
import { Handler, Route, Server } from 'smartexpress';
|
import { Handler, Route, Server } from 'smartexpress';
|
||||||
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
||||||
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
||||||
import { UniverseStore } from './smartuniverse.classes.universestore';
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
||||||
|
|
||||||
import * as paths from './smartuniverse.paths';
|
import * as paths from './smartuniverse.paths';
|
||||||
|
|
||||||
@ -17,6 +17,9 @@ export interface IServerGetMessagesRequestBody {
|
|||||||
youngerThan: number;
|
youngerThan: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the interface for a standard request
|
||||||
|
*/
|
||||||
export interface IServerPutMessageRequestBody {
|
export interface IServerPutMessageRequestBody {
|
||||||
channel: string;
|
channel: string;
|
||||||
passphrase: string;
|
passphrase: string;
|
||||||
@ -24,9 +27,12 @@ export interface IServerPutMessageRequestBody {
|
|||||||
payload: any;
|
payload: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main class that setsup a Universe
|
||||||
|
*/
|
||||||
export class Universe {
|
export class Universe {
|
||||||
// subinstances
|
// subinstances
|
||||||
public universeStore: UniverseStore;
|
public universeCache: UniverseCache;
|
||||||
|
|
||||||
// options
|
// options
|
||||||
private options: ISmartUniverseConstructorOptions;
|
private options: ISmartUniverseConstructorOptions;
|
||||||
@ -48,7 +54,7 @@ export class Universe {
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,10 +70,15 @@ export class Universe {
|
|||||||
|
|
||||||
// message handling
|
// message handling
|
||||||
// adds messages
|
// adds messages
|
||||||
const addMessageHandler = new Handler('PUT', request => {
|
const addMessageHandler = new Handler('PUT', async request => {
|
||||||
const requestBody: IServerPutMessageRequestBody = request.body;
|
const requestBody: IServerPutMessageRequestBody = request.body;
|
||||||
const message = new UniverseMessage(requestBody.message, requestBody.payload);
|
const message = new UniverseMessage(
|
||||||
this.universeStore.addMessage(message);
|
requestBody.message,
|
||||||
|
requestBody.channel,
|
||||||
|
requestBody.passphrase,
|
||||||
|
requestBody.payload
|
||||||
|
);
|
||||||
|
this.universeCache.addMessage(message);
|
||||||
console.log(requestBody);
|
console.log(requestBody);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
@ -76,7 +87,7 @@ export class Universe {
|
|||||||
const readMessageHandler = new Handler('GET', request => {
|
const readMessageHandler = new Handler('GET', request => {
|
||||||
const done = plugins.smartq.defer<UniverseMessage[]>();
|
const done = plugins.smartq.defer<UniverseMessage[]>();
|
||||||
const requestBody = request.body;
|
const requestBody = request.body;
|
||||||
const messageObservable = this.universeStore.readMessagesYoungerThan(requestBody.since);
|
const messageObservable = this.universeCache.readMessagesYoungerThan(requestBody.since);
|
||||||
messageObservable.toArray().subscribe(universeMessageArrayArg => {
|
messageObservable.toArray().subscribe(universeMessageArrayArg => {
|
||||||
done.resolve(universeMessageArrayArg);
|
done.resolve(universeMessageArrayArg);
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
||||||
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
import { UniverseMessage } from './smartuniverse.classes.universemessage';
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from 'lik';
|
||||||
@ -10,10 +11,25 @@ import { rxjs } from 'smartrx';
|
|||||||
/**
|
/**
|
||||||
* universe store handles the creation, storage and retrieval of messages.
|
* universe store handles the creation, storage and retrieval of messages.
|
||||||
*/
|
*/
|
||||||
export class UniverseStore {
|
export class UniverseCache {
|
||||||
public standardMessageExpiry: number;
|
public standardMessageExpiry: number;
|
||||||
public destructionTime: number = 60000;
|
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
|
private lastId: number = 0; // stores the last id
|
||||||
|
|
||||||
constructor(standardMessageExpiryArg: number) {
|
constructor(standardMessageExpiryArg: number) {
|
||||||
@ -25,15 +41,17 @@ export class UniverseStore {
|
|||||||
* @param messageArg
|
* @param messageArg
|
||||||
* @param attachedPayloadArg
|
* @param attachedPayloadArg
|
||||||
*/
|
*/
|
||||||
public addMessage(messageArg: UniverseMessage) {
|
public async addMessage(messageArg: UniverseMessage) {
|
||||||
this.messageStore.add(messageArg);
|
messageArg.setUniverseCache(this);
|
||||||
|
UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
|
||||||
|
this.messageCache.add(messageArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a message from the UniverseStore
|
* Read a message from the UniverseStore
|
||||||
*/
|
*/
|
||||||
public readMessagesYoungerThan(unixTimeArg?: number): Observable<UniverseMessage> {
|
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 => {
|
messageArg => {
|
||||||
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
return messageArg.timestamp.isYoungerThanMilliSeconds(this.destructionTime);
|
||||||
}
|
}
|
@ -1,46 +1,89 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
import { Objectmap } from 'lik';
|
import { Objectmap } from '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 passphraseArg the secret thats used for a certain topic.
|
* @param passphraseArg the secret thats used for a certain topic.
|
||||||
*/
|
*/
|
||||||
public static createChannel = (channelNameArg: string, passphraseArg: string) => {
|
public static createChannel(
|
||||||
const newChannel = new UniverseChannel(channelNameArg, passphraseArg);
|
universeCacheArg: UniverseCache,
|
||||||
|
channelNameArg: string,
|
||||||
|
passphraseArg: string
|
||||||
|
) {
|
||||||
|
const newChannel = new UniverseChannel(universeCacheArg, channelNameArg, passphraseArg);
|
||||||
return newChannel;
|
return newChannel;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (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 {
|
||||||
|
universeMessageArg.authenticated = false;
|
||||||
|
universeMessageArg.universeChannelList.add(universeCacheArg.blackListChannel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ========
|
||||||
|
// INSTANCE
|
||||||
|
// ========
|
||||||
/**
|
/**
|
||||||
* the name of the channel
|
* the name of the channel
|
||||||
*/
|
*/
|
||||||
public name: string;
|
public name: string;
|
||||||
|
public universeCacheInstance: UniverseCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the passphrase for the channel
|
* the passphrase for the channel
|
||||||
*/
|
*/
|
||||||
public passphrase: string;
|
public passphrase: string;
|
||||||
|
|
||||||
constructor(channelNameArg: string, passphraseArg: string) {
|
constructor(universeCacheArg: UniverseCache, channelNameArg: string, passphraseArg: string) {
|
||||||
this.name = channelNameArg;
|
this.name = channelNameArg;
|
||||||
this.passphrase = passphraseArg;
|
this.passphrase = passphraseArg;
|
||||||
UniverseChannel.channelStore.add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* authenticates a client on the server side
|
* authenticates a client on the server side
|
||||||
*/
|
*/
|
||||||
public async authenticateClient(passphraseArg: string): boolean {
|
public authenticate(universeMessageArg: UniverseMessage): boolean {
|
||||||
return passphraseArg === this.passphrase;
|
return (
|
||||||
|
this.name === universeMessageArg.requestedChannelName &&
|
||||||
|
this.passphrase === universeMessageArg.requestedChannelPassphrase
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
import * as plugins from './smartuniverse.plugins';
|
import * as plugins from './smartuniverse.plugins';
|
||||||
|
|
||||||
|
import { Objectmap } from 'lik';
|
||||||
|
|
||||||
|
|
||||||
import { Timer, TimeStamp } from 'smarttime';
|
import { Timer, TimeStamp } from 'smarttime';
|
||||||
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
|
* represents a message within a universe
|
||||||
|
* acts as a container to save message states like authentication status
|
||||||
*/
|
*/
|
||||||
export class UniverseMessage {
|
export class UniverseMessage {
|
||||||
/**
|
/**
|
||||||
@ -16,13 +22,25 @@ export class UniverseMessage {
|
|||||||
public id: number;
|
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.
|
* enables unprotected grouping of messages for efficiency purposes.
|
||||||
*/
|
*/
|
||||||
public universeChannel: string;
|
public universeChannelList = new Objectmap<UniverseChannel>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wether the message is authenticated
|
||||||
|
*/
|
||||||
|
public authenticated: boolean = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* time of creation
|
* time of creation
|
||||||
@ -45,15 +63,23 @@ export class UniverseMessage {
|
|||||||
* @param messageArg
|
* @param messageArg
|
||||||
* @param attachedPayloadArg
|
* @param attachedPayloadArg
|
||||||
*/
|
*/
|
||||||
constructor(messageArg: string, attachedPayloadArg: any) {
|
constructor(
|
||||||
|
messageArg: string,
|
||||||
|
requestedChannelNameArg: string,
|
||||||
|
passphraseArg: string,
|
||||||
|
attachedPayloadArg: any
|
||||||
|
) {
|
||||||
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;
|
||||||
|
// prevent memory issues
|
||||||
this.fallBackDestruction();
|
this.fallBackDestruction();
|
||||||
}
|
}
|
||||||
|
|
||||||
public setUniverseStore(universeStoreArg: UniverseStore) {
|
public setUniverseCache(universeCacheArg: UniverseCache) {
|
||||||
this.universeStore = universeStoreArg;
|
this.universeCache = universeCacheArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setDestructionTimer(selfdestructAfterArg: number) {
|
public setDestructionTimer(selfdestructAfterArg: number) {
|
||||||
@ -61,15 +87,22 @@ export class UniverseMessage {
|
|||||||
this.destructionTimer = new Timer(selfdestructAfterArg);
|
this.destructionTimer = new Timer(selfdestructAfterArg);
|
||||||
this.destructionTimer.start();
|
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.destructionTimer.completed.then(async () => {
|
||||||
this.universeStore.messageStore.remove(this);
|
this.universeCache.messageCache.remove(this);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.fallBackDestruction();
|
this.fallBackDestruction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handles bad messages for further analysis
|
||||||
|
*/
|
||||||
|
handleAsBadMessage() {
|
||||||
|
console.log('received a bad message');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* prevents memory leaks if channels have no default
|
* prevents memory leaks if channels have no default
|
||||||
*/
|
*/
|
||||||
|
@ -6,7 +6,7 @@ process.env.CLI = 'true';
|
|||||||
|
|
||||||
const universeCli = new plugins.smartcli.Smartcli();
|
const universeCli = new plugins.smartcli.Smartcli();
|
||||||
|
|
||||||
universeCli.standardTask().then(async argvArg => {
|
universeCli.standardTask().subscribe(async argvArg => {
|
||||||
const standardUniverse = new Universe({
|
const standardUniverse = new Universe({
|
||||||
messageExpiryInMilliseconds: 60000
|
messageExpiryInMilliseconds: 60000
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
import * as smartcli from '@pushrocks/smartcli';
|
||||||
import * as lik from 'lik';
|
import * as lik from 'lik';
|
||||||
import * as nodehash from 'nodehash';
|
import * as nodehash from 'nodehash';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as smartcli from 'smartcli';
|
|
||||||
import * as smartdelay from 'smartdelay';
|
import * as smartdelay from 'smartdelay';
|
||||||
import * as smartexpress from 'smartexpress';
|
import * as smartexpress from 'smartexpress';
|
||||||
import * as smartfile from 'smartfile';
|
import * as smartfile from 'smartfile';
|
||||||
|
Reference in New Issue
Block a user