This commit is contained in:
2025-11-11 00:04:55 -06:00
parent a76ad41fde
commit 40538eddfd
6405 changed files with 1289756 additions and 0 deletions

117
node_modules/p-throttle/index.d.ts generated vendored Normal file
View File

@ -0,0 +1,117 @@
export class AbortError extends Error {
readonly name: 'AbortError';
private constructor();
}
type AnyFunction = (...arguments_: readonly any[]) => unknown;
export type ThrottledFunction<F extends AnyFunction> = F & {
/**
Whether future function calls should be throttled or count towards throttling thresholds.
@default true
*/
isEnabled: boolean;
/**
The number of queued items waiting to be executed.
*/
readonly queueSize: number;
/**
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
abort(): void;
};
export type Options = {
/**
The maximum number of calls within an `interval`.
*/
readonly limit: number;
/**
The timespan for `limit` in milliseconds.
*/
readonly interval: number;
/**
Use a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval.
@default false
*/
readonly strict?: boolean;
/**
Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. The delayed call arguments are passed to the `onDelay` callback.
Can be useful for monitoring the throttling efficiency.
@example
```
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 2,
interval: 1000,
onDelay: (a, b) => {
console.log(`Reached interval limit, call is delayed for ${a} ${b}`);
},
});
const throttled = throttle((a, b) => {
console.log(`Executing with ${a} ${b}...`);
});
await throttled(1, 2);
await throttled(3, 4);
await throttled(5, 6);
//=> Executing with 1 2...
//=> Executing with 3 4...
//=> Reached interval limit, call is delayed for 5 6
//=> Executing with 5 6...
```
*/
readonly onDelay?: (...arguments_: readonly any[]) => void;
};
/**
Throttle promise-returning/async/normal functions.
It rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.
@returns A throttle function.
Both the `limit` and `interval` options must be specified.
@example
```
import pThrottle from 'p-throttle';
const now = Date.now();
const throttle = pThrottle({
limit: 2,
interval: 1000
});
const throttled = throttle(async index => {
const secDiff = ((Date.now() - now) / 1000).toFixed();
return `${index}: ${secDiff}s`;
});
for (let index = 1; index <= 6; index++) {
(async () => {
console.log(await throttled(index));
})();
}
//=> 1: 0s
//=> 2: 0s
//=> 3: 1s
//=> 4: 1s
//=> 5: 2s
//=> 6: 2s
```
*/
export default function pThrottle(options: Options): <F extends AnyFunction>(function_: F) => ThrottledFunction<F>;

114
node_modules/p-throttle/index.js generated vendored Normal file
View File

@ -0,0 +1,114 @@
export class AbortError extends Error {
constructor() {
super('Throttled function aborted');
this.name = 'AbortError';
}
}
export default function pThrottle({limit, interval, strict, onDelay}) {
if (!Number.isFinite(limit)) {
throw new TypeError('Expected `limit` to be a finite number');
}
if (!Number.isFinite(interval)) {
throw new TypeError('Expected `interval` to be a finite number');
}
const queue = new Map();
let currentTick = 0;
let activeCount = 0;
function windowedDelay() {
const now = Date.now();
if ((now - currentTick) > interval) {
activeCount = 1;
currentTick = now;
return 0;
}
if (activeCount < limit) {
activeCount++;
} else {
currentTick += interval;
activeCount = 1;
}
return currentTick - now;
}
const strictTicks = [];
function strictDelay() {
const now = Date.now();
// Clear the queue if there's a significant delay since the last execution
if (strictTicks.length > 0 && now - strictTicks.at(-1) > interval) {
strictTicks.length = 0;
}
// If the queue is not full, add the current time and execute immediately
if (strictTicks.length < limit) {
strictTicks.push(now);
return 0;
}
// Calculate the next execution time based on the first item in the queue
const nextExecutionTime = strictTicks[0] + interval;
// Shift the queue and add the new execution time
strictTicks.shift();
strictTicks.push(nextExecutionTime);
// Calculate the delay for the current execution
return Math.max(0, nextExecutionTime - now);
}
const getDelay = strict ? strictDelay : windowedDelay;
return function_ => {
const throttled = function (...arguments_) {
if (!throttled.isEnabled) {
return (async () => function_.apply(this, arguments_))();
}
let timeoutId;
return new Promise((resolve, reject) => {
const execute = () => {
resolve(function_.apply(this, arguments_));
queue.delete(timeoutId);
};
const delay = getDelay();
if (delay > 0) {
timeoutId = setTimeout(execute, delay);
queue.set(timeoutId, reject);
onDelay?.(...arguments_);
} else {
execute();
}
});
};
throttled.abort = () => {
for (const timeout of queue.keys()) {
clearTimeout(timeout);
queue.get(timeout)(new AbortError());
}
queue.clear();
strictTicks.splice(0, strictTicks.length);
};
throttled.isEnabled = true;
Object.defineProperty(throttled, 'queueSize', {
get() {
return queue.size;
},
});
return throttled;
};
}

