Compare commits

..

6 Commits

Author SHA1 Message Date
4fc6e327ec 1.0.18 2018-05-24 00:14:57 +02:00
7991baf2bf fix(core): improve channel handling 2018-05-24 00:14:57 +02:00
d033780015 1.0.17 2018-05-23 23:50:46 +02:00
eae46e6461 fix(structure): format TypeScript 2018-05-23 23:50:45 +02:00
785acfaba4 1.0.16 2018-05-20 00:41:59 +02:00
5a4dceb75d fix(core): prepare for release 2018-05-20 00:41:59 +02:00
9 changed files with 2597 additions and 1822 deletions

2491
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
{ {
"name": "@pushrocks/smartuniverse", "name": "@pushrocks/smartuniverse",
"version": "1.0.15", "version": "1.0.18",
"private": true,
"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",
@ -13,12 +14,16 @@
}, },
"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": {
"lik": "^2.0.5", "lik": "^2.0.5",
"nodehash": "^1.0.4",
"rxjs": "^5.5.8", "rxjs": "^5.5.8",
"smartcli": "^2.0.12", "smartcli": "^2.0.12",
"smartdelay": "^1.0.4",
"smartexpress": "^1.0.21", "smartexpress": "^1.0.21",
"smartfile": "^4.2.28", "smartfile": "^4.2.28",
"smartq": "^1.1.8", "smartq": "^1.1.8",

View File

@ -1,20 +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) {}
}

View File

@ -1,8 +1,6 @@
import * as plugins from './smartuniverse.plugins'; import * as plugins from './smartuniverse.plugins';
import { Handler, Route, Server } from 'smartexpress'; import { Handler, Route, Server } from 'smartexpress';
import { UniverseManager } from './smartuniverse.classes.manager';
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 { UniverseStore } from './smartuniverse.classes.universestore';
@ -19,15 +17,22 @@ export interface IServerGetMessagesRequestBody {
youngerThan: number; youngerThan: number;
} }
/**
* the interface for a standard request
*/
export interface IServerPutMessageRequestBody { export interface IServerPutMessageRequestBody {
channel: string;
passphrase: string;
message: string; message: string;
payload: any; payload: any;
} }
/**
* main class that setsup a Universe
*/
export class Universe { export class Universe {
// subinstances // subinstances
public universeStore: UniverseStore; public universeStore: UniverseStore;
public universeManager: UniverseManager;
// options // options
private options: ISmartUniverseConstructorOptions; private options: ISmartUniverseConstructorOptions;
@ -50,7 +55,6 @@ export class Universe {
constructor(optionsArg: ISmartUniverseConstructorOptions) { constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg; this.options = optionsArg;
this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds); this.universeStore = new UniverseStore(this.options.messageExpiryInMilliseconds);
this.universeManager = new UniverseManager();
} }
/** /**
@ -67,8 +71,9 @@ export class Universe {
// message handling // message handling
// adds messages // adds messages
const addMessageHandler = new Handler('PUT', request => { const addMessageHandler = new Handler('PUT', request => {
const requestBody = request.body; const requestBody: IServerPutMessageRequestBody = request.body;
this.universeStore.addMessage(requestBody.message, requestBody.payload); const message = new UniverseMessage(requestBody.message, requestBody.channel, requestBody.passphrase, requestBody.payload);
this.universeStore.addMessage(message);
console.log(requestBody); console.log(requestBody);
return true; return true;
}); });

View File

@ -9,27 +9,55 @@ export class UniverseChannel {
/** /**
* stores the channels that are available within the universe * stores the channels that are available within the universe
*/ */
public static channelStore = new Objectmap(); public static channelStore = new Objectmap<UniverseChannel>();
/** /**
* the credentials for the channel * allows messages to be processed in a blacklist mode for further analysis
*/ */
private credentials: { public static blackListChannel = new UniverseChannel('blacklist', 'nada');
user: string;
password: string;
};
/** /**
* 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 (channelNameArg: string, passphraseArg: string) {
const newChannel = new UniverseChannel(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;
});
if(foundChannel) {
return foundChannel;
} else {
return this.blackListChannel;
}
};
/**
* the name of the channel
*/
public name: string;
/**
* the passphrase for the channel
*/
public passphrase: string;
constructor(channelNameArg: string, passphraseArg: string) {
this.name = channelNameArg;
this.passphrase = passphraseArg;
UniverseChannel.channelStore.add(this);
}
/** /**
* authenticates a client on the server side * authenticates a client on the server side
*/ */
async authenticateClient() {} public authenticate(channelNameArg: string, passphraseArg: string): boolean {
return (this.name === channelNameArg && this.passphrase === passphraseArg);
}
} }

