import { Logger } from "../logger"; import { Room, Worker } from "../worker"; import { PluginPackageJson } from "./PluginLoader"; /** * Metadata about a plugin, created with {@link HindenburgPlugin}. */ export interface PluginMetadata { /** * The ID of the plugin, beginning with `hbplugin-`. * * @example "hbplugin-my-plugin" */ id: string; /** * The version of the plugin. * @example "1.0.0" * @example "2.0.0-beta.1" */ version: string; /** * The order that the plugin should be loaded into, where: * First = -1 * None = 0 * Last = 1 * * Or you can provide a number to provide your own priority. * * @example "first" * @example 9999999999999 * @example -9999999999999 */ loadOrder: "first" | "none" | "last" | number; /** * The default configuration values for the plugin. * @example * ```json * { * "redis": { * "host": "127.0.0.1", * "port": "6379", * "password": "H1nd3nburgR0cks" * } * } * ``` */ defaultConfig: any; } export declare type PluginInstanceType = K extends { createInstance(...args: any[]): infer X; } ? X : never; /** * Represents a base plugin for Hindenburg. Should not be extended directly, * see {@link WorkerPlugin} and {@link RoomPlugin} to choose the scope of the * plugin. * * Needs to be decorated with {@link HindenburgPlugin} to actually be able to * be imported and loaded. */ export declare abstract class Plugin { /** * The config passed into this plugin, usually by the `config.json` on the * server. */ config: any; /** * The metadata for the plugin, as passed into {@link HindenburgPlugin}. */ static meta: PluginMetadata; /** * The base directory of the plugin. */ static baseDirectory: string; /** * The package.json of this plugin. */ static packageJson: PluginPackageJson; /** * The metadata for the plugin, as passed into {@link HindenburgPlugin}. */ meta: PluginMetadata; /** * A console logger for the plugin. */ logger: Logger; /** * The directory of the plugin. */ baseDirectory: string; /** * The package.json associated with this plugin. */ packageJson: PluginPackageJson; protected constructor( /** * The config passed into this plugin, usually by the `config.json` on the * server. */ config: any); /** * Asynchronous method that is called when the plugin is first loaded into * the worker or a room, useful for connecting to any servers or loading * large amounts of data before the plugin can actually be used, as the * server will wait for it to finish. * * @example * ```ts * .@HindenburgPlugin("hbplugin-my-plugin") * export class MyPlugin extends WorkerPlugin { * async onPluginLoad() { * const res = await fetch("https://icanhazip.com/"); * const ip = await res.text(); * * console.log("My ip is " + ip); * } * } * ``` */ onPluginLoad(): any; /** * Method that is called when the plugin is unloaded from the worker or * room, useful for destroying any connections to any servers, clearing up * extra event listeners to prevent memory leaks, or closing any server sockets. * * Not called when the server shuts down, and the server also does not wait * for it to finish. * * @example * ```ts * .@HindenburgPlugin("hbplugin-my-plugin") * export class MyPlugin extends WorkerPlugin { * async onPluginUnload() { * this.logger.info("Closing socket.."); * await this.socket.close(); * this.logger.info("Closed socket"); * } * } * ``` */ onPluginUnload(): any; /** * Method that is called when the plugin's config updates. You can use this * to verify configuration, or just to do something such as switch ports or * change authentication when the config changes. */ onConfigUpdate(oldConfig: any, newConfig: any): any; getDependency(pluginId: string): Plugin; getDependency(plugin: K): WorkerPlugin | RoomPlugin; } export declare class RoomPlugin extends Plugin { /** * The room that this plugin is loaded into. */ readonly room: Room; config: any; /** * The worker of the room that this plugin is loaded into. */ readonly worker: Worker; /** * Create a new instance of this plugin. * @param room The room that this plugin is for. * @param config Configuration for the plugin. */ static createInstance(room: Room, config: any): RoomPlugin; /** * @example * ```ts * .@HindenburgPlugin("hbplugin-my-plugin") * export class MyPlugin extends RoomPlugin { * * } * ``` */ protected constructor( /** * The room that this plugin is loaded into. */ room: Room, config: any); getDependency(pluginId: string): RoomPlugin; getDependency(plugin: K): PluginInstanceType; } export declare class WorkerPlugin extends Plugin { /** * The worker that this plugin is loaded into. */ readonly worker: Worker; config: any; /** * Create a new instance of this plugin. * @param worker The Hindenburg worker that this plugin is for. * @param config Configuration for the plugin. */ static createInstance(worker: Worker, config: any): WorkerPlugin; /** * @example * ```ts * .@HindenburgPlugin("hbplugin-my-plugin") * export class MyPlugin extends WorkerPlugin { * * } * ``` */ protected constructor( /** * The worker that this plugin is loaded into. */ worker: Worker, config: any); getDependency(pluginId: string): WorkerPlugin; getDependency(plugin: K): PluginInstanceType; } export declare type SomePluginCtr = typeof WorkerPlugin | typeof RoomPlugin;