@wxn0brp/falcon-frame 0.7.1 → 0.8.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/compress.js +1 -1
- package/dist/helpers.js +6 -1
- package/dist/index.js +6 -2
- package/dist/render.d.ts +9 -2
- package/dist/render.js +28 -7
- package/dist/sse.d.ts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
package/dist/compress.js
CHANGED
package/dist/helpers.js
CHANGED
|
@@ -72,7 +72,12 @@ export function handleStaticFiles(dirPath, opts) {
|
|
|
72
72
|
if (req.method.toLowerCase() !== "get")
|
|
73
73
|
return next();
|
|
74
74
|
const apiPath = req.middleware.path;
|
|
75
|
-
const
|
|
75
|
+
const unsafePath = req.path.replace(apiPath, "");
|
|
76
|
+
const filePath = path.join(dirPath, path.normalize(unsafePath));
|
|
77
|
+
const resolvedPath = path.resolve(filePath);
|
|
78
|
+
const resolvedDir = path.resolve(dirPath);
|
|
79
|
+
if (!resolvedPath.startsWith(resolvedDir))
|
|
80
|
+
return next();
|
|
76
81
|
try {
|
|
77
82
|
const stats = fs.statSync(filePath);
|
|
78
83
|
if (stats.isFile()) {
|
package/dist/index.js
CHANGED
|
@@ -43,9 +43,13 @@ export class FalconFrame extends Router {
|
|
|
43
43
|
this.addBodyParser(json({ limit: this.opts.bodyLimit }));
|
|
44
44
|
if (!this.opts.disableUrlencodedParser)
|
|
45
45
|
this.addBodyParser(urlencoded({ limit: this.opts.bodyLimit }));
|
|
46
|
-
this.engine(".html", (path,
|
|
46
|
+
this.engine(".html", (path, data, callback, FF) => {
|
|
47
47
|
try {
|
|
48
|
-
const content = renderHTML(
|
|
48
|
+
const content = renderHTML({
|
|
49
|
+
templatePath: path,
|
|
50
|
+
data,
|
|
51
|
+
FF,
|
|
52
|
+
});
|
|
49
53
|
callback(null, content);
|
|
50
54
|
}
|
|
51
55
|
catch (e) {
|
package/dist/render.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import FalconFrame from "./index.js";
|
|
2
|
-
|
|
2
|
+
import { CombinedVars } from "./types.js";
|
|
3
|
+
export interface RenderHTMLOptions {
|
|
4
|
+
templatePath: string;
|
|
5
|
+
data?: Record<string, any>;
|
|
6
|
+
FF?: FalconFrame<any>;
|
|
3
7
|
noLayout?: boolean;
|
|
8
|
+
FFVar?: CombinedVars<any>;
|
|
9
|
+
/** don't use, used internally */
|
|
10
|
+
_renderedPaths?: string[];
|
|
4
11
|
}
|
|
5
|
-
export declare function renderHTML(
|
|
12
|
+
export declare function renderHTML(options: RenderHTMLOptions): string;
|
package/dist/render.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
|
-
export function renderHTML(
|
|
3
|
+
export function renderHTML(options) {
|
|
4
4
|
try {
|
|
5
|
+
let data = options.data || {};
|
|
6
|
+
const { templatePath, _renderedPaths: renderedPaths = [], FF, noLayout, FFVar, } = options;
|
|
5
7
|
const realPath = path.resolve(templatePath);
|
|
6
8
|
if (renderedPaths.includes(realPath))
|
|
7
9
|
return `<!-- Circular dependency detected: tried to render ${templatePath} again -->`;
|
|
10
|
+
if (!fs.existsSync(templatePath))
|
|
11
|
+
return `<!-- Template not found: ${templatePath} -->`;
|
|
8
12
|
let template = fs.readFileSync(templatePath, "utf8");
|
|
9
13
|
// Loading internal data, e.g. <!-- data { "title": "My title" } -->
|
|
10
14
|
const templateDataMatch = template.match(/<!--\s*data\s*(\{.*?\})\s*-->/s);
|
|
@@ -18,7 +22,7 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts
|
|
|
18
22
|
template = template.replace(templateDataMatch[0], "<!-- Invalid template data -->");
|
|
19
23
|
}
|
|
20
24
|
}
|
|
21
|
-
const FFData = FF && FF.getVar("render data");
|
|
25
|
+
const FFData = FFVar?.["render data"] ?? (FF && FF.getVar("render data"));
|
|
22
26
|
data = {
|
|
23
27
|
...(FFData || {}),
|
|
24
28
|
...templateData,
|
|
@@ -29,7 +33,14 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts
|
|
|
29
33
|
// Loading partials, e.g. <!-- include header -->
|
|
30
34
|
template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
|
|
31
35
|
const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
|
|
32
|
-
return renderHTML(
|
|
36
|
+
return renderHTML({
|
|
37
|
+
templatePath: partialPath,
|
|
38
|
+
_renderedPaths: [...renderedPaths, realPath],
|
|
39
|
+
noLayout: true,
|
|
40
|
+
data,
|
|
41
|
+
FF,
|
|
42
|
+
FFVar,
|
|
43
|
+
});
|
|
33
44
|
});
|
|
34
45
|
// Loading files, e.g. /* include style.css */
|
|
35
46
|
template = template.replace(/\/\*\s*include\s*(.*?)\s*\*\//g, (_, fileName) => {
|
|
@@ -42,8 +53,8 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts
|
|
|
42
53
|
}
|
|
43
54
|
});
|
|
44
55
|
// Layout
|
|
45
|
-
const FFLayout = FF && FF.getVar("layout");
|
|
46
|
-
if (!
|
|
56
|
+
const FFLayout = FFVar?.layout ?? (FF && FF.getVar("layout"));
|
|
57
|
+
if (!noLayout && FFLayout) {
|
|
47
58
|
const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
|
|
48
59
|
const forceLayout = /<!--\s*force-layout\s*-->/.test(template);
|
|
49
60
|
const forceNoLayout = /<!--\s*force-no-layout\s*-->/.test(template);
|
|
@@ -52,11 +63,21 @@ export function renderHTML(templatePath, data = {}, renderedPaths = [], FF, opts
|
|
|
52
63
|
return template;
|
|
53
64
|
if (!hasHtmlStructure && forceNoLayout)
|
|
54
65
|
return template;
|
|
55
|
-
return renderHTML(
|
|
66
|
+
return renderHTML({
|
|
67
|
+
templatePath: FFLayout,
|
|
68
|
+
data: {
|
|
69
|
+
...data,
|
|
70
|
+
body: template
|
|
71
|
+
},
|
|
72
|
+
_renderedPaths: [...renderedPaths, realPath],
|
|
73
|
+
noLayout: true,
|
|
74
|
+
FF,
|
|
75
|
+
FFVar,
|
|
76
|
+
});
|
|
56
77
|
}
|
|
57
78
|
return template;
|
|
58
79
|
}
|
|
59
80
|
catch (error) {
|
|
60
|
-
return
|
|
81
|
+
return `<-- Template ${options.templatePath} has errors -->`;
|
|
61
82
|
}
|
|
62
83
|
}
|
package/dist/sse.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { FFRequest, RouteHandler } from "./types.js";
|
|
2
1
|
import { FFResponse } from "./res.js";
|
|
2
|
+
import type { FFRequest, RouteHandler } from "./types.js";
|
|
3
3
|
export declare class SSEManager {
|
|
4
4
|
private clients;
|
|
5
5
|
getMiddleware(lastHandler?: RouteHandler): RouteHandler;
|
package/dist/types.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ export interface StaticServeOptions {
|
|
|
66
66
|
errorIfDirNotFound?: boolean;
|
|
67
67
|
notRenderHtml?: boolean;
|
|
68
68
|
}
|
|
69
|
-
export type EngineCallback = (path: string,
|
|
69
|
+
export type EngineCallback = (path: string, data: any, callback: (e: any, rendered?: string) => void, FF?: FalconFrame) => void;
|
|
70
70
|
export interface RenderOptions {
|
|
71
71
|
notUseViews?: boolean;
|
|
72
72
|
contentType?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/falcon-frame",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "*",
|
|
28
28
|
"tsc-alias": "*",
|
|
29
|
-
"typescript": "
|
|
29
|
+
"typescript": "^6"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@wxn0brp/lucerna-log": "^0.2.2"
|