(reader)
| 11586 | |
| 11587 | // src/ext/eventsource.js |
| 11588 | async function* parseSSE(reader) { |
| 11589 | var decoder = new TextDecoder(); |
| 11590 | var buffer = ""; |
| 11591 | var hasData = false; |
| 11592 | var message = { data: "", event: "", id: "", retry: null }; |
| 11593 | var firstChunk = true; |
| 11594 | try { |
| 11595 | while (true) { |
| 11596 | var { done, value } = await reader.read(); |
| 11597 | if (done) break; |
| 11598 | var chunk = decoder.decode(value, { stream: true }); |
| 11599 | if (firstChunk) { |
| 11600 | if (chunk.charCodeAt(0) === 65279) chunk = chunk.slice(1); |
| 11601 | firstChunk = false; |
| 11602 | } |
| 11603 | buffer += chunk; |
| 11604 | var lines = buffer.split(/\r\n|\r|\n/); |
| 11605 | buffer = lines.pop() || ""; |
| 11606 | for (var i = 0; i < lines.length; i++) { |
| 11607 | var line = lines[i]; |
| 11608 | if (!line) { |
| 11609 | if (hasData) { |
| 11610 | yield message; |
| 11611 | hasData = false; |
| 11612 | message = { data: "", event: "", id: "", retry: null }; |
| 11613 | } |
| 11614 | continue; |
| 11615 | } |
| 11616 | var colonIndex = line.indexOf(":"); |
| 11617 | if (colonIndex === 0) continue; |
| 11618 | var field, val; |
| 11619 | if (colonIndex < 0) { |
| 11620 | field = line; |
| 11621 | val = ""; |
| 11622 | } else { |
| 11623 | field = line.slice(0, colonIndex); |
| 11624 | val = line.slice(colonIndex + 1); |
| 11625 | if (val[0] === " ") val = val.slice(1); |
| 11626 | } |
| 11627 | if (field === "data") { |
| 11628 | message.data += (hasData ? "\n" : "") + val; |
| 11629 | hasData = true; |
| 11630 | } else if (field === "event") { |
| 11631 | message.event = val; |
| 11632 | } else if (field === "id") { |
| 11633 | if (!val.includes("\0")) message.id = val; |
| 11634 | } else if (field === "retry") { |
| 11635 | var retryValue = parseInt(val, 10); |
| 11636 | if (!isNaN(retryValue)) message.retry = retryValue; |
| 11637 | } |
| 11638 | } |
| 11639 | } |
| 11640 | } finally { |
| 11641 | reader.releaseLock(); |
| 11642 | } |
| 11643 | } |
| 11644 | function matchesEventPattern(pattern, eventName) { |
| 11645 | if (pattern === eventName) return true; |
no outgoing calls
no test coverage detected