@krate/runtime 0.5.0-rc.4 → 0.5.0-rc.6
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/actions.js +10 -1
- package/dist/cache-scope.d.ts +6 -0
- package/dist/cache-scope.js +14 -1
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +148 -74
- package/dist/index.d.ts +1 -0
- package/dist/krate-core.js +1 -1
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +24 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/list.d.ts +14 -4
- package/dist/list.js +4 -5
- 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/react-compat.d.ts +26 -0
- package/dist/react-compat.js +292 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/router.js +9 -0
- package/dist/server-handler.js +89 -10
- package/dist/server.d.ts +57 -10
- package/dist/server.js +455 -94
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +8 -0
- package/package.json +19 -2
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -75
- package/dist/after.test.d.ts +0 -1
- package/dist/after.test.js +0 -58
- package/dist/builtin-tags.test.d.ts +0 -1
- package/dist/builtin-tags.test.js +0 -12
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -45
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -37
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -42
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -55
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -25
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -24
- package/dist/inline-action-token.test.d.ts +0 -1
- package/dist/inline-action-token.test.js +0 -31
- package/dist/jsx-svg-types.test.d.ts +0 -1
- package/dist/jsx-svg-types.test.js +0 -58
- package/dist/link-base-path.test.d.ts +0 -1
- package/dist/link-base-path.test.js +0 -28
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -138
- package/dist/platform.test.d.ts +0 -1
- package/dist/platform.test.js +0 -44
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -130
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -247
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -162
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -80
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/site-config.test.d.ts +0 -1
- package/dist/site-config.test.js +0 -30
- package/dist/slot.test.d.ts +0 -1
- package/dist/slot.test.js +0 -25
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -108
- package/dist/testing.test.d.ts +0 -1
- package/dist/testing.test.js +0 -73
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
//
|
|
3
|
-
// End-to-end test of the Node SSR sidecar. server-renderer.ts starts a server
|
|
4
|
-
// when it is loaded, so the test runs the built dist/server-renderer.js as the
|
|
5
|
-
// Go host does: with KRATE_* environment variables, a server manifest and page
|
|
6
|
-
// bundles on disk, and talks to it over HTTP. Skipped when the runtime has not
|
|
7
|
-
// been built (KRATE_REQUIRE_E2E=1 turns that into a failure, as in CI).
|
|
8
|
-
import { spawn } from "node:child_process";
|
|
9
|
-
import { createHash } from "node:crypto";
|
|
10
|
-
import fs from "node:fs";
|
|
11
|
-
import net from "node:net";
|
|
12
|
-
import os from "node:os";
|
|
13
|
-
import path from "node:path";
|
|
14
|
-
import { pathToFileURL } from "node:url";
|
|
15
|
-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
16
|
-
const here = path.dirname(new URL(import.meta.url).pathname);
|
|
17
|
-
const renderer = path.resolve(here, "..", "dist", "server-renderer.js");
|
|
18
|
-
const serverJs = path.resolve(here, "..", "dist", "server.js");
|
|
19
|
-
const built = fs.existsSync(renderer) && fs.existsSync(serverJs);
|
|
20
|
-
if (!built && process.env.KRATE_REQUIRE_E2E) {
|
|
21
|
-
throw new Error("dist/server-renderer.js is missing (KRATE_REQUIRE_E2E is set): build @krate/runtime first");
|
|
22
|
-
}
|
|
23
|
-
const SECRET = "test-secret";
|
|
24
|
-
let tmp = "";
|
|
25
|
-
let out = "";
|
|
26
|
-
let isrFile = "";
|
|
27
|
-
let child = null;
|
|
28
|
-
let port = 0;
|
|
29
|
-
let logs = "";
|
|
30
|
-
function freePort() {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
const s = net.createServer();
|
|
33
|
-
s.once("error", reject);
|
|
34
|
-
s.listen(0, "127.0.0.1", () => {
|
|
35
|
-
const p = s.address().port;
|
|
36
|
-
s.close(() => resolve(p));
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
function write(rel, data) {
|
|
41
|
-
const f = path.join(out, rel);
|
|
42
|
-
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
43
|
-
fs.writeFileSync(f, data);
|
|
44
|
-
}
|
|
45
|
-
const jsxImport = `import { jsx } from ${JSON.stringify(pathToFileURL(serverJs).href)};\n`;
|
|
46
|
-
function bundles() {
|
|
47
|
-
write("pages/ssr.mjs", jsxImport +
|
|
48
|
-
`export default (props) => jsx("main", { children: [jsx("title", { children: "T " + ((props.params || {}).id || "") }), jsx("p", { children: "q=" + ((props.query || {}).q || "") })] });\n`);
|
|
49
|
-
write("pages/isr.mjs", jsxImport + `let n = 0;\nexport default () => jsx("p", { children: "isr-render-" + (++n) });\n`);
|
|
50
|
-
write("pages/boom.mjs", jsxImport + `export default () => { const e = new Error("secret detail"); e.stack = "at x"; throw e; };\n`);
|
|
51
|
-
write("pages/redirect.mjs", jsxImport + `export default () => { throw Object.assign(new Error("r"), { __krateRedirect: "/login" }); };\n`);
|
|
52
|
-
write("pages/missing.mjs", jsxImport + `export default () => { throw Object.assign(new Error("NEXT_NOT_FOUND"), { __krateNotFound: true, digest: "NEXT_NOT_FOUND" }); };\n`);
|
|
53
|
-
write("pages/regions.mjs", jsxImport +
|
|
54
|
-
`export const Slow = async () => { await new Promise((ok) => setTimeout(ok, 250)); return jsx("i", { children: "slow" }); };\n` +
|
|
55
|
-
`export const Fast = async () => jsx("i", { children: "fast" });\n` +
|
|
56
|
-
`export const Bad = async () => { throw new Error("region failed"); };\n` +
|
|
57
|
-
`export default () => jsx("p", { children: "whole page" });\n`);
|
|
58
|
-
write("rt/price.js", `globalThis.__krate_render = (json) => { const p = typeof json === "string" ? JSON.parse(json) : json; return "<b>" + p.price + "/" + (p.params ? p.params.id : "-") + "</b>"; };\n`);
|
|
59
|
-
write("404.html", "<h1>custom not found</h1>");
|
|
60
|
-
write("500.html", "<h1>custom error</h1>");
|
|
61
|
-
const page = (route, mode, bundle, extra = {}) => ({ route, source: route + ".tsx", mode, bundlePath: "pages/" + bundle + ".mjs", ...extra });
|
|
62
|
-
write(".krate/server-manifest.json", JSON.stringify({
|
|
63
|
-
pages: [
|
|
64
|
-
page("/ssr/[id]", "ssr", "ssr"),
|
|
65
|
-
page("/isr", "isr", "isr", { revalidate: 300 }),
|
|
66
|
-
page("/boom", "ssr", "boom"),
|
|
67
|
-
page("/redirect", "ssr", "redirect"),
|
|
68
|
-
page("/missing", "ssr", "missing"),
|
|
69
|
-
page("/regions", "ssr", "regions", { serverComponents: true }),
|
|
70
|
-
],
|
|
71
|
-
regions: {
|
|
72
|
-
"/regions": [
|
|
73
|
-
{ id: "slow", serverExport: "Slow" },
|
|
74
|
-
{ id: "fast", serverExport: "Fast" },
|
|
75
|
-
{ id: "bad", serverExport: "Bad" },
|
|
76
|
-
{ id: "price", bundlePath: "rt/price.js", props: { price: 12 } },
|
|
77
|
-
],
|
|
78
|
-
},
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
async function start() {
|
|
82
|
-
port = await freePort();
|
|
83
|
-
logs = "";
|
|
84
|
-
child = spawn(process.execPath, [renderer], {
|
|
85
|
-
env: {
|
|
86
|
-
PATH: process.env.PATH,
|
|
87
|
-
KRATE_SSR_PORT: String(port),
|
|
88
|
-
KRATE_OUT_DIR: out,
|
|
89
|
-
KRATE_ROOT: tmp,
|
|
90
|
-
KRATE_INTERNAL_SECRET: SECRET,
|
|
91
|
-
KRATE_ISR_CACHE: isrFile,
|
|
92
|
-
},
|
|
93
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
94
|
-
});
|
|
95
|
-
child.stdout.on("data", (d) => (logs += d));
|
|
96
|
-
child.stderr.on("data", (d) => (logs += d));
|
|
97
|
-
const deadline = Date.now() + 15000;
|
|
98
|
-
for (;;) {
|
|
99
|
-
try {
|
|
100
|
-
const r = await fetch(`http://127.0.0.1:${port}/__krate/ssr/health`);
|
|
101
|
-
if (r.ok)
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
// not listening yet
|
|
106
|
-
}
|
|
107
|
-
if (child.exitCode !== null)
|
|
108
|
-
throw new Error("renderer exited early:\n" + logs);
|
|
109
|
-
if (Date.now() > deadline)
|
|
110
|
-
throw new Error("renderer did not start:\n" + logs);
|
|
111
|
-
await new Promise((ok) => setTimeout(ok, 50));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async function stop() {
|
|
115
|
-
const c = child;
|
|
116
|
-
child = null;
|
|
117
|
-
if (!c || c.exitCode !== null)
|
|
118
|
-
return;
|
|
119
|
-
await new Promise((ok) => {
|
|
120
|
-
c.once("exit", () => ok());
|
|
121
|
-
c.kill("SIGKILL");
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
const secret = { "x-krate-internal": SECRET };
|
|
125
|
-
const url = (p) => `http://127.0.0.1:${port}${p}`;
|
|
126
|
-
async function regions(body, headers = secret) {
|
|
127
|
-
const res = await fetch(url("/__krate/regions"), { method: "POST", headers, body: JSON.stringify(body) });
|
|
128
|
-
const text = await res.text();
|
|
129
|
-
return { res, frames: text.split("\n").filter(Boolean).map((l) => JSON.parse(l)) };
|
|
130
|
-
}
|
|
131
|
-
describe.skipIf(!built)("server-renderer sidecar", () => {
|
|
132
|
-
beforeAll(async () => {
|
|
133
|
-
tmp = fs.mkdtempSync(path.join(os.tmpdir(), "krate-ssr-"));
|
|
134
|
-
out = path.join(tmp, "dist");
|
|
135
|
-
isrFile = path.join(tmp, "isr-cache.json");
|
|
136
|
-
bundles();
|
|
137
|
-
await start();
|
|
138
|
-
}, 30000);
|
|
139
|
-
afterAll(async () => {
|
|
140
|
-
await stop();
|
|
141
|
-
fs.rmSync(tmp, { recursive: true, force: true });
|
|
142
|
-
});
|
|
143
|
-
describe("internal endpoints need the host's secret", () => {
|
|
144
|
-
it("refuses regions, revalidation and direct renders without it", async () => {
|
|
145
|
-
expect((await regions({ route: "/regions", url: "/regions" }, {})).res.status).toBe(403);
|
|
146
|
-
expect((await fetch(url("/__krate/ssr/cache-invalidate"), { method: "POST", body: "{}" })).status).toBe(403);
|
|
147
|
-
expect((await fetch(url("/__krate/ssr/cache-invalidate"), { method: "POST", body: "{}", headers: { "x-krate-internal": "wrong" } })).status).toBe(403);
|
|
148
|
-
expect((await fetch(url("/ssr/[id]"))).status).toBe(403);
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
describe("direct page requests", () => {
|
|
152
|
-
it("serves the build's 500 page for a failing render, without the error text", async () => {
|
|
153
|
-
const r = await fetch(url("/boom"), { headers: secret });
|
|
154
|
-
expect(r.status).toBe(500);
|
|
155
|
-
const body = await r.text();
|
|
156
|
-
expect(body).toBe("<h1>custom error</h1>");
|
|
157
|
-
expect(body).not.toContain("secret detail");
|
|
158
|
-
const digest = createHash("sha256").update("secret detail\nat x").digest("hex").slice(0, 10);
|
|
159
|
-
expect(logs).toContain(`digest ${digest}`);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
});
|
package/dist/server.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/server.test.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { clientBoundary, jsx, renderToString, renderToStringAsync } from './server';
|
|
3
|
-
describe('renderToString', () => {
|
|
4
|
-
it('renders nested elements and escapes text', () => {
|
|
5
|
-
const node = jsx('div', { class: 'x', children: [jsx('span', { children: 'a & b' })] });
|
|
6
|
-
expect(renderToString(node)).toBe('<div class="x"><span>a & b</span></div>');
|
|
7
|
-
});
|
|
8
|
-
it('drops event and internal props', () => {
|
|
9
|
-
const node = jsx('button', { onClick: () => undefined, children: 'x' });
|
|
10
|
-
expect(renderToString(node)).toBe('<button>x</button>');
|
|
11
|
-
});
|
|
12
|
-
it('blocks javascript: URLs and drops reserved props', () => {
|
|
13
|
-
const html = renderToString(jsx('a', { href: 'javascript:alert(1)', ref: {}, key: 'k', children: 'x' }));
|
|
14
|
-
expect(html).toContain('href="javascript:throw new Error(');
|
|
15
|
-
expect(html).not.toContain(' ref=');
|
|
16
|
-
expect(html).not.toContain(' key=');
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
describe('server actions', () => {
|
|
20
|
-
it('renders a formAction function as a POST submit endpoint', () => {
|
|
21
|
-
const action = Object.assign(() => undefined, {
|
|
22
|
-
actionUrl: '/__krate/action/a456',
|
|
23
|
-
});
|
|
24
|
-
const html = renderToString(jsx('button', { formAction: action, children: 'go' }));
|
|
25
|
-
expect(html).toContain('formaction="/__krate/action/a456"');
|
|
26
|
-
expect(html).toContain('formmethod="post"');
|
|
27
|
-
expect(html).toContain('data-krate-action');
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe('clientBoundary', () => {
|
|
31
|
-
// The render callback stands in for the Preact render module: it echoes the
|
|
32
|
-
// serialized props so the test sees exactly what travels to the client.
|
|
33
|
-
const render = (_c, props) => `<i>${JSON.stringify(props)}</i>`;
|
|
34
|
-
it('renders a stateless component as plain HTML without an island', async () => {
|
|
35
|
-
const Icon = clientBoundary('m1', 'Icon', '/c.js', function Icon() { return null; }, render, false);
|
|
36
|
-
const html = await renderToStringAsync(jsx(Icon, { size: 4 }));
|
|
37
|
-
expect(html).toBe('<i>{"size":4}</i>');
|
|
38
|
-
});
|
|
39
|
-
it('wraps a stateful component in a krate-island with its props', async () => {
|
|
40
|
-
const Box = clientBoundary('m2', 'Box', '/c.js', function Box() { return null; }, render);
|
|
41
|
-
const html = await renderToStringAsync(jsx(Box, { id: 'x' }));
|
|
42
|
-
expect(html).toContain('<krate-island');
|
|
43
|
-
expect(html).toContain('data-module="m2"');
|
|
44
|
-
expect(html).toContain('data-export="Box"');
|
|
45
|
-
});
|
|
46
|
-
it('sends children as a tree, not HTML, when asked to (real-React islands)', async () => {
|
|
47
|
-
const Box = clientBoundary('m7', 'Box', '/c.js', function Box() { return null; }, render, true, true);
|
|
48
|
-
const html = await renderToStringAsync(jsx(Box, { children: jsx('mesh', { position: [1, 2, 3] }) }));
|
|
49
|
-
expect(html).toContain('__krateTree');
|
|
50
|
-
expect(html).not.toContain('__krateChildren');
|
|
51
|
-
});
|
|
52
|
-
it('wraps the members of a namespace export under dotted names', async () => {
|
|
53
|
-
const ns = clientBoundary('m3', 'Dialog', '/c.js', { Root() { return null; }, helper() { return 1; } }, render);
|
|
54
|
-
expect(typeof ns.Root).toBe('function');
|
|
55
|
-
expect(ns.helper()).toBe(1);
|
|
56
|
-
const html = await renderToStringAsync(jsx(ns.Root, {}));
|
|
57
|
-
expect(html).toContain('data-export="Dialog.Root"');
|
|
58
|
-
});
|
|
59
|
-
it('keeps nested client components in the parent tree so they can share context', async () => {
|
|
60
|
-
const ns = clientBoundary('m4', 'Dialog', '/c.js', { Root() { return null; }, Trigger() { return null; } }, render);
|
|
61
|
-
const html = await renderToStringAsync(jsx(ns.Root, { children: [jsx('div', { children: jsx(ns.Trigger, { id: 't', children: 'Open' }) })] }));
|
|
62
|
-
const attr = html.match(/data-props="([^"]*)"/)[1].replace(/"/g, '"');
|
|
63
|
-
const data = JSON.parse(attr);
|
|
64
|
-
expect(data.__krateChildren).toBeUndefined();
|
|
65
|
-
expect(data.__krateTree).toEqual([
|
|
66
|
-
{ $: 'e', t: 'div', p: {}, c: [{ $: 'c', m: 'm4', e: 'Dialog.Trigger', s: '/c.js', p: { id: 't' }, c: ['Open'] }] },
|
|
67
|
-
]);
|
|
68
|
-
});
|
|
69
|
-
it('hydrates a stateless parent when a nested component has state', async () => {
|
|
70
|
-
const Wrap = clientBoundary('m5', 'Wrap', '/c.js', function Wrap() { return null; }, render, false);
|
|
71
|
-
const Inner = clientBoundary('m5', 'Inner', '/c.js', function Inner() { return null; }, render, true);
|
|
72
|
-
const html = await renderToStringAsync(jsx(Wrap, { children: [jsx(Inner, {})] }));
|
|
73
|
-
expect(html).toContain('<krate-island');
|
|
74
|
-
});
|
|
75
|
-
it('sends server-only children as HTML', async () => {
|
|
76
|
-
const Box = clientBoundary('m6', 'Box', '/c.js', function Box() { return null; }, render);
|
|
77
|
-
const html = await renderToStringAsync(jsx(Box, { children: jsx('b', { children: 'note' }) }));
|
|
78
|
-
expect(html).toContain('__krateChildren');
|
|
79
|
-
});
|
|
80
|
-
});
|
package/dist/sha256.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/sha256.test.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import { sha256Hex } from "./sha256.js";
|
|
4
|
-
describe("portable sha256", () => {
|
|
5
|
-
it("matches node:crypto across block boundaries and UTF-8", () => {
|
|
6
|
-
for (const s of ["", "abc", "a".repeat(55), "a".repeat(56), "a".repeat(63), "a".repeat(64), "a".repeat(1000), "héllo 世界 😀"]) {
|
|
7
|
-
expect(sha256Hex(s)).toBe(createHash("sha256").update(s).digest("hex"));
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
it("error digest fixed vector", () => {
|
|
11
|
-
expect(sha256Hex("boom\nat x").slice(0, 10)).toBe("8a2474a04d");
|
|
12
|
-
});
|
|
13
|
-
});
|
package/dist/signal.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/signal.test.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { createSignal, createEffect, createMemo, onCleanup, disposeAll, } from './signal';
|
|
3
|
-
const flush = () => new Promise((r) => setTimeout(r, 0));
|
|
4
|
-
describe('createSignal', () => {
|
|
5
|
-
beforeEach(() => disposeAll());
|
|
6
|
-
it('reads and writes values', () => {
|
|
7
|
-
const [n, setN] = createSignal(1);
|
|
8
|
-
expect(n()).toBe(1);
|
|
9
|
-
setN(2);
|
|
10
|
-
expect(n()).toBe(2);
|
|
11
|
-
setN((p) => p + 3);
|
|
12
|
-
expect(n()).toBe(5);
|
|
13
|
-
});
|
|
14
|
-
it('does not notify when the value is unchanged', async () => {
|
|
15
|
-
const [n, setN] = createSignal(1);
|
|
16
|
-
let runs = 0;
|
|
17
|
-
createEffect(() => {
|
|
18
|
-
n();
|
|
19
|
-
runs++;
|
|
20
|
-
});
|
|
21
|
-
await flush();
|
|
22
|
-
const before = runs;
|
|
23
|
-
setN(1);
|
|
24
|
-
await flush();
|
|
25
|
-
expect(runs).toBe(before);
|
|
26
|
-
});
|
|
27
|
-
it('runs dependent effects with the new value', async () => {
|
|
28
|
-
const [n, setN] = createSignal(0);
|
|
29
|
-
let seen = -1;
|
|
30
|
-
createEffect(() => {
|
|
31
|
-
seen = n();
|
|
32
|
-
});
|
|
33
|
-
setN(7);
|
|
34
|
-
await flush();
|
|
35
|
-
expect(seen).toBe(7);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
describe('createMemo', () => {
|
|
39
|
-
beforeEach(() => disposeAll());
|
|
40
|
-
it('caches until a dependency changes', async () => {
|
|
41
|
-
const [n, setN] = createSignal(2);
|
|
42
|
-
let computations = 0;
|
|
43
|
-
const doubled = createMemo(() => {
|
|
44
|
-
computations++;
|
|
45
|
-
return n() * 2;
|
|
46
|
-
});
|
|
47
|
-
await flush();
|
|
48
|
-
expect(doubled()).toBe(4);
|
|
49
|
-
expect(doubled()).toBe(4);
|
|
50
|
-
expect(computations).toBe(1);
|
|
51
|
-
setN(5);
|
|
52
|
-
await flush();
|
|
53
|
-
expect(doubled()).toBe(10);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
describe('onCleanup / disposeAll', () => {
|
|
57
|
-
it('runs cleanups when the world is disposed', async () => {
|
|
58
|
-
let cleaned = false;
|
|
59
|
-
createEffect(() => {
|
|
60
|
-
onCleanup(() => {
|
|
61
|
-
cleaned = true;
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
await flush();
|
|
65
|
-
disposeAll();
|
|
66
|
-
expect(cleaned).toBe(true);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/site-config.test.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import { basePath } from './base-path.js';
|
|
3
|
-
import { siteConfig } from './site-config.js';
|
|
4
|
-
const g = globalThis;
|
|
5
|
-
function withBlock(text) {
|
|
6
|
-
g.document = { getElementById: (id) => (id === '__krate_cfg__' && text !== null ? { textContent: text } : null) };
|
|
7
|
-
}
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
delete g.document;
|
|
10
|
-
delete g.__KRATE_CFG__;
|
|
11
|
-
});
|
|
12
|
-
describe('site config JSON block', () => {
|
|
13
|
-
it('reads the base path and flags from the head data block', () => {
|
|
14
|
-
withBlock('{"basePath":"/docs","viewTransitions":false}');
|
|
15
|
-
expect(siteConfig().viewTransitions).toBe(false);
|
|
16
|
-
expect(basePath()).toBe('/docs');
|
|
17
|
-
});
|
|
18
|
-
it('falls back to the defaults without a block or with a malformed one', () => {
|
|
19
|
-
withBlock(null);
|
|
20
|
-
expect(siteConfig()).toEqual({});
|
|
21
|
-
expect(basePath()).toBe('');
|
|
22
|
-
withBlock('{nope');
|
|
23
|
-
expect(siteConfig()).toEqual({});
|
|
24
|
-
});
|
|
25
|
-
it('prefers a config the server already set on globalThis', () => {
|
|
26
|
-
g.__KRATE_CFG__ = { basePath: '/app' };
|
|
27
|
-
withBlock('{"basePath":"/other"}');
|
|
28
|
-
expect(basePath()).toBe('/app');
|
|
29
|
-
});
|
|
30
|
-
});
|
package/dist/slot.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/slot.test.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { createSlot, createSlottable, Slot, Slottable } from './slot.js';
|
|
3
|
-
describe('react-slot shim', () => {
|
|
4
|
-
it('createSlot merges props and ref onto the single child', () => {
|
|
5
|
-
const SlotImpl = createSlot('Dialog');
|
|
6
|
-
const child = document.createElement('a');
|
|
7
|
-
child.setAttribute('class', 'base');
|
|
8
|
-
let refd = null;
|
|
9
|
-
const node = SlotImpl({ className: 'extra', 'data-x': '1', ref: (n) => (refd = n), children: child });
|
|
10
|
-
expect(node).toBe(child);
|
|
11
|
-
expect(child.getAttribute('class')).toBe('base extra');
|
|
12
|
-
expect(child.getAttribute('data-x')).toBe('1');
|
|
13
|
-
expect(refd).toBe(child);
|
|
14
|
-
const objRef = { current: null };
|
|
15
|
-
SlotImpl({ ref: objRef, children: [child] });
|
|
16
|
-
expect(objRef.current).toBe(child);
|
|
17
|
-
expect(SlotImpl.displayName).toBe('Dialog.Slot');
|
|
18
|
-
});
|
|
19
|
-
it('createSlottable returns a marker that renders its children', () => {
|
|
20
|
-
const Marker = createSlottable('Dialog');
|
|
21
|
-
expect(Marker({ children: 'x' })).toBe('x');
|
|
22
|
-
expect(typeof Slot).toBe('function');
|
|
23
|
-
expect(typeof Slottable).toBe('function');
|
|
24
|
-
});
|
|
25
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
-
import { createSSRCore } from "./ssr-core.js";
|
|
5
|
-
import { installRenderStore, markNoStore } from "./cache-scope.js";
|
|
6
|
-
import { forbidden, unauthorized } from "./request-context.js";
|
|
7
|
-
import { jsx } from "./server.js";
|
|
8
|
-
// The renderer installs the cache scope store once per process; so does this file.
|
|
9
|
-
installRenderStore(new AsyncLocalStorage());
|
|
10
|
-
let errors = [];
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
errors = [];
|
|
13
|
-
vi.spyOn(console, "error").mockImplementation((...a) => void errors.push(a));
|
|
14
|
-
vi.spyOn(console, "log").mockImplementation(() => { });
|
|
15
|
-
});
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
vi.restoreAllMocks();
|
|
18
|
-
vi.useRealTimers();
|
|
19
|
-
});
|
|
20
|
-
function make(s) {
|
|
21
|
-
const background = [];
|
|
22
|
-
const core = createSSRCore({
|
|
23
|
-
manifest: { pages: s.pages || [], regions: s.regions || {} },
|
|
24
|
-
loadPageModule: async (page) => {
|
|
25
|
-
const m = (s.modules || {})[page.bundlePath || page.source];
|
|
26
|
-
if (!m)
|
|
27
|
-
throw new Error("no module " + page.source);
|
|
28
|
-
return m;
|
|
29
|
-
},
|
|
30
|
-
loadRuntimeRenderer: (rel) => {
|
|
31
|
-
const r = (s.runtime || {})[rel];
|
|
32
|
-
if (!r)
|
|
33
|
-
throw new Error("no runtime bundle " + rel);
|
|
34
|
-
return r;
|
|
35
|
-
},
|
|
36
|
-
background: (p) => void background.push(p),
|
|
37
|
-
...s.opts,
|
|
38
|
-
});
|
|
39
|
-
return { core, settle: () => Promise.allSettled(background.splice(0)) };
|
|
40
|
-
}
|
|
41
|
-
const req = (route, extra = {}) => ({ route, url: route, method: "GET", headers: {}, ...extra });
|
|
42
|
-
const page = (route, mode = "ssr", extra = {}) => ({ route, source: route + ".tsx", mode, bundlePath: route, ...extra });
|
|
43
|
-
describe("renderPage", () => {
|
|
44
|
-
it("escapes text and attribute values coming from the request", async () => {
|
|
45
|
-
const { core } = make({
|
|
46
|
-
pages: [page("/e")],
|
|
47
|
-
modules: {
|
|
48
|
-
"/e": {
|
|
49
|
-
default: (props) => jsx("div", { title: props.query.t, children: [jsx("p", { children: props.query.t }), jsx("p", { children: props.params.id })] }),
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
const r = await core.renderPage(req("/e", { query: { t: `"><script>alert(1)</script>` }, params: { id: "<b>&" } }));
|
|
54
|
-
expect(r.status).toBe(200);
|
|
55
|
-
expect(r.html).not.toContain("<script>");
|
|
56
|
-
expect(r.html).not.toContain("<b>");
|
|
57
|
-
expect(r.html).toContain("<script>");
|
|
58
|
-
expect(r.html).toContain("title=");
|
|
59
|
-
expect(r.html).toContain("<b>&");
|
|
60
|
-
});
|
|
61
|
-
describe("failures", () => {
|
|
62
|
-
const boom = () => () => {
|
|
63
|
-
const e = new Error("boom");
|
|
64
|
-
e.stack = "at x";
|
|
65
|
-
throw e;
|
|
66
|
-
};
|
|
67
|
-
it("answers 500 with a short digest of the error, never the error text in the HTML", async () => {
|
|
68
|
-
const { core } = make({ pages: [page("/b")], modules: { "/b": { default: boom() } } });
|
|
69
|
-
const r = await core.renderPage(req("/b"));
|
|
70
|
-
expect(r.status).toBe(500);
|
|
71
|
-
expect(r.html).toBe("");
|
|
72
|
-
expect(r.error).toBe("boom");
|
|
73
|
-
expect(r.digest).toMatch(/^[0-9a-f]{10}$/);
|
|
74
|
-
expect(errors.flat().join(" ")).toContain(`digest ${r.digest}`);
|
|
75
|
-
});
|
|
76
|
-
it("maps forbidden() to 403 and unauthorized() to 401 and never caches them", async () => {
|
|
77
|
-
const { core } = make({
|
|
78
|
-
pages: [{ ...page("/fb"), mode: "isr", revalidate: 60 }, page("/un")],
|
|
79
|
-
modules: { "/fb": { default: () => forbidden() }, "/un": { default: () => unauthorized() } },
|
|
80
|
-
});
|
|
81
|
-
expect(await core.renderPage(req("/fb"))).toMatchObject({ status: 403, denied: 403, html: "" });
|
|
82
|
-
expect(await core.renderPage(req("/fb"))).toMatchObject({ status: 403, denied: 403 });
|
|
83
|
-
expect((await core.renderPage(req("/fb"))).cacheStatus).not.toBe("hit");
|
|
84
|
-
expect(await core.renderPage(req("/un"))).toMatchObject({ status: 401, denied: 401 });
|
|
85
|
-
expect(await core.renderPage(req("/un"))).not.toHaveProperty("notFound");
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
describe("ISR pages", () => {
|
|
90
|
-
it("does not cache a render that opted out, nor one that was invalidated while it ran", async () => {
|
|
91
|
-
let n = 0;
|
|
92
|
-
const { core } = make({
|
|
93
|
-
pages: [page("/ns", "isr", { revalidate: 10 })],
|
|
94
|
-
modules: {
|
|
95
|
-
"/ns": {
|
|
96
|
-
default: () => {
|
|
97
|
-
markNoStore();
|
|
98
|
-
return jsx("p", { children: String(++n) });
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
const a = await core.renderPage(req("/ns"));
|
|
104
|
-
const b = await core.renderPage(req("/ns"));
|
|
105
|
-
expect(a).toMatchObject({ noStore: true, cacheStatus: "miss", html: "<p>1</p>" });
|
|
106
|
-
expect(b).toMatchObject({ cacheStatus: "miss", html: "<p>2</p>" });
|
|
107
|
-
});
|
|
108
|
-
});
|
package/dist/testing.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/testing.test.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// @vitest-environment node
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { cookies, headers, redirect } from "./server.js";
|
|
4
|
-
import { callRoute, runAction } from "./testing.js";
|
|
5
|
-
describe("callRoute", () => {
|
|
6
|
-
const route = {
|
|
7
|
-
async GET(req, ctx) {
|
|
8
|
-
const url = new URL(req.url);
|
|
9
|
-
return Response.json({ id: ctx.params.id, q: url.searchParams.get("q") });
|
|
10
|
-
},
|
|
11
|
-
async POST(req) {
|
|
12
|
-
return Response.json(await req.json(), { status: 201 });
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
it("passes the request and route params to a named export", async () => {
|
|
16
|
-
const res = await callRoute(route, "GET", "/api/users/7?q=x", { params: { id: "7" } });
|
|
17
|
-
expect(res.status).toBe(200);
|
|
18
|
-
expect(await res.json()).toEqual({ id: "7", q: "x" });
|
|
19
|
-
});
|
|
20
|
-
it("sends a JSON body", async () => {
|
|
21
|
-
const res = await callRoute(route, "POST", "/api/users", { json: { name: "ada" } });
|
|
22
|
-
expect(res.status).toBe(201);
|
|
23
|
-
expect(await res.json()).toEqual({ name: "ada" });
|
|
24
|
-
});
|
|
25
|
-
it("answers HEAD from GET, OPTIONS and 405 like the server", async () => {
|
|
26
|
-
const head = await callRoute(route, "HEAD", "/api/users/7");
|
|
27
|
-
expect(head.status).toBe(200);
|
|
28
|
-
expect(await head.text()).toBe("");
|
|
29
|
-
const options = await callRoute(route, "OPTIONS", "/api/users");
|
|
30
|
-
expect(options.headers.get("allow")).toBe("GET, POST, HEAD, OPTIONS");
|
|
31
|
-
const missing = await callRoute(route, "DELETE", "/api/users");
|
|
32
|
-
expect(missing.status).toBe(405);
|
|
33
|
-
expect(missing.headers.get("allow")).toContain("GET");
|
|
34
|
-
});
|
|
35
|
-
it("calls a default Web handler for every method and rejects the legacy signature", async () => {
|
|
36
|
-
const any = { default: async (req) => new Response(req.method) };
|
|
37
|
-
expect(await (await callRoute(any, "PUT", "/x")).text()).toBe("PUT");
|
|
38
|
-
const legacy = { default: (_req, res) => res.json({}) };
|
|
39
|
-
await expect(callRoute(legacy, "GET", "/x")).rejects.toThrow(/legacy/);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe("runAction", () => {
|
|
43
|
-
it("gives the action cookies() and headers() and records cookie writes", async () => {
|
|
44
|
-
async function login() {
|
|
45
|
-
const jar = cookies();
|
|
46
|
-
const before = jar.get("session");
|
|
47
|
-
jar.set("session", "abc", { httpOnly: true });
|
|
48
|
-
jar.delete("old");
|
|
49
|
-
return { before, agent: headers().get("user-agent") };
|
|
50
|
-
}
|
|
51
|
-
const { result, setCookies } = await runAction(login, {
|
|
52
|
-
cookies: { session: "prev", old: "1" },
|
|
53
|
-
headers: { "user-agent": "vitest" },
|
|
54
|
-
});
|
|
55
|
-
expect(result).toEqual({ before: { name: "session", value: "prev" }, agent: "vitest" });
|
|
56
|
-
expect(setCookies).toEqual([
|
|
57
|
-
{ name: "session", value: "abc", options: { httpOnly: true }, deleted: false },
|
|
58
|
-
{ name: "old", value: "", options: undefined, deleted: true },
|
|
59
|
-
]);
|
|
60
|
-
});
|
|
61
|
-
it("rejects with what the action throws, including redirect()", async () => {
|
|
62
|
-
await expect(runAction(async () => redirect("/done"))).rejects.toBeDefined();
|
|
63
|
-
await expect(runAction(async () => { throw new Error("nope"); })).rejects.toThrow("nope");
|
|
64
|
-
});
|
|
65
|
-
it("keeps parallel actions apart", async () => {
|
|
66
|
-
const read = async () => {
|
|
67
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
68
|
-
return cookies().get("who");
|
|
69
|
-
};
|
|
70
|
-
const [a, b] = await Promise.all([runAction(read, { cookies: { who: "a" } }), runAction(read, { cookies: { who: "b" } })]);
|
|
71
|
-
expect([a.result, b.result]).toEqual([{ name: "who", value: "a" }, { name: "who", value: "b" }]);
|
|
72
|
-
});
|
|
73
|
-
});
|