Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
Mithgol opened this Issue · 6 comments

3 participants

@Mithgol

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.)

@bnoordhuis

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
@Mithgol

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.

@piscisaureus

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

@Mithgol

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

@Mithgol

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.

@piscisaureus

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
Something went wrong with that request. Please try again.