@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
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test a project through its real request pipeline: hooks, plugins, layouts,
|
|
3
|
+
* loads, actions, `+server.ts` handlers and rendering, with no port opened.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { expect, test } from "bun:test";
|
|
7
|
+
* import { createTestApp } from "@sigil-dev/grimoire/testing/server";
|
|
8
|
+
*
|
|
9
|
+
* test("the home page renders", async () => {
|
|
10
|
+
* const app = await createTestApp();
|
|
11
|
+
* const res = await app.get("/");
|
|
12
|
+
* expect(res.status).toBe(200);
|
|
13
|
+
* expect(await res.text()).toContain("<h1>");
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Name these files `*.server.test.ts`: `sigil test` runs them separately from
|
|
18
|
+
* component tests, because the app compiles for the server and a component
|
|
19
|
+
* test compiles for the browser, and a module can only compile once per run.
|
|
20
|
+
*/
|
|
21
|
+
import { existsSync } from "node:fs";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { createApp, type GrimoireApp } from "../server";
|
|
24
|
+
import type { GrimoireConfig } from "../types";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Responses a test may never read. The app waits for each body to finish
|
|
28
|
+
* streaming before it shuts down, and that wait covers the whole process, so
|
|
29
|
+
* `close()` cancels the unread ones, as a client that disconnected would.
|
|
30
|
+
*/
|
|
31
|
+
const sent: Response[] = [];
|
|
32
|
+
|
|
33
|
+
export interface TestApp {
|
|
34
|
+
/** The app underneath, for anything the helpers do not cover. */
|
|
35
|
+
app: GrimoireApp;
|
|
36
|
+
/** Cookies the app has set, sent back on later requests like a browser would. */
|
|
37
|
+
cookies: Map<string, string>;
|
|
38
|
+
fetch(input: string | Request, init?: RequestInit): Promise<Response>;
|
|
39
|
+
get(path: string, init?: RequestInit): Promise<Response>;
|
|
40
|
+
/**
|
|
41
|
+
* Submit a form the way a browser does: fetch the page for its CSRF
|
|
42
|
+
* cookie if there is none yet, then post the fields with the token. Redirects
|
|
43
|
+
* are returned, not followed.
|
|
44
|
+
*/
|
|
45
|
+
submit(
|
|
46
|
+
path: string,
|
|
47
|
+
fields?: Record<string, string>,
|
|
48
|
+
init?: RequestInit,
|
|
49
|
+
): Promise<Response>;
|
|
50
|
+
/** Send JSON to an endpoint. */
|
|
51
|
+
json(path: string, body: unknown, init?: RequestInit): Promise<Response>;
|
|
52
|
+
close(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `overrides` replaces fields of the project's `sigil.config.ts`, which is the
|
|
57
|
+
* way to swap a real database for an in-memory one:
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* const app = await createTestApp({ plugins: [fakeDb] });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* Set the environment the app needs (`env.required` is checked) before calling.
|
|
64
|
+
*/
|
|
65
|
+
export async function createTestApp(
|
|
66
|
+
overrides: Partial<GrimoireConfig> = {},
|
|
67
|
+
options: { root?: string; origin?: string } = {},
|
|
68
|
+
): Promise<TestApp> {
|
|
69
|
+
const root = options.root ?? process.cwd();
|
|
70
|
+
const origin = options.origin ?? "http://localhost";
|
|
71
|
+
const file = join(root, "sigil.config.ts");
|
|
72
|
+
const loaded: GrimoireConfig = existsSync(file)
|
|
73
|
+
? ((await import(file)).default ?? {})
|
|
74
|
+
: {};
|
|
75
|
+
const app = await createApp({
|
|
76
|
+
...loaded,
|
|
77
|
+
...overrides,
|
|
78
|
+
dev: false,
|
|
79
|
+
_forceProduction: true,
|
|
80
|
+
_skipBuild: true,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const cookies = new Map<string, string>();
|
|
84
|
+
const remember = (res: Response) => {
|
|
85
|
+
for (const line of res.headers.getSetCookie()) {
|
|
86
|
+
const [pair = "", ...attrs] = line.split(";");
|
|
87
|
+
const eq = pair.indexOf("=");
|
|
88
|
+
if (eq < 0) continue;
|
|
89
|
+
const name = pair.slice(0, eq).trim();
|
|
90
|
+
const value = decodeURIComponent(pair.slice(eq + 1).trim());
|
|
91
|
+
const gone =
|
|
92
|
+
attrs.some((a) => /^\s*max-age\s*=\s*0\s*$/i.test(a)) || value === "";
|
|
93
|
+
if (gone) cookies.delete(name);
|
|
94
|
+
else cookies.set(name, value);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const send = async (input: string | Request, init?: RequestInit) => {
|
|
99
|
+
const req =
|
|
100
|
+
typeof input === "string"
|
|
101
|
+
? new Request(new URL(input, origin), init)
|
|
102
|
+
: input;
|
|
103
|
+
if (cookies.size && !req.headers.has("cookie")) {
|
|
104
|
+
req.headers.set(
|
|
105
|
+
"cookie",
|
|
106
|
+
[...cookies]
|
|
107
|
+
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
|
108
|
+
.join("; "),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
const res = await app.fetch(req);
|
|
112
|
+
remember(res);
|
|
113
|
+
sent.push(res);
|
|
114
|
+
return res;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
app,
|
|
119
|
+
cookies,
|
|
120
|
+
fetch: send,
|
|
121
|
+
get: (path, init) => send(path, { ...init, method: "GET" }),
|
|
122
|
+
async submit(path, fields = {}, init) {
|
|
123
|
+
if (!cookies.has("_csrf")) {
|
|
124
|
+
// any page that renders a form sets the token cookie; the page the
|
|
125
|
+
// form is on is the one that is certain to
|
|
126
|
+
await send(path.split("?")[0] ?? path);
|
|
127
|
+
}
|
|
128
|
+
const token = cookies.get("_csrf") ?? "";
|
|
129
|
+
return send(path, {
|
|
130
|
+
redirect: "manual",
|
|
131
|
+
...init,
|
|
132
|
+
method: init?.method ?? "POST",
|
|
133
|
+
headers: {
|
|
134
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
135
|
+
...(init?.headers as Record<string, string> | undefined),
|
|
136
|
+
},
|
|
137
|
+
body: new URLSearchParams({ ...fields, _csrf: token }),
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
json: (path, body, init) =>
|
|
141
|
+
send(path, {
|
|
142
|
+
...init,
|
|
143
|
+
method: init?.method ?? "POST",
|
|
144
|
+
headers: {
|
|
145
|
+
"content-type": "application/json",
|
|
146
|
+
...(init?.headers as Record<string, string> | undefined),
|
|
147
|
+
},
|
|
148
|
+
body: JSON.stringify(body),
|
|
149
|
+
}),
|
|
150
|
+
async close() {
|
|
151
|
+
// the drain is process-wide, so cancel everyone's unread bodies, not
|
|
152
|
+
// just this app's
|
|
153
|
+
const unread = sent
|
|
154
|
+
.splice(0)
|
|
155
|
+
.filter((r) => r.body && !r.bodyUsed && !r.body.locked);
|
|
156
|
+
await Promise.all(unread.map((r) => r.body!.cancel().catch(() => {})));
|
|
157
|
+
// bounded: in a process shared with other test files the drain would
|
|
158
|
+
// otherwise wait on their unread responses too
|
|
159
|
+
await app.close("shutdown", { drainMs: 500 });
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|