Open History to see the last commit
1import { assert, describe, it } from "vite-plus/test";
2
3import { verifyPreloadBundle } from "./verify-preload-bundle.mjs";
4
5const validPreload = `
6 const electron = require("electron");
7 const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
8 electron.contextBridge.exposeInMainWorld("__clerk_internal_electron_passkeys", {});
9 electron.contextBridge.exposeInMainWorld("desktopBridge", {
10 getClientPlatform: () => process.platform,
11 getLocalEnvironmentBootstraps: () => [],
12 pickFolder: (options) => electron.ipcRenderer.invoke(PICK_FOLDER_CHANNEL, options),
13 });
14`;
15
16describe("desktop preload bundle verifier", () => {
17 it("rejects required API names that only appear in strings", () => {
18 assert.throws(
19 () =>
20 verifyPreloadBundle(`
21 "desktopBridge getClientPlatform getLocalEnvironmentBootstraps pickFolder";
22 "__clerk_internal_electron_passkeys";
23 require("electron");
24 `),
25 /missing executable APIs/,
26 );
27 });
28
29 it("rejects a required API whose exposed value is not callable", () => {
30 assert.throws(
31 () =>
32 verifyPreloadBundle(
33 validPreload.replace(
34 "getClientPlatform: () => process.platform,",
35 "getClientPlatform: undefined,",
36 ),
37 ),
38 /missing executable APIs: getClientPlatform/,
39 );
40 });
41
42 it("accepts a required API exposed through a function alias", () => {
43 assert.doesNotThrow(() =>
44 verifyPreloadBundle(`
45 const readClientPlatform = () => process.platform;
46 ${validPreload.replace(
47 "getClientPlatform: () => process.platform,",
48 "getClientPlatform: readClientPlatform,",
49 )}
50 `),
51 );
52 });
53
54 it("rejects dynamic imports with comments before the opening parenthesis", () => {
55 assert.throws(
56 () =>
57 verifyPreloadBundle(`${validPreload}\nimport /* @vite-ignore */("unsupported-module");`),
58 /dynamic import\(\)/,
59 );
60 });
61
62 it("ignores import-like text in strings", () => {
63 assert.doesNotThrow(() =>
64 verifyPreloadBundle(`${validPreload}\nconst message = 'import /* comment */("module")';`),
65 );
66 });
67
68 it("rejects unsupported require calls with comments before the opening parenthesis", () => {
69 assert.throws(
70 () => verifyPreloadBundle(`${validPreload}\nrequire /* @__PURE__ */ ("node:fs");`),
71 /unsupported sandbox imports: node:fs/,
72 );
73 });
74
75 it("rejects unsupported optional require calls", () => {
76 assert.throws(
77 () => verifyPreloadBundle(`${validPreload}\nrequire?.("node:fs");`),
78 /unsupported sandbox imports: node:fs/,
79 );
80 });
81
82 it("accepts Electron sandbox module aliases", () => {
83 assert.doesNotThrow(() =>
84 verifyPreloadBundle(`
85 ${validPreload}
86 require("electron/common");
87 require("electron/renderer");
88 require("node:events");
89 require("node:timers");
90 require("node:url");
91 `),
92 );
93 });
94
95 it("ignores require-like text in strings and comments", () => {
96 assert.doesNotThrow(() =>
97 verifyPreloadBundle(`
98 ${validPreload}
99 const message = 'require("node:fs")';
100 // require("node:path")
101 `),
102 );
103 });
104});