MCPcopy Create free account
hub / github.com/cloudflare/computer / bundleComputerd

Function bundleComputerd

packages/computerd/scripts/sea/bundle.mjs:27–132  ·  view source on GitHub ↗
({ outfile, target })

Source from the content-addressed store, hash-verified

25const repoRoot = resolve(computerdRoot, "../..");
26
27export async function bundleComputerd({ outfile, target }) {
28 const nodeGypBuildShim = `
29const { writeFileSync, chmodSync, existsSync, mkdirSync } = require("node:fs");
30const { tmpdir } = require("node:os");
31const { join } = require("node:path");
32const { createHash } = require("node:crypto");
33const sea = require("node:sea");
34
35const LIBFUSE_NAME = ${JSON.stringify(target.libfuseName)};
36const ENV_VAR = ${JSON.stringify(target.envVar)};
37
38let cached;
39function loadNative() {
40 if (cached) return cached;
41 const addonBuf = Buffer.from(sea.getAsset("fuse-native.node"));
42 const libBuf = Buffer.from(sea.getAsset(LIBFUSE_NAME));
43 const hash = createHash("sha256").update(addonBuf).update(libBuf).digest("hex").slice(0, 16);
44 const dir = join(tmpdir(), "computerd-sea-" + hash);
45 try { mkdirSync(dir, { recursive: true }); } catch {}
46 const libPath = join(dir, LIBFUSE_NAME);
47 const addonPath = join(dir, "fuse-native.node");
48 if (!existsSync(libPath)) writeFileSync(libPath, libBuf);
49 if (!existsSync(addonPath)) { writeFileSync(addonPath, addonBuf); chmodSync(addonPath, 0o755); }
50 process.env[ENV_VAR] = dir + (process.env[ENV_VAR] ? ":" + process.env[ENV_VAR] : "");
51 const m = { exports: {} };
52 process.dlopen(m, addonPath);
53 cached = m.exports;
54 return cached;
55}
56
57module.exports = function nodeGypBuild(_dir) { return loadNative(); };
58`;
59
60 const fuseSharedLibraryShim = `
61function noop(cb) { if (typeof cb === "function") process.nextTick(cb); }
62function isConfigured(cb) { process.nextTick(() => cb(null, true)); }
63module.exports = { beforeMount: noop, beforeUnmount: noop, configure: noop, unconfigure: noop, isConfigured };
64`;
65
66 const nativeShimPlugin = {
67 name: "fuse-native-shim",
68 setup(b) {
69 b.onResolve({ filter: /^node-gyp-build$/ }, () => ({
70 path: "node-gyp-build",
71 namespace: "computerd-shim",
72 }));
73 b.onResolve({ filter: /^fuse-shared-library$/ }, () => ({
74 path: "fuse-shared-library",
75 namespace: "computerd-shim",
76 }));
77 b.onLoad({ filter: /.*/, namespace: "computerd-shim" }, (args) => {
78 if (args.path === "node-gyp-build") {
79 return { contents: nodeGypBuildShim, loader: "js", resolveDir: repoRoot };
80 }
81 if (args.path === "fuse-shared-library") {
82 return { contents: fuseSharedLibraryShim, loader: "js" };
83 }
84 return null;

Callers 2

buildTargetFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected