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.
Files changed (86) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +49 -83
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +40 -92
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -6
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +37 -52
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -200
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +341 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +30 -30
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +67 -82
  33. package/dist/util/index.d.ts +38 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +88 -0
  36. package/package.json +3 -3
  37. package/src/app/index.ts +99 -224
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -7
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -223
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +486 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +87 -111
  48. package/src/util/index.ts +112 -0
  49. package/dist/app/context.d.ts +0 -149
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -246
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -51
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -82
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/jsx/chunk/index.d.ts +0 -38
  65. package/dist/jsx/chunk/index.d.ts.map +0 -1
  66. package/dist/jsx/chunk/index.js +0 -69
  67. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  68. package/dist/jsx/merge-async-generators.d.ts +0 -14
  69. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  70. package/dist/jsx/merge-async-generators.js +0 -42
  71. package/dist/types/index.d.ts +0 -17
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/util/hash.d.ts +0 -11
  74. package/dist/util/hash.d.ts.map +0 -1
  75. package/dist/util/hash.js +0 -16
  76. package/src/app/context.ts +0 -303
  77. package/src/app/helper/get.ts +0 -46
  78. package/src/app/helper/index.ts +0 -156
  79. package/src/app/helper/post.ts +0 -54
  80. package/src/app/memo/index.ts +0 -40
  81. package/src/jsx/chunk/index.ts +0 -79
  82. package/src/jsx/merge-async-generators.ts +0 -47
  83. package/src/types/index.ts +0 -42
  84. package/src/util/hash.ts +0 -17
  85. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  86. /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 { jsx, toGenerator, toStream, toString, type JSX } from "./jsx/index.js";
2
- export { Chunk } from "./jsx/chunk/index.js";
3
- export { Trie, Route } from "./trie/index.js";
4
- export { App, type Middleware, type Next } from "./app/index.js";
5
- export { Context } from "./app/context.js";
6
- export { Get } from "./app/helper/get.js";
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, Props } from "./index.js";
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, // for void elements `undefined` is passed in here
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,16 +1,14 @@
1
- import type { MaybeFunction, MaybePromise } from "../types/index.js";
2
- import { Chunk } from "./chunk/index.js";
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 { Util } from "../util/index.js";
3
+ import type { IntrinsicElements as IE } from "./elements.js";
5
4
 
6
- /** ovr JSX namespace */
7
5
  export namespace JSX {
8
6
  /** Standard HTML elements */
9
7
  export interface IntrinsicElements extends IE {}
10
8
 
11
- /** ovr Element */
12
- export type Element = MaybeFunction<
13
- MaybePromise<
9
+ /** JSX Element */
10
+ export type Element = Util.MaybeFunction<
11
+ Util.MaybePromise<
14
12
  | string
15
13
  | number
16
14
  | bigint
@@ -23,236 +21,96 @@ export namespace JSX {
23
21
  | AsyncIterable<Element>
24
22
  >
25
23
  >;
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
24
 
100
- yield Chunk.safe(`</${tag}>`);
25
+ /** Unknown component props */
26
+ export type Props = Record<string, JSX.Element>;
101
27
  }
102
28
 
103
- /**
104
- * JSX requires a `Fragment` export to resolve `<></>`
105
- *
106
- * @param props containing `children` to render
107
- * @returns async generator that yields concatenated children
108
- */
109
- export async function* Fragment(props: { children?: JSX.Element } = {}) {
110
- yield* toGenerator(props.children);
111
- }
112
-
113
- /**
114
- * @param element
115
- * @yields `Chunk`s of HTML as the `Element` resolves
116
- */
117
- export async function* toGenerator(
118
- element: JSX.Element,
119
- ): AsyncGenerator<Chunk, void, unknown> {
120
- // modifications
121
- // these are required to allow functions to be used as children
122
- // instead of creating a separate component to use them
123
- if (typeof element === "function") element = element();
124
- if (element instanceof Promise) element = await element;
125
-
126
- // resolve based on type
127
- // should not render
128
- if (element == null || typeof element === "boolean" || element === "") return;
129
-
130
- if (element instanceof Chunk) {
131
- // already escaped or safe
132
- yield element;
133
- return;
134
- }
135
-
136
- if (typeof element === "object") {
137
- if (Symbol.asyncIterator in element) {
138
- // any async iterable - lazily resolve
139
- for await (const children of element) yield* toGenerator(children);
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));
140
70
  return;
141
71
  }
142
72
 
143
- if (Symbol.iterator in element) {
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
- }
73
+ if (tag === "html") yield Render.html("<!doctype html>");
150
74
 
151
- // other iterable - array, set, etc.
152
- // process children in parallel
153
- const generators = Array.from(element, toGenerator);
154
- const n = generators.length;
155
- const queue = new Array<Chunk | null>(n);
156
- const complete = new Uint8Array(n);
157
- let current = 0;
75
+ // intrinsic element
76
+ // faster to concatenate attributes than to yield them as separate chunks
77
+ let attributes = "";
158
78
 
159
- for await (const m of merge(generators)) {
160
- if (m.result.done) {
161
- complete[m.index] = 1;
79
+ for (const key in props) {
80
+ // more memory efficient to skip children instead of destructuring and using ...rest
81
+ if (key === "children") continue;
162
82
 
163
- if (m.index === current) {
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
- }
83
+ const value = props[key];
170
84
 
171
- // if it hasn't completed, stop iterating to the next
172
- if (!complete[current]) break;
173
- }
174
- }
175
- } else if (m.index === current) {
176
- yield m.result.value; // stream the current value directly
177
- } else {
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
- }
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}"`;
182
92
  }
183
-
184
- // clear the queue
185
- yield* queue.filter((chunk) => chunk !== null);
186
- return;
93
+ // otherwise, don't include the attribute
187
94
  }
188
- }
189
-
190
- // primitive or other object
191
- yield new Chunk(element);
192
- }
193
95
 
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("");
96
+ yield Render.html(`<${tag}${attributes}>`);
207
97
 
208
- /** Single encoder to use across requests. */
209
- const encoder = new TextEncoder();
98
+ if (JSX.#void.has(tag)) return;
210
99
 
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);
100
+ yield* new Render(props.children);
220
101
 
221
- return new ReadableStream<Uint8Array>(
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();
102
+ yield Render.html(`</${tag}>`);
103
+ }
229
104
 
230
- if (result.done) {
231
- c.close();
232
- gen.return();
233
- return;
234
- }
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
+ }
235
115
 
236
- c.enqueue(
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
- };
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;