@wxn0brp/falcon-frame 0.9.6 → 0.9.8

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.
@@ -0,0 +1,7 @@
1
+ export declare const mimeTypes: Record<string, string>;
2
+ /**
3
+ * Registers a MIME type for a file extension
4
+ */
5
+ export declare function registerMimeType(extension: string, contentType: string): void;
6
+ export declare function isTextualContentType(contentType: string): boolean;
7
+ export declare function getContentType(filePath: string, utf8?: boolean): string;
@@ -0,0 +1,71 @@
1
+ import path from "path";
2
+ export const mimeTypes = {
3
+ ".html": "text/html",
4
+ ".xhtml": "application/xhtml+xml",
5
+ ".css": "text/css",
6
+ ".js": "application/javascript",
7
+ ".mjs": "application/javascript",
8
+ ".cjs": "application/javascript",
9
+ ".json": "application/json",
10
+ ".json5": "application/json5",
11
+ ".yaml": "application/yaml",
12
+ ".yml": "application/yaml",
13
+ ".toml": "application/toml",
14
+ ".xml": "application/xml",
15
+ ".csv": "text/csv",
16
+ ".md": "text/markdown",
17
+ ".vtt": "text/vtt",
18
+ ".map": "application/json",
19
+ ".webmanifest": "application/manifest+json",
20
+ ".png": "image/png",
21
+ ".apng": "image/apng",
22
+ ".jpg": "image/jpeg",
23
+ ".jpeg": "image/jpeg",
24
+ ".gif": "image/gif",
25
+ ".svg": "image/svg+xml",
26
+ ".ico": "image/x-icon",
27
+ ".webp": "image/webp",
28
+ ".avif": "image/avif",
29
+ ".bmp": "image/bmp",
30
+ ".tiff": "image/tiff",
31
+ ".txt": "text/plain",
32
+ ".pdf": "application/pdf",
33
+ ".wasm": "application/wasm",
34
+ ".wav": "audio/wav",
35
+ ".mp3": "audio/mpeg",
36
+ ".ogg": "audio/ogg",
37
+ ".aac": "audio/aac",
38
+ ".weba": "audio/webm",
39
+ ".flac": "audio/flac",
40
+ ".opus": "audio/opus",
41
+ ".m4a": "audio/mp4",
42
+ ".mp4": "video/mp4",
43
+ ".m4v": "video/mp4",
44
+ ".webm": "video/webm",
45
+ ".ogv": "video/ogg",
46
+ ".m3u8": "application/vnd.apple.mpegurl",
47
+ ".woff": "font/woff",
48
+ ".woff2": "font/woff2",
49
+ ".ttf": "font/ttf",
50
+ ".otf": "font/otf",
51
+ ".eot": "application/vnd.ms-fontobject",
52
+ };
53
+ /**
54
+ * Registers a MIME type for a file extension
55
+ */
56
+ export function registerMimeType(extension, contentType) {
57
+ const ext = extension.trim().toLowerCase();
58
+ mimeTypes[ext.startsWith(".") ? ext : "." + ext] = contentType;
59
+ }
60
+ const textualTypeRe = /^(?:text\/|application\/(?:json5?|javascript|xml|yaml|toml)$|[\w.-]+\/[\w.-]+\+(?:json|xml)$)/;
61
+ export function isTextualContentType(contentType) {
62
+ const type = contentType.split(";")[0].trim().toLowerCase();
63
+ return type === "image/svg+xml" || textualTypeRe.test(type);
64
+ }
65
+ export function getContentType(filePath, utf8 = true) {
66
+ let contentType = mimeTypes[path.extname(filePath).toLowerCase()] ??
67
+ "application/octet-stream";
68
+ if (utf8 && !contentType.includes(";") && isTextualContentType(contentType))
69
+ contentType += "; charset=utf-8";
70
+ return contentType;
71
+ }
package/dist/helpers.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Cookies, FFRequest } from "./types.js";
2
+ export { getContentType } from "./encoding.js";
2
3
  export declare function parseCookies(cookieHeader: string): Cookies;
3
4
  export declare function getIP(req: FFRequest): string;
4
- export declare function getContentType(filePath: string, utf8?: boolean): string;
5
5
  export declare const ensureLeadingDot: (str: string) => string;
