| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | // undocumented cb() API, needed for core, not for public API
|
|---|
| 4 | function destroy(err, cb) {
|
|---|
| 5 | var _this = this;
|
|---|
| 6 | var readableDestroyed = this._readableState && this._readableState.destroyed;
|
|---|
| 7 | var writableDestroyed = this._writableState && this._writableState.destroyed;
|
|---|
| 8 | if (readableDestroyed || writableDestroyed) {
|
|---|
| 9 | if (cb) {
|
|---|
| 10 | cb(err);
|
|---|
| 11 | } else if (err) {
|
|---|
| 12 | if (!this._writableState) {
|
|---|
| 13 | process.nextTick(emitErrorNT, this, err);
|
|---|
| 14 | } else if (!this._writableState.errorEmitted) {
|
|---|
| 15 | this._writableState.errorEmitted = true;
|
|---|
| 16 | process.nextTick(emitErrorNT, this, err);
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 | return this;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | // we set destroyed to true before firing error callbacks in order
|
|---|
| 23 | // to make it re-entrance safe in case destroy() is called within callbacks
|
|---|
| 24 |
|
|---|
| 25 | if (this._readableState) {
|
|---|
| 26 | this._readableState.destroyed = true;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // if this is a duplex stream mark the writable part as destroyed as well
|
|---|
| 30 | if (this._writableState) {
|
|---|
| 31 | this._writableState.destroyed = true;
|
|---|
| 32 | }
|
|---|
| 33 | this._destroy(err || null, function (err) {
|
|---|
| 34 | if (!cb && err) {
|
|---|
| 35 | if (!_this._writableState) {
|
|---|
| 36 | process.nextTick(emitErrorAndCloseNT, _this, err);
|
|---|
| 37 | } else if (!_this._writableState.errorEmitted) {
|
|---|
| 38 | _this._writableState.errorEmitted = true;
|
|---|
| 39 | process.nextTick(emitErrorAndCloseNT, _this, err);
|
|---|
| 40 | } else {
|
|---|
| 41 | process.nextTick(emitCloseNT, _this);
|
|---|
| 42 | }
|
|---|
| 43 | } else if (cb) {
|
|---|
| 44 | process.nextTick(emitCloseNT, _this);
|
|---|
| 45 | cb(err);
|
|---|
| 46 | } else {
|
|---|
| 47 | process.nextTick(emitCloseNT, _this);
|
|---|
| 48 | }
|
|---|
| 49 | });
|
|---|
| 50 | return this;
|
|---|
| 51 | }
|
|---|
| 52 | function emitErrorAndCloseNT(self, err) {
|
|---|
| 53 | emitErrorNT(self, err);
|
|---|
| 54 | emitCloseNT(self);
|
|---|
| 55 | }
|
|---|
| 56 | function emitCloseNT(self) {
|
|---|
| 57 | if (self._writableState && !self._writableState.emitClose) return;
|
|---|
| 58 | if (self._readableState && !self._readableState.emitClose) return;
|
|---|
| 59 | self.emit('close');
|
|---|
| 60 | }
|
|---|
| 61 | function undestroy() {
|
|---|
| 62 | if (this._readableState) {
|
|---|
| 63 | this._readableState.destroyed = false;
|
|---|
| 64 | this._readableState.reading = false;
|
|---|
| 65 | this._readableState.ended = false;
|
|---|
| 66 | this._readableState.endEmitted = false;
|
|---|
| 67 | }
|
|---|
| 68 | if (this._writableState) {
|
|---|
| 69 | this._writableState.destroyed = false;
|
|---|
| 70 | this._writableState.ended = false;
|
|---|
| 71 | this._writableState.ending = false;
|
|---|
| 72 | this._writableState.finalCalled = false;
|
|---|
| 73 | this._writableState.prefinished = false;
|
|---|
| 74 | this._writableState.finished = false;
|
|---|
| 75 | this._writableState.errorEmitted = false;
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | function emitErrorNT(self, err) {
|
|---|
| 79 | self.emit('error', err);
|
|---|
| 80 | }
|
|---|
| 81 | function errorOrDestroy(stream, err) {
|
|---|
| 82 | // We have tests that rely on errors being emitted
|
|---|
| 83 | // in the same tick, so changing this is semver major.
|
|---|
| 84 | // For now when you opt-in to autoDestroy we allow
|
|---|
| 85 | // the error to be emitted nextTick. In a future
|
|---|
| 86 | // semver major update we should change the default to this.
|
|---|
| 87 |
|
|---|
| 88 | var rState = stream._readableState;
|
|---|
| 89 | var wState = stream._writableState;
|
|---|
| 90 | if (rState && rState.autoDestroy || wState && wState.autoDestroy) stream.destroy(err);else stream.emit('error', err);
|
|---|
| 91 | }
|
|---|
| 92 | module.exports = {
|
|---|
| 93 | destroy: destroy,
|
|---|
| 94 | undestroy: undestroy,
|
|---|
| 95 | errorOrDestroy: errorOrDestroy
|
|---|
| 96 | }; |
|---|