stream.d.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * The type of error that occurred.
  3. * @public
  4. */
  5. export declare type ErrorType = 'invalid-retry' | 'unknown-field'
  6. /**
  7. * A parsed EventSource message event
  8. *
  9. * @public
  10. */
  11. export declare interface EventSourceMessage {
  12. /**
  13. * The event type sent from the server. Note that this differs from the browser `EventSource`
  14. * implementation in that browsers will default this to `message`, whereas this parser will
  15. * leave this as `undefined` if not explicitly declared.
  16. */
  17. event?: string | undefined
  18. /**
  19. * ID of the message, if any was provided by the server. Can be used by clients to keep the
  20. * last received message ID in sync when reconnecting.
  21. */
  22. id?: string | undefined
  23. /**
  24. * The data received for this message
  25. */
  26. data: string
  27. }
  28. /**
  29. * A TransformStream that ingests a stream of strings and produces a stream of `EventSourceMessage`.
  30. *
  31. * @example Basic usage
  32. * ```
  33. * const eventStream =
  34. * response.body
  35. * .pipeThrough(new TextDecoderStream())
  36. * .pipeThrough(new EventSourceParserStream())
  37. * ```
  38. *
  39. * @example Terminate stream on parsing errors
  40. * ```
  41. * const eventStream =
  42. * response.body
  43. * .pipeThrough(new TextDecoderStream())
  44. * .pipeThrough(new EventSourceParserStream({terminateOnError: true}))
  45. * ```
  46. *
  47. * @public
  48. */
  49. export declare class EventSourceParserStream extends TransformStream<string, EventSourceMessage> {
  50. constructor({onError, onRetry, onComment}?: StreamOptions)
  51. }
  52. /**
  53. * Error thrown when encountering an issue during parsing.
  54. *
  55. * @public
  56. */
  57. export declare class ParseError extends Error {
  58. /**
  59. * The type of error that occurred.
  60. */
  61. type: ErrorType
  62. /**
  63. * In the case of an unknown field encountered in the stream, this will be the field name.
  64. */
  65. field?: string | undefined
  66. /**
  67. * In the case of an unknown field encountered in the stream, this will be the value of the field.
  68. */
  69. value?: string | undefined
  70. /**
  71. * The line that caused the error, if available.
  72. */
  73. line?: string | undefined
  74. constructor(
  75. message: string,
  76. options: {
  77. type: ErrorType
  78. field?: string
  79. value?: string
  80. line?: string
  81. },
  82. )
  83. }
  84. /**
  85. * Options for the EventSourceParserStream.
  86. *
  87. * @public
  88. */
  89. export declare interface StreamOptions {
  90. /**
  91. * Behavior when a parsing error occurs.
  92. *
  93. * - A custom function can be provided to handle the error.
  94. * - `'terminate'` will error the stream and stop parsing.
  95. * - Any other value will ignore the error and continue parsing.
  96. *
  97. * @defaultValue `undefined`
  98. */
  99. onError?: ('terminate' | ((error: Error) => void)) | undefined
  100. /**
  101. * Callback for when a reconnection interval is sent from the server.
  102. *
  103. * @param retry - The number of milliseconds to wait before reconnecting.
  104. */
  105. onRetry?: ((retry: number) => void) | undefined
  106. /**
  107. * Callback for when a comment is encountered in the stream.
  108. *
  109. * @param comment - The comment encountered in the stream.
  110. */
  111. onComment?: ((comment: string) => void) | undefined
  112. }
  113. export {}