socket.d.ts 10 KB

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