ovr 4.6.0 → 6.0.0

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.
Files changed (90) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +55 -103
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +67 -145
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -8
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -7
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +37 -52
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -197
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +341 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +31 -26
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +71 -80
  33. package/dist/util/index.d.ts +38 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +88 -0
  36. package/package.json +4 -4
  37. package/src/app/index.ts +128 -265
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -8
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -222
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +486 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +90 -107
  48. package/src/util/index.ts +112 -0
  49. package/dist/app/context.d.ts +0 -147
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -283
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -55
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -96
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/app/mw/csrf.d.ts +0 -23
  65. package/dist/app/mw/csrf.d.ts.map +0 -1
  66. package/dist/app/mw/csrf.js +0 -30
  67. package/dist/jsx/chunk/index.d.ts +0 -38
  68. package/dist/jsx/chunk/index.d.ts.map +0 -1
  69. package/dist/jsx/chunk/index.js +0 -69
  70. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  71. package/dist/jsx/merge-async-generators.d.ts +0 -14
  72. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  73. package/dist/jsx/merge-async-generators.js +0 -42
  74. package/dist/types/index.d.ts +0 -17
  75. package/dist/types/index.d.ts.map +0 -1
  76. package/dist/util/hash.d.ts +0 -11
  77. package/dist/util/hash.d.ts.map +0 -1
  78. package/dist/util/hash.js +0 -16
  79. package/src/app/context.ts +0 -365
  80. package/src/app/helper/get.ts +0 -46
  81. package/src/app/helper/index.ts +0 -174
  82. package/src/app/helper/post.ts +0 -54
  83. package/src/app/memo/index.ts +0 -40
  84. package/src/app/mw/csrf.ts +0 -32
  85. package/src/jsx/chunk/index.ts +0 -79
  86. package/src/jsx/merge-async-generators.ts +0 -47
  87. package/src/types/index.ts +0 -42
  88. package/src/util/hash.ts +0 -17
  89. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  90. /package/dist/{types → middleware}/index.js +0 -0
