All files / lib/internal/streams utils.js

94.13% Statements 273/290
86.95% Branches 160/184
100% Functions 22/22
94.13% Lines 273/290

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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 29123x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 4336x 4336x 4336x 4336x 4336x 4274x 4274x 68x 4336x 4336x 4336x 4336x 4336x 23x 2745x 2745x 2745x 2745x 2745x 2745x 2745x 2745x 23x 30x 30x 30x 30x 30x 3x 30x 30x 23x 7625x 7625x 7625x 7624x 7624x 7624x 7624x 142x 7624x 7625x 7625x 23x 631x 631x 631x 631x 629x 631x 631x 23x 1795x 1795x 1795x 1795x 1795x 1795x 1795x 23x 23x 236x 236x 236x 225x 236x 236x 211x 236x 23x 23x 1146x 1146x 1146x 502x 1146x 1146x 450x 450x 450x 1146x 1146x 23x 23x 15x 15x 15x 15x 15x 15x 13x 15x 23x 23x 1747x 1747x 1622x 1747x 1747x 1571x 1593x 1566x 1747x 1747x 23x 902x 902x 902x 889x 837x 902x 902x 902x 23x 901x 901x 901x 274x 901x 901x 901x 23x 147x 147x     147x 147x     147x 147x 102x 102x 45x 147x 4x 4x 41x 41x 147x 23x 1023x 1023x     1023x 1023x     1023x 1023x 1023x 23x 884x 884x     884x 884x     884x 884x 884x 23x 1115x 1115x     1115x 1115x 1076x 1076x 39x 39x 39x 39x 39x 1115x 39x 1115x     39x 1115x 22x 22x 17x 17x 1115x 23x 34x 34x 34x 34x 34x 34x 34x 34x 23x 38x 38x 38x 12x 38x 38x 23x 207x 207x 207x 207x 4x 207x 207x 23x 1115x 1115x 1115x 1115x 1115x 1115x 1115x 1115x 1103x 1103x 1103x 964x 1115x 1115x 23x 249x 249x 249x 8x 249x 249x 23x 228x 228x 228x 228x 228x 228x 228x 228x   228x 228x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x  
'use strict';
 
const {
  Symbol,
  SymbolAsyncIterator,
  SymbolIterator,
} = primordials;
 
const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
const kIsDisturbed = Symbol('kIsDisturbed');
 
function isReadableNodeStream(obj, strict = false) {
  return !!(
    obj &&
    typeof obj.pipe === 'function' &&
    typeof obj.on === 'function' &&
    (
      !strict ||
      (typeof obj.pause === 'function' && typeof obj.resume === 'function')
    ) &&
    (!obj._writableState || obj._readableState?.readable !== false) && // Duplex
    (!obj._writableState || obj._readableState) // Writable has .pipe.
  );
}
 
function isWritableNodeStream(obj) {
  return !!(
    obj &&
    typeof obj.write === 'function' &&
    typeof obj.on === 'function' &&
    (!obj._readableState || obj._writableState?.writable !== false) // Duplex
  );
}
 
function isDuplexNodeStream(obj) {
  return !!(
    obj &&
    (typeof obj.pipe === 'function' && obj._readableState) &&
    typeof obj.on === 'function' &&
    typeof obj.write === 'function'
  );
}
 
function isNodeStream(obj) {
  return (
    obj &&
    (
      obj._readableState ||
      obj._writableState ||
      (typeof obj.write === 'function' && typeof obj.on === 'function') ||
      (typeof obj.pipe === 'function' && typeof obj.on === 'function')
    )
  );
}
 
function isIterable(obj, isAsync) {
  if (obj == null) return false;
  if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
  if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
  return typeof obj[SymbolAsyncIterator] === 'function' ||
    typeof obj[SymbolIterator] === 'function';
}
 
function isDestroyed(stream) {
  if (!isNodeStream(stream)) return null;
  const wState = stream._writableState;
  const rState = stream._readableState;
  const state = wState || rState;
  return !!(stream.destroyed || stream[kDestroyed] || state?.destroyed);
}
 
// Have been end():d.
function isWritableEnded(stream) {
  if (!isWritableNodeStream(stream)) return null;
  if (stream.writableEnded === true) return true;
  const wState = stream._writableState;
  if (wState?.errored) return false;
  if (typeof wState?.ended !== 'boolean') return null;
  return wState.ended;
}
 
// Have emitted 'finish'.
function isWritableFinished(stream, strict) {
  if (!isWritableNodeStream(stream)) return null;
  if (stream.writableFinished === true) return true;
  const wState = stream._writableState;
  if (wState?.errored) return false;
  if (typeof wState?.finished !== 'boolean') return null;
  return !!(
    wState.finished ||
    (strict === false && wState.ended === true && wState.length === 0)
  );
}
 
