@amazon/vinyl-util 3.2.1 → 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
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { DisposedError } from '../../core/disposable';
6
+ import { IllegalArgumentError } from '../../error/IllegalArgumentError';
7
+ import { TimeoutSlot } from '../async/TimeoutSlot';
8
+ /**
9
+ * Rate-limits invocations with a token bucket. The bucket holds up to
10
+ * {@link RateLimitOptions.capacity} tokens and refills one token every
11
+ * {@link RateLimitOptions.interval} seconds. Each call consumes a token and runs
12
+ * immediately; a call made while the bucket is empty is queued and invoked as
13
+ * tokens refill (in call order).
14
+ *
15
+ * Unlike {@link throttle}, which drops calls that arrive within its window,
16
+ * `rateLimit` never drops a call — it only delays the ones that exceed the rate.
17
+ * A full bucket allows an initial burst of `capacity` calls.
18
+ *
19
+ * @param inner The function to invoke at the limited rate.
20
+ * @param options The bucket capacity (max burst) and refill interval.
21
+ * @return A callback that queues-and-limits invocations of `inner`, with `reset`
22
+ * and `dispose` handles. `dispose` cancels any queued calls and prevents further
23
+ * invocation.
24
+ */
25
+ export function rateLimit(inner, options) {
26
+ const { capacity, interval } = options;
27
+ if (capacity < 1)
28
+ throw new IllegalArgumentError('capacity must be at least 1.');
29
+ if (interval <= 0)
30
+ throw new IllegalArgumentError('interval must be greater than 0.');
31
+ const intervalMs = interval * 1000;
32
+ let tokens = capacity;
33
+ let lastRefillMs = Date.now();
34
+ // The number of calls waiting for a token. Inner takes no arguments, so a
35
+ // count preserves call order without needing a queue of thunks.
36
+ let pending = 0;
37
+ const timeout = new TimeoutSlot();
38
+ let disposed = false;
39
+ // Credits whole tokens for the time elapsed since the last refill, capped at
40
+ // capacity. When the bucket is full the refill clock is synced to now so idle
41
+ // time can't bank tokens beyond capacity.
42
+ const refill = () => {
43
+ const now = Date.now();
44
+ const refilled = Math.floor((now - lastRefillMs) / intervalMs);
45
+ if (refilled > 0) {
46
+ tokens = Math.min(capacity, tokens + refilled);
47
+ lastRefillMs += refilled * intervalMs;
48
+ }
49
+ if (tokens === capacity)
50
+ lastRefillMs = now;
51
+ };
52
+ const drain = () => {
53
+ refill();
54
+ while (pending > 0 && tokens >= 1) {
55
+ tokens -= 1;
56
+ pending -= 1;
57
+ inner();
58
+ }
59
+ if (pending > 0 && !timeout.active) {
60
+ // Wait until the next whole token refills, then drain again.
61
+ const nextTokenMs = intervalMs - (Date.now() - lastRefillMs);
62
+ timeout.set(drain, nextTokenMs / 1000);
63
+ }
64
+ };
65
+ return Object.assign(() => {
66
+ if (disposed)
67
+ throw new DisposedError();
68
+ pending += 1;
69
+ drain();
70
+ }, {
71
+ reset() {
72
+ tokens = capacity;
73
+ lastRefillMs = Date.now();
74
+ if (timeout.active)
75
+ timeout.clear();
76
+ while (pending > 0) {
77
+ pending -= 1;
78
+ inner();
79
+ }
80
+ },
81
+ dispose() {
82
+ disposed = true;
83
+ timeout.dispose();
84
+ },
85
+ });
86
+ }
@@ -36,6 +36,10 @@ export declare function isWhitespaceChar(charCode: number): boolean;
36
36
  export declare const predefinedDecodeEntities: ReadonlyMap<string, string>;
