@sigil-dev/grimoire 0.9.20 → 0.9.22
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 +83 -83
- package/index.ts +50 -48
- package/package.json +16 -5
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +870 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -114
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -745
- package/src/rendering/preload.ts +101 -95
- package/src/rendering/ssrPlugin.ts +108 -108
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -194
- package/src/server/asset-cache.ts +57 -55
- package/src/server/build.ts +262 -249
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -0
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +1138 -1022
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -0
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -113
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -69
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +92 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +798 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +378 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +805 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +131 -131
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/test/deferred.test.ts
CHANGED
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { deferred, onDeferred } from "../src/client/deferred";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* `deferred()` reads a value that is still in flight.
|
|
6
|
-
*
|
|
7
|
-
* The interesting cases are all about timing, so the tests drive the real
|
|
8
|
-
* patch script — the exact string the server appends when a streamed promise
|
|
9
|
-
* settles — against a real event target, rather than dispatching a
|
|
10
|
-
* hand-written event. If the server's script changes shape, these fail,
|
|
11
|
-
* which is the point: the helper and the patch script are a pair and should
|
|
12
|
-
* not be able to drift apart silently.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/** The script from `deferredChunk()` in src/rendering/index.ts. */
|
|
16
|
-
const patchScript = (key: string, value: unknown, error = false) =>
|
|
17
|
-
`<script>(function(){var value=${JSON.stringify(value)};var tag=document.getElementById("__grimoire_state__");if(tag){try{var s=JSON.parse(tag.textContent);s.data[${JSON.stringify(
|
|
18
|
-
key,
|
|
19
|
-
)}]=value;tag.textContent=JSON.stringify(s)}catch(e){}}var w=window.__grimoire_state__;if(!w||w.nodeType)w=window.__grimoire_state__={data:{}};if(!w.data)w.data={};w.data[${JSON.stringify(
|
|
20
|
-
key,
|
|
21
|
-
)}]=value;window.dispatchEvent(new CustomEvent("grimoire:deferred",{detail:{key:${JSON.stringify(
|
|
22
|
-
key,
|
|
23
|
-
)},error:${error ? "true" : "false"}}}))})()</script>`;
|
|
24
|
-
|
|
25
|
-
const listeners = new Map<string, Set<(e: Event) => void>>();
|
|
26
|
-
|
|
27
|
-
/** The `window` the helper sees: enough of one to exercise the real paths. */
|
|
28
|
-
interface FakeWindow {
|
|
29
|
-
__grimoire_state__: unknown;
|
|
30
|
-
addEventListener: (type: string, fn: (e: Event) => void) => void;
|
|
31
|
-
dispatchEvent: (e: Event) => boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const setWindow = (w: FakeWindow | undefined) => {
|
|
35
|
-
(globalThis as unknown as { window?: FakeWindow }).window = w;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const fakeWindow = (state: unknown = { pattern: "/", data: {} }): FakeWindow => {
|
|
39
|
-
const w: FakeWindow = {
|
|
40
|
-
__grimoire_state__: state,
|
|
41
|
-
addEventListener: (type, fn) => {
|
|
42
|
-
if (!listeners.has(type)) listeners.set(type, new Set());
|
|
43
|
-
listeners.get(type)!.add(fn);
|
|
44
|
-
},
|
|
45
|
-
dispatchEvent: (e) => {
|
|
46
|
-
listeners.get(e.type)?.forEach((fn) => fn(e));
|
|
47
|
-
return true;
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
setWindow(w);
|
|
51
|
-
return w;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const fire = (script: string) => {
|
|
55
|
-
// The real script body, run with window in scope. Anything it catches,
|
|
56
|
-
// it catches the same way in a browser.
|
|
57
|
-
const body = script.replace(/^<script>|<\/script>$/g, "");
|
|
58
|
-
const run = new Function("window", "document", body) as (
|
|
59
|
-
w: FakeWindow,
|
|
60
|
-
d: { getElementById: () => null },
|
|
61
|
-
) => void;
|
|
62
|
-
run(fakeWindow(), { getElementById: () => null });
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// The real `window`, captured once. Bun provides one, and other tests in
|
|
66
|
-
// this package read `window.location` — so "no window, as on the server"
|
|
67
|
-
// cannot be modelled by assigning undefined to the global, which breaks
|
|
68
|
-
// every test that runs after this file. The server path is a `typeof window
|
|
69
|
-
// === "undefined"` branch, so it is exercised by deleting the binding
|
|
70
|
-
// temporarily and restoring it immediately, inside the one test that needs
|
|
71
|
-
// it.
|
|
72
|
-
const REAL_WINDOW = (globalThis as { window?: unknown }).window;
|
|
73
|
-
|
|
74
|
-
// Every test leaves a stub window behind, so put the real one back whatever
|
|
75
|
-
// happens. Without this the stub outlives the file and the next test to read
|
|
76
|
-
// `window.location` gets a hangup — which is exactly what happened before.
|
|
77
|
-
afterEach(() => {
|
|
78
|
-
(globalThis as unknown as { window?: unknown }).window = REAL_WINDOW;
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
function reset() {
|
|
82
|
-
listeners.clear();
|
|
83
|
-
(globalThis as unknown as { window?: unknown }).window = REAL_WINDOW;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Run with no `window` at all, then put the real one back. */
|
|
87
|
-
async function withoutWindow<T>(fn: () => T): Promise<T> {
|
|
88
|
-
const g = globalThis as unknown as { window?: unknown };
|
|
89
|
-
const had = Object.prototype.hasOwnProperty.call(g, "window");
|
|
90
|
-
const saved = g.window;
|
|
91
|
-
// `delete` rather than `undefined`: a global that exists with the value
|
|
92
|
-
// undefined is still `typeof window === "object"`, so the helper would
|
|
93
|
-
// take the browser path and crash on the missing addEventListener.
|
|
94
|
-
delete g.window;
|
|
95
|
-
try {
|
|
96
|
-
return fn();
|
|
97
|
-
} finally {
|
|
98
|
-
if (had) g.window = saved;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
describe("deferred()", () => {
|
|
103
|
-
test("an already-resolved value is done immediately", () => {
|
|
104
|
-
reset();
|
|
105
|
-
// The normal case: a client-side navigation resolves the promise
|
|
106
|
-
// before responding, so there is nothing to wait for.
|
|
107
|
-
const d = deferred<string[]>(["a"], []);
|
|
108
|
-
expect(d.done).toBe(true);
|
|
109
|
-
expect(d.value).toEqual(["a"]);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("an undefined value reads as the fallback, not done", () => {
|
|
113
|
-
reset();
|
|
114
|
-
const d = deferred<string[]>(undefined, []);
|
|
115
|
-
expect(d.done).toBe(false);
|
|
116
|
-
expect(d.value).toEqual([]);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test("a late patch script fills the value in", () => {
|
|
120
|
-
reset();
|
|
121
|
-
fakeWindow();
|
|
122
|
-
const d = deferred<string[]>(undefined, []);
|
|
123
|
-
expect(d.done).toBe(false);
|
|
124
|
-
|
|
125
|
-
fire(patchScript("reviews", ["great", "long"]));
|
|
126
|
-
|
|
127
|
-
expect(d.done).toBe(true);
|
|
128
|
-
expect(d.value).toEqual(["great", "long"]);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test("a value that landed before the listener was attached is not missed", () => {
|
|
132
|
-
reset();
|
|
133
|
-
fakeWindow();
|
|
134
|
-
// The realistic race: a value that resolves in ~1ms patches the state
|
|
135
|
-
// before this component gets to wire its listener. Reading the state
|
|
136
|
-
// on the way in is what closes it.
|
|
137
|
-
fakeWindow({ pattern: "/", data: { reviews: ["already here"] } });
|
|
138
|
-
|
|
139
|
-
const d = deferred<string[]>(undefined, []);
|
|
140
|
-
expect(d.done).toBe(true);
|
|
141
|
-
expect(d.value).toEqual(["already here"]);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test("a rejected promise arrives as null, so it reads as still loading", () => {
|
|
145
|
-
reset();
|
|
146
|
-
fakeWindow();
|
|
147
|
-
const d = deferred<string[]>(undefined, []);
|
|
148
|
-
// error:true with a null value is what a rejected promise sends. The
|
|
149
|
-
// helper must not claim to be done with a null it cannot use, and
|
|
150
|
-
// must not throw on it either.
|
|
151
|
-
fire(patchScript("reviews", null, true));
|
|
152
|
-
expect(d.done).toBe(false);
|
|
153
|
-
expect(d.value).toEqual([]);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test("the state element is distinguished from the plain object", () => {
|
|
157
|
-
reset();
|
|
158
|
-
fakeWindow();
|
|
159
|
-
// The runtime's own guard: the element has nodeType, the mirror does
|
|
160
|
-
// not. Reading `.data` off the element would be undefined and the
|
|
161
|
-
// value would be silently missed.
|
|
162
|
-
const carrier = fakeWindow();
|
|
163
|
-
setWindow({
|
|
164
|
-
__grimoire_state__: { nodeType: 1, textContent: "{}" },
|
|
165
|
-
addEventListener: carrier.addEventListener,
|
|
166
|
-
dispatchEvent: () => true,
|
|
167
|
-
});
|
|
168
|
-
const d = deferred<string[]>(undefined, []);
|
|
169
|
-
expect(d.done).toBe(false);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test("with a key, another value settling does not mark this one done", () => {
|
|
173
|
-
reset();
|
|
174
|
-
fakeWindow();
|
|
175
|
-
// Two deferred values on one page. The patch event says that
|
|
176
|
-
// *something* settled, so without a key the first component to read
|
|
177
|
-
// would take the other's data — two pages showing the same list.
|
|
178
|
-
const reviews = deferred<string[]>(undefined, [], "reviews");
|
|
179
|
-
const similar = deferred<string[]>(undefined, [], "similar");
|
|
180
|
-
expect(reviews.done).toBe(false);
|
|
181
|
-
|
|
182
|
-
// A different key settles. This one must not be marked done.
|
|
183
|
-
fire(patchScript("similar", ["other"], false));
|
|
184
|
-
expect(similar.done).toBe(true);
|
|
185
|
-
expect(similar.value).toEqual(["other"]);
|
|
186
|
-
expect(reviews.done).toBe(false);
|
|
187
|
-
expect(reviews.value).toEqual([]);
|
|
188
|
-
|
|
189
|
-
// Now the right one.
|
|
190
|
-
fire(patchScript("reviews", ["mine"], false));
|
|
191
|
-
expect(reviews.done).toBe(true);
|
|
192
|
-
expect(reviews.value).toEqual(["mine"]);
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test("with a key, a value that landed early is found by name", () => {
|
|
196
|
-
reset();
|
|
197
|
-
fakeWindow({
|
|
198
|
-
pattern: "/",
|
|
199
|
-
data: { reviews: ["early"], similar: ["also early"] },
|
|
200
|
-
});
|
|
201
|
-
const reviews = deferred<string[]>(undefined, [], "reviews");
|
|
202
|
-
expect(reviews.done).toBe(true);
|
|
203
|
-
expect(reviews.value).toEqual(["early"]);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
test("onDeferred applies a late value", () => {
|
|
207
|
-
reset();
|
|
208
|
-
fakeWindow();
|
|
209
|
-
// The module-level `$state` shape, which `deferred()` cannot serve
|
|
210
|
-
// because a snapshot does not survive outside a render.
|
|
211
|
-
const seen: string[] = [];
|
|
212
|
-
onDeferred<string>((v) => seen.push(v), "omen");
|
|
213
|
-
expect(seen).toEqual([]);
|
|
214
|
-
|
|
215
|
-
fire(patchScript("omen", "the ink dries", false));
|
|
216
|
-
expect(seen).toEqual(["the ink dries"]);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
test("onDeferred applies immediately when the value already landed", () => {
|
|
220
|
-
reset();
|
|
221
|
-
fakeWindow({ pattern: "/", data: { omen: "already dry" } });
|
|
222
|
-
// Without this, a value that settled before the module ran would
|
|
223
|
-
// never be applied at all.
|
|
224
|
-
const seen: string[] = [];
|
|
225
|
-
onDeferred<string>((v) => seen.push(v), "omen");
|
|
226
|
-
expect(seen).toEqual(["already dry"]);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
test("onDeferred ignores another key settling", () => {
|
|
230
|
-
reset();
|
|
231
|
-
fakeWindow();
|
|
232
|
-
const seen: string[] = [];
|
|
233
|
-
onDeferred<string>((v) => seen.push(v), "omen");
|
|
234
|
-
fire(patchScript("other", "not mine", false));
|
|
235
|
-
expect(seen).toEqual([]);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
test("onDeferred on the server is a no-op", async () => {
|
|
239
|
-
await withoutWindow(() => {
|
|
240
|
-
// Must not throw: a module that calls this runs on the server too.
|
|
241
|
-
const seen: string[] = [];
|
|
242
|
-
onDeferred<string>((v) => seen.push(v), "omen");
|
|
243
|
-
expect(seen).toEqual([]);
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test("no window, as on the server, is not a crash", async () => {
|
|
248
|
-
await withoutWindow(() => {
|
|
249
|
-
// A universal load renders on the server too, where window does
|
|
250
|
-
// not exist and the value is legitimately still in flight.
|
|
251
|
-
const d = deferred<string[]>(undefined, []);
|
|
252
|
-
expect(d.done).toBe(false);
|
|
253
|
-
expect(d.value).toEqual([]);
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
});
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { deferred, onDeferred } from "../src/client/deferred";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `deferred()` reads a value that is still in flight.
|
|
6
|
+
*
|
|
7
|
+
* The interesting cases are all about timing, so the tests drive the real
|
|
8
|
+
* patch script — the exact string the server appends when a streamed promise
|
|
9
|
+
* settles — against a real event target, rather than dispatching a
|
|
10
|
+
* hand-written event. If the server's script changes shape, these fail,
|
|
11
|
+
* which is the point: the helper and the patch script are a pair and should
|
|
12
|
+
* not be able to drift apart silently.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** The script from `deferredChunk()` in src/rendering/index.ts. */
|
|
16
|
+
const patchScript = (key: string, value: unknown, error = false) =>
|
|
17
|
+
`<script>(function(){var value=${JSON.stringify(value)};var tag=document.getElementById("__grimoire_state__");if(tag){try{var s=JSON.parse(tag.textContent);s.data[${JSON.stringify(
|
|
18
|
+
key,
|
|
19
|
+
)}]=value;tag.textContent=JSON.stringify(s)}catch(e){}}var w=window.__grimoire_state__;if(!w||w.nodeType)w=window.__grimoire_state__={data:{}};if(!w.data)w.data={};w.data[${JSON.stringify(
|
|
20
|
+
key,
|
|
21
|
+
)}]=value;window.dispatchEvent(new CustomEvent("grimoire:deferred",{detail:{key:${JSON.stringify(
|
|
22
|
+
key,
|
|
23
|
+
)},error:${error ? "true" : "false"}}}))})()</script>`;
|
|
24
|
+
|
|
25
|
+
const listeners = new Map<string, Set<(e: Event) => void>>();
|
|
26
|
+
|
|
27
|
+
/** The `window` the helper sees: enough of one to exercise the real paths. */
|
|
28
|
+
interface FakeWindow {
|
|
29
|
+
__grimoire_state__: unknown;
|
|
30
|
+
addEventListener: (type: string, fn: (e: Event) => void) => void;
|
|
31
|
+
dispatchEvent: (e: Event) => boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const setWindow = (w: FakeWindow | undefined) => {
|
|
35
|
+
(globalThis as unknown as { window?: FakeWindow }).window = w;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const fakeWindow = (state: unknown = { pattern: "/", data: {} }): FakeWindow => {
|
|
39
|
+
const w: FakeWindow = {
|
|
40
|
+
__grimoire_state__: state,
|
|
41
|
+
addEventListener: (type, fn) => {
|
|
42
|
+
if (!listeners.has(type)) listeners.set(type, new Set());
|
|
43
|
+
listeners.get(type)!.add(fn);
|
|
44
|
+
},
|
|
45
|
+
dispatchEvent: (e) => {
|
|
46
|
+
listeners.get(e.type)?.forEach((fn) => fn(e));
|
|
47
|
+
return true;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
setWindow(w);
|
|
51
|
+
return w;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const fire = (script: string) => {
|
|
55
|
+
// The real script body, run with window in scope. Anything it catches,
|
|
56
|
+
// it catches the same way in a browser.
|
|
57
|
+
const body = script.replace(/^<script>|<\/script>$/g, "");
|
|
58
|
+
const run = new Function("window", "document", body) as (
|
|
59
|
+
w: FakeWindow,
|
|
60
|
+
d: { getElementById: () => null },
|
|
61
|
+
) => void;
|
|
62
|
+
run(fakeWindow(), { getElementById: () => null });
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// The real `window`, captured once. Bun provides one, and other tests in
|
|
66
|
+
// this package read `window.location` — so "no window, as on the server"
|
|
67
|
+
// cannot be modelled by assigning undefined to the global, which breaks
|
|
68
|
+
// every test that runs after this file. The server path is a `typeof window
|
|
69
|
+
// === "undefined"` branch, so it is exercised by deleting the binding
|
|
70
|
+
// temporarily and restoring it immediately, inside the one test that needs
|
|
71
|
+
// it.
|
|
72
|
+
const REAL_WINDOW = (globalThis as { window?: unknown }).window;
|
|
73
|
+
|
|
74
|
+
// Every test leaves a stub window behind, so put the real one back whatever
|
|
75
|
+
// happens. Without this the stub outlives the file and the next test to read
|
|
76
|
+
// `window.location` gets a hangup — which is exactly what happened before.
|
|
77
|
+
afterEach(() => {
|
|
78
|
+
(globalThis as unknown as { window?: unknown }).window = REAL_WINDOW;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function reset() {
|
|
82
|
+
listeners.clear();
|
|
83
|
+
(globalThis as unknown as { window?: unknown }).window = REAL_WINDOW;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Run with no `window` at all, then put the real one back. */
|
|
87
|
+
async function withoutWindow<T>(fn: () => T): Promise<T> {
|
|
88
|
+
const g = globalThis as unknown as { window?: unknown };
|
|
89
|
+
const had = Object.prototype.hasOwnProperty.call(g, "window");
|
|
90
|
+
const saved = g.window;
|
|
91
|
+
// `delete` rather than `undefined`: a global that exists with the value
|
|
92
|
+
// undefined is still `typeof window === "object"`, so the helper would
|
|
93
|
+
// take the browser path and crash on the missing addEventListener.
|
|
94
|
+
delete g.window;
|
|
95
|
+
try {
|
|
96
|
+
return fn();
|
|
97
|
+
} finally {
|
|
98
|
+
if (had) g.window = saved;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
describe("deferred()", () => {
|
|
103
|
+
test("an already-resolved value is done immediately", () => {
|
|
104
|
+
reset();
|
|
105
|
+
// The normal case: a client-side navigation resolves the promise
|
|
106
|
+
// before responding, so there is nothing to wait for.
|
|
107
|
+
const d = deferred<string[]>(["a"], []);
|
|
108
|
+
expect(d.done).toBe(true);
|
|
109
|
+
expect(d.value).toEqual(["a"]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("an undefined value reads as the fallback, not done", () => {
|
|
113
|
+
reset();
|
|
114
|
+
const d = deferred<string[]>(undefined, []);
|
|
115
|
+
expect(d.done).toBe(false);
|
|
116
|
+
expect(d.value).toEqual([]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("a late patch script fills the value in", () => {
|
|
120
|
+
reset();
|
|
121
|
+
fakeWindow();
|
|
122
|
+
const d = deferred<string[]>(undefined, []);
|
|
123
|
+
expect(d.done).toBe(false);
|
|
124
|
+
|
|
125
|
+
fire(patchScript("reviews", ["great", "long"]));
|
|
126
|
+
|
|
127
|
+
expect(d.done).toBe(true);
|
|
128
|
+
expect(d.value).toEqual(["great", "long"]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("a value that landed before the listener was attached is not missed", () => {
|
|
132
|
+
reset();
|
|
133
|
+
fakeWindow();
|
|
134
|
+
// The realistic race: a value that resolves in ~1ms patches the state
|
|
135
|
+
// before this component gets to wire its listener. Reading the state
|
|
136
|
+
// on the way in is what closes it.
|
|
137
|
+
fakeWindow({ pattern: "/", data: { reviews: ["already here"] } });
|
|
138
|
+
|
|
139
|
+
const d = deferred<string[]>(undefined, []);
|
|
140
|
+
expect(d.done).toBe(true);
|
|
141
|
+
expect(d.value).toEqual(["already here"]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a rejected promise arrives as null, so it reads as still loading", () => {
|
|
145
|
+
reset();
|
|
146
|
+
fakeWindow();
|
|
147
|
+
const d = deferred<string[]>(undefined, []);
|
|
148
|
+
// error:true with a null value is what a rejected promise sends. The
|
|
149
|
+
// helper must not claim to be done with a null it cannot use, and
|
|
150
|
+
// must not throw on it either.
|
|
151
|
+
fire(patchScript("reviews", null, true));
|
|
152
|
+
expect(d.done).toBe(false);
|
|
153
|
+
expect(d.value).toEqual([]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("the state element is distinguished from the plain object", () => {
|
|
157
|
+
reset();
|
|
158
|
+
fakeWindow();
|
|
159
|
+
// The runtime's own guard: the element has nodeType, the mirror does
|
|
160
|
+
// not. Reading `.data` off the element would be undefined and the
|
|
161
|
+
// value would be silently missed.
|
|
162
|
+
const carrier = fakeWindow();
|
|
163
|
+
setWindow({
|
|
164
|
+
__grimoire_state__: { nodeType: 1, textContent: "{}" },
|
|
165
|
+
addEventListener: carrier.addEventListener,
|
|
166
|
+
dispatchEvent: () => true,
|
|
167
|
+
});
|
|
168
|
+
const d = deferred<string[]>(undefined, []);
|
|
169
|
+
expect(d.done).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("with a key, another value settling does not mark this one done", () => {
|
|
173
|
+
reset();
|
|
174
|
+
fakeWindow();
|
|
175
|
+
// Two deferred values on one page. The patch event says that
|
|
176
|
+
// *something* settled, so without a key the first component to read
|
|
177
|
+
// would take the other's data — two pages showing the same list.
|
|
178
|
+
const reviews = deferred<string[]>(undefined, [], "reviews");
|
|
179
|
+
const similar = deferred<string[]>(undefined, [], "similar");
|
|
180
|
+
expect(reviews.done).toBe(false);
|
|
181
|
+
|
|
182
|
+
// A different key settles. This one must not be marked done.
|
|
183
|
+
fire(patchScript("similar", ["other"], false));
|
|
184
|
+
expect(similar.done).toBe(true);
|
|
185
|
+
expect(similar.value).toEqual(["other"]);
|
|
186
|
+
expect(reviews.done).toBe(false);
|
|
187
|
+
expect(reviews.value).toEqual([]);
|
|
188
|
+
|
|
189
|
+
// Now the right one.
|
|
190
|
+
fire(patchScript("reviews", ["mine"], false));
|
|
191
|
+
expect(reviews.done).toBe(true);
|
|
192
|
+
expect(reviews.value).toEqual(["mine"]);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("with a key, a value that landed early is found by name", () => {
|
|
196
|
+
reset();
|
|
197
|
+
fakeWindow({
|
|
198
|
+
pattern: "/",
|
|
199
|
+
data: { reviews: ["early"], similar: ["also early"] },
|
|
200
|
+
});
|
|
201
|
+
const reviews = deferred<string[]>(undefined, [], "reviews");
|
|
202
|
+
expect(reviews.done).toBe(true);
|
|
203
|
+
expect(reviews.value).toEqual(["early"]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("onDeferred applies a late value", () => {
|
|
207
|
+
reset();
|
|
208
|
+
fakeWindow();
|
|
209
|
+
// The module-level `$state` shape, which `deferred()` cannot serve
|
|
210
|
+
// because a snapshot does not survive outside a render.
|
|
211
|
+
const seen: string[] = [];
|
|
212
|
+
onDeferred<string>((v) => seen.push(v), "omen");
|
|
213
|
+
expect(seen).toEqual([]);
|
|
214
|
+
|
|
215
|
+
fire(patchScript("omen", "the ink dries", false));
|
|
216
|
+
expect(seen).toEqual(["the ink dries"]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("onDeferred applies immediately when the value already landed", () => {
|
|
220
|
+
reset();
|
|
221
|
+
fakeWindow({ pattern: "/", data: { omen: "already dry" } });
|
|
222
|
+
// Without this, a value that settled before the module ran would
|
|
223
|
+
// never be applied at all.
|
|
224
|
+
const seen: string[] = [];
|
|
225
|
+
onDeferred<string>((v) => seen.push(v), "omen");
|
|
226
|
+
expect(seen).toEqual(["already dry"]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("onDeferred ignores another key settling", () => {
|
|
230
|
+
reset();
|
|
231
|
+
fakeWindow();
|
|
232
|
+
const seen: string[] = [];
|
|
233
|
+
onDeferred<string>((v) => seen.push(v), "omen");
|
|
234
|
+
fire(patchScript("other", "not mine", false));
|
|
235
|
+
expect(seen).toEqual([]);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("onDeferred on the server is a no-op", async () => {
|
|
239
|
+
await withoutWindow(() => {
|
|
240
|
+
// Must not throw: a module that calls this runs on the server too.
|
|
241
|
+
const seen: string[] = [];
|
|
242
|
+
onDeferred<string>((v) => seen.push(v), "omen");
|
|
243
|
+
expect(seen).toEqual([]);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("no window, as on the server, is not a crash", async () => {
|
|
248
|
+
await withoutWindow(() => {
|
|
249
|
+
// A universal load renders on the server too, where window does
|
|
250
|
+
// not exist and the value is legitimately still in flight.
|
|
251
|
+
const d = deferred<string[]>(undefined, []);
|
|
252
|
+
expect(d.done).toBe(false);
|
|
253
|
+
expect(d.value).toEqual([]);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
package/test/dev-loader.test.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { afterAll, expect, test } from "bun:test";
|
|
2
|
-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { DevGraph } from "../src/dev/graph";
|
|
6
|
-
import { makeDevLoader } from "../src/dev/loader";
|
|
7
|
-
|
|
8
|
-
const root = mkdtempSync(join(tmpdir(), "grimoire-dev-loader-"));
|
|
9
|
-
afterAll(() => rmSync(root, { recursive: true, force: true }));
|
|
10
|
-
|
|
11
|
-
test("a component with a scoped <style> block renders server-side", async () => {
|
|
12
|
-
const file = join(root, "Card.tsx");
|
|
13
|
-
// the tag is split so this test file's own preload doesn't strip it
|
|
14
|
-
const tag = (name: string) => `<${name}>`;
|
|
15
|
-
writeFileSync(
|
|
16
|
-
file,
|
|
17
|
-
`export const Card = ({ title }) => <div class="card">{title}</div>;
|
|
18
|
-
|
|
19
|
-
${tag("style")}
|
|
20
|
-
.card { padding: 1rem; }
|
|
21
|
-
.card .title { text-decoration: line-through; color: #888; }
|
|
22
|
-
${tag("/style")}
|
|
23
|
-
`,
|
|
24
|
-
);
|
|
25
|
-
const load = makeDevLoader(new DevGraph(), root);
|
|
26
|
-
const mod = await load(file);
|
|
27
|
-
const html = String(mod.Card({ title: "hi" }));
|
|
28
|
-
expect(html).toContain(">hi<");
|
|
29
|
-
expect(html).toMatch(/class="card s-?[a-z0-9]+"|class="card [a-z0-9-]+"/);
|
|
30
|
-
});
|
|
1
|
+
import { afterAll, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DevGraph } from "../src/dev/graph";
|
|
6
|
+
import { makeDevLoader } from "../src/dev/loader";
|
|
7
|
+
|
|
8
|
+
const root = mkdtempSync(join(tmpdir(), "grimoire-dev-loader-"));
|
|
9
|
+
afterAll(() => rmSync(root, { recursive: true, force: true }));
|
|
10
|
+
|
|
11
|
+
test("a component with a scoped <style> block renders server-side", async () => {
|
|
12
|
+
const file = join(root, "Card.tsx");
|
|
13
|
+
// the tag is split so this test file's own preload doesn't strip it
|
|
14
|
+
const tag = (name: string) => `<${name}>`;
|
|
15
|
+
writeFileSync(
|
|
16
|
+
file,
|
|
17
|
+
`export const Card = ({ title }) => <div class="card">{title}</div>;
|
|
18
|
+
|
|
19
|
+
${tag("style")}
|
|
20
|
+
.card { padding: 1rem; }
|
|
21
|
+
.card .title { text-decoration: line-through; color: #888; }
|
|
22
|
+
${tag("/style")}
|
|
23
|
+
`,
|
|
24
|
+
);
|
|
25
|
+
const load = makeDevLoader(new DevGraph(), root);
|
|
26
|
+
const mod = await load(file);
|
|
27
|
+
const html = String(mod.Card({ title: "hi" }));
|
|
28
|
+
expect(html).toContain(">hi<");
|
|
29
|
+
expect(html).toMatch(/class="card s-?[a-z0-9]+"|class="card [a-z0-9-]+"/);
|
|
30
|
+
});
|