@@ -0,0 +1,26 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ross Robino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ ---
12
+
13
+ Portions of this software have been adapted from the following projects, which are also licensed under the MIT License:
14
+
15
+ - Middleware composition - https://github.com/koajs/compose
16
+ Copyright (c) 2013 TJ Holowaychuk <tj@apex.sh>
17
+ - djb2 hash util - https://github.com/sveltejs/kit
18
+ Copyright (c) 2020 SvelteKit contributors
19
+ - Buffer search and HTML escape replacer - https://github.com/remix-run/remix
20
+ Copyright (c) 2025 Shopify Inc.
21
+ - Trie router - https://github.com/SaltyAom/memoirist
22
+ Copyright (c) 2023 saltyAom
23
+ Copyright (c) 2019 Medley
24
+ - Cookie handling - https://github.com/jshttp/cookie
25
+ Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
26
+ Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
@@ -1,116 +1,68 @@
1
- import { type Params } from "../trie/index.js";
2
- import type { DeepArray, ExtractMultiParams, ExtractParams } from "../types/index.js";
3
- import { Context } from "./context.js";
4
- import { Get } from "./helper/get.js";
5
- import { Post } from "./helper/post.js";
6
- import { AsyncLocalStorage } from "node:async_hooks";
7
- export type Middleware<P extends Params = Params> = (context: Context<P>, next: () => Promise<void>) => any;
8
- export type TrailingSlash = "always" | "never" | "ignore";
9
- type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
10
- type UnmatchedContext<P extends Params = Params> = Omit<Context<P>, "route"> & Partial<Pick<Context<P>, "route">>;
11
- export type ErrorHandler<P extends Params = Params> = ((context: UnmatchedContext<P>, error: unknown) => any) | null;
12
- export type NotFoundHandler<P extends Params = Params> = (context: UnmatchedContext<P>) => any;
1
+ import type { Middleware } from "../middleware/index.js";
2
+ import type { Multipart } from "../multipart/index.js";
3
+ import { Route } from "../route/index.js";
4
+ import type { Util } from "../util/index.js";
5
+ export declare namespace App {
6
+ namespace Options {
7
+ /** Trailing slash preference */
8
+ type TrailingSlash = "always" | "never" | "ignore";
9
+ }
10
+ /** Configuration options */
11
+ type Options = {
12
+ /**
13
+ * Basic
14
+ * [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
15
+ * protection that checks the request's `method`, and its
16
+ * [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
17
+ * and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
18
+ * headers.
19
+ *
20
+ * More robust protection requires a stateful server or a database to store
21
+ * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
22
+ *
23
+ * @default true
24
+ */
25
+ readonly csrf?: boolean;
26
+ /**
27
+ * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
28
+ * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
29
+ * - `"ignore"` - no redirects (not recommended, bad for SEO)
30
+ *
31
+ * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
32
+ *
33
+ * @default "never"
34
+ */
35
+ readonly trailingSlash?: Options.TrailingSlash;
36
+ /**
37
+ * Multipart form data options
38
+ * (overridden by options passed into `Context.form(options)`)
39
+ */
40
+ readonly form?: Multipart.Options;
41
+ };
42
+ }
43
+ /** Web server application */
13
44
  export declare class App {
14
45
  #private;
15
- [key: string | symbol | number]: any;
16
46
  /**
17
- * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
18
- * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
19
- * - `"ignore"` - no redirects (not recommended, bad for SEO)
20
- *
21
- * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
47
+ * Create a new application.
22
48
  *
23
- * @default "never"
49
+ * @param options configuration options
24
50
  */
25
- trailingSlash: TrailingSlash;
51
+ constructor(options?: App.Options);
26
52
  /**
27
- * Base HTML to inject the `head` and `page` elements into.
28
- *
29
- * @default
30
- *
31
- * ```html
32
- * <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
33
- * ```
34
- */
35
- base: string;
36
- /**
37
- * Assign a handler to run when an `Error` is thrown.
38
- *
39
- * If not set, `Error` will be thrown. This might be desired
40
- * if your server already includes error handling. Set in `config.start`
41
- * to handle errors globally.
42
- *
43
- * @default null
44
- */
45
- error: ErrorHandler;
46
- /**
47
- * Middleware to run when no `body` or `status` has been set on the `context`.
48
- * Set to a new function to override the default.
49
- *
50
- * @default
51
- *
52
- * ```ts
53
- * (c) => c.html("Not found", 404)
54
- * ```
55
- */
56
- notFound: NotFoundHandler;
57
- /** Stores context per request. */
58
- static storage: AsyncLocalStorage<Context<Params>>;
59
- /**
60
- * @param helpers `Helper` to add to `App`
53
+ * @param routes Route or middleware to use
61
54
  * @returns `App` instance
62
55
  */
63
- add(...helpers: DeepArray<Get | Post | Record<string, Get | Post>>[]): this;
56
+ use(...routes: Util.DeepArray<Route | Middleware | Record<string, Route | Middleware>>[]): this;
64
57
  /**
65
- * Add global middleware.
58
+ * Request a resource from the application.
66
59
  *
67
- * @param middleware
68
- * @returns `App` instance
69
- */
70
- use(...middleware: Middleware[]): this;
71
- /**
72
- * @param method HTTP method
73
- * @param pattern Route pattern
74
- * @param middleware
75
- * @returns `App` instance
76
- */
77
- on<Pattern extends string>(method: Method | Method[], pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
78
- /**
79
- * @param method HTTP method
80
- * @param patterns Array of route patterns
81
- * @param middleware
82
- * @returns `App` instance
83
- */
84
- on<Patterns extends string[]>(method: Method | Method[], patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
85
- /**
86
- * @param pattern Route pattern
87
- * @param middleware
88
- * @returns `App` instance
89
- */
90
- get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
91
- /**
92
- * @param patterns Array of route patterns
93
- * @param middleware
94
- * @returns `App` instance
95
- */
96
- get<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
97
- /**
98
- * @param pattern Route pattern
99
- * @param middleware
100
- * @returns the router instance
101
- */
102
- post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]): this;
103
- /**
104
- * @param patterns Array of route patterns
105
- * @param middleware
106
- * @returns the router instance
107
- */
108
- post<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<ExtractMultiParams<Patterns>>[]): this;
109
- /**
110
- * @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
111
- * @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
60
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch)
61
+ *
62
+ * @param resource [Resource](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#resource) to fetch
63
+ * @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
64
+ * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
112
65
  */
113
- fetch: (req: Request) => Promise<Response>;
66
+ readonly fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
114
67
  }
