Skip to content
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

Throws encoding error when used with readSync #156

Closed
ysobolev opened this issue Feb 12, 2017 · 1 comment
Closed

Throws encoding error when used with readSync #156

ysobolev opened this issue Feb 12, 2017 · 1 comment

Comments

@ysobolev
Copy link

The following code throws an exception in node v6.x (and earlier):

Buffer = require('buffer/').Buffer;
buff = Buffer(13);
fd = fs.openSync('./test', 'r');
fs.readSync(fd, buff, 0, 13);

The reason is that in fs.js in the node distribution, the readSync() function contains:

  if (!(buffer instanceof Buffer)) {
    // legacy string interface (fd, length, position, encoding, callback)

See https://github.com/nodejs/node/blob/v6.x/lib/fs.js#L705

As a result, the function thinks it is being passed a string and the length ends up being passed in as the encoding.

I realize this is fixed in later version of node per your issue: nodejs/node#4530

Are there plans to allow this Buffer implemenation to work with the read calls in node v6?

@feross
Copy link
Owner

feross commented Feb 12, 2017

Is there a particular reason that you're using this buffer implementation in Node.js?

I don't recommend passing this Buffer implementation into functions from Node.js core packages, like fs. These buffer instances will not be treated as real Node.js buffers because they fail a instanceof Buffer check. Things still might work out, since they're still a subclass of Uint8Array and they have a length property (so they're array-like), but instanceof won't ever work.

Let me know what problem you're trying to solve, and perhaps I can suggest a solution for your needs?

@feross feross closed this as completed Feb 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants