2017-05-06 14:20:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-01 20:44:02 +02:00
|
|
|
// Undocumented cb() API, needed for core, not for public API.
|
|
|
|
// The cb() will be invoked synchronously if _destroy is synchronous.
|
2019-12-08 09:12:14 +01:00
|
|
|
// If cb is passed no 'error' event will be emitted.
|
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
|
|
|
|
2020-01-03 21:41:44 +01:00
|
|
|
// TODO(ronag): readable & writable = false?
|
|
|
|
|
2019-12-08 09:12:14 +01:00
|
|
|
if (err) {
|
|
|
|
if (w) {
|
|
|
|
w.errored = true;
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
r.errored = true;
|
|
|
|
}
|
2019-08-24 16:33:46 +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-12-08 09:12:14 +01:00
|
|
|
} else if (err) {
|
2019-07-16 00:03:23 +02:00
|
|
|
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-12-08 09:12:14 +01:00
|
|
|
if (err) {
|
|
|
|
if (w) {
|
|
|
|
w.errored = true;
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
r.errored = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if (cb) {
|
2019-08-24 16:33:46 +02:00
|
|
|
// Invoke callback before scheduling emitClose so that callback
|
|
|
|
// can schedule before.
|
|
|
|
cb(err);
|
2019-12-08 09:12:14 +01:00
|
|
|
// Don't emit 'error' if passed a callback.
|
|
|
|
process.nextTick(emitCloseNT, this);
|
|
|
|
} else if (err) {
|
|
|
|
process.nextTick(emitErrorCloseNT, this, err);
|
|
|
|
} else {
|
2018-04-05 20:52:19 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-18 18:08:53 +02:00
|
|
|
function emitErrorCloseNT(self, err) {
|
2019-12-08 09:12:14 +01:00
|
|
|
emitErrorNT(self, err);
|
|
|
|
emitCloseNT(self);
|
2018-04-05 20:52:19 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 19:32:34 +01:00
|
|
|
function emitCloseNT(self) {
|
2019-12-08 09:12:14 +01:00
|
|
|
const r = self._readableState;
|
|
|
|
const w = self._writableState;
|
|
|
|
|
|
|
|
if ((w && w.emitClose) || (r && r.emitClose)) {
|
|
|
|
self.emit('close');
|
|
|
|
}
|
2018-01-29 19:32:34 +01:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:08:53 +02:00
|
|
|
function emitErrorNT(self, err) {
|
2019-12-08 09:12:14 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:08:53 +02:00
|
|
|
self.emit('error', err);
|
|
|
|
}
|
|
|
|
|
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;
|
2019-12-08 09:12:14 +01:00
|
|
|
r.errored = false;
|
2019-07-16 00:03:23 +02:00
|
|
|
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;
|
2019-08-24 16:33:46 +02:00
|
|
|
w.errored = false;
|
2019-07-16 00:03:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 07:33:47 +02:00
|
|
|
function errorOrDestroy(stream, err, sync) {
|
2018-08-21 20:05:12 +02:00
|
|
|
// 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
|
|
|
|
2020-01-03 21:41:44 +01:00
|
|
|
// TODO(ronag): readable & writable = false?
|
|
|
|
|
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-12-08 09:12:14 +01:00
|
|
|
else if (err) {
|
|
|
|
if (w) {
|
|
|
|
w.errored = true;
|
|
|
|
}
|
|
|
|
if (r) {
|
|
|
|
r.errored = true;
|
|
|
|
}
|
2019-09-28 07:33:47 +02:00
|
|
|
|
|
|
|
if (sync) {
|
|
|
|
process.nextTick(emitErrorNT, stream, err);
|
|
|
|
} else {
|
|
|
|
emitErrorNT(stream, err);
|
|
|
|
}
|
2019-12-08 09:12:14 +01:00
|
|
|
}
|
2018-08-21 20:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|