transport.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type { Packet, RawData } from "engine.io-parser";
  2. import { Emitter } from "@socket.io/component-emitter";
  3. import { SocketOptions } from "./socket.js";
  4. declare class TransportError extends Error {
  5. readonly description: any;
  6. readonly context: any;
  7. readonly type = "TransportError";
  8. constructor(reason: string, description: any, context: any);
  9. }
  10. export interface CloseDetails {
  11. description: string;
  12. context?: unknown;
  13. }
  14. interface TransportReservedEvents {
  15. open: () => void;
  16. error: (err: TransportError) => void;
  17. packet: (packet: Packet) => void;
  18. close: (details?: CloseDetails) => void;
  19. poll: () => void;
  20. pollComplete: () => void;
  21. drain: () => void;
  22. }
  23. type TransportState = "opening" | "open" | "closed" | "pausing" | "paused";
  24. export declare abstract class Transport extends Emitter<Record<never, never>, Record<never, never>, TransportReservedEvents> {
  25. query: Record<string, string>;
  26. writable: boolean;
  27. protected opts: SocketOptions;
  28. protected supportsBinary: boolean;
  29. protected readyState: TransportState;
  30. protected socket: any;
  31. protected setTimeoutFn: typeof setTimeout;
  32. /**
  33. * Transport abstract constructor.
  34. *
  35. * @param {Object} opts - options
  36. * @protected
  37. */
  38. constructor(opts: any);
  39. /**
  40. * Emits an error.
  41. *
  42. * @param {String} reason
  43. * @param description
  44. * @param context - the error context
  45. * @return {Transport} for chaining
  46. * @protected
  47. */
  48. protected onError(reason: string, description: any, context?: any): this;
  49. /**
  50. * Opens the transport.
  51. */
  52. open(): this;
  53. /**
  54. * Closes the transport.
  55. */
  56. close(): this;
  57. /**
  58. * Sends multiple packets.
  59. *
  60. * @param {Array} packets
  61. */
  62. send(packets: any): void;
  63. /**
  64. * Called upon open
  65. *
  66. * @protected
  67. */
  68. protected onOpen(): void;
  69. /**
  70. * Called with data.
  71. *
  72. * @param {String} data
  73. * @protected
  74. */
  75. protected onData(data: RawData): void;
  76. /**
  77. * Called with a decoded packet.
  78. *
  79. * @protected
  80. */
  81. protected onPacket(packet: Packet): void;
  82. /**
  83. * Called upon close.
  84. *
  85. * @protected
  86. */
  87. protected onClose(details?: CloseDetails): void;
  88. /**
  89. * The name of the transport
  90. */
  91. abstract get name(): string;
  92. /**
  93. * Pauses the transport, in order not to lose packets during an upgrade.
  94. *
  95. * @param onPause
  96. */
  97. pause(onPause: () => void): void;
  98. protected createUri(schema: string, query?: Record<string, unknown>): string;
  99. private _hostname;
  100. private _port;
  101. private _query;
  102. protected abstract doOpen(): any;
  103. protected abstract doClose(): any;
  104. protected abstract write(packets: Packet[]): any;
  105. }
  106. export {};