Skip to content

Commit

Permalink
stream: fix web streams have no Symbol.toStringTag
Browse files Browse the repository at this point in the history
stream: fix web streams have no Symbol.toStringTag

stream: add unit tests
PR-URL: #45117
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MrJithil authored and danielleadams committed Jan 3, 2023
1 parent f1aa82f commit 1fb6e82
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
14 changes: 10 additions & 4 deletions lib/internal/webstreams/queuingstrategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,6 +116,7 @@ class ByteLengthQueuingStrategy {
ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, {
highWaterMark: kEnumerableProperty,
size: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ByteLengthQueuingStrategy.name),
});

/**
Expand All @@ -118,8 +125,6 @@ ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, {
class CountQueuingStrategy {
[kType] = 'CountQueuingStrategy';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {{
* highWaterMark : number
Expand Down Expand Up @@ -166,6 +171,7 @@ class CountQueuingStrategy {
ObjectDefineProperties(CountQueuingStrategy.prototype, {
highWaterMark: kEnumerableProperty,
size: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(CountQueuingStrategy.name),
});

module.exports = {
Expand Down
26 changes: 14 additions & 12 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -206,8 +214,6 @@ const kRelease = Symbol('kRelease');
class ReadableStream {
[kType] = 'ReadableStream';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {UnderlyingSource} [source]
* @param {QueuingStrategy} [strategy]
Expand Down Expand Up @@ -604,6 +610,7 @@ ObjectDefineProperties(ReadableStream.prototype, {
pipeThrough: kEnumerableProperty,
pipeTo: kEnumerableProperty,
tee: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStream.name),
});

function TransferredReadableStream() {
Expand All @@ -629,8 +636,6 @@ TransferredReadableStream.prototype[kDeserialize] = () => {};
class ReadableStreamBYOBRequest {
[kType] = 'ReadableStreamBYOBRequest';

get [SymbolToStringTag]() { return this[kType]; }

constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
Expand Down Expand Up @@ -710,6 +715,7 @@ ObjectDefineProperties(ReadableStreamBYOBRequest.prototype, {
view: kEnumerableProperty,
respond: kEnumerableProperty,
respondWithNewView: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamBYOBRequest.name),
});

function createReadableStreamBYOBRequest(controller, view) {
Expand Down Expand Up @@ -769,8 +775,6 @@ class ReadIntoRequest {
class ReadableStreamDefaultReader {
[kType] = 'ReadableStreamDefaultReader';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {ReadableStream} stream
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -1032,6 +1034,7 @@ ObjectDefineProperties(ReadableStreamDefaultController.prototype, {
close: kEnumerableProperty,
enqueue: kEnumerableProperty,
error: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableStreamDefaultController.name),
});

function createReadableStreamDefaultController() {
Expand All @@ -1048,8 +1051,6 @@ function createReadableStreamDefaultController() {
class ReadableByteStreamController {
[kType] = 'ReadableByteStreamController';

get [SymbolToStringTag]() { return this[kType]; }

constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
Expand Down Expand Up @@ -1168,6 +1169,7 @@ ObjectDefineProperties(ReadableByteStreamController.prototype, {
close: kEnumerableProperty,
enqueue: kEnumerableProperty,
error: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(ReadableByteStreamController.name),
});

function createReadableByteStreamController() {
Expand Down
14 changes: 10 additions & 4 deletions lib/internal/webstreams/transformstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ const {

const assert = require('internal/assert');

const getNonWritablePropertyDescriptor = (value) => {
return {
__proto__: null,
configurable: true,
value,
};
};

/**
* @typedef {import('./queuingstrategies').QueuingStrategy
* } QueuingStrategy
Expand Down Expand Up @@ -102,8 +110,6 @@ const assert = require('internal/assert');
class TransformStream {
[kType] = 'TransformStream';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {Transformer} [transformer]
* @param {QueuingStrategy} [writableStrategy]
Expand Down Expand Up @@ -236,6 +242,7 @@ class TransformStream {
ObjectDefineProperties(TransformStream.prototype, {
readable: kEnumerableProperty,
writable: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStream.name),
});

function TransferredTransformStream() {
Expand All @@ -261,8 +268,6 @@ TransferredTransformStream.prototype[kDeserialize] = () => {};
class TransformStreamDefaultController {
[kType] = 'TransformStreamDefaultController';

get [SymbolToStringTag]() { return this[kType]; }

constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
Expand Down Expand Up @@ -322,6 +327,7 @@ ObjectDefineProperties(TransformStreamDefaultController.prototype, {
enqueue: kEnumerableProperty,
error: kEnumerableProperty,
terminate: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name),
});

function createTransformStreamDefaultController() {
Expand Down
17 changes: 11 additions & 6 deletions lib/internal/webstreams/writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -121,8 +129,6 @@ const kError = Symbol('kError');
class WritableStream {
[kType] = 'WritableStream';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {UnderlyingSink} [sink]
* @param {QueuingStrategy} [strategy]
Expand Down Expand Up @@ -295,6 +301,7 @@ ObjectDefineProperties(WritableStream.prototype, {
abort: kEnumerableProperty,
close: kEnumerableProperty,
getWriter: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStream.name),
});

function TransferredWritableStream() {
Expand Down Expand Up @@ -348,8 +355,6 @@ TransferredWritableStream.prototype[kDeserialize] = () => {};
class WritableStreamDefaultWriter {
[kType] = 'WritableStreamDefaultWriter';

get [SymbolToStringTag]() { return this[kType]; }

/**
* @param {WritableStream} stream
*/
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -538,6 +542,7 @@ class WritableStreamDefaultController {
ObjectDefineProperties(WritableStreamDefaultController.prototype, {
signal: kEnumerableProperty,
error: kEnumerableProperty,
[SymbolToStringTag]: getNonWritablePropertyDescriptor(WritableStreamDefaultController.name),
});

function createWritableStreamDefaultController() {
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-webstream-string-tag.js
Original file line number Diff line number Diff line change
@@ -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 });
});

0 comments on commit 1fb6e82

Please sign in to comment.