Compare commits

..

12 Commits

Author SHA1 Message Date
bd849d347d 1.0.84 2019-09-17 12:46:35 +02:00
f2a85d4719 fix(core): update 2019-09-17 12:46:35 +02:00
4e7c28ac83 1.0.83 2019-09-11 14:57:36 +02:00
243f1a70e9 fix(core): update 2019-09-11 14:57:36 +02:00
b5a6517756 1.0.82 2019-09-11 10:11:34 +02:00
e12b128619 fix(core): update 2019-09-11 10:11:34 +02:00
03fbab5265 1.0.81 2019-09-10 23:55:21 +02:00
1d13bf5bcc fix(core): update 2019-09-10 23:55:20 +02:00
c2052f16a8 1.0.80 2019-09-10 19:36:11 +02:00
ff7cdc908c fix(core): update 2019-09-10 19:36:10 +02:00
f3d41b8719 1.0.79 2019-09-10 18:03:47 +02:00
f9f0fc45e2 fix(core): update 2019-09-10 18:03:46 +02:00
17 changed files with 241 additions and 50 deletions

8
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartuniverse", "name": "@pushrocks/smartuniverse",
"version": "1.0.78", "version": "1.0.84",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -1243,7 +1243,7 @@
}, },
"esutils": { "esutils": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://verdaccio.lossless.one/esutils/-/esutils-2.0.3.tgz", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz",
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"dev": true "dev": true
}, },
@ -1949,7 +1949,7 @@
}, },
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"resolved": "https://verdaccio.lossless.one/minimist/-/minimist-0.0.8.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true "dev": true
}, },
@ -2364,7 +2364,7 @@
}, },
"resolve": { "resolve": {
"version": "1.12.0", "version": "1.12.0",
"resolved": "https://verdaccio.lossless.one/resolve/-/resolve-1.12.0.tgz", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz",
"integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==", "integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==",
"dev": true, "dev": true,
"requires": { "requires": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartuniverse", "name": "@pushrocks/smartuniverse",
"version": "1.0.78", "version": "1.0.84",
"private": false, "private": false,
"description": "messaging service for your micro services", "description": "messaging service for your micro services",
"main": "dist/index.js", "main": "dist/index.js",
@ -25,6 +25,7 @@
"rxjs": "*" "rxjs": "*"
}, },
"dependencies": { "dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.7",
"@pushrocks/lik": "^3.0.11", "@pushrocks/lik": "^3.0.11",
"@pushrocks/smartdelay": "^2.0.3", "@pushrocks/smartdelay": "^2.0.3",
"@pushrocks/smartexpress": "^3.0.40", "@pushrocks/smartexpress": "^3.0.40",

View File

@ -69,9 +69,22 @@ tap.test('a second client should be able to subscibe', async () => {
}); });
testClientUniverse2.addChannel(testChannelData.channelName, testChannelData.channelPass); testClientUniverse2.addChannel(testChannelData.channelName, testChannelData.channelPass);
await testClientUniverse2.start();
}); });
tap.test('should receive a message correctly', async () => {}); tap.test('should receive a message correctly', async (tools) => {
const done = tools.defer();
const testChannel = testClientUniverse.getChannel(testChannelData.channelName);
const testChannel2 = testClientUniverse2.getChannel(testChannelData.channelName);
const subscription = testChannel2.subscribe(messageArg => {
console.log('Yay##########');
done.resolve();
});
await testChannel.sendMessage({
messageText: 'hellothere'
});
await done.promise;
});
tap.test('should disconnect the client correctly', async () => { tap.test('should disconnect the client correctly', async () => {
await testClientUniverse.stop(); await testClientUniverse.stop();

View File

@ -9,4 +9,8 @@ export * from './smartuniverse.classes.universecache';
export * from './smartuniverse.classes.universechannel'; export * from './smartuniverse.classes.universechannel';
export * from './smartuniverse.classes.universemessage'; export * from './smartuniverse.classes.universemessage';
// Reaction Response
export * from './smartuniverse.classes.reactionrequest';
export * from './smartuniverse.classes.reactionresponse';
export * from './interfaces'; export * from './interfaces';

View File

@ -1,9 +1,3 @@
export type IServerCallActions =
| 'channelSubscription'
| 'processMessage'
| 'channelUnsubscribe'
| 'terminateConnection';
export interface IServerUnsubscribeActionPayload { export interface IServerUnsubscribeActionPayload {
name: string; name: string;
} }

View File

@ -1,7 +1,6 @@
export interface IMessageCreator { export interface IMessageCreator {
messageText: string; messageText: string;
payload?: string | number | any; payload?: string | number | any;
payloadStringType?: 'Buffer' | 'string' | 'object';
} }
/** /**

View File

@ -21,7 +21,7 @@ export interface IClientOptions {
export class ClientUniverse { export class ClientUniverse {
public options; public options;
public smartsocketClient: plugins.smartsocket.SmartsocketClient; public smartsocketClient: plugins.smartsocket.SmartsocketClient;
public observableIntake: plugins.smartrx.ObservableIntake<ClientUniverseMessage>; public observableIntake: plugins.smartrx.ObservableIntake<ClientUniverseMessage<any>>;
public clientUniverseCache = new ClientUniverseCache(); public clientUniverseCache = new ClientUniverseCache();
constructor(optionsArg: IClientOptions) { constructor(optionsArg: IClientOptions) {
@ -147,7 +147,7 @@ export class ClientUniverse {
await this.smartsocketClient.connect(); await this.smartsocketClient.connect();
plugins.smartlog.defaultLogger.log('info', 'universe client connected successfully'); plugins.smartlog.defaultLogger.log('info', 'universe client connected successfully');
await this.clientUniverseCache.channelMap.forEach(async clientUniverseChannelArg => { await this.clientUniverseCache.channelMap.forEach(async clientUniverseChannelArg => {
await clientUniverseChannelArg.subscribe(); await clientUniverseChannelArg.populateSubscriptionToServer();
}); });
} }
} }

View File

@ -38,7 +38,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
public name: string; public name: string;
public passphrase: string; public passphrase: string;
public status: 'subscribed' | 'unsubscribed' = 'unsubscribed'; public status: 'subscribed' | 'unsubscribed' = 'unsubscribed';
private subject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage>(); private subject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage<any>>();
// refs // refs
public clientUniverseRef: ClientUniverse; public clientUniverseRef: ClientUniverse;
@ -53,7 +53,17 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
* subscribes to a channel * subscribes to a channel
* tells the universe about this instances interest into a channel * tells the universe about this instances interest into a channel
*/ */
public async subscribe(observerArg?: plugins.smartrx.rxjs.Observer<any>) { public subscribe(observingFunctionArg: (messageArg: ClientUniverseMessage<any>) => void) {
return this.subject.subscribe(
messageArg => {
observingFunctionArg(messageArg);
},
error => console.log(error)
);
}
public async populateSubscriptionToServer() {
// lets make sure the channel is connected // lets make sure the channel is connected
if (this.status === 'unsubscribed') { if (this.status === 'unsubscribed') {
const response = await this.clientUniverseRef.smartsocketClient.serverCall< const response = await this.clientUniverseRef.smartsocketClient.serverCall<
@ -64,21 +74,12 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
}); });
this.status = response.subscriptionStatus; this.status = response.subscriptionStatus;
} }
if (observerArg) {
return this.subject.subscribe(observerArg);
}
} }
public async emitMessageLocally(messageArg: ClientUniverseMessage) { public async emitMessageLocally(messageArg: ClientUniverseMessage<any>) {
this.subject.next(messageArg); this.subject.next(messageArg);
} }
public askForReaction(reactionRequest: ReactionRequest): ReactionResponse {
const reactionResponse = new ReactionResponse();
return reactionResponse;
}
/** /**
* sends a message towards the server * sends a message towards the server
* @param messageArg * @param messageArg
@ -91,8 +92,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
passphrase: this.passphrase, passphrase: this.passphrase,
targetChannelName: this.name, targetChannelName: this.name,
messageText: messageArg.messageText, messageText: messageArg.messageText,
payload: messageArg.payload, payload: messageArg.payload
payloadStringType: messageArg.payloadStringType
}; };
await this.clientUniverseRef.smartsocketClient.serverCall( await this.clientUniverseRef.smartsocketClient.serverCall(
'processMessage', 'processMessage',

View File

@ -2,7 +2,7 @@ import * as plugins from './smartuniverse.plugins';
import * as interfaces from './interfaces'; import * as interfaces from './interfaces';
export class ClientUniverseMessage implements interfaces.IUniverseMessage { export class ClientUniverseMessage<T> implements interfaces.IUniverseMessage {
// ====== // ======
// STATIC // STATIC
// ====== // ======
@ -22,8 +22,7 @@ export class ClientUniverseMessage implements interfaces.IUniverseMessage {
public smartTimestamp: plugins.smarttime.TimeStamp; public smartTimestamp: plugins.smarttime.TimeStamp;
public messageText: string; public messageText: string;
public passphrase: string; public passphrase: string;
public payload: any; public payload: T;
public payloadStringType;
public targetChannelName: string; public targetChannelName: string;
constructor(messageArg: interfaces.IUniverseMessage) { constructor(messageArg: interfaces.IUniverseMessage) {

View File

@ -1,3 +1,66 @@
import * as plugins from './smartuniverse.plugins'; import * as plugins from './smartuniverse.plugins';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { ClientUniverseChannel } from './smartuniverse.classes.clientuniversechannel';
import { ReactionResult } from './smartuniverse.classes.reactionresult';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { ClientUniverseMessage } from './smartuniverse.classes.clientuniversemessage';
export class ReactionRequest {} export interface IReactionRequestConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
method: T['method'];
}
export interface ICombinatorPayload<T extends plugins.typedrequestInterfaces.ITypedRequest> {
/**
* needed for tying responses to requests
*/
id: string;
typedRequestPayload: {
method: T['method'],
request : T['request'],
response: T['response']
};
}
export class ReactionRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
public method: T['method'];
constructor(optionsArg: IReactionRequestConstructorOptions<T>) {
this.method = optionsArg.method;
}
public async fire(channelsArg: Array<UniverseChannel | ClientUniverseChannel>, requestDataArg: T['request'], timeoutMillisArg=60000) {
const subscriptionMap = new plugins.lik.Objectmap<plugins.smartrx.rxjs.Subscription>();
const reactionResult = new ReactionResult<T>();
const requestId = plugins.smartunique.shortId();
for (const channel of channelsArg) {
subscriptionMap.add(channel.subscribe((messageArg: UniverseMessage<ICombinatorPayload<T>> | ClientUniverseMessage<ICombinatorPayload<T>>) => {
if (messageArg.messageText === 'reactionResponse' && messageArg.payload.typedRequestPayload.method === this.method) {
const payload: ICombinatorPayload<T> = messageArg.payload;
if (payload.id !== requestId) {
return;
}
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
}
}));
const payload: ICombinatorPayload<T> = {
id: requestId,
typedRequestPayload: {
method: this.method,
request: requestDataArg,
response: null
}
};
channel.sendMessage({
messageText: 'reactionRequest',
payload
});
}
plugins.smartdelay.delayFor(timeoutMillisArg).then(async () => {
await subscriptionMap.forEach(subscriptionArg => {
subscriptionArg.unsubscribe();
});
reactionResult.complete();
});
return reactionResult;
}
}

