Skip to content

Commit 88eb619

Browse files
yashLadhatargos
authored andcommitted
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: #35727 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
1 parent e0a1541 commit 88eb619

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

lib/internal/streams/destroy.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ const { Symbol } = primordials;
88
const kDestroy = Symbol('kDestroy');
99
const kConstruct = Symbol('kConstruct');
1010

11+
function checkError(err, w, r) {
12+
if (err) {
13+
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
14+
err.stack;
15+
16+
if (w && !w.errored) {
17+
w.errored = err;
18+
}
19+
if (r && !r.errored) {
20+
r.errored = err;
21+
}
22+
}
23+
}
24+
1125
// Backwards compat. cb() is undocumented and unused in core but
1226
// unfortunately might be used by modules.
1327
function destroy(err, cb) {
@@ -24,20 +38,10 @@ function destroy(err, cb) {
2438
return this;
2539
}
2640

27-
if (err) {
28-
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
29-
err.stack;
30-
31-
if (w && !w.errored) {
32-
w.errored = err;
33-
}
34-
if (r && !r.errored) {
35-
r.errored = err;
36-
}
37-
}
3841

3942
// We set destroyed to true before firing error callbacks in order
4043
// to make it re-entrance safe in case destroy() is called within callbacks
44+
checkError(err, w, r);
4145

4246
if (w) {
4347
w.destroyed = true;
@@ -66,17 +70,7 @@ function _destroy(self, err, cb) {
6670

6771
called = true;
6872

69-
if (err) {
70-
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
71-
err.stack;
72-
73-
if (w && !w.errored) {
74-
w.errored = err;
75-
}
76-
if (r && !r.errored) {
77-
r.errored = err;
78-
}
79-
}
73+
checkError(err, w, r);
8074

8175
if (w) {
8276
w.closed = true;

0 commit comments

Comments
 (0)