import * as plugins from './smartuniverse.plugins.js'; import * as pluginsTyped from './smartuniverse.pluginstyped.js'; import { UniverseCache, UniverseChannel, UniverseMessage } from './index.js'; import * as interfaces from './interfaces/index.js'; import { UniverseConnection } from './smartuniverse.classes.universeconnection.js'; import { logger } from './smartuniverse.logging.js'; export interface ISmartUniverseConstructorOptions { messageExpiryInMilliseconds: number; externalServer?: pluginsTyped.typedserver.servertools.Server; } /** * main class that setups a Universe */ export class Universe { // subinstances public universeCache: UniverseCache; // options private options: ISmartUniverseConstructorOptions; /** * the smartexpress server used */ private server: pluginsTyped.typedserver.servertools.Server; /** * the smartsocket used */ private smartsocket: plugins.smartsocket.Smartsocket; constructor(optionsArg: ISmartUniverseConstructorOptions) { this.options = optionsArg; this.universeCache = new UniverseCache(this, this.options.messageExpiryInMilliseconds); } /** * stores the version of the universe server running * this is done since the version is exposed through the api and multiple fs actions are avoided this way. */ private universeVersionStore: string; /** * get the currently running version of smartuniverse */ /* public getUniverseVersion() { if (this.universeVersionStore) { return this.universeVersionStore; } else { const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson); this.universeVersionStore = packageJson.version; return this.universeVersionStore; } } */ /** * adds a channel to the Universe */ public addChannel(nameArg: string, passphraseArg: string) { const newChannel = UniverseChannel.createChannel(this, nameArg, passphraseArg); return newChannel; } /** * returns a channel */ public getChannel(channelNameArg: string) { return this.universeCache.channelMap.findSync((channelArg) => { return channelArg.name === channelNameArg; }); } /** * initiates a server */ public async start(portArg?: number) { if (!this.options.externalServer && !portArg) { throw new Error(`You supplied an external error. You need to specify a portArg to start on.`); } portArg = portArg || 3000; // TODO: remove // add websocket upgrade this.smartsocket = new plugins.smartsocket.Smartsocket({ alias: 'smartuniverse', port: portArg, }); // lets create the base smartexpress server if (this.options.externalServer) { console.log('Universe is using externally supplied server'); this.smartsocket.setExternalServer('smartexpress', this.options.externalServer); } const socketFunctionSubscription = new plugins.smartsocket.SocketFunction({ funcName: 'subscribeChannel', funcDef: async (dataArg, socketConnectionArg) => { const universeConnection = new UniverseConnection({ universe: this, socketConnection: socketConnectionArg, authenticationRequests: [dataArg], }); await UniverseConnection.addConnectionToCache(this, universeConnection); return { subscriptionStatus: 'subscribed', }; }, }); const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({ // TODO proper ITypedRequest here instead of any funcName: 'processMessage', funcDef: async (messageDataArg: interfaces.IUniverseMessage, socketConnectionArg) => { const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection( this.universeCache, socketConnectionArg ); if (universeConnection) { logger.log('ok', 'found UniverseConnection for socket for incoming message'); } else { logger.log('warn', 'found no Authorized channel for incoming message'); return { error: 'You need to authenticate for a channel', }; } const unauthenticatedMessage = UniverseMessage.createMessageFromPayload( socketConnectionArg, messageDataArg ); const foundChannel = await UniverseChannel.authorizeAMessageForAChannel( this.universeCache, unauthenticatedMessage ); if (foundChannel && unauthenticatedMessage.authenticated) { const authenticatedMessage = unauthenticatedMessage; await this.universeCache.addMessage(authenticatedMessage); } }, }); // add socket functions this.smartsocket.addSocketFunction(socketFunctionSubscription); this.smartsocket.addSocketFunction(socketFunctionProcessMessage); // add smartsocket to the running smartexpress app await this.smartsocket.start(); logger.log('success', 'started universe'); } /** * stop everything */ public async stopServer() { await this.smartsocket.stop(); } }