View File

@ -1,3 +1,59 @@
import * as plugins from './smartuniverse.plugins'; import * as plugins from './smartuniverse.plugins';
export class ReactionResponse {} import { ICombinatorPayload } from './smartuniverse.classes.reactionrequest';
import { UniverseChannel } from './smartuniverse.classes.universechannel';
import { ClientUniverseChannel } from './smartuniverse.classes.clientuniversechannel';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { ClientUniverseMessage } from './smartuniverse.classes.clientuniversemessage';
export type TReactionResponseFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = (dataArg: T['request']) => Promise<T['response']>;
export interface IReactionResponseConstructorOptions<
T extends plugins.typedrequestInterfaces.ITypedRequest
> {
method: T['method'];
channels: Array<UniverseChannel | ClientUniverseChannel>;
funcDef: TReactionResponseFuncDef<T>;
}
export class ReactionResponse<T extends plugins.typedrequestInterfaces.ITypedRequest> {
public method: T['method'];
public channels = new plugins.lik.Objectmap<UniverseChannel | ClientUniverseChannel>();
public funcDef: TReactionResponseFuncDef<T>;
constructor(optionsArg: IReactionResponseConstructorOptions<T>) {
this.method = optionsArg.method;
this.channels.addArray(optionsArg.channels);
this.funcDef = optionsArg.funcDef;
for (const channel of this.channels.getArray()) {
channel.subscribe(messageArg => {
this.processMessageForReaction(channel, messageArg);
});
}
}
private async processMessageForReaction(
channelArg: UniverseChannel | ClientUniverseChannel,
messageArg:
| UniverseMessage<ICombinatorPayload<T>>
| ClientUniverseMessage<ICombinatorPayload<T>>
) {
if (
messageArg.messageText === 'reactionRequest' &&
messageArg.payload.typedRequestPayload.method === this.method
) {
const response: T['response'] = await this.funcDef(messageArg.payload.typedRequestPayload.request);
const payload: ICombinatorPayload<T> = {
...messageArg.payload,
typedRequestPayload: {
...messageArg.payload.typedRequestPayload,
response
}
};
channelArg.sendMessage({
messageText: 'reactionResponse',
payload
});
}
}
}

