@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 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(apiPath: string, dirPath: string, utf8?: boolean): RouteHandler;
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(apiPath, dirPath, utf8 = true) {
63
+ export function handleStaticFiles(dirPath, utf8 = true) {
64
64
  return (req, res, next) => {
65
- if (!req.path.startsWith(apiPath))
65
+ if (req.method.toLowerCase() !== "get")
66
66
  return next();
67
- const filePath = path.join(dirPath, req.path.slice(apiPath.length));
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 (req.path.endsWith("/")) {
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
- res.ct(getContentType(indexPath, utf8));
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 middlewares = getMiddlewares(FF.middlewares, (req.url + "/").replace(/\/+/g, "/"));
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.middlewares.push({
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
@@ -21,6 +21,7 @@ export declare class FFRequest extends http.IncomingMessage {
21
21
  cookies: Cookies;
22
22
  body: Body;
23
23
  valid: (schema: ValidationSchema) => ValidationResult;
24
+ middleware: Middleware;
24
25
  }
25
26
  export interface Middleware {
26
27
  path: string;
package/dist/types.js CHANGED
@@ -6,4 +6,5 @@ export class FFRequest extends http.IncomingMessage {
6
6
  cookies;
7
7
  body;
8
8
  valid;
9
+ middleware;
9
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",