Skip to content

Commit

Permalink
stream: use private properties for strategies
Browse files Browse the repository at this point in the history
PR-URL: #47218
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
anonrig authored and nodejs-github-bot committed Apr 13, 2023
1 parent 1fa084e commit dac8de6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
40 changes: 12 additions & 28 deletions lib/internal/webstreams/queuingstrategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {

const {
codes: {
ERR_INVALID_THIS,
ERR_MISSING_OPTION,
},
} = require('internal/errors');
Expand All @@ -20,21 +19,12 @@ const {

const {
customInspect,
isBrandCheck,
kType,
kState,
} = require('internal/webstreams/util');

const {
validateObject,
} = require('internal/validators');

const isByteLengthQueuingStrategy =
isBrandCheck('ByteLengthQueuingStrategy');

const isCountQueuingStrategy =
isBrandCheck('CountQueuingStrategy');

/**
* @callback QueuingStrategySize
* @param {any} chunk
Expand Down Expand Up @@ -68,7 +58,8 @@ const getNonWritablePropertyDescriptor = (value) => {
* @type {QueuingStrategy}
*/
class ByteLengthQueuingStrategy {
[kType] = 'ByteLengthQueuingStrategy';
#state;
#byteSizeFunction = byteSizeFunction;

/**
* @param {{
Expand All @@ -82,7 +73,7 @@ class ByteLengthQueuingStrategy {

// The highWaterMark value is not checked until the strategy
// is actually used, per the spec.
this[kState] = {
this.#state = {
highWaterMark: +init.highWaterMark,
};
}
Expand All @@ -92,22 +83,18 @@ class ByteLengthQueuingStrategy {
* @type {number}
*/
get highWaterMark() {
if (!isByteLengthQueuingStrategy(this))
throw new ERR_INVALID_THIS('ByteLengthQueuingStrategy');
return this[kState].highWaterMark;
return this.#state.highWaterMark;
}

/**
* @type {QueuingStrategySize}
*/
get size() {
if (!isByteLengthQueuingStrategy(this))
throw new ERR_INVALID_THIS('ByteLengthQueuingStrategy');
return byteSizeFunction;
return this.#byteSizeFunction;
}

[kInspect](depth, options) {
return customInspect(depth, options, this[kType], {
return customInspect(depth, options, 'ByteLengthQueuingStrategy', {
highWaterMark: this.highWaterMark,
});
}
Expand All @@ -123,7 +110,8 @@ ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, {
* @type {QueuingStrategy}
*/
class CountQueuingStrategy {
[kType] = 'CountQueuingStrategy';
#state;
#countSizeFunction = countSizeFunction;

/**
* @param {{
Expand All @@ -137,7 +125,7 @@ class CountQueuingStrategy {

// The highWaterMark value is not checked until the strategy
// is actually used, per the spec.
this[kState] = {
this.#state = {
highWaterMark: +init.highWaterMark,
};
}
Expand All @@ -147,22 +135,18 @@ class CountQueuingStrategy {
* @type {number}
*/
get highWaterMark() {
if (!isCountQueuingStrategy(this))
throw new ERR_INVALID_THIS('CountQueuingStrategy');
return this[kState].highWaterMark;
return this.#state.highWaterMark;
}

/**
* @type {QueuingStrategySize}
*/
get size() {
if (!isCountQueuingStrategy(this))
throw new ERR_INVALID_THIS('CountQueuingStrategy');
return countSizeFunction;
return this.#countSizeFunction;
}

[kInspect](depth, options) {
return customInspect(depth, options, this[kType], {
return customInspect(depth, options, 'CountQueuingStrategy', {
highWaterMark: this.highWaterMark,
});
}
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-whatwg-webstreams-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@ assert(isPromisePending(new Promise(() => {})));
assert.throws(() => {
Reflect.get(ByteLengthQueuingStrategy.prototype, 'highWaterMark', {});
}, {
code: 'ERR_INVALID_THIS'
name: 'TypeError',
message: /Cannot read private member/,
});

assert.throws(() => {
Reflect.get(ByteLengthQueuingStrategy.prototype, 'size', {});
}, {
code: 'ERR_INVALID_THIS'
name: 'TypeError',
message: /Cannot read private member/,
});

assert.throws(() => {
Reflect.get(CountQueuingStrategy.prototype, 'highWaterMark', {});
}, {
code: 'ERR_INVALID_THIS'
name: 'TypeError',
message: /Cannot read private member/,
});

assert.throws(() => {
Reflect.get(CountQueuingStrategy.prototype, 'size', {});
}, {
code: 'ERR_INVALID_THIS'
name: 'TypeError',
message: /Cannot read private member/,
});

// Custom Inspect Works
Expand Down

0 comments on commit dac8de6

Please sign in to comment.