|
1 | 1 | const { Readable } = require('readable-stream') |
2 | 2 |
|
3 | | -// return the next value of the iterator but if it is a promise, resolve it and |
4 | | -// reinject it |
5 | | -// |
6 | | -// this enables the use of a simple generator instead of an async generator |
7 | | -// (which are less widely supported) |
8 | | -const next = async (iterator, arg) => { |
9 | | - let cursor = iterator.next(arg) |
10 | | - if (typeof cursor.then === 'function') { |
11 | | - return cursor |
12 | | - } |
13 | | - let value |
14 | | - while ( |
15 | | - !cursor.done && |
16 | | - (value = cursor.value) != null && |
17 | | - typeof value.then === 'function' |
18 | | - ) { |
19 | | - let success = false |
20 | | - try { |
21 | | - value = await value |
22 | | - success = true |
23 | | - } catch (error) { |
24 | | - cursor = iterator.throw(error) |
25 | | - } |
26 | | - if (success) { |
27 | | - cursor = iterator.next(value) |
28 | | - } |
29 | | - } |
30 | | - return cursor |
31 | | -} |
32 | | - |
33 | 3 | const getSymbol = |
34 | 4 | typeof Symbol === 'function' |
35 | 5 | ? name => { |
@@ -97,32 +67,53 @@ function asyncIteratorToStream (iterable, options) { |
97 | 67 | } |
98 | 68 | cb(error) |
99 | 69 | } |
100 | | - readable._read = size => { |
101 | | - let running = false |
102 | | - const read = (readable._read = async size => { |
103 | | - if (running) { |
104 | | - return |
105 | | - } |
106 | | - running = true |
107 | | - try { |
108 | | - let canPush = true |
109 | | - do { |
110 | | - const cursor = await next(iterator, size) |
111 | | - if (cursor.done) { |
112 | | - return readable.push(null) |
113 | | - } |
114 | | - const value = cursor.value |
115 | | - if (value !== undefined) { |
116 | | - canPush = readable.push(value) |
| 70 | + let running = false |
| 71 | + readable._read = async size => { |
| 72 | + if (running) { |
| 73 | + return |
| 74 | + } |
| 75 | + running = true |
| 76 | + try { |
| 77 | + let value |
| 78 | + do { |
| 79 | + let cursor = iterator.next(size) |
| 80 | + |
| 81 | + // return the next value of the iterator but if it is a promise, resolve it and |
| 82 | + // reinject it |
| 83 | + // |
| 84 | + // this enables the use of a simple generator instead of an async generator |
| 85 | + // (which are less widely supported) |
| 86 | + if (typeof cursor.then === 'function') { |
| 87 | + cursor = await cursor |
| 88 | + } else { |
| 89 | + while ( |
| 90 | + !cursor.done && |
| 91 | + (value = cursor.value) != null && |
| 92 | + typeof value.then === 'function' |
| 93 | + ) { |
| 94 | + let success = false |
| 95 | + try { |
| 96 | + value = await value |
| 97 | + success = true |
| 98 | + } catch (error) { |
| 99 | + cursor = iterator.throw(error) |
| 100 | + } |
| 101 | + if (success) { |
| 102 | + cursor = iterator.next(value) |
| 103 | + } |
117 | 104 | } |
118 | | - } while (canPush) |
119 | | - } catch (error) { |
120 | | - process.nextTick(readable.emit.bind(readable, 'error', error)) |
121 | | - } finally { |
122 | | - running = false |
123 | | - } |
124 | | - }) |
125 | | - return read(size) |
| 105 | + } |
| 106 | + |
| 107 | + if (cursor.done) { |
| 108 | + return readable.push(null) |
| 109 | + } |
| 110 | + value = cursor.value |
| 111 | + } while (value === undefined || readable.push(value)) |
| 112 | + } catch (error) { |
| 113 | + process.nextTick(readable.emit.bind(readable, 'error', error)) |
| 114 | + } finally { |
| 115 | + running = false |
| 116 | + } |
126 | 117 | } |
127 | 118 | return readable |
128 | 119 | } |
|
0 commit comments