* Converts this stream to a newline-separated ReadableStream of * JSON stringified values in the stream which can be turned back into a Stream with `Stream.fromReadableStream()`.
()
| 178 | * JSON stringified values in the stream which can be turned back into a Stream with `Stream.fromReadableStream()`. |
| 179 | */ |
| 180 | toReadableStream(): ReadableStream { |
| 181 | const self = this; |
| 182 | let iter: AsyncIterator<Item>; |
| 183 | const encoder = new TextEncoder(); |
| 184 | |
| 185 | return new ReadableStream({ |
| 186 | async start() { |
| 187 | iter = self[Symbol.asyncIterator](); |
| 188 | }, |
| 189 | async pull(ctrl: any) { |
| 190 | try { |
| 191 | const {value, done} = await iter.next(); |
| 192 | if (done) return ctrl.close(); |
| 193 | |
| 194 | const bytes = encoder.encode(JSON.stringify(value) + '\n'); |
| 195 | |
| 196 | ctrl.enqueue(bytes); |
| 197 | } catch (err) { |
| 198 | ctrl.error(err); |
| 199 | } |
| 200 | }, |
| 201 | async cancel() { |
| 202 | await iter.return?.(); |
| 203 | }, |
| 204 | }); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
no outgoing calls
no test coverage detected