Skip to content

Commit cbe2fce

Browse files
committed
stream: fix Writable instanceof for subclasses
The current custom instanceof for `Writable` subclasses previously returned false positives for instances of *other* subclasses of `Writable` because it was inherited by these subclasses. Fixes: nodejs#14943
1 parent 5e7c697 commit cbe2fce

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/_stream_writable.js

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ if (typeof Symbol === 'function' && Symbol.hasInstance) {
179179
value: function(object) {
180180
if (realHasInstance.call(this, object))
181181
return true;
182+
if (this !== Writable)
183+
return false;
182184

183185
return object && object._writableState instanceof WritableState;
184186
}

test/parallel/test-stream-inheritance.js

+5
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ common.expectsError(
5656
message: 'undefined does not inherit from CustomWritable'
5757
}
5858
);
59+
60+
class OtherCustomWritable extends Writable {}
61+
62+
assert(!(new OtherCustomWritable() instanceof CustomWritable));
63+
assert(!(new CustomWritable() instanceof OtherCustomWritable));

0 commit comments

Comments
 (0)