37
37
  /**
38
38
  * Replaces all specified entities with their mapped representations.
39
+ * Decimal (`&#33;`) and hexadecimal (`&#x21;`) character references are both
40
+ * decoded. An unrecognized named entity, or a character reference that is not a
41
+ * valid code point, is left as-is.
42
+ *
39
43
  * For example (using {@link predefinedDecodeEntities}):
40
44
  * ```
41
45
  * `&lt;Hello to y&apos;all&#33;&gt;` becomes `<Hello to y'all!>`
@@ -1 +1 @@
1
- {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../../../src/util/string/string.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAc5D;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CACtB,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EACxC,OAAO,GAAE,MAAmC,GAC7C,MAAM,CASR;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACjC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAC/C,MAAM,CAYR;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAO1D;AAID;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAM/D,CAAA;AAEF;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC1B,GAAG,EAAE,MAAM,EACX,aAAa,GAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAA4B,GACtE,MAAM,CAeR;AAKD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CASlD;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CACjB,CA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AgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAElE;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAElE;AAED;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,EAC5C,GAAG,EAAE,CAAC,EACN,SAAS,EAAE,MAAM,EACjB,SAAS,SAAM,GAChB,CAAC,GAAG,MAAM,CAKZ;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAQ/C"}
@@ -68,7 +68,11 @@ export function isWhitespaceChar(charCode) {
68
68
  charCode === 0x0a // '\n' (line feed)
69
69
  );
70
70
  }
71
- const encodedEntitiesRegex = /&(#(\d+)|\w+);/gi;
71
+ const encodedEntitiesRegex = /&(?:#(?:x([\da-f]+)|(\d+))|(\w+));/gi;
72
+ /**
73
+ * The highest code point representable in UTF-16.
74
+ */
75
+ const MAX_CODE_POINT = 0x10ffff;
72
76
  /**
73
77
  * Predefined general xml entities
74
78
  * https://www.w3.org/TR/2008/REC-xml-20081126/#sec-predefined-ent
@@ -82,6 +86,10 @@ export const predefinedDecodeEntities = new Map([
82
86
  ]);
83
87
  /**
84
88
  * Replaces all specified entities with their mapped representations.
89
+ * Decimal (`&#33;`) and hexadecimal (`&#x21;`) character references are both
90
+ * decoded. An unrecognized named entity, or a character reference that is not a
91
+ * valid code point, is left as-is.
92
+ *
85
93
  * For example (using {@link predefinedDecodeEntities}):
86
94
  * ```
87
95
  * `&lt;Hello to y&apos;all&#33;&gt;` becomes `<Hello to y'all!>`
@@ -91,19 +99,20 @@ export const predefinedDecodeEntities = new Map([
91
99
  * @param namedEntities
92
100
  */
93
101
  export function decodeEntities(str, namedEntities = predefinedDecodeEntities) {
94
- if (encodedEntitiesRegex.test(str)) {
95
- return str.replace(encodedEntitiesRegex, (substring, entityName, codePoint) => {
96
- if (codePoint !== undefined)
97
- return String.fromCharCode(parseInt(codePoint));
98
- const entityValue = namedEntities.get(entityName.toLowerCase());
99
- if (!entityValue)
100
- return substring;
101
- return entityValue;
102
- });
103
- }
104
- else {
105
- return str;
106
- }
102
+ return str.replace(encodedEntitiesRegex, (substring, hex, decimal, entityName) => {
103
+ if (hex == null && decimal == null) {
104
+ return namedEntities.get(entityName.toLowerCase()) ?? substring;
105
+ }
106
+ const codePoint = hex != null ? parseInt(hex, 16) : parseInt(decimal, 10);
107
+ // Surrogate halves are not characters, and code points beyond the
108
+ // Unicode range would throw.
109
+ if (codePoint > MAX_CODE_POINT || isSurrogate(codePoint))
110
+ return substring;
111
+ return String.fromCodePoint(codePoint);
112
+ });
113
+ }
114
+ function isSurrogate(codePoint) {
115
+ return codePoint >= 0xd800 && codePoint <= 0xdfff;
107
116
  }
108
117
  const standardEntityValueToNames = flipMap(predefinedDecodeEntities);
