|
| 1 | +const { Readable } = require("stream"); |
| 2 | + |
| 3 | +/** |
| 4 | + * A server-sent event. |
| 5 | + */ |
| 6 | +class ServerSentEvent { |
| 7 | + /** |
| 8 | + * Create a new server-sent event. |
| 9 | + * |
| 10 | + * @param {string} event The event name. |
| 11 | + * @param {string} data The event data. |
| 12 | + * @param {string} id The event ID. |
| 13 | + * @param {number} retry The retry time. |
| 14 | + */ |
| 15 | + constructor(event, data, id, retry) { |
| 16 | + this.event = event; |
| 17 | + this.data = data; |
| 18 | + this.id = id; |
| 19 | + this.retry = retry; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Convert the event to a string. |
| 24 | + */ |
| 25 | + toString() { |
| 26 | + if (this.event === "output") { |
| 27 | + return this.data; |
| 28 | + } |
| 29 | + |
| 30 | + return ""; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A stream of server-sent events. |
| 36 | + */ |
| 37 | +class Stream extends Readable { |
| 38 | + /** |
| 39 | + * Create a new stream of server-sent events. |
| 40 | + * |
| 41 | + * @param {string} url The URL to connect to. |
| 42 | + * @param {object} options The fetch options. |
| 43 | + */ |
| 44 | + constructor(url, options) { |
| 45 | + super(); |
| 46 | + this.url = url; |
| 47 | + this.options = options; |
| 48 | + |
| 49 | + this.event = null; |
| 50 | + this.data = []; |
| 51 | + this.lastEventId = null; |
| 52 | + this.retry = null; |
| 53 | + } |
| 54 | + |
| 55 | + decode(line) { |
| 56 | + if (!line) { |
| 57 | + if (!this.event && !this.data.length && !this.lastEventId) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + const sse = new ServerSentEvent( |
| 62 | + this.event, |
| 63 | + this.data.join("\n"), |
| 64 | + this.lastEventId |
| 65 | + ); |
| 66 | + |
| 67 | + this.event = null; |
| 68 | + this.data = []; |
| 69 | + this.retry = null; |
| 70 | + |
| 71 | + return sse; |
| 72 | + } |
| 73 | + |
| 74 | + if (line.startsWith(":")) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + const [field, value] = line.split(": "); |
| 79 | + if (field === "event") { |
| 80 | + this.event = value; |
| 81 | + } else if (field === "data") { |
| 82 | + this.data.push(value); |
| 83 | + } else if (field === "id") { |
| 84 | + this.lastEventId = value; |
| 85 | + } |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + async *[Symbol.asyncIterator]() { |
| 91 | + const response = await fetch(this.url, { |
| 92 | + ...this.options, |
| 93 | + headers: { |
| 94 | + Accept: "text/event-stream", |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + for await (const chunk of response.body) { |
| 99 | + const decoder = new TextDecoder("utf-8"); |
| 100 | + const text = decoder.decode(chunk); |
| 101 | + const lines = text.split("\n"); |
| 102 | + for (const line of lines) { |
| 103 | + const sse = this.decode(line); |
| 104 | + if (sse) { |
| 105 | + if (sse.event === "error") { |
| 106 | + throw new Error(sse.data); |
| 107 | + } |
| 108 | + |
| 109 | + yield sse; |
| 110 | + |
| 111 | + if (sse.event === "done") { |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +module.exports = { |
| 121 | + Stream, |
| 122 | + ServerSentEvent, |
| 123 | +}; |
0 commit comments