All files / lib/internal/streams end-of-stream.js

98.44% Statements 254/258
97.16% Branches 103/106
91.66% Functions 11/12
98.44% Lines 254/258

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 25922x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 1104x 1104x 1104x 22x 22x 22x 1109x 1109x 166x 166x 1109x 7x 942x 936x 936x 1108x 1108x 1108x 1108x 1108x 1109x 1109x 1109x 1109x 2x 2x 2x 1104x 1104x 1104x 1104x 1104x 22x 10x 10x 1104x 1104x 1104x 1104x 1104x 1104x 1108x 1109x 842x 1109x 1109x 1109x 1109x 239x 239x 239x 239x 239x 9x 9x 239x 239x 93x 93x 146x 239x 104x 104x 1109x 1109x 1109x 1109x 555x 555x 555x 555x 555x 1x 1x 555x 555x 429x 429x 126x 555x 108x 108x 1109x 1109x 1109x 326x 1109x 1109x 1109x 1109x 1109x 1015x 1015x 1015x 1015x 1015x 309x 309x 706x 1015x 58x 58x 57x 58x 1015x 31x 31x 29x 31x 620x 620x 1109x 1109x 1109x   1109x 1109x 1109x 10x 10x 10x 10x 10x   10x 10x 10x 1109x 22x 22x 22x 1104x 1104x 1109x 9x 9x 1104x 1104x 1104x 1104x 1104x 1104x 1104x 1104x 1109x 42x 1107x 1x 1x 1x 1062x 1061x 1061x 135x 1061x 1x 1061x 1060x 1060x 134x 1060x 3x 1060x     1104x 1104x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 155x 1104x 1104x 1109x 6x 5x 5x 5x 5x 5x 5x 6x 6x 2x 6x 4x 4x 4x 4x 4x 4x 4x 6x 1104x 1104x 1109x 22x 8x 8x 8x 8x 5x 7x 3x 3x 8x 8x 8x 22x 22x 22x  
// Ported from https://github.com/mafintosh/end-of-stream with
// permission from the author, Mathias Buus (@mafintosh).
 
'use strict';
 
const {
  AbortError,
  codes,
} = require('internal/errors');
const {
  ERR_INVALID_ARG_TYPE,
  ERR_STREAM_PREMATURE_CLOSE
} = codes;
const {
  kEmptyObject,
  once,
} = require('internal/util');
const {
  validateAbortSignal,
  validateFunction,
  validateObject,
} = require('internal/validators');
 
const { Promise } = primordials;
 
const {
  isClosed,
  isReadable,
  isReadableNodeStream,
  isReadableFinished,
  isReadableErrored,
  isWritable,
  isWritableNodeStream,
  isWritableFinished,
  isWritableErrored,
  isNodeStream,
  willEmitClose: _willEmitClose,
} = require('internal/streams/utils');
 
function isRequest(stream) {
  return stream.setHeader && typeof stream.abort === 'function';
}
 
const nop = () => {};
 
