UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

116 lines (115 loc) 4.68 kB
import { EventEmitter } from "events"; import { ActionheroLogLevel } from "../modules/log"; import { Connection } from "./connection"; interface ServerConfig { [key: string]: any; } /** * Create a new Actionhero Server. The required properties of an server. These can be defined statically (this.name) or as methods which return a value. */ export declare abstract class Server extends EventEmitter { /**The name & type of the server. */ type: string; /**What connection verbs can connections of this type use? */ verbs?: Array<string>; /**Shorthand for `api.config[this.type]` */ config?: ServerConfig; options?: { [key: string]: any; }; /** attributes of the server */ attributes: { [key: string]: any; }; /**Can connections of this server use the chat system? */ canChat: boolean; /**Should we log every new connection? */ logConnections: boolean; /**Should we log when a connection disconnects/exits? */ logExits: boolean; /**Should every new connection of this server type receive the welcome message */ sendWelcomeMessage: boolean; /**Methods described by the server to apply to each connection (like connection.setHeader for web connections) */ connectionCustomMethods: { [key: string]: Function; }; /**A place to store the actually server object you create */ server?: any; constructor(); /** * Event called when a formal new connection is created for this server type. This is a response to calling Actionhero.Server#buildConnection * * @event Actionhero.Server#connection */ /** * Event called when a an action is complete for a connection created by this server. You may want to send a response to the client as a response to this event. * * @event Actionhero.Server#actionComplete * @property {object} data - The same data from the Action. Includes the connection, response, etc. */ /** * Method run as part of the `initialize` lifecycle of your server. Usually configures the server. */ abstract initialize(): Promise<void>; /** * Method run as part of the `start` lifecycle of your server. Usually boots the server (listens on port, etc). */ abstract start(): Promise<void>; /** * Method run as part of the `stop` lifecycle of your server. Usually configures the server (disconnects from port, etc). */ abstract stop(): Promise<void>; /** * Must be defined explaining how to send a message to an individual connection. */ abstract sendMessage(connection: Connection, message: string | object | Array<any>, messageId?: string): Promise<void>; /** * Must be defined explaining how to send a file to an individual connection. Might be a noop for some connection types. */ abstract sendFile(connection: Connection, error: NodeJS.ErrnoException, fileStream: any, mime: string, length: number, lastModified: Date): Promise<void>; /**An optional message to send to clients when they disconnect */ goodbye?(connection: Connection): Promise<void>; validate(): void; /** * * Build a the Actionhero.Connection from the raw parts provided by the server. * ```js *this.buildConnection({ * rawConnection: { * req: req, * res: res, * params: {}, * method: method, * cookies: cookies, * responseHeaders: responseHeaders, * responseHttpCode: responseHttpCode, * parsedURL: parsedURL * }, * id: fingerprint + '-' + uuid.v4(), * fingerprint: fingerprint, * remoteAddress: remoteIP, * remotePort: remotePort *}) * ``` */ buildConnection(data: { [key: string]: any; }): Promise<void>; /** * When a connection has called an Action command, and all properties are set. Connection should have `params.action` set at least. * on(event: 'actionComplete', cb: (data: object) => void): this; */ processAction(connection: Connection): Promise<void>; /** * When a connection has called an File command, and all properties are set. Connection should have `params.file` set at least. Will eventually call Actionhero.Server#sendFile. */ processFile(connection: Connection): Promise<void>; /** * Enumerate the connections for this server type on this server. */ connections(): Array<Connection>; /** * Log a message from this server type. A wrapper around log() with a server prefix. */ log(message: string, severity?: ActionheroLogLevel, data?: any): void; } export {};