ovr 6.2.4 → 7.0.0-next.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/README.md +3 -1
- package/dist/app/index.d.ts +51 -7
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +163 -28
- package/dist/auth/auth-data.d.ts +26 -0
- package/dist/auth/auth-data.d.ts.map +1 -0
- package/dist/auth/auth-data.js +42 -0
- package/dist/auth/cbor.d.ts +50 -0
- package/dist/auth/cbor.d.ts.map +1 -0
- package/dist/auth/cbor.js +200 -0
- package/dist/auth/client-source.d.ts +3 -0
- package/dist/auth/client-source.d.ts.map +1 -0
- package/dist/auth/client-source.js +3 -0
- package/dist/auth/client.d.ts +3 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +186 -0
- package/dist/auth/configure.d.ts +28 -0
- package/dist/auth/configure.d.ts.map +1 -0
- package/dist/auth/configure.js +228 -0
- package/dist/auth/der.d.ts +6 -0
- package/dist/auth/der.d.ts.map +1 -0
- package/dist/auth/der.js +36 -0
- package/dist/auth/index.d.ts +242 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +152 -0
- package/dist/auth/issue.d.ts +36 -0
- package/dist/auth/issue.d.ts.map +1 -0
- package/dist/auth/issue.js +47 -0
- package/dist/auth/metadata.d.ts +22 -0
- package/dist/auth/metadata.d.ts.map +1 -0
- package/dist/auth/metadata.js +134 -0
- package/dist/auth/oauth.d.ts +29 -0
- package/dist/auth/oauth.d.ts.map +1 -0
- package/dist/auth/oauth.js +724 -0
- package/dist/auth/passkey.d.ts +101 -0
- package/dist/auth/passkey.d.ts.map +1 -0
- package/dist/auth/passkey.js +393 -0
- package/dist/auth/state.d.ts +49 -0
- package/dist/auth/state.d.ts.map +1 -0
- package/dist/auth/state.js +184 -0
- package/dist/auth/storage.d.ts +193 -0
- package/dist/auth/storage.d.ts.map +1 -0
- package/dist/auth/storage.js +410 -0
- package/dist/auth/token.d.ts +12 -0
- package/dist/auth/token.d.ts.map +1 -0
- package/dist/auth/token.js +39 -0
- package/dist/context/index.d.ts +86 -16
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +166 -42
- package/dist/cookie/index.d.ts +3 -0
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +1 -0
- package/dist/database/index.d.ts +262 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +807 -0
- package/dist/form/index.d.ts +699 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +901 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/jsx/elements.d.ts +28 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts +20 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +6 -65
- package/dist/limiter/index.d.ts +65 -0
- package/dist/limiter/index.d.ts.map +1 -0
- package/dist/limiter/index.js +157 -0
- package/dist/markdown/index.d.ts +61 -0
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +1160 -0
- package/dist/markup/index.d.ts +188 -0
- package/dist/markup/index.d.ts.map +1 -0
- package/dist/markup/index.js +718 -0
- package/dist/mcp/index.d.ts +73 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +988 -0
- package/dist/middleware/index.d.ts +3 -4
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +53 -7
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +332 -124
- package/dist/route/index.d.ts +226 -80
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +239 -99
- package/dist/schema/index.d.ts +307 -734
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +783 -1050
- package/dist/table/index.d.ts +132 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +188 -0
- package/dist/trie/index.js +4 -4
- package/dist/util/index.d.ts +37 -5
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +156 -41
- package/package.json +11 -4
- package/src/app/index.ts +230 -42
- package/src/auth/auth-data.ts +56 -0
- package/src/auth/cbor.ts +231 -0
- package/src/auth/client-source.ts +3 -0
- package/src/auth/client.ts +205 -0
- package/src/auth/configure.ts +279 -0
- package/src/auth/der.ts +35 -0
- package/src/auth/index.ts +387 -0
- package/src/auth/issue.ts +52 -0
- package/src/auth/metadata.ts +149 -0
- package/src/auth/oauth.ts +882 -0
- package/src/auth/passkey.ts +556 -0
- package/src/auth/state.ts +213 -0
- package/src/auth/storage.ts +572 -0
- package/src/auth/token.ts +67 -0
- package/src/context/index.ts +240 -65
- package/src/cookie/index.ts +3 -0
- package/src/database/index.ts +1193 -0
- package/src/form/index.ts +1720 -0
- package/src/index.ts +10 -3
- package/src/jsx/elements.ts +45 -25
- package/src/jsx/index.ts +24 -69
- package/src/limiter/index.ts +221 -0
- package/src/markdown/index.ts +1322 -0
- package/src/markup/index.ts +853 -0
- package/src/mcp/conformance.2025-11-25.yml +39 -0
- package/src/mcp/conformance.2026-07-28.yml +56 -0
- package/src/mcp/index.ts +1262 -0
- package/src/middleware/index.ts +5 -6
- package/src/multipart/index.ts +410 -158
- package/src/route/index.ts +559 -278
- package/src/schema/index.ts +1542 -2256
- package/src/table/index.ts +387 -0
- package/src/trie/index.ts +4 -4
- package/src/util/index.ts +163 -49
- package/dist/render/index.d.ts +0 -79
- package/dist/render/index.d.ts.map +0 -1
- package/dist/render/index.js +0 -257
- package/src/render/index.ts +0 -314
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
import { Codec, Size } from "../util/index.js";
|
|
2
|
+
/** Chunk of a rendered element: text, or markup */
|
|
3
|
+
class Chunk {
|
|
4
|
+
/** Text, or markup when `raw` */
|
|
5
|
+
value;
|
|
6
|
+
/** `true` if the value is markup, which is not escaped */
|
|
7
|
+
raw;
|
|
8
|
+
/**
|
|
9
|
+
* Chunk of a rendered element.
|
|
10
|
+
*
|
|
11
|
+
* @param value Text, escaped when it is rendered as markup
|
|
12
|
+
* @param raw Set to `true` if the value is safe markup that should not be escaped
|
|
13
|
+
*/
|
|
14
|
+
constructor(value, raw) {
|
|
15
|
+
this.value = String(value ?? "");
|
|
16
|
+
this.raw = raw;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @returns Safe markup to render
|
|
20
|
+
*/
|
|
21
|
+
toString() {
|
|
22
|
+
return this.raw ? this.value : Markup.escape(this.value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Start or end tag of an element, internal to ovr. It keeps the element's
|
|
27
|
+
* name and props, so the render can also be written as Markdown.
|
|
28
|
+
*/
|
|
29
|
+
export class Tag extends Chunk {
|
|
30
|
+
/** Tag name */
|
|
31
|
+
name;
|
|
32
|
+
/** Props of a start tag, `undefined` for an end tag */
|
|
33
|
+
props;
|
|
34
|
+
/** `true` for a start tag without an end tag, such as `<input>` */
|
|
35
|
+
closed;
|
|
36
|
+
/**
|
|
37
|
+
* @param name Tag name
|
|
38
|
+
* @param props Props of a start tag
|
|
39
|
+
* @param closed The element has no end tag
|
|
40
|
+
*/
|
|
41
|
+
constructor(name, props, closed) {
|
|
42
|
+
super("", true);
|
|
43
|
+
this.name = name;
|
|
44
|
+
this.props = props;
|
|
45
|
+
this.closed = closed;
|
|
46
|
+
}
|
|
47
|
+
/** @returns The tag's markup, built when it is written */
|
|
48
|
+
toString() {
|
|
49
|
+
const { name, props } = this;
|
|
50
|
+
if (!props)
|
|
51
|
+
return `</${name}>`;
|
|
52
|
+
// faster to concatenate attributes than to yield them as separate chunks
|
|
53
|
+
let opening = name;
|
|
54
|
+
for (const key in props) {
|
|
55
|
+
// more memory efficient to skip children instead of destructuring and using ...rest
|
|
56
|
+
if (key === "children")
|
|
57
|
+
continue;
|
|
58
|
+
const value = props[key];
|
|
59
|
+
if (value === true) {
|
|
60
|
+
// just put the key without the value
|
|
61
|
+
opening += ` ${key}`;
|
|
62
|
+
}
|
|
63
|
+
else if (typeof value === "string") {
|
|
64
|
+
opening += ` ${key}="${Markup.escape(value, true)}"`;
|
|
65
|
+
}
|
|
66
|
+
else if (typeof value === "number" || typeof value === "bigint") {
|
|
67
|
+
opening += ` ${key}="${value}"`;
|
|
68
|
+
}
|
|
69
|
+
// otherwise, don't include the attribute
|
|
70
|
+
}
|
|
71
|
+
return name === "html"
|
|
72
|
+
? `<!doctype html><${opening}>`
|
|
73
|
+
: name === "xml"
|
|
74
|
+
? `<?${opening}?>`
|
|
75
|
+
: `<${opening}>`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Chunks a later sibling rendered while an earlier one was still rendering,
|
|
80
|
+
* yielded together once they are next. Internal to ovr.
|
|
81
|
+
*/
|
|
82
|
+
export class Group extends Chunk {
|
|
83
|
+
/** Chunks in order */
|
|
84
|
+
chunks;
|
|
85
|
+
/** @param chunks Chunks in order */
|
|
86
|
+
constructor(chunks) {
|
|
87
|
+
super("", true);
|
|
88
|
+
this.chunks = chunks;
|
|
89
|
+
}
|
|
90
|
+
toString() {
|
|
91
|
+
// `join` is several times slower for chunks than for strings
|
|
92
|
+
let markup = "";
|
|
93
|
+
for (const chunk of this.chunks)
|
|
94
|
+
markup += chunk.toString();
|
|
95
|
+
return markup;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* HTML tags that do not require a closing tag.
|
|
100
|
+
*
|
|
101
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
|
|
102
|
+
*/
|
|
103
|
+
const voids = new Set([
|
|
104
|
+
"area",
|
|
105
|
+
"base",
|
|
106
|
+
"br",
|
|
107
|
+
"col",
|
|
108
|
+
"embed",
|
|
109
|
+
"hr",
|
|
110
|
+
"img",
|
|
111
|
+
"input",
|
|
112
|
+
"link",
|
|
113
|
+
"meta",
|
|
114
|
+
"source",
|
|
115
|
+
"track",
|
|
116
|
+
"wbr",
|
|
117
|
+
"xml",
|
|
118
|
+
]);
|
|
119
|
+
/**
|
|
120
|
+
* Element created by `jsx`, internal to ovr. A render reads its tag and props
|
|
121
|
+
* instead of iterating it, which costs a promise for each chunk at every level
|
|
122
|
+
* of the tree. Anything else can still iterate it as the generator of its
|
|
123
|
+
* chunks.
|
|
124
|
+
*/
|
|
125
|
+
export class Element {
|
|
126
|
+
/** Tag name, or component */
|
|
127
|
+
tag;
|
|
128
|
+
/** Props */
|
|
129
|
+
props;
|
|
130
|
+
/** Rendered, or being rendered, which a generator is only once */
|
|
131
|
+
#taken = false;
|
|
132
|
+
/** Chunks for what iterates the element itself */
|
|
133
|
+
#chunks;
|
|
134
|
+
/**
|
|
135
|
+
* @param tag Tag name, or component
|
|
136
|
+
* @param props Props
|
|
137
|
+
*/
|
|
138
|
+
constructor(tag, props) {
|
|
139
|
+
this.tag = tag;
|
|
140
|
+
this.props = props;
|
|
141
|
+
}
|
|
142
|
+
/** @returns `true` once, to the render that writes the element */
|
|
143
|
+
take() {
|
|
144
|
+
return !this.#taken && (this.#taken = true);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Generator of the element's chunks, empty if it was already rendered.
|
|
148
|
+
* Chunks, not strings, so a render it is part of keeps its tags and what
|
|
149
|
+
* is already escaped.
|
|
150
|
+
*/
|
|
151
|
+
get #generator() {
|
|
152
|
+
return (this.#chunks ??= (async function* (element) {
|
|
153
|
+
const render = new Render(element, undefined);
|
|
154
|
+
try {
|
|
155
|
+
for (let result; (result = render.step());) {
|
|
156
|
+
if (result instanceof Chunk)
|
|
157
|
+
yield result;
|
|
158
|
+
else
|
|
159
|
+
await result;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
await render.close();
|
|
164
|
+
}
|
|
165
|
+
// a copy the render can take, since this one now counts as rendered
|
|
166
|
+
})(this.take() ? new Element(this.tag, this.props) : null));
|
|
167
|
+
}
|
|
168
|
+
next() {
|
|
169
|
+
return this.#generator.next();
|
|
170
|
+
}
|
|
171
|
+
return(value) {
|
|
172
|
+
return this.#generator.return(value);
|
|
173
|
+
}
|
|
174
|
+
throw(error) {
|
|
175
|
+
return this.#generator.throw(error);
|
|
176
|
+
}
|
|
177
|
+
[Symbol.asyncIterator]() {
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
async [Symbol.asyncDispose]() {
|
|
181
|
+
await this.return();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Internal protocol for a render, to read the element a `Markup` renders. */
|
|
185
|
+
const source = Symbol("source");
|
|
186
|
+
/**
|
|
187
|
+
* Renders an element one chunk at a time, internal to ovr. A step is
|
|
188
|
+
* synchronous unless the element has to be waited for, so nested elements cost
|
|
189
|
+
* a loop instead of a generator and a promise for each chunk at every level.
|
|
190
|
+
*/
|
|
191
|
+
export class Render {
|
|
192
|
+
/** Chunks rendered before the element is its parent's current child */
|
|
193
|
+
queue;
|
|
194
|
+
/** Waiting, set by the parent of a child */
|
|
195
|
+
waiting = false;
|
|
196
|
+
/** Nothing more to render, set by the parent of a child */
|
|
197
|
+
done = false;
|
|
198
|
+
/** Element to render next */
|
|
199
|
+
#element;
|
|
200
|
+
/** `0` no element is next, `1` an element is, `2` the value of a promise is */
|
|
201
|
+
#next = 1;
|
|
202
|
+
/** Disables escaping, for content that is not markup */
|
|
203
|
+
#raw;
|
|
204
|
+
/** What follows the element: end tags, and iterators and children in progress */
|
|
205
|
+
#stack;
|
|
206
|
+
/** The wait the last step returned, if it did */
|
|
207
|
+
#wait;
|
|
208
|
+
/**
|
|
209
|
+
* @param element Element to render
|
|
210
|
+
* @param raw Set to `true` to disable escaping
|
|
211
|
+
*/
|
|
212
|
+
constructor(element, raw) {
|
|
213
|
+
this.#element = element;
|
|
214
|
+
this.#raw = raw;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @returns The next chunk, a promise to wait for before the next step, or
|
|
218
|
+
* `undefined` once the element is rendered
|
|
219
|
+
*/
|
|
220
|
+
step() {
|
|
221
|
+
this.#wait = undefined;
|
|
222
|
+
for (;;) {
|
|
223
|
+
if (this.#next) {
|
|
224
|
+
let element = this.#element;
|
|
225
|
+
const resolved = this.#next === 2;
|
|
226
|
+
this.#next = 0;
|
|
227
|
+
this.#element = undefined;
|
|
228
|
+
if (!resolved) {
|
|
229
|
+
// modifications
|
|
230
|
+
// these are required to allow functions to be used as children
|
|
231
|
+
// instead of creating a separate component to use them
|
|
232
|
+
if (typeof element === "function")
|
|
233
|
+
element = element();
|
|
234
|
+
if (element instanceof Promise) {
|
|
235
|
+
return (this.#wait = element.then((value) => {
|
|
236
|
+
this.#element = value;
|
|
237
|
+
this.#next = 2;
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// resolve based on type
|
|
242
|
+
if (element == null || typeof element === "boolean" || element === "") {
|
|
243
|
+
// should not render
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (element instanceof Chunk) {
|
|
247
|
+
// already escaped or raw
|
|
248
|
+
return element;
|
|
249
|
+
}
|
|
250
|
+
if (typeof element === "object") {
|
|
251
|
+
// an element is written as markup whatever the options, so one
|
|
252
|
+
// in content that is not markup is iterated as it always was
|
|
253
|
+
if (element instanceof Element && !this.#raw && element.take()) {
|
|
254
|
+
const { tag, props } = element;
|
|
255
|
+
if (typeof tag === "function") {
|
|
256
|
+
// component or fragment
|
|
257
|
+
this.#element = tag(props);
|
|
258
|
+
this.#next = 1;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
// intrinsic element, its markup is only built if it is written
|
|
262
|
+
// if children and a void element, render it like normal
|
|
263
|
+
// to account for cases like XML <link>content</link>
|
|
264
|
+
// assumes the user knows what they are doing
|
|
265
|
+
const closed = props.children == null && voids.has(tag);
|
|
266
|
+
if (!closed) {
|
|
267
|
+
(this.#stack ??= []).push(new Tag(tag));
|
|
268
|
+
this.#element = props.children;
|
|
269
|
+
this.#next = 1;
|
|
270
|
+
}
|
|
271
|
+
return new Tag(tag, props, closed);
|
|
272
|
+
}
|
|
273
|
+
if (element instanceof Markup) {
|
|
274
|
+
// its element, which its strings would be escaped as text
|
|
275
|
+
this.#element = element[source];
|
|
276
|
+
this.#next = 1;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (Symbol.asyncIterator in element) {
|
|
280
|
+
// any async iterable - lazily resolve
|
|
281
|
+
(this.#stack ??= []).push({
|
|
282
|
+
iterator: element[Symbol.asyncIterator](),
|
|
283
|
+
async: true,
|
|
284
|
+
});
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (Symbol.iterator in element) {
|
|
288
|
+
(this.#stack ??= []).push("next" in element
|
|
289
|
+
? // sync generator - lazily resolve, avoids loading all in memory
|
|
290
|
+
{
|
|
291
|
+
iterator: element[Symbol.iterator](),
|
|
292
|
+
async: false,
|
|
293
|
+
}
|
|
294
|
+
: // other iterable - array, set, etc.
|
|
295
|
+
new Parallel(element, this.#raw));
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// primitive or other object
|
|
300
|
+
return new Chunk(element, this.#raw);
|
|
301
|
+
}
|
|
302
|
+
const stack = this.#stack;
|
|
303
|
+
const top = stack?.[stack.length - 1];
|
|
304
|
+
if (!top)
|
|
305
|
+
return;
|
|
306
|
+
if (top instanceof Parallel) {
|
|
307
|
+
// stays on the stack to be closed if it throws
|
|
308
|
+
const result = top.step();
|
|
309
|
+
if (!result)
|
|
310
|
+
stack.pop();
|
|
311
|
+
else if (result instanceof Chunk)
|
|
312
|
+
return result;
|
|
313
|
+
else
|
|
314
|
+
return (this.#wait = result);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
// an iterator is off the stack while it is read, so one that
|
|
318
|
+
// throws is not closed as well
|
|
319
|
+
stack.pop();
|
|
320
|
+
if (top instanceof Tag)
|
|
321
|
+
return top; // end tag
|
|
322
|
+
if (top.async) {
|
|
323
|
+
return (this.#wait = Promise.resolve(top.iterator.next()).then((result) => {
|
|
324
|
+
if (!result.done) {
|
|
325
|
+
stack.push(top);
|
|
326
|
+
this.#element = result.value;
|
|
327
|
+
this.#next = 1;
|
|
328
|
+
}
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
const result = top.iterator.next();
|
|
332
|
+
if (!result.done) {
|
|
333
|
+
stack.push(top);
|
|
334
|
+
this.#element = result.value;
|
|
335
|
+
this.#next = 1;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Ends the iterators and children in progress, so a generator runs its
|
|
342
|
+
* `finally` block when the render fails or is not read to its end.
|
|
343
|
+
*/
|
|
344
|
+
async close() {
|
|
345
|
+
try {
|
|
346
|
+
// as `return` does for a generator: a step that is waiting first goes
|
|
347
|
+
// on to its chunk, so a promise it reaches is not left to reject unhandled
|
|
348
|
+
if (this.#wait) {
|
|
349
|
+
await this.#wait;
|
|
350
|
+
for (let result; (result = this.step()) && !(result instanceof Chunk);)
|
|
351
|
+
await result;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
catch { }
|
|
355
|
+
for (let top; (top = this.#stack?.pop());) {
|
|
356
|
+
try {
|
|
357
|
+
if (top instanceof Parallel)
|
|
358
|
+
await top.close();
|
|
359
|
+
else if (!(top instanceof Tag))
|
|
360
|
+
await top.iterator.return?.();
|
|
361
|
+
}
|
|
362
|
+
catch { }
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Children of an array, or another iterable that is not a generator: rendered
|
|
368
|
+
* at the same time, and yielded in order.
|
|
369
|
+
*/
|
|
370
|
+
class Parallel {
|
|
371
|
+
/** Render of each child */
|
|
372
|
+
#children;
|
|
373
|
+
/**
|
|
374
|
+
* Children after the current one that can step: not waiting, and not done.
|
|
375
|
+
* Only these are looked at as the others wait, so a list of many children
|
|
376
|
+
* that each wait costs no more for each one that resolves.
|
|
377
|
+
*/
|
|
378
|
+
#idle = [];
|
|
379
|
+
/** Index of the child being yielded */
|
|
380
|
+
#current = 0;
|
|
381
|
+
/** A child failed */
|
|
382
|
+
#failed = false;
|
|
383
|
+
/** What the child failed with */
|
|
384
|
+
#error;
|
|
385
|
+
/** Closed, so no child goes on */
|
|
386
|
+
#closed = false;
|
|
387
|
+
/** Ends the wait for a child */
|
|
388
|
+
#notify;
|
|
389
|
+
/**
|
|
390
|
+
* @param elements Children
|
|
391
|
+
* @param raw Set to `true` to disable escaping
|
|
392
|
+
*/
|
|
393
|
+
constructor(elements, raw) {
|
|
394
|
+
this.#children = [];
|
|
395
|
+
// each child starts before any is waited for
|
|
396
|
+
for (const element of elements) {
|
|
397
|
+
const child = new Render(element, raw);
|
|
398
|
+
const chunk = this.#advance(child);
|
|
399
|
+
if (chunk)
|
|
400
|
+
child.queue = [chunk];
|
|
401
|
+
if (this.#children.push(child) > 1 && !child.done && !child.waiting)
|
|
402
|
+
this.#idle.push(child);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/** @returns The next step of the children, in their order */
|
|
406
|
+
step() {
|
|
407
|
+
for (;;) {
|
|
408
|
+
if (this.#failed)
|
|
409
|
+
throw this.#error;
|
|
410
|
+
const child = this.#children[this.#current];
|
|
411
|
+
if (!child)
|
|
412
|
+
return;
|
|
413
|
+
const chunks = child.queue;
|
|
414
|
+
if (chunks) {
|
|
415
|
+
child.queue = undefined;
|
|
416
|
+
// the others go on as the current one is read, so whatever they
|
|
417
|
+
// wait for has started before they are next
|
|
418
|
+
this.#rest();
|
|
419
|
+
// chunks are queued, never changed, the same chunk may be rendered again
|
|
420
|
+
return chunks.length === 1 ? chunks[0] : new Group(chunks);
|
|
421
|
+
}
|
|
422
|
+
if (child.done)
|
|
423
|
+
this.#current++;
|
|
424
|
+
else if (child.waiting)
|
|
425
|
+
return this.#wait(child);
|
|
426
|
+
else {
|
|
427
|
+
const chunk = this.#advance(child);
|
|
428
|
+
if (chunk) {
|
|
429
|
+
this.#rest();
|
|
430
|
+
return chunk;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** Ends the children in progress. */
|
|
436
|
+
close() {
|
|
437
|
+
this.#closed = true;
|
|
438
|
+
this.#wake();
|
|
439
|
+
return Promise.allSettled(this.#children.map((child) => child.close()));
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Steps a child once.
|
|
443
|
+
*
|
|
444
|
+
* @param child Child that is not waiting
|
|
445
|
+
* @returns The chunk, if it rendered one
|
|
446
|
+
*/
|
|
447
|
+
#advance(child) {
|
|
448
|
+
let result;
|
|
449
|
+
try {
|
|
450
|
+
result = child.step();
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
return this.#fail(error);
|
|
454
|
+
}
|
|
455
|
+
if (result instanceof Chunk)
|
|
456
|
+
return result;
|
|
457
|
+
if (result) {
|
|
458
|
+
child.waiting = true;
|
|
459
|
+
result.then(() => {
|
|
460
|
+
child.waiting = false;
|
|
461
|
+
// a step goes on to its chunk without being asked again, so
|
|
462
|
+
// whatever the child waits for next starts as soon as it can
|
|
463
|
+
if (!this.#failed && !this.#closed) {
|
|
464
|
+
const chunk = this.#advance(child);
|
|
465
|
+
if (chunk)
|
|
466
|
+
(child.queue ??= []).push(chunk);
|
|
467
|
+
if (!child.done &&
|
|
468
|
+
!child.waiting &&
|
|
469
|
+
child !== this.#children[this.#current])
|
|
470
|
+
this.#idle.push(child);
|
|
471
|
+
}
|
|
472
|
+
this.#wake();
|
|
473
|
+
}, (error) => this.#fail(error));
|
|
474
|
+
}
|
|
475
|
+
else
|
|
476
|
+
child.done = true;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Steps each child after the current one that can step. Their chunks are
|
|
480
|
+
* queued until they are the current one.
|
|
481
|
+
*
|
|
482
|
+
* @returns `true` if a child stepped
|
|
483
|
+
*/
|
|
484
|
+
#rest() {
|
|
485
|
+
const idle = this.#idle;
|
|
486
|
+
if (!idle.length || this.#failed || this.#closed)
|
|
487
|
+
return false;
|
|
488
|
+
const current = this.#children[this.#current];
|
|
489
|
+
let stepped = false;
|
|
490
|
+
let kept = 0;
|
|
491
|
+
for (let i = 0; i < idle.length; i++) {
|
|
492
|
+
const child = idle[i];
|
|
493
|
+
// the current child is stepped as it is read
|
|
494
|
+
if (child !== current) {
|
|
495
|
+
const chunk = this.#advance(child);
|
|
496
|
+
if (chunk)
|
|
497
|
+
(child.queue ??= []).push(chunk);
|
|
498
|
+
// one that waits is added again once it can step
|
|
499
|
+
if (!child.done && !child.waiting)
|
|
500
|
+
idle[kept++] = child;
|
|
501
|
+
stepped = true;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
idle.length = kept;
|
|
505
|
+
return stepped;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Waits for the current child. The others go on meanwhile, a round of
|
|
509
|
+
* steps at a time, so the current one is next as soon as it can be.
|
|
510
|
+
*
|
|
511
|
+
* @param child Current child, which is waiting
|
|
512
|
+
*/
|
|
513
|
+
async #wait(child) {
|
|
514
|
+
while (child.waiting && !this.#failed && !this.#closed) {
|
|
515
|
+
if (this.#rest())
|
|
516
|
+
await undefined;
|
|
517
|
+
else
|
|
518
|
+
await new Promise((resolve) => (this.#notify = resolve));
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** Ends the wait, if there is one, to look at the children again. */
|
|
522
|
+
#wake() {
|
|
523
|
+
const notify = this.#notify;
|
|
524
|
+
this.#notify = undefined;
|
|
525
|
+
notify?.();
|
|
526
|
+
}
|
|
527
|
+
/** @param error What a child failed with, the first is thrown */
|
|
528
|
+
#fail(error) {
|
|
529
|
+
if (!this.#failed) {
|
|
530
|
+
this.#failed = true;
|
|
531
|
+
this.#error = error;
|
|
532
|
+
}
|
|
533
|
+
this.#wake();
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* A render piped into a `ReadableStream`, internal to ovr.
|
|
538
|
+
*
|
|
539
|
+
* @param render Render of markup, or of text such as Markdown
|
|
540
|
+
* @returns `ReadableStream` of what it renders
|
|
541
|
+
*/
|
|
542
|
+
export const readable = (render) => {
|
|
543
|
+
// `highWaterMark` defaults to 1
|
|
544
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#highwatermark
|
|
545
|
+
// setting this ensures at least a small buffer is maintained if the
|
|
546
|
+
// underlying server does not have its own high water mark set
|
|
547
|
+
// https://blog.cloudflare.com/unpacking-cloudflare-workers-cpu-performance-benchmarks/#inefficient-streams-adapters
|
|
548
|
+
// in Node, the default is 16kb, so this stacks another 2kb in front
|
|
549
|
+
// https://nodejs.org/api/http.html#outgoingmessagewritablehighwatermark
|
|
550
|
+
const highWaterMark = 2 * Size.kb;
|
|
551
|
+
/** Wait a pull left for the next one, since it had text to send first */
|
|
552
|
+
let wait;
|
|
553
|
+
/** Cancelled, so a pull that was waiting does not render more */
|
|
554
|
+
let cancelled = false;
|
|
555
|
+
return new ReadableStream({
|
|
556
|
+
// enables zero-copy transfer from underlying source when queue is empty
|
|
557
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_byte_streams#overview
|
|
558
|
+
type: "bytes",
|
|
559
|
+
// `pull` ensures backpressure and cancelled requests stop the render
|
|
560
|
+
async pull(c) {
|
|
561
|
+
let text = "";
|
|
562
|
+
let result;
|
|
563
|
+
try {
|
|
564
|
+
if (wait) {
|
|
565
|
+
const last = wait;
|
|
566
|
+
wait = undefined;
|
|
567
|
+
await last;
|
|
568
|
+
}
|
|
569
|
+
// The stream pulls until its queue is full, and each chunk it is
|
|
570
|
+
// given costs far more than rendering one. So what is rendered
|
|
571
|
+
// without waiting is sent together, up to as much as the stream
|
|
572
|
+
// would have pulled.
|
|
573
|
+
while (!cancelled && (result = render.step())) {
|
|
574
|
+
if (!(result instanceof Promise)) {
|
|
575
|
+
text += result.toString();
|
|
576
|
+
if (text.length >= highWaterMark)
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
else if (text) {
|
|
580
|
+
// send what is rendered before waiting for more
|
|
581
|
+
wait = result;
|
|
582
|
+
// handled when it is awaited, by the next pull or the close
|
|
583
|
+
result.catch(() => { });
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
else
|
|
587
|
+
await result;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
catch (error) {
|
|
591
|
+
await render.close();
|
|
592
|
+
throw error;
|
|
593
|
+
}
|
|
594
|
+
if (cancelled)
|
|
595
|
+
return;
|
|
596
|
+
if (text) {
|
|
597
|
+
c.enqueue(
|
|
598
|
+
// need to encode for Node (ex: during prerendering) or it will error
|
|
599
|
+
// doesn't seem to be needed for browsers
|
|
600
|
+
// faster than piping through a `TextEncoderStream`
|
|
601
|
+
Codec.encode(text));
|
|
602
|
+
}
|
|
603
|
+
if (!result)
|
|
604
|
+
c.close();
|
|
605
|
+
},
|
|
606
|
+
async cancel() {
|
|
607
|
+
cancelled = true;
|
|
608
|
+
await render.close();
|
|
609
|
+
},
|
|
610
|
+
}, { highWaterMark });
|
|
611
|
+
};
|
|
612
|
+
/**
|
|
613
|
+
* Renders an element to markup, such as HTML, SVG, or XML, as an
|
|
614
|
+
* `AsyncIterable` of strings, a stream, or a string. `Markdown` renders the
|
|
615
|
+
* same elements to Markdown.
|
|
616
|
+
*/
|
|
617
|
+
export class Markup {
|
|
618
|
+
/** Regex to find escape character for attributes */
|
|
619
|
+
static #attr = /[&"<]/g;
|
|
620
|
+
/** Regex to find escape character for content */
|
|
621
|
+
static #content = /[&<]/g;
|
|
622
|
+
/** Escape character map */
|
|
623
|
+
static #map = { "&": "&", '"': """, "<": "<" };
|
|
624
|
+
/** Element to render */
|
|
625
|
+
#element;
|
|
626
|
+
/** Markup options */
|
|
627
|
+
#options;
|
|
628
|
+
/**
|
|
629
|
+
* Creates an `AsyncIterable` that renders the `Element`.
|
|
630
|
+
*
|
|
631
|
+
* @param element Element to render
|
|
632
|
+
* @param options Markup options
|
|
633
|
+
*/
|
|
634
|
+
constructor(element, options) {
|
|
635
|
+
this.#element = element;
|
|
636
|
+
this.#options = options;
|
|
637
|
+
}
|
|
638
|
+
/** @returns The element, rendered in place where a `Markup` is a child */
|
|
639
|
+
get [source]() {
|
|
640
|
+
return this.#element;
|
|
641
|
+
}
|
|
642
|
+
/** @yields Markup as the `Element` resolves */
|
|
643
|
+
async *[Symbol.asyncIterator]() {
|
|
644
|
+
const render = new Render(this.#element, this.#options?.raw);
|
|
645
|
+
try {
|
|
646
|
+
for (let result; (result = render.step());) {
|
|
647
|
+
if (result instanceof Chunk)
|
|
648
|
+
yield result.toString();
|
|
649
|
+
else
|
|
650
|
+
await result;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
finally {
|
|
654
|
+
await render.close();
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* `render` piped into a `ReadableStream`.
|
|
659
|
+
* Use `render` when possible to avoid the overhead of the stream.
|
|
660
|
+
*
|
|
661
|
+
* @param element Element to render
|
|
662
|
+
* @param options Markup options
|
|
663
|
+
* @returns `ReadableStream` of markup
|
|
664
|
+
*/
|
|
665
|
+
static stream(element, options) {
|
|
666
|
+
return readable(new Render(element, options?.raw));
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Converts a render into a fully concatenated string of markup.
|
|
670
|
+
*
|
|
671
|
+
* ### WARNING
|
|
672
|
+
*
|
|
673
|
+
* This negates streaming benefits and buffers the result into a string.
|
|
674
|
+
*
|
|
675
|
+
* @param element Element to render
|
|
676
|
+
* @param options Markup options
|
|
677
|
+
* @returns Concatenated markup
|
|
678
|
+
*/
|
|
679
|
+
static async string(element, options) {
|
|
680
|
+
const render = new Render(element, options?.raw);
|
|
681
|
+
const chunks = [];
|
|
682
|
+
try {
|
|
683
|
+
for (let result; (result = render.step());) {
|
|
684
|
+
if (result instanceof Chunk)
|
|
685
|
+
chunks.push(result);
|
|
686
|
+
else
|
|
687
|
+
await result;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
finally {
|
|
691
|
+
await render.close();
|
|
692
|
+
}
|
|
693
|
+
// written once every chunk is rendered, as a group of them is
|
|
694
|
+
return new Group(chunks).toString();
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Render raw markup and dangerously bypass escaping. It is written as it is
|
|
698
|
+
* in Markdown too, which can contain HTML.
|
|
699
|
+
*
|
|
700
|
+
* @param html Safe markup to render
|
|
701
|
+
* @returns New _safe_ `Chunk`
|
|
702
|
+
*/
|
|
703
|
+
static raw(html) {
|
|
704
|
+
return new Chunk(html, true);
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Escapes text for markup.
|
|
708
|
+
*
|
|
709
|
+
* @param html String to escape
|
|
710
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it is content
|
|
711
|
+
* @returns Escaped text
|
|
712
|
+
*/
|
|
713
|
+
static escape(html, attr) {
|
|
714
|
+
return html.replace(attr ? Markup.#attr : Markup.#content, (c) =>
|
|
715
|
+
// @ts-expect-error - private method type error
|
|
716
|
+
Markup.#map[c]);
|
|
717
|
+
}
|
|
718
|
+
}
|