@sigil-dev/grimoire 0.9.17 → 0.9.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -138
- package/index.ts +7 -0
- package/package.json +11 -4
- package/src/client/enhance.ts +22 -3
- package/src/client/router.ts +18 -2
- package/src/dev/runtime-bundle.ts +18 -19
- package/src/logger/transports.ts +1 -1
- package/src/rendering/index.ts +2 -0
- package/src/rendering/preload.ts +2 -1
- package/src/server/build.ts +18 -11
- package/src/server/client-address.ts +82 -0
- package/src/server/client-dir.ts +57 -0
- package/src/server/coordinator.ts +18 -2
- package/src/server/hooks.ts +6 -0
- package/src/server/index.ts +37 -12
- package/src/server/plugins.ts +16 -0
- package/src/typegen/index.ts +78 -30
- package/src/types.ts +16 -1
- package/test/client-address.test.ts +78 -0
- package/test/client-dir.test.ts +69 -0
- package/test/plugin-handle.test.ts +62 -0
- package/test/preload.test.ts +1 -1
- package/test/server.test.ts +24 -0
- package/test/typegen.test.ts +79 -7
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import type { Handle, RequestEvent } from "../src/server/hooks.ts";
|
|
3
|
+
import { composeHandle } from "../src/server/plugins.ts";
|
|
4
|
+
import type { GrimoirePlugin } from "../src/types.ts";
|
|
5
|
+
|
|
6
|
+
const event = () => ({ locals: {} }) as unknown as RequestEvent;
|
|
7
|
+
const ok = () => new Response("page");
|
|
8
|
+
const named = (name: string, handle?: Handle): GrimoirePlugin => ({
|
|
9
|
+
name,
|
|
10
|
+
handle,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("composeHandle", () => {
|
|
14
|
+
test("is undefined when nobody has a handle", () => {
|
|
15
|
+
expect(composeHandle([named("a")], undefined)).toBeUndefined();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("plugins run in order, then the app's handle, which sees their locals", async () => {
|
|
19
|
+
const seen: string[] = [];
|
|
20
|
+
const a = named("a", ({ event, resolve }) => {
|
|
21
|
+
(event.locals as unknown as Record<string, unknown>).user = "ada";
|
|
22
|
+
seen.push("a");
|
|
23
|
+
return resolve(event);
|
|
24
|
+
});
|
|
25
|
+
const b = named("b", ({ event, resolve }) => {
|
|
26
|
+
seen.push("b");
|
|
27
|
+
return resolve(event);
|
|
28
|
+
});
|
|
29
|
+
const app: Handle = ({ event, resolve }) => {
|
|
30
|
+
seen.push(
|
|
31
|
+
`app:${(event.locals as unknown as Record<string, unknown>).user}`,
|
|
32
|
+
);
|
|
33
|
+
return resolve(event);
|
|
34
|
+
};
|
|
35
|
+
const res = await composeHandle([a, b], app)!({
|
|
36
|
+
event: event(),
|
|
37
|
+
resolve: ok,
|
|
38
|
+
});
|
|
39
|
+
expect(await res.text()).toBe("page");
|
|
40
|
+
expect(seen).toEqual(["a", "b", "app:ada"]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("a plugin can answer without calling resolve", async () => {
|
|
44
|
+
let reached = false;
|
|
45
|
+
const stop = named("stop", () => new Response("no", { status: 401 }));
|
|
46
|
+
const res = await composeHandle([stop], () => {
|
|
47
|
+
reached = true;
|
|
48
|
+
return ok();
|
|
49
|
+
})!({ event: event(), resolve: ok });
|
|
50
|
+
expect(res.status).toBe(401);
|
|
51
|
+
expect(reached).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("a lone plugin handle is used as is", async () => {
|
|
55
|
+
const only = named("only", ({ resolve, event }) => resolve(event));
|
|
56
|
+
const res = await composeHandle([only], undefined)!({
|
|
57
|
+
event: event(),
|
|
58
|
+
resolve: ok,
|
|
59
|
+
});
|
|
60
|
+
expect(await res.text()).toBe("page");
|
|
61
|
+
});
|
|
62
|
+
});
|
package/test/preload.test.ts
CHANGED
|
@@ -42,7 +42,7 @@ describe("chunk graph", () => {
|
|
|
42
42
|
describe("preloadLinks", () => {
|
|
43
43
|
test("is empty in dev and when no build exists", () => {
|
|
44
44
|
expect(preloadLinks("/", "", true)).toBe("");
|
|
45
|
-
// cwd here has no
|
|
45
|
+
// cwd here has no .grimoire/client/hydrate.js
|
|
46
46
|
expect(preloadLinks("/", "")).toBe("");
|
|
47
47
|
});
|
|
48
48
|
});
|
package/test/server.test.ts
CHANGED
|
@@ -22,6 +22,7 @@ beforeAll(async () => {
|
|
|
22
22
|
tmpDir = join(tmpdir(), `grimoire-server-${Date.now()}`);
|
|
23
23
|
await mkdir(join(tmpDir, "api", "hello"), { recursive: true });
|
|
24
24
|
await mkdir(join(tmpDir, "api", "items"), { recursive: true });
|
|
25
|
+
await mkdir(join(tmpDir, "api", "away"), { recursive: true });
|
|
25
26
|
|
|
26
27
|
await writeFile(
|
|
27
28
|
join(tmpDir, "api", "hello", "+server.ts"),
|
|
@@ -38,6 +39,14 @@ beforeAll(async () => {
|
|
|
38
39
|
return Response.json({ id: 2, ...body }, { status: 201 });
|
|
39
40
|
}`,
|
|
40
41
|
);
|
|
42
|
+
|
|
43
|
+
// what redirect(303, "/login") throws
|
|
44
|
+
await writeFile(
|
|
45
|
+
join(tmpDir, "api", "away", "+server.ts"),
|
|
46
|
+
`export async function POST() {
|
|
47
|
+
throw { __redirect: true, status: 303, location: "/login" };
|
|
48
|
+
}`,
|
|
49
|
+
);
|
|
41
50
|
});
|
|
42
51
|
|
|
43
52
|
describe("Server — API routes (+server.ts)", () => {
|
|
@@ -83,6 +92,21 @@ describe("Server — API routes (+server.ts)", () => {
|
|
|
83
92
|
server.stop();
|
|
84
93
|
});
|
|
85
94
|
|
|
95
|
+
test("a thrown redirect becomes a redirect response", async () => {
|
|
96
|
+
const server = await createServer({
|
|
97
|
+
port: 3004,
|
|
98
|
+
routes: tmpDir,
|
|
99
|
+
_skipBuild: true,
|
|
100
|
+
});
|
|
101
|
+
const res = await fetch("http://localhost:3004/api/away", {
|
|
102
|
+
method: "POST",
|
|
103
|
+
redirect: "manual",
|
|
104
|
+
});
|
|
105
|
+
expect(res.status).toBe(303);
|
|
106
|
+
expect(res.headers.get("Location")).toBe("/login");
|
|
107
|
+
server.stop();
|
|
108
|
+
});
|
|
109
|
+
|
|
86
110
|
test("unsupported method returns 405", async () => {
|
|
87
111
|
const server = await createServer({
|
|
88
112
|
port: 3004,
|
package/test/typegen.test.ts
CHANGED
|
@@ -444,7 +444,7 @@ describe("generateTypes — route without +page.server.ts", () => {
|
|
|
444
444
|
test("PageProps contains data and params", async () => {
|
|
445
445
|
const content = await readGenerated("src/routes/blog");
|
|
446
446
|
expect(content).toContain(
|
|
447
|
-
"export type PageProps = { data: PageData; params: Params };",
|
|
447
|
+
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
448
448
|
);
|
|
449
449
|
});
|
|
450
450
|
});
|
|
@@ -470,15 +470,15 @@ describe("generateTypes — route with +page.server.ts", () => {
|
|
|
470
470
|
test("PageData inferred from load() via ReturnType", async () => {
|
|
471
471
|
const content = await readGenerated("src/routes/blog/[slug]");
|
|
472
472
|
expect(content).toContain(
|
|
473
|
-
|
|
473
|
+
"export type PageData = _PS extends { load: (...args: any[]) => infer R }",
|
|
474
474
|
);
|
|
475
|
-
expect(content).toContain(
|
|
475
|
+
expect(content).toContain("? _Streamed<Awaited<R>>");
|
|
476
476
|
});
|
|
477
477
|
|
|
478
478
|
test("PageProps = { data: PageData; params: Params }", async () => {
|
|
479
479
|
const content = await readGenerated("src/routes/blog/[slug]");
|
|
480
480
|
expect(content).toContain(
|
|
481
|
-
"export type PageProps = { data: PageData; params: Params };",
|
|
481
|
+
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
482
482
|
);
|
|
483
483
|
});
|
|
484
484
|
|
|
@@ -500,9 +500,9 @@ describe("generateTypes — +layout.server.ts", () => {
|
|
|
500
500
|
const content = await readGenerated("src/routes/blog");
|
|
501
501
|
expect(content).toMatch(/type _LS = typeof import\("/);
|
|
502
502
|
expect(content).toContain(
|
|
503
|
-
|
|
503
|
+
"export type LayoutData = _LS extends { load: (...args: any[]) => infer R }",
|
|
504
504
|
);
|
|
505
|
-
expect(content).toContain(
|
|
505
|
+
expect(content).toContain("? Awaited<R>");
|
|
506
506
|
});
|
|
507
507
|
|
|
508
508
|
test("LayoutProps includes children", async () => {
|
|
@@ -586,10 +586,30 @@ describe("tsc integration", () => {
|
|
|
586
586
|
await writeFile(
|
|
587
587
|
join(routeDir, "+page.server.ts"),
|
|
588
588
|
`export async function load({ params }: { params: { slug: string } }) {
|
|
589
|
-
return { post: { title: "Hello", views: 42 } };
|
|
589
|
+
return { post: { title: "Hello", views: 42 }, comments: Promise.resolve(["first"]) };
|
|
590
|
+
}
|
|
591
|
+
// what fail(400, { problem }) returns
|
|
592
|
+
export async function comment() {
|
|
593
|
+
if (Math.random() > 1) return { __fail: true as const, status: 400, data: { problem: "Say something" } };
|
|
590
594
|
}`,
|
|
591
595
|
);
|
|
592
596
|
|
|
597
|
+
// a +server.ts route: its types must compile without Bun's types installed
|
|
598
|
+
const chatDir = join(tscDir, "src", "routes", "chat");
|
|
599
|
+
await mkdir(chatDir, { recursive: true });
|
|
600
|
+
await writeFile(
|
|
601
|
+
join(chatDir, "+server.ts"),
|
|
602
|
+
`export function GET() { return new Response("ok"); }
|
|
603
|
+
export function upgrade() { return { room: "lobby" }; }`,
|
|
604
|
+
);
|
|
605
|
+
await writeFile(
|
|
606
|
+
join(chatDir, "check.ts"),
|
|
607
|
+
`import type { WebSocketHandler } from "./$types";
|
|
608
|
+
export const websocket: WebSocketHandler = {
|
|
609
|
+
message(ws, data) { ws.send(\`\${ws.data.room}: \${String(data)}\`); },
|
|
610
|
+
};`,
|
|
611
|
+
);
|
|
612
|
+
|
|
593
613
|
// File that uses the generated types — must live in the route dir for ./$types to resolve
|
|
594
614
|
await writeFile(
|
|
595
615
|
join(routeDir, "check.ts"),
|
|
@@ -598,6 +618,12 @@ declare const props: PageProps;
|
|
|
598
618
|
const _title: string = props.data.post.title;
|
|
599
619
|
const _views: number = props.data.post.views;
|
|
600
620
|
const _slug: string = props.params.slug;
|
|
621
|
+
// a promise left unawaited streams in: undefined, then the value (null on rejection)
|
|
622
|
+
const _comments: string[] | null | undefined = props.data.comments;
|
|
623
|
+
// @ts-expect-error not a promise on the page
|
|
624
|
+
props.data.comments?.then;
|
|
625
|
+
// the action's fail() data comes back as \`form\`
|
|
626
|
+
const _problem: string | undefined = props.form?.problem;
|
|
601
627
|
export {};`,
|
|
602
628
|
);
|
|
603
629
|
|
|
@@ -677,6 +703,21 @@ export {};`,
|
|
|
677
703
|
await unlink(join(routeDir, "bad.ts"));
|
|
678
704
|
});
|
|
679
705
|
|
|
706
|
+
test("form only has what the actions fail() with", async () => {
|
|
707
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
708
|
+
await writeFile(
|
|
709
|
+
join(routeDir, "bad-form.ts"),
|
|
710
|
+
`import type { PageProps } from "./$types";
|
|
711
|
+
declare const props: PageProps;
|
|
712
|
+
const _x = props.form?.nope;
|
|
713
|
+
export {};`,
|
|
714
|
+
);
|
|
715
|
+
const result = runTsc(tscDir);
|
|
716
|
+
expect(result.exitCode).not.toBe(0);
|
|
717
|
+
const { unlink } = await import("node:fs/promises");
|
|
718
|
+
await unlink(join(routeDir, "bad-form.ts"));
|
|
719
|
+
});
|
|
720
|
+
|
|
680
721
|
test("params properties are typed as string", async () => {
|
|
681
722
|
// Assigning params.slug to a number should fail
|
|
682
723
|
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
@@ -696,6 +737,37 @@ export {};`,
|
|
|
696
737
|
});
|
|
697
738
|
});
|
|
698
739
|
|
|
740
|
+
describe("stale route types", () => {
|
|
741
|
+
test("a deleted route's $types.d.ts is removed on the next run", async () => {
|
|
742
|
+
const root = join(tmpdir(), `grimoire-stale-${Date.now()}`);
|
|
743
|
+
const routesDir = join(root, "src", "routes");
|
|
744
|
+
await mkdir(join(routesDir, "gone"), { recursive: true });
|
|
745
|
+
await writeFile(join(routesDir, "+page.tsx"), "export default () => null;");
|
|
746
|
+
await writeFile(
|
|
747
|
+
join(routesDir, "gone", "+page.tsx"),
|
|
748
|
+
"export default () => null;",
|
|
749
|
+
);
|
|
750
|
+
const outDir = join(root, ".grimoire", "types");
|
|
751
|
+
const run = async () =>
|
|
752
|
+
generateTypes(await scanRoutes(routesDir, root), {
|
|
753
|
+
projectRoot: root,
|
|
754
|
+
routesDir,
|
|
755
|
+
outDir,
|
|
756
|
+
});
|
|
757
|
+
await run();
|
|
758
|
+
const stale = join(outDir, "src", "routes", "gone", "$types.d.ts");
|
|
759
|
+
expect(await Bun.file(stale).exists()).toBe(true);
|
|
760
|
+
|
|
761
|
+
await rm(join(routesDir, "gone"), { recursive: true });
|
|
762
|
+
await run();
|
|
763
|
+
expect(await Bun.file(stale).exists()).toBe(false);
|
|
764
|
+
expect(
|
|
765
|
+
await Bun.file(join(outDir, "src", "routes", "$types.d.ts")).exists(),
|
|
766
|
+
).toBe(true);
|
|
767
|
+
await rm(root, { recursive: true, force: true });
|
|
768
|
+
});
|
|
769
|
+
});
|
|
770
|
+
|
|
699
771
|
describe("tsconfig paths for aliases", () => {
|
|
700
772
|
test("each alias gets a paths entry relative to .grimoire", async () => {
|
|
701
773
|
const root = join(tmpdir(), `grimoire-paths-${Date.now()}`);
|