Given an iterator that yields raw binary data, iterate over it and yield individual SSE chunks
(self, iterator: Iterator[bytes])
| 209 | yield sse |
| 210 | |
| 211 | def _iter_chunks(self, iterator: Iterator[bytes]) -> Iterator[bytes]: |
| 212 | """Given an iterator that yields raw binary data, iterate over it and yield individual SSE chunks""" |
| 213 | data = b"" |
| 214 | for chunk in iterator: |
| 215 | for line in chunk.splitlines(keepends=True): |
| 216 | data += line |
| 217 | if data.endswith((b"\r\r", b"\n\n", b"\r\n\r\n")): |
| 218 | yield data |
| 219 | data = b"" |
| 220 | if data: |
| 221 | yield data |
| 222 | |
| 223 | async def aiter_bytes(self, iterator: AsyncIterator[bytes]) -> AsyncIterator[ServerSentEvent]: |
| 224 | """Given an iterator that yields raw binary data, iterate over it & yield every event encountered""" |