View File

@ -0,0 +1,52 @@
import * as plugins from './smartuniverse.plugins';
import { ReactionResponse } from './smartuniverse.classes.reactionresponse';
export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedRequest> {
private resultSubject = new plugins.smartrx.rxjs.Subject<T['response']>();
private endResult: Array<T['response']> = [];
private completeDeferred = plugins.smartpromise.defer<Array<T['response']>>();
constructor () {
this.resultSubscribe(responseArg => {
this.endResult.push(responseArg);
});
}
public resultSubscribe(observerArg: (responseArg: T['response']) => void) {
return this.resultSubject.subscribe(observerArg);
}
/**
* gets the end result as an array of all results
*/
public async getEndResult() {
const result = await this.completeDeferred.promise;
return result;
}
/**
* if there is a single respondant, or you are only interested in the first result
*/
public async getFirstResult() {
const done = plugins.smartpromise.defer<T['response']>();
const subscription = this.resultSubject.subscribe(result => {
done.resolve(result);
subscription.unsubscribe();
});
return await done.promise;
}
/**
* push a reactionResponse
*/
public async pushReactionResponse(responseArg: T['response']) {
this.resultSubject.next(responseArg);
}
/**
* completes the ReactionResult
*/
public async complete() {
this.completeDeferred.resolve(this.endResult);
}
}

