@sigil-dev/grimoire 0.9.5 → 0.9.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/package.json +3 -3
- package/src/client/router.ts +87 -19
- package/src/rendering/index.ts +34 -12
- package/src/server/coordinator.ts +364 -364
- package/src/server/index.ts +718 -711
- package/test/client-router.test.ts +366 -295
- package/test/streaming.test.ts +281 -242
package/test/streaming.test.ts
CHANGED
|
@@ -1,242 +1,281 @@
|
|
|
1
|
-
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { renderRoute } from "../src/rendering";
|
|
6
|
-
import { matchRoute } from "../src/routing/router";
|
|
7
|
-
import { scanRoutes } from "../src/routing/scanner";
|
|
8
|
-
|
|
9
|
-
function makeMatched(path: string, filePath: string, params = {}) {
|
|
10
|
-
return {
|
|
11
|
-
route: { path, params: {}, filePath },
|
|
12
|
-
params,
|
|
13
|
-
layouts: [],
|
|
14
|
-
layoutServers: [],
|
|
15
|
-
} as any;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
describe("Streaming SSR", () => {
|
|
19
|
-
test("returns a ReadableStream response", async () => {
|
|
20
|
-
const matched = makeMatched(
|
|
21
|
-
"/",
|
|
22
|
-
"data:text/javascript,export default (p) => '<div>hello</div>'",
|
|
23
|
-
);
|
|
24
|
-
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
25
|
-
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("stream contains full HTML document", async () => {
|
|
29
|
-
const matched = makeMatched(
|
|
30
|
-
"/test",
|
|
31
|
-
"data:text/javascript,export default (p) => '<p>streaming works</p>'",
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
const res = await renderRoute(
|
|
35
|
-
matched,
|
|
36
|
-
new Request("http://localhost/test"),
|
|
37
|
-
);
|
|
38
|
-
const html = await res.text();
|
|
39
|
-
|
|
40
|
-
expect(html).toContain("<!DOCTYPE html>");
|
|
41
|
-
expect(html).toContain('<meta charset="UTF-8" />');
|
|
42
|
-
expect(html).toContain("streaming works");
|
|
43
|
-
expect(html).toContain("</html>");
|
|
44
|
-
expect(html).toContain("__grimoire_state__");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("stream includes state JSON with route pattern", async () => {
|
|
48
|
-
const matched = makeMatched(
|
|
49
|
-
"/spells/:id",
|
|
50
|
-
"data:text/javascript,export default (p) => '<div>spell</div>'",
|
|
51
|
-
{ id: "fireball" },
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
const res = await renderRoute(
|
|
55
|
-
matched,
|
|
56
|
-
new Request("http://localhost/spells/fireball"),
|
|
57
|
-
);
|
|
58
|
-
const html = await res.text();
|
|
59
|
-
|
|
60
|
-
expect(html).toContain('"pattern":"/spells/:id"');
|
|
61
|
-
expect(html).toContain('"id":"fireball"');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("head script tag comes before head content", async () => {
|
|
65
|
-
const matched = makeMatched(
|
|
66
|
-
"/head-test",
|
|
67
|
-
"data:text/javascript,export default (p) => '<div>head</div>'",
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const res = await renderRoute(
|
|
71
|
-
matched,
|
|
72
|
-
new Request("http://localhost/head-test"),
|
|
73
|
-
);
|
|
74
|
-
const html = await res.text();
|
|
75
|
-
|
|
76
|
-
const hydrateScript = html.indexOf("hydrate.js");
|
|
77
|
-
const headClosing = html.indexOf("</head>");
|
|
78
|
-
// hydrate.js should be in <head>
|
|
79
|
-
expect(hydrateScript).toBeGreaterThan(-1);
|
|
80
|
-
expect(hydrateScript).toBeLessThan(headClosing);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("stream chunks arrive incrementally", async () => {
|
|
84
|
-
const matched = makeMatched(
|
|
85
|
-
"/chunked",
|
|
86
|
-
"data:text/javascript,export default (p) => '<div>chunked</div>'",
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const res = await renderRoute(
|
|
90
|
-
matched,
|
|
91
|
-
new Request("http://localhost/chunked"),
|
|
92
|
-
);
|
|
93
|
-
const reader = res.body!.getReader();
|
|
94
|
-
const chunks: string[] = [];
|
|
95
|
-
|
|
96
|
-
while (true) {
|
|
97
|
-
const { done, value } = await reader.read();
|
|
98
|
-
if (done) break;
|
|
99
|
-
// Bun ReadableStream returns Uint8Array
|
|
100
|
-
chunks.push(
|
|
101
|
-
typeof value === "string"
|
|
102
|
-
? value
|
|
103
|
-
: new TextDecoder().decode(value.buffer),
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Should have multiple chunks (at least DOCTYPE + body)
|
|
108
|
-
expect(chunks.length).toBeGreaterThan(1);
|
|
109
|
-
// First chunk should start with DOCTYPE
|
|
110
|
-
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
111
|
-
// Last chunk should close the document
|
|
112
|
-
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test("navigation request returns JSON not stream", async () => {
|
|
116
|
-
const matched = makeMatched(
|
|
117
|
-
"/nav",
|
|
118
|
-
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
const req = new Request("http://localhost/nav", {
|
|
122
|
-
headers: { "x-grimoire-navigate": "1" },
|
|
123
|
-
});
|
|
124
|
-
const res = await renderRoute(matched, req);
|
|
125
|
-
const json = await res.json();
|
|
126
|
-
|
|
127
|
-
expect(json.pattern).toBe("/nav");
|
|
128
|
-
expect(json.data).toBeDefined();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
expect(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
);
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { renderRoute } from "../src/rendering";
|
|
6
|
+
import { matchRoute } from "../src/routing/router";
|
|
7
|
+
import { scanRoutes } from "../src/routing/scanner";
|
|
8
|
+
|
|
9
|
+
function makeMatched(path: string, filePath: string, params = {}) {
|
|
10
|
+
return {
|
|
11
|
+
route: { path, params: {}, filePath },
|
|
12
|
+
params,
|
|
13
|
+
layouts: [],
|
|
14
|
+
layoutServers: [],
|
|
15
|
+
} as any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("Streaming SSR", () => {
|
|
19
|
+
test("returns a ReadableStream response", async () => {
|
|
20
|
+
const matched = makeMatched(
|
|
21
|
+
"/",
|
|
22
|
+
"data:text/javascript,export default (p) => '<div>hello</div>'",
|
|
23
|
+
);
|
|
24
|
+
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
25
|
+
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("stream contains full HTML document", async () => {
|
|
29
|
+
const matched = makeMatched(
|
|
30
|
+
"/test",
|
|
31
|
+
"data:text/javascript,export default (p) => '<p>streaming works</p>'",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const res = await renderRoute(
|
|
35
|
+
matched,
|
|
36
|
+
new Request("http://localhost/test"),
|
|
37
|
+
);
|
|
38
|
+
const html = await res.text();
|
|
39
|
+
|
|
40
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
41
|
+
expect(html).toContain('<meta charset="UTF-8" />');
|
|
42
|
+
expect(html).toContain("streaming works");
|
|
43
|
+
expect(html).toContain("</html>");
|
|
44
|
+
expect(html).toContain("__grimoire_state__");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("stream includes state JSON with route pattern", async () => {
|
|
48
|
+
const matched = makeMatched(
|
|
49
|
+
"/spells/:id",
|
|
50
|
+
"data:text/javascript,export default (p) => '<div>spell</div>'",
|
|
51
|
+
{ id: "fireball" },
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const res = await renderRoute(
|
|
55
|
+
matched,
|
|
56
|
+
new Request("http://localhost/spells/fireball"),
|
|
57
|
+
);
|
|
58
|
+
const html = await res.text();
|
|
59
|
+
|
|
60
|
+
expect(html).toContain('"pattern":"/spells/:id"');
|
|
61
|
+
expect(html).toContain('"id":"fireball"');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("head script tag comes before head content", async () => {
|
|
65
|
+
const matched = makeMatched(
|
|
66
|
+
"/head-test",
|
|
67
|
+
"data:text/javascript,export default (p) => '<div>head</div>'",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const res = await renderRoute(
|
|
71
|
+
matched,
|
|
72
|
+
new Request("http://localhost/head-test"),
|
|
73
|
+
);
|
|
74
|
+
const html = await res.text();
|
|
75
|
+
|
|
76
|
+
const hydrateScript = html.indexOf("hydrate.js");
|
|
77
|
+
const headClosing = html.indexOf("</head>");
|
|
78
|
+
// hydrate.js should be in <head>
|
|
79
|
+
expect(hydrateScript).toBeGreaterThan(-1);
|
|
80
|
+
expect(hydrateScript).toBeLessThan(headClosing);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("stream chunks arrive incrementally", async () => {
|
|
84
|
+
const matched = makeMatched(
|
|
85
|
+
"/chunked",
|
|
86
|
+
"data:text/javascript,export default (p) => '<div>chunked</div>'",
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const res = await renderRoute(
|
|
90
|
+
matched,
|
|
91
|
+
new Request("http://localhost/chunked"),
|
|
92
|
+
);
|
|
93
|
+
const reader = res.body!.getReader();
|
|
94
|
+
const chunks: string[] = [];
|
|
95
|
+
|
|
96
|
+
while (true) {
|
|
97
|
+
const { done, value } = await reader.read();
|
|
98
|
+
if (done) break;
|
|
99
|
+
// Bun ReadableStream returns Uint8Array
|
|
100
|
+
chunks.push(
|
|
101
|
+
typeof value === "string"
|
|
102
|
+
? value
|
|
103
|
+
: new TextDecoder().decode(value.buffer),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Should have multiple chunks (at least DOCTYPE + body)
|
|
108
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
109
|
+
// First chunk should start with DOCTYPE
|
|
110
|
+
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
111
|
+
// Last chunk should close the document
|
|
112
|
+
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("navigation request returns JSON not stream", async () => {
|
|
116
|
+
const matched = makeMatched(
|
|
117
|
+
"/nav",
|
|
118
|
+
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const req = new Request("http://localhost/nav", {
|
|
122
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
123
|
+
});
|
|
124
|
+
const res = await renderRoute(matched, req);
|
|
125
|
+
const json = await res.json();
|
|
126
|
+
|
|
127
|
+
expect(json.pattern).toBe("/nav");
|
|
128
|
+
expect(json.data).toBeDefined();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// The JSON payload and the HTML document share one URL and are picked by a
|
|
132
|
+
// request header. Without Vary any cache may serve the JSON to a plain
|
|
133
|
+
// document request, which rendered the raw state object as the page.
|
|
134
|
+
test("navigation JSON declares Vary on the navigate header", async () => {
|
|
135
|
+
const matched = makeMatched(
|
|
136
|
+
"/nav-vary",
|
|
137
|
+
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const res = await renderRoute(
|
|
141
|
+
matched,
|
|
142
|
+
new Request("http://localhost/nav-vary", {
|
|
143
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
expect(res.headers.get("content-type")).toContain("application/json");
|
|
148
|
+
expect(res.headers.get("vary")?.toLowerCase()).toContain(
|
|
149
|
+
"x-grimoire-navigate",
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("HTML document declares Vary on the navigate header", async () => {
|
|
154
|
+
const matched = makeMatched(
|
|
155
|
+
"/doc-vary",
|
|
156
|
+
"data:text/javascript,export default (p) => '<div>doc</div>'",
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const res = await renderRoute(
|
|
160
|
+
matched,
|
|
161
|
+
new Request("http://localhost/doc-vary"),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(res.headers.get("content-type")).toContain("text/html");
|
|
165
|
+
expect(res.headers.get("vary")?.toLowerCase()).toContain(
|
|
166
|
+
"x-grimoire-navigate",
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("state JSON escapes </script> in data (XSS)", async () => {
|
|
171
|
+
const evil = "</script><script>alert(1)</script>";
|
|
172
|
+
const src = [
|
|
173
|
+
"export const load = async () => ({ evil })",
|
|
174
|
+
"const evil = " + JSON.stringify(evil),
|
|
175
|
+
'export default () => "<div>ok</div>"',
|
|
176
|
+
].join(";\n");
|
|
177
|
+
const matched = makeMatched(
|
|
178
|
+
"/xss",
|
|
179
|
+
`data:text/javascript,${encodeURIComponent(src)}`,
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const res = await renderRoute(matched, new Request("http://localhost/xss"));
|
|
183
|
+
const html = await res.text();
|
|
184
|
+
|
|
185
|
+
const tagStart = html.indexOf('<script id="__grimoire_state__"');
|
|
186
|
+
expect(tagStart).toBeGreaterThan(-1);
|
|
187
|
+
const payloadStart = html.indexOf(">", tagStart) + 1;
|
|
188
|
+
const payloadEnd = html.indexOf("</script>", payloadStart);
|
|
189
|
+
const payload = html.slice(payloadStart, payloadEnd);
|
|
190
|
+
|
|
191
|
+
// no literal closing tag inside the JSON payload
|
|
192
|
+
expect(payload).not.toContain("</script>");
|
|
193
|
+
// `<` escaped as \u003c — JSON.parse decodes it back
|
|
194
|
+
expect(payload).toContain("\\u003c/script");
|
|
195
|
+
const parsed = JSON.parse(payload);
|
|
196
|
+
expect(parsed.data.evil).toBe(evil);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("streamed promises (deferred data)", () => {
|
|
201
|
+
let root: string;
|
|
202
|
+
|
|
203
|
+
beforeAll(async () => {
|
|
204
|
+
root = join(tmpdir(), `grimoire-defer-${Date.now()}`);
|
|
205
|
+
await mkdir(root, { recursive: true });
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
afterAll(async () => {
|
|
209
|
+
await rm(root, { recursive: true });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
async function readChunks(res: Response): Promise<string[]> {
|
|
213
|
+
const reader = res.body!.getReader();
|
|
214
|
+
const chunks: string[] = [];
|
|
215
|
+
while (true) {
|
|
216
|
+
const { done, value } = await reader.read();
|
|
217
|
+
if (done) break;
|
|
218
|
+
chunks.push(String(value));
|
|
219
|
+
}
|
|
220
|
+
return chunks;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function setup(name: string, serverSrc: string): Promise<string[]> {
|
|
224
|
+
// unique dir per test — Bun caches modules by resolved path
|
|
225
|
+
const dir = join(root, name);
|
|
226
|
+
await mkdir(dir, { recursive: true });
|
|
227
|
+
await writeFile(join(dir, "+page.server.ts"), serverSrc);
|
|
228
|
+
await writeFile(
|
|
229
|
+
join(dir, "+page.tsx"),
|
|
230
|
+
"export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}</div>; }",
|
|
231
|
+
);
|
|
232
|
+
const tree = await scanRoutes(dir);
|
|
233
|
+
const matched = matchRoute(tree, new URL("http://localhost/"));
|
|
234
|
+
const res = await renderRoute(matched!, new Request("http://localhost/"));
|
|
235
|
+
return readChunks(res);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
test("slow promise flushes after the document shell", async () => {
|
|
239
|
+
const chunks = await setup(
|
|
240
|
+
"slow",
|
|
241
|
+
[
|
|
242
|
+
"export async function load() {",
|
|
243
|
+
" return {",
|
|
244
|
+
' fast: "now",',
|
|
245
|
+
' slow: new Promise((r) => setTimeout(() => r("late"), 30)),',
|
|
246
|
+
" };",
|
|
247
|
+
"}",
|
|
248
|
+
].join("\n"),
|
|
249
|
+
);
|
|
250
|
+
const all = chunks.join("");
|
|
251
|
+
|
|
252
|
+
// state payload marks the field as deferred
|
|
253
|
+
expect(all).toContain('"__deferred":"gd-0"');
|
|
254
|
+
// the shell flushed before the promise settled
|
|
255
|
+
const shell = chunks.find((c) => c.includes("__grimoire_state__"))!;
|
|
256
|
+
expect(shell).not.toContain('"late"');
|
|
257
|
+
// a later chunk carries the self-contained patch script
|
|
258
|
+
const late = chunks.find((c) => c.includes("grimoire:deferred"));
|
|
259
|
+
expect(late).toBeDefined();
|
|
260
|
+
expect(late).toContain('"late"');
|
|
261
|
+
expect(late).toContain("__grimoire_state__");
|
|
262
|
+
expect(late).toContain('data["slow"]');
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test("rejected promise emits error detail and null value", async () => {
|
|
266
|
+
const all = (
|
|
267
|
+
await setup(
|
|
268
|
+
"reject",
|
|
269
|
+
[
|
|
270
|
+
"export async function load() {",
|
|
271
|
+
" const boom = Promise.reject(new Error('nope'));",
|
|
272
|
+
" boom.catch(() => {});",
|
|
273
|
+
" return { boom };",
|
|
274
|
+
"}",
|
|
275
|
+
].join("\n"),
|
|
276
|
+
)
|
|
277
|
+
).join("");
|
|
278
|
+
expect(all).toContain("error:true");
|
|
279
|
+
expect(all).toContain('data["boom"]');
|
|
280
|
+
});
|
|
281
|
+
});
|