From 66187fa0445019cee8a4684870a7ea76e93de6df 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 PR-URL: https://github.com/nodejs/node/pull/14945 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Refael Ackermann Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: Evan Lucas Reviewed-By: James M Snell --- 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 6cf7390b841a33..8251e8e219a519 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -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; } diff --git a/test/parallel/test-stream-inheritance.js b/test/parallel/test-stream-inheritance.js index 2c6daed0385d4a..aefb39af512646 100644 --- a/test/parallel/test-stream-inheritance.js +++ b/test/parallel/test-stream-inheritance.js @@ -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));