@wxn0brp/falcon-frame 0.5.7 → 0.5.9

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/README.md CHANGED
@@ -97,7 +97,7 @@ app.post("/logout", (req, res) => {
97
97
  });
98
98
 
99
99
  // if no route matches
100
- app.all("*", (req, res) => {
100
+ app.set404((req, res) => {
101
101
  res.status(404).json({ error: "Not found" });
102
102
  });
103
103
 
@@ -1,4 +1,4 @@
1
1
  import { FFResponse } from "./res.js";
2
- import { FFRequest } from "./types.js";
2
+ import { FFRequest, RouteHandler } from "./types.js";
3
3
  export declare function compression(req: FFRequest, res: FFResponse): void;
4
- export declare function compressionMiddleware(): (req: FFRequest, res: FFResponse, next: () => void) => void;
4
+ export declare const compressionMiddleware: RouteHandler;
package/dist/compress.js CHANGED
@@ -27,29 +27,54 @@ export function compression(req, res) {
27
27
  compressionStream.on("end", () => {
28
28
  originalEnd.call(res);
29
29
  });
30
+ compressionStream.on("error", (err) => {
31
+ if (!res.headersSent) {
32
+ res.statusCode = 500;
33
+ originalEnd.call(res);
34
+ }
35
+ else {
36
+ res.destroy(err);
37
+ }
38
+ });
39
+ res.on("close", () => {
40
+ compressionStream.destroy();
41
+ });
30
42
  // @ts-ignore
31
- res.write = (chunk, encoding) => {
43
+ res.write = (...args) => {
44
+ const [chunk, encoding, cb] = args;
45
+ const callback = typeof encoding === "function" ? encoding : cb;
46
+ const enc = typeof encoding === "function" ? undefined : encoding;
32
47
  if (!res.headersSent) {
33
48
  res.writeHead(res.statusCode);
34
49
  }
35
- return compressionStream.write(chunk, encoding);
50
+ return compressionStream.write(chunk, enc, callback);
36
51
  };
37
52
  // @ts-ignore
38
- res.end = (chunk, encoding) => {
39
- if (res.writableEnded) {
53
+ res.end = (...args) => {
54
+ if (res.writableEnded)
40
55
  return res;
56
+ const [chunk, encoding, cb] = args;
57
+ let finalChunk = chunk;
58
+ let finalCb = cb;
59
+ let finalEncoding = encoding;
60
+ if (typeof chunk === "function") {
61
+ finalCb = chunk;
62
+ finalChunk = undefined;
63
+ }
64
+ else if (typeof encoding === "function") {
65
+ finalCb = encoding;
66
+ finalEncoding = undefined;
41
67
  }
42
- if (chunk) {
43
- // @ts-ignore
44
- res.write(chunk, encoding);
68
+ if (finalCb)
69
+ res.once("finish", finalCb);
70
+ if (finalChunk) {
71
+ res.write(finalChunk, finalEncoding);
45
72
  }
46
73
  compressionStream.end();
47
74
  return res;
48
75
  };
49
76
  }
50
- export function compressionMiddleware() {
51
- return (req, res, next) => {
52
- compression(req, res);
53
- next();
54
- };
55
- }
77
+ export const compressionMiddleware = (req, res, next) => {
78
+ compression(req, res);
79
+ next();
80
+ };
package/dist/index.d.ts CHANGED
@@ -16,12 +16,14 @@ export declare class FalconFrame<Vars extends Record<string, any> = any> extends
16
16
  vars: Vars;
17
17
  opts: Opts;
18
18
  engines: Record<string, EngineCallback>;
19
+ _404: RouteHandler;
19
20
  constructor(opts?: Partial<Opts>);
20
21
  addBodyParser(parser: RouteHandler): this;
21
22
  listen(port: number | string, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
22
23
  getApp(beforeHandleRequest?: BeforeHandleRequest): (req: any, res: any) => Promise<void>;
23
24
  engine(ext: string, callback: EngineCallback): this;
24
25
  setVar(key: keyof Vars, value: typeof this.vars[keyof Vars]): void;
26
+ set(key: keyof Vars, value: typeof this.vars[keyof Vars]): void;
25
27
  getVar(key: string): typeof this.vars[keyof Vars];
26
28
  /**
27
29
  * Sets the allowed origins for CORS.
@@ -38,6 +40,7 @@ export declare class FalconFrame<Vars extends Record<string, any> = any> extends
38
40
  * @returns The server object returned by the listen method.
39
41
  */
40
42
  l(port: number): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
43
+ set404(handler: RouteHandler): void;
41
44
  }
42
45
  export default FalconFrame;
43
46
  export * as Helpers from "./helpers.js";
package/dist/index.js CHANGED
@@ -12,6 +12,9 @@ export class FalconFrame extends Router {
12
12
  vars = {};
13
13
  opts = {};
14
14
  engines = {};
15
+ _404 = (req, res) => {
16
+ res.end("404: File had second thoughts");
17
+ };
15
18
  constructor(opts = {}) {
16
19
  super();
17
20
  const loggerOpts = opts?.loggerOpts || {};
@@ -89,6 +92,10 @@ export class FalconFrame extends Router {
89
92
  // @ts-ignore
90
93
  this.vars[key] = value;
91
94
  }
95
+ set(key, value) {
96
+ // @ts-ignore
97
+ this.vars[key] = value;
98
+ }
92
99
  getVar(key) {
93
100
  // @ts-ignore
94
101
  return this.vars[key];
@@ -112,6 +119,9 @@ export class FalconFrame extends Router {
112
119
  l(port) {
113
120
  return this.listen(+process.env.PORT || port, true);
114
121
  }
122
+ set404(handler) {
123
+ this._404 = handler;
124
+ }
115
125
  }
116
126
  export default FalconFrame;
117
127
  export * as Helpers from "./helpers.js";
package/dist/req.js CHANGED
@@ -36,13 +36,16 @@ export function handleRequest(req, res, FF) {
36
36
  logger.debug("Matched middlewares: " +
37
37
  matchedMiddlewares.map((middleware) => middleware.path).join(", "));
38
38
  if (matchedMiddlewares.length === 0) {
39
- res.status(404).end("404: File had second thoughts");
39
+ res.status(404);
40
+ FF._404(req, res);
40
41
  return;
41
42
  }
42
43
  let middlewareIndex = 0;
43
44
  async function next() {
44
45
  if (middlewareIndex >= matchedMiddlewares.length) {
45
- return res.status(404).end("404: File had second thoughts");
46
+ res.status(404);
47
+ FF._404(req, res);
48
+ return;
46
49
  }
47
50
  const middleware = matchedMiddlewares[middlewareIndex++];
48
51
  logger.debug(`Executing middleware ${middlewareIndex} of ${matchedMiddlewares.length} matched for path [${middleware.path}]`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",