All files / lib/internal/webstreams util.js

100% Statements 273/273
98.48% Branches 65/66
100% Functions 25/25
100% Lines 273/273

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 27421x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 2997x 2997x 831x 831x 2997x 2997x 2997x 807x 2997x 21x 2711x 2711x 703x 2711x 695x 2711x 21x 205x 205x 205x 203x 203x 203x 205x 205x 205x 205x 205x 21x 21x 21x 21x 21x 1106x 1106x 1106x 21x 1120x 1120x 1120x 21x 534x 534x 534x 21x 1329x 1329x 1329x 21x 16x 16x 16x 16x 16x 16x 16x 16x 21x 8880x 8880x 12516x 12516x 12516x 8880x 8880x 21x 964x 964x 964x 3x 3x 3x 961x 964x 21x 154x 154x 9x 9x 9x 9x 7x 7x 7x 9x 147x 154x 21x 25x 25x 25x 11x 25x 25x 21x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 2707x 21x 709x 709x 709x 709x 709x 709x 21x 2179x 2179x 2179x 2179x 2179x 2179x 21x 2946x 2946x 2946x 2946x 2946x 2946x 2946x 2946x 12x 12x 2934x 2934x 2946x 21x 5150x 5150x 5150x 5150x 5150x 66x 66x 5150x 21x 1161x 1161x 1161x 1161x 1161x 21x 3499x 3499x 3499x 3499x 3499x 3499x 3499x 21x 23x 21x 933x 21x 282x 21x 195x 21x 18x 21x 21x 32x 32x 32x 32x 32x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x  
'use strict';
 
const {
  ArrayBufferPrototype,
  ArrayBufferPrototypeSlice,
  ArrayPrototypePush,
  ArrayPrototypeShift,
  AsyncIteratorPrototype,
  FunctionPrototypeCall,
  MathMax,
  NumberIsNaN,
  ObjectCreate,
  PromisePrototypeThen,
  PromiseResolve,
  PromiseReject,
  ReflectGet,
  Symbol,
  Uint8Array,
} = primordials;
 
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_INVALID_ARG_VALUE,
    ERR_OPERATION_FAILED,
  },
} = require('internal/errors');
 
const {
  copyArrayBuffer,
  detachArrayBuffer
} = internalBinding('buffer');
 
const {
  isPromise,
} = require('internal/util/types');
 
const {
  inspect,
} = require('util');
 
const {
  getPromiseDetails,
  kPending,
} = internalBinding('util');
 
const assert = require('internal/assert');
 
const kState = Symbol('kState');
const kType = Symbol('kType');
 
const AsyncIterator = ObjectCreate(AsyncIteratorPrototype, {
  next: {
    __proto__: null,
    configurable: true,
    enumerable: true,
    writable: true,
  },
  return: {
    __proto__: null,
    configurable: true,
    enumerable: true,
    writable: true,
  },
});
 
function extractHighWaterMark(value, defaultHWM) {
  if (value === undefined) return defaultHWM;
  value = +value;
  if (typeof value !== 'number' ||
      NumberIsNaN(value) ||
      value < 0)
    throw new ERR_INVALID_ARG_VALUE.RangeError('strategy.highWaterMark', value);
  return value;
}
 
function extractSizeAlgorithm(size) {
  if (size === undefined) return () => 1;
  if (typeof size !== 'function')
    throw new ERR_INVALID_ARG_TYPE('strategy.size', 'Function', size);
  return size;
}
 
function customInspect(depth, options, name, data) {
  if (depth < 0)
    return this;
 
  const opts = {
    ...options,
    depth: options.depth == null ? null : options.depth - 1
  };
 
  return `${name} ${inspect(data, opts)}`;
}
 
// These are defensive to work around the possibility that
// the buffer, byteLength, and byteOffset properties on
// ArrayBuffer and ArrayBufferView's may have been tampered with.
 
function ArrayBufferViewGetBuffer(view) {
  return ReflectGet(view.constructor.prototype, 'buffer', view);
}
 
function ArrayBufferViewGetByteLength(view) {
  return ReflectGet(view.constructor.prototype, 'byteLength', view);
}
 
function ArrayBufferViewGetByteOffset(view) {
  return ReflectGet(view.constructor.prototype, 'byteOffset', view);
}
 
function ArrayBufferGetByteLength(view) {
  return ReflectGet(ArrayBufferPrototype, 'byteLength', view);
}
 
