index.d.cts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Creates a new EventSource parser.
  3. *
  4. * @param callbacks - Callbacks to invoke on different parsing events:
  5. * - `onEvent` when a new event is parsed
  6. * - `onError` when an error occurs
  7. * - `onRetry` when a new reconnection interval has been sent from the server
  8. * - `onComment` when a comment is encountered in the stream
  9. *
  10. * @returns A new EventSource parser, with `parse` and `reset` methods.
  11. * @public
  12. */
  13. export declare function createParser(callbacks: ParserCallbacks): EventSourceParser
  14. /**
  15. * The type of error that occurred.
  16. * @public
  17. */
  18. export declare type ErrorType = 'invalid-retry' | 'unknown-field'
  19. /**
  20. * A parsed EventSource message event
  21. *
  22. * @public
  23. */
  24. export declare interface EventSourceMessage {
  25. /**
  26. * The event type sent from the server. Note that this differs from the browser `EventSource`
  27. * implementation in that browsers will default this to `message`, whereas this parser will
  28. * leave this as `undefined` if not explicitly declared.
  29. */
  30. event?: string | undefined
  31. /**
  32. * ID of the message, if any was provided by the server. Can be used by clients to keep the
  33. * last received message ID in sync when reconnecting.
  34. */
  35. id?: string | undefined
  36. /**
  37. * The data received for this message
  38. */
  39. data: string
  40. }
  41. /**
  42. * EventSource parser instance.
  43. *
  44. * Needs to be reset between reconnections/when switching data source, using the `reset()` method.
  45. *
  46. * @public
  47. */
  48. export declare interface EventSourceParser {
  49. /**
  50. * Feeds the parser another chunk. The method _does not_ return a parsed message.
  51. * Instead, callbacks passed when creating the parser will be triggered once we see enough data
  52. * for a valid/invalid parsing step (see {@link ParserCallbacks}).
  53. *
  54. * @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
  55. * @public
  56. */
  57. feed(chunk: string): void
  58. /**
  59. * Resets the parser state. This is required when you have a new stream of messages -
  60. * for instance in the case of a client being disconnected and reconnecting.
  61. *
  62. * Previously received, incomplete data will NOT be parsed unless you pass `consume: true`,
  63. * which tells the parser to attempt to consume any incomplete data as if it ended with a newline
  64. * character. This is useful for cases when a server sends a non-EventSource message that you
  65. * want to be able to react to in an `onError` callback.
  66. *
  67. * @public
  68. */
  69. reset(options?: {consume?: boolean}): void
  70. }
  71. /**
  72. * Error thrown when encountering an issue during parsing.
  73. *
  74. * @public
  75. */
  76. export declare class ParseError extends Error {
  77. /**
  78. * The type of error that occurred.
  79. */
  80. type: ErrorType
  81. /**
  82. * In the case of an unknown field encountered in the stream, this will be the field name.
  83. */
  84. field?: string | undefined
  85. /**
  86. * In the case of an unknown field encountered in the stream, this will be the value of the field.
  87. */
  88. value?: string | undefined
  89. /**
  90. * The line that caused the error, if available.
  91. */
  92. line?: string | undefined
  93. constructor(
  94. message: string,
  95. options: {
  96. type: ErrorType
  97. field?: string
  98. value?: string
  99. line?: string
  100. },
  101. )
  102. }
  103. /**
  104. * Callbacks that can be passed to the parser to handle different types of parsed messages
  105. * and errors.
  106. *
  107. * @public
  108. */
  109. export declare interface ParserCallbacks {
  110. /**
  111. * Callback for when a new event/message is parsed from the stream.
  112. * This is the main callback that clients will use to handle incoming messages.
  113. *
  114. * @param event - The parsed event/message
  115. */
  116. onEvent?: ((event: EventSourceMessage) => void) | undefined
  117. /**
  118. * Callback for when the server sends a new reconnection interval through the `retry` field.
  119. *
  120. * @param retry - The number of milliseconds to wait before reconnecting.
  121. */
  122. onRetry?: ((retry: number) => void) | undefined
  123. /**
  124. * Callback for when a comment is encountered in the stream.
  125. *
  126. * @param comment - The comment encountered in the stream.
  127. */
  128. onComment?: ((comment: string) => void) | undefined
  129. /**
  130. * Callback for when an error occurs during parsing. This is a catch-all for any errors
  131. * that occur during parsing, and can be used to handle them in a custom way. Most clients
  132. * tend to silently ignore any errors and instead retry, but it can be helpful to log/debug.
  133. *
  134. * @param error - The error that occurred during parsing
  135. */
  136. onError?: ((error: ParseError) => void) | undefined
  137. }
  138. export {}