diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-10-14 00:09:30 +0200 |
---|---|---|
committer | Evan Lucas <evanlucas@me.com> | 2016-10-14 16:29:51 -0500 |
commit | 8c4fab0a28edfe32c3e7305fe55f0a1bc5e3200d (patch) | |
tree | d54f0995f24fe101610a3d33aa76ea2d7254f23a /lib/_stream_writable.js | |
parent | 7171bd6311f2511ed29de478a6788f739186e61b (diff) | |
download | node-new-8c4fab0a28edfe32c3e7305fe55f0a1bc5e3200d.tar.gz |
stream: fix `Writable` subclass instanceof checks
2a4b068acaa160 introduced a regression in where checking
`instanceof` would fail for `Writable` subclasses inside the
subclass constructor, i.e. before `Writable()` was called.
Also, calling `null instanceof Writable` or
`undefined instanceof Writable` would fail due to accessing the
`_writableState` property of the target object.
This fixes these problems.
PR-URL: https://github.com/nodejs/node/pull/9088
Ref: https://github.com/nodejs/node/pull/8834#issuecomment-253640692
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r-- | lib/_stream_writable.js | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index f6c970e04d..575c56cdd7 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -141,9 +141,10 @@ if (typeof Symbol === 'function' && Symbol.hasInstance) { realHasInstance = Function.prototype[Symbol.hasInstance]; Object.defineProperty(Writable, Symbol.hasInstance, { value: function(object) { - // Trying to move the `realHasInstance` from the Writable constructor - // to here will break the Node.js LazyTransform implementation. - return object._writableState instanceof WritableState; + if (realHasInstance.call(this, object)) + return true; + + return object && object._writableState instanceof WritableState; } }); } else { @@ -156,6 +157,10 @@ function Writable(options) { // Writable ctor is applied to Duplexes, too. // `realHasInstance` is necessary because using plain `instanceof` // would return false, as no `_writableState` property is attached. + + // Trying to use the custom `instanceof` for Writable here will also break the + // Node.js LazyTransform implementation, which has a non-trivial getter for + // `_writableState` that would lead to infinite recursion. if (!(realHasInstance.call(Writable, this)) && !(this instanceof Stream.Duplex)) { return new Writable(options); |