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';
|
2018-05-28 10:07:25 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
export interface ISmartUniverseConstructorOptions {
|
|
|
|
messageExpiryInMilliseconds: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IServerGetMessagesRequestBody {
|
2018-04-29 12:17:26 +00:00
|
|
|
channel: string;
|
|
|
|
topic?: string;
|
2018-03-13 05:15:40 +00:00
|
|
|
youngerThan: number;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:14:57 +00:00
|
|
|
/**
|
|
|
|
* the interface for a standard request
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
export interface IServerPutMessageRequestBody {
|
2018-05-19 22:41:59 +00:00
|
|
|
channel: string;
|
|
|
|
passphrase: string;
|
2018-03-20 07:16:54 +00:00
|
|
|
message: string;
|
|
|
|
payload: any;
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:14:57 +00:00
|
|
|
/**
|
|
|
|
* main class that setsup a Universe
|
|
|
|
*/
|
2018-03-13 05:15:40 +00:00
|
|
|
export class Universe {
|
|
|
|
// subinstances
|
2018-05-26 11:44:32 +00:00
|
|
|
public universeCache: UniverseCache;
|
2018-03-13 05:15:40 +00:00
|
|
|
|
|
|
|
// options
|
|
|
|
private options: ISmartUniverseConstructorOptions;
|
2018-03-20 07:16:54 +00:00
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
// Store version handling
|
|
|
|
private universeVersionStore: string;
|
|
|
|
private get universeVersion() {
|
|
|
|
if (this.universeVersionStore) {
|
|
|
|
return this.universeVersionStore;
|
|
|
|
} else {
|
|
|
|
const packageJson = plugins.smartfile.fs.toObjectSync(paths.packageJson);
|
|
|
|
this.universeVersionStore = packageJson.version;
|
|
|
|
return this.universeVersionStore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private smartexpressServer: plugins.smartexpress.Server;
|
2018-03-20 07:16:54 +00:00
|
|
|
private smartsocket: plugins.smartsocket.Smartsocket;
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
constructor(optionsArg: ISmartUniverseConstructorOptions) {
|
|
|
|
this.options = optionsArg;
|
2018-05-26 11:44:32 +00:00
|
|
|
this.universeCache = new UniverseCache(this.options.messageExpiryInMilliseconds);
|
2018-03-13 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 10:07:25 +00:00
|
|
|
/**
|
|
|
|
* adds a channel to the Universe
|
|
|
|
*/
|
|
|
|
public async addChannel(nameArg: string, passphraseArg: string) {
|
|
|
|
const newChannel = new UniverseChannel(this.universeCache, nameArg, passphraseArg);
|
|
|
|
this.universeCache.channelMap.add(newChannel);
|
|
|
|
}
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
/**
|
|
|
|
* initiates a server
|
|
|
|
*/
|
|
|
|
public async initServer(portArg: number | string) {
|
|
|
|
this.smartexpressServer = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
2019-01-31 01:52:18 +00:00
|
|
|
defaultAnswer: async () => {
|
|
|
|
return `smartuniverse server ${this.universeVersion}`;
|
|
|
|
},
|
2018-03-13 05:15:40 +00:00
|
|
|
forceSsl: false,
|
|
|
|
port: portArg
|
|
|
|
});
|
|
|
|
|
2018-04-13 13:45:48 +00:00
|
|
|
// add websocket upgrade
|
|
|
|
this.smartsocket = new plugins.smartsocket.Smartsocket({
|
|
|
|
port: 12345 // fix this within smartsocket
|
|
|
|
});
|
|
|
|
|
2018-05-30 14:34:06 +00:00
|
|
|
const ClientRole = new plugins.smartsocket.SocketRole({
|
|
|
|
name: 'clientuniverse',
|
|
|
|
passwordHash: 'clientuniverse' // authentication happens on another level
|
|
|
|
});
|
|
|
|
|
|
|
|
this.smartsocket.addSocketRoles([ClientRole]);
|
|
|
|
|
|
|
|
const SubscriptionSocketFunction = new plugins.smartsocket.SocketFunction({
|
|
|
|
allowedRoles: [ClientRole],
|
|
|
|
funcName: 'channelSubscription',
|
|
|
|
funcDef: () => {}
|
|
|
|
});
|
|
|
|
|
2018-05-28 10:07:25 +00:00
|
|
|
this.smartsocket.setExternalServer('express', this.smartexpressServer as any);
|
|
|
|
// should work with express as well
|
2018-04-13 13:45:48 +00:00
|
|
|
this.smartsocket.start();
|
|
|
|
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.start();
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:16:54 +00:00
|
|
|
public async stopServer() {
|
2018-03-27 22:38:32 +00:00
|
|
|
await this.smartsocket.stop();
|
2018-03-13 05:15:40 +00:00
|
|
|
await this.smartexpressServer.stop();
|
|
|
|
}
|
|
|
|
}
|