ovr 5.0.0 → 6.0.1
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 +40 -53
- 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 +340 -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 +36 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +87 -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 -221
- package/src/middleware/index.ts +29 -0
- package/src/multipart/index.ts +485 -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 +109 -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
|
@@ -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,7 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export { Post } from "./app/helper/post.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,14 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import type { IntrinsicElements as IE } from "./intrinsic-elements.js";
|
|
4
|
-
import { merge } from "./merge-async-generators.js";
|
|
1
|
+
import { Render } from "../render/index.js";
|
|
2
|
+
import type { IntrinsicElements as IE } from "./elements.js";
|
|
5
3
|
|
|
6
|
-
/** ovr JSX namespace */
|
|
7
4
|
export namespace JSX {
|
|
5
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
6
|
+
type MaybeFunction<T> = T | (() => T);
|
|
7
|
+
|
|
8
8
|
/** Standard HTML elements */
|
|
9
9
|
export interface IntrinsicElements extends IE {}
|
|
10
10
|
|
|
11
|
-
/**
|
|
11
|
+
/** JSX Element */
|
|
12
12
|
export type Element = MaybeFunction<
|
|
13
13
|
MaybePromise<
|
|
14
14
|
| string
|
|
@@ -23,236 +23,96 @@ export namespace JSX {
|
|
|
23
23
|
| AsyncIterable<Element>
|
|
24
24
|
>
|
|
25
25
|
>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Unknown component props */
|
|
29
|
-
export type Props = Record<string, JSX.Element>;
|
|
30
|
-
|
|
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
|
-
const voidElements = 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 this function to be resolved. This function doesn't need to be
|
|
55
|
-
* called recursively, JSX will be transformed into these `jsx()` function calls.
|
|
56
|
-
*
|
|
57
|
-
* @param tag string or function component
|
|
58
|
-
* @param props object containing all the properties and attributes passed to the element or component
|
|
59
|
-
* @returns `AsyncGenerator` that yields `Chunk`s of HTML
|
|
60
|
-
*/
|
|
61
|
-
export async function* jsx<P extends Props = Props>(
|
|
62
|
-
tag: ((props: P) => JSX.Element) | string,
|
|
63
|
-
props: P,
|
|
64
|
-
) {
|
|
65
|
-
if (typeof tag === "function") {
|
|
66
|
-
// component or fragment
|
|
67
|
-
yield* toGenerator(tag(props));
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// intrinsic element
|
|
72
|
-
// faster to concatenate attributes than to yield them as separate chunks
|
|
73
|
-
let attributes = "";
|
|
74
|
-
|
|
75
|
-
for (const key in props) {
|
|
76
|
-
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
77
|
-
if (key === "children") continue;
|
|
78
|
-
|
|
79
|
-
const value = props[key];
|
|
80
|
-
|
|
81
|
-
if (value === true) {
|
|
82
|
-
// just put the key without the value
|
|
83
|
-
attributes += ` ${key}`;
|
|
84
|
-
} else if (typeof value === "string") {
|
|
85
|
-
attributes += ` ${key}="${Chunk.escape(value, true)}"`;
|
|
86
|
-
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
87
|
-
attributes += ` ${key}="${value}"`;
|
|
88
|
-
}
|
|
89
|
-
// otherwise, don't include the attribute
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (tag === "html") yield Chunk.safe("<!doctype html>");
|
|
93
|
-
|
|
94
|
-
yield Chunk.safe(`<${tag}${attributes}>`);
|
|
95
|
-
|
|
96
|
-
if (voidElements.has(tag)) return;
|
|
97
|
-
|
|
98
|
-
yield* toGenerator(props.children);
|
|
99
26
|
|
|
100
|
-
|
|
27
|
+
/** Unknown component props */
|
|
28
|
+
export type Props = Record<string, JSX.Element>;
|
|
101
29
|
}
|
|
102
30
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
31
|
+
/** ovr JSX */
|
|
32
|
+
export class JSX {
|
|
33
|
+
/**
|
|
34
|
+
* These are the HTML tags that do not require a closing tag.
|
|
35
|
+
*
|
|
36
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
|
|
37
|
+
*/
|
|
38
|
+
static readonly #void = new Set([
|
|
39
|
+
"area",
|
|
40
|
+
"base",
|
|
41
|
+
"br",
|
|
42
|
+
"col",
|
|
43
|
+
"embed",
|
|
44
|
+
"hr",
|
|
45
|
+
"img",
|
|
46
|
+
"input",
|
|
47
|
+
"link",
|
|
48
|
+
"meta",
|
|
49
|
+
"source",
|
|
50
|
+
"track",
|
|
51
|
+
"wbr",
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
56
|
+
* it is passed into `jsx` to be resolved.
|
|
57
|
+
*
|
|
58
|
+
* @param tag tag name or function component
|
|
59
|
+
* @param props object containing all the properties and attributes passed to the element or component
|
|
60
|
+
* @yields `Chunk`s of HTML
|
|
61
|
+
*/
|
|
62
|
+
static async *jsx<P extends JSX.Props = JSX.Props>(
|
|
63
|
+
tag: ((props: P) => JSX.Element) | string,
|
|
64
|
+
props: P,
|
|
65
|
+
) {
|
|
66
|
+
// this function doesn't need to be called recursively
|
|
67
|
+
// JSX will be transformed into `jsx()` function calls automatically
|
|
68
|
+
|
|
69
|
+
if (typeof tag === "function") {
|
|
70
|
+
// component or fragment
|
|
71
|
+
yield* new Render(tag(props));
|
|
140
72
|
return;
|
|
141
73
|
}
|
|
142
74
|
|
|
143
|
-
if (
|
|
144
|
-
// sync iterable
|
|
145
|
-
if ("next" in element) {
|
|
146
|
-
// sync generator - lazily resolve, avoids loading all in memory
|
|
147
|
-
for (const children of element) yield* toGenerator(children);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
75
|
+
if (tag === "html") yield Render.html("<!doctype html>");
|
|
150
76
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const n = generators.length;
|
|
155
|
-
const queue = new Array<Chunk | null>(n);
|
|
156
|
-
const complete = new Uint8Array(n);
|
|
157
|
-
let current = 0;
|
|
77
|
+
// intrinsic element
|
|
78
|
+
// faster to concatenate attributes than to yield them as separate chunks
|
|
79
|
+
let attributes = "";
|
|
158
80
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
81
|
+
for (const key in props) {
|
|
82
|
+
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
83
|
+
if (key === "children") continue;
|
|
162
84
|
|
|
163
|
-
|
|
164
|
-
while (++current < n) {
|
|
165
|
-
if (queue[current]) {
|
|
166
|
-
// yield whatever is in the next queue even if it hasn't completed yet
|
|
167
|
-
yield queue[current]!;
|
|
168
|
-
queue[current] = null;
|
|
169
|
-
}
|
|
85
|
+
const value = props[key];
|
|
170
86
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
// queue the value for later
|
|
179
|
-
if (queue[m.index]) queue[m.index]!.concat(m.result.value);
|
|
180
|
-
else queue[m.index] = m.result.value;
|
|
181
|
-
}
|
|
87
|
+
if (value === true) {
|
|
88
|
+
// just put the key without the value
|
|
89
|
+
attributes += ` ${key}`;
|
|
90
|
+
} else if (typeof value === "string") {
|
|
91
|
+
attributes += ` ${key}="${Render.escape(value, true)}"`;
|
|
92
|
+
} else if (typeof value === "number" || typeof value === "bigint") {
|
|
93
|
+
attributes += ` ${key}="${value}"`;
|
|
182
94
|
}
|
|
183
|
-
|
|
184
|
-
// clear the queue
|
|
185
|
-
yield* queue.filter((chunk) => chunk !== null);
|
|
186
|
-
return;
|
|
95
|
+
// otherwise, don't include the attribute
|
|
187
96
|
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// primitive or other object
|
|
191
|
-
yield new Chunk(element);
|
|
192
|
-
}
|
|
193
97
|
|
|
194
|
-
|
|
195
|
-
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
196
|
-
*
|
|
197
|
-
* ### WARNING
|
|
198
|
-
*
|
|
199
|
-
* This negates streaming benefits and buffers the result into a string.
|
|
200
|
-
* Use `toGenerator` whenever possible.
|
|
201
|
-
*
|
|
202
|
-
* @param element
|
|
203
|
-
* @returns Concatenated HTML
|
|
204
|
-
*/
|
|
205
|
-
export const toString = async (element: JSX.Element) =>
|
|
206
|
-
(await Array.fromAsync(toGenerator(element))).join("");
|
|
98
|
+
yield Render.html(`<${tag}${attributes}>`);
|
|
207
99
|
|
|
208
|
-
|
|
209
|
-
const encoder = new TextEncoder();
|
|
100
|
+
if (JSX.#void.has(tag)) return;
|
|
210
101
|
|
|
211
|
-
|
|
212
|
-
* `toGenerator` piped into a `ReadableStream`.
|
|
213
|
-
* Use `toGenerator` when possible to avoid the overhead of the stream.
|
|
214
|
-
*
|
|
215
|
-
* @param element
|
|
216
|
-
* @returns `ReadableStream` of HTML
|
|
217
|
-
*/
|
|
218
|
-
export const toStream = (element: JSX.Element) => {
|
|
219
|
-
const gen = toGenerator(element);
|
|
102
|
+
yield* new Render(props.children);
|
|
220
103
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// enables zero-copy transfer from underlying source when queue is empty
|
|
224
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_byte_streams#overview
|
|
225
|
-
type: "bytes",
|
|
226
|
-
// `pull` ensures backpressure and cancelled requests stop the generator
|
|
227
|
-
async pull(c) {
|
|
228
|
-
const result = await gen.next();
|
|
104
|
+
yield Render.html(`</${tag}>`);
|
|
105
|
+
}
|
|
229
106
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
107
|
+
/**
|
|
108
|
+
* JSX requires a `Fragment` export to resolve `<></>`
|
|
109
|
+
*
|
|
110
|
+
* @param props containing `children` to render
|
|
111
|
+
* @yields concatenated children
|
|
112
|
+
*/
|
|
113
|
+
static async *Fragment(props: { children?: JSX.Element }) {
|
|
114
|
+
yield* new Render(props.children);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
235
117
|
|
|
236
|
-
|
|
237
|
-
// need to encode for Node (ex: during prerendering) or it will error
|
|
238
|
-
// doesn't seem to be needed for browsers
|
|
239
|
-
// faster than piping through a `TextEncoderStream`
|
|
240
|
-
encoder.encode(result.value.value),
|
|
241
|
-
);
|
|
242
|
-
},
|
|
243
|
-
cancel() {
|
|
244
|
-
gen.return();
|
|
245
|
-
},
|
|
246
|
-
},
|
|
247
|
-
{
|
|
248
|
-
// `highWaterMark` defaults to 1
|
|
249
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#highwatermark
|
|
250
|
-
// setting this ensures at least a small buffer is maintained if the
|
|
251
|
-
// underlying server does not have its own high water mark set
|
|
252
|
-
// https://blog.cloudflare.com/unpacking-cloudflare-workers-cpu-performance-benchmarks/#inefficient-streams-adapters
|
|
253
|
-
// in Node, the default is 16kb, so this stacks another 2kb in front
|
|
254
|
-
// https://nodejs.org/api/http.html#outgoingmessagewritablehighwatermark
|
|
255
|
-
highWaterMark: 2048,
|
|
256
|
-
},
|
|
257
|
-
);
|
|
258
|
-
};
|
|
118
|
+
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;
|