-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should feature-detect that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving as it is will lead to infinite recursion, because 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 commentThe reason will be displayed to describe this comment to others. Learn more. How about we solve this by adding a check on 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 commentThe reason will be displayed to describe this comment to others. Learn more.
@mcollina Yeah, me too, so I tried it – that’s what breaks There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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); | ||
|
||
|
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); |
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.
dot
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.
@italoacasas done :)