node/lib/stream/promises.js
Robert Nagy e110c96f48
stream: pipeline with end option
Currently pipeline cannot fully replace pipe due
to the missing end option. This PR adds the end
option to the promisified pipeline method.

PR-URL: https://github.com/nodejs/node/pull/40886
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-01-31 22:59:26 -05:00

53 lines
1 KiB
JavaScript

'use strict';
const {
ArrayPrototypePop,
Promise,
} = primordials;
const {
isIterable,
isNodeStream,
} = require('internal/streams/utils');
const { pipelineImpl: pl } = require('internal/streams/pipeline');
const eos = require('internal/streams/end-of-stream');
function pipeline(...streams) {
return new Promise((resolve, reject) => {
let signal;
let end;
const lastArg = streams[streams.length - 1];
if (lastArg && typeof lastArg === 'object' &&
!isNodeStream(lastArg) && !isIterable(lastArg)) {
const options = ArrayPrototypePop(streams);
signal = options.signal;
end = options.end;
}
pl(streams, (err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
}, { signal, end });
});
}
function finished(stream, opts) {
return new Promise((resolve, reject) => {
eos(stream, opts, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
module.exports = {
finished,
pipeline,
};