|
| 1 | +// Source: https://github.com/rexxars/eventsource-parser/tree/v1.1.2 |
| 2 | +// |
| 3 | +// MIT License |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Espen Hovlandsdal <[email protected]> |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | +var __defProp = Object.defineProperty; |
| 25 | +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 26 | +var __getOwnPropNames = Object.getOwnPropertyNames; |
| 27 | +var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 28 | +var __export = (target, all) => { |
| 29 | + for (var name in all) |
| 30 | + __defProp(target, name, { get: all[name], enumerable: true }); |
| 31 | +}; |
| 32 | +var __copyProps = (to, from, except, desc) => { |
| 33 | + if ((from && typeof from === "object") || typeof from === "function") { |
| 34 | + for (let key of __getOwnPropNames(from)) |
| 35 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 36 | + __defProp(to, key, { |
| 37 | + get: () => from[key], |
| 38 | + enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable, |
| 39 | + }); |
| 40 | + } |
| 41 | + return to; |
| 42 | +}; |
| 43 | +var __toCommonJS = (mod) => |
| 44 | + __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 45 | + |
| 46 | +// /input.ts |
| 47 | +var input_exports = {}; |
| 48 | +__export(input_exports, { |
| 49 | + EventSourceParserStream: () => EventSourceParserStream, |
| 50 | +}); |
| 51 | +module.exports = __toCommonJS(input_exports); |
| 52 | + |
| 53 | +// http-url:https://unpkg.com/[email protected]/dist/index.js |
| 54 | +function createParser(onParse) { |
| 55 | + let isFirstChunk; |
| 56 | + let buffer; |
| 57 | + let startingPosition; |
| 58 | + let startingFieldLength; |
| 59 | + let eventId; |
| 60 | + let eventName; |
| 61 | + let data; |
| 62 | + reset(); |
| 63 | + return { |
| 64 | + feed, |
| 65 | + reset, |
| 66 | + }; |
| 67 | + function reset() { |
| 68 | + isFirstChunk = true; |
| 69 | + buffer = ""; |
| 70 | + startingPosition = 0; |
| 71 | + startingFieldLength = -1; |
| 72 | + eventId = void 0; |
| 73 | + eventName = void 0; |
| 74 | + data = ""; |
| 75 | + } |
| 76 | + function feed(chunk) { |
| 77 | + buffer = buffer ? buffer + chunk : chunk; |
| 78 | + if (isFirstChunk && hasBom(buffer)) { |
| 79 | + buffer = buffer.slice(BOM.length); |
| 80 | + } |
| 81 | + isFirstChunk = false; |
| 82 | + const length = buffer.length; |
| 83 | + let position = 0; |
| 84 | + let discardTrailingNewline = false; |
| 85 | + while (position < length) { |
| 86 | + if (discardTrailingNewline) { |
| 87 | + if (buffer[position] === "\n") { |
| 88 | + ++position; |
| 89 | + } |
| 90 | + discardTrailingNewline = false; |
| 91 | + } |
| 92 | + let lineLength = -1; |
| 93 | + let fieldLength = startingFieldLength; |
| 94 | + let character; |
| 95 | + for ( |
| 96 | + let index = startingPosition; |
| 97 | + lineLength < 0 && index < length; |
| 98 | + ++index |
| 99 | + ) { |
| 100 | + character = buffer[index]; |
| 101 | + if (character === ":" && fieldLength < 0) { |
| 102 | + fieldLength = index - position; |
| 103 | + } else if (character === "\r") { |
| 104 | + discardTrailingNewline = true; |
| 105 | + lineLength = index - position; |
| 106 | + } else if (character === "\n") { |
| 107 | + lineLength = index - position; |
| 108 | + } |
| 109 | + } |
| 110 | + if (lineLength < 0) { |
| 111 | + startingPosition = length - position; |
| 112 | + startingFieldLength = fieldLength; |
| 113 | + break; |
| 114 | + } else { |
| 115 | + startingPosition = 0; |
| 116 | + startingFieldLength = -1; |
| 117 | + } |
| 118 | + parseEventStreamLine(buffer, position, fieldLength, lineLength); |
| 119 | + position += lineLength + 1; |
| 120 | + } |
| 121 | + if (position === length) { |
| 122 | + buffer = ""; |
| 123 | + } else if (position > 0) { |
| 124 | + buffer = buffer.slice(position); |
| 125 | + } |
| 126 | + } |
| 127 | + function parseEventStreamLine(lineBuffer, index, fieldLength, lineLength) { |
| 128 | + if (lineLength === 0) { |
| 129 | + if (data.length > 0) { |
| 130 | + onParse({ |
| 131 | + type: "event", |
| 132 | + id: eventId, |
| 133 | + event: eventName || void 0, |
| 134 | + data: data.slice(0, -1), |
| 135 | + // remove trailing newline |
| 136 | + }); |
| 137 | + data = ""; |
| 138 | + eventId = void 0; |
| 139 | + } |
| 140 | + eventName = void 0; |
| 141 | + return; |
| 142 | + } |
| 143 | + const noValue = fieldLength < 0; |
| 144 | + const field = lineBuffer.slice( |
| 145 | + index, |
| 146 | + index + (noValue ? lineLength : fieldLength) |
| 147 | + ); |
| 148 | + let step = 0; |
| 149 | + if (noValue) { |
| 150 | + step = lineLength; |
| 151 | + } else if (lineBuffer[index + fieldLength + 1] === " ") { |
| 152 | + step = fieldLength + 2; |
| 153 | + } else { |
| 154 | + step = fieldLength + 1; |
| 155 | + } |
| 156 | + const position = index + step; |
| 157 | + const valueLength = lineLength - step; |
| 158 | + const value = lineBuffer.slice(position, position + valueLength).toString(); |
| 159 | + if (field === "data") { |
| 160 | + data += value ? "".concat(value, "\n") : "\n"; |
| 161 | + } else if (field === "event") { |
| 162 | + eventName = value; |
| 163 | + } else if (field === "id" && !value.includes("\0")) { |
| 164 | + eventId = value; |
| 165 | + } else if (field === "retry") { |
| 166 | + const retry = parseInt(value, 10); |
| 167 | + if (!Number.isNaN(retry)) { |
| 168 | + onParse({ |
| 169 | + type: "reconnect-interval", |
| 170 | + value: retry, |
| 171 | + }); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +var BOM = [239, 187, 191]; |
| 177 | +function hasBom(buffer) { |
| 178 | + return BOM.every((charCode, index) => buffer.charCodeAt(index) === charCode); |
| 179 | +} |
| 180 | + |
| 181 | +// http-url:https://unpkg.com/[email protected]/dist/stream.js |
| 182 | +var EventSourceParserStream = class extends TransformStream { |
| 183 | + constructor() { |
| 184 | + let parser; |
| 185 | + super({ |
| 186 | + start(controller) { |
| 187 | + parser = createParser((event) => { |
| 188 | + if (event.type === "event") { |
| 189 | + controller.enqueue(event); |
| 190 | + } |
| 191 | + }); |
| 192 | + }, |
| 193 | + transform(chunk) { |
| 194 | + parser.feed(chunk); |
| 195 | + }, |
| 196 | + }); |
| 197 | + } |
| 198 | +}; |
0 commit comments