-
Notifications
You must be signed in to change notification settings - Fork 7.3k
fs: Add DuplexStream and fs.createDuplexStream #8002
Conversation
This work was discussed briefly in issue #7707 |
@benjamincburns how is this useful? |
Makes it possible to do full duplex I/O on a character device, such as in http://github.com/benjamincburns/node-tuntap. |
Also see this StackOverflow question: http://stackoverflow.com/q/24464709/203705 |
@indutny / @tjfontaine Thoughts on this? I can do the review and merge, but want feedback on allowing the new feature? |
FWIW, I'd be happy to extend this PR to also include |
I think it is nice addition to the |
Just a comment about the general idea behind this, as it has been mentioned that it would be useful for talking to character devices: That is an API abuse. It relies on the fact that We need to do this properly in libuv and have some My 2 cents. |
@saghul I'm not sure I understand your objection. One of the core tenants of unix is that devices (character or otherwise) are actual files in the VFS. I'd agree that this would be API abuse if we were talking about a socket fd, but that's not the case here. However I don't disagree that there might be a way to express the general purpose character device use case in terms of more appropriate multiplatform semantics. If you can propose something more specific I'd be happy to submit another PR if it's all well and good. In the mean time this PR is a logical extension of the semantics which are already in place. We've got |
(disclaimer: my involvement with Node is quite limited, I'm on libuv core, so look at my comments from that angle)
While that is true, libuv is an abstraction layer.
I haven't sat down to think about this, and unfortunately I don't have the time to do so. I believe libuv should expose that, so if you want to give it a shot, design the feature and open an issue so we can discuss it before jumping into writing code.
Ultimately it's not my decision, I'm just John Doe complaining :-) I see this as increasing the techincal debt in fs streams, which I believe to be wrong in the first place. The way I see it, 2 things are needed:
As you can see, I think fo them as separate things. Those are my 2 cents. |
No worries - I think this debate is constructive. I'm new to the node community, but from reading other PRs and issues it seems like the node community/maintainers haven't really settled on how to handle files and file descriptors in a broad sense. I think the most common use cases are nailed down well (stat file, open file, read file, write file, close file; and again but I think my knowledge of libuv internals puts me at a disadvantage here. From the perspective of someone who writes a fair amount of code for Linux devices, I expect to be able to treat devices as though they are regular files, because they are regular files. All of the operations and caveats of regular file access should apply. I can't think of any other language where I wouldn't accomplish my goal by opening the Put differently, I very much agree with the goal that the runtime should be as platform agnostic as possible, but it should not prevent me from writing platform-specific modules. That said, I really like your idea. If you'd like to chat more out of band about the higher level semantics, feel free to contact me on Skype at benjamin.burns, or on Google hangouts at benjamin.c.burns. |
@saghul
But if you add any value |
@benjamincburns I understand your point. The porblem is that while libuv tries to abstract many things, some have "leaked". I consider the fs API to be one of them (on Unix). I don't have a proposal for a change right now, but after the next libuv stable version is released we'll start working on some new design ideas. It will take time, but hopefully we can find a good and future proof approach. FWIW, the current model is not going away in 0.12, so if it helps others maybe it's a good idea to merge this. We can replace the guts later while maintaining the API, (hopefully) making everyone happy :-) |
@trevnorris ouch, it seems worse than I thought. |
Cheers. I'll definitely keep an eye out for this! I think there is room for all sorts of very interesting and nice clean abstraction here, and I hope that whatever the solution becomes it doesn't prevent me from "reaching down a layer" when I'd like to do so. |
Given that there's been no further motion on this and it looks to introduce a significant new feature, I'm inclined to close this here and ask that if it's still something that is wanted, please update and open a new PR against master on http://github.com/nodejs/node. /cc @joyent/node-tsc |
@jasnell - yes, this is something that is still wanted. I'll do as suggested. |
This adds a
DuplexStream
and matchingcreateDuplexStream
function to the fs module. Similar to howDuplex
uses parasitic inheritance from theWritable
class, I've implemented this by inheriting prototypically fromDuplex
and parasitically fromReadStream
andWriteStream
.Please note that I had to modify slightly the
ReadStream
class to make this work. The only modification necessary was to prefix theend
member (undocumented and used in a private context), so it is now_end
. This prevents this field from clashing with theend
method specified by theDuplex
stream.