index.d.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Emitter } from "@socket.io/component-emitter";
  2. /**
  3. * Protocol version.
  4. *
  5. * @public
  6. */
  7. export declare const protocol: number;
  8. export declare enum PacketType {
  9. CONNECT = 0,
  10. DISCONNECT = 1,
  11. EVENT = 2,
  12. ACK = 3,
  13. CONNECT_ERROR = 4,
  14. BINARY_EVENT = 5,
  15. BINARY_ACK = 6
  16. }
  17. export interface Packet {
  18. type: PacketType;
  19. nsp: string;
  20. data?: any;
  21. id?: number;
  22. attachments?: number;
  23. }
  24. /**
  25. * A socket.io Encoder instance
  26. */
  27. export declare class Encoder {
  28. private replacer?;
  29. /**
  30. * Encoder constructor
  31. *
  32. * @param {function} replacer - custom replacer to pass down to JSON.parse
  33. */
  34. constructor(replacer?: (this: any, key: string, value: any) => any);
  35. /**
  36. * Encode a packet as a single string if non-binary, or as a
  37. * buffer sequence, depending on packet type.
  38. *
  39. * @param {Object} obj - packet object
  40. */
  41. encode(obj: Packet): any[];
  42. /**
  43. * Encode packet as string.
  44. */
  45. private encodeAsString;
  46. /**
  47. * Encode packet as 'buffer sequence' by removing blobs, and
  48. * deconstructing packet into object with placeholders and
  49. * a list of buffers.
  50. */
  51. private encodeAsBinary;
  52. }
  53. interface DecoderReservedEvents {
  54. decoded: (packet: Packet) => void;
  55. }
  56. type JSONReviver = (this: any, key: string, value: any) => any;
  57. export interface DecoderOptions {
  58. /**
  59. * Custom reviver to pass down to JSON.parse()
  60. */
  61. reviver?: JSONReviver;
  62. /**
  63. * Maximum number of binary attachments per packet
  64. * @default 10
  65. */
  66. maxAttachments?: number;
  67. }
  68. /**
  69. * A socket.io Decoder instance
  70. *
  71. * @return {Object} decoder
  72. */
  73. export declare class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
  74. private reconstructor;
  75. private opts;
  76. /**
  77. * Decoder constructor
  78. */
  79. constructor(opts?: DecoderOptions | JSONReviver);
  80. /**
  81. * Decodes an encoded packet string into packet JSON.
  82. *
  83. * @param {String} obj - encoded packet
  84. */
  85. add(obj: any): void;
  86. /**
  87. * Decode a packet String (JSON data)
  88. *
  89. * @param {String} str
  90. * @return {Object} packet
  91. */
  92. private decodeString;
  93. private tryParse;
  94. private static isPayloadValid;
  95. /**
  96. * Deallocates a parser's resources
  97. */
  98. destroy(): void;
  99. }
  100. export declare function isPacketValid(packet: Packet): boolean;
  101. export {};