smartuniverse/ts/smartuniverse.classes.universe.ts

162 lines
5.0 KiB
TypeScript
Raw Permalink Normal View History

2023-07-25 09:33:13 +00:00
import * as plugins from './smartuniverse.plugins.js';
import * as pluginsTyped from './smartuniverse.pluginstyped.js';
2018-03-13 05:15:40 +00:00
2023-07-25 09:33:13 +00:00
import { UniverseCache, UniverseChannel, UniverseMessage } from './index.js';
2018-04-13 13:45:48 +00:00
2023-07-25 09:33:13 +00:00
import * as interfaces from './interfaces/index.js';
import { UniverseConnection } from './smartuniverse.classes.universeconnection.js';
import { logger } from './smartuniverse.logging.js';
2019-04-11 15:52:01 +00:00
2018-03-13 05:15:40 +00:00
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
2023-07-25 09:33:13 +00:00
externalServer?: pluginsTyped.typedserver.servertools.Server;
2018-03-13 05:15:40 +00:00
}
2018-05-23 22:14:57 +00:00
/**
2019-08-12 12:59:37 +00:00
* main class that setups a Universe
2018-05-23 22:14:57 +00:00
*/
2018-03-13 05:15:40 +00:00
export class Universe {
// subinstances
public universeCache: UniverseCache;
2018-03-13 05:15:40 +00:00
// options
private options: ISmartUniverseConstructorOptions;
2018-03-20 07:16:54 +00:00
2019-04-30 17:16:03 +00:00
/**
* the smartexpress server used
*/
2023-07-25 09:33:13 +00:00
private server: pluginsTyped.typedserver.servertools.Server;
2019-04-30 17:16:03 +00:00
/**
* the smartsocket used
*/
private smartsocket: plugins.smartsocket.Smartsocket;
constructor(optionsArg: ISmartUniverseConstructorOptions) {
this.options = optionsArg;
2019-08-13 13:48:20 +00:00
this.universeCache = new UniverseCache(this, this.options.messageExpiryInMilliseconds);
2019-04-30 17:16:03 +00:00
}
2019-04-11 15:52:01 +00:00
/**
* 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.
*/
2018-03-13 05:15:40 +00:00
private universeVersionStore: string;
2019-04-11 15:52:01 +00:00
/**
* get the currently running version of smartuniverse
*/
2020-09-30 00:50:43 +00:00
/* public getUniverseVersion() {
2018-03-13 05:15:40 +00:00
if (this.universeVersionStore) {
return this.universeVersionStore;
} else {
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
this.universeVersionStore = packageJson.version;
return this.universeVersionStore;
}
2020-09-30 00:50:43 +00:00
} */
2018-03-13 05:15:40 +00:00
/**
* adds a channel to the Universe
*/
2019-08-13 16:43:33 +00:00
public addChannel(nameArg: string, passphraseArg: string) {
2019-08-13 13:48:20 +00:00
const newChannel = UniverseChannel.createChannel(this, nameArg, passphraseArg);
2019-09-10 08:50:55 +00:00
return newChannel;
}
2019-09-10 08:50:55 +00:00
/**
* returns a channel
*/
2019-09-17 12:01:24 +00:00
public getChannel(channelNameArg: string) {
2023-07-25 09:33:13 +00:00
return this.universeCache.channelMap.findSync((channelArg) => {
2019-09-10 08:50:55 +00:00
return channelArg.name === channelNameArg;
});
2019-09-10 08:51:18 +00:00
}
2019-09-10 08:50:55 +00:00
2018-03-13 05:15:40 +00:00
/**
* initiates a server
*/
2020-09-30 17:39:29 +00:00
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
2020-09-30 00:13:14 +00:00
// add websocket upgrade
this.smartsocket = new plugins.smartsocket.Smartsocket({
2023-07-25 09:33:13 +00:00
alias: 'smartuniverse',
port: portArg,
2020-09-30 00:13:14 +00:00
});
2019-04-11 15:52:01 +00:00
// lets create the base smartexpress server
2020-09-30 00:13:14 +00:00
if (this.options.externalServer) {
2019-09-01 16:22:44 +00:00
console.log('Universe is using externally supplied server');
2023-07-25 09:33:13 +00:00
this.smartsocket.setExternalServer('smartexpress', this.options.externalServer);
2019-09-01 14:54:36 +00:00
}
2018-03-13 05:15:40 +00:00
2023-07-25 09:33:13 +00:00
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',
};
},
});
2019-08-12 12:59:37 +00:00
2023-07-25 09:33:13 +00:00
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction<any>({
// TODO proper ITypedRequest here instead of any
2019-08-12 12:59:37 +00:00
funcName: 'processMessage',
2021-01-26 01:59:06 +00:00
funcDef: async (messageDataArg: interfaces.IUniverseMessage, socketConnectionArg) => {
2019-09-09 22:29:08 +00:00
const universeConnection = UniverseConnection.findUniverseConnectionBySocketConnection(
this.universeCache,
socketConnectionArg
);
if (universeConnection) {
2020-09-24 18:17:52 +00:00
logger.log('ok', 'found UniverseConnection for socket for incoming message');
2019-09-09 22:29:08 +00:00
} else {
2020-09-24 18:17:52 +00:00
logger.log('warn', 'found no Authorized channel for incoming message');
2019-09-09 22:29:08 +00:00
return {
2020-09-24 18:17:52 +00:00
error: 'You need to authenticate for a channel',
2019-09-09 22:29:08 +00:00
};
}
const unauthenticatedMessage = UniverseMessage.createMessageFromPayload(
socketConnectionArg,
2021-01-26 01:59:06 +00:00
messageDataArg
2019-09-09 22:29:08 +00:00
);
const foundChannel = await UniverseChannel.authorizeAMessageForAChannel(
this.universeCache,
unauthenticatedMessage
);
if (foundChannel && unauthenticatedMessage.authenticated) {
const authenticatedMessage = unauthenticatedMessage;
await this.universeCache.addMessage(authenticatedMessage);
}
2020-09-24 18:17:52 +00:00
},
});
2019-08-13 11:04:49 +00:00
// add socket functions
this.smartsocket.addSocketFunction(socketFunctionSubscription);
this.smartsocket.addSocketFunction(socketFunctionProcessMessage);
2019-09-01 19:27:45 +00:00
// add smartsocket to the running smartexpress app
2019-04-24 16:20:31 +00:00
await this.smartsocket.start();
2020-09-24 18:13:48 +00:00
logger.log('success', 'started universe');
2018-03-13 05:15:40 +00:00
}
2019-04-11 15:52:01 +00:00
/**
* stop everything
*/
2018-03-20 07:16:54 +00:00
public async stopServer() {
await this.smartsocket.stop();
2018-03-13 05:15:40 +00:00
}
}