function cloneAsUint8Array(view) {
  const buffer = ArrayBufferViewGetBuffer(view);
  const byteOffset = ArrayBufferViewGetByteOffset(view);
  const byteLength = ArrayBufferViewGetByteLength(view);
  return new Uint8Array(
    ArrayBufferPrototypeSlice(buffer, byteOffset, byteOffset + byteLength)
  );
}
 
function isBrandCheck(brand) {
  return (value) => {
    return value != null &&
           value[kState] !== undefined &&
           value[kType] === brand;
  };
}
 
function transferArrayBuffer(buffer) {
  const res = detachArrayBuffer(buffer);
  if (res === undefined) {
    throw new ERR_OPERATION_FAILED.TypeError(
      'The ArrayBuffer could not be transferred');
  }
  return res;
}
 
function isDetachedBuffer(buffer) {
  if (ArrayBufferGetByteLength(buffer) === 0) {
    // TODO(daeyeon): Consider using C++ builtin to improve performance.
    try {
      new Uint8Array(buffer);
    } catch (error) {
      assert(error.name === 'TypeError');
      return true;
    }
  }
  return false;
}
 
function isViewedArrayBufferDetached(view) {
  return (
    ArrayBufferViewGetByteLength(view) === 0 &&
    isDetachedBuffer(ArrayBufferViewGetBuffer(view))
  );
}
 
function dequeueValue(controller) {
  assert(controller[kState].queue !== undefined);
  assert(controller[kState].queueTotalSize !== undefined);
  assert(controller[kState].queue.length);
  const {
    value,
    size,
  } = ArrayPrototypeShift(controller[kState].queue);
  controller[kState].queueTotalSize =
    MathMax(0, controller[kState].queueTotalSize - size);
  return value;
}
 
function resetQueue(controller) {
  assert(controller[kState].queue !== undefined);
  assert(controller[kState].queueTotalSize !== undefined);
  controller[kState].queue = [];
  controller[kState].queueTotalSize = 0;
}
 
function peekQueueValue(controller) {
  assert(controller[kState].queue !== undefined);
  assert(controller[kState].queueTotalSize !== undefined);
  assert(controller[kState].queue.length);
  return controller[kState].queue[0].value;
}
 
function enqueueValueWithSize(controller, value, size) {
  assert(controller[kState].queue !== undefined);
  assert(controller[kState].queueTotalSize !== undefined);
  size = +size;
  if (typeof size !== 'number' ||
      size < 0 ||
      NumberIsNaN(size) ||
      size === Infinity) {
    throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
  }
  ArrayPrototypePush(controller[kState].queue, { value, size });
  controller[kState].queueTotalSize += size;
}
 
function ensureIsPromise(fn, thisArg, ...args) {
  try {
    const value = FunctionPrototypeCall(fn, thisArg, ...args);
    return isPromise(value) ? value : PromiseResolve(value);
  } catch (error) {
    return PromiseReject(error);
  }
}
 
function isPromisePending(promise) {
  if (promise === undefined) return false;
  const details = getPromiseDetails(promise);
  return details?.[0] === kPending;
}
 
function setPromiseHandled(promise) {
  // Alternatively, we could use the native API
  // MarkAsHandled, but this avoids the extra boundary cross
  // and is hopefully faster at the cost of an extra Promise
  // allocation.
  PromisePrototypeThen(promise, () => {}, () => {});
}
 
async function nonOpFlush() {}
 
function nonOpStart() {}
 
async function nonOpPull() {}
 
async function nonOpCancel() {}
 
async function nonOpWrite() {}
 
let transfer;
function lazyTransfer() {
  if (transfer === undefined)
    transfer = require('internal/webstreams/transfer');
  return transfer;
}
 
module.exports = {
  ArrayBufferViewGetBuffer,
  ArrayBufferViewGetByteLength,
  ArrayBufferViewGetByteOffset,
  ArrayBufferGetByteLength,
  AsyncIterator,
  cloneAsUint8Array,
  copyArrayBuffer,
  customInspect,
  dequeueValue,
  ensureIsPromise,
  enqueueValueWithSize,
  extractHighWaterMark,
  extractSizeAlgorithm,
  lazyTransfer,
  isBrandCheck,
  isDetachedBuffer,
  isPromisePending,
  isViewedArrayBufferDetached,
  peekQueueValue,
  resetQueue,
  setPromiseHandled,
  transferArrayBuffer,
  nonOpCancel,
  nonOpFlush,
  nonOpPull,
  nonOpStart,
  nonOpWrite,
  kType,
  kState,
};