function eos(stream, options, callback) {
  if (arguments.length === 2) {
    callback = options;
    options = kEmptyObject;
  } else if (options == null) {
    options = kEmptyObject;
  } else {
    validateObject(options, 'options');
  }
  validateFunction(callback, 'callback');
  validateAbortSignal(options.signal, 'options.signal');
 
  callback = once(callback);
 
  const readable = options.readable ?? isReadableNodeStream(stream);
  const writable = options.writable ?? isWritableNodeStream(stream);
 
  if (!isNodeStream(stream)) {
    // TODO: Webstreams.
    throw new ERR_INVALID_ARG_TYPE('stream', 'Stream', stream);
  }
 
  const wState = stream._writableState;
  const rState = stream._readableState;
 
  const onlegacyfinish = () => {
    if (!stream.writable) {
      onfinish();
    }
  };
 
  // TODO (ronag): Improve soft detection to include core modules and
  // common ecosystem modules that do properly emit 'close' but fail
  // this generic check.
  let willEmitClose = (
    _willEmitClose(stream) &&
    isReadableNodeStream(stream) === readable &&
    isWritableNodeStream(stream) === writable
  );
 
  let writableFinished = isWritableFinished(stream, false);
  const onfinish = () => {
    writableFinished = true;
    // Stream should not be destroyed here. If it is that
    // means that user space is doing something differently and
    // we cannot trust willEmitClose.
    if (stream.destroyed) {
      willEmitClose = false;
    }
 
    if (willEmitClose && (!stream.readable || readable)) {
      return;
    }
 
    if (!readable || readableFinished) {
      callback.call(stream);
    }
  };
 
  let readableFinished = isReadableFinished(stream, false);
  const onend = () => {
    readableFinished = true;
    // Stream should not be destroyed here. If it is that
    // means that user space is doing something differently and
    // we cannot trust willEmitClose.
    if (stream.destroyed) {
      willEmitClose = false;
    }
 
    if (willEmitClose && (!stream.writable || writable)) {
      return;
    }
 
    if (!writable || writableFinished) {
      callback.call(stream);
    }
  };
 
  const onerror = (err) => {
    callback.call(stream, err);
  };
 
  let closed = isClosed(stream);
 
  const onclose = () => {
    closed = true;
 
    const errored = isWritableErrored(stream) || isReadableErrored(stream);
 
    if (errored && typeof errored !== 'boolean') {
      return callback.call(stream, errored);
    }
 
    if (readable && !readableFinished && isReadableNodeStream(stream, true)) {
      if (!isReadableFinished(stream, false))
        return callback.call(stream,
                             new ERR_STREAM_PREMATURE_CLOSE());
    }
    if (writable && !writableFinished) {
      if (!isWritableFinished(stream, false))
        return callback.call(stream,
                             new ERR_STREAM_PREMATURE_CLOSE());
    }
 
    callback.call(stream);
  };
 
  const onrequest = () => {
    stream.req.on('finish', onfinish);
  };
 
  if (isRequest(stream)) {
    stream.on('complete', onfinish);
    if (!willEmitClose) {
      stream.on('abort', onclose);
    }
    if (stream.req) {
      onrequest();
    } else {
      stream.on('request', onrequest);
    }
  } else if (writable && !wState) { // legacy streams
    stream.on('end', onlegacyfinish);
    stream.on('close', onlegacyfinish);
  }
 
  // Not all streams will emit 'close' after 'aborted'.
  if (!willEmitClose && typeof stream.aborted === 'boolean') {
    stream.on('aborted', onclose);
  }
 
  stream.on('end', onend);
  stream.on('finish', onfinish);
  if (options.error !== false) {
    stream.on('error', onerror);
  }
  stream.on('close', onclose);
 
  if (closed) {
    process.nextTick(onclose);
  } else if (wState?.errorEmitted || rState?.errorEmitted) {
    if (!willEmitClose) {
      process.nextTick(onclose);
    }
  } else if (
    !readable &&
    (!willEmitClose || isReadable(stream)) &&
    (writableFinished || isWritable(stream) === false)
  ) {
    process.nextTick(onclose);
  } else if (
    !writable &&
    (!willEmitClose || isWritable(stream)) &&
    (readableFinished || isReadable(stream) === false)
  ) {
    process.nextTick(onclose);
  } else if ((rState && stream.req && stream.aborted)) {
    process.nextTick(onclose);
  }
 
  const cleanup = () => {
    callback = nop;
    stream.removeListener('aborted', onclose);
    stream.removeListener('complete', onfinish);
    stream.removeListener('abort', onclose);
    stream.removeListener('request', onrequest);
    if (stream.req) stream.req.removeListener('finish', onfinish);
    stream.removeListener('end', onlegacyfinish);
    stream.removeListener('close', onlegacyfinish);
    stream.removeListener('finish', onfinish);
    stream.removeListener('end', onend);
    stream.removeListener('error', onerror);
    stream.removeListener('close', onclose);
  };
 
  if (options.signal && !closed) {
    const abort = () => {
      // Keep it because cleanup removes it.
      const endCallback = callback;
      cleanup();
      endCallback.call(
        stream,
        new AbortError(undefined, { cause: options.signal.reason }));
    };
    if (options.signal.aborted) {
      process.nextTick(abort);
    } else {
      const originalCallback = callback;
      callback = once((...args) => {
        options.signal.removeEventListener('abort', abort);
        originalCallback.apply(stream, args);
      });
      options.signal.addEventListener('abort', abort);
    }
  }
 
  return cleanup;
}
 
function finished(stream, opts) {
  return new Promise((resolve, reject) => {
    eos(stream, opts, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve();
      }
    });
  });
}
 
module.exports = eos;
module.exports.finished = finished;