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
  • Loading branch information
MrJithil committed Oct 25, 2022
1 parent bb7650b commit eda0c87
Show file tree
Hide file tree
Showing 5 changed files with 80 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 getSymbolToStringTagProperty = (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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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 getSymbolToStringTagProperty = (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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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 getSymbolToStringTagProperty = (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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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 getSymbolToStringTagProperty = (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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(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]: getSymbolToStringTagProperty(WritableStreamDefaultController.name)
});

function createWritableStreamDefaultController() {
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-webstream-string-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

require('../common');

const assert = require('assert');

assert.strictEqual(WritableStream.prototype[Symbol.toStringTag], 'WritableStream');
assert.strictEqual(WritableStreamDefaultWriter.prototype[Symbol.toStringTag], 'WritableStreamDefaultWriter');
assert.strictEqual(WritableStreamDefaultController.prototype[Symbol.toStringTag], 'WritableStreamDefaultController');

assert.strictEqual(ReadableStream.prototype[Symbol.toStringTag], 'ReadableStream');
assert.strictEqual(ReadableStreamBYOBRequest.prototype[Symbol.toStringTag], 'ReadableStreamBYOBRequest');
assert.strictEqual(ReadableStreamDefaultReader.prototype[Symbol.toStringTag], 'ReadableStreamDefaultReader');
assert.strictEqual(ReadableStreamBYOBReader.prototype[Symbol.toStringTag], 'ReadableStreamBYOBReader');
assert.strictEqual(ReadableStreamDefaultController.prototype[Symbol.toStringTag], 'ReadableStreamDefaultController');
assert.strictEqual(ReadableByteStreamController.prototype[Symbol.toStringTag], 'ReadableByteStreamController');

assert.strictEqual(ByteLengthQueuingStrategy.prototype[Symbol.toStringTag], 'ByteLengthQueuingStrategy');
assert.strictEqual(CountQueuingStrategy.prototype[Symbol.toStringTag], 'CountQueuingStrategy');

assert.strictEqual(TransformStream.prototype[Symbol.toStringTag], 'TransformStream');
assert.strictEqual(TransformStreamDefaultController.prototype[Symbol.toStringTag], 'TransformStreamDefaultController');


const classesToBeTested = [ WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController,
ReadableStream, ReadableStreamBYOBRequest, ReadableStreamDefaultReader,
ReadableStreamBYOBReader, ReadableStreamDefaultController, ReadableByteStreamController,
ByteLengthQueuingStrategy, CountQueuingStrategy, TransformStream,
TransformStreamDefaultController];


classesToBeTested.forEach((cls) => {
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(cls.prototype, Symbol.toStringTag),
{ configurable: true, enumerable: false, value: cls.name, writable: false });
});

0 comments on commit eda0c87

Please sign in to comment.