socket.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import { Emitter } from "@socket.io/component-emitter";
  2. import type { Packet, BinaryType, RawData } from "engine.io-parser";
  3. import { CloseDetails, Transport } from "./transport.js";
  4. export interface SocketOptions {
  5. /**
  6. * The host that we're connecting to. Set from the URI passed when connecting
  7. */
  8. host: string;
  9. /**
  10. * The hostname for our connection. Set from the URI passed when connecting
  11. */
  12. hostname: string;
  13. /**
  14. * If this is a secure connection. Set from the URI passed when connecting
  15. */
  16. secure: boolean;
  17. /**
  18. * The port for our connection. Set from the URI passed when connecting
  19. */
  20. port: string | number;
  21. /**
  22. * Any query parameters in our uri. Set from the URI passed when connecting
  23. */
  24. query: {
  25. [key: string]: any;
  26. };
  27. /**
  28. * `http.Agent` to use, defaults to `false` (NodeJS only)
  29. *
  30. * Note: the type should be "undefined | http.Agent | https.Agent | false", but this would break browser-only clients.
  31. *
  32. * @see https://nodejs.org/api/http.html#httprequestoptions-callback
  33. */
  34. agent: string | boolean;
  35. /**
  36. * Whether the client should try to upgrade the transport from
  37. * long-polling to something better.
  38. * @default true
  39. */
  40. upgrade: boolean;
  41. /**
  42. * Forces base 64 encoding for polling transport even when XHR2
  43. * responseType is available and WebSocket even if the used standard
  44. * supports binary.
  45. */
  46. forceBase64: boolean;
  47. /**
  48. * The param name to use as our timestamp key
  49. * @default 't'
  50. */
  51. timestampParam: string;
  52. /**
  53. * Whether to add the timestamp with each transport request. Note: this
  54. * is ignored if the browser is IE or Android, in which case requests
  55. * are always stamped
  56. * @default false
  57. */
  58. timestampRequests: boolean;
  59. /**
  60. * A list of transports to try (in order). Engine.io always attempts to
  61. * connect directly with the first one, provided the feature detection test
  62. * for it passes.
  63. *
  64. * @default ['polling','websocket', 'webtransport']
  65. */
  66. transports: string[];
  67. /**
  68. * If true and if the previous websocket connection to the server succeeded,
  69. * the connection attempt will bypass the normal upgrade process and will
  70. * initially try websocket. A connection attempt following a transport error
  71. * will use the normal upgrade process. It is recommended you turn this on
  72. * only when using SSL/TLS connections, or if you know that your network does
  73. * not block websockets.
  74. * @default false
  75. */
  76. rememberUpgrade: boolean;
  77. /**
  78. * Are we only interested in transports that support binary?
  79. */
  80. onlyBinaryUpgrades: boolean;
  81. /**
  82. * Timeout for xhr-polling requests in milliseconds (0) (only for polling transport)
  83. */
  84. requestTimeout: number;
  85. /**
  86. * Transport options for Node.js client (headers etc)
  87. */
  88. transportOptions: Object;
  89. /**
  90. * (SSL) Certificate, Private key and CA certificates to use for SSL.
  91. * Can be used in Node.js client environment to manually specify
  92. * certificate information.
  93. */
  94. pfx: string;
  95. /**
  96. * (SSL) Private key to use for SSL. Can be used in Node.js client
  97. * environment to manually specify certificate information.
  98. */
  99. key: string;
  100. /**
  101. * (SSL) A string or passphrase for the private key or pfx. Can be
  102. * used in Node.js client environment to manually specify certificate
  103. * information.
  104. */
  105. passphrase: string;
  106. /**
  107. * (SSL) Public x509 certificate to use. Can be used in Node.js client
  108. * environment to manually specify certificate information.
  109. */
  110. cert: string;
  111. /**
  112. * (SSL) An authority certificate or array of authority certificates to
  113. * check the remote host against.. Can be used in Node.js client
  114. * environment to manually specify certificate information.
  115. */
  116. ca: string | string[];
  117. /**
  118. * (SSL) A string describing the ciphers to use or exclude. Consult the
  119. * [cipher format list]
  120. * (http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT) for
  121. * details on the format.. Can be used in Node.js client environment to
  122. * manually specify certificate information.
  123. */
  124. ciphers: string;
  125. /**
  126. * (SSL) If true, the server certificate is verified against the list of
  127. * supplied CAs. An 'error' event is emitted if verification fails.
  128. * Verification happens at the connection level, before the HTTP request
  129. * is sent. Can be used in Node.js client environment to manually specify
  130. * certificate information.
  131. */
  132. rejectUnauthorized: boolean;
  133. /**
  134. * Headers that will be passed for each request to the server (via xhr-polling and via websockets).
  135. * These values then can be used during handshake or for special proxies.
  136. */
  137. extraHeaders?: {
  138. [header: string]: string;
  139. };
  140. /**
  141. * Whether to include credentials (cookies, authorization headers, TLS
  142. * client certificates, etc.) with cross-origin XHR polling requests
  143. * @default false
  144. */
  145. withCredentials: boolean;
  146. /**
  147. * Whether to automatically close the connection whenever the beforeunload event is received.
  148. * @default false
  149. */
  150. closeOnBeforeunload: boolean;
  151. /**
  152. * Whether to always use the native timeouts. This allows the client to
  153. * reconnect when the native timeout functions are overridden, such as when
  154. * mock clocks are installed.
  155. * @default false
  156. */
  157. useNativeTimers: boolean;
  158. /**
  159. * weather we should unref the reconnect timer when it is
  160. * create automatically
  161. * @default false
  162. */
  163. autoUnref: boolean;
  164. /**
  165. * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
  166. * @default false
  167. */
  168. perMessageDeflate: {
  169. threshold: number;
  170. };
  171. /**
  172. * The path to get our client file from, in the case of the server
  173. * serving it
  174. * @default '/engine.io'
  175. */
  176. path: string;
  177. /**
  178. * Whether we should add a trailing slash to the request path.
  179. * @default true
  180. */
  181. addTrailingSlash: boolean;
  182. /**
  183. * Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols,
  184. * so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to
  185. * be able to handle different types of interactions depending on the specified protocol)
  186. * @default []
  187. */
  188. protocols: string | string[];
  189. }
  190. interface HandshakeData {
  191. sid: string;
  192. upgrades: string[];
  193. pingInterval: number;
  194. pingTimeout: number;
  195. maxPayload: number;
  196. }
  197. interface SocketReservedEvents {
  198. open: () => void;
  199. handshake: (data: HandshakeData) => void;
  200. packet: (packet: Packet) => void;
  201. packetCreate: (packet: Packet) => void;
  202. data: (data: any) => void;
  203. message: (data: any) => void;
  204. drain: () => void;
  205. flush: () => void;
  206. heartbeat: () => void;
  207. ping: () => void;
  208. pong: () => void;
  209. error: (err: string | Error) => void;
  210. upgrading: (transport: any) => void;
  211. upgrade: (transport: any) => void;
  212. upgradeError: (err: Error) => void;
  213. close: (reason: string, description?: CloseDetails | Error) => void;
  214. }
  215. type SocketState = "opening" | "open" | "closing" | "closed";
  216. export declare class Socket extends Emitter<Record<never, never>, Record<never, never>, SocketReservedEvents> {
  217. id: string;
  218. transport: Transport;
  219. binaryType: BinaryType;
  220. readyState: SocketState;
  221. writeBuffer: Packet[];
  222. private prevBufferLen;
  223. private upgrades;
  224. private pingInterval;
  225. private pingTimeout;
  226. private pingTimeoutTimer;
  227. private setTimeoutFn;
  228. private clearTimeoutFn;
  229. private readonly beforeunloadEventListener;
  230. private readonly offlineEventListener;
  231. private upgrading;
  232. private maxPayload?;
  233. private readonly opts;
  234. private readonly secure;
  235. private readonly hostname;
  236. private readonly port;
  237. private readonly transports;
  238. static priorWebsocketSuccess: boolean;
  239. static protocol: number;
  240. /**
  241. * Socket constructor.
  242. *
  243. * @param {String|Object} uri - uri or options
  244. * @param {Object} opts - options
  245. */
  246. constructor(uri: any, opts?: Partial<SocketOptions>);
  247. /**
  248. * Creates transport of the given type.
  249. *
  250. * @param {String} name - transport name
  251. * @return {Transport}
  252. * @private
  253. */
  254. private createTransport;
  255. /**
  256. * Initializes transport to use and starts probe.
  257. *
  258. * @private
  259. */
  260. private open;
  261. /**
  262. * Sets the current transport. Disables the existing one (if any).
  263. *
  264. * @private
  265. */
  266. private setTransport;
  267. /**
  268. * Probes a transport.
  269. *
  270. * @param {String} name - transport name
  271. * @private
  272. */
  273. private probe;
  274. /**
  275. * Called when connection is deemed open.
  276. *
  277. * @private
  278. */
  279. private onOpen;
  280. /**
  281. * Handles a packet.
  282. *
  283. * @private
  284. */
  285. private onPacket;
  286. /**
  287. * Called upon handshake completion.
  288. *
  289. * @param {Object} data - handshake obj
  290. * @private
  291. */
  292. private onHandshake;
  293. /**
  294. * Sets and resets ping timeout timer based on server pings.
  295. *
  296. * @private
  297. */
  298. private resetPingTimeout;
  299. /**
  300. * Called on `drain` event
  301. *
  302. * @private
  303. */
  304. private onDrain;
  305. /**
  306. * Flush write buffers.
  307. *
  308. * @private
  309. */
  310. private flush;
  311. /**
  312. * Ensure the encoded size of the writeBuffer is below the maxPayload value sent by the server (only for HTTP
  313. * long-polling)
  314. *
  315. * @private
  316. */
  317. private getWritablePackets;
  318. /**
  319. * Sends a message.
  320. *
  321. * @param {String} msg - message.
  322. * @param {Object} options.
  323. * @param {Function} callback function.
  324. * @return {Socket} for chaining.
  325. */
  326. write(msg: RawData, options?: any, fn?: any): this;
  327. send(msg: RawData, options?: any, fn?: any): this;
  328. /**
  329. * Sends a packet.
  330. *
  331. * @param {String} type: packet type.
  332. * @param {String} data.
  333. * @param {Object} options.
  334. * @param {Function} fn - callback function.
  335. * @private
  336. */
  337. private sendPacket;
  338. /**
  339. * Closes the connection.
  340. */
  341. close(): this;
  342. /**
  343. * Called upon transport error
  344. *
  345. * @private
  346. */
  347. private onError;
  348. /**
  349. * Called upon transport close.
  350. *
  351. * @private
  352. */
  353. private onClose;
  354. /**
  355. * Filters upgrades, returning only those matching client transports.
  356. *
  357. * @param {Array} upgrades - server upgrades
  358. * @private
  359. */
  360. private filterUpgrades;
  361. }
  362. export {};