Skip to content
Source
Sign inSign up
Open History to see the last commit

server-compile-cache.test.ts

329 lines14.8 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6import { expect, it } from "vite-plus/test";
7import { cacheKey, seedPreparedCache } from "./server-compile-cache.ts";
8​
9it.each([
10 { platform: "freebsd", hostArch: "x64", arch: 1, reason: "unsupported target freebsd" },
11 { platform: "linux", hostArch: "x64", arch: 3, reason: "unexecutable target linux/arm64" },
12 { platform: "linux", hostArch: "arm64", arch: 1, reason: "unexecutable target linux/x64" },
13])(
14 "skips unsupported $platform/$hostArch cache generation",
15 async ({ platform, hostArch, arch, reason }) => {
16 const { spawnSync } = await import("node:child_process");
17 const hook = NodeURL.fileURLToPath(
18 new URL("../prepare-server-compile-cache.cjs", import.meta.url),
19 );
20 const result = spawnSync(
21 process.execPath,
22 [
23 "-e",
24 `Object.defineProperty(process, "platform", { value: ${JSON.stringify(platform)} });
25 Object.defineProperty(process, "arch", { value: ${JSON.stringify(hostArch)} });
26 require(${JSON.stringify(hook)})({ electronPlatformName: ${JSON.stringify(platform)}, arch: ${arch} }).catch(error => { console.error(error); process.exitCode = 1; });`,
27 ],
28 { encoding: "utf8" },
29 );
30 expect(result.status, result.stderr).toBe(0);
31 expect(result.stdout).toContain(`Skipping ${reason}`);
32 },
33);
34​
35// macOS runs on an ARM64 host to exercise the Rosetta build-host check.
36it.each([
37 {
38 platform: "darwin",
39 hostArch: "arm64",
40 arch: 1,
41 resources: "Fixture.app/Contents/Resources",
42 executable: "Fixture.app/Contents/MacOS/Fixture",
43 },
44 { platform: "linux", hostArch: "x64", arch: 1, resources: "resources", executable: "fixture" },
45 { platform: "linux", hostArch: "arm64", arch: 3, resources: "resources", executable: "fixture" },
46])("prepares $platform/$hostArch cache", async ({ platform, hostArch, arch, ...layout }) => {
47 const { spawnSync } = await import("node:child_process");
48 const root = NodeFS.realpathSync(
49 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-hook-cache-")),
50 );
51 try {
52 const resources = NodePath.join(root, layout.resources);
53 const archive = NodePath.join(resources, "app.asar");
54 const dist = NodePath.join(archive, "apps/server/dist");
55 const helper = NodePath.join(archive, "apps/desktop/dist-electron/serverCompileCache.cjs");
56 NodeFS.mkdirSync(dist, { recursive: true });
57 NodeFS.mkdirSync(NodePath.dirname(helper), { recursive: true });
58 NodeFS.writeFileSync(NodePath.join(dist, "binCli-fixture.mjs"), "export const answer = 42;");
59 NodeFS.writeFileSync(NodePath.join(dist, "server-fixture.mjs"), "export const answer = 43;");
60 NodeFS.writeFileSync(
61 helper,
62 `module.exports = require(${JSON.stringify(NodeURL.fileURLToPath(new URL("./server-compile-cache.ts", import.meta.url)))});`,
63 );
64 const hook = NodeURL.fileURLToPath(
65 new URL("../prepare-server-compile-cache.cjs", import.meta.url),
66 );
67 // Node stands in for the packaged engine.
68 const result = spawnSync(
69 process.execPath,
70 [
71 "-e",
72 `
73 const childProcess = require("node:child_process");
74 const spawn = childProcess.spawnSync;
75 childProcess.spawnSync = (executable, args, options) => {
76 require("node:assert/strict").equal(executable, ${JSON.stringify(NodePath.join(root, layout.executable))});
77 return spawn(process.execPath, args, options);
78 };
79 Object.defineProperty(process, "platform", { value: ${JSON.stringify(platform)} });
80 Object.defineProperty(process, "arch", { value: ${JSON.stringify(hostArch)} });
81 require(${JSON.stringify(hook)})({
82 appOutDir: ${JSON.stringify(root)}, electronPlatformName: ${JSON.stringify(platform)}, arch: ${arch},
83 packager: { appInfo: { productFilename: "Fixture" }, executableName: "fixture" }
84 }).catch(error => { console.error(error); process.exitCode = 1; });
85 `,
86 ],
87 { encoding: "utf8" },
88 );
89 expect(result.status, result.stderr).toBe(0);
90 const manifest = JSON.parse(
91 NodeFS.readFileSync(NodePath.join(resources, "server-compile-cache/manifest.json"), "utf8"),
92 );
93 expect(
94 manifest.entries.sort((a: { module: string }, b: { module: string }) =>
95 a.module.localeCompare(b.module),
96 ),
97 ).toEqual([
98 {
99 module: "app.asar/apps/server/dist/binCli-fixture.mjs",
100 file: expect.stringMatching(/^[a-f0-9]{8}$/),
101 },
102 {
103 module: "app.asar/apps/server/dist/server-fixture.mjs",
104 file: expect.stringMatching(/^[a-f0-9]{8}$/),
105 },
106 ]);
107 expect(
108 NodeFS.statSync(NodePath.join(resources, "server-compile-cache", manifest.entries[0].file))
109 .size,
110 ).toBeGreaterThan(0);
111 } finally {
112 NodeFS.rmSync(root, { recursive: true, force: true });
113 }
114});
115​
116it("rejects ambiguous bundled server entries before importing them", async () => {
117 const { spawnSync } = await import("node:child_process");
118 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-ambiguous-"));
119 try {
120 const dist = NodePath.join(root, "app.asar/apps/server/dist");
121 NodeFS.mkdirSync(dist, { recursive: true });
122 for (const file of ["binCli-fixture.mjs", "server-fixture.mjs", "server-unrelated.mjs"]) {
123 NodeFS.writeFileSync(NodePath.join(dist, file), "throw Error('Unexpected import');");
124 }
125 const result = spawnSync(
126 process.execPath,
127 [
128 "--input-type=module",
129 "-e",
130 `import {generatePreparedServerCache} from ${JSON.stringify(new URL("./server-compile-cache.ts", import.meta.url).href)}; await generatePreparedServerCache(${JSON.stringify(root)});`,
131 ],
132 {
133 encoding: "utf8",
134 env: {
135 ...process.env,
136 NODE_COMPILE_CACHE: undefined,
137 NODE_DISABLE_COMPILE_CACHE: undefined,
138 },
139 },
140 );
141 expect(result.status).not.toBe(0);
142 expect(result.stderr).toContain("Ambiguous bundled server entry");
143 expect(NodeFS.existsSync(NodePath.join(root, "server-compile-cache/manifest.json"))).toBe(
144 false,
145 );
146 } finally {
147 NodeFS.rmSync(root, { recursive: true, force: true });
148 }
149});
150​
151it("reclaims only AppImage cache directories linked to gone mounts", async () => {
152 const { spawnSync } = await import("node:child_process");
153 const root = NodeFS.realpathSync(
154 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-reclaim-")),
155 );
156 try {
157 const resources = NodePath.join(root, ".mount_live/resources");
158 const prepared = NodePath.join(resources, "server-compile-cache");
159 const cacheRoot = NodePath.join(root, "cache");
160 const server = NodePath.join(cacheRoot, "server");
161 NodeFS.mkdirSync(prepared, { recursive: true });
162 NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), "{}");
163 NodeFS.writeFileSync(NodePath.join(prepared, "12345678"), "live bytes");
164 const gone = NodePath.join(root, ".mount_gone/resources/server-compile-cache/12345678");
165 for (const [name, targets] of [
166 ["appimage-stale", [gone]],
167 ["appimage-live", [NodePath.join(prepared, "12345678")]],
168 ["appimage-live-damaged", [NodePath.join(prepared, "87654321")]],
169 ["appimage-mixed", [gone, NodePath.join(prepared, "12345678")]],
170 ["appimage-unrelated", [NodePath.join(root, "unrelated-missing")]],
171 ["other-cache", [gone]],
172 ["appimage-initializing", []],
173 ] as const) {
174 const active = NodePath.join(server, name, "v24-engine-uid");
175 NodeFS.mkdirSync(active, { recursive: true });
176 targets.forEach((target, index) =>
177 NodeFS.symlinkSync(target, NodePath.join(active, index === 0 ? "12345678" : "87654321")),
178 );
179 NodeFS.writeFileSync(NodePath.join(active, "abcdef01"), "runtime replacement");
180 }
181 NodeFS.symlinkSync(
182 NodePath.join(server, "other-cache"),
183 NodePath.join(server, "appimage-directory-link"),
184 );
185 const result = spawnSync(
186 process.execPath,
187 [
188 "--input-type=module",
189 "-e",
190 `process.env.APPIMAGE = "fixture"; import {enablePreparedServerCache} from ${JSON.stringify(new URL("./server-compile-cache.ts", import.meta.url).href)}; if (!enablePreparedServerCache(${JSON.stringify(NodePath.join(resources, "app.asar/apps/server/dist/binCli-fixture.mjs"))}, ${JSON.stringify(cacheRoot)})) throw Error("cache disabled");`,
191 ],
192 {
193 encoding: "utf8",
194 env: {
195 ...process.env,
196 NODE_COMPILE_CACHE: undefined,
197 NODE_DISABLE_COMPILE_CACHE: undefined,
198 },
199 },
200 );
201 expect(result.status, result.stderr).toBe(0);
202 expect(NodeFS.readdirSync(server).sort()).toEqual([
203 "appimage-directory-link",
204 "appimage-initializing",
205 "appimage-live",
206 "appimage-live-damaged",
207 "appimage-mixed",
208 "appimage-unrelated",
209 "other-cache",
210 ]);
211 expect(NodeFS.readFileSync(NodePath.join(prepared, "12345678"), "utf8")).toBe("live bytes");
212 } finally {
213 NodeFS.rmSync(root, { recursive: true, force: true });
214 }
215});
216​
217it("seeds relocated cache entries without allowing writes into shipped resources", () => {
218 const root = NodeFS.realpathSync(
219 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-prepared-test-")),
220 );
221 try {
222 const prepared = NodePath.join(root, "resources", "server-compile-cache");
223 const active = NodePath.join(root, "user", "different-uid-tag");
224 NodeFS.mkdirSync(prepared, { recursive: true });
225 NodeFS.mkdirSync(active, { recursive: true });
226 NodeFS.writeFileSync(NodePath.join(prepared, "12345678"), "prepared bytes");
227 const manifest = {
228 format: 1,
229 runtime: "matching",
230 entries: [{ module: "app.asar/apps/server/dist/chunk.mjs", file: "12345678" }],
231 };
232 NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), JSON.stringify(manifest));
233 expect(seedPreparedCache(prepared, active, "wrong-engine")).toBe(false);
234 expect(seedPreparedCache(prepared, active, "matching")).toBe(true);
235 const key = cacheKey(active, NodePath.join(root, "resources", manifest.entries[0]!.module));
236 expect(NodeFS.readFileSync(NodePath.join(active, key), "utf8")).toBe("prepared bytes");
237 // Node persists by replacing the directory entry, never by overwriting the linked inode.
238 NodeFS.rmSync(NodePath.join(active, key));
239 NodeFS.writeFileSync(NodePath.join(active, key), "runtime replacement");
240 expect(NodeFS.readFileSync(NodePath.join(prepared, "12345678"), "utf8")).toBe("prepared bytes");
241 expect(seedPreparedCache(prepared, active, "matching")).toBe(true);
242 expect(NodeFS.readFileSync(NodePath.join(active, key), "utf8")).toBe("runtime replacement");
243 const missing = { module: "app.asar/apps/server/dist/missing.mjs", file: "87654321" };
244 for (const entries of [
245 [...manifest.entries, missing],
246 [missing, ...manifest.entries],
247 ]) {
248 NodeFS.writeFileSync(
249 NodePath.join(prepared, "manifest.json"),
250 JSON.stringify({ ...manifest, entries }),
251 );
252 expect(seedPreparedCache(prepared, active, "matching")).toBe(false);
253 }
254 NodeFS.writeFileSync(NodePath.join(prepared, "manifest.json"), "corrupt");
255 expect(seedPreparedCache(prepared, active, "matching")).toBe(false);
256 expect(seedPreparedCache(NodePath.join(root, "missing"), active, "matching")).toBe(false);
257 } finally {
258 NodeFS.rmSync(root, { recursive: true, force: true });
259 }
260});
261​
262it.each(["app", "server"])(
263 "generates %s.asar cache with the consumer engine and falls back after damage",
264 async (archive) => {
265 const { spawnSync } = await import("node:child_process");
266 const helper = new URL("./server-compile-cache.ts", import.meta.url).href;
267 const root = NodeFS.realpathSync(
268 NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cache-engine-")),
269 );
270 const run = (code: string) => {
271 const env = {
272 ...process.env,
273 NODE_DEBUG_NATIVE: "COMPILE_CACHE",
274 NODE_COMPILE_CACHE: undefined,
275 NODE_DISABLE_COMPILE_CACHE: undefined,
276 };
277 const result = spawnSync(process.execPath, ["--input-type=module", "-e", code], {
278 env,
279 encoding: "utf8",
280 });
281 expect(result.status, result.stderr).toBe(0);
282 return result.stderr;
283 };
284 try {
285 const producer = NodePath.join(root, "producer", "resources");
286 const consumer = NodePath.join(root, "relocated", "resources");
287 const relative = `${archive}.asar/apps/server/dist/binCli-fixture.mjs`;
288 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(producer, relative)), { recursive: true });
289 NodeFS.writeFileSync(
290 NodePath.join(producer, relative),
291 "export const answer = 42; export const loadServer = () => import('./server-fixture.mjs');",
292 );
293 NodeFS.writeFileSync(
294 NodePath.join(producer, `${archive}.asar/apps/server/dist/server-fixture.mjs`),
295 "export const answer = 43;",
296 );
297 run(
298 `import {generatePreparedServerCache} from ${JSON.stringify(helper)}; await generatePreparedServerCache(${JSON.stringify(producer)});`,
299 );
300 NodeFS.cpSync(producer, consumer, { recursive: true });
301 const consume = (home: string, appImage = false) =>
302 run(
303 `if (${appImage}) process.env.APPIMAGE = "fixture"; import {enablePreparedServerCache} from ${JSON.stringify(helper)}; import {pathToFileURL} from 'node:url'; if(!enablePreparedServerCache(${JSON.stringify(NodePath.join(consumer, relative))},${JSON.stringify(NodePath.join(root, home))})) throw Error('cache disabled'); const m = await import(pathToFileURL(${JSON.stringify(NodePath.join(consumer, relative))}).href); if(m.answer!==42 || (await m.loadServer()).answer!==43) throw Error('source fallback failed');`,
304 );
305 const prepared = NodePath.join(consumer, "server-compile-cache");
306 NodeFS.chmodSync(prepared, 0o555);
307 try {
308 const first = consume("first-user");
309 expect(first).toMatch(/binCli-fixture\.mjs was accepted/);
310 expect(first).toMatch(/server-fixture\.mjs was accepted/);
311 expect(consume("appimage-user", true)).toContain("was accepted");
312 expect(NodeFS.readdirSync(NodePath.join(root, "appimage-user", "server"))).toEqual([]);
313 } finally {
314 NodeFS.chmodSync(prepared, 0o755);
315 }
316 for (const file of NodeFS.readdirSync(prepared).filter((file) => file !== "manifest.json")) {
317 NodeFS.writeFileSync(NodePath.join(prepared, file), "broken");
318 }
319 expect(consume("second-user")).toContain("was not initialized");
320 for (const file of NodeFS.readdirSync(prepared).filter((file) => file !== "manifest.json")) {
321 expect(NodeFS.readFileSync(NodePath.join(prepared, file), "utf8")).toBe("broken");
322 }
323 NodeFS.rmSync(prepared, { recursive: true });
324 expect(consume("third-user")).toContain("was not initialized");
325 } finally {
326 NodeFS.rmSync(root, { recursive: true, force: true });
327 }
328 },
329);