@sigil-dev/grimoire 0.6.8 → 0.6.10
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/rendering/index.ts +151 -152
- package/src/routing/router.ts +72 -68
- package/test/exports.test.ts +0 -1
- package/test/rendering.test.ts +360 -254
- package/test/streaming.test.ts +101 -140
package/test/streaming.test.ts
CHANGED
|
@@ -1,145 +1,106 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
import { renderRoute } from "../src/rendering";
|
|
3
3
|
|
|
4
|
+
function makeMatched(path: string, filePath: string, params = {}) {
|
|
5
|
+
return {
|
|
6
|
+
route: { path, params: {}, filePath },
|
|
7
|
+
params,
|
|
8
|
+
layouts: [],
|
|
9
|
+
layoutServers: [],
|
|
10
|
+
} as any;
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
describe("Streaming SSR", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
params: {},
|
|
98
|
-
} as any;
|
|
99
|
-
|
|
100
|
-
const res = await renderRoute(
|
|
101
|
-
matched,
|
|
102
|
-
new Request("http://localhost/chunked"),
|
|
103
|
-
);
|
|
104
|
-
const reader = res.body!.getReader();
|
|
105
|
-
const chunks: string[] = [];
|
|
106
|
-
|
|
107
|
-
while (true) {
|
|
108
|
-
const { done, value } = await reader.read();
|
|
109
|
-
if (done) break;
|
|
110
|
-
// Bun ReadableStream returns Uint8Array
|
|
111
|
-
chunks.push(
|
|
112
|
-
typeof value === "string"
|
|
113
|
-
? value
|
|
114
|
-
: new TextDecoder().decode(value.buffer),
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Should have multiple chunks (at least DOCTYPE + body)
|
|
119
|
-
expect(chunks.length).toBeGreaterThan(1);
|
|
120
|
-
// First chunk should start with DOCTYPE
|
|
121
|
-
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
122
|
-
// Last chunk should close the document
|
|
123
|
-
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test("navigation request returns JSON not stream", async () => {
|
|
127
|
-
const matched = {
|
|
128
|
-
route: {
|
|
129
|
-
path: "/nav",
|
|
130
|
-
params: {},
|
|
131
|
-
filePath: "data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
132
|
-
},
|
|
133
|
-
params: {},
|
|
134
|
-
} as any;
|
|
135
|
-
|
|
136
|
-
const req = new Request("http://localhost/nav", {
|
|
137
|
-
headers: { "x-grimoire-navigate": "1" },
|
|
138
|
-
});
|
|
139
|
-
const res = await renderRoute(matched, req);
|
|
140
|
-
const json = await res.json();
|
|
141
|
-
|
|
142
|
-
expect(json.pattern).toBe("/nav");
|
|
143
|
-
expect(json.data).toBeDefined();
|
|
144
|
-
});
|
|
14
|
+
test("returns a ReadableStream response", async () => {
|
|
15
|
+
const matched = makeMatched("/", "data:text/javascript,export default (p) => '<div>hello</div>'");
|
|
16
|
+
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
17
|
+
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("stream contains full HTML document", async () => {
|
|
21
|
+
const matched = makeMatched("/test", "data:text/javascript,export default (p) => '<p>streaming works</p>'");
|
|
22
|
+
|
|
23
|
+
const res = await renderRoute(
|
|
24
|
+
matched,
|
|
25
|
+
new Request("http://localhost/test"),
|
|
26
|
+
);
|
|
27
|
+
const html = await res.text();
|
|
28
|
+
|
|
29
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
30
|
+
expect(html).toContain('<meta charset="UTF-8" />');
|
|
31
|
+
expect(html).toContain("streaming works");
|
|
32
|
+
expect(html).toContain("</html>");
|
|
33
|
+
expect(html).toContain("__grimoire_state__");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("stream includes state JSON with route pattern", async () => {
|
|
37
|
+
const matched = makeMatched("/spells/:id", "data:text/javascript,export default (p) => '<div>spell</div>'", { id: "fireball" });
|
|
38
|
+
|
|
39
|
+
const res = await renderRoute(
|
|
40
|
+
matched,
|
|
41
|
+
new Request("http://localhost/spells/fireball"),
|
|
42
|
+
);
|
|
43
|
+
const html = await res.text();
|
|
44
|
+
|
|
45
|
+
expect(html).toContain('"pattern":"/spells/:id"');
|
|
46
|
+
expect(html).toContain('"id":"fireball"');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("head script tag comes before head content", async () => {
|
|
50
|
+
const matched = makeMatched("/head-test", "data:text/javascript,export default (p) => '<div>head</div>'");
|
|
51
|
+
|
|
52
|
+
const res = await renderRoute(
|
|
53
|
+
matched,
|
|
54
|
+
new Request("http://localhost/head-test"),
|
|
55
|
+
);
|
|
56
|
+
const html = await res.text();
|
|
57
|
+
|
|
58
|
+
const hydrateScript = html.indexOf("hydrate.js");
|
|
59
|
+
const headClosing = html.indexOf("</head>");
|
|
60
|
+
// hydrate.js should be in <head>
|
|
61
|
+
expect(hydrateScript).toBeGreaterThan(-1);
|
|
62
|
+
expect(hydrateScript).toBeLessThan(headClosing);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("stream chunks arrive incrementally", async () => {
|
|
66
|
+
const matched = makeMatched("/chunked", "data:text/javascript,export default (p) => '<div>chunked</div>'");
|
|
67
|
+
|
|
68
|
+
const res = await renderRoute(
|
|
69
|
+
matched,
|
|
70
|
+
new Request("http://localhost/chunked"),
|
|
71
|
+
);
|
|
72
|
+
const reader = res.body!.getReader();
|
|
73
|
+
const chunks: string[] = [];
|
|
74
|
+
|
|
75
|
+
while (true) {
|
|
76
|
+
const { done, value } = await reader.read();
|
|
77
|
+
if (done) break;
|
|
78
|
+
// Bun ReadableStream returns Uint8Array
|
|
79
|
+
chunks.push(
|
|
80
|
+
typeof value === "string"
|
|
81
|
+
? value
|
|
82
|
+
: new TextDecoder().decode(value.buffer),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Should have multiple chunks (at least DOCTYPE + body)
|
|
87
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
88
|
+
// First chunk should start with DOCTYPE
|
|
89
|
+
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
90
|
+
// Last chunk should close the document
|
|
91
|
+
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("navigation request returns JSON not stream", async () => {
|
|
95
|
+
const matched = makeMatched("/nav", "data:text/javascript,export default (p) => '<div>nav</div>'");
|
|
96
|
+
|
|
97
|
+
const req = new Request("http://localhost/nav", {
|
|
98
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
99
|
+
});
|
|
100
|
+
const res = await renderRoute(matched, req);
|
|
101
|
+
const json = await res.json();
|
|
102
|
+
|
|
103
|
+
expect(json.pattern).toBe("/nav");
|
|
104
|
+
expect(json.data).toBeDefined();
|
|
105
|
+
});
|
|
145
106
|
});
|