UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

156 lines (134 loc) 3.35 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/webstreams/queuingstrategies.js import { codes as __codes__ } from "nstdlib/lib/internal/errors"; import { customInspectSymbol as kInspect, kEnumerableProperty, } from "nstdlib/lib/internal/util"; import { customInspect } from "nstdlib/lib/internal/webstreams/util"; import { validateObject } from "nstdlib/lib/internal/validators"; const { ERR_MISSING_OPTION } = __codes__; /** * @callback QueuingStrategySize * @param {any} chunk * @returns {number} */ /** * @typedef {{ * highWaterMark : number, * size? : QueuingStrategySize, * }} QueuingStrategy */ const nameDescriptor = { __proto__: null, value: "size" }; const byteSizeFunction = Object.defineProperty( (chunk) => chunk.byteLength, "name", nameDescriptor, ); const countSizeFunction = Object.defineProperty( () => 1, "name", nameDescriptor, ); const getNonWritablePropertyDescriptor = (value) => { return { __proto__: null, configurable: true, value, }; }; /** * @type {QueuingStrategy} */ class ByteLengthQueuingStrategy { #state; #byteSizeFunction = byteSizeFunction; /** * @param {{ * highWaterMark : number * }} init */ constructor(init) { validateObject(init, "init"); if (init.highWaterMark === undefined) throw new ERR_MISSING_OPTION("init.highWaterMark"); // The highWaterMark value is not checked until the strategy // is actually used, per the spec. this.#state = { highWaterMark: +init.highWaterMark, }; } /** * @readonly * @type {number} */ get highWaterMark() { return this.#state.highWaterMark; } /** * @type {QueuingStrategySize} */ get size() { return this.#byteSizeFunction; } [kInspect](depth, options) { return customInspect(depth, options, "ByteLengthQueuingStrategy", { highWaterMark: this.highWaterMark, }); } } Object.defineProperties(ByteLengthQueuingStrategy.prototype, { highWaterMark: kEnumerableProperty, size: kEnumerableProperty, [Symbol.toStringTag]: getNonWritablePropertyDescriptor( ByteLengthQueuingStrategy.name, ), }); /** * @type {QueuingStrategy} */ class CountQueuingStrategy { #state; #countSizeFunction = countSizeFunction; /** * @param {{ * highWaterMark : number * }} init */ constructor(init) { validateObject(init, "init"); if (init.highWaterMark === undefined) throw new ERR_MISSING_OPTION("init.highWaterMark"); // The highWaterMark value is not checked until the strategy // is actually used, per the spec. this.#state = { highWaterMark: +init.highWaterMark, }; } /** * @readonly * @type {number} */ get highWaterMark() { return this.#state.highWaterMark; } /** * @type {QueuingStrategySize} */ get size() { return this.#countSizeFunction; } [kInspect](depth, options) { return customInspect(depth, options, "CountQueuingStrategy", { highWaterMark: this.highWaterMark, }); } } Object.defineProperties(CountQueuingStrategy.prototype, { highWaterMark: kEnumerableProperty, size: kEnumerableProperty, [Symbol.toStringTag]: getNonWritablePropertyDescriptor( CountQueuingStrategy.name, ), }); export { ByteLengthQueuingStrategy }; export { CountQueuingStrategy };