@wxn0brp/falcon-frame 0.7.0 → 0.7.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/dist/cors.d.ts +1 -1
- package/dist/cors.js +53 -17
- package/package.json +2 -2
package/dist/cors.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export interface Opts {
|
|
|
4
4
|
accessControlAllowHeaders?: boolean;
|
|
5
5
|
headers?: Record<string, string>;
|
|
6
6
|
}
|
|
7
|
-
export declare function createCORS(allowedOrigins: string[], opts?: Opts): RouteHandler;
|
|
7
|
+
export declare function createCORS(allowedOrigins: (string | RegExp)[], opts?: Opts): RouteHandler;
|
package/dist/cors.js
CHANGED
|
@@ -4,6 +4,54 @@ function setHeader(res, opts) {
|
|
|
4
4
|
if (opts.accessControlAllowHeaders)
|
|
5
5
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
6
6
|
}
|
|
7
|
+
function parseOrigin(origin) {
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(origin);
|
|
10
|
+
return {
|
|
11
|
+
host: url.hostname,
|
|
12
|
+
port: url.port ||
|
|
13
|
+
(url.protocol === "https:" ? "443" : "80")
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function matches(origin, pattern) {
|
|
21
|
+
if (pattern === "*")
|
|
22
|
+
return true;
|
|
23
|
+
if (pattern instanceof RegExp)
|
|
24
|
+
return pattern.test(origin);
|
|
25
|
+
if (pattern.toLowerCase() === origin.toLowerCase())
|
|
26
|
+
return true;
|
|
27
|
+
const o = parseOrigin(origin);
|
|
28
|
+
if (!o)
|
|
29
|
+
return false;
|
|
30
|
+
// *.example.com[:port]
|
|
31
|
+
if (pattern.startsWith("*.")) {
|
|
32
|
+
const [pHost, pPort] = pattern.split(":");
|
|
33
|
+
const base = pHost.slice(2).toLowerCase();
|
|
34
|
+
if (o.host !== base && o.host.endsWith("." + base)) {
|
|
35
|
+
return !pPort || pPort === "*" || pPort === o.port;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// localhost:port
|
|
40
|
+
if (pattern.includes(":")) {
|
|
41
|
+
const [pHost, pPort] = pattern.split(":");
|
|
42
|
+
const base = pHost.toLowerCase();
|
|
43
|
+
return o.host === base && (pPort === "*" || o.port === pPort);
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
function handleEnd(req, res, next, origin) {
|
|
48
|
+
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
49
|
+
if (req.method === "OPTIONS") {
|
|
50
|
+
res.statusCode = 204;
|
|
51
|
+
return res.end();
|
|
52
|
+
}
|
|
53
|
+
next();
|
|
54
|
+
}
|
|
7
55
|
export function createCORS(allowedOrigins, opts = {}) {
|
|
8
56
|
opts = {
|
|
9
57
|
accessControlAllowMethods: true,
|
|
@@ -16,24 +64,12 @@ export function createCORS(allowedOrigins, opts = {}) {
|
|
|
16
64
|
res.setHeader(key, value);
|
|
17
65
|
}
|
|
18
66
|
}
|
|
19
|
-
|
|
20
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
21
|
-
setHeader(res, opts);
|
|
22
|
-
if (req.method === "OPTIONS") {
|
|
23
|
-
res.statusCode = 204;
|
|
24
|
-
return res.end();
|
|
25
|
-
}
|
|
26
|
-
return next();
|
|
27
|
-
}
|
|
67
|
+
setHeader(res, opts);
|
|
28
68
|
const origin = req.headers.origin;
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
res.statusCode = 204;
|
|
34
|
-
return res.end();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
69
|
+
if (allowedOrigins.includes("*"))
|
|
70
|
+
return handleEnd(req, res, next, origin || "*");
|
|
71
|
+
if (origin && allowedOrigins.some((pattern) => matches(origin, pattern)))
|
|
72
|
+
return handleEnd(req, res, next, origin);
|
|
37
73
|
next();
|
|
38
74
|
};
|
|
39
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/falcon-frame",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"typescript": "*"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@wxn0brp/lucerna-log": "^0.2.
|
|
32
|
+
"@wxn0brp/lucerna-log": "^0.2.2"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|