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

stream, doc: an ambiguity in the readable.read([size]) doc #7337

Closed
vsemozhetbyt opened this issue Jun 19, 2016 · 8 comments
Closed

stream, doc: an ambiguity in the readable.read([size]) doc #7337

vsemozhetbyt opened this issue Jun 19, 2016 · 8 comments
Labels
doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jun 19, 2016

  • Version: 6.2.2
  • Platform: Windows 7 x64
  • Subsystem: stream, doc

The readable.read() method pulls some data out of the internal buffer and returns it. If no data available to be read, null is returned. By default, the data will be returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned (even if it exceeds size bytes).

The first paragraph makes distinction between buffer and string mode, but in the second paragraph only bytes are mentioned. It could seem that even in the string mode the size argument means bytes. However, in the string mode the size argument means decoded characters.

Save this script in the UTF-8 with BOM and run it:

/******************************************************************************/
'use strict';
/******************************************************************************/
const fs = require('fs');

const readStream = fs.createReadStream(__filename, { encoding: 'utf8' });

readStream.once('readable', () => {
  console.log(readStream.read(3));
});

The output:

 /*

If in the string mode the size argument meant bytes, the output would be just the BOM.

@nouex
Copy link

nouex commented Jun 19, 2016

Related: #6798

@mscdex mscdex added doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem. labels Jun 19, 2016
@vsemozhetbyt
Copy link
Contributor Author

By the way, is this another lacuna about readable stream? Should I post it as an issue here or do I just not understand the doc well?

@joaocgreis
Copy link
Member

cc @nodejs/streams

@mcollina
Copy link
Member

I think it's s duplicate of #6798.

What is BOM?

@addaleax
Copy link
Member

What is BOM?

The byte-order mark, aka U+FEFF. It’s primarily used at the start of text for indicating the endianness of a UTF16/UTF32/etc-encoded text. Although it’s discouraged, it can also be used at the start of UTF-8 text (encoded as ef bb bf, so 3 bytes).

@jasnell
Copy link
Member

jasnell commented May 30, 2017

ping @nodejs/streams

@calvinmetcalf
Copy link
Contributor

sounds like we need to update the docs to specify that it's in encoding dependent units

@mcollina
Copy link
Member

Closing as a duplicate of #6798.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

8 participants