Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: proper instanceof for Writables #8834

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,8 @@ Because JavaScript does not have support for multiple inheritance, the
to extending the `stream.Readable` *and* `stream.Writable` classes).

*Note*: The `stream.Duplex` class prototypically inherits from `stream.Readable`
and parasitically from `stream.Writable`.
and parasitically from `stream.Writable`, but `instanceof` will work properly
for both base classes by overriding [`Symbol.hasInstance`][]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dot

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italoacasas done :)


Custom Duplex streams *must* call the `new stream.Duplex([options])`
constructor and implement *both* the `readable._read()` and
Expand Down Expand Up @@ -2009,3 +2010,4 @@ readable buffer so there is nothing for a user to consume.
[Transform]: #stream_class_stream_transform
[Writable]: #stream_class_stream_writable
[zlib]: zlib.html
[`Symbol.hasInstance`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance
23 changes: 20 additions & 3 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,28 @@ 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 (Symbol.hasInstance) {
Copy link
Member

@mcollina mcollina Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should feature-detect that Symbol exists to avoid a regexp in readable-stream. Some of our supported platforms there do not have symbols yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Done!

realHasInstance = Function.prototype[Symbol.hasInstance];
Object.defineProperty(Writable, Symbol.hasInstance, {
value: function(object) {
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.
if (!(realHasInstance.call(Writable, this)) &&
!(this instanceof Stream.Duplex)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change this block? I think we can probably leave as it was before here.
As I understand this PR, all the awesomeness is implemented by overriding Writable.hasInstance.
So, can we remove realHasInstance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as it is will lead to infinite recursion, because this instanceof Writable returns false before the constructor is run, so the constructor just calls itself over and over again. And adding an extra realHasInstance check inside of the symbol-based override would break our fancy LazyTransform logic.

Feel free to play around with this, but I am afraid this would be necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we solve this by adding a check on Writable.hasInstance like: Function.prototype[Symbol.hasInstance].call(Writable, this) || this._writableState instanceof WritableState? I think that will reduce our cruft.

I would like to avoid adding one more variable to support older node variables :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will reduce our cruft.

@mcollina Yeah, me too, so I tried it – that’s what breaks LazyTransform, because it invokes the Writable constructor from inside a _writableState getter. :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment for that, because it is definitely not obvious.

@calvinmetcalf what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve added comments that hopefully clarify things a bit. :)

return new Writable(options);
}

this._writableState = new WritableState(options, this);

Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-stream-inheritance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
require('../common');
const assert = require('assert');
const { Readable, Writable, Duplex, Transform } = require('stream');

const readable = new Readable({ read() {} });
const writable = new Writable({ write() {} });
const duplex = new Duplex({ read() {}, write() {} });
const transform = new Transform({ transform() {} });

assert.ok(readable instanceof Readable);
assert.ok(!(writable instanceof Readable));
assert.ok(duplex instanceof Readable);
assert.ok(transform instanceof Readable);

assert.ok(!(readable instanceof Writable));
assert.ok(writable instanceof Writable);
assert.ok(duplex instanceof Writable);
assert.ok(transform instanceof Writable);

assert.ok(!(readable instanceof Duplex));
assert.ok(!(writable instanceof Duplex));
assert.ok(duplex instanceof Duplex);
assert.ok(transform instanceof Duplex);

assert.ok(!(readable instanceof Transform));
assert.ok(!(writable instanceof Transform));
assert.ok(!(duplex instanceof Transform));
assert.ok(transform instanceof Transform);