transport.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { EventEmitter } from "events";
  2. import type { IncomingMessage, ServerResponse } from "http";
  3. import { Packet, RawData } from "engine.io-parser";
  4. type ReadyState = "open" | "closing" | "closed";
  5. export type EngineRequest = IncomingMessage & {
  6. _query: Record<string, string>;
  7. res?: ServerResponse;
  8. cleanup?: Function;
  9. websocket?: any;
  10. };
  11. export declare abstract class Transport extends EventEmitter {
  12. /**
  13. * The session ID.
  14. */
  15. sid: string;
  16. /**
  17. * Whether the transport is currently ready to send packets.
  18. */
  19. writable: boolean;
  20. /**
  21. * The revision of the protocol:
  22. *
  23. * - 3 is used in Engine.IO v3 / Socket.IO v2
  24. * - 4 is used in Engine.IO v4 and above / Socket.IO v3 and above
  25. *
  26. * It is found in the `EIO` query parameters of the HTTP requests.
  27. *
  28. * @see https://github.com/socketio/engine.io-protocol
  29. */
  30. protocol: number;
  31. /**
  32. * The current state of the transport.
  33. * @protected
  34. */
  35. protected _readyState: ReadyState;
  36. /**
  37. * Whether the transport is discarded and can be safely closed (used during upgrade).
  38. * @protected
  39. */
  40. protected discarded: boolean;
  41. /**
  42. * The parser to use (depends on the revision of the {@link Transport#protocol}.
  43. * @protected
  44. */
  45. protected parser: any;
  46. /**
  47. * Whether the transport supports binary payloads (else it will be base64-encoded)
  48. * @protected
  49. */
  50. protected supportsBinary: boolean;
  51. get readyState(): ReadyState;
  52. set readyState(state: ReadyState);
  53. /**
  54. * Transport constructor.
  55. *
  56. * @param {EngineRequest} req
  57. */
  58. constructor(req: {
  59. _query: Record<string, string>;
  60. });
  61. /**
  62. * Flags the transport as discarded.
  63. *
  64. * @package
  65. */
  66. discard(): void;
  67. /**
  68. * Called with an incoming HTTP request.
  69. *
  70. * @param req
  71. * @package
  72. */
  73. onRequest(req: any): void;
  74. /**
  75. * Closes the transport.
  76. *
  77. * @package
  78. */
  79. close(fn?: () => void): void;
  80. /**
  81. * Called with a transport error.
  82. *
  83. * @param {String} msg - message error
  84. * @param {Object} desc - error description
  85. * @protected
  86. */
  87. protected onError(msg: string, desc?: any): void;
  88. /**
  89. * Called with parsed out a packets from the data stream.
  90. *
  91. * @param {Object} packet
  92. * @protected
  93. */
  94. protected onPacket(packet: Packet): void;
  95. /**
  96. * Called with the encoded packet data.
  97. *
  98. * @param {String} data
  99. * @protected
  100. */
  101. protected onData(data: RawData): void;
  102. /**
  103. * Called upon transport close.
  104. *
  105. * @protected
  106. */
  107. protected onClose(): void;
  108. /**
  109. * The name of the transport.
  110. */
  111. abstract get name(): string;
  112. /**
  113. * Sends an array of packets.
  114. *
  115. * @param {Array} packets
  116. * @package
  117. */
  118. abstract send(packets: Packet[]): void;
  119. /**
  120. * Closes the transport.
  121. */
  122. abstract doClose(fn?: () => void): void;
  123. }
  124. export {};