server.d.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. WebTransport is disabled by default and must be manually enabled:
  60. *
  61. * @example
  62. * new Server({
  63. * transports: ["polling", "websocket", "webtransport"]
  64. * });
  65. *
  66. * @default ["polling", "websocket"]
  67. */
  68. transports?: Transport[];
  69. /**
  70. * whether to allow transport upgrades
  71. * @default true
  72. */
  73. allowUpgrades?: boolean;
  74. /**
  75. * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
  76. * @default false
  77. */
  78. perMessageDeflate?: boolean | object;
  79. /**
  80. * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
  81. * @default true
  82. */
  83. httpCompression?: boolean | object;
  84. /**
  85. * what WebSocket server implementation to use. Specified module must
  86. * conform to the ws interface (see ws module api docs).
  87. * An alternative c++ addon is also available by installing eiows module.
  88. *
  89. * @default `require("ws").Server`
  90. */
  91. wsEngine?: any;
  92. /**
  93. * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
  94. */
  95. initialPacket?: any;
  96. /**
  97. * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
  98. * might be used for sticky-session. Defaults to not sending any cookie.
  99. * @default false
  100. */
  101. cookie?: (CookieSerializeOptions & {
  102. name: string;
  103. }) | boolean;
  104. /**
  105. * the options that will be forwarded to the cors module
  106. */
  107. cors?: CorsOptions | CorsOptionsDelegate;
  108. /**
  109. * whether to enable compatibility with Socket.IO v2 clients
  110. * @default false
  111. */
  112. allowEIO3?: boolean;
  113. }
  114. /**
  115. * An Express-compatible middleware.
  116. *
  117. * Middleware functions are functions that have access to the request object (req), the response object (res), and the
  118. * next middleware function in the application’s request-response cycle.
  119. *
  120. * @see https://expressjs.com/en/guide/using-middleware.html
  121. */
  122. declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;
  123. export declare abstract class BaseServer extends EventEmitter {
  124. opts: ServerOptions;
  125. protected clients: any;
  126. clientsCount: number;
  127. protected middlewares: Middleware[];
  128. /**
  129. * Server constructor.
  130. *
  131. * @param {Object} opts - options
  132. * @api public
  133. */
  134. constructor(opts?: ServerOptions);
  135. protected abstract init(): any;
  136. /**
  137. * Compute the pathname of the requests that are handled by the server
  138. * @param options
  139. * @protected
  140. */
  141. protected _computePath(options: AttachOptions): string;
  142. /**
  143. * Returns a list of available transports for upgrade given a certain transport.
  144. *
  145. * @return {Array}
  146. * @api public
  147. */
  148. upgrades(transport: any): any;
  149. /**
  150. * Verifies a request.
  151. *
  152. * @param {http.IncomingMessage}
  153. * @return {Boolean} whether the request is valid
  154. * @api private
  155. */
  156. protected verify(req: any, upgrade: any, fn: any): any;
  157. /**
  158. * Adds a new middleware.
  159. *
  160. * @example
  161. * import helmet from "helmet";
  162. *
  163. * engine.use(helmet());
  164. *
  165. * @param fn
  166. */
  167. use(fn: any): void;
  168. /**
  169. * Apply the middlewares to the request.
  170. *
  171. * @param req
  172. * @param res
  173. * @param callback
  174. * @protected
  175. */
  176. protected _applyMiddlewares(req: IncomingMessage, res: ServerResponse, callback: (err?: any) => void): void;
  177. /**
  178. * Closes all clients.
  179. *
  180. * @api public
  181. */
  182. close(): this;
  183. protected abstract cleanup(): any;
  184. /**
  185. * generate a socket id.
  186. * Overwrite this method to generate your custom socket id
  187. *
  188. * @param {Object} request object
  189. * @api public
  190. */
  191. generateId(req: any): any;
  192. /**
  193. * Handshakes a new client.
  194. *
  195. * @param {String} transport name
  196. * @param {Object} request object
  197. * @param {Function} closeConnection
  198. *
  199. * @api protected
  200. */
  201. protected handshake(transportName: any, req: any, closeConnection: any): Promise<any>;
  202. onWebTransportSession(session: any): Promise<any>;
  203. protected abstract createTransport(transportName: any, req: any): any;
  204. /**
  205. * Protocol errors mappings.
  206. */
  207. static errors: {
  208. UNKNOWN_TRANSPORT: number;
  209. UNKNOWN_SID: number;
  210. BAD_HANDSHAKE_METHOD: number;
  211. BAD_REQUEST: number;
  212. FORBIDDEN: number;
  213. UNSUPPORTED_PROTOCOL_VERSION: number;
  214. };
  215. static errorMessages: {
  216. 0: string;
  217. 1: string;
  218. 2: string;
  219. 3: string;
  220. 4: string;
  221. 5: string;
  222. };
  223. }
  224. export declare class Server extends BaseServer {
  225. httpServer?: HttpServer;
  226. private ws;
  227. /**
  228. * Initialize websocket server
  229. *
  230. * @api protected
  231. */
  232. protected init(): void;
  233. protected cleanup(): void;
  234. /**
  235. * Prepares a request by processing the query string.
  236. *
  237. * @api private
  238. */
  239. private prepare;
  240. protected createTransport(transportName: any, req: any): any;
  241. /**
  242. * Handles an Engine.IO HTTP request.
  243. *
  244. * @param {IncomingMessage} req
  245. * @param {ServerResponse} res
  246. * @api public
  247. */
  248. handleRequest(req: IncomingMessage, res: ServerResponse): void;
  249. /**
  250. * Handles an Engine.IO HTTP Upgrade.
  251. *
  252. * @api public
  253. */
  254. handleUpgrade(req: IncomingMessage, socket: Duplex, upgradeHead: Buffer): void;
  255. /**
  256. * Called upon a ws.io connection.
  257. *
  258. * @param {ws.Socket} websocket
  259. * @api private
  260. */
  261. private onWebSocket;
  262. /**
  263. * Captures upgrade requests for a http.Server.
  264. *
  265. * @param {http.Server} server
  266. * @param {Object} options
  267. * @api public
  268. */
  269. attach(server: HttpServer, options?: AttachOptions): void;
  270. }
  271. export {};