server.d.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /// <reference types="node" />
  2. import { EventEmitter } from "events";
  3. import type { IncomingMessage, Server as HttpServer, ServerResponse } from "http";
  4. import type { CookieSerializeOptions } from "cookie";
  5. import type { CorsOptions, CorsOptionsDelegate } from "cors";
  6. import type { Duplex } from "stream";
  7. declare type Transport = "polling" | "websocket";
  8. export interface AttachOptions {
  9. /**
  10. * name of the path to capture
  11. * @default "/engine.io"
  12. */
  13. path?: string;
  14. /**
  15. * destroy unhandled upgrade requests
  16. * @default true
  17. */
  18. destroyUpgrade?: boolean;
  19. /**
  20. * milliseconds after which unhandled requests are ended
  21. * @default 1000
  22. */
  23. destroyUpgradeTimeout?: number;
  24. /**
  25. * Whether we should add a trailing slash to the request path.
  26. * @default true
  27. */
  28. addTrailingSlash?: boolean;
  29. }
  30. export interface ServerOptions {
  31. /**
  32. * how many ms without a pong packet to consider the connection closed
  33. * @default 20000
  34. */
  35. pingTimeout?: number;
  36. /**
  37. * how many ms before sending a new ping packet
  38. * @default 25000
  39. */
  40. pingInterval?: number;
  41. /**
  42. * how many ms before an uncompleted transport upgrade is cancelled
  43. * @default 10000
  44. */
  45. upgradeTimeout?: number;
  46. /**
  47. * how many bytes or characters a message can be, before closing the session (to avoid DoS).
  48. * @default 1e5 (100 KB)
  49. */
  50. maxHttpBufferSize?: number;
  51. /**
  52. * A function that receives a given handshake or upgrade request as its first parameter,
  53. * and can decide whether to continue or not. The second argument is a function that needs
  54. * to be called with the decided information: fn(err, success), where success is a boolean
  55. * value where false means that the request is rejected, and err is an error code.
  56. */
  57. allowRequest?: (req: IncomingMessage, fn: (err: string | null | undefined, success: boolean) => void) => void;
  58. /**
  59. * the low-level transports that are enabled
  60. * @default ["polling", "websocket"]
  61. */
  62. transports?: Transport[];
  63. /**
  64. * whether to allow transport upgrades
  65. * @default true
  66. */
  67. allowUpgrades?: boolean;
  68. /**
  69. * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
  70. * @default false
  71. */
  72. perMessageDeflate?: boolean | object;
  73. /**
  74. * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
  75. * @default true
  76. */
  77. httpCompression?: boolean | object;
  78. /**
  79. * what WebSocket server implementation to use. Specified module must
  80. * conform to the ws interface (see ws module api docs).
  81. * An alternative c++ addon is also available by installing eiows module.
  82. *
  83. * @default `require("ws").Server`
  84. */
  85. wsEngine?: any;
  86. /**
  87. * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
  88. */
  89. initialPacket?: any;
  90. /**
  91. * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
  92. * might be used for sticky-session. Defaults to not sending any cookie.
  93. * @default false
  94. */
  95. cookie?: (CookieSerializeOptions & {
  96. name: string;
  97. }) | boolean;
  98. /**
  99. * the options that will be forwarded to the cors module
  100. */
  101. cors?: CorsOptions | CorsOptionsDelegate;
  102. /**
  103. * whether to enable compatibility with Socket.IO v2 clients
  104. * @default false
  105. */
  106. allowEIO3?: boolean;
  107. }
  108. /**
  109. * An Express-compatible middleware.
  110. *
  111. * Middleware functions are functions that have access to the request object (req), the response object (res), and the
  112. * next middleware function in the application’s request-response cycle.
  113. *
  114. * @see https://expressjs.com/en/guide/using-middleware.html
  115. */
  116. declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;
  117. export declare abstract class BaseServer extends EventEmitter {
  118. opts: ServerOptions;
  119. protected clients: any;
  120. clientsCount: number;
  121. protected middlewares: Middleware[];
  122. /**
  123. * Server constructor.
  124. *
  125. * @param {Object} opts - options
  126. * @api public
  127. */
  128. constructor(opts?: ServerOptions);
  129. protected abstract init(): any;
  130. /**
  131. * Compute the pathname of the requests that are handled by the server
  132. * @param options
  133. * @protected
  134. */
  135. protected _computePath(options: AttachOptions): string;
  136. /**
  137. * Returns a list of available transports for upgrade given a certain transport.
  138. *
  139. * @return {Array}
  140. * @api public
  141. */
  142. upgrades(transport: any): any;
  143. /**
  144. * Verifies a request.
  145. *
  146. * @param {http.IncomingMessage}
  147. * @return {Boolean} whether the request is valid
  148. * @api private
  149. */
  150. protected verify(req: any, upgrade: any, fn: any): any;
  151. /**
  152. * Adds a new middleware.
  153. *
  154. * @example
  155. * import helmet from "helmet";
  156. *
  157. * engine.use(helmet());
  158. *
  159. * @param fn
  160. */
  161. use(fn: any): void;
  162. /**
  163. * Apply the middlewares to the request.
  164. *
  165. * @param req
  166. * @param res
  167. * @param callback
  168. * @protected
  169. */
  170. protected _applyMiddlewares(req: IncomingMessage, res: ServerResponse, callback: (err?: any) => void): void;
  171. /**
  172. * Closes all clients.
  173. *
  174. * @api public
  175. */
  176. close(): this;
  177. protected abstract cleanup(): any;
  178. /**
  179. * generate a socket id.
  180. * Overwrite this method to generate your custom socket id
  181. *
  182. * @param {Object} request object
  183. * @api public
  184. */
  185. generateId(req: any): any;
  186. /**
  187. * Handshakes a new client.
  188. *
  189. * @param {String} transport name
  190. * @param {Object} request object
  191. * @param {Function} closeConnection
  192. *
  193. * @api protected
  194. */
  195. protected handshake(transportName: any, req: any, closeConnection: any): Promise<any>;
  196. protected abstract createTransport(transportName: any, req: any): any;
  197. /**
  198. * Protocol errors mappings.
  199. */
  200. static errors: {
  201. UNKNOWN_TRANSPORT: number;
  202. UNKNOWN_SID: number;
  203. BAD_HANDSHAKE_METHOD: number;
  204. BAD_REQUEST: number;
  205. FORBIDDEN: number;
  206. UNSUPPORTED_PROTOCOL_VERSION: number;
  207. };
  208. static errorMessages: {
  209. 0: string;
  210. 1: string;
  211. 2: string;
  212. 3: string;
  213. 4: string;
  214. 5: string;
  215. };
  216. }
  217. export declare class Server extends BaseServer {
  218. httpServer?: HttpServer;
  219. private ws;
  220. /**
  221. * Initialize websocket server
  222. *
  223. * @api protected
  224. */
  225. protected init(): void;
  226. protected cleanup(): void;
  227. /**
  228. * Prepares a request by processing the query string.
  229. *
  230. * @api private
  231. */
  232. private prepare;
  233. protected createTransport(transportName: any, req: any): any;
  234. /**
  235. * Handles an Engine.IO HTTP request.
  236. *
  237. * @param {IncomingMessage} req
  238. * @param {ServerResponse} res
  239. * @api public
  240. */
  241. handleRequest(req: IncomingMessage, res: ServerResponse): void;
  242. /**
  243. * Handles an Engine.IO HTTP Upgrade.
  244. *
  245. * @api public
  246. */
  247. handleUpgrade(req: IncomingMessage, socket: Duplex, upgradeHead: Buffer): void;
  248. /**
  249. * Called upon a ws.io connection.
  250. *
  251. * @param {ws.Socket} websocket
  252. * @api private
  253. */
  254. private onWebSocket;
  255. /**
  256. * Captures upgrade requests for a http.Server.
  257. *
  258. * @param {http.Server} server
  259. * @param {Object} options
  260. * @api public
  261. */
  262. attach(server: HttpServer, options?: AttachOptions): void;
  263. }
  264. export {};