@amazon/vinyl-util 3.2.2 → 3.2.3-next.20261001143710

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/index.cjs +127 -49
  2. package/dist/index.cjs.map +4 -4
  3. package/dist/index.js +127 -49
  4. package/dist/index.js.map +4 -4
  5. package/dist/test/benchmark.es5.js +3 -2
  6. package/dist/test/benchmark.js +5 -4
  7. package/dist/test/benchmark.js.map +2 -2
  8. package/dist/testUtil/types/current/tsconfig.tsbuildinfo +1 -1
  9. package/dist/types/current/index.d.ts +1 -0
  10. package/dist/types/current/index.d.ts.map +1 -1
  11. package/dist/types/current/index.js +1 -0
  12. package/dist/types/current/network/RequesterImpl.d.ts.map +1 -1
  13. package/dist/types/current/network/RequesterImpl.js +33 -25
  14. package/dist/types/current/network/metrics/NetworkMetricsControllerImpl.d.ts.map +1 -1
  15. package/dist/types/current/network/metrics/NetworkMetricsControllerImpl.js +4 -2
  16. package/dist/types/current/tsconfig.tsbuildinfo +1 -1
  17. package/dist/types/current/util/buffer/buffer.d.ts +9 -0
  18. package/dist/types/current/util/buffer/buffer.d.ts.map +1 -1
  19. package/dist/types/current/util/buffer/buffer.js +13 -0
  20. package/dist/types/current/util/collection/LruCache.d.ts +2 -1
  21. package/dist/types/current/util/collection/LruCache.d.ts.map +1 -1
  22. package/dist/types/current/util/collection/LruCache.js +2 -1
  23. package/dist/types/current/util/fun/memoize.js +7 -3
  24. package/dist/types/current/util/fun/rateLimit.d.ts +40 -0
  25. package/dist/types/current/util/fun/rateLimit.d.ts.map +1 -0
  26. package/dist/types/current/util/fun/rateLimit.js +86 -0
  27. package/dist/types/current/util/string/string.d.ts +4 -0
  28. package/dist/types/current/util/string/string.d.ts.map +1 -1
  29. package/dist/types/current/util/string/string.js +23 -14
  30. package/dist/types/ts3.7/index.d.ts +1 -0
  31. package/dist/types/ts3.7/util/buffer/buffer.d.ts +9 -0
  32. package/dist/types/ts3.7/util/collection/LruCache.d.ts +2 -1
  33. package/dist/types/ts3.7/util/fun/rateLimit.d.ts +40 -0
  34. package/dist/types/ts3.7/util/string/string.d.ts +4 -0
  35. package/dist/types/ts4.1/index.d.ts +1 -0
  36. package/dist/types/ts4.1/util/buffer/buffer.d.ts +9 -0
  37. package/dist/types/ts4.1/util/collection/LruCache.d.ts +2 -1
  38. package/dist/types/ts4.1/util/fun/rateLimit.d.ts +40 -0
  39. package/dist/types/ts4.1/util/string/string.d.ts +4 -0
  40. package/dist/types/ts4.7/index.d.ts +1 -0
  41. package/dist/types/ts4.7/util/buffer/buffer.d.ts +9 -0
  42. package/dist/types/ts4.7/util/collection/LruCache.d.ts +2 -1
  43. package/dist/types/ts4.7/util/fun/rateLimit.d.ts +40 -0
  44. package/dist/types/ts4.7/util/string/string.d.ts +4 -0
  45. package/dist/unit/types/current/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +4 -4
