From 1fb6e82f845bd24343cae03216429dfdacb8a24e Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Thu, 27 Oct 2022 02:48:48 +1100 Subject: [PATCH] stream: fix web streams have no Symbol.toStringTag stream: fix web streams have no Symbol.toStringTag stream: add unit tests PR-URL: https://github.com/nodejs/node/pull/45117 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum --- lib/internal/webstreams/queuingstrategies.js | 14 ++++++++--- lib/internal/webstreams/readablestream.js | 26 +++++++++++--------- lib/internal/webstreams/transformstream.js | 14 ++++++++--- lib/internal/webstreams/writablestream.js | 17 ++++++++----- test/parallel/test-webstream-string-tag.js | 18 ++++++++++++++ 5 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 test/parallel/test-webstream-string-tag.js diff --git a/lib/internal/webstreams/queuingstrategies.js b/lib/internal/webstreams/queuingstrategies.js index 32d58af1dfad19..719fa254f5cf8d 100644 --- a/lib/internal/webstreams/queuingstrategies.js +++ b/lib/internal/webstreams/queuingstrategies.js @@ -56,14 +56,20 @@ const byteSizeFunction = ObjectDefineProperty( ); const countSizeFunction = ObjectDefineProperty(() => 1, 'name', nameDescriptor); +const getNonWritablePropertyDescriptor = (value) => { + return { + __proto__: null, + configurable: true, + value, + }; +}; + /** * @type {QueuingStrategy} */ class ByteLengthQueuingStrategy { [kType] = 'ByteLengthQueuingStrategy'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {{ * highWaterMark : number @@ -110,6 +116,7 @@ class ByteLengthQueuingStrategy { ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, { highWaterMark: kEnumerableProperty, size: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ByteLengthQueuingStrategy.name), }); /** @@ -118,8 +125,6 @@ ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, { class CountQueuingStrategy { [kType] = 'CountQueuingStrategy'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {{ * highWaterMark : number @@ -166,6 +171,7 @@ class CountQueuingStrategy { ObjectDefineProperties(CountQueuingStrategy.prototype, { highWaterMark: kEnumerableProperty, size: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(CountQueuingStrategy.name), }); module.exports = { diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index ade543c957dd5a..252f8eaa1a8780 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -141,6 +141,14 @@ const kError = Symbol('kError'); const kPull = Symbol('kPull'); const kRelease = Symbol('kRelease'); +const getNonWritablePropertyDescriptor = (value) => { + return { + __proto__: null, + configurable: true, + value, + }; +}; + /** * @typedef {import('../abort_controller').AbortSignal} AbortSignal * @typedef {import('./queuingstrategies').QueuingStrategy} QueuingStrategy @@ -206,8 +214,6 @@ const kRelease = Symbol('kRelease'); class ReadableStream { [kType] = 'ReadableStream'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {UnderlyingSource} [source] * @param {QueuingStrategy} [strategy] @@ -604,6 +610,7 @@ ObjectDefineProperties(ReadableStream.prototype, { pipeThrough: kEnumerableProperty, pipeTo: kEnumerableProperty, tee: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStream.name), }); function TransferredReadableStream() { @@ -629,8 +636,6 @@ TransferredReadableStream.prototype[kDeserialize] = () => {}; class ReadableStreamBYOBRequest { [kType] = 'ReadableStreamBYOBRequest'; - get [SymbolToStringTag]() { return this[kType]; } - constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -710,6 +715,7 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, { view: kEnumerableProperty, respond: kEnumerableProperty, respondWithNewView: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamBYOBRequest.name), }); function createReadableStreamBYOBRequest(controller, view) { @@ -769,8 +775,6 @@ class ReadIntoRequest { class ReadableStreamDefaultReader { [kType] = 'ReadableStreamDefaultReader'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {ReadableStream} stream */ @@ -854,13 +858,12 @@ ObjectDefineProperties(ReadableStreamDefaultReader.prototype, { read: kEnumerableProperty, releaseLock: kEnumerableProperty, cancel: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultReader.name), }); class ReadableStreamBYOBReader { [kType] = 'ReadableStreamBYOBReader'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {ReadableStream} stream */ @@ -971,13 +974,12 @@ ObjectDefineProperties(ReadableStreamBYOBReader.prototype, { read: kEnumerableProperty, releaseLock: kEnumerableProperty, cancel: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamBYOBReader.name), }); class ReadableStreamDefaultController { [kType] = 'ReadableStreamDefaultController'; - get [SymbolToStringTag]() { return this[kType]; } - constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -1032,6 +1034,7 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, { close: kEnumerableProperty, enqueue: kEnumerableProperty, error: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name), }); function createReadableStreamDefaultController() { @@ -1048,8 +1051,6 @@ function createReadableStreamDefaultController() { class ReadableByteStreamController { [kType] = 'ReadableByteStreamController'; - get [SymbolToStringTag]() { return this[kType]; } - constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -1168,6 +1169,7 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, { close: kEnumerableProperty, enqueue: kEnumerableProperty, error: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name), }); function createReadableByteStreamController() { diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index b7366fb1ba2576..38342b41804385 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -64,6 +64,14 @@ const { const assert = require('internal/assert'); +const getNonWritablePropertyDescriptor = (value) => { + return { + __proto__: null, + configurable: true, + value, + }; +}; + /** * @typedef {import('./queuingstrategies').QueuingStrategy * } QueuingStrategy @@ -102,8 +110,6 @@ const assert = require('internal/assert'); class TransformStream { [kType] = 'TransformStream'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {Transformer} [transformer] * @param {QueuingStrategy} [writableStrategy] @@ -236,6 +242,7 @@ class TransformStream { ObjectDefineProperties(TransformStream.prototype, { readable: kEnumerableProperty, writable: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStream.name), }); function TransferredTransformStream() { @@ -261,8 +268,6 @@ TransferredTransformStream.prototype[kDeserialize] = () => {}; class TransformStreamDefaultController { [kType] = 'TransformStreamDefaultController'; - get [SymbolToStringTag]() { return this[kType]; } - constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -322,6 +327,7 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, { enqueue: kEnumerableProperty, error: kEnumerableProperty, terminate: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name), }); function createTransformStreamDefaultController() { diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index 1e1b64faceab57..9c58019dc1aa5a 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -77,6 +77,14 @@ const kAbort = Symbol('kAbort'); const kCloseSentinel = Symbol('kCloseSentinel'); const kError = Symbol('kError'); +const getNonWritablePropertyDescriptor = (value) => { + return { + __proto__: null, + configurable: true, + value, + }; +}; + /** * @typedef {import('../abort_controller').AbortSignal} AbortSignal * @typedef {import('./queuingstrategies').QueuingStrategy @@ -121,8 +129,6 @@ const kError = Symbol('kError'); class WritableStream { [kType] = 'WritableStream'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {UnderlyingSink} [sink] * @param {QueuingStrategy} [strategy] @@ -295,6 +301,7 @@ ObjectDefineProperties(WritableStream.prototype, { abort: kEnumerableProperty, close: kEnumerableProperty, getWriter: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStream.name), }); function TransferredWritableStream() { @@ -348,8 +355,6 @@ TransferredWritableStream.prototype[kDeserialize] = () => {}; class WritableStreamDefaultWriter { [kType] = 'WritableStreamDefaultWriter'; - get [SymbolToStringTag]() { return this[kType]; } - /** * @param {WritableStream} stream */ @@ -487,13 +492,12 @@ ObjectDefineProperties(WritableStreamDefaultWriter.prototype, { close: kEnumerableProperty, releaseLock: kEnumerableProperty, write: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultWriter.name), }); class WritableStreamDefaultController { [kType] = 'WritableStreamDefaultController'; - get [SymbolToStringTag]() { return this[kType]; } - constructor() { throw new ERR_ILLEGAL_CONSTRUCTOR(); } @@ -538,6 +542,7 @@ class WritableStreamDefaultController { ObjectDefineProperties(WritableStreamDefaultController.prototype, { signal: kEnumerableProperty, error: kEnumerableProperty, + [SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name), }); function createWritableStreamDefaultController() { diff --git a/test/parallel/test-webstream-string-tag.js b/test/parallel/test-webstream-string-tag.js new file mode 100644 index 00000000000000..980a204a9b0204 --- /dev/null +++ b/test/parallel/test-webstream-string-tag.js @@ -0,0 +1,18 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); + +const classesToBeTested = [ WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController, + ReadableStream, ReadableStreamBYOBRequest, ReadableStreamDefaultReader, + ReadableStreamBYOBReader, ReadableStreamDefaultController, ReadableByteStreamController, + ByteLengthQueuingStrategy, CountQueuingStrategy, TransformStream, + TransformStreamDefaultController]; + + +classesToBeTested.forEach((cls) => { + assert.strictEqual(cls.prototype[Symbol.toStringTag], cls.name); + assert.deepStrictEqual(Object.getOwnPropertyDescriptor(cls.prototype, Symbol.toStringTag), + { configurable: true, enumerable: false, value: cls.name, writable: false }); +});