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: use readableEncoding public api for child_process #28548

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,16 @@ will be ignored.
Implementors should not override this method, but instead implement
[`readable._destroy()`][readable-_destroy].

##### readable.readableEncoding
ZYSzys marked this conversation as resolved.
Show resolved Hide resolved
<!-- YAML
added: REPLACEME
-->

* {null|string}

Getter for the property `encoding` of a given `Readable` stream. The `encoding`
property can be set using the [`readable.setEncoding()`][] method.

##### readable.isPaused()
<!-- YAML
added: v0.11.14
Expand Down Expand Up @@ -2650,6 +2660,7 @@ contain multi-byte characters.
[`process.stderr`]: process.html#process_process_stderr
[`process.stdin`]: process.html#process_process_stdin
[`process.stdout`]: process.html#process_process_stdout
[`readable.setEncoding()`]: #stream_readable_setencoding_encoding
ZYSzys marked this conversation as resolved.
Show resolved Hide resolved
[`readable.push('')`]: #stream_readable_push
[`stream.Readable.from()`]: #stream_stream_readable_from_iterable_options
[`stream.cork()`]: #stream_writable_cork
Expand Down
7 changes: 7 additions & 0 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,13 @@ Object.defineProperty(Readable.prototype, 'readableObjectMode', {
}
});

Object.defineProperty(Readable.prototype, 'readableEncoding', {
enumerable: false,
get() {
return this._readableState ? this._readableState.encoding : null;
}
});

// Pluck off n bytes from an array of buffers.
// Length is the combined lengths of all the buffers in the list.
// This function is designed to be inlinable, so please take care when making
Expand Down
10 changes: 4 additions & 6 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ function execFile(file /* , args, options, callback */) {
if (encoding ||
(
child.stdout &&
child.stdout._readableState &&
child.stdout._readableState.encoding
child.stdout.readableEncoding
)) {
stdout = _stdout.join('');
} else {
Expand All @@ -276,8 +275,7 @@ function execFile(file /* , args, options, callback */) {
if (encoding ||
(
child.stderr &&
child.stderr._readableState &&
child.stderr._readableState.encoding
child.stderr.readableEncoding
)) {
stderr = _stderr.join('');
} else {
Expand Down Expand Up @@ -344,7 +342,7 @@ function execFile(file /* , args, options, callback */) {
child.stdout.setEncoding(encoding);

child.stdout.on('data', function onChildStdout(chunk) {
const encoding = child.stdout._readableState.encoding;
const encoding = child.stdout.readableEncoding;
const length = encoding ?
Buffer.byteLength(chunk, encoding) :
chunk.length;
Expand All @@ -367,7 +365,7 @@ function execFile(file /* , args, options, callback */) {
child.stderr.setEncoding(encoding);

child.stderr.on('data', function onChildStderr(chunk) {
const encoding = child.stderr._readableState.encoding;
const encoding = child.stderr.readableEncoding;
const length = encoding ?
Buffer.byteLength(chunk, encoding) :
chunk.length;
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-stream2-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,26 @@ class TestWriter extends EE {
assert.strictEqual(r, r2);
}

{
// Verify readableEncoding property
assert(R.prototype.hasOwnProperty('readableEncoding'));

const r = new R({ encoding: 'utf8' });
assert.strictEqual(r.readableEncoding, 'utf8');
}

{
// Verify readableObjectMode property
assert(R.prototype.hasOwnProperty('readableObjectMode'));

const r = new R({ objectMode: true });
assert.strictEqual(r.readableObjectMode, true);
}

{
// Verify writableObjectMode property
assert(W.prototype.hasOwnProperty('writableObjectMode'));

const w = new W({ objectMode: true });
assert.strictEqual(w.writableObjectMode, true);
}