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

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.js CHANGED
@@ -1214,7 +1214,8 @@ var LruCache = class {
1214
1214
  return true;
1215
1215
  }
1216
1216
  /**
1217
- * Executes a provided function once per each key/value pair in the Map, in insertion order.
1217
+ * Executes a provided function once per each key/value pair in the Map,
1218
+ * ordered from least recently used to most.
1218
1219
  *
1219
1220
  * @param callback A callback to invoke for every element in the cache.
1220
1221
  * @param thisArg Value to use as this when executing callback
@@ -1259,8 +1260,8 @@ function memoize(inner, keyProvider, capacity = 500) {
1259
1260
  let value = void 0;
1260
1261
  const memoized = () => {
1261
1262
  if (!cached) {
1262
- cached = true;
1263
1263
  value = inner();
1264
+ cached = true;
1264
1265
  }
1265
1266
  return value;
1266
1267
  };
@@ -1289,9 +1290,9 @@ function memoize(inner, keyProvider, capacity = 500) {
1289
1290
  const memoized = (...args) => {
1290
1291
  const key = keyProvider(...args);
1291
1292
  if (!hasValue || cachedKey !== key) {
1292
- hasValue = true;
1293
- cachedKey = key;
1294
1293
  cachedValue = inner(...args);
1294
+ cachedKey = key;
1295
+ hasValue = true;
1295
1296
  }
1296
1297
  return cachedValue;
1297
1298
  };
@@ -1354,7 +1355,8 @@ function isWhitespaceChar(charCode) {
1354
1355
  charCode === 13 || // '\r' (carriage return)
1355
1356
  charCode === 10;
1356
1357
  }
1357
- var encodedEntitiesRegex = /&(#(\d+)|\w+);/gi;
1358
+ var encodedEntitiesRegex = /&(?:#(?:x([\da-f]+)|(\d+))|(\w+));/gi;
1359
+ var MAX_CODE_POINT = 1114111;
1358
1360
  var predefinedDecodeEntities = /* @__PURE__ */ new Map([
1359
1361
  ["apos", "'"],
1360
1362
  ["amp", "&"],
@@ -1363,20 +1365,22 @@ var predefinedDecodeEntities = /* @__PURE__ */ new Map([
1363
1365
  ["quot", '"']
1364
1366
  ]);
1365
1367
  function decodeEntities(str, namedEntities = predefinedDecodeEntities) {
1366
- if (encodedEntitiesRegex.test(str)) {
1367
- return str.replace(
1368
- encodedEntitiesRegex,
1369
- (substring, entityName, codePoint) => {
1370
- if (codePoint !== void 0)
1371
- return String.fromCharCode(parseInt(codePoint));
1372
- const entityValue = namedEntities.get(entityName.toLowerCase());
1373
- if (!entityValue) return substring;
1374
- return entityValue;
1368
+ return str.replace(
1369
+ encodedEntitiesRegex,
1370
+ (substring, hex, decimal, entityName) => {
1371
+ var _a;
1372
+ if (hex == null && decimal == null) {
1373
+ return (_a = namedEntities.get(entityName.toLowerCase())) != null ? _a : substring;
1375
1374
  }
1376
- );
1377
- } else {
1378
- return str;
1379
- }
1375
+ const codePoint = hex != null ? parseInt(hex, 16) : parseInt(decimal, 10);
1376
+ if (codePoint > MAX_CODE_POINT || isSurrogate(codePoint))
1377
+ return substring;
1378
+ return String.fromCodePoint(codePoint);
1379
+ }
1380
+ );
1381
+ }
1382
+ function isSurrogate(codePoint) {
1383
+ return codePoint >= 55296 && codePoint <= 57343;
1380
1384
  }
1381
1385
  var standardEntityValueToNames = /* @__PURE__ */ flipMap(predefinedDecodeEntities);
1382
1386
  var entitiesRegex = /['&<>"]/g;
@@ -3659,7 +3663,11 @@ var NetworkMetricsControllerImpl = class extends EventHostImpl {
3659
3663
  __publicField(this, "onDownloadSpeedChange", () => {
3660
3664
  var _a;
3661
3665
  const downlink = (_a = this.deps.networkInformation) == null ? void 0 : _a.downlink;
3662
- const estimatedSpeed = downlink ? downlink * 1024 * 1024 : this.options.unknownDownlinkBandwidthEstimate;
3666
+ const estimatedSpeed = downlink ? (
3667
+ // Megabits are decimal, matching the measured `bytes * 8 / seconds`
3668
+ // this estimate is averaged with.
3669
+ downlink * 1e6
3670
+ ) : this.options.unknownDownlinkBandwidthEstimate;
3663
3671
  updateStatMetrics(
3664
3672
  this._metrics.estimatedDownlinkBandwidth,
3665
3673
  estimatedSpeed,
@@ -4167,38 +4175,41 @@ var RequesterImpl = class extends EventHostImpl {
4167
4175
  this.dispatch("request", { requestInfo });
4168
4176
  let lastResult = null;
4169
4177
  let currentTry = 0;
4170
- do {
4171
- ++currentTry;
4172
- const attemptInfo = {
4173
- currentTry,
4174
- timestamp: Date.now()
4175
- };
4176
- this.dispatch("requestAttemptStart", {
4177
- requestInfo,
4178
- attemptInfo
4179
- });
4180
- lastResult = await this.doRequest(
4181
- requestInfo,
4182
- requestOptions,
4183
- attemptInfo,
4184
- timeoutController
4185
- ).catch((reason) => {
4186
- var _a2;
4187
- const completeInfo = {
4188
- ok: false,
4189
- type: ((_a2 = requestOptions.abort) == null ? void 0 : _a2.aborted()) ? "abort" /* ABORT */ : "internal" /* INTERNAL */,
4190
- requestInfo,
4191
- reason,
4192
- willRetry: false,
4193
- timestamp: Date.now(),
4194
- retryAfter: null
4178
+ try {
4179
+ do {
4180
+ ++currentTry;
4181
+ const attemptInfo = {
4182
+ currentTry,
4183
+ timestamp: Date.now()
4195
4184
  };
4196
- return { info: completeInfo, response: null };
4197
- });
4185
+ this.dispatch("requestAttemptStart", {
4186
+ requestInfo,
4187
+ attemptInfo
4188
+ });
4189
+ lastResult = await this.doRequest(
4190
+ requestInfo,
4191
+ requestOptions,
4192
+ attemptInfo,
4193
+ timeoutController
4194
+ ).catch((reason) => {
4195
+ var _a2;
4196
+ const completeInfo = {
4197
+ ok: false,
4198
+ type: ((_a2 = requestOptions.abort) == null ? void 0 : _a2.aborted()) ? "abort" /* ABORT */ : "internal" /* INTERNAL */,
4199
+ requestInfo,
4200
+ reason,
4201
+ willRetry: false,
4202
+ timestamp: Date.now(),
4203
+ retryAfter: null
4204
+ };
4205
+ return { info: completeInfo, response: null };
4206
+ });
4207
+ this.reportMetricsEntry(lastResult.info);
4208
+ this.dispatch("requestAttemptComplete", lastResult.info);
4209
+ } while (!lastResult.info.ok && lastResult.info.willRetry);
4210
+ } finally {
4198
4211
  timeoutController.dispose();
4199
- this.reportMetricsEntry(lastResult.info);
4200
- this.dispatch("requestAttemptComplete", lastResult.info);
4201
- } while (!lastResult.info.ok && lastResult.info.willRetry);
4212
+ }
4202
4213
  this.dispatch("requestComplete", lastResult.info);
4203
4214
  if (lastResult.info.ok) return lastResult.response;
4204
4215
  else throw new RequestError(lastResult.response, lastResult.info);
@@ -4659,6 +4670,13 @@ function utf16ToUint16Array(str) {
4659
4670
  function bufferToByteArray(buffer) {
4660
4671
  return buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
4661
4672
  }
4673
+ function bufferToArrayBuffer(buffer) {
4674
+ if (buffer instanceof ArrayBuffer) return buffer;
4675
+ return buffer.buffer.slice(
4676
+ buffer.byteOffset,
4677
+ buffer.byteOffset + buffer.byteLength
4678
+ );
4679
+ }
4662
4680
  function buffersEqual(bufferA, bufferB) {
4663
4681
  if (bufferA === bufferB) return true;
4664
4682
  if (!bufferA && !bufferB) return true;
@@ -5063,6 +5081,64 @@ function flowRightAsync(...fns) {
5063
5081
  };
5064
5082
  }
5065
5083
 
5084
+ // src/util/fun/rateLimit.ts
5085
+ function rateLimit(inner, options) {
5086
+ const { capacity, interval } = options;
5087
+ if (capacity < 1)
5088
+ throw new IllegalArgumentError("capacity must be at least 1.");
5089
+ if (interval <= 0)
5090
+ throw new IllegalArgumentError("interval must be greater than 0.");
5091
+ const intervalMs = interval * 1e3;
5092
+ let tokens = capacity;
5093
+ let lastRefillMs = Date.now();
5094
+ let pending = 0;
5095
+ const timeout2 = new TimeoutSlot();
5096
+ let disposed = false;
5097
+ const refill = () => {
5098
+ const now = Date.now();
5099
+ const refilled = Math.floor((now - lastRefillMs) / intervalMs);
5100
+ if (refilled > 0) {
5101
+ tokens = Math.min(capacity, tokens + refilled);
5102
+ lastRefillMs += refilled * intervalMs;
5103
+ }
5104
+ if (tokens === capacity) lastRefillMs = now;
5105
+ };
5106
+ const drain = () => {
5107
+ refill();
5108
+ while (pending > 0 && tokens >= 1) {
5109
+ tokens -= 1;
5110
+ pending -= 1;
5111
+ inner();
5112
+ }
5113
+ if (pending > 0 && !timeout2.active) {
5114
+ const nextTokenMs = intervalMs - (Date.now() - lastRefillMs);
5115
+ timeout2.set(drain, nextTokenMs / 1e3);
5116
+ }
5117
+ };
5118
+ return Object.assign(
5119
+ () => {
5120
+ if (disposed) throw new DisposedError();
5121
+ pending += 1;
5122
+ drain();
5123
+ },
5124
+ {
5125
+ reset() {
5126
+ tokens = capacity;
5127
+ lastRefillMs = Date.now();
5128
+ if (timeout2.active) timeout2.clear();
5129
+ while (pending > 0) {
5130
+ pending -= 1;
5131
+ inner();
5132
+ }
5133
+ },
5134
+ dispose() {
5135
+ disposed = true;
5136
+ timeout2.dispose();
5137
+ }
5138
+ }
5139
+ );
5140
+ }
5141
+
5066
5142
  // src/util/mpeg/parseSidxBox.ts
5067
5143
  function parseSidxBox(buffer) {
5068
5144
  const reader = new BufferReader(buffer);
@@ -5854,6 +5930,7 @@ export {
5854
5930
  arrayEqualRule,
5855
5931
  arrayMergeRule,
5856
5932
  base64ToByteArray,
5933
+ bufferToArrayBuffer,
5857
5934
  bufferToBase64,
5858
5935
  bufferToByteArray,
5859
5936
  bufferToByteStr,
@@ -6027,6 +6104,7 @@ export {
6027
6104
  promise,
6028
6105
  rangeIntersects,
6029
6106
  rangesOf,
6107
+ rateLimit,
6030
6108
  readResponseBody,
6031
6109
  redispatchEvents,
6032
6110
  regexRule,