import {
NodeRuntime,
allowAllFs,
createInMemoryFileSystem,
createNodeDriver,
createNodeRuntimeDriverFactory,
} from "secure-exec";
const filesystem = createInMemoryFileSystem();
await filesystem.mkdir("/plugins");
await filesystem.writeFile(
"/plugins/title-case.mjs",
`
export const manifest = {
name: "title-case",
version: "1.0.0",
};
export function transform(input, options = {}) {
const words = String(input)
.split(/\\s+/)
.filter(Boolean)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
return (options.prefix ?? "") + words.join(" ");
}
`
);
const runtime = new NodeRuntime({
systemDriver: createNodeDriver({
filesystem,
permissions: { ...allowAllFs },
}),
runtimeDriverFactory: createNodeRuntimeDriverFactory(),
memoryLimit: 64,
cpuTimeLimitMs: 1000,
});
const input = "hello from plugin land";
const options = { prefix: "Plugin says: " };
const result = await runtime.run<{
manifest: { name: string; version: string };
output: string;
}>(`
import { manifest, transform } from "/plugins/title-case.mjs";
export { manifest };
export const output = transform(
${JSON.stringify(input)},
${JSON.stringify(options)}
);
`, "/root/run-plugin.mjs");
console.log(result.exports?.manifest.name); // "title-case"
console.log(result.exports?.output); // "Plugin says: Hello From Plugin Land"