Skip to content

Commit 00b2200

Browse files
ZYSzystargos
authored andcommitted
stream: use readableEncoding public api for child_process
PR-URL: #28548 Refs: #445 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 499969d commit 00b2200

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

doc/api/stream.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,16 @@ added: v11.4.0
10851085

10861086
Is `true` if it is safe to call [`readable.read()`][stream-read].
10871087

1088+
##### readable.readableEncoding
1089+
<!-- YAML
1090+
added: REPLACEME
1091+
-->
1092+
1093+
* {null|string}
1094+
1095+
Getter for the property `encoding` of a given `Readable` stream. The `encoding`
1096+
property can be set using the [`readable.setEncoding()`][] method.
1097+
10881098
##### readable.readableHighWaterMark
10891099
<!-- YAML
10901100
added: v9.3.0
@@ -2655,6 +2665,7 @@ contain multi-byte characters.
26552665
[`process.stdin`]: process.html#process_process_stdin
26562666
[`process.stdout`]: process.html#process_process_stdout
26572667
[`readable.push('')`]: #stream_readable_push
2668+
[`readable.setEncoding()`]: #stream_readable_setencoding_encoding
26582669
[`stream.Readable.from()`]: #stream_stream_readable_from_iterable_options
26592670
[`stream.cork()`]: #stream_writable_cork
26602671
[`stream.finished()`]: #stream_stream_finished_stream_options_callback

lib/_stream_readable.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,13 @@ Object.defineProperty(Readable.prototype, 'readableObjectMode', {
11051105
}
11061106
});
11071107

1108+
Object.defineProperty(Readable.prototype, 'readableEncoding', {
1109+
enumerable: false,
1110+
get() {
1111+
return this._readableState ? this._readableState.encoding : null;
1112+
}
1113+
});
1114+
11081115
// Pluck off n bytes from an array of buffers.
11091116
// Length is the combined lengths of all the buffers in the list.
11101117
// This function is designed to be inlinable, so please take care when making

lib/child_process.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ function execFile(file /* , args, options, callback */) {
266266
if (encoding ||
267267
(
268268
child.stdout &&
269-
child.stdout._readableState &&
270-
child.stdout._readableState.encoding
269+
child.stdout.readableEncoding
271270
)) {
272271
stdout = _stdout.join('');
273272
} else {
@@ -276,8 +275,7 @@ function execFile(file /* , args, options, callback */) {
276275
if (encoding ||
277276
(
278277
child.stderr &&
279-
child.stderr._readableState &&
280-
child.stderr._readableState.encoding
278+
child.stderr.readableEncoding
281279
)) {
282280
stderr = _stderr.join('');
283281
} else {
@@ -344,7 +342,7 @@ function execFile(file /* , args, options, callback */) {
344342
child.stdout.setEncoding(encoding);
345343

346344
child.stdout.on('data', function onChildStdout(chunk) {
347-
const encoding = child.stdout._readableState.encoding;
345+
const encoding = child.stdout.readableEncoding;
348346
const length = encoding ?
349347
Buffer.byteLength(chunk, encoding) :
350348
chunk.length;
@@ -367,7 +365,7 @@ function execFile(file /* , args, options, callback */) {
367365
child.stderr.setEncoding(encoding);
368366

369367
child.stderr.on('data', function onChildStderr(chunk) {
370-
const encoding = child.stderr._readableState.encoding;
368+
const encoding = child.stderr.readableEncoding;
371369
const length = encoding ?
372370
Buffer.byteLength(chunk, encoding) :
373371
chunk.length;

test/parallel/test-stream2-basic.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,26 @@ class TestWriter extends EE {
422422
assert.strictEqual(r, r2);
423423
}
424424

425+
{
426+
// Verify readableEncoding property
427+
assert(R.prototype.hasOwnProperty('readableEncoding'));
428+
429+
const r = new R({ encoding: 'utf8' });
430+
assert.strictEqual(r.readableEncoding, 'utf8');
431+
}
432+
425433
{
426434
// Verify readableObjectMode property
435+
assert(R.prototype.hasOwnProperty('readableObjectMode'));
436+
427437
const r = new R({ objectMode: true });
428438
assert.strictEqual(r.readableObjectMode, true);
429439
}
430440

431441
{
432442
// Verify writableObjectMode property
443+
assert(W.prototype.hasOwnProperty('writableObjectMode'));
444+
433445
const w = new W({ objectMode: true });
434446
assert.strictEqual(w.writableObjectMode, true);
435447
}

0 commit comments

Comments
 (0)