import { AttachOptions, ServerOptions, Socket } from "engine.io"; import { EventEmitter } from "events"; import http from "http"; import { Map, Set } from "immutable"; import { NydusInvokeMessage, protocolVersion } from "nydus-protocol"; //#region composer.d.ts type NextFunc = (data: Map) => Promise; type ComposableFunc = (data: Map, next: NextFunc) => Promise; //#endregion //#region index.d.ts declare const PACKAGE_ONLY: unique symbol; type NydusClientEvents = { /** Fired when the client has disconnected. */ close: [reason: string, description?: Error]; /** Fired when a general error occurs. */ error: [err: Error]; }; /** A client that is connected to the server. */ export declare class NydusClient extends EventEmitter { readonly id: string; readonly conn: Socket; private onInvoke; private onClose; private onParserError; subscriptions: Set; constructor(id: string, conn: Socket, onInvoke: (client: NydusClient, message: NydusInvokeMessage) => void, onClose: (client: NydusClient) => void, onParserError: (client: NydusClient, msg: string) => void); /** Returns the current `readyState` of the connection. */ get readyState(): "opening" | "open" | "closing" | "closed"; equals(other: unknown): boolean; hashCode(): number; /** Closes the underlying connection. */ close(): void; send(packageOnlyKey: typeof PACKAGE_ONLY, encoded: string): void; private onMessage; private handleClose; } export declare class InvokeError extends Error { readonly status: number; readonly body: any; constructor(message: string, status: number, body?: any); } export interface NydusServerOptions extends ServerOptions, AttachOptions { /** * A function to convert errors to a sanitized version before sending them to clients. Optional, * the default implementation passes a status code, message, and body if present. In development * mode, it will include the stack trace as the body if there isn't a body already. */ invokeErrorConverter: (err: Error, client: NydusClient) => unknown; } export type RouteHandler = ComposableFunc; type NydusServerEvents = { /** Fired when a new client has connected. */ connection: [client: NydusClient]; /** Fired when a general error occurs. */ error: [err: Error]; /** Fired when a client receives a message it could not parse. */ parserError: [client: NydusClient, msg: string]; /** * Fired when an invoke throws an error that gets converted to a 500 (e.g. an internal server * error). */ invokeError: [err: Error, client: NydusClient, msg: NydusInvokeMessage]; }; export declare class NydusServer extends EventEmitter { static readonly protocolVersion = 3; /** A map of client ID -> client. */ clients: Map; private eioServer; private invokeErrorConverter; private idGen; private subscriptions; private router; private onInvokeFunc; private onCloseFunc; private onParserErrorFunc; constructor(options?: Partial); /** * Attach this NydusServer to a particular HTTP(S) server, making it listen to UPGRADE requests. */ attach(httpServer: http.Server, options: AttachOptions): void; /** Close any open connections and then stop this Nydus server. */ close(): void; /** * Set the function used to generate IDs for messages. Should return a string of 32 characters or * less, matching /[A-z0-9-]+/. * * This is mainly useful for testing, generally this shouldn't need to be used. */ setIdGen(idGenFunc: () => string): void; /** * Registers one or more handlers to respond to INVOKEs on paths matching a pattern. Handlers are * async functions (and thus return promises when called) of the form: * `(data, next) => Promise` * * Handlers will be composed in order, and are expected to call next(data, next) to make execution * continue further down the chain. Data is an immutable map that can be changed before passing it * to the next function, but should present the same API (or ideally, be an ImmutableJS map) for * compatibility with other handlers. * * The final resolved value will be sent to the client (as a RESULT if the promise was * successfully resolved, an ERROR if it was rejected). */ registerRoute(pathPattern: string, ...handlers: RouteHandler[]): void; /** * Add a subscription to a publish path for a client. Whenever messages are published to that * path, this client will receive a message (until unsubscribed). If initialData is specified and * the client was not previously subscribed, this data will be published to the client * immediately (but not to the other subscribed clients). * * @param client the NydusClient to subscribe to the specificed path * @param path the path to be registered * @param initialData optional data to send immediately to this client (and only this client). * This can also be a Promise, in which case the resolved data will be sent to the client. If * the client is unsubscribed before the Promise resolves, no data will be sent. */ subscribeClient(client: NydusClient, path: string, initialData?: T | Promise): void; /** Remove a client's subsription to a path (if it was subscribed). */ unsubscribeClient(client: NydusClient, path: string): boolean; /** * Unsubscribe all the clients currently subscribed to a path. */ unsubscribeAll(path: string): boolean; /** * Publish a message to all clients subscribed to `path`. */ publish(path: string, data?: T): void; private onConnection; private onDisconnect; private onInvoke; private onParserError; } export default function createNydusServer(httpServer: http.Server, opts?: Partial): NydusServer; //#endregion export { type NextFunc, protocolVersion }; //# sourceMappingURL=index.d.mts.map