Skip to content

Commit 3f4bd39

Browse files
committed
don't detach server
1 parent 34f40e0 commit 3f4bd39

1 file changed

Lines changed: 71 additions & 24 deletions

File tree

‎apps/cli/src/build.ts‎

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,76 @@ const createEmbeddedWebUISource = async () => {
121121
].join("\n");
122122
};
123123

124+
// ---------------------------------------------------------------------------
125+
// Bun.build plugin for @secure-exec bundling issues
126+
// ---------------------------------------------------------------------------
127+
128+
/**
129+
* Plugin that fixes two issues with @secure-exec in compiled binaries:
130+
*
131+
* 1. node-stdlib-browser / web-streams-polyfill eagerly call require.resolve()
132+
* at import time, which fails in bunfs. We stub these out since our code
133+
* never uses the polyfill functions.
134+
*
135+
* 2. bridge-loader.js reads bridge.js from disk via fs.readFileSync using
136+
* __dirname. In a compiled binary __dirname points into bunfs where the
137+
* file doesn't exist. We replace bridge-loader with a version that embeds
138+
* bridge.js content directly.
139+
*/
140+
const secureExecBundlePlugin = async (): Promise<import("bun").BunPlugin> => {
141+
// Read files at build time so we can inline them into the compiled binary.
142+
// bridge.js is loaded via fs.readFileSync at runtime, which fails in bunfs.
143+
// bridgeAttach comes from @secure-exec/core's generated isolate-runtime.
144+
const secureExecNodejs = join(
145+
repoRoot,
146+
"node_modules/.bun/@secure-exec+nodejs@0.2.1/node_modules/@secure-exec/nodejs",
147+
);
148+
const secureExecCore = join(
149+
repoRoot,
150+
"node_modules/.bun/@secure-exec+core@0.2.1/node_modules/@secure-exec/core",
151+
);
152+
const bridgeCode = await Bun.file(join(secureExecNodejs, "dist/bridge.js")).text();
153+
const isolateRuntime = await import(join(secureExecCore, "dist/generated/isolate-runtime.js"));
154+
const bridgeAttachCode = isolateRuntime.ISOLATE_RUNTIME_SOURCES.bridgeAttach;
155+
156+
return {
157+
name: "secure-exec-bundle-fixes",
158+
setup(build) {
159+
// Stub polyfill modules that fail at import time in compiled binaries
160+
const stubTargets = /node-stdlib-browser|web-streams-polyfill/;
161+
build.onResolve({ filter: stubTargets }, (args) => ({
162+
path: args.path,
163+
namespace: "stub",
164+
}));
165+
build.onResolve({ filter: /polyfills/ }, (args) => {
166+
if (args.importer.includes("secure-exec")) {
167+
return { path: args.path, namespace: "stub" };
168+
}
169+
});
170+
build.onLoad({ filter: /.*/, namespace: "stub" }, () => ({
171+
contents: `export default {}; export const POLYFILL_CODE_MAP = {}; export const bundlePolyfill = () => {}; export const getAvailableStdlib = () => []; export const hasPolyfill = () => false; export const prebundleAllPolyfills = () => ({});`,
172+
loader: "js",
173+
}));
174+
175+
// Replace bridge-loader with pre-read content
176+
build.onResolve({ filter: /bridge-loader/ }, (args) => {
177+
if (args.importer.includes("secure-exec")) {
178+
return { path: args.path, namespace: "bridge" };
179+
}
180+
});
181+
build.onLoad({ filter: /.*/, namespace: "bridge" }, () => ({
182+
contents: `
183+
const bridgeCode = ${JSON.stringify(bridgeCode)};
184+
const bridgeAttachCode = ${JSON.stringify(bridgeAttachCode)};
185+
export function getRawBridgeCode() { return bridgeCode; }
186+
export function getBridgeAttachCode() { return bridgeAttachCode; }
187+
`,
188+
loader: "js",
189+
}));
190+
},
191+
};
192+
};
193+
124194
// ---------------------------------------------------------------------------
125195
// Build platform binaries
126196
// ---------------------------------------------------------------------------
@@ -155,30 +225,7 @@ const buildBinaries = async (targets: Target[]) => {
155225
target: bunTarget(target) as any,
156226
outfile: join(binDir, binaryName(target)),
157227
},
158-
plugins: [
159-
{
160-
name: "stub-node-stdlib-browser",
161-
setup(build) {
162-
// @secure-exec re-exports polyfill helpers that eagerly resolve
163-
// Node stdlib browser polyfill paths at import time (e.g. require.resolve("assert/")),
164-
// which fails in compiled binaries. Our code never uses these polyfills, so stub them out.
165-
const stubTargets = /node-stdlib-browser|web-streams-polyfill/;
166-
build.onResolve({ filter: stubTargets }, (args) => ({
167-
path: args.path,
168-
namespace: "stub",
169-
}));
170-
build.onResolve({ filter: /polyfills/ }, (args) => {
171-
if (args.importer.includes("secure-exec")) {
172-
return { path: args.path, namespace: "stub" };
173-
}
174-
});
175-
build.onLoad({ filter: /.*/, namespace: "stub" }, () => ({
176-
contents: `export default {}; export const POLYFILL_CODE_MAP = {}; export const bundlePolyfill = () => {}; export const getAvailableStdlib = () => []; export const hasPolyfill = () => false; export const prebundleAllPolyfills = () => ({});`,
177-
loader: "js",
178-
}));
179-
},
180-
},
181-
],
228+
plugins: [await secureExecBundlePlugin()],
182229
});
183230

184231
// Copy QuickJS WASM next to binary — loaded at runtime by the server

0 commit comments

Comments
 (0)