@wxn0brp/falcon-frame 0.9.4 → 0.9.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.
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import http from "http";
3
3
  import { renderHTML } from "./render.js";
4
4
  import { FFResponse } from "./res.js";
5
5
  import { Router } from "./router.js";
6
- import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, EngineCallback, ErrorHandler, FFOpts, FFRequest, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
6
+ import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, EngineCallback, ErrorHandler, FFOpts, FFRequest, FinalHandler, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
7
7
  export declare class FalconFrame<Vars extends Record<string, any> = {}> extends Router {
8
8
  logger: Logger;
9
9
  bodyParsers: Record<string, BodyParserEntry>;
@@ -11,8 +11,8 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
11
11
  opts: FFOpts;
12
12
  engines: Record<string, EngineCallback>;
13
13
  _400_formatter: ValidationErrorFormatter;
14
- _404: RouteHandler;
15
- _413: RouteHandler;
14
+ _404: FinalHandler;
15
+ _413: FinalHandler;
16
16
  _500: ErrorHandler;
17
17
  constructor(opts?: Partial<FFOpts>);
18
18
  addBodyParser(contentType: string, parser: ParseBodyFunction, opts?: StandardBodyParserOptions): this;
@@ -38,13 +38,14 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
38
38
  */
39
39
  l(port: number): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
40
40
  set400Formatter(formatter: ValidationErrorFormatter): void;
41
- set404(handler: RouteHandler): void;
42
- set413(handler: RouteHandler): void;
41
+ set404(handler: FinalHandler): void;
42
+ set413(handler: FinalHandler): void;
43
43
  set500(handler: ErrorHandler): void;
44
44
  }
45
45
  export default FalconFrame;
46
- export { OfflineEngine, OfflineRequest, OfflineResponse } from "./engine.js";
46
+ export { OfflineEngine } from "./engine.js";
47
+ export type { OfflineRequest, OfflineResponse } from "./engine.js";
47
48
  export * as Helpers from "./helpers.js";
48
49
  export type { FFOpts as Opts } from "./types.js";
49
50
  export { validateBody } from "./valid.js";
50
- export { FFRequest, FFResponse, renderHTML, RouteHandler, Router };
51
+ export { FFRequest, FFResponse, FinalHandler, renderHTML, RouteHandler, Router, };
package/dist/index.js CHANGED
@@ -195,4 +195,4 @@ export default FalconFrame;
195
195
  export { OfflineEngine } from "./engine.js";
196
196
  export * as Helpers from "./helpers.js";
197
197
  export { validateBody } from "./valid.js";
198
- export { FFResponse, renderHTML, Router };
198
+ export { FFResponse, renderHTML, Router, };
package/dist/router.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SSEManager } from "./sse.js";
2
- import { Method, Middleware, RouteHandler, StaticServeOptions } from "./types.js";
2
+ import { Method, Middleware, RouteHandler, StaticFileOptions, StaticServeOptions } from "./types.js";
3
3
  export type MiddlewareFn = RouteHandler | Router | {
4
4
  getRouteHandler(): RouteHandler;
5
5
  };
