@wxn0brp/falcon-frame 0.0.12 → 0.0.14

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/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Logger, LoggerOptions } from "@wxn0brp/lucerna-log";
2
2
  import http from "http";
3
3
  import { FFResponse } from "./res.js";
4
- import { AfterHandleRequest, FFRequest, Method, Middleware, RouteHandler } from "./types.js";
4
+ import { AfterHandleRequest as BeforeHandleRequest, FFRequest, Method, Middleware, RouteHandler } from "./types.js";
5
5
  import { renderHTML } from "./render.js";
6
6
  import { PluginSystem } from "./plugins.js";
7
7
  export declare class FalconFrame {
@@ -16,8 +16,8 @@ export declare class FalconFrame {
16
16
  delete(path: string, ...handlers: RouteHandler[]): void;
17
17
  all(path: string, ...handlers: RouteHandler[]): void;
18
18
  static(apiPath: string, dirPath: string): void;
19
- listen(port: number, callback?: () => void, afterHandleRequest?: AfterHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
20
- getApp(afterHandleRequest?: AfterHandleRequest): (req: any, res: any) => any;
19
+ listen(port: number, callback?: () => void, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
20
+ getApp(beforeHandleRequest?: BeforeHandleRequest): (req: any, res: any) => Promise<void>;
21
21
  }
22
22
  export default FalconFrame;
23
23
  export { FFResponse, FFRequest, RouteHandler, renderHTML, PluginSystem, };
package/dist/index.js CHANGED
@@ -54,19 +54,19 @@ export class FalconFrame {
54
54
  middleware: handleStaticFiles(apiPath, dirPath)
55
55
  });
56
56
  }
57
- listen(port, callback, afterHandleRequest) {
58
- const server = http.createServer(this.getApp(afterHandleRequest));
57
+ listen(port, callback, beforeHandleRequest) {
58
+ const server = http.createServer(this.getApp(beforeHandleRequest));
59
59
  server.listen(port, callback);
60
60
  return server;
61
61
  }
62
- getApp(afterHandleRequest) {
63
- return (req, res) => {
64
- if (afterHandleRequest) {
65
- const result = afterHandleRequest(req, res);
66
- if (result)
67
- return result;
62
+ getApp(beforeHandleRequest) {
63
+ return async (req, res) => {
64
+ if (beforeHandleRequest) {
65
+ const result = await beforeHandleRequest(req, res);
66
+ if (result || res._ended)
67
+ return;
68
68
  }
69
- handleRequest(req, res, this);
69
+ await handleRequest(req, res, this);
70
70
  };
71
71
  }
72
72
  }
@@ -16,16 +16,20 @@ export function createCORSPlugin(allowedOrigins, opts = {}) {
16
16
  if (allowedOrigins.includes("*")) {
17
17
  res.setHeader("Access-Control-Allow-Origin", "*");
18
18
  setHeader(res, opts);
19
+ if (req.method === "OPTIONS") {
20
+ res.statusCode = 204;
21
+ return res.end();
22
+ }
19
23
  return next();
20
24
  }
21
25
  const origin = req.headers.origin;
22
26
  if (origin && allowedOrigins.includes(origin)) {
23
27
  res.setHeader("Access-Control-Allow-Origin", origin);
24
28
  setHeader(res, opts);
25
- }
26
- if (req.method === "OPTIONS") {
27
- res.statusCode = 204;
28
- return res.end();
29
+ if (req.method === "OPTIONS") {
30
+ res.statusCode = 204;
31
+ return res.end();
32
+ }
29
33
  }
30
34
  next();
31
35
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",