@nodecg/types
Version:
Dynamic broadcast graphics rendered in a browser
370 lines (369 loc) • 18.3 kB
TypeScript
import { n as RootNS, r as ServerToClientEvents, t as NodeCG } from "../nodecg.js";
import { d as EventKey, n as AbstractReplicant, r as ReplicantValue, u as Builtins } from "../replicants.shared.js";
import { r as LoggerInterface } from "../logger-interface.js";
import { t as MessageHandler } from "../api.base.js";
import { DeepReadonly } from "ts-essentials";
import { DatabaseAdapter, Replicant } from "@nodecg/database-adapter-types";
import express from "express";
import * as express_serve_static_core0 from "express-serve-static-core";
import * as events0 from "events";
import * as qs0 from "qs";
//#region src/server/config/index.d.ts
declare const config: {
baseURL: string;
host: string;
port: number;
exitOnUncaught: boolean;
logging: {
console: {
enabled: boolean;
level: "verbose" | "debug" | "info" | "warn" | "error" | "silent";
timestamps: boolean;
replicants: boolean;
};
file: {
enabled: boolean;
level: "verbose" | "debug" | "info" | "warn" | "error" | "silent";
timestamps: boolean;
replicants: boolean;
path: string;
};
};
bundles: {
enabled: string[] | null;
disabled: string[] | null;
paths: string[];
};
login: {
enabled: boolean;
forceHttpsReturn: boolean;
sessionSecret?: string | undefined;
steam?: {
enabled: boolean;
allowedIds: string[];
apiKey?: string | undefined;
} | undefined;
twitch?: {
enabled: boolean;
allowedIds: string[];
scope: string;
allowedUsernames: string[];
clientID?: string | undefined;
clientSecret?: string | undefined;
} | undefined;
discord?: {
enabled: boolean;
scope: string;
allowedUserIDs: string[];
allowedGuilds: {
guildID: string;
allowedRoleIDs: string[];
guildBotToken: string;
}[];
clientID?: string | undefined;
clientSecret?: string | undefined;
} | undefined;
local?: {
enabled: boolean;
allowedUsers: {
username: string;
password: string;
}[];
} | undefined;
};
ssl: {
enabled: boolean;
allowHTTP: boolean;
keyPath?: string | undefined;
certificatePath?: string | undefined;
passphrase?: string | undefined;
};
sentry: {
enabled: boolean;
dsn?: string | undefined;
};
}, filteredConfig: NodeCG.FilteredConfig;
//#endregion
//#region src/server/replicant/server-replicant.d.ts
/**
* Never instantiate this directly.
* Always use Replicator.declare instead.
* The Replicator needs to have complete control over the ServerReplicant class.
*/
declare class ServerReplicant<V, O extends NodeCG.Replicant.Options<V> = NodeCG.Replicant.Options<V>> extends AbstractReplicant<"server", V, O> {
constructor(name: string, namespace: string, opts?: O, startingValue?: V | undefined);
get value(): ReplicantValue<"server", V, O>;
set value(newValue: ReplicantValue<"server", V, O>);
/**
* Refer to the abstract base class' implementation for details.
* @private
*/
_addOperation(operation: NodeCG.Replicant.Operation<ReplicantValue<"server", V, O>>): void;
/**
* Refer to the abstract base class' implementation for details.
* @private
*/
_flushOperations(): void;
}
//#endregion
//#region src/server/replicant/replicator.d.ts
declare class Replicator {
readonly io: RootNS;
private readonly db;
readonly declaredReplicants: Map<string, Map<string, ServerReplicant<any, NodeCG.Replicant.Options<any>>>>;
private readonly _uuid;
private readonly _repEntities;
private readonly _pendingSave;
constructor(io: RootNS, db: DatabaseAdapter, repEntities: Replicant[]);
/**
* Declares a Replicant.
* @param {string} name - The name of the Replicant to declare.
* @param {string} namespace - The namespace to which this Replicant belongs.
* @param {object} [opts] - The options for this replicant.
* @param {*} [opts.defaultValue] - The default value to instantiate this Replicant with. The default value is only
* applied if this Replicant has not previously been declared and if it has no persisted value.
* @param {boolean} [opts.persistent=true] - Whether to persist the Replicant's value to disk on every change.
* Persisted values are re-loaded on startup.
* @param {string} [opts.schemaPath] - The filepath at which to look for a JSON Schema for this Replicant.
* Defaults to `nodecg/bundles/${bundleName}/schemas/${replicantName}.json`.
* @returns {object}
*/
declare<V, O extends NodeCG.Replicant.OptionsWithDefault<V> = NodeCG.Replicant.OptionsWithDefault<V>>(name: string, namespace: string, opts?: O): ServerReplicant<V, O>;
declare<V, O extends NodeCG.Replicant.OptionsNoDefault = NodeCG.Replicant.OptionsNoDefault>(name: string, namespace: string, opts?: O): ServerReplicant<V, O>;
/**
* Applies an array of operations to a replicant.
* @param replicant {object} - The Replicant to perform these operation on.
* @param operations {array} - An array of operations.
*/
applyOperations<V>(replicant: ServerReplicant<V, NodeCG.Replicant.Options<V>>, operations: NodeCG.Replicant.Operation<V>[]): void;
/**
* Emits an event to all remote Socket.IO listeners.
* @param namespace - The namespace in which to emit this event. Only applies to Socket.IO listeners.
* @param eventName - The name of the event to emit.
* @param data - The data to emit with the event.
*/
emitToClients<T extends keyof ServerToClientEvents>(replicant: ServerReplicant<any>, eventName: T, data: Parameters<ServerToClientEvents[T]>[0]): void;
saveAllReplicants(): void;
saveAllReplicantsNow(): Promise<void>;
saveReplicant(replicant: ServerReplicant<any>): void;
private _saveReplicant;
private _attachToSocket;
}
//#endregion
//#region src/server/server/extensions.d.ts
interface ExtensionEventMap {
login: (user: Express.Request["user"]) => void;
logout: (user: Express.Request["user"]) => void;
extensionsLoaded: () => void;
serverStarted: () => void;
serverStopping: () => void;
}
//#endregion
//#region src/server/api.server.d.ts
declare function serverApiFactory(io: RootNS, replicator: Replicator, extensions: Record<string, unknown>, mount: NodeCG.Middleware): {
new <C extends Record<string, any> = NodeCG.Bundle.UnknownConfig>(bundle: NodeCG.Bundle): {
readonly Logger: new (name: string) => LoggerInterface;
readonly log: LoggerInterface;
/**
* The full NodeCG server config, including potentially sensitive keys.
*/
readonly config: DeepReadonly<typeof config>;
/**
* _Extension only_<br/>
* Creates a new express router.
* See the [express docs](http://expressjs.com/en/api.html#express.router) for usage.
* @function
*/
readonly Router: typeof express.Router;
util: {
/**
* _Extension only_<br/>
* Checks if a session is authorized. Intended to be used in express routes.
* @param {object} req - A HTTP request.
* @param {object} res - A HTTP response.
* @param {function} next - The next middleware in the control flow.
*/
authCheck: express.RequestHandler<express_serve_static_core0.ParamsDictionary, any, any, qs0.ParsedQs, Record<string, any>>;
};
/**
* _Extension only_<br/>
* Object containing references to all other loaded extensions. To access another bundle's extension,
* it _must_ be declared as a `bundleDependency` in your bundle's [`package.json`]{@tutorial manifest}.
* @name NodeCG#extensions
*
* @example
* // bundles/my-bundle/package.json
* {
* "name": "my-bundle"
* ...
* "bundleDependencies": {
* "other-bundle": "^1.0.0"
* }
* }
*
* // bundles/my-bundle/extension.js
* module.exports = function (nodecg) {
* const otherBundle = nodecg.extensions['other-bundle'];
* // Now I can use `otherBundle`!
* }
*/
readonly extensions: Record<string, unknown>;
/**
* _Extension only_<br/>
* Mounts Express middleware to the main server Express app.
* Middleware mounted using this method comes _after_ all the middlware that NodeCG
* uses internally.
* See the [Express docs](http://expressjs.com/en/api.html#app.use) for usage.
* @function
*/
mount: NodeCG.Middleware;
/**
* _Extension only_<br/>
* Gets the server Socket.IO context.
* @function
*/
readonly getSocketIOServer: () => RootNS;
/**
* Sends a message to a specific bundle. Also available as a static method.
* See {@link NodeCG#sendMessage} for usage details.
* @param {string} messageName - The name of the message.
* @param {string} bundleName - The name of the target bundle.
* @param {mixed} [data] - The data to send.
* @param {function} [cb] - _Browser only_ The error-first callback to handle the server's
* [acknowledgement](http://socket.io/docs/#sending-and-getting-data-%28acknowledgements%29) message, if any.
* @return {Promise|undefined} - _Browser only_ A Promise that is rejected if the first argument provided to the
* acknowledgement is an `Error`, otherwise it is resolved with the remaining arguments provided to the acknowledgement.
* But, if a callback was provided, this return value will be `undefined`, and there will be no Promise.
*/
sendMessageToBundle(messageName: string, bundleName: string, data?: unknown): void;
/**
* Sends a message with optional data within the current bundle.
* Messages can be sent from client to server, server to client, or client to client.
*
* Messages are namespaced by bundle. To send a message in another bundle's namespace,
* use {@link NodeCG#sendMessageToBundle}.
*
* When a `sendMessage` is used from a client context (i.e., graphic or dashboard panel),
* it returns a `Promise` called an "acknowledgement". Your server-side code (i.e., extension)
* can invoke this acknowledgement with whatever data (or error) it wants. Errors sent to acknowledgements
* from the server will be properly serialized and intact when received on the client.
*
* Alternatively, if you do not wish to use a `Promise`, you can provide a standard error-first
* callback as the last argument to `sendMessage`.
*
* If your server-side code has multiple listenFor handlers for your message,
* you must first check if the acknowledgement has already been handled before
* attempting to call it. You may so do by checking the `.handled` boolean
* property of the `ack` function passed to your listenFor handler.
*
* See [Socket.IO's docs](http://socket.io/docs/#sending-and-getting-data-%28acknowledgements%29)
* for more information on how acknowledgements work under the hood.
*
* @param {string} messageName - The name of the message.
* @param {mixed} [data] - The data to send.
* @param {function} [cb] - _Browser only_ The error-first callback to handle the server's
* [acknowledgement](http://socket.io/docs/#sending-and-getting-data-%28acknowledgements%29) message, if any.
* @return {Promise} - _Browser only_ A Promise that is rejected if the first argument provided to the
* acknowledgement is an `Error`, otherwise it is resolved with the remaining arguments provided to the acknowledgement.
*
* @example <caption>Sending a normal message:</caption>
* nodecg.sendMessage('printMessage', 'dope.');
*
* @example <caption>Sending a message and replying with an acknowledgement:</caption>
* // bundles/my-bundle/extension.js
* module.exports = function (nodecg) {
* nodecg.listenFor('multiplyByTwo', (value, ack) => {
* if (value === 4) {
* ack(new Error('I don\'t like multiplying the number 4!');
* return;
* }
*
* // acknowledgements should always be error-first callbacks.
* // If you do not wish to send an error, send "null"
* if (ack && !ack.handled) {
* ack(null, value * 2);
* }
* });
* }
*
* // bundles/my-bundle/graphics/script.js
* // Both of these examples are functionally identical.
*
* // Promise acknowledgement
* nodecg.sendMessage('multiplyByTwo', 2)
* .then(result => {
* console.log(result); // Will eventually print '4'
* .catch(error => {
* console.error(error);
* });
*
* // Error-first callback acknowledgement
* nodecg.sendMessage('multiplyByTwo', 2, (error, result) => {
* if (error) {
* console.error(error);
* return;
* }
*
* console.log(result); // Will eventually print '4'
* });
*/
sendMessage(messageName: string, data?: unknown): void;
/**
* Reads the value of a replicant once, and doesn't create a subscription to it. Also available as a static method.
* @param {string} name - The name of the replicant.
* @param {string} [bundle=CURR_BNDL] - The bundle namespace to in which to look for this replicant.
* @param {function} cb - _Browser only_ The callback that handles the server's response which contains the value.
* @example <caption>From an extension:</caption>
* // Extensions have immediate access to the database of Replicants.
* // For this reason, they can use readReplicant synchronously, without a callback.
* module.exports = function (nodecg) {
* var myVal = nodecg.readReplicant('myVar', 'some-bundle');
* }
* @example <caption>From a graphic or dashboard panel:</caption>
* // Graphics and dashboard panels must query the server to retrieve the value,
* // and therefore must provide a callback.
* nodecg.readReplicant('myRep', 'some-bundle', value => {
* // I can use 'value' now!
* console.log('myRep has the value '+ value +'!');
* });
*/
readReplicant<T = unknown>(name: string, param2?: string | NodeCG.Bundle): T | undefined;
_replicantFactory: <V, O extends NodeCG.Replicant.Options<V> = NodeCG.Replicant.Options<V>>(name: string, namespace: string, opts: O) => ServerReplicant<V, O>;
readonly bundleName: string;
readonly bundleConfig: DeepReadonly<C>;
readonly bundleVersion?: string;
readonly bundleGit: Readonly<NodeCG.Bundle.GitData>;
_messageHandlers: MessageHandler[];
listenFor(messageName: string, handlerFunc: NodeCG.ListenHandler): void;
listenFor(messageName: string, bundleName: string, handlerFunc: NodeCG.ListenHandler): void;
unlisten(messageName: string, handlerFunc: NodeCG.ListenHandler): boolean;
unlisten(messageName: string, bundleName: string, handlerFunc: NodeCG.ListenHandler): boolean;
Replicant<V, O_1 extends NodeCG.Replicant.OptionsNoDefault = NodeCG.Replicant.OptionsNoDefault>(name: string, namespace: string, opts?: O_1 | undefined): AbstractReplicant<"server", V, O_1, false>;
Replicant<V, O_2 extends NodeCG.Replicant.OptionsNoDefault = NodeCG.Replicant.OptionsNoDefault>(name: string, opts?: O_2 | undefined): AbstractReplicant<"server", V, O_2, false>;
Replicant<V, O_3 extends NodeCG.Replicant.OptionsNoDefault = NodeCG.Replicant.OptionsNoDefault>(name: string, namespaceOrOpts?: string | O_3 | undefined, opts?: O_3 | undefined): AbstractReplicant<"server", V, O_3, false>;
Replicant<V, O_4 extends NodeCG.Replicant.OptionsWithDefault<V> = NodeCG.Replicant.OptionsWithDefault<V>>(name: string, namespace: string, opts?: O_4 | undefined): AbstractReplicant<"server", V, O_4, false>;
Replicant<V, O_5 extends NodeCG.Replicant.OptionsWithDefault<V> = NodeCG.Replicant.OptionsWithDefault<V>>(name: string, opts?: O_5 | undefined): AbstractReplicant<"server", V, O_5, false>;
Replicant<V, O_6 extends NodeCG.Replicant.OptionsWithDefault<V> = NodeCG.Replicant.OptionsWithDefault<V>>(name: string, namespaceOrOpts?: string | O_6 | undefined, opts?: O_6 | undefined): AbstractReplicant<"server", V, O_6, false>;
Replicant<V, O_7 extends NodeCG.Replicant.Options<V> = NodeCG.Replicant.Options<V>>(name: string, namespace: string, opts?: O_7 | undefined): AbstractReplicant<"server", V, O_7, false>;
Replicant<V, O_8 extends NodeCG.Replicant.Options<V> = NodeCG.Replicant.Options<V>>(name: string, opts?: O_8 | undefined): AbstractReplicant<"server", V, O_8, false>;
readonly _emitter: events0<[never]>;
addListener<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, fn: (ExtensionEventMap & Builtins)[K]): void;
on<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, fn: (ExtensionEventMap & Builtins)[K]): void;
off<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, fn: (ExtensionEventMap & Builtins)[K]): void;
removeListener<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, fn: (ExtensionEventMap & Builtins)[K]): void;
emit<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, ...params: Parameters<(ExtensionEventMap & Builtins)[K]>): void;
once<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K, fn: (ExtensionEventMap & Builtins)[K]): void;
setMaxListeners(max: number): void;
listenerCount(eventName: string): number;
listeners<K extends EventKey<ExtensionEventMap & Builtins>>(eventName: K): (ExtensionEventMap & Builtins)[K][];
};
sendMessageToBundle(messageName: string, bundleName: string, data?: unknown): void;
readReplicant<T = unknown>(name: string, namespace: string): T | undefined;
Replicant<V, O extends NodeCG.Replicant.Options<V> = NodeCG.Replicant.Options<V>>(name: string, namespace: string, opts: O): ServerReplicant<V, O>;
version: any;
declaredReplicants: Map<string, Map<string, AbstractReplicant<"client", any>>>;
waitForReplicants(...replicants: AbstractReplicant<"client", any>[]): Promise<void>;
};
//#endregion
export { serverApiFactory };
//# sourceMappingURL=api.server.d.ts.map