| 65 | |
| 66 | |
| 67 | class AsyncIteratorByteStream(AsyncByteStream): |
| 68 | CHUNK_SIZE = 65_536 |
| 69 | |
| 70 | def __init__(self, stream: AsyncIterable[bytes]) -> None: |
| 71 | self._stream = stream |
| 72 | self._is_stream_consumed = False |
| 73 | self._is_generator = inspect.isasyncgen(stream) |
| 74 | |
| 75 | async def __aiter__(self) -> AsyncIterator[bytes]: |
| 76 | if self._is_stream_consumed and self._is_generator: |
| 77 | raise StreamConsumed() |
| 78 | |
| 79 | self._is_stream_consumed = True |
| 80 | if hasattr(self._stream, "aread"): |
| 81 | # File-like interfaces should use 'aread' directly. |
| 82 | chunk = await self._stream.aread(self.CHUNK_SIZE) |
| 83 | while chunk: |
| 84 | yield chunk |
| 85 | chunk = await self._stream.aread(self.CHUNK_SIZE) |
| 86 | else: |
| 87 | # Otherwise iterate. |
| 88 | async for part in self._stream: |
| 89 | yield part |
| 90 | |
| 91 | |
| 92 | class UnattachedStream(AsyncByteStream, SyncByteStream): |