9
node_modules/p-throttle/license generated vendored Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

60
node_modules/p-throttle/package.json generated vendored Normal file
View File

@ -0,0 +1,60 @@
{
"name": "p-throttle",
"version": "6.2.0",
"description": "Throttle promise-returning & async functions",
"license": "MIT",
"repository": "sindresorhus/p-throttle",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
"throttle",
"throat",
"limit",
"limited",
"interval",
"rate",
"batch",
"ratelimit",
"queue",
"discard",
"async",
"await",
"promises",
"time",
"out",
"cancel",
"bluebird"
],
"devDependencies": {
"ava": "^5.3.1",
"delay": "^6.0.0",
"in-range": "^3.0.0",
"time-span": "^5.1.0",
"tsd": "^0.29.0",
"xo": "^0.56.0"
},
"ava": {
"serial": true
}
}

144
node_modules/p-throttle/readme.md generated vendored Normal file
View File

@ -0,0 +1,144 @@
# p-throttle
> Throttle promise-returning & async functions
It also works with normal functions.
It rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.
## Install
```sh
npm install p-throttle
```
## Usage
Here, the throttled function is only called twice a second:
```js
import pThrottle from 'p-throttle';
const now = Date.now();
const throttle = pThrottle({
limit: 2,
interval: 1000
});
const throttled = throttle(async index => {
const secDiff = ((Date.now() - now) / 1000).toFixed();
return `${index}: ${secDiff}s`;
});
for (let index = 1; index <= 6; index++) {
(async () => {
console.log(await throttled(index));
})();
}
//=> 1: 0s
//=> 2: 0s
//=> 3: 1s
//=> 4: 1s
//=> 5: 2s
//=> 6: 2s
```
## API
### pThrottle(options)
Returns a throttle function.
#### options
Type: `object`
Both the `limit` and `interval` options must be specified.
##### limit
Type: `number`
The maximum number of calls within an `interval`.
##### interval
Type: `number`
The timespan for `limit` in milliseconds.
##### strict
Type: `boolean`\
Default: `false`
Use a strict, more resource intensive, throttling algorithm. The default algorithm uses a windowed approach that will work correctly in most cases, limiting the total number of calls at the specified limit per interval window. The strict algorithm throttles each call individually, ensuring the limit is not exceeded for any interval.
##### onDelay
Type: `Function`
Get notified when function calls are delayed due to exceeding the `limit` of allowed calls within the given `interval`. The delayed call arguments are passed to the `onDelay` callback.
Can be useful for monitoring the throttling efficiency.
In the following example, the third call gets delayed and triggers the `onDelay` callback:
```js
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 2,
interval: 1000,
onDelay: (a, b) => {
console.log(`Reached interval limit, call is delayed for ${a} ${b}`);
},
});
const throttled = throttle((a, b) => {
console.log(`Executing with ${a} ${b}...`);
});
await throttled(1, 2);
await throttled(3, 4);
await throttled(5, 6);
//=> Executing with 1 2...
//=> Executing with 3 4...
//=> Reached interval limit, call is delayed for 5 6
//=> Executing with 5 6...
```
### throttle(function_)
Returns a throttled version of `function_`.
#### function_
Type: `Function`
A promise-returning/async function or a normal function.
### throttledFn.abort()
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
### throttledFn.isEnabled
Type: `boolean`\
Default: `true`
Whether future function calls should be throttled and count towards throttling thresholds.
### throttledFn.queueSize
Type: `number`
The number of queued items waiting to be executed.
## Related
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
- [p-memoize](https://github.com/sindresorhus/p-memoize) - Memoize promise-returning & async functions
- [More…](https://github.com/sindresorhus/promise-fun)