added all files to project

This commit is contained in:
2022-03-10 10:36:59 +01:00
parent 09dd957b33
commit 46a936d7de
25351 changed files with 3883356 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
declare class AbortErrorClass extends Error {
readonly name: 'AbortError';
/**
Abort pending execution. All unresolved promised are rejected with a `AbortError` error.
*/
constructor();
}
type PromiseResolve<ValueType> = ValueType extends PromiseLike<infer ValueType> ? Promise<ValueType> : Promise<ValueType>;
declare namespace pThrottle {
type ThrottledFunction<Argument extends readonly unknown[], ReturnValue> = ((
...arguments: Argument
) => PromiseResolve<ReturnValue>) & {
/**
Whether future function calls should be throttled or count towards throttling thresholds.
@default true
*/
isEnabled: boolean;
/**
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
abort(): void;
};
interface Options {
/**
Maximum number of calls within an `interval`.
*/
readonly limit: number;
/**
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;
}
type AbortError = AbortErrorClass;
}
declare const pThrottle: {
AbortError: typeof AbortErrorClass;
/**
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions.
@returns A throttled version of `fn`.
Both the `limit` and `interval` options must be specified.
@example
```
import pThrottle from 'p-throttle';
const throttle = pThrottle({
limit: 2,
interval: 1000
});
const throttled = throttle(async index => {
return index * 2;
});
for (let i = 1; i <= 6; i++) {
throttled(i).then(console.log);
}
```
*/
(
options: pThrottle.Options
): <Argument extends readonly unknown[], ReturnValue>(function_: (...arguments: Argument) => ReturnValue) => pThrottle.ThrottledFunction<Argument, ReturnValue>;
};
export = pThrottle;
+102
View File
@@ -0,0 +1,102 @@
'use strict';
class AbortError extends Error {
constructor() {
super('Throttled function aborted');
this.name = 'AbortError';
}
}
const pThrottle = ({limit, interval, strict}) => {
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();
if (strictTicks.length < limit) {
strictTicks.push(now);
return 0;
}
const earliestTime = strictTicks.shift() + interval;
if (now >= earliestTime) {
strictTicks.push(now);
return 0;
}
strictTicks.push(earliestTime);
return earliestTime - now;
}
const getDelay = strict ? strictDelay : windowedDelay;
return function_ => {
const throttled = function (...args) {
if (!throttled.isEnabled) {
return (async () => function_.apply(this, args))();
}
let timeout;
return new Promise((resolve, reject) => {
const execute = () => {
resolve(function_.apply(this, args));
queue.delete(timeout);
};
timeout = setTimeout(execute, getDelay());
queue.set(timeout, reject);
});
};
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;
return throttled;
};
};
module.exports = pThrottle;
module.exports.AbortError = AbortError;
+9
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.
+83
View File
@@ -0,0 +1,83 @@
{
"_from": "p-throttle@^4.1.1",
"_id": "p-throttle@4.1.1",
"_inBundle": false,
"_integrity": "sha512-TuU8Ato+pRTPJoDzYD4s7ocJYcNSEZRvlxoq3hcPI2kZDZ49IQ1Wkj7/gDJc3X7XiEAAvRGtDzdXJI0tC3IL1g==",
"_location": "/p-throttle",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "p-throttle@^4.1.1",
"name": "p-throttle",
"escapedName": "p-throttle",
"rawSpec": "^4.1.1",
"saveSpec": null,
"fetchSpec": "^4.1.1"
},
"_requiredBy": [
"/hafas-client"
],
"_resolved": "https://registry.npmjs.org/p-throttle/-/p-throttle-4.1.1.tgz",
"_shasum": "80b1fbd358af40a8bfa1667f9dc8b72b714ad692",
"_spec": "p-throttle@^4.1.1",
"_where": "/home/pi/MagicMirror/modules/MMM-PublicTransportHafas/node_modules/hafas-client",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/p-throttle/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Throttle promise-returning & async functions",
"devDependencies": {
"ava": "^2.4.0",
"delay": "^5.0.0",
"in-range": "^2.0.0",
"time-span": "^4.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.1"
},
"engines": {
"node": ">=10"
},
"files": [
"index.js",
"index.d.ts"
],
"funding": "https://github.com/sponsors/sindresorhus",
"homepage": "https://github.com/sindresorhus/p-throttle#readme",
"keywords": [
"promise",
"throttle",
"throat",
"limit",
"limited",
"interval",
"rate",
"batch",
"ratelimit",
"queue",
"discard",
"async",
"await",
"promises",
"time",
"out",
"cancel",
"bluebird"
],
"license": "MIT",
"name": "p-throttle",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/p-throttle.git"
},
"scripts": {
"test": "xo && ava && tsd"
},
"version": "4.1.1"
}
+102
View File
@@ -0,0 +1,102 @@
# p-throttle
> [Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions
It also works with normal functions.
Useful for rate limiting calls to an external API, for example.
## Install
```
$ npm install p-throttle
```
## Usage
Here, the throttled function is only called twice a second:
```js
const pThrottle = require('p-throttle');
const now = Date.now();
const throttle = pThrottle({
limit: 2,
interval: 1000
});
const throttled = throttle(index => {
const secDiff = ((Date.now() - now) / 1000).toFixed();
return Promise.resolve(`${index}: ${secDiff}s`);
});
for (let i = 1; i <= 6; i++) {
throttled(i).then(console.log);
}
//=> 1: 0s
//=> 2: 0s
//=> 3: 1s
//=> 4: 1s
//=> 5: 2s
//=> 6: 2s
```
## API
### pThrottle(options)
Returns a `throttle` function.
Returns a throttled version of `fn`.
#### options
Type: `object`
Both the `limit` and `interval` options must be specified.
##### limit
Type: `number`
Maximum number of calls within an `interval`.
##### interval
Type: `number`
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.
### throttle(function_)
#### function_
Type: `Function`
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.
## 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)