1'use strict'; 2 3const { 4 ObjectDefineProperties, 5 ObjectDefineProperty, 6 SymbolToStringTag, 7} = primordials; 8 9const { 10 codes: { 11 ERR_INVALID_THIS, 12 ERR_MISSING_OPTION, 13 }, 14} = require('internal/errors'); 15 16const { 17 customInspectSymbol: kInspect, 18 kEnumerableProperty, 19} = require('internal/util'); 20 21const { 22 customInspect, 23 isBrandCheck, 24 kType, 25 kState, 26} = require('internal/webstreams/util'); 27 28const { 29 validateObject, 30} = require('internal/validators'); 31 32const isByteLengthQueuingStrategy = 33 isBrandCheck('ByteLengthQueuingStrategy'); 34 35const isCountQueuingStrategy = 36 isBrandCheck('CountQueuingStrategy'); 37 38/** 39 * @callback QueuingStrategySize 40 * @param {any} chunk 41 * @returns {number} 42 */ 43 44/** 45 * @typedef {{ 46 * highWaterMark : number, 47 * size? : QueuingStrategySize, 48 * }} QueuingStrategy 49 */ 50 51const nameDescriptor = { __proto__: null, value: 'size' }; 52const byteSizeFunction = ObjectDefineProperty( 53 (chunk) => chunk.byteLength, 54 'name', 55 nameDescriptor, 56); 57const countSizeFunction = ObjectDefineProperty(() => 1, 'name', nameDescriptor); 58 59const getNonWritablePropertyDescriptor = (value) => { 60 return { 61 __proto__: null, 62 configurable: true, 63 value, 64 }; 65}; 66 67/** 68 * @type {QueuingStrategy} 69 */ 70class ByteLengthQueuingStrategy { 71 [kType] = 'ByteLengthQueuingStrategy'; 72 73 /** 74 * @param {{ 75 * highWaterMark : number 76 * }} init 77 */ 78 constructor(init) { 79 validateObject(init, 'init'); 80 if (init.highWaterMark === undefined) 81 throw new ERR_MISSING_OPTION('init.highWaterMark'); 82 83 // The highWaterMark value is not checked until the strategy 84 // is actually used, per the spec. 85 this[kState] = { 86 highWaterMark: +init.highWaterMark, 87 }; 88 } 89 90 /** 91 * @readonly 92 * @type {number} 93 */ 94 get highWaterMark() { 95 if (!isByteLengthQueuingStrategy(this)) 96 throw new ERR_INVALID_THIS('ByteLengthQueuingStrategy'); 97 return this[kState].highWaterMark; 98 } 99 100 /** 101 * @type {QueuingStrategySize} 102 */ 103 get size() { 104 if (!isByteLengthQueuingStrategy(this)) 105 throw new ERR_INVALID_THIS('ByteLengthQueuingStrategy'); 106 return byteSizeFunction; 107 } 108 109 [kInspect](depth, options) { 110 return customInspect(depth, options, this[kType], { 111 highWaterMark: this.highWaterMark, 112 }); 113 } 114} 115 116ObjectDefineProperties(ByteLengthQueuingStrategy.prototype, { 117 highWaterMark: kEnumerableProperty, 118 size: kEnumerableProperty, 119 [SymbolToStringTag]: getNonWritablePropertyDescriptor(ByteLengthQueuingStrategy.name), 120}); 121 122/** 123 * @type {QueuingStrategy} 124 */ 125class CountQueuingStrategy { 126 [kType] = 'CountQueuingStrategy'; 127 128 /** 129 * @param {{ 130 * highWaterMark : number 131 * }} init 132 */ 133 constructor(init) { 134 validateObject(init, 'init'); 135 if (init.highWaterMark === undefined) 136 throw new ERR_MISSING_OPTION('init.highWaterMark'); 137 138 // The highWaterMark value is not checked until the strategy 139 // is actually used, per the spec. 140 this[kState] = { 141 highWaterMark: +init.highWaterMark, 142 }; 143 } 144 145 /** 146 * @readonly 147 * @type {number} 148 */ 149 get highWaterMark() { 150 if (!isCountQueuingStrategy(this)) 151 throw new ERR_INVALID_THIS('CountQueuingStrategy'); 152 return this[kState].highWaterMark; 153 } 154 155 /** 156 * @type {QueuingStrategySize} 157 */ 158 get size() { 159 if (!isCountQueuingStrategy(this)) 160 throw new ERR_INVALID_THIS('CountQueuingStrategy'); 161 return countSizeFunction; 162 } 163 164 [kInspect](depth, options) { 165 return customInspect(depth, options, this[kType], { 166 highWaterMark: this.highWaterMark, 167 }); 168 } 169} 170 171ObjectDefineProperties(CountQueuingStrategy.prototype, { 172 highWaterMark: kEnumerableProperty, 173 size: kEnumerableProperty, 174 [SymbolToStringTag]: getNonWritablePropertyDescriptor(CountQueuingStrategy.name), 175}); 176 177module.exports = { 178 ByteLengthQueuingStrategy, 179 CountQueuingStrategy, 180}; 181