Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,14 @@ getReadableStreamSomehow()
##### readable.setEncoding(encoding)
<!-- YAML
added: v0.9.4
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/12766
description: If `encoding` is `null` or `'buffer'`, this will reset the stream
to the default of returning `Buffer` objects.
-->

* `encoding` {string} The encoding to use.
* `encoding` {string|null} The encoding to use.
* Returns: `this`

The `readable.setEncoding()` method sets the default character encoding for
Expand All @@ -934,9 +939,8 @@ The Readable stream will properly handle multi-byte characters delivered through
the stream that would otherwise become improperly decoded if simply pulled from
the stream as `Buffer` objects.

Encoding can be disabled by calling `readable.setEncoding(null)`. This approach
is useful when working with binary data or with large multi-byte strings spread
out over multiple chunks.
Decoding can be disabled by calling `readable.setEncoding(null)` or
`readable.setEncoding('buffer')`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may just be me, but I had to read the docs for readable.setEncoding() several times to convince myself that it (and the change here) are correct due to the use of both encoding/decoding and encoded/decoded.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s part of why I wanted to change this paragraph, I hope the result is okay. I’ll gladly take suggestions for improvements!


```js
const readable = getReadableStreamSomehow();
Expand Down
10 changes: 9 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,16 @@ function needMoreData(state) {
state.length === 0);
}

// backwards compatibility.
Readable.prototype.setEncoding = function(enc) {
if (enc === null || enc === 'buffer') {
if (this._readableState.decoder) {
this.push(this._readableState.decoder.end());
}
this._readableState.decoder = null;
this._readableState.encoding = null;
return;
}

if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
this._readableState.decoder = new StringDecoder(enc);
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-stream-set-encoding-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
require('../common');
const { PassThrough } = require('stream');
const assert = require('assert');

{
const stream = new PassThrough();
stream.setEncoding('utf8');
stream.write('foobar');
assert.strictEqual(stream.read(), 'foobar');
stream.setEncoding(null);
stream.write('foobar');
assert.deepStrictEqual(stream.read(), Buffer.from('foobar'));
}

{
const stream = new PassThrough();
stream.setEncoding('utf8');
stream.write('foobar');
assert.strictEqual(stream.read(), 'foobar');
stream.setEncoding('buffer');
stream.write('foobar');
assert.deepStrictEqual(stream.read(), Buffer.from('foobar'));
}

{
const stream = new PassThrough();
stream.setEncoding('utf8');
stream.write(Buffer.from([0xc3]));
stream.setEncoding('buffer');
assert.strictEqual(stream.read(), '\ufffd');
}