@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10
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 +496 -91
- package/dist/dom.d.ts +4 -0
- package/dist/dom.js +62 -16
- package/dist/effects.js +43 -10
- package/dist/events.d.ts +2 -0
- package/dist/events.js +18 -0
- 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 +407 -80
- 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 +455 -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/krate-suspense.js +1 -1
- package/dist/list.d.ts +14 -4
- package/dist/list.js +30 -23
- 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 +675 -100
- 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 +47 -20
- 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/suspense.js +34 -2
- 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 -152
- 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 -31
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -163
- 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 -344
- 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 -98
- 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 -96
- 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 -464
- 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 -169
- 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 -518
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { allSuspense, canStreamOutOfOrder, flashMessages, injectFlashNotice, parseRegionShell, regionReplaceScript, replaceTitle, requestFlash, signFlash, verifyFlash, flashPayloadOf, splitBodyScripts, spliceShell, } from "./handler-html.js";
|
|
4
|
-
const suspense = (id, inner = "") => `<!--suspense:${id}-->${inner}<!--/suspense:${id}-->`;
|
|
5
|
-
const region = (id, inner = "") => `<!--region:${id}-->${inner}<!--/region:${id}-->`;
|
|
6
|
-
function splice(shell, html) {
|
|
7
|
-
const parsed = parseRegionShell(shell);
|
|
8
|
-
expect(parsed.ok).toBe(true);
|
|
9
|
-
const out = [];
|
|
10
|
-
spliceShell(shell, parsed.roots, new Map(Object.entries(html)), (c) => out.push(c));
|
|
11
|
-
return out.join("");
|
|
12
|
-
}
|
|
13
|
-
describe("parseRegionShell", () => {
|
|
14
|
-
it("reports a shell without markers as not ok", () => {
|
|
15
|
-
const p = parseRegionShell("<html><body>static</body></html>");
|
|
16
|
-
expect(p.ok).toBe(false);
|
|
17
|
-
expect(p.roots).toEqual([]);
|
|
18
|
-
});
|
|
19
|
-
it("finds sibling and nested markers in document order", () => {
|
|
20
|
-
const shell = `<main>${suspense("a", "fb-a")}${region("b", suspense("c", "fb-c"))}</main>`;
|
|
21
|
-
const p = parseRegionShell(shell);
|
|
22
|
-
expect(p.ok).toBe(true);
|
|
23
|
-
expect(p.all.map((b) => b.id)).toEqual(["a", "b", "c"]);
|
|
24
|
-
expect(p.roots.map((b) => b.id)).toEqual(["a", "b"]);
|
|
25
|
-
expect(p.roots[1].children.map((b) => b.id)).toEqual(["c"]);
|
|
26
|
-
const a = p.all[0];
|
|
27
|
-
expect(shell.slice(a.openEnd, a.fbEnd)).toBe("fb-a");
|
|
28
|
-
});
|
|
29
|
-
it("rejects a marker whose closing comment is missing, dropping partial results", () => {
|
|
30
|
-
const p = parseRegionShell(`${suspense("a", "x")}<!--suspense:b-->never closed`);
|
|
31
|
-
expect(p.ok).toBe(false);
|
|
32
|
-
expect(p.roots).toEqual([]);
|
|
33
|
-
expect(p.all).toEqual([]);
|
|
34
|
-
});
|
|
35
|
-
it("does not confuse a region and a suspense marker that share an id", () => {
|
|
36
|
-
const p = parseRegionShell(`<!--suspense:x-->s<!--/region:x-->`);
|
|
37
|
-
expect(p.ok).toBe(false);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
describe("streaming mode", () => {
|
|
41
|
-
it("streams siblings out of order, but not the coarse page region or nested markers", () => {
|
|
42
|
-
expect(canStreamOutOfOrder(parseRegionShell(suspense("a") + suspense("b")).all)).toBe(true);
|
|
43
|
-
expect(canStreamOutOfOrder(parseRegionShell(suspense("page")).all)).toBe(false);
|
|
44
|
-
expect(canStreamOutOfOrder(parseRegionShell(suspense("a", suspense("b"))).all)).toBe(false);
|
|
45
|
-
expect(canStreamOutOfOrder([])).toBe(false);
|
|
46
|
-
});
|
|
47
|
-
it("allSuspense is false as soon as one region marker is present", () => {
|
|
48
|
-
expect(allSuspense(parseRegionShell(suspense("a") + suspense("b")).all)).toBe(true);
|
|
49
|
-
expect(allSuspense(parseRegionShell(suspense("a") + region("b")).all)).toBe(false);
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
describe("splitBodyScripts", () => {
|
|
53
|
-
it("defers the first body script and everything after it", () => {
|
|
54
|
-
const [head, tail] = splitBodyScripts("<html><body><p>x</p><SCRIPT src=/a.js></SCRIPT></body></html>");
|
|
55
|
-
expect(head).toBe("<html><body><p>x</p>");
|
|
56
|
-
expect(tail).toBe("<SCRIPT src=/a.js></SCRIPT></body></html>");
|
|
57
|
-
});
|
|
58
|
-
it("ignores a script in <head>", () => {
|
|
59
|
-
const shell = "<html><head><script>1</script></head><body><p>x</p></body></html>";
|
|
60
|
-
const [head, tail] = splitBodyScripts(shell);
|
|
61
|
-
expect(head).toBe("<html><head><script>1</script></head><body><p>x</p>");
|
|
62
|
-
expect(tail).toBe("</body></html>");
|
|
63
|
-
});
|
|
64
|
-
it("returns the whole shell when there is no body", () => {
|
|
65
|
-
expect(splitBodyScripts("<p>fragment</p>")).toEqual(["<p>fragment</p>", ""]);
|
|
66
|
-
});
|
|
67
|
-
it("round-trips: head + tail is the original", () => {
|
|
68
|
-
const shell = "<body>a<script>b</script>c</body>";
|
|
69
|
-
const [h, t] = splitBodyScripts(shell);
|
|
70
|
-
expect(h + t).toBe(shell);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
describe("replaceTitle", () => {
|
|
74
|
-
it("swaps the content and drops title attributes", () => {
|
|
75
|
-
expect(replaceTitle('<head><title data-x="1">Old</title></head>', "New")).toBe("<head><title>New</title></head>");
|
|
76
|
-
});
|
|
77
|
-
it("leaves a shell without a title alone", () => {
|
|
78
|
-
expect(replaceTitle("<head></head>", "New")).toBe("<head></head>");
|
|
79
|
-
});
|
|
80
|
-
it("does not interpret replacement patterns in the new title", () => {
|
|
81
|
-
expect(replaceTitle("<title>Old</title>", "Cost $& $1 $$")).toBe("<title>Cost $& $1 $$</title>");
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
describe("regionReplaceScript", () => {
|
|
85
|
-
const payloadOf = (script) => {
|
|
86
|
-
const m = /var html=(".*?");var kinds=/s.exec(script);
|
|
87
|
-
expect(m).not.toBeNull();
|
|
88
|
-
return JSON.parse(m[1]);
|
|
89
|
-
};
|
|
90
|
-
it("embeds the HTML so it cannot close the script element or break the literal", () => {
|
|
91
|
-
const html = `</script><script>alert(1)</script> & "q" \u2028 \u2029 <!-- x -->`;
|
|
92
|
-
const script = regionReplaceScript("r1", html);
|
|
93
|
-
// Only the wrapper's own tags may appear: the payload has no raw < or >.
|
|
94
|
-
expect(script.match(/<\/script>/gi)).toHaveLength(1);
|
|
95
|
-
expect(script.match(/<script/gi)).toHaveLength(1);
|
|
96
|
-
expect(script).not.toContain("\u2028");
|
|
97
|
-
expect(script).not.toContain("\u2029");
|
|
98
|
-
expect(payloadOf(script)).toBe(html);
|
|
99
|
-
});
|
|
100
|
-
it("quotes the id as a JSON string", () => {
|
|
101
|
-
const script = regionReplaceScript("a\"b'c\\d", "x");
|
|
102
|
-
expect(script).toContain('var id="a\\"b\'c\\\\d";');
|
|
103
|
-
});
|
|
104
|
-
it("executes against a minimal DOM and replaces only the marked block", () => {
|
|
105
|
-
// Run the generated script with stub DOM objects to check the splice logic.
|
|
106
|
-
const comments = [
|
|
107
|
-
{ nodeType: 8, nodeValue: "suspense:r1", nextSibling: null },
|
|
108
|
-
{ nodeType: 8, nodeValue: "/suspense:r1", nextSibling: null },
|
|
109
|
-
];
|
|
110
|
-
comments[0].nextSibling = { nodeType: 3, nodeValue: "fallback", nextSibling: comments[1] };
|
|
111
|
-
const calls = [];
|
|
112
|
-
let idx = -1;
|
|
113
|
-
const doc = {
|
|
114
|
-
documentElement: {},
|
|
115
|
-
createTreeWalker: () => ({
|
|
116
|
-
get currentNode() {
|
|
117
|
-
return comments[idx];
|
|
118
|
-
},
|
|
119
|
-
nextNode: () => ++idx < comments.length,
|
|
120
|
-
}),
|
|
121
|
-
createRange: () => ({
|
|
122
|
-
setStartAfter: (n) => calls.push("start:" + n.nodeValue),
|
|
123
|
-
setEndBefore: (n) => calls.push("end:" + n.nodeValue),
|
|
124
|
-
deleteContents: () => calls.push("delete"),
|
|
125
|
-
insertNode: (n) => calls.push("insert:" + n),
|
|
126
|
-
}),
|
|
127
|
-
createElement: () => {
|
|
128
|
-
const t = { content: "<c>", set innerHTML(v) { t.content = v; } };
|
|
129
|
-
return t;
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
const code = regionReplaceScript("r1", "<b>done</b>").replace(/^<script[^>]*>/, "").replace(/<\/script>$/, "");
|
|
133
|
-
new Function("document", "NodeFilter", code)(doc, { SHOW_COMMENT: 128 });
|
|
134
|
-
expect(calls).toEqual(["start:suspense:r1", "end:/suspense:r1", "delete", "insert:<b>done</b>"]);
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
describe("spliceShell", () => {
|
|
138
|
-
it("replaces the baked content of a marker and keeps the markers and the rest of the shell", () => {
|
|
139
|
-
const shell = `<main>${suspense("a", "loading")}<footer>f</footer></main>`;
|
|
140
|
-
expect(splice(shell, { a: "<p>done</p>" })).toBe(`<main>${suspense("a", "<p>done</p>")}<footer>f</footer></main>`);
|
|
141
|
-
});
|
|
142
|
-
it("keeps the baked fallback of a region that produced no HTML", () => {
|
|
143
|
-
const shell = `${suspense("a", "fb-a")}|${suspense("b", "fb-b")}`;
|
|
144
|
-
expect(splice(shell, { b: "B" })).toBe(`${suspense("a", "fb-a")}|${suspense("b", "B")}`);
|
|
145
|
-
});
|
|
146
|
-
it("descends into a suspense fallback to fill nested regions", () => {
|
|
147
|
-
const shell = suspense("outer", `shell ${region("inner", "baked-inner")} end`);
|
|
148
|
-
expect(splice(shell, { inner: "NEW" })).toBe(suspense("outer", `shell ${region("inner", "NEW")} end`));
|
|
149
|
-
});
|
|
150
|
-
it("lets the outer region win over nested ones when both have HTML", () => {
|
|
151
|
-
const shell = suspense("outer", region("inner", "baked"));
|
|
152
|
-
expect(splice(shell, { outer: "OUT", inner: "IN" })).toBe(suspense("outer", "OUT"));
|
|
153
|
-
});
|
|
154
|
-
it("leaves a standalone runtime region empty when it produced no HTML (same as the Go host)", () => {
|
|
155
|
-
const shell = `a${region("r", suspense("s", "baked"))}b`;
|
|
156
|
-
expect(splice(shell, { s: "IGNORED" })).toBe(`a${region("r")}b`);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
const SECRET = "krate-test-flash-secret-0123456789";
|
|
160
|
-
// The same vector is pinned in internal/build/serve_flash_test.go: what the Go
|
|
161
|
-
// host signs verifies here, and what is signed here verifies there.
|
|
162
|
-
const VECTOR_PAYLOAD = "eyJhIjoiYWN0MSIsInIiOnsiZXJyb3IiOiJOYW1lIGlzIHJlcXVpcmVkIn19";
|
|
163
|
-
const VECTOR_COOKIE = VECTOR_PAYLOAD + ".zLQd73rhp1HN8J0vNvSeLqvlCqquygUX-mXuuN24dlY";
|
|
164
|
-
async function flashCookie(payload, secret = SECRET) {
|
|
165
|
-
const b64 = Buffer.from(JSON.stringify(payload), "utf-8").toString("base64url");
|
|
166
|
-
return secret === null ? b64 : signFlash(secret, b64);
|
|
167
|
-
}
|
|
168
|
-
describe("requestFlash", () => {
|
|
169
|
-
const payload = { a: "act1", r: { error: "Name is required" } };
|
|
170
|
-
it("reads the signed flash cookie among other cookies", async () => {
|
|
171
|
-
const got = await requestFlash(`theme=dark; krate_flash=${await flashCookie(payload)}; x=1`, SECRET);
|
|
172
|
-
expect(got?.payload).toEqual(payload);
|
|
173
|
-
});
|
|
174
|
-
it("accepts a quoted value and returns the payload part as raw", async () => {
|
|
175
|
-
const got = await requestFlash(`krate_flash="${await flashCookie(payload)}"`, SECRET);
|
|
176
|
-
expect(got?.payload).toEqual(payload);
|
|
177
|
-
expect(got?.raw).toBe(VECTOR_PAYLOAD);
|
|
178
|
-
});
|
|
179
|
-
it("signs and verifies the same bytes as the Go host", async () => {
|
|
180
|
-
expect(await signFlash(SECRET, VECTOR_PAYLOAD)).toBe(VECTOR_COOKIE);
|
|
181
|
-
expect(await verifyFlash(SECRET, VECTOR_COOKIE)).toBe(VECTOR_PAYLOAD);
|
|
182
|
-
expect(flashPayloadOf("act1", { error: "Name is required" })).toBe(VECTOR_PAYLOAD);
|
|
183
|
-
});
|
|
184
|
-
it("never trusts an unsigned, forged or foreign-signed cookie", async () => {
|
|
185
|
-
const unsigned = await flashCookie(payload, null);
|
|
186
|
-
expect(await requestFlash(`krate_flash=${unsigned}`, SECRET)).toBeNull();
|
|
187
|
-
expect(await requestFlash(`krate_flash=${unsigned}.`, SECRET)).toBeNull();
|
|
188
|
-
expect(await requestFlash(`krate_flash=${unsigned}.deadbeef`, SECRET)).toBeNull();
|
|
189
|
-
expect(await requestFlash(`krate_flash=${await flashCookie(payload, "another-secret-another-secret")}`, SECRET)).toBeNull();
|
|
190
|
-
// A valid signature does not carry over to a different payload.
|
|
191
|
-
const sig = VECTOR_COOKIE.slice(VECTOR_COOKIE.indexOf(".") + 1);
|
|
192
|
-
const other = Buffer.from(JSON.stringify({ a: "act1", r: { error: "Injected notice" } })).toString("base64url");
|
|
193
|
-
expect(await requestFlash(`krate_flash=${other}.${sig}`, SECRET)).toBeNull();
|
|
194
|
-
// Without a secret nothing is trusted, signed or not.
|
|
195
|
-
expect(await requestFlash(`krate_flash=${VECTOR_COOKIE}`, null)).toBeNull();
|
|
196
|
-
expect(await requestFlash(`krate_flash=${VECTOR_COOKIE}`, "")).toBeNull();
|
|
197
|
-
});
|
|
198
|
-
it("does not match a cookie that merely ends with the name", async () => {
|
|
199
|
-
expect(await requestFlash(`other_krate_flash=${await flashCookie(payload)}`, SECRET)).toBeNull();
|
|
200
|
-
});
|
|
201
|
-
it("returns null for no header, an empty value, bad base64, bad JSON or no action id", async () => {
|
|
202
|
-
expect(await requestFlash(null, SECRET)).toBeNull();
|
|
203
|
-
expect(await requestFlash("krate_flash=", SECRET)).toBeNull();
|
|
204
|
-
expect(await requestFlash("krate_flash=%%%", SECRET)).toBeNull();
|
|
205
|
-
expect(await requestFlash(`krate_flash=${await signFlash(SECRET, Buffer.from("not json").toString("base64url"))}`, SECRET)).toBeNull();
|
|
206
|
-
expect(await requestFlash(`krate_flash=${await flashCookie({ a: "", r: "x" })}`, SECRET)).toBeNull();
|
|
207
|
-
expect(await requestFlash(`krate_flash=${await flashCookie({ r: "x" })}`, SECRET)).toBeNull();
|
|
208
|
-
});
|
|
209
|
-
it("decodes multi-byte text", async () => {
|
|
210
|
-
const got = await requestFlash(`krate_flash=${await flashCookie({ a: "a", r: "café €" })}`, SECRET);
|
|
211
|
-
expect(got?.payload.r).toBe("café €");
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
describe("flashMessages", () => {
|
|
215
|
-
it("collects strings in a stable order (sorted keys, array order), trimmed", () => {
|
|
216
|
-
expect(flashMessages({ b: " second ", a: "first", c: ["x", "", " ", "y"], n: 5, ok: true })).toEqual(["first", "second", "x", "y"]);
|
|
217
|
-
});
|
|
218
|
-
it("stops descending after a few levels", () => {
|
|
219
|
-
expect(flashMessages({ a: { b: { c: { d: "too deep" } } } })).toEqual([]);
|
|
220
|
-
expect(flashMessages({ a: { b: { c: "kept" } } })).toEqual(["kept"]);
|
|
221
|
-
});
|
|
222
|
-
it("handles a bare string and empty values", () => {
|
|
223
|
-
expect(flashMessages("hi")).toEqual(["hi"]);
|
|
224
|
-
expect(flashMessages(null)).toEqual([]);
|
|
225
|
-
expect(flashMessages(undefined)).toEqual([]);
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
describe("injectFlashNotice", () => {
|
|
229
|
-
const page = `<body><form method="post" action="/__krate/action/save"><input name="n"></form><form action="/__krate/action/other"></form></body>`;
|
|
230
|
-
it("inserts the notice right after the opening tag of the form that produced it", () => {
|
|
231
|
-
const out = injectFlashNotice(page, "RAW", { a: "save", r: { error: "Bad <name>" } });
|
|
232
|
-
expect(out).not.toBeNull();
|
|
233
|
-
const first = out.indexOf("krate-flash");
|
|
234
|
-
expect(first).toBeGreaterThan(out.indexOf('action="/__krate/action/save"'));
|
|
235
|
-
expect(first).toBeLessThan(out.indexOf('<input name="n">'));
|
|
236
|
-
// the other form is untouched
|
|
237
|
-
expect(out.match(/krate-flash"/g)).toHaveLength(1);
|
|
238
|
-
});
|
|
239
|
-
it("escapes the message, the action id and the cookie value", () => {
|
|
240
|
-
const out = injectFlashNotice(`<form action="/__krate/action/a"b"></form>`, 'v"><img src=x onerror=alert(1)>', { a: 'a"b', r: "<script>alert(1)</script>" });
|
|
241
|
-
expect(out).not.toContain("<script>");
|
|
242
|
-
expect(out).not.toContain("<img src=x");
|
|
243
|
-
expect(out).toContain("<script>");
|
|
244
|
-
expect(out).toContain('role="alert"');
|
|
245
|
-
});
|
|
246
|
-
it("matches an action id that contains regex characters literally", () => {
|
|
247
|
-
const html = `<form action="/__krate/action/a.b+c"></form><form action="/__krate/action/aXb+c"></form>`;
|
|
248
|
-
const out = injectFlashNotice(html, "v", { a: "a.b+c", r: "msg" });
|
|
249
|
-
expect(out.indexOf("krate-flash")).toBeLessThan(out.indexOf("aXb+c"));
|
|
250
|
-
expect(out.match(/krate-flash"/g)).toHaveLength(1);
|
|
251
|
-
});
|
|
252
|
-
it("returns null when there is nothing to show or no matching form", () => {
|
|
253
|
-
expect(injectFlashNotice(page, "v", { a: "save", r: 7 })).toBeNull();
|
|
254
|
-
expect(injectFlashNotice(page, "v", { a: "missing", r: "msg" })).toBeNull();
|
|
255
|
-
});
|
|
256
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { applyDynamicRouteParams, compareRouteSpecificity, compileMatcher, goRedirect, headerSourceMatches, isHiddenPath, isInternalOutputPath, matchRedirect, matchRoutePattern, rewriteDestination, } from "./handler-routing.js";
|
|
3
|
-
// The vectors below are copied from the Go host's tests (internal/build
|
|
4
|
-
// serve_test.go, serve_security_test.go, routematch tests) so the portable
|
|
5
|
-
// handler decides like `krate start`.
|
|
6
|
-
describe("redirect matching", () => {
|
|
7
|
-
const cases = [
|
|
8
|
-
["/old", "/old", true],
|
|
9
|
-
["/old", "/old/", false],
|
|
10
|
-
["/old/page", "/old", false],
|
|
11
|
-
["/old/page", "/old/*", true],
|
|
12
|
-
["/old", "/old/*", true],
|
|
13
|
-
["/oldpage", "/old/*", false],
|
|
14
|
-
["/old/", "/old/*", true],
|
|
15
|
-
["/old//double", "/old/*", true],
|
|
16
|
-
["/a", "/b", false],
|
|
17
|
-
];
|
|
18
|
-
for (const [path, source, want] of cases) {
|
|
19
|
-
it(`${path} ~ ${source}`, () => expect(matchRedirect(path, source)).toBe(want));
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
describe("rewriteDestination", () => {
|
|
23
|
-
const cases = [
|
|
24
|
-
["/legacy//evil.com", "/legacy/*", "/:splat", "/evil.com"],
|
|
25
|
-
["/legacy/\\evil.com", "/legacy/*", "/:splat", "/%5Cevil.com"],
|
|
26
|
-
["/u/\\\\evil.com", "/u/:name", "/:name", "/%5C%5Cevil.com"],
|
|
27
|
-
["/legacy/https://evil.com", "/legacy/*", "/:splat", "/https://evil.com"],
|
|
28
|
-
["/legacy/a\nb", "/legacy/*", "/x/:splat", "/x/a%0Ab"],
|
|
29
|
-
["/old/\\evil.com", "/old/*", "/new", "/new/%5Cevil.com"],
|
|
30
|
-
["/go/x", "/go/*", "https://example.com/:splat", "https://example.com/x"],
|
|
31
|
-
["/go/x", "/go/*", "//cdn.example.com/:splat", "//cdn.example.com/x"],
|
|
32
|
-
["/legacy/a b", "/legacy/*", "/new/:splat", "/new/a%20b"],
|
|
33
|
-
["/docs/a/b", "/docs/:path*", "/documentation/:path*", "/documentation/a/b"],
|
|
34
|
-
["/docs", "/docs/:path*", "/documentation/:path*", "/documentation/"],
|
|
35
|
-
["/docs/a", "/docs/:slug?", "/documentation/:slug?", "/documentation/a"],
|
|
36
|
-
["/p/7", "/p/:id", "/item/:id?ref=1", "/item/7?ref=1"],
|
|
37
|
-
["/u/bob", "/u/:name", "/:name", "/bob"],
|
|
38
|
-
["/old", "/old", "/new", "/new"],
|
|
39
|
-
];
|
|
40
|
-
for (const [path, source, dest, want] of cases) {
|
|
41
|
-
it(`${path} ${source} -> ${dest}`, () => expect(rewriteDestination(path, source, dest)).toBe(want));
|
|
42
|
-
}
|
|
43
|
-
it("rejects non-matching optional/plus forms", () => {
|
|
44
|
-
expect(matchRedirect("/docs", "/docs/:path+")).toBe(false);
|
|
45
|
-
expect(matchRedirect("/docs/a/b", "/docs/:slug?")).toBe(false);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
describe("route patterns", () => {
|
|
49
|
-
it("matches params and catch-alls", () => {
|
|
50
|
-
expect(matchRoutePattern("/blog/x", "blog/[slug]")).toEqual({ slug: "x" });
|
|
51
|
-
expect(matchRoutePattern("/docs/a/b", "docs/[...rest]")).toEqual({ rest: "a/b" });
|
|
52
|
-
expect(matchRoutePattern("/docs", "docs/[...rest]")).toBeNull();
|
|
53
|
-
expect(matchRoutePattern("/shop", "shop/[[...filter]]")).toEqual({ filter: "" });
|
|
54
|
-
expect(matchRoutePattern("/blog", "blog/[slug]")).toBeNull();
|
|
55
|
-
});
|
|
56
|
-
it("orders by specificity: static > param > catch-all > optional catch-all", () => {
|
|
57
|
-
const routes = ["/a/[[...x]]", "/a/[...x]", "/a/[x]", "/a/b"];
|
|
58
|
-
expect([...routes].sort(compareRouteSpecificity)).toEqual(["/a/b", "/a/[x]", "/a/[...x]", "/a/[[...x]]"]);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
describe("paths and headers", () => {
|
|
62
|
-
it("hides internal and dot paths", () => {
|
|
63
|
-
for (const p of ["/api-node/hello.js", "/server-manifest.json", "/%61pi-node/x.js", "/.krate/x", "/a/../.krate/x"]) {
|
|
64
|
-
expect(isInternalOutputPath(p)).toBe(true);
|
|
65
|
-
}
|
|
66
|
-
for (const p of ["/manifest.json", "/api/hello", "/api-nodes/x"])
|
|
67
|
-
expect(isInternalOutputPath(p)).toBe(false);
|
|
68
|
-
expect(isHiddenPath("/.env")).toBe(true);
|
|
69
|
-
expect(isHiddenPath("/.well-known/acme")).toBe(false);
|
|
70
|
-
expect(isHiddenPath("/a/%2eenv")).toBe(true);
|
|
71
|
-
});
|
|
72
|
-
it("matches header sources", () => {
|
|
73
|
-
expect(headerSourceMatches("/about", "/about")).toBe(true);
|
|
74
|
-
expect(headerSourceMatches("/assets/*", "/assets/a/b.js")).toBe(true);
|
|
75
|
-
expect(headerSourceMatches("/a/*/c", "/a/b/c")).toBe(true);
|
|
76
|
-
expect(headerSourceMatches("/a/*/c", "/a/b/d/c")).toBe(false);
|
|
77
|
-
});
|
|
78
|
-
it("compiles the middleware matcher like the Go runtimes", () => {
|
|
79
|
-
expect(compileMatcher("/api/:path*").test("/api/x/y")).toBe(true);
|
|
80
|
-
expect(compileMatcher("/api/:path*").test("/other")).toBe(false);
|
|
81
|
-
expect(compileMatcher("/admin").test("/admin/")).toBe(true);
|
|
82
|
-
expect(() => compileMatcher("nope")).toThrow(/start with/);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
describe("templates and redirects", () => {
|
|
86
|
-
it("substitutes sentinels and injects params", () => {
|
|
87
|
-
const html = applyDynamicRouteParams("<head></head><p>__KRATE_PARAM_slug__</p>", { slug: "a<b" });
|
|
88
|
-
expect(html).toContain("<p>a<b</p>");
|
|
89
|
-
expect(html).toContain('<script>window.__KRATE_PARAMS__={"slug":"a\\u003cb"}</script></head>');
|
|
90
|
-
});
|
|
91
|
-
it("builds http.Redirect responses", async () => {
|
|
92
|
-
const r = goRedirect("/about", "/old", "GET", 301);
|
|
93
|
-
expect(r.status).toBe(301);
|
|
94
|
-
expect(r.headers.get("location")).toBe("/about");
|
|
95
|
-
expect(await r.text()).toBe('<a href="/about">Moved Permanently</a>.\n\n');
|
|
96
|
-
expect(await goRedirect("rel", "/a/b", "GET", 302).headers.get("location")).toBe("/a/rel");
|
|
97
|
-
expect(goRedirect("/x", "/y", "HEAD", 302).headers.get("content-type")).toContain("text/html");
|
|
98
|
-
});
|
|
99
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { fillServerError, lookupSegmentPage, newServerError, SEGMENT_KIND_OF_STATUS } from "./handler-segments.js";
|
|
4
|
-
import { createKrateHandler } from "./server-handler.js";
|
|
5
|
-
import { jsx } from "./server.js";
|
|
6
|
-
describe("lookupSegmentPage (port of build/segments.go)", () => {
|
|
7
|
-
it("URL walk", () => {
|
|
8
|
-
const entries = [
|
|
9
|
-
{ key: "", dir: "", notFound: "404.html", error: "500.html" },
|
|
10
|
-
{ key: "blog", dir: "blog", notFound: "blog/404.html" },
|
|
11
|
-
{ key: "blog/new", dir: "blog/new", notFound: "blog/new/404.html" },
|
|
12
|
-
{ key: "blog/[slug]", dir: "blog/[slug]", notFound: "blog/[slug]/404.html" },
|
|
13
|
-
{ key: "docs/[...rest]", dir: "docs/[...rest]", notFound: "docs/[...rest]/404.html" },
|
|
14
|
-
{ key: "(shop)", dir: "", group: true, notFound: "(shop)/404.html" },
|
|
15
|
-
{ key: "(shop)/cart", dir: "cart", group: true, notFound: "(shop)/cart/404.html" },
|
|
16
|
-
];
|
|
17
|
-
const cases = [
|
|
18
|
-
["/", "notFound", "404.html"],
|
|
19
|
-
["/nope", "notFound", "404.html"],
|
|
20
|
-
["/blog/", "notFound", "blog/404.html"],
|
|
21
|
-
["/blog", "notFound", "blog/404.html"],
|
|
22
|
-
["/blog/new/x/y", "notFound", "blog/new/404.html"],
|
|
23
|
-
["/blog/hello", "notFound", "blog/[slug]/404.html", "hello"],
|
|
24
|
-
["/blog/hello/deeper/still", "notFound", "blog/[slug]/404.html", "hello"],
|
|
25
|
-
["/docs", "notFound", "404.html"],
|
|
26
|
-
["/docs/a/b/c", "notFound", "docs/[...rest]/404.html"],
|
|
27
|
-
["/cart/x", "notFound", "404.html"],
|
|
28
|
-
["/blog/x", "error", "500.html"],
|
|
29
|
-
];
|
|
30
|
-
for (const [path, kind, want, slug] of cases) {
|
|
31
|
-
const m = lookupSegmentPage(entries, kind, path, false, "");
|
|
32
|
-
expect(m && m.file, `${kind} ${path}`).toBe(want);
|
|
33
|
-
if (slug)
|
|
34
|
-
expect(m.params.slug).toBe(slug);
|
|
35
|
-
}
|
|
36
|
-
expect(lookupSegmentPage([], "notFound", "/x", false, "")).toBeNull();
|
|
37
|
-
expect(lookupSegmentPage([{ key: "blog", dir: "blog", notFound: "blog/404.html" }], "notFound", "/x", false, "")).toBeNull();
|
|
38
|
-
});
|
|
39
|
-
it("finds forbidden and unauthorized pages like not-found pages", () => {
|
|
40
|
-
const entries = [
|
|
41
|
-
{ key: "", dir: "", forbidden: "403.html" },
|
|
42
|
-
{ key: "admin", dir: "admin", forbidden: "admin/403.html", unauthorized: "admin/401.html" },
|
|
43
|
-
];
|
|
44
|
-
expect(lookupSegmentPage(entries, "forbidden", "/x", true, "admin/users")?.file).toBe("admin/403.html");
|
|
45
|
-
expect(lookupSegmentPage(entries, "forbidden", "/x", true, "blog")?.file).toBe("403.html");
|
|
46
|
-
expect(lookupSegmentPage(entries, "unauthorized", "/x", true, "admin")?.file).toBe("admin/401.html");
|
|
47
|
-
expect(lookupSegmentPage(entries, "unauthorized", "/x", true, "blog")).toBeNull();
|
|
48
|
-
expect(lookupSegmentPage(entries, "forbidden", "/admin/zzz", false, "")?.file).toBe("admin/403.html");
|
|
49
|
-
expect(SEGMENT_KIND_OF_STATUS[401]).toBe("unauthorized");
|
|
50
|
-
expect(SEGMENT_KIND_OF_STATUS[403]).toBe("forbidden");
|
|
51
|
-
});
|
|
52
|
-
it("route walk", () => {
|
|
53
|
-
const entries = [
|
|
54
|
-
{ key: "", dir: "", notFound: "404.html" },
|
|
55
|
-
{ key: "(shop)", dir: "", group: true, notFound: "(shop)/404.html", error: "(shop)/500.html" },
|
|
56
|
-
{ key: "(shop)/cart", dir: "cart", group: true, notFound: "(shop)/cart/404.html" },
|
|
57
|
-
{ key: "blog", dir: "blog", error: "blog/500.html" },
|
|
58
|
-
];
|
|
59
|
-
const cases = [
|
|
60
|
-
["", "notFound", "404.html"],
|
|
61
|
-
["(shop)", "notFound", "(shop)/404.html"],
|
|
62
|
-
["(shop)/cart", "notFound", "(shop)/cart/404.html"],
|
|
63
|
-
["(shop)/cart/[id]", "notFound", "(shop)/cart/404.html"],
|
|
64
|
-
["(shop)/cart", "error", "(shop)/500.html"],
|
|
65
|
-
["blog", "notFound", "404.html"],
|
|
66
|
-
["blog/[slug]", "error", "blog/500.html"],
|
|
67
|
-
["other", "error", ""],
|
|
68
|
-
];
|
|
69
|
-
for (const [segDir, kind, want] of cases) {
|
|
70
|
-
const m = lookupSegmentPage(entries, kind, "/ignored", true, segDir);
|
|
71
|
-
expect(m ? m.file : "", `${kind} ${segDir}`).toBe(want);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
describe("error prop", () => {
|
|
76
|
-
it("is sanitized in production and escaped into the page", () => {
|
|
77
|
-
const e = newServerError("secret <b>", "at x", "abc1234567", false);
|
|
78
|
-
expect(e).toEqual({ name: "Error", message: "Internal Server Error", digest: "abc1234567" });
|
|
79
|
-
const dev = newServerError("a<b>", "at y", "d", true);
|
|
80
|
-
const out = fillServerError("<html><head></head><body>__KRATE_ERR_MESSAGE__|__KRATE_ERR_DIGEST__|__KRATE_ERR_STACK__</body></html>", dev);
|
|
81
|
-
expect(out).toContain("a<b>|d|at y");
|
|
82
|
-
expect(out).toContain('<script type="application/json" id="__krate_error">{"name":"Error","message":"a\\u003cb\\u003e","digest":"d","stack":"at y"}</script></head>');
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
const config = {
|
|
86
|
-
basePath: "",
|
|
87
|
-
trustProxy: true,
|
|
88
|
-
maxBodyBytes: 1 << 20,
|
|
89
|
-
maxAPIBodyBytes: 1 << 20,
|
|
90
|
-
redirects: [],
|
|
91
|
-
rewrites: [],
|
|
92
|
-
headers: [],
|
|
93
|
-
security: { nosniff: true, frameOptions: "", referrerPolicy: "", permissionsPolicy: "", hsts: "", csp: "" },
|
|
94
|
-
cors: { enabled: false, origins: [], methods: [], headers: [], credentials: false, maxAge: 0 },
|
|
95
|
-
ssr: { timeout: 2000, maxCacheSize: 16, ppr: false, pprRevalidate: 60 },
|
|
96
|
-
};
|
|
97
|
-
const shell = (id) => `<html><head><title>t</title></head><body><!--suspense:page-->baked ${id}<!--/suspense:page--></body></html>`;
|
|
98
|
-
function segData() {
|
|
99
|
-
return {
|
|
100
|
-
version: 1,
|
|
101
|
-
config,
|
|
102
|
-
pages: [
|
|
103
|
-
{ route: "/blog/boom", mode: "ssr", bundlePath: "boom", segDir: "blog" },
|
|
104
|
-
{ route: "/blog/gone", mode: "ssr", bundlePath: "gone", segDir: "blog" },
|
|
105
|
-
{ route: "/blog/[slug]", mode: "isr", revalidate: 30, bundlePath: "slug", segDir: "blog" },
|
|
106
|
-
{ route: "/boom", mode: "ssr", bundlePath: "boom", segDir: "" },
|
|
107
|
-
],
|
|
108
|
-
regions: {},
|
|
109
|
-
allPages: [{ route: "/blog/[slug]", mode: "isr", segDir: "blog" }, { route: "/blog/closed/[id]", mode: "ssg", segDir: "blog" }],
|
|
110
|
-
shells: { "/blog/boom": shell("a"), "/blog/gone": shell("b"), "/blog/[slug]": shell("c"), "/boom": shell("d") },
|
|
111
|
-
templates: [{ pattern: "blog/closed/[id]", html: "<html><head></head><body>t</body></html>" }],
|
|
112
|
-
staticOnlyRoutes: ["/blog/closed/[id]"],
|
|
113
|
-
hashedAssets: [],
|
|
114
|
-
html404: "<h1>global 404</h1>",
|
|
115
|
-
html500: "",
|
|
116
|
-
segments: [
|
|
117
|
-
{ key: "", dir: "", notFound: "404.html", error: "500.html" },
|
|
118
|
-
{ key: "blog", dir: "blog", notFound: "blog/404.html", error: "blog/500.html" },
|
|
119
|
-
{ key: "docs/[...rest]", dir: "docs/[...rest]", notFound: "docs/[...rest]/404.html" },
|
|
120
|
-
],
|
|
121
|
-
segmentPages: {
|
|
122
|
-
"404.html": "<html><head></head><body>root 404</body></html>",
|
|
123
|
-
"500.html": "<html><head></head><body>root 500 __KRATE_ERR_DIGEST__</body></html>",
|
|
124
|
-
"blog/404.html": "<html><head></head><body>blog 404</body></html>",
|
|
125
|
-
"blog/500.html": "<html><head></head><body>blog 500 __KRATE_ERR_MESSAGE__ __KRATE_ERR_DIGEST__ __KRATE_ERR_STACK__</body></html>",
|
|
126
|
-
"docs/[...rest]/404.html": "<html><head></head><body>docs 404 __KRATE_PARAM_rest__</body></html>",
|
|
127
|
-
},
|
|
128
|
-
apiRoutes: [],
|
|
129
|
-
actions: [],
|
|
130
|
-
hasMiddleware: false,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
let slugRenders = 0;
|
|
134
|
-
const mods = {
|
|
135
|
-
pages: {
|
|
136
|
-
boom: { default: () => { throw new Error("secret <boom>"); } },
|
|
137
|
-
gone: { default: async () => { const { notFound } = await import("./server.js"); notFound(); return jsx("p", {}); } },
|
|
138
|
-
slug: { default: () => jsx("main", { children: "slug " + ++slugRenders }) },
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
describe("segment pages in the handler", () => {
|
|
142
|
-
const handler = createKrateHandler({ target: "node", data: segData(), modules: mods });
|
|
143
|
-
const get = (p) => handler(new Request("http://localhost" + p, { headers: { accept: "text/html" } }));
|
|
144
|
-
const errors = console.error;
|
|
145
|
-
const quiet = () => { console.error = () => { }; };
|
|
146
|
-
const loud = () => { console.error = errors; };
|
|
147
|
-
it("unmatched URLs use the nearest segment 404", async () => {
|
|
148
|
-
const r = await get("/blog/nothing/at/all");
|
|
149
|
-
expect(r.status).toBe(404);
|
|
150
|
-
expect(r.headers.get("cache-control")).toBe("no-store");
|
|
151
|
-
expect(await r.text()).toContain("blog 404");
|
|
152
|
-
expect(await (await get("/zzz")).text()).toContain("root 404");
|
|
153
|
-
const d = await get("/docs/a/b");
|
|
154
|
-
expect(d.status).toBe(404);
|
|
155
|
-
expect(await d.text()).toContain("docs 404 a/b");
|
|
156
|
-
});
|
|
157
|
-
it("a closed dynamic route resolves through its directory", async () => {
|
|
158
|
-
const r = await get("/blog/closed/42/");
|
|
159
|
-
expect(r.status).toBe(404);
|
|
160
|
-
expect(await r.text()).toContain("blog 404");
|
|
161
|
-
});
|
|
162
|
-
it("notFound() answers the route's nearest page and is not cached", async () => {
|
|
163
|
-
const r = await get("/blog/gone/");
|
|
164
|
-
expect(r.status).toBe(404);
|
|
165
|
-
expect(r.headers.get("cache-control")).toBe("no-store");
|
|
166
|
-
expect(await r.text()).toContain("blog 404");
|
|
167
|
-
});
|
|
168
|
-
it("a failed render answers the segment error page with the digest", async () => {
|
|
169
|
-
quiet();
|
|
170
|
-
try {
|
|
171
|
-
const r = await get("/blog/boom/");
|
|
172
|
-
expect(r.status).toBe(500);
|
|
173
|
-
expect(r.headers.get("x-krate-error")).toBe("render");
|
|
174
|
-
expect(r.headers.get("cache-control")).toBe("no-store");
|
|
175
|
-
const body = await r.text();
|
|
176
|
-
expect(body).toContain("blog 500 Internal Server Error ");
|
|
177
|
-
expect(body).not.toContain("secret");
|
|
178
|
-
const m = /"digest":"([0-9a-f]{10})"/.exec(body);
|
|
179
|
-
expect(m).not.toBeNull();
|
|
180
|
-
expect(body).toContain('id="__krate_error"');
|
|
181
|
-
const root = await (await get("/boom/")).text();
|
|
182
|
-
expect(root).toContain("root 500 ");
|
|
183
|
-
}
|
|
184
|
-
finally {
|
|
185
|
-
loud();
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
it("ISR serves from cache, but never stores failed or not-found renders", async () => {
|
|
189
|
-
const a = await get("/blog/x/");
|
|
190
|
-
expect(a.status).toBe(200);
|
|
191
|
-
expect(a.headers.get("x-krate-cache")).toBe("MISS");
|
|
192
|
-
expect((await get("/blog/x/")).headers.get("x-krate-cache")).toBe("HIT");
|
|
193
|
-
expect(slugRenders).toBe(1);
|
|
194
|
-
});
|
|
195
|
-
it("an ISR page that throws is rendered again, not cached", async () => {
|
|
196
|
-
quiet();
|
|
197
|
-
try {
|
|
198
|
-
let n = 0;
|
|
199
|
-
const d = segData();
|
|
200
|
-
const h = createKrateHandler({
|
|
201
|
-
target: "node",
|
|
202
|
-
data: d,
|
|
203
|
-
modules: { pages: { ...mods.pages, slug: { default: () => { n++; throw new Error("x"); } } } },
|
|
204
|
-
});
|
|
205
|
-
for (let i = 0; i < 2; i++) {
|
|
206
|
-
const r = await h(new Request("http://localhost/blog/y/"));
|
|
207
|
-
expect(r.status).toBe(500);
|
|
208
|
-
expect(r.headers.get("cache-control")).toBe("no-store");
|
|
209
|
-
expect(await r.text()).toContain("blog 500");
|
|
210
|
-
}
|
|
211
|
-
expect(n).toBe(2);
|
|
212
|
-
}
|
|
213
|
-
finally {
|
|
214
|
-
loud();
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
});
|
package/dist/head-tags.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|