fix(core): update
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| export * from './http.interfaces'; | ||||
| export * from './socketfunctionrequests'; | ||||
| export * from './universechannel.interfaces'; | ||||
| export * from './universemessage.interfaces'; | ||||
| export * from './universeactions.interfaces'; | ||||
|   | ||||
							
								
								
									
										20
									
								
								ts/interfaces/socketfunctionrequests.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								ts/interfaces/socketfunctionrequests.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -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' | ||||
|   }; | ||||
| } | ||||
| @@ -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; | ||||
|   | ||||
| @@ -5,7 +5,7 @@ export interface IMessageCreator { | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * A universe | ||||
|  */ | ||||
| export interface IUniverseMessage extends IMessageCreator { | ||||
|   id: string; | ||||
|   | ||||
| @@ -113,14 +113,17 @@ export class ClientUniverse { | ||||
|       /** | ||||
|        * handles message reception | ||||
|        */ | ||||
|       const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({ | ||||
|       const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_ProcessMessage>({ | ||||
|         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' | ||||
|           }; | ||||
|         } | ||||
|       }); | ||||
|  | ||||
|   | ||||
| @@ -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<interfaces.ISocketRequest_SubscribeChannel>( | ||||
|         'subscribeChannel', | ||||
|         { | ||||
|           name: this.name, | ||||
|           passphrase: this.passphrase | ||||
|         } | ||||
|       ); | ||||
|       this.status = response.subscriptionStatus; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|   | ||||
| @@ -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<interfaces.ISocketRequest_SubscribeChannel>({ | ||||
|       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); | ||||
|         } | ||||
|       } | ||||
|     }); | ||||
|  | ||||
|   | ||||
| @@ -98,7 +98,7 @@ export class UniverseConnection { | ||||
|    * the socketClient to ping | ||||
|    */ | ||||
|   public socketConnection: plugins.smartsocket.SocketConnection; | ||||
|   public authenticationRequests: interfaces.IServerCallSubscribeActionPayload[] = []; | ||||
|   public authenticationRequests: Array<interfaces.ISocketRequest_SubscribeChannel['request']> = []; | ||||
|   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<interfaces.ISocketRequest_SubscribeChannel['request']>; | ||||
|   }) { | ||||
|     this.authenticationRequests = optionsArg.authenticationRequests; | ||||
|     this.socketConnection = optionsArg.socketConnection; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user