11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ArrayBufferPrototypeSlice,
51cb0ef41Sopenharmony_ci  ArrayPrototypePush,
61cb0ef41Sopenharmony_ci  ArrayPrototypeShift,
71cb0ef41Sopenharmony_ci  AsyncIteratorPrototype,
81cb0ef41Sopenharmony_ci  FunctionPrototypeCall,
91cb0ef41Sopenharmony_ci  MathMax,
101cb0ef41Sopenharmony_ci  NumberIsNaN,
111cb0ef41Sopenharmony_ci  ObjectCreate,
121cb0ef41Sopenharmony_ci  PromisePrototypeThen,
131cb0ef41Sopenharmony_ci  PromiseResolve,
141cb0ef41Sopenharmony_ci  PromiseReject,
151cb0ef41Sopenharmony_ci  ReflectGet,
161cb0ef41Sopenharmony_ci  Symbol,
171cb0ef41Sopenharmony_ci  Uint8Array,
181cb0ef41Sopenharmony_ci} = primordials;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst {
211cb0ef41Sopenharmony_ci  codes: {
221cb0ef41Sopenharmony_ci    ERR_INVALID_ARG_VALUE,
231cb0ef41Sopenharmony_ci    ERR_OPERATION_FAILED,
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci} = require('internal/errors');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst {
281cb0ef41Sopenharmony_ci  copyArrayBuffer,
291cb0ef41Sopenharmony_ci  detachArrayBuffer,
301cb0ef41Sopenharmony_ci} = internalBinding('buffer');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst {
331cb0ef41Sopenharmony_ci  isPromise,
341cb0ef41Sopenharmony_ci} = require('internal/util/types');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst {
371cb0ef41Sopenharmony_ci  inspect,
381cb0ef41Sopenharmony_ci} = require('util');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciconst {
411cb0ef41Sopenharmony_ci  constants: {
421cb0ef41Sopenharmony_ci    kPending,
431cb0ef41Sopenharmony_ci  },
441cb0ef41Sopenharmony_ci  getPromiseDetails,
451cb0ef41Sopenharmony_ci} = internalBinding('util');
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciconst assert = require('internal/assert');
481cb0ef41Sopenharmony_ciconst { isArrayBufferDetached } = require('internal/util');
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciconst {
511cb0ef41Sopenharmony_ci  validateFunction,
521cb0ef41Sopenharmony_ci} = require('internal/validators');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst kState = Symbol('kState');
551cb0ef41Sopenharmony_ciconst kType = Symbol('kType');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciconst AsyncIterator = ObjectCreate(AsyncIteratorPrototype, {
581cb0ef41Sopenharmony_ci  next: {
591cb0ef41Sopenharmony_ci    __proto__: null,
601cb0ef41Sopenharmony_ci    configurable: true,
611cb0ef41Sopenharmony_ci    enumerable: true,
621cb0ef41Sopenharmony_ci    writable: true,
631cb0ef41Sopenharmony_ci  },
641cb0ef41Sopenharmony_ci  return: {
651cb0ef41Sopenharmony_ci    __proto__: null,
661cb0ef41Sopenharmony_ci    configurable: true,
671cb0ef41Sopenharmony_ci    enumerable: true,
681cb0ef41Sopenharmony_ci    writable: true,
691cb0ef41Sopenharmony_ci  },
701cb0ef41Sopenharmony_ci});
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cifunction extractHighWaterMark(value, defaultHWM) {
731cb0ef41Sopenharmony_ci  if (value === undefined) return defaultHWM;
741cb0ef41Sopenharmony_ci  value = +value;
751cb0ef41Sopenharmony_ci  if (typeof value !== 'number' ||
761cb0ef41Sopenharmony_ci      NumberIsNaN(value) ||
771cb0ef41Sopenharmony_ci      value < 0)
781cb0ef41Sopenharmony_ci    throw new ERR_INVALID_ARG_VALUE.RangeError('strategy.highWaterMark', value);
791cb0ef41Sopenharmony_ci  return value;
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cifunction extractSizeAlgorithm(size) {
831cb0ef41Sopenharmony_ci  if (size === undefined) return () => 1;
841cb0ef41Sopenharmony_ci  validateFunction(size, 'strategy.size');
851cb0ef41Sopenharmony_ci  return size;
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cifunction customInspect(depth, options, name, data) {
891cb0ef41Sopenharmony_ci  if (depth < 0)
901cb0ef41Sopenharmony_ci    return this;
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  const opts = {
931cb0ef41Sopenharmony_ci    ...options,
941cb0ef41Sopenharmony_ci    depth: options.depth == null ? null : options.depth - 1,
951cb0ef41Sopenharmony_ci  };
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  return `${name} ${inspect(data, opts)}`;
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// These are defensive to work around the possibility that
1011cb0ef41Sopenharmony_ci// the buffer, byteLength, and byteOffset properties on
1021cb0ef41Sopenharmony_ci// ArrayBuffer and ArrayBufferView's may have been tampered with.
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_cifunction ArrayBufferViewGetBuffer(view) {
1051cb0ef41Sopenharmony_ci  return ReflectGet(view.constructor.prototype, 'buffer', view);
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_cifunction ArrayBufferViewGetByteLength(view) {
1091cb0ef41Sopenharmony_ci  return ReflectGet(view.constructor.prototype, 'byteLength', view);
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_cifunction ArrayBufferViewGetByteOffset(view) {
1131cb0ef41Sopenharmony_ci  return ReflectGet(view.constructor.prototype, 'byteOffset', view);
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_cifunction cloneAsUint8Array(view) {
1171cb0ef41Sopenharmony_ci  const buffer = ArrayBufferViewGetBuffer(view);
1181cb0ef41Sopenharmony_ci  const byteOffset = ArrayBufferViewGetByteOffset(view);
1191cb0ef41Sopenharmony_ci  const byteLength = ArrayBufferViewGetByteLength(view);
1201cb0ef41Sopenharmony_ci  return new Uint8Array(
1211cb0ef41Sopenharmony_ci    ArrayBufferPrototypeSlice(buffer, byteOffset, byteOffset + byteLength),
1221cb0ef41Sopenharmony_ci  );
1231cb0ef41Sopenharmony_ci}
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_cifunction isBrandCheck(brand) {
1261cb0ef41Sopenharmony_ci  return (value) => {
1271cb0ef41Sopenharmony_ci    return value != null &&
1281cb0ef41Sopenharmony_ci           value[kState] !== undefined &&
1291cb0ef41Sopenharmony_ci           value[kType] === brand;
1301cb0ef41Sopenharmony_ci  };
1311cb0ef41Sopenharmony_ci}
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_cifunction transferArrayBuffer(buffer) {
1341cb0ef41Sopenharmony_ci  const res = detachArrayBuffer(buffer);
1351cb0ef41Sopenharmony_ci  if (res === undefined) {
1361cb0ef41Sopenharmony_ci    throw new ERR_OPERATION_FAILED.TypeError(
1371cb0ef41Sopenharmony_ci      'The ArrayBuffer could not be transferred');
1381cb0ef41Sopenharmony_ci  }
1391cb0ef41Sopenharmony_ci  return res;
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_cifunction isViewedArrayBufferDetached(view) {
1431cb0ef41Sopenharmony_ci  return (
1441cb0ef41Sopenharmony_ci    ArrayBufferViewGetByteLength(view) === 0 &&
1451cb0ef41Sopenharmony_ci    isArrayBufferDetached(ArrayBufferViewGetBuffer(view))
1461cb0ef41Sopenharmony_ci  );
1471cb0ef41Sopenharmony_ci}
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_cifunction dequeueValue(controller) {
1501cb0ef41Sopenharmony_ci  assert(controller[kState].queue !== undefined);
1511cb0ef41Sopenharmony_ci  assert(controller[kState].queueTotalSize !== undefined);
1521cb0ef41Sopenharmony_ci  assert(controller[kState].queue.length);
1531cb0ef41Sopenharmony_ci  const {
1541cb0ef41Sopenharmony_ci    value,
1551cb0ef41Sopenharmony_ci    size,
1561cb0ef41Sopenharmony_ci  } = ArrayPrototypeShift(controller[kState].queue);
1571cb0ef41Sopenharmony_ci  controller[kState].queueTotalSize =
1581cb0ef41Sopenharmony_ci    MathMax(0, controller[kState].queueTotalSize - size);
1591cb0ef41Sopenharmony_ci  return value;
1601cb0ef41Sopenharmony_ci}
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_cifunction resetQueue(controller) {
1631cb0ef41Sopenharmony_ci  assert(controller[kState].queue !== undefined);
1641cb0ef41Sopenharmony_ci  assert(controller[kState].queueTotalSize !== undefined);
1651cb0ef41Sopenharmony_ci  controller[kState].queue = [];
1661cb0ef41Sopenharmony_ci  controller[kState].queueTotalSize = 0;
1671cb0ef41Sopenharmony_ci}
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_cifunction peekQueueValue(controller) {
1701cb0ef41Sopenharmony_ci  assert(controller[kState].queue !== undefined);
1711cb0ef41Sopenharmony_ci  assert(controller[kState].queueTotalSize !== undefined);
1721cb0ef41Sopenharmony_ci  assert(controller[kState].queue.length);
1731cb0ef41Sopenharmony_ci  return controller[kState].queue[0].value;
1741cb0ef41Sopenharmony_ci}
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_cifunction enqueueValueWithSize(controller, value, size) {
1771cb0ef41Sopenharmony_ci  assert(controller[kState].queue !== undefined);
1781cb0ef41Sopenharmony_ci  assert(controller[kState].queueTotalSize !== undefined);
1791cb0ef41Sopenharmony_ci  size = +size;
1801cb0ef41Sopenharmony_ci  if (typeof size !== 'number' ||
1811cb0ef41Sopenharmony_ci      size < 0 ||
1821cb0ef41Sopenharmony_ci      NumberIsNaN(size) ||
1831cb0ef41Sopenharmony_ci      size === Infinity) {
1841cb0ef41Sopenharmony_ci    throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
1851cb0ef41Sopenharmony_ci  }
1861cb0ef41Sopenharmony_ci  ArrayPrototypePush(controller[kState].queue, { value, size });
1871cb0ef41Sopenharmony_ci  controller[kState].queueTotalSize += size;
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_cifunction ensureIsPromise(fn, thisArg, ...args) {
1911cb0ef41Sopenharmony_ci  try {
1921cb0ef41Sopenharmony_ci    const value = FunctionPrototypeCall(fn, thisArg, ...args);
1931cb0ef41Sopenharmony_ci    return isPromise(value) ? value : PromiseResolve(value);
1941cb0ef41Sopenharmony_ci  } catch (error) {
1951cb0ef41Sopenharmony_ci    return PromiseReject(error);
1961cb0ef41Sopenharmony_ci  }
1971cb0ef41Sopenharmony_ci}
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_cifunction isPromisePending(promise) {
2001cb0ef41Sopenharmony_ci  if (promise === undefined) return false;
2011cb0ef41Sopenharmony_ci  const details = getPromiseDetails(promise);
2021cb0ef41Sopenharmony_ci  return details?.[0] === kPending;
2031cb0ef41Sopenharmony_ci}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_cifunction setPromiseHandled(promise) {
2061cb0ef41Sopenharmony_ci  // Alternatively, we could use the native API
2071cb0ef41Sopenharmony_ci  // MarkAsHandled, but this avoids the extra boundary cross
2081cb0ef41Sopenharmony_ci  // and is hopefully faster at the cost of an extra Promise
2091cb0ef41Sopenharmony_ci  // allocation.
2101cb0ef41Sopenharmony_ci  PromisePrototypeThen(promise, () => {}, () => {});
2111cb0ef41Sopenharmony_ci}
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_ciasync function nonOpFlush() {}
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_cifunction nonOpStart() {}
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ciasync function nonOpPull() {}
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ciasync function nonOpCancel() {}
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ciasync function nonOpWrite() {}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_cilet transfer;
2241cb0ef41Sopenharmony_cifunction lazyTransfer() {
2251cb0ef41Sopenharmony_ci  if (transfer === undefined)
2261cb0ef41Sopenharmony_ci    transfer = require('internal/webstreams/transfer');
2271cb0ef41Sopenharmony_ci  return transfer;
2281cb0ef41Sopenharmony_ci}
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_cimodule.exports = {
2311cb0ef41Sopenharmony_ci  ArrayBufferViewGetBuffer,
2321cb0ef41Sopenharmony_ci  ArrayBufferViewGetByteLength,
2331cb0ef41Sopenharmony_ci  ArrayBufferViewGetByteOffset,
2341cb0ef41Sopenharmony_ci  AsyncIterator,
2351cb0ef41Sopenharmony_ci  cloneAsUint8Array,
2361cb0ef41Sopenharmony_ci  copyArrayBuffer,
2371cb0ef41Sopenharmony_ci  customInspect,
2381cb0ef41Sopenharmony_ci  dequeueValue,
2391cb0ef41Sopenharmony_ci  ensureIsPromise,
2401cb0ef41Sopenharmony_ci  enqueueValueWithSize,
2411cb0ef41Sopenharmony_ci  extractHighWaterMark,
2421cb0ef41Sopenharmony_ci  extractSizeAlgorithm,
2431cb0ef41Sopenharmony_ci  lazyTransfer,
2441cb0ef41Sopenharmony_ci  isBrandCheck,
2451cb0ef41Sopenharmony_ci  isPromisePending,
2461cb0ef41Sopenharmony_ci  isViewedArrayBufferDetached,
2471cb0ef41Sopenharmony_ci  peekQueueValue,
2481cb0ef41Sopenharmony_ci  resetQueue,
2491cb0ef41Sopenharmony_ci  setPromiseHandled,
2501cb0ef41Sopenharmony_ci  transferArrayBuffer,
2511cb0ef41Sopenharmony_ci  nonOpCancel,
2521cb0ef41Sopenharmony_ci  nonOpFlush,
2531cb0ef41Sopenharmony_ci  nonOpPull,
2541cb0ef41Sopenharmony_ci  nonOpStart,
2551cb0ef41Sopenharmony_ci  nonOpWrite,
2561cb0ef41Sopenharmony_ci  kType,
2571cb0ef41Sopenharmony_ci  kState,
2581cb0ef41Sopenharmony_ci};
259