@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.11
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/action-context.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +470 -77
- package/dist/dom.js +18 -3
- package/dist/effects.js +2 -2
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +385 -66
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +454 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/list.d.ts +14 -4
- package/dist/list.js +4 -5
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +628 -98
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +0 -6
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -165
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -39
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -199
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -435
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -125
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -133
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -559
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -187
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -556
package/dist/head-tags.js
CHANGED
|
@@ -25,7 +25,7 @@ export const HEAD_CLOSE = "<!--/krate-head-->";
|
|
|
25
25
|
* markers. Both are removed from the returned html.
|
|
26
26
|
*/
|
|
27
27
|
export function takeHead(html) {
|
|
28
|
-
const hasHead = html.
|
|
28
|
+
const hasHead = html.includes(HEAD_OPEN);
|
|
29
29
|
const hasTitle = /<title[\s>]/i.test(html);
|
|
30
30
|
if (!hasHead && !hasTitle)
|
|
31
31
|
return { title: "", head: "", html };
|
|
@@ -83,7 +83,7 @@ export function takeHead(html) {
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
function takeTitles(html) {
|
|
86
|
-
if (html.
|
|
86
|
+
if (!html.includes("<title") && !html.includes("<TITLE"))
|
|
87
87
|
return { title: "", html };
|
|
88
88
|
const re = /<title(?:\s[^>]*)?>([\s\S]*?)<\/title>/gi;
|
|
89
89
|
const found = [];
|
|
@@ -107,7 +107,7 @@ function takeTitles(html) {
|
|
|
107
107
|
function insideSVG(html, pos) {
|
|
108
108
|
const before = html.slice(0, pos).toLowerCase();
|
|
109
109
|
let depth = 0;
|
|
110
|
-
const re = /<(\/?)svg[\s
|
|
110
|
+
const re = /<(\/?)svg[\s>/]/g;
|
|
111
111
|
let m;
|
|
112
112
|
while ((m = re.exec(before)))
|
|
113
113
|
depth += m[1] ? -1 : 1;
|
|
@@ -218,8 +218,11 @@ export function headKey(el) {
|
|
|
218
218
|
if (el.name === "meta") {
|
|
219
219
|
if (a.has("charset"))
|
|
220
220
|
return "meta:charset";
|
|
221
|
-
if (a.has("name"))
|
|
222
|
-
|
|
221
|
+
if (a.has("name")) {
|
|
222
|
+
// A media-qualified tag (light and dark theme-color) is a different tag.
|
|
223
|
+
const media = (a.get("media") || "").toLowerCase();
|
|
224
|
+
return "meta:name:" + (a.get("name") || "").toLowerCase() + (media ? "|" + media : "");
|
|
225
|
+
}
|
|
223
226
|
if (a.has("property"))
|
|
224
227
|
return "meta:property:" + (a.get("property") || "").toLowerCase();
|
|
225
228
|
if (a.has("http-equiv"))
|
|
@@ -318,6 +321,87 @@ export function headEndOf(doc) {
|
|
|
318
321
|
return h;
|
|
319
322
|
return lower.indexOf("<body");
|
|
320
323
|
}
|
|
324
|
+
const ATTR_NAME = /^[A-Za-z_:][-A-Za-z0-9_:.]*$/;
|
|
325
|
+
function decodeAttr(v) {
|
|
326
|
+
return v.replace(/&(quot|amp|lt|gt|apos|#34|#39);/g, (_m, e) => {
|
|
327
|
+
switch (e) {
|
|
328
|
+
case "quot":
|
|
329
|
+
case "#34":
|
|
330
|
+
return '"';
|
|
331
|
+
case "lt":
|
|
332
|
+
return "<";
|
|
333
|
+
case "gt":
|
|
334
|
+
return ">";
|
|
335
|
+
case "apos":
|
|
336
|
+
case "#39":
|
|
337
|
+
return "'";
|
|
338
|
+
default:
|
|
339
|
+
return "&";
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
/** Removes the <html> and <body> elements from a head fragment and returns their attributes. */
|
|
344
|
+
export function splitDocTags(fragment) {
|
|
345
|
+
const html = new Map();
|
|
346
|
+
const body = new Map();
|
|
347
|
+
if (!/<(html|body)[\s>/]/i.test(fragment))
|
|
348
|
+
return { rest: fragment, html, body };
|
|
349
|
+
let rest = "";
|
|
350
|
+
let at = 0;
|
|
351
|
+
for (const el of scanHeadElements(fragment)) {
|
|
352
|
+
if (el.name !== "html" && el.name !== "body")
|
|
353
|
+
continue;
|
|
354
|
+
const target = el.name === "html" ? html : body;
|
|
355
|
+
for (const [k, v] of el.attrs)
|
|
356
|
+
if (ATTR_NAME.test(k))
|
|
357
|
+
target.set(k, decodeAttr(v));
|
|
358
|
+
rest += fragment.slice(at, el.start);
|
|
359
|
+
at = el.end;
|
|
360
|
+
}
|
|
361
|
+
rest += fragment.slice(at);
|
|
362
|
+
return { rest: rest.replace(/<\/(html|body)\s*>/gi, ""), html, body };
|
|
363
|
+
}
|
|
364
|
+
function renderAttrs(attrs) {
|
|
365
|
+
return [...attrs.entries()]
|
|
366
|
+
.filter(([k]) => ATTR_NAME.test(k))
|
|
367
|
+
.sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0))
|
|
368
|
+
.map(([k, v]) => (v === "" ? " " + k : " " + k + '="' + escapeAttr(v) + '"'))
|
|
369
|
+
.join("");
|
|
370
|
+
}
|
|
371
|
+
function escapeAttr(v) {
|
|
372
|
+
return v.replace(/[&<>"']/g, (c) => (c === '"' ? """ : c === "&" ? "&" : c === "<" ? "<" : c === ">" ? ">" : "'"));
|
|
373
|
+
}
|
|
374
|
+
/** Sets attrs on the first start tag of `name` in doc, keeping the attributes it already has. */
|
|
375
|
+
export function setStartTagAttrs(doc, name, attrs) {
|
|
376
|
+
if (attrs.size === 0)
|
|
377
|
+
return doc;
|
|
378
|
+
const re = new RegExp("<" + name + "(?=[\\s>/])", "i");
|
|
379
|
+
const m = re.exec(doc);
|
|
380
|
+
if (!m)
|
|
381
|
+
return doc;
|
|
382
|
+
let end = m.index + 1;
|
|
383
|
+
let quote = "";
|
|
384
|
+
for (; end < doc.length; end++) {
|
|
385
|
+
const c = doc[end];
|
|
386
|
+
if (quote) {
|
|
387
|
+
if (c === quote)
|
|
388
|
+
quote = "";
|
|
389
|
+
}
|
|
390
|
+
else if (c === '"' || c === "'")
|
|
391
|
+
quote = c;
|
|
392
|
+
else if (c === ">") {
|
|
393
|
+
end++;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const merged = new Map();
|
|
398
|
+
for (const [k, v] of scanHeadElements(doc.slice(m.index, end))[0]?.attrs ?? [])
|
|
399
|
+
if (ATTR_NAME.test(k))
|
|
400
|
+
merged.set(k, decodeAttr(v));
|
|
401
|
+
for (const [k, v] of attrs)
|
|
402
|
+
merged.set(k, v);
|
|
403
|
+
return doc.slice(0, m.index) + "<" + name + renderAttrs(merged) + ">" + doc.slice(end);
|
|
404
|
+
}
|
|
321
405
|
/** Merges the title and head tags into the head of a whole document. */
|
|
322
406
|
export function mergeHead(doc, title, fragment) {
|
|
323
407
|
if (title === "" && fragment === "")
|
|
@@ -325,5 +409,8 @@ export function mergeHead(doc, title, fragment) {
|
|
|
325
409
|
const end = headEndOf(doc);
|
|
326
410
|
if (end === -1)
|
|
327
411
|
return doc;
|
|
328
|
-
|
|
412
|
+
const { rest, html, body } = splitDocTags(fragment);
|
|
413
|
+
let out = mergeHeadSection(doc.slice(0, end), title, rest) + doc.slice(end);
|
|
414
|
+
out = setStartTagAttrs(out, "html", html);
|
|
415
|
+
return setStartTagAttrs(out, "body", body);
|
|
329
416
|
}
|
package/dist/highlight-bind.js
CHANGED
|
@@ -126,7 +126,7 @@ function bindCode(code) {
|
|
|
126
126
|
return;
|
|
127
127
|
for (let n = node.nextSibling; n && n !== r.end;) {
|
|
128
128
|
const next = n.nextSibling;
|
|
129
|
-
n.parentNode
|
|
129
|
+
n.parentNode?.removeChild(n);
|
|
130
130
|
n = next;
|
|
131
131
|
}
|
|
132
132
|
const text = node.textContent || "";
|
package/dist/highlight.d.ts
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* (rule lists with JavaScript regular expressions); this file runs them. It
|
|
5
5
|
* follows chroma's RegexLexer state machine and its HTML formatter, so for the
|
|
6
6
|
* same code and language the markup is identical to what a build produces:
|
|
7
|
-
* one `<span class="line
|
|
8
|
-
*
|
|
7
|
+
* one classed span per token (wrapped in a `<span class="line hl">` only for
|
|
8
|
+
* lines marked for highlighting), coloured by the same stylesheet in light and
|
|
9
|
+
* dark.
|
|
9
10
|
*
|
|
10
11
|
* Nothing here touches the DOM or any global, so it runs unchanged on the
|
|
11
12
|
* server (SSR sidecars) and in the browser.
|
package/dist/highlight.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* (rule lists with JavaScript regular expressions); this file runs them. It
|
|
5
5
|
* follows chroma's RegexLexer state machine and its HTML formatter, so for the
|
|
6
6
|
* same code and language the markup is identical to what a build produces:
|
|
7
|
-
* one `<span class="line
|
|
8
|
-
*
|
|
7
|
+
* one classed span per token (wrapped in a `<span class="line hl">` only for
|
|
8
|
+
* lines marked for highlighting), coloured by the same stylesheet in light and
|
|
9
|
+
* dark.
|
|
9
10
|
*
|
|
10
11
|
* Nothing here touches the DOM or any global, so it runs unchanged on the
|
|
11
12
|
* server (SSR sidecars) and in the browser.
|
|
@@ -64,7 +65,7 @@ function lexerFor(name) {
|
|
|
64
65
|
return key === undefined ? undefined : lexers.get(key);
|
|
65
66
|
}
|
|
66
67
|
function ensureLF(text) {
|
|
67
|
-
if (text.
|
|
68
|
+
if (!text.includes("\r"))
|
|
68
69
|
return text;
|
|
69
70
|
return text.replace(/\r\n?/g, "\n");
|
|
70
71
|
}
|
|
@@ -125,7 +126,7 @@ function lex(lx, text, startState, nested, out) {
|
|
|
125
126
|
continue;
|
|
126
127
|
}
|
|
127
128
|
const cp = text.codePointAt(pos);
|
|
128
|
-
const n = cp >
|
|
129
|
+
const n = cp > 0xFFFF ? 2 : 1;
|
|
129
130
|
out.push(["err", text.substr(pos, n)]);
|
|
130
131
|
pos += n;
|
|
131
132
|
continue;
|
|
@@ -212,7 +213,6 @@ function emitTokens(lx, emit, groups, out) {
|
|
|
212
213
|
emitTokens(lx, e, [group(groups, gi)], out);
|
|
213
214
|
}
|
|
214
215
|
}
|
|
215
|
-
return;
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
}
|
|
@@ -245,12 +245,12 @@ export function parseLineRanges(spec) {
|
|
|
245
245
|
if (part === "")
|
|
246
246
|
continue;
|
|
247
247
|
const dash = part.indexOf("-");
|
|
248
|
-
const lo = dash
|
|
248
|
+
const lo = dash === -1 ? part : part.slice(0, dash);
|
|
249
249
|
const a = atoi(lo.trim());
|
|
250
250
|
if (a === null || a < 1)
|
|
251
251
|
continue;
|
|
252
252
|
let b = a;
|
|
253
|
-
if (dash
|
|
253
|
+
if (dash !== -1) {
|
|
254
254
|
const hi = atoi(part.slice(dash + 1).trim());
|
|
255
255
|
if (hi === null || hi < a)
|
|
256
256
|
continue;
|
|
@@ -303,19 +303,24 @@ function shouldHighlight(ranges, idx, line) {
|
|
|
303
303
|
/** Writes tokens as chroma's HTML formatter does (class mode, no line numbers). */
|
|
304
304
|
function format(tokens, ranges) {
|
|
305
305
|
const lines = splitLines(tokens);
|
|
306
|
+
// Line wrappers only carry the highlight class, so unmarked code omits them.
|
|
307
|
+
const wrap = ranges.length > 0;
|
|
306
308
|
let html = "";
|
|
307
309
|
let idx = 0;
|
|
308
310
|
for (let i = 0; i < lines.length; i++) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
311
|
+
if (wrap) {
|
|
312
|
+
const [hl, next] = shouldHighlight(ranges, idx, i + 1);
|
|
313
|
+
idx = next;
|
|
314
|
+
html += hl ? '<span class="line hl"><span class="cl">' : '<span class="line"><span class="cl">';
|
|
315
|
+
}
|
|
312
316
|
for (const [cls, value] of lines[i]) {
|
|
313
317
|
if (cls === null)
|
|
314
318
|
continue;
|
|
315
319
|
const text = escapeHTML(value);
|
|
316
320
|
html += cls === "" ? text : '<span class="' + cls + '">' + text + "</span>";
|
|
317
321
|
}
|
|
318
|
-
|
|
322
|
+
if (wrap)
|
|
323
|
+
html += "</span></span>";
|
|
319
324
|
}
|
|
320
325
|
return html;
|
|
321
326
|
}
|
|
@@ -397,7 +402,7 @@ function splitFrontmatter(text) {
|
|
|
397
402
|
return null;
|
|
398
403
|
for (let pos = lineEnd + 1; pos < text.length;) {
|
|
399
404
|
const next = text.indexOf("\n", pos);
|
|
400
|
-
if (next
|
|
405
|
+
if (next === -1)
|
|
401
406
|
break;
|
|
402
407
|
lineEnd = next;
|
|
403
408
|
const line = text.slice(pos, lineEnd).replace(/\r$/, "");
|
package/dist/hints.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
interface HintOptions {
|
|
1
|
+
export interface HintOptions {
|
|
2
2
|
as?: string;
|
|
3
3
|
crossOrigin?: string;
|
|
4
4
|
integrity?: string;
|
|
@@ -9,11 +9,34 @@ interface HintOptions {
|
|
|
9
9
|
referrerPolicy?: string;
|
|
10
10
|
imageSrcSet?: string;
|
|
11
11
|
imageSizes?: string;
|
|
12
|
+
precedence?: string;
|
|
12
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Records head tags the build collected for a component that the server does
|
|
16
|
+
* not render (a real-React client component). Server only; the browser
|
|
17
|
+
* already has them in the document.
|
|
18
|
+
*/
|
|
19
|
+
export declare function krateHintTags(tags: readonly string[]): void;
|
|
13
20
|
export declare function krateHintPreload(href: string, opts?: HintOptions): void;
|
|
14
21
|
export declare function krateHintPreloadModule(href: string, opts?: HintOptions): void;
|
|
15
22
|
export declare function krateHintPreconnect(href: string, opts?: HintOptions): void;
|
|
16
23
|
export declare function krateHintPrefetchDNS(href: string): void;
|
|
17
24
|
export declare function krateHintPreinit(href: string, opts?: HintOptions): void;
|
|
18
25
|
export declare function krateHintPreinitModule(href: string, opts?: HintOptions): void;
|
|
19
|
-
export
|
|
26
|
+
export declare const preload: typeof krateHintPreload;
|
|
27
|
+
export declare const preinit: typeof krateHintPreinit;
|
|
28
|
+
export declare const preconnect: typeof krateHintPreconnect;
|
|
29
|
+
export declare const prefetchDNS: typeof krateHintPrefetchDNS;
|
|
30
|
+
export declare const preloadModule: typeof krateHintPreloadModule;
|
|
31
|
+
export declare const preinitModule: typeof krateHintPreinitModule;
|
|
32
|
+
export declare function flushSync<T>(fn: () => T): T;
|
|
33
|
+
declare const _default: {
|
|
34
|
+
preload: typeof krateHintPreload;
|
|
35
|
+
preinit: typeof krateHintPreinit;
|
|
36
|
+
preconnect: typeof krateHintPreconnect;
|
|
37
|
+
prefetchDNS: typeof krateHintPrefetchDNS;
|
|
38
|
+
preloadModule: typeof krateHintPreloadModule;
|
|
39
|
+
preinitModule: typeof krateHintPreinitModule;
|
|
40
|
+
flushSync: typeof flushSync;
|
|
41
|
+
};
|
|
42
|
+
export default _default;
|
package/dist/hints.js
CHANGED
|
@@ -1,97 +1,170 @@
|
|
|
1
1
|
// React DOM's resource hints (preload, preinit, preconnect, prefetchDNS,
|
|
2
2
|
// preloadModule, preinitModule). React emits <link>/<script> tags into the
|
|
3
|
-
// document head for them
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
// document head for them. In the browser they add the tag once, and nothing
|
|
4
|
+
// when the document already has it (the server HTML put it there). While the
|
|
5
|
+
// server renders, they record the tag on the render scope and the renderer
|
|
6
|
+
// puts the recorded tags into the <head> of the response (withCacheScope).
|
|
7
|
+
// When the build renders an island outside any request, they record the tag
|
|
8
|
+
// in the set the build installs as globalThis.__krate_island_hints, and the
|
|
9
|
+
// build puts it into the head of the page.
|
|
10
|
+
// This module must stay free of imports: island bundles inline it.
|
|
11
|
+
function optAttrs(opts, names) {
|
|
12
|
+
const out = [];
|
|
13
|
+
if (!opts)
|
|
14
|
+
return out;
|
|
15
|
+
for (const name of names) {
|
|
16
|
+
const v = opts[name];
|
|
17
|
+
if (v === undefined || v === null || v === false)
|
|
18
|
+
continue;
|
|
19
|
+
// crossOrigin: "" is a valid value (anonymous); the others need text.
|
|
20
|
+
if (v === '' && name !== 'crossOrigin')
|
|
21
|
+
continue;
|
|
22
|
+
const attr = name === 'crossOrigin' ? 'crossorigin' :
|
|
23
|
+
name === 'fetchPriority' ? 'fetchpriority' :
|
|
24
|
+
name === 'referrerPolicy' ? 'referrerpolicy' :
|
|
25
|
+
name === 'imageSrcSet' ? 'imagesrcset' :
|
|
26
|
+
name === 'imageSizes' ? 'imagesizes' : name;
|
|
27
|
+
if (name === 'crossOrigin') {
|
|
28
|
+
// As React: "use-credentials" or anonymous (""), so react-dom in a
|
|
29
|
+
// real-React island finds the tag by the same selector.
|
|
30
|
+
out.push([attr, v === 'use-credentials' ? v : '']);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
out.push([attr, v === true ? '' : String(v)]);
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
6
36
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
37
|
+
function escapeAttr(v) {
|
|
38
|
+
return v.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
|
|
39
|
+
}
|
|
40
|
+
// The scope of the render that is running on the server, if any. The cache
|
|
41
|
+
// runtime keeps it on globalThis, so every copy of this module sees it.
|
|
42
|
+
function serverScope() {
|
|
43
|
+
const rt = globalThis[Symbol.for('krate.cache.runtime')];
|
|
44
|
+
const store = rt && rt.renderStore;
|
|
45
|
+
return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
|
|
46
|
+
}
|
|
47
|
+
// serverHints is where a hint called outside a browser goes: the running
|
|
48
|
+
// render's scope, else the build's island collector, else nowhere.
|
|
49
|
+
function serverHints() {
|
|
50
|
+
const scope = serverScope();
|
|
51
|
+
if (scope)
|
|
52
|
+
return (scope.hints ?? (scope.hints = new Set()));
|
|
53
|
+
const c = globalThis.__krate_island_hints;
|
|
54
|
+
return c instanceof Set ? c : undefined;
|
|
55
|
+
}
|
|
56
|
+
function absURL(href) {
|
|
57
|
+
try {
|
|
58
|
+
return new URL(href, document.baseURI).href;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return href;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// present reports whether the document already has the tag: the same rel (or
|
|
65
|
+
// script type) and the same URL, however it is spelled. A preload also needs
|
|
66
|
+
// the same `as`, as in React.
|
|
67
|
+
function present(tag, attrs) {
|
|
68
|
+
const get = (name) => {
|
|
69
|
+
for (const [k, v] of attrs)
|
|
70
|
+
if (k === name)
|
|
71
|
+
return v;
|
|
72
|
+
};
|
|
73
|
+
if (tag === 'script') {
|
|
74
|
+
const src = absURL(get('src') || '');
|
|
75
|
+
const module = get('type') === 'module';
|
|
76
|
+
for (const el of Array.from(document.querySelectorAll('script[src]'))) {
|
|
77
|
+
if (el.src === src && (el.type === 'module') === module)
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const rel = get('rel') || '';
|
|
83
|
+
const href = absURL(get('href') || '');
|
|
84
|
+
const as = get('as');
|
|
85
|
+
for (const el of Array.from(document.querySelectorAll('link[href]'))) {
|
|
86
|
+
if (el.href !== href)
|
|
87
|
+
continue;
|
|
88
|
+
if ((el.getAttribute('rel') || '').toLowerCase() !== rel)
|
|
89
|
+
continue;
|
|
90
|
+
if (rel === 'preload' && (el.getAttribute('as') || '') !== (as || ''))
|
|
91
|
+
continue;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
function tagText(tag, attrs) {
|
|
97
|
+
const text = attrs.map(([k, v]) => (v === '' && k !== 'crossorigin' ? ' ' + k : ' ' + k + '="' + escapeAttr(v) + '"')).join('');
|
|
98
|
+
return tag === 'link' ? '<link' + text + '>' : '<script' + text + '></script>';
|
|
99
|
+
}
|
|
100
|
+
// emit adds one head tag: to the document in a browser, to the render scope
|
|
101
|
+
// (or the build's collector) on the server. A tag that is already there is
|
|
102
|
+
// not added twice.
|
|
103
|
+
function emit(tag, attrs) {
|
|
104
|
+
if (typeof document === 'undefined') {
|
|
105
|
+
serverHints()?.add(tagText(tag, attrs));
|
|
12
106
|
return;
|
|
13
|
-
const link = document.createElement('link');
|
|
14
|
-
link.rel = rel;
|
|
15
|
-
link.href = href;
|
|
16
|
-
if (opts) {
|
|
17
|
-
if (opts.as)
|
|
18
|
-
link.as = opts.as;
|
|
19
|
-
if (opts.crossOrigin !== undefined)
|
|
20
|
-
link.crossOrigin = opts.crossOrigin;
|
|
21
|
-
if (opts.integrity)
|
|
22
|
-
link.integrity = opts.integrity;
|
|
23
|
-
if (opts.type)
|
|
24
|
-
link.type = opts.type;
|
|
25
|
-
if (opts.media)
|
|
26
|
-
link.media = opts.media;
|
|
27
|
-
if (opts.nonce)
|
|
28
|
-
link.nonce = opts.nonce;
|
|
29
|
-
if (opts.fetchPriority)
|
|
30
|
-
link.setAttribute('fetchpriority', opts.fetchPriority);
|
|
31
|
-
if (opts.referrerPolicy)
|
|
32
|
-
link.referrerPolicy = opts.referrerPolicy;
|
|
33
|
-
if (opts.imageSrcSet)
|
|
34
|
-
link.setAttribute('imagesrcset', opts.imageSrcSet);
|
|
35
|
-
if (opts.imageSizes)
|
|
36
|
-
link.setAttribute('imagesizes', opts.imageSizes);
|
|
37
107
|
}
|
|
38
|
-
|
|
108
|
+
if (present(tag, attrs))
|
|
109
|
+
return;
|
|
110
|
+
const el = document.createElement(tag);
|
|
111
|
+
for (const [k, v] of attrs)
|
|
112
|
+
el.setAttribute(k, v);
|
|
113
|
+
document.head.appendChild(el);
|
|
114
|
+
}
|
|
115
|
+
function link(rel, href, opts, names) {
|
|
116
|
+
if (!href || typeof href !== 'string')
|
|
117
|
+
return;
|
|
118
|
+
emit('link', [['rel', rel], ['href', href], ...optAttrs(opts, names)]);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Records head tags the build collected for a component that the server does
|
|
122
|
+
* not render (a real-React client component). Server only; the browser
|
|
123
|
+
* already has them in the document.
|
|
124
|
+
*/
|
|
125
|
+
export function krateHintTags(tags) {
|
|
126
|
+
if (typeof document !== 'undefined')
|
|
127
|
+
return;
|
|
128
|
+
const hints = serverHints();
|
|
129
|
+
if (hints)
|
|
130
|
+
for (const t of tags)
|
|
131
|
+
hints.add(t);
|
|
39
132
|
}
|
|
40
133
|
export function krateHintPreload(href, opts) {
|
|
41
|
-
|
|
134
|
+
link('preload', href, opts, ['as', 'crossOrigin', 'integrity', 'type', 'media', 'nonce', 'fetchPriority', 'referrerPolicy', 'imageSrcSet', 'imageSizes']);
|
|
42
135
|
}
|
|
43
136
|
export function krateHintPreloadModule(href, opts) {
|
|
44
|
-
|
|
137
|
+
link('modulepreload', href, opts, ['as', 'crossOrigin', 'integrity', 'nonce']);
|
|
45
138
|
}
|
|
46
139
|
export function krateHintPreconnect(href, opts) {
|
|
47
|
-
|
|
140
|
+
link('preconnect', href, opts, ['crossOrigin']);
|
|
48
141
|
}
|
|
49
142
|
export function krateHintPrefetchDNS(href) {
|
|
50
|
-
|
|
143
|
+
link('dns-prefetch', href, undefined, []);
|
|
51
144
|
}
|
|
52
145
|
export function krateHintPreinit(href, opts) {
|
|
53
|
-
if (typeof
|
|
146
|
+
if (!href || typeof href !== 'string')
|
|
54
147
|
return;
|
|
55
148
|
if (opts && opts.as === 'script') {
|
|
56
|
-
|
|
57
|
-
return;
|
|
58
|
-
const s = document.createElement('script');
|
|
59
|
-
s.src = href;
|
|
60
|
-
s.async = true;
|
|
61
|
-
if (opts.crossOrigin !== undefined)
|
|
62
|
-
s.crossOrigin = opts.crossOrigin;
|
|
63
|
-
if (opts.integrity)
|
|
64
|
-
s.integrity = opts.integrity;
|
|
65
|
-
if (opts.nonce)
|
|
66
|
-
s.nonce = opts.nonce;
|
|
67
|
-
document.head.appendChild(s);
|
|
149
|
+
emit('script', [['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce', 'fetchPriority'])]);
|
|
68
150
|
return;
|
|
69
151
|
}
|
|
70
|
-
// as: "style" (the default use)
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
const link = document.createElement('link');
|
|
74
|
-
link.rel = 'stylesheet';
|
|
75
|
-
link.href = href;
|
|
76
|
-
if (opts && opts.crossOrigin !== undefined)
|
|
77
|
-
link.crossOrigin = opts.crossOrigin;
|
|
78
|
-
if (opts && opts.integrity)
|
|
79
|
-
link.integrity = opts.integrity;
|
|
80
|
-
if (opts && opts.media)
|
|
81
|
-
link.media = opts.media;
|
|
82
|
-
document.head.appendChild(link);
|
|
152
|
+
// as: "style" (the default use) loads the stylesheet.
|
|
153
|
+
link('stylesheet', href, opts, ['crossOrigin', 'integrity', 'media', 'nonce', 'fetchPriority']);
|
|
83
154
|
}
|
|
84
155
|
export function krateHintPreinitModule(href, opts) {
|
|
85
|
-
if (typeof
|
|
156
|
+
if (!href || typeof href !== 'string')
|
|
86
157
|
return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
158
|
+
emit('script', [['type', 'module'], ['src', href], ['async', ''], ...optAttrs(opts, ['crossOrigin', 'integrity', 'nonce'])]);
|
|
159
|
+
}
|
|
160
|
+
// The react-dom exports a server module can use. flushSync(fn) is fn() here.
|
|
161
|
+
export const preload = krateHintPreload;
|
|
162
|
+
export const preinit = krateHintPreinit;
|
|
163
|
+
export const preconnect = krateHintPreconnect;
|
|
164
|
+
export const prefetchDNS = krateHintPrefetchDNS;
|
|
165
|
+
export const preloadModule = krateHintPreloadModule;
|
|
166
|
+
export const preinitModule = krateHintPreinitModule;
|
|
167
|
+
export function flushSync(fn) {
|
|
168
|
+
return fn();
|
|
97
169
|
}
|
|
170
|
+
export default { preload, preinit, preconnect, prefetchDNS, preloadModule, preinitModule, flushSync };
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { clearPersisted, readPersisted, writePersisted } from './storage.js';
|
|
|
10
10
|
export type { PersistOptions, PersistSpec, PersistStore, StorageLike } from './storage.js';
|
|
11
11
|
export { h, mount, hydrate, insert, clearNodes, disposeNode, krateApplyStyle } from './dom.js';
|
|
12
12
|
export { krateList, krateBindList } from './list.js';
|
|
13
|
+
export type { KrateListValue } from './list.js';
|
|
13
14
|
export { krateHighlightInit, krateHighlight } from './highlight-bind.js';
|
|
14
15
|
export { krateCondSwap, krateCondDetached, krateCondReset, krateSanitizeAttr, BLOCKED_URL } from './slots.js';
|
|
15
16
|
export { Fragment, jsx, jsxs } from './jsx-runtime.js';
|
|
@@ -29,4 +30,5 @@ export type { ResourceReturn, ResourceActions } from './resource.js';
|
|
|
29
30
|
export type { Component, PropsWithChildren, ComponentProps, RefCallback } from './dom.js';
|
|
30
31
|
export type { RefObject, Ref, UseRefResult } from './signal.js';
|
|
31
32
|
import './jsx-types.js';
|
|
33
|
+
export { Link, Head, Icon, Image, Script, Style, Suspense, SyntaxHighlight } from './builtin-tags.js';
|
|
32
34
|
import './css-modules.js';
|
package/dist/index.js
CHANGED
|
@@ -16,4 +16,7 @@ export { createContext, createScope, createScopeFlags, resetContexts } from './c
|
|
|
16
16
|
export { createResource } from './resource.js';
|
|
17
17
|
export { requestFormReset, getFormStatus, isServerAction, ServerActionError } from './actions.js';
|
|
18
18
|
import './jsx-types.js';
|
|
19
|
+
// The compiler provides these tags as globals. The values here make
|
|
20
|
+
// `import { Head } from "@krate/runtime"` a real export; see builtin-tags.ts.
|
|
21
|
+
export { Link, Head, Icon, Image, Script, Style, Suspense, SyntaxHighlight } from './builtin-tags.js';
|
|
19
22
|
import './css-modules.js';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** A server-children slot a Preact island adopted from the document. */
|
|
2
|
+
export interface IslandSlot {
|
|
3
|
+
id: string;
|
|
4
|
+
/** The slot's server HTML (outerHTML of its nodes) when it was adopted. */
|
|
5
|
+
html: string;
|
|
6
|
+
/** The live top-level nodes of the slot (the island keeps the array current). */
|
|
7
|
+
set: Node[];
|
|
8
|
+
}
|
|
9
|
+
/** What an island chunk's mount returns. */
|
|
10
|
+
export interface IslandHandle {
|
|
11
|
+
update(props: unknown, pool?: Record<string, Node[][]>): unknown;
|
|
12
|
+
unmount(): void;
|
|
13
|
+
slots?: IslandSlot[];
|
|
14
|
+
/** Re-reads the slots' sets from the document (nested islands changed their nodes). */
|
|
15
|
+
sync?(): void;
|
|
16
|
+
}
|
|
17
|
+
/** An island record of window.__krateIslands. */
|
|
18
|
+
export interface IslandRecord {
|
|
19
|
+
status?: string;
|
|
20
|
+
handle?: IslandHandle | null;
|
|
21
|
+
gone?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Serializes nodes as the island chunks snapshot a slot's server HTML. */
|
|
24
|
+
export declare function nodesHTML(nodes: ArrayLike<Node>): string;
|
|
25
|
+
/**
|
|
26
|
+
* HTML with every island replaced by a marker naming its component, so two
|
|
27
|
+
* renders compare equal when only the islands (their props, or what they
|
|
28
|
+
* rendered since) differ.
|
|
29
|
+
*/
|
|
30
|
+
export declare function maskIslands(html: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Unmounts every island in nodes, innermost first, so their effects and
|
|
33
|
+
* portals are cleaned up before the nodes leave the document. An island still
|
|
34
|
+
* loading is marked gone (the loader unmounts it once its chunk mounted).
|
|
35
|
+
*/
|
|
36
|
+
export declare function unmountIslandsIn(nodes: ArrayLike<Node>): void;
|
|
37
|
+
export interface SwapOptions {
|
|
38
|
+
/** Only islands whose server props are unchanged are kept (a navigation to another page). */
|
|
39
|
+
sameProps?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Replaces the live children of parent between start and end (exclusive;
|
|
43
|
+
* all children when both are null) with newNodes (imported into the
|
|
44
|
+
* document, detached), keeping matched islands mounted. Returns the pending
|
|
45
|
+
* work of island updates (a re-render that loads more chunks first).
|
|
46
|
+
*/
|
|
47
|
+
export declare function swapPreservingIslands(parent: Node, start: Node | null, end: Node | null, newNodes: Node[], opts?: SwapOptions): Promise<unknown>[];
|