@sandboxrouter/sdk 0.16.0 → 0.17.0
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/dist/move.d.ts +13 -1
- package/dist/move.js +57 -8
- package/dist/providers/boat.js +7 -2
- package/package.json +1 -1
package/dist/move.d.ts
CHANGED
|
@@ -28,6 +28,15 @@ export interface CopyOptions {
|
|
|
28
28
|
timeoutSeconds?: number;
|
|
29
29
|
/** Bytes sent per file transfer (default 4 MB). Providers cap how much one file call carries. */
|
|
30
30
|
chunkBytes?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Encrypt the folder between the two sandboxes, so everything in between (this process, and the
|
|
33
|
+
* sandboxRouter server when you use the hosted API) only relays bytes it can't read. The destination
|
|
34
|
+
* makes a key pair and keeps the private key; the source encrypts the archive (AES-256) with a
|
|
35
|
+
* one-time key sealed to the destination's public key; the destination checks the archive's SHA-256
|
|
36
|
+
* before unpacking. Both sandboxes need `openssl`. It doesn't hide the files from the two providers
|
|
37
|
+
* that run the sandboxes, or from whoever can run commands in them.
|
|
38
|
+
*/
|
|
39
|
+
encrypt?: boolean;
|
|
31
40
|
}
|
|
32
41
|
export interface CopyResult {
|
|
33
42
|
/** The folder read on the source. */
|
|
@@ -36,6 +45,8 @@ export interface CopyResult {
|
|
|
36
45
|
to: string;
|
|
37
46
|
/** Size of the packed (gzip) folder that was sent. */
|
|
38
47
|
bytes: number;
|
|
48
|
+
/** True when the folder crossed encrypted (`encrypt`). */
|
|
49
|
+
encrypted: boolean;
|
|
39
50
|
ms: number;
|
|
40
51
|
}
|
|
41
52
|
export interface MoveOptions extends CopyOptions, RouteOptions {
|
|
@@ -49,7 +60,8 @@ export declare const DEFAULT_COPY_MAX_BYTES: number;
|
|
|
49
60
|
* Copy a folder from one sandbox to another, on any two providers: packed with tar on the source,
|
|
50
61
|
* carried through this process in pieces, unpacked on the destination. Files, their modes and
|
|
51
62
|
* symlinks come across; running processes, memory and env vars don't. The source isn't changed.
|
|
52
|
-
* Both sandboxes need `tar` and `split` (every stock image has them).
|
|
63
|
+
* Both sandboxes need `tar` and `split` (every stock image has them). With `encrypt`, what crosses
|
|
64
|
+
* is ciphertext only the destination sandbox can open.
|
|
53
65
|
*/
|
|
54
66
|
export declare function copyFiles(from: FileBox, to: FileBox, opts?: CopyOptions): Promise<CopyResult>;
|
|
55
67
|
/** What a move needs from a router: a `Router` and a hosted `Client` both fit. */
|
package/dist/move.js
CHANGED
|
@@ -10,7 +10,8 @@ const DEFAULT_COPY_TIMEOUT_S = 600;
|
|
|
10
10
|
* Copy a folder from one sandbox to another, on any two providers: packed with tar on the source,
|
|
11
11
|
* carried through this process in pieces, unpacked on the destination. Files, their modes and
|
|
12
12
|
* symlinks come across; running processes, memory and env vars don't. The source isn't changed.
|
|
13
|
-
* Both sandboxes need `tar` and `split` (every stock image has them).
|
|
13
|
+
* Both sandboxes need `tar` and `split` (every stock image has them). With `encrypt`, what crosses
|
|
14
|
+
* is ciphertext only the destination sandbox can open.
|
|
14
15
|
*/
|
|
15
16
|
export async function copyFiles(from, to, opts = {}) {
|
|
16
17
|
const started = Date.now();
|
|
@@ -25,6 +26,7 @@ export async function copyFiles(from, to, opts = {}) {
|
|
|
25
26
|
if (p.replace(/\/+$/, "") === "")
|
|
26
27
|
throw new RouterError("copy won't take the whole filesystem (/): name a folder", [], "bad_request");
|
|
27
28
|
}
|
|
29
|
+
const encrypt = opts.encrypt === true;
|
|
28
30
|
const tmp = `/tmp/sr-move-${randomUUID().slice(0, 8)}`;
|
|
29
31
|
const run = async (box, what, cmd) => {
|
|
30
32
|
const r = await box.exec(cmd, { timeoutSeconds });
|
|
@@ -49,17 +51,48 @@ export async function copyFiles(from, to, opts = {}) {
|
|
|
49
51
|
if (bytes > maxBytes) {
|
|
50
52
|
throw new RouterError(`the folder packs to ${mb(bytes)}, over the ${mb(maxBytes)} limit: pass exclude (e.g. node_modules) or a higher maxBytes`, [], "bad_request");
|
|
51
53
|
}
|
|
54
|
+
await run(to, "preparing the destination", `mkdir -p ${shq(tmp)} ${shq(dst)}`);
|
|
55
|
+
// What is sent: the archive, or its ciphertext (a few bytes longer).
|
|
56
|
+
let sent = bytes;
|
|
57
|
+
if (encrypt) {
|
|
58
|
+
for (const box of [from, to]) {
|
|
59
|
+
const has = await box.exec("command -v openssl >/dev/null 2>&1", { timeoutSeconds: 60 });
|
|
60
|
+
if (has.exitCode !== 0)
|
|
61
|
+
throw new RouterError(`encrypt needs openssl in both sandboxes, and ${box.provider}'s image has none`, [], "unsupported");
|
|
62
|
+
}
|
|
63
|
+
// The private key is made on the destination and never leaves it; only the public key travels.
|
|
64
|
+
const pem = await run(to, "making the destination's key", `cd ${shq(tmp)} && umask 077 && openssl genrsa -out k.pem 2048 2>/dev/null && openssl rsa -in k.pem -pubout 2>/dev/null`);
|
|
65
|
+
const pub = /-----BEGIN PUBLIC KEY-----[\s\S]+?-----END PUBLIC KEY-----/.exec(pem)?.[0];
|
|
66
|
+
if (!pub)
|
|
67
|
+
throw new RouterError(`couldn't read the public key made on ${to.provider}`, [], "all_failed");
|
|
68
|
+
await from.writeFile(`${tmp}/pub.pem`, `${pub}\n`);
|
|
69
|
+
// A one-time AES key encrypts the archive; that key and the archive's hash are sealed to the public key.
|
|
70
|
+
const sealed = await run(from, "encrypting the archive", [
|
|
71
|
+
`cd ${shq(tmp)}`,
|
|
72
|
+
"umask 077",
|
|
73
|
+
"openssl rand -hex 32 > k",
|
|
74
|
+
`h=$(openssl dgst -sha256 -r a.tgz | cut -d' ' -f1)`,
|
|
75
|
+
"openssl enc -aes-256-cbc -pbkdf2 -pass file:k -in a.tgz -out a.enc",
|
|
76
|
+
`printf '%s %s' "$(cat k)" "$h" | openssl pkeyutl -encrypt -pubin -inkey pub.pem -out k.enc`,
|
|
77
|
+
"rm -f k a.tgz pub.pem",
|
|
78
|
+
"mv a.enc a.tgz",
|
|
79
|
+
`echo "size=$(wc -c < a.tgz | tr -d ' ')"`,
|
|
80
|
+
].join(" && "));
|
|
81
|
+
sent = Number(/size=(\d+)/.exec(sealed)?.[1] ?? NaN);
|
|
82
|
+
if (!Number.isFinite(sent))
|
|
83
|
+
throw new RouterError(`couldn't read the encrypted size on ${from.provider}`, [], "all_failed");
|
|
84
|
+
await to.writeFile(`${tmp}/k.enc`, await from.readFile(`${tmp}/k.enc`));
|
|
85
|
+
}
|
|
52
86
|
const listed = await run(from, "splitting the archive", `cd ${shq(tmp)} && split -b ${chunk} -a 4 a.tgz p. && rm a.tgz && ls p.*`);
|
|
53
87
|
const parts = listed.split("\n").map((l) => l.trim()).filter((l) => /^p\.[a-z]{4}$/.test(l)).sort();
|
|
54
88
|
if (parts.length === 0)
|
|
55
89
|
throw new RouterError(`no archive pieces found on ${from.provider}`, [], "all_failed");
|
|
56
|
-
await run(to, "preparing the destination", `mkdir -p ${shq(tmp)} ${shq(dst)}`);
|
|
57
90
|
for (const part of parts)
|
|
58
91
|
await to.writeFile(`${tmp}/${part}`, await from.readFile(`${tmp}/${part}`));
|
|
59
92
|
// The size check catches a piece a provider cut short before tar unpacks half a folder.
|
|
60
93
|
const got = await run(to, "checking the archive", `cat ${shq(tmp)}/p.* | wc -c | tr -d ' '`);
|
|
61
|
-
if (Number(got.trim().split("\n").pop()) !==
|
|
62
|
-
throw new RouterError(`the archive arrived as ${got.trim()} bytes on ${to.provider}, expected ${
|
|
94
|
+
if (Number(got.trim().split("\n").pop()) !== sent) {
|
|
95
|
+
throw new RouterError(`the archive arrived as ${got.trim()} bytes on ${to.provider}, expected ${sent}`, [], "all_failed");
|
|
63
96
|
}
|
|
64
97
|
// -o: the files belong to whoever runs the destination sandbox, not the source's user id.
|
|
65
98
|
// Copying a whole working folder would replace the destination image's shell startup files with
|
|
@@ -67,8 +100,24 @@ export async function copyFiles(from, to, opts = {}) {
|
|
|
67
100
|
const keep = opts.path === undefined ? IMAGE_FILES.map(shq).join(" ") : "";
|
|
68
101
|
const save = keep ? `mkdir -p ${shq(`${tmp}/keep`)} && for f in ${keep}; do if [ -f ${shq(dst)}/"$f" ]; then cp -p ${shq(dst)}/"$f" ${shq(`${tmp}/keep`)}/; fi; done && ` : "";
|
|
69
102
|
const restore = keep ? ` && for f in ${keep}; do if [ -f ${shq(`${tmp}/keep`)}/"$f" ]; then cp -p ${shq(`${tmp}/keep`)}/"$f" ${shq(dst)}/; fi; done` : "";
|
|
70
|
-
|
|
71
|
-
|
|
103
|
+
// Encrypted: open the one-time key with the private key, decrypt, and unpack only if the hash matches.
|
|
104
|
+
const open = encrypt
|
|
105
|
+
? [
|
|
106
|
+
`cd ${shq(tmp)}`,
|
|
107
|
+
// The keys and the plain archive are written private (umask 077), in a subshell so that
|
|
108
|
+
// tar below still unpacks with the sandbox's own umask and the files keep their modes.
|
|
109
|
+
`( ${[
|
|
110
|
+
"umask 077",
|
|
111
|
+
"openssl pkeyutl -decrypt -inkey k.pem -in k.enc -out kh",
|
|
112
|
+
"cut -d' ' -f1 kh > k",
|
|
113
|
+
"cat p.* | openssl enc -d -aes-256-cbc -pbkdf2 -pass file:k -out a.tgz",
|
|
114
|
+
`[ "$(openssl dgst -sha256 -r a.tgz | cut -d' ' -f1)" = "$(cut -d' ' -f2 kh)" ]`,
|
|
115
|
+
].join(" && ")} )`,
|
|
116
|
+
`tar -xzo -f a.tgz -C ${shq(dst)}`,
|
|
117
|
+
].join(" && ")
|
|
118
|
+
: `cat ${shq(tmp)}/p.* | tar -xzo -f - -C ${shq(dst)}`;
|
|
119
|
+
await run(to, encrypt ? "decrypting and unpacking the folder" : "unpacking the folder", `${save}${open}${restore}`);
|
|
120
|
+
return { from: src, to: dst, bytes, encrypted: encrypt, ms: Date.now() - started };
|
|
72
121
|
}
|
|
73
122
|
finally {
|
|
74
123
|
await Promise.all([clean(from), clean(to)]);
|
|
@@ -79,7 +128,7 @@ export async function copyFiles(from, to, opts = {}) {
|
|
|
79
128
|
* fails the new sandbox is killed and the old one is left as it was.
|
|
80
129
|
*/
|
|
81
130
|
export async function moveSandbox(mover, from, opts = {}) {
|
|
82
|
-
const { spec: given = {}, keep, path, to, exclude, maxBytes, timeoutSeconds, chunkBytes, ...route } = opts;
|
|
131
|
+
const { spec: given = {}, keep, path, to, exclude, maxBytes, timeoutSeconds, chunkBytes, encrypt, ...route } = opts;
|
|
83
132
|
let spec = { ...given };
|
|
84
133
|
const elsewhere = async (s) => (await mover.candidates(s, route)).filter((c) => !c.skip && c.provider !== from.provider).map((c) => c.provider);
|
|
85
134
|
// The new sandbox keeps the old one's size. A provider's fixed default size (E2B's 2 vCPU / 0.5 GB)
|
|
@@ -103,7 +152,7 @@ export async function moveSandbox(mover, from, opts = {}) {
|
|
|
103
152
|
const next = await mover.create(spec, route);
|
|
104
153
|
let moved;
|
|
105
154
|
try {
|
|
106
|
-
moved = await copyFiles(from, next, { path, to, exclude, maxBytes, timeoutSeconds, chunkBytes });
|
|
155
|
+
moved = await copyFiles(from, next, { path, to, exclude, maxBytes, timeoutSeconds, chunkBytes, encrypt });
|
|
107
156
|
}
|
|
108
157
|
catch (error) {
|
|
109
158
|
await next.kill().catch(() => { });
|
package/dist/providers/boat.js
CHANGED
|
@@ -123,6 +123,8 @@ class BoatError extends ProviderError {
|
|
|
123
123
|
}
|
|
124
124
|
const READY = new Set(["ready", "idle", "running"]);
|
|
125
125
|
const STOPPED = new Set(["archived", "stopped"]);
|
|
126
|
+
/** 409 codes a data call gets while the sandbox is stopped or still booting: resume or wait, then retry. */
|
|
127
|
+
const STOPPED_CODES = new Set(["sandbox_not_ready", "machine_not_running", "boat_starting"]);
|
|
126
128
|
/** Boat has four fixed sizes (catalog.ts). Pick the smallest that covers the request. */
|
|
127
129
|
function machineType(vcpu, memoryGb) {
|
|
128
130
|
const fit = BOAT_SIZES.find((s) => s.vcpu >= vcpu && s.memoryGb >= memoryGb);
|
|
@@ -145,13 +147,16 @@ class BoatSandbox {
|
|
|
145
147
|
this.nativeId = nativeId;
|
|
146
148
|
this.base = `/sandboxes/${encodeURIComponent(nativeId)}`;
|
|
147
149
|
}
|
|
148
|
-
/**
|
|
150
|
+
/**
|
|
151
|
+
* Data call; when the sandbox is stopped or still starting, wait/resume once and retry. Boat has answered a
|
|
152
|
+
* stopped sandbox with 409 sandbox_not_ready and, since Nov 2026, 409 machine_not_running.
|
|
153
|
+
*/
|
|
149
154
|
async call(method, path, json) {
|
|
150
155
|
try {
|
|
151
156
|
return await this.p.request(method, `${this.base}${path}`, { json });
|
|
152
157
|
}
|
|
153
158
|
catch (err) {
|
|
154
|
-
if (!(err instanceof BoatError && err.status === 409 && (err.code
|
|
159
|
+
if (!(err instanceof BoatError && err.status === 409 && STOPPED_CODES.has(err.code ?? "")))
|
|
155
160
|
throw err;
|
|
156
161
|
await this.p.ready(this.nativeId);
|
|
157
162
|
return this.p.request(method, `${this.base}${path}`, { json });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sandboxrouter/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "One API for every sandbox provider: route AI agents' code by price, speed or capability, with automatic fallback. Free and open source.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|