11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  FunctionPrototypeBind,
51cb0ef41Sopenharmony_ci  FunctionPrototypeCall,
61cb0ef41Sopenharmony_ci  ObjectDefineProperties,
71cb0ef41Sopenharmony_ci  PromisePrototypeThen,
81cb0ef41Sopenharmony_ci  PromiseResolve,
91cb0ef41Sopenharmony_ci  ReflectConstruct,
101cb0ef41Sopenharmony_ci  SymbolToStringTag,
111cb0ef41Sopenharmony_ci  Symbol,
121cb0ef41Sopenharmony_ci} = primordials;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst {
151cb0ef41Sopenharmony_ci  codes: {
161cb0ef41Sopenharmony_ci    ERR_ILLEGAL_CONSTRUCTOR,
171cb0ef41Sopenharmony_ci    ERR_INVALID_ARG_VALUE,
181cb0ef41Sopenharmony_ci    ERR_INVALID_STATE,
191cb0ef41Sopenharmony_ci    ERR_INVALID_THIS,
201cb0ef41Sopenharmony_ci  },
211cb0ef41Sopenharmony_ci} = require('internal/errors');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst {
241cb0ef41Sopenharmony_ci  DOMException,
251cb0ef41Sopenharmony_ci} = internalBinding('messaging');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst {
281cb0ef41Sopenharmony_ci  createDeferredPromise,
291cb0ef41Sopenharmony_ci  customInspectSymbol: kInspect,
301cb0ef41Sopenharmony_ci  kEmptyObject,
311cb0ef41Sopenharmony_ci  kEnumerableProperty,
321cb0ef41Sopenharmony_ci} = require('internal/util');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst {
351cb0ef41Sopenharmony_ci  kDeserialize,
361cb0ef41Sopenharmony_ci  kTransfer,
371cb0ef41Sopenharmony_ci  kTransferList,
381cb0ef41Sopenharmony_ci  makeTransferable,
391cb0ef41Sopenharmony_ci} = require('internal/worker/js_transferable');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciconst {
421cb0ef41Sopenharmony_ci  customInspect,
431cb0ef41Sopenharmony_ci  ensureIsPromise,
441cb0ef41Sopenharmony_ci  extractHighWaterMark,
451cb0ef41Sopenharmony_ci  extractSizeAlgorithm,
461cb0ef41Sopenharmony_ci  isBrandCheck,
471cb0ef41Sopenharmony_ci  nonOpFlush,
481cb0ef41Sopenharmony_ci  kType,
491cb0ef41Sopenharmony_ci  kState,
501cb0ef41Sopenharmony_ci} = require('internal/webstreams/util');
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciconst {
531cb0ef41Sopenharmony_ci  ReadableStream,
541cb0ef41Sopenharmony_ci  readableStreamDefaultControllerCanCloseOrEnqueue,
551cb0ef41Sopenharmony_ci  readableStreamDefaultControllerClose,
561cb0ef41Sopenharmony_ci  readableStreamDefaultControllerEnqueue,
571cb0ef41Sopenharmony_ci  readableStreamDefaultControllerError,
581cb0ef41Sopenharmony_ci  readableStreamDefaultControllerGetDesiredSize,
591cb0ef41Sopenharmony_ci  readableStreamDefaultControllerHasBackpressure,
601cb0ef41Sopenharmony_ci} = require('internal/webstreams/readablestream');
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciconst {
631cb0ef41Sopenharmony_ci  WritableStream,
641cb0ef41Sopenharmony_ci  writableStreamDefaultControllerErrorIfNeeded,
651cb0ef41Sopenharmony_ci} = require('internal/webstreams/writablestream');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciconst assert = require('internal/assert');
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ciconst kSkipThrow = Symbol('kSkipThrow');
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciconst getNonWritablePropertyDescriptor = (value) => {
721cb0ef41Sopenharmony_ci  return {
731cb0ef41Sopenharmony_ci    __proto__: null,
741cb0ef41Sopenharmony_ci    configurable: true,
751cb0ef41Sopenharmony_ci    value,
761cb0ef41Sopenharmony_ci  };
771cb0ef41Sopenharmony_ci};
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci/**
801cb0ef41Sopenharmony_ci * @typedef {import('./queuingstrategies').QueuingStrategy
811cb0ef41Sopenharmony_ci * } QueuingStrategy
821cb0ef41Sopenharmony_ci * @typedef {import('./queuingstrategies').QueuingStrategySize
831cb0ef41Sopenharmony_ci * } QueuingStrategySize
841cb0ef41Sopenharmony_ci */
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci/**
871cb0ef41Sopenharmony_ci * @callback TransformerStartCallback
881cb0ef41Sopenharmony_ci * @param {TransformStreamDefaultController} controller;
891cb0ef41Sopenharmony_ci */
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci/**
921cb0ef41Sopenharmony_ci * @callback TransformerFlushCallback
931cb0ef41Sopenharmony_ci * @param {TransformStreamDefaultController} controller;
941cb0ef41Sopenharmony_ci * @returns {Promise<void>}
951cb0ef41Sopenharmony_ci */
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci/**
981cb0ef41Sopenharmony_ci * @callback TransformerTransformCallback
991cb0ef41Sopenharmony_ci * @param {any} chunk
1001cb0ef41Sopenharmony_ci * @param {TransformStreamDefaultController} controller
1011cb0ef41Sopenharmony_ci * @returns {Promise<void>}
1021cb0ef41Sopenharmony_ci */
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci/**
1051cb0ef41Sopenharmony_ci * @typedef {{
1061cb0ef41Sopenharmony_ci *  start? : TransformerStartCallback,
1071cb0ef41Sopenharmony_ci *  transform? : TransformerTransformCallback,
1081cb0ef41Sopenharmony_ci *  flush? : TransformerFlushCallback,
1091cb0ef41Sopenharmony_ci *  readableType? : any,
1101cb0ef41Sopenharmony_ci *  writableType? : any,
1111cb0ef41Sopenharmony_ci * }} Transformer
1121cb0ef41Sopenharmony_ci */
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciclass TransformStream {
1151cb0ef41Sopenharmony_ci  [kType] = 'TransformStream';
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  /**
1181cb0ef41Sopenharmony_ci   * @param {Transformer} [transformer]
1191cb0ef41Sopenharmony_ci   * @param {QueuingStrategy} [writableStrategy]
1201cb0ef41Sopenharmony_ci   * @param {QueuingStrategy} [readableStrategy]
1211cb0ef41Sopenharmony_ci   */
1221cb0ef41Sopenharmony_ci  constructor(
1231cb0ef41Sopenharmony_ci    transformer = null,
1241cb0ef41Sopenharmony_ci    writableStrategy = kEmptyObject,
1251cb0ef41Sopenharmony_ci    readableStrategy = kEmptyObject) {
1261cb0ef41Sopenharmony_ci    const readableType = transformer?.readableType;
1271cb0ef41Sopenharmony_ci    const writableType = transformer?.writableType;
1281cb0ef41Sopenharmony_ci    const start = transformer?.start;
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci    if (readableType !== undefined) {
1311cb0ef41Sopenharmony_ci      throw new ERR_INVALID_ARG_VALUE.RangeError(
1321cb0ef41Sopenharmony_ci        'transformer.readableType',
1331cb0ef41Sopenharmony_ci        readableType);
1341cb0ef41Sopenharmony_ci    }
1351cb0ef41Sopenharmony_ci    if (writableType !== undefined) {
1361cb0ef41Sopenharmony_ci      throw new ERR_INVALID_ARG_VALUE.RangeError(
1371cb0ef41Sopenharmony_ci        'transformer.writableType',
1381cb0ef41Sopenharmony_ci        writableType);
1391cb0ef41Sopenharmony_ci    }
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci    const readableHighWaterMark = readableStrategy?.highWaterMark;
1421cb0ef41Sopenharmony_ci    const readableSize = readableStrategy?.size;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    const writableHighWaterMark = writableStrategy?.highWaterMark;
1451cb0ef41Sopenharmony_ci    const writableSize = writableStrategy?.size;
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci    const actualReadableHighWaterMark =
1481cb0ef41Sopenharmony_ci      extractHighWaterMark(readableHighWaterMark, 0);
1491cb0ef41Sopenharmony_ci    const actualReadableSize = extractSizeAlgorithm(readableSize);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci    const actualWritableHighWaterMark =
1521cb0ef41Sopenharmony_ci      extractHighWaterMark(writableHighWaterMark, 1);
1531cb0ef41Sopenharmony_ci    const actualWritableSize = extractSizeAlgorithm(writableSize);
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci    const startPromise = createDeferredPromise();
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci    initializeTransformStream(
1581cb0ef41Sopenharmony_ci      this,
1591cb0ef41Sopenharmony_ci      startPromise,
1601cb0ef41Sopenharmony_ci      actualWritableHighWaterMark,
1611cb0ef41Sopenharmony_ci      actualWritableSize,
1621cb0ef41Sopenharmony_ci      actualReadableHighWaterMark,
1631cb0ef41Sopenharmony_ci      actualReadableSize);
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci    setupTransformStreamDefaultControllerFromTransformer(this, transformer);
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci    if (start !== undefined) {
1681cb0ef41Sopenharmony_ci      startPromise.resolve(
1691cb0ef41Sopenharmony_ci        FunctionPrototypeCall(
1701cb0ef41Sopenharmony_ci          start,
1711cb0ef41Sopenharmony_ci          transformer,
1721cb0ef41Sopenharmony_ci          this[kState].controller));
1731cb0ef41Sopenharmony_ci    } else {
1741cb0ef41Sopenharmony_ci      startPromise.resolve();
1751cb0ef41Sopenharmony_ci    }
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-constructor-return
1781cb0ef41Sopenharmony_ci    return makeTransferable(this);
1791cb0ef41Sopenharmony_ci  }
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci  /**
1821cb0ef41Sopenharmony_ci   * @readonly
1831cb0ef41Sopenharmony_ci   * @type {ReadableStream}
1841cb0ef41Sopenharmony_ci   */
1851cb0ef41Sopenharmony_ci  get readable() {
1861cb0ef41Sopenharmony_ci    if (!isTransformStream(this))
1871cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStream');
1881cb0ef41Sopenharmony_ci    return this[kState].readable;
1891cb0ef41Sopenharmony_ci  }
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  /**
1921cb0ef41Sopenharmony_ci   * @readonly
1931cb0ef41Sopenharmony_ci   * @type {WritableStream}
1941cb0ef41Sopenharmony_ci   */
1951cb0ef41Sopenharmony_ci  get writable() {
1961cb0ef41Sopenharmony_ci    if (!isTransformStream(this))
1971cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStream');
1981cb0ef41Sopenharmony_ci    return this[kState].writable;
1991cb0ef41Sopenharmony_ci  }
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  [kInspect](depth, options) {
2021cb0ef41Sopenharmony_ci    return customInspect(depth, options, this[kType], {
2031cb0ef41Sopenharmony_ci      readable: this.readable,
2041cb0ef41Sopenharmony_ci      writable: this.writable,
2051cb0ef41Sopenharmony_ci      backpressure: this[kState].backpressure,
2061cb0ef41Sopenharmony_ci    });
2071cb0ef41Sopenharmony_ci  }
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci  [kTransfer]() {
2101cb0ef41Sopenharmony_ci    if (!isTransformStream(this))
2111cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStream');
2121cb0ef41Sopenharmony_ci    const {
2131cb0ef41Sopenharmony_ci      readable,
2141cb0ef41Sopenharmony_ci      writable,
2151cb0ef41Sopenharmony_ci    } = this[kState];
2161cb0ef41Sopenharmony_ci    if (readable.locked) {
2171cb0ef41Sopenharmony_ci      throw new DOMException(
2181cb0ef41Sopenharmony_ci        'Cannot transfer a locked ReadableStream',
2191cb0ef41Sopenharmony_ci        'DataCloneError');
2201cb0ef41Sopenharmony_ci    }
2211cb0ef41Sopenharmony_ci    if (writable.locked) {
2221cb0ef41Sopenharmony_ci      throw new DOMException(
2231cb0ef41Sopenharmony_ci        'Cannot transfer a locked WritableStream',
2241cb0ef41Sopenharmony_ci        'DataCloneError');
2251cb0ef41Sopenharmony_ci    }
2261cb0ef41Sopenharmony_ci    return {
2271cb0ef41Sopenharmony_ci      data: {
2281cb0ef41Sopenharmony_ci        readable,
2291cb0ef41Sopenharmony_ci        writable,
2301cb0ef41Sopenharmony_ci      },
2311cb0ef41Sopenharmony_ci      deserializeInfo:
2321cb0ef41Sopenharmony_ci        'internal/webstreams/transformstream:TransferredTransformStream',
2331cb0ef41Sopenharmony_ci    };
2341cb0ef41Sopenharmony_ci  }
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci  [kTransferList]() {
2371cb0ef41Sopenharmony_ci    return [ this[kState].readable, this[kState].writable ];
2381cb0ef41Sopenharmony_ci  }
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci  [kDeserialize]({ readable, writable }) {
2411cb0ef41Sopenharmony_ci    this[kState].readable = readable;
2421cb0ef41Sopenharmony_ci    this[kState].writable = writable;
2431cb0ef41Sopenharmony_ci  }
2441cb0ef41Sopenharmony_ci}
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ciObjectDefineProperties(TransformStream.prototype, {
2471cb0ef41Sopenharmony_ci  readable: kEnumerableProperty,
2481cb0ef41Sopenharmony_ci  writable: kEnumerableProperty,
2491cb0ef41Sopenharmony_ci  [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStream.name),
2501cb0ef41Sopenharmony_ci});
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_cifunction TransferredTransformStream() {
2531cb0ef41Sopenharmony_ci  return makeTransferable(ReflectConstruct(
2541cb0ef41Sopenharmony_ci    function() {
2551cb0ef41Sopenharmony_ci      this[kType] = 'TransformStream';
2561cb0ef41Sopenharmony_ci      this[kState] = {
2571cb0ef41Sopenharmony_ci        readable: undefined,
2581cb0ef41Sopenharmony_ci        writable: undefined,
2591cb0ef41Sopenharmony_ci        backpressure: undefined,
2601cb0ef41Sopenharmony_ci        backpressureChange: {
2611cb0ef41Sopenharmony_ci          promise: undefined,
2621cb0ef41Sopenharmony_ci          resolve: undefined,
2631cb0ef41Sopenharmony_ci          reject: undefined,
2641cb0ef41Sopenharmony_ci        },
2651cb0ef41Sopenharmony_ci        controller: undefined,
2661cb0ef41Sopenharmony_ci      };
2671cb0ef41Sopenharmony_ci    },
2681cb0ef41Sopenharmony_ci    [], TransformStream));
2691cb0ef41Sopenharmony_ci}
2701cb0ef41Sopenharmony_ciTransferredTransformStream.prototype[kDeserialize] = () => {};
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ciclass TransformStreamDefaultController {
2731cb0ef41Sopenharmony_ci  [kType] = 'TransformStreamDefaultController';
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci  constructor(skipThrowSymbol = undefined) {
2761cb0ef41Sopenharmony_ci    if (skipThrowSymbol !== kSkipThrow) {
2771cb0ef41Sopenharmony_ci      throw new ERR_ILLEGAL_CONSTRUCTOR();
2781cb0ef41Sopenharmony_ci    }
2791cb0ef41Sopenharmony_ci  }
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci  /**
2821cb0ef41Sopenharmony_ci   * @readonly
2831cb0ef41Sopenharmony_ci   * @type {number}
2841cb0ef41Sopenharmony_ci   */
2851cb0ef41Sopenharmony_ci  get desiredSize() {
2861cb0ef41Sopenharmony_ci    if (!isTransformStreamDefaultController(this))
2871cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStreamDefaultController');
2881cb0ef41Sopenharmony_ci    const {
2891cb0ef41Sopenharmony_ci      stream,
2901cb0ef41Sopenharmony_ci    } = this[kState];
2911cb0ef41Sopenharmony_ci    const {
2921cb0ef41Sopenharmony_ci      readable,
2931cb0ef41Sopenharmony_ci    } = stream[kState];
2941cb0ef41Sopenharmony_ci    const {
2951cb0ef41Sopenharmony_ci      controller: readableController,
2961cb0ef41Sopenharmony_ci    } = readable[kState];
2971cb0ef41Sopenharmony_ci    return readableStreamDefaultControllerGetDesiredSize(readableController);
2981cb0ef41Sopenharmony_ci  }
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci  /**
3011cb0ef41Sopenharmony_ci   * @param {any} [chunk]
3021cb0ef41Sopenharmony_ci   */
3031cb0ef41Sopenharmony_ci  enqueue(chunk = undefined) {
3041cb0ef41Sopenharmony_ci    if (!isTransformStreamDefaultController(this))
3051cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStreamDefaultController');
3061cb0ef41Sopenharmony_ci    transformStreamDefaultControllerEnqueue(this, chunk);
3071cb0ef41Sopenharmony_ci  }
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci  /**
3101cb0ef41Sopenharmony_ci   * @param {any} [reason]
3111cb0ef41Sopenharmony_ci   */
3121cb0ef41Sopenharmony_ci  error(reason = undefined) {
3131cb0ef41Sopenharmony_ci    if (!isTransformStreamDefaultController(this))
3141cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStreamDefaultController');
3151cb0ef41Sopenharmony_ci    transformStreamDefaultControllerError(this, reason);
3161cb0ef41Sopenharmony_ci  }
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_ci  terminate() {
3191cb0ef41Sopenharmony_ci    if (!isTransformStreamDefaultController(this))
3201cb0ef41Sopenharmony_ci      throw new ERR_INVALID_THIS('TransformStreamDefaultController');
3211cb0ef41Sopenharmony_ci    transformStreamDefaultControllerTerminate(this);
3221cb0ef41Sopenharmony_ci  }
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci  [kInspect](depth, options) {
3251cb0ef41Sopenharmony_ci    return customInspect(depth, options, this[kType], {
3261cb0ef41Sopenharmony_ci      stream: this[kState].stream,
3271cb0ef41Sopenharmony_ci    });
3281cb0ef41Sopenharmony_ci  }
3291cb0ef41Sopenharmony_ci}
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ciObjectDefineProperties(TransformStreamDefaultController.prototype, {
3321cb0ef41Sopenharmony_ci  desiredSize: kEnumerableProperty,
3331cb0ef41Sopenharmony_ci  enqueue: kEnumerableProperty,
3341cb0ef41Sopenharmony_ci  error: kEnumerableProperty,
3351cb0ef41Sopenharmony_ci  terminate: kEnumerableProperty,
3361cb0ef41Sopenharmony_ci  [SymbolToStringTag]: getNonWritablePropertyDescriptor(TransformStreamDefaultController.name),
3371cb0ef41Sopenharmony_ci});
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ciconst isTransformStream =
3401cb0ef41Sopenharmony_ci  isBrandCheck('TransformStream');
3411cb0ef41Sopenharmony_ciconst isTransformStreamDefaultController =
3421cb0ef41Sopenharmony_ci  isBrandCheck('TransformStreamDefaultController');
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ciasync function defaultTransformAlgorithm(chunk, controller) {
3451cb0ef41Sopenharmony_ci  transformStreamDefaultControllerEnqueue(controller, chunk);
3461cb0ef41Sopenharmony_ci}
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_cifunction initializeTransformStream(
3491cb0ef41Sopenharmony_ci  stream,
3501cb0ef41Sopenharmony_ci  startPromise,
3511cb0ef41Sopenharmony_ci  writableHighWaterMark,
3521cb0ef41Sopenharmony_ci  writableSizeAlgorithm,
3531cb0ef41Sopenharmony_ci  readableHighWaterMark,
3541cb0ef41Sopenharmony_ci  readableSizeAlgorithm) {
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci  const writable = new WritableStream({
3571cb0ef41Sopenharmony_ci    __proto__: null,
3581cb0ef41Sopenharmony_ci    start() { return startPromise.promise; },
3591cb0ef41Sopenharmony_ci    write(chunk) {
3601cb0ef41Sopenharmony_ci      return transformStreamDefaultSinkWriteAlgorithm(stream, chunk);
3611cb0ef41Sopenharmony_ci    },
3621cb0ef41Sopenharmony_ci    abort(reason) {
3631cb0ef41Sopenharmony_ci      return transformStreamDefaultSinkAbortAlgorithm(stream, reason);
3641cb0ef41Sopenharmony_ci    },
3651cb0ef41Sopenharmony_ci    close() {
3661cb0ef41Sopenharmony_ci      return transformStreamDefaultSinkCloseAlgorithm(stream);
3671cb0ef41Sopenharmony_ci    },
3681cb0ef41Sopenharmony_ci  }, {
3691cb0ef41Sopenharmony_ci    highWaterMark: writableHighWaterMark,
3701cb0ef41Sopenharmony_ci    size: writableSizeAlgorithm,
3711cb0ef41Sopenharmony_ci  });
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci  const readable = new ReadableStream({
3741cb0ef41Sopenharmony_ci    __proto__: null,
3751cb0ef41Sopenharmony_ci    start() { return startPromise.promise; },
3761cb0ef41Sopenharmony_ci    pull() {
3771cb0ef41Sopenharmony_ci      return transformStreamDefaultSourcePullAlgorithm(stream);
3781cb0ef41Sopenharmony_ci    },
3791cb0ef41Sopenharmony_ci    cancel(reason) {
3801cb0ef41Sopenharmony_ci      transformStreamErrorWritableAndUnblockWrite(stream, reason);
3811cb0ef41Sopenharmony_ci      return PromiseResolve();
3821cb0ef41Sopenharmony_ci    },
3831cb0ef41Sopenharmony_ci  }, {
3841cb0ef41Sopenharmony_ci    highWaterMark: readableHighWaterMark,
3851cb0ef41Sopenharmony_ci    size: readableSizeAlgorithm,
3861cb0ef41Sopenharmony_ci  });
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci  stream[kState] = {
3891cb0ef41Sopenharmony_ci    readable,
3901cb0ef41Sopenharmony_ci    writable,
3911cb0ef41Sopenharmony_ci    controller: undefined,
3921cb0ef41Sopenharmony_ci    backpressure: undefined,
3931cb0ef41Sopenharmony_ci    backpressureChange: {
3941cb0ef41Sopenharmony_ci      promise: undefined,
3951cb0ef41Sopenharmony_ci      resolve: undefined,
3961cb0ef41Sopenharmony_ci      reject: undefined,
3971cb0ef41Sopenharmony_ci    },
3981cb0ef41Sopenharmony_ci  };
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci  transformStreamSetBackpressure(stream, true);
4011cb0ef41Sopenharmony_ci}
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_cifunction transformStreamError(stream, error) {
4041cb0ef41Sopenharmony_ci  const {
4051cb0ef41Sopenharmony_ci    readable,
4061cb0ef41Sopenharmony_ci  } = stream[kState];
4071cb0ef41Sopenharmony_ci  const {
4081cb0ef41Sopenharmony_ci    controller,
4091cb0ef41Sopenharmony_ci  } = readable[kState];
4101cb0ef41Sopenharmony_ci  readableStreamDefaultControllerError(controller, error);
4111cb0ef41Sopenharmony_ci  transformStreamErrorWritableAndUnblockWrite(stream, error);
4121cb0ef41Sopenharmony_ci}
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_cifunction transformStreamErrorWritableAndUnblockWrite(stream, error) {
4151cb0ef41Sopenharmony_ci  const {
4161cb0ef41Sopenharmony_ci    controller,
4171cb0ef41Sopenharmony_ci    writable,
4181cb0ef41Sopenharmony_ci  } = stream[kState];
4191cb0ef41Sopenharmony_ci  transformStreamDefaultControllerClearAlgorithms(controller);
4201cb0ef41Sopenharmony_ci  writableStreamDefaultControllerErrorIfNeeded(
4211cb0ef41Sopenharmony_ci    writable[kState].controller,
4221cb0ef41Sopenharmony_ci    error);
4231cb0ef41Sopenharmony_ci  if (stream[kState].backpressure)
4241cb0ef41Sopenharmony_ci    transformStreamSetBackpressure(stream, false);
4251cb0ef41Sopenharmony_ci}
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_cifunction transformStreamSetBackpressure(stream, backpressure) {
4281cb0ef41Sopenharmony_ci  assert(stream[kState].backpressure !== backpressure);
4291cb0ef41Sopenharmony_ci  if (stream[kState].backpressureChange.promise !== undefined)
4301cb0ef41Sopenharmony_ci    stream[kState].backpressureChange.resolve?.();
4311cb0ef41Sopenharmony_ci  stream[kState].backpressureChange = createDeferredPromise();
4321cb0ef41Sopenharmony_ci  stream[kState].backpressure = backpressure;
4331cb0ef41Sopenharmony_ci}
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_cifunction setupTransformStreamDefaultController(
4361cb0ef41Sopenharmony_ci  stream,
4371cb0ef41Sopenharmony_ci  controller,
4381cb0ef41Sopenharmony_ci  transformAlgorithm,
4391cb0ef41Sopenharmony_ci  flushAlgorithm) {
4401cb0ef41Sopenharmony_ci  assert(isTransformStream(stream));
4411cb0ef41Sopenharmony_ci  assert(stream[kState].controller === undefined);
4421cb0ef41Sopenharmony_ci  controller[kState] = {
4431cb0ef41Sopenharmony_ci    stream,
4441cb0ef41Sopenharmony_ci    transformAlgorithm,
4451cb0ef41Sopenharmony_ci    flushAlgorithm,
4461cb0ef41Sopenharmony_ci  };
4471cb0ef41Sopenharmony_ci  stream[kState].controller = controller;
4481cb0ef41Sopenharmony_ci}
4491cb0ef41Sopenharmony_ci
4501cb0ef41Sopenharmony_cifunction setupTransformStreamDefaultControllerFromTransformer(
4511cb0ef41Sopenharmony_ci  stream,
4521cb0ef41Sopenharmony_ci  transformer) {
4531cb0ef41Sopenharmony_ci  const controller = new TransformStreamDefaultController(kSkipThrow);
4541cb0ef41Sopenharmony_ci  const transform = transformer?.transform || defaultTransformAlgorithm;
4551cb0ef41Sopenharmony_ci  const flush = transformer?.flush || nonOpFlush;
4561cb0ef41Sopenharmony_ci  const transformAlgorithm =
4571cb0ef41Sopenharmony_ci    FunctionPrototypeBind(transform, transformer);
4581cb0ef41Sopenharmony_ci  const flushAlgorithm =
4591cb0ef41Sopenharmony_ci    FunctionPrototypeBind(flush, transformer);
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ci  setupTransformStreamDefaultController(
4621cb0ef41Sopenharmony_ci    stream,
4631cb0ef41Sopenharmony_ci    controller,
4641cb0ef41Sopenharmony_ci    transformAlgorithm,
4651cb0ef41Sopenharmony_ci    flushAlgorithm);
4661cb0ef41Sopenharmony_ci}
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_cifunction transformStreamDefaultControllerClearAlgorithms(controller) {
4691cb0ef41Sopenharmony_ci  controller[kState].transformAlgorithm = undefined;
4701cb0ef41Sopenharmony_ci  controller[kState].flushAlgorithm = undefined;
4711cb0ef41Sopenharmony_ci}
4721cb0ef41Sopenharmony_ci
4731cb0ef41Sopenharmony_cifunction transformStreamDefaultControllerEnqueue(controller, chunk) {
4741cb0ef41Sopenharmony_ci  const {
4751cb0ef41Sopenharmony_ci    stream,
4761cb0ef41Sopenharmony_ci  } = controller[kState];
4771cb0ef41Sopenharmony_ci  const {
4781cb0ef41Sopenharmony_ci    readable,
4791cb0ef41Sopenharmony_ci  } = stream[kState];
4801cb0ef41Sopenharmony_ci  const {
4811cb0ef41Sopenharmony_ci    controller: readableController,
4821cb0ef41Sopenharmony_ci  } = readable[kState];
4831cb0ef41Sopenharmony_ci  if (!readableStreamDefaultControllerCanCloseOrEnqueue(readableController))
4841cb0ef41Sopenharmony_ci    throw new ERR_INVALID_STATE.TypeError('Unable to enqueue');
4851cb0ef41Sopenharmony_ci  try {
4861cb0ef41Sopenharmony_ci    readableStreamDefaultControllerEnqueue(readableController, chunk);
4871cb0ef41Sopenharmony_ci  } catch (error) {
4881cb0ef41Sopenharmony_ci    transformStreamErrorWritableAndUnblockWrite(stream, error);
4891cb0ef41Sopenharmony_ci    throw readable[kState].storedError;
4901cb0ef41Sopenharmony_ci  }
4911cb0ef41Sopenharmony_ci  const backpressure =
4921cb0ef41Sopenharmony_ci    readableStreamDefaultControllerHasBackpressure(readableController);
4931cb0ef41Sopenharmony_ci  if (backpressure !== stream[kState].backpressure) {
4941cb0ef41Sopenharmony_ci    assert(backpressure);
4951cb0ef41Sopenharmony_ci    transformStreamSetBackpressure(stream, true);
4961cb0ef41Sopenharmony_ci  }
4971cb0ef41Sopenharmony_ci}
4981cb0ef41Sopenharmony_ci
4991cb0ef41Sopenharmony_cifunction transformStreamDefaultControllerError(controller, error) {
5001cb0ef41Sopenharmony_ci  transformStreamError(controller[kState].stream, error);
5011cb0ef41Sopenharmony_ci}
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ciasync function transformStreamDefaultControllerPerformTransform(controller, chunk) {
5041cb0ef41Sopenharmony_ci  try {
5051cb0ef41Sopenharmony_ci    return await ensureIsPromise(
5061cb0ef41Sopenharmony_ci      controller[kState].transformAlgorithm,
5071cb0ef41Sopenharmony_ci      controller,
5081cb0ef41Sopenharmony_ci      chunk,
5091cb0ef41Sopenharmony_ci      controller);
5101cb0ef41Sopenharmony_ci  } catch (error) {
5111cb0ef41Sopenharmony_ci    transformStreamError(controller[kState].stream, error);
5121cb0ef41Sopenharmony_ci    throw error;
5131cb0ef41Sopenharmony_ci  }
5141cb0ef41Sopenharmony_ci}
5151cb0ef41Sopenharmony_ci
5161cb0ef41Sopenharmony_cifunction transformStreamDefaultControllerTerminate(controller) {
5171cb0ef41Sopenharmony_ci  const {
5181cb0ef41Sopenharmony_ci    stream,
5191cb0ef41Sopenharmony_ci  } = controller[kState];
5201cb0ef41Sopenharmony_ci  const {
5211cb0ef41Sopenharmony_ci    readable,
5221cb0ef41Sopenharmony_ci  } = stream[kState];
5231cb0ef41Sopenharmony_ci  assert(readable !== undefined);
5241cb0ef41Sopenharmony_ci  const {
5251cb0ef41Sopenharmony_ci    controller: readableController,
5261cb0ef41Sopenharmony_ci  } = readable[kState];
5271cb0ef41Sopenharmony_ci  readableStreamDefaultControllerClose(readableController);
5281cb0ef41Sopenharmony_ci  transformStreamErrorWritableAndUnblockWrite(
5291cb0ef41Sopenharmony_ci    stream,
5301cb0ef41Sopenharmony_ci    new ERR_INVALID_STATE.TypeError('TransformStream has been terminated'));
5311cb0ef41Sopenharmony_ci}
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_cifunction transformStreamDefaultSinkWriteAlgorithm(stream, chunk) {
5341cb0ef41Sopenharmony_ci  const {
5351cb0ef41Sopenharmony_ci    writable,
5361cb0ef41Sopenharmony_ci    controller,
5371cb0ef41Sopenharmony_ci  } = stream[kState];
5381cb0ef41Sopenharmony_ci  assert(writable[kState].state === 'writable');
5391cb0ef41Sopenharmony_ci  if (stream[kState].backpressure) {
5401cb0ef41Sopenharmony_ci    const backpressureChange = stream[kState].backpressureChange.promise;
5411cb0ef41Sopenharmony_ci    return PromisePrototypeThen(
5421cb0ef41Sopenharmony_ci      backpressureChange,
5431cb0ef41Sopenharmony_ci      () => {
5441cb0ef41Sopenharmony_ci        const {
5451cb0ef41Sopenharmony_ci          writable,
5461cb0ef41Sopenharmony_ci        } = stream[kState];
5471cb0ef41Sopenharmony_ci        if (writable[kState].state === 'erroring')
5481cb0ef41Sopenharmony_ci          throw writable[kState].storedError;
5491cb0ef41Sopenharmony_ci        assert(writable[kState].state === 'writable');
5501cb0ef41Sopenharmony_ci        return transformStreamDefaultControllerPerformTransform(
5511cb0ef41Sopenharmony_ci          controller,
5521cb0ef41Sopenharmony_ci          chunk);
5531cb0ef41Sopenharmony_ci      });
5541cb0ef41Sopenharmony_ci  }
5551cb0ef41Sopenharmony_ci  return transformStreamDefaultControllerPerformTransform(controller, chunk);
5561cb0ef41Sopenharmony_ci}
5571cb0ef41Sopenharmony_ci
5581cb0ef41Sopenharmony_ciasync function transformStreamDefaultSinkAbortAlgorithm(stream, reason) {
5591cb0ef41Sopenharmony_ci  transformStreamError(stream, reason);
5601cb0ef41Sopenharmony_ci}
5611cb0ef41Sopenharmony_ci
5621cb0ef41Sopenharmony_cifunction transformStreamDefaultSinkCloseAlgorithm(stream) {
5631cb0ef41Sopenharmony_ci  const {
5641cb0ef41Sopenharmony_ci    readable,
5651cb0ef41Sopenharmony_ci    controller,
5661cb0ef41Sopenharmony_ci  } = stream[kState];
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ci  const flushPromise =
5691cb0ef41Sopenharmony_ci    ensureIsPromise(
5701cb0ef41Sopenharmony_ci      controller[kState].flushAlgorithm,
5711cb0ef41Sopenharmony_ci      controller,
5721cb0ef41Sopenharmony_ci      controller);
5731cb0ef41Sopenharmony_ci  transformStreamDefaultControllerClearAlgorithms(controller);
5741cb0ef41Sopenharmony_ci  return PromisePrototypeThen(
5751cb0ef41Sopenharmony_ci    flushPromise,
5761cb0ef41Sopenharmony_ci    () => {
5771cb0ef41Sopenharmony_ci      if (readable[kState].state === 'errored')
5781cb0ef41Sopenharmony_ci        throw readable[kState].storedError;
5791cb0ef41Sopenharmony_ci      readableStreamDefaultControllerClose(readable[kState].controller);
5801cb0ef41Sopenharmony_ci    },
5811cb0ef41Sopenharmony_ci    (error) => {
5821cb0ef41Sopenharmony_ci      transformStreamError(stream, error);
5831cb0ef41Sopenharmony_ci      throw readable[kState].storedError;
5841cb0ef41Sopenharmony_ci    });
5851cb0ef41Sopenharmony_ci}
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_cifunction transformStreamDefaultSourcePullAlgorithm(stream) {
5881cb0ef41Sopenharmony_ci  assert(stream[kState].backpressure);
5891cb0ef41Sopenharmony_ci  assert(stream[kState].backpressureChange.promise !== undefined);
5901cb0ef41Sopenharmony_ci  transformStreamSetBackpressure(stream, false);
5911cb0ef41Sopenharmony_ci  return stream[kState].backpressureChange.promise;
5921cb0ef41Sopenharmony_ci}
5931cb0ef41Sopenharmony_ci
5941cb0ef41Sopenharmony_cimodule.exports = {
5951cb0ef41Sopenharmony_ci  TransformStream,
5961cb0ef41Sopenharmony_ci  TransformStreamDefaultController,
5971cb0ef41Sopenharmony_ci  TransferredTransformStream,
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ci  // Exported Brand Checks
6001cb0ef41Sopenharmony_ci  isTransformStream,
6011cb0ef41Sopenharmony_ci  isTransformStreamDefaultController,
6021cb0ef41Sopenharmony_ci};
603