@wxn0brp/falcon-frame 0.5.4 → 0.5.6

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,3 @@
1
+ import { FFResponse } from "./res.js";
2
+ import { FFRequest } from "./types.js";
3
+ export declare function compressionMiddleware(req: FFRequest, res: FFResponse): void;
@@ -0,0 +1,49 @@
1
+ import { createGzip, createDeflate } from "zlib";
2
+ export function compressionMiddleware(req, res) {
3
+ const encoding = req.headers["accept-encoding"];
4
+ if (!encoding || typeof encoding !== "string" || res.headersSent) {
5
+ return;
6
+ }
7
+ let compressionStream;
8
+ let encodingType;
9
+ if (/\bgzip\b/.test(encoding)) {
10
+ encodingType = "gzip";
11
+ compressionStream = createGzip();
12
+ }
13
+ else if (/\bdeflate\b/.test(encoding)) {
14
+ encodingType = "deflate";
15
+ compressionStream = createDeflate();
16
+ }
17
+ else {
18
+ return;
19
+ }
20
+ res.setHeader("Content-Encoding", encodingType);
21
+ res.removeHeader("Content-Length");
22
+ const originalWrite = res.write;
23
+ const originalEnd = res.end;
24
+ compressionStream.on("data", (chunk) => {
25
+ originalWrite.call(res, chunk);
26
+ });
27
+ compressionStream.on("end", () => {
28
+ originalEnd.call(res);
29
+ });
30
+ // @ts-ignore
31
+ res.write = (chunk, encoding) => {
32
+ if (!res.headersSent) {
33
+ res.writeHead(res.statusCode);
34
+ }
35
+ return compressionStream.write(chunk, encoding);
36
+ };
37
+ // @ts-ignore
38
+ res.end = (chunk, encoding) => {
39
+ if (res.writableEnded) {
40
+ return res;
41
+ }
42
+ if (chunk) {
43
+ // @ts-ignore
44
+ res.write(chunk, encoding);
45
+ }
46
+ compressionStream.end();
47
+ return res;
48
+ };
49
+ }
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
@@ -3,6 +3,7 @@ import { parseCookies } from "./helpers.js";
3
3
  import { FFResponse } from "./res.js";
4
4
  import { validate } from "./valid.js";
5
5
  import { getMiddlewares, matchMiddleware } from "./middleware.js";
6
+ import { compressionMiddleware } from "./compress.js";
6
7
  export function handleRequest(req, res, FF) {
7
8
  Object.setPrototypeOf(res, FFResponse.prototype);
8
9
  res.FF = FF;
@@ -39,6 +40,8 @@ export function handleRequest(req, res, FF) {
39
40
  res.status(404).end("404: File had second thoughts");
40
41
  return;
41
42
  }
43
+ if (!FF.getVar("disable compression"))
44
+ compressionMiddleware(req, res);
42
45
  let middlewareIndex = 0;
43
46
  async function next() {
44
47
  if (middlewareIndex >= matchedMiddlewares.length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",