@common.js/function-timeout 0.1.1

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.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @common.js/function-timeout
2
+
3
+ The [function-timeout](https://www.npmjs.com/package/function-timeout) package exported as CommonJS modules.
4
+
5
+ Exported from [function-timeout@0.1.1](https://www.npmjs.com/package/function-timeout/v/0.1.1) using https://github.com/etienne-martin/common.js.
package/browser.js ADDED
@@ -0,0 +1,60 @@
1
+ // Even though the browser version is a no-op, we wrap it to ensure consistent behavior.
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: all[name]
10
+ });
11
+ }
12
+ _export(exports, {
13
+ default: function() {
14
+ return functionTimeout;
15
+ },
16
+ isTimeoutError: function() {
17
+ return isTimeoutError;
18
+ }
19
+ });
20
+ function _arrayLikeToArray(arr, len) {
21
+ if (len == null || len > arr.length) len = arr.length;
22
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
23
+ return arr2;
24
+ }
25
+ function _arrayWithoutHoles(arr) {
26
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
27
+ }
28
+ function _iterableToArray(iter) {
29
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
30
+ }
31
+ function _nonIterableSpread() {
32
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
33
+ }
34
+ function _toConsumableArray(arr) {
35
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
36
+ }
37
+ function _unsupportedIterableToArray(o, minLen) {
38
+ if (!o) return;
39
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
40
+ var n = Object.prototype.toString.call(o).slice(8, -1);
41
+ if (n === "Object" && o.constructor) n = o.constructor.name;
42
+ if (n === "Map" || n === "Set") return Array.from(n);
43
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
44
+ }
45
+ function functionTimeout(function_) {
46
+ var wrappedFunction = function() {
47
+ for(var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
48
+ arguments_[_key] = arguments[_key];
49
+ }
50
+ return function_.apply(void 0, _toConsumableArray(arguments_));
51
+ };
52
+ Object.defineProperty(wrappedFunction, "name", {
53
+ value: "functionTimeout(".concat(function_.name || "<anonymous>", ")"),
54
+ configurable: true
55
+ });
56
+ return wrappedFunction;
57
+ }
58
+ function isTimeoutError() {
59
+ return false;
60
+ }
package/index.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ export type Options = {
2
+ /**
3
+ The time in milliseconds to wait before timing out.
4
+
5
+ Keep in mind that execution time can vary between different hardware and Node.js versions. Set a generous timeout to avoid flakiness.
6
+ */
7
+ readonly timeout?: number;
8
+ };
9
+
10
+ /**
11
+ Returns a wrapped version of the given function that throws a timeout error if the execution takes longer than the given timeout.
12
+
13
+ @example
14
+ ```
15
+ import functionTimeout, {isTimeoutError} from 'function-timeout';
16
+
17
+ const generateNumbers = count => {
18
+ // Imagine this takes a long time.
19
+ };
20
+
21
+ const generateNumbersWithTimeout = functionTimeout(generateNumbers, {timeout: 100});
22
+
23
+ try {
24
+ console.log(generateNumbersWithTimeout(500));
25
+ } catch (error) {
26
+ if (isTimeoutError(error)) {
27
+ console.error('Timed out');
28
+ } else {
29
+ throw error;
30
+ }
31
+ }
32
+ ```
33
+ */
34
+ export default function functionTimeout<T extends Function>(function_: T, options: Options): T; // eslint-disable-line @typescript-eslint/ban-types
35
+
36
+ /**
37
+ Returns a boolean for whether the given error is a timeout error.
38
+ */
39
+ export function isTimeoutError(error: unknown): boolean;
package/index.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ default: function() {
13
+ return functionTimeout;
14
+ },
15
+ isTimeoutError: function() {
16
+ return isTimeoutError;
17
+ }
18
+ });
19
+ var _nodeVm = /*#__PURE__*/ _interopRequireDefault(require("node:vm"));
20
+ function _arrayLikeToArray(arr, len) {
21
+ if (len == null || len > arr.length) len = arr.length;
22
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
23
+ return arr2;
24
+ }
25
+ function _arrayWithoutHoles(arr) {
26
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
27
+ }
28
+ function _interopRequireDefault(obj) {
29
+ return obj && obj.__esModule ? obj : {
30
+ default: obj
31
+ };
32
+ }
33
+ function _iterableToArray(iter) {
34
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
35
+ }
36
+ function _nonIterableSpread() {
37
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
38
+ }
39
+ function _toConsumableArray(arr) {
40
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
41
+ }
42
+ function _unsupportedIterableToArray(o, minLen) {
43
+ if (!o) return;
44
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
45
+ var n = Object.prototype.toString.call(o).slice(8, -1);
46
+ if (n === "Object" && o.constructor) n = o.constructor.name;
47
+ if (n === "Map" || n === "Set") return Array.from(n);
48
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
49
+ }
50
+ function functionTimeout(function_) {
51
+ var timeout = (arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}).timeout;
52
+ var script = new _nodeVm.default.Script("returnValue = function_()");
53
+ var wrappedFunction = function() {
54
+ for(var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
55
+ arguments_[_key] = arguments[_key];
56
+ }
57
+ var context = {
58
+ function_: function() {
59
+ return function_.apply(void 0, _toConsumableArray(arguments_));
60
+ }
61
+ };
62
+ script.runInNewContext(context, {
63
+ timeout: timeout
64
+ });
65
+ return context.returnValue;
66
+ };
67
+ Object.defineProperty(wrappedFunction, "name", {
68
+ value: "functionTimeout(".concat(function_.name || "<anonymous>", ")"),
69
+ configurable: true
70
+ });
71
+ return wrappedFunction;
72
+ }
73
+ function isTimeoutError(error) {
74
+ return (error === null || error === void 0 ? void 0 : error.code) === "ERR_SCRIPT_EXECUTION_TIMEOUT";
75
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@common.js/function-timeout",
3
+ "version": "0.1.1",
4
+ "description": "Make a synchronous function have a timeout",
5
+ "license": "MIT",
6
+ "repository": "etienne-martin/common.js",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "type": "commonjs",
9
+ "engines": {
10
+ "node": ">=14.16"
11
+ },
12
+ "scripts": {
13
+ "test": "xo && ava && tsd"
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts",
18
+ "browser.js"
19
+ ],
20
+ "devDependencies": {
21
+ "ava": "^4.3.0",
22
+ "in-range": "^3.0.0",
23
+ "time-span": "^5.0.0",
24
+ "tsd": "^0.20.0",
25
+ "xo": "^0.49.0"
26
+ },
27
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
28
+ "dependencies": {},
29
+ "types": "./index.d.ts",
30
+ "main": "./index.js",
31
+ "browser": "./browser.js"
32
+ }
package/readme.md ADDED
@@ -0,0 +1,64 @@
1
+ # function-timeout
2
+
3
+ > Make a synchronous function have a timeout
4
+
5
+ This can be useful if you accept external data and want to ensure processing it does not take too long.
6
+
7
+ The timeout only works in Node.js. When used in a browser, the function will be wrapped, but never time out.
8
+
9
+ *I have a [different package](https://github.com/sindresorhus/super-regex) to prevent [ReDoS](https://en.wikipedia.org/wiki/ReDoS) for regexes.*
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install function-timeout
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```js
20
+ import functionTimeout, {isTimeoutError} from 'function-timeout';
21
+
22
+ const generateNumbers = count => {
23
+ // Imagine this takes a long time.
24
+ };
25
+
26
+ const generateNumbersWithTimeout = functionTimeout(generateNumbers, {timeout: 100});
27
+
28
+ try {
29
+ console.log(generateNumbersWithTimeout(500));
30
+ } catch (error) {
31
+ if (isTimeoutError(error)) {
32
+ console.error('Timed out');
33
+ } else {
34
+ throw error;
35
+ }
36
+ }
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### functionTimeout(function, options?)
42
+
43
+ Returns a wrapped version of the given function that throws a timeout error if the execution takes longer than the given timeout.
44
+
45
+ #### options
46
+
47
+ Type: `object`
48
+
49
+ ##### timeout?
50
+
51
+ Type: `number` *(integer)*
52
+
53
+ The time in milliseconds to wait before timing out.
54
+
55
+ Keep in mind that execution time can vary between different hardware and Node.js versions. Set a generous timeout to avoid flakiness.
56
+
57
+ ### isTimeoutError(error)
58
+
59
+ Returns a boolean for whether the given error is a timeout error.
60
+
61
+ ## Related
62
+
63
+ - [super-regex](https://github.com/sindresorhus/super-regex) - Make a regular expression time out if it takes too long to execute
64
+ - [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a certain amount of time