@hoajs/timeout 0.1.0 → 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/CHANGELOG.md +7 -2
- package/LICENSE +21 -21
- package/README.md +42 -42
- package/dist/cjs/timeout.cjs +36 -0
- package/dist/esm/timeout.mjs +33 -0
- package/package.json +13 -13
- package/types/index.d.ts +5 -7
- package/dist/cjs/timeout.js +0 -50
- package/dist/esm/timeout.js +0 -27
package/CHANGELOG.md
CHANGED
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 - present, Hoa contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 - present, Hoa contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
## @hoajs/timeout
|
|
2
|
-
|
|
3
|
-
Timeout middleware for Hoa.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
$ npm i @hoajs/timeout --save
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Start
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import { Hoa } from 'hoa'
|
|
15
|
-
import { timeout } from '@hoajs/timeout'
|
|
16
|
-
|
|
17
|
-
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))
|
|
18
|
-
|
|
19
|
-
const app = new Hoa()
|
|
20
|
-
app.use(timeout(5000))
|
|
21
|
-
|
|
22
|
-
app.use(async (ctx) => {
|
|
23
|
-
await delay(6000)
|
|
24
|
-
ctx.res.body = 'Hello, Hoa!'
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
export default app
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Documentation
|
|
31
|
-
|
|
32
|
-
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/timeout.html)
|
|
33
|
-
|
|
34
|
-
## Test (100% coverage)
|
|
35
|
-
|
|
36
|
-
```sh
|
|
37
|
-
$ npm test
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## License
|
|
41
|
-
|
|
42
|
-
MIT
|
|
1
|
+
## @hoajs/timeout
|
|
2
|
+
|
|
3
|
+
Timeout middleware for Hoa.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
$ npm i @hoajs/timeout --save
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Hoa } from 'hoa'
|
|
15
|
+
import { timeout } from '@hoajs/timeout'
|
|
16
|
+
|
|
17
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))
|
|
18
|
+
|
|
19
|
+
const app = new Hoa()
|
|
20
|
+
app.use(timeout(5000))
|
|
21
|
+
|
|
22
|
+
app.use(async (ctx) => {
|
|
23
|
+
await delay(6000)
|
|
24
|
+
ctx.res.body = 'Hello, Hoa!'
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
export default app
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Documentation
|
|
31
|
+
|
|
32
|
+
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/timeout.html)
|
|
33
|
+
|
|
34
|
+
## Test (100% coverage)
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
$ npm test
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
+
|
|
3
|
+
//#region src/timeout.js
|
|
4
|
+
/**
|
|
5
|
+
* Timeout middleware for Hoa.
|
|
6
|
+
*
|
|
7
|
+
* Rejects the request with a 504 Gateway Timeout if downstream middleware does not
|
|
8
|
+
* finish within the specified duration.
|
|
9
|
+
*
|
|
10
|
+
* @param {number} duration - Timeout duration in milliseconds.
|
|
11
|
+
* @returns {(ctx: import('hoa').HoaContext, next: () => Promise<void>) => Promise<void>} Hoa middleware.
|
|
12
|
+
*/
|
|
13
|
+
function timeout(duration) {
|
|
14
|
+
if (!Number.isFinite(duration) || duration <= 0) throw new TypeError("timeout(duration) requires a positive number");
|
|
15
|
+
return async function hoaTimeout(ctx, next) {
|
|
16
|
+
let timer;
|
|
17
|
+
const timeoutPromise = new Promise((resolve, reject) => {
|
|
18
|
+
timer = setTimeout(() => {
|
|
19
|
+
try {
|
|
20
|
+
ctx.throw(504, "Gateway Timeout");
|
|
21
|
+
} catch (err) {
|
|
22
|
+
reject(err);
|
|
23
|
+
}
|
|
24
|
+
}, duration);
|
|
25
|
+
});
|
|
26
|
+
try {
|
|
27
|
+
await Promise.race([next(), timeoutPromise]);
|
|
28
|
+
} finally {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
exports.default = timeout;
|
|
36
|
+
exports.timeout = timeout;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/timeout.js
|
|
2
|
+
/**
|
|
3
|
+
* Timeout middleware for Hoa.
|
|
4
|
+
*
|
|
5
|
+
* Rejects the request with a 504 Gateway Timeout if downstream middleware does not
|
|
6
|
+
* finish within the specified duration.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} duration - Timeout duration in milliseconds.
|
|
9
|
+
* @returns {(ctx: import('hoa').HoaContext, next: () => Promise<void>) => Promise<void>} Hoa middleware.
|
|
10
|
+
*/
|
|
11
|
+
function timeout(duration) {
|
|
12
|
+
if (!Number.isFinite(duration) || duration <= 0) throw new TypeError("timeout(duration) requires a positive number");
|
|
13
|
+
return async function hoaTimeout(ctx, next) {
|
|
14
|
+
let timer;
|
|
15
|
+
const timeoutPromise = new Promise((resolve, reject) => {
|
|
16
|
+
timer = setTimeout(() => {
|
|
17
|
+
try {
|
|
18
|
+
ctx.throw(504, "Gateway Timeout");
|
|
19
|
+
} catch (err) {
|
|
20
|
+
reject(err);
|
|
21
|
+
}
|
|
22
|
+
}, duration);
|
|
23
|
+
});
|
|
24
|
+
try {
|
|
25
|
+
await Promise.race([next(), timeoutPromise]);
|
|
26
|
+
} finally {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { timeout as default, timeout };
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hoajs/timeout",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Timeout middleware for Hoa.",
|
|
5
|
-
"main": "./dist/cjs/timeout.
|
|
5
|
+
"main": "./dist/cjs/timeout.cjs",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"module": "./dist/esm/timeout.
|
|
7
|
+
"module": "./dist/esm/timeout.mjs",
|
|
8
8
|
"types": "./types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./types/index.d.ts",
|
|
12
|
-
"import": "./dist/esm/timeout.
|
|
13
|
-
"require": "./dist/cjs/timeout.
|
|
14
|
-
"default": "./dist/esm/timeout.
|
|
12
|
+
"import": "./dist/esm/timeout.mjs",
|
|
13
|
+
"require": "./dist/cjs/timeout.cjs",
|
|
14
|
+
"default": "./dist/esm/timeout.mjs"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
23
|
"lint": "eslint .",
|
|
24
|
-
"build": "
|
|
24
|
+
"build": "tsdown",
|
|
25
25
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
26
26
|
"prepublishOnly": "npm run lint && npm run test && npm run build",
|
|
27
27
|
"prepare": "husky"
|
|
@@ -38,15 +38,15 @@
|
|
|
38
38
|
"timeout"
|
|
39
39
|
],
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@commitlint/cli": "20.1
|
|
42
|
-
"@commitlint/config-conventional": "20.
|
|
43
|
-
"eslint": "9.
|
|
44
|
-
"globals": "
|
|
45
|
-
"hoa": "
|
|
41
|
+
"@commitlint/cli": "20.4.1",
|
|
42
|
+
"@commitlint/config-conventional": "20.4.1",
|
|
43
|
+
"eslint": "9.39.2",
|
|
44
|
+
"globals": "17.3.0",
|
|
45
|
+
"hoa": "*",
|
|
46
46
|
"husky": "9.1.7",
|
|
47
47
|
"jest": "30.2.0",
|
|
48
48
|
"neostandard": "0.12.2",
|
|
49
|
-
"
|
|
49
|
+
"tsdown": "^0.20.3"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"hoa": "*"
|
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
export default timeout
|
|
1
|
+
import type { HoaMiddleware } from 'hoa'
|
|
2
|
+
|
|
3
|
+
export function timeout(duration: number): HoaMiddleware
|
|
4
|
+
|
|
5
|
+
export default timeout
|
package/dist/cjs/timeout.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var timeout_exports = {};
|
|
19
|
-
__export(timeout_exports, {
|
|
20
|
-
default: () => timeout_default,
|
|
21
|
-
timeout: () => timeout
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(timeout_exports);
|
|
24
|
-
function timeout(duration) {
|
|
25
|
-
if (!Number.isFinite(duration) || duration <= 0) {
|
|
26
|
-
throw new TypeError("timeout(duration) requires a positive number");
|
|
27
|
-
}
|
|
28
|
-
return async function hoaTimeout(ctx, next) {
|
|
29
|
-
let timer;
|
|
30
|
-
const timeoutPromise = new Promise((resolve, reject) => {
|
|
31
|
-
timer = setTimeout(() => {
|
|
32
|
-
try {
|
|
33
|
-
ctx.throw(504, "Gateway Timeout");
|
|
34
|
-
} catch (err) {
|
|
35
|
-
reject(err);
|
|
36
|
-
}
|
|
37
|
-
}, duration);
|
|
38
|
-
});
|
|
39
|
-
try {
|
|
40
|
-
await Promise.race([next(), timeoutPromise]);
|
|
41
|
-
} finally {
|
|
42
|
-
clearTimeout(timer);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
var timeout_default = timeout;
|
|
47
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
-
0 && (module.exports = {
|
|
49
|
-
timeout
|
|
50
|
-
});
|
package/dist/esm/timeout.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
function timeout(duration) {
|
|
2
|
-
if (!Number.isFinite(duration) || duration <= 0) {
|
|
3
|
-
throw new TypeError("timeout(duration) requires a positive number");
|
|
4
|
-
}
|
|
5
|
-
return async function hoaTimeout(ctx, next) {
|
|
6
|
-
let timer;
|
|
7
|
-
const timeoutPromise = new Promise((resolve, reject) => {
|
|
8
|
-
timer = setTimeout(() => {
|
|
9
|
-
try {
|
|
10
|
-
ctx.throw(504, "Gateway Timeout");
|
|
11
|
-
} catch (err) {
|
|
12
|
-
reject(err);
|
|
13
|
-
}
|
|
14
|
-
}, duration);
|
|
15
|
-
});
|
|
16
|
-
try {
|
|
17
|
-
await Promise.race([next(), timeoutPromise]);
|
|
18
|
-
} finally {
|
|
19
|
-
clearTimeout(timer);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
var timeout_default = timeout;
|
|
24
|
-
export {
|
|
25
|
-
timeout_default as default,
|
|
26
|
-
timeout
|
|
27
|
-
};
|