(reader)
| 64 | |
| 65 | // src/ext/eventsource.js |
| 66 | async function* parseSSE(reader) { |
| 67 | var decoder = new TextDecoder(); |
| 68 | var buffer = ""; |
| 69 | var hasData = false; |
| 70 | var message = { data: "", event: "", id: "", retry: null }; |
| 71 | var firstChunk = true; |
| 72 | try { |
| 73 | while (true) { |
| 74 | var { done, value } = await reader.read(); |
| 75 | if (done) break; |
| 76 | var chunk = decoder.decode(value, { stream: true }); |
| 77 | if (firstChunk) { |
| 78 | if (chunk.charCodeAt(0) === 65279) chunk = chunk.slice(1); |
| 79 | firstChunk = false; |
| 80 | } |
| 81 | buffer += chunk; |
| 82 | var lines = buffer.split(/\r\n|\r|\n/); |
| 83 | buffer = lines.pop() || ""; |
| 84 | for (var i = 0; i < lines.length; i++) { |
| 85 | var line = lines[i]; |
| 86 | if (!line) { |
| 87 | if (hasData) { |
| 88 | yield message; |
| 89 | hasData = false; |
| 90 | message = { data: "", event: "", id: "", retry: null }; |
| 91 | } |
| 92 | continue; |
| 93 | } |
| 94 | var colonIndex = line.indexOf(":"); |
| 95 | if (colonIndex === 0) continue; |
| 96 | var field, val; |
| 97 | if (colonIndex < 0) { |
| 98 | field = line; |
| 99 | val = ""; |
| 100 | } else { |
| 101 | field = line.slice(0, colonIndex); |
| 102 | val = line.slice(colonIndex + 1); |
| 103 | if (val[0] === " ") val = val.slice(1); |
| 104 | } |
| 105 | if (field === "data") { |
| 106 | message.data += (hasData ? "\n" : "") + val; |
| 107 | hasData = true; |
| 108 | } else if (field === "event") { |
| 109 | message.event = val; |
| 110 | } else if (field === "id") { |
| 111 | if (!val.includes("\0")) message.id = val; |
| 112 | } else if (field === "retry") { |
| 113 | var retryValue = parseInt(val, 10); |
| 114 | if (!isNaN(retryValue)) message.retry = retryValue; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } finally { |
| 119 | reader.releaseLock(); |
| 120 | } |
| 121 | } |
| 122 | function matchesEventPattern(pattern, eventName) { |
| 123 | if (pattern === eventName) return true; |
no outgoing calls
no test coverage detected