name changes to private members

This commit is contained in:
Erik 2023-07-13 19:28:55 +03:00
parent 1423dbf133
commit bb31c54ab8
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -37,33 +37,33 @@ import FlagManager from './components/FlagManager.js';
class Server extends EventEmitter
{
#_name: string;
#_options: ServerOptions;
#_shardId: number;
#name: string;
#options: ServerOptions;
#shardId: number;
#_ready: boolean;
#_proto: string;
#_port: number;
#_domain: string;
#_serveFiles: string | null;
#_registrationEnabled: boolean;
#_requireCodeForRegister: boolean;
#_OAuthProviders: OAuthProvider[];
#proto: string;
#port: number;
#domain: string;
#serveFiles: string | null;
#registrationEnabled: boolean;
#requireCodeForRegister: boolean;
#OAuthProviders: OAuthProvider[];
#_registry: Registry;
#registry: Registry;
#_server: http.Server | null;
#_app: Express;
#server: http.Server | null;
#app: Express;
#_mariadb: MariaDB;
#_mongodb: MongoDB;
#_memoryStoreProvider: MongoMemory;
#_userDatabase: UserDatabaseInterface;
#_messageBroker?: MessageBroker;
#mariadb: MariaDB;
#mongodb: MongoDB;
#memoryStoreProvider: MongoMemory;
#userDatabase: UserDatabaseInterface;
#messageBroker?: MessageBroker;
#_rateLimiter: RateLimiter;
#_authenticator: Authenticator;
#rateLimiter: RateLimiter;
#authenticator: Authenticator;
#_flagManager: FlagManager;
#flagManager: FlagManager;
#logger: LoggerClient;
@ -87,13 +87,13 @@ class Server extends EventEmitter
const encryption = Util.createEncryptionKey(CRYPTO_SECRET as string, CRYPTO_SALT as string);
process.env.ENCRYPTION_KEY = encryption.key.toString('base64');
this.#_name = name || 'webserver-framework';
this.#_options = options;
this.#_shardId = parseInt(process.env.SHARD_ID as string);
if (isNaN(this.#_shardId))
this.#name = name || 'webserver-framework';
this.#options = options;
this.#shardId = parseInt(process.env.SHARD_ID as string);
if (isNaN(this.#shardId))
throw Util.fatal(new Error('Missing shard ID'));
this.#_ready = false;
this.#_proto = NODE_ENV === 'development' ? 'http' : 'https';
this.#proto = NODE_ENV === 'development' ? 'http' : 'https';
if (!httpOpts?.port)
throw Util.fatal(new Error('Missing http.port in server options'));
@ -101,37 +101,37 @@ class Server extends EventEmitter
this.#logger = new LoggerClient({ ...options.logger, name: this.constructor.name });
// Port number is automatically incremented based on shard #
this.#_port = httpOpts.port + this.#_shardId;
this.#port = httpOpts.port + this.#shardId;
// Primarily used by the OAuth methods for the callback url
if (NODE_ENV === 'development')
{
this.#_domain = `localhost:${this.#_port}`;
this.#domain = `localhost:${this.#port}`;
}
else
{
if (!options.domain)
this.#logger.warn('No domain provided in options.json:serverOptions');
this.#_domain = options.domain;
this.#domain = options.domain;
}
this.#_serveFiles = null; // Holds a reference to the directory from which to serve content
this.#serveFiles = null; // Holds a reference to the directory from which to serve content
if (options.serveFiles)
this.#_serveFiles = path.resolve(options.serveFiles);
this.#serveFiles = path.resolve(options.serveFiles);
this.#_registrationEnabled = options.registrationEnabled;
this.#registrationEnabled = options.registrationEnabled;
// Only lets people with valid registration urls to register
this.#_requireCodeForRegister = options.requireCodeForRegister;
this.#_OAuthProviders = options.OAuthProviders;
this.#requireCodeForRegister = options.requireCodeForRegister;
this.#OAuthProviders = options.OAuthProviders;
this.#_server = null;
this.#_app = express();
this.#server = null;
this.#app = express();
// Takes care of loading endpoints, all endpoints need to inherit the endpoint class in /interfaces
this.#_registry = new Registry(this, { path: path.join(options.dir, 'endpoints') });
this.#registry = new Registry(this, { path: path.join(options.dir, 'endpoints') });
// TODO: Database definitions should probably be elsewhere through injection
// Mariadb isn't strictly necessary here for anything, it's just here pre-emptively
this.#_mariadb = new MariaDB(this, {
this.#mariadb = new MariaDB(this, {
...databases.mariadb,
credentials: {
host: MARIA_HOST,
@ -142,7 +142,7 @@ class Server extends EventEmitter
}
});
// Mongo is used for session and user storage
this.#_mongodb = new MongoDB(this, {
this.#mongodb = new MongoDB(this, {
...databases.mongodb,
credentials: {
URI: MONGO_URI,
@ -154,10 +154,10 @@ class Server extends EventEmitter
authDb: MONGO_AUTH_DB
}
});
this.#_userDatabase = new UserDatabase(this, this.#_mongodb);
this.#userDatabase = new UserDatabase(this, this.#mongodb);
if (databases.broker?.load)
this.#_messageBroker = new MessageBroker(this, {
this.#messageBroker = new MessageBroker(this, {
...databases.broker,
host: RABBIT_HOST,
user: RABBIT_USER,
@ -168,7 +168,7 @@ class Server extends EventEmitter
// Provider needs to implement getKey(key) and setKey(key, value)
// Distributed memory storage, using mongo in this case, but this could be redis or whatever
this.#_memoryStoreProvider = new MongoMemory(this, {
this.#memoryStoreProvider = new MongoMemory(this, {
...databases.mongodb,
credentials: {
URI: MONGO_MEMORY_URI,
@ -181,12 +181,12 @@ class Server extends EventEmitter
}
});
this.#_rateLimiter = new RateLimiter(this, this.#_memoryStoreProvider);
this.#_flagManager = new FlagManager(this);
this.#rateLimiter = new RateLimiter(this, this.#memoryStoreProvider);
this.#flagManager = new FlagManager(this);
// Authenticator takes care of sessions, logins and authorisations
this.#_authenticator = new Authenticator(this, this.#_userDatabase, {
sessionStorage: MongoStore.create({ client: this.#_mongodb.client, dbName: this.#_mongodb.database, touchAfter: 600 }),
this.#authenticator = new Authenticator(this, this.#userDatabase, {
sessionStorage: MongoStore.create({ client: this.#mongodb.client, dbName: this.#mongodb.database, touchAfter: 600 }),
secret: SECRET, // Secret for sessions
name: `${this.name}.s`,
cookie: {
@ -198,10 +198,10 @@ class Server extends EventEmitter
// Expecting every other env to be behind a proxy
if (NODE_ENV !== 'development')
this.#_app.set('trust proxy', 1);
this.#app.set('trust proxy', 1);
this.#_app.use(cors());
this.#_app.use(helmet({
this.#app.use(cors());
this.#app.use(helmet({
contentSecurityPolicy: {
useDefaults: true,
directives: {
@ -216,13 +216,13 @@ class Server extends EventEmitter
policy: 'cross-origin'
}
}));
this.#_app.use(express.json({ limit: '10mb' }));
this.#_app.use(express.urlencoded({ extended: true }));
this.#app.use(express.json({ limit: '10mb' }));
this.#app.use(express.urlencoded({ extended: true }));
// Not sure why binding is a problem
this.#_app.use(this.#ready.bind(this) as never); // denies requests before the server is ready
this.#_app.use(this.#attachMisc.bind(this) as never);
this.#_app.use(this.#logRequest.bind(this) as never); // Logs every request
this.#app.use(this.#ready.bind(this) as never); // denies requests before the server is ready
this.#app.use(this.#attachMisc.bind(this) as never);
this.#app.use(this.#logRequest.bind(this) as never); // Logs every request
process.on('message', this._handleMessage.bind(this));
process.on('SIGINT', this.shutdown.bind(this));
@ -236,29 +236,29 @@ class Server extends EventEmitter
this.#logger.status('Starting server');
this.#logger.info('Initialising MariaDB');
this.#_mariadb.init();
this.#mariadb.init();
this.#logger.info('Initialising MongoDB');
await this.#_mongodb.init();
await this.#mongodb.init();
this.#logger.info('Initialising memory storage provider');
await this.#_memoryStoreProvider.init();
await this.#memoryStoreProvider.init();
await this.#_userDatabase.init();
await this.#userDatabase.init();
await this.#_messageBroker?.init();
await this.#messageBroker?.init();
await this.#_flagManager.init();
await this.#flagManager.init();
this.#logger.info('Loading endpoints');
await this.#_registry.loadEndpoints();
this.#logger.debug(this.#_registry.print);
await this.#registry.loadEndpoints();
this.#logger.debug(this.#registry.print);
// Must come last, after loading endpoints
this.#_app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500
this.#app.use(this.#logError.bind(this) as never); // Logs endpoints that error and sends a 500
this.#logger.info('Creating http server');
this.#_server = http.createServer(this.#_options.http, this.app).listen(this.port);
this.#server = http.createServer(this.#options.http, this.app).listen(this.port);
this.#_ready = true;
this.#logger.debug('Registered permissions:');
@ -320,14 +320,14 @@ class Server extends EventEmitter
this.#_ready = false;
// Close http server first and allow it to finish serving requests
if (this.#_server)
if (this.#server)
{
this.#_server.close(async () =>
this.#server.close(async () =>
{
await this.#_mongodb.close();
await this.#_mariadb.close();
await this.#_memoryStoreProvider.close();
await this.#_messageBroker?.close();
await this.#mongodb.close();
await this.#mariadb.close();
await this.#memoryStoreProvider.close();
await this.#messageBroker?.close();
this.#logger.status('DB shutdowns complete.');
this.#logger.status('Server shutdown complete.');
@ -382,19 +382,19 @@ class Server extends EventEmitter
// Helper function to pass options to the logger in a unified way
createLogger (comp: object, options = {})
{
return new LoggerClient({ name: comp.constructor.name, ...this.#_options.logger, ...options });
return new LoggerClient({ name: comp.constructor.name, ...this.#options.logger, ...options });
}
get baseURL ()
{
return `${this.#_proto}://${this.#_domain}`;
return `${this.#proto}://${this.#domain}`;
}
#addAuthStrategies ()
{
const { DISCORD_ID, DISCORD_SECRET } = process.env;
const { callbackPath, discord } = this.#_options;
const { callbackPath, discord } = this.#options;
const authParams = {
clientID: DISCORD_ID as string,
clientSecret: DISCORD_SECRET as string,
@ -450,87 +450,87 @@ class Server extends EventEmitter
get shardId ()
{
return this.#_shardId;
return this.#shardId;
}
get port ()
{
return this.#_port;
return this.#port;
}
get mongodb ()
{
return this.#_mongodb;
return this.#mongodb;
}
get memoryStoreProvider ()
{
return this.#_memoryStoreProvider;
return this.#memoryStoreProvider;
}
get mariadb ()
{
return this.#_mariadb;
return this.#mariadb;
}
get messageBroker ()
{
return this.#_messageBroker;
return this.#messageBroker;
}
get users ()
{
return this.#_userDatabase;
return this.#userDatabase;
}
get rateLimiter ()
{
return this.#_rateLimiter;
return this.#rateLimiter;
}
get app ()
{
return this.#_app;
return this.#app;
}
get authenticator ()
{
return this.#_authenticator;
return this.#authenticator;
}
get auth ()
{
return this.#_authenticator;
return this.#authenticator;
}
get serveFiles ()
{
return this.#_serveFiles;
return this.#serveFiles;
}
get registrationEnabled ()
{
return this.#_registrationEnabled;
return this.#registrationEnabled;
}
get requireCodeForRegister ()
{
return this.#_requireCodeForRegister;
return this.#requireCodeForRegister;
}
get OAuthProviders ()
{
return this.#_OAuthProviders;
return this.#OAuthProviders;
}
get name ()
{
return this.#_name;
return this.#name;
}
get flagManager ()
{
return this.#_flagManager;
return this.#flagManager;
}
}