@@ -14,6 +14,7 @@ export declare class Router {
14
14
  patch(path: string, ...handlers: RouteHandler[]): this;
15
15
  all(path: string, ...handlers: RouteHandler[]): this;
16
16
  static(apiPath: string, dirPath?: string, opts?: StaticServeOptions): this;
17
+ file(routePath: string, filePath: string, opts?: StaticFileOptions): this;
17
18
  sse(path: string, ...handlers: RouteHandler[]): SSEManager;
18
19
  customParser(path: string, handler: RouteHandler, method?: Method): this;
19
20
  router(path?: string): Router;
package/dist/router.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SSEManager } from "./sse.js";
2
- import { handleStaticFiles } from "./static.js";
2
+ import { handleSingleFile, handleStaticFiles } from "./static.js";
3
3
  export class Router {
4
4
  middlewares = [];
5
5
  addRoute(method, path, ...handlers) {
@@ -68,6 +68,10 @@ export class Router {
68
68
  this.use(apiPath, handleStaticFiles(dirPath, opts));
69
69
  return this;
70
70
  }
71
+ file(routePath, filePath, opts = {}) {
72
+ this.use(routePath, handleSingleFile(filePath, opts));
73
+ return this;
74
+ }
71
75
  sse(path, ...handlers) {
72
76
  const lastHandler = handlers.pop() || (() => { });
73
77
  const manager = new SSEManager();
package/dist/static.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- import { RouteHandler, StaticServeOptions } from "./types.js";
1
+ import { RouteHandler, StaticFileOptions, StaticServeOptions } from "./types.js";
2
+ export declare function handleSingleFile(filePath: string, opts: StaticFileOptions): RouteHandler;
2
3
  export declare function handleStaticFiles(dirPath: string, opts: StaticServeOptions): RouteHandler;
package/dist/static.js CHANGED
@@ -1,6 +1,44 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
  import { ensureLeadingDot, getContentType } from "./helpers.js";
4
+ export function handleSingleFile(filePath, opts) {
5
+ opts = {
6
+ utf8: true,
7
+ etag: true,
8
+ errorIfFileNotFound: true,
9
+ ...opts,
10
+ };
11
+ if (opts.errorIfFileNotFound && !fs.existsSync(filePath))
12
+ throw new Error(`File ${filePath} does not exist`);
13
+ return (req, res, next) => {
14
+ if (req.method !== "GET" && req.method !== "HEAD")
15
+ return next();
16
+ let stats;
17
+ try {
18
+ stats = fs.statSync(filePath);
19
+ }
20
+ catch (e) {
21
+ res.statusCode = 404;
22
+ res.FF._404(req, res);
23
+ return;
24
+ }
25
+ if (opts.etag) {
26
+ const etag = `W/"${stats.size}-${stats.mtime.getTime()}"`;
27
+ if (req.headers["if-none-match"] === etag) {
28
+ res.status(304).end();
29
+ return;
30
+ }
31
+ res.setHeader("ETag", etag);
32
+ }
33
+ res.ct(getContentType(filePath, opts.utf8));
34
+ if (req.method === "HEAD") {
35
+ res.setHeader("Content-Length", stats.size);
36
+ res.end();
37
+ return;
38
+ }
39
+ fs.createReadStream(filePath).pipe(res);
40
+ };
41
+ }
4
42
  export function handleStaticFiles(dirPath, opts) {
5
43
  opts = {
6
44
  utf8: true,
package/dist/types.d.ts CHANGED
@@ -2,7 +2,8 @@ import { LoggerOptions } from "@wxn0brp/lucerna-log";
2
2
  import FalconFrame from "./index.js";
3
3
  import { FFResponse } from "./res.js";
4
4
  import http from "http";
5
- export type RouteHandler = (req: FFRequest, res: FFResponse, next?: () => void) => void | any;
5
+ export type RouteHandler = (req: FFRequest, res: FFResponse, next: () => void) => void | any;
6
+ export type FinalHandler = (req: FFRequest, res: FFResponse) => void | any;
6
7
  export type ErrorHandler = (err: Error, req: FFRequest, res: FFResponse) => void | any;
7
8
  export type Method = "get" | "post" | "put" | "delete" | "patch" | "all";
8
9
  export interface Params {
@@ -76,6 +77,11 @@ export interface StaticServeOptions {
76
77
  errorIfDirNotFound?: boolean;
77
78
  notRenderHtml?: boolean;
78
79
  }
80
+ export interface StaticFileOptions {
81
+ utf8?: boolean;
82
+ etag?: boolean;
83
+ errorIfFileNotFound?: boolean;
84
+ }
79
85
  export type EngineCallback = (path: string, data: any, callback: (e: any, rendered?: string) => void, FF?: FalconFrame) => void;
80
86
  export interface RenderOptions {
81
87
  notUseViews?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.9.4",
3
+ "version": "0.9.6",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",