@catamorphic/microsandbox 0.0.0-stage → 0.1.0-alpha.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +202 -0
- package/NOTICE.txt +32 -0
- package/dist/egress-policy.d.ts +16 -0
- package/dist/egress-policy.d.ts.map +1 -0
- package/dist/image-builder.d.ts +30 -0
- package/dist/image-builder.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +683 -0
- package/dist/index.js.map +14 -0
- package/dist/sandbox-provider.d.ts +110 -0
- package/dist/sandbox-provider.d.ts.map +1 -0
- package/dist/stdio-runtime-provider.d.ts +13 -0
- package/dist/stdio-runtime-provider.d.ts.map +1 -0
- package/dist/support.d.ts +23 -0
- package/dist/support.d.ts.map +1 -0
- package/package.json +53 -4
- package/src/egress-policy.ts +71 -0
- package/src/image-builder.ts +79 -0
- package/src/index.ts +15 -0
- package/src/sandbox-provider.ts +773 -0
- package/src/stdio-runtime-provider.ts +60 -0
- package/src/support.ts +114 -0
- package/README.md +0 -3
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StdioDeploymentRuntimeProvider,
|
|
3
|
+
type SupervisorProcessHandle,
|
|
4
|
+
} from "@catamorphic/sandbox";
|
|
5
|
+
import type { ExecHandle, Sandbox } from "microsandbox";
|
|
6
|
+
|
|
7
|
+
export interface MsbStdioTransportOptions {
|
|
8
|
+
connect: (sandboxId: string) => Promise<Sandbox>;
|
|
9
|
+
uploadFiles: (
|
|
10
|
+
sandboxId: string,
|
|
11
|
+
files: Record<string, string>,
|
|
12
|
+
basePath: string,
|
|
13
|
+
) => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The generic stdio deployment runtime (`@catamorphic/sandbox`) over
|
|
18
|
+
* microsandbox's exec-stream channel: `bun run entry.mjs` inside the
|
|
19
|
+
* sandbox, frames over the exec stream's stdin/stdout.
|
|
20
|
+
*/
|
|
21
|
+
export function msbStdioRuntimeProvider(
|
|
22
|
+
options: MsbStdioTransportOptions,
|
|
23
|
+
): StdioDeploymentRuntimeProvider {
|
|
24
|
+
return new StdioDeploymentRuntimeProvider({
|
|
25
|
+
uploadFiles: options.uploadFiles,
|
|
26
|
+
mkdirp: async (sandboxId, directory) => {
|
|
27
|
+
const sandbox = await options.connect(sandboxId);
|
|
28
|
+
await sandbox.shell(`mkdir -p '${directory.replaceAll("'", `'\\''`)}'`);
|
|
29
|
+
},
|
|
30
|
+
openSupervisor: async (args) => {
|
|
31
|
+
const sandbox = await options.connect(args.sandboxId);
|
|
32
|
+
const handle = await sandbox.execStreamWith("bun", (exec) =>
|
|
33
|
+
exec
|
|
34
|
+
.args(["run", "entry.mjs"])
|
|
35
|
+
.cwd(args.runtimeDirectory)
|
|
36
|
+
.envs(args.env)
|
|
37
|
+
.stdinPipe(),
|
|
38
|
+
);
|
|
39
|
+
const stdin = await handle.takeStdin();
|
|
40
|
+
if (!stdin) {
|
|
41
|
+
await handle.kill().catch(() => {});
|
|
42
|
+
throw new Error("Supervisor exec stream has no stdin sink");
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
write: (data) => stdin.write(data),
|
|
46
|
+
kill: async () => {
|
|
47
|
+
await handle.kill();
|
|
48
|
+
},
|
|
49
|
+
stdout: execStdout(handle),
|
|
50
|
+
} satisfies SupervisorProcessHandle;
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function* execStdout(handle: ExecHandle): AsyncIterable<Uint8Array> {
|
|
56
|
+
for await (const event of handle) {
|
|
57
|
+
if (event.kind === "stdout") yield event.data;
|
|
58
|
+
if (event.kind === "exited") break;
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/support.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
/** Whether microsandbox can run on this machine, and why not. */
|
|
7
|
+
export type MicrosandboxSupport = { ok: true } | { ok: false; reason: string };
|
|
8
|
+
|
|
9
|
+
const NO_KVM =
|
|
10
|
+
"This machine has no usable /dev/kvm, so microsandbox cannot run here. Use WORK_SANDBOX=container (gVisor) or auto.";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Whether microsandbox can run here (ADR 0204): an Apple silicon Mac, or
|
|
14
|
+
* Linux (x64, arm64) with a `/dev/kvm` this process may open read-write;
|
|
15
|
+
* and an `msb` runtime the SDK resolves (`MSB_PATH`, the runtime home's
|
|
16
|
+
* `~/.microsandbox/bin/msb`, or the SDK's platform package).
|
|
17
|
+
*/
|
|
18
|
+
export function microsandboxSupport(input?: {
|
|
19
|
+
platform?: NodeJS.Platform;
|
|
20
|
+
arch?: string;
|
|
21
|
+
env?: Record<string, string | undefined>;
|
|
22
|
+
/** Checks `/dev/kvm`; the real device by default. */
|
|
23
|
+
kvmUsable?: () => boolean;
|
|
24
|
+
/** Finds the msb binary; the SDK's own order by default. */
|
|
25
|
+
msbPath?: () => string | undefined;
|
|
26
|
+
}): MicrosandboxSupport {
|
|
27
|
+
const platform = input?.platform ?? process.platform;
|
|
28
|
+
const arch = input?.arch ?? process.arch;
|
|
29
|
+
const env = input?.env ?? process.env;
|
|
30
|
+
if (platform === "darwin") {
|
|
31
|
+
if (arch !== "arm64")
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
reason: "Microsandbox runs on Apple silicon Macs only",
|
|
35
|
+
};
|
|
36
|
+
} else if (platform === "linux") {
|
|
37
|
+
if (arch !== "x64" && arch !== "arm64")
|
|
38
|
+
return {
|
|
39
|
+
ok: false,
|
|
40
|
+
reason: `Microsandbox does not run on Linux ${arch}`,
|
|
41
|
+
};
|
|
42
|
+
if (!(input?.kvmUsable ?? kvmUsable)())
|
|
43
|
+
return { ok: false, reason: NO_KVM };
|
|
44
|
+
} else {
|
|
45
|
+
return {
|
|
46
|
+
ok: false,
|
|
47
|
+
reason: `Microsandbox does not run on ${platform}`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const msb = (input?.msbPath ?? (() => resolveMsb(env)))();
|
|
51
|
+
if (!msb)
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
reason:
|
|
55
|
+
"The microsandbox runtime (msb) is not installed on this machine: install it, or set MSB_PATH to its msb binary",
|
|
56
|
+
};
|
|
57
|
+
return { ok: true };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Whether `/dev/kvm` exists and opens read-write. */
|
|
61
|
+
function kvmUsable(): boolean {
|
|
62
|
+
try {
|
|
63
|
+
fs.closeSync(fs.openSync("/dev/kvm", "r+"));
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The msb binary, in the order the SDK looks for it. */
|
|
71
|
+
function resolveMsb(
|
|
72
|
+
env: Record<string, string | undefined>,
|
|
73
|
+
): string | undefined {
|
|
74
|
+
const candidates = [
|
|
75
|
+
env.MSB_PATH,
|
|
76
|
+
path.join(os.homedir(), ".microsandbox", "bin", "msb"),
|
|
77
|
+
packagedMsb(),
|
|
78
|
+
];
|
|
79
|
+
return candidates.find(
|
|
80
|
+
(candidate): candidate is string =>
|
|
81
|
+
Boolean(candidate) && executable(candidate ?? ""),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function executable(file: string): boolean {
|
|
86
|
+
try {
|
|
87
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
88
|
+
return fs.statSync(file).isFile();
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The msb in the SDK's platform package, which sits beside the SDK. */
|
|
95
|
+
function packagedMsb(): string | undefined {
|
|
96
|
+
const triples: Record<string, string> = {
|
|
97
|
+
"darwin-arm64": "darwin-arm64",
|
|
98
|
+
"linux-x64": "linux-x64-gnu",
|
|
99
|
+
"linux-arm64": "linux-arm64-gnu",
|
|
100
|
+
};
|
|
101
|
+
const triple = triples[`${process.platform}-${process.arch}`];
|
|
102
|
+
if (!triple) return undefined;
|
|
103
|
+
try {
|
|
104
|
+
const sdk = createRequire(import.meta.url).resolve(
|
|
105
|
+
"microsandbox/package.json",
|
|
106
|
+
);
|
|
107
|
+
const platformPackage = createRequire(sdk).resolve(
|
|
108
|
+
`@superradcompany/microsandbox-${triple}/package.json`,
|
|
109
|
+
);
|
|
110
|
+
return path.join(path.dirname(platformPackage), "bin", "msb");
|
|
111
|
+
} catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
package/README.md
DELETED