( iterable: AsyncIterable<Uint8Array>, writable: Writable, )
| 130 | } |
| 131 | |
| 132 | export async function writeAsyncIterableToWritable( |
| 133 | iterable: AsyncIterable<Uint8Array>, |
| 134 | writable: Writable, |
| 135 | ) { |
| 136 | let writableError = monitorWritableError(writable); |
| 137 | let iterator = iterable[Symbol.asyncIterator](); |
| 138 | let completed = false; |
| 139 | |
| 140 | try { |
| 141 | while (true) { |
| 142 | writableError.throwIfClosed(); |
| 143 | |
| 144 | let { done, value: chunk } = await writableError.race(iterator.next()); |
| 145 | |
| 146 | if (done) { |
| 147 | completed = true; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | writableError.throwIfClosed(); |
| 152 | |
| 153 | let canContinueWriting = writable.write(chunk); |
| 154 | |
| 155 | if (!canContinueWriting) { |
| 156 | await waitForDrain(writable, writableError); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | writable.end(); |
| 161 | } catch (error: any) { |
| 162 | if (!completed) { |
| 163 | try { |
| 164 | Promise.resolve(iterator.return?.()).catch(() => {}); |
| 165 | } catch { |
| 166 | // Ignore return errors so we preserve the original write failure. |
| 167 | } |
| 168 | } |
| 169 | writable.destroy(error); |
| 170 | throw error; |
| 171 | } finally { |
| 172 | writableError.cleanup(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | export async function readableStreamToString( |
| 177 | stream: ReadableStream<Uint8Array>, |
no test coverage detected
searching dependent graphs…