@wxn0brp/falcon-frame 0.5.5 → 0.5.7

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.
@@ -1,3 +1,4 @@
1
1
  import { FFResponse } from "./res.js";
2
2
  import { FFRequest } from "./types.js";
3
- export declare function compressionMiddleware(req: FFRequest, res: FFResponse): void;
3
+ export declare function compression(req: FFRequest, res: FFResponse): void;
4
+ export declare function compressionMiddleware(): (req: FFRequest, res: FFResponse, next: () => void) => void;
package/dist/compress.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createGzip, createDeflate } from "zlib";
2
- export function compressionMiddleware(req, res) {
2
+ export function compression(req, res) {
3
3
  const encoding = req.headers["accept-encoding"];
4
4
  if (!encoding || typeof encoding !== "string" || res.headersSent) {
5
5
  return;
@@ -47,3 +47,9 @@ export function compressionMiddleware(req, res) {
47
47
  return res;
48
48
  };
49
49
  }
50
+ export function compressionMiddleware() {
51
+ return (req, res, next) => {
52
+ compression(req, res);
53
+ next();
54
+ };
55
+ }
package/dist/helpers.js CHANGED
@@ -37,6 +37,7 @@ export function handleStaticFiles(dirPath, opts) {
37
37
  opts = {
38
38
  utf8: true,
39
39
  render: true,
40
+ renderData: {},
40
41
  etag: true,
41
42
  errorIfDirNotFound: true,
42
43
  ...opts,
@@ -50,7 +51,8 @@ export function handleStaticFiles(dirPath, opts) {
50
51
  const renderStatement = (defaultRenderer && filePath.endsWith(ensureLeadingDot(defaultRenderer))) ||
51
52
  (!opts.notRenderHtml && filePath.endsWith(".html"));
52
53
  if (renderStatement) {
53
- res.render(filePath, {}, { notUseViews: true });
54
+ const relativePath = filePath.replace(dirPath + "/", "").replace(".html", "");
55
+ res.render(filePath, opts.renderData?.[relativePath] || {}, { notUseViews: true });
54
56
  return true;
55
57
  }
56
58
  }
package/dist/render.js CHANGED
@@ -22,6 +22,16 @@ export function renderHTML(templatePath, data, renderedPaths = [], FF) {
22
22
  realPath,
23
23
  ], FF);
24
24
  });
25
+ // Loading files, e.g. /* include style.css */
26
+ template = template.replace(/\/\*\s*include\s*(.*?)\s*\*\//g, (_, fileName) => {
27
+ fileName = fileName.trim();
28
+ try {
29
+ return fs.readFileSync(fileName, "utf8");
30
+ }
31
+ catch (error) {
32
+ return `/* File not found: ${fileName} */`;
33
+ }
34
+ });
25
35
  // Layout
26
36
  if (FF && FF.vars["layout"]) {
27
37
  const hasHtmlStructure = /<\s*html|<\s*body/i.test(template);
package/dist/req.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { URL } from "url";
2
2
  import { parseCookies } from "./helpers.js";
3
+ import { getMiddlewares, matchMiddleware } from "./middleware.js";
3
4
  import { FFResponse } from "./res.js";
4
5
  import { validate } from "./valid.js";
5
- import { getMiddlewares, matchMiddleware } from "./middleware.js";
6
- import { compressionMiddleware } from "./compress.js";
7
6
  export function handleRequest(req, res, FF) {
8
7
  Object.setPrototypeOf(res, FFResponse.prototype);
9
8
  res.FF = FF;
@@ -40,8 +39,6 @@ export function handleRequest(req, res, FF) {
40
39
  res.status(404).end("404: File had second thoughts");
41
40
  return;
42
41
  }
43
- if (!FF.getVar("disable compression"))
44
- compressionMiddleware(req, res);
45
42
  let middlewareIndex = 0;
46
43
  async function next() {
47
44
  if (middlewareIndex >= matchedMiddlewares.length) {
package/dist/types.d.ts CHANGED
@@ -57,6 +57,7 @@ export type BeforeHandleRequest = (req: http.IncomingMessage, res: http.ServerRe
57
57
  export interface StaticServeOptions {
58
58
  utf8?: boolean;
59
59
  render?: boolean;
60
+ renderData?: Record<string, Record<string, any>>;
60
61
  etag?: boolean;
61
62
  errorIfDirNotFound?: boolean;
62
63
  notRenderHtml?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",