From e4f80547e122fb171fad629baef10761bc4d5909 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Thu, 12 Jan 2023 18:35:44 +0900 Subject: [PATCH] lib: use kEmptyObject and update JSDoc in webstreams Use kEmptyObject as default value of strategy. Plus, make reason and chunk as optional. And refactor to use validateBuffer. Refs: https://github.com/nodejs/node/blob/main/doc/api/webstreams.md#transformstreamdefaultcontrollerenqueuechunk Refs: https://github.com/nodejs/node/blob/main/doc/api/webstreams.md#transformstreamdefaultcontrollererrorreason Refs: https://github.com/nodejs/node/blob/main/doc/api/webstreams.md#writablestreamdefaultwriterabortreason Refs: https://github.com/nodejs/node/blob/main/doc/api/webstreams.md#writablestreamdefaultwriterwritechunk --- lib/internal/webstreams/readablestream.js | 11 +---------- lib/internal/webstreams/transformstream.js | 9 +++++---- lib/internal/webstreams/writablestream.js | 7 ++++--- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 5344785b90cd3e..e83bd17a06d9cb 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -1105,16 +1105,7 @@ class ReadableByteStreamController { enqueue(chunk) { if (!isReadableByteStreamController(this)) throw new ERR_INVALID_THIS('ReadableByteStreamController'); - if (!isArrayBufferView(chunk)) { - throw new ERR_INVALID_ARG_TYPE( - 'chunk', - [ - 'Buffer', - 'TypedArray', - 'DataView', - ], - chunk); - } + validateBuffer(chunk); const chunkByteLength = ArrayBufferViewGetByteLength(chunk); const chunkBuffer = ArrayBufferViewGetBuffer(chunk); const chunkBufferByteLength = ArrayBufferPrototypeGetByteLength(chunkBuffer); diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index 38342b41804385..6e119bde63bb2f 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -26,6 +26,7 @@ const { const { createDeferredPromise, customInspectSymbol: kInspect, + kEmptyObject, kEnumerableProperty, } = require('internal/util'); @@ -117,8 +118,8 @@ class TransformStream { */ constructor( transformer = null, - writableStrategy = {}, - readableStrategy = {}) { + writableStrategy = kEmptyObject, + readableStrategy = kEmptyObject) { const readableType = transformer?.readableType; const writableType = transformer?.writableType; const start = transformer?.start; @@ -292,7 +293,7 @@ class TransformStreamDefaultController { } /** - * @param {any} chunk + * @param {any} [chunk] */ enqueue(chunk = undefined) { if (!isTransformStreamDefaultController(this)) @@ -301,7 +302,7 @@ class TransformStreamDefaultController { } /** - * @param {any} reason + * @param {any} [reason] */ error(reason = undefined) { if (!isTransformStreamDefaultController(this)) diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index ba66cea7a4850d..a86843e176175b 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -33,6 +33,7 @@ const { const { createDeferredPromise, customInspectSymbol: kInspect, + kEmptyObject, kEnumerableProperty, } = require('internal/util'); @@ -133,7 +134,7 @@ class WritableStream { * @param {UnderlyingSink} [sink] * @param {QueuingStrategy} [strategy] */ - constructor(sink = null, strategy = {}) { + constructor(sink = null, strategy = kEmptyObject) { const type = sink?.type; if (type !== undefined) throw new ERR_INVALID_ARG_VALUE.RangeError('type', type); @@ -202,7 +203,7 @@ class WritableStream { } /** - * @param {any} reason + * @param {any} [reason] * @returns {Promise} */ abort(reason = undefined) { @@ -460,7 +461,7 @@ class WritableStreamDefaultWriter { } /** - * @param {any} chunk + * @param {any} [chunk] * @returns {Promise} */ write(chunk = undefined) {