109
118
  const entitiesRegex = /['&<>"]/g;
@@ -87,6 +87,7 @@ export * from './util/fun/debounce';
87
87
  export * from './util/fun/flow';
88
88
  export * from './util/fun/function';
89
89
  export * from './util/fun/memoize';
90
+ export * from './util/fun/rateLimit';
90
91
  export * from './util/fun/throttle';
91
92
  export * from './util/iteration/iterable';
92
93
  export * from './util/map/map';
@@ -48,6 +48,15 @@ export declare function utf16ToUint16Array(str: string): Uint16Array;
48
48
  * @param buffer
49
49
  */
50
50
  export declare function bufferToByteArray(buffer: Uint8Array | ArrayLike<number> | ArrayBuffer): Uint8Array;
51
+ /**
52
+ * Returns the bytes a buffer view spans as a standalone ArrayBuffer.
53
+ *
54
+ * Reading `buffer` from a view directly is rarely correct; it spans the whole
55
+ * underlying allocation, ignoring the view's `byteOffset` and `byteLength`.
56
+ *
57
+ * @param buffer An ArrayBuffer, which is returned as is, or a view to copy from.
58
+ */
59
+ export declare function bufferToArrayBuffer(buffer: ArrayBufferView<ArrayBuffer> | ArrayBuffer): ArrayBuffer;
51
60
  /**
52
61
  * Returns true if the two buffers are equal.
53
62
  */
@@ -64,7 +64,8 @@ export declare class LruCache<K, V> implements CacheMap<K, V> {
64
64
  */
65
65
  delete(key: K): boolean;
66
66
  /**
67
- * Executes a provided function once per each key/value pair in the Map, in insertion order.
67
+ * Executes a provided function once per each key/value pair in the Map,
68
+ * ordered from least recently used to most.
68
69
  *
69
70
  * @param callback A callback to invoke for every element in the cache.
70
71
  * @param thisArg Value to use as this when executing callback
@@ -0,0 +1,40 @@
1
+ import { Disposable } from '../../core/disposable';
2
+ export interface RateLimitOptions {
3
+ /**
4
+ * The maximum number of calls that may run immediately in a burst — the
5
+ * token bucket's capacity. Must be at least 1.
6
+ */
7
+ readonly capacity: number;
8
+ /**
9
+ * Seconds to refill one token. In steady state the inner function runs at
10
+ * most once every `interval` seconds. Must be greater than 0.
11
+ */
12
+ readonly interval: number;
13
+ }
14
+ export interface RateLimitedCallback extends Disposable {
15
+ (): void;
16
+ /**
17
+ * Refills the bucket to capacity and immediately invokes any queued
18
+ * (delayed) calls.
19
+ */
20
+ reset(): void;
21
+ }
22
+ /**
23
+ * Rate-limits invocations with a token bucket. The bucket holds up to
24
+ * {@link RateLimitOptions.capacity} tokens and refills one token every
25
+ * {@link RateLimitOptions.interval} seconds. Each call consumes a token and runs
26
+ * immediately; a call made while the bucket is empty is queued and invoked as
27
+ * tokens refill (in call order).
28
+ *
29
+ * Unlike {@link throttle}, which drops calls that arrive within its window,
30
+ * `rateLimit` never drops a call — it only delays the ones that exceed the rate.
31
+ * A full bucket allows an initial burst of `capacity` calls.
32
+ *
33
+ * @param inner The function to invoke at the limited rate.
34
+ * @param options The bucket capacity (max burst) and refill interval.
35
+ * @return A callback that queues-and-limits invocations of `inner`, with `reset`
36
+ * and `dispose` handles. `dispose` cancels any queued calls and prevents further
37
+ * invocation.
38
+ */
39
+ export declare function rateLimit(inner: () => void, options: RateLimitOptions): RateLimitedCallback;
40
+ //# sourceMappingURL=rateLimit.d.ts.map
@@ -36,6 +36,10 @@ export declare function isWhitespaceChar(charCode: number): boolean;
36
36
  export declare const predefinedDecodeEntities: ReadonlyMap<string, string>;
37
37
  /**
38
38
  * Replaces all specified entities with their mapped representations.
39
+ * Decimal (`&#33;`) and hexadecimal (`&#x21;`) character references are both
40
+ * decoded. An unrecognized named entity, or a character reference that is not a
41
+ * valid code point, is left as-is.
42
+ *
39
43
  * For example (using {@link predefinedDecodeEntities}):
40
44
  * ```
41
45
  * `&lt;Hello to y&apos;all&#33;&gt;` becomes `<Hello to y'all!>`
@@ -87,6 +87,7 @@ export * from './util/fun/debounce';
87
87
  export * from './util/fun/flow';
88
88
  export * from './util/fun/function';
89
89
  export * from './util/fun/memoize';
90
+ export * from './util/fun/rateLimit';
90
91
  export * from './util/fun/throttle';
91
92
  export * from './util/iteration/iterable';
92
93
  export * from './util/map/map';
@@ -48,6 +48,15 @@ export declare function utf16ToUint16Array(str: string): Uint16Array;
48
48
  * @param buffer
49
49
  */
50
50
  export declare function bufferToByteArray(buffer: Uint8Array | ArrayLike<number> | ArrayBuffer): Uint8Array;
51
+ /**
52
+ * Returns the bytes a buffer view spans as a standalone ArrayBuffer.
53
+ *
54
+ * Reading `buffer` from a view directly is rarely correct; it spans the whole
55
+ * underlying allocation, ignoring the view's `byteOffset` and `byteLength`.
56
+ *
57
+ * @param buffer An ArrayBuffer, which is returned as is, or a view to copy from.
58
+ */
59
+ export declare function bufferToArrayBuffer(buffer: ArrayBufferView<ArrayBuffer> | ArrayBuffer): ArrayBuffer;
51
60
  /**
52
61
  * Returns true if the two buffers are equal.
53
62
  */
@@ -64,7 +64,8 @@ export declare class LruCache<K, V> implements CacheMap<K, V> {
64
64
  */
65
65
  delete(key: K): boolean;
66
66
  /**
67
- * Executes a provided function once per each key/value pair in the Map, in insertion order.
67
+ * Executes a provided function once per each key/value pair in the Map,
68
+ * ordered from least recently used to most.
68
69
  *
69
70
  * @param callback A callback to invoke for every element in the cache.
70
71
  * @param thisArg Value to use as this when executing callback
@@ -0,0 +1,40 @@
1
+ import type { Disposable } from '../../core/disposable';
2
+ export interface RateLimitOptions {
3
+ /**
4
+ * The maximum number of calls that may run immediately in a burst — the
5
+ * token bucket's capacity. Must be at least 1.
6
+ */
7
+ readonly capacity: number;
8
+ /**
9
+ * Seconds to refill one token. In steady state the inner function runs at
10
+ * most once every `interval` seconds. Must be greater than 0.
11
+ */
12
+ readonly interval: number;
13
+ }
14
+ export interface RateLimitedCallback extends Disposable {
15
+ (): void;
16
+ /**
17
+ * Refills the bucket to capacity and immediately invokes any queued
18
+ * (delayed) calls.
19
+ */
20
+ reset(): void;
21
+ }
22
+ /**
23
+ * Rate-limits invocations with a token bucket. The bucket holds up to
24
+ * {@link RateLimitOptions.capacity} tokens and refills one token every
25
+ * {@link RateLimitOptions.interval} seconds. Each call consumes a token and runs
26
+ * immediately; a call made while the bucket is empty is queued and invoked as
27
+ * tokens refill (in call order).
28
+ *
29
+ * Unlike {@link throttle}, which drops calls that arrive within its window,
30
+ * `rateLimit` never drops a call — it only delays the ones that exceed the rate.
31
+ * A full bucket allows an initial burst of `capacity` calls.
32
+ *
33
+ * @param inner The function to invoke at the limited rate.
34
+ * @param options The bucket capacity (max burst) and refill interval.
35
+ * @return A callback that queues-and-limits invocations of `inner`, with `reset`
36
+ * and `dispose` handles. `dispose` cancels any queued calls and prevents further
37
+ * invocation.
38
+ */
39
+ export declare function rateLimit(inner: () => void, options: RateLimitOptions): RateLimitedCallback;
40
+ //# sourceMappingURL=rateLimit.d.ts.map
@@ -36,6 +36,10 @@ export declare function isWhitespaceChar(charCode: number): boolean;
36
36
  export declare const predefinedDecodeEntities: ReadonlyMap<string, string>;
37
37
  /**
38
38
  * Replaces all specified entities with their mapped representations.
39
+ * Decimal (`&#33;`) and hexadecimal (`&#x21;`) character references are both
40
+ * decoded. An unrecognized named entity, or a character reference that is not a
41
+ * valid code point, is left as-is.
42
+ *
39
43
  * For example (using {@link predefinedDecodeEntities}):
40
44
  * ```
41
45
  * `&lt;Hello to y&apos;all&#33;&gt;` becomes `<Hello to y'all!>`
@@ -87,6 +87,7 @@ export * from './util/fun/debounce';
87
87
  export * from './util/fun/flow';
88
88
  export * from './util/fun/function';
89
89
  export * from './util/fun/memoize';
90
+ export * from './util/fun/rateLimit';
90
91
  export * from './util/fun/throttle';
91
92
  export * from './util/iteration/iterable';
92
93
  export * from './util/map/map';
@@ -48,6 +48,15 @@ export declare function utf16ToUint16Array(str: string): Uint16Array;
48
48
  * @param buffer
49
49
  */
50
50
  export declare function bufferToByteArray(buffer: Uint8Array | ArrayLike<number> | ArrayBuffer): Uint8Array;
51
+ /**
52
+ * Returns the bytes a buffer view spans as a standalone ArrayBuffer.
53
+ *
54
+ * Reading `buffer` from a view directly is rarely correct; it spans the whole
55
+ * underlying allocation, ignoring the view's `byteOffset` and `byteLength`.
56
+ *
57
+ * @param buffer An ArrayBuffer, which is returned as is, or a view to copy from.
58
+ */
59
+ export declare function bufferToArrayBuffer(buffer: ArrayBufferView<ArrayBuffer> | ArrayBuffer): ArrayBuffer;
51
60
  /**
52
61
  * Returns true if the two buffers are equal.
53
62
  */
@@ -67,7 +67,8 @@ export declare class LruCache<K, V> implements CacheMap<K, V> {
67
67
  */
68
68
  delete(key: K): boolean;
69
69
  /**
70
- * Executes a provided function once per each key/value pair in the Map, in insertion order.
70
+ * Executes a provided function once per each key/value pair in the Map,
71
+ * ordered from least recently used to most.
71
72
  *
72
73
  * @param callback A callback to invoke for every element in the cache.
73
74
  * @param thisArg Value to use as this when executing callback
@@ -0,0 +1,40 @@
1
+ import type { Disposable } from '../../core/disposable';
2
+ export interface RateLimitOptions {
3
+ /**
4
+ * The maximum number of calls that may run immediately in a burst — the
5
+ * token bucket's capacity. Must be at least 1.
6
+ */
7
+ readonly capacity: number;
8
+ /**
9
+ * Seconds to refill one token. In steady state the inner function runs at
10
+ * most once every `interval` seconds. Must be greater than 0.
11
+ */
12
+ readonly interval: number;
13
+ }
14
+ export interface RateLimitedCallback extends Disposable {
15
+ (): void;
16
+ /**
17
+ * Refills the bucket to capacity and immediately invokes any queued
18
+ * (delayed) calls.
19
+ */
20
+ reset(): void;
21
+ }
22
+ /**
23
+ * Rate-limits invocations with a token bucket. The bucket holds up to
24
+ * {@link RateLimitOptions.capacity} tokens and refills one token every
25
+ * {@link RateLimitOptions.interval} seconds. Each call consumes a token and runs
26
+ * immediately; a call made while the bucket is empty is queued and invoked as
27
+ * tokens refill (in call order).
28
+ *
29
+ * Unlike {@link throttle}, which drops calls that arrive within its window,
30
+ * `rateLimit` never drops a call — it only delays the ones that exceed the rate.
31
+ * A full bucket allows an initial burst of `capacity` calls.
32
+ *
33
+ * @param inner The function to invoke at the limited rate.
34
+ * @param options The bucket capacity (max burst) and refill interval.
35
+ * @return A callback that queues-and-limits invocations of `inner`, with `reset`
36
+ * and `dispose` handles. `dispose` cancels any queued calls and prevents further
37
+ * invocation.
38
+ */
39
+ export declare function rateLimit(inner: () => void, options: RateLimitOptions): RateLimitedCallback;
40
+ //# sourceMappingURL=rateLimit.d.ts.map
@@ -36,6 +36,10 @@ export declare function isWhitespaceChar(charCode: number): boolean;
36
36
  export declare const predefinedDecodeEntities: ReadonlyMap<string, string>;
37
37
  /**
38
38
  * Replaces all specified entities with their mapped representations.
39
+ * Decimal (`&#33;`) and hexadecimal (`&#x21;`) character references are both
40
+ * decoded. An unrecognized named entity, or a character reference that is not a
41
+ * valid code point, is left as-is.
42
+ *
39
43
  * For example (using {@link predefinedDecodeEntities}):
40
44
  * ```
41
45
  * `&lt;Hello to y&apos;all&#33;&gt;` becomes `<Hello to y'all!>`