@wxn0brp/falcon-frame 0.0.7 → 0.0.10
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/plugins/cors.js +12 -4
- package/dist/req.js +12 -3
- package/package.json +1 -1
package/dist/plugins/cors.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
function setHeader(res, opts) {
|
|
2
|
+
if (opts.accessControlAllowMethods)
|
|
3
|
+
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
|
|
4
|
+
if (opts.accessControlAllowHeaders)
|
|
5
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
6
|
+
}
|
|
1
7
|
export function createCORSPlugin(allowedOrigins, opts = {}) {
|
|
2
8
|
opts = {
|
|
3
9
|
accessControlAllowMethods: true,
|
|
@@ -7,13 +13,15 @@ export function createCORSPlugin(allowedOrigins, opts = {}) {
|
|
|
7
13
|
return {
|
|
8
14
|
id: "cors",
|
|
9
15
|
process: (req, res, next) => {
|
|
16
|
+
if (allowedOrigins.includes("*")) {
|
|
17
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
18
|
+
setHeader(res, opts);
|
|
19
|
+
return next();
|
|
20
|
+
}
|
|
10
21
|
const origin = req.headers.origin;
|
|
11
22
|
if (origin && allowedOrigins.includes(origin)) {
|
|
12
23
|
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
13
|
-
|
|
14
|
-
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
|
|
15
|
-
if (opts.accessControlAllowHeaders)
|
|
16
|
-
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
24
|
+
setHeader(res, opts);
|
|
17
25
|
}
|
|
18
26
|
if (req.method === "OPTIONS") {
|
|
19
27
|
res.statusCode = 204;
|
package/dist/req.js
CHANGED
|
@@ -10,9 +10,18 @@ export function handleRequest(req, res, FF) {
|
|
|
10
10
|
return originalEnd.call(res, ...any);
|
|
11
11
|
};
|
|
12
12
|
const { logger, middlewares } = FF;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
try {
|
|
14
|
+
const [path, params] = (req.url || "").split("?");
|
|
15
|
+
const normalizedPath = path.replace(/\/{2,}/g, "/");
|
|
16
|
+
const parsedUrl = new URL(normalizedPath + (params ? `?${params}` : ""), "http://localhost");
|
|
17
|
+
req.path = parsedUrl.pathname || "/";
|
|
18
|
+
req.query = Object.fromEntries(parsedUrl.searchParams);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
logger.error(`Error parsing URL (${req.url}): ${e}`);
|
|
22
|
+
res.status(400).end("400: Bad request");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
16
25
|
req.cookies = parseCookies(req.headers.cookie || "");
|
|
17
26
|
req.params = {};
|
|
18
27
|
req.valid = (schema) => validate(schema, req.body);
|