From b200a5ff67b7e91c1579221f113abce815241cb8 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 0b2044d53e735b..806280ebc1f1d2 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -11,7 +11,10 @@ const { ERR_INVALID_ARG_TYPE, ERR_STREAM_PREMATURE_CLOSE } = codes; -const { once } = require('internal/util'); +const { + kEmptyObject, + once, +} = require('internal/util'); const { validateAbortSignal, validateFunction, @@ -43,9 +46,9 @@ const nop = () => {}; 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 1058c1c0356ead..0fd571bfe11d19 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 9cc76fae2ce10d..233a09a216d72d 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 33496587043523..b44df2ff77896d 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');