All files / lib/internal/streams destroy.js

97.37% Statements 370/380
94.66% Branches 124/131
100% Functions 13/13
97.37% Lines 370/380

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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 38119x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 201168x 201168x 4267x 4267x 4267x 4267x 1372x 1372x 4267x 2758x 2758x 4267x 201168x 19x 19x 19x 114512x 114512x 114512x 114512x 114512x 114512x 114512x 13919x 2x 2x 13919x 13919x 13919x 100593x 100593x 100593x 100593x 100593x 100593x 102172x 81289x 81289x 102190x 100316x 100316x 100593x 100593x 114512x 123x 123x 123x 114497x 100470x 100470x 100593x 100593x 114512x 19x 100593x 100593x 100593x 100575x 100575x 100575x 100575x 100575x 100575x 100575x 100575x 81285x 81285x 100575x 100300x 100300x 100575x 100575x 7x 7x 100575x 100575x 1492x 100486x 99083x 99083x 100593x 100593x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 2x 2x 2x     2x 100593x 19x 1493x 1493x 1493x 1493x 19x 100507x 100507x 100507x 100507x 100507x 81217x 81217x 100507x 100235x 100235x 100507x 100507x 64505x 64505x 100507x 19x 1512x 1512x 1512x 1512x 1512x 3x 3x 1509x 1509x 1431x 1431x 1509x 1428x 1428x 1509x 1509x 1512x 19x 42862x 42862x 42862x 42862x 42862x 42861x 42861x 42861x 42861x 42861x 42861x 42861x 42861x 42861x 42861x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 42862x 19x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 20x 20x 118x 138x 138x 19x 19x 19x 19x 19x 11x 11x 19x 11x 11x 19x 7x 19x 12x 12x 19x 138x 19x 193350x 193350x 191023x 191023x 2327x 2327x 2327x 2327x 193330x 2250x 2250x 193350x 82x 82x 2327x 2327x 2327x 2884x 5x 5x 5x 2322x 2322x 193350x 19x 2321x 2321x 2321x 2321x 2321x 2321x 2321x 2321x 2319x 2242x 2242x 2319x 80x 80x 2319x 2319x 2x 2319x 2317x 2317x 2319x 2319x 123x 2316x 16x 2195x 2180x 2180x 2321x 2321x 3x 3x 3x 3x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x   2x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 3x 3x 3x     3x 2321x 19x 2181x 2181x 2181x 19x 377x 377x 377x 19x 19x 189x 189x 189x 189x 65x 189x 19x 19x 19x 19x 19x 19x 19x 19x  
'use strict';
 
const {
  aggregateTwoErrors,
  codes: {
    ERR_MULTIPLE_CALLBACK,
  },
} = require('internal/errors');
const {
  Symbol,
} = primordials;
 
const kDestroy = Symbol('kDestroy');
const kConstruct = Symbol('kConstruct');
 
function checkError(err, w, r) {
  if (err) {
    // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
    err.stack; // eslint-disable-line no-unused-expressions
 
    if (w && !w.errored) {
      w.errored = err;
    }
    if (r && !r.errored) {
      r.errored = err;
    }
  }
}
 
// Backwards compat. cb() is undocumented and unused in core but
// unfortunately might be used by modules.
function destroy(err, cb) {
  const r = this._readableState;
  const w = this._writableState;
  // With duplex streams we use the writable side for state.
  const s = w || r;
 
  if ((w && w.destroyed) || (r && r.destroyed)) {
    if (typeof cb === 'function') {
      cb();
    }
 
    return this;
  }
 
 
  // We set destroyed to true before firing error callbacks in order
  // to make it re-entrance safe in case destroy() is called within callbacks
  checkError(err, w, r);
 
  if (w) {
    w.destroyed = true;
  }
  if (r) {
    r.destroyed = true;
  }
 
  // If still constructing then defer calling _destroy.
  if (!s.constructed) {
    this.once(kDestroy, function(er) {
      _destroy(this, aggregateTwoErrors(er, err), cb);
    });
  } else {
    _destroy(this, err, cb);
  }
 
  return this;
}
 
function _destroy(self, err, cb) {
  let called = false;
  const result = self._destroy(err || null, (err) => {
    const r = self._readableState;
    const w = self._writableState;
 
    called = true;
 
    checkError(err, w, r);
 
    if (w) {
      w.closed = true;
    }
    if (r) {
      r.closed = true;
    }
 
    if (typeof cb === 'function') {
      cb(err);
    }
 
    if (err) {
      process.nextTick(emitErrorCloseNT, self, err);
    } else {
      process.nextTick(emitCloseNT, self);
    }
  });
  if (result !== undefined && result !== null) {
    try {
      const then = result.then;
      if (typeof then === 'function') {
        then.call(
          result,
          function() {
            if (called)
              return;
 
            const r = self._readableState;
            const w = self._writableState;
 
            if (w) {
              w.closed = true;
            }
            if (r) {
              r.closed = true;
            }
 
            if (typeof cb === 'function') {
              process.nextTick(cb);
            }
 
            process.nextTick(emitCloseNT, self);
          },
          function(err) {
            const r = self._readableState;
            const w = self._writableState;
            err.stack; // eslint-disable-line no-unused-expressions
 
            called = true;
 
            if (w && !w.errored) {
              w.errored = err;
            }
            if (r && !r.errored) {
              r.errored = err;
            }
 
            if (w) {
              w.closed = true;
            }
            if (r) {
              r.closed = true;
            }
 
            if (typeof cb === 'function') {
              process.nextTick(cb, err);
            }
 
            process.nextTick(emitErrorCloseNT, self, err);
          });
      }
    } catch (err) {
      process.nextTick(emitErrorNT, self, err);
    }
  }
}
 
