|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const pipeline = require('internal/streams/pipeline'); |
| 4 | +const Duplex = require('internal/streams/duplex'); |
| 5 | +const { destroyer } = require('internal/streams/destroy'); |
| 6 | +const { |
| 7 | + isNodeStream, |
| 8 | + isReadable, |
| 9 | + isWritable, |
| 10 | +} = require('internal/streams/utils'); |
| 11 | +const { |
| 12 | + AbortError, |
| 13 | + codes: { |
| 14 | + ERR_INVALID_ARG_VALUE, |
| 15 | + ERR_MISSING_ARGS, |
| 16 | + }, |
| 17 | +} = require('internal/errors'); |
| 18 | + |
| 19 | +// This is needed for pre node 17. |
| 20 | +class ComposeDuplex extends Duplex { |
| 21 | + constructor(options) { |
| 22 | + super(options); |
| 23 | + |
| 24 | + // https://github.com/nodejs/node/pull/34385 |
| 25 | + |
| 26 | + if (options?.readable === false) { |
| 27 | + this._readableState.readable = false; |
| 28 | + this._readableState.ended = true; |
| 29 | + this._readableState.endEmitted = true; |
| 30 | + } |
| 31 | + |
| 32 | + if (options?.writable === false) { |
| 33 | + this._writableState.writable = false; |
| 34 | + this._writableState.ending = true; |
| 35 | + this._writableState.ended = true; |
| 36 | + this._writableState.finished = true; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +module.exports = function compose(...streams) { |
| 42 | + if (streams.length === 0) { |
| 43 | + throw new ERR_MISSING_ARGS('streams'); |
| 44 | + } |
| 45 | + |
| 46 | + if (streams.length === 1) { |
| 47 | + return Duplex.from(streams[0]); |
| 48 | + } |
| 49 | + |
| 50 | + const orgStreams = [...streams]; |
| 51 | + |
| 52 | + if (typeof streams[0] === 'function') { |
| 53 | + streams[0] = Duplex.from(streams[0]); |
| 54 | + } |
| 55 | + |
| 56 | + if (typeof streams[streams.length - 1] === 'function') { |
| 57 | + const idx = streams.length - 1; |
| 58 | + streams[idx] = Duplex.from(streams[idx]); |
| 59 | + } |
| 60 | + |
| 61 | + for (let n = 0; n < streams.length; ++n) { |
| 62 | + if (!isNodeStream(streams[n])) { |
| 63 | + // TODO(ronag): Add checks for non streams. |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (n < streams.length - 1 && !isReadable(streams[n])) { |
| 67 | + throw new ERR_INVALID_ARG_VALUE( |
| 68 | + `streams[${n}]`, |
| 69 | + orgStreams[n], |
| 70 | + 'must be readable' |
| 71 | + ); |
| 72 | + } |
| 73 | + if (n > 0 && !isWritable(streams[n])) { |
| 74 | + throw new ERR_INVALID_ARG_VALUE( |
| 75 | + `streams[${n}]`, |
| 76 | + orgStreams[n], |
| 77 | + 'must be writable' |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + let ondrain; |
| 83 | + let onfinish; |
| 84 | + let onreadable; |
| 85 | + let onclose; |
| 86 | + let d; |
| 87 | + |
| 88 | + function onfinished(err) { |
| 89 | + const cb = onclose; |
| 90 | + onclose = null; |
| 91 | + |
| 92 | + if (cb) { |
| 93 | + cb(err); |
| 94 | + } else if (err) { |
| 95 | + d.destroy(err); |
| 96 | + } else if (!readable && !writable) { |
| 97 | + d.destroy(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + const head = streams[0]; |
| 102 | + const tail = pipeline(streams, onfinished); |
| 103 | + |
| 104 | + const writable = !!isWritable(head); |
| 105 | + const readable = !!isReadable(tail); |
| 106 | + |
| 107 | + // TODO(ronag): Avoid double buffering. |
| 108 | + // Implement Writable/Readable/Duplex traits. |
| 109 | + // See, https://github.com/nodejs/node/pull/33515. |
| 110 | + d = new ComposeDuplex({ |
| 111 | + // TODO (ronag): highWaterMark? |
| 112 | + writableObjectMode: !!head?.writableObjectMode, |
| 113 | + readableObjectMode: !!tail?.writableObjectMode, |
| 114 | + writable, |
| 115 | + readable, |
| 116 | + }); |
| 117 | + |
| 118 | + if (writable) { |
| 119 | + d._write = function(chunk, encoding, callback) { |
| 120 | + if (head.write(chunk, encoding)) { |
| 121 | + callback(); |
| 122 | + } else { |
| 123 | + ondrain = callback; |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + d._final = function(callback) { |
| 128 | + head.end(); |
| 129 | + onfinish = callback; |
| 130 | + }; |
| 131 | + |
| 132 | + head.on('drain', function() { |
| 133 | + if (ondrain) { |
| 134 | + const cb = ondrain; |
| 135 | + ondrain = null; |
| 136 | + cb(); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + tail.on('finish', function() { |
| 141 | + if (onfinish) { |
| 142 | + const cb = onfinish; |
| 143 | + onfinish = null; |
| 144 | + cb(); |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + if (readable) { |
| 150 | + tail.on('readable', function() { |
| 151 | + if (onreadable) { |
| 152 | + const cb = onreadable; |
| 153 | + onreadable = null; |
| 154 | + cb(); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + tail.on('end', function() { |
| 159 | + d.push(null); |
| 160 | + }); |
| 161 | + |
| 162 | + d._read = function() { |
| 163 | + while (true) { |
| 164 | + const buf = tail.read(); |
| 165 | + |
| 166 | + if (buf === null) { |
| 167 | + onreadable = d._read; |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + if (!d.push(buf)) { |
| 172 | + return; |
| 173 | + } |
| 174 | + } |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + d._destroy = function(err, callback) { |
| 179 | + if (!err && onclose !== null) { |
| 180 | + err = new AbortError(); |
| 181 | + } |
| 182 | + |
| 183 | + onreadable = null; |
| 184 | + ondrain = null; |
| 185 | + onfinish = null; |
| 186 | + |
| 187 | + if (onclose === null) { |
| 188 | + callback(err); |
| 189 | + } else { |
| 190 | + onclose = callback; |
| 191 | + destroyer(tail, err); |
| 192 | + } |
| 193 | + }; |
| 194 | + |
| 195 | + return d; |
| 196 | +}; |
0 commit comments