From 88eb6191e427186a98d1a8a49d6e94290d889f1d Mon Sep 17 00:00:00 2001 From: Yash Ladha Date: Wed, 21 Oct 2020 06:34:10 +0530 Subject: [PATCH] stream: fix duplicate logic in stream destroy Fix duplicate logic in stream destroy as the same logic is being shared across methods and thus can be encapsulated into a single method. PR-URL: https://github.com/nodejs/node/pull/35727 Reviewed-By: Matteo Collina Reviewed-By: Rich Trott Reviewed-By: Anto Aravinth Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Robert Nagy Reviewed-By: Gireesh Punathil Reviewed-By: Ricky Zhou <0x19951125@gmail.com> --- lib/internal/streams/destroy.js | 38 ++++++++++++++------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 510f7a40c8ddd6..7477ec97379ff9 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -8,6 +8,20 @@ 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; + + 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) { @@ -24,20 +38,10 @@ function destroy(err, cb) { return this; } - if (err) { - // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 - err.stack; - - if (w && !w.errored) { - w.errored = err; - } - if (r && !r.errored) { - r.errored = err; - } - } // 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; @@ -66,17 +70,7 @@ function _destroy(self, err, cb) { called = true; - if (err) { - // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 - err.stack; - - if (w && !w.errored) { - w.errored = err; - } - if (r && !r.errored) { - r.errored = err; - } - } + checkError(err, w, r); if (w) { w.closed = true;