package/dist/helpers.js CHANGED
@@ -1,4 +1,4 @@
1
- import path from "path";
1
+ export { getContentType } from "./encoding.js";
2
2
  export function parseCookies(cookieHeader) {
3
3
  const cookies = Object.create(null);
4
4
  cookieHeader.split(";").forEach(cookie => {
@@ -17,32 +17,4 @@ export function getIP(req) {
17
17
  }
18
18
  return req.socket.remoteAddress || "";
19
19
  }
20
- const mimeTypes = {
21
- ".html": "text/html",
22
- ".css": "text/css",
23
- ".js": "application/javascript",
24
- ".json": "application/json",
25
- ".json5": "application/json5",
26
- ".yaml": "application/yaml",
27
- ".yml": "application/yaml",
28
- ".toml": "application/toml",
29
- ".png": "image/png",
30
- ".jpg": "image/jpeg",
31
- ".jpeg": "image/jpeg",
32
- ".gif": "image/gif",
33
- ".svg": "image/svg+xml",
34
- ".ico": "image/x-icon",
35
- ".txt": "text/plain",
36
- ".pdf": "application/pdf",
37
- };
38
- function _getContentType(filePath) {
39
- const ext = path.extname(filePath).toLowerCase();
40
- return mimeTypes[ext] || "application/octet-stream";
41
- }
42
- export function getContentType(filePath, utf8 = false) {
43
- let contentType = _getContentType(filePath);
44
- if (utf8)
45
- contentType += "; charset=utf-8";
46
- return contentType;
47
- }
48
20
  export const ensureLeadingDot = (str) => str.startsWith(".") ? str : "." + str;
package/dist/render.d.ts CHANGED
@@ -10,3 +10,5 @@ export interface RenderHTMLOptions {
10
10
  _renderedPaths?: string[];
11
11
  }
12
12
  export declare function renderHTML(options: RenderHTMLOptions): string;
13
+ export declare function interpolate(template: string, data: Record<string, any>): string;
14
+ export declare function expandLoops(template: string, data: Record<string, any>): string;
package/dist/render.js CHANGED
@@ -28,10 +28,21 @@ export function renderHTML(options) {
28
28
  ...templateData,
29
29
  ...data,
30
30
  };
31
+ // Preliminary loop rendering
32
+ template = expandLoops(template, data);
31
33
  // Inserting data, e.g. {{name}}
32
- template = template.replace(/{{(.*?)}}/g, (_, key) => data[key.trim()] || "");
34
+ template = interpolate(template, data);
33
35
  // Loading partials, e.g. <!-- include header -->
34
- template = template.replace(/<!--\s*include\s*(.*?)\s*-->/g, (_, partialName) => {
36
+ template = template.replace(/<!--\s*include\s+([^\s{]+)(?:\s+(\{[\s\S]*?\}))?\s*-->/g, (_, partialName, json) => {
37
+ let includeData = {};
38
+ if (json) {
39
+ try {
40
+ includeData = JSON.parse(json);
41
+ }
42
+ catch (error) {
43
+ return "<!-- Invalid include data -->";
44
+ }
45
+ }
35
46
  const partialPath = path.join(path.dirname(templatePath), partialName + ".html");
36
47
  return renderHTML({
37
48
  templatePath: partialPath,
@@ -40,7 +51,10 @@ export function renderHTML(options) {
40
51
  realPath,
41
52
  ],
42
53
  noLayout: true,
43
- data,
54
+ data: {
55
+ ...data,
56
+ ...includeData,
57
+ },
44
58
  FF,
45
59
  FFVar,
46
60
  });
@@ -87,3 +101,36 @@ export function renderHTML(options) {
87
101
  return `<-- Template ${options.templatePath} has errors -->`;
88
102
  }
89
103
  }
104
+ function resolvePath(data, key) {
105
+ let value = data;
106
+ for (const part of key.split(".")) {
107
+ if (value == null)
108
+ return undefined;
109
+ value = value[part];
110
+ }
111
+ return value;
112
+ }
113
+ export function interpolate(template, data) {
114
+ return template.replace(/{{(.*?)}}/g, (_, key) => resolvePath(data, key.trim()) ?? "");
115
+ }
116
+ export function expandLoops(template, data) {
117
+ return template
118
+ .replace(/<!--\s*each\s+(\w+)\s+as\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/each\s*-->/g, (_, key, itemVar, body) => (resolvePath(data, key) ?? [])
119
+ .map((value, index) => interpolate(body, {
120
+ ...data,
121
+ [itemVar]: value,
122
+ i: index,
123
+ }))
124
+ .join(""))
125
+ .replace(/<!--\s*range\s+(\w+)\s*-->([\s\S]*?)<!--\s*\/range\s*-->/g, (_, key, body) => {
126
+ const range = Array.from({
127
+ length: +key,
128
+ }, (_, i) => i);
129
+ return range
130
+ .map((i) => interpolate(body, {
131
+ ...data,
132
+ i,
133
+ }))
134
+ .join("");
135
+ });
136
+ }
package/dist/res.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { createReadStream } from "fs";
2
2
  import http from "http";
3
3
  import path from "path";
4
- import { ensureLeadingDot, getContentType } from "./helpers.js";
4
+ import { ensureLeadingDot } from "./helpers.js";
5
+ import { getContentType } from "./encoding.js";
5
6
  export class FFResponse extends http.ServerResponse {
6
7
  _ended = false;
7
8
  FF;
@@ -98,7 +99,7 @@ export class FFResponse extends http.ServerResponse {
98
99
  */
99
100
  sendFile(filePath, contentType) {
100
101
  if (contentType === "utf8")
101
- contentType = getContentType(filePath);
102
+ contentType = getContentType(filePath, true);
102
103
  this.ct(contentType || getContentType(filePath));
103
104
  createReadStream(filePath).pipe(this);
104
105
  this._ended = true;
package/dist/static.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import { ensureLeadingDot, getContentType } from "./helpers.js";
3
+ import { ensureLeadingDot } from "./helpers.js";
4
+ import { getContentType } from "./encoding.js";
4
5
  export function handleSingleFile(filePath, opts) {
5
6
  opts = {
6
7
  utf8: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.9.6",
3
+ "version": "0.9.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",