Skip to content

Commit c5d89e6

Browse files
dgholzaddaleax
authored andcommittedJan 23, 2019
doc: reword stream docs to clarify that decodeStrings encodes strings
I was implementing a Writable stream and misunderstood `decodeStrings` to mean 'will decode `Buffer`s into `string`s before calling `_write`'. This change adds a little more detail to the description of `decodeStrings` to clarify its effect on a Writable stream & what gets passed to `_write`. Changing the name of the option to `encodeStrings` would make it much easier to understand, but the name was chosen in 2012 and the option used in many projects (22k mentions of 'decodeStringsr in JS projects in GitHub). Deprecating the old name & rolling out a replacement is beyond my capabilities as a first-time contributor. PR-URL: #25468 Fixes: #25464 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent e55c5c3 commit c5d89e6

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed
 

‎doc/api/stream.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -1537,10 +1537,12 @@ changes:
15371537
* `highWaterMark` {number} Buffer level when
15381538
[`stream.write()`][stream-write] starts returning `false`. **Default:**
15391539
`16384` (16kb), or `16` for `objectMode` streams.
1540-
* `decodeStrings` {boolean} Whether or not to encode strings as
1541-
`Buffer`s before passing them to [`stream._write()`][stream-_write],
1542-
using the encoding specified in the [`stream.write()`][stream-write] call.
1543-
**Default:** `true`.
1540+
* `decodeStrings` {boolean} Whether to encode `string`s passed to
1541+
[`stream.write()`][stream-write] to `Buffer`s (with the encoding
1542+
specified in the [`stream.write()`][stream-write] call) before passing
1543+
them to [`stream._write()`][stream-_write]. Other types of data are not
1544+
converted (i.e. `Buffer`s are not decoded into `string`s). Setting to
1545+
false will prevent `string`s from being converted. **Default:** `true`.
15441546
* `defaultEncoding` {string} The default encoding that is used when no
15451547
encoding is specified as an argument to [`stream.write()`][stream-write].
15461548
**Default:** `'utf8'`.
@@ -1606,9 +1608,11 @@ const myWritable = new Writable({
16061608

16071609
#### writable.\_write(chunk, encoding, callback)
16081610

1609-
* `chunk` {Buffer|string|any} The chunk to be written. Will **always**
1610-
be a buffer unless the `decodeStrings` option was set to `false`
1611-
or the stream is operating in object mode.
1611+
* `chunk` {Buffer|string|any} The `Buffer` to be written, converted from the
1612+
`string` passed to [`stream.write()`][stream-write]. If the stream's
1613+
`decodeStrings` option is `false` or the stream is operating in object mode,
1614+
the chunk will not be converted & will be whatever was passed to
1615+
[`stream.write()`][stream-write].
16121616
* `encoding` {string} If the chunk is a string, then `encoding` is the
16131617
character encoding of that string. If chunk is a `Buffer`, or if the
16141618
stream is operating in object mode, `encoding` may be ignored.
@@ -2301,9 +2305,11 @@ user programs.
23012305

23022306
#### transform.\_transform(chunk, encoding, callback)
23032307

2304-
* `chunk` {Buffer|string|any} The chunk to be transformed. Will **always**
2305-
be a buffer unless the `decodeStrings` option was set to `false`
2306-
or the stream is operating in object mode.
2308+
* `chunk` {Buffer|string|any} The `Buffer` to be transformed, converted from
2309+
the `string` passed to [`stream.write()`][stream-write]. If the stream's
2310+
`decodeStrings` option is `false` or the stream is operating in object mode,
2311+
the chunk will not be converted & will be whatever was passed to
2312+
[`stream.write()`][stream-write].
23072313
* `encoding` {string} If the chunk is a string, then this is the
23082314
encoding type. If chunk is a buffer, then this is the special
23092315
value - 'buffer', ignore it in this case.

0 commit comments

Comments
 (0)