package/dist/index.cjs CHANGED
@@ -105,6 +105,7 @@ __export(index_exports, {
105
105
  arrayEqualRule: () => arrayEqualRule,
106
106
  arrayMergeRule: () => arrayMergeRule,
107
107
  base64ToByteArray: () => base64ToByteArray,
108
+ bufferToArrayBuffer: () => bufferToArrayBuffer,
108
109
  bufferToBase64: () => bufferToBase64,
109
110
  bufferToByteArray: () => bufferToByteArray,
110
111
  bufferToByteStr: () => bufferToByteStr,
@@ -278,6 +279,7 @@ __export(index_exports, {
278
279
  promise: () => promise,
279
280
  rangeIntersects: () => rangeIntersects,
280
281
  rangesOf: () => rangesOf,
282
+ rateLimit: () => rateLimit,
281
283
  readResponseBody: () => readResponseBody,
282
284
  redispatchEvents: () => redispatchEvents,
283
285
  regexRule: () => regexRule,
@@ -1546,7 +1548,8 @@ var LruCache = class {
1546
1548
  return true;
1547
1549
  }
1548
1550
  /**
1549
- * Executes a provided function once per each key/value pair in the Map, in insertion order.
1551
+ * Executes a provided function once per each key/value pair in the Map,
1552
+ * ordered from least recently used to most.
1550
1553
  *
1551
1554
  * @param callback A callback to invoke for every element in the cache.
1552
1555
  * @param thisArg Value to use as this when executing callback
@@ -1591,8 +1594,8 @@ function memoize(inner, keyProvider, capacity = 500) {
1591
1594
  let value = void 0;
1592
1595
  const memoized = () => {
1593
1596
  if (!cached) {
1594
- cached = true;
1595
1597
  value = inner();
1598
+ cached = true;
1596
1599
  }
1597
1600
  return value;
1598
1601
  };
@@ -1621,9 +1624,9 @@ function memoize(inner, keyProvider, capacity = 500) {
1621
1624
  const memoized = (...args) => {
1622
1625
  const key = keyProvider(...args);
1623
1626
  if (!hasValue || cachedKey !== key) {
1624
- hasValue = true;
1625
- cachedKey = key;
1626
1627
  cachedValue = inner(...args);
1628
+ cachedKey = key;
1629
+ hasValue = true;
1627
1630
  }
1628
1631
  return cachedValue;
1629
1632
  };
@@ -1686,7 +1689,8 @@ function isWhitespaceChar(charCode) {
1686
1689
  charCode === 13 || // '\r' (carriage return)
1687
1690
  charCode === 10;
1688
1691
  }
1689
- var encodedEntitiesRegex = /&(#(\d+)|\w+);/gi;
1692
+ var encodedEntitiesRegex = /&(?:#(?:x([\da-f]+)|(\d+))|(\w+));/gi;
1693
+ var MAX_CODE_POINT = 1114111;
1690
1694
  var predefinedDecodeEntities = /* @__PURE__ */ new Map([
1691
1695
  ["apos", "'"],
1692
1696
  ["amp", "&"],
@@ -1695,20 +1699,22 @@ var predefinedDecodeEntities = /* @__PURE__ */ new Map([
1695
1699
  ["quot", '"']
1696
1700
  ]);
1697
1701
  function decodeEntities(str, namedEntities = predefinedDecodeEntities) {
1698
- if (encodedEntitiesRegex.test(str)) {
1699
- return str.replace(
1700
- encodedEntitiesRegex,
1701
- (substring, entityName, codePoint) => {
1702
- if (codePoint !== void 0)
1703
- return String.fromCharCode(parseInt(codePoint));
1704
- const entityValue = namedEntities.get(entityName.toLowerCase());
1705
- if (!entityValue) return substring;
1706
- return entityValue;
1702
+ return str.replace(
1703
+ encodedEntitiesRegex,
1704
+ (substring, hex, decimal, entityName) => {
1705
+ var _a;
1706
+ if (hex == null && decimal == null) {
1707
+ return (_a = namedEntities.get(entityName.toLowerCase())) != null ? _a : substring;
1707
1708
  }
1708
- );
1709
- } else {
1710
- return str;
1711
- }
1709
+ const codePoint = hex != null ? parseInt(hex, 16) : parseInt(decimal, 10);
1710
+ if (codePoint > MAX_CODE_POINT || isSurrogate(codePoint))
1711
+ return substring;
1712
+ return String.fromCodePoint(codePoint);
1713
+ }
1714
+ );
1715
+ }
1716
+ function isSurrogate(codePoint) {
1717
+ return codePoint >= 55296 && codePoint <= 57343;
1712
1718
  }
1713
1719
  var standardEntityValueToNames = /* @__PURE__ */ flipMap(predefinedDecodeEntities);
1714
1720
  var entitiesRegex = /['&<>"]/g;
@@ -3991,7 +3997,11 @@ var NetworkMetricsControllerImpl = class extends EventHostImpl {
3991
3997
  __publicField(this, "onDownloadSpeedChange", () => {
3992
3998
  var _a;
3993
3999
  const downlink = (_a = this.deps.networkInformation) == null ? void 0 : _a.downlink;
3994
- const estimatedSpeed = downlink ? downlink * 1024 * 1024 : this.options.unknownDownlinkBandwidthEstimate;
4000
+ const estimatedSpeed = downlink ? (
4001
+ // Megabits are decimal, matching the measured `bytes * 8 / seconds`
4002
+ // this estimate is averaged with.
4003
+ downlink * 1e6
4004
+ ) : this.options.unknownDownlinkBandwidthEstimate;
3995
4005
  updateStatMetrics(
3996
4006
  this._metrics.estimatedDownlinkBandwidth,
3997
4007
  estimatedSpeed,
@@ -4499,38 +4509,41 @@ var RequesterImpl = class extends EventHostImpl {
4499
4509
  this.dispatch("request", { requestInfo });
4500
4510
  let lastResult = null;
4501
4511
  let currentTry = 0;
4502
- do {
4503
- ++currentTry;
4504
- const attemptInfo = {
4505
- currentTry,
4506
- timestamp: Date.now()
4507
- };
4508
- this.dispatch("requestAttemptStart", {
4509
- requestInfo,
4510
- attemptInfo
4511
- });
4512
- lastResult = await this.doRequest(
4513
- requestInfo,
4514
- requestOptions,
4515
- attemptInfo,
4516
- timeoutController
4517
- ).catch((reason) => {
4518
- var _a2;
4519
- const completeInfo = {
4520
- ok: false,
4521
- type: ((_a2 = requestOptions.abort) == null ? void 0 : _a2.aborted()) ? "abort" /* ABORT */ : "internal" /* INTERNAL */,
4522
- requestInfo,
4523
- reason,
4524
- willRetry: false,
4525
- timestamp: Date.now(),
4526
- retryAfter: null
4512
+ try {
4513
+ do {
4514
+ ++currentTry;
4515
+ const attemptInfo = {
4516
+ currentTry,
4517
+ timestamp: Date.now()
4527
4518
  };
4528
- return { info: completeInfo, response: null };
4529
- });
4519
+ this.dispatch("requestAttemptStart", {
4520
+ requestInfo,
4521
+ attemptInfo
4522
+ });
4523
+ lastResult = await this.doRequest(
4524
+ requestInfo,
4525
+ requestOptions,
4526
+ attemptInfo,
4527
+ timeoutController
4528
+ ).catch((reason) => {
4529
+ var _a2;
4530
+ const completeInfo = {
4531
+ ok: false,
4532
+ type: ((_a2 = requestOptions.abort) == null ? void 0 : _a2.aborted()) ? "abort" /* ABORT */ : "internal" /* INTERNAL */,
4533
+ requestInfo,
4534
+ reason,
4535
+ willRetry: false,
4536
+ timestamp: Date.now(),
4537
+ retryAfter: null
4538
+ };
4539
+ return { info: completeInfo, response: null };
4540
+ });
4541
+ this.reportMetricsEntry(lastResult.info);
4542
+ this.dispatch("requestAttemptComplete", lastResult.info);
4543
+ } while (!lastResult.info.ok && lastResult.info.willRetry);
4544
+ } finally {
4530
4545
  timeoutController.dispose();
4531
- this.reportMetricsEntry(lastResult.info);
4532
- this.dispatch("requestAttemptComplete", lastResult.info);
4533
- } while (!lastResult.info.ok && lastResult.info.willRetry);
4546
+ }
4534
4547
  this.dispatch("requestComplete", lastResult.info);
4535
4548
  if (lastResult.info.ok) return lastResult.response;
4536
4549
  else throw new RequestError(lastResult.response, lastResult.info);
@@ -4991,6 +5004,13 @@ function utf16ToUint16Array(str) {
4991
5004
  function bufferToByteArray(buffer) {
4992
5005
  return buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
4993
5006
  }
5007
+ function bufferToArrayBuffer(buffer) {
5008
+ if (buffer instanceof ArrayBuffer) return buffer;
5009
+ return buffer.buffer.slice(
5010
+ buffer.byteOffset,
5011
+ buffer.byteOffset + buffer.byteLength
5012
+ );
5013
+ }
4994
5014
  function buffersEqual(bufferA, bufferB) {
4995
5015
  if (bufferA === bufferB) return true;
4996
5016
  if (!bufferA && !bufferB) return true;
@@ -5395,6 +5415,64 @@ function flowRightAsync(...fns) {
5395
5415
  };
5396
5416
  }
5397
5417
 
5418
+ // src/util/fun/rateLimit.ts
5419
+ function rateLimit(inner, options) {
5420
+ const { capacity, interval } = options;
5421
+ if (capacity < 1)
5422
+ throw new IllegalArgumentError("capacity must be at least 1.");
5423
+ if (interval <= 0)
5424
+ throw new IllegalArgumentError("interval must be greater than 0.");
5425
+ const intervalMs = interval * 1e3;
5426
+ let tokens = capacity;
5427
+ let lastRefillMs = Date.now();
5428
+ let pending = 0;
5429
+ const timeout2 = new TimeoutSlot();
5430
+ let disposed = false;
5431
+ const refill = () => {
5432
+ const now = Date.now();
5433
+ const refilled = Math.floor((now - lastRefillMs) / intervalMs);
5434
+ if (refilled > 0) {
5435
+ tokens = Math.min(capacity, tokens + refilled);
5436
+ lastRefillMs += refilled * intervalMs;
5437
+ }
5438
+ if (tokens === capacity) lastRefillMs = now;
5439
+ };
5440
+ const drain = () => {
5441
+ refill();
5442
+ while (pending > 0 && tokens >= 1) {
5443
+ tokens -= 1;
5444
+ pending -= 1;
5445
+ inner();
5446
+ }
5447
+ if (pending > 0 && !timeout2.active) {
5448
+ const nextTokenMs = intervalMs - (Date.now() - lastRefillMs);
5449
+ timeout2.set(drain, nextTokenMs / 1e3);
5450
+ }
5451
+ };
5452
+ return Object.assign(
5453
+ () => {
5454
+ if (disposed) throw new DisposedError();
5455
+ pending += 1;
5456
+ drain();
5457
+ },
5458
+ {
5459
+ reset() {
5460
+ tokens = capacity;
5461
+ lastRefillMs = Date.now();
5462
+ if (timeout2.active) timeout2.clear();
5463
+ while (pending > 0) {
5464
+ pending -= 1;
5465
+ inner();
5466
+ }
5467
+ },
5468
+ dispose() {
5469
+ disposed = true;
5470
+ timeout2.dispose();
5471
+ }
5472
+ }
5473
+ );
5474
+ }
5475
+
5398
5476
  // src/util/mpeg/parseSidxBox.ts
5399
5477
  function parseSidxBox(buffer) {
5400
5478
  const reader = new BufferReader(buffer);