summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZYSzys <zyszys98@gmail.com>2019-07-05 15:54:34 +0800
committerRich Trott <rtrott@gmail.com>2019-07-14 22:27:33 -0700
commit6c430b48b9dff238995aeffe3626bd8b156f563d (patch)
treec49eeb324db47f42257ef2cad0d94dfb309e8f22
parent461bf36d701f3f7c669e2d916d5a5bc17fc447bf (diff)
downloadandroid-node-v8-6c430b48b9dff238995aeffe3626bd8b156f563d.tar.gz
android-node-v8-6c430b48b9dff238995aeffe3626bd8b156f563d.tar.bz2
android-node-v8-6c430b48b9dff238995aeffe3626bd8b156f563d.zip
stream: use readableEncoding public api for child_process
PR-URL: https://github.com/nodejs/node/pull/28548 Refs: https://github.com/nodejs/node/issues/445 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
-rw-r--r--doc/api/stream.md11
-rw-r--r--lib/_stream_readable.js7
-rw-r--r--lib/child_process.js10
-rw-r--r--test/parallel/test-stream2-basic.js12
4 files changed, 34 insertions, 6 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md
index 16edb6d80b..c0a14c7aaa 100644
--- a/doc/api/stream.md
+++ b/doc/api/stream.md
@@ -1085,6 +1085,16 @@ added: v11.4.0
Is `true` if it is safe to call [`readable.read()`][stream-read].
+##### readable.readableEncoding
+<!-- 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.readableHighWaterMark
<!-- YAML
added: v9.3.0
@@ -2655,6 +2665,7 @@ contain multi-byte characters.
[`process.stdin`]: process.html#process_process_stdin
[`process.stdout`]: process.html#process_process_stdout
[`readable.push('')`]: #stream_readable_push
+[`readable.setEncoding()`]: #stream_readable_setencoding_encoding
[`stream.Readable.from()`]: #stream_stream_readable_from_iterable_options
[`stream.cork()`]: #stream_writable_cork
[`stream.finished()`]: #stream_stream_finished_stream_options_callback
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index d2de4122bb..5b73646db8 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -1075,6 +1075,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
diff --git a/lib/child_process.js b/lib/child_process.js
index 8bd6e9c2db..3df73ab5e8 100644
--- a/lib/child_process.js
+++ b/lib/child_process.js
@@ -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 {
@@ -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 {
@@ -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;
@@ -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;
diff --git a/test/parallel/test-stream2-basic.js b/test/parallel/test-stream2-basic.js
index 094ecabd49..5e0f9c6e91 100644
--- a/test/parallel/test-stream2-basic.js
+++ b/test/parallel/test-stream2-basic.js
@@ -423,13 +423,25 @@ class TestWriter extends EE {
}
{
+ // 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);
}