@robotostudio/sandbox-runner 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +125 -2
- package/dist/compute.d.ts +26 -0
- package/dist/compute.js +28 -0
- package/dist/docker.d.ts +20 -0
- package/dist/docker.js +22 -0
- package/dist/error.d.ts +10 -0
- package/dist/error.js +4 -0
- package/dist/fake.d.ts +23 -0
- package/dist/fake.js +24 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/sandbox.d.ts +34 -0
- package/dist/sandbox.js +8 -0
- package/dist/shell-env.d.ts +6 -0
- package/dist/shell-env.js +30 -0
- package/dist/vercel.d.ts +11 -0
- package/dist/vercel.js +8 -0
- package/package.json +62 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Roboto Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @robotostudio/sandbox-runner
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One [Effect](https://effect.website) port to run commands in a sandbox. Write the run once against `Sandbox`, then pick
|
|
4
|
+
the provider with a Layer: Docker, Vercel Sandbox, any other [ComputeSDK](https://github.com/computesdk/computesdk)
|
|
5
|
+
provider, or a fake for tests.
|
|
6
|
+
|
|
7
|
+
This package adds the Effect port on top of ComputeSDK. ComputeSDK's own providers, configs and option types stay
|
|
8
|
+
visible.
|
|
9
|
+
|
|
10
|
+
## Use it
|
|
11
|
+
|
|
12
|
+
1. Install it with Effect, ComputeSDK and the provider you use:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
bun add @robotostudio/sandbox-runner effect computesdk @computesdk/docker
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. Write the run:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Sandbox } from "@robotostudio/sandbox-runner";
|
|
22
|
+
import { dockerLayer } from "@robotostudio/sandbox-runner/docker";
|
|
23
|
+
import { Effect } from "effect";
|
|
24
|
+
|
|
25
|
+
const nodeVersion = Sandbox.use((sandbox) =>
|
|
26
|
+
Effect.scoped(
|
|
27
|
+
Effect.gen(function* () {
|
|
28
|
+
const session = yield* sandbox.open;
|
|
29
|
+
|
|
30
|
+
yield* session.writeFile("/tmp/hello.txt", "hi");
|
|
31
|
+
|
|
32
|
+
return yield* session.run("node -v", {
|
|
33
|
+
timeoutMs: 30_000,
|
|
34
|
+
env: { NODE_ENV: "production" },
|
|
35
|
+
});
|
|
36
|
+
}),
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const result = await Effect.runPromise(
|
|
41
|
+
nodeVersion.pipe(
|
|
42
|
+
Effect.provide(
|
|
43
|
+
dockerLayer({ runtime: "node", image: { name: "node:22-slim" } }),
|
|
44
|
+
),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
3. Change the provider in one place: `Effect.provide(vercelLayer())`. The run does not change.
|
|
50
|
+
|
|
51
|
+
## The port
|
|
52
|
+
|
|
53
|
+
| Call | What it does | Timeout | Retries |
|
|
54
|
+
| ------------------------------------------ | ----------------------------------------- | ----------- | ------------------------------ |
|
|
55
|
+
| `sandbox.open` | Creates one sandbox for the scope | 2 min | 2, backoff from 1 s |
|
|
56
|
+
| `session.run(command, { timeoutMs, env })` | Runs a shell command with `env` | `timeoutMs` | None: a repeat may not be safe |
|
|
57
|
+
| `session.writeFile(path, content)` | Writes a UTF-8 file, and makes its folder | 1 min | 2, backoff from 1 s |
|
|
58
|
+
| The scope closes | Destroys the sandbox | 30 s | 2; a failure is logged |
|
|
59
|
+
|
|
60
|
+
- A non-zero exit code is a result, not an error.
|
|
61
|
+
- A failure is a `SandboxError`. Its `message` names the provider and the call, such as `docker: run timed out`.
|
|
62
|
+
Its `cause` holds the provider's own error. A provider can also turn its own error into an exit code: see Vercel.
|
|
63
|
+
- The scope closes on success, on a failure and on an interrupt.
|
|
64
|
+
- A create that times out is aborted. ComputeSDK then destroys the sandbox if it starts later.
|
|
65
|
+
- A run that is safe to repeat can add its own `Effect.retry`.
|
|
66
|
+
|
|
67
|
+
## Layers
|
|
68
|
+
|
|
69
|
+
| Layer | Import from | Provider |
|
|
70
|
+
| -------------------------------- | ------------------------------------- | -------------------------------------------- |
|
|
71
|
+
| `computeLayer(provider, create)` | `@robotostudio/sandbox-runner` | Any ComputeSDK provider, such as `railway()` |
|
|
72
|
+
| `dockerLayer(config)` | `@robotostudio/sandbox-runner/docker` | `@computesdk/docker`, on this machine |
|
|
73
|
+
| `vercelLayer(config, create)` | `@robotostudio/sandbox-runner/vercel` | `@computesdk/vercel` |
|
|
74
|
+
| `fakeLayer(answer, events)` | `@robotostudio/sandbox-runner` | None: in memory, for tests |
|
|
75
|
+
|
|
76
|
+
`config` is the provider's own config. `create` holds ComputeSDK's `CreateInstanceOptions`, passed to every create:
|
|
77
|
+
an image, a snapshot, a size or `envs`.
|
|
78
|
+
|
|
79
|
+
### Docker
|
|
80
|
+
|
|
81
|
+
Tested with `@computesdk/docker` 1.2.77 against a running Docker.
|
|
82
|
+
|
|
83
|
+
- Each container gets 2 CPUs and 4 GB (`--cpus 2 --memory 4g`) and `Init`, through `createOptions.HostConfig`. The
|
|
84
|
+
provider's `container.resources` sets no Docker limit.
|
|
85
|
+
- Put binds, network mode and capabilities in `createOptions.HostConfig`, Docker's own API. Put ports in
|
|
86
|
+
`createOptions.ExposedPorts` and `HostConfig.PortBindings`, and a user in `createOptions.User`. The provider replaces
|
|
87
|
+
the `HostConfig` it builds from `container` with yours, and reads no user. So `container` takes only `env`,
|
|
88
|
+
`workdir`, `tty` and `openStdin`. Your `HostConfig` fields merge over the caps, so `NanoCpus` and `Memory` change
|
|
89
|
+
them.
|
|
90
|
+
- With `runtime: "node"`, the image name must contain `node`. Else the provider swaps the image for `node:20-alpine`.
|
|
91
|
+
The default runtime is `python`, which needs `python` in the name.
|
|
92
|
+
- The provider ignores the per-run `env` and `timeout` options. This Layer exports `env` in the shell instead, so the
|
|
93
|
+
values show in the container's command lines (`ps`). The run times out on the Effect side. A command that timed out
|
|
94
|
+
keeps running until the scope closes.
|
|
95
|
+
- `writeFile` sends the file as one shell argument. A file over about 96 KB fails, and so does a path whose new
|
|
96
|
+
folder name has a space.
|
|
97
|
+
|
|
98
|
+
### Vercel Sandbox
|
|
99
|
+
|
|
100
|
+
Built on `@computesdk/vercel` 1.7.63. The tests here never start a Vercel Sandbox. Type checks prove the Layer, and so
|
|
101
|
+
does a test that fails a create with no credentials, before any call to Vercel.
|
|
102
|
+
|
|
103
|
+
- Auth is `VERCEL_OIDC_TOKEN` (`vercel env pull`), or `token`, `teamId` and `projectId` in the config.
|
|
104
|
+
- A run's `env` goes to Vercel as the command's env, not in its command line.
|
|
105
|
+
- The provider turns an error during a run (a network or API failure) into exit code 127, with the message in
|
|
106
|
+
`stderr`. It does not reach you as a `SandboxError`.
|
|
107
|
+
- Each sandbox stops itself after 10 min, also when a destroy failed. It exposes no daemon port
|
|
108
|
+
(`daemonSsePort: false`). Pass your own `timeout` for longer runs.
|
|
109
|
+
- On Bun 1.4.3, `@vercel/sandbox` 4.x fails at import: it needs `Dispatcher1Wrapper` from `undici`, and Bun's own
|
|
110
|
+
`undici` lacks it. Pin 3.9.1 in your app's `package.json`. This package's tests run on Bun with this pin:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{ "overrides": { "@vercel/sandbox": "3.9.1" } }
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Other providers
|
|
117
|
+
|
|
118
|
+
`computeLayer` takes any ComputeSDK provider: `computeLayer(railway({ ... }), { image: "node:22-slim" })`. A run's
|
|
119
|
+
`env` goes to the provider's `runCommand`, and a provider that ignores it drops it: use `dockerLayer` for Docker, not
|
|
120
|
+
`computeLayer(docker(...))`. This package has no tests for providers other than Docker and Vercel.
|
|
121
|
+
|
|
122
|
+
## Tests
|
|
123
|
+
|
|
124
|
+
`bun test` runs the unit tests on an in-memory ComputeSDK provider, and the Docker test. The Docker test pulls
|
|
125
|
+
`node:22-slim` if it is missing, and opens one container at a time. It skips when `CI` is set or Docker is not
|
|
126
|
+
running.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CommandResult, CreateInstanceOptions, RunCommandOptions } from "computesdk";
|
|
2
|
+
import { Layer } from "effect";
|
|
3
|
+
import { Sandbox } from "./sandbox.ts";
|
|
4
|
+
/** The part of a ComputeSDK instance this Layer calls. */
|
|
5
|
+
export interface ComputeInstance {
|
|
6
|
+
readonly runCommand: (command: string, options?: RunCommandOptions) => Promise<CommandResult>;
|
|
7
|
+
readonly filesystem: {
|
|
8
|
+
readonly writeFile: (path: string, content: string) => Promise<void>;
|
|
9
|
+
};
|
|
10
|
+
readonly destroy: () => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/** The part of a ComputeSDK provider this Layer calls. Every ComputeSDK `Provider` fits. */
|
|
13
|
+
export interface ComputeProvider {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly instances: {
|
|
16
|
+
readonly create: (options?: CreateInstanceOptions) => Promise<ComputeInstance>;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The Sandbox port over a ComputeSDK provider, such as `vercel()` or `railway()`. For Docker, use `dockerLayer`: its
|
|
21
|
+
* provider ignores a run's `env`. `options` go to every create: the image, snapshot, size or `envs` the provider takes.
|
|
22
|
+
* A run's `env` goes to the provider's `runCommand`. Create and write files time out and retry twice. A create that
|
|
23
|
+
* times out is aborted, and ComputeSDK destroys the instance if it starts later. Destroy runs when the scope closes,
|
|
24
|
+
* and a failed destroy is logged, not raised.
|
|
25
|
+
*/
|
|
26
|
+
export declare const computeLayer: (provider: ComputeProvider, options?: CreateInstanceOptions) => Layer.Layer<Sandbox>;
|
package/dist/compute.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Effect, Layer, Schedule } from "effect";
|
|
2
|
+
import { SandboxError } from "./error.js";
|
|
3
|
+
import { Sandbox } from "./sandbox.js";
|
|
4
|
+
const CREATE_TIMEOUT = "2 minutes";
|
|
5
|
+
const WRITE_TIMEOUT = "1 minute";
|
|
6
|
+
const DESTROY_TIMEOUT = "30 seconds";
|
|
7
|
+
const RETRY = { times: 2, schedule: Schedule.exponential("1 second") };
|
|
8
|
+
/** One remote call: its rejection and its timeout both fail with a `SandboxError` that names `label`. */
|
|
9
|
+
const call = (label, timeout, run) => Effect.tryPromise({
|
|
10
|
+
try: run,
|
|
11
|
+
catch: (cause) => new SandboxError({ message: `${label} failed`, cause }),
|
|
12
|
+
}).pipe(Effect.timeoutOrElse({
|
|
13
|
+
duration: timeout,
|
|
14
|
+
orElse: () => Effect.fail(new SandboxError({ message: `${label} timed out` })),
|
|
15
|
+
}));
|
|
16
|
+
/**
|
|
17
|
+
* The Sandbox port over a ComputeSDK provider, such as `vercel()` or `railway()`. For Docker, use `dockerLayer`: its
|
|
18
|
+
* provider ignores a run's `env`. `options` go to every create: the image, snapshot, size or `envs` the provider takes.
|
|
19
|
+
* A run's `env` goes to the provider's `runCommand`. Create and write files time out and retry twice. A create that
|
|
20
|
+
* times out is aborted, and ComputeSDK destroys the instance if it starts later. Destroy runs when the scope closes,
|
|
21
|
+
* and a failed destroy is logged, not raised.
|
|
22
|
+
*/
|
|
23
|
+
export const computeLayer = (provider, options) => Layer.succeed(Sandbox)({
|
|
24
|
+
open: Effect.acquireRelease(call(`${provider.name}: create`, CREATE_TIMEOUT, async (signal) => provider.instances.create({ ...options, signal })).pipe(Effect.retry(RETRY)), (instance) => call(`${provider.name}: destroy`, DESTROY_TIMEOUT, async () => instance.destroy()).pipe(Effect.retry(RETRY), Effect.ignore({ log: "Warn" }))).pipe(Effect.map((instance) => ({
|
|
25
|
+
run: (command, { env, timeoutMs }) => call(`${provider.name}: run`, timeoutMs, async () => instance.runCommand(command, { ...(env && { env }) })),
|
|
26
|
+
writeFile: (path, content) => call(`${provider.name}: write ${path}`, WRITE_TIMEOUT, async () => instance.filesystem.writeFile(path, content)).pipe(Effect.retry(RETRY)),
|
|
27
|
+
}))),
|
|
28
|
+
});
|
package/dist/docker.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { docker } from "@computesdk/docker";
|
|
2
|
+
import type { Layer } from "effect";
|
|
3
|
+
import type { Sandbox } from "./sandbox.ts";
|
|
4
|
+
type ProviderConfig = Parameters<typeof docker>[0];
|
|
5
|
+
/**
|
|
6
|
+
* `@computesdk/docker`'s config, with `container` cut to the fields the provider reads outside `HostConfig`. Set binds,
|
|
7
|
+
* ports, network mode and capabilities in `createOptions` instead: the provider replaces its own `HostConfig`
|
|
8
|
+
* with that one.
|
|
9
|
+
*/
|
|
10
|
+
export type DockerConfig = Omit<ProviderConfig, "container"> & {
|
|
11
|
+
readonly container?: Pick<NonNullable<ProviderConfig["container"]>, "env" | "openStdin" | "tty" | "workdir">;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Docker on this machine, one container per sandbox. Each container is capped at 2 CPUs and 4 GB (`--cpus 2 --memory
|
|
15
|
+
* 4g`), with `Init` so it stops at once. The caps sit in `createOptions.HostConfig`, under your own `HostConfig`
|
|
16
|
+
* fields: the provider's `container.resources` sets no Docker limit. The provider ignores a run's `env`, so each run
|
|
17
|
+
* exports it in the shell.
|
|
18
|
+
*/
|
|
19
|
+
export declare const dockerLayer: (config: DockerConfig) => Layer.Layer<Sandbox>;
|
|
20
|
+
export {};
|
package/dist/docker.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { docker } from "@computesdk/docker";
|
|
2
|
+
import { computeLayer } from "./compute.js";
|
|
3
|
+
import { withShellEnv } from "./shell-env.js";
|
|
4
|
+
const GIB = 1024 ** 3;
|
|
5
|
+
/**
|
|
6
|
+
* Docker on this machine, one container per sandbox. Each container is capped at 2 CPUs and 4 GB (`--cpus 2 --memory
|
|
7
|
+
* 4g`), with `Init` so it stops at once. The caps sit in `createOptions.HostConfig`, under your own `HostConfig`
|
|
8
|
+
* fields: the provider's `container.resources` sets no Docker limit. The provider ignores a run's `env`, so each run
|
|
9
|
+
* exports it in the shell.
|
|
10
|
+
*/
|
|
11
|
+
export const dockerLayer = (config) => computeLayer(withShellEnv(docker({
|
|
12
|
+
...config,
|
|
13
|
+
createOptions: {
|
|
14
|
+
...config.createOptions,
|
|
15
|
+
HostConfig: {
|
|
16
|
+
NanoCpus: 2e9,
|
|
17
|
+
Memory: 4 * GIB,
|
|
18
|
+
Init: true,
|
|
19
|
+
...config.createOptions?.HostConfig,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
})));
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const SandboxError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
2
|
+
readonly _tag: "SandboxError";
|
|
3
|
+
} & Readonly<A>;
|
|
4
|
+
/** A sandbox call that failed or timed out. `message` names the provider and the call. */
|
|
5
|
+
export declare class SandboxError extends SandboxError_base<{
|
|
6
|
+
readonly message: string;
|
|
7
|
+
readonly cause?: unknown;
|
|
8
|
+
}> {
|
|
9
|
+
}
|
|
10
|
+
export {};
|
package/dist/error.js
ADDED
package/dist/fake.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { Sandbox } from "./sandbox.ts";
|
|
3
|
+
import type { RunOptions, RunResult } from "./sandbox.ts";
|
|
4
|
+
/** What the fake saw: every open, close, run and file write, in order. */
|
|
5
|
+
export type FakeEvent = {
|
|
6
|
+
readonly op: "open" | "close";
|
|
7
|
+
readonly id: number;
|
|
8
|
+
} | {
|
|
9
|
+
readonly op: "run";
|
|
10
|
+
readonly id: number;
|
|
11
|
+
readonly command: string;
|
|
12
|
+
readonly options: RunOptions;
|
|
13
|
+
} | {
|
|
14
|
+
readonly op: "write";
|
|
15
|
+
readonly id: number;
|
|
16
|
+
readonly path: string;
|
|
17
|
+
readonly content: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The Sandbox port in memory, for tests. `answer` plays the sandbox: it gets each command and returns what the
|
|
21
|
+
* command would print. `events` collects what the fake saw.
|
|
22
|
+
*/
|
|
23
|
+
export declare const fakeLayer: (answer: (command: string, options: RunOptions) => Omit<RunResult, "durationMs">, events?: FakeEvent[]) => Layer.Layer<Sandbox>;
|
package/dist/fake.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { Sandbox } from "./sandbox.js";
|
|
3
|
+
/**
|
|
4
|
+
* The Sandbox port in memory, for tests. `answer` plays the sandbox: it gets each command and returns what the
|
|
5
|
+
* command would print. `events` collects what the fake saw.
|
|
6
|
+
*/
|
|
7
|
+
export const fakeLayer = (answer, events = []) => {
|
|
8
|
+
let opened = 0;
|
|
9
|
+
return Layer.succeed(Sandbox)({
|
|
10
|
+
open: Effect.acquireRelease(Effect.sync(() => {
|
|
11
|
+
opened += 1;
|
|
12
|
+
events.push({ op: "open", id: opened });
|
|
13
|
+
return opened;
|
|
14
|
+
}), (id) => Effect.sync(() => events.push({ op: "close", id }))).pipe(Effect.map((id) => ({
|
|
15
|
+
run: (command, options) => Effect.sync(() => {
|
|
16
|
+
events.push({ op: "run", id, command, options });
|
|
17
|
+
return { ...answer(command, options), durationMs: 0 };
|
|
18
|
+
}),
|
|
19
|
+
writeFile: (path, content) => Effect.sync(() => {
|
|
20
|
+
events.push({ op: "write", id, path, content });
|
|
21
|
+
}),
|
|
22
|
+
}))),
|
|
23
|
+
});
|
|
24
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { SandboxError } from "./error.ts";
|
|
2
|
+
export { Sandbox } from "./sandbox.ts";
|
|
3
|
+
export type { RunOptions, RunResult, Session } from "./sandbox.ts";
|
|
4
|
+
export { computeLayer } from "./compute.ts";
|
|
5
|
+
export type { ComputeInstance, ComputeProvider } from "./compute.ts";
|
|
6
|
+
export { fakeLayer } from "./fake.ts";
|
|
7
|
+
export type { FakeEvent } from "./fake.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Context } from "effect";
|
|
2
|
+
import type { Effect, Scope } from "effect";
|
|
3
|
+
import type { SandboxError } from "./error.ts";
|
|
4
|
+
/** What a command printed, and its exit code. A non-zero exit is a result, not an error. */
|
|
5
|
+
export interface RunResult {
|
|
6
|
+
readonly exitCode: number;
|
|
7
|
+
readonly stdout: string;
|
|
8
|
+
readonly stderr: string;
|
|
9
|
+
readonly durationMs: number;
|
|
10
|
+
}
|
|
11
|
+
export interface RunOptions {
|
|
12
|
+
/** The run fails with a `SandboxError` after this many milliseconds. */
|
|
13
|
+
readonly timeoutMs: number;
|
|
14
|
+
/** Exported in the shell before the command, on every provider. */
|
|
15
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
16
|
+
}
|
|
17
|
+
/** One live sandbox, open until its scope closes. */
|
|
18
|
+
export interface Session {
|
|
19
|
+
/** Runs a shell command. A run is not retried: a command may not be safe to repeat. */
|
|
20
|
+
readonly run: (command: string, options: RunOptions) => Effect.Effect<RunResult, SandboxError>;
|
|
21
|
+
/** Writes a UTF-8 file, and makes its folder. */
|
|
22
|
+
readonly writeFile: (path: string, content: string) => Effect.Effect<void, SandboxError>;
|
|
23
|
+
}
|
|
24
|
+
declare const Sandbox_base: Context.ServiceClass<Sandbox, "@robotostudio/sandbox-runner/Sandbox", {
|
|
25
|
+
readonly open: Effect.Effect<Session, SandboxError, Scope.Scope>;
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* The port every sandbox run goes through. `open` makes one sandbox for the scope. The scope's close stops it, also
|
|
29
|
+
* on a failure or an interrupt. Each provider is a Layer: `computeLayer`, `dockerLayer`, `vercelLayer` and
|
|
30
|
+
* `fakeLayer`.
|
|
31
|
+
*/
|
|
32
|
+
export declare class Sandbox extends Sandbox_base {
|
|
33
|
+
}
|
|
34
|
+
export {};
|
package/dist/sandbox.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Context } from "effect";
|
|
2
|
+
/**
|
|
3
|
+
* The port every sandbox run goes through. `open` makes one sandbox for the scope. The scope's close stops it, also
|
|
4
|
+
* on a failure or an interrupt. Each provider is a Layer: `computeLayer`, `dockerLayer`, `vercelLayer` and
|
|
5
|
+
* `fakeLayer`.
|
|
6
|
+
*/
|
|
7
|
+
export class Sandbox extends Context.Service()("@robotostudio/sandbox-runner/Sandbox") {
|
|
8
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ComputeProvider } from "./compute.ts";
|
|
2
|
+
/**
|
|
3
|
+
* For a provider whose `runCommand` ignores `options.env`, such as `@computesdk/docker`: each run exports its env in
|
|
4
|
+
* the shell before the command. The values then show in the command line, as in `ps`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const withShellEnv: (provider: ComputeProvider) => ComputeProvider;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { shQuote } from "computesdk";
|
|
2
|
+
const ENV_NAME = /^[A-Za-z_]\w*$/u;
|
|
3
|
+
/** `export NAME='value'; ` for each entry. Throws on a name that is not a shell name. */
|
|
4
|
+
const envPrefix = (env) => {
|
|
5
|
+
const entries = Object.entries(env || {});
|
|
6
|
+
const bad = entries.find(([name]) => !ENV_NAME.test(name));
|
|
7
|
+
if (bad) {
|
|
8
|
+
throw new Error(`env name ${JSON.stringify(bad[0])} is not a shell name`);
|
|
9
|
+
}
|
|
10
|
+
return entries.length > 0
|
|
11
|
+
? `export ${entries.map(([name, value]) => `${name}=${shQuote(value)}`).join(" ")}; `
|
|
12
|
+
: "";
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* For a provider whose `runCommand` ignores `options.env`, such as `@computesdk/docker`: each run exports its env in
|
|
16
|
+
* the shell before the command. The values then show in the command line, as in `ps`.
|
|
17
|
+
*/
|
|
18
|
+
export const withShellEnv = (provider) => ({
|
|
19
|
+
name: provider.name,
|
|
20
|
+
instances: {
|
|
21
|
+
create: async (options) => {
|
|
22
|
+
const instance = await provider.instances.create(options);
|
|
23
|
+
return {
|
|
24
|
+
runCommand: async (command, runOptions) => instance.runCommand(`${envPrefix(runOptions?.env)}${command}`),
|
|
25
|
+
filesystem: instance.filesystem,
|
|
26
|
+
destroy: async () => instance.destroy(),
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
package/dist/vercel.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { vercel } from "@computesdk/vercel";
|
|
2
|
+
import type { CreateInstanceOptions } from "computesdk";
|
|
3
|
+
import type { Layer } from "effect";
|
|
4
|
+
import type { Sandbox } from "./sandbox.ts";
|
|
5
|
+
export type VercelConfig = Parameters<typeof vercel>[0];
|
|
6
|
+
/**
|
|
7
|
+
* Vercel Sandbox, one microVM per sandbox. Auth is `VERCEL_OIDC_TOKEN` (`vercel env pull`), or `token`, `teamId` and
|
|
8
|
+
* `projectId`. Each sandbox stops itself after 10 min, also when a destroy failed, and exposes no daemon port
|
|
9
|
+
* (`daemonSsePort: false`). `options` go to every create: `snapshotId`, `resources: { vcpus }` or `envs`.
|
|
10
|
+
*/
|
|
11
|
+
export declare const vercelLayer: (config?: VercelConfig, options?: CreateInstanceOptions) => Layer.Layer<Sandbox>;
|
package/dist/vercel.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { vercel } from "@computesdk/vercel";
|
|
2
|
+
import { computeLayer } from "./compute.js";
|
|
3
|
+
/**
|
|
4
|
+
* Vercel Sandbox, one microVM per sandbox. Auth is `VERCEL_OIDC_TOKEN` (`vercel env pull`), or `token`, `teamId` and
|
|
5
|
+
* `projectId`. Each sandbox stops itself after 10 min, also when a destroy failed, and exposes no daemon port
|
|
6
|
+
* (`daemonSsePort: false`). `options` go to every create: `snapshotId`, `resources: { vcpus }` or `envs`.
|
|
7
|
+
*/
|
|
8
|
+
export const vercelLayer = (config = {}, options) => computeLayer(vercel({ timeout: 10 * 60_000, daemonSsePort: false, ...config }), options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@robotostudio/sandbox-runner",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "One Effect port to run commands in a sandbox, with a Layer per ComputeSDK provider: Docker, Vercel Sandbox and a fake for tests.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/robotostudio/platform.git",
|
|
9
|
+
"directory": "packages/sandbox-runner"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"!dist/**/*.test.*",
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./docker": {
|
|
24
|
+
"types": "./dist/docker.d.ts",
|
|
25
|
+
"default": "./dist/docker.js"
|
|
26
|
+
},
|
|
27
|
+
"./vercel": {
|
|
28
|
+
"types": "./dist/vercel.d.ts",
|
|
29
|
+
"default": "./dist/vercel.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
37
|
+
"lint": "oxlint",
|
|
38
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
39
|
+
"test": "bun test"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@computesdk/docker": "1.2.77",
|
|
43
|
+
"@computesdk/vercel": "1.7.63",
|
|
44
|
+
"@robotostudio/tsconfig": "workspace:*",
|
|
45
|
+
"@types/dockerode": "4.0.2",
|
|
46
|
+
"@vercel/sandbox": "3.9.1",
|
|
47
|
+
"computesdk": "4.1.25",
|
|
48
|
+
"effect": "4.1.2"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@computesdk/docker": "^1.2.77",
|
|
52
|
+
"@computesdk/vercel": "^1.7.63",
|
|
53
|
+
"computesdk": "^4.1.25",
|
|
54
|
+
"effect": "^4.1.2"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"@computesdk/docker": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"@computesdk/vercel": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|