diff --git a/package-lock.json b/package-lock.json index eae87ab..23acf32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,11 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@apiglobal/typedrequest-interfaces": { + "version": "1.0.7", + "resolved": "https://verdaccio.lossless.one/@apiglobal%2ftypedrequest-interfaces/-/typedrequest-interfaces-1.0.7.tgz", + "integrity": "sha512-yPl0UcLFMwSQL7bK52wVjkgvadC+x2YS3+7T15V1A1dXNxa96yd4WX1fqcKqwnBrvYexq/8FaxWGi98tZ0oNwg==" + }, "@babel/code-frame": { "version": "7.5.5", "resolved": "https://verdaccio.lossless.one/@babel%2fcode-frame/-/code-frame-7.5.5.tgz", @@ -290,9 +295,9 @@ "integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ==" }, "@pushrocks/smartrequest": { - "version": "1.1.23", - "resolved": "https://registry.npmjs.org/@pushrocks/smartrequest/-/smartrequest-1.1.23.tgz", - "integrity": "sha512-Hws3YfzIE0b/E3aTkSugLskKWBq7e8HDXEN+RlRyTFONxW/XONKJFTw4mp3jk+puWpYGDoOTcP+Ua4jd19z9pA==", + "version": "1.1.27", + "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrequest/-/smartrequest-1.1.27.tgz", + "integrity": "sha512-GJO6duKBro2oILtXduVQwxA50AzjHZbKP0P32OoiM+B1ftoQFf4D8zSs/SCljNKXUqS8SZ9IMQVlKLfRC4nu+A==", "requires": { "@pushrocks/smartpromise": "^3.0.2", "@types/form-data": "^2.2.1", @@ -331,10 +336,11 @@ } }, "@pushrocks/smartsocket": { - "version": "1.1.46", - "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.1.46.tgz", - "integrity": "sha512-MaUjOhd/v/YACRf+JLlaXJB8efiUncmy46f1RF9sHnBgkWH8Pq55WY+sa5rFd+1Tk+rVy2VRI22tmc0r4pqwJA==", + "version": "1.1.49", + "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartsocket/-/smartsocket-1.1.49.tgz", + "integrity": "sha512-jDcqVU9/cXTkLBY2N3epPUBKtRRkFINzK//CRxmBcuZx/k0s5oeNM1tOI7aiP5p5arrybU/YksSQil1QKz24Gg==", "requires": { + "@apiglobal/typedrequest-interfaces": "^1.0.7", "@pushrocks/lik": "^3.0.11", "@pushrocks/smartdelay": "^2.0.3", "@pushrocks/smartexpress": "^3.0.40", @@ -344,9 +350,24 @@ "@types/shortid": "0.0.29", "@types/socket.io": "^2.1.2", "@types/socket.io-client": "^1.4.32", - "shortid": "^2.2.14", + "shortid": "^2.2.15", "socket.io": "^2.2.0", "socket.io-client": "^2.2.0" + }, + "dependencies": { + "nanoid": { + "version": "2.1.0", + "resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.0.tgz", + "integrity": "sha512-g5WwS+p6Cm+zQhO2YOpRbQThZVnNb7DDq74h8YDCLfAGynrEOrbx2E16dc8ciENiP1va5sqaAruqn2sN+xpkWg==" + }, + "shortid": { + "version": "2.2.15", + "resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.15.tgz", + "integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==", + "requires": { + "nanoid": "^2.1.0" + } + } } }, "@pushrocks/smarttime": { @@ -515,9 +536,9 @@ "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==" }, "@types/node": { - "version": "12.7.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.3.tgz", - "integrity": "sha512-3SiLAIBkDWDg6vFo0+5YJyHPWU9uwu40Qe+v+0MH8wRKYBimHvvAOyk3EzMrD/TrIlLYfXrqDqrg913PynrMJQ==" + "version": "12.7.4", + "resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-12.7.4.tgz", + "integrity": "sha512-W0+n1Y+gK/8G2P/piTkBBN38Qc5Q1ZSO6B5H3QmPCUewaiXOo2GCAWZ4ElZCcNhjJuBSUSLGFUJnmlCn5+nxOQ==" }, "@types/range-parser": { "version": "1.2.3", diff --git a/package.json b/package.json index 0f42c2f..a7ae6a6 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@gitzone/tsbuild": "^2.1.17", "@gitzone/tstest": "^1.0.24", "@pushrocks/tapbundle": "^3.0.13", - "@types/node": "^12.7.3", + "@types/node": "^12.7.4", "tslint": "^5.19.0", "tslint-config-prettier": "^1.18.0" }, @@ -32,9 +32,9 @@ "@pushrocks/smarthash": "^2.0.6", "@pushrocks/smartlog": "^2.0.19", "@pushrocks/smartpromise": "^3.0.2", - "@pushrocks/smartrequest": "^1.1.23", + "@pushrocks/smartrequest": "^1.1.27", "@pushrocks/smartrx": "^2.0.3", - "@pushrocks/smartsocket": "^1.1.46", + "@pushrocks/smartsocket": "^1.1.49", "@pushrocks/smarttime": "^3.0.12", "@pushrocks/smartunique": "^3.0.1" }, diff --git a/ts/interfaces/index.ts b/ts/interfaces/index.ts index ff388d2..0234f9f 100644 --- a/ts/interfaces/index.ts +++ b/ts/interfaces/index.ts @@ -1,4 +1,5 @@ export * from './http.interfaces'; +export * from './socketfunctionrequests'; export * from './universechannel.interfaces'; export * from './universemessage.interfaces'; export * from './universeactions.interfaces'; diff --git a/ts/interfaces/socketfunctionrequests.ts b/ts/interfaces/socketfunctionrequests.ts new file mode 100644 index 0000000..ece7bc2 --- /dev/null +++ b/ts/interfaces/socketfunctionrequests.ts @@ -0,0 +1,20 @@ +import * as interfaces from './index'; + +export interface ISocketRequest_SubscribeChannel { + method: 'subscribeChannel'; + request: { + name: string; + passphrase: string; + }; + response: { + subscriptionStatus: 'subscribed' | 'unsubscribed' + }; +} + +export interface ISocketRequest_ProcessMessage { + method: 'processMessage'; + request: interfaces.IUniverseMessage; + response: { + messageStatus: 'ok' + }; +} \ No newline at end of file diff --git a/ts/interfaces/universeactions.interfaces.ts b/ts/interfaces/universeactions.interfaces.ts index 243057a..bfdca83 100644 --- a/ts/interfaces/universeactions.interfaces.ts +++ b/ts/interfaces/universeactions.interfaces.ts @@ -4,13 +4,6 @@ export type IServerCallActions = | 'channelUnsubscribe' | 'terminateConnection'; -/** - * the interface for a subscription - */ -export interface IServerCallSubscribeActionPayload { - name: string; - passphrase: string; -} export interface IServerUnsubscribeActionPayload { name: string; diff --git a/ts/interfaces/universemessage.interfaces.ts b/ts/interfaces/universemessage.interfaces.ts index faccbd1..775b42d 100644 --- a/ts/interfaces/universemessage.interfaces.ts +++ b/ts/interfaces/universemessage.interfaces.ts @@ -5,7 +5,7 @@ export interface IMessageCreator { } /** - * + * A universe */ export interface IUniverseMessage extends IMessageCreator { id: string; diff --git a/ts/smartuniverse.classes.clientuniverse.ts b/ts/smartuniverse.classes.clientuniverse.ts index 9551960..34d7504 100644 --- a/ts/smartuniverse.classes.clientuniverse.ts +++ b/ts/smartuniverse.classes.clientuniverse.ts @@ -113,14 +113,17 @@ export class ClientUniverse { /** * handles message reception */ - const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({ + const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({ funcName: 'processMessage', allowedRoles: [], - funcDef: async (messageDescriptorArg: interfaces.IUniverseMessage) => { + funcDef: async (messageDescriptorArg) => { plugins.smartlog.defaultLogger.log('info', 'Got message from server'); this.observableIntake.push( ClientUniverseMessage.createMessageFromMessageDescriptor(messageDescriptorArg) ); + return { + messageStatus: 'ok' + }; } }); diff --git a/ts/smartuniverse.classes.clientuniversechannel.ts b/ts/smartuniverse.classes.clientuniversechannel.ts index ba9d0be..ee3fd10 100644 --- a/ts/smartuniverse.classes.clientuniversechannel.ts +++ b/ts/smartuniverse.classes.clientuniversechannel.ts @@ -34,6 +34,7 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { // properties public name: string; public passphrase: string; + public status: 'subscribed' | 'unsubscribed' = 'unsubscribed'; // refs public clientUniverseRef: ClientUniverse; @@ -49,15 +50,16 @@ export class ClientUniverseChannel implements interfaces.IUniverseChannel { * tells the universe about this instances interest into a channel */ public async subscribe() { - const serverCallActionName: interfaces.IServerCallActions = 'channelSubscription'; - const serverCallActionPayload: interfaces.IServerCallSubscribeActionPayload = { - name: this.name, - passphrase: this.passphrase - }; - await this.clientUniverseRef.smartsocketClient.serverCall( - serverCallActionName, - serverCallActionPayload - ); + if (this.status === 'unsubscribed') { + const response = await this.clientUniverseRef.smartsocketClient.serverCall( + 'subscribeChannel', + { + name: this.name, + passphrase: this.passphrase + } + ); + this.status = response.subscriptionStatus; + } } /** diff --git a/ts/smartuniverse.classes.universe.ts b/ts/smartuniverse.classes.universe.ts index 9ab040f..61696a9 100644 --- a/ts/smartuniverse.classes.universe.ts +++ b/ts/smartuniverse.classes.universe.ts @@ -95,24 +95,21 @@ export class Universe { // add the role to smartsocket this.smartsocket.addSocketRoles([ClientRole]); - const socketFunctionSubscription = new plugins.smartsocket.SocketFunction({ + const socketFunctionSubscription = new plugins.smartsocket.SocketFunction({ allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level - funcName: 'channelSubscription', + funcName: 'subscribeChannel', funcDef: async ( - dataArg: interfaces.IServerCallSubscribeActionPayload, + dataArg, socketConnectionArg ) => { - // run in "this context" of this class - await (async () => { - const universeConnection = new UniverseConnection({ - socketConnection: socketConnectionArg, - authenticationRequests: [dataArg] - }); - await UniverseConnection.addConnectionToCache(this, universeConnection); - return { - 'subscription status': 'success' - }; - })(); + const universeConnection = new UniverseConnection({ + socketConnection: socketConnectionArg, + authenticationRequests: [dataArg] + }); + await UniverseConnection.addConnectionToCache(this, universeConnection); + return { + subscriptionStatus: 'subscribed' + }; } }); @@ -120,39 +117,36 @@ export class Universe { allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level funcName: 'processMessage', funcDef: async (dataArg: interfaces.IUniverseMessage, socketConnectionArg) => { - // run in "this" context of this class - await (async () => { - const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection( - this.universeCache, - socketConnectionArg + const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection( + this.universeCache, + socketConnectionArg + ); + if (universeConnection) { + plugins.smartlog.defaultLogger.log( + 'ok', + 'found UniverseConnection for socket for incoming message' ); - if (universeConnection) { - plugins.smartlog.defaultLogger.log( - 'ok', - 'found UniverseConnection for socket for incoming message' - ); - } else { - plugins.smartlog.defaultLogger.log( - 'warn', - 'found no Authorized channel for incoming message' - ); - return { - error: 'You need to authenticate for a channel' - }; - } - const unauthenticatedMessage = UniverseMessage.createMessageFromPayload( - socketConnectionArg, - dataArg + } else { + plugins.smartlog.defaultLogger.log( + 'warn', + 'found no Authorized channel for incoming message' ); - const foundChannel = await UniverseChannel.authorizeAMessageForAChannel( - this.universeCache, - unauthenticatedMessage - ); - if (foundChannel && unauthenticatedMessage.authenticated) { - const authenticatedMessage = unauthenticatedMessage; - await this.universeCache.addMessage(authenticatedMessage); - } - })(); + return { + error: 'You need to authenticate for a channel' + }; + } + const unauthenticatedMessage = UniverseMessage.createMessageFromPayload( + socketConnectionArg, + dataArg + ); + const foundChannel = await UniverseChannel.authorizeAMessageForAChannel( + this.universeCache, + unauthenticatedMessage + ); + if (foundChannel && unauthenticatedMessage.authenticated) { + const authenticatedMessage = unauthenticatedMessage; + await this.universeCache.addMessage(authenticatedMessage); + } } }); diff --git a/ts/smartuniverse.classes.universeconnection.ts b/ts/smartuniverse.classes.universeconnection.ts index 2881fa1..d693b57 100644 --- a/ts/smartuniverse.classes.universeconnection.ts +++ b/ts/smartuniverse.classes.universeconnection.ts @@ -98,7 +98,7 @@ export class UniverseConnection { * the socketClient to ping */ public socketConnection: plugins.smartsocket.SocketConnection; - public authenticationRequests: interfaces.IServerCallSubscribeActionPayload[] = []; + public authenticationRequests: Array = []; public subscribedChannels: UniverseChannel[] = []; public authenticatedChannels: UniverseChannel[] = []; public failedToJoinChannels: UniverseChannel[] = []; @@ -113,7 +113,7 @@ export class UniverseConnection { constructor(optionsArg: { socketConnection: plugins.smartsocket.SocketConnection; - authenticationRequests: interfaces.IServerCallSubscribeActionPayload[]; + authenticationRequests: Array; }) { this.authenticationRequests = optionsArg.authenticationRequests; this.socketConnection = optionsArg.socketConnection;