Skip to content

Commit

Permalink
stream: fix Writable instanceof for subclasses
Browse files Browse the repository at this point in the history
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: #14943
PR-URL: #14945
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed Sep 20, 2017
1 parent f0328f6 commit 66187fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ if (typeof Symbol === 'function' && Symbol.hasInstance) {
value: function(object) {
if (realHasInstance.call(this, object))
return true;
if (this !== Writable)
return false;

return object && object._writableState instanceof WritableState;
}
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-stream-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ Object.setPrototypeOf(CustomWritable.prototype, Writable.prototype);
new CustomWritable();

assert.throws(CustomWritable, /AssertionError: undefined does not inherit from CustomWritable/);

class OtherCustomWritable extends Writable {}

assert(!(new OtherCustomWritable() instanceof CustomWritable));
assert(!(new CustomWritable() instanceof OtherCustomWritable));

0 comments on commit 66187fa

Please sign in to comment.