ovr 2.1.0 → 3.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.
Files changed (67) hide show
  1. package/dist/app/action.d.ts +2 -1
  2. package/dist/app/action.d.ts.map +1 -0
  3. package/dist/app/action.js +1 -1
  4. package/dist/app/context.d.ts +1 -0
  5. package/dist/app/context.d.ts.map +1 -0
  6. package/dist/app/context.js +9 -4
  7. package/dist/app/index.d.ts +1 -0
  8. package/dist/app/index.d.ts.map +1 -0
  9. package/dist/app/index.js +1 -1
  10. package/dist/{memo → app/memo}/index.d.ts +1 -0
  11. package/dist/app/memo/index.d.ts.map +1 -0
  12. package/dist/{mw → app/mw}/csrf.d.ts +2 -1
  13. package/dist/app/mw/csrf.d.ts.map +1 -0
  14. package/dist/app/page.d.ts +2 -1
  15. package/dist/app/page.d.ts.map +1 -0
  16. package/dist/index.d.ts +3 -2
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +2 -2
  19. package/dist/jsx/chunk/index.d.ts +29 -0
  20. package/dist/jsx/chunk/index.d.ts.map +1 -0
  21. package/dist/jsx/chunk/index.js +61 -0
  22. package/dist/jsx/index.d.ts +17 -12
  23. package/dist/jsx/index.d.ts.map +1 -0
  24. package/dist/jsx/index.js +95 -79
  25. package/dist/jsx/{elements.d.ts → intrinsic-elements.d.ts} +2 -1
  26. package/dist/jsx/intrinsic-elements.d.ts.map +1 -0
  27. package/dist/jsx/{merge-async-iterables.d.ts → merge-async-generators.d.ts} +1 -0
  28. package/dist/jsx/merge-async-generators.d.ts.map +1 -0
  29. package/dist/jsx-dev-runtime.d.ts +1 -0
  30. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  31. package/dist/jsx-runtime.d.ts +1 -0
  32. package/dist/jsx-runtime.d.ts.map +1 -0
  33. package/dist/trie/index.d.ts +1 -0
  34. package/dist/trie/index.d.ts.map +1 -0
  35. package/dist/trie/insert-params.d.ts +1 -0
  36. package/dist/trie/insert-params.d.ts.map +1 -0
  37. package/dist/types/index.d.ts +1 -0
  38. package/dist/types/index.d.ts.map +1 -0
  39. package/dist/util/hash.d.ts +11 -0
  40. package/dist/util/hash.d.ts.map +1 -0
  41. package/dist/util/hash.js +16 -0
  42. package/package.json +4 -2
  43. package/src/app/action.ts +81 -0
  44. package/src/app/context.ts +329 -0
  45. package/src/app/index.ts +302 -0
  46. package/src/app/memo/index.ts +40 -0
  47. package/src/app/mw/csrf.ts +28 -0
  48. package/src/app/page.ts +56 -0
  49. package/src/index.ts +9 -0
  50. package/src/jsx/chunk/index.ts +70 -0
  51. package/src/jsx/index.ts +235 -0
  52. package/src/jsx/intrinsic-elements.ts +812 -0
  53. package/src/jsx/merge-async-generators.ts +42 -0
  54. package/src/jsx-dev-runtime.ts +1 -0
  55. package/src/jsx-runtime.ts +1 -0
  56. package/src/trie/index.ts +335 -0
  57. package/src/trie/insert-params.ts +31 -0
  58. package/src/types/index.ts +23 -0
  59. package/src/util/hash.ts +17 -0
  60. package/dist/escape/index.d.ts +0 -8
  61. package/dist/escape/index.js +0 -33
  62. package/dist/hash/index.d.ts +0 -1
  63. package/dist/hash/index.js +0 -9
  64. /package/dist/{memo → app/memo}/index.js +0 -0
  65. /package/dist/{mw → app/mw}/csrf.js +0 -0
  66. /package/dist/jsx/{elements.js → intrinsic-elements.js} +0 -0
  67. /package/dist/jsx/{merge-async-iterables.js → merge-async-generators.js} +0 -0
