(chunk: string)
| 178 | } |
| 179 | |
| 180 | function feed(chunk: string): SseError | undefined { |
| 181 | buffer = buffer ? buffer + chunk : chunk |
| 182 | |
| 183 | // Strip any UTF8 byte order mark (BOM) at the start of the stream. |
| 184 | // Note that we do not strip any non - UTF8 BOM, as eventsource streams are |
| 185 | // always decoded as UTF8 as per the specification. |
| 186 | if (isFirstChunk && hasBom(buffer)) { |
| 187 | buffer = buffer.slice(BOM.length) |
| 188 | } |
| 189 | |
| 190 | isFirstChunk = false |
| 191 | |
| 192 | // Set up chunk-specific processing state |
| 193 | const length = buffer.length |
| 194 | let position = 0 |
| 195 | let discardTrailingNewline = false |
| 196 | |
| 197 | // Read the current buffer byte by byte |
| 198 | while (position < length) { |
| 199 | // EventSource allows for carriage return + line feed, which means we |
| 200 | // need to ignore a linefeed character if the previous character was a |
| 201 | // carriage return |
| 202 | // @todo refactor to reduce nesting, consider checking previous byte? |
| 203 | // @todo but consider multiple chunks etc |
| 204 | if (discardTrailingNewline) { |
| 205 | if (buffer[position] === "\n") { |
| 206 | ++position |
| 207 | } |
| 208 | discardTrailingNewline = false |
| 209 | } |
| 210 | |
| 211 | let lineLength = -1 |
| 212 | let fieldLength = startingFieldLength |
| 213 | let character: string |
| 214 | |
| 215 | for (let index = startingPosition; lineLength < 0 && index < length; ++index) { |
| 216 | character = buffer[index] |
| 217 | if (character === ":" && fieldLength < 0) { |
| 218 | fieldLength = index - position |
| 219 | } else if (character === "\r") { |
| 220 | discardTrailingNewline = true |
| 221 | lineLength = index - position |
| 222 | } else if (character === "\n") { |
| 223 | lineLength = index - position |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (lineLength < 0) { |
| 228 | startingPosition = length - position |
| 229 | startingFieldLength = fieldLength |
| 230 | break |
| 231 | } else { |
| 232 | startingPosition = 0 |
| 233 | startingFieldLength = -1 |
| 234 | } |
| 235 | |
| 236 | parseEventStreamLine(buffer, position, fieldLength, lineLength) |
| 237 |
nothing calls this directly
no test coverage detected