@run-cloud/sdk 0.5.0 → 0.6.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Newly
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
@@ -48,10 +48,9 @@ await writeFile(
48
48
  await cloud.sandboxes.destroy(sandbox.id);
49
49
  ```
50
50
 
51
- Ask for more than the default with either a preset or explicit resources:
51
+ Ask for the exact resources you need:
52
52
 
53
53
  ```ts
54
- await cloud.sandboxes.create({ size: "s-4" });
55
54
  await cloud.sandboxes.create({ cpu: 2, memory: 4096, disk: 40 });
56
55
  ```
57
56
 
@@ -70,18 +69,47 @@ Snapshot a sandbox and restore it later:
70
69
 
71
70
  ```ts
72
71
  const snap = await cloud.sandboxes.snapshot(sandbox.id, { label: "deps-installed" });
73
- const restored = await cloud.snapshots.restore(snap.id, { size: "s-1" });
72
+ const restored = await cloud.snapshots.restore(snap.id);
74
73
  ```
75
74
 
76
- A restore with no `size` boots the control plane's `s-2` preset (2 vCPU / 2 GiB),
77
- not the smaller default `sandboxes.create()` applies — pass `size` explicitly.
78
-
79
75
  Sandbox fields are returned in both the API's `snake_case` and camelCase
80
76
  (`milli_cpu` and `milliCpu` are both present).
81
77
 
82
78
  The client reads `RUN_CLOUD_API_KEY` first, then `RUN_CLOUD_API_TOKEN`. Override
83
79
  the API origin with `RUN_CLOUD_API_URL` or by passing `new Client({ apiUrl })`.
84
80
 
81
+ ## Compatibility adapters
82
+
83
+ Provider-shaped migration adapters ship in this same package:
84
+
85
+ | Provider | Import |
86
+ | --- | --- |
87
+ | Modal | `@run-cloud/sdk/compat/modal` |
88
+ | E2B | `@run-cloud/sdk/compat/e2b` |
89
+ | Daytona | `@run-cloud/sdk/compat/daytona` |
90
+ | Vercel Sandbox | `@run-cloud/sdk/compat/vercel` |
91
+ | Blaxel | `@run-cloud/sdk/compat/blaxel` |
92
+ | Fly Sprites | `@run-cloud/sdk/compat/sprites` |
93
+ | Cloudflare Sandbox | `@run-cloud/sdk/compat/cloudflare` |
94
+ | CodeSandbox | `@run-cloud/sdk/compat/codesandbox` |
95
+ | Fly Machines | `@run-cloud/sdk/compat/fly` |
96
+
97
+ ```ts
98
+ // import { Sandbox } from "@vercel/sandbox";
99
+ import { Sandbox } from "@run-cloud/sdk/compat/vercel";
100
+
101
+ const box = await Sandbox.create();
102
+ const result = await box.runCommand("node", ["-e", "console.log('hello')"]);
103
+ console.log(await result.stdout());
104
+ await box.stop();
105
+ ```
106
+
107
+ The adapters preserve core creation, lookup/listing, synchronous command
108
+ execution, pause/resume where the provider exposes it, and destruction.
109
+ Provider-specific features that do not have a public run.cloud equivalent throw
110
+ `UnsupportedCompatibilityFeatureError` with a migration hint. In particular,
111
+ public port capabilities and filesystem mutation are not silently emulated.
112
+
85
113
  `cloud.ios.screenshot(sessionId)` returns PNG bytes as a `Uint8Array`. Save the
86
114
  bytes directly with `node:fs/promises.writeFile`, attach them to test artifacts,
87
115
  or pass them to an image-processing library.
@@ -0,0 +1,49 @@
1
+ import { type CompatibilityClientOptions, type NativeSandbox } from "./common.js";
2
+ declare class Process {
3
+ private readonly native;
4
+ constructor(native: NativeSandbox);
5
+ exec(config: {
6
+ name?: string;
7
+ command: string;
8
+ workingDir?: string;
9
+ env?: Record<string, string>;
10
+ waitForCompletion?: boolean;
11
+ }): Promise<{
12
+ pid: number;
13
+ name: string;
14
+ status: string;
15
+ exitCode: number;
16
+ logs: string;
17
+ }>;
18
+ }
19
+ declare class FileSystem {
20
+ private readonly native;
21
+ constructor(native: NativeSandbox);
22
+ read(path: string): Promise<string>;
23
+ write(): never;
24
+ ls(): never;
25
+ mkdir(): never;
26
+ rm(): never;
27
+ }
28
+ export declare class SandboxInstance {
29
+ readonly native: NativeSandbox;
30
+ readonly process: Process;
31
+ readonly fs: FileSystem;
32
+ private constructor();
33
+ static create(options?: CompatibilityClientOptions & {
34
+ name?: string;
35
+ image?: string;
36
+ memory?: number;
37
+ cpu?: number;
38
+ region?: string;
39
+ ttl?: number | string;
40
+ }): Promise<SandboxInstance>;
41
+ static get(id: string, options?: CompatibilityClientOptions): Promise<SandboxInstance>;
42
+ static list(options?: CompatibilityClientOptions): Promise<SandboxInstance[]>;
43
+ get id(): string;
44
+ get previews(): {
45
+ createIfNotExists: () => never;
46
+ };
47
+ delete(): Promise<void>;
48
+ }
49
+ export {};
@@ -0,0 +1,85 @@
1
+ import { compatibilityClient, createNative, getNative, listNative, unsupported, } from "./common.js";
2
+ class Process {
3
+ native;
4
+ constructor(native) {
5
+ this.native = native;
6
+ }
7
+ async exec(config) {
8
+ if (config.waitForCompletion === false)
9
+ unsupported("Blaxel", "background processes");
10
+ const result = await this.native.exec(config.command, {
11
+ cwd: config.workingDir,
12
+ env: config.env,
13
+ });
14
+ return {
15
+ pid: 0,
16
+ name: config.name ?? "",
17
+ status: result.exitCode === 0 ? "completed" : "failed",
18
+ exitCode: result.exitCode,
19
+ logs: result.stdout + result.stderr,
20
+ };
21
+ }
22
+ }
23
+ class FileSystem {
24
+ native;
25
+ constructor(native) {
26
+ this.native = native;
27
+ }
28
+ async read(path) {
29
+ return new TextDecoder().decode(await this.native.readFile(path));
30
+ }
31
+ write() {
32
+ return unsupported("Blaxel", "file writes", "use an image or a command that writes the content");
33
+ }
34
+ ls() {
35
+ return unsupported("Blaxel", "directory listing");
36
+ }
37
+ mkdir() {
38
+ return unsupported("Blaxel", "directory creation");
39
+ }
40
+ rm() {
41
+ return unsupported("Blaxel", "file removal");
42
+ }
43
+ }
44
+ export class SandboxInstance {
45
+ native;
46
+ process;
47
+ fs;
48
+ constructor(native) {
49
+ this.native = native;
50
+ this.process = new Process(native);
51
+ this.fs = new FileSystem(native);
52
+ }
53
+ static async create(options = {}) {
54
+ const client = compatibilityClient(options);
55
+ const ttl = typeof options.ttl === "number" ? options.ttl : undefined;
56
+ return new SandboxInstance(await createNative(client, {
57
+ name: options.name,
58
+ image: options.image,
59
+ memory: options.memory,
60
+ cpu: options.cpu,
61
+ region: options.region,
62
+ idlePauseSeconds: ttl,
63
+ }));
64
+ }
65
+ static async get(id, options = {}) {
66
+ const client = compatibilityClient(options);
67
+ return new SandboxInstance(await getNative(client, id));
68
+ }
69
+ static async list(options = {}) {
70
+ const client = compatibilityClient(options);
71
+ return (await listNative(client)).map((sandbox) => new SandboxInstance(sandbox));
72
+ }
73
+ get id() {
74
+ return this.native.id;
75
+ }
76
+ get previews() {
77
+ return {
78
+ createIfNotExists: () => unsupported("Blaxel", "previews", "run.cloud public port capabilities are not available in the SDK yet"),
79
+ };
80
+ }
81
+ async delete() {
82
+ await this.native.destroy();
83
+ }
84
+ }
85
+ //# sourceMappingURL=blaxel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blaxel.js","sourceRoot":"","sources":["../../src/compat/blaxel.ts"],"names":[],"mappings":"AAAA,OAA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M,MAAM,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;QAC5C,OAAO,CAAC,MAAM,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;IACnF,CAAC;IAED,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ;QACV,OAAO;YACL,iBAAiB,EAAE,GAAG,EAAE,CACtB,WAAW,CACT,QAAQ,EACR,UAAU,EACV,qEAAqE,CACtE;SACJ,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM;QACV,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;IAC9B,CAAC;CACF"}
@@ -0,0 +1,32 @@
1
+ import type { Client } from "../index.js";
2
+ import { type CompatibilityClientOptions, type NativeSandbox } from "./common.js";
3
+ export declare class Sandbox {
4
+ private readonly client;
5
+ readonly id: string;
6
+ private ensured?;
7
+ constructor(client: Client, id: string);
8
+ private ensure;
9
+ get native(): Promise<NativeSandbox>;
10
+ exec(command: string, options?: {
11
+ cwd?: string;
12
+ env?: Record<string, string>;
13
+ }): Promise<{
14
+ success: boolean;
15
+ exitCode: number;
16
+ stdout: string;
17
+ stderr: string;
18
+ command: string;
19
+ duration: number;
20
+ }>;
21
+ readFile(path: string): Promise<{
22
+ content: string;
23
+ }>;
24
+ writeFile(): never;
25
+ mkdir(): never;
26
+ listFiles(): never;
27
+ deleteFile(): never;
28
+ renameFile(): never;
29
+ exposePort(): never;
30
+ destroy(): Promise<void>;
31
+ }
32
+ export declare function getSandbox(binding: Client | unknown, id: string, options?: CompatibilityClientOptions): Sandbox;
@@ -0,0 +1,66 @@
1
+ import { bytesToText, compatibilityClient, createNative, listNative, unsupported, } from "./common.js";
2
+ export class Sandbox {
3
+ client;
4
+ id;
5
+ ensured;
6
+ constructor(client, id) {
7
+ this.client = client;
8
+ this.id = id;
9
+ }
10
+ async ensure() {
11
+ if (!this.ensured) {
12
+ this.ensured = (async () => {
13
+ const existing = (await listNative(this.client)).find((sandbox) => sandbox.name === this.id);
14
+ return existing ?? (await createNative(this.client, { name: this.id }));
15
+ })();
16
+ }
17
+ return await this.ensured;
18
+ }
19
+ get native() {
20
+ return this.ensure();
21
+ }
22
+ async exec(command, options = {}) {
23
+ const startedAt = Date.now();
24
+ const result = await (await this.ensure()).exec(command, options);
25
+ return {
26
+ success: result.exitCode === 0,
27
+ exitCode: result.exitCode,
28
+ stdout: result.stdout,
29
+ stderr: result.stderr,
30
+ command,
31
+ duration: Date.now() - startedAt,
32
+ };
33
+ }
34
+ async readFile(path) {
35
+ return { content: bytesToText(await (await this.ensure()).readFile(path)) };
36
+ }
37
+ writeFile() {
38
+ return unsupported("Cloudflare Sandbox", "file writes", "use an image or command");
39
+ }
40
+ mkdir() {
41
+ return unsupported("Cloudflare Sandbox", "directory creation");
42
+ }
43
+ listFiles() {
44
+ return unsupported("Cloudflare Sandbox", "directory listing");
45
+ }
46
+ deleteFile() {
47
+ return unsupported("Cloudflare Sandbox", "file deletion");
48
+ }
49
+ renameFile() {
50
+ return unsupported("Cloudflare Sandbox", "file rename");
51
+ }
52
+ exposePort() {
53
+ return unsupported("Cloudflare Sandbox", "exposePort", "run.cloud public port capabilities are not available in the SDK yet");
54
+ }
55
+ async destroy() {
56
+ await (await this.ensure()).destroy();
57
+ }
58
+ }
59
+ export function getSandbox(binding, id, options = {}) {
60
+ const client = options.client ??
61
+ (binding && typeof binding === "object" && "sandboxes" in binding
62
+ ? binding
63
+ : compatibilityClient(options));
64
+ return new Sandbox(client, id);
65
+ }
66
+ //# sourceMappingURL=cloudflare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare.js","sourceRoot":"","sources":["../../src/compat/cloudflare.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,YAAY,EACZ,UAAU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@
1
+ import type { Client } from "../index.js";
2
+ import { type CompatibilityClientOptions, type NativeSandbox } from "./common.js";
3
+ export declare class CommandError extends Error {
4
+ readonly exitCode: number;
5
+ readonly output: string;
6
+ constructor(exitCode: number, output: string);
7
+ }
8
+ declare class Commands {
9
+ private readonly native;
10
+ constructor(native: NativeSandbox);
11
+ run(command: string | string[]): Promise<string>;
12
+ runBackground(): never;
13
+ }
14
+ declare class FileSystem {
15
+ private readonly native;
16
+ constructor(native: NativeSandbox);
17
+ readTextFile(path: string): Promise<string>;
18
+ readFile(path: string): Promise<Uint8Array>;
19
+ writeTextFile(): never;
20
+ writeFile(): never;
21
+ readdir(): never;
22
+ mkdir(): never;
23
+ remove(): never;
24
+ rename(): never;
25
+ }
26
+ export declare class SandboxClient {
27
+ readonly native: NativeSandbox;
28
+ readonly commands: Commands;
29
+ readonly fs: FileSystem;
30
+ constructor(native: NativeSandbox);
31
+ get id(): string;
32
+ get ports(): {
33
+ get: () => never;
34
+ getAll: () => Promise<never[]>;
35
+ waitForPort: () => never;
36
+ };
37
+ disconnect(): void;
38
+ }
39
+ export declare class Sandbox {
40
+ readonly native: NativeSandbox;
41
+ private readonly client;
42
+ constructor(native: NativeSandbox, client: Client);
43
+ get id(): string;
44
+ connect(): Promise<SandboxClient>;
45
+ }
46
+ declare class Sandboxes {
47
+ private readonly client;
48
+ constructor(client: Client);
49
+ create(options?: {
50
+ id?: string;
51
+ image?: string;
52
+ vmTier?: {
53
+ cpu?: number;
54
+ memGiB?: number;
55
+ };
56
+ title?: string;
57
+ region?: string;
58
+ }): Promise<Sandbox>;
59
+ resume(id: string): Promise<Sandbox>;
60
+ get(id: string): Promise<Sandbox>;
61
+ list(): Promise<Sandbox[]>;
62
+ hibernate(id: string): Promise<void>;
63
+ shutdown(id: string): Promise<void>;
64
+ delete(id: string): Promise<void>;
65
+ }
66
+ export declare class CodeSandbox {
67
+ readonly sandboxes: Sandboxes;
68
+ constructor(apiToken?: string, options?: CompatibilityClientOptions & {
69
+ baseUrl?: string;
70
+ });
71
+ }
72
+ export {};
@@ -0,0 +1,138 @@
1
+ import { bytesToText, compatibilityClient, createNative, getNative, listNative, unsupported, } from "./common.js";
2
+ export class CommandError extends Error {
3
+ exitCode;
4
+ output;
5
+ constructor(exitCode, output) {
6
+ super(`command failed with exit code ${exitCode}`);
7
+ this.exitCode = exitCode;
8
+ this.output = output;
9
+ this.name = "CommandError";
10
+ }
11
+ }
12
+ class Commands {
13
+ native;
14
+ constructor(native) {
15
+ this.native = native;
16
+ }
17
+ async run(command) {
18
+ const result = await this.native.exec(command);
19
+ const output = result.stdout + result.stderr;
20
+ if (result.exitCode !== 0)
21
+ throw new CommandError(result.exitCode, output);
22
+ return output;
23
+ }
24
+ runBackground() {
25
+ return unsupported("CodeSandbox", "background commands");
26
+ }
27
+ }
28
+ class FileSystem {
29
+ native;
30
+ constructor(native) {
31
+ this.native = native;
32
+ }
33
+ async readTextFile(path) {
34
+ return bytesToText(await this.native.readFile(path));
35
+ }
36
+ async readFile(path) {
37
+ return await this.native.readFile(path);
38
+ }
39
+ writeTextFile() {
40
+ return unsupported("CodeSandbox", "file writes", "use an image or command");
41
+ }
42
+ writeFile() {
43
+ return unsupported("CodeSandbox", "file writes", "use an image or command");
44
+ }
45
+ readdir() {
46
+ return unsupported("CodeSandbox", "directory listing");
47
+ }
48
+ mkdir() {
49
+ return unsupported("CodeSandbox", "directory creation");
50
+ }
51
+ remove() {
52
+ return unsupported("CodeSandbox", "file removal");
53
+ }
54
+ rename() {
55
+ return unsupported("CodeSandbox", "file rename");
56
+ }
57
+ }
58
+ export class SandboxClient {
59
+ native;
60
+ commands;
61
+ fs;
62
+ constructor(native) {
63
+ this.native = native;
64
+ this.commands = new Commands(native);
65
+ this.fs = new FileSystem(native);
66
+ }
67
+ get id() {
68
+ return this.native.id;
69
+ }
70
+ get ports() {
71
+ return {
72
+ get: () => unsupported("CodeSandbox", "ports.get", "run.cloud public port capabilities are not available in the SDK yet"),
73
+ getAll: async () => [],
74
+ waitForPort: () => unsupported("CodeSandbox", "ports.waitForPort"),
75
+ };
76
+ }
77
+ disconnect() { }
78
+ }
79
+ export class Sandbox {
80
+ native;
81
+ client;
82
+ constructor(native, client) {
83
+ this.native = native;
84
+ this.client = client;
85
+ }
86
+ get id() {
87
+ return this.native.id;
88
+ }
89
+ async connect() {
90
+ return new SandboxClient(this.native);
91
+ }
92
+ }
93
+ class Sandboxes {
94
+ client;
95
+ constructor(client) {
96
+ this.client = client;
97
+ }
98
+ async create(options = {}) {
99
+ return new Sandbox(await createNative(this.client, {
100
+ image: options.image ?? options.id,
101
+ cpu: options.vmTier?.cpu,
102
+ memory: options.vmTier?.memGiB ? options.vmTier.memGiB * 1024 : undefined,
103
+ name: options.title,
104
+ region: options.region,
105
+ }), this.client);
106
+ }
107
+ async resume(id) {
108
+ const native = await getNative(this.client, id);
109
+ await native.resume();
110
+ return new Sandbox(native, this.client);
111
+ }
112
+ async get(id) {
113
+ return new Sandbox(await getNative(this.client, id), this.client);
114
+ }
115
+ async list() {
116
+ return (await listNative(this.client)).map((sandbox) => new Sandbox(sandbox, this.client));
117
+ }
118
+ async hibernate(id) {
119
+ await (await getNative(this.client, id)).pause();
120
+ }
121
+ async shutdown(id) {
122
+ await (await getNative(this.client, id)).destroy();
123
+ }
124
+ async delete(id) {
125
+ await (await getNative(this.client, id)).destroy();
126
+ }
127
+ }
128
+ export class CodeSandbox {
129
+ sandboxes;
130
+ constructor(apiToken, options = {}) {
131
+ this.sandboxes = new Sandboxes(compatibilityClient({
132
+ ...options,
133
+ apiKey: options.apiKey ?? apiToken,
134
+ apiUrl: options.apiUrl ?? options.baseUrl,
135
+ }));
136
+ }
137
+ }
138
+ //# sourceMappingURL=codesandbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codesandbox.js","sourceRoot":"","sources":["../../src/compat/codesandbox.ts"],"names":[],"mappings":"AACA,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,YAAY,EACZ,SAAS,EACT,UAAU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G,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7F,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,EAAU;QACxB,MAAM,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,EAAU;QACvB,MAAM,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACrD,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,EAAU;QACrB,MAAM,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACrD,CAAC;CACF;AAED,MAAM,OAAO,WAAW;IACb,SAAS,CAAY;IAE9B,YACE,QAAiB,EACjB,UAA6D,EAAE;QAE/D,IAAI,CAAC,SAAS,GAAG,IAAI,SAAS,CAC5B,mBAAmB,CAAC;YAClB,GAAG,OAAO;YACV,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,QAAQ;YAClC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,OAAO;SAC1C,CAAC,CACH,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,40 @@
1
+ import { Client, type ExecOptions, type ExecResult, type Sandbox as SandboxRecord } from "../index.js";
2
+ export declare const DEFAULT_IMAGE = "runcloud/agent-base";
3
+ export declare class UnsupportedCompatibilityFeatureError extends Error {
4
+ constructor(provider: string, feature: string, alternative?: string);
5
+ }
6
+ export interface CompatibilityClientOptions {
7
+ client?: Client;
8
+ apiKey?: string;
9
+ apiUrl?: string;
10
+ fetch?: typeof fetch;
11
+ }
12
+ export declare function compatibilityClient(options?: CompatibilityClientOptions): Client;
13
+ export declare class NativeSandbox {
14
+ readonly client: Client;
15
+ private record;
16
+ constructor(client: Client, record: SandboxRecord);
17
+ get id(): string;
18
+ get name(): string;
19
+ get state(): string;
20
+ refresh(): Promise<this>;
21
+ exec(command: string | string[], options?: ExecOptions): Promise<ExecResult>;
22
+ readFile(path: string): Promise<Uint8Array>;
23
+ pause(): Promise<this>;
24
+ resume(): Promise<this>;
25
+ destroy(): Promise<void>;
26
+ }
27
+ export declare function createNative(client: Client, options?: {
28
+ image?: string;
29
+ cpu?: number;
30
+ memory?: number;
31
+ disk?: number;
32
+ idlePauseSeconds?: number;
33
+ timeoutSeconds?: number;
34
+ region?: string;
35
+ name?: string;
36
+ }): Promise<NativeSandbox>;
37
+ export declare function getNative(client: Client, id: string): Promise<NativeSandbox>;
38
+ export declare function listNative(client: Client): Promise<NativeSandbox[]>;
39
+ export declare function unsupported(provider: string, feature: string, alternative?: string): never;
40
+ export declare function bytesToText(bytes: Uint8Array): string;
@@ -0,0 +1,85 @@
1
+ import { Client, } from "../index.js";
2
+ export const DEFAULT_IMAGE = "runcloud/agent-base";
3
+ export class UnsupportedCompatibilityFeatureError extends Error {
4
+ constructor(provider, feature, alternative) {
5
+ super(`${provider} compatibility does not support ${feature} on run.cloud yet` +
6
+ (alternative ? `; ${alternative}` : ""));
7
+ this.name = "UnsupportedCompatibilityFeatureError";
8
+ }
9
+ }
10
+ export function compatibilityClient(options = {}) {
11
+ if (options.client)
12
+ return options.client;
13
+ const clientOptions = {
14
+ apiKey: options.apiKey,
15
+ apiUrl: options.apiUrl,
16
+ fetch: options.fetch,
17
+ };
18
+ return new Client(clientOptions);
19
+ }
20
+ export class NativeSandbox {
21
+ client;
22
+ record;
23
+ constructor(client, record) {
24
+ this.client = client;
25
+ this.record = record;
26
+ }
27
+ get id() {
28
+ return this.record.id;
29
+ }
30
+ get name() {
31
+ return typeof this.record.name === "string" ? this.record.name : "";
32
+ }
33
+ get state() {
34
+ return this.record.state;
35
+ }
36
+ async refresh() {
37
+ this.record = await this.client.sandboxes.get(this.id);
38
+ return this;
39
+ }
40
+ async exec(command, options = {}) {
41
+ return await this.client.sandboxes.exec(this.id, command, options);
42
+ }
43
+ async readFile(path) {
44
+ return await this.client.sandboxes.readFile(this.id, path);
45
+ }
46
+ async pause() {
47
+ this.record = await lifecycle(this.client, this.id, "pause");
48
+ return this;
49
+ }
50
+ async resume() {
51
+ this.record = await lifecycle(this.client, this.id, "resume");
52
+ return this;
53
+ }
54
+ async destroy() {
55
+ await this.client.sandboxes.destroy(this.id);
56
+ }
57
+ }
58
+ export async function createNative(client, options = {}) {
59
+ return new NativeSandbox(client, await client.sandboxes.create({
60
+ image: options.image ?? DEFAULT_IMAGE,
61
+ cpu: options.cpu,
62
+ memory: options.memory,
63
+ disk: options.disk,
64
+ idlePauseSeconds: options.idlePauseSeconds,
65
+ timeoutSeconds: options.timeoutSeconds,
66
+ region: options.region,
67
+ name: options.name,
68
+ }));
69
+ }
70
+ export async function getNative(client, id) {
71
+ return new NativeSandbox(client, await client.sandboxes.get(id));
72
+ }
73
+ export async function listNative(client) {
74
+ return (await client.sandboxes.list()).map((item) => new NativeSandbox(client, item));
75
+ }
76
+ async function lifecycle(client, id, action) {
77
+ return await client.request("POST", `/run-cloud/sandboxes/${encodeURIComponent(id)}/${action}`, {});
78
+ }
79
+ export function unsupported(provider, feature, alternative) {
80
+ throw new UnsupportedCompatibilityFeatureError(provider, feature, alternative);
81
+ }
82
+ export function bytesToText(bytes) {
83
+ return new TextDecoder().decode(bytes);
84
+ }
85
+ //# sourceMappingURL=common.js.map