View File

@ -24,7 +24,7 @@ export class UniverseCache {
/** /**
* stores messages for this instance * stores messages for this instance
*/ */
public messageMap = new Objectmap<UniverseMessage>(); public messageMap = new Objectmap<UniverseMessage<any>>();
/** /**
* stores the channels that are available within the universe * stores the channels that are available within the universe
@ -54,7 +54,7 @@ export class UniverseCache {
* @param messageArg * @param messageArg
* @param attachedPayloadArg * @param attachedPayloadArg
*/ */
public async addMessage(messageArg: UniverseMessage) { public async addMessage(messageArg: UniverseMessage<any>) {
messageArg.setUniverseCache(this); messageArg.setUniverseCache(this);
UniverseChannel.authorizeAMessageForAChannel(this, messageArg); UniverseChannel.authorizeAMessageForAChannel(this, messageArg);
this.messageMap.add(messageArg); this.messageMap.add(messageArg);
@ -69,7 +69,7 @@ export class UniverseCache {
public readMessagesYoungerThan( public readMessagesYoungerThan(
unixTimeArg?: number, unixTimeArg?: number,
channelName?: string channelName?: string
): Observable<UniverseMessage> { ): Observable<UniverseMessage<any>> {
const messageObservable = from(this.messageMap.getArray()).pipe( const messageObservable = from(this.messageMap.getArray()).pipe(
filter(messageArg => { filter(messageArg => {
return messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime); return messageArg.smartTimestamp.isYoungerThanMilliSeconds(this.destructionTime);

View File

@ -52,7 +52,7 @@ export class UniverseChannel {
*/ */
public static authorizeAMessageForAChannel( public static authorizeAMessageForAChannel(
universeCacheArg: UniverseCache, universeCacheArg: UniverseCache,
universeMessageArg: UniverseMessage universeMessageArg: UniverseMessage<any>
): UniverseChannel { ): UniverseChannel {
const foundChannel = universeCacheArg.channelMap.find(universeChannel => { const foundChannel = universeCacheArg.channelMap.find(universeChannel => {
const result = universeChannel.authenticate(universeMessageArg); const result = universeChannel.authenticate(universeMessageArg);
@ -85,7 +85,7 @@ export class UniverseChannel {
*/ */
public name: string; public name: string;
public universeRef: Universe; public universeRef: Universe;
private subject = new plugins.smartrx.rxjs.Subject<UniverseMessage>(); private subject = new plugins.smartrx.rxjs.Subject<UniverseMessage<any>>();
/** /**
* the passphrase for the channel * the passphrase for the channel
@ -103,7 +103,7 @@ export class UniverseChannel {
* # the messages channelName against the unverseChannel's name * # the messages channelName against the unverseChannel's name
* # the messages password against the universeChannel's password * # the messages password against the universeChannel's password
*/ */
public authenticate(universeMessageArg: UniverseMessage): boolean { public authenticate(universeMessageArg: UniverseMessage<any>): boolean {
return ( return (
this.name === universeMessageArg.targetChannelName && this.name === universeMessageArg.targetChannelName &&
this.passphrase === universeMessageArg.passphrase this.passphrase === universeMessageArg.passphrase
@ -114,10 +114,10 @@ export class UniverseChannel {
* pushes a message to clients * pushes a message to clients
* @param messageArg * @param messageArg
*/ */
public async push(messageArg: UniverseMessage) { public async push(messageArg: UniverseMessage<any>) {
this.subject.next(messageArg); this.subject.next(messageArg);
const universeConnectionsWithChannelAccess: UniverseConnection[] = []; const universeConnectionsWithChannelAccess: UniverseConnection[] = [];
this.universeRef.universeCache.connectionMap.forEach(async socketConnection => { await this.universeRef.universeCache.connectionMap.forEach(async socketConnection => {
if (socketConnection.authenticatedChannels.includes(this)) { if (socketConnection.authenticatedChannels.includes(this)) {
universeConnectionsWithChannelAccess.push(socketConnection); universeConnectionsWithChannelAccess.push(socketConnection);
} }
@ -131,8 +131,7 @@ export class UniverseChannel {
passphrase: messageArg.passphrase, passphrase: messageArg.passphrase,
targetChannelName: this.name, targetChannelName: this.name,
messageText: messageArg.messageText, messageText: messageArg.messageText,
payload: messageArg.payload, payload: messageArg.payload
payloadStringType: messageArg.payloadStringType
}; };
smartsocket.clientCall( smartsocket.clientCall(
'processMessage', 'processMessage',
@ -143,8 +142,13 @@ export class UniverseChannel {
} }
// functions to interact with a channel locally // functions to interact with a channel locally
public async subscribe(observer: plugins.smartrx.rxjs.Observer<any>) { public subscribe(observingFunctionArg: (messageArg: UniverseMessage<any>) => void) {
return this.subject.subscribe(observer); return this.subject.subscribe(
messageArg => {
observingFunctionArg(messageArg);
},
error => console.log(error)
);
} }
/** /**
@ -155,7 +159,6 @@ export class UniverseChannel {
id: plugins.smartunique.shortId(), id: plugins.smartunique.shortId(),
messageText: messageDescriptor.messageText, messageText: messageDescriptor.messageText,
payload: messageDescriptor.payload, payload: messageDescriptor.payload,
payloadStringType: messageDescriptor.payloadStringType,
targetChannelName: this.name, targetChannelName: this.name,
passphrase: this.passphrase, passphrase: this.passphrase,
timestamp: Date.now() timestamp: Date.now()

View File

@ -26,6 +26,7 @@ export class UniverseConnection {
universeConnection universeConnection
); );
universeRef.universeCache.connectionMap.add(universeConnection); universeRef.universeCache.connectionMap.add(universeConnection);
console.log('hi')
} }
/** /**

View File

@ -13,7 +13,7 @@ import { SocketConnection } from '@pushrocks/smartsocket';
* represents a message within a universe * represents a message within a universe
* acts as a container to save message states like authentication status * acts as a container to save message states like authentication status
*/ */
export class UniverseMessage implements interfaces.IUniverseMessage { export class UniverseMessage<T> implements interfaces.IUniverseMessage {
public static createMessageFromPayload( public static createMessageFromPayload(
socketConnectionArg: SocketConnection, socketConnectionArg: SocketConnection,
dataArg: interfaces.IUniverseMessage dataArg: interfaces.IUniverseMessage
@ -28,8 +28,7 @@ export class UniverseMessage implements interfaces.IUniverseMessage {
public smartTimestamp: TimeStamp; public smartTimestamp: TimeStamp;
public messageText: string; public messageText: string;
public passphrase: string; public passphrase: string;
public payload: any; public payload: T;
public payloadStringType;
public targetChannelName: string; public targetChannelName: string;
public socketConnection: SocketConnection; public socketConnection: SocketConnection;

View File

@ -3,6 +3,13 @@ import * as path from 'path';
export { path }; export { path };
// apiglobal scope
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
export {
typedrequestInterfaces
};
// pushrocks scope // pushrocks scope
import * as lik from '@pushrocks/lik'; import * as lik from '@pushrocks/lik';
import * as smarthash from '@pushrocks/smarthash'; import * as smarthash from '@pushrocks/smarthash';