@hoajs/response-time 0.1.0 → 0.1.2
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 +11 -2
- package/LICENSE +21 -21
- package/README.md +39 -39
- package/dist/cjs/response-time.cjs +28 -0
- package/dist/esm/response-time.mjs +25 -0
- package/package.json +13 -13
- package/types/index.d.ts +22 -24
- package/dist/cjs/response-time.js +0 -38
- package/dist/esm/response-time.js +0 -15
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,39 +1,39 @@
|
|
|
1
|
-
## @hoajs/response-time
|
|
2
|
-
|
|
3
|
-
Response-Time middleware for Hoa.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
$ npm i @hoajs/response-time --save
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Start
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import { Hoa } from 'hoa'
|
|
15
|
-
import { responseTime } from '@hoajs/response-time'
|
|
16
|
-
|
|
17
|
-
const app = new Hoa()
|
|
18
|
-
app.use(responseTime())
|
|
19
|
-
|
|
20
|
-
app.use(async (ctx) => {
|
|
21
|
-
ctx.res.body = 'Hello, Hoa!'
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
export default app
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Documentation
|
|
28
|
-
|
|
29
|
-
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/response-time.html)
|
|
30
|
-
|
|
31
|
-
## Test (100% coverage)
|
|
32
|
-
|
|
33
|
-
```sh
|
|
34
|
-
$ npm test
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## License
|
|
38
|
-
|
|
39
|
-
MIT
|
|
1
|
+
## @hoajs/response-time
|
|
2
|
+
|
|
3
|
+
Response-Time middleware for Hoa.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
$ npm i @hoajs/response-time --save
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Hoa } from 'hoa'
|
|
15
|
+
import { responseTime } from '@hoajs/response-time'
|
|
16
|
+
|
|
17
|
+
const app = new Hoa()
|
|
18
|
+
app.use(responseTime())
|
|
19
|
+
|
|
20
|
+
app.use(async (ctx) => {
|
|
21
|
+
ctx.res.body = 'Hello, Hoa!'
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
export default app
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Documentation
|
|
28
|
+
|
|
29
|
+
The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/response-time.html)
|
|
30
|
+
|
|
31
|
+
## Test (100% coverage)
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
$ npm test
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
+
|
|
3
|
+
//#region src/response-time.js
|
|
4
|
+
/**
|
|
5
|
+
* Response-Time middleware for Hoa.
|
|
6
|
+
*
|
|
7
|
+
* Adds an `X-Response-Time` (configurable) header with the elapsed time measured via `performance.now()`.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} [options]
|
|
10
|
+
* @param {number} [options.digits=0] number of digits to keep when formatting milliseconds (ms). Default: 0.
|
|
11
|
+
* @param {string} [options.header='X-Response-Time'] header name to set. Default: 'X-Response-Time'.
|
|
12
|
+
* @param {boolean} [options.suffix=true] whether to append the 'ms' suffix to the header value. Default: true.
|
|
13
|
+
* @returns {(ctx: import('hoa').HoaContext, next: () => Promise<void>) => Promise<void>} Hoa middleware function.
|
|
14
|
+
*/
|
|
15
|
+
function responseTime(options = {}) {
|
|
16
|
+
const { digits = 0, suffix = true, header = "X-Response-Time" } = options;
|
|
17
|
+
return async function hoaResponseTime(ctx, next) {
|
|
18
|
+
const start = performance.now();
|
|
19
|
+
await next();
|
|
20
|
+
const deltaMs = performance.now() - start;
|
|
21
|
+
const delta = Number.isFinite(digits) ? deltaMs.toFixed(digits) : String(deltaMs);
|
|
22
|
+
ctx.res.set(header, suffix ? `${delta}ms` : delta);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.default = responseTime;
|
|
28
|
+
exports.responseTime = responseTime;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/response-time.js
|
|
2
|
+
/**
|
|
3
|
+
* Response-Time middleware for Hoa.
|
|
4
|
+
*
|
|
5
|
+
* Adds an `X-Response-Time` (configurable) header with the elapsed time measured via `performance.now()`.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} [options]
|
|
8
|
+
* @param {number} [options.digits=0] number of digits to keep when formatting milliseconds (ms). Default: 0.
|
|
9
|
+
* @param {string} [options.header='X-Response-Time'] header name to set. Default: 'X-Response-Time'.
|
|
10
|
+
* @param {boolean} [options.suffix=true] whether to append the 'ms' suffix to the header value. Default: true.
|
|
11
|
+
* @returns {(ctx: import('hoa').HoaContext, next: () => Promise<void>) => Promise<void>} Hoa middleware function.
|
|
12
|
+
*/
|
|
13
|
+
function responseTime(options = {}) {
|
|
14
|
+
const { digits = 0, suffix = true, header = "X-Response-Time" } = options;
|
|
15
|
+
return async function hoaResponseTime(ctx, next) {
|
|
16
|
+
const start = performance.now();
|
|
17
|
+
await next();
|
|
18
|
+
const deltaMs = performance.now() - start;
|
|
19
|
+
const delta = Number.isFinite(digits) ? deltaMs.toFixed(digits) : String(deltaMs);
|
|
20
|
+
ctx.res.set(header, suffix ? `${delta}ms` : delta);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { responseTime as default, responseTime };
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hoajs/response-time",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Response-Time middleware for Hoa.",
|
|
5
|
-
"main": "./dist/cjs/response-time.
|
|
5
|
+
"main": "./dist/cjs/response-time.cjs",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"module": "./dist/esm/response-time.
|
|
7
|
+
"module": "./dist/esm/response-time.mjs",
|
|
8
8
|
"types": "./types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./types/index.d.ts",
|
|
12
|
-
"import": "./dist/esm/response-time.
|
|
13
|
-
"require": "./dist/cjs/response-time.
|
|
14
|
-
"default": "./dist/esm/response-time.
|
|
12
|
+
"import": "./dist/esm/response-time.mjs",
|
|
13
|
+
"require": "./dist/cjs/response-time.cjs",
|
|
14
|
+
"default": "./dist/esm/response-time.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"
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
"x-response-time"
|
|
41
41
|
],
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@commitlint/cli": "20.1
|
|
44
|
-
"@commitlint/config-conventional": "20.
|
|
45
|
-
"eslint": "9.
|
|
46
|
-
"globals": "
|
|
47
|
-
"hoa": "
|
|
43
|
+
"@commitlint/cli": "20.4.1",
|
|
44
|
+
"@commitlint/config-conventional": "20.4.1",
|
|
45
|
+
"eslint": "9.39.2",
|
|
46
|
+
"globals": "17.3.0",
|
|
47
|
+
"hoa": "*",
|
|
48
48
|
"husky": "9.1.7",
|
|
49
49
|
"jest": "30.2.0",
|
|
50
50
|
"neostandard": "0.12.2",
|
|
51
|
-
"
|
|
51
|
+
"tsdown": "^0.20.3"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"hoa": "*"
|
package/types/index.d.ts
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export interface ResponseTimeOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Number of digits to keep when formatting milliseconds.
|
|
6
|
-
* Default: 0 (integer milliseconds)
|
|
7
|
-
*/
|
|
8
|
-
digits?: number
|
|
9
|
-
/**
|
|
10
|
-
* Header name to set.
|
|
11
|
-
* Default: 'X-Response-Time'
|
|
12
|
-
*/
|
|
13
|
-
header?: string
|
|
14
|
-
/**
|
|
15
|
-
* Whether to append the 'ms' suffix to the header value.
|
|
16
|
-
* Default: true
|
|
17
|
-
*/
|
|
18
|
-
suffix?: boolean
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
export function responseTime(options?: ResponseTimeOptions): ResponseTimeMiddleware
|
|
24
|
-
|
|
1
|
+
import type { HoaMiddleware } from 'hoa'
|
|
2
|
+
|
|
3
|
+
export interface ResponseTimeOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Number of digits to keep when formatting milliseconds.
|
|
6
|
+
* Default: 0 (integer milliseconds)
|
|
7
|
+
*/
|
|
8
|
+
digits?: number
|
|
9
|
+
/**
|
|
10
|
+
* Header name to set.
|
|
11
|
+
* Default: 'X-Response-Time'
|
|
12
|
+
*/
|
|
13
|
+
header?: string
|
|
14
|
+
/**
|
|
15
|
+
* Whether to append the 'ms' suffix to the header value.
|
|
16
|
+
* Default: true
|
|
17
|
+
*/
|
|
18
|
+
suffix?: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function responseTime(options?: ResponseTimeOptions): HoaMiddleware
|
|
22
|
+
|
|
25
23
|
export default responseTime
|
|
@@ -1,38 +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 response_time_exports = {};
|
|
19
|
-
__export(response_time_exports, {
|
|
20
|
-
default: () => response_time_default,
|
|
21
|
-
responseTime: () => responseTime
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(response_time_exports);
|
|
24
|
-
function responseTime(options = {}) {
|
|
25
|
-
const { digits = 0, suffix = true, header = "X-Response-Time" } = options;
|
|
26
|
-
return async function hoaResponseTime(ctx, next) {
|
|
27
|
-
const start = performance.now();
|
|
28
|
-
await next();
|
|
29
|
-
const deltaMs = performance.now() - start;
|
|
30
|
-
const delta = Number.isFinite(digits) ? deltaMs.toFixed(digits) : String(deltaMs);
|
|
31
|
-
ctx.res.set(header, suffix ? `${delta}ms` : delta);
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
var response_time_default = responseTime;
|
|
35
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
-
0 && (module.exports = {
|
|
37
|
-
responseTime
|
|
38
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
function responseTime(options = {}) {
|
|
2
|
-
const { digits = 0, suffix = true, header = "X-Response-Time" } = options;
|
|
3
|
-
return async function hoaResponseTime(ctx, next) {
|
|
4
|
-
const start = performance.now();
|
|
5
|
-
await next();
|
|
6
|
-
const deltaMs = performance.now() - start;
|
|
7
|
-
const delta = Number.isFinite(digits) ? deltaMs.toFixed(digits) : String(deltaMs);
|
|
8
|
-
ctx.res.set(header, suffix ? `${delta}ms` : delta);
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
var response_time_default = responseTime;
|
|
12
|
-
export {
|
|
13
|
-
response_time_default as default,
|
|
14
|
-
responseTime
|
|
15
|
-
};
|