| 185 | } |
| 186 | |
| 187 | function createParser(onParse) { |
| 188 | let isFirstChunk; |
| 189 | let buffer; |
| 190 | let startingPosition; |
| 191 | let startingFieldLength; |
| 192 | let eventId; |
| 193 | let eventName; |
| 194 | let data; |
| 195 | reset(); |
| 196 | return { |
| 197 | feed, |
| 198 | reset, |
| 199 | }; |
| 200 | function reset() { |
| 201 | isFirstChunk = true; |
| 202 | buffer = ""; |
| 203 | startingPosition = 0; |
| 204 | startingFieldLength = -1; |
| 205 | eventId = void 0; |
| 206 | eventName = void 0; |
| 207 | data = ""; |
| 208 | } |
| 209 | function feed(chunk) { |
| 210 | buffer = buffer ? buffer + chunk : chunk; |
| 211 | if (isFirstChunk && hasBom(buffer)) { |
| 212 | buffer = buffer.slice(BOM.length); |
| 213 | } |
| 214 | isFirstChunk = false; |
| 215 | const length = buffer.length; |
| 216 | let position = 0; |
| 217 | let discardTrailingNewline = false; |
| 218 | while (position < length) { |
| 219 | if (discardTrailingNewline) { |
| 220 | if (buffer[position] === "\n") { |
| 221 | ++position; |
| 222 | } |
| 223 | discardTrailingNewline = false; |
| 224 | } |
| 225 | let lineLength = -1; |
| 226 | let fieldLength = startingFieldLength; |
| 227 | let character; |
| 228 | for ( |
| 229 | let index = startingPosition; |
| 230 | lineLength < 0 && index < length; |
| 231 | ++index |
| 232 | ) { |
| 233 | character = buffer[index]; |
| 234 | if (character === ":" && fieldLength < 0) { |
| 235 | fieldLength = index - position; |
| 236 | } else if (character === "\r") { |
| 237 | discardTrailingNewline = true; |
| 238 | lineLength = index - position; |
| 239 | } else if (character === "\n") { |
| 240 | lineLength = index - position; |
| 241 | } |
| 242 | } |
| 243 | if (lineLength < 0) { |
| 244 | startingPosition = length - position; |