@wxn0brp/falcon-frame 0.3.0 → 0.4.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/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/plugin.d.ts +9 -0
- package/dist/plugin.js +9 -0
- package/dist/render.d.ts +2 -1
- package/dist/render.js +12 -1
- package/dist/req.js +2 -0
- package/dist/router.d.ts +3 -1
- package/dist/router.js +11 -3
- package/dist/sse.d.ts +13 -0
- package/dist/sse.js +37 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -29,11 +29,11 @@ export declare class FalconFrame<Vars extends Record<string, any> = any> extends
|
|
|
29
29
|
* Sets the allowed origins for CORS.
|
|
30
30
|
* This method is a shortcut that simplifies CORS configuration
|
|
31
31
|
* without needing to manually create and register a plugin.
|
|
32
|
-
* @param origin - An array of allowed origins.
|
|
32
|
+
* @param [origin] - An array of allowed origins. (default: ["*"])
|
|
33
33
|
* @example
|
|
34
34
|
* app.setOrigin(["http://example.com", "https://example.com"]);
|
|
35
35
|
*/
|
|
36
|
-
setOrigin(origin
|
|
36
|
+
setOrigin(origin?: string[] | string): void;
|
|
37
37
|
}
|
|
38
38
|
export default FalconFrame;
|
|
39
39
|
export * as Helpers from "./helpers.js";
|
package/dist/index.js
CHANGED
|
@@ -84,12 +84,12 @@ export class FalconFrame extends Router {
|
|
|
84
84
|
* Sets the allowed origins for CORS.
|
|
85
85
|
* This method is a shortcut that simplifies CORS configuration
|
|
86
86
|
* without needing to manually create and register a plugin.
|
|
87
|
-
* @param origin - An array of allowed origins.
|
|
87
|
+
* @param [origin] - An array of allowed origins. (default: ["*"])
|
|
88
88
|
* @example
|
|
89
89
|
* app.setOrigin(["http://example.com", "https://example.com"]);
|
|
90
90
|
*/
|
|
91
|
-
setOrigin(origin) {
|
|
92
|
-
this.use(createCORSPlugin(origin).process);
|
|
91
|
+
setOrigin(origin = "*") {
|
|
92
|
+
this.use(createCORSPlugin(Array.isArray(origin) ? origin : [origin]).process);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
export default FalconFrame;
|
package/dist/plugin.d.ts
CHANGED
|
@@ -11,6 +11,15 @@ export interface PluginOptions {
|
|
|
11
11
|
after?: PluginId | PluginId[];
|
|
12
12
|
optional?: boolean;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated
|
|
16
|
+
* The `PluginSystem` class is deprecated and should not be used in new projects.
|
|
17
|
+
*
|
|
18
|
+
* ⚠️ Notes:
|
|
19
|
+
* - It may be refactored in the future, but any refactor will introduce breaking changes.
|
|
20
|
+
* - If your system currently relies on it and it works, you may continue using it.
|
|
21
|
+
* - If it does not work in your setup, it is recommended to skip it or wait for an update.
|
|
22
|
+
*/
|
|
14
23
|
export declare class PluginSystem {
|
|
15
24
|
private plugins;
|
|
16
25
|
private executionOrder;
|
package/dist/plugin.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated
|
|
3
|
+
* The `PluginSystem` class is deprecated and should not be used in new projects.
|
|
4
|
+
*
|
|
5
|
+
* ⚠️ Notes:
|
|
6
|
+
* - It may be refactored in the future, but any refactor will introduce breaking changes.
|
|
7
|
+
* - If your system currently relies on it and it works, you may continue using it.
|
|
8
|
+
* - If it does not work in your setup, it is recommended to skip it or wait for an update.
|
|
9
|
+
*/
|
|
1
10
|
export class PluginSystem {
|
|
2
11
|
plugins = [];
|
|
3
12
|
executionOrder = [];
|
package/dist/render.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import FalconFrame from "./index.js";
|
|
1
2
|
interface RenderData {
|
|
2
3
|
[key: string]: string;
|
|
3
4
|
}
|
|
4
|
-
export declare function renderHTML(templatePath: string, data: RenderData, renderedPaths?: string[]): string;
|
|
5
|
+
export declare function renderHTML(templatePath: string, data: RenderData, renderedPaths?: string[], FF?: FalconFrame): string;
|
|
5
6
|
export {};
|
package/dist/render.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
export function renderHTML(templatePath, data, renderedPaths = []) {
|
|
3
|
+
export function renderHTML(templatePath, data, renderedPaths = [], FF) {
|
|
4
4
|
try {
|
|
5
5
|
const realPath = path.resolve(templatePath);
|
|
6
6
|
if (renderedPaths.includes(realPath)) {
|
|
@@ -17,6 +17,17 @@ export function renderHTML(templatePath, data, renderedPaths = []) {
|
|
|
17
17
|
realPath,
|
|
18
18
|
]);
|
|
19
19
|
});
|
|
20
|
+
// Layout
|
|
21
|
+
if (FF && FF.vars["layout"]) {
|
|
22
|
+
const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
|
|
23
|
+
const forceLayout = /<!--\s*force-layout\s*-->/.test(template);
|
|
24
|
+
const forceNoLayout = /<!--\s*force-no-layout\s*-->/.test(template);
|
|
25
|
+
if (hasHtmlStructure && !forceLayout)
|
|
26
|
+
return template;
|
|
27
|
+
if (!hasHtmlStructure && forceNoLayout)
|
|
28
|
+
return template;
|
|
29
|
+
return renderHTML(FF.vars["layout"], { ...data, body: template }, [...renderedPaths, realPath]);
|
|
30
|
+
}
|
|
20
31
|
return template;
|
|
21
32
|
}
|
|
22
33
|
catch (error) {
|
package/dist/req.js
CHANGED
|
@@ -72,6 +72,7 @@ export function handleRequest(req, res, FF) {
|
|
|
72
72
|
}
|
|
73
73
|
const hasCustomParserEndpoint = matchedMiddlewares.some(middleware => middleware.customParser);
|
|
74
74
|
if (hasCustomParserEndpoint) {
|
|
75
|
+
logger.debug("Executing custom parser endpoint");
|
|
75
76
|
req.body = {};
|
|
76
77
|
next();
|
|
77
78
|
return;
|
|
@@ -79,6 +80,7 @@ export function handleRequest(req, res, FF) {
|
|
|
79
80
|
if (req.method === "GET" ||
|
|
80
81
|
req.method === "HEAD" ||
|
|
81
82
|
req.method === "OPTIONS") {
|
|
83
|
+
logger.debug("Method does not require body. Executing middlewares");
|
|
82
84
|
req.body = {};
|
|
83
85
|
next();
|
|
84
86
|
return;
|
package/dist/router.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PluginSystem } from "./plugin.js";
|
|
2
|
+
import { SSEManager } from "./sse.js";
|
|
2
3
|
import { Method, Middleware, RouteHandler, StaticServeOptions } from "./types.js";
|
|
3
4
|
export type MiddlewareFn = RouteHandler | Router | PluginSystem;
|
|
4
5
|
export declare class Router {
|
|
@@ -11,6 +12,7 @@ export declare class Router {
|
|
|
11
12
|
delete(path: string, ...handlers: RouteHandler[]): this;
|
|
12
13
|
all(path: string, ...handlers: RouteHandler[]): this;
|
|
13
14
|
static(apiPath: string, dirPath?: string, opts?: StaticServeOptions): this;
|
|
14
|
-
sse(path: string, ...handlers: RouteHandler[]):
|
|
15
|
+
sse(path: string, ...handlers: RouteHandler[]): SSEManager;
|
|
15
16
|
customParser(path: string, handler: RouteHandler, method?: Method): this;
|
|
17
|
+
router(path: string): Router;
|
|
16
18
|
}
|
package/dist/router.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { handleStaticFiles } from "./helpers.js";
|
|
2
2
|
import { PluginSystem } from "./plugin.js";
|
|
3
|
+
import { SSEManager } from "./sse.js";
|
|
3
4
|
export class Router {
|
|
4
5
|
middlewares = [];
|
|
5
6
|
addRoute(method, path, ...handlers) {
|
|
@@ -59,13 +60,20 @@ export class Router {
|
|
|
59
60
|
return this;
|
|
60
61
|
}
|
|
61
62
|
sse(path, ...handlers) {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
const lastHandler = handlers.pop() || (() => { });
|
|
64
|
+
const manager = new SSEManager();
|
|
65
|
+
handlers.push(manager.getMiddleware(lastHandler));
|
|
66
|
+
this.addRoute("get", path, ...handlers);
|
|
67
|
+
return manager;
|
|
65
68
|
}
|
|
66
69
|
customParser(path, handler, method = "post") {
|
|
67
70
|
const index = this.addRoute(method, path, handler);
|
|
68
71
|
this.middlewares[index - 1].customParser = true;
|
|
69
72
|
return this;
|
|
70
73
|
}
|
|
74
|
+
router(path) {
|
|
75
|
+
const router = new Router();
|
|
76
|
+
this.use(path, router);
|
|
77
|
+
return router;
|
|
78
|
+
}
|
|
71
79
|
}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FFRequest, RouteHandler } from "./types.js";
|
|
2
|
+
import { FFResponse } from "./res.js";
|
|
3
|
+
export declare class SSEManager {
|
|
4
|
+
private clients;
|
|
5
|
+
getMiddleware(lastHandler?: RouteHandler): RouteHandler;
|
|
6
|
+
getClients(): Map<string, {
|
|
7
|
+
req: FFRequest;
|
|
8
|
+
res: FFResponse;
|
|
9
|
+
}>;
|
|
10
|
+
sendAll(data: any): void;
|
|
11
|
+
sendTo(id: string, data: any): void;
|
|
12
|
+
disconnect(id: string): void;
|
|
13
|
+
}
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class SSEManager {
|
|
3
|
+
clients = new Map();
|
|
4
|
+
getMiddleware(lastHandler) {
|
|
5
|
+
return (req, res, next) => {
|
|
6
|
+
res.sseInit();
|
|
7
|
+
const id = randomUUID();
|
|
8
|
+
this.clients.set(id, { req, res });
|
|
9
|
+
req.on("close", () => {
|
|
10
|
+
this.clients.delete(id);
|
|
11
|
+
});
|
|
12
|
+
req.sseId = id;
|
|
13
|
+
lastHandler?.(req, res, next);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
getClients() {
|
|
17
|
+
return this.clients;
|
|
18
|
+
}
|
|
19
|
+
sendAll(data) {
|
|
20
|
+
for (const { res } of this.clients.values()) {
|
|
21
|
+
res.sseSend(data);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
sendTo(id, data) {
|
|
25
|
+
const client = this.clients.get(id);
|
|
26
|
+
if (client) {
|
|
27
|
+
client.res.sseSend(data);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
disconnect(id) {
|
|
31
|
+
const client = this.clients.get(id);
|
|
32
|
+
if (client) {
|
|
33
|
+
client.res.end();
|
|
34
|
+
this.clients.delete(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ export declare class FFRequest extends http.IncomingMessage {
|
|
|
27
27
|
body: Body;
|
|
28
28
|
valid: (schema: ValidationSchema) => ValidationResult;
|
|
29
29
|
middleware: Middleware;
|
|
30
|
+
sseId?: string;
|
|
30
31
|
}
|
|
31
32
|
export interface Middleware {
|
|
32
33
|
path: string;
|
|
@@ -34,7 +35,6 @@ export interface Middleware {
|
|
|
34
35
|
middleware: RouteHandler;
|
|
35
36
|
use?: true;
|
|
36
37
|
router?: Middleware[];
|
|
37
|
-
sse?: true;
|
|
38
38
|
customParser?: true;
|
|
39
39
|
}
|
|
40
40
|
export interface CookieOptions {
|
package/dist/types.js
CHANGED