@@ -0,0 +1,235 @@
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";
5
+
6
+ /** ovr JSX namespace */
7
+ export namespace JSX {
8
+ /** Standard HTML elements */
9
+ export interface IntrinsicElements extends IE {}
10
+
11
+ /** ovr Element */
12
+ export type Element = MaybeFunction<
13
+ MaybePromise<
14
+ | string
15
+ | number
16
+ | bigint
17
+ | boolean
18
+ | object
19
+ | null
20
+ | undefined
21
+ | Symbol
22
+ | Iterable<Element>
23
+ | AsyncIterable<Element>
24
+ >
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.
55
+ *
56
+ * @param tag string or function component
57
+ * @param props object containing all the properties and attributes passed to the element or component
58
+ * @returns an async generator that yields `Chunk`s of HTML
59
+ */
60
+ export async function* jsx<P extends Props = Props>(
61
+ tag: ((props: P) => JSX.Element) | string,
62
+ props: P,
63
+ ) {
64
+ if (typeof tag === "function") {
65
+ // component or fragment
66
+ yield* toGenerator(tag(props));
67
+
68
+ return;
69
+ }
70
+
71
+ // intrinsic element
72
+ const { children, ...rest } = props;
73
+
74
+ const attributes: string[] = [];
75
+
76
+ for (let key in rest) {
77
+ const value = rest[key]; // needs to come before reassigning keys
78
+
79
+ if (key === "className") key = "class";
80
+ else if (key === "htmlFor") key = "for";
81
+
82
+ if (value === true) {
83
+ // just put the key without the value
84
+ attributes.push(` ${key}`);
85
+ } else if (
86
+ typeof value === "string" ||
87
+ typeof value === "number" ||
88
+ typeof value === "bigint"
89
+ ) {
90
+ attributes.push(` ${key}="${Chunk.escape(String(value), true)}"`);
91
+ }
92
+ // otherwise, don't include the attribute
93
+ }
94
+
95
+ yield new Chunk(`<${tag}${attributes.join("")}>`, true);
96
+
97
+ if (voidElements.has(tag)) return;
98
+
99
+ yield* toGenerator(children);
100
+
101
+ yield new Chunk(`</${tag}>`, true);
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);
112
+ }
113
+
114
+ /**
115
+ * @param element
116
+ * @yields Chunks of HTML as the `Element` resolves.
117
+ */
118
+ export async function* toGenerator(
119
+ element: JSX.Element,
120
+ ): AsyncGenerator<Chunk, void, unknown> {
121
+ // modifications
122
+ // these are required to allow functions to be used as children
123
+ // instead of creating a separate component to use them
124
+ if (typeof element === "function") element = element();
125
+ if (element instanceof Promise) element = await element;
126
+
127
+ // resolve based on type
128
+ // should not render
129
+ if (element == null || typeof element === "boolean" || element === "") return;
130
+
131
+ if (element instanceof Chunk) {
132
+ // already escaped or safe
133
+ yield element;
134
+
135
+ return;
136
+ }
137
+
138
+ if (typeof element === "object") {
139
+ if (Symbol.asyncIterator in element) {
140
+ // async iterable - lazily resolve
141
+ for await (const children of element) yield* toGenerator(children);
142
+
143
+ return;
144
+ }
145
+
146
+ if (Symbol.iterator in element) {
147
+ // sync iterable
148
+ const iterator = element[Symbol.iterator]();
149
+
150
+ if (
151
+ typeof iterator.next === "function" &&
152
+ typeof iterator.throw === "function" &&
153
+ typeof iterator.return === "function"
154
+ ) {
155
+ // sync generator
156
+ // process lazily - avoids loading all in memory
157
+ while (true) {
158
+ const result = iterator.next();
159
+ if (result.done) break;
160
+ yield* toGenerator(result.value);
161
+ }
162
+
163
+ return;
164
+ }
165
+
166
+ // other iterable - array, set, etc.
167
+ // process children in parallel
168
+ const generators: AsyncGenerator<Chunk, void, unknown>[] = [];
169
+
170
+ while (true) {
171
+ const result = iterator.next();
172
+ if (result.done) break;
173
+ generators.push(toGenerator(result.value));
174
+ }
175
+
176
+ const queue: (Chunk | null)[] = new Array(generators.length).fill(null);
177
+
178
+ let current = 0;
179
+ const complete = new Set<number>();
180
+
181
+ for await (const {
182
+ index,
183
+ result: { done, value: chunk },
184
+ } of merge(generators)) {
185
+ if (done) {
186
+ complete.add(index);
187
+
188
+ if (index === current) {
189
+ while (++current < generators.length) {
190
+ if (queue[current]) {
191
+ // yield whatever is in the next queue even if it hasn't completed yet
192
+ yield queue[current]!;
193
+ queue[current] = null;
194
+ }
195
+
196
+ // if it hasn't completed, stop iterating to the next
197
+ if (!complete.has(current)) break;
198
+ }
199
+ }
200
+ } else if (index === current) {
201
+ yield chunk; // stream the current value directly
202
+ } else {
203
+ // queue the value for later
204
+ if (queue[index]) queue[index].concat(chunk);
205
+ else queue[index] = chunk;
206
+ }
207
+ }
208
+
209
+ // clear the queue
210
+ yield* queue.filter(Boolean) as Chunk[];
211
+
212
+ return;
213
+ }
214
+ }
215
+
216
+ // primitive or other object
217
+ yield new Chunk(element);
218
+ }
219
+
220
+ /**
221
+ * Converts a `JSX.Element` into a fully concatenated string of HTML.
222
+ *
223
+ * ### WARNING
224
+ *
225
+ * This negates streaming benefits and buffers the result into a string.
226
+ * Use `toGenerator` whenever possible.
227
+ *
228
+ * @param element
229
+ * @returns Concatenated HTML
230
+ */
231
+ export const toString = async (element: JSX.Element) => {
232
+ const buffer: Chunk[] = [];
233
+ for await (const chunk of toGenerator(element)) buffer.push(chunk);
234
+ return buffer.join("");
235
+ };