|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const console = require('console'); |
| 4 | +const { AbortController } = require('internal/abort_controller'); |
3 | 5 | const { AbortError } = require('internal/errors');
|
4 |
| -const compose = require('internal/streams/compose'); |
| 6 | +const Readable = require('internal/streams/readable'); |
| 7 | +const eos = require('internal/streams/end-of-stream'); |
5 | 8 |
|
6 |
| -module.exports.map = function map(stream, fn) { |
7 |
| - return compose(stream, async function* (source, { signal }) { |
8 |
| - for await (const item of source) { |
9 |
| - if (signal.aborted) { |
10 |
| - throw new AbortError('The iteration has been interrupted'); |
| 9 | +module.exports.map = function map(stream, fn, options) { |
| 10 | + let concurrency = 1; |
| 11 | + if (Number.isFinite(options)) { |
| 12 | + concurrency = options; |
| 13 | + } else if (options && Number.isFinite(options.concurrency)) { |
| 14 | + concurrency = options.concurrency; |
| 15 | + } |
| 16 | + |
| 17 | + let highWaterMark = 1; |
| 18 | + if (options && Number.isFinite(options.highWaterMark)) { |
| 19 | + highWaterMark = options.highWaterMark; |
| 20 | + } |
| 21 | + |
| 22 | + // TODO: Argument validation |
| 23 | + // TODO: in order and out of order options? |
| 24 | + |
| 25 | + const ac = new AbortController(); |
| 26 | + const signal = ac.signal; |
| 27 | + const queue = []; |
| 28 | + |
| 29 | + let reading = false; |
| 30 | + let waiting = false; |
| 31 | + |
| 32 | + const ret = new Readable({ |
| 33 | + objectMode: stream.readableObjectMode ?? stream.objectMode ?? true, |
| 34 | + highWaterMark: Math.max(0, highWaterMark - concurrency), |
| 35 | + read () { |
| 36 | + if (!reading) { |
| 37 | + read(); |
| 38 | + } |
| 39 | + }, |
| 40 | + destroy (err, callback) { |
| 41 | + if (!err && !this.readableEnded) { |
| 42 | + err = new AbortError(); |
| 43 | + } |
| 44 | + ac.abort(); |
| 45 | + callback(err); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + async function read () { |
| 50 | + try { |
| 51 | + waiting = false; |
| 52 | + reading = true; |
| 53 | + while (queue.length && !ret.destroyed) { |
| 54 | + const [err, val] = await queue.shift(); |
| 55 | + if (err) { |
| 56 | + ret.destroy(err); |
| 57 | + } else if (!ret.push(val)) { |
| 58 | + waiting = true; |
| 59 | + break; |
| 60 | + } else { |
| 61 | + pump(); |
| 62 | + } |
| 63 | + } |
| 64 | + reading = false; |
| 65 | + } catch (err) { |
| 66 | + ret.destroy(err); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + async function wrap (val) { |
| 71 | + try { |
| 72 | + return [null, await fn(val, { signal })]; |
| 73 | + } catch (err) { |
| 74 | + return [err, null]; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + function enqueue(val) { |
| 79 | + queue.push(val); |
| 80 | + if (waiting) { |
| 81 | + waiting = false; |
| 82 | + read(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + function pump () { |
| 87 | + while (true) { |
| 88 | + const val = stream.read(); |
| 89 | + if (val === null) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + enqueue(wrap(val)); |
| 94 | + |
| 95 | + if (queue.length === concurrency) { |
| 96 | + return; |
11 | 97 | }
|
12 |
| - yield await fn(item, { signal }); |
13 | 98 | }
|
| 99 | + } |
| 100 | + |
| 101 | + eos(stream, (err) => { |
| 102 | + enqueue([err, null]); |
14 | 103 | });
|
| 104 | + |
| 105 | + process.nextTick(pump); |
| 106 | + |
| 107 | + stream.on('readable', pump); |
| 108 | + |
| 109 | + return ret; |
15 | 110 | };
|
0 commit comments