@wxn0brp/falcon-frame 0.9.4 → 0.9.5

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
@@ -43,7 +43,8 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
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";
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
@@ -76,6 +76,11 @@ export interface StaticServeOptions {
76
76
  errorIfDirNotFound?: boolean;
77
77
  notRenderHtml?: boolean;
78
78
  }
79
+ export interface StaticFileOptions {
80
+ utf8?: boolean;
81
+ etag?: boolean;
82
+ errorIfFileNotFound?: boolean;
83
+ }
79
84
  export type EngineCallback = (path: string, data: any, callback: (e: any, rendered?: string) => void, FF?: FalconFrame) => void;
80
85
  export interface RenderOptions {
81
86
  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.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",