2017-05-06 14:20:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
function needError(stream, err) {
|
|
|
|
if (!err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = stream._readableState;
|
|
|
|
const w = stream._writableState;
|
|
|
|
|
|
|
|
if ((w && w.errorEmitted) || (r && r.errorEmitted)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w) {
|
|
|
|
w.errorEmitted = true;
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
r.errorEmitted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Undocumented cb() API, needed for core, not for public API
|
2017-05-06 14:20:52 +02:00
|
|
|
function destroy(err, cb) {
|
2019-07-16 00:03:23 +02:00
|
|
|
const r = this._readableState;
|
|
|
|
const w = this._writableState;
|
2017-05-06 14:20:52 +02:00
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if ((w && w.destroyed) || (r && r.destroyed)) {
|
2017-05-22 18:03:55 +02:00
|
|
|
if (cb) {
|
|
|
|
cb(err);
|
2019-07-16 00:03:23 +02:00
|
|
|
} else if (needError(this, err)) {
|
|
|
|
process.nextTick(emitErrorNT, this, err);
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
2019-02-12 19:24:39 +01:00
|
|
|
|
2017-06-07 12:48:35 -07:00
|
|
|
return this;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// We set destroyed to true before firing error callbacks in order
|
2017-05-06 14:20:52 +02:00
|
|
|
// to make it re-entrance safe in case destroy() is called within callbacks
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if (w) {
|
|
|
|
w.destroyed = true;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
2019-07-16 00:03:23 +02:00
|
|
|
if (r) {
|
|
|
|
r.destroyed = true;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._destroy(err || null, (err) => {
|
2019-07-16 00:03:23 +02:00
|
|
|
if (cb) {
|
2018-04-05 20:52:19 +02:00
|
|
|
process.nextTick(emitCloseNT, this);
|
2017-05-06 14:20:52 +02:00
|
|
|
cb(err);
|
2019-07-16 00:03:23 +02:00
|
|
|
} else if (needError(this, err)) {
|
|
|
|
process.nextTick(emitErrorAndCloseNT, this, err);
|
2018-04-05 20:52:19 +02:00
|
|
|
} else {
|
|
|
|
process.nextTick(emitCloseNT, this);
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
});
|
2017-06-07 12:48:35 -07:00
|
|
|
|
|
|
|
return this;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:52:19 +02:00
|
|
|
function emitErrorAndCloseNT(self, err) {
|
|
|
|
emitErrorNT(self, err);
|
|
|
|
emitCloseNT(self);
|
|
|
|
}
|
|
|
|
|
2018-01-29 19:32:34 +01:00
|
|
|
function emitCloseNT(self) {
|
2019-07-16 00:03:23 +02:00
|
|
|
const r = self._readableState;
|
|
|
|
const w = self._writableState;
|
|
|
|
|
|
|
|
if (w && !w.emitClose)
|
2018-01-29 19:32:34 +01:00
|
|
|
return;
|
2019-07-16 00:03:23 +02:00
|
|
|
if (r && !r.emitClose)
|
2018-01-29 19:32:34 +01:00
|
|
|
return;
|
|
|
|
self.emit('close');
|
|
|
|
}
|
|
|
|
|
2017-05-06 14:20:52 +02:00
|
|
|
function undestroy() {
|
2019-07-16 00:03:23 +02:00
|
|
|
const r = this._readableState;
|
|
|
|
const w = this._writableState;
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
r.destroyed = false;
|
|
|
|
r.reading = false;
|
|
|
|
r.ended = false;
|
|
|
|
r.endEmitted = false;
|
|
|
|
r.errorEmitted = false;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if (w) {
|
|
|
|
w.destroyed = false;
|
|
|
|
w.ended = false;
|
|
|
|
w.ending = false;
|
|
|
|
w.finalCalled = false;
|
|
|
|
w.prefinished = false;
|
|
|
|
w.finished = false;
|
|
|
|
w.errorEmitted = false;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function emitErrorNT(self, err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
2018-08-21 20:05:12 +02:00
|
|
|
function errorOrDestroy(stream, err) {
|
|
|
|
// 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.
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
const r = stream._readableState;
|
|
|
|
const w = stream._writableState;
|
2018-08-21 20:05:12 +02:00
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if ((r && r.autoDestroy) || (w && w.autoDestroy))
|
2018-08-21 20:05:12 +02:00
|
|
|
stream.destroy(err);
|
2019-07-16 00:03:23 +02:00
|
|
|
else if (needError(stream, err))
|
2018-08-21 20:05:12 +02:00
|
|
|
stream.emit('error', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-06 14:20:52 +02:00
|
|
|
module.exports = {
|
|
|
|
destroy,
|
2018-08-21 20:05:12 +02:00
|
|
|
undestroy,
|
|
|
|
errorOrDestroy
|
2017-05-06 14:20:52 +02:00
|
|
|
};
|