@wxn0brp/falcon-frame 0.0.15 → 0.0.17
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/helpers.d.ts +1 -1
- package/dist/helpers.js +12 -10
- package/dist/req.js +4 -1
- package/dist/router.js +1 -10
- package/dist/types.d.ts +1 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
package/dist/helpers.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import { Body, Cookies, RouteHandler } from "./types.js";
|
|
|
2
2
|
export declare function parseCookies(cookieHeader: string): Cookies;
|
|
3
3
|
export declare function parseBody(contentType: string, body: string): Body;
|
|
4
4
|
export declare function getContentType(filePath: string, utf8?: boolean): string;
|
|
5
|
-
export declare function handleStaticFiles(
|
|
5
|
+
export declare function handleStaticFiles(dirPath: string, utf8?: boolean): RouteHandler;
|
package/dist/helpers.js
CHANGED
|
@@ -60,24 +60,26 @@ export function getContentType(filePath, utf8 = false) {
|
|
|
60
60
|
contentType += "; charset=utf-8";
|
|
61
61
|
return contentType;
|
|
62
62
|
}
|
|
63
|
-
export function handleStaticFiles(
|
|
63
|
+
export function handleStaticFiles(dirPath, utf8 = true) {
|
|
64
64
|
return (req, res, next) => {
|
|
65
|
-
if (
|
|
65
|
+
if (req.method.toLowerCase() !== "get")
|
|
66
66
|
return next();
|
|
67
|
-
const
|
|
67
|
+
const apiPath = req.middleware.path;
|
|
68
|
+
const filePath = path.join(dirPath, req.path.replace(apiPath, ""));
|
|
68
69
|
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
|
69
70
|
res.ct(getContentType(filePath, utf8));
|
|
70
71
|
fs.createReadStream(filePath).pipe(res);
|
|
71
72
|
return true;
|
|
72
73
|
}
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
res.
|
|
78
|
-
fs.createReadStream(indexPath).pipe(res);
|
|
79
|
-
return true;
|
|
74
|
+
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
|
|
75
|
+
const indexPath = path.join(filePath, "index.html");
|
|
76
|
+
if (fs.existsSync(indexPath) && fs.statSync(indexPath).isFile()) {
|
|
77
|
+
if (!req.path.endsWith("/")) {
|
|
78
|
+
res.redirect(req.path + "/");
|
|
80
79
|
}
|
|
80
|
+
res.ct(getContentType(indexPath, utf8));
|
|
81
|
+
fs.createReadStream(indexPath).pipe(res);
|
|
82
|
+
return true;
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
85
|
next();
|
package/dist/req.js
CHANGED
|
@@ -26,7 +26,8 @@ export function handleRequest(req, res, FF) {
|
|
|
26
26
|
req.params = {};
|
|
27
27
|
req.valid = (schema) => validate(schema, req.body);
|
|
28
28
|
logger.info(`Incoming request: ${req.method} ${req.url}`);
|
|
29
|
-
const
|
|
29
|
+
const middlewaresPath = req.url.split("?")[0] + "/";
|
|
30
|
+
const middlewares = getMiddlewares(FF.middlewares, middlewaresPath.replace(/\/+/g, "/"));
|
|
30
31
|
let body = "";
|
|
31
32
|
req.on("data", chunk => (body += chunk.toString()));
|
|
32
33
|
req.on("end", () => {
|
|
@@ -57,6 +58,7 @@ export function handleRequest(req, res, FF) {
|
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
}
|
|
61
|
+
req.middleware = middleware;
|
|
60
62
|
const result = await middleware.middleware(req, res, next);
|
|
61
63
|
if (result && !res._ended) {
|
|
62
64
|
if (typeof result === "string") {
|
|
@@ -123,6 +125,7 @@ function getMiddlewares(middlewares, matchUrl, basePath = "") {
|
|
|
123
125
|
const midPath = (middleware.path || "").replace(/\/+$/, "");
|
|
124
126
|
const fullPath = (basePath + "/" + midPath).replace(/\/+/g, "/");
|
|
125
127
|
const matches = matchUrl === fullPath ||
|
|
128
|
+
(middleware.use && matchUrl.startsWith(fullPath)) ||
|
|
126
129
|
fullPath.includes(":") ||
|
|
127
130
|
fullPath.includes("*") ||
|
|
128
131
|
matchUrl.startsWith(fullPath + "/");
|
package/dist/router.js
CHANGED
|
@@ -45,15 +45,6 @@ export class Router {
|
|
|
45
45
|
dirPath = apiPath;
|
|
46
46
|
apiPath = "/";
|
|
47
47
|
}
|
|
48
|
-
this.
|
|
49
|
-
path: (apiPath + "/*").replace("//", "/"),
|
|
50
|
-
method: "get",
|
|
51
|
-
middleware: handleStaticFiles(apiPath, dirPath, utf8)
|
|
52
|
-
});
|
|
53
|
-
this.middlewares.push({
|
|
54
|
-
path: apiPath,
|
|
55
|
-
method: "get",
|
|
56
|
-
middleware: handleStaticFiles(apiPath, dirPath, utf8)
|
|
57
|
-
});
|
|
48
|
+
this.use(apiPath, handleStaticFiles(dirPath, utf8));
|
|
58
49
|
}
|
|
59
50
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/types.js
CHANGED