From 83c884b4b7338b34e8fe176ce27b4f55b4c0d929 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 21 May 2022 17:55:42 +0800 Subject: [PATCH] stream: use `kEmptyObject` PR-URL: https://github.com/nodejs/node/pull/43159 Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel --- lib/internal/streams/end-of-stream.js | 9 ++++++--- lib/internal/webstreams/adapters.js | 9 +++++---- lib/internal/webstreams/encoding.js | 3 ++- lib/internal/webstreams/readablestream.js | 11 ++++++----- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 9edd6cc9def..db407fb251c 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -10,7 +10,10 @@ const { const { ERR_STREAM_PREMATURE_CLOSE } = codes; -const { once } = require('internal/util'); +const { + kEmptyObject, + once, +} = require('internal/util'); const { validateAbortSignal, validateFunction, @@ -64,9 +67,9 @@ function isReadableEnded(stream) { function eos(stream, options, callback) { if (arguments.length === 2) { callback = options; - options = {}; + options = kEmptyObject; } else if (options == null) { - options = {}; + options = kEmptyObject; } else { validateObject(options, 'options'); } diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 1058c1c0356..0fd571bfe11 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -55,6 +55,7 @@ const { const { createDeferredPromise, + kEmptyObject, } = require('internal/util'); const { @@ -198,7 +199,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { * }} [options] * @returns {Writable} */ -function newStreamWritableFromWritableStream(writableStream, options = {}) { +function newStreamWritableFromWritableStream(writableStream, options = kEmptyObject) { if (!isWritableStream(writableStream)) { throw new ERR_INVALID_ARG_TYPE( 'writableStream', @@ -441,7 +442,7 @@ function newReadableStreamFromStreamReadable(streamReadable) { * }} [options] * @returns {Readable} */ -function newStreamReadableFromReadableStream(readableStream, options = {}) { +function newStreamReadableFromReadableStream(readableStream, options = kEmptyObject) { if (!isReadableStream(readableStream)) { throw new ERR_INVALID_ARG_TYPE( 'readableStream', @@ -585,7 +586,7 @@ function newReadableWritablePairFromDuplex(duplex) { * }} [options] * @returns {Duplex} */ -function newStreamDuplexFromReadableWritablePair(pair = {}, options = {}) { +function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options = kEmptyObject) { validateObject(pair, 'pair'); const { readable: readableStream, @@ -876,7 +877,7 @@ function newWritableStreamFromStreamBase(streamBase, strategy) { * @param {QueuingStrategy} strategy * @returns {ReadableStream} */ -function newReadableStreamFromStreamBase(streamBase, strategy, options = {}) { +function newReadableStreamFromStreamBase(streamBase, strategy, options = kEmptyObject) { validateObject(streamBase, 'streamBase'); validateObject(options, 'options'); diff --git a/lib/internal/webstreams/encoding.js b/lib/internal/webstreams/encoding.js index 9cc76fae2ce..233a09a216d 100644 --- a/lib/internal/webstreams/encoding.js +++ b/lib/internal/webstreams/encoding.js @@ -24,6 +24,7 @@ const { const { customInspectSymbol: kInspect, + kEmptyObject, kEnumerableProperty, } = require('internal/util'); @@ -114,7 +115,7 @@ class TextDecoderStream { * ignoreBOM? : boolean, * }} [options] */ - constructor(encoding = 'utf-8', options = {}) { + constructor(encoding = 'utf-8', options = kEmptyObject) { this[kType] = 'TextDecoderStream'; this[kHandle] = new TextDecoder(encoding, options); this[kTransform] = new TransformStream({ diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index bea1084bdd7..e49aa5c358b 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -50,6 +50,7 @@ const { const { createDeferredPromise, customInspectSymbol: kInspect, + kEmptyObject, kEnumerableProperty, } = require('internal/util'); @@ -206,7 +207,7 @@ class ReadableStream { * @param {UnderlyingSource} [source] * @param {QueuingStrategy} [strategy] */ - constructor(source = {}, strategy = {}) { + constructor(source = {}, strategy = kEmptyObject) { if (source === null) throw new ERR_INVALID_ARG_VALUE('source', 'Object', source); this[kState] = { @@ -296,7 +297,7 @@ class ReadableStream { * }} [options] * @returns {ReadableStreamReader} */ - getReader(options = {}) { + getReader(options = kEmptyObject) { if (!isReadableStream(this)) throw new ERR_INVALID_THIS('ReadableStream'); validateObject(options, 'options', { nullable: true, allowFunction: true }); @@ -315,7 +316,7 @@ class ReadableStream { * @param {StreamPipeOptions} [options] * @returns {ReadableStream} */ - pipeThrough(transform, options = {}) { + pipeThrough(transform, options = kEmptyObject) { if (!isReadableStream(this)) throw new ERR_INVALID_THIS('ReadableStream'); const readable = transform?.readable; @@ -365,7 +366,7 @@ class ReadableStream { * @param {StreamPipeOptions} [options] * @returns {Promise} */ - pipeTo(destination, options = {}) { + pipeTo(destination, options = kEmptyObject) { try { if (!isReadableStream(this)) throw new ERR_INVALID_THIS('ReadableStream'); @@ -416,7 +417,7 @@ class ReadableStream { * }} [options] * @returns {AsyncIterable} */ - values(options = {}) { + values(options = kEmptyObject) { if (!isReadableStream(this)) throw new ERR_INVALID_THIS('ReadableStream'); validateObject(options, 'options');