summaryrefslogtreecommitdiff
path: root/lib/_stream_writable.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2016-09-28 19:47:20 +0200
committerAnna Henningsen <anna@addaleax.net>2016-10-10 18:05:23 +0200
commit2a4b068acaa160a2d76ec5a3728e29ac6cdc715b (patch)
treecbb748888840748caac6e0cc688db235b5776cf7 /lib/_stream_writable.js
parenta6e1be4a4ea992b0068a7743d2879ccbe8345fc5 (diff)
downloadandroid-node-v8-2a4b068acaa160a2d76ec5a3728e29ac6cdc715b.tar.gz
android-node-v8-2a4b068acaa160a2d76ec5a3728e29ac6cdc715b.tar.bz2
android-node-v8-2a4b068acaa160a2d76ec5a3728e29ac6cdc715b.zip
stream: proper `instanceof` for `Writable`s
Use `[Symbol.hasInstance]()` to return `true` when asking for `new Duplex() instanceof Writable`. PR-URL: https://github.com/nodejs/node/pull/8834 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Calvin Metcalf <calvin.metcalf@gmail.com>
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r--lib/_stream_writable.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index 7a728c9de2..9e462a8f60 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -134,11 +134,32 @@ Object.defineProperty(WritableState.prototype, 'buffer', {
'instead.')
});
+// Test _writableState for inheritance to account for Duplex streams,
+// whose prototype chain only points to Readable.
+var realHasInstance;
+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;
+ }
+ });
+} else {
+ realHasInstance = function(object) {
+ return object instanceof this;
+ };
+}
+
function Writable(options) {
- // Writable ctor is applied to Duplexes, though they're not
- // instanceof Writable, they're instanceof Readable.
- if (!(this instanceof Writable) && !(this instanceof Stream.Duplex))
+ // Writable ctor is applied to Duplexes, too.
+ // `realHasInstance` is necessary because using plain `instanceof`
+ // would return false, as no `_writableState` property is attached.
+ if (!(realHasInstance.call(Writable, this)) &&
+ !(this instanceof Stream.Duplex)) {
return new Writable(options);
+ }
this._writableState = new WritableState(options, this);