server.d.ts 6.3 KB

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