|
| 1 | +// https://github.com/valadaptive/server-sent-stream |
| 2 | + |
| 3 | +/** |
| 4 | + * A parser for the server-sent events stream format. |
| 5 | + * |
| 6 | + * Note that this parser does not handle text decoding! To do it correctly, use a streaming text decoder, since the |
| 7 | + * stream could be split up mid-Unicode character, and decoding each chunk at once could lead to incorrect results. |
| 8 | + * |
| 9 | + * This parser is used by streaming chunks in using the {@link push} method, and then calling the {@link end} method |
| 10 | + * when the stream has ended. |
| 11 | + */ |
| 12 | +class EventStreamParser { |
| 13 | + /** |
| 14 | + * Construct a new parser for a single stream. |
| 15 | + * @param onEvent A callback which will be called for each new event parsed. The parameters in order are the |
| 16 | + * event data, the event type, and the last seen event ID. This may be called none, once, or many times per push() |
| 17 | + * call, and may be called from the end() call. |
| 18 | + */ |
| 19 | + constructor(onEvent) { |
| 20 | + this.streamBuffer = ""; |
| 21 | + this.lastEventId = ""; |
| 22 | + this.onEvent = onEvent; |
| 23 | + } |
| 24 | + /** |
| 25 | + * Process a single incoming chunk of the event stream. |
| 26 | + */ |
| 27 | + _processChunk() { |
| 28 | + // Events are separated by two newlines |
| 29 | + const events = this.streamBuffer.split(/\r\n\r\n|\r\r|\n\n/g); |
| 30 | + if (events.length === 0) { |
| 31 | + return; |
| 32 | + } |
| 33 | + // The leftover text to remain in the buffer is whatever doesn't have two newlines after it. If the buffer ended |
| 34 | + // with two newlines, this will be an empty string. |
| 35 | + this.streamBuffer = events.pop(); |
| 36 | + for (const eventChunk of events) { |
| 37 | + let eventType = ""; |
| 38 | + // Split up by single newlines. |
| 39 | + const lines = eventChunk.split(/\n|\r|\r\n/g); |
| 40 | + let eventData = ""; |
| 41 | + for (const line of lines) { |
| 42 | + const lineMatch = /([^:]+)(?:: ?(.*))?/.exec(line); |
| 43 | + if (lineMatch) { |
| 44 | + const field = lineMatch[1]; |
| 45 | + const value = lineMatch[2] || ""; |
| 46 | + switch (field) { |
| 47 | + case "event": |
| 48 | + eventType = value; |
| 49 | + break; |
| 50 | + case "data": |
| 51 | + eventData += value; |
| 52 | + eventData += "\n"; |
| 53 | + break; |
| 54 | + case "id": |
| 55 | + // The ID field cannot contain null, per the spec |
| 56 | + if (!value.includes("\0")) { |
| 57 | + this.lastEventId = value; |
| 58 | + } |
| 59 | + break; |
| 60 | + // We do nothing for the `delay` type, and other types are explicitly ignored |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + // https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage |
| 65 | + // Skip the event if the data buffer is the empty string. |
| 66 | + if (eventData === "") { |
| 67 | + continue; |
| 68 | + } |
| 69 | + if (eventData[eventData.length - 1] === "\n") { |
| 70 | + eventData = eventData.slice(0, -1); |
| 71 | + } |
| 72 | + // Trim the *last* trailing newline only. |
| 73 | + this.onEvent(eventData, eventType || "message", this.lastEventId); |
| 74 | + } |
| 75 | + } |
| 76 | + /** |
| 77 | + * Push a new chunk of data to the parser. This may cause the {@link onEvent} callback to be called, possibly |
| 78 | + * multiple times depending on the number of events contained within the chunk. |
| 79 | + * @param chunk The incoming chunk of data. |
| 80 | + */ |
| 81 | + push(chunk) { |
| 82 | + this.streamBuffer += chunk; |
| 83 | + this._processChunk(); |
| 84 | + } |
| 85 | + /** |
| 86 | + * Indicate that the stream has ended. |
| 87 | + */ |
| 88 | + end() { |
| 89 | + // This is a no-op |
| 90 | + } |
| 91 | +} |
| 92 | +export default EventStreamParser; |
0 commit comments