function emitErrorCloseNT(self, err) {
  emitErrorNT(self, err);
  emitCloseNT(self);
}
 
function emitCloseNT(self) {
  const r = self._readableState;
  const w = self._writableState;
 
  if (w) {
    w.closeEmitted = true;
  }
  if (r) {
    r.closeEmitted = true;
  }
 
  if ((w && w.emitClose) || (r && r.emitClose)) {
    self.emit('close');
  }
}
 
function emitErrorNT(self, err) {
  const r = self._readableState;
  const w = self._writableState;
 
  if ((w && w.errorEmitted) || (r && r.errorEmitted)) {
    return;
  }
 
  if (w) {
    w.errorEmitted = true;
  }
  if (r) {
    r.errorEmitted = true;
  }
 
  self.emit('error', err);
}
 
function undestroy() {
  const r = this._readableState;
  const w = this._writableState;
 
  if (r) {
    r.constructed = true;
    r.closed = false;
    r.closeEmitted = false;
    r.destroyed = false;
    r.errored = null;
    r.errorEmitted = false;
    r.reading = false;
    r.ended = false;
    r.endEmitted = false;
  }
 
  if (w) {
    w.constructed = true;
    w.destroyed = false;
    w.closed = false;
    w.closeEmitted = false;
    w.errored = null;
    w.errorEmitted = false;
    w.ended = false;
    w.ending = false;
    w.finalCalled = false;
    w.prefinished = false;
    w.finished = false;
  }
}
 
function errorOrDestroy(stream, err, sync) {
  // We have tests that rely on errors being emitted
  // in the same tick, so changing this is semver major.
  // For now when you opt-in to autoDestroy we allow
  // the error to be emitted nextTick. In a future
  // semver major update we should change the default to this.
 
  const r = stream._readableState;
  const w = stream._writableState;
 
  if ((w && w.destroyed) || (r && r.destroyed)) {
    return this;
  }
 
  if ((r && r.autoDestroy) || (w && w.autoDestroy))
    stream.destroy(err);
  else if (err) {
    // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
    err.stack; // eslint-disable-line no-unused-expressions
 
    if (w && !w.errored) {
      w.errored = err;
    }
    if (r && !r.errored) {
      r.errored = err;
    }
    if (sync) {
      process.nextTick(emitErrorNT, stream, err);
    } else {
      emitErrorNT(stream, err);
    }
  }
}
 
function construct(stream, cb) {
  if (typeof stream._construct !== 'function') {
    return;
  }
 
  const r = stream._readableState;
  const w = stream._writableState;
 
  if (r) {
    r.constructed = false;
  }
  if (w) {
    w.constructed = false;
  }
 
  stream.once(kConstruct, cb);
 
  if (stream.listenerCount(kConstruct) > 1) {
    // Duplex
    return;
  }
 
  process.nextTick(constructNT, stream);
}
 
function constructNT(stream) {
  const r = stream._readableState;
  const w = stream._writableState;
  // With duplex streams we use the writable side for state.
  const s = w || r;
 
  let called = false;
  const result = stream._construct((err) => {
    if (r) {
      r.constructed = true;
    }
    if (w) {
      w.constructed = true;
    }
 
    if (called) {
      err = new ERR_MULTIPLE_CALLBACK();
    } else {
      called = true;
    }
 
    if (s.destroyed) {
      stream.emit(kDestroy, err);
    } else if (err) {
      errorOrDestroy(stream, err, true);
    } else {
      process.nextTick(emitConstructNT, stream);
    }
  });
  if (result !== undefined && result !== null) {
    try {
      const then = result.then;
      if (typeof then === 'function') {
        then.call(
          result,
          function() {
            // If the callback was invoked, do nothing further.
            if (called)
              return;
            if (r) {
              r.constructed = true;
            }
            if (w) {
              w.constructed = true;
            }
            if (s.destroyed) {
              process.nextTick(() => stream.emit(kDestroy));
            } else {
              process.nextTick(emitConstructNT, stream);
            }
          },
          function(err) {
            if (r) {
              r.constructed = true;
            }
            if (w) {
              w.constructed = true;
            }
            called = true;
            if (s.destroyed) {
              process.nextTick(() => stream.emit(kDestroy, err));
            } else {
              process.nextTick(errorOrDestroy, stream, err);
            }
          });
      }
    } catch (err) {
      process.nextTick(emitErrorNT, stream, err);
    }
  }
}
 
function emitConstructNT(stream) {
  stream.emit(kConstruct);
}
 
function isRequest(stream) {
  return stream && stream.setHeader && typeof stream.abort === 'function';
}
 
// Normalize destroy for legacy.
function destroyer(stream, err) {
  if (isRequest(stream)) return stream.abort();
  if (isRequest(stream.req)) return stream.req.abort();
  if (typeof stream.destroy === 'function') return stream.destroy(err);
  if (typeof stream.close === 'function') return stream.close();
}
 
module.exports = {
  construct,
  destroyer,
  destroy,
  undestroy,
  errorOrDestroy
};