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

Already on GitHub? Sign in to your account

Feature request: boolean function require('fs').isFD(var) to test if the variable contains a file descriptor #3987

Closed
Mithgol opened this Issue Sep 9, 2012 · 6 comments

Comments

Projects
None yet
3 participants

Mithgol commented Sep 9, 2012

Currently jDataView's constructor accepts an ArrayBuffer, or Node's Buffer, or a raw String as its first argument.

It could be made to accept Node's file descriptors as well (to eliminate the need of fs.readFile() which is otherwise necessary to get a Buffer out of the file), but it requires a reliable method to test if a variable (or a function's argument) contains a Node's file descriptor.

Unfortunately, as far as I know, there's no instanceof or typeof test for that.

That's why I request a new feature: a boolean function require('fs').isFD(var) that could be used to test if some variable contains a file descriptor of Node.js.

(Of course, the above jDataView problem is just an example to illustrate the use case, which is a function accepting an argument that has one of several possible types, including a file descriptor type, and then that function has to test that argument's type.)

Owner

bnoordhuis commented Sep 9, 2012

Unfortunately, as far as I know, there's no instanceof or typeof test for that.

That's because a file descriptor is simply an integer >= 0. You can use fs.fstat() to find out if it refers to an open file.

@bnoordhuis bnoordhuis closed this Sep 9, 2012

Mithgol commented Sep 11, 2012

Can I really find out if a descriptor is invalid?

If I enter fs (to require the module) and then fs.fstatSync(312) in REPL of Node v0.8.8 under Windows XP, it causes a silent exit from Node.

Member

piscisaureus commented Sep 11, 2012

@Mithgol You're right, you can't.

Mithgol commented Sep 23, 2012

Now, after #3995 is fixed, it would be possible to check if a file descriptor refers to an open file.

Mithgol commented Sep 23, 2012

I wonder if a file descriptor may be not referring to an open file but still be “valid” is some more general sense.

But I am satisfied for now.

Member

piscisaureus commented Sep 23, 2012

I wonder if a file descriptor may be not referring to an open file but still be “valid” is some more general sense.

Yes, that could happen. It might, for example, refer to an epoll or kqueue fd. But why bother? if the user passes an invalid fd he'll find out soon enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment