smartuniverse/ts/smartuniverse.classes.universe.ts

183 lines
5.8 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
import * as plugins from './smartuniverse.plugins';
2019-01-31 01:52:18 +00:00
import { Handler, Route, Server } from '@pushrocks/smartexpress';
import { UniverseCache, UniverseChannel, UniverseMessage } from './';
2018-04-13 13:45:48 +00:00
2018-03-13 05:15:40 +00:00
import * as paths from './smartuniverse.paths';
2019-04-11 15:52:01 +00:00
import * as interfaces from './interfaces';
2019-04-30 17:16:03 +00:00
import { UniverseConnection } from './smartuniverse.classes.universeconnection';
2020-09-24 18:13:48 +00:00
import { logger } from './smartuniverse.logging';
2019-04-11 15:52:01 +00:00
2018-03-13 05:15:40 +00:00
export interface ISmartUniverseConstructorOptions {
messageExpiryInMilliseconds: number;
2019-09-01 14:54:36 +00:00
externalServer?: plugins.smartexpress.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
*/
private smartexpressServer: plugins.smartexpress.Server;
/**
* 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
*/
2019-04-30 17:16:03 +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;
}
}
/**
* 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) {
2020-09-24 18:17:52 +00:00
return this.universeCache.channelMap.find((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
*/
2019-04-22 22:28:57 +00:00
public async start(portArg: number) {
2019-04-11 15:52:01 +00:00
// lets create the base smartexpress server
2019-09-01 14:54:36 +00:00
if (!this.options.externalServer) {
this.smartexpressServer = new plugins.smartexpress.Server({
cors: true,
defaultAnswer: async () => {
return `smartuniverse server ${this.getUniverseVersion()}`;
},
forceSsl: false,
2020-09-24 18:17:52 +00:00
port: portArg,
2019-09-01 14:54:36 +00:00
});
} else {
2019-09-01 16:22:44 +00:00
console.log('Universe is using externally supplied server');
2019-09-01 14:54:36 +00:00
this.smartexpressServer = this.options.externalServer;
}
2018-03-13 05:15:40 +00:00
2018-04-13 13:45:48 +00:00
// add websocket upgrade
2019-04-24 16:20:31 +00:00
this.smartsocket = new plugins.smartsocket.Smartsocket({});
2018-04-13 13:45:48 +00:00
2019-04-11 15:52:01 +00:00
// add a role for the clients
const ClientRole = new plugins.smartsocket.SocketRole({
2019-04-24 16:20:31 +00:00
name: 'UniverseClient',
2020-09-24 18:17:52 +00:00
passwordHash: plugins.smarthash.sha256FromStringSync('UniverseClient'), // authentication happens on another level
});
2019-04-11 15:52:01 +00:00
// add the role to smartsocket
this.smartsocket.addSocketRoles([ClientRole]);
2019-09-10 08:55:10 +00:00
const socketFunctionSubscription = new plugins.smartsocket.SocketFunction<
interfaces.ISocketRequest_SubscribeChannel
>({
2019-06-06 21:23:37 +00:00
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
2019-09-09 22:29:08 +00:00
funcName: 'subscribeChannel',
2019-09-10 08:55:10 +00:00
funcDef: async (dataArg, socketConnectionArg) => {
2019-09-09 22:29:08 +00:00
const universeConnection = new UniverseConnection({
2019-11-06 23:59:45 +00:00
universe: this,
2019-09-09 22:29:08 +00:00
socketConnection: socketConnectionArg,
2020-09-24 18:17:52 +00:00
authenticationRequests: [dataArg],
2019-09-09 22:29:08 +00:00
});
await UniverseConnection.addConnectionToCache(this, universeConnection);
return {
2020-09-24 18:17:52 +00:00
subscriptionStatus: 'subscribed',
2019-09-09 22:29:08 +00:00
};
2020-09-24 18:17:52 +00:00
},
2019-08-12 12:59:37 +00:00
});
2019-08-13 11:04:49 +00:00
const socketFunctionProcessMessage = new plugins.smartsocket.SocketFunction({
2019-08-12 12:59:37 +00:00
allowedRoles: [ClientRole], // there is only one client role, Authentication happens on another level
funcName: 'processMessage',
funcDef: async (dataArg: 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,
dataArg
);
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
// start the server
2019-09-01 14:54:36 +00:00
if (!this.options.externalServer) {
await this.smartexpressServer.start();
}
2019-09-01 19:27:45 +00:00
// add smartsocket to the running smartexpress app
await this.smartsocket.setExternalServer('smartexpress', this.smartexpressServer);
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();
2019-09-01 14:54:36 +00:00
if (!this.options.externalServer) {
await this.smartexpressServer.stop();
}
2018-03-13 05:15:40 +00:00
}
}