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: why use string_decoder in Readable? #5223

Closed
zoubin opened this issue Feb 14, 2016 · 0 comments
Closed

stream: why use string_decoder in Readable? #5223

zoubin opened this issue Feb 14, 2016 · 0 comments
Labels
confirmed-bug Issues with confirmed bugs. stream Issues and PRs related to the stream subsystem. string_decoder Issues and PRs related to the string_decoder subsystem.

Comments

@zoubin
Copy link
Contributor

zoubin commented Feb 14, 2016

I'm a little confused about the use of string_decoder in Readable (Line 140).

I thought it was meant to support pushing incomplete UTF-8 character sequence, but the following example failed:

const stream = require('stream')
const euro = new Buffer([0xE2, 0x82, 0xAC])
const cent = new Buffer([0xC2, 0xA2])
const source = Buffer.concat([euro, cent])

const readable = stream.Readable({ encoding: 'utf8' })
readable.push(source.slice(0, 2))
readable.push(source.slice(2, 4))
readable.push(source.slice(4, 6))
readable.push(null)

readable.on('data', data => console.log(data))

No output.

I was expecting the output to be the same with

const stream = require('stream')
const euro = new Buffer([0xE2, 0x82, 0xAC])
const cent = new Buffer([0xC2, 0xA2])
const source = Buffer.concat([euro, cent])

const readable = stream.Readable({ encoding: 'utf8' })
readable.push(source.slice(0, 3))
readable.push(source.slice(3, 5))
readable.push(null)

readable.on('data', data => console.log(data))
// €
// ¢

It seems that I have to push complete character sequence, or else chunk returned by the decoder in Line 140 will be empty ('').
If the stream is working in the flowing mode, every time read() try to get that empty string out, howMuchToRead will return 0 and fromList is not called, and data stops flowing.

Did I misunderstand the purpose of using string_decoder here?

@mscdex mscdex added question Issues that look for answers. stream Issues and PRs related to the stream subsystem. string_decoder Issues and PRs related to the string_decoder subsystem. labels Feb 14, 2016
@mscdex mscdex added confirmed-bug Issues with confirmed bugs. and removed question Issues that look for answers. labels Feb 14, 2016
mscdex added a commit to mscdex/io.js that referenced this issue Feb 14, 2016
Before this commit, it was possible to push a partial character
to a readable stream where it was decoded as an empty string and
then added to the internal buffer. This caused the stream to not
emit any data, even when the rest of the character bytes were pushed
separately, because of a non-zero length check of the first chunk in
the internal buffer.

Fixes: nodejs#5223
@mscdex mscdex closed this as completed in 9221201 Feb 17, 2016
rvagg pushed a commit that referenced this issue Feb 18, 2016
Before this commit, it was possible to push a partial character
to a readable stream where it was decoded as an empty string and
then added to the internal buffer. This caused the stream to not
emit any data, even when the rest of the character bytes were pushed
separately, because of a non-zero length check of the first chunk in
the internal buffer.

Fixes: #5223
PR-URL: #5226
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 1, 2016
Before this commit, it was possible to push a partial character
to a readable stream where it was decoded as an empty string and
then added to the internal buffer. This caused the stream to not
emit any data, even when the rest of the character bytes were pushed
separately, because of a non-zero length check of the first chunk in
the internal buffer.

Fixes: #5223
PR-URL: #5226
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 17, 2016
Before this commit, it was possible to push a partial character
to a readable stream where it was decoded as an empty string and
then added to the internal buffer. This caused the stream to not
emit any data, even when the rest of the character bytes were pushed
separately, because of a non-zero length check of the first chunk in
the internal buffer.

Fixes: #5223
PR-URL: #5226
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this issue Mar 21, 2016
Before this commit, it was possible to push a partial character
to a readable stream where it was decoded as an empty string and
then added to the internal buffer. This caused the stream to not
emit any data, even when the rest of the character bytes were pushed
separately, because of a non-zero length check of the first chunk in
the internal buffer.

Fixes: #5223
PR-URL: #5226
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. stream Issues and PRs related to the stream subsystem. string_decoder Issues and PRs related to the string_decoder subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants