index.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /// <reference types="node" />
  2. import http = require("http");
  3. import { EventEmitter } from "events";
  4. import { ExtendedError, Namespace } from "./namespace";
  5. import { Room, SocketId } from "socket.io-adapter";
  6. import { Encoder } from "socket.io-parser";
  7. import { Socket } from "./socket";
  8. import { CookieSerializeOptions } from "cookie";
  9. import { CorsOptions } from "cors";
  10. declare type Transport = "polling" | "websocket";
  11. interface EngineOptions {
  12. /**
  13. * how many ms without a pong packet to consider the connection closed
  14. * @default 5000
  15. */
  16. pingTimeout: number;
  17. /**
  18. * how many ms before sending a new ping packet
  19. * @default 25000
  20. */
  21. pingInterval: number;
  22. /**
  23. * how many ms before an uncompleted transport upgrade is cancelled
  24. * @default 10000
  25. */
  26. upgradeTimeout: number;
  27. /**
  28. * how many bytes or characters a message can be, before closing the session (to avoid DoS).
  29. * @default 1e5 (100 KB)
  30. */
  31. maxHttpBufferSize: number;
  32. /**
  33. * A function that receives a given handshake or upgrade request as its first parameter,
  34. * and can decide whether to continue or not. The second argument is a function that needs
  35. * to be called with the decided information: fn(err, success), where success is a boolean
  36. * value where false means that the request is rejected, and err is an error code.
  37. */
  38. allowRequest: (req: http.IncomingMessage, fn: (err: string | null | undefined, success: boolean) => void) => void;
  39. /**
  40. * the low-level transports that are enabled
  41. * @default ["polling", "websocket"]
  42. */
  43. transports: Transport[];
  44. /**
  45. * whether to allow transport upgrades
  46. * @default true
  47. */
  48. allowUpgrades: boolean;
  49. /**
  50. * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
  51. * @default false
  52. */
  53. perMessageDeflate: boolean | object;
  54. /**
  55. * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
  56. * @default true
  57. */
  58. httpCompression: boolean | object;
  59. /**
  60. * what WebSocket server implementation to use. Specified module must
  61. * conform to the ws interface (see ws module api docs). Default value is ws.
  62. * An alternative c++ addon is also available by installing uws module.
  63. */
  64. wsEngine: string;
  65. /**
  66. * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
  67. */
  68. initialPacket: any;
  69. /**
  70. * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
  71. * might be used for sticky-session. Defaults to not sending any cookie.
  72. * @default false
  73. */
  74. cookie: CookieSerializeOptions | boolean;
  75. /**
  76. * the options that will be forwarded to the cors module
  77. */
  78. cors: CorsOptions;
  79. }
  80. interface AttachOptions {
  81. /**
  82. * name of the path to capture
  83. * @default "/engine.io"
  84. */
  85. path: string;
  86. /**
  87. * destroy unhandled upgrade requests
  88. * @default true
  89. */
  90. destroyUpgrade: boolean;
  91. /**
  92. * milliseconds after which unhandled requests are ended
  93. * @default 1000
  94. */
  95. destroyUpgradeTimeout: number;
  96. }
  97. interface EngineAttachOptions extends EngineOptions, AttachOptions {
  98. }
  99. interface ServerOptions extends EngineAttachOptions {
  100. /**
  101. * name of the path to capture
  102. * @default "/socket.io"
  103. */
  104. path: string;
  105. /**
  106. * whether to serve the client files
  107. * @default true
  108. */
  109. serveClient: boolean;
  110. /**
  111. * the adapter to use
  112. * @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
  113. */
  114. adapter: any;
  115. /**
  116. * the parser to use
  117. * @default the default parser (https://github.com/socketio/socket.io-parser)
  118. */
  119. parser: any;
  120. /**
  121. * how many ms before a client without namespace is closed
  122. * @default 45000
  123. */
  124. connectTimeout: number;
  125. }
  126. export declare class Server extends EventEmitter {
  127. readonly sockets: Namespace;
  128. /** @private */
  129. readonly _parser: any;
  130. /** @private */
  131. readonly encoder: Encoder;
  132. /**
  133. * @private
  134. */
  135. _nsps: Map<string, Namespace>;
  136. private parentNsps;
  137. private _adapter;
  138. private _serveClient;
  139. private opts;
  140. private eio;
  141. private engine;
  142. private _path;
  143. private clientPathRegex;
  144. /**
  145. * @private
  146. */
  147. _connectTimeout: number;
  148. private httpServer;
  149. /**
  150. * Server constructor.
  151. *
  152. * @param {http.Server|Number|Object} srv http server, port or options
  153. * @param {Object} [opts]
  154. * @public
  155. */
  156. constructor(opts?: Partial<ServerOptions>);
  157. constructor(srv: http.Server, opts?: Partial<ServerOptions>);
  158. constructor(srv: number, opts?: Partial<ServerOptions>);
  159. /**
  160. * Sets/gets whether client code is being served.
  161. *
  162. * @param {Boolean} v - whether to serve client code
  163. * @return {Server|Boolean} self when setting or value when getting
  164. * @public
  165. */
  166. serveClient(v: boolean): Server;
  167. serveClient(): boolean;
  168. /**
  169. * Executes the middleware for an incoming namespace not already created on the server.
  170. *
  171. * @param {String} name - name of incoming namespace
  172. * @param {Object} auth - the auth parameters
  173. * @param {Function} fn - callback
  174. *
  175. * @private
  176. */
  177. _checkNamespace(name: string, auth: object, fn: (nsp: Namespace | boolean) => void): void;
  178. /**
  179. * Sets the client serving path.
  180. *
  181. * @param {String} v pathname
  182. * @return {Server|String} self when setting or value when getting
  183. * @public
  184. */
  185. path(v: string): Server;
  186. path(): string;
  187. /**
  188. * Set the delay after which a client without namespace is closed
  189. * @param v
  190. * @public
  191. */
  192. connectTimeout(v: number): Server;
  193. connectTimeout(): number;
  194. /**
  195. * Sets the adapter for rooms.
  196. *
  197. * @param {Adapter} v pathname
  198. * @return {Server|Adapter} self when setting or value when getting
  199. * @public
  200. */
  201. adapter(): any;
  202. adapter(v: any): any;
  203. /**
  204. * Attaches socket.io to a server or port.
  205. *
  206. * @param {http.Server|Number} srv - server or port
  207. * @param {Object} opts - options passed to engine.io
  208. * @return {Server} self
  209. * @public
  210. */
  211. listen(srv: http.Server, opts?: Partial<ServerOptions>): Server;
  212. listen(srv: number, opts?: Partial<ServerOptions>): Server;
  213. /**
  214. * Attaches socket.io to a server or port.
  215. *
  216. * @param {http.Server|Number} srv - server or port
  217. * @param {Object} opts - options passed to engine.io
  218. * @return {Server} self
  219. * @public
  220. */
  221. attach(srv: http.Server, opts?: Partial<ServerOptions>): Server;
  222. attach(port: number, opts?: Partial<ServerOptions>): Server;
  223. /**
  224. * Initialize engine
  225. *
  226. * @param srv - the server to attach to
  227. * @param opts - options passed to engine.io
  228. * @private
  229. */
  230. private initEngine;
  231. /**
  232. * Attaches the static file serving.
  233. *
  234. * @param {Function|http.Server} srv http server
  235. * @private
  236. */
  237. private attachServe;
  238. /**
  239. * Handles a request serving of client source and map
  240. *
  241. * @param {http.IncomingMessage} req
  242. * @param {http.ServerResponse} res
  243. * @private
  244. */
  245. private serve;
  246. /**
  247. * @param filename
  248. * @param req
  249. * @param res
  250. * @private
  251. */
  252. private static sendFile;
  253. /**
  254. * Binds socket.io to an engine.io instance.
  255. *
  256. * @param {engine.Server} engine engine.io (or compatible) server
  257. * @return {Server} self
  258. * @public
  259. */
  260. bind(engine: any): Server;
  261. /**
  262. * Called with each incoming transport connection.
  263. *
  264. * @param {engine.Socket} conn
  265. * @return {Server} self
  266. * @private
  267. */
  268. private onconnection;
  269. /**
  270. * Looks up a namespace.
  271. *
  272. * @param {String|RegExp|Function} name nsp name
  273. * @param {Function} [fn] optional, nsp `connection` ev handler
  274. * @public
  275. */
  276. of(name: string | RegExp | ((name: string, query: object, fn: (err: Error, success: boolean) => void) => void), fn?: (socket: Socket) => void): Namespace;
  277. /**
  278. * Closes server connection
  279. *
  280. * @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed
  281. * @public
  282. */
  283. close(fn?: (err?: Error) => void): void;
  284. /**
  285. * Sets up namespace middleware.
  286. *
  287. * @return {Server} self
  288. * @public
  289. */
  290. use(fn: (socket: Socket, next: (err?: ExtendedError) => void) => void): Server;
  291. /**
  292. * Targets a room when emitting.
  293. *
  294. * @param {String} name
  295. * @return {Server} self
  296. * @public
  297. */
  298. to(name: Room): Server;
  299. /**
  300. * Targets a room when emitting.
  301. *
  302. * @param {String} name
  303. * @return {Server} self
  304. * @public
  305. */
  306. in(name: Room): Server;
  307. /**
  308. * Sends a `message` event to all clients.
  309. *
  310. * @return {Server} self
  311. * @public
  312. */
  313. send(...args: any[]): Server;
  314. /**
  315. * Sends a `message` event to all clients.
  316. *
  317. * @return {Server} self
  318. * @public
  319. */
  320. write(...args: any[]): Server;
  321. /**
  322. * Gets a list of socket ids.
  323. *
  324. * @public
  325. */
  326. allSockets(): Promise<Set<SocketId>>;
  327. /**
  328. * Sets the compress flag.
  329. *
  330. * @param {Boolean} compress - if `true`, compresses the sending data
  331. * @return {Server} self
  332. * @public
  333. */
  334. compress(compress: boolean): Server;
  335. /**
  336. * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
  337. * receive messages (because of network slowness or other issues, or because they’re connected through long polling
  338. * and is in the middle of a request-response cycle).
  339. *
  340. * @return {Server} self
  341. * @public
  342. */
  343. get volatile(): Server;
  344. /**
  345. * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
  346. *
  347. * @return {Server} self
  348. * @public
  349. */
  350. get local(): Server;
  351. }
  352. export { Socket, ServerOptions, Namespace };