// Have been push(null):d.
function isReadableEnded(stream) {
  if (!isReadableNodeStream(stream)) return null;
  if (stream.readableEnded === true) return true;
  const rState = stream._readableState;
  if (!rState || rState.errored) return false;
  if (typeof rState?.ended !== 'boolean') return null;
  return rState.ended;
}
 
// Have emitted 'end'.
function isReadableFinished(stream, strict) {
  if (!isReadableNodeStream(stream)) return null;
  const rState = stream._readableState;
  if (rState?.errored) return false;
  if (typeof rState?.endEmitted !== 'boolean') return null;
  return !!(
    rState.endEmitted ||
    (strict === false && rState.ended === true && rState.length === 0)
  );
}
 
function isReadable(stream) {
  if (stream && stream[kIsReadable] != null) return stream[kIsReadable];
  if (typeof stream?.readable !== 'boolean') return null;
  if (isDestroyed(stream)) return false;
  return isReadableNodeStream(stream) &&
    stream.readable &&
    !isReadableFinished(stream);
}
 
function isWritable(stream) {
  if (typeof stream?.writable !== 'boolean') return null;
  if (isDestroyed(stream)) return false;
  return isWritableNodeStream(stream) &&
    stream.writable &&
    !isWritableEnded(stream);
}
 
function isFinished(stream, opts) {
  if (!isNodeStream(stream)) {
    return null;
  }
 
  if (isDestroyed(stream)) {
    return true;
  }
 
  if (opts?.readable !== false && isReadable(stream)) {
    return false;
  }
 
  if (opts?.writable !== false && isWritable(stream)) {
    return false;
  }
 
  return true;
}
 
function isWritableErrored(stream) {
  if (!isNodeStream(stream)) {
    return null;
  }
 
  if (stream.writableErrored) {
    return stream.writableErrored;
  }
 
  return stream._writableState?.errored ?? null;
}
 
function isReadableErrored(stream) {
  if (!isNodeStream(stream)) {
    return null;
  }
 
  if (stream.readableErrored) {
    return stream.readableErrored;
  }
 
  return stream._readableState?.errored ?? null;
}
 
function isClosed(stream) {
  if (!isNodeStream(stream)) {
    return null;
  }
 
  if (typeof stream.closed === 'boolean') {
    return stream.closed;
  }
 
  const wState = stream._writableState;
  const rState = stream._readableState;
 
  if (
    typeof wState?.closed === 'boolean' ||
    typeof rState?.closed === 'boolean'
  ) {
    return wState?.closed || rState?.closed;
  }
 
  if (typeof stream._closed === 'boolean' && isOutgoingMessage(stream)) {
    return stream._closed;
  }
 
  return null;
}
 
function isOutgoingMessage(stream) {
  return (
    typeof stream._closed === 'boolean' &&
    typeof stream._defaultKeepAlive === 'boolean' &&
    typeof stream._removedConnection === 'boolean' &&
    typeof stream._removedContLen === 'boolean'
  );
}
 
function isServerResponse(stream) {
  return (
    typeof stream._sent100 === 'boolean' &&
    isOutgoingMessage(stream)
  );
}
 
function isServerRequest(stream) {
  return (
    typeof stream._consuming === 'boolean' &&
    typeof stream._dumped === 'boolean' &&
    stream.req?.upgradeOrConnect === undefined
  );
}
 
function willEmitClose(stream) {
  if (!isNodeStream(stream)) return null;
 
  const wState = stream._writableState;
  const rState = stream._readableState;
  const state = wState || rState;
 
  return (!state && isServerResponse(stream)) || !!(
    state &&
    state.autoDestroy &&
    state.emitClose &&
    state.closed === false
  );
}
 
function isDisturbed(stream) {
  return !!(stream && (
    stream[kIsDisturbed] ??
    (stream.readableDidRead || stream.readableAborted)
  ));
}
 
function isErrored(stream) {
  return !!(stream && (
    stream[kIsErrored] ??
    stream.readableErrored ??
    stream.writableErrored ??
    stream._readableState?.errorEmitted ??
    stream._writableState?.errorEmitted ??
    stream._readableState?.errored ??
    stream._writableState?.errored
  ));
}
 
module.exports = {
  kDestroyed,
  isDisturbed,
  kIsDisturbed,
  isErrored,
  kIsErrored,
  isReadable,
  kIsReadable,
  isClosed,
  isDestroyed,
  isDuplexNodeStream,
  isFinished,
  isIterable,
  isReadableNodeStream,
  isReadableEnded,
  isReadableFinished,
  isReadableErrored,
  isNodeStream,
  isWritable,
  isWritableNodeStream,
  isWritableEnded,
  isWritableFinished,
  isWritableErrored,
  isServerRequest,
  isServerResponse,
  willEmitClose,
};