|
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 | + // TODO: Argument validation |
| 11 | + |
| 12 | + let concurrency = 1; |
| 13 | + if (Number.isFinite(options)) { |
| 14 | + concurrency = options; |
| 15 | + } else if (options && Number.isFinite(options.concurrency)) { |
| 16 | + concurrency = options.concurrency; |
| 17 | + } |
| 18 | + |
| 19 | + let highWaterMark = 1; |
| 20 | + if (options && Number.isFinite(options.highWaterMark)) { |
| 21 | + highWaterMark = options.highWaterMark; |
| 22 | + } |
| 23 | + highWaterMark = Math.max(0, highWaterMark - concurrency) |
| 24 | + |
| 25 | + let objectMode = stream.readableObjectMode ?? stream.objectMode ?? true; |
| 26 | + if (options && typeof options.objectMode === 'boolean') { |
| 27 | + objectMode = options.objectMode; |
| 28 | + } |
| 29 | + |
| 30 | + const ac = new AbortController(); |
| 31 | + const signal = ac.signal; |
| 32 | + const queue = []; |
| 33 | + |
| 34 | + let reading = false; |
| 35 | + |
| 36 | + const ret = new Readable({ |
| 37 | + objectMode, |
| 38 | + highWaterMark, |
| 39 | + read () { |
| 40 | + read(); |
| 41 | + }, |
| 42 | + destroy (err, callback) { |
| 43 | + if (!err && !this.readableEnded) { |
| 44 | + err = new AbortError(); |
11 | 45 | }
|
12 |
| - yield await fn(item, { signal }); |
| 46 | + ac.abort(); |
| 47 | + callback(err); |
13 | 48 | }
|
14 | 49 | });
|
| 50 | + |
| 51 | + async function read () { |
| 52 | + if (reading) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (ret.readableLength && ret.readableLength >= ret.readableHighWaterMark) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + reading = true; |
| 62 | + while (queue.length && !ret.destroyed) { |
| 63 | + const [err, val] = await queue.shift(); |
| 64 | + if (err) { |
| 65 | + ret.destroy(err); |
| 66 | + } else if (!ret.push(val)) { |
| 67 | + break; |
| 68 | + } else { |
| 69 | + pump(); |
| 70 | + } |
| 71 | + } |
| 72 | + reading = false; |
| 73 | + } catch (err) { |
| 74 | + ret.destroy(err); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + async function wrap (val) { |
| 79 | + try { |
| 80 | + return [null, await fn(val, { signal })]; |
| 81 | + } catch (err) { |
| 82 | + return [err, null]; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + function enqueue(val) { |
| 87 | + queue.push(val); |
| 88 | + read(); |
| 89 | + } |
| 90 | + |
| 91 | + function pump () { |
| 92 | + while (true) { |
| 93 | + const val = stream.read(); |
| 94 | + if (val === null) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + enqueue(wrap(val)); |
| 99 | + |
| 100 | + if (queue.length === concurrency) { |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + eos(stream, (err) => { |
| 107 | + enqueue([err, null]); |
| 108 | + }); |
| 109 | + |
| 110 | + process.nextTick(pump); |
| 111 | + |
| 112 | + stream.on('readable', pump); |
| 113 | + |
| 114 | + return ret; |
15 | 115 | };
|
0 commit comments