@sigil-dev/grimoire 0.9.29 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- 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 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/test/env.test.ts
CHANGED
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
// packages/grimoire/src/env/env.test.ts
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
3
|
-
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { envModuleSource } from "../src/env/modules.ts";
|
|
7
|
-
import { resolverPlugin, rewriteAliases } from "../src/server/resolver.ts";
|
|
8
|
-
|
|
9
|
-
describe("$env/dynamic/public", () => {
|
|
10
|
-
let orig: Record<string, string | undefined> = {};
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
orig = {
|
|
14
|
-
PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
|
|
15
|
-
PUBLIC_API_URL: process.env.PUBLIC_API_URL,
|
|
16
|
-
SECRET_KEY: process.env.SECRET_KEY,
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
afterEach(() => {
|
|
21
|
-
for (const [k, v] of Object.entries(orig)) {
|
|
22
|
-
if (v === undefined) delete process.env[k];
|
|
23
|
-
else process.env[k] = v;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("reads PUBLIC_ vars at call time", async () => {
|
|
28
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
29
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
30
|
-
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("reflects changes after import", async () => {
|
|
34
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
35
|
-
process.env.PUBLIC_APP_NAME = "changed";
|
|
36
|
-
expect(env.PUBLIC_APP_NAME).toBe("changed");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("does not expose non-PUBLIC_ vars", async () => {
|
|
40
|
-
process.env.SECRET_KEY = "supersecret";
|
|
41
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
42
|
-
expect(env.SECRET_KEY).toBeUndefined();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("returns empty string for missing PUBLIC_ key", async () => {
|
|
46
|
-
delete process.env.PUBLIC_API_URL;
|
|
47
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
48
|
-
expect(env.PUBLIC_API_URL).toBe("");
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("ownKeys only includes PUBLIC_ vars", async () => {
|
|
52
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
53
|
-
process.env.SECRET_KEY = "secret";
|
|
54
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
55
|
-
const keys = Object.keys(env);
|
|
56
|
-
expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe("$env/dynamic/private", () => {
|
|
61
|
-
let orig: string | undefined;
|
|
62
|
-
|
|
63
|
-
beforeEach(() => {
|
|
64
|
-
orig = process.env.DATABASE_URL;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
afterEach(() => {
|
|
68
|
-
if (orig === undefined) delete process.env.DATABASE_URL;
|
|
69
|
-
else process.env.DATABASE_URL = orig;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("reads any env var at call time", async () => {
|
|
73
|
-
process.env.DATABASE_URL = "postgres://localhost/test";
|
|
74
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
75
|
-
expect(env.DATABASE_URL).toBe("postgres://localhost/test");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("reflects changes after import", async () => {
|
|
79
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
80
|
-
process.env.DATABASE_URL = "postgres://localhost/changed";
|
|
81
|
-
expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("returns empty string for missing key", async () => {
|
|
85
|
-
delete process.env.DATABASE_URL;
|
|
86
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
87
|
-
expect(env.DATABASE_URL).toBe("");
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("exposes PUBLIC_ vars too", async () => {
|
|
91
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
92
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
93
|
-
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe("$env modules in builds", () => {
|
|
98
|
-
test("static modules export each variable by name, and env", () => {
|
|
99
|
-
process.env.PUBLIC_SITE = "docs";
|
|
100
|
-
const src: string = envModuleSource("$env/static/public", "browser");
|
|
101
|
-
expect(src).toContain('export const PUBLIC_SITE = env["PUBLIC_SITE"];');
|
|
102
|
-
expect(src).not.toContain("SECRET");
|
|
103
|
-
delete process.env.PUBLIC_SITE;
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("private modules can't be built for the browser", () => {
|
|
107
|
-
for (const kind of ["static", "dynamic"]) {
|
|
108
|
-
expect(() => envModuleSource(`$env/${kind}/private`, "browser")).toThrow(
|
|
109
|
-
/server secrets/,
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test("a browser build resolves $env and $lib, and inlines no secrets", async () => {
|
|
115
|
-
const dir = join(tmpdir(), `grimoire-env-build-${Date.now()}`);
|
|
116
|
-
await mkdir(join(dir, "src/lib"), { recursive: true });
|
|
117
|
-
await writeFile(
|
|
118
|
-
join(dir, "src/lib/greet.ts"),
|
|
119
|
-
'export const greet = (s: string) => "hi " + s;',
|
|
120
|
-
);
|
|
121
|
-
await writeFile(
|
|
122
|
-
join(dir, "entry.ts"),
|
|
123
|
-
[
|
|
124
|
-
'import { PUBLIC_WHO } from "$env/static/public";',
|
|
125
|
-
'import { env } from "$env/dynamic/public";',
|
|
126
|
-
'import { greet } from "$lib/greet";',
|
|
127
|
-
"console.log(greet(PUBLIC_WHO), env.PUBLIC_WHO);",
|
|
128
|
-
].join("\n"),
|
|
129
|
-
);
|
|
130
|
-
process.env.PUBLIC_WHO = "world";
|
|
131
|
-
process.env.SECRET_WHO = "hunter2";
|
|
132
|
-
const cwd = process.cwd();
|
|
133
|
-
process.chdir(dir);
|
|
134
|
-
try {
|
|
135
|
-
const result = await Bun.build({
|
|
136
|
-
entrypoints: [join(dir, "entry.ts")],
|
|
137
|
-
plugins: [resolverPlugin({}, "browser")],
|
|
138
|
-
throw: false,
|
|
139
|
-
});
|
|
140
|
-
expect(result.success).toBe(true);
|
|
141
|
-
const code = await result.outputs[0]!.text();
|
|
142
|
-
expect(code).toContain('"world"');
|
|
143
|
-
expect(code).toContain('"hi "');
|
|
144
|
-
expect(code).not.toContain("hunter2");
|
|
145
|
-
} finally {
|
|
146
|
-
process.chdir(cwd);
|
|
147
|
-
delete process.env.PUBLIC_WHO;
|
|
148
|
-
delete process.env.SECRET_WHO;
|
|
149
|
-
await rm(dir, { recursive: true, force: true });
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test("server modules get aliased imports rewritten to files", async () => {
|
|
154
|
-
const dir = join(tmpdir(), `grimoire-alias-${Date.now()}`);
|
|
155
|
-
await mkdir(join(dir, "lib"), { recursive: true });
|
|
156
|
-
await writeFile(join(dir, "lib/db.ts"), "export const db = 1;");
|
|
157
|
-
try {
|
|
158
|
-
const out = rewriteAliases(
|
|
159
|
-
'import { db } from "$lib/db";\nimport x from "other";',
|
|
160
|
-
{ $lib: join(dir, "lib") },
|
|
161
|
-
);
|
|
162
|
-
// forward slashes even on Windows: the path lands in a string literal
|
|
163
|
-
expect(out).toContain(
|
|
164
|
-
`from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
|
|
165
|
-
);
|
|
166
|
-
expect(out).toContain('from "other"');
|
|
167
|
-
} finally {
|
|
168
|
-
await rm(dir, { recursive: true, force: true });
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
});
|
|
1
|
+
// packages/grimoire/src/env/env.test.ts
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
3
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { envModuleSource } from "../src/env/modules.ts";
|
|
7
|
+
import { resolverPlugin, rewriteAliases } from "../src/server/resolver.ts";
|
|
8
|
+
|
|
9
|
+
describe("$env/dynamic/public", () => {
|
|
10
|
+
let orig: Record<string, string | undefined> = {};
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
orig = {
|
|
14
|
+
PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
|
|
15
|
+
PUBLIC_API_URL: process.env.PUBLIC_API_URL,
|
|
16
|
+
SECRET_KEY: process.env.SECRET_KEY,
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
for (const [k, v] of Object.entries(orig)) {
|
|
22
|
+
if (v === undefined) delete process.env[k];
|
|
23
|
+
else process.env[k] = v;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("reads PUBLIC_ vars at call time", async () => {
|
|
28
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
29
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
30
|
+
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("reflects changes after import", async () => {
|
|
34
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
35
|
+
process.env.PUBLIC_APP_NAME = "changed";
|
|
36
|
+
expect(env.PUBLIC_APP_NAME).toBe("changed");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("does not expose non-PUBLIC_ vars", async () => {
|
|
40
|
+
process.env.SECRET_KEY = "supersecret";
|
|
41
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
42
|
+
expect(env.SECRET_KEY).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("returns empty string for missing PUBLIC_ key", async () => {
|
|
46
|
+
delete process.env.PUBLIC_API_URL;
|
|
47
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
48
|
+
expect(env.PUBLIC_API_URL).toBe("");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("ownKeys only includes PUBLIC_ vars", async () => {
|
|
52
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
53
|
+
process.env.SECRET_KEY = "secret";
|
|
54
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
55
|
+
const keys = Object.keys(env);
|
|
56
|
+
expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("$env/dynamic/private", () => {
|
|
61
|
+
let orig: string | undefined;
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
orig = process.env.DATABASE_URL;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
afterEach(() => {
|
|
68
|
+
if (orig === undefined) delete process.env.DATABASE_URL;
|
|
69
|
+
else process.env.DATABASE_URL = orig;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("reads any env var at call time", async () => {
|
|
73
|
+
process.env.DATABASE_URL = "postgres://localhost/test";
|
|
74
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
75
|
+
expect(env.DATABASE_URL).toBe("postgres://localhost/test");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("reflects changes after import", async () => {
|
|
79
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
80
|
+
process.env.DATABASE_URL = "postgres://localhost/changed";
|
|
81
|
+
expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("returns empty string for missing key", async () => {
|
|
85
|
+
delete process.env.DATABASE_URL;
|
|
86
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
87
|
+
expect(env.DATABASE_URL).toBe("");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("exposes PUBLIC_ vars too", async () => {
|
|
91
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
92
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
93
|
+
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("$env modules in builds", () => {
|
|
98
|
+
test("static modules export each variable by name, and env", () => {
|
|
99
|
+
process.env.PUBLIC_SITE = "docs";
|
|
100
|
+
const src: string = envModuleSource("$env/static/public", "browser");
|
|
101
|
+
expect(src).toContain('export const PUBLIC_SITE = env["PUBLIC_SITE"];');
|
|
102
|
+
expect(src).not.toContain("SECRET");
|
|
103
|
+
delete process.env.PUBLIC_SITE;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("private modules can't be built for the browser", () => {
|
|
107
|
+
for (const kind of ["static", "dynamic"]) {
|
|
108
|
+
expect(() => envModuleSource(`$env/${kind}/private`, "browser")).toThrow(
|
|
109
|
+
/server secrets/,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("a browser build resolves $env and $lib, and inlines no secrets", async () => {
|
|
115
|
+
const dir = join(tmpdir(), `grimoire-env-build-${Date.now()}`);
|
|
116
|
+
await mkdir(join(dir, "src/lib"), { recursive: true });
|
|
117
|
+
await writeFile(
|
|
118
|
+
join(dir, "src/lib/greet.ts"),
|
|
119
|
+
'export const greet = (s: string) => "hi " + s;',
|
|
120
|
+
);
|
|
121
|
+
await writeFile(
|
|
122
|
+
join(dir, "entry.ts"),
|
|
123
|
+
[
|
|
124
|
+
'import { PUBLIC_WHO } from "$env/static/public";',
|
|
125
|
+
'import { env } from "$env/dynamic/public";',
|
|
126
|
+
'import { greet } from "$lib/greet";',
|
|
127
|
+
"console.log(greet(PUBLIC_WHO), env.PUBLIC_WHO);",
|
|
128
|
+
].join("\n"),
|
|
129
|
+
);
|
|
130
|
+
process.env.PUBLIC_WHO = "world";
|
|
131
|
+
process.env.SECRET_WHO = "hunter2";
|
|
132
|
+
const cwd = process.cwd();
|
|
133
|
+
process.chdir(dir);
|
|
134
|
+
try {
|
|
135
|
+
const result = await Bun.build({
|
|
136
|
+
entrypoints: [join(dir, "entry.ts")],
|
|
137
|
+
plugins: [resolverPlugin({}, "browser")],
|
|
138
|
+
throw: false,
|
|
139
|
+
});
|
|
140
|
+
expect(result.success).toBe(true);
|
|
141
|
+
const code = await result.outputs[0]!.text();
|
|
142
|
+
expect(code).toContain('"world"');
|
|
143
|
+
expect(code).toContain('"hi "');
|
|
144
|
+
expect(code).not.toContain("hunter2");
|
|
145
|
+
} finally {
|
|
146
|
+
process.chdir(cwd);
|
|
147
|
+
delete process.env.PUBLIC_WHO;
|
|
148
|
+
delete process.env.SECRET_WHO;
|
|
149
|
+
await rm(dir, { recursive: true, force: true });
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("server modules get aliased imports rewritten to files", async () => {
|
|
154
|
+
const dir = join(tmpdir(), `grimoire-alias-${Date.now()}`);
|
|
155
|
+
await mkdir(join(dir, "lib"), { recursive: true });
|
|
156
|
+
await writeFile(join(dir, "lib/db.ts"), "export const db = 1;");
|
|
157
|
+
try {
|
|
158
|
+
const out = rewriteAliases(
|
|
159
|
+
'import { db } from "$lib/db";\nimport x from "other";',
|
|
160
|
+
{ $lib: join(dir, "lib") },
|
|
161
|
+
);
|
|
162
|
+
// forward slashes even on Windows: the path lands in a string literal
|
|
163
|
+
expect(out).toContain(
|
|
164
|
+
`from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
|
|
165
|
+
);
|
|
166
|
+
expect(out).toContain('from "other"');
|
|
167
|
+
} finally {
|
|
168
|
+
await rm(dir, { recursive: true, force: true });
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
});
|
|
@@ -18,7 +18,10 @@ let dir: string;
|
|
|
18
18
|
beforeAll(async () => {
|
|
19
19
|
dir = join(tmpdir(), `grimoire-force-prod-${Date.now()}`);
|
|
20
20
|
await mkdir(join(dir, "ping"), { recursive: true });
|
|
21
|
-
await writeFile(
|
|
21
|
+
await writeFile(
|
|
22
|
+
join(dir, "ping", "+server.ts"),
|
|
23
|
+
"export const GET = () => new Response('pong');",
|
|
24
|
+
);
|
|
22
25
|
});
|
|
23
26
|
|
|
24
27
|
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
@@ -38,7 +41,9 @@ describe("_forceProduction", () => {
|
|
|
38
41
|
});
|
|
39
42
|
expect(app.config.dev).toBe(false);
|
|
40
43
|
// the probe is the HMR client script: harmless, and only served in dev
|
|
41
|
-
const res = await app.fetch(
|
|
44
|
+
const res = await app.fetch(
|
|
45
|
+
new Request("http://localhost/__grimoire__/hmr-client.js"),
|
|
46
|
+
);
|
|
42
47
|
expect(res.status).toBe(404);
|
|
43
48
|
});
|
|
44
49
|
});
|
package/test/images.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { EAGER_IMAGE_COUNT, optimizeImages } from "../src/rendering/images.ts";
|
|
3
3
|
|
|
4
4
|
const imgs = (n: number, attrs = "") =>
|
|
5
5
|
Array.from({ length: n }, (_, i) => `<img src="/${i}.png"${attrs}>`).join("");
|
|
@@ -11,30 +11,42 @@ describe("optimizeImages", () => {
|
|
|
11
11
|
for (const t of tags.slice(0, EAGER_IMAGE_COUNT))
|
|
12
12
|
expect(t).not.toContain("loading");
|
|
13
13
|
for (const t of tags.slice(EAGER_IMAGE_COUNT))
|
|
14
|
-
expect(t).toBe(
|
|
14
|
+
expect(t).toBe(
|
|
15
|
+
`<img src="/${tags.indexOf(t)}.png" loading="lazy" decoding="async">`,
|
|
16
|
+
);
|
|
15
17
|
});
|
|
16
18
|
|
|
17
19
|
test("respects the author's loading and decoding", () => {
|
|
18
20
|
const out = optimizeImages(
|
|
19
|
-
imgs(EAGER_IMAGE_COUNT) +
|
|
21
|
+
imgs(EAGER_IMAGE_COUNT) +
|
|
22
|
+
`<img src="/x.png" loading="eager" decoding="sync" />`,
|
|
20
23
|
);
|
|
21
|
-
expect(
|
|
24
|
+
expect(
|
|
25
|
+
out.endsWith(`<img src="/x.png" loading="eager" decoding="sync" />`),
|
|
26
|
+
).toBe(true);
|
|
22
27
|
});
|
|
23
28
|
|
|
24
29
|
test("handles self-closing tags and never touches script strings", () => {
|
|
25
30
|
const script = `<script>var s='<img src="q.png">'</script>`;
|
|
26
|
-
const out = optimizeImages(
|
|
31
|
+
const out = optimizeImages(
|
|
32
|
+
imgs(EAGER_IMAGE_COUNT) + script + `<img src="/y.png" />`,
|
|
33
|
+
);
|
|
27
34
|
expect(out).toContain(script);
|
|
28
|
-
expect(out).toContain(
|
|
35
|
+
expect(out).toContain(
|
|
36
|
+
`<img src="/y.png" loading="lazy" decoding="async" />`,
|
|
37
|
+
);
|
|
29
38
|
});
|
|
30
39
|
|
|
31
40
|
test("a priority image is fetched first and does not use up the eager budget", () => {
|
|
32
41
|
const hero = `<img src="/hero.png" data-sigil-priority>`;
|
|
33
42
|
const out = optimizeImages(hero + imgs(EAGER_IMAGE_COUNT + 1));
|
|
34
43
|
const tags = out.match(/<img[^>]*>/g)!;
|
|
35
|
-
expect(tags[0]).toBe(
|
|
44
|
+
expect(tags[0]).toBe(
|
|
45
|
+
`<img src="/hero.png" data-sigil-priority fetchpriority="high">`,
|
|
46
|
+
);
|
|
36
47
|
// the four after it are still eager, the fifth is deferred
|
|
37
|
-
for (const t of tags.slice(1, 1 + EAGER_IMAGE_COUNT))
|
|
48
|
+
for (const t of tags.slice(1, 1 + EAGER_IMAGE_COUNT))
|
|
49
|
+
expect(t).not.toContain("loading");
|
|
38
50
|
expect(tags.at(-1)).toContain('loading="lazy"');
|
|
39
51
|
});
|
|
40
52
|
|
|
@@ -1,61 +1,61 @@
|
|
|
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 { createApp } from "../src/server";
|
|
6
|
-
import type { GrimoirePlugin } from "../src/types";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* `X-Grimoire-Locals` is how the coordinator tells a worker who the user is.
|
|
10
|
-
* A server that answers the public directly has no coordinator, so the header
|
|
11
|
-
* is just something a client typed: honouring it would let anyone choose their
|
|
12
|
-
* own `locals`, including a session.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
let dir: string;
|
|
16
|
-
|
|
17
|
-
beforeAll(async () => {
|
|
18
|
-
dir = join(tmpdir(), `grimoire-locals-${Date.now()}`);
|
|
19
|
-
await mkdir(join(dir, "who"), { recursive: true });
|
|
20
|
-
await writeFile(
|
|
21
|
-
join(dir, "who", "+server.ts"),
|
|
22
|
-
"export function GET({ locals }) { return Response.json({ user: locals.user ?? null }); }",
|
|
23
|
-
);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
27
|
-
|
|
28
|
-
// What a signing plugin would do once it has verified the coordinator's mark.
|
|
29
|
-
const trusting: GrimoirePlugin = {
|
|
30
|
-
name: "trusting",
|
|
31
|
-
deserializeLocals: (raw) => ({ user: raw }) as unknown as App.Locals,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
async function whoAmI(
|
|
35
|
-
behindCoordinator: boolean | undefined,
|
|
36
|
-
): Promise<unknown> {
|
|
37
|
-
const app = await createApp({
|
|
38
|
-
routes: dir,
|
|
39
|
-
_skipBuild: true,
|
|
40
|
-
plugins: [trusting],
|
|
41
|
-
_behindCoordinator: behindCoordinator,
|
|
42
|
-
});
|
|
43
|
-
const res = await app.fetch(
|
|
44
|
-
new Request("http://localhost/who", {
|
|
45
|
-
headers: { "X-Grimoire-Locals": "admin" },
|
|
46
|
-
}),
|
|
47
|
-
);
|
|
48
|
-
// no close(): it starts a process-wide shutdown, and nothing here holds a resource
|
|
49
|
-
return ((await res.json()) as { user: unknown }).user;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
describe("X-Grimoire-Locals", () => {
|
|
53
|
-
test("is ignored by a server with no coordinator in front of it", async () => {
|
|
54
|
-
expect(await whoAmI(undefined)).toBeNull();
|
|
55
|
-
expect(await whoAmI(false)).toBeNull();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("is honoured by a worker behind the coordinator", async () => {
|
|
59
|
-
expect(await whoAmI(true)).toBe("admin");
|
|
60
|
-
});
|
|
61
|
-
});
|
|
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 { createApp } from "../src/server";
|
|
6
|
+
import type { GrimoirePlugin } from "../src/types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `X-Grimoire-Locals` is how the coordinator tells a worker who the user is.
|
|
10
|
+
* A server that answers the public directly has no coordinator, so the header
|
|
11
|
+
* is just something a client typed: honouring it would let anyone choose their
|
|
12
|
+
* own `locals`, including a session.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
let dir: string;
|
|
16
|
+
|
|
17
|
+
beforeAll(async () => {
|
|
18
|
+
dir = join(tmpdir(), `grimoire-locals-${Date.now()}`);
|
|
19
|
+
await mkdir(join(dir, "who"), { recursive: true });
|
|
20
|
+
await writeFile(
|
|
21
|
+
join(dir, "who", "+server.ts"),
|
|
22
|
+
"export function GET({ locals }) { return Response.json({ user: locals.user ?? null }); }",
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
27
|
+
|
|
28
|
+
// What a signing plugin would do once it has verified the coordinator's mark.
|
|
29
|
+
const trusting: GrimoirePlugin = {
|
|
30
|
+
name: "trusting",
|
|
31
|
+
deserializeLocals: (raw) => ({ user: raw }) as unknown as App.Locals,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
async function whoAmI(
|
|
35
|
+
behindCoordinator: boolean | undefined,
|
|
36
|
+
): Promise<unknown> {
|
|
37
|
+
const app = await createApp({
|
|
38
|
+
routes: dir,
|
|
39
|
+
_skipBuild: true,
|
|
40
|
+
plugins: [trusting],
|
|
41
|
+
_behindCoordinator: behindCoordinator,
|
|
42
|
+
});
|
|
43
|
+
const res = await app.fetch(
|
|
44
|
+
new Request("http://localhost/who", {
|
|
45
|
+
headers: { "X-Grimoire-Locals": "admin" },
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
// no close(): it starts a process-wide shutdown, and nothing here holds a resource
|
|
49
|
+
return ((await res.json()) as { user: unknown }).user;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe("X-Grimoire-Locals", () => {
|
|
53
|
+
test("is ignored by a server with no coordinator in front of it", async () => {
|
|
54
|
+
expect(await whoAmI(undefined)).toBeNull();
|
|
55
|
+
expect(await whoAmI(false)).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("is honoured by a worker behind the coordinator", async () => {
|
|
59
|
+
expect(await whoAmI(true)).toBe("admin");
|
|
60
|
+
});
|
|
61
|
+
});
|