@vafast/cors 0.0.4 → 0.0.6
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 +1 -1
- package/dist/index.d.mts +116 -0
- package/dist/index.mjs +108 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +11 -11
- package/dist/index.d.ts +0 -114
- package/dist/index.js +0 -181
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as vafast0 from "vafast";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type Origin = string | RegExp | ((request: Request) => boolean | void);
|
|
5
|
+
type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE';
|
|
6
|
+
type MaybeArray<T> = T | T[];
|
|
7
|
+
interface CORSConfig {
|
|
8
|
+
/**
|
|
9
|
+
* @default `true`
|
|
10
|
+
*
|
|
11
|
+
* Assign the **Access-Control-Allow-Origin** header.
|
|
12
|
+
*
|
|
13
|
+
* Value can be one of the following:
|
|
14
|
+
* - `string` - String of origin which will be directly assign to `Access-Control-Allow-Origin`
|
|
15
|
+
*
|
|
16
|
+
* - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)
|
|
17
|
+
*
|
|
18
|
+
* - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.
|
|
19
|
+
*
|
|
20
|
+
* - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.
|
|
21
|
+
* - Function will accepts `Request` as parameter
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // ? Example usage
|
|
25
|
+
* const corsMiddleware = cors({
|
|
26
|
+
* origin: (request) => true
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* // Type Definition
|
|
30
|
+
* type CORSOriginFn = (request: Request) => boolean | void
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept request if one is `true`.
|
|
34
|
+
*/
|
|
35
|
+
origin?: Origin | boolean | Origin[];
|
|
36
|
+
/**
|
|
37
|
+
* @default `*`
|
|
38
|
+
*
|
|
39
|
+
* Assign **Access-Control-Allow-Methods** header.
|
|
40
|
+
*
|
|
41
|
+
* Value can be one of the following:
|
|
42
|
+
* Accept:
|
|
43
|
+
* - `undefined | null | ''` - Ignore all methods.
|
|
44
|
+
*
|
|
45
|
+
* - `*` - Accept all methods.
|
|
46
|
+
*
|
|
47
|
+
* - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.
|
|
48
|
+
* - Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')
|
|
49
|
+
*
|
|
50
|
+
* - `HTTPMethod[]` - Allow multiple HTTP methods.
|
|
51
|
+
* - eg: ['GET', 'PUT', 'POST']
|
|
52
|
+
*/
|
|
53
|
+
methods?: boolean | undefined | null | '' | '*' | MaybeArray<HTTPMethod | (string & {})>;
|
|
54
|
+
/**
|
|
55
|
+
* @default `*`
|
|
56
|
+
*
|
|
57
|
+
* Assign **Access-Control-Allow-Headers** header.
|
|
58
|
+
*
|
|
59
|
+
* Allow incoming request with the specified headers.
|
|
60
|
+
*
|
|
61
|
+
* Value can be one of the following:
|
|
62
|
+
* - `string`
|
|
63
|
+
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
|
|
64
|
+
*
|
|
65
|
+
* - `string[]` - Allow multiple HTTP methods.
|
|
66
|
+
* - eg: ['Content-Type', 'Authorization']
|
|
67
|
+
*/
|
|
68
|
+
allowedHeaders?: true | string | string[];
|
|
69
|
+
/**
|
|
70
|
+
* @default `*`
|
|
71
|
+
*
|
|
72
|
+
* Assign **Access-Control-Expose-Headers** header.
|
|
73
|
+
*
|
|
74
|
+
* Return the specified headers to request in CORS mode.
|
|
75
|
+
*
|
|
76
|
+
* Value can be one of the following:
|
|
77
|
+
* - `string`
|
|
78
|
+
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
|
|
79
|
+
*
|
|
80
|
+
* - `string[]` - Allow multiple HTTP methods.
|
|
81
|
+
* - eg: ['Content-Type', 'X-Powered-By']
|
|
82
|
+
*/
|
|
83
|
+
exposeHeaders?: true | string | string[];
|
|
84
|
+
/**
|
|
85
|
+
* @default `true`
|
|
86
|
+
*
|
|
87
|
+
* Assign **Access-Control-Allow-Credentials** header.
|
|
88
|
+
*
|
|
89
|
+
* Allow incoming requests to send `credentials` header.
|
|
90
|
+
*
|
|
91
|
+
* - `boolean` - Available if set to `true`.
|
|
92
|
+
*/
|
|
93
|
+
credentials?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* @default `5`
|
|
96
|
+
*
|
|
97
|
+
* Assign **Access-Control-Max-Age** header.
|
|
98
|
+
*
|
|
99
|
+
* Allow incoming requests to send `credentials` header.
|
|
100
|
+
*
|
|
101
|
+
* - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.
|
|
102
|
+
*/
|
|
103
|
+
maxAge?: number;
|
|
104
|
+
/**
|
|
105
|
+
* @default `true`
|
|
106
|
+
*
|
|
107
|
+
* Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.
|
|
108
|
+
*
|
|
109
|
+
* - `boolean` - Available if set to `true`.
|
|
110
|
+
*/
|
|
111
|
+
preflight?: boolean;
|
|
112
|
+
}
|
|
113
|
+
declare const cors: (config?: CORSConfig) => vafast0.TypedMiddleware<object>;
|
|
114
|
+
//#endregion
|
|
115
|
+
export { HTTPMethod, cors, cors as default };
|
|
116
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { defineMiddleware, empty } from "vafast";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
new Headers()?.toJSON;
|
|
5
|
+
/**
|
|
6
|
+
* This function is use when headers config is true.
|
|
7
|
+
* Attempts to process headers based on request headers.
|
|
8
|
+
*/
|
|
9
|
+
const processHeaders = (headers) => {
|
|
10
|
+
if ("toJSON" in headers && typeof headers.toJSON === "function") return Object.keys(headers.toJSON()).join(", ");
|
|
11
|
+
let keys = "";
|
|
12
|
+
let i = 0;
|
|
13
|
+
headers.forEach((_, key) => {
|
|
14
|
+
if (i) keys = keys + ", " + key;
|
|
15
|
+
else keys = key;
|
|
16
|
+
i++;
|
|
17
|
+
});
|
|
18
|
+
return keys;
|
|
19
|
+
};
|
|
20
|
+
const cors = (config) => {
|
|
21
|
+
let { origin = true, methods = true, allowedHeaders = true, exposeHeaders = true, credentials = true, maxAge = 5, preflight = true } = config ?? {};
|
|
22
|
+
if (Array.isArray(allowedHeaders)) allowedHeaders = allowedHeaders.join(", ");
|
|
23
|
+
if (Array.isArray(exposeHeaders)) exposeHeaders = exposeHeaders.join(", ");
|
|
24
|
+
const origins = typeof origin === "boolean" ? void 0 : Array.isArray(origin) ? origin : [origin];
|
|
25
|
+
const anyOrigin = origins?.some((o) => o === "*");
|
|
26
|
+
const originMap = {};
|
|
27
|
+
if (origins) {
|
|
28
|
+
for (const origin$1 of origins) if (typeof origin$1 === "string") originMap[origin$1] = true;
|
|
29
|
+
}
|
|
30
|
+
const processOrigin = (origin$1, request, from) => {
|
|
31
|
+
if (Array.isArray(origin$1)) return origin$1.some((o) => processOrigin(o, request, from));
|
|
32
|
+
switch (typeof origin$1) {
|
|
33
|
+
case "string":
|
|
34
|
+
if (from in originMap) return true;
|
|
35
|
+
const fromProtocol = from.indexOf("://");
|
|
36
|
+
if (fromProtocol !== -1) from = from.slice(fromProtocol + 3);
|
|
37
|
+
return origin$1 === from;
|
|
38
|
+
case "function": return origin$1(request) === true;
|
|
39
|
+
case "object": if (origin$1 instanceof RegExp) return origin$1.test(from);
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
43
|
+
const handleOrigin = (response, request) => {
|
|
44
|
+
if (origin === true) {
|
|
45
|
+
response.headers.set("vary", "*");
|
|
46
|
+
response.headers.set("access-control-allow-origin", request.headers.get("Origin") || "*");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (anyOrigin) {
|
|
50
|
+
response.headers.set("vary", "*");
|
|
51
|
+
response.headers.set("access-control-allow-origin", "*");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!origins?.length) return;
|
|
55
|
+
const headers = [];
|
|
56
|
+
if (origins.length) {
|
|
57
|
+
const from = request.headers.get("Origin") ?? "";
|
|
58
|
+
for (let i = 0; i < origins.length; i++) if (processOrigin(origins[i], request, from) === true) {
|
|
59
|
+
response.headers.set("vary", origin ? "Origin" : "*");
|
|
60
|
+
response.headers.set("access-control-allow-origin", from || "*");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
response.headers.set("vary", "Origin");
|
|
65
|
+
if (headers.length) response.headers.set("access-control-allow-origin", headers.join(", "));
|
|
66
|
+
};
|
|
67
|
+
const handleMethod = (response, method) => {
|
|
68
|
+
if (!method) return;
|
|
69
|
+
if (methods === true) return response.headers.set("access-control-allow-methods", method ?? "*");
|
|
70
|
+
if (methods === false || !methods?.length) return;
|
|
71
|
+
if (methods === "*") return response.headers.set("access-control-allow-methods", "*");
|
|
72
|
+
if (!Array.isArray(methods)) return response.headers.set("access-control-allow-methods", methods);
|
|
73
|
+
response.headers.set("access-control-allow-methods", methods.join(", "));
|
|
74
|
+
};
|
|
75
|
+
const setDefaultHeaders = (response) => {
|
|
76
|
+
if (typeof exposeHeaders === "string") response.headers.set("access-control-expose-headers", exposeHeaders);
|
|
77
|
+
if (typeof allowedHeaders === "string") response.headers.set("access-control-allow-headers", allowedHeaders);
|
|
78
|
+
if (credentials === true) response.headers.set("access-control-allow-credentials", "true");
|
|
79
|
+
};
|
|
80
|
+
return defineMiddleware(async (request, next) => {
|
|
81
|
+
if (preflight && request.method === "OPTIONS") {
|
|
82
|
+
const response$1 = empty(204);
|
|
83
|
+
handleOrigin(response$1, request);
|
|
84
|
+
handleMethod(response$1, request.headers.get("access-control-request-method"));
|
|
85
|
+
if (allowedHeaders === true || exposeHeaders === true) {
|
|
86
|
+
if (allowedHeaders === true) response$1.headers.set("access-control-allow-headers", request.headers.get("access-control-request-headers") || "");
|
|
87
|
+
if (exposeHeaders === true) response$1.headers.set("access-control-expose-headers", processHeaders(request.headers));
|
|
88
|
+
}
|
|
89
|
+
if (maxAge) response$1.headers.set("access-control-max-age", maxAge.toString());
|
|
90
|
+
return response$1;
|
|
91
|
+
}
|
|
92
|
+
const response = await next();
|
|
93
|
+
handleOrigin(response, request);
|
|
94
|
+
handleMethod(response, request.method);
|
|
95
|
+
setDefaultHeaders(response);
|
|
96
|
+
if (allowedHeaders === true || exposeHeaders === true) {
|
|
97
|
+
const headers = processHeaders(request.headers);
|
|
98
|
+
if (allowedHeaders === true) response.headers.set("access-control-allow-headers", headers);
|
|
99
|
+
if (exposeHeaders === true) response.headers.set("access-control-expose-headers", headers);
|
|
100
|
+
}
|
|
101
|
+
return response;
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
var src_default = cors;
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
export { cors, src_default as default };
|
|
108
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["origin","response"],"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable no-case-declarations */\nimport { defineMiddleware } from 'vafast'\nimport { empty } from 'vafast'\n\ntype Origin = string | RegExp | ((request: Request) => boolean | void)\n\nexport type HTTPMethod =\n\t| 'ACL'\n\t| 'BIND'\n\t| 'CHECKOUT'\n\t| 'CONNECT'\n\t| 'COPY'\n\t| 'DELETE'\n\t| 'GET'\n\t| 'HEAD'\n\t| 'LINK'\n\t| 'LOCK'\n\t| 'M-SEARCH'\n\t| 'MERGE'\n\t| 'MKACTIVITY'\n\t| 'MKCALENDAR'\n\t| 'MKCOL'\n\t| 'MOVE'\n\t| 'NOTIFY'\n\t| 'OPTIONS'\n\t| 'PATCH'\n\t| 'POST'\n\t| 'PROPFIND'\n\t| 'PROPPATCH'\n\t| 'PURGE'\n\t| 'PUT'\n\t| 'REBIND'\n\t| 'REPORT'\n\t| 'SEARCH'\n\t| 'SOURCE'\n\t| 'SUBSCRIBE'\n\t| 'TRACE'\n\t| 'UNBIND'\n\t| 'UNLINK'\n\t| 'UNLOCK'\n\t| 'UNSUBSCRIBE'\n\ntype MaybeArray<T> = T | T[]\n\ninterface CORSConfig {\n\t/**\n\t * @default `true`\n\t *\n\t * Assign the **Access-Control-Allow-Origin** header.\n\t *\n\t * Value can be one of the following:\n\t * - `string` - String of origin which will be directly assign to `Access-Control-Allow-Origin`\n\t *\n\t * - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)\n\t *\n\t * - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.\n\t *\n\t * - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.\n\t * - Function will accepts `Request` as parameter\n\t *\n\t * ```typescript\n\t * // ? Example usage\n\t * const corsMiddleware = cors({\n\t * origin: (request) => true\n\t * })\n\t *\n\t * // Type Definition\n\t * type CORSOriginFn = (request: Request) => boolean | void\n\t * ```\n\t *\n\t * - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept request if one is `true`.\n\t */\n\torigin?: Origin | boolean | Origin[]\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Allow-Methods** header.\n\t *\n\t * Value can be one of the following:\n\t * Accept:\n\t * - `undefined | null | ''` - Ignore all methods.\n\t *\n\t * - `*` - Accept all methods.\n\t *\n\t * - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.\n\t * - Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')\n\t *\n\t * - `HTTPMethod[]` - Allow multiple HTTP methods.\n\t * - eg: ['GET', 'PUT', 'POST']\n\t */\n\tmethods?:\n\t\t| boolean\n\t\t| undefined\n\t\t| null\n\t\t| ''\n\t\t| '*'\n\t\t| MaybeArray<HTTPMethod | (string & {})>\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Allow-Headers** header.\n\t *\n\t * Allow incoming request with the specified headers.\n\t *\n\t * Value can be one of the following:\n\t * - `string`\n\t * - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').\n\t *\n\t * - `string[]` - Allow multiple HTTP methods.\n\t * - eg: ['Content-Type', 'Authorization']\n\t */\n\tallowedHeaders?: true | string | string[]\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Expose-Headers** header.\n\t *\n\t * Return the specified headers to request in CORS mode.\n\t *\n\t * Value can be one of the following:\n\t * - `string`\n\t * - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').\n\t *\n\t * - `string[]` - Allow multiple HTTP methods.\n\t * - eg: ['Content-Type', 'X-Powered-By']\n\t */\n\texposeHeaders?: true | string | string[]\n\t/**\n\t * @default `true`\n\t *\n\t * Assign **Access-Control-Allow-Credentials** header.\n\t *\n\t * Allow incoming requests to send `credentials` header.\n\t *\n\t * - `boolean` - Available if set to `true`.\n\t */\n\tcredentials?: boolean\n\t/**\n\t * @default `5`\n\t *\n\t * Assign **Access-Control-Max-Age** header.\n\t *\n\t * Allow incoming requests to send `credentials` header.\n\t *\n\t * - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.\n\t */\n\tmaxAge?: number\n\t/**\n\t * @default `true`\n\t *\n\t * Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.\n\t *\n\t * - `boolean` - Available if set to `true`.\n\t */\n\tpreflight?: boolean\n}\n\n// @ts-ignore\nconst isBun = typeof new Headers()?.toJSON === 'function'\n\n/**\n * This function is use when headers config is true.\n * Attempts to process headers based on request headers.\n */\nconst processHeaders = (headers: any) => {\n\t// Check if toJSON method exists (Bun specific)\n\tif ('toJSON' in headers && typeof headers.toJSON === 'function') {\n\t\treturn Object.keys(headers.toJSON()).join(', ')\n\t}\n\n\tlet keys = ''\n\n\tlet i = 0\n\theaders.forEach((_: any, key: string) => {\n\t\tif (i) keys = keys + ', ' + key\n\t\telse keys = key\n\n\t\ti++\n\t})\n\n\treturn keys\n}\n\nexport const cors = (config?: CORSConfig) => {\n\tlet {\n\t\torigin = true,\n\t\tmethods = true,\n\t\tallowedHeaders = true,\n\t\texposeHeaders = true,\n\t\tcredentials = true,\n\t\tmaxAge = 5,\n\t\tpreflight = true\n\t} = config ?? {}\n\n\tif (Array.isArray(allowedHeaders))\n\t\tallowedHeaders = allowedHeaders.join(', ')\n\n\tif (Array.isArray(exposeHeaders)) exposeHeaders = exposeHeaders.join(', ')\n\n\tconst origins =\n\t\ttypeof origin === 'boolean'\n\t\t\t? undefined\n\t\t\t: Array.isArray(origin)\n\t\t\t? origin\n\t\t\t: [origin]\n\n\tconst anyOrigin = origins?.some((o) => o === '*')\n\n\tconst originMap = <Record<string, true>>{}\n\tif (origins)\n\t\tfor (const origin of origins)\n\t\t\tif (typeof origin === 'string') originMap[origin] = true\n\n\tconst processOrigin = (\n\t\torigin: Origin,\n\t\trequest: Request,\n\t\tfrom: string\n\t): boolean => {\n\t\tif (Array.isArray(origin))\n\t\t\treturn origin.some((o) => processOrigin(o, request, from))\n\n\t\tswitch (typeof origin) {\n\t\t\tcase 'string':\n\t\t\t\tif (from in originMap) return true\n\n\t\t\t\tconst fromProtocol = from.indexOf('://')\n\t\t\t\tif (fromProtocol !== -1) from = from.slice(fromProtocol + 3)\n\n\t\t\t\treturn origin === from\n\n\t\t\tcase 'function':\n\t\t\t\treturn origin(request) === true\n\n\t\t\tcase 'object':\n\t\t\t\tif (origin instanceof RegExp) return origin.test(from)\n\t\t}\n\n\t\treturn false\n\t}\n\n\tconst handleOrigin = (response: Response, request: Request) => {\n\t\t// origin === `true` means any origin\n\t\tif (origin === true) {\n\t\t\tresponse.headers.set('vary', '*')\n\t\t\tresponse.headers.set(\n\t\t\t\t'access-control-allow-origin',\n\t\t\t\trequest.headers.get('Origin') || '*'\n\t\t\t)\n\n\t\t\treturn\n\t\t}\n\n\t\tif (anyOrigin) {\n\t\t\tresponse.headers.set('vary', '*')\n\t\t\tresponse.headers.set('access-control-allow-origin', '*')\n\n\t\t\treturn\n\t\t}\n\n\t\tif (!origins?.length) return\n\n\t\tconst headers: string[] = []\n\n\t\tif (origins.length) {\n\t\t\tconst from = request.headers.get('Origin') ?? ''\n\t\t\tfor (let i = 0; i < origins.length; i++) {\n\t\t\t\tconst value = processOrigin(origins[i]!, request, from)\n\t\t\t\tif (value === true) {\n\t\t\t\t\tresponse.headers.set('vary', origin ? 'Origin' : '*')\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-allow-origin',\n\t\t\t\t\t\tfrom || '*'\n\t\t\t\t\t)\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tresponse.headers.set('vary', 'Origin')\n\t\tif (headers.length)\n\t\t\tresponse.headers.set(\n\t\t\t\t'access-control-allow-origin',\n\t\t\t\theaders.join(', ')\n\t\t\t)\n\t}\n\n\tconst handleMethod = (response: Response, method?: string | null) => {\n\t\tif (!method) return\n\n\t\tif (methods === true)\n\t\t\treturn response.headers.set(\n\t\t\t\t'access-control-allow-methods',\n\t\t\t\tmethod ?? '*'\n\t\t\t)\n\n\t\tif (methods === false || !methods?.length) return\n\n\t\tif (methods === '*')\n\t\t\treturn response.headers.set('access-control-allow-methods', '*')\n\n\t\tif (!Array.isArray(methods))\n\t\t\treturn response.headers.set('access-control-allow-methods', methods)\n\n\t\tresponse.headers.set('access-control-allow-methods', methods.join(', '))\n\t}\n\n\tconst setDefaultHeaders = (response: Response) => {\n\t\tif (typeof exposeHeaders === 'string')\n\t\t\tresponse.headers.set('access-control-expose-headers', exposeHeaders)\n\n\t\tif (typeof allowedHeaders === 'string')\n\t\t\tresponse.headers.set('access-control-allow-headers', allowedHeaders)\n\n\t\tif (credentials === true)\n\t\t\tresponse.headers.set('access-control-allow-credentials', 'true')\n\t}\n\n\tconst handlePreflight = async (request: Request): Promise<Response> => {\n\t\tconst response = empty(204)\n\n\t\thandleOrigin(response, request)\n\t\thandleMethod(\n\t\t\tresponse,\n\t\t\trequest.headers.get('access-control-request-method')\n\t\t)\n\n\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\tif (allowedHeaders === true)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-allow-headers',\n\t\t\t\t\trequest.headers.get('access-control-request-headers') || ''\n\t\t\t\t)\n\n\t\t\tif (exposeHeaders === true)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-expose-headers',\n\t\t\t\t\tprocessHeaders(request.headers)\n\t\t\t\t)\n\t\t}\n\n\t\tif (maxAge)\n\t\t\tresponse.headers.set('access-control-max-age', maxAge.toString())\n\n\t\treturn response\n\t}\n\n\treturn defineMiddleware(async (request, next) => {\n\t\t// Handle preflight requests\n\t\tif (preflight && request.method === 'OPTIONS') {\n\t\t\tconst response = empty(204)\n\n\t\t\thandleOrigin(response, request)\n\t\t\thandleMethod(\n\t\t\t\tresponse,\n\t\t\t\trequest.headers.get('access-control-request-method')\n\t\t\t)\n\n\t\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\t\tif (allowedHeaders === true)\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-allow-headers',\n\t\t\t\t\t\trequest.headers.get('access-control-request-headers') ||\n\t\t\t\t\t\t\t''\n\t\t\t\t\t)\n\n\t\t\t\tif (exposeHeaders === true)\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-expose-headers',\n\t\t\t\t\t\tprocessHeaders(request.headers)\n\t\t\t\t\t)\n\t\t\t}\n\n\t\t\tif (maxAge)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-max-age',\n\t\t\t\t\tmaxAge.toString()\n\t\t\t\t)\n\n\t\t\treturn response\n\t\t}\n\n\t\t// Process CORS for actual requests\n\t\tconst response = await next()\n\n\t\thandleOrigin(response, request)\n\t\thandleMethod(response, request.method)\n\t\tsetDefaultHeaders(response)\n\n\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\tconst headers = processHeaders(request.headers)\n\n\t\t\tif (allowedHeaders === true)\n\t\t\t\tresponse.headers.set('access-control-allow-headers', headers)\n\n\t\t\tif (exposeHeaders === true)\n\t\t\t\tresponse.headers.set('access-control-expose-headers', headers)\n\t\t}\n\n\t\treturn response\n\t})\n}\n\nexport default cors\n"],"mappings":";;;AA8JqB,IAAI,SAAS,EAAE;;;;;AAMpC,MAAM,kBAAkB,YAAiB;AAExC,KAAI,YAAY,WAAW,OAAO,QAAQ,WAAW,WACpD,QAAO,OAAO,KAAK,QAAQ,QAAQ,CAAC,CAAC,KAAK,KAAK;CAGhD,IAAI,OAAO;CAEX,IAAI,IAAI;AACR,SAAQ,SAAS,GAAQ,QAAgB;AACxC,MAAI,EAAG,QAAO,OAAO,OAAO;MACvB,QAAO;AAEZ;GACC;AAEF,QAAO;;AAGR,MAAa,QAAQ,WAAwB;CAC5C,IAAI,EACH,SAAS,MACT,UAAU,MACV,iBAAiB,MACjB,gBAAgB,MAChB,cAAc,MACd,SAAS,GACT,YAAY,SACT,UAAU,EAAE;AAEhB,KAAI,MAAM,QAAQ,eAAe,CAChC,kBAAiB,eAAe,KAAK,KAAK;AAE3C,KAAI,MAAM,QAAQ,cAAc,CAAE,iBA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H,YAAS,QAAQ,IAChB,0BACA,OAAO,UAAU,CACjB;AAEF,UAAOA;;EAIR,MAAM,WAAW,MAAM,MAAM;AAE7B,eAAa,UAAU,QAAQ;AAC/B,eAAa,UAAU,QAAQ,OAAO;AACtC,oBAAkB,SAAS;AAE3B,MAAI,mBAAmB,QAAQ,kBAAkB,MAAM;GACtD,MAAM,UAAU,eAAe,QAAQ,QAAQ;AAE/C,OAAI,mBAAmB,KACtB,UAAS,QAAQ,IAAI,gCAAgC,QAAQ;AAE9D,OAAI,kBAAkB,KACrB,UAAS,QAAQ,IAAI,iCAAiC,QAAQ;;AAGhE,SAAO;GACN;;AAGH,kBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vafast/cors",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "CORS middleware plugin for Vafast framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"type": "git",
|
|
13
13
|
"url": "https://github.com/gaurishhs/@vafast/cors"
|
|
14
14
|
},
|
|
15
|
-
"main": "./dist/index.
|
|
16
|
-
"module": "./dist/index.
|
|
17
|
-
"types": "./dist/index.d.
|
|
15
|
+
"main": "./dist/index.mjs",
|
|
16
|
+
"module": "./dist/index.mjs",
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
|
-
"types": "./dist/index.d.
|
|
21
|
-
"import": "./dist/index.
|
|
22
|
-
"default": "./dist/index.
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"default": "./dist/index.mjs"
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"bugs": "https://github.com/gaurishhs/@vafast/cors/issues",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"cors"
|
|
31
31
|
],
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build": "
|
|
33
|
+
"build": "tsdown",
|
|
34
34
|
"dev": "npx tsx watch example/index.ts",
|
|
35
35
|
"test": "npx vitest run",
|
|
36
36
|
"release": "npm run build && npm run test && npx bumpp && npm publish --access=public"
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
"@types/debug": "^4.1.12",
|
|
46
46
|
"@types/node": "^24.3.0",
|
|
47
47
|
"rimraf": "^6.0.1",
|
|
48
|
-
"
|
|
48
|
+
"tsdown": "^0.19.0-beta.4",
|
|
49
49
|
"typescript": "^5.4.5",
|
|
50
|
-
"vafast": "
|
|
50
|
+
"vafast": "^0.5.1",
|
|
51
51
|
"vitest": "^4.0.16"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"vafast": "
|
|
54
|
+
"vafast": "^0.5.1"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { Middleware } from 'vafast';
|
|
2
|
-
|
|
3
|
-
type Origin = string | RegExp | ((request: Request) => boolean | void);
|
|
4
|
-
type HTTPMethod = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' | 'M-SEARCH' | 'MERGE' | 'MKACTIVITY' | 'MKCALENDAR' | 'MKCOL' | 'MOVE' | 'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE';
|
|
5
|
-
type MaybeArray<T> = T | T[];
|
|
6
|
-
interface CORSConfig {
|
|
7
|
-
/**
|
|
8
|
-
* @default `true`
|
|
9
|
-
*
|
|
10
|
-
* Assign the **Access-Control-Allow-Origin** header.
|
|
11
|
-
*
|
|
12
|
-
* Value can be one of the following:
|
|
13
|
-
* - `string` - String of origin which will be directly assign to `Access-Control-Allow-Origin`
|
|
14
|
-
*
|
|
15
|
-
* - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)
|
|
16
|
-
*
|
|
17
|
-
* - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.
|
|
18
|
-
*
|
|
19
|
-
* - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.
|
|
20
|
-
* - Function will accepts `Request` as parameter
|
|
21
|
-
*
|
|
22
|
-
* ```typescript
|
|
23
|
-
* // ? Example usage
|
|
24
|
-
* const corsMiddleware = cors({
|
|
25
|
-
* origin: (request) => true
|
|
26
|
-
* })
|
|
27
|
-
*
|
|
28
|
-
* // Type Definition
|
|
29
|
-
* type CORSOriginFn = (request: Request) => boolean | void
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept request if one is `true`.
|
|
33
|
-
*/
|
|
34
|
-
origin?: Origin | boolean | Origin[];
|
|
35
|
-
/**
|
|
36
|
-
* @default `*`
|
|
37
|
-
*
|
|
38
|
-
* Assign **Access-Control-Allow-Methods** header.
|
|
39
|
-
*
|
|
40
|
-
* Value can be one of the following:
|
|
41
|
-
* Accept:
|
|
42
|
-
* - `undefined | null | ''` - Ignore all methods.
|
|
43
|
-
*
|
|
44
|
-
* - `*` - Accept all methods.
|
|
45
|
-
*
|
|
46
|
-
* - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.
|
|
47
|
-
* - Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')
|
|
48
|
-
*
|
|
49
|
-
* - `HTTPMethod[]` - Allow multiple HTTP methods.
|
|
50
|
-
* - eg: ['GET', 'PUT', 'POST']
|
|
51
|
-
*/
|
|
52
|
-
methods?: boolean | undefined | null | '' | '*' | MaybeArray<HTTPMethod | (string & {})>;
|
|
53
|
-
/**
|
|
54
|
-
* @default `*`
|
|
55
|
-
*
|
|
56
|
-
* Assign **Access-Control-Allow-Headers** header.
|
|
57
|
-
*
|
|
58
|
-
* Allow incoming request with the specified headers.
|
|
59
|
-
*
|
|
60
|
-
* Value can be one of the following:
|
|
61
|
-
* - `string`
|
|
62
|
-
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
|
|
63
|
-
*
|
|
64
|
-
* - `string[]` - Allow multiple HTTP methods.
|
|
65
|
-
* - eg: ['Content-Type', 'Authorization']
|
|
66
|
-
*/
|
|
67
|
-
allowedHeaders?: true | string | string[];
|
|
68
|
-
/**
|
|
69
|
-
* @default `*`
|
|
70
|
-
*
|
|
71
|
-
* Assign **Access-Control-Expose-Headers** header.
|
|
72
|
-
*
|
|
73
|
-
* Return the specified headers to request in CORS mode.
|
|
74
|
-
*
|
|
75
|
-
* Value can be one of the following:
|
|
76
|
-
* - `string`
|
|
77
|
-
* - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
|
|
78
|
-
*
|
|
79
|
-
* - `string[]` - Allow multiple HTTP methods.
|
|
80
|
-
* - eg: ['Content-Type', 'X-Powered-By']
|
|
81
|
-
*/
|
|
82
|
-
exposeHeaders?: true | string | string[];
|
|
83
|
-
/**
|
|
84
|
-
* @default `true`
|
|
85
|
-
*
|
|
86
|
-
* Assign **Access-Control-Allow-Credentials** header.
|
|
87
|
-
*
|
|
88
|
-
* Allow incoming requests to send `credentials` header.
|
|
89
|
-
*
|
|
90
|
-
* - `boolean` - Available if set to `true`.
|
|
91
|
-
*/
|
|
92
|
-
credentials?: boolean;
|
|
93
|
-
/**
|
|
94
|
-
* @default `5`
|
|
95
|
-
*
|
|
96
|
-
* Assign **Access-Control-Max-Age** header.
|
|
97
|
-
*
|
|
98
|
-
* Allow incoming requests to send `credentials` header.
|
|
99
|
-
*
|
|
100
|
-
* - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.
|
|
101
|
-
*/
|
|
102
|
-
maxAge?: number;
|
|
103
|
-
/**
|
|
104
|
-
* @default `true`
|
|
105
|
-
*
|
|
106
|
-
* Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.
|
|
107
|
-
*
|
|
108
|
-
* - `boolean` - Available if set to `true`.
|
|
109
|
-
*/
|
|
110
|
-
preflight?: boolean;
|
|
111
|
-
}
|
|
112
|
-
declare const cors: (config?: CORSConfig) => Middleware;
|
|
113
|
-
|
|
114
|
-
export { type HTTPMethod, cors, cors as default };
|
package/dist/index.js
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { empty } from "vafast";
|
|
3
|
-
var isBun = typeof new Headers()?.toJSON === "function";
|
|
4
|
-
var processHeaders = (headers) => {
|
|
5
|
-
if ("toJSON" in headers && typeof headers.toJSON === "function") {
|
|
6
|
-
return Object.keys(headers.toJSON()).join(", ");
|
|
7
|
-
}
|
|
8
|
-
let keys = "";
|
|
9
|
-
let i = 0;
|
|
10
|
-
headers.forEach((_, key) => {
|
|
11
|
-
if (i) keys = keys + ", " + key;
|
|
12
|
-
else keys = key;
|
|
13
|
-
i++;
|
|
14
|
-
});
|
|
15
|
-
return keys;
|
|
16
|
-
};
|
|
17
|
-
var cors = (config) => {
|
|
18
|
-
let {
|
|
19
|
-
origin = true,
|
|
20
|
-
methods = true,
|
|
21
|
-
allowedHeaders = true,
|
|
22
|
-
exposeHeaders = true,
|
|
23
|
-
credentials = true,
|
|
24
|
-
maxAge = 5,
|
|
25
|
-
preflight = true
|
|
26
|
-
} = config ?? {};
|
|
27
|
-
if (Array.isArray(allowedHeaders))
|
|
28
|
-
allowedHeaders = allowedHeaders.join(", ");
|
|
29
|
-
if (Array.isArray(exposeHeaders)) exposeHeaders = exposeHeaders.join(", ");
|
|
30
|
-
const origins = typeof origin === "boolean" ? void 0 : Array.isArray(origin) ? origin : [origin];
|
|
31
|
-
const anyOrigin = origins?.some((o) => o === "*");
|
|
32
|
-
const originMap = {};
|
|
33
|
-
if (origins) {
|
|
34
|
-
for (const origin2 of origins)
|
|
35
|
-
if (typeof origin2 === "string") originMap[origin2] = true;
|
|
36
|
-
}
|
|
37
|
-
const processOrigin = (origin2, request, from) => {
|
|
38
|
-
if (Array.isArray(origin2))
|
|
39
|
-
return origin2.some((o) => processOrigin(o, request, from));
|
|
40
|
-
switch (typeof origin2) {
|
|
41
|
-
case "string":
|
|
42
|
-
if (from in originMap) return true;
|
|
43
|
-
const fromProtocol = from.indexOf("://");
|
|
44
|
-
if (fromProtocol !== -1) from = from.slice(fromProtocol + 3);
|
|
45
|
-
return origin2 === from;
|
|
46
|
-
case "function":
|
|
47
|
-
return origin2(request) === true;
|
|
48
|
-
case "object":
|
|
49
|
-
if (origin2 instanceof RegExp) return origin2.test(from);
|
|
50
|
-
}
|
|
51
|
-
return false;
|
|
52
|
-
};
|
|
53
|
-
const handleOrigin = (response, request) => {
|
|
54
|
-
if (origin === true) {
|
|
55
|
-
response.headers.set("vary", "*");
|
|
56
|
-
response.headers.set(
|
|
57
|
-
"access-control-allow-origin",
|
|
58
|
-
request.headers.get("Origin") || "*"
|
|
59
|
-
);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (anyOrigin) {
|
|
63
|
-
response.headers.set("vary", "*");
|
|
64
|
-
response.headers.set("access-control-allow-origin", "*");
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (!origins?.length) return;
|
|
68
|
-
const headers = [];
|
|
69
|
-
if (origins.length) {
|
|
70
|
-
const from = request.headers.get("Origin") ?? "";
|
|
71
|
-
for (let i = 0; i < origins.length; i++) {
|
|
72
|
-
const value = processOrigin(origins[i], request, from);
|
|
73
|
-
if (value === true) {
|
|
74
|
-
response.headers.set("vary", origin ? "Origin" : "*");
|
|
75
|
-
response.headers.set(
|
|
76
|
-
"access-control-allow-origin",
|
|
77
|
-
from || "*"
|
|
78
|
-
);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
response.headers.set("vary", "Origin");
|
|
84
|
-
if (headers.length)
|
|
85
|
-
response.headers.set(
|
|
86
|
-
"access-control-allow-origin",
|
|
87
|
-
headers.join(", ")
|
|
88
|
-
);
|
|
89
|
-
};
|
|
90
|
-
const handleMethod = (response, method) => {
|
|
91
|
-
if (!method) return;
|
|
92
|
-
if (methods === true)
|
|
93
|
-
return response.headers.set(
|
|
94
|
-
"access-control-allow-methods",
|
|
95
|
-
method ?? "*"
|
|
96
|
-
);
|
|
97
|
-
if (methods === false || !methods?.length) return;
|
|
98
|
-
if (methods === "*")
|
|
99
|
-
return response.headers.set("access-control-allow-methods", "*");
|
|
100
|
-
if (!Array.isArray(methods))
|
|
101
|
-
return response.headers.set("access-control-allow-methods", methods);
|
|
102
|
-
response.headers.set("access-control-allow-methods", methods.join(", "));
|
|
103
|
-
};
|
|
104
|
-
const setDefaultHeaders = (response) => {
|
|
105
|
-
if (typeof exposeHeaders === "string")
|
|
106
|
-
response.headers.set("access-control-expose-headers", exposeHeaders);
|
|
107
|
-
if (typeof allowedHeaders === "string")
|
|
108
|
-
response.headers.set("access-control-allow-headers", allowedHeaders);
|
|
109
|
-
if (credentials === true)
|
|
110
|
-
response.headers.set("access-control-allow-credentials", "true");
|
|
111
|
-
};
|
|
112
|
-
const handlePreflight = async (request) => {
|
|
113
|
-
const response = empty(204);
|
|
114
|
-
handleOrigin(response, request);
|
|
115
|
-
handleMethod(
|
|
116
|
-
response,
|
|
117
|
-
request.headers.get("access-control-request-method")
|
|
118
|
-
);
|
|
119
|
-
if (allowedHeaders === true || exposeHeaders === true) {
|
|
120
|
-
if (allowedHeaders === true)
|
|
121
|
-
response.headers.set(
|
|
122
|
-
"access-control-allow-headers",
|
|
123
|
-
request.headers.get("access-control-request-headers") || ""
|
|
124
|
-
);
|
|
125
|
-
if (exposeHeaders === true)
|
|
126
|
-
response.headers.set(
|
|
127
|
-
"access-control-expose-headers",
|
|
128
|
-
processHeaders(request.headers)
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
if (maxAge)
|
|
132
|
-
response.headers.set("access-control-max-age", maxAge.toString());
|
|
133
|
-
return response;
|
|
134
|
-
};
|
|
135
|
-
return async (request, next) => {
|
|
136
|
-
if (preflight && request.method === "OPTIONS") {
|
|
137
|
-
const response2 = empty(204);
|
|
138
|
-
handleOrigin(response2, request);
|
|
139
|
-
handleMethod(
|
|
140
|
-
response2,
|
|
141
|
-
request.headers.get("access-control-request-method")
|
|
142
|
-
);
|
|
143
|
-
if (allowedHeaders === true || exposeHeaders === true) {
|
|
144
|
-
if (allowedHeaders === true)
|
|
145
|
-
response2.headers.set(
|
|
146
|
-
"access-control-allow-headers",
|
|
147
|
-
request.headers.get("access-control-request-headers") || ""
|
|
148
|
-
);
|
|
149
|
-
if (exposeHeaders === true)
|
|
150
|
-
response2.headers.set(
|
|
151
|
-
"access-control-expose-headers",
|
|
152
|
-
processHeaders(request.headers)
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
if (maxAge)
|
|
156
|
-
response2.headers.set(
|
|
157
|
-
"access-control-max-age",
|
|
158
|
-
maxAge.toString()
|
|
159
|
-
);
|
|
160
|
-
return response2;
|
|
161
|
-
}
|
|
162
|
-
const response = await next();
|
|
163
|
-
handleOrigin(response, request);
|
|
164
|
-
handleMethod(response, request.method);
|
|
165
|
-
setDefaultHeaders(response);
|
|
166
|
-
if (allowedHeaders === true || exposeHeaders === true) {
|
|
167
|
-
const headers = processHeaders(request.headers);
|
|
168
|
-
if (allowedHeaders === true)
|
|
169
|
-
response.headers.set("access-control-allow-headers", headers);
|
|
170
|
-
if (exposeHeaders === true)
|
|
171
|
-
response.headers.set("access-control-expose-headers", headers);
|
|
172
|
-
}
|
|
173
|
-
return response;
|
|
174
|
-
};
|
|
175
|
-
};
|
|
176
|
-
var index_default = cors;
|
|
177
|
-
export {
|
|
178
|
-
cors,
|
|
179
|
-
index_default as default
|
|
180
|
-
};
|
|
181
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable no-case-declarations */\nimport type { Middleware } from 'vafast'\nimport { empty } from 'vafast'\n\ntype Origin = string | RegExp | ((request: Request) => boolean | void)\n\nexport type HTTPMethod =\n\t| 'ACL'\n\t| 'BIND'\n\t| 'CHECKOUT'\n\t| 'CONNECT'\n\t| 'COPY'\n\t| 'DELETE'\n\t| 'GET'\n\t| 'HEAD'\n\t| 'LINK'\n\t| 'LOCK'\n\t| 'M-SEARCH'\n\t| 'MERGE'\n\t| 'MKACTIVITY'\n\t| 'MKCALENDAR'\n\t| 'MKCOL'\n\t| 'MOVE'\n\t| 'NOTIFY'\n\t| 'OPTIONS'\n\t| 'PATCH'\n\t| 'POST'\n\t| 'PROPFIND'\n\t| 'PROPPATCH'\n\t| 'PURGE'\n\t| 'PUT'\n\t| 'REBIND'\n\t| 'REPORT'\n\t| 'SEARCH'\n\t| 'SOURCE'\n\t| 'SUBSCRIBE'\n\t| 'TRACE'\n\t| 'UNBIND'\n\t| 'UNLINK'\n\t| 'UNLOCK'\n\t| 'UNSUBSCRIBE'\n\ntype MaybeArray<T> = T | T[]\n\ninterface CORSConfig {\n\t/**\n\t * @default `true`\n\t *\n\t * Assign the **Access-Control-Allow-Origin** header.\n\t *\n\t * Value can be one of the following:\n\t * - `string` - String of origin which will be directly assign to `Access-Control-Allow-Origin`\n\t *\n\t * - `boolean` - If set to true, `Access-Control-Allow-Origin` will be set to `*` (accept all origin)\n\t *\n\t * - `RegExp` - Pattern to use to test with request's url, will accept origin if matched.\n\t *\n\t * - `Function` - Custom logic to validate origin acceptance or not. will accept origin if `true` is returned.\n\t * - Function will accepts `Request` as parameter\n\t *\n\t * ```typescript\n\t * // ? Example usage\n\t * const corsMiddleware = cors({\n\t * origin: (request) => true\n\t * })\n\t *\n\t * // Type Definition\n\t * type CORSOriginFn = (request: Request) => boolean | void\n\t * ```\n\t *\n\t * - `Array<string | RegExp | Function>` - Will try to find truthy value of all options above. Will accept request if one is `true`.\n\t */\n\torigin?: Origin | boolean | Origin[]\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Allow-Methods** header.\n\t *\n\t * Value can be one of the following:\n\t * Accept:\n\t * - `undefined | null | ''` - Ignore all methods.\n\t *\n\t * - `*` - Accept all methods.\n\t *\n\t * - `HTTPMethod` - Will be directly set to **Access-Control-Allow-Methods**.\n\t * - Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')\n\t *\n\t * - `HTTPMethod[]` - Allow multiple HTTP methods.\n\t * - eg: ['GET', 'PUT', 'POST']\n\t */\n\tmethods?:\n\t\t| boolean\n\t\t| undefined\n\t\t| null\n\t\t| ''\n\t\t| '*'\n\t\t| MaybeArray<HTTPMethod | (string & {})>\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Allow-Headers** header.\n\t *\n\t * Allow incoming request with the specified headers.\n\t *\n\t * Value can be one of the following:\n\t * - `string`\n\t * - Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').\n\t *\n\t * - `string[]` - Allow multiple HTTP methods.\n\t * - eg: ['Content-Type', 'Authorization']\n\t */\n\tallowedHeaders?: true | string | string[]\n\t/**\n\t * @default `*`\n\t *\n\t * Assign **Access-Control-Expose-Headers** header.\n\t *\n\t * Return the specified headers to request in CORS mode.\n\t *\n\t * Value can be one of the following:\n\t * - `string`\n\t * - Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').\n\t *\n\t * - `string[]` - Allow multiple HTTP methods.\n\t * - eg: ['Content-Type', 'X-Powered-By']\n\t */\n\texposeHeaders?: true | string | string[]\n\t/**\n\t * @default `true`\n\t *\n\t * Assign **Access-Control-Allow-Credentials** header.\n\t *\n\t * Allow incoming requests to send `credentials` header.\n\t *\n\t * - `boolean` - Available if set to `true`.\n\t */\n\tcredentials?: boolean\n\t/**\n\t * @default `5`\n\t *\n\t * Assign **Access-Control-Max-Age** header.\n\t *\n\t * Allow incoming requests to send `credentials` header.\n\t *\n\t * - `number` - Duration in seconds to indicates how long the results of a preflight request can be cached.\n\t */\n\tmaxAge?: number\n\t/**\n\t * @default `true`\n\t *\n\t * Add `[OPTIONS] /*` handler to handle preflight request which response with `HTTP 204` and CORS hints.\n\t *\n\t * - `boolean` - Available if set to `true`.\n\t */\n\tpreflight?: boolean\n}\n\n// @ts-ignore\nconst isBun = typeof new Headers()?.toJSON === 'function'\n\n/**\n * This function is use when headers config is true.\n * Attempts to process headers based on request headers.\n */\nconst processHeaders = (headers: any) => {\n\t// Check if toJSON method exists (Bun specific)\n\tif ('toJSON' in headers && typeof headers.toJSON === 'function') {\n\t\treturn Object.keys(headers.toJSON()).join(', ')\n\t}\n\n\tlet keys = ''\n\n\tlet i = 0\n\theaders.forEach((_: any, key: string) => {\n\t\tif (i) keys = keys + ', ' + key\n\t\telse keys = key\n\n\t\ti++\n\t})\n\n\treturn keys\n}\n\nexport const cors = (config?: CORSConfig): Middleware => {\n\tlet {\n\t\torigin = true,\n\t\tmethods = true,\n\t\tallowedHeaders = true,\n\t\texposeHeaders = true,\n\t\tcredentials = true,\n\t\tmaxAge = 5,\n\t\tpreflight = true\n\t} = config ?? {}\n\n\tif (Array.isArray(allowedHeaders))\n\t\tallowedHeaders = allowedHeaders.join(', ')\n\n\tif (Array.isArray(exposeHeaders)) exposeHeaders = exposeHeaders.join(', ')\n\n\tconst origins =\n\t\ttypeof origin === 'boolean'\n\t\t\t? undefined\n\t\t\t: Array.isArray(origin)\n\t\t\t? origin\n\t\t\t: [origin]\n\n\tconst anyOrigin = origins?.some((o) => o === '*')\n\n\tconst originMap = <Record<string, true>>{}\n\tif (origins)\n\t\tfor (const origin of origins)\n\t\t\tif (typeof origin === 'string') originMap[origin] = true\n\n\tconst processOrigin = (\n\t\torigin: Origin,\n\t\trequest: Request,\n\t\tfrom: string\n\t): boolean => {\n\t\tif (Array.isArray(origin))\n\t\t\treturn origin.some((o) => processOrigin(o, request, from))\n\n\t\tswitch (typeof origin) {\n\t\t\tcase 'string':\n\t\t\t\tif (from in originMap) return true\n\n\t\t\t\tconst fromProtocol = from.indexOf('://')\n\t\t\t\tif (fromProtocol !== -1) from = from.slice(fromProtocol + 3)\n\n\t\t\t\treturn origin === from\n\n\t\t\tcase 'function':\n\t\t\t\treturn origin(request) === true\n\n\t\t\tcase 'object':\n\t\t\t\tif (origin instanceof RegExp) return origin.test(from)\n\t\t}\n\n\t\treturn false\n\t}\n\n\tconst handleOrigin = (response: Response, request: Request) => {\n\t\t// origin === `true` means any origin\n\t\tif (origin === true) {\n\t\t\tresponse.headers.set('vary', '*')\n\t\t\tresponse.headers.set(\n\t\t\t\t'access-control-allow-origin',\n\t\t\t\trequest.headers.get('Origin') || '*'\n\t\t\t)\n\n\t\t\treturn\n\t\t}\n\n\t\tif (anyOrigin) {\n\t\t\tresponse.headers.set('vary', '*')\n\t\t\tresponse.headers.set('access-control-allow-origin', '*')\n\n\t\t\treturn\n\t\t}\n\n\t\tif (!origins?.length) return\n\n\t\tconst headers: string[] = []\n\n\t\tif (origins.length) {\n\t\t\tconst from = request.headers.get('Origin') ?? ''\n\t\t\tfor (let i = 0; i < origins.length; i++) {\n\t\t\t\tconst value = processOrigin(origins[i]!, request, from)\n\t\t\t\tif (value === true) {\n\t\t\t\t\tresponse.headers.set('vary', origin ? 'Origin' : '*')\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-allow-origin',\n\t\t\t\t\t\tfrom || '*'\n\t\t\t\t\t)\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tresponse.headers.set('vary', 'Origin')\n\t\tif (headers.length)\n\t\t\tresponse.headers.set(\n\t\t\t\t'access-control-allow-origin',\n\t\t\t\theaders.join(', ')\n\t\t\t)\n\t}\n\n\tconst handleMethod = (response: Response, method?: string | null) => {\n\t\tif (!method) return\n\n\t\tif (methods === true)\n\t\t\treturn response.headers.set(\n\t\t\t\t'access-control-allow-methods',\n\t\t\t\tmethod ?? '*'\n\t\t\t)\n\n\t\tif (methods === false || !methods?.length) return\n\n\t\tif (methods === '*')\n\t\t\treturn response.headers.set('access-control-allow-methods', '*')\n\n\t\tif (!Array.isArray(methods))\n\t\t\treturn response.headers.set('access-control-allow-methods', methods)\n\n\t\tresponse.headers.set('access-control-allow-methods', methods.join(', '))\n\t}\n\n\tconst setDefaultHeaders = (response: Response) => {\n\t\tif (typeof exposeHeaders === 'string')\n\t\t\tresponse.headers.set('access-control-expose-headers', exposeHeaders)\n\n\t\tif (typeof allowedHeaders === 'string')\n\t\t\tresponse.headers.set('access-control-allow-headers', allowedHeaders)\n\n\t\tif (credentials === true)\n\t\t\tresponse.headers.set('access-control-allow-credentials', 'true')\n\t}\n\n\tconst handlePreflight = async (request: Request): Promise<Response> => {\n\t\tconst response = empty(204)\n\n\t\thandleOrigin(response, request)\n\t\thandleMethod(\n\t\t\tresponse,\n\t\t\trequest.headers.get('access-control-request-method')\n\t\t)\n\n\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\tif (allowedHeaders === true)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-allow-headers',\n\t\t\t\t\trequest.headers.get('access-control-request-headers') || ''\n\t\t\t\t)\n\n\t\t\tif (exposeHeaders === true)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-expose-headers',\n\t\t\t\t\tprocessHeaders(request.headers)\n\t\t\t\t)\n\t\t}\n\n\t\tif (maxAge)\n\t\t\tresponse.headers.set('access-control-max-age', maxAge.toString())\n\n\t\treturn response\n\t}\n\n\treturn async (request: Request, next: () => Promise<Response>) => {\n\t\t// Handle preflight requests\n\t\tif (preflight && request.method === 'OPTIONS') {\n\t\t\tconst response = empty(204)\n\n\t\t\thandleOrigin(response, request)\n\t\t\thandleMethod(\n\t\t\t\tresponse,\n\t\t\t\trequest.headers.get('access-control-request-method')\n\t\t\t)\n\n\t\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\t\tif (allowedHeaders === true)\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-allow-headers',\n\t\t\t\t\t\trequest.headers.get('access-control-request-headers') ||\n\t\t\t\t\t\t\t''\n\t\t\t\t\t)\n\n\t\t\t\tif (exposeHeaders === true)\n\t\t\t\t\tresponse.headers.set(\n\t\t\t\t\t\t'access-control-expose-headers',\n\t\t\t\t\t\tprocessHeaders(request.headers)\n\t\t\t\t\t)\n\t\t\t}\n\n\t\t\tif (maxAge)\n\t\t\t\tresponse.headers.set(\n\t\t\t\t\t'access-control-max-age',\n\t\t\t\t\tmaxAge.toString()\n\t\t\t\t)\n\n\t\t\treturn response\n\t\t}\n\n\t\t// Process CORS for actual requests\n\t\tconst response = await next()\n\n\t\thandleOrigin(response, request)\n\t\thandleMethod(response, request.method)\n\t\tsetDefaultHeaders(response)\n\n\t\tif (allowedHeaders === true || exposeHeaders === true) {\n\t\t\tconst headers = processHeaders(request.headers)\n\n\t\t\tif (allowedHeaders === true)\n\t\t\t\tresponse.headers.set('access-control-allow-headers', headers)\n\n\t\t\tif (exposeHeaders === true)\n\t\t\t\tresponse.headers.set('access-control-expose-headers', headers)\n\t\t}\n\n\t\treturn response\n\t}\n}\n\nexport default cors\n"],"mappings":";AAEA,SAAS,aAAa;AA4JtB,IAAM,QAAQ,OAAO,IAAI,QAAQ,GAAG,WAAW;AAM/C,IAAM,iBAAiB,CAAC,YAAiB;AAExC,MAAI,YAAY,WAAW,OAAO,QAAQ,WAAW,YAAY;AAChE,WAAO,OAAO,KAAK,QAAQ,OAAO,CAAC,EAAE,KAAK,IAAI;AAAA,EAC/C;AAEA,MAAI,OAAO;AAEX,MAAI,IAAI;AACR,UAAQ,QAAQ,CAAC,GAAQ,QAAgB;AACxC,QAAI,EAAG,QAAO,OAAO,OAAO;AAAA,QACvB,QAAO;AAEZ;AAAA,EACD,CAAC;AAED,SAAO;AACR;AAEO,IAA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