fix(core): update

This commit is contained in:
Philipp Kunz 2019-04-22 09:58:36 +02:00
parent 904a48d414
commit 39d3a0f2f8
11 changed files with 91 additions and 57 deletions

19
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}

View File

@ -41,8 +41,11 @@ tap.test('should get a observable correctly', async () => {
});
tap.test('should send a message correctly', async () => {
await testUniverseClient.sendMessage('greeting', {
anyBool: true
await testUniverseClient.sendMessage({
messageText: 'hello',
passphrase: 'wowza',
targetChannelName: 'channel1',
});
});

View File

@ -1,7 +1,15 @@
export interface IUniverseMessage {
export interface IMessageCreator {
messageText: string;
targetChannelName: string;
passphrase: 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;
}

View File

@ -31,8 +31,14 @@ export class ClientUniverse {
this.options = optionsArg;
}
public async sendMessage(messageArg: interfaces.IUniverseMessage) {
const requestBody: interfaces.IUniverseMessage = messageArg;
public async sendMessage(messageArg: interfaces.IMessageCreator) {
const requestBody: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),
passphrase: (await this.getChannel(messageArg.targetChannelName)).,
...messageArg,
};
const requestBodyString = JSON.stringify(requestBody);
// TODO: User websocket connection if available
const response = await plugins.smartrequest.postJson(`${this.options.serverAddress}/sendmessage` , {
@ -40,7 +46,7 @@ export class ClientUniverse {
});
}
public async getChannel(channelName: string, passphrase): Promise<ClientUniverseChannel> {
public async getChannel(channelName: string, passphraseArg?: string): Promise<ClientUniverseChannel> {
await this.checkConnection();
const clientUniverseChannel = await ClientUniverseChannel.createClientUniverseChannel(
this,

View File

@ -9,9 +9,10 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
// ======
public static async createClientUniverseChannel(
clientUniverseArg: ClientUniverse,
channelName: string
channelName: string,
passphraseArg: string
): Promise<ClientUniverseChannel> {
const clientChannel = new ClientUniverseChannel(clientUniverseArg);
const clientChannel = new ClientUniverseChannel(clientUniverseArg, passphraseArg);
await clientChannel.transmitSubscription();
return clientChannel;
}
@ -21,9 +22,11 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
// ========
public clientUniverse: ClientUniverse;
public passphrase: string;
constructor(clientUniverseArg: ClientUniverse) {
constructor(clientUniverseArg: ClientUniverse, passphraseArg: string) {
this.clientUniverse = clientUniverseArg;
this.passphrase = passphraseArg
}
/**

View File

@ -6,13 +6,24 @@ export class ClientUniverseMessage implements interfaces.IUniverseMessage {
// ======
// STATIC
// ======
public static createMessageFromPayload(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 () {

View File

@ -80,7 +80,8 @@ export class Universe {
// lets create the http request route
this.smartexpressServer.addRoute('/sendmessage', new Handler('POST', async (req, res) => {
this.universeCache.addMessage(req.body);
const universeMessageInstance = new UniverseMessage(req.body);
this.universeCache.addMessage(universeMessageInstance);
}));
// add websocket upgrade

View File

@ -55,7 +55,7 @@ export class 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;

View File

@ -7,34 +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 implements interfaces.IUniverseMessage {
/**
* public and unique id
* numeric ascending
* adheres to time in milliseconds
* -> 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: 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.
*/
@ -44,21 +40,10 @@ export class UniverseMessage implements interfaces.IUniverseMessage {
* wether the message is authenticated
*/
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
/**
@ -66,17 +51,12 @@ export class UniverseMessage implements interfaces.IUniverseMessage {
* @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();
}
@ -102,7 +82,7 @@ export class UniverseMessage implements interfaces.IUniverseMessage {
/**
* handles bad messages for further analysis
*/
handleAsBadMessage() {
public handleAsBadMessage() {
console.log('received a bad message');
}

View File

@ -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
};