feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+15
View File
@@ -0,0 +1,15 @@
// Even though the browser version is a no-op, we wrap it to ensure consistent behavior.
export default function functionTimeout(function_) {
const wrappedFunction = (...arguments_) => function_(...arguments_);
Object.defineProperty(wrappedFunction, 'name', {
value: `functionTimeout(${function_.name || '<anonymous>'})`,
configurable: true,
});
return wrappedFunction;
}
export function isTimeoutError() {
return false;
}
+39
View File
@@ -0,0 +1,39 @@
export type Options = {
/**
The time in milliseconds to wait before timing out.
Keep in mind that execution time can vary between different hardware and Node.js versions. Set a generous timeout to avoid flakiness.
*/
readonly timeout?: number;
};
/**
Returns a wrapped version of the given function that throws a timeout error if the execution takes longer than the given timeout.
@example
```
import functionTimeout, {isTimeoutError} from 'function-timeout';
const generateNumbers = count => {
// Imagine this takes a long time.
};
const generateNumbersWithTimeout = functionTimeout(generateNumbers, {timeout: 100});
try {
console.log(generateNumbersWithTimeout(500));
} catch (error) {
if (isTimeoutError(error)) {
console.error('Timed out');
} else {
throw error;
}
}
```
*/
export default function functionTimeout<T extends Function>(function_: T, options: Options): T; // eslint-disable-line @typescript-eslint/ban-types
/**
Returns a boolean for whether the given error is a timeout error.
*/
export function isTimeoutError(error: unknown): boolean;
+26
View File
@@ -0,0 +1,26 @@
import vm from 'node:vm';
export default function functionTimeout(function_, {timeout} = {}) {
const script = new vm.Script('returnValue = function_()');
const wrappedFunction = (...arguments_) => {
const context = {
function_: () => function_(...arguments_),
};
script.runInNewContext(context, {timeout});
return context.returnValue;
};
Object.defineProperty(wrappedFunction, 'name', {
value: `functionTimeout(${function_.name || '<anonymous>'})`,
configurable: true,
});
return wrappedFunction;
}
export function isTimeoutError(error) {
return error?.code === 'ERR_SCRIPT_EXECUTION_TIMEOUT';
}
+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.
+55
View File
@@ -0,0 +1,55 @@
{
"name": "function-timeout",
"version": "0.1.1",
"description": "Make a synchronous function have a timeout",
"license": "MIT",
"repository": "sindresorhus/function-timeout",
"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",
"node": "./index.js",
"default": "./browser.js"
},
"engines": {
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
],
"keywords": [
"function",
"synchronous",
"timeout",
"time",
"out",
"cancel",
"expire",
"abort",
"regex",
"regexp",
"redos",
"security",
"func",
"fn",
"script",
"execute"
],
"devDependencies": {
"ava": "^4.3.0",
"in-range": "^3.0.0",
"time-span": "^5.0.0",
"tsd": "^0.20.0",
"xo": "^0.49.0"
}
}
+64
View File
@@ -0,0 +1,64 @@
# function-timeout
> Make a synchronous function have a timeout
This can be useful if you accept external data and want to ensure processing it does not take too long.
The timeout only works in Node.js. When used in a browser, the function will be wrapped, but never time out.
*I have a [different package](https://github.com/sindresorhus/super-regex) to prevent [ReDoS](https://en.wikipedia.org/wiki/ReDoS) for regexes.*
## Install
```sh
npm install function-timeout
```
## Usage
```js
import functionTimeout, {isTimeoutError} from 'function-timeout';
const generateNumbers = count => {
// Imagine this takes a long time.
};
const generateNumbersWithTimeout = functionTimeout(generateNumbers, {timeout: 100});
try {
console.log(generateNumbersWithTimeout(500));
} catch (error) {
if (isTimeoutError(error)) {
console.error('Timed out');
} else {
throw error;
}
}
```
## API
### functionTimeout(function, options?)
Returns a wrapped version of the given function that throws a timeout error if the execution takes longer than the given timeout.
#### options
Type: `object`
##### timeout?
Type: `number` *(integer)*
The time in milliseconds to wait before timing out.
Keep in mind that execution time can vary between different hardware and Node.js versions. Set a generous timeout to avoid flakiness.
### isTimeoutError(error)
Returns a boolean for whether the given error is a timeout error.
## Related
- [super-regex](https://github.com/sindresorhus/super-regex) - Make a regular expression time out if it takes too long to execute
- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a certain amount of time