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