@sigil-dev/grimoire 0.9.17 → 0.9.18
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 +1 -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 +63 -26
- 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 +63 -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,7 +586,11 @@ 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
|
|
|
@@ -598,6 +602,12 @@ declare const props: PageProps;
|
|
|
598
602
|
const _title: string = props.data.post.title;
|
|
599
603
|
const _views: number = props.data.post.views;
|
|
600
604
|
const _slug: string = props.params.slug;
|
|
605
|
+
// a promise left unawaited streams in: undefined, then the value (null on rejection)
|
|
606
|
+
const _comments: string[] | null | undefined = props.data.comments;
|
|
607
|
+
// @ts-expect-error not a promise on the page
|
|
608
|
+
props.data.comments?.then;
|
|
609
|
+
// the action's fail() data comes back as \`form\`
|
|
610
|
+
const _problem: string | undefined = props.form?.problem;
|
|
601
611
|
export {};`,
|
|
602
612
|
);
|
|
603
613
|
|
|
@@ -677,6 +687,21 @@ export {};`,
|
|
|
677
687
|
await unlink(join(routeDir, "bad.ts"));
|
|
678
688
|
});
|
|
679
689
|
|
|
690
|
+
test("form only has what the actions fail() with", async () => {
|
|
691
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
692
|
+
await writeFile(
|
|
693
|
+
join(routeDir, "bad-form.ts"),
|
|
694
|
+
`import type { PageProps } from "./$types";
|
|
695
|
+
declare const props: PageProps;
|
|
696
|
+
const _x = props.form?.nope;
|
|
697
|
+
export {};`,
|
|
698
|
+
);
|
|
699
|
+
const result = runTsc(tscDir);
|
|
700
|
+
expect(result.exitCode).not.toBe(0);
|
|
701
|
+
const { unlink } = await import("node:fs/promises");
|
|
702
|
+
await unlink(join(routeDir, "bad-form.ts"));
|
|
703
|
+
});
|
|
704
|
+
|
|
680
705
|
test("params properties are typed as string", async () => {
|
|
681
706
|
// Assigning params.slug to a number should fail
|
|
682
707
|
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
@@ -696,6 +721,37 @@ export {};`,
|
|
|
696
721
|
});
|
|
697
722
|
});
|
|
698
723
|
|
|
724
|
+
describe("stale route types", () => {
|
|
725
|
+
test("a deleted route's $types.d.ts is removed on the next run", async () => {
|
|
726
|
+
const root = join(tmpdir(), `grimoire-stale-${Date.now()}`);
|
|
727
|
+
const routesDir = join(root, "src", "routes");
|
|
728
|
+
await mkdir(join(routesDir, "gone"), { recursive: true });
|
|
729
|
+
await writeFile(join(routesDir, "+page.tsx"), "export default () => null;");
|
|
730
|
+
await writeFile(
|
|
731
|
+
join(routesDir, "gone", "+page.tsx"),
|
|
732
|
+
"export default () => null;",
|
|
733
|
+
);
|
|
734
|
+
const outDir = join(root, ".grimoire", "types");
|
|
735
|
+
const run = async () =>
|
|
736
|
+
generateTypes(await scanRoutes(routesDir, root), {
|
|
737
|
+
projectRoot: root,
|
|
738
|
+
routesDir,
|
|
739
|
+
outDir,
|
|
740
|
+
});
|
|
741
|
+
await run();
|
|
742
|
+
const stale = join(outDir, "src", "routes", "gone", "$types.d.ts");
|
|
743
|
+
expect(await Bun.file(stale).exists()).toBe(true);
|
|
744
|
+
|
|
745
|
+
await rm(join(routesDir, "gone"), { recursive: true });
|
|
746
|
+
await run();
|
|
747
|
+
expect(await Bun.file(stale).exists()).toBe(false);
|
|
748
|
+
expect(
|
|
749
|
+
await Bun.file(join(outDir, "src", "routes", "$types.d.ts")).exists(),
|
|
750
|
+
).toBe(true);
|
|
751
|
+
await rm(root, { recursive: true, force: true });
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
|
|
699
755
|
describe("tsconfig paths for aliases", () => {
|
|
700
756
|
test("each alias gets a paths entry relative to .grimoire", async () => {
|
|
701
757
|
const root = join(tmpdir(), `grimoire-paths-${Date.now()}`);
|