Files
smartuniverse/ts/smartuniverse.classes.universe.ts
T

152 lines
4.6 KiB
TypeScript

import * as plugins from './smartuniverse.plugins.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;
}
/**
* main class that setups a Universe
*/
export class Universe {
// subinstances
public universeCache: UniverseCache;
// options
private options: ISmartUniverseConstructorOptions;
/**
* 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 = '';
/**
* 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 (!portArg) {
throw new Error(`You need to specify a portArg to start on.`);
}
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
alias: 'smartuniverse',
port: portArg,
});
const socketFunctionSubscription =
new plugins.smartsocket.SocketFunction<interfaces.ISocketRequest_SubscribeChannel>({
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<interfaces.ISocketRequest_ProcessMessage>({
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 {
messageStatus: 'authentication required',
};
}
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);
return {
messageStatus: 'ok',
};
}
return {
messageStatus: 'channel not found',
};
},
});
// 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();
}
}