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.
- package/dist/LICENSE.md +26 -0
- package/dist/app/index.d.ts +55 -103
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +67 -145
- package/dist/context/index.d.ts +138 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +218 -0
- package/dist/cookie/index.d.ts +121 -0
- package/dist/cookie/index.d.ts.map +1 -0
- package/dist/cookie/index.js +56 -0
- package/dist/index.d.ts +6 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -7
- package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
- package/dist/jsx/elements.d.ts.map +1 -0
- package/dist/jsx/index.d.ts +37 -52
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +71 -197
- package/dist/middleware/index.d.ts +22 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/multipart/index.d.ts +122 -0
- package/dist/multipart/index.d.ts.map +1 -0
- package/dist/multipart/index.js +341 -0
- package/dist/render/index.d.ts +78 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +248 -0
- package/dist/route/index.d.ts +147 -0
- package/dist/route/index.d.ts.map +1 -0
- package/dist/route/index.js +126 -0
- package/dist/trie/index.d.ts +31 -26
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +71 -80
- package/dist/util/index.d.ts +38 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +88 -0
- package/package.json +4 -4
- package/src/app/index.ts +128 -265
- package/src/context/index.ts +262 -0
- package/src/cookie/index.ts +189 -0
- package/src/index.ts +6 -8
- package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
- package/src/jsx/index.ts +81 -222
- package/src/middleware/index.ts +29 -0
- package/src/multipart/index.ts +486 -0
- package/src/render/index.ts +300 -0
- package/src/route/index.ts +332 -0
- package/src/trie/index.ts +90 -107
- package/src/util/index.ts +112 -0
- package/dist/app/context.d.ts +0 -147
- package/dist/app/context.d.ts.map +0 -1
- package/dist/app/context.js +0 -283
- package/dist/app/helper/get.d.ts +0 -30
- package/dist/app/helper/get.d.ts.map +0 -1
- package/dist/app/helper/get.js +0 -33
- package/dist/app/helper/index.d.ts +0 -55
- package/dist/app/helper/index.d.ts.map +0 -1
- package/dist/app/helper/index.js +0 -96
- package/dist/app/helper/post.d.ts +0 -35
- package/dist/app/helper/post.d.ts.map +0 -1
- package/dist/app/helper/post.js +0 -15
- package/dist/app/memo/index.d.ts +0 -12
- package/dist/app/memo/index.d.ts.map +0 -1
- package/dist/app/memo/index.js +0 -34
- package/dist/app/mw/csrf.d.ts +0 -23
- package/dist/app/mw/csrf.d.ts.map +0 -1
- package/dist/app/mw/csrf.js +0 -30
- package/dist/jsx/chunk/index.d.ts +0 -38
- package/dist/jsx/chunk/index.d.ts.map +0 -1
- package/dist/jsx/chunk/index.js +0 -69
- package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.d.ts +0 -14
- package/dist/jsx/merge-async-generators.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.js +0 -42
- package/dist/types/index.d.ts +0 -17
- package/dist/types/index.d.ts.map +0 -1
- package/dist/util/hash.d.ts +0 -11
- package/dist/util/hash.d.ts.map +0 -1
- package/dist/util/hash.js +0 -16
- package/src/app/context.ts +0 -365
- package/src/app/helper/get.ts +0 -46
- package/src/app/helper/index.ts +0 -174
- package/src/app/helper/post.ts +0 -54
- package/src/app/memo/index.ts +0 -40
- package/src/app/mw/csrf.ts +0 -32
- package/src/jsx/chunk/index.ts +0 -79
- package/src/jsx/merge-async-generators.ts +0 -47
- package/src/types/index.ts +0 -42
- package/src/util/hash.ts +0 -17
- /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
- /package/dist/{types → middleware}/index.js +0 -0
package/dist/LICENSE.md
ADDED
|
@@ -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>
|
package/dist/app/index.d.ts
CHANGED
|
@@ -1,116 +1,68 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
49
|
+
* @param options configuration options
|
|
24
50
|
*/
|
|
25
|
-
|
|
51
|
+
constructor(options?: App.Options);
|
|
26
52
|
/**
|
|
27
|
-
*
|
|
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
|
-
|
|
56
|
+
use(...routes: Util.DeepArray<Route | Middleware | Record<string, Route | Middleware>>[]): this;
|
|
64
57
|
/**
|
|
65
|
-
*
|
|
58
|
+
* Request a resource from the application.
|
|
66
59
|
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
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: (
|
|
66
|
+
readonly fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
|
|
114
67
|
}
|
|
115
|
-
export {};
|
|
116
68
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/app/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,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 {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
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
|
-
/**
|
|
8
|
-
#
|
|
9
|
-
/**
|
|
10
|
-
#
|
|
11
|
-
/**
|
|
12
|
-
#
|
|
6
|
+
/** Route trie */
|
|
7
|
+
#trie = new Trie();
|
|
8
|
+
/** Global middleware */
|
|
9
|
+
#global = [];
|
|
10
|
+
/** App options */
|
|
11
|
+
#options;
|
|
13
12
|
/**
|
|
14
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
26
|
+
* @param routes Route or middleware to use
|
|
58
27
|
* @returns `App` instance
|
|
59
28
|
*/
|
|
60
|
-
|
|
61
|
-
for (const
|
|
62
|
-
if (
|
|
63
|
-
this.add(
|
|
29
|
+
use(...routes) {
|
|
30
|
+
for (const route of routes) {
|
|
31
|
+
if (route instanceof Route) {
|
|
32
|
+
this.#trie.add(route);
|
|
64
33
|
}
|
|
65
|
-
else if (
|
|
66
|
-
this.
|
|
34
|
+
else if (route instanceof Array) {
|
|
35
|
+
this.use(...route);
|
|
67
36
|
}
|
|
68
|
-
else if (
|
|
69
|
-
this.
|
|
37
|
+
else if (typeof route === "function") {
|
|
38
|
+
this.#global.push(route);
|
|
70
39
|
}
|
|
71
40
|
else {
|
|
72
|
-
this.
|
|
41
|
+
this.use(...Object.values(route));
|
|
73
42
|
}
|
|
74
43
|
}
|
|
75
44
|
return this;
|
|
76
45
|
}
|
|
77
46
|
/**
|
|
78
|
-
*
|
|
47
|
+
* Request a resource from the application.
|
|
79
48
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
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
|
-
|
|
84
|
-
this.#
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
106
|
-
}
|
|
107
|
-
get(patternOrPatterns, ...middleware) {
|
|
108
|
-
return this.on("GET", patternOrPatterns, ...middleware);
|
|
71
|
+
c.text("Forbidden", 403);
|
|
109
72
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
|
|
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,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;IAEvB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;IACH,OAAO,UAAiB;CACxB;AAED,yBAAiB,OAAO,CAAC;IAExB,KAAY,MAAM,GAAG,YAAY,CAAC,OAAO,MAAM,CAAC,CAAC;CACjD;AAED;;;;GAIG;AACH,qBAAa,OAAO,CAAC,MAAM,SAAS,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM;;IAC5D;;;;OAIG;IACH,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;IAEtB;;;;OAIG;IACH,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAElB,+BAA+B;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAgB;IAEvC,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IAEvB,iEAAiE;IACjE,QAAQ,CAAC,GAAG,mBAA0B;IAEtC,oCAAoC;IACpC,QAAQ,CAAC,MAAM,SAAoB;IAUnC;;;;OAIG;gBACS,GAAG,EAAE,OAAO,EAAE,OAAO,GAAE,GAAG,CAAC,OAAY;IAMnD;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IAM3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAMnC;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAMpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAM1E;;;;;;;;OAQG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM;IAenB;;;;;;;;;;;;;;;;;;OAkBG;IACH,IAAI,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO;IAoDhC;;;;;;OAMG;WACU,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE;CAgBzD"}
|