@wxn0brp/falcon-frame 0.2.0 → 0.2.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/helpers.js +4 -14
- package/dist/index.d.ts +1 -1
- package/dist/router.d.ts +3 -1
- package/dist/router.js +5 -1
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/dist/helpers.js
CHANGED
|
@@ -38,9 +38,10 @@ export function handleStaticFiles(dirPath, opts) {
|
|
|
38
38
|
utf8: true,
|
|
39
39
|
render: true,
|
|
40
40
|
etag: true,
|
|
41
|
+
errorIfDirNotFound: true,
|
|
41
42
|
...opts,
|
|
42
43
|
};
|
|
43
|
-
if (!fs.existsSync(dirPath)) {
|
|
44
|
+
if (opts.errorIfDirNotFound && !fs.existsSync(dirPath)) {
|
|
44
45
|
throw new Error(`Directory ${dirPath} does not exist`);
|
|
45
46
|
}
|
|
46
47
|
const serveFile = (req, res, filePath, stats) => {
|
|
@@ -94,19 +95,8 @@ export function handleStaticFiles(dirPath, opts) {
|
|
|
94
95
|
try {
|
|
95
96
|
const htmlPath = filePath + ".html";
|
|
96
97
|
const htmlStats = fs.statSync(htmlPath);
|
|
97
|
-
if (htmlStats.isFile())
|
|
98
|
-
|
|
99
|
-
const etag = `W/"${htmlStats.size}-${htmlStats.mtime.getTime()}"`;
|
|
100
|
-
if (req.headers["if-none-match"] === etag) {
|
|
101
|
-
res.status(304).end();
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
res.setHeader("ETag", etag);
|
|
105
|
-
}
|
|
106
|
-
res.ct(getContentType(htmlPath, opts.utf8));
|
|
107
|
-
fs.createReadStream(htmlPath).pipe(res);
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
98
|
+
if (htmlStats.isFile())
|
|
99
|
+
return serveFile(req, res, htmlPath, htmlStats);
|
|
110
100
|
}
|
|
111
101
|
catch (e) {
|
|
112
102
|
/* .html file not found, fall through to next() */
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare class FalconFrame<Vars extends Record<string, any> = any> extends
|
|
|
17
17
|
opts: Opts;
|
|
18
18
|
engines: Record<string, EngineCallback>;
|
|
19
19
|
constructor(opts?: Partial<Opts>);
|
|
20
|
-
listen(port: number, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
20
|
+
listen(port: number | string, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
21
21
|
getApp(beforeHandleRequest?: BeforeHandleRequest): (req: any, res: any) => Promise<void>;
|
|
22
22
|
engine(ext: string, callback: EngineCallback): this;
|
|
23
23
|
setVar(key: keyof Vars, value: typeof this.vars[keyof Vars]): void;
|
package/dist/router.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { PluginSystem } from "./plugin.js";
|
|
1
2
|
import { Method, Middleware, RouteHandler, StaticServeOptions } from "./types.js";
|
|
3
|
+
export type MiddlewareFn = RouteHandler | Router | PluginSystem;
|
|
2
4
|
export declare class Router {
|
|
3
5
|
middlewares: Middleware[];
|
|
4
6
|
addRoute(method: Method, path: string, ...handlers: RouteHandler[]): number;
|
|
5
|
-
use(path?: string |
|
|
7
|
+
use(path?: string | MiddlewareFn, middlewareFn?: MiddlewareFn, method?: Method): this;
|
|
6
8
|
get(path: string, ...handlers: RouteHandler[]): this;
|
|
7
9
|
post(path: string, ...handlers: RouteHandler[]): this;
|
|
8
10
|
put(path: string, ...handlers: RouteHandler[]): this;
|
package/dist/router.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { handleStaticFiles } from "./helpers.js";
|
|
2
|
+
import { PluginSystem } from "./plugin.js";
|
|
2
3
|
export class Router {
|
|
3
4
|
middlewares = [];
|
|
4
5
|
addRoute(method, path, ...handlers) {
|
|
@@ -7,7 +8,7 @@ export class Router {
|
|
|
7
8
|
return this.middlewares.push({ path, middleware: handler, method });
|
|
8
9
|
}
|
|
9
10
|
use(path = "/", middlewareFn, method = "all") {
|
|
10
|
-
if (typeof path === "function" || path instanceof Router) {
|
|
11
|
+
if (typeof path === "function" || path instanceof Router || path instanceof PluginSystem) {
|
|
11
12
|
middlewareFn = path;
|
|
12
13
|
path = "/";
|
|
13
14
|
}
|
|
@@ -20,6 +21,9 @@ export class Router {
|
|
|
20
21
|
if (middlewareFn instanceof Router) {
|
|
21
22
|
middleware.router = middlewareFn.middlewares;
|
|
22
23
|
}
|
|
24
|
+
else if (middlewareFn instanceof PluginSystem) {
|
|
25
|
+
middleware.middleware = middlewareFn.getRouteHandler();
|
|
26
|
+
}
|
|
23
27
|
else {
|
|
24
28
|
middleware.middleware = middlewareFn;
|
|
25
29
|
}
|
package/dist/types.d.ts
CHANGED