@computesdk/buddy 1.0.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 +209 -0
- package/dist/index.d.mts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +786 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +758 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 computesdk
|
|
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
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# @computesdk/buddy
|
|
2
|
+
|
|
3
|
+
Buddy provider for ComputeSDK.
|
|
4
|
+
|
|
5
|
+
Each sandbox is a Buddy sandbox — an Ubuntu microVM in your Buddy project, served from a pre-warmed pool. Commands stream their output as they run, ports are published through Buddy tunnels, and snapshots double as reusable base images.
|
|
6
|
+
|
|
7
|
+
Built on [`@buddy-works/sandbox-sdk`](https://www.npmjs.com/package/@buddy-works/sandbox-sdk), Buddy's official TypeScript SDK.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @computesdk/buddy
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Node 20.19 or newer is required, because the Buddy SDK ships ESM only.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
1. Create an API token in **Buddy → My ID → Access tokens** with the `SANDBOX_MANAGE` scope
|
|
20
|
+
2. Create (or pick) a Buddy project for your sandboxes
|
|
21
|
+
3. Set environment variables:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
export BUDDY_TOKEN=your_api_token_here
|
|
25
|
+
export BUDDY_WORKSPACE=your_workspace_domain
|
|
26
|
+
export BUDDY_PROJECT=your_project_name
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { buddy } from '@computesdk/buddy';
|
|
33
|
+
|
|
34
|
+
const compute = buddy();
|
|
35
|
+
|
|
36
|
+
const sandbox = await compute.sandbox.create();
|
|
37
|
+
|
|
38
|
+
const result = await sandbox.runCommand('echo "hello from buddy"');
|
|
39
|
+
console.log(result.stdout);
|
|
40
|
+
|
|
41
|
+
await sandbox.destroy();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Credentials are read from the environment, so `buddy()` needs no arguments. Pass them explicitly to override:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const compute = buddy({
|
|
48
|
+
token: process.env.BUDDY_TOKEN!,
|
|
49
|
+
workspace: 'acme',
|
|
50
|
+
project: 'sandboxes',
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
interface BuddyConfig {
|
|
58
|
+
/** API token with the `SANDBOX_MANAGE` scope. Falls back to `BUDDY_TOKEN`. */
|
|
59
|
+
token?: string;
|
|
60
|
+
/** Workspace domain. Falls back to `BUDDY_WORKSPACE`. */
|
|
61
|
+
workspace?: string;
|
|
62
|
+
/** Project the sandboxes belong to. Falls back to `BUDDY_PROJECT`. */
|
|
63
|
+
project?: string;
|
|
64
|
+
/** Base OS image — `'ubuntu:24.04'` (default) or `'ubuntu:22.04'`. */
|
|
65
|
+
os?: string;
|
|
66
|
+
/** Resource preset, `"{vCPU}x{RAM GB}"` from `1x2` to `12x24`. Buddy's own default applies when omitted. */
|
|
67
|
+
resources?: BuddyResources;
|
|
68
|
+
/** Buddy installation — `'US'` (default) or `'EU'`. */
|
|
69
|
+
region?: BuddyRegion;
|
|
70
|
+
/** Sandbox lifetime in milliseconds, after which Buddy stops it. Defaults to 1 hour. */
|
|
71
|
+
timeout?: number;
|
|
72
|
+
/** Ports to expose on every sandbox this provider creates. */
|
|
73
|
+
ports?: BuddyPortInput[];
|
|
74
|
+
/** API base URL. Overrides `region`, for on-premise installations. */
|
|
75
|
+
apiUrl?: string;
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Choosing an installation
|
|
80
|
+
|
|
81
|
+
Buddy runs independent installations rather than regions within one API, each with
|
|
82
|
+
its own host, accounts and tokens. `region` selects both the API host and where
|
|
83
|
+
tunnels terminate:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const compute = buddy({ region: 'EU' });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| `region` | API host |
|
|
90
|
+
| ------------ | ------------------------------ |
|
|
91
|
+
| `US` (default) | `https://api.buddy.works` |
|
|
92
|
+
| `EU` | `https://api.eu.buddy.works` |
|
|
93
|
+
|
|
94
|
+
A token issued by one installation is not valid on the others. For an on-premise
|
|
95
|
+
installation, set `apiUrl` instead.
|
|
96
|
+
|
|
97
|
+
## Create options
|
|
98
|
+
|
|
99
|
+
Beyond the cross-provider options, `create` accepts:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
await compute.sandbox.create({
|
|
103
|
+
// Expose ports up front, so getUrl needs no extra round trip
|
|
104
|
+
ports: [3000, { port: 443, type: 'TLS', region: 'EU' }],
|
|
105
|
+
// Resources — either a preset, or cpu/memory that map onto one
|
|
106
|
+
resources: '4x8',
|
|
107
|
+
// Commands run once when the sandbox first boots
|
|
108
|
+
firstBootCommands: 'npm ci',
|
|
109
|
+
// Working directory for the sandbox's apps
|
|
110
|
+
appDir: '/buddy/app',
|
|
111
|
+
tags: ['ci'],
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`snapshotId` (or `templateId`) starts the sandbox from a snapshot instead of a base image.
|
|
116
|
+
|
|
117
|
+
## Exposing ports
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const sandbox = await compute.sandbox.create({ ports: [3000] });
|
|
121
|
+
await sandbox.runCommand('npx serve -l 3000', { background: true });
|
|
122
|
+
|
|
123
|
+
const url = await sandbox.getUrl({ port: 3000 });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`getUrl` opens a tunnel on demand if the port was not declared at create time,
|
|
127
|
+
keeping any tunnels already open. Only `HTTP` and `TLS` tunnels get a public URL;
|
|
128
|
+
`TCP` and `SSH` tunnels are reached by host and port, reported in `getInfo`.
|
|
129
|
+
|
|
130
|
+
## Filesystem
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
await sandbox.filesystem.mkdir('/buddy/app');
|
|
134
|
+
await sandbox.filesystem.writeFile('/buddy/app/index.js', 'console.log(1)');
|
|
135
|
+
await sandbox.filesystem.readFile('/buddy/app/index.js');
|
|
136
|
+
await sandbox.filesystem.readdir('/buddy/app');
|
|
137
|
+
await sandbox.filesystem.exists('/buddy/app/index.js');
|
|
138
|
+
await sandbox.filesystem.remove('/buddy/app');
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Relative paths resolve against `/buddy`, the home directory of the default `buddy` user.
|
|
142
|
+
|
|
143
|
+
The first filesystem call on a sandbox waits for its first-boot setup to finish. Buddy
|
|
144
|
+
queues *commands* against a starting sandbox, but the content endpoints do not: during
|
|
145
|
+
setup they either refuse the request or accept a write and discard it once setup
|
|
146
|
+
completes. The wait costs a few hundred milliseconds on a freshly created sandbox and
|
|
147
|
+
nothing afterwards.
|
|
148
|
+
|
|
149
|
+
## Snapshots
|
|
150
|
+
|
|
151
|
+
A snapshot is a disk image taken from a live sandbox, and doubles as the reusable base
|
|
152
|
+
image other providers call a template.
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const snapshot = await compute.snapshot.create(sandbox.sandboxId, { name: 'with-deps' });
|
|
156
|
+
|
|
157
|
+
await compute.snapshot.list(); // the whole project
|
|
158
|
+
await compute.snapshot.list({ sandboxId: sandbox.sandboxId }); // one sandbox
|
|
159
|
+
await compute.snapshot.delete(snapshot.id);
|
|
160
|
+
|
|
161
|
+
// Booting from it is a normal create
|
|
162
|
+
const prepared = await compute.sandbox.create({ snapshotId: snapshot.id });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Buddy creates snapshots asynchronously and they cannot be restored until they turn
|
|
166
|
+
`CREATED`, so `create` waits for that before returning. `metadata` is accepted for
|
|
167
|
+
cross-provider compatibility but not stored: Buddy keeps only a name.
|
|
168
|
+
|
|
169
|
+
Buddy has no separate template entity, so `template.list` and `template.delete` alias
|
|
170
|
+
the snapshot methods and `template.create` throws, pointing at `snapshot.create`.
|
|
171
|
+
|
|
172
|
+
## Dropping down to the Buddy SDK
|
|
173
|
+
|
|
174
|
+
`getInstance()` returns the handle the provider works with, including the live SDK
|
|
175
|
+
client and filesystem, so Buddy features outside the ComputeSDK surface stay reachable
|
|
176
|
+
without building a second client:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
const { client, fs, sandboxId } = sandbox.getInstance();
|
|
180
|
+
await client.getSandboxAppLogs({ path: { sandbox_id: sandboxId, app_id: 'web' } });
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The handle's `fs` and `client` call the API directly, without the provider's first-boot wait: on a sandbox that has just been created, make one `sandbox.filesystem` call (even `exists`) before uploading through them.
|
|
184
|
+
|
|
185
|
+
## Supported Operations
|
|
186
|
+
|
|
187
|
+
| Method | Supported | Notes |
|
|
188
|
+
| ------------ | --------- | -------------------------------------------------------------------------------------------- |
|
|
189
|
+
| `create` | ✅ | One request; returns while the sandbox is still starting, since commands queue against it. |
|
|
190
|
+
| `getById` | ✅ | Returns `null` when the sandbox no longer exists. |
|
|
191
|
+
| `list` | ✅ | Lists the sandboxes in the configured project. |
|
|
192
|
+
| `destroy` | ✅ | Retries on server errors; a no-op if the sandbox is already gone. |
|
|
193
|
+
| `runCommand` | ✅ | Streams stdout/stderr separately as the command runs, with real exit codes. |
|
|
194
|
+
| `getInfo` | ✅ | Reports Buddy's own status and setup status, plus every tunnel, in `metadata`. |
|
|
195
|
+
| `getUrl` | ✅ | Opens a tunnel if the port has none yet, keeping the existing ones. |
|
|
196
|
+
| `filesystem` | ✅ | Uses Buddy's native content endpoints, not the shell — no quoting or size limits of a command line, one round trip each. |
|
|
197
|
+
| `snapshot` | ✅ | `create` / `list` / `delete`; `create` waits until the snapshot is restorable. |
|
|
198
|
+
| `template` | ⚠️ | No template entity in Buddy — `list`/`delete` alias snapshots, `create` throws with a hint. |
|
|
199
|
+
|
|
200
|
+
## Notes
|
|
201
|
+
|
|
202
|
+
- `create` does not wait for the sandbox to reach `RUNNING`. Buddy queues commands submitted against a starting sandbox, so waiting would only add latency to the first command. For the same reason the provider calls the SDK's `BuddyApiClient` directly instead of `Sandbox.create()`, which polls for readiness once a second.
|
|
203
|
+
- `runCommand(cmd, { background: true })` returns as soon as Buddy has queued the command, without reading its output.
|
|
204
|
+
- `runCommand(cmd, { timeout })` terminates the command when the timeout passes.
|
|
205
|
+
- Sandbox placement inside an installation is not selectable; `region` picks the installation and the tunnel location.
|
|
206
|
+
|
|
207
|
+
## License
|
|
208
|
+
|
|
209
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as _computesdk_provider from '@computesdk/provider';
|
|
2
|
+
import { RunCommandOptions } from '@computesdk/provider';
|
|
3
|
+
import { Snapshot } from 'computesdk';
|
|
4
|
+
import { BuddyApiClient, FileSystem } from '@buddy-works/sandbox-sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shared types, config resolution and SDK plumbing for the Buddy provider.
|
|
8
|
+
*
|
|
9
|
+
* All API access goes through `@buddy-works/sandbox-sdk`. The provider uses the
|
|
10
|
+
* SDK's `BuddyApiClient` rather than its high-level `Sandbox` façade for sandbox
|
|
11
|
+
* creation: `Sandbox.create()` waits for `setup_status: SUCCESS` and
|
|
12
|
+
* `status: RUNNING` with a 1 s poll, and since a sandbox from Buddy's warm pool
|
|
13
|
+
* is usable in ~400 ms that wait would dominate time-to-first-command. The
|
|
14
|
+
* façade's `Command`, `FileSystem` and snapshot helpers are used as-is.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Buddy installation, in the spelling its tunnel API uses. Each installation is
|
|
19
|
+
* a separate deployment with its own API host, workspaces and tokens — `US` is
|
|
20
|
+
* not a placement hint within one API.
|
|
21
|
+
*/
|
|
22
|
+
type BuddyRegion = 'US' | 'EU';
|
|
23
|
+
/** Interpreter Buddy runs a command with. `BASH` is the default. */
|
|
24
|
+
type BuddyCommandRuntime = 'BASH' | 'JAVASCRIPT' | 'TYPESCRIPT' | 'PYTHON';
|
|
25
|
+
/** Tunnel protocol. Only `HTTP` yields a public `endpoint_url`. */
|
|
26
|
+
type BuddyTunnelType = 'HTTP' | 'TLS' | 'TCP' | 'SSH';
|
|
27
|
+
/**
|
|
28
|
+
* Buddy resource preset, `"{vCPU}x{RAM in GB}"`. RAM is always 2 GB per vCPU.
|
|
29
|
+
*/
|
|
30
|
+
type BuddyResources = '1x2' | '2x4' | '3x6' | '4x8' | '5x10' | '6x12' | '7x14' | '8x16' | '9x18' | '10x20' | '11x22' | '12x24';
|
|
31
|
+
/** A port to expose publicly. A bare number means `{ port, type: 'HTTP' }`. */
|
|
32
|
+
interface BuddyPort {
|
|
33
|
+
port: number;
|
|
34
|
+
/** Tunnel name. Becomes the first label of the public hostname. */
|
|
35
|
+
name?: string;
|
|
36
|
+
type?: BuddyTunnelType;
|
|
37
|
+
region?: BuddyRegion;
|
|
38
|
+
}
|
|
39
|
+
type BuddyPortInput = number | BuddyPort;
|
|
40
|
+
interface BuddyConfig {
|
|
41
|
+
/** API token with the `SANDBOX_MANAGE` scope. Falls back to `BUDDY_TOKEN`. */
|
|
42
|
+
token?: string;
|
|
43
|
+
/** Workspace domain. Falls back to `BUDDY_WORKSPACE`. */
|
|
44
|
+
workspace?: string;
|
|
45
|
+
/** Project the sandboxes belong to. Falls back to `BUDDY_PROJECT`. */
|
|
46
|
+
project?: string;
|
|
47
|
+
/** Base OS image — `'ubuntu:24.04'` (default) or `'ubuntu:22.04'`. */
|
|
48
|
+
os?: string;
|
|
49
|
+
/** Resource preset. Buddy's own default applies when omitted. */
|
|
50
|
+
resources?: BuddyResources;
|
|
51
|
+
/**
|
|
52
|
+
* Buddy installation to use. Selects both the API host and where tunnels
|
|
53
|
+
* terminate. Defaults to `'US'`. A token is only valid on its own
|
|
54
|
+
* installation.
|
|
55
|
+
*/
|
|
56
|
+
region?: BuddyRegion;
|
|
57
|
+
/** Sandbox lifetime in milliseconds, after which Buddy stops it. */
|
|
58
|
+
timeout?: number;
|
|
59
|
+
/** Ports to expose on every sandbox this provider creates. */
|
|
60
|
+
ports?: BuddyPortInput[];
|
|
61
|
+
/** API base URL. Overrides `region` — set it for on-premise installations. */
|
|
62
|
+
apiUrl?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Config after env-var fallbacks and validation — what the methods receive. */
|
|
65
|
+
interface ResolvedBuddyConfig {
|
|
66
|
+
token: string;
|
|
67
|
+
workspace: string;
|
|
68
|
+
project: string;
|
|
69
|
+
os: string;
|
|
70
|
+
resources?: BuddyResources;
|
|
71
|
+
region: BuddyRegion;
|
|
72
|
+
timeout: number;
|
|
73
|
+
ports?: BuddyPortInput[];
|
|
74
|
+
apiUrl: string;
|
|
75
|
+
}
|
|
76
|
+
/** Buddy's tunnel object. `endpoint` holds the sandbox-side port as a string. */
|
|
77
|
+
interface BuddyEndpoint {
|
|
78
|
+
name?: string;
|
|
79
|
+
endpoint?: string;
|
|
80
|
+
type?: BuddyTunnelType;
|
|
81
|
+
/** As reported by the API, which may know installations this provider does not offer. */
|
|
82
|
+
region?: string;
|
|
83
|
+
endpoint_url?: string;
|
|
84
|
+
active?: boolean;
|
|
85
|
+
whitelist?: string[];
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What the provider passes between its own methods, and what `getInstance()`
|
|
90
|
+
* hands back to callers. `client` and `fs` come straight from the SDK, so
|
|
91
|
+
* consumers can drop down to it without building their own client.
|
|
92
|
+
*/
|
|
93
|
+
interface BuddySandboxHandle {
|
|
94
|
+
sandboxId: string;
|
|
95
|
+
identifier: string;
|
|
96
|
+
config: ResolvedBuddyConfig;
|
|
97
|
+
client: BuddyApiClient;
|
|
98
|
+
fs: FileSystem;
|
|
99
|
+
createdAt: Date;
|
|
100
|
+
/** True unless this process created the sandbox; Buddy reports no creation date. */
|
|
101
|
+
createdAtIsReconnectTime: boolean;
|
|
102
|
+
timeout: number;
|
|
103
|
+
os?: string;
|
|
104
|
+
resources?: string;
|
|
105
|
+
/** Tunnels known so far. `getUrl` extends this when it opens a new one. */
|
|
106
|
+
endpoints: BuddyEndpoint[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Command execution.
|
|
111
|
+
*
|
|
112
|
+
* Buddy accepts a command and returns its id immediately; output arrives over a
|
|
113
|
+
* JSONL log stream. The provider consumes that stream once through the SDK's
|
|
114
|
+
* `Command` entity, splits it into stdout/stderr and forwards chunks to the
|
|
115
|
+
* streaming callbacks, then reads the exit code from the command details.
|
|
116
|
+
* `Command.wait()` is deliberately unused — it polls once a second, which would
|
|
117
|
+
* add up to a second to every call.
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
interface BuddyRunCommandOptions extends RunCommandOptions {
|
|
121
|
+
/**
|
|
122
|
+
* Interpreter Buddy runs the command with. Defaults to `BASH`; the other
|
|
123
|
+
* runtimes take a script instead of a shell line, so `cwd` and `env` do not
|
|
124
|
+
* apply to them.
|
|
125
|
+
*/
|
|
126
|
+
runtime?: BuddyCommandRuntime;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Deletes a sandbox, retrying transient server failures. "Already gone" is the
|
|
131
|
+
* state the caller asked for, so it counts as success.
|
|
132
|
+
*/
|
|
133
|
+
declare function deleteSandbox(client: BuddyApiClient, sandboxId: string): Promise<void>;
|
|
134
|
+
declare const buddy: (config: BuddyConfig) => _computesdk_provider.Provider<BuddySandboxHandle, any, Snapshot>;
|
|
135
|
+
/** Buddy takes whole seconds; rounding up keeps the sandbox alive at least as long as asked. */
|
|
136
|
+
declare function toSeconds(ms: number): number;
|
|
137
|
+
/**
|
|
138
|
+
* Buddy's create-from-snapshot request has no `timeout` field (the SDK schema
|
|
139
|
+
* strips it), so a sandbox booted from a snapshot comes back with whatever the
|
|
140
|
+
* snapshot carried. Patch it afterwards when it differs from what was asked.
|
|
141
|
+
*/
|
|
142
|
+
declare function ensureTimeout(sandbox: BuddySandboxHandle, timeoutMs: number): Promise<void>;
|
|
143
|
+
|
|
144
|
+
export { type BuddyConfig, type BuddyRunCommandOptions, type BuddySandboxHandle as BuddySandbox, buddy, deleteSandbox, ensureTimeout, toSeconds };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as _computesdk_provider from '@computesdk/provider';
|
|
2
|
+
import { RunCommandOptions } from '@computesdk/provider';
|
|
3
|
+
import { Snapshot } from 'computesdk';
|
|
4
|
+
import { BuddyApiClient, FileSystem } from '@buddy-works/sandbox-sdk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shared types, config resolution and SDK plumbing for the Buddy provider.
|
|
8
|
+
*
|
|
9
|
+
* All API access goes through `@buddy-works/sandbox-sdk`. The provider uses the
|
|
10
|
+
* SDK's `BuddyApiClient` rather than its high-level `Sandbox` façade for sandbox
|
|
11
|
+
* creation: `Sandbox.create()` waits for `setup_status: SUCCESS` and
|
|
12
|
+
* `status: RUNNING` with a 1 s poll, and since a sandbox from Buddy's warm pool
|
|
13
|
+
* is usable in ~400 ms that wait would dominate time-to-first-command. The
|
|
14
|
+
* façade's `Command`, `FileSystem` and snapshot helpers are used as-is.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Buddy installation, in the spelling its tunnel API uses. Each installation is
|
|
19
|
+
* a separate deployment with its own API host, workspaces and tokens — `US` is
|
|
20
|
+
* not a placement hint within one API.
|
|
21
|
+
*/
|
|
22
|
+
type BuddyRegion = 'US' | 'EU';
|
|
23
|
+
/** Interpreter Buddy runs a command with. `BASH` is the default. */
|
|
24
|
+
type BuddyCommandRuntime = 'BASH' | 'JAVASCRIPT' | 'TYPESCRIPT' | 'PYTHON';
|
|
25
|
+
/** Tunnel protocol. Only `HTTP` yields a public `endpoint_url`. */
|
|
26
|
+
type BuddyTunnelType = 'HTTP' | 'TLS' | 'TCP' | 'SSH';
|
|
27
|
+
/**
|
|
28
|
+
* Buddy resource preset, `"{vCPU}x{RAM in GB}"`. RAM is always 2 GB per vCPU.
|
|
29
|
+
*/
|
|
30
|
+
type BuddyResources = '1x2' | '2x4' | '3x6' | '4x8' | '5x10' | '6x12' | '7x14' | '8x16' | '9x18' | '10x20' | '11x22' | '12x24';
|
|
31
|
+
/** A port to expose publicly. A bare number means `{ port, type: 'HTTP' }`. */
|
|
32
|
+
interface BuddyPort {
|
|
33
|
+
port: number;
|
|
34
|
+
/** Tunnel name. Becomes the first label of the public hostname. */
|
|
35
|
+
name?: string;
|
|
36
|
+
type?: BuddyTunnelType;
|
|
37
|
+
region?: BuddyRegion;
|
|
38
|
+
}
|
|
39
|
+
type BuddyPortInput = number | BuddyPort;
|
|
40
|
+
interface BuddyConfig {
|
|
41
|
+
/** API token with the `SANDBOX_MANAGE` scope. Falls back to `BUDDY_TOKEN`. */
|
|
42
|
+
token?: string;
|
|
43
|
+
/** Workspace domain. Falls back to `BUDDY_WORKSPACE`. */
|
|
44
|
+
workspace?: string;
|
|
45
|
+
/** Project the sandboxes belong to. Falls back to `BUDDY_PROJECT`. */
|
|
46
|
+
project?: string;
|
|
47
|
+
/** Base OS image — `'ubuntu:24.04'` (default) or `'ubuntu:22.04'`. */
|
|
48
|
+
os?: string;
|
|
49
|
+
/** Resource preset. Buddy's own default applies when omitted. */
|
|
50
|
+
resources?: BuddyResources;
|
|
51
|
+
/**
|
|
52
|
+
* Buddy installation to use. Selects both the API host and where tunnels
|
|
53
|
+
* terminate. Defaults to `'US'`. A token is only valid on its own
|
|
54
|
+
* installation.
|
|
55
|
+
*/
|
|
56
|
+
region?: BuddyRegion;
|
|
57
|
+
/** Sandbox lifetime in milliseconds, after which Buddy stops it. */
|
|
58
|
+
timeout?: number;
|
|
59
|
+
/** Ports to expose on every sandbox this provider creates. */
|
|
60
|
+
ports?: BuddyPortInput[];
|
|
61
|
+
/** API base URL. Overrides `region` — set it for on-premise installations. */
|
|
62
|
+
apiUrl?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Config after env-var fallbacks and validation — what the methods receive. */
|
|
65
|
+
interface ResolvedBuddyConfig {
|
|
66
|
+
token: string;
|
|
67
|
+
workspace: string;
|
|
68
|
+
project: string;
|
|
69
|
+
os: string;
|
|
70
|
+
resources?: BuddyResources;
|
|
71
|
+
region: BuddyRegion;
|
|
72
|
+
timeout: number;
|
|
73
|
+
ports?: BuddyPortInput[];
|
|
74
|
+
apiUrl: string;
|
|
75
|
+
}
|
|
76
|
+
/** Buddy's tunnel object. `endpoint` holds the sandbox-side port as a string. */
|
|
77
|
+
interface BuddyEndpoint {
|
|
78
|
+
name?: string;
|
|
79
|
+
endpoint?: string;
|
|
80
|
+
type?: BuddyTunnelType;
|
|
81
|
+
/** As reported by the API, which may know installations this provider does not offer. */
|
|
82
|
+
region?: string;
|
|
83
|
+
endpoint_url?: string;
|
|
84
|
+
active?: boolean;
|
|
85
|
+
whitelist?: string[];
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What the provider passes between its own methods, and what `getInstance()`
|
|
90
|
+
* hands back to callers. `client` and `fs` come straight from the SDK, so
|
|
91
|
+
* consumers can drop down to it without building their own client.
|
|
92
|
+
*/
|
|
93
|
+
interface BuddySandboxHandle {
|
|
94
|
+
sandboxId: string;
|
|
95
|
+
identifier: string;
|
|
96
|
+
config: ResolvedBuddyConfig;
|
|
97
|
+
client: BuddyApiClient;
|
|
98
|
+
fs: FileSystem;
|
|
99
|
+
createdAt: Date;
|
|
100
|
+
/** True unless this process created the sandbox; Buddy reports no creation date. */
|
|
101
|
+
createdAtIsReconnectTime: boolean;
|
|
102
|
+
timeout: number;
|
|
103
|
+
os?: string;
|
|
104
|
+
resources?: string;
|
|
105
|
+
/** Tunnels known so far. `getUrl` extends this when it opens a new one. */
|
|
106
|
+
endpoints: BuddyEndpoint[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Command execution.
|
|
111
|
+
*
|
|
112
|
+
* Buddy accepts a command and returns its id immediately; output arrives over a
|
|
113
|
+
* JSONL log stream. The provider consumes that stream once through the SDK's
|
|
114
|
+
* `Command` entity, splits it into stdout/stderr and forwards chunks to the
|
|
115
|
+
* streaming callbacks, then reads the exit code from the command details.
|
|
116
|
+
* `Command.wait()` is deliberately unused — it polls once a second, which would
|
|
117
|
+
* add up to a second to every call.
|
|
118
|
+
*/
|
|
119
|
+
|
|
120
|
+
interface BuddyRunCommandOptions extends RunCommandOptions {
|
|
121
|
+
/**
|
|
122
|
+
* Interpreter Buddy runs the command with. Defaults to `BASH`; the other
|
|
123
|
+
* runtimes take a script instead of a shell line, so `cwd` and `env` do not
|
|
124
|
+
* apply to them.
|
|
125
|
+
*/
|
|
126
|
+
runtime?: BuddyCommandRuntime;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Deletes a sandbox, retrying transient server failures. "Already gone" is the
|
|
131
|
+
* state the caller asked for, so it counts as success.
|
|
132
|
+
*/
|
|
133
|
+
declare function deleteSandbox(client: BuddyApiClient, sandboxId: string): Promise<void>;
|
|
134
|
+
declare const buddy: (config: BuddyConfig) => _computesdk_provider.Provider<BuddySandboxHandle, any, Snapshot>;
|
|
135
|
+
/** Buddy takes whole seconds; rounding up keeps the sandbox alive at least as long as asked. */
|
|
136
|
+
declare function toSeconds(ms: number): number;
|
|
137
|
+
/**
|
|
138
|
+
* Buddy's create-from-snapshot request has no `timeout` field (the SDK schema
|
|
139
|
+
* strips it), so a sandbox booted from a snapshot comes back with whatever the
|
|
140
|
+
* snapshot carried. Patch it afterwards when it differs from what was asked.
|
|
141
|
+
*/
|
|
142
|
+
declare function ensureTimeout(sandbox: BuddySandboxHandle, timeoutMs: number): Promise<void>;
|
|
143
|
+
|
|
144
|
+
export { type BuddyConfig, type BuddyRunCommandOptions, type BuddySandboxHandle as BuddySandbox, buddy, deleteSandbox, ensureTimeout, toSeconds };
|