2017-05-06 14:20:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
2024-04-21 18:53:08 +02:00
|
|
|
const {
|
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
2024-04-23 19:05:38 +02:00
|
|
|
|
2019-08-25 18:13:27 +02:00
|
|
|
const {
|
2024-04-23 19:05:38 +02:00
|
|
|
AbortError,
|
2021-03-04 17:46:40 +01:00
|
|
|
aggregateTwoErrors,
|
|
|
|
codes: {
|
|
|
|
ERR_MULTIPLE_CALLBACK,
|
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2021-07-07 11:33:55 +02:00
|
|
|
const {
|
2023-03-24 08:20:18 +01:00
|
|
|
kIsDestroyed,
|
2021-07-07 11:33:55 +02:00
|
|
|
isDestroyed,
|
|
|
|
isFinished,
|
2023-02-18 18:53:57 +01:00
|
|
|
isServerRequest,
|
2023-10-28 15:55:34 +02:00
|
|
|
kState,
|
|
|
|
kErrorEmitted,
|
|
|
|
kEmitClose,
|
|
|
|
kClosed,
|
|
|
|
kCloseEmitted,
|
|
|
|
kConstructed,
|
|
|
|
kDestroyed,
|
|
|
|
kAutoDestroy,
|
|
|
|
kErrored,
|
2021-07-07 11:33:55 +02:00
|
|
|
} = require('internal/streams/utils');
|
2019-08-25 18:13:27 +02:00
|
|
|
|
|
|
|
const kDestroy = Symbol('kDestroy');
|
|
|
|
const kConstruct = Symbol('kConstruct');
|
|
|
|
|
2020-10-21 06:34:10 +05:30
|
|
|
function checkError(err, w, r) {
|
|
|
|
if (err) {
|
|
|
|
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
|
2020-11-24 14:11:20 +01:00
|
|
|
err.stack; // eslint-disable-line no-unused-expressions
|
2020-10-21 06:34:10 +05:30
|
|
|
|
|
|
|
if (w && !w.errored) {
|
|
|
|
w.errored = err;
|
|
|
|
}
|
|
|
|
if (r && !r.errored) {
|
|
|
|
r.errored = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:12:13 +02:00
|
|
|
// Backwards compat. cb() is undocumented and unused in core but
|
|
|
|
// unfortunately might be used by modules.
|
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;
|
2019-08-25 18:13:27 +02:00
|
|
|
// With duplex streams we use the writable side for state.
|
|
|
|
const s = w || r;
|
2017-05-06 14:20:52 +02:00
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (
|
|
|
|
(w && (w[kState] & kDestroyed) !== 0) ||
|
|
|
|
(r && (r[kState] & kDestroyed) !== 0)
|
|
|
|
) {
|
2019-08-18 23:38:35 +02:00
|
|
|
if (typeof cb === 'function') {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:33:46 +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
|
2020-10-21 06:34:10 +05:30
|
|
|
checkError(err, w, r);
|
2017-05-06 14:20:52 +02:00
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] |= kDestroyed;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
2019-07-16 00:03:23 +02:00
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] |= kDestroyed;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 18:13:27 +02:00
|
|
|
// If still constructing then defer calling _destroy.
|
2023-10-28 15:55:34 +02:00
|
|
|
if ((s[kState] & kConstructed) === 0) {
|
2019-08-25 18:13:27 +02:00
|
|
|
this.once(kDestroy, function(er) {
|
2021-03-04 17:46:40 +01:00
|
|
|
_destroy(this, aggregateTwoErrors(er, err), cb);
|
2019-08-25 18:13:27 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_destroy(this, err, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _destroy(self, err, cb) {
|
2020-07-17 15:48:22 -07:00
|
|
|
let called = false;
|
2019-08-25 18:13:27 +02:00
|
|
|
|
2021-07-09 20:18:06 +02:00
|
|
|
function onDestroy(err) {
|
|
|
|
if (called) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-17 15:48:22 -07:00
|
|
|
called = true;
|
|
|
|
|
2021-07-09 20:18:06 +02:00
|
|
|
const r = self._readableState;
|
|
|
|
const w = self._writableState;
|
|
|
|
|
2020-10-21 06:34:10 +05:30
|
|
|
checkError(err, w, r);
|
2019-12-08 09:12:14 +01:00
|
|
|
|
2020-01-25 15:35:38 +01:00
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] |= kClosed;
|
2020-01-25 15:35:38 +01:00
|
|
|
}
|
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] |= kClosed;
|
2020-01-25 15:35:38 +01:00
|
|
|
}
|
|
|
|
|
2019-08-18 23:38:35 +02:00
|
|
|
if (typeof cb === 'function') {
|
2019-08-24 16:33:46 +02:00
|
|
|
cb(err);
|
2019-08-18 23:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
2019-08-25 18:13:27 +02:00
|
|
|
process.nextTick(emitErrorCloseNT, self, err);
|
2019-12-08 09:12:14 +01:00
|
|
|
} else {
|
2019-08-25 18:13:27 +02:00
|
|
|
process.nextTick(emitCloseNT, self);
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
2021-07-09 20:18:06 +02:00
|
|
|
}
|
|
|
|
try {
|
2022-04-08 12:29:24 +02:00
|
|
|
self._destroy(err || null, onDestroy);
|
2021-07-09 20:18:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
onDestroy(err);
|
2020-07-17 15:48:22 -07:00
|
|
|
}
|
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;
|
|
|
|
|
2020-04-19 23:28:58 +02:00
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] |= kCloseEmitted;
|
2020-04-19 23:28:58 +02:00
|
|
|
}
|
2020-04-26 11:38:47 +02:00
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] |= kCloseEmitted;
|
2020-04-26 11:38:47 +02:00
|
|
|
}
|
2020-04-19 23:28:58 +02:00
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (
|
|
|
|
(w && (w[kState] & kEmitClose) !== 0) ||
|
|
|
|
(r && (r[kState] & kEmitClose) !== 0)
|
|
|
|
) {
|
2019-12-08 09:12:14 +01:00
|
|
|
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;
|
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (
|
|
|
|
(w && (w[kState] & kErrorEmitted) !== 0) ||
|
|
|
|
(r && (r[kState] & kErrorEmitted) !== 0)
|
|
|
|
) {
|
2019-12-08 09:12:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] |= kErrorEmitted;
|
2019-12-08 09:12:14 +01:00
|
|
|
}
|
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] |= kErrorEmitted;
|
2019-12-08 09:12:14 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-08-25 18:13:27 +02:00
|
|
|
r.constructed = true;
|
2020-01-25 15:35:38 +01:00
|
|
|
r.closed = false;
|
2020-04-26 11:38:47 +02:00
|
|
|
r.closeEmitted = false;
|
2019-07-16 00:03:23 +02:00
|
|
|
r.destroyed = false;
|
2020-06-28 18:43:59 +02:00
|
|
|
r.errored = null;
|
2020-04-26 11:38:47 +02:00
|
|
|
r.errorEmitted = false;
|
2019-07-16 00:03:23 +02:00
|
|
|
r.reading = false;
|
2020-07-15 21:44:10 +02:00
|
|
|
r.ended = r.readable === false;
|
|
|
|
r.endEmitted = r.readable === false;
|
2017-05-06 14:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 00:03:23 +02:00
|
|
|
if (w) {
|
2019-08-25 18:13:27 +02:00
|
|
|
w.constructed = true;
|
2019-07-16 00:03:23 +02:00
|
|
|
w.destroyed = false;
|
2020-04-19 23:28:58 +02:00
|
|
|
w.closed = false;
|
|
|
|
w.closeEmitted = false;
|
2020-06-28 18:43:59 +02:00
|
|
|
w.errored = null;
|
2020-04-19 23:28:58 +02:00
|
|
|
w.errorEmitted = false;
|
2019-07-16 00:03:23 +02:00
|
|
|
w.finalCalled = false;
|
|
|
|
w.prefinished = false;
|
2020-07-15 21:44:10 +02:00
|
|
|
w.ended = w.writable === false;
|
|
|
|
w.ending = w.writable === false;
|
|
|
|
w.finished = w.writable === 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
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (
|
|
|
|
(w && (w[kState] ? (w[kState] & kDestroyed) !== 0 : w.destroyed)) ||
|
|
|
|
(r && (r[kState] ? (r[kState] & kDestroyed) !== 0 : r.destroyed))
|
|
|
|
) {
|
2019-08-18 23:38:35 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (
|
|
|
|
(r && (r[kState] & kAutoDestroy) !== 0) ||
|
|
|
|
(w && (w[kState] & kAutoDestroy) !== 0)
|
|
|
|
) {
|
2018-08-21 20:05:12 +02:00
|
|
|
stream.destroy(err);
|
2023-10-28 15:55:34 +02:00
|
|
|
} else if (err) {
|
2020-06-28 18:43:59 +02:00
|
|
|
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
|
2020-11-24 14:11:20 +01:00
|
|
|
err.stack; // eslint-disable-line no-unused-expressions
|
2020-06-28 18:43:59 +02:00
|
|
|
|
2023-10-28 15:55:34 +02:00
|
|
|
if (w && (w[kState] & kErrored) === 0) {
|
2020-06-28 18:43:59 +02:00
|
|
|
w.errored = err;
|
2019-12-08 09:12:14 +01:00
|
|
|
}
|
2023-10-28 15:55:34 +02:00
|
|
|
if (r && (r[kState] & kErrored) === 0) {
|
2020-06-28 18:43:59 +02:00
|
|
|
r.errored = err;
|
2019-12-08 09:12:14 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-08-25 18:13:27 +02:00
|
|
|
function construct(stream, cb) {
|
|
|
|
if (typeof stream._construct !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const r = stream._readableState;
|
|
|
|
const w = stream._writableState;
|
|
|
|
|
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] &= ~kConstructed;
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] &= ~kConstructed;
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 07:45:11 +02:00
|
|
|
stream.once(kConstruct, cb);
|
|
|
|
|
|
|
|
if (stream.listenerCount(kConstruct) > 1) {
|
|
|
|
// Duplex
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:13:27 +02:00
|
|
|
process.nextTick(constructNT, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
function constructNT(stream) {
|
|
|
|
let called = false;
|
2021-07-09 20:18:06 +02:00
|
|
|
|
|
|
|
function onConstruct(err) {
|
|
|
|
if (called) {
|
|
|
|
errorOrDestroy(stream, err ?? new ERR_MULTIPLE_CALLBACK());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
called = true;
|
|
|
|
|
|
|
|
const r = stream._readableState;
|
|
|
|
const w = stream._writableState;
|
|
|
|
const s = w || r;
|
|
|
|
|
2019-08-25 18:13:27 +02:00
|
|
|
if (r) {
|
2023-10-28 15:55:34 +02:00
|
|
|
r[kState] |= kConstructed;
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
|
|
|
if (w) {
|
2023-10-28 15:55:34 +02:00
|
|
|
w[kState] |= kConstructed;
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s.destroyed) {
|
|
|
|
stream.emit(kDestroy, err);
|
|
|
|
} else if (err) {
|
|
|
|
errorOrDestroy(stream, err, true);
|
|
|
|
} else {
|
2024-03-07 17:28:25 +01:00
|
|
|
stream.emit(kConstruct);
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
2021-07-09 20:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-02-26 16:56:20 +01:00
|
|
|
stream._construct((err) => {
|
|
|
|
process.nextTick(onConstruct, err);
|
|
|
|
});
|
2021-07-09 20:18:06 +02:00
|
|
|
} catch (err) {
|
2023-02-26 16:56:20 +01:00
|
|
|
process.nextTick(onConstruct, err);
|
2020-07-17 15:48:22 -07:00
|
|
|
}
|
2019-08-25 18:13:27 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:47:33 +01:00
|
|
|
function isRequest(stream) {
|
2022-09-06 15:58:31 +08:00
|
|
|
return stream?.setHeader && typeof stream.abort === 'function';
|
2020-01-21 20:47:33 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 18:17:18 +02:00
|
|
|
function emitCloseLegacy(stream) {
|
|
|
|
stream.emit('close');
|
|
|
|
}
|
|
|
|
|
|
|
|
function emitErrorCloseLegacy(stream, err) {
|
|
|
|
stream.emit('error', err);
|
|
|
|
process.nextTick(emitCloseLegacy, stream);
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:47:33 +01:00
|
|
|
// Normalize destroy for legacy.
|
|
|
|
function destroyer(stream, err) {
|
2021-07-25 20:01:43 +02:00
|
|
|
if (!stream || isDestroyed(stream)) {
|
2021-05-02 18:17:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-07 11:33:55 +02:00
|
|
|
if (!err && !isFinished(stream)) {
|
2021-05-02 18:17:18 +02:00
|
|
|
err = new AbortError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove isRequest branches.
|
2021-07-07 11:33:55 +02:00
|
|
|
if (isServerRequest(stream)) {
|
|
|
|
stream.socket = null;
|
|
|
|
stream.destroy(err);
|
2021-05-02 18:17:18 +02:00
|
|
|
} else if (isRequest(stream)) {
|
|
|
|
stream.abort();
|
|
|
|
} else if (isRequest(stream.req)) {
|
|
|
|
stream.req.abort();
|
|
|
|
} else if (typeof stream.destroy === 'function') {
|
|
|
|
stream.destroy(err);
|
|
|
|
} else if (typeof stream.close === 'function') {
|
|
|
|
// TODO: Don't lose err?
|
|
|
|
stream.close();
|
|
|
|
} else if (err) {
|
2022-06-28 10:59:24 +02:00
|
|
|
process.nextTick(emitErrorCloseLegacy, stream, err);
|
2021-05-02 18:17:18 +02:00
|
|
|
} else {
|
|
|
|
process.nextTick(emitCloseLegacy, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream.destroyed) {
|
2023-03-24 08:20:18 +01:00
|
|
|
stream[kIsDestroyed] = true;
|
2021-05-02 18:17:18 +02:00
|
|
|
}
|
2020-01-21 20:47:33 +01:00
|
|
|
}
|
2018-08-21 20:05:12 +02:00
|
|
|
|
2017-05-06 14:20:52 +02:00
|
|
|
module.exports = {
|
2019-08-25 18:13:27 +02:00
|
|
|
construct,
|
2020-01-21 20:47:33 +01:00
|
|
|
destroyer,
|
2017-05-06 14:20:52 +02:00
|
|
|
destroy,
|
2018-08-21 20:05:12 +02:00
|
|
|
undestroy,
|
2023-02-18 18:53:57 +01:00
|
|
|
errorOrDestroy,
|
2017-05-06 14:20:52 +02:00
|
|
|
};
|