@wxn0brp/falcon-frame 0.0.15 → 0.0.16

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
@@ -57,6 +57,7 @@ export function handleRequest(req, res, FF) {
57
57
  }
58
58
  }
59
59
  }
60
+ req.middleware = middleware;
60
61
  const result = await middleware.middleware(req, res, next);
61
62
  if (result && !res._ended) {
62
63
  if (typeof result === "string") {
@@ -123,6 +124,7 @@ function getMiddlewares(middlewares, matchUrl, basePath = "") {
123
124
  const midPath = (middleware.path || "").replace(/\/+$/, "");
124
125
  const fullPath = (basePath + "/" + midPath).replace(/\/+/g, "/");
125
126
  const matches = matchUrl === fullPath ||
127
+ (middleware.use && matchUrl.startsWith(fullPath)) ||
126
128
  fullPath.includes(":") ||
127
129
  fullPath.includes("*") ||
128
130
  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.16",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",