* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally * reading lines from text. * * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
| 387 | * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258 |
| 388 | */ |
| 389 | class LineDecoder { |
| 390 | // prettier-ignore |
| 391 | static NEWLINE_CHARS = new Set(['\n', '\r']); |
| 392 | static NEWLINE_REGEXP = /\r\n|[\n\r]/g; |
| 393 | |
| 394 | buffer: string[]; |
| 395 | trailingCR: boolean; |
| 396 | textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types. |
| 397 | |
| 398 | constructor() { |
| 399 | this.buffer = []; |
| 400 | this.trailingCR = false; |
| 401 | } |
| 402 | |
| 403 | decode(chunk: Bytes): string[] { |
| 404 | let text = this.decodeText(chunk); |
| 405 | |
| 406 | if (this.trailingCR) { |
| 407 | text = '\r' + text; |
| 408 | this.trailingCR = false; |
| 409 | } |
| 410 | if (text.endsWith('\r')) { |
| 411 | this.trailingCR = true; |
| 412 | text = text.slice(0, -1); |
| 413 | } |
| 414 | |
| 415 | if (!text) { |
| 416 | return []; |
| 417 | } |
| 418 | |
| 419 | const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || ''); |
| 420 | let lines = text.split(LineDecoder.NEWLINE_REGEXP); |
| 421 | |
| 422 | // if there is a trailing new line then the last entry will be an empty |
| 423 | // string which we don't care about |
| 424 | if (trailingNewline) { |
| 425 | lines.pop(); |
| 426 | } |
| 427 | |
| 428 | if (lines.length === 1 && !trailingNewline) { |
| 429 | this.buffer.push(lines[0]!); |
| 430 | return []; |
| 431 | } |
| 432 | |
| 433 | if (this.buffer.length > 0) { |
| 434 | lines = [this.buffer.join('') + lines[0], ...lines.slice(1)]; |
| 435 | this.buffer = []; |
| 436 | } |
| 437 | |
| 438 | if (!trailingNewline) { |
| 439 | this.buffer = [lines.pop() || '']; |
| 440 | } |
| 441 | |
| 442 | return lines; |
| 443 | } |
| 444 | |
| 445 | decodeText(bytes: Bytes): string { |
| 446 | if (bytes == null) return ''; |
nothing calls this directly
no outgoing calls
no test coverage detected