@wxn0brp/falcon-frame 0.5.0 → 0.5.2

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,3 +2,4 @@ import { Cookies, RouteHandler, StaticServeOptions } from "./types.js";
2
2
  export declare function parseCookies(cookieHeader: string): Cookies;
3
3
  export declare function getContentType(filePath: string, utf8?: boolean): string;
4
4
  export declare function handleStaticFiles(dirPath: string, opts: StaticServeOptions): RouteHandler;
5
+ export declare const ensureLeadingDot: (str: string) => string;
package/dist/helpers.js CHANGED
@@ -45,6 +45,15 @@ export function handleStaticFiles(dirPath, opts) {
45
45
  throw new Error(`Directory ${dirPath} does not exist`);
46
46
  }
47
47
  const serveFile = (req, res, filePath, stats) => {
48
+ if (opts.render) {
49
+ const defaultRenderer = res.FF.getVar("view engine");
50
+ const renderStatement = (defaultRenderer && filePath.endsWith(ensureLeadingDot(defaultRenderer))) ||
51
+ (!opts.notRenderHtml && filePath.endsWith(".html"));
52
+ if (renderStatement) {
53
+ res.render(filePath, {}, { notUseViews: true });
54
+ return true;
55
+ }
56
+ }
48
57
  if (opts.etag) {
49
58
  const etag = `W/"${stats.size}-${stats.mtime.getTime()}"`;
50
59
  if (req.headers["if-none-match"] === etag) {
@@ -53,11 +62,6 @@ export function handleStaticFiles(dirPath, opts) {
53
62
  }
54
63
  res.setHeader("ETag", etag);
55
64
  }
56
- if (opts.render && filePath.endsWith(".html")) {
57
- res.ct("text/html");
58
- res.render(filePath);
59
- return true;
60
- }
61
65
  res.ct(getContentType(filePath, opts.utf8));
62
66
  fs.createReadStream(filePath).pipe(res);
63
67
  return true;
@@ -104,3 +108,4 @@ export function handleStaticFiles(dirPath, opts) {
104
108
  next();
105
109
  };
106
110
  }
111
+ export const ensureLeadingDot = (str) => (str.startsWith(".") ? str : "." + str);
package/dist/render.js CHANGED
@@ -3,8 +3,13 @@ import path from "path";
3
3
  export function renderHTML(templatePath, data, renderedPaths = [], FF) {
4
4
  try {
5
5
  const realPath = path.resolve(templatePath);
6
- if (renderedPaths.includes(realPath)) {
6
+ if (renderedPaths.includes(realPath))
7
7
  return `<!-- Circular dependency detected: tried to render ${templatePath} again -->`;
8
+ if (FF && FF.vars["render data"]) {
9
+ data = {
10
+ ...FF.vars["render data"],
11
+ ...data,
12
+ };
8
13
  }
9
14
  let template = fs.readFileSync(templatePath, "utf8");
10
15
  // Inserting data, e.g. {{name}}
package/dist/res.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import http from "http";
2
2
  import FalconFrame from "./index.js";
3
- import { CookieOptions } from "./types.js";
3
+ import { CookieOptions, RenderOptions } from "./types.js";
4
4
  export declare class FFResponse extends http.ServerResponse {
5
5
  _ended: boolean;
6
6
  FF: FalconFrame;
@@ -62,7 +62,7 @@ export declare class FFResponse extends http.ServerResponse {
62
62
  * @param data An object containing data to be injected into the template.
63
63
  * @returns The response object.
64
64
  */
65
- render(view: string, data?: any): this;
65
+ render(view: string, data?: any, opts?: RenderOptions): this;
66
66
  /**
67
67
  * Initialize SSE headers to start a server-sent event stream.
68
68
  * Sets:
package/dist/res.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createReadStream } from "fs";
2
2
  import http from "http";
3
3
  import path from "path";
4
- import { getContentType } from "./helpers.js";
4
+ import { ensureLeadingDot, getContentType } from "./helpers.js";
5
5
  export class FFResponse extends http.ServerResponse {
6
6
  _ended = false;
7
7
  FF;
@@ -104,20 +104,24 @@ export class FFResponse extends http.ServerResponse {
104
104
  * @param data An object containing data to be injected into the template.
105
105
  * @returns The response object.
106
106
  */
107
- render(view, data = {}) {
107
+ render(view, data = {}, opts = {}) {
108
108
  const ff = this.FF;
109
- const viewEngine = ff.getVar("view engine");
110
- const viewsDir = ff.getVar("views") || ".";
111
- let finalExt = path.extname(view);
109
+ const viewsDir = opts.baseDir ??
110
+ (opts.notUseViews ? "." : (ff.getVar("views") || "."));
111
+ let engineName = path.extname(view);
112
112
  let filePath = view;
113
- if (!finalExt) {
114
- const defaultExt = viewEngine ? (viewEngine.startsWith(".") ? viewEngine : "." + viewEngine) : ".html";
115
- finalExt = defaultExt;
116
- filePath += finalExt;
113
+ if (opts.engine) {
114
+ engineName = ensureLeadingDot(opts.engine);
117
115
  }
118
- const engine = ff.engines[finalExt];
116
+ else if (!engineName) {
117
+ const defaultEngine = ff.getVar("view engine");
118
+ engineName = defaultEngine ? ensureLeadingDot(defaultEngine) : ".html";
119
+ if (!opts.notAppendExt)
120
+ filePath += engineName;
121
+ }
122
+ const engine = ff.engines[engineName];
119
123
  if (!engine) {
120
- const errMessage = `No engine registered for extension ${finalExt}`;
124
+ const errMessage = `No engine registered for extension ${engineName}`;
121
125
  ff.logger.error(errMessage);
122
126
  this.status(500).end("Server Error: " + errMessage);
123
127
  return this;
@@ -130,7 +134,7 @@ export class FFResponse extends http.ServerResponse {
130
134
  this.status(500).end("Server Error: Failed to render view.");
131
135
  }
132
136
  else {
133
- this.setHeader("Content-Type", "text/html");
137
+ this.ct(opts.contentType || "text/html");
134
138
  this.end(str);
135
139
  }
136
140
  });
package/dist/types.d.ts CHANGED
@@ -59,5 +59,13 @@ export interface StaticServeOptions {
59
59
  render?: boolean;
60
60
  etag?: boolean;
61
61
  errorIfDirNotFound?: boolean;
62
+ notRenderHtml?: boolean;
62
63
  }
63
64
  export type EngineCallback = (path: string, options: any, callback: (e: any, rendered?: string) => void) => void;
65
+ export interface RenderOptions {
66
+ notUseViews?: boolean;
67
+ contentType?: string;
68
+ baseDir?: string;
69
+ engine?: string;
70
+ notAppendExt?: boolean;
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",