File size: 831 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import type {EventSourceParser, ParsedEvent} from './types.js'
import {createParser} from './parse.js'
/**
* A TransformStream that ingests a stream of strings and produces a stream of ParsedEvents.
*
* @example
* ```
* const eventStream =
* response.body
* .pipeThrough(new TextDecoderStream())
* .pipeThrough(new EventSourceParserStream())
* ```
* @public
*/
export class EventSourceParserStream extends TransformStream<string, ParsedEvent> {
constructor() {
let parser!: EventSourceParser
super({
start(controller) {
parser = createParser((event) => {
if (event.type === 'event') {
controller.enqueue(event)
}
})
},
transform(chunk) {
parser.feed(chunk)
},
})
}
}
export type {ParsedEvent} from './types.js'
|