View File

@ -1,7 +1,9 @@
import * as plugins from './smartuniverse.plugins'; import * as plugins from './smartuniverse.plugins';
import { Timer, TimeStamp } from 'smarttime'; import { Timer, TimeStamp } from 'smarttime';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { UniverseStore } from './smartuniverse.classes.universestore'; import { UniverseStore } from './smartuniverse.classes.universestore';
import { Universe } from './smartuniverse.classes.universe';
/** /**
* represents a message within a universe * represents a message within a universe
@ -19,6 +21,10 @@ export class UniverseMessage {
* the universe store the message is attached to * the universe store the message is attached to
*/ */
public universeStore: UniverseStore; public universeStore: UniverseStore;
/**
* enables unprotected grouping of messages for efficiency purposes.
*/
public universeChannel: UniverseChannel;
/** /**
* time of creation * time of creation
@ -26,36 +32,55 @@ export class UniverseMessage {
public timestamp: TimeStamp; public timestamp: TimeStamp;
/** /**
* enables unprotected grouping of messages for efficiency purposes. * the actual message
*/ */
public universeChannel: string; public message: string;
public message: string; // the actual message
public attachedPayload: any; // any attached payloads. Can be of binary format. /**
* 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(messageArg: string, channelNameArg: string, passphraseArg: string, attachedPayloadArg: any) {
parentUniverseStore: UniverseStore,
messageArg: string,
attachedPayloadArg: any,
selfdestructAfterArg: number
) {
this.universeStore = parentUniverseStore;
this.timestamp = new TimeStamp(); this.timestamp = new TimeStamp();
this.message = messageArg; this.message = messageArg;
this.universeChannel = UniverseChannel.authorizeForChannel(channelNameArg, passphraseArg);
this.attachedPayload = attachedPayloadArg; this.attachedPayload = attachedPayloadArg;
this.destructionTimer = new Timer(selfdestructAfterArg); this.fallBackDestruction();
this.destructionTimer.start(); }
// set up self destruction by removing this from the parent messageStore public setUniverseStore(universeStoreArg: UniverseStore) {
this.destructionTimer.completed.then(async () => { this.universeStore = universeStoreArg;
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 messageStore
this.destructionTimer.completed.then(async () => {
this.universeStore.messageStore.remove(this);
});
} else {
this.fallBackDestruction();
}
}
/**
* prevents memory leaks if channels have no default
*/
private fallBackDestruction() {
plugins.smartdelay.delayFor(1000).then(() => {
if (!this.destructionTimer) {
this.setDestructionTimer(6000);
}
}); });
} }
} }

View File

@ -25,10 +25,8 @@ export class UniverseStore {
* @param messageArg * @param messageArg
* @param attachedPayloadArg * @param attachedPayloadArg
*/ */
public addMessage(messageArg, attachedPayloadArg) { public addMessage(messageArg: UniverseMessage) {
this.messageStore.add( this.messageStore.add(messageArg);
new UniverseMessage(this, messageArg, attachedPayloadArg, this.destructionTime)
);
} }
/** /**

View File

@ -1,6 +1,8 @@
import * as lik from 'lik'; import * as lik from 'lik';
import * as nodehash from 'nodehash';
import * as path from 'path'; import * as path from 'path';
import * as smartcli from 'smartcli'; import * as smartcli from 'smartcli';
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';
import * as smartq from 'smartq'; import * as smartq from 'smartq';
@ -11,8 +13,10 @@ import * as smarttime from 'smarttime';
export { export {
lik, lik,
nodehash,
path, path,
smartcli, smartcli,
smartdelay,
smartexpress, smartexpress,
smartfile, smartfile,
smartq, smartq,

1761
yarn.lock

File diff suppressed because it is too large Load Diff