(contentType, chunk)
| 699 | } |
| 700 | |
| 701 | function processXHRChunk(contentType, chunk) { |
| 702 | if (!chunk) return; |
| 703 | |
| 704 | parseBuffer += chunk; |
| 705 | log('XHR received chunk, length:', chunk.length); |
| 706 | |
| 707 | if (contentType.includes('text/event-stream')) { |
| 708 | const boundaryIndex = findCompleteSSEBoundary(parseBuffer); |
| 709 | if (boundaryIndex === null) return; |
| 710 | |
| 711 | const completeData = parseBuffer.substring(0, boundaryIndex); |
| 712 | parseBuffer = parseBuffer.substring(boundaryIndex); |
| 713 | |
| 714 | const events = parseSSEEvents(completeData); |
| 715 | for (const event of events) { |
| 716 | emitXHRMessage(event.event, event.data, event.id); |
| 717 | } |
| 718 | } else if (contentType.includes('application/x-ndjson') || contentType.includes('application/jsonlines')) { |
| 719 | const lines = parseBuffer.split(/\r?\n/); |
| 720 | parseBuffer = lines.pop() || ''; |
| 721 | |
| 722 | for (const line of lines) { |
| 723 | if (line.trim()) { |
| 724 | emitXHRMessage('message', line); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | function flushXHRBuffer(contentType) { |
| 731 | if (!parseBuffer.trim()) { |
no test coverage detected