Description
- Version:
v11.2.0
- Platform: MacOS
- Subsystem: streams2
I am implementing a Writable stream to process output from spawned processes. Since I know that the processes will write utf8-encoded strings, I wanted to implement the stream so the incoming data would arrive as Strings. I was delighted to see decodeStrings
mentioned under writable._write
:
chunk
<Buffer>
|<string>
|<any>
The chunk to be written. Will always be a buffer unless thedecodeStrings
option was set to false or the stream is operating in object mode.
I didn't quite understand how setting decodeStrings
to false would result in the stream not getting chunks as Buffers, but I tried it. It didn't work the way I interpreted the docs, so I was quite surprised to see the chunks still arriving as Buffers.
After reading the Constructor docs:
decodeStrings
<boolean>
Whether or not to encode strings asBuffer
s before passing them tostream._write()
, using the encoding specified in thestream.write()
call. Default: true.
and the actual implementation of decodeChunk
, I realised that the option name didn't match the behavior, and it would be more accurate to call it encodeStrings
(to match the nomenclature used in other languages/libraries when they refer to the act of taking an internal string representation & converting it to a byte sequence using a specified encoding).
The Constructor docs confused me, because the option said decode
and the description said encode
and I knew they couldn't both be accurate, and so I had to choose between assuming the option name was right (and Strings were decoded from Buffers), or the description was right (and Strings were encoding into Buffers).
I'm filing this issue because I'm grumpy about the time it took to understand the behaviour and feel that I would have spent less time to work out my use-case wasn't supported if there was a clearer option name, or docs which made it clear that the option name didn't reflect the behaviour. Something like:
chunk
<Buffer>
|<string>
|<any>
ABuffer
converted from theString
passed tostream.write()
. If the stream is operating in object mode or the stream'sdecodeStrings
option isfalse
, chunk will not be converted & will be whatever was passed tostream.write()
.
decodeStrings
<boolean>
ConvertString
s passed tostream.write()
toBuffer
s (using the encoding given to thestream.write()
call) before passing them towritable._write
. Other types of data are not converted. Setting tofalse
will passString
s through without conversion. Default: true.
I now know I have to implement something to handle my original problem (decode incoming Buffers into Strings) myself. I'm happy to do that, but if you think the Writable's decodeStrings
feature could be more configurable, I'd be very happy to use it instead.