(chunk: string)
| 278 | } |
| 279 | |
| 280 | function feed(chunk: string): SseError | undefined { |
| 281 | buffer = buffer ? buffer + chunk : chunk |
| 282 | |
| 283 | // Strip any UTF-8 byte order mark (BOM) at the start of the stream. |
| 284 | // Note that we do not strip any non - UTF8 BOM, as eventsource streams are |
| 285 | // always decoded as UTF8 as per the specification. |
| 286 | if (isFirstChunk && buffer.startsWith(BOM)) { |
| 287 | buffer = buffer.slice(BOM.length) |
| 288 | } |
| 289 | |
| 290 | isFirstChunk = false |
| 291 | |
| 292 | // Set up chunk-specific processing state |
| 293 | const length = buffer.length |
| 294 | let position = 0 |
| 295 | |
| 296 | // Read the current buffer byte by byte |
| 297 | while (position < length) { |
| 298 | // EventSource allows for carriage return + line feed, which means we |
| 299 | // need to ignore a linefeed character if the previous character was a |
| 300 | // carriage return |
| 301 | // @todo refactor to reduce nesting, consider checking previous byte? |
| 302 | // @todo but consider multiple chunks etc |
| 303 | if (discardTrailingNewline) { |
| 304 | if (buffer[position] === "\n") { |
| 305 | ++position |
| 306 | } |
| 307 | discardTrailingNewline = false |
| 308 | } |
| 309 | |
| 310 | let lineLength = -1 |
| 311 | let fieldLength = startingFieldLength |
| 312 | let character: string |
| 313 | |
| 314 | for (let index = startingPosition; lineLength < 0 && index < length; ++index) { |
| 315 | character = buffer[index] |
| 316 | if (character === ":" && fieldLength < 0) { |
| 317 | fieldLength = index - position |
| 318 | } else if (character === "\r") { |
| 319 | discardTrailingNewline = true |
| 320 | lineLength = index - position |
| 321 | } else if (character === "\n") { |
| 322 | lineLength = index - position |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (lineLength < 0) { |
| 327 | startingPosition = length - position |
| 328 | startingFieldLength = fieldLength |
| 329 | break |
| 330 | } else { |
| 331 | startingPosition = 0 |
| 332 | startingFieldLength = -1 |
| 333 | } |
| 334 | |
| 335 | parseEventStreamLine(buffer, position, fieldLength, lineLength) |
| 336 | |
| 337 | position += lineLength + 1 |
nothing calls this directly
no test coverage detected