node/lib/stream/promises.js

52 lines
1013 B
JavaScript
Raw Normal View History

'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;
const lastArg = streams[streams.length - 1];
if (lastArg && typeof lastArg === 'object' &&
!isNodeStream(lastArg) && !isIterable(lastArg)) {
const options = ArrayPrototypePop(streams);
signal = options.signal;
}
pl(streams, (err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
}, { signal });
});
}
function finished(stream, opts) {
return new Promise((resolve, reject) => {
eos(stream, opts, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
module.exports = {
finished,
pipeline,
};