115
- export {};
116
68
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAe,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,CACnD,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACrB,GAAG,CAAC;AAET,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE1D,KAAK,MAAM,GACR,KAAK,GACL,MAAM,GACN,MAAM,GACN,KAAK,GACL,QAAQ,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U,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C,yBAAiB,GAAG,CAAC;IACpB,UAAiB,OAAO,CAAC;QACxB,gCAAgC;QAChC,KAAY,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;KAC1D;IAED,4BAA4B;IAC5B,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;WAYG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAExB;;;;;;;;WAQG;QACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC;QAE/C;;;WAGG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC;KAClC,CAAC;CACF;AAED,6BAA6B;AAC7B,qBAAa,GAAG;;IAUf;;;;OAIG;gBACS,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO;IAajC;;;OAGG;IACH,GAAG,CACF,GAAG,MAAM,EAAE,IAAI,CAAC,SAAS,CACxB,KAAK,GAAG,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,UAAU,CAAC,CACvD,EAAE;IAiBJ;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAK,GACb,UAAU,WAAW,GAAG,GAAG,EAC3B,UAAU,WAAW,uBAepB;CAkCF"}
package/dist/app/index.js CHANGED
@@ -1,168 +1,90 @@
1
- import { Route, Trie } from "../trie/index.js";
2
- import { Context } from "./context.js";
3
- import { Get } from "./helper/get.js";
4
- import { Post } from "./helper/post.js";
5
- import { AsyncLocalStorage } from "node:async_hooks";
1
+ import { Context } from "../context/index.js";
2
+ import { Route } from "../route/index.js";
3
+ import { Trie } from "../trie/index.js";
4
+ /** Web server application */
6
5
  export class App {
7
- /** Built tries per HTTP method. */
8
- #trieMap = new Map();
9
- /** Added routes per HTTP method. */
10
- #routesMap = new Map();
11
- /** Global middleware. */
12
- #use = [];
6
+ /** Route trie */
7
+ #trie = new Trie();
8
+ /** Global middleware */
9
+ #global = [];
10
+ /** App options */
11
+ #options;
13
12
  /**
14
- * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
15
- * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
16
- * - `"ignore"` - no redirects (not recommended, bad for SEO)
13
+ * Create a new application.
17
14
  *
18
- * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
19
- *
20
- * @default "never"
21
- */
22
- trailingSlash = "never";
23
- /**
24
- * Base HTML to inject the `head` and `page` elements into.
25
- *
26
- * @default
27
- *
28
- * ```html
29
- * <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
30
- * ```
31
- */
32
- base = '<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>';
33
- /**
34
- * Assign a handler to run when an `Error` is thrown.
35
- *
36
- * If not set, `Error` will be thrown. This might be desired
37
- * if your server already includes error handling. Set in `config.start`
38
- * to handle errors globally.
39
- *
40
- * @default null
15
+ * @param options configuration options
41
16
  */
42
- error = null;
43
- /**
44
- * Middleware to run when no `body` or `status` has been set on the `context`.
45
- * Set to a new function to override the default.
46
- *
47
- * @default
48
- *
49
- * ```ts
50
- * (c) => c.html("Not found", 404)
51
- * ```
52
- */
53
- notFound = (c) => c.html("Not found", 404);
54
- /** Stores context per request. */
55
- static storage = new AsyncLocalStorage();
17
+ constructor(options) {
18
+ this.#options = Object.assign({ csrf: true, trailingSlash: "never" }, options);
19
+ if (this.#options.csrf === true)
20
+ this.#global.push(App.#csrf);
21
+ if (this.#options.trailingSlash !== "ignore") {
22
+ this.#global.push(App.#createTrailingSlash(this.#options.trailingSlash));
23
+ }
24
+ }
56
25
  /**
57
- * @param helpers `Helper` to add to `App`
26
+ * @param routes Route or middleware to use
58
27
  * @returns `App` instance
59
28
  */
60
- add(...helpers) {
61
- for (const helper of helpers) {
62
- if (helper instanceof Array) {
63
- this.add(...helper);
29
+ use(...routes) {
30
+ for (const route of routes) {
31
+ if (route instanceof Route) {
32
+ this.#trie.add(route);
64
33
  }
65
- else if (helper instanceof Get) {
66
- this.get(helper.pattern, ...helper.middleware);
34
+ else if (route instanceof Array) {
35
+ this.use(...route);
67
36
  }
68
- else if (helper instanceof Post) {
69
- this.post(helper.pattern, ...helper.middleware);
37
+ else if (typeof route === "function") {
38
+ this.#global.push(route);
70
39
  }
71
40
  else {
72
- this.add(...Object.values(helper));
41
+ this.use(...Object.values(route));
73
42
  }
74
43
  }
75
44
  return this;
76
45
  }
77
46
  /**
78
- * Add global middleware.
47
+ * Request a resource from the application.
79
48
  *
80
- * @param middleware
81
- * @returns `App` instance
49
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch)
50
+ *
51
+ * @param resource [Resource](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#resource) to fetch
52
+ * @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
53
+ * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
82
54
  */
83
- use(...middleware) {
84
- this.#use.push(...middleware);
85
- return this;
86
- }
87
- on(method, pattern, ...middleware) {
88
- if (!Array.isArray(method))
89
- method = [method];
90
- let patterns;
91
- if (!Array.isArray(pattern))
92
- patterns = [pattern];
93
- else
94
- patterns = pattern;
95
- for (const p of patterns) {
96
- const route = new Route(p, middleware);
97
- for (const m of method) {
98
- const routes = this.#routesMap.get(m);
99
- if (routes)
100
- routes.push(route);
101
- else
102
- this.#routesMap.set(m, [route]);
103
- }
55
+ fetch = async (resource, options) => {
56
+ const c = new Context(new Request(resource, options), this.#options);
57
+ return Context.compose(Object.assign(c,
58
+ // match
59
+ this.#trie.find(
60
+ // use GET for HEAD requests to find the correct route
61
+ (c.req.method === "HEAD" ? "GET" : c.req.method) + c.url.pathname)), this.#global.concat(c.route?.middleware ?? []));
62
+ };
63
+ /** Basic CSRF middleware */
64
+ static #csrf(c, next) {
65
+ if (c.req.method === "GET" ||
66
+ c.req.method === "HEAD" ||
67
+ c.req.headers.get("sec-fetch-site") === "same-origin" ||
68
+ c.req.headers.get("origin") === c.url.origin) {
69
+ return next();
104
70
  }
105
- return this;
106
- }
107
- get(patternOrPatterns, ...middleware) {
108
- return this.on("GET", patternOrPatterns, ...middleware);
71
+ c.text("Forbidden", 403);
109
72
  }
110
- post(patternOrPatterns, ...middleware) {
111
- return this.on("POST", patternOrPatterns, ...middleware);
112
- }
113
- /**
114
- * @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
115
- * @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
116
- */
117
- fetch = (req) => {
118
- const c = new Context(req, new URL(req.url), this.trailingSlash, this.base, this.error, this.notFound);
119
- return App.storage.run(c, async () => {
120
- try {
121
- // check to see if the method is already built
122
- let trie = this.#trieMap.get(req.method);
123
- if (!trie) {
124
- // check if there are any routes with the method
125
- const routes = this.#routesMap.get(req.method);
126
- if (routes) {
127
- // build trie
128
- trie = new Trie();
129
- for (const route of routes)
130
- trie.add(route);
131
- this.#trieMap.set(req.method, trie);
132
- }
133
- }
134
- if (trie) {
135
- const match = trie.find(c.url.pathname);
136
- if (match) {
137
- Object.assign(c, match);
138
- const middleware = [...this.#use, ...match.route.store];
139
- // compose
140
- let i = -1;
141
- const dispatch = async (current) => {
142
- if (current <= i)
143
- throw new Error("next() called multiple times");
144
- i = current;
145
- if (middleware[current]) {
146
- const result = await middleware[current](c, () => dispatch(current + 1));
147
- if (result instanceof Response)
148
- c.res(result.body, result);
149
- else if (result instanceof ReadableStream)
150
- c.body = result;
151
- else if (result != null)
152
- c.page(result);
153
- }
154
- };
155
- await dispatch(0);
156
- }
157
- }
73
+ /** @returns Trailing slash middleware */
74
+ static #createTrailingSlash(mode) {
75
+ return async (c, next) => {
76
+ await next();
77
+ if (c.res.status && c.res.status !== 404)
78
+ return;
79
+ const last = c.url.pathname.at(-1);
80
+ if (mode === "always" && last !== "/") {
81
+ c.url.pathname += "/";
82
+ c.redirect(c.url, 308);
158
83
  }
159
- catch (error) {
160
- if (c.error)
161
- c.error(c, error);
162
- else
163
- throw error;
84
+ else if (mode === "never" && c.url.pathname !== "/" && last === "/") {
85
+ c.url.pathname = c.url.pathname.slice(0, -1);
86
+ c.redirect(c.url, 308);
164
87
  }
165
- return c.build();
166
- });
167
- };
88
+ };
89
+ }
168
90
  }
@@ -0,0 +1,138 @@
1
+ import type { App } from "../app/index.js";
2
+ import { Cookie } from "../cookie/index.js";
3
+ import { type Middleware } from "../middleware/index.js";
4
+ import { Multipart } from "../multipart/index.js";
5
+ import { Route } from "../route/index.js";
6
+ import { type Trie } from "../trie/index.js";
7
+ /** Properties to build the final `Response` with once middleware has run. */
8
+ declare class PreparedResponse {
9
+ /**
10
+ * `body` used to create the `Response`.
11
+ *
12
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body)
13
+ */
14
+ body?: BodyInit | null;
15
+ /**
16
+ * `status` used to create the `Response`.
17
+ *
18
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
19
+ */
20
+ status?: number;
21
+ /**
22
+ * `Headers` used to create the `Response`.
23
+ *
24
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
25
+ */
26
+ headers: Headers;
27
+ }
28
+ export declare namespace Context {
29
+ type Cookie = InstanceType<typeof Cookie>;
30
+ }
31
+ /**
32
+ * Request context.
33
+ *
34
+ * @template Params Parameters created from a route match
35
+ */
36
+ export declare class Context<Params extends Trie.Params = Trie.Params> {
37
+ #private;
38
+ /**
39
+ * Incoming `Request` to the server.
40
+ *
41
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
42
+ */
43
+ readonly req: Request;
44
+ /**
45
+ * Parsed `URL` created from `req.url`.
46
+ *
47
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
48
+ */
49
+ readonly url: URL;
50
+ /** Route pattern parameters */
51
+ readonly params: Params;
52
+ /** Matched `Route` instance */
53
+ readonly route?: Route;
54
+ /** Contains the arguments to used create the final `Response` */
55
+ readonly res: PreparedResponse;
56
+ /** Get, set, and delete cookies. */
57
+ readonly cookie: Cookie;
58
+ /**
59
+ * Creates a new `Context` with the current `Request`.
60
+ *
61
+ * @param req Request
62
+ */
63
+ constructor(req: Request, options?: App.Options);
64
+ /**
65
+ * Creates an HTML response.
66
+ *
67
+ * @param body HTML body
68
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
69
+ */
70
+ html(body: BodyInit | null, status?: number): void;
71
+ /**
72
+ * Creates a JSON response.
73
+ *
74
+ * @param data passed into JSON.stringify to create the body
75
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
76
+ */
77
+ json(data: unknown, status?: number): void;
78
+ /**
79
+ * Creates a plain text response.
80
+ *
81
+ * @param body response body
82
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
83
+ */
84
+ text(body: BodyInit, status?: number): void;
85
+ /**
86
+ * Creates a redirect response.
87
+ *
88
+ * @param location redirect `Location` header
89
+ * @param status HTTP status code
90
+ *
91
+ * - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
92
+ * - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
93
+ * - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
94
+ * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
95
+ * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
96
+ */
97
+ redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
98
+ /**
99
+ * Generates an etag from a hash of the string provided.
100
+ * If the etag matches, sets the response to not modified.
101
+ *
102
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
103
+ *
104
+ * @param string string to hash
105
+ * @returns `true` if the etag matches, `false` otherwise
106
+ */
107
+ etag(string: string): boolean;
108
+ /**
109
+ * Parse multipart requests.
110
+ *
111
+ * @yields Multipart request `Part`(s)
112
+ *
113
+ * @example
114
+ *
115
+ * ```ts
116
+ * import { Route } from "ovr";
117
+ *
118
+ * const post = Route.post(async (c) => {
119
+ * for await (const part of c.form()) {
120
+ * if (part.name === "email") {
121
+ * // ...
122
+ * }
123
+ * }
124
+ * })
125
+ * ```
126
+ */
127
+ form(options?: Multipart.Options): Multipart;
128
+ /**
129
+ * Composes a stack of `middleware` into a `Response`.
130
+ *
131
+ * @param c Context
132
+ * @param middleware stack to compose
133
+ * @returns constructed `Response`
134
+ */
135
+ static compose(c: Context, middleware: Middleware[]): Promise<Response>;
136
+ }
137
+ export {};
138
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAG7C,6EAA6E;AAC7E,cAAM,gBAAgB;IACrB;;;;OAIG;IACH,IAAI,CAA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