-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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 Writable
s
#8834
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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
returnsfalse
before the constructor is run, so the constructor just calls itself over and over again. And adding an extrarealHasInstance
check inside of the symbol-based override would break our fancyLazyTransform
logic.Feel free to play around with this, but I am afraid this would be necessary.
There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcollina Yeah, me too, so I tried it – that’s what breaks
LazyTransform
, because it invokes theWritable
constructor from inside a_writableState
getter. :(There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. :)