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,42 @@
1
+ const never = new Promise(() => {}) as Promise<any>;
2
+
3
+ const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
4
+ index,
5
+ result: await iterator.next(),
6
+ });
7
+
8
+ /**
9
+ * Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
10
+ * The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
11
+ *
12
+ * Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
13
+ *
14
+ * @param generators Resolved in parallel.
15
+ * @param returnValue Passed into each `iterator.return()`
16
+ * @yields `IteratorResult` and `index` of the resolved generator.
17
+ */
18
+ export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
19
+ const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
20
+ const promises = iterators.map(next);
21
+
22
+ let remaining = promises.length;
23
+ let current: Awaited<ReturnType<typeof next>>;
24
+
25
+ try {
26
+ while (remaining) {
27
+ yield (current = await Promise.race(promises));
28
+
29
+ if (current.result.done) {
30
+ promises[current.index] = never;
31
+ remaining--;
32
+ } else {
33
+ promises[current.index] = next(
34
+ iterators[current.index]!,
35
+ current.index,
36
+ );
37
+ }
38
+ }
39
+ } finally {
40
+ for (const iter of iterators) iter.return().catch(); // could have already returned
41
+ }
42
+ }
@@ -0,0 +1 @@
1
+ export { jsx as jsxDEV, Fragment, type JSX } from "./jsx/index.js";
@@ -0,0 +1 @@
1
+ export { jsx, jsx as jsxs, Fragment, type JSX } from "./jsx/index.js";
@@ -0,0 +1,335 @@
1
+ export type Params = Record<string, string>;
2
+
3
+ export class Route<Store> {
4
+ /** Route pattern */
5
+ pattern: string;
6
+
7
+ /** Store returned when route is found */
8
+ store: Store;
9
+
10
+ constructor(pattern: string, store: Store) {
11
+ if (pattern[0] !== "/") {
12
+ throw new Error(
13
+ `Invalid route: ${pattern} - route pattern must begin with "/"`,
14
+ );
15
+ }
16
+
17
+ this.pattern = pattern;
18
+ this.store = store;
19
+ }
20
+ }
21
+
22
+ class ParamNode<Store> {
23
+ /** Name of the parameter (without the colon ":") */
24
+ name: string;
25
+
26
+ /** Matched route */
27
+ route: Route<Store> | null = null;
28
+
29
+ /** Static child node */
30
+ staticChild: Trie<Store> | null = null;
31
+
32
+ constructor(name: string) {
33
+ this.name = name;
34
+ }
35
+ }
36
+
37
+ export class Trie<Store> {
38
+ /** Unique segment of the pattern trie */
39
+ segment: string;
40
+
41
+ /** Static child node map, key is the first character in the segment */
42
+ staticMap: Map<number, Trie<Store>> | null = null;
43
+
44
+ /** Parametric child node */
45
+ paramChild: ParamNode<Store> | null = null;
46
+
47
+ /** Matched route */
48
+ route: Route<Store> | null = null;
49
+
50
+ /** Matched wildcard route */
51
+ wildcardRoute: Route<Store> | null = null;
52
+
53
+ /**
54
+ * @param segment pattern segment
55
+ * @param staticChildren static children nodes to add to staticMap
56
+ */
57
+ constructor(segment = "/", staticChildren?: Trie<Store>[]) {
58
+ this.segment = segment;
59
+
60
+ if (staticChildren?.length) {
61
+ this.staticMap ??= new Map();
62
+
63
+ for (const child of staticChildren) {
64
+ this.staticMap.set(child.segment.charCodeAt(0), child);
65
+ }
66
+ }
67
+ }
68
+
69
+ /**
70
+ * @param segment new segment
71
+ * @returns a clone of the Node with a new segment
72
+ */
73
+ clone(segment: string) {
74
+ const clone = new Trie(segment);
75
+
76
+ clone.staticMap = this.staticMap;
77
+ clone.paramChild = this.paramChild;
78
+ clone.route = this.route;
79
+ clone.wildcardRoute = this.wildcardRoute;
80
+
81
+ return clone;
82
+ }
83
+
84
+ /**
85
+ * If the current segment is "api/posts/"
86
+ * and "api/movies/" is added,
87
+ * the node will need to be reassigned to "api/" and create two static children.
88
+ *
89
+ * @param charIndex where to split the node
90
+ * @param segment new segment to use
91
+ * @returns the new child produced from the new segment
92
+ */
93
+ fork(charIndex: number, segment: string) {
94
+ const existingChild = this.clone(this.segment.slice(charIndex)); // "posts/"
95
+ const newChild = new Trie<Store>(segment.slice(charIndex)); // "movies/"
96
+
97
+ Object.assign(
98
+ this,
99
+ // "api/" with the above as children
100
+ new Trie(this.segment.slice(0, charIndex), [existingChild, newChild]),
101
+ );
102
+
103
+ return newChild;
104
+ }
105
+
106
+ /**
107
+ * node.segment = "static/static",
108
+ * staticSegment = "static/",
109
+ * then the node needs to be split to accommodate the shorter segment
110
+ *
111
+ * @param segment
112
+ */
113
+ split(segment: string) {
114
+ const secondHalf = this.clone(this.segment.slice(segment.length));
115
+
116
+ Object.assign(this, new Trie(segment, [secondHalf]));
117
+ }
118
+
119
+ /**
120
+ * @param name name of the param
121
+ * @returns the existing child with the same name, or creates a new
122
+ */
123
+ setParamChild(name: string) {
124
+ if (this.paramChild && this.paramChild.name !== name) {
125
+ throw new Error(
126
+ `Cannot create parameter "${name}" because a different parameter ` +
127
+ `("${this.paramChild.name}") already exists in this location.\n\n${this}`,
128
+ );
129
+ }
130
+
131
+ return (this.paramChild ??= new ParamNode<Store>(name));
132
+ }
133
+
134
+ /**
135
+ * @param route route return when pattern is matched
136
+ * @returns this - the Node
137
+ */
138
+ add(route: Route<Store>) {
139
+ let current: Trie<Store> = this;
140
+ let pattern = route.pattern; // created to not modify the original
141
+
142
+ const endsWithWildcard = pattern.endsWith("*");
143
+ if (endsWithWildcard) pattern = pattern.slice(0, -1);
144
+
145
+ const paramSegments = pattern.match(/:.+?(?=\/|$)/g) ?? []; // match the params
146
+ const staticSegments = pattern.split(/:.+?(?=\/|$)/); // split on the params
147
+
148
+ // if the last segment is a param without a trailing slash
149
+ // then there will be an empty string, remove
150
+ if (staticSegments.at(-1) === "") staticSegments.pop();
151
+
152
+ let paramIndex = 0;
153
+ // for each static segment, if there are no static segments, this is skipped
154
+ for (
155
+ let staticIndex = 0;
156
+ staticIndex < staticSegments.length;
157
+ staticIndex++
158
+ ) {
159
+ let staticSegment = staticSegments[staticIndex]!;
160
+
161
+ if (staticIndex > 0) {
162
+ // there is only a second static segment (could just be "/")
163
+ // if there is a param to split them, so there must be a param here
164
+
165
+ const paramChild = current.setParamChild(
166
+ // param without the ":" (only increment when this is reached)
167
+ paramSegments[paramIndex++]!.slice(1),
168
+ );
169
+
170
+ if (!paramChild.staticChild) {
171
+ // new - create node with the next static segment
172
+ current = paramChild.staticChild = new Trie<Store>(staticSegment);
173
+ continue; // next segment - no need to check since it's new
174
+ }
175
+
176
+ // there's already a static child - need to check if it's a match
177
+ current = paramChild.staticChild;
178
+ }
179
+
180
+ // check if the staticSegment matches the current node
181
+ for (let charIndex = 0; ; ) {
182
+ if (charIndex === staticSegment.length) {
183
+ // finished iterating through the staticSegment
184
+ if (charIndex < current.segment.length) {
185
+ // too short
186
+ current.split(staticSegment);
187
+ }
188
+
189
+ break; // next segment
190
+ }
191
+
192
+ if (charIndex === current.segment.length) {
193
+ // passed the end of the current node
194
+ if (!current.staticMap) {
195
+ // new pattern, create new leaf
196
+ current.staticMap = new Map();
197
+ } else {
198
+ // there's already static children,
199
+ // check to see if there's a leaf that starts with the char
200
+ const staticChild = current.staticMap.get(
201
+ staticSegment.charCodeAt(charIndex),
202
+ );
203
+
204
+ if (staticChild) {
205
+ // re-run loop with existing staticChild
206
+ current = staticChild;
207
+ staticSegment = staticSegment.slice(charIndex);
208
+ charIndex = 0;
209
+ continue;
210
+ }
211
+ }
212
+
213
+ // otherwise, add new staticChild
214
+ const staticChild = new Trie<Store>(staticSegment.slice(charIndex));
215
+ current.staticMap.set(
216
+ staticSegment.charCodeAt(charIndex),
217
+ staticChild,
218
+ );
219
+ current = staticChild;
220
+
221
+ break; // next segment
222
+ }
223
+
224
+ if (staticSegment[charIndex] !== current.segment[charIndex]) {
225
+ // different than the node - fork
226
+ current = current.fork(charIndex, staticSegment);
227
+
228
+ break; // next segment
229
+ }
230
+
231
+ // character is the same - rerun to check next char
232
+ charIndex++;
233
+ }
234
+ }
235
+
236
+ if (paramIndex < paramSegments.length) {
237
+ // final segment is a param
238
+ current.setParamChild(paramSegments[paramIndex]!.slice(1)).route ??=
239
+ route;
240
+ } else if (endsWithWildcard) {
241
+ // final segment is a wildcard
242
+ current.wildcardRoute ??= route;
243
+ } else {
244
+ // final segment is static
245
+ current.route ??= route;
246
+ }
247
+
248
+ return this;
249
+ }
250
+
251
+ /**
252
+ * @param pathname Path to find
253
+ * @returns `Route` and the matched `params` if found, otherwise `null`
254
+ */
255
+ find(pathname: string): { route: Route<Store>; params: Params } | null {
256
+ if (
257
+ // too short
258
+ pathname.length < this.segment.length ||
259
+ // segment does not match current node segment
260
+ !pathname.startsWith(this.segment)
261
+ ) {
262
+ return null;
263
+ }
264
+
265
+ if (pathname === this.segment) {
266
+ // reached the end of the path
267
+ if (this.route) return { route: this.route, params: {} };
268
+
269
+ if (this.wildcardRoute)
270
+ return { route: this.wildcardRoute, params: { "*": "" } };
271
+
272
+ return null;
273
+ }
274
+
275
+ if (this.staticMap) {
276
+ // check for a static leaf that starts with the first character
277
+ const staticChild = this.staticMap.get(
278
+ pathname.charCodeAt(this.segment.length),
279
+ );
280
+
281
+ if (staticChild) {
282
+ const result = staticChild.find(pathname.slice(this.segment.length));
283
+ if (result) return result;
284
+ }
285
+ }
286
+
287
+ // check for param leaf
288
+ if (this.paramChild) {
289
+ const slashIndex = pathname.indexOf("/", this.segment.length);
290
+
291
+ // if there is not a slash immediately following this.segment
292
+ if (slashIndex !== this.segment.length) {
293
+ // there is a valid parameter
294
+ if (
295
+ // param is the end of the pathname
296
+ slashIndex === -1 &&
297
+ this.paramChild.route
298
+ ) {
299
+ return {
300
+ route: this.paramChild.route,
301
+ params: {
302
+ [this.paramChild.name]: pathname.slice(this.segment.length),
303
+ },
304
+ };
305
+ } else if (this.paramChild.staticChild) {
306
+ // there's a static node after the param
307
+ // this is how there can be multiple params, "/" in between
308
+ const result = this.paramChild.staticChild.find(
309
+ pathname.slice(slashIndex),
310
+ );
311
+
312
+ if (result) {
313
+ // add original params to the result
314
+ result.params[this.paramChild.name] = pathname.slice(
315
+ this.segment.length,
316
+ slashIndex,
317
+ );
318
+
319
+ return result;
320
+ }
321
+ }
322
+ }
323
+ }
324
+
325
+ // check for wildcard leaf
326
+ if (this.wildcardRoute) {
327
+ return {
328
+ route: this.wildcardRoute,
329
+ params: { "*": pathname.slice(this.segment.length) },
330
+ };
331
+ }
332
+
333
+ return null;
334
+ }
335
+ }
@@ -0,0 +1,31 @@
1
+ import type { Params } from "./index.js";
2
+
3
+ /**
4
+ * @param parts Pattern parts
5
+ * @param params Parameters to insert
6
+ * @returns Resolved pathname with params
7
+ */
8
+ export const insertParams = (parts: string[], params: Params) => {
9
+ const wild = "*";
10
+
11
+ return parts
12
+ .map((part) => {
13
+ if (part.startsWith(":")) {
14
+ const param = part.slice(1);
15
+
16
+ if (!(param in params))
17
+ throw new Error(`Parameter "${param}" did not match pattern.`);
18
+
19
+ return params[param as keyof typeof params];
20
+ }
21
+
22
+ if (part === wild) {
23
+ if (!(wild in params)) throw new Error("No wildcard parameter found.");
24
+
25
+ return params[wild];
26
+ }
27
+
28
+ return part;
29
+ })
30
+ .join("/");
31
+ };
@@ -0,0 +1,23 @@
1
+ export type MaybePromise<T> = T | Promise<T>;
2
+
3
+ export type MaybeFunction<T> = T | (() => T);
4
+
5
+ export type DeepArray<T> = T | DeepArray<T>[];
6
+
7
+ export type ExtractParams<Pattern extends string = string> =
8
+ Pattern extends `${infer _Start}:${infer Param}/${infer Rest}`
9
+ ? { [k in Param | keyof ExtractParams<Rest>]: string }
10
+ : Pattern extends `${infer _Start}:${infer Param}`
11
+ ? { [k in Param]: string }
12
+ : Pattern extends `${infer _Rest}*`
13
+ ? { "*": string }
14
+ : {};
15
+
16
+ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
17
+ infer First extends string,
18
+ ...infer Rest extends string[],
19
+ ]
20
+ ? Rest["length"] extends 0
21
+ ? ExtractParams<First>
22
+ : ExtractParams<First> | ExtractMultiParams<Rest>
23
+ : never;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
3
+ *
4
+ * Adapted from
5
+ * [SvelteKit](https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js)
6
+ *
7
+ * @param s String to hash
8
+ * @returns Hashed string
9
+ */
10
+ export const hash = (s: string) => {
11
+ let hash = 5381;
12
+
13
+ let i = s.length;
14
+ while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
15
+
16
+ return (hash >>> 0).toString(36);
17
+ };
@@ -1,8 +0,0 @@
1
- /**
2
- * Escapes strings of HTML.
3
- *
4
- * @param value Converted to a string, then escaped.
5
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
6
- * @returns Escaped string of HTML
7
- */
8
- export declare const escape: (value: unknown, attr?: boolean) => string;
@@ -1,33 +0,0 @@
1
- const attrRegex = /[&"<]/g;
2
- const contentRegex = /[&<]/g;
3
- /**
4
- * Escapes strings of HTML.
5
- *
6
- * @param value Converted to a string, then escaped.
7
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
8
- * @returns Escaped string of HTML
9
- */
10
- export const escape = (value, attr) => {
11
- // copied and adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
12
- const s = String(value ?? "");
13
- const regex = attr ? attrRegex : contentRegex;
14
- // search starts at beginning of the string
15
- regex.lastIndex = 0;
16
- // tracks the position of the last successful match in the input string
17
- let start = 0;
18
- let result = "";
19
- // since `g` flag is used, regex maintains state with each loop,
20
- // checking from the lastIndex onward each time
21
- while (regex.test(s)) {
22
- // index of the match
23
- const i = regex.lastIndex - 1;
24
- const match = s[i];
25
- result +=
26
- // everything that didn't match during this test
27
- s.slice(start, i) +
28
- // replacement
29
- (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
30
- start = regex.lastIndex;
31
- }
32
- return result + s.slice(start);
33
- };
@@ -1 +0,0 @@
1
- export declare const hash: (s: string) => string;
@@ -1,9 +0,0 @@
1
- // Fast hashing algorithm http://www.cse.yorku.ca/~oz/hash.html
2
- // Adapted from SvelteKit https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js
3
- export const hash = (s) => {
4
- let hash = 5381;
5
- let i = s.length;
6
- while (i)
7
- hash = (hash * 33) ^ s.charCodeAt(--i);
8
- return (hash >>> 0).toString(36);
9
- };
File without changes
File without changes
File without changes