ovr 4.5.1 → 5.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/app/context.d.ts +58 -56
- package/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +97 -134
- package/dist/app/helper/index.d.ts +2 -6
- package/dist/app/helper/index.d.ts.map +1 -1
- package/dist/app/helper/index.js +5 -19
- package/dist/app/index.d.ts +31 -45
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +68 -94
- package/dist/app/memo/index.d.ts +1 -1
- package/dist/app/memo/index.d.ts.map +1 -1
- package/dist/app/memo/index.js +2 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/jsx/chunk/index.d.ts +9 -0
- package/dist/jsx/chunk/index.d.ts.map +1 -1
- package/dist/jsx/chunk/index.js +11 -0
- package/dist/jsx/index.d.ts +5 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +43 -45
- package/dist/jsx/merge-async-generators.d.ts.map +1 -1
- package/dist/jsx/merge-async-generators.js +9 -3
- package/dist/trie/index.d.ts +5 -0
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +8 -2
- package/package.json +2 -2
- package/src/app/context.ts +100 -164
- package/src/app/helper/index.ts +6 -24
- package/src/app/index.ts +111 -123
- package/src/app/memo/index.ts +2 -2
- package/src/index.ts +1 -2
- package/src/jsx/chunk/index.ts +12 -0
- package/src/jsx/index.ts +61 -74
- package/src/jsx/merge-async-generators.ts +8 -5
- package/src/trie/index.ts +9 -2
- package/src/util/hash.ts +1 -1
- 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/src/app/mw/csrf.ts +0 -32
package/src/app/context.ts
CHANGED
|
@@ -1,41 +1,33 @@
|
|
|
1
1
|
import { Chunk } from "../jsx/chunk/index.js";
|
|
2
2
|
import { type JSX, toStream } from "../jsx/index.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type Params, Route } from "../trie/index.js";
|
|
4
4
|
import { hash } from "../util/hash.js";
|
|
5
|
-
import {
|
|
6
|
-
App,
|
|
7
|
-
type ErrorHandler,
|
|
8
|
-
type Middleware,
|
|
9
|
-
type NotFoundHandler,
|
|
10
|
-
type TrailingSlash,
|
|
11
|
-
} from "./index.js";
|
|
5
|
+
import { type Middleware } from "./index.js";
|
|
12
6
|
import { Memo } from "./memo/index.js";
|
|
13
7
|
|
|
14
8
|
type Layout = (props: { children: JSX.Element }) => JSX.Element;
|
|
15
9
|
|
|
16
10
|
class TagNotFound extends Error {
|
|
17
11
|
override name = "TagNotFound";
|
|
12
|
+
|
|
18
13
|
constructor(tag: string) {
|
|
19
14
|
super(`No closing ${tag} tag found`);
|
|
20
15
|
}
|
|
21
16
|
}
|
|
22
17
|
|
|
23
|
-
class ResponseFinalized extends Error {
|
|
24
|
-
override name = "ResponseFinalized";
|
|
25
|
-
constructor(property: string) {
|
|
26
|
-
super(
|
|
27
|
-
`Cannot set \`${property}\` after the \`Response\` has been finalized. ` +
|
|
28
|
-
`This can occur if \`${property}\` is set within a component since ` +
|
|
29
|
-
"components are evaluated during streaming.",
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
18
|
export class Context<P extends Params = Params> {
|
|
35
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Incoming `Request` to the server.
|
|
21
|
+
*
|
|
22
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
23
|
+
*/
|
|
36
24
|
req: Request;
|
|
37
25
|
|
|
38
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Parsed `URL` created from `req.url`.
|
|
28
|
+
*
|
|
29
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
|
|
30
|
+
*/
|
|
39
31
|
url: URL;
|
|
40
32
|
|
|
41
33
|
/**
|
|
@@ -46,92 +38,75 @@ export class Context<P extends Params = Params> {
|
|
|
46
38
|
*
|
|
47
39
|
* @example { slug: "my-post" }
|
|
48
40
|
*/
|
|
49
|
-
params
|
|
41
|
+
params: P = {} as P; // set after match
|
|
50
42
|
|
|
51
|
-
/**
|
|
52
|
-
route
|
|
43
|
+
/** Matched `Route` instance. */
|
|
44
|
+
route: Route<Middleware<P>[]> | null = null;
|
|
53
45
|
|
|
54
|
-
#body
|
|
46
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
47
|
+
body: BodyInit | null = null;
|
|
55
48
|
|
|
56
|
-
|
|
49
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
50
|
+
status?: number;
|
|
57
51
|
|
|
58
52
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
59
53
|
headers = new Headers();
|
|
60
54
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
#headElements: JSX.Element[] = [];
|
|
55
|
+
/** `layouts` for the `Page` to be passed into before being injected into the `<body>` */
|
|
56
|
+
layouts: Layout[] = [];
|
|
64
57
|
|
|
65
|
-
|
|
58
|
+
/** `JSX.Element`(s) to add to the head */
|
|
59
|
+
head: JSX.Element[] = [];
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
63
|
+
* Set to a new function to override the default.
|
|
64
|
+
*
|
|
65
|
+
* @default
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* (c) => {
|
|
69
|
+
* c.text("Not found", 404);
|
|
70
|
+
* c.headers.set("cache-control", "no-cache");
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
notFound: Middleware<P> = (c) => {
|
|
75
|
+
c.text("Not found", 404);
|
|
76
|
+
c.headers.set("cache-control", "no-cache");
|
|
77
|
+
};
|
|
71
78
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Base HTML to inject the `head` and `page` elements into.
|
|
81
|
+
*
|
|
82
|
+
* If left empty, components will be returned as partials.
|
|
83
|
+
*
|
|
84
|
+
* @default ""
|
|
85
|
+
*/
|
|
86
|
+
base = "";
|
|
74
87
|
|
|
75
88
|
/** `Memo` unique to the current `Request` context */
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
memo = this.#memo.use;
|
|
79
|
-
|
|
80
|
-
/** Check if the `Response` has been built already */
|
|
81
|
-
#finalized = false;
|
|
89
|
+
memo = new Memo();
|
|
82
90
|
|
|
83
91
|
static readonly #contentType = "content-type";
|
|
84
92
|
static readonly #headClose = "</head>";
|
|
85
93
|
static readonly #bodyClose = "</body>";
|
|
86
94
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
notFound: NotFoundHandler<P>,
|
|
94
|
-
) {
|
|
95
|
+
/**
|
|
96
|
+
* Creates a new `Context` with the current `Request`.
|
|
97
|
+
*
|
|
98
|
+
* @param req Request
|
|
99
|
+
*/
|
|
100
|
+
constructor(req: Request) {
|
|
95
101
|
this.req = req;
|
|
96
|
-
this.url = url;
|
|
97
|
-
this.#trailingSlash = trailingSlash;
|
|
98
|
-
this.#base = base;
|
|
99
|
-
this.error = error;
|
|
100
|
-
this.notFound = notFound;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
104
|
-
get status() {
|
|
105
|
-
return this.#status;
|
|
106
|
-
}
|
|
107
|
-
set status(status) {
|
|
108
|
-
if (this.#finalized) throw new ResponseFinalized("status");
|
|
109
|
-
this.#status = status;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
113
|
-
get body() {
|
|
114
|
-
return this.#body;
|
|
115
|
-
}
|
|
116
|
-
set body(body) {
|
|
117
|
-
if (this.#finalized) throw new ResponseFinalized("body");
|
|
118
|
-
this.#body = body;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Passed from `app.base` */
|
|
122
|
-
get base() {
|
|
123
|
-
return this.#base;
|
|
124
|
-
}
|
|
125
|
-
set base(base) {
|
|
126
|
-
if (this.#finalized) throw new ResponseFinalized("base");
|
|
127
|
-
this.#base = base;
|
|
102
|
+
this.url = new URL(req.url);
|
|
128
103
|
}
|
|
129
104
|
|
|
130
105
|
/**
|
|
131
106
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
132
107
|
*
|
|
133
108
|
* @param body Response BodyInit
|
|
134
|
-
* @param init
|
|
109
|
+
* @param init ResponseInit
|
|
135
110
|
*/
|
|
136
111
|
res(
|
|
137
112
|
body: BodyInit | null,
|
|
@@ -209,24 +184,7 @@ export class Context<P extends Params = Params> {
|
|
|
209
184
|
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
210
185
|
*/
|
|
211
186
|
redirect(location: string | URL, status: 301 | 302 | 303 | 307 | 308 = 302) {
|
|
212
|
-
this.
|
|
213
|
-
this.status = status;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* @param Element `JSX.Element` to add to the head
|
|
218
|
-
*/
|
|
219
|
-
head(...Element: JSX.Element[]) {
|
|
220
|
-
if (this.#finalized) throw new ResponseFinalized("head");
|
|
221
|
-
this.#headElements.push(...Element);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
226
|
-
*/
|
|
227
|
-
layout(...Layout: Layout[]) {
|
|
228
|
-
if (this.#finalized) throw new ResponseFinalized("layout");
|
|
229
|
-
this.#layouts.push(...Layout);
|
|
187
|
+
this.res(null, { status, headers: { location: location.toString() } });
|
|
230
188
|
}
|
|
231
189
|
|
|
232
190
|
/**
|
|
@@ -236,18 +194,17 @@ export class Context<P extends Params = Params> {
|
|
|
236
194
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
237
195
|
*/
|
|
238
196
|
page(Page: JSX.Element, status?: number) {
|
|
239
|
-
for (let i = this
|
|
240
|
-
|
|
197
|
+
for (let i = this.layouts.length - 1; i >= 0; i--) {
|
|
198
|
+
// add layouts around the page in reverse order (1st is the root layout)
|
|
199
|
+
Page = this.layouts[i]!({ children: Page });
|
|
241
200
|
}
|
|
242
201
|
|
|
243
|
-
let stream: ReadableStream<Uint8Array>;
|
|
244
|
-
|
|
245
202
|
if (this.base) {
|
|
246
203
|
// inject into base
|
|
247
204
|
const elements: JSX.Element[] = this.base.split(Context.#headClose);
|
|
248
205
|
if (!elements[1]) throw new TagNotFound(Context.#headClose);
|
|
249
206
|
|
|
250
|
-
elements.splice(1, 0, this
|
|
207
|
+
elements.splice(1, 0, this.head, Context.#headClose);
|
|
251
208
|
|
|
252
209
|
const bodyParts = (elements[3] as string).split(Context.#bodyClose);
|
|
253
210
|
if (!bodyParts[1]) throw new TagNotFound(Context.#bodyClose);
|
|
@@ -263,17 +220,17 @@ export class Context<P extends Params = Params> {
|
|
|
263
220
|
elements[3] = bodyParts[0];
|
|
264
221
|
elements.push(Page, Context.#bodyClose + bodyParts[1]);
|
|
265
222
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
typeof el === "string" ?
|
|
223
|
+
return this.html(
|
|
224
|
+
toStream(
|
|
225
|
+
elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el)),
|
|
269
226
|
),
|
|
227
|
+
status,
|
|
270
228
|
);
|
|
271
|
-
} else {
|
|
272
|
-
// HTML partial - just use the layouts + page
|
|
273
|
-
stream = toStream(Page);
|
|
274
229
|
}
|
|
275
230
|
|
|
276
|
-
|
|
231
|
+
// HTML partial - just use the layouts + page
|
|
232
|
+
// head elements are ignored if no base is set
|
|
233
|
+
return this.html(toStream(Page), status);
|
|
277
234
|
}
|
|
278
235
|
|
|
279
236
|
/**
|
|
@@ -301,67 +258,46 @@ export class Context<P extends Params = Params> {
|
|
|
301
258
|
}
|
|
302
259
|
|
|
303
260
|
/**
|
|
304
|
-
*
|
|
261
|
+
* Modifies the context based on the the return value of middleware.
|
|
262
|
+
*
|
|
263
|
+
* @param value value returned from middleware
|
|
305
264
|
*/
|
|
306
|
-
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
this.url.pathname += "/";
|
|
315
|
-
this.redirect(this.url, 308);
|
|
316
|
-
} else if (
|
|
317
|
-
this.#trailingSlash === "never" &&
|
|
318
|
-
this.url.pathname !== "/" &&
|
|
319
|
-
last === "/"
|
|
320
|
-
) {
|
|
321
|
-
this.url.pathname = this.url.pathname.slice(0, -1);
|
|
322
|
-
this.redirect(this.url, 308);
|
|
323
|
-
}
|
|
265
|
+
#resolveReturn(value: unknown) {
|
|
266
|
+
if (value instanceof Response) {
|
|
267
|
+
this.res(value.body, value);
|
|
268
|
+
} else if (value instanceof ReadableStream) {
|
|
269
|
+
this.body = value;
|
|
270
|
+
} else if (value != null) {
|
|
271
|
+
// nullish are not used so `void` will not render empty page
|
|
272
|
+
this.page(value);
|
|
324
273
|
}
|
|
274
|
+
}
|
|
325
275
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
276
|
+
/**
|
|
277
|
+
* Dispatches the stack of `middleware` provided.
|
|
278
|
+
*
|
|
279
|
+
* @param middleware stack to run
|
|
280
|
+
* @param i current middleware index (default `0`)
|
|
281
|
+
*/
|
|
282
|
+
async #run(middleware: Middleware<P>[], i = 0) {
|
|
283
|
+
if (!middleware[i]) return;
|
|
329
284
|
|
|
330
|
-
|
|
285
|
+
this.#resolveReturn(
|
|
286
|
+
await middleware[i](this, () => this.#run(middleware, i + 1)),
|
|
287
|
+
);
|
|
331
288
|
}
|
|
332
289
|
|
|
333
290
|
/**
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
* @returns Request context
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
291
|
+
* Composes a stack of `middleware` into a `Response`.
|
|
339
292
|
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
* const app = new Router();
|
|
344
|
-
*
|
|
345
|
-
* const fn = () => {
|
|
346
|
-
* const c = Context.get();
|
|
347
|
-
* // ...
|
|
348
|
-
* }
|
|
349
|
-
*
|
|
350
|
-
* app.get("/", () => {
|
|
351
|
-
* fn(); // OK
|
|
352
|
-
* });
|
|
353
|
-
*
|
|
354
|
-
* fn() // ReferenceError - outside AsyncLocalStorage scope
|
|
355
|
-
* ```
|
|
293
|
+
* @param middleware stack to compose
|
|
294
|
+
* @returns constructed `Response`
|
|
356
295
|
*/
|
|
357
|
-
|
|
358
|
-
|
|
296
|
+
async build(middleware: Middleware<P>[]) {
|
|
297
|
+
await this.#run(middleware);
|
|
359
298
|
|
|
360
|
-
if (!
|
|
361
|
-
throw new ReferenceError(
|
|
362
|
-
"Context can only be obtained within a handler.",
|
|
363
|
-
);
|
|
299
|
+
if (!this.body && !this.status) this.notFound(this, Promise.resolve);
|
|
364
300
|
|
|
365
|
-
return
|
|
301
|
+
return new Response(this.body, this);
|
|
366
302
|
}
|
|
367
303
|
}
|
package/src/app/helper/index.ts
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
import { type JSX, jsx } from "../../jsx/index.js";
|
|
2
2
|
import type { Params } from "../../trie/index.js";
|
|
3
3
|
import type { ExtractParams, InsertParams } from "../../types/index.js";
|
|
4
|
-
import { Context } from "../context.js";
|
|
5
4
|
import type { Middleware } from "../index.js";
|
|
6
5
|
|
|
7
6
|
export type UrlOptions<P extends Params> = {
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
10
|
-
* can only be used in the context of a request.
|
|
11
|
-
*
|
|
12
|
-
* Otherwise the value is passed into `URLSearchParams` constructor to
|
|
13
|
-
* create new params.
|
|
8
|
+
* Passed into `URLSearchParams` constructor to create new params.
|
|
14
9
|
*
|
|
15
10
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
16
11
|
*/
|
|
17
12
|
search?:
|
|
18
|
-
| boolean
|
|
19
13
|
| string
|
|
20
14
|
// Iterable is more accurate than the built in string[][] + URLSearchParams
|
|
21
15
|
// https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/2070
|
|
@@ -104,25 +98,15 @@ export class Helper<Pattern extends string> {
|
|
|
104
98
|
// @ts-expect-error - do not have to pass in {} if no params
|
|
105
99
|
options?.params,
|
|
106
100
|
);
|
|
101
|
+
let search = "";
|
|
102
|
+
let hash = "";
|
|
107
103
|
|
|
108
|
-
let search: string;
|
|
109
104
|
if (options?.search) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
} else {
|
|
114
|
-
// use the value as the init
|
|
115
|
-
search =
|
|
116
|
-
"?" +
|
|
117
|
-
new URLSearchParams(
|
|
118
|
-
options.search as ConstructorParameters<typeof URLSearchParams>[0],
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
search = "";
|
|
105
|
+
// use the value as the init
|
|
106
|
+
// @ts-expect-error - see above
|
|
107
|
+
search = "?" + new URLSearchParams(options.search);
|
|
123
108
|
}
|
|
124
109
|
|
|
125
|
-
let hash: string;
|
|
126
110
|
if (options?.hash) {
|
|
127
111
|
// adding # prefix if not present matches the URL setter:
|
|
128
112
|
// https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
|
|
@@ -131,8 +115,6 @@ export class Helper<Pattern extends string> {
|
|
|
131
115
|
} else {
|
|
132
116
|
hash = "#" + options.hash;
|
|
133
117
|
}
|
|
134
|
-
} else {
|
|
135
|
-
hash = "";
|
|
136
118
|
}
|
|
137
119
|
|
|
138
120
|
return pathname + search + hash;
|