|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const kLastResolve = Symbol('lastResolve'); |
| 4 | +const kLastReject = Symbol('lastReject'); |
| 5 | +const kError = Symbol('error'); |
| 6 | +const kEnded = Symbol('ended'); |
| 7 | +const kLastPromise = Symbol('lastPromise'); |
| 8 | +const kHandlePromise = Symbol('handlePromise'); |
| 9 | +const kStream = Symbol('stream'); |
| 10 | + |
| 11 | +const AsyncIteratorRecord = class AsyncIteratorRecord { |
| 12 | + constructor(value, done) { |
| 13 | + this.done = done; |
| 14 | + this.value = value; |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +function readAndResolve(iter) { |
| 19 | + const resolve = iter[kLastResolve]; |
| 20 | + if (resolve !== null) { |
| 21 | + const data = iter[kStream].read(); |
| 22 | + // we defer if data is null |
| 23 | + // we can be expecting either 'end' or |
| 24 | + // 'error' |
| 25 | + if (data !== null) { |
| 26 | + iter[kLastPromise] = null; |
| 27 | + iter[kLastResolve] = null; |
| 28 | + iter[kLastReject] = null; |
| 29 | + resolve(new AsyncIteratorRecord(data, false)); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function onReadable(iter) { |
| 35 | + // we wait for the next tick, because it might |
| 36 | + // emit an error with process.nextTick |
| 37 | + process.nextTick(readAndResolve, iter); |
| 38 | +} |
| 39 | + |
| 40 | +function onEnd(iter) { |
| 41 | + const resolve = iter[kLastResolve]; |
| 42 | + if (resolve !== null) { |
| 43 | + iter[kLastPromise] = null; |
| 44 | + iter[kLastResolve] = null; |
| 45 | + iter[kLastReject] = null; |
| 46 | + resolve(new AsyncIteratorRecord(null, true)); |
| 47 | + } |
| 48 | + iter[kEnded] = true; |
| 49 | +} |
| 50 | + |
| 51 | +function onError(iter, err) { |
| 52 | + const reject = iter[kLastReject]; |
| 53 | + // reject if we are waiting for data in the Promise |
| 54 | + // returned by next() and store the error |
| 55 | + if (reject !== null) { |
| 56 | + iter[kLastPromise] = null; |
| 57 | + iter[kLastResolve] = null; |
| 58 | + iter[kLastReject] = null; |
| 59 | + reject(err); |
| 60 | + } |
| 61 | + iter.error = err; |
| 62 | +} |
| 63 | + |
| 64 | +function wrapForNext(lastPromise, iter) { |
| 65 | + return function(resolve, reject) { |
| 66 | + lastPromise.then(function() { |
| 67 | + iter[kHandlePromise](resolve, reject); |
| 68 | + }, reject); |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +const ReadableAsyncIterator = class ReadableAsyncIterator { |
| 73 | + constructor(stream) { |
| 74 | + this[kStream] = stream; |
| 75 | + this[kLastResolve] = null; |
| 76 | + this[kLastReject] = null; |
| 77 | + this[kError] = null; |
| 78 | + this[kEnded] = false; |
| 79 | + this[kLastPromise] = null; |
| 80 | + |
| 81 | + stream.on('readable', onReadable.bind(null, this)); |
| 82 | + stream.on('end', onEnd.bind(null, this)); |
| 83 | + stream.on('error', onError.bind(null, this)); |
| 84 | + |
| 85 | + // the function passed to new Promise |
| 86 | + // is cached so we avoid allocating a new |
| 87 | + // closure at every run |
| 88 | + this[kHandlePromise] = (resolve, reject) => { |
| 89 | + const data = this[kStream].read(); |
| 90 | + if (data) { |
| 91 | + this[kLastPromise] = null; |
| 92 | + this[kLastResolve] = null; |
| 93 | + this[kLastReject] = null; |
| 94 | + resolve(new AsyncIteratorRecord(data, false)); |
| 95 | + } else { |
| 96 | + this[kLastResolve] = resolve; |
| 97 | + this[kLastReject] = reject; |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + get stream() { |
| 103 | + return this[kStream]; |
| 104 | + } |
| 105 | + |
| 106 | + next() { |
| 107 | + // if we have detected an error in the meanwhile |
| 108 | + // reject straight away |
| 109 | + const error = this[kError]; |
| 110 | + if (error !== null) { |
| 111 | + return Promise.reject(error); |
| 112 | + } |
| 113 | + |
| 114 | + if (this[kEnded]) { |
| 115 | + return Promise.resolve(new AsyncIteratorRecord(null, true)); |
| 116 | + } |
| 117 | + |
| 118 | + // if we have multiple next() calls |
| 119 | + // we will wait for the previous Promise to finish |
| 120 | + // this logic is optimized to support for await loops, |
| 121 | + // where next() is only called once at a time |
| 122 | + const lastPromise = this[kLastPromise]; |
| 123 | + let promise; |
| 124 | + |
| 125 | + if (lastPromise) { |
| 126 | + promise = new Promise(wrapForNext(lastPromise, this)); |
| 127 | + } else { |
| 128 | + // fast path needed to support multiple this.push() |
| 129 | + // without triggering the next() queue |
| 130 | + const data = this[kStream].read(); |
| 131 | + if (data !== null) { |
| 132 | + return Promise.resolve(new AsyncIteratorRecord(data, false)); |
| 133 | + } |
| 134 | + |
| 135 | + promise = new Promise(this[kHandlePromise]); |
| 136 | + } |
| 137 | + |
| 138 | + this[kLastPromise] = promise; |
| 139 | + |
| 140 | + return promise; |
| 141 | + } |
| 142 | + |
| 143 | + return() { |
| 144 | + // destroy(err, cb) is a private API |
| 145 | + // we can guarantee we have that here, because we control the |
| 146 | + // Readable class this is attached to |
| 147 | + return new Promise((resolve, reject) => { |
| 148 | + this[kStream].destroy(null, (err) => { |
| 149 | + if (err) { |
| 150 | + reject(err); |
| 151 | + return; |
| 152 | + } |
| 153 | + resolve(new AsyncIteratorRecord(null, true)); |
| 154 | + }); |
| 155 | + }); |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +module.exports = ReadableAsyncIterator; |
0 commit comments