ovr 3.0.0 → 4.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.
- package/dist/app/context.d.ts +1 -0
- package/dist/app/context.d.ts.map +1 -0
- package/dist/app/context.js +46 -27
- package/dist/app/{page.d.ts → get.d.ts} +5 -6
- package/dist/app/get.d.ts.map +1 -0
- package/dist/app/{page.js → get.js} +4 -6
- package/dist/app/index.d.ts +5 -4
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +7 -7
- package/dist/app/memo/index.d.ts +1 -0
- package/dist/app/memo/index.d.ts.map +1 -0
- package/dist/app/mw/csrf.d.ts +1 -0
- package/dist/app/mw/csrf.d.ts.map +1 -0
- package/dist/app/{action.d.ts → post.d.ts} +8 -9
- package/dist/app/post.d.ts.map +1 -0
- package/dist/app/{action.js → post.js} +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -2
- package/dist/jsx/chunk/index.d.ts +14 -11
- package/dist/jsx/chunk/index.d.ts.map +1 -0
- package/dist/jsx/chunk/index.js +26 -28
- package/dist/jsx/index.d.ts +13 -9
- package/dist/jsx/index.d.ts.map +1 -0
- package/dist/jsx/index.js +57 -36
- package/dist/jsx/{elements.d.ts → intrinsic-elements.d.ts} +2 -1
- package/dist/jsx/intrinsic-elements.d.ts.map +1 -0
- package/dist/jsx/{merge-async-iterables.d.ts → merge-async-generators.d.ts} +2 -4
- package/dist/jsx/merge-async-generators.d.ts.map +1 -0
- package/dist/jsx/{merge-async-iterables.js → merge-async-generators.js} +7 -9
- package/dist/jsx-dev-runtime.d.ts +1 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +1 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/trie/index.d.ts +1 -0
- package/dist/trie/index.d.ts.map +1 -0
- package/dist/trie/insert-params.d.ts +1 -0
- package/dist/trie/insert-params.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/util/hash.d.ts +10 -0
- package/dist/util/hash.d.ts.map +1 -0
- package/dist/util/hash.js +9 -2
- package/package.json +4 -2
- package/src/app/context.ts +350 -0
- package/src/app/get.ts +54 -0
- package/src/app/index.ts +302 -0
- package/src/app/memo/index.ts +40 -0
- package/src/app/mw/csrf.ts +28 -0
- package/src/app/post.ts +79 -0
- package/src/index.ts +9 -0
- package/src/jsx/chunk/index.ts +68 -0
- package/src/jsx/index.ts +247 -0
- package/src/jsx/intrinsic-elements.ts +812 -0
- package/src/jsx/merge-async-generators.ts +42 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +1 -0
- package/src/trie/index.ts +335 -0
- package/src/trie/insert-params.ts +31 -0
- package/src/types/index.ts +23 -0
- package/src/util/hash.ts +17 -0
- /package/dist/jsx/{elements.js → intrinsic-elements.js} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class Memo {
|
|
2
|
+
/** Already memoized functions. */
|
|
3
|
+
#memoized = new WeakMap<Function, Function>();
|
|
4
|
+
|
|
5
|
+
/** Symbol to store the result of each memoized function. */
|
|
6
|
+
static #RESULT = Symbol();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calls to the returned function with the same arguments will
|
|
10
|
+
* return a cached result.
|
|
11
|
+
*
|
|
12
|
+
* @param fn Function to memoize.
|
|
13
|
+
* @returns The memoized function.
|
|
14
|
+
*/
|
|
15
|
+
use = <A extends any[], R>(fn: (...args: A) => R) => {
|
|
16
|
+
if (this.#memoized.has(fn)) return this.#memoized.get(fn) as typeof fn;
|
|
17
|
+
|
|
18
|
+
// closure will GC automatically
|
|
19
|
+
const trie = new Map();
|
|
20
|
+
|
|
21
|
+
const memo = (...args: A): R => {
|
|
22
|
+
let node = trie;
|
|
23
|
+
|
|
24
|
+
for (const arg of args) {
|
|
25
|
+
// get/create a node for each arg
|
|
26
|
+
if (!node.has(arg)) node.set(arg, new Map());
|
|
27
|
+
node = node.get(arg);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// set the final result if not there
|
|
31
|
+
if (!node.has(Memo.#RESULT)) node.set(Memo.#RESULT, fn(...args));
|
|
32
|
+
|
|
33
|
+
return node.get(Memo.#RESULT);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
this.#memoized.set(fn, memo);
|
|
37
|
+
|
|
38
|
+
return memo;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Middleware } from "../index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
5
|
+
*
|
|
6
|
+
* @param options
|
|
7
|
+
* @returns CSRF middleware
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { csrf } from "ovr";
|
|
13
|
+
*
|
|
14
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const csrf = (options: { origin: string | string[] }): Middleware => {
|
|
18
|
+
if (typeof options.origin === "string") options.origin = [options.origin];
|
|
19
|
+
|
|
20
|
+
return (c, next) => {
|
|
21
|
+
if (["GET", "HEAD"].includes(c.req.method)) return next();
|
|
22
|
+
|
|
23
|
+
const origin = c.req.headers.get("Origin");
|
|
24
|
+
if (origin && options.origin.includes(origin)) return next();
|
|
25
|
+
|
|
26
|
+
return c.text("Forbidden", 403);
|
|
27
|
+
};
|
|
28
|
+
};
|
package/src/app/post.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
+
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
+
import type { ExtractParams } from "../types/index.js";
|
|
4
|
+
import { hash } from "../util/hash.js";
|
|
5
|
+
import type { Middleware, Params } from "./index.js";
|
|
6
|
+
|
|
7
|
+
type FormProps<P extends Params> = JSX.IntrinsicElements["form"] &
|
|
8
|
+
(keyof P extends never ? { params?: never } : { params: P });
|
|
9
|
+
|
|
10
|
+
export class Post<Pattern extends string = string> {
|
|
11
|
+
/** Route pattern */
|
|
12
|
+
pattern: Pattern;
|
|
13
|
+
|
|
14
|
+
/** POST middleware */
|
|
15
|
+
middleware: Middleware<any>[];
|
|
16
|
+
|
|
17
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
18
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
19
|
+
|
|
20
|
+
#parts: string[] = [];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param middleware POST middleware
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { Post } from "ovr";
|
|
29
|
+
*
|
|
30
|
+
* const post = new Post((c) => {
|
|
31
|
+
* console.log("posted");
|
|
32
|
+
* c.redirect("/", 303);
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* const page = new Get("/", () => (
|
|
36
|
+
* <post.Form>
|
|
37
|
+
* <input />
|
|
38
|
+
* <button>Submit</button>
|
|
39
|
+
* </post.Form>
|
|
40
|
+
* ));
|
|
41
|
+
*
|
|
42
|
+
* app.add(page, post); register
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
constructor(...middleware: Middleware<{}>[]);
|
|
46
|
+
/**
|
|
47
|
+
* @param pattern Route pattern
|
|
48
|
+
* @param middleware POST middleware
|
|
49
|
+
*/
|
|
50
|
+
constructor(
|
|
51
|
+
pattern: Pattern,
|
|
52
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
53
|
+
);
|
|
54
|
+
constructor(
|
|
55
|
+
patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
|
|
56
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
57
|
+
) {
|
|
58
|
+
this.middleware = middleware;
|
|
59
|
+
|
|
60
|
+
if (typeof patternOrMiddleware === "string") {
|
|
61
|
+
this.pattern = patternOrMiddleware;
|
|
62
|
+
} else {
|
|
63
|
+
this.middleware.unshift(patternOrMiddleware);
|
|
64
|
+
this.pattern = `/_action/${hash(this.middleware.join())}` as Pattern;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.#parts = this.pattern.split("/");
|
|
68
|
+
|
|
69
|
+
this.Form = (props) => {
|
|
70
|
+
const { params = {}, ...rest } = props;
|
|
71
|
+
|
|
72
|
+
return jsx("form", {
|
|
73
|
+
action: insertParams(this.#parts, params),
|
|
74
|
+
method: "post",
|
|
75
|
+
...rest,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { jsx, toGenerator, 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 Params } from "./app/index.js";
|
|
5
|
+
export { Context } from "./app/context.js";
|
|
6
|
+
export { Get } from "./app/get.js";
|
|
7
|
+
export { Post } from "./app/post.js";
|
|
8
|
+
export { csrf } from "./app/mw/csrf.js";
|
|
9
|
+
export type * from "./types/index.js";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class Chunk {
|
|
2
|
+
static #attr = /[&"<]/g;
|
|
3
|
+
static #content = /[&<]/g;
|
|
4
|
+
|
|
5
|
+
/** Safe value to render */
|
|
6
|
+
value: string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `Chunk` to send in an HTML stream.
|
|
10
|
+
*
|
|
11
|
+
* @param s string of HTML to escape
|
|
12
|
+
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
13
|
+
*/
|
|
14
|
+
constructor(s: unknown, safe?: boolean) {
|
|
15
|
+
const value = String(s ?? "");
|
|
16
|
+
this.value = safe ? value : Chunk.escape(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @returns `value`
|
|
21
|
+
*/
|
|
22
|
+
toString() {
|
|
23
|
+
return this.value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param chunks Chunks to append to the end of the chunk
|
|
28
|
+
*/
|
|
29
|
+
concat(...chunks: Chunk[]) {
|
|
30
|
+
this.value += chunks.join("");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Escapes strings of HTML.
|
|
35
|
+
*
|
|
36
|
+
* @param s String to escape
|
|
37
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
38
|
+
* @returns Escaped string of HTML
|
|
39
|
+
*/
|
|
40
|
+
static escape(s: string, attr?: boolean) {
|
|
41
|
+
// adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
42
|
+
const regex = attr ? Chunk.#attr : Chunk.#content;
|
|
43
|
+
|
|
44
|
+
// search starts at beginning of the string
|
|
45
|
+
regex.lastIndex = 0;
|
|
46
|
+
// tracks the position of the last successful match in the input string
|
|
47
|
+
let start = 0;
|
|
48
|
+
let result = "";
|
|
49
|
+
|
|
50
|
+
// since `g` flag is used, regex maintains state with each loop,
|
|
51
|
+
// checking from the lastIndex onward each time
|
|
52
|
+
while (regex.test(s)) {
|
|
53
|
+
// index of the match
|
|
54
|
+
const i = regex.lastIndex - 1;
|
|
55
|
+
const match = s[i];
|
|
56
|
+
|
|
57
|
+
result +=
|
|
58
|
+
// everything that didn't match during this test
|
|
59
|
+
s.slice(start, i) +
|
|
60
|
+
// replacement
|
|
61
|
+
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
62
|
+
|
|
63
|
+
start = regex.lastIndex;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return result + s.slice(start);
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/jsx/index.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
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
|
+
import { setImmediate } from "node:timers/promises";
|
|
6
|
+
|
|
7
|
+
/** ovr JSX namespace */
|
|
8
|
+
export namespace JSX {
|
|
9
|
+
/** Standard HTML elements */
|
|
10
|
+
export interface IntrinsicElements extends IE {}
|
|
11
|
+
|
|
12
|
+
/** ovr Element */
|
|
13
|
+
export type Element = MaybeFunction<
|
|
14
|
+
MaybePromise<
|
|
15
|
+
| string
|
|
16
|
+
| number
|
|
17
|
+
| bigint
|
|
18
|
+
| boolean
|
|
19
|
+
| object
|
|
20
|
+
| null
|
|
21
|
+
| undefined
|
|
22
|
+
| Symbol
|
|
23
|
+
| Iterable<Element>
|
|
24
|
+
| AsyncIterable<Element>
|
|
25
|
+
>
|
|
26
|
+
>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Unknown component props. */
|
|
30
|
+
export type Props = Record<string, JSX.Element>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* These are the HTML tags that do not require a closing tag.
|
|
34
|
+
*
|
|
35
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
|
|
36
|
+
*/
|
|
37
|
+
const voidElements = new Set([
|
|
38
|
+
"area",
|
|
39
|
+
"base",
|
|
40
|
+
"br",
|
|
41
|
+
"col",
|
|
42
|
+
"embed",
|
|
43
|
+
"hr",
|
|
44
|
+
"img",
|
|
45
|
+
"input",
|
|
46
|
+
"link",
|
|
47
|
+
"meta",
|
|
48
|
+
"source",
|
|
49
|
+
"track",
|
|
50
|
+
"wbr",
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
55
|
+
* it is passed into this function to be resolved.
|
|
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 an async generator 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
|
+
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// intrinsic element
|
|
73
|
+
const { children, ...rest } = props;
|
|
74
|
+
|
|
75
|
+
const attributes: string[] = [];
|
|
76
|
+
|
|
77
|
+
for (let key in rest) {
|
|
78
|
+
const value = rest[key]; // needs to come before reassigning keys
|
|
79
|
+
|
|
80
|
+
if (key === "className") key = "class";
|
|
81
|
+
else if (key === "htmlFor") key = "for";
|
|
82
|
+
|
|
83
|
+
if (value === true) {
|
|
84
|
+
// just put the key without the value
|
|
85
|
+
attributes.push(` ${key}`);
|
|
86
|
+
} else if (
|
|
87
|
+
typeof value === "string" ||
|
|
88
|
+
typeof value === "number" ||
|
|
89
|
+
typeof value === "bigint"
|
|
90
|
+
) {
|
|
91
|
+
attributes.push(` ${key}="${Chunk.escape(String(value), true)}"`);
|
|
92
|
+
}
|
|
93
|
+
// otherwise, don't include the attribute
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
yield new Chunk(`<${tag}${attributes.join("")}>`, true);
|
|
97
|
+
|
|
98
|
+
if (voidElements.has(tag)) return;
|
|
99
|
+
|
|
100
|
+
yield* toGenerator(children);
|
|
101
|
+
|
|
102
|
+
yield new Chunk(`</${tag}>`, true);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* JSX requires a `Fragment` export to resolve `<></>`
|
|
107
|
+
*
|
|
108
|
+
* @param props containing `children` to render
|
|
109
|
+
* @returns async generator that yields concatenated children
|
|
110
|
+
*/
|
|
111
|
+
export async function* Fragment(props: { children?: JSX.Element } = {}) {
|
|
112
|
+
yield* toGenerator(props.children);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param element
|
|
117
|
+
* @yields Chunks of HTML as the `Element` resolves.
|
|
118
|
+
*/
|
|
119
|
+
export async function* toGenerator(
|
|
120
|
+
element: JSX.Element,
|
|
121
|
+
): AsyncGenerator<Chunk, void, unknown> {
|
|
122
|
+
// modifications
|
|
123
|
+
// these are required to allow functions to be used as children
|
|
124
|
+
// instead of creating a separate component to use them
|
|
125
|
+
if (typeof element === "function") element = element();
|
|
126
|
+
if (element instanceof Promise) element = await element;
|
|
127
|
+
|
|
128
|
+
// resolve based on type
|
|
129
|
+
// should not render
|
|
130
|
+
if (element == null || typeof element === "boolean" || element === "") return;
|
|
131
|
+
|
|
132
|
+
if (element instanceof Chunk) {
|
|
133
|
+
// already escaped or safe
|
|
134
|
+
yield element;
|
|
135
|
+
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (typeof element === "object") {
|
|
140
|
+
if (Symbol.asyncIterator in element) {
|
|
141
|
+
// async iterable - lazily resolve
|
|
142
|
+
for await (const children of element) yield* toGenerator(children);
|
|
143
|
+
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (Symbol.iterator in element) {
|
|
148
|
+
// sync iterable
|
|
149
|
+
const iterator = element[Symbol.iterator]();
|
|
150
|
+
|
|
151
|
+
if (
|
|
152
|
+
typeof iterator.next === "function" &&
|
|
153
|
+
typeof iterator.throw === "function" &&
|
|
154
|
+
typeof iterator.return === "function"
|
|
155
|
+
) {
|
|
156
|
+
// sync generator
|
|
157
|
+
// process lazily - avoids loading all in memory
|
|
158
|
+
const yieldIterations = 50;
|
|
159
|
+
let yieldCounter = yieldIterations;
|
|
160
|
+
|
|
161
|
+
while (true) {
|
|
162
|
+
const result = iterator.next();
|
|
163
|
+
|
|
164
|
+
if (result.done) break;
|
|
165
|
+
yield* toGenerator(result.value);
|
|
166
|
+
|
|
167
|
+
if (--yieldCounter === 0) {
|
|
168
|
+
// yields back to the event loop to send chunks
|
|
169
|
+
// or check if the request has been cancelled
|
|
170
|
+
await setImmediate();
|
|
171
|
+
yieldCounter = yieldIterations;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// other iterable - array, set, etc.
|
|
179
|
+
// process children in parallel
|
|
180
|
+
const generators: AsyncGenerator<Chunk, void, unknown>[] = [];
|
|
181
|
+
|
|
182
|
+
while (true) {
|
|
183
|
+
const result = iterator.next();
|
|
184
|
+
if (result.done) break;
|
|
185
|
+
generators.push(toGenerator(result.value));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const queue: (Chunk | null)[] = new Array(generators.length).fill(null);
|
|
189
|
+
|
|
190
|
+
let current = 0;
|
|
191
|
+
const complete = new Set<number>();
|
|
192
|
+
|
|
193
|
+
for await (const {
|
|
194
|
+
index,
|
|
195
|
+
result: { done, value: chunk },
|
|
196
|
+
} of merge(generators)) {
|
|
197
|
+
if (done) {
|
|
198
|
+
complete.add(index);
|
|
199
|
+
|
|
200
|
+
if (index === current) {
|
|
201
|
+
while (++current < generators.length) {
|
|
202
|
+
if (queue[current]) {
|
|
203
|
+
// yield whatever is in the next queue even if it hasn't completed yet
|
|
204
|
+
yield queue[current]!;
|
|
205
|
+
queue[current] = null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// if it hasn't completed, stop iterating to the next
|
|
209
|
+
if (!complete.has(current)) break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} else if (index === current) {
|
|
213
|
+
yield chunk; // stream the current value directly
|
|
214
|
+
} else {
|
|
215
|
+
// queue the value for later
|
|
216
|
+
if (queue[index]) queue[index].concat(chunk);
|
|
217
|
+
else queue[index] = chunk;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// clear the queue
|
|
222
|
+
yield* queue.filter(Boolean) as Chunk[];
|
|
223
|
+
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// primitive or other object
|
|
229
|
+
yield new Chunk(element);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
234
|
+
*
|
|
235
|
+
* ### WARNING
|
|
236
|
+
*
|
|
237
|
+
* This negates streaming benefits and buffers the result into a string.
|
|
238
|
+
* Use `toGenerator` whenever possible.
|
|
239
|
+
*
|
|
240
|
+
* @param element
|
|
241
|
+
* @returns Concatenated HTML
|
|
242
|
+
*/
|
|
243
|
+
export const toString = async (element: JSX.Element) => {
|
|
244
|
+
const buffer: Chunk[] = [];
|
|
245
|
+
for await (const chunk of toGenerator(element)) buffer.push(chunk);
|
|
246
|
+
return buffer.join("");
|
|
247
|
+
};
|