All files / lib/stream promises.js

100% Statements 63/63
100% Branches 16/16
100% Functions 2/2
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 6420x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 6x 6x 6x 4x 5x 2x 2x 6x 6x 3x 3x 6x 6x 20x 5x 5x 5x 5x 3x 4x 2x 2x 5x 5x 5x 20x 20x 20x 20x 20x  
'use strict';
 
const {
  ArrayPrototypePop,
  Promise,
} = primordials;
 
const {
  addAbortSignalNoValidate,
} = require('internal/streams/add-abort-signal');
 
const {
  validateAbortSignal,
} = require('internal/validators');
 
const {
  isIterable,
  isStream,
} = require('internal/streams/utils');
 
const 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' &&
        !isStream(lastArg) && !isIterable(lastArg)) {
      const options = ArrayPrototypePop(streams);
      signal = options.signal;
      validateAbortSignal(signal, 'options.signal');
    }
 
    const pipe = pl(...streams, (err, value) => {
      if (err) {
        reject(err);
      } else {
        resolve(value);
      }
    });
    if (signal) {
      addAbortSignalNoValidate(signal, pipe);
    }
  });
}
 
function finished(stream, opts) {
  return new Promise((resolve, reject) => {
    eos(stream, opts, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
}
 
module.exports = {
  finished,
  pipeline,
};