@hoajs/vary 0.1.0

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 ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.1.0 / 2025-10-26
2
+
3
+ - init
package/LICENSE ADDED
@@ -0,0 +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.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ ## @hoajs/vary
2
+
3
+ Vary middleware for Hoa.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ $ npm i @hoajs/vary --save
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```js
14
+ import { Hoa } from 'hoa'
15
+ import { vary } from '@hoajs/vary'
16
+
17
+ const app = new Hoa()
18
+ app.extend(vary())
19
+
20
+ app.use(async (ctx, next) => {
21
+ ctx.res.vary('Accept')
22
+ ctx.res.body = 'Hello, Hoa!'
23
+ })
24
+
25
+ export default app
26
+ ```
27
+
28
+ ## Documentation
29
+
30
+ The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/vary.html)
31
+
32
+ ## Test (100% coverage)
33
+
34
+ ```sh
35
+ $ npm test
36
+ ```
37
+
38
+ ## License
39
+
40
+ MIT
@@ -0,0 +1,90 @@
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 vary_exports = {};
19
+ __export(vary_exports, {
20
+ default: () => vary_default,
21
+ vary: () => vary
22
+ });
23
+ module.exports = __toCommonJS(vary_exports);
24
+ const FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
25
+ function append(header, field) {
26
+ const fields = !Array.isArray(field) ? parse(String(field)) : field;
27
+ for (let j = 0; j < fields.length; j++) {
28
+ if (!FIELD_NAME_REGEXP.test(fields[j])) {
29
+ throw new TypeError("field argument contains an invalid header name");
30
+ }
31
+ }
32
+ if (header === "*") {
33
+ return header;
34
+ }
35
+ let val = header;
36
+ const vals = parse(header.toLowerCase());
37
+ if (fields.indexOf("*") !== -1 || vals.indexOf("*") !== -1) {
38
+ return "*";
39
+ }
40
+ for (let i = 0; i < fields.length; i++) {
41
+ const fld = fields[i].toLowerCase();
42
+ if (vals.indexOf(fld) === -1) {
43
+ vals.push(fld);
44
+ val = val ? val + ", " + fields[i] : fields[i];
45
+ }
46
+ }
47
+ return val;
48
+ }
49
+ function parse(header) {
50
+ let end = 0;
51
+ const list = [];
52
+ let start = 0;
53
+ for (let i = 0, len = header.length; i < len; i++) {
54
+ switch (header.charCodeAt(i)) {
55
+ case 32:
56
+ if (start === end) {
57
+ start = end = i + 1;
58
+ }
59
+ break;
60
+ case 44:
61
+ list.push(header.substring(start, end));
62
+ start = end = i + 1;
63
+ break;
64
+ default:
65
+ end = i + 1;
66
+ break;
67
+ }
68
+ }
69
+ list.push(header.substring(start, end));
70
+ return list;
71
+ }
72
+ function vary() {
73
+ return function varyExtension(app) {
74
+ app.HoaResponse.prototype.vary = function vary2(field) {
75
+ if (!field) {
76
+ throw new TypeError("field argument is required");
77
+ }
78
+ const header = this.get("Vary") || "";
79
+ const val = append(header, field);
80
+ if (val && val !== header) {
81
+ this.set("Vary", val);
82
+ }
83
+ };
84
+ };
85
+ }
86
+ var vary_default = vary;
87
+ // Annotate the CommonJS export names for ESM import in node:
88
+ 0 && (module.exports = {
89
+ vary
90
+ });
@@ -0,0 +1,67 @@
1
+ const FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
2
+ function append(header, field) {
3
+ const fields = !Array.isArray(field) ? parse(String(field)) : field;
4
+ for (let j = 0; j < fields.length; j++) {
5
+ if (!FIELD_NAME_REGEXP.test(fields[j])) {
6
+ throw new TypeError("field argument contains an invalid header name");
7
+ }
8
+ }
9
+ if (header === "*") {
10
+ return header;
11
+ }
12
+ let val = header;
13
+ const vals = parse(header.toLowerCase());
14
+ if (fields.indexOf("*") !== -1 || vals.indexOf("*") !== -1) {
15
+ return "*";
16
+ }
17
+ for (let i = 0; i < fields.length; i++) {
18
+ const fld = fields[i].toLowerCase();
19
+ if (vals.indexOf(fld) === -1) {
20
+ vals.push(fld);
21
+ val = val ? val + ", " + fields[i] : fields[i];
22
+ }
23
+ }
24
+ return val;
25
+ }
26
+ function parse(header) {
27
+ let end = 0;
28
+ const list = [];
29
+ let start = 0;
30
+ for (let i = 0, len = header.length; i < len; i++) {
31
+ switch (header.charCodeAt(i)) {
32
+ case 32:
33
+ if (start === end) {
34
+ start = end = i + 1;
35
+ }
36
+ break;
37
+ case 44:
38
+ list.push(header.substring(start, end));
39
+ start = end = i + 1;
40
+ break;
41
+ default:
42
+ end = i + 1;
43
+ break;
44
+ }
45
+ }
46
+ list.push(header.substring(start, end));
47
+ return list;
48
+ }
49
+ function vary() {
50
+ return function varyExtension(app) {
51
+ app.HoaResponse.prototype.vary = function vary2(field) {
52
+ if (!field) {
53
+ throw new TypeError("field argument is required");
54
+ }
55
+ const header = this.get("Vary") || "";
56
+ const val = append(header, field);
57
+ if (val && val !== header) {
58
+ this.set("Vary", val);
59
+ }
60
+ };
61
+ };
62
+ }
63
+ var vary_default = vary;
64
+ export {
65
+ vary_default as default,
66
+ vary
67
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@hoajs/vary",
3
+ "version": "0.1.0",
4
+ "description": "Vary middleware for Hoa.",
5
+ "main": "./dist/cjs/vary.js",
6
+ "type": "module",
7
+ "module": "./dist/esm/vary.js",
8
+ "types": "./types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./types/index.d.ts",
12
+ "import": "./dist/esm/vary.js",
13
+ "require": "./dist/cjs/vary.js",
14
+ "default": "./dist/esm/vary.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "types",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "scripts": {
23
+ "lint": "eslint .",
24
+ "build": "tsup",
25
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
26
+ "prepublishOnly": "npm run lint && npm run test && npm run build",
27
+ "prepare": "husky"
28
+ },
29
+ "author": "nswbmw",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/hoa-js/vary.git"
34
+ },
35
+ "keywords": [
36
+ "hoa",
37
+ "middleware",
38
+ "vary"
39
+ ],
40
+ "devDependencies": {
41
+ "@commitlint/cli": "20.1.0",
42
+ "@commitlint/config-conventional": "20.0.0",
43
+ "eslint": "9.38.0",
44
+ "globals": "16.4.0",
45
+ "hoa": "*",
46
+ "husky": "9.1.7",
47
+ "jest": "30.2.0",
48
+ "neostandard": "0.12.2",
49
+ "tsup": "8.5.0"
50
+ },
51
+ "peerDependencies": {
52
+ "hoa": "*"
53
+ },
54
+ "engines": {
55
+ "node": ">=20"
56
+ }
57
+ }
@@ -0,0 +1,10 @@
1
+ import type { HoaExtension } from 'hoa'
2
+
3
+ declare module 'hoa' {
4
+ interface HoaResponse {
5
+ vary: (field: string | string[]) => void
6
+ }
7
+ }
8
+
9
+ export declare function vary(): HoaExtension
10
+ export default vary