From cbe2fce21a4bddbb623d3307c8f22f66fd7de0e4 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 19 Aug 2017 18:29:25 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/14943 --- lib/_stream_writable.js | 2 ++ test/parallel/test-stream-inheritance.js | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 6e0eaf45b5464d..7de77958d56b4c 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -179,6 +179,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; } diff --git a/test/parallel/test-stream-inheritance.js b/test/parallel/test-stream-inheritance.js index 2d9a1e83ef7528..a687ea9da054c9 100644 --- a/test/parallel/test-stream-inheritance.js +++ b/test/parallel/test-stream-inheritance.js @@ -56,3 +56,8 @@ common.expectsError( message: 'undefined does not inherit from CustomWritable' } ); + +class OtherCustomWritable extends Writable {} + +assert(!(new OtherCustomWritable() instanceof CustomWritable)); +assert(!(new CustomWritable() instanceof OtherCustomWritable));