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
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { Context } from "../context/index.js";
|
|
2
|
+
import { Header } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
export namespace Cookie {
|
|
5
|
+
type BaseOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Specifies the domain for which the cookie is valid.
|
|
8
|
+
* If not specified, this defaults to the host portion of the current document location.
|
|
9
|
+
*/
|
|
10
|
+
readonly domain?: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The maximum lifetime of the cookie as an HTTP-date timestamp.
|
|
14
|
+
* If unspecified, the cookie becomes a session cookie.
|
|
15
|
+
*/
|
|
16
|
+
readonly expires?: Date;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* If true, the cookie is inaccessible to the JavaScript `Document.cookie` API.
|
|
20
|
+
* Used to help prevent cross-site scripting (XSS) attacks.
|
|
21
|
+
*/
|
|
22
|
+
readonly httpOnly?: boolean;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The number of **seconds** until the cookie expires.
|
|
26
|
+
* A zero or negative number will expire the cookie immediately.
|
|
27
|
+
*/
|
|
28
|
+
readonly maxAge?: number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Specifies the path that must exist in the requested URL for the browser
|
|
32
|
+
* to send the Cookie header.
|
|
33
|
+
*
|
|
34
|
+
* @default "/"
|
|
35
|
+
*/
|
|
36
|
+
readonly path?: string;
|
|
37
|
+
|
|
38
|
+
/** Determines which cookies to keep when the browser limit is exceeded. */
|
|
39
|
+
readonly priority?: "Low" | "Medium" | "High";
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type HostPrefixOptions = BaseOptions & {
|
|
43
|
+
/**
|
|
44
|
+
* Limits the scope of the cookie to a secure context (HTTPS).
|
|
45
|
+
* **Requirement:** `__Host-` cookies must be Secure.
|
|
46
|
+
*/
|
|
47
|
+
readonly secure: true;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Specifies the path that must exist in the requested URL.
|
|
51
|
+
* **Requirement:** `__Host-` cookies must use the path "/".
|
|
52
|
+
*/
|
|
53
|
+
readonly path?: "/";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Specifies the domain for which the cookie is valid.
|
|
57
|
+
* **Requirement:** `__Host-` cookies must not have a defined domain.
|
|
58
|
+
*/
|
|
59
|
+
readonly domain?: never;
|
|
60
|
+
|
|
61
|
+
/** Controls whether the cookie is sent with cross-site requests. */
|
|
62
|
+
readonly sameSite?: "Lax" | "Strict" | "None";
|
|
63
|
+
|
|
64
|
+
/** Indicates that the cookie should be stored using partitioned storage (CHIPS). */
|
|
65
|
+
readonly partitioned?: boolean;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type SecurePrefixOptions = BaseOptions & {
|
|
69
|
+
/**
|
|
70
|
+
* Limits the scope of the cookie to a secure context (HTTPS).
|
|
71
|
+
* **Requirement:** `__Secure-` cookies must be Secure.
|
|
72
|
+
*/
|
|
73
|
+
readonly secure: true;
|
|
74
|
+
|
|
75
|
+
/** Controls whether the cookie is sent with cross-site requests. */
|
|
76
|
+
readonly sameSite?: "Lax" | "Strict" | "None";
|
|
77
|
+
|
|
78
|
+
/** Indicates that the cookie should be stored using partitioned storage (CHIPS). */
|
|
79
|
+
readonly partitioned?: boolean;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
type SecureOptions = BaseOptions & {
|
|
83
|
+
/** Limits the scope of the cookie to a secure context (HTTPS). */
|
|
84
|
+
readonly secure: true;
|
|
85
|
+
|
|
86
|
+
/** Controls whether the cookie is sent with cross-site requests. */
|
|
87
|
+
readonly sameSite?: "Lax" | "Strict" | "None";
|
|
88
|
+
|
|
89
|
+
/** Indicates that the cookie should be stored using partitioned storage (CHIPS). */
|
|
90
|
+
readonly partitioned?: boolean;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
type InsecureOptions = BaseOptions & {
|
|
94
|
+
/** Limits the scope of the cookie to a secure context (HTTPS). */
|
|
95
|
+
readonly secure?: false;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Controls whether the cookie is sent with cross-site requests.
|
|
99
|
+
* **Note:** `"none"` is not allowed unless `secure` is true.
|
|
100
|
+
*/
|
|
101
|
+
readonly sameSite?: "Lax" | "Strict";
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Indicates that the cookie should be stored using partitioned storage (CHIPS).
|
|
105
|
+
* **Note:** `true` is not allowed unless `secure` is true.
|
|
106
|
+
*/
|
|
107
|
+
readonly partitioned?: false;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Configuration options for setting a cookie.
|
|
112
|
+
*
|
|
113
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
|
|
114
|
+
*/
|
|
115
|
+
export type Options<Name extends string = string> =
|
|
116
|
+
Name extends `__${"host" | "secure"}-${string}`
|
|
117
|
+
? never // typos
|
|
118
|
+
: Name extends `__Host-${string}`
|
|
119
|
+
? HostPrefixOptions
|
|
120
|
+
: Name extends `__Secure-${string}`
|
|
121
|
+
? SecurePrefixOptions
|
|
122
|
+
: SecureOptions | InsecureOptions;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** HTTP cookie manager */
|
|
126
|
+
export class Cookie {
|
|
127
|
+
/** Current context */
|
|
128
|
+
readonly #c: Context;
|
|
129
|
+
|
|
130
|
+
/** Lazily parsed cookie map */
|
|
131
|
+
#parsed?: Record<string, string>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Create a new cookie manager.
|
|
135
|
+
*
|
|
136
|
+
* @param c Request context
|
|
137
|
+
*/
|
|
138
|
+
constructor(c: Context) {
|
|
139
|
+
this.#c = c;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Retrieves a specific cookie value from the request headers.
|
|
144
|
+
*
|
|
145
|
+
* @param name Name of the cookie to retrieve
|
|
146
|
+
* @returns Value of the cookie, or `undefined` if not found
|
|
147
|
+
*/
|
|
148
|
+
get(name: string) {
|
|
149
|
+
return (this.#parsed ??= Header.params(this.#c.req.headers.get("cookie")))[
|
|
150
|
+
name
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Sets a cookie on the response using the `Set-Cookie` header.
|
|
156
|
+
*
|
|
157
|
+
* @param name Name of the cookie
|
|
158
|
+
* @param value Value to store
|
|
159
|
+
* @param options Options configuration
|
|
160
|
+
*/
|
|
161
|
+
set<Name extends string>(
|
|
162
|
+
name: Name,
|
|
163
|
+
value: string,
|
|
164
|
+
...[
|
|
165
|
+
options,
|
|
166
|
+
]: Name extends `__${"Secure" | "secure" | "Host" | "host"}-${string}`
|
|
167
|
+
? [options: Cookie.Options<Name>]
|
|
168
|
+
: [options?: Cookie.Options<Name>]
|
|
169
|
+
) {
|
|
170
|
+
options ??= {};
|
|
171
|
+
|
|
172
|
+
const cookie = [
|
|
173
|
+
`${name}=${encodeURIComponent(value)}`,
|
|
174
|
+
`Path=${options.path ?? "/"}`,
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
if (options.secure) cookie.push("Secure");
|
|
178
|
+
if (options.httpOnly) cookie.push("HttpOnly");
|
|
179
|
+
if (options.partitioned) cookie.push("Partitioned");
|
|
180
|
+
if (options.domain) cookie.push(`Domain=${options.domain}`);
|
|
181
|
+
if (options.priority) cookie.push(`Priority=${options.priority}`);
|
|
182
|
+
if (options.sameSite) cookie.push(`SameSite=${options.sameSite}`);
|
|
183
|
+
if (options.maxAge !== undefined) cookie.push(`Max-Age=${options.maxAge}`);
|
|
184
|
+
if (options.expires)
|
|
185
|
+
cookie.push(`Expires=${options.expires.toUTCString()}`);
|
|
186
|
+
|
|
187
|
+
this.#c.res.headers.append("set-cookie", cookie.join("; "));
|
|
188
|
+
}
|
|
189
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export { Post } from "./app/helper/post.js";
|
|
8
|
-
export { csrf } from "./app/mw/csrf.js";
|
|
1
|
+
export { App } from "./app/index.js";
|
|
2
|
+
export { Route } from "./route/index.js";
|
|
3
|
+
export { Multipart } from "./multipart/index.js";
|
|
4
|
+
export { JSX } from "./jsx/index.js";
|
|
5
|
+
export { Render } from "./render/index.js";
|
|
6
|
+
export type { Middleware } from "./middleware/index.js";
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
// cspell: disable
|
|
2
|
-
import type { JSX
|
|
2
|
+
import type { JSX } from "./index.js";
|
|
3
3
|
|
|
4
4
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes
|
|
5
|
+
/**
|
|
6
|
+
* HTML Attributes
|
|
7
|
+
*
|
|
8
|
+
* @template P Additional props
|
|
9
|
+
* @template Children For void elements - `undefined` is passed in here
|
|
10
|
+
*/
|
|
5
11
|
type Attributes<
|
|
6
|
-
P extends Props = Props,
|
|
7
|
-
Children = JSX.Element,
|
|
12
|
+
P extends JSX.Props = JSX.Props,
|
|
13
|
+
Children = JSX.Element,
|
|
8
14
|
> = Partial<{
|
|
9
15
|
children: Children;
|
|
10
16
|
accesskey: string;
|
|
@@ -148,7 +154,7 @@ type Attributes<
|
|
|
148
154
|
"aria-relevant": "additions" | "all" | "removals" | "text" | "additions text";
|
|
149
155
|
"aria-roledescription": string;
|
|
150
156
|
}> &
|
|
151
|
-
Props &
|
|
157
|
+
JSX.Props &
|
|
152
158
|
Partial<P>;
|
|
153
159
|
|
|
154
160
|
// have lots of values and shared by some elements but not global
|
|
@@ -707,6 +713,7 @@ type VideoAttributes = Attributes<{
|
|
|
707
713
|
width: string | number;
|
|
708
714
|
}>;
|
|
709
715
|
|
|
716
|
+
/** Standard HTML elements */
|
|
710
717
|
export type IntrinsicElements =
|
|
711
718
|
// allows custom elements
|
|
712
719
|
Record<string, Attributes> & {
|
|
@@ -722,7 +729,7 @@ export type IntrinsicElements =
|
|
|
722
729
|
bdo: Attributes;
|
|
723
730
|
blockquote: BlockquoteAttributes;
|
|
724
731
|
body: BodyAttributes;
|
|
725
|
-
br: Attributes<Props, undefined>;
|
|
732
|
+
br: Attributes<JSX.Props, undefined>;
|
|
726
733
|
button: ButtonAttributes;
|
|
727
734
|
canvas: CanvasAttributes;
|
|
728
735
|
caption: Attributes;
|
|
@@ -756,7 +763,7 @@ export type IntrinsicElements =
|
|
|
756
763
|
head: Attributes;
|
|
757
764
|
header: Attributes;
|
|
758
765
|
hgroup: Attributes;
|
|
759
|
-
hr: Attributes<Props, undefined>;
|
|
766
|
+
hr: Attributes<JSX.Props, undefined>;
|
|
760
767
|
html: HtmlAttributes;
|
|
761
768
|
iframe: IframeAttributes;
|
|
762
769
|
i: Attributes;
|
|
@@ -820,5 +827,5 @@ export type IntrinsicElements =
|
|
|
820
827
|
ul: Attributes;
|
|
821
828
|
var: Attributes;
|
|
822
829
|
video: VideoAttributes;
|
|
823
|
-
wbr: Attributes<Props, undefined>;
|
|
830
|
+
wbr: Attributes<JSX.Props, undefined>;
|
|
824
831
|
};
|
package/src/jsx/index.ts
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import type { IntrinsicElements as IE } from "./
|
|
4
|
-
import { merge } from "./merge-async-generators.js";
|
|
5
|
-
import { performance } from "node:perf_hooks";
|
|
6
|
-
import { setImmediate } from "node:timers/promises";
|
|
7
|
-
import { types } from "node:util";
|
|
1
|
+
import { Render } from "../render/index.js";
|
|
2
|
+
import type { Util } from "../util/index.js";
|
|
3
|
+
import type { IntrinsicElements as IE } from "./elements.js";
|
|
8
4
|
|
|
9
|
-
/** ovr JSX namespace */
|
|
10
5
|
export namespace JSX {
|
|
11
6
|
/** Standard HTML elements */
|
|
12
7
|
export interface IntrinsicElements extends IE {}
|
|
13
8
|
|
|
14
|
-
/**
|
|
15
|
-
export type Element = MaybeFunction<
|
|
16
|
-
MaybePromise<
|
|
9
|
+
/** JSX Element */
|
|
10
|
+
export type Element = Util.MaybeFunction<
|
|
11
|
+
Util.MaybePromise<
|
|
17
12
|
| string
|
|
18
13
|
| number
|
|
19
14
|
| bigint
|
|
@@ -26,232 +21,96 @@ export namespace JSX {
|
|
|
26
21
|
| AsyncIterable<Element>
|
|
27
22
|
>
|
|
28
23
|
>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Unknown component props */
|
|
32
|
-
export type Props = Record<string, JSX.Element>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* These are the HTML tags that do not require a closing tag.
|
|
36
|
-
*
|
|
37
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
|
|
38
|
-
*/
|
|
39
|
-
const voidElements = new Set([
|
|
40
|
-
"area",
|
|
41
|
-
"base",
|
|
42
|
-
"br",
|
|
43
|
-
"col",
|
|
44
|
-
"embed",
|
|
45
|
-
"hr",
|
|
46
|
-
"img",
|
|
47
|
-
"input",
|
|
48
|
-
"link",
|
|
49
|
-
"meta",
|
|
50
|
-
"source",
|
|
51
|
-
"track",
|
|
52
|
-
"wbr",
|
|
53
|
-
]);
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
57
|
-
* it is passed into this function to be resolved. This function doesn't need to be
|
|
58
|
-
* called recursively, JSX will be transformed into these `jsx()` function calls.
|
|
59
|
-
*
|
|
60
|
-
* @param tag string or function component
|
|
61
|
-
* @param props object containing all the properties and attributes passed to the element or component
|
|
62
|
-
* @returns `AsyncGenerator` that yields `Chunk`s of HTML
|
|
63
|
-
*/
|
|
64
|
-
export async function* jsx<P extends Props = Props>(
|
|
65
|
-
tag: ((props: P) => JSX.Element) | string,
|
|
66
|
-
props: P,
|
|
67
|
-
) {
|
|
68
|
-
if (typeof tag === "function") {
|
|
69
|
-
// component or fragment
|
|
70
|
-
yield* toGenerator(tag(props));
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
24
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
let attributes = "";
|
|
77
|
-
|
|
78
|
-
for (const key in props) {
|
|
79
|
-
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
80
|
-
if (key === "children") continue;
|
|
81
|
-
|
|
82
|
-
const value = props[key];
|
|
83
|
-
|
|
84
|
-
if (value === true) {
|
|
85
|
-
// just put the key without the value
|
|
86
|
-
attributes += ` ${key}`;
|
|
87
|
-
} else if (typeof value === "string") {
|
|
88
|
-
attributes += ` ${key}="${Chunk.escape(value, true)}"`;
|
|
89
|
-
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
90
|
-
attributes += ` ${key}="${value}"`;
|
|
91
|
-
}
|
|
92
|
-
// otherwise, don't include the attribute
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
yield Chunk.safe(`<${tag}${attributes}>`);
|
|
96
|
-
|
|
97
|
-
if (voidElements.has(tag)) return;
|
|
98
|
-
|
|
99
|
-
yield* toGenerator(props.children);
|
|
100
|
-
|
|
101
|
-
yield Chunk.safe(`</${tag}>`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* JSX requires a `Fragment` export to resolve `<></>`
|
|
106
|
-
*
|
|
107
|
-
* @param props containing `children` to render
|
|
108
|
-
* @returns async generator that yields concatenated children
|
|
109
|
-
*/
|
|
110
|
-
export async function* Fragment(props: { children?: JSX.Element } = {}) {
|
|
111
|
-
yield* toGenerator(props.children);
|
|
25
|
+
/** Unknown component props */
|
|
26
|
+
export type Props = Record<string, JSX.Element>;
|
|
112
27
|
}
|
|
113
28
|
|
|
114
|
-
/**
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
29
|
+
/** ovr JSX */
|
|
30
|
+
export class JSX {
|
|
31
|
+
/**
|
|
32
|
+
* These are the HTML tags that do not require a closing tag.
|
|
33
|
+
*
|
|
34
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
|
|
35
|
+
*/
|
|
36
|
+
static readonly #void = new Set([
|
|
37
|
+
"area",
|
|
38
|
+
"base",
|
|
39
|
+
"br",
|
|
40
|
+
"col",
|
|
41
|
+
"embed",
|
|
42
|
+
"hr",
|
|
43
|
+
"img",
|
|
44
|
+
"input",
|
|
45
|
+
"link",
|
|
46
|
+
"meta",
|
|
47
|
+
"source",
|
|
48
|
+
"track",
|
|
49
|
+
"wbr",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
54
|
+
* it is passed into `jsx` to be resolved.
|
|
55
|
+
*
|
|
56
|
+
* @param tag tag name or function component
|
|
57
|
+
* @param props object containing all the properties and attributes passed to the element or component
|
|
58
|
+
* @yields `Chunk`s of HTML
|
|
59
|
+
*/
|
|
60
|
+
static async *jsx<P extends JSX.Props = JSX.Props>(
|
|
61
|
+
tag: ((props: P) => JSX.Element) | string,
|
|
62
|
+
props: P,
|
|
63
|
+
) {
|
|
64
|
+
// this function doesn't need to be called recursively
|
|
65
|
+
// JSX will be transformed into `jsx()` function calls automatically
|
|
66
|
+
|
|
67
|
+
if (typeof tag === "function") {
|
|
68
|
+
// component or fragment
|
|
69
|
+
yield* new Render(tag(props));
|
|
141
70
|
return;
|
|
142
71
|
}
|
|
143
72
|
|
|
144
|
-
if (
|
|
145
|
-
// sync iterable
|
|
146
|
-
if (types.isGeneratorObject(element)) {
|
|
147
|
-
// sync generator - lazily resolve, avoids loading all in memory
|
|
148
|
-
const ms = 8;
|
|
149
|
-
let deadline = performance.now() + ms;
|
|
73
|
+
if (tag === "html") yield Render.html("<!doctype html>");
|
|
150
74
|
|
|
151
|
-
|
|
152
|
-
|
|
75
|
+
// intrinsic element
|
|
76
|
+
// faster to concatenate attributes than to yield them as separate chunks
|
|
77
|
+
let attributes = "";
|
|
153
78
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
await setImmediate();
|
|
158
|
-
deadline = performance.now() + ms;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
79
|
+
for (const key in props) {
|
|
80
|
+
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
81
|
+
if (key === "children") continue;
|
|
164
82
|
|
|
165
|
-
|
|
166
|
-
// process children in parallel
|
|
167
|
-
const generators = Array.from(element, toGenerator);
|
|
168
|
-
const n = generators.length;
|
|
169
|
-
const queue = new Array<Chunk | null>(n);
|
|
170
|
-
const complete = new Uint8Array(n);
|
|
171
|
-
let current = 0;
|
|
83
|
+
const value = props[key];
|
|
172
84
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// yield whatever is in the next queue even if it hasn't completed yet
|
|
181
|
-
yield queue[current]!;
|
|
182
|
-
queue[current] = null;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// if it hasn't completed, stop iterating to the next
|
|
186
|
-
if (!complete[current]) break;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} else if (m.index === current) {
|
|
190
|
-
yield m.result.value; // stream the current value directly
|
|
191
|
-
} else {
|
|
192
|
-
// queue the value for later
|
|
193
|
-
if (queue[m.index]) queue[m.index]!.concat(m.result.value);
|
|
194
|
-
else queue[m.index] = m.result.value;
|
|
195
|
-
}
|
|
85
|
+
if (value === true) {
|
|
86
|
+
// just put the key without the value
|
|
87
|
+
attributes += ` ${key}`;
|
|
88
|
+
} else if (typeof value === "string") {
|
|
89
|
+
attributes += ` ${key}="${Render.escape(value, true)}"`;
|
|
90
|
+
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
91
|
+
attributes += ` ${key}="${value}"`;
|
|
196
92
|
}
|
|
197
|
-
|
|
198
|
-
// clear the queue
|
|
199
|
-
yield* queue.filter((chunk) => chunk !== null);
|
|
200
|
-
return;
|
|
93
|
+
// otherwise, don't include the attribute
|
|
201
94
|
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// primitive or other object
|
|
205
|
-
yield new Chunk(element);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
210
|
-
*
|
|
211
|
-
* ### WARNING
|
|
212
|
-
*
|
|
213
|
-
* This negates streaming benefits and buffers the result into a string.
|
|
214
|
-
* Use `toGenerator` whenever possible.
|
|
215
|
-
*
|
|
216
|
-
* @param element
|
|
217
|
-
* @returns Concatenated HTML
|
|
218
|
-
*/
|
|
219
|
-
export const toString = async (element: JSX.Element) =>
|
|
220
|
-
(await Array.fromAsync(toGenerator(element))).join("");
|
|
221
95
|
|
|
222
|
-
|
|
223
|
-
const encoder = new TextEncoder();
|
|
96
|
+
yield Render.html(`<${tag}${attributes}>`);
|
|
224
97
|
|
|
225
|
-
|
|
226
|
-
* `toGenerator` piped into a `ReadableStream`.
|
|
227
|
-
* Use `toGenerator` when possible to avoid the overhead of the stream.
|
|
228
|
-
*
|
|
229
|
-
* @param element
|
|
230
|
-
* @returns `ReadableStream` of HTML
|
|
231
|
-
*/
|
|
232
|
-
export const toStream = (element: JSX.Element) => {
|
|
233
|
-
const gen = toGenerator(element);
|
|
98
|
+
if (JSX.#void.has(tag)) return;
|
|
234
99
|
|
|
235
|
-
|
|
236
|
-
async pull(c) {
|
|
237
|
-
const result = await gen.next();
|
|
100
|
+
yield* new Render(props.children);
|
|
238
101
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
gen.return();
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
102
|
+
yield Render.html(`</${tag}>`);
|
|
103
|
+
}
|
|
244
104
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
105
|
+
/**
|
|
106
|
+
* JSX requires a `Fragment` export to resolve `<></>`
|
|
107
|
+
*
|
|
108
|
+
* @param props containing `children` to render
|
|
109
|
+
* @yields concatenated children
|
|
110
|
+
*/
|
|
111
|
+
static async *Fragment(props: { children?: JSX.Element }) {
|
|
112
|
+
yield* new Render(props.children);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
252
115
|
|
|
253
|
-
|
|
254
|
-
gen.return();
|
|
255
|
-
},
|
|
256
|
-
});
|
|
257
|
-
};
|
|
116
|
+
export const { jsx, Fragment } = JSX;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Context } from "../context/index.js";
|
|
2
|
+
import type { Trie } from "../trie/index.js";
|
|
3
|
+
|
|
4
|
+
export namespace Middleware {
|
|
5
|
+
/**
|
|
6
|
+
* Middleware context.
|
|
7
|
+
*
|
|
8
|
+
* @template Params Parameters created from a route match
|
|
9
|
+
*/
|
|
10
|
+
export type Context<Params extends Trie.Params = Trie.Params> = InstanceType<
|
|
11
|
+
typeof Context<Params>
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
/** Dispatches the next middleware in the stack */
|
|
15
|
+
export type Next = () => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* App middleware.
|
|
20
|
+
*
|
|
21
|
+
* @template Params Parameters created from a route match
|
|
22
|
+
* @param context Request context
|
|
23
|
+
* @param next Dispatches the next middleware in the stack
|
|
24
|
+
* @returns `Response` or element(s) to stream as HTML
|
|
25
|
+
*/
|
|
26
|
+
export type Middleware<Params extends Trie.Params = Trie.Params> = (
|
|
27
|
+
context: Middleware.Context<Params>,
|
|
28
|
+
next: Middleware.Next,
|
|
29
|
+
) => any;
|