ovr 5.0.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 +49 -83
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +40 -92
- 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 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- 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 -200
- 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 +30 -30
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +67 -82
- 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 +3 -3
- package/src/app/index.ts +99 -224
- package/src/context/index.ts +262 -0
- package/src/cookie/index.ts +189 -0
- package/src/index.ts +6 -7
- package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
- package/src/jsx/index.ts +81 -223
- 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 +87 -111
- package/src/util/index.ts +112 -0
- package/dist/app/context.d.ts +0 -149
- package/dist/app/context.d.ts.map +0 -1
- package/dist/app/context.js +0 -246
- 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 -51
- package/dist/app/helper/index.d.ts.map +0 -1
- package/dist/app/helper/index.js +0 -82
- 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/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 -303
- package/src/app/helper/get.ts +0 -46
- package/src/app/helper/index.ts +0 -156
- package/src/app/helper/post.ts +0 -54
- package/src/app/memo/index.ts +0 -40
- 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/src/app/index.ts
CHANGED
|
@@ -1,215 +1,105 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "../
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Context } from "../context/index.js";
|
|
2
|
+
import type { Middleware } from "../middleware/index.js";
|
|
3
|
+
import type { Multipart } from "../multipart/index.js";
|
|
4
|
+
import { Route } from "../route/index.js";
|
|
5
|
+
import { Trie } from "../trie/index.js";
|
|
6
|
+
import type { Util } from "../util/index.js";
|
|
7
|
+
|
|
8
|
+
export namespace App {
|
|
9
|
+
export namespace Options {
|
|
10
|
+
/** Trailing slash preference */
|
|
11
|
+
export type TrailingSlash = "always" | "never" | "ignore";
|
|
12
|
+
}
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
/** Configuration options */
|
|
15
|
+
export type Options = {
|
|
16
|
+
/**
|
|
17
|
+
* Basic
|
|
18
|
+
* [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
|
|
19
|
+
* protection that checks the request's `method`, and its
|
|
20
|
+
* [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
|
|
21
|
+
* and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
|
|
22
|
+
* headers.
|
|
23
|
+
*
|
|
24
|
+
* More robust protection requires a stateful server or a database to store
|
|
25
|
+
* [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
readonly csrf?: boolean;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
|
|
33
|
+
* - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
|
|
34
|
+
* - `"ignore"` - no redirects (not recommended, bad for SEO)
|
|
35
|
+
*
|
|
36
|
+
* [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
|
|
37
|
+
*
|
|
38
|
+
* @default "never"
|
|
39
|
+
*/
|
|
40
|
+
readonly trailingSlash?: Options.TrailingSlash;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Multipart form data options
|
|
44
|
+
* (overridden by options passed into `Context.form(options)`)
|
|
45
|
+
*/
|
|
46
|
+
readonly form?: Multipart.Options;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
12
49
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
50
|
+
/** Web server application */
|
|
51
|
+
export class App {
|
|
52
|
+
/** Route trie */
|
|
53
|
+
readonly #trie = new Trie();
|
|
17
54
|
|
|
18
|
-
|
|
55
|
+
/** Global middleware */
|
|
56
|
+
readonly #global: Middleware[] = [];
|
|
19
57
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
| "HEAD"
|
|
23
|
-
| "POST"
|
|
24
|
-
| "PUT"
|
|
25
|
-
| "DELETE"
|
|
26
|
-
| "CONNECT"
|
|
27
|
-
| "OPTIONS"
|
|
28
|
-
| "TRACE"
|
|
29
|
-
| "PATCH"
|
|
30
|
-
| (string & {});
|
|
58
|
+
/** App options */
|
|
59
|
+
readonly #options;
|
|
31
60
|
|
|
32
|
-
/** `App` configuration options */
|
|
33
|
-
type AppOptions = {
|
|
34
61
|
/**
|
|
35
|
-
*
|
|
36
|
-
* [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
|
|
37
|
-
* protection that checks the request's `method`, and its
|
|
38
|
-
* [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
|
|
39
|
-
* and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
|
|
40
|
-
* headers.
|
|
41
|
-
*
|
|
42
|
-
* More robust protection requires a stateful server or a database to store
|
|
43
|
-
* [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
|
|
62
|
+
* Create a new application.
|
|
44
63
|
*
|
|
45
|
-
* @
|
|
64
|
+
* @param options configuration options
|
|
46
65
|
*/
|
|
47
|
-
|
|
66
|
+
constructor(options?: App.Options) {
|
|
67
|
+
this.#options = Object.assign(
|
|
68
|
+
{ csrf: true, trailingSlash: "never" },
|
|
69
|
+
options,
|
|
70
|
+
);
|
|
48
71
|
|
|
49
|
-
|
|
50
|
-
* - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
|
|
51
|
-
* - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
|
|
52
|
-
* - `"ignore"` - no redirects (not recommended, bad for SEO)
|
|
53
|
-
*
|
|
54
|
-
* [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
|
|
55
|
-
*
|
|
56
|
-
* @default "never"
|
|
57
|
-
*/
|
|
58
|
-
trailingSlash?: TrailingSlash;
|
|
59
|
-
};
|
|
72
|
+
if (this.#options.csrf === true) this.#global.push(App.#csrf);
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const resolved: Required<AppOptions> = {
|
|
64
|
-
csrf: true,
|
|
65
|
-
trailingSlash: "never",
|
|
66
|
-
};
|
|
67
|
-
Object.assign(resolved, options);
|
|
68
|
-
|
|
69
|
-
if (resolved.csrf === true) this.#use.push(App.#csrf);
|
|
70
|
-
|
|
71
|
-
if (resolved.trailingSlash !== "ignore") {
|
|
72
|
-
this.#use.push(App.#trailingSlash(resolved.trailingSlash));
|
|
74
|
+
if (this.#options.trailingSlash !== "ignore") {
|
|
75
|
+
this.#global.push(App.#createTrailingSlash(this.#options.trailingSlash));
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
/** Built tries per HTTP method. */
|
|
77
|
-
#trieMap = new Map<Method, Trie<Middleware[]>>();
|
|
78
|
-
|
|
79
|
-
/** Added routes per HTTP method. */
|
|
80
|
-
#routesMap = new Map<Method, Route<Middleware[]>[]>();
|
|
81
|
-
|
|
82
|
-
/** Global middleware. */
|
|
83
|
-
#use: Middleware[] = [];
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @param helpers `Helper` to add to `App`
|
|
87
|
-
* @returns `App` instance
|
|
88
|
-
*/
|
|
89
|
-
add(...helpers: DeepArray<Get | Post | Record<string, Get | Post>>[]) {
|
|
90
|
-
for (const helper of helpers) {
|
|
91
|
-
if (helper instanceof Array) {
|
|
92
|
-
this.add(...helper);
|
|
93
|
-
} else if (helper instanceof Get) {
|
|
94
|
-
this.get(helper.pattern, ...helper.middleware);
|
|
95
|
-
} else if (helper instanceof Post) {
|
|
96
|
-
this.post(helper.pattern, ...helper.middleware);
|
|
97
|
-
} else {
|
|
98
|
-
this.add(...Object.values(helper));
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return this;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Add global middleware.
|
|
107
|
-
*
|
|
108
|
-
* @param middleware
|
|
109
|
-
* @returns `App` instance
|
|
110
|
-
*/
|
|
111
|
-
use(...middleware: Middleware[]) {
|
|
112
|
-
this.#use.push(...middleware);
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
79
|
/**
|
|
117
|
-
* @param
|
|
118
|
-
* @param pattern Route pattern
|
|
119
|
-
* @param middleware
|
|
80
|
+
* @param routes Route or middleware to use
|
|
120
81
|
* @returns `App` instance
|
|
121
82
|
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
): this;
|
|
127
|
-
/**
|
|
128
|
-
* @param method HTTP method
|
|
129
|
-
* @param patterns Array of route patterns
|
|
130
|
-
* @param middleware
|
|
131
|
-
* @returns `App` instance
|
|
132
|
-
*/
|
|
133
|
-
on<Patterns extends string[]>(
|
|
134
|
-
method: Method | Method[],
|
|
135
|
-
patterns: [...Patterns],
|
|
136
|
-
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
137
|
-
): this;
|
|
138
|
-
on<PatternOrPatterns extends string | string[]>(
|
|
139
|
-
method: Method | Method[],
|
|
140
|
-
pattern: PatternOrPatterns,
|
|
141
|
-
...middleware: Middleware[]
|
|
83
|
+
use(
|
|
84
|
+
...routes: Util.DeepArray<
|
|
85
|
+
Route | Middleware | Record<string, Route | Middleware>
|
|
86
|
+
>[]
|
|
142
87
|
) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
for (const m of method) {
|
|
153
|
-
const routes = this.#routesMap.get(m);
|
|
154
|
-
|
|
155
|
-
if (routes) routes.push(route);
|
|
156
|
-
else this.#routesMap.set(m, [route]);
|
|
88
|
+
for (const route of routes) {
|
|
89
|
+
if (route instanceof Route) {
|
|
90
|
+
this.#trie.add(route);
|
|
91
|
+
} else if (route instanceof Array) {
|
|
92
|
+
this.use(...route);
|
|
93
|
+
} else if (typeof route === "function") {
|
|
94
|
+
this.#global.push(route);
|
|
95
|
+
} else {
|
|
96
|
+
this.use(...Object.values(route));
|
|
157
97
|
}
|
|
158
98
|
}
|
|
159
99
|
|
|
160
100
|
return this;
|
|
161
101
|
}
|
|
162
102
|
|
|
163
|
-
/**
|
|
164
|
-
* @param pattern Route pattern
|
|
165
|
-
* @param middleware
|
|
166
|
-
* @returns `App` instance
|
|
167
|
-
*/
|
|
168
|
-
get<Pattern extends string>(
|
|
169
|
-
pattern: Pattern,
|
|
170
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
171
|
-
): this;
|
|
172
|
-
/**
|
|
173
|
-
* @param patterns Array of route patterns
|
|
174
|
-
* @param middleware
|
|
175
|
-
* @returns `App` instance
|
|
176
|
-
*/
|
|
177
|
-
get<Patterns extends string[]>(
|
|
178
|
-
patterns: [...Patterns],
|
|
179
|
-
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
180
|
-
): this;
|
|
181
|
-
get<PatternOrPatterns extends string | string[]>(
|
|
182
|
-
patternOrPatterns: PatternOrPatterns,
|
|
183
|
-
...middleware: Middleware[]
|
|
184
|
-
) {
|
|
185
|
-
return this.on("GET", patternOrPatterns as string, ...middleware);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* @param pattern Route pattern
|
|
190
|
-
* @param middleware
|
|
191
|
-
* @returns the router instance
|
|
192
|
-
*/
|
|
193
|
-
post<Pattern extends string>(
|
|
194
|
-
pattern: Pattern,
|
|
195
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
196
|
-
): this;
|
|
197
|
-
/**
|
|
198
|
-
* @param patterns Array of route patterns
|
|
199
|
-
* @param middleware
|
|
200
|
-
* @returns the router instance
|
|
201
|
-
*/
|
|
202
|
-
post<Patterns extends string[]>(
|
|
203
|
-
patterns: [...Patterns],
|
|
204
|
-
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
205
|
-
): this;
|
|
206
|
-
post<PatternOrPatterns extends string | string[]>(
|
|
207
|
-
patternOrPatterns: PatternOrPatterns,
|
|
208
|
-
...middleware: Middleware[]
|
|
209
|
-
): this {
|
|
210
|
-
return this.on("POST", patternOrPatterns as string, ...middleware);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
103
|
/**
|
|
214
104
|
* Request a resource from the application.
|
|
215
105
|
*
|
|
@@ -219,42 +109,27 @@ export class App {
|
|
|
219
109
|
* @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
|
|
220
110
|
* @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
|
|
221
111
|
*/
|
|
222
|
-
fetch = async (
|
|
112
|
+
readonly fetch = async (
|
|
223
113
|
resource: RequestInfo | URL,
|
|
224
114
|
options?: RequestInit,
|
|
225
|
-
)
|
|
226
|
-
const c = new Context(new Request(resource, options));
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
this.#trieMap.set(c.req.method, trie);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (trie) {
|
|
244
|
-
const match = trie.find(c.url.pathname);
|
|
245
|
-
|
|
246
|
-
if (match) {
|
|
247
|
-
Object.assign(c, match);
|
|
248
|
-
return c.build(this.#use.concat(match.route.store));
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// no match, just run global middleware
|
|
253
|
-
return c.build(this.#use);
|
|
115
|
+
) => {
|
|
116
|
+
const c = new Context(new Request(resource, options), this.#options);
|
|
117
|
+
|
|
118
|
+
return Context.compose(
|
|
119
|
+
Object.assign(
|
|
120
|
+
c,
|
|
121
|
+
// match
|
|
122
|
+
this.#trie.find(
|
|
123
|
+
// use GET for HEAD requests to find the correct route
|
|
124
|
+
(c.req.method === "HEAD" ? "GET" : c.req.method) + c.url.pathname,
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
this.#global.concat(c.route?.middleware ?? []),
|
|
128
|
+
);
|
|
254
129
|
};
|
|
255
130
|
|
|
256
|
-
/** Basic CSRF middleware
|
|
257
|
-
static
|
|
131
|
+
/** Basic CSRF middleware */
|
|
132
|
+
static #csrf(c: Middleware.Context, next: Middleware.Next) {
|
|
258
133
|
if (
|
|
259
134
|
c.req.method === "GET" ||
|
|
260
135
|
c.req.method === "HEAD" ||
|
|
@@ -268,11 +143,11 @@ export class App {
|
|
|
268
143
|
}
|
|
269
144
|
|
|
270
145
|
/** @returns Trailing slash middleware */
|
|
271
|
-
static #
|
|
272
|
-
return async (c: Context, next: Next) => {
|
|
146
|
+
static #createTrailingSlash(mode: App.Options.TrailingSlash) {
|
|
147
|
+
return async (c: Context, next: Middleware.Next) => {
|
|
273
148
|
await next();
|
|
274
149
|
|
|
275
|
-
if (c.status && c.status !== 404) return;
|
|
150
|
+
if (c.res.status && c.res.status !== 404) return;
|
|
276
151
|
|
|
277
152
|
const last = c.url.pathname.at(-1);
|
|
278
153
|
|
|
@@ -0,0 +1,262 @@
|
|
|
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 { Render } from "../render/index.js";
|
|
6
|
+
import { Route } from "../route/index.js";
|
|
7
|
+
import { type Trie } from "../trie/index.js";
|
|
8
|
+
import { Hash, Header } from "../util/index.js";
|
|
9
|
+
|
|
10
|
+
/** Properties to build the final `Response` with once middleware has run. */
|
|
11
|
+
class PreparedResponse {
|
|
12
|
+
/**
|
|
13
|
+
* `body` used to create the `Response`.
|
|
14
|
+
*
|
|
15
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body)
|
|
16
|
+
*/
|
|
17
|
+
body?: BodyInit | null;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `status` used to create the `Response`.
|
|
21
|
+
*
|
|
22
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
23
|
+
*/
|
|
24
|
+
status?: number;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `Headers` used to create the `Response`.
|
|
28
|
+
*
|
|
29
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
|
30
|
+
*/
|
|
31
|
+
headers = new Headers();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export namespace Context {
|
|
35
|
+
// gives users access to Middleware.Context.Cookie.Options
|
|
36
|
+
export type Cookie = InstanceType<typeof Cookie>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Request context.
|
|
41
|
+
*
|
|
42
|
+
* @template Params Parameters created from a route match
|
|
43
|
+
*/
|
|
44
|
+
export class Context<Params extends Trie.Params = Trie.Params> {
|
|
45
|
+
/**
|
|
46
|
+
* Incoming `Request` to the server.
|
|
47
|
+
*
|
|
48
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
49
|
+
*/
|
|
50
|
+
readonly req: Request;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Parsed `URL` created from `req.url`.
|
|
54
|
+
*
|
|
55
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
56
|
+
*/
|
|
57
|
+
readonly url: URL;
|
|
58
|
+
|
|
59
|
+
/** Route pattern parameters */
|
|
60
|
+
readonly params: Params = {} as Params; // set after match
|
|
61
|
+
|
|
62
|
+
/** Matched `Route` instance */
|
|
63
|
+
readonly route?: Route;
|
|
64
|
+
|
|
65
|
+
/** Contains the arguments to used create the final `Response` */
|
|
66
|
+
readonly res = new PreparedResponse();
|
|
67
|
+
|
|
68
|
+
/** Get, set, and delete cookies. */
|
|
69
|
+
readonly cookie = new Cookie(this);
|
|
70
|
+
|
|
71
|
+
/** Forwarded app options */
|
|
72
|
+
readonly #options: App.Options;
|
|
73
|
+
|
|
74
|
+
// for reuse across methods
|
|
75
|
+
static readonly #textHtml = "text/html";
|
|
76
|
+
static readonly #utf8 = "charset=utf-8";
|
|
77
|
+
static readonly #htmlType = `${Context.#textHtml}; ${Context.#utf8}`;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new `Context` with the current `Request`.
|
|
81
|
+
*
|
|
82
|
+
* @param req Request
|
|
83
|
+
*/
|
|
84
|
+
constructor(req: Request, options: App.Options = {}) {
|
|
85
|
+
this.req = req;
|
|
86
|
+
this.url = new URL(req.url);
|
|
87
|
+
this.#options = options;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Creates an HTML response.
|
|
92
|
+
*
|
|
93
|
+
* @param body HTML body
|
|
94
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
95
|
+
*/
|
|
96
|
+
html(body: BodyInit | null, status?: number) {
|
|
97
|
+
this.res.body = body;
|
|
98
|
+
this.res.status = status;
|
|
99
|
+
this.res.headers.set(Header.contentType, Context.#htmlType);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a JSON response.
|
|
104
|
+
*
|
|
105
|
+
* @param data passed into JSON.stringify to create the body
|
|
106
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
107
|
+
*/
|
|
108
|
+
json(data: unknown, status?: number) {
|
|
109
|
+
this.res.body = JSON.stringify(data);
|
|
110
|
+
this.res.status = status;
|
|
111
|
+
this.res.headers.set(Header.contentType, "application/json");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Creates a plain text response.
|
|
116
|
+
*
|
|
117
|
+
* @param body response body
|
|
118
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
119
|
+
*/
|
|
120
|
+
text(body: BodyInit, status?: number) {
|
|
121
|
+
this.res.body = body;
|
|
122
|
+
this.res.status = status;
|
|
123
|
+
this.res.headers.set(Header.contentType, `text/plain; ${Context.#utf8}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Creates a redirect response.
|
|
128
|
+
*
|
|
129
|
+
* @param location redirect `Location` header
|
|
130
|
+
* @param status HTTP status code
|
|
131
|
+
*
|
|
132
|
+
* - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
|
|
133
|
+
* - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
|
|
134
|
+
* - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
|
|
135
|
+
* - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
|
|
136
|
+
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
137
|
+
*/
|
|
138
|
+
redirect(location: string | URL, status: 301 | 302 | 303 | 307 | 308 = 302) {
|
|
139
|
+
this.res.body = null;
|
|
140
|
+
this.res.status = status;
|
|
141
|
+
this.res.headers.set("location", String(location));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Generates an etag from a hash of the string provided.
|
|
146
|
+
* If the etag matches, sets the response to not modified.
|
|
147
|
+
*
|
|
148
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
|
|
149
|
+
*
|
|
150
|
+
* @param string string to hash
|
|
151
|
+
* @returns `true` if the etag matches, `false` otherwise
|
|
152
|
+
*/
|
|
153
|
+
etag(string: string) {
|
|
154
|
+
const etag = `"${Hash.djb2(string)}"`;
|
|
155
|
+
|
|
156
|
+
this.res.headers.set("etag", etag);
|
|
157
|
+
|
|
158
|
+
if (this.req.headers.get("if-none-match") === etag) {
|
|
159
|
+
this.res.body = null;
|
|
160
|
+
this.res.status = 304;
|
|
161
|
+
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Parse multipart requests.
|
|
170
|
+
*
|
|
171
|
+
* @yields Multipart request `Part`(s)
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { Route } from "ovr";
|
|
177
|
+
*
|
|
178
|
+
* const post = Route.post(async (c) => {
|
|
179
|
+
* for await (const part of c.form()) {
|
|
180
|
+
* if (part.name === "email") {
|
|
181
|
+
* // ...
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
* })
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
form(options?: Multipart.Options) {
|
|
188
|
+
return new Multipart(
|
|
189
|
+
this.req,
|
|
190
|
+
Object.assign({}, this.#options.form, options),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Dispatches the stack of `middleware` provided.
|
|
196
|
+
*
|
|
197
|
+
* @param middleware stack to run
|
|
198
|
+
* @param i current middleware index (default `0`)
|
|
199
|
+
* @returns return value of `middleware[i]`
|
|
200
|
+
*/
|
|
201
|
+
async #run(middleware: Middleware<Params>[], i = 0) {
|
|
202
|
+
const mw = middleware[i];
|
|
203
|
+
|
|
204
|
+
if (!mw) return; // end of stack
|
|
205
|
+
|
|
206
|
+
const r = await mw(
|
|
207
|
+
this, // c
|
|
208
|
+
() => this.#run(middleware, i + 1), // next
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
// resolve the return value
|
|
212
|
+
if (r instanceof Response) {
|
|
213
|
+
// overwrite
|
|
214
|
+
this.res.body = r.body;
|
|
215
|
+
this.res.status = r.status;
|
|
216
|
+
|
|
217
|
+
// merge
|
|
218
|
+
for (const [name, header] of r.headers) {
|
|
219
|
+
this.res.headers.set(name, header);
|
|
220
|
+
}
|
|
221
|
+
} else if (r !== undefined) {
|
|
222
|
+
// something to stream
|
|
223
|
+
const type = this.res.headers.get(Header.contentType);
|
|
224
|
+
|
|
225
|
+
this.res.body = Render.stream(r, {
|
|
226
|
+
// other defined types are safe
|
|
227
|
+
safe: Boolean(type && !type.startsWith(Context.#textHtml)),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
if (!type) {
|
|
231
|
+
// default to HTML
|
|
232
|
+
this.res.headers.set(Header.contentType, Context.#htmlType);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// do not overwrite/remove status - that way user can set it before returning
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Composes a stack of `middleware` into a `Response`.
|
|
241
|
+
*
|
|
242
|
+
* @param c Context
|
|
243
|
+
* @param middleware stack to compose
|
|
244
|
+
* @returns constructed `Response`
|
|
245
|
+
*/
|
|
246
|
+
static async compose(c: Context, middleware: Middleware[]) {
|
|
247
|
+
await c.#run(middleware);
|
|
248
|
+
|
|
249
|
+
if (c.req.method === "HEAD") {
|
|
250
|
+
if (c.res.body instanceof ReadableStream) {
|
|
251
|
+
// cancel unused stream to prevent leaks
|
|
252
|
+
await c.res.body.cancel("HEAD");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
c.res.body = null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
Object.freeze(c.res);
|
|
259
|
+
|
|
260
|
+
return new Response(c.res.body, c.res);
|
|
261
|
+
}
|
|
262
|
+
}
|