@sigil-dev/grimoire 0.3.0 → 0.5.0
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 +174 -1
- package/index.ts +29 -22
- package/package.json +1 -1
- package/public/__grimoire__/client.js +5 -36
- package/public/__grimoire__/hydrate.js +7 -45
- package/src/build.ts +88 -0
- package/src/client-router.ts +1 -1
- package/src/cookie-utils.ts +66 -66
- package/src/enhance.ts +1 -1
- package/src/hydrate.ts +1 -1
- package/src/plugins.ts +19 -5
- package/src/renderer.ts +38 -6
- package/src/router.ts +2 -2
- package/src/scanner.ts +20 -0
- package/src/server.ts +83 -83
- package/src/ssrPlugin.ts +7 -2
- package/src/transform-routes.ts +6 -1
- package/src/typegen.ts +77 -3
- package/src/types.ts +45 -1
- package/test/exports.test.ts +38 -0
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +96 -96
- package/test/middleware.test.ts +217 -217
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +310 -172
- package/test/scanning.test.ts +87 -2
- package/test/server.test.ts +178 -8
- package/test/streaming.test.ts +132 -132
- package/test/typegen.test.ts +6 -6
- package/.grimoire/_routes.dom.js +0 -4
- package/.grimoire/_routes.hydrate.js +0 -4
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -6
- package/.grimoire/types/api/hello/$types.d.ts +0 -29
package/test/server.test.ts
CHANGED
|
@@ -1,30 +1,200 @@
|
|
|
1
1
|
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, writeFile } from "fs/promises";
|
|
2
|
+
import { mkdir, rm, writeFile } from "fs/promises";
|
|
3
3
|
import { tmpdir } from "os";
|
|
4
4
|
import { join } from "path";
|
|
5
5
|
import { createServer } from "../src/server";
|
|
6
6
|
|
|
7
|
+
// ── Sandbox note ─────────────────────────────────────────────────────────────
|
|
8
|
+
// The sigil babel plugin unconditionally inserts `import ... from "@sigil-dev/runtime"`
|
|
9
|
+
// for all non-SSR route files. When createServer() calls Bun.build to bundle hydrate.js,
|
|
10
|
+
// those compiled page files pull in @sigil-dev/runtime → Bun's test sandbox blocks the
|
|
11
|
+
// read of packages/runtime/index.ts.
|
|
12
|
+
//
|
|
13
|
+
// Consequence: createServer() integration tests can only use +server.ts API routes
|
|
14
|
+
// (which are never bundled by Bun.build). Form action behavior at the HTTP level cannot
|
|
15
|
+
// be tested here — it is covered at unit level by fail.test.ts and redirect-error.test.ts,
|
|
16
|
+
// and at the renderer level by rendering.test.ts.
|
|
17
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
18
|
+
|
|
7
19
|
let tmpDir: string;
|
|
8
20
|
|
|
9
21
|
beforeAll(async () => {
|
|
10
22
|
tmpDir = join(tmpdir(), `grimoire-server-${Date.now()}`);
|
|
11
23
|
await mkdir(join(tmpDir, "api", "hello"), { recursive: true });
|
|
24
|
+
await mkdir(join(tmpDir, "api", "items"), { recursive: true });
|
|
25
|
+
|
|
12
26
|
await writeFile(
|
|
13
27
|
join(tmpDir, "api", "hello", "+server.ts"),
|
|
14
|
-
`
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
`export async function GET() { return Response.json({ message: "hello" }); }`,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
await writeFile(
|
|
32
|
+
join(tmpDir, "api", "items", "+server.ts"),
|
|
33
|
+
`export async function GET() {
|
|
34
|
+
return Response.json([{ id: 1, name: "sword" }]);
|
|
35
|
+
}
|
|
36
|
+
export async function POST({ request }) {
|
|
37
|
+
const body = await request.json();
|
|
38
|
+
return Response.json({ id: 2, ...body }, { status: 201 });
|
|
39
|
+
}`,
|
|
19
40
|
);
|
|
20
41
|
});
|
|
21
42
|
|
|
22
|
-
describe("Server", () => {
|
|
23
|
-
test("
|
|
43
|
+
describe("Server — API routes (+server.ts)", () => {
|
|
44
|
+
test("GET returns JSON", async () => {
|
|
24
45
|
const server = await createServer({ port: 3004, routes: tmpDir });
|
|
25
46
|
const res = await fetch("http://localhost:3004/api/hello");
|
|
26
47
|
const data = await res.json();
|
|
27
48
|
expect(data.message).toBe("hello");
|
|
28
49
|
server.stop();
|
|
29
50
|
});
|
|
51
|
+
|
|
52
|
+
test("GET on multi-method route returns correct JSON", async () => {
|
|
53
|
+
const server = await createServer({ port: 3004, routes: tmpDir });
|
|
54
|
+
const res = await fetch("http://localhost:3004/api/items");
|
|
55
|
+
expect(res.status).toBe(200);
|
|
56
|
+
const items = await res.json();
|
|
57
|
+
expect(items[0].name).toBe("sword");
|
|
58
|
+
server.stop();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("POST returns 201 with created resource", async () => {
|
|
62
|
+
const server = await createServer({ port: 3004, routes: tmpDir });
|
|
63
|
+
const res = await fetch("http://localhost:3004/api/items", {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: { "Content-Type": "application/json" },
|
|
66
|
+
body: JSON.stringify({ name: "shield" }),
|
|
67
|
+
});
|
|
68
|
+
expect(res.status).toBe(201);
|
|
69
|
+
const item = await res.json();
|
|
70
|
+
expect(item.name).toBe("shield");
|
|
71
|
+
server.stop();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("unsupported method returns 405", async () => {
|
|
75
|
+
const server = await createServer({ port: 3004, routes: tmpDir });
|
|
76
|
+
const res = await fetch("http://localhost:3004/api/items", { method: "DELETE" });
|
|
77
|
+
expect(res.status).toBe(405);
|
|
78
|
+
server.stop();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("Server — WebSocket routes", () => {
|
|
83
|
+
let wsDir: string;
|
|
84
|
+
let server: Awaited<ReturnType<typeof createServer>>;
|
|
85
|
+
|
|
86
|
+
beforeAll(async () => {
|
|
87
|
+
wsDir = join(tmpdir(), `grimoire-ws-${Date.now()}`);
|
|
88
|
+
await mkdir(join(wsDir, "echo"), { recursive: true });
|
|
89
|
+
await mkdir(join(wsDir, "params", "[docId]"), { recursive: true });
|
|
90
|
+
await mkdir(join(wsDir, "reject"), { recursive: true });
|
|
91
|
+
await mkdir(join(wsDir, "mixed"), { recursive: true });
|
|
92
|
+
|
|
93
|
+
// echo: upgrade() merges extra data, message handler echoes back
|
|
94
|
+
await writeFile(
|
|
95
|
+
join(wsDir, "echo", "+server.ts"),
|
|
96
|
+
`export function upgrade({ params }) {
|
|
97
|
+
return { sessionId: "abc123" };
|
|
98
|
+
}
|
|
99
|
+
export const websocket = {
|
|
100
|
+
message(ws, data) { ws.send("echo:" + data); },
|
|
101
|
+
};`,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// params: no upgrade(), ws.data.params still available
|
|
105
|
+
await writeFile(
|
|
106
|
+
join(wsDir, "params", "[docId]", "+server.ts"),
|
|
107
|
+
`export const websocket = {
|
|
108
|
+
open(ws) { ws.send("docId:" + ws.data.params.docId); },
|
|
109
|
+
};`,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
// reject: upgrade() throws → 426
|
|
113
|
+
await writeFile(
|
|
114
|
+
join(wsDir, "reject", "+server.ts"),
|
|
115
|
+
`export function upgrade() {
|
|
116
|
+
throw new Error("Unauthorized");
|
|
117
|
+
}
|
|
118
|
+
export const websocket = {
|
|
119
|
+
message(ws, data) { ws.send(data); },
|
|
120
|
+
};`,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// mixed: HTTP GET and WebSocket both exported
|
|
124
|
+
await writeFile(
|
|
125
|
+
join(wsDir, "mixed", "+server.ts"),
|
|
126
|
+
`export function GET() {
|
|
127
|
+
return Response.json({ kind: "http" });
|
|
128
|
+
}
|
|
129
|
+
export function upgrade({ params }) { return {}; }
|
|
130
|
+
export const websocket = {
|
|
131
|
+
message(ws, data) { ws.send(data); },
|
|
132
|
+
};`,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
server = await createServer({ port: 3005, routes: wsDir });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
afterAll(() => {
|
|
139
|
+
server.stop();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
function wsConnect(path: string): Promise<WebSocket> {
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
const ws = new WebSocket(`ws://localhost:3005${path}`);
|
|
145
|
+
ws.onopen = () => resolve(ws);
|
|
146
|
+
ws.onerror = () => reject(new Error(`WebSocket connection failed: ${path}`));
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function nextMessage(ws: WebSocket): Promise<string> {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
const timer = setTimeout(() => reject(new Error("timeout waiting for message")), 2000);
|
|
153
|
+
ws.onmessage = (ev) => { clearTimeout(timer); resolve(String(ev.data)); };
|
|
154
|
+
ws.onerror = () => { clearTimeout(timer); reject(new Error("ws error")); };
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
test("message is echoed back", async () => {
|
|
159
|
+
const ws = await wsConnect("/echo");
|
|
160
|
+
const pending = nextMessage(ws);
|
|
161
|
+
ws.send("hello");
|
|
162
|
+
expect(await pending).toBe("echo:hello");
|
|
163
|
+
ws.close();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("upgrade() return value merges into ws.data", async () => {
|
|
167
|
+
// sessionId set in upgrade() — echo route sends it back via a second message
|
|
168
|
+
const ws = new WebSocket("ws://localhost:3005/echo");
|
|
169
|
+
// just verify connection opens cleanly (sessionId is in ws.data server-side)
|
|
170
|
+
await new Promise<void>((resolve, reject) => {
|
|
171
|
+
const t = setTimeout(() => reject(new Error("timeout")), 2000);
|
|
172
|
+
ws.onopen = () => { clearTimeout(t); resolve(); };
|
|
173
|
+
ws.onerror = () => { clearTimeout(t); reject(new Error("connect failed")); };
|
|
174
|
+
});
|
|
175
|
+
ws.close();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("ws.data.params available when no upgrade() export", async () => {
|
|
179
|
+
const ws = await wsConnect("/params/42");
|
|
180
|
+
expect(await nextMessage(ws)).toBe("docId:42");
|
|
181
|
+
ws.close();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("upgrade() throwing rejects the connection", async () => {
|
|
185
|
+
await new Promise<void>((resolve, reject) => {
|
|
186
|
+
const ws = new WebSocket("ws://localhost:3005/reject");
|
|
187
|
+
const t = setTimeout(() => reject(new Error("timeout")), 2000);
|
|
188
|
+
ws.onopen = () => { clearTimeout(t); ws.close(); reject(new Error("should not have opened")); };
|
|
189
|
+
ws.onerror = () => { clearTimeout(t); resolve(); };
|
|
190
|
+
ws.onclose = (ev) => { clearTimeout(t); if (ev.code !== 1006) resolve(); else resolve(); };
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("plain HTTP GET on mixed route still works (no Upgrade header)", async () => {
|
|
195
|
+
const res = await fetch("http://localhost:3005/mixed");
|
|
196
|
+
expect(res.status).toBe(200);
|
|
197
|
+
const body = await res.json();
|
|
198
|
+
expect(body.kind).toBe("http");
|
|
199
|
+
});
|
|
30
200
|
});
|
package/test/streaming.test.ts
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { renderRoute } from "../src/renderer";
|
|
3
|
-
|
|
4
|
-
describe("Streaming SSR", () => {
|
|
5
|
-
test("returns a ReadableStream response", async () => {
|
|
6
|
-
const matched = {
|
|
7
|
-
route: {
|
|
8
|
-
path: "/",
|
|
9
|
-
params: {},
|
|
10
|
-
filePath: "data:text/javascript,export default (p) => '<div>hello</div>'",
|
|
11
|
-
},
|
|
12
|
-
params: {},
|
|
13
|
-
} as any;
|
|
14
|
-
|
|
15
|
-
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
16
|
-
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("stream contains full HTML document", async () => {
|
|
20
|
-
const matched = {
|
|
21
|
-
route: {
|
|
22
|
-
path: "/test",
|
|
23
|
-
params: {},
|
|
24
|
-
filePath:
|
|
25
|
-
"data:text/javascript,export default (p) => '<p>streaming works</p>'",
|
|
26
|
-
},
|
|
27
|
-
params: {},
|
|
28
|
-
} as any;
|
|
29
|
-
|
|
30
|
-
const res = await renderRoute(matched, new Request("http://localhost/test"));
|
|
31
|
-
const html = await res.text();
|
|
32
|
-
|
|
33
|
-
expect(html).toContain("<!DOCTYPE html>");
|
|
34
|
-
expect(html).toContain('<meta charset="UTF-8" />');
|
|
35
|
-
expect(html).toContain("streaming works");
|
|
36
|
-
expect(html).toContain("</html>");
|
|
37
|
-
expect(html).toContain("__grimoire_state__");
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("stream includes state JSON with route pattern", async () => {
|
|
41
|
-
const matched = {
|
|
42
|
-
route: {
|
|
43
|
-
path: "/spells/:id",
|
|
44
|
-
params: {},
|
|
45
|
-
filePath:
|
|
46
|
-
"data:text/javascript,export default (p) => '<div>spell</div>'",
|
|
47
|
-
},
|
|
48
|
-
params: { id: "fireball" },
|
|
49
|
-
} as any;
|
|
50
|
-
|
|
51
|
-
const res = await renderRoute(
|
|
52
|
-
matched,
|
|
53
|
-
new Request("http://localhost/spells/fireball"),
|
|
54
|
-
);
|
|
55
|
-
const html = await res.text();
|
|
56
|
-
|
|
57
|
-
expect(html).toContain('"pattern":"/spells/:id"');
|
|
58
|
-
expect(html).toContain('"id":"fireball"');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("head script tag comes before head content", async () => {
|
|
62
|
-
const matched = {
|
|
63
|
-
route: {
|
|
64
|
-
path: "/head-test",
|
|
65
|
-
params: {},
|
|
66
|
-
filePath:
|
|
67
|
-
"data:text/javascript,export default (p) => '<div>head</div>'",
|
|
68
|
-
},
|
|
69
|
-
params: {},
|
|
70
|
-
} as any;
|
|
71
|
-
|
|
72
|
-
const res = await renderRoute(matched, new Request("http://localhost/head-test"));
|
|
73
|
-
const html = await res.text();
|
|
74
|
-
|
|
75
|
-
const hydrateScript = html.indexOf("hydrate.js");
|
|
76
|
-
const headClosing = html.indexOf("</head>");
|
|
77
|
-
// hydrate.js should be in <head>
|
|
78
|
-
expect(hydrateScript).toBeGreaterThan(-1);
|
|
79
|
-
expect(hydrateScript).toBeLessThan(headClosing);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("stream chunks arrive incrementally", async () => {
|
|
83
|
-
const matched = {
|
|
84
|
-
route: {
|
|
85
|
-
path: "/chunked",
|
|
86
|
-
params: {},
|
|
87
|
-
filePath:
|
|
88
|
-
"data:text/javascript,export default (p) => '<div>chunked</div>'",
|
|
89
|
-
},
|
|
90
|
-
params: {},
|
|
91
|
-
} as any;
|
|
92
|
-
|
|
93
|
-
const res = await renderRoute(matched, new Request("http://localhost/chunked"));
|
|
94
|
-
const reader = res.body!.getReader();
|
|
95
|
-
const chunks: string[] = [];
|
|
96
|
-
|
|
97
|
-
while (true) {
|
|
98
|
-
const { done, value } = await reader.read();
|
|
99
|
-
if (done) break;
|
|
100
|
-
// Bun ReadableStream returns Uint8Array
|
|
101
|
-
chunks.push(typeof value === "string" ? value : new TextDecoder().decode(value.buffer));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Should have multiple chunks (at least DOCTYPE + body)
|
|
105
|
-
expect(chunks.length).toBeGreaterThan(1);
|
|
106
|
-
// First chunk should start with DOCTYPE
|
|
107
|
-
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
108
|
-
// Last chunk should close the document
|
|
109
|
-
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("navigation request returns JSON not stream", async () => {
|
|
113
|
-
const matched = {
|
|
114
|
-
route: {
|
|
115
|
-
path: "/nav",
|
|
116
|
-
params: {},
|
|
117
|
-
filePath:
|
|
118
|
-
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
119
|
-
},
|
|
120
|
-
params: {},
|
|
121
|
-
} as any;
|
|
122
|
-
|
|
123
|
-
const req = new Request("http://localhost/nav", {
|
|
124
|
-
headers: { "x-grimoire-navigate": "1" },
|
|
125
|
-
});
|
|
126
|
-
const res = await renderRoute(matched, req);
|
|
127
|
-
const json = await res.json();
|
|
128
|
-
|
|
129
|
-
expect(json.pattern).toBe("/nav");
|
|
130
|
-
expect(json.data).toBeDefined();
|
|
131
|
-
});
|
|
132
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { renderRoute } from "../src/renderer";
|
|
3
|
+
|
|
4
|
+
describe("Streaming SSR", () => {
|
|
5
|
+
test("returns a ReadableStream response", async () => {
|
|
6
|
+
const matched = {
|
|
7
|
+
route: {
|
|
8
|
+
path: "/",
|
|
9
|
+
params: {},
|
|
10
|
+
filePath: "data:text/javascript,export default (p) => '<div>hello</div>'",
|
|
11
|
+
},
|
|
12
|
+
params: {},
|
|
13
|
+
} as any;
|
|
14
|
+
|
|
15
|
+
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
16
|
+
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("stream contains full HTML document", async () => {
|
|
20
|
+
const matched = {
|
|
21
|
+
route: {
|
|
22
|
+
path: "/test",
|
|
23
|
+
params: {},
|
|
24
|
+
filePath:
|
|
25
|
+
"data:text/javascript,export default (p) => '<p>streaming works</p>'",
|
|
26
|
+
},
|
|
27
|
+
params: {},
|
|
28
|
+
} as any;
|
|
29
|
+
|
|
30
|
+
const res = await renderRoute(matched, new Request("http://localhost/test"));
|
|
31
|
+
const html = await res.text();
|
|
32
|
+
|
|
33
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
34
|
+
expect(html).toContain('<meta charset="UTF-8" />');
|
|
35
|
+
expect(html).toContain("streaming works");
|
|
36
|
+
expect(html).toContain("</html>");
|
|
37
|
+
expect(html).toContain("__grimoire_state__");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("stream includes state JSON with route pattern", async () => {
|
|
41
|
+
const matched = {
|
|
42
|
+
route: {
|
|
43
|
+
path: "/spells/:id",
|
|
44
|
+
params: {},
|
|
45
|
+
filePath:
|
|
46
|
+
"data:text/javascript,export default (p) => '<div>spell</div>'",
|
|
47
|
+
},
|
|
48
|
+
params: { id: "fireball" },
|
|
49
|
+
} as any;
|
|
50
|
+
|
|
51
|
+
const res = await renderRoute(
|
|
52
|
+
matched,
|
|
53
|
+
new Request("http://localhost/spells/fireball"),
|
|
54
|
+
);
|
|
55
|
+
const html = await res.text();
|
|
56
|
+
|
|
57
|
+
expect(html).toContain('"pattern":"/spells/:id"');
|
|
58
|
+
expect(html).toContain('"id":"fireball"');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("head script tag comes before head content", async () => {
|
|
62
|
+
const matched = {
|
|
63
|
+
route: {
|
|
64
|
+
path: "/head-test",
|
|
65
|
+
params: {},
|
|
66
|
+
filePath:
|
|
67
|
+
"data:text/javascript,export default (p) => '<div>head</div>'",
|
|
68
|
+
},
|
|
69
|
+
params: {},
|
|
70
|
+
} as any;
|
|
71
|
+
|
|
72
|
+
const res = await renderRoute(matched, new Request("http://localhost/head-test"));
|
|
73
|
+
const html = await res.text();
|
|
74
|
+
|
|
75
|
+
const hydrateScript = html.indexOf("hydrate.js");
|
|
76
|
+
const headClosing = html.indexOf("</head>");
|
|
77
|
+
// hydrate.js should be in <head>
|
|
78
|
+
expect(hydrateScript).toBeGreaterThan(-1);
|
|
79
|
+
expect(hydrateScript).toBeLessThan(headClosing);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("stream chunks arrive incrementally", async () => {
|
|
83
|
+
const matched = {
|
|
84
|
+
route: {
|
|
85
|
+
path: "/chunked",
|
|
86
|
+
params: {},
|
|
87
|
+
filePath:
|
|
88
|
+
"data:text/javascript,export default (p) => '<div>chunked</div>'",
|
|
89
|
+
},
|
|
90
|
+
params: {},
|
|
91
|
+
} as any;
|
|
92
|
+
|
|
93
|
+
const res = await renderRoute(matched, new Request("http://localhost/chunked"));
|
|
94
|
+
const reader = res.body!.getReader();
|
|
95
|
+
const chunks: string[] = [];
|
|
96
|
+
|
|
97
|
+
while (true) {
|
|
98
|
+
const { done, value } = await reader.read();
|
|
99
|
+
if (done) break;
|
|
100
|
+
// Bun ReadableStream returns Uint8Array
|
|
101
|
+
chunks.push(typeof value === "string" ? value : new TextDecoder().decode(value.buffer));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Should have multiple chunks (at least DOCTYPE + body)
|
|
105
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
106
|
+
// First chunk should start with DOCTYPE
|
|
107
|
+
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
108
|
+
// Last chunk should close the document
|
|
109
|
+
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("navigation request returns JSON not stream", async () => {
|
|
113
|
+
const matched = {
|
|
114
|
+
route: {
|
|
115
|
+
path: "/nav",
|
|
116
|
+
params: {},
|
|
117
|
+
filePath:
|
|
118
|
+
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
119
|
+
},
|
|
120
|
+
params: {},
|
|
121
|
+
} as any;
|
|
122
|
+
|
|
123
|
+
const req = new Request("http://localhost/nav", {
|
|
124
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
125
|
+
});
|
|
126
|
+
const res = await renderRoute(matched, req);
|
|
127
|
+
const json = await res.json();
|
|
128
|
+
|
|
129
|
+
expect(json.pattern).toBe("/nav");
|
|
130
|
+
expect(json.data).toBeDefined();
|
|
131
|
+
});
|
|
132
|
+
});
|
package/test/typegen.test.ts
CHANGED
|
@@ -416,9 +416,9 @@ describe("generateTypes — route without +page.server.ts", () => {
|
|
|
416
416
|
expect(content).toContain("export type Actions = Record<string, never>;");
|
|
417
417
|
});
|
|
418
418
|
|
|
419
|
-
test("PageProps contains params", async () => {
|
|
419
|
+
test("PageProps contains data and params", async () => {
|
|
420
420
|
const content = await readGenerated("src/routes/blog");
|
|
421
|
-
expect(content).toContain("export type PageProps = { params: Params };");
|
|
421
|
+
expect(content).toContain("export type PageProps = { data: PageData; params: Params };");
|
|
422
422
|
});
|
|
423
423
|
});
|
|
424
424
|
|
|
@@ -448,10 +448,10 @@ describe("generateTypes — route with +page.server.ts", () => {
|
|
|
448
448
|
expect(content).toContain('Awaited<ReturnType<_PS["load"]>>');
|
|
449
449
|
});
|
|
450
450
|
|
|
451
|
-
test("PageProps =
|
|
451
|
+
test("PageProps = { data: PageData; params: Params }", async () => {
|
|
452
452
|
const content = await readGenerated("src/routes/blog/[slug]");
|
|
453
453
|
expect(content).toContain(
|
|
454
|
-
"export type PageProps =
|
|
454
|
+
"export type PageProps = { data: PageData; params: Params };",
|
|
455
455
|
);
|
|
456
456
|
});
|
|
457
457
|
|
|
@@ -559,8 +559,8 @@ describe("tsc integration", () => {
|
|
|
559
559
|
join(routeDir, "check.ts"),
|
|
560
560
|
`import type { PageProps } from "./$types";
|
|
561
561
|
declare const props: PageProps;
|
|
562
|
-
const _title: string = props.post.title;
|
|
563
|
-
const _views: number = props.post.views;
|
|
562
|
+
const _title: string = props.data.post.title;
|
|
563
|
+
const _views: number = props.data.post.views;
|
|
564
564
|
const _slug: string = props.params.slug;
|
|
565
565
|
export {};`,
|
|
566
566
|
);
|
package/.grimoire/_routes.dom.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// Auto-generated by @sigil-dev/grimoire — do not edit
|
|
2
|
-
|
|
3
|
-
export type Params = {};
|
|
4
|
-
|
|
5
|
-
export type PageData = Record<string, never>;
|
|
6
|
-
export type PageProps = { params: Params };
|
|
7
|
-
export type PageServerLoad = never;
|
|
8
|
-
export type Actions = Record<string, never>;
|
|
9
|
-
|
|
10
|
-
export type LayoutData = Record<string, never>;
|
|
11
|
-
export type LayoutProps = LayoutData & { params: Params; children?: unknown };
|
|
12
|
-
export type LayoutServerLoad = (ctx: {
|
|
13
|
-
params: Params;
|
|
14
|
-
request: Request;
|
|
15
|
-
url: URL;
|
|
16
|
-
locals: App.Locals;
|
|
17
|
-
}) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
18
|
-
|
|
19
|
-
type _SRV = typeof import("C:/Users/Cane1712/AppData/Local/Temp/grimoire-server-1780500535824/api/hello/+server.js");
|
|
20
|
-
type _HttpMethods = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
|
|
21
|
-
type _ServerKeys = Extract<keyof _SRV, _HttpMethods>;
|
|
22
|
-
export type ServerHandlers = {
|
|
23
|
-
[K in _ServerKeys]: (ctx: {
|
|
24
|
-
params: Params;
|
|
25
|
-
request: Request;
|
|
26
|
-
url: URL;
|
|
27
|
-
locals: App.Locals;
|
|
28
|
-
}) => Response | Promise<Response>;
|
|
29
|
-
};
|