Closed
Description
I'm a little confused about the use of string_decoder
in Readable (Line 140).
I thought it was meant to support push
ing 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?