Compare commits

..

6 Commits

Author SHA1 Message Date
bd4897f392 1.0.97 2019-11-09 18:44:34 +01:00
dbdc8a2811 fix(core): update 2019-11-09 18:44:33 +01:00
908d00981b 1.0.96 2019-11-09 14:06:52 +01:00
669ef262d7 fix(core): update 2019-11-09 14:06:51 +01:00
30053fe441 1.0.95 2019-11-09 13:00:30 +01:00
afb4e3339a fix(core): update 2019-11-09 13:00:30 +01:00
14 changed files with 145 additions and 98 deletions

View File

@ -3,14 +3,14 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
- security
- test
- release
- metadata
# ====================
# security stage
@ -18,10 +18,11 @@ stages:
mirror:
stage: security
script:
- npmci git mirror
- npmci git mirror
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
snyk:
stage: security
@ -31,8 +32,9 @@ snyk:
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
# ====================
# test stage
@ -41,37 +43,40 @@ snyk:
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- priv
- lossless
- docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci command npm run build
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
release:
stage: release
script:
- npmci node install lts
- npmci npm publish
- npmci node install stable
- npmci npm publish
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
# ====================
# metadata stage
@ -81,33 +86,35 @@ codequality:
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
- npmci trigger
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
pages:
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:stable-dind
stage: metadata
script:
- npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- lossless
- docker
- notpriv
only:
@ -115,5 +122,5 @@ pages:
artifacts:
expire_in: 1 week
paths:
- public
- public
allow_failure: true

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartuniverse",
"version": "1.0.94",
"version": "1.0.97",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartuniverse",
"version": "1.0.94",
"version": "1.0.97",
"private": false,
"description": "messaging service for your micro services",
"main": "dist/index.js",
@ -40,12 +40,12 @@
"@pushrocks/smartunique": "^3.0.1"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_web/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"

View File

@ -47,6 +47,10 @@ myUniverse.start(8765); // start the server and provide the port on which to lis
All your microservices represents clients in the universe that may talk to each other using the universe server.
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)

View File

@ -74,7 +74,7 @@ tap.test('a second client should be able to subscibe', async () => {
await testClientUniverse2.start();
});
tap.test('should receive a message correctly', async (tools) => {
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);
@ -91,7 +91,7 @@ tap.test('should receive a message correctly', async (tools) => {
});
interface IDemoReqRes {
method: 'demo',
method: 'demo';
request: {
wowso: string;
};
@ -114,19 +114,22 @@ tap.test('ReactionRequest and ReactionResponse should work', async () => {
const reactionRequest = new smartuniverse.ReactionRequest<IDemoReqRes>({
method: 'demo'
});
const reactionResult = await reactionRequest.fire([testClientUniverse2.getChannel(testChannelData.channelName)], {
wowso: 'wowza'
});
const reactionResult = await reactionRequest.fire(
[testClientUniverse2.getChannel(testChannelData.channelName)],
{
wowso: 'wowza'
}
);
const result = await reactionResult.getFirstResult();
console.log(result);
});
tap.test('should disconnect the client correctly', async (tools) => {
tap.test('should disconnect the client correctly', async tools => {
await testClientUniverse.stop();
await testClientUniverse2.stop();
});
tap.test('should end the server correctly', async (tools) => {
tap.test('should end the server correctly', async tools => {
await testUniverse.stopServer();
});

View File

@ -4,7 +4,5 @@ import * as plugins from './smartuniverse.plugins';
* broadcasts an event to multiple channels
*/
export class BroadcastEvent<T> {
fire() {
}
};
fire() {}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './smartuniverse.plugins';
export class BroadcastSUbscription {
export class BroadcastSubscription {
}

View File

@ -25,6 +25,8 @@ export class ClientUniverse {
public messageRxjsSubject = new plugins.smartrx.rxjs.Subject<ClientUniverseMessage<any>>();
public clientUniverseCache = new ClientUniverseCache();
public autoReconnectStatus: 'on' | 'off' = 'off';
constructor(optionsArg: IClientOptions) {
this.options = optionsArg;
}
@ -74,10 +76,14 @@ export class ClientUniverse {
}
public async start() {
if (this.options.autoReconnect) {
this.autoReconnectStatus = 'on';
}
await this.checkConnection();
}
public async stop() {
this.autoReconnectStatus = 'off';
await this.disconnect('triggered');
}
@ -85,7 +91,7 @@ export class ClientUniverse {
* checks the connection towards a universe server
* since password validation is done through other means, a connection should always be possible
*/
public async checkConnection(): Promise<void> {
private async checkConnection(): Promise<void> {
if (!this.smartsocketClient) {
const parsedURL = url.parse(this.options.serverAddress);
const socketConfig: plugins.smartsocket.ISmartsocketClientOptions = {
@ -98,7 +104,7 @@ export class ClientUniverse {
this.smartsocketClient = new SmartsocketClient(socketConfig);
this.smartsocketClient.eventSubject.subscribe(async eventArg => {
switch(eventArg) {
switch (eventArg) {
case 'disconnected':
this.disconnect('upstreamEvent');
}
@ -165,16 +171,25 @@ export class ClientUniverse {
}
}
public async disconnect(reason: 'upstreamEvent' | 'triggered' = 'triggered', tryReconnect = false) {
if (reason === 'triggered') {
const smartsocketToDisconnect = this.smartsocketClient;
this.smartsocketClient = null; // making sure the upstreamEvent does not interfere
await smartsocketToDisconnect.disconnect();
private async disconnect(
reason: 'upstreamEvent' | 'triggered' = 'triggered',
tryReconnect = false
) {
const instructDisconnect = async () => {
if (this.smartsocketClient) {
const smartsocketToDisconnect = this.smartsocketClient;
this.smartsocketClient = null; // making sure the upstreamEvent does not interfere
await smartsocketToDisconnect.disconnect();
}
};
if (reason === 'triggered' && this.smartsocketClient) {
await instructDisconnect();
}
if (this.options.autoReconnect && reason === 'upstreamEvent' && this.smartsocketClient) {
if (this.autoReconnectStatus === 'on' && reason === 'upstreamEvent') {
await instructDisconnect();
await plugins.smartdelay.delayForRandom(5000, 20000);
this.smartsocketClient = null;
this.checkConnection();
await this.checkConnection();
}
}
}

View File

@ -54,7 +54,6 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
* tells the universe about this instances interest into a channel
*/
public subscribe(observingFunctionArg: (messageArg: ClientUniverseMessage<any>) => void) {
return this.subject.subscribe(
messageArg => {
observingFunctionArg(messageArg);
@ -89,7 +88,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel {
* @param messageArg
*/
public async sendMessage(messageArg: interfaces.IMessageCreator) {
await this.clientUniverseRef.checkConnection();
await this.clientUniverseRef.start(); // its ok to call this multiple times
const universeMessageToSend: interfaces.IUniverseMessage = {
id: plugins.smartunique.shortId(),
timestamp: Date.now(),

View File

@ -5,7 +5,9 @@ import { ReactionResult } from './smartuniverse.classes.reactionresult';
import { UniverseMessage } from './smartuniverse.classes.universemessage';
import { ClientUniverseMessage } from './smartuniverse.classes.clientuniversemessage';
export interface IReactionRequestConstructorOptions<T extends plugins.typedrequestInterfaces.ITypedRequest> {
export interface IReactionRequestConstructorOptions<
T extends plugins.typedrequestInterfaces.ITypedRequest
> {
method: T['method'];
}
@ -15,9 +17,9 @@ export interface ICombinatorPayload<T extends plugins.typedrequestInterfaces.ITy
*/
id: string;
typedRequestPayload: {
method: T['method'],
request : T['request'],
response: T['response']
method: T['method'];
request: T['request'];
response: T['response'];
};
}
@ -28,20 +30,35 @@ export class ReactionRequest<T extends plugins.typedrequestInterfaces.ITypedRequ
this.method = optionsArg.method;
}
public async fire(channelsArg: Array<UniverseChannel | ClientUniverseChannel>, requestDataArg: T['request'], timeoutMillisArg=5000) {
public async fire(
channelsArg: Array<UniverseChannel | ClientUniverseChannel>,
requestDataArg: T['request'],
timeoutMillisArg = 5000
) {
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;
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);
}
}
reactionResult.pushReactionResponse(payload.typedRequestPayload.response);
}
}));
)
);
const payload: ICombinatorPayload<T> = {
id: requestId,
typedRequestPayload: {

View File

@ -6,7 +6,9 @@ import { ClientUniverseChannel } from './smartuniverse.classes.clientuniversecha
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 type TReactionResponseFuncDef<T extends plugins.typedrequestInterfaces.ITypedRequest> = (
dataArg: T['request']
) => Promise<T['response']>;
export interface IReactionResponseConstructorOptions<
T extends plugins.typedrequestInterfaces.ITypedRequest
@ -42,7 +44,9 @@ export class ReactionResponse<T extends plugins.typedrequestInterfaces.ITypedReq
messageArg.messageText === 'reactionRequest' &&
messageArg.payload.typedRequestPayload.method === this.method
) {
const response: T['response'] = await this.funcDef(messageArg.payload.typedRequestPayload.request);
const response: T['response'] = await this.funcDef(
messageArg.payload.typedRequestPayload.request
);
const payload: ICombinatorPayload<T> = {
...messageArg.payload,
typedRequestPayload: {
@ -52,7 +56,7 @@ export class ReactionResponse<T extends plugins.typedrequestInterfaces.ITypedReq
};
channelArg.sendMessage({
messageText: 'reactionResponse',
payload
payload
});
}
}

View File

@ -6,7 +6,7 @@ export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedReque
private endResult: Array<T['response']> = [];
private completeDeferred = plugins.smartpromise.defer<Array<T['response']>>();
constructor () {
constructor() {
this.resultSubscribe(responseArg => {
this.endResult.push(responseArg);
});
@ -42,7 +42,7 @@ export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedReque
public async pushReactionResponse(responseArg: T['response']) {
this.resultReplaySubject.next(responseArg);
}
/**
* completes the ReactionResult
*/

View File

@ -78,12 +78,14 @@ export class UniverseMessage<T> implements interfaces.IUniverseMessage {
this.destructionTimer = new Timer(selfdestructAfterArg);
this.destructionTimer.start();
// set up self destruction by removing this from the parent messageCache
this.destructionTimer.completed.then(async () => {
this.universeCache.messageMap.remove(this);
}).catch(err => {
console.log(err);
console.log(this);
});
this.destructionTimer.completed
.then(async () => {
this.universeCache.messageMap.remove(this);
})
.catch(err => {
console.log(err);
console.log(this);
});
} else {
plugins.smartdelay.delayFor(1000).then(() => {
if (!this.destructionTimer) {

View File

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