ovr 1.1.0 → 1.2.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/jsx/index.d.ts +3 -3
- package/dist/jsx/index.js +11 -11
- package/dist/jsx/merge-async-iterables.d.ts +9 -8
- package/dist/jsx/merge-async-iterables.js +23 -32
- package/dist/router/context.js +1 -1
- package/dist/router/index.d.ts +7 -0
- package/dist/router/index.js +13 -1
- package/package.json +1 -1
package/dist/jsx/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type FC<Props> = (props: Props) => JSX.Element;
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerator<string, void, unknown>;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* JSX requires a `Fragment` export to resolve <></>
|
|
21
21
|
*
|
|
22
22
|
* @param props containing `children` to render
|
|
23
23
|
* @returns async generator that yields concatenated children
|
|
@@ -27,11 +27,11 @@ export declare function Fragment(props?: {
|
|
|
27
27
|
}): AsyncGenerator<string, void, unknown>;
|
|
28
28
|
/**
|
|
29
29
|
* @param element
|
|
30
|
-
* @
|
|
30
|
+
* @yields Rendered strings of HTML as the `Element` resolves.
|
|
31
31
|
*/
|
|
32
32
|
export declare function toGenerator(element: JSX.Element): AsyncGenerator<string, void, unknown>;
|
|
33
33
|
/**
|
|
34
|
-
* Converts a `JSX.Element` into
|
|
34
|
+
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
35
35
|
*
|
|
36
36
|
* > [!WARNING]
|
|
37
37
|
* >
|
package/dist/jsx/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { merge } from "./merge-async-iterables.js";
|
|
2
2
|
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
|
|
3
3
|
const voidElements = new Set([
|
|
4
4
|
"area",
|
|
@@ -32,7 +32,7 @@ export async function* jsx(tag, props) {
|
|
|
32
32
|
const { children, ...attrs } = props;
|
|
33
33
|
let attrStr = "";
|
|
34
34
|
for (let key in attrs) {
|
|
35
|
-
const value = attrs[key];
|
|
35
|
+
const value = attrs[key]; // needs to come before reassigning keys
|
|
36
36
|
if (key === "className")
|
|
37
37
|
key = "class";
|
|
38
38
|
else if (key === "htmlFor")
|
|
@@ -57,7 +57,7 @@ export async function* jsx(tag, props) {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
60
|
+
* JSX requires a `Fragment` export to resolve <></>
|
|
61
61
|
*
|
|
62
62
|
* @param props containing `children` to render
|
|
63
63
|
* @returns async generator that yields concatenated children
|
|
@@ -67,15 +67,15 @@ export async function* Fragment(props = {}) {
|
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* @param element
|
|
70
|
-
* @
|
|
70
|
+
* @yields Rendered strings of HTML as the `Element` resolves.
|
|
71
71
|
*/
|
|
72
72
|
export async function* toGenerator(element) {
|
|
73
73
|
if (typeof element === "function")
|
|
74
74
|
element = element();
|
|
75
75
|
if (element instanceof Promise)
|
|
76
76
|
element = await element;
|
|
77
|
-
// undefined, null, or
|
|
78
|
-
if (element == null || element ===
|
|
77
|
+
// undefined, null, or boolean should not render
|
|
78
|
+
if (element == null || typeof element === "boolean")
|
|
79
79
|
return;
|
|
80
80
|
if (typeof element === "object") {
|
|
81
81
|
if (Symbol.asyncIterator in element) {
|
|
@@ -89,8 +89,8 @@ export async function* toGenerator(element) {
|
|
|
89
89
|
const queue = new Array(generators.length).fill("");
|
|
90
90
|
const complete = new Set();
|
|
91
91
|
let current = 0;
|
|
92
|
-
for await (const { index,
|
|
93
|
-
if (done) {
|
|
92
|
+
for await (const { index, result } of merge(generators)) {
|
|
93
|
+
if (result.done) {
|
|
94
94
|
complete.add(index);
|
|
95
95
|
if (index === current) {
|
|
96
96
|
while (++current < generators.length) {
|
|
@@ -106,10 +106,10 @@ export async function* toGenerator(element) {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
else if (index === current) {
|
|
109
|
-
yield value; // stream the current value directly
|
|
109
|
+
yield result.value; // stream the current value directly
|
|
110
110
|
}
|
|
111
111
|
else {
|
|
112
|
-
queue[index] += value; // queue the value for later
|
|
112
|
+
queue[index] += result.value; // queue the value for later
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
yield queue.join(""); // clear the queue
|
|
@@ -123,7 +123,7 @@ export async function* toGenerator(element) {
|
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
|
-
* Converts a `JSX.Element` into
|
|
126
|
+
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
127
127
|
*
|
|
128
128
|
* > [!WARNING]
|
|
129
129
|
* >
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Merges `AsyncIterable[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
3
|
+
* The return of each `AsyncIterable` is yielded from the generator with `done: true`.
|
|
4
|
+
*
|
|
5
|
+
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
6
|
+
*
|
|
7
|
+
* @param iterables Resolved in parallel.
|
|
8
|
+
* @yields `IteratorResult` and `index` of the resolved iterator.
|
|
3
9
|
*/
|
|
4
|
-
export declare function
|
|
10
|
+
export declare function merge<T, R>(iterables: AsyncIterable<T, R>[]): AsyncGenerator<{
|
|
5
11
|
index: number;
|
|
6
|
-
|
|
7
|
-
value?: undefined;
|
|
8
|
-
} | {
|
|
9
|
-
index: number;
|
|
10
|
-
value: T;
|
|
11
|
-
done: false;
|
|
12
|
+
result: IteratorResult<T, R>;
|
|
12
13
|
}, void, unknown>;
|
|
@@ -1,39 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.index = index;
|
|
7
|
-
this.iterator = iterable[Symbol.asyncIterator]();
|
|
8
|
-
this.promise = this.next();
|
|
9
|
-
}
|
|
10
|
-
next() {
|
|
11
|
-
return (this.promise = this.iterator.next().then((result) => ({
|
|
12
|
-
result,
|
|
13
|
-
handler: this,
|
|
14
|
-
})));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function* getPromises(handlers) {
|
|
18
|
-
for (const handler of handlers)
|
|
19
|
-
yield handler.promise;
|
|
20
|
-
}
|
|
1
|
+
const never = new Promise(() => { });
|
|
2
|
+
const next = async (iterator, index) => ({
|
|
3
|
+
index,
|
|
4
|
+
result: await iterator.next(),
|
|
5
|
+
});
|
|
21
6
|
/**
|
|
22
|
-
*
|
|
7
|
+
* Merges `AsyncIterable[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
8
|
+
* The return of each `AsyncIterable` is yielded from the generator with `done: true`.
|
|
9
|
+
*
|
|
10
|
+
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
11
|
+
*
|
|
12
|
+
* @param iterables Resolved in parallel.
|
|
13
|
+
* @yields `IteratorResult` and `index` of the resolved iterator.
|
|
23
14
|
*/
|
|
24
|
-
export async function*
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
export async function* merge(iterables) {
|
|
16
|
+
const iterators = iterables.map((iter) => iter[Symbol.asyncIterator]());
|
|
17
|
+
const promises = iterators.map(next);
|
|
18
|
+
let remaining = promises.length;
|
|
19
|
+
let current;
|
|
20
|
+
while (remaining) {
|
|
21
|
+
yield (current = await Promise.race(promises));
|
|
22
|
+
if (current.result.done) {
|
|
23
|
+
promises[current.index] = never;
|
|
24
|
+
remaining--;
|
|
33
25
|
}
|
|
34
26
|
else {
|
|
35
|
-
|
|
36
|
-
handler.next();
|
|
27
|
+
promises[current.index] = next(iterators[current.index], current.index);
|
|
37
28
|
}
|
|
38
29
|
}
|
|
39
30
|
}
|
package/dist/router/context.js
CHANGED
|
@@ -169,7 +169,7 @@ export class Context {
|
|
|
169
169
|
elements[3] = bodyParts[0];
|
|
170
170
|
elements.push(Page, bodyClose + bodyParts[1]);
|
|
171
171
|
this.html(new ReadableStream({
|
|
172
|
-
|
|
172
|
+
async start(c) {
|
|
173
173
|
for await (const value of toGenerator(elements))
|
|
174
174
|
c.enqueue(value);
|
|
175
175
|
c.close();
|
package/dist/router/index.d.ts
CHANGED
|
@@ -39,6 +39,13 @@ export declare class Router<State = null> {
|
|
|
39
39
|
*/
|
|
40
40
|
start?: Start<State>;
|
|
41
41
|
});
|
|
42
|
+
/**
|
|
43
|
+
* Add global middleware.
|
|
44
|
+
*
|
|
45
|
+
* @param middleware
|
|
46
|
+
* @returns the router instance
|
|
47
|
+
*/
|
|
48
|
+
use(...middleware: Middleware<State, Params>[]): this;
|
|
42
49
|
/**
|
|
43
50
|
* @param method HTTP method
|
|
44
51
|
* @param pattern route pattern
|
package/dist/router/index.js
CHANGED
|
@@ -5,6 +5,8 @@ export class Router {
|
|
|
5
5
|
#trieMap = new Map();
|
|
6
6
|
/** Added routes per HTTP method */
|
|
7
7
|
#routesMap = new Map();
|
|
8
|
+
/** Global middleware */
|
|
9
|
+
#use = [];
|
|
8
10
|
#start;
|
|
9
11
|
#trailingSlash;
|
|
10
12
|
constructor(config = {}) {
|
|
@@ -12,6 +14,16 @@ export class Router {
|
|
|
12
14
|
this.#start = config.start;
|
|
13
15
|
this.fetch = this.fetch.bind(this);
|
|
14
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Add global middleware.
|
|
19
|
+
*
|
|
20
|
+
* @param middleware
|
|
21
|
+
* @returns the router instance
|
|
22
|
+
*/
|
|
23
|
+
use(...middleware) {
|
|
24
|
+
this.#use.push(...middleware);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
15
27
|
on(method, pattern, ...middleware) {
|
|
16
28
|
let patterns;
|
|
17
29
|
if (!Array.isArray(pattern))
|
|
@@ -78,7 +90,7 @@ export class Router {
|
|
|
78
90
|
const match = trie.find(c.url.pathname);
|
|
79
91
|
if (match) {
|
|
80
92
|
Object.assign(c, match);
|
|
81
|
-
await this.#compose(match.route.store)(c, () => Promise.resolve());
|
|
93
|
+
await this.#compose([...this.#use, ...match.route.store])(c, () => Promise.resolve());
|
|
82
94
|
}
|
|
83
95
|
}
|
|
84
96
|
}
|