withruntime 1.1.2 → 1.4.58
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/README.md +5 -2
- package/dist/generated/agentSessions.d.ts +2 -2
- package/dist/generated/agentSessions.js +2 -2
- package/dist/generated/agents.d.ts +2 -2
- package/dist/generated/agents.js +1 -1
- package/dist/generated/apps.d.ts +8 -6
- package/dist/generated/apps.js +10 -6
- package/dist/generated/browsers.d.ts +8 -6
- package/dist/generated/browsers.js +10 -6
- package/dist/generated/builds.d.ts +8 -6
- package/dist/generated/builds.js +10 -6
- package/dist/generated/ci.d.ts +106 -6
- package/dist/generated/ci.js +14 -6
- package/dist/generated/containers.d.ts +8 -6
- package/dist/generated/containers.js +10 -6
- package/dist/generated/desktops.d.ts +8 -6
- package/dist/generated/desktops.js +10 -6
- package/dist/generated/dropins/e2b.d.ts +180 -0
- package/dist/generated/dropins/e2b.js +342 -0
- package/dist/generated/dropins/e2b_code_interpreter.d.ts +180 -0
- package/dist/generated/dropins/e2b_code_interpreter.js +342 -0
- package/dist/generated/dropins/modal.d.ts +195 -0
- package/dist/generated/dropins/modal.js +375 -0
- package/dist/generated/dropins/runtime.d.ts +74 -0
- package/dist/generated/dropins/runtime.js +180 -0
- package/dist/generated/dropins/vercel.d.ts +78 -0
- package/dist/generated/dropins/vercel.js +154 -0
- package/dist/generated/egressIps.d.ts +2 -2
- package/dist/generated/egressIps.js +2 -2
- package/dist/generated/functions.d.ts +8 -6
- package/dist/generated/functions.js +10 -6
- package/dist/generated/identities.d.ts +42 -4
- package/dist/generated/identities.js +60 -0
- package/dist/generated/images.d.ts +3 -0
- package/dist/generated/images.js +4 -0
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/jobs.d.ts +2 -2
- package/dist/generated/jobs.js +1 -1
- package/dist/generated/keys.d.ts +28 -0
- package/dist/generated/keys.js +4 -0
- package/dist/generated/notebooks.d.ts +8 -6
- package/dist/generated/notebooks.js +10 -6
- package/dist/generated/ports.d.ts +2 -0
- package/dist/generated/projects.d.ts +49 -7
- package/dist/generated/projects.js +72 -3
- package/dist/generated/roles.d.ts +43 -5
- package/dist/generated/roles.js +61 -0
- package/dist/generated/routes.js +1 -1
- package/dist/generated/runtime.d.ts +49 -3
- package/dist/generated/runtime.js +235 -26
- package/dist/generated/sandboxes.d.ts +33 -14
- package/dist/generated/sandboxes.js +10 -6
- package/dist/generated/sites.d.ts +8 -6
- package/dist/generated/sites.js +10 -6
- package/dist/generated/snapshots.d.ts +7 -2
- package/dist/generated/snapshots.js +6 -2
- package/dist/generated/usage.d.ts +9 -3
- package/dist/generated/volumes.d.ts +30 -11
- package/dist/generated/volumes.js +19 -6
- package/dist/generated/workspaces.d.ts +8 -6
- package/dist/generated/workspaces.js +10 -6
- package/dist/v2/cli.d.ts +29 -0
- package/dist/v2/cli.js +156 -7
- package/dist/v2/login.d.ts +4 -0
- package/dist/v2/login.js +1 -1
- package/dist/v2/mcp.d.ts +4 -2
- package/dist/v2/mcp.js +69 -12
- package/dist/v2/openapi.json +2656 -1375
- package/dist/v2/shims/base.d.ts +0 -3
- package/dist/v2/shims/base.js +2 -7
- package/dist/v2/shims/blaxel.js +2 -1
- package/dist/v2/shims/daytona.js +3 -2
- package/dist/v2/shims/morph.js +3 -1
- package/dist/v2/shims/vercel.js +2 -1
- package/package.json +3 -2
- package/dist/v2/shims/e2b.d.ts +0 -142
- package/dist/v2/shims/e2b.js +0 -238
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
// Generated by spec-gen from engines/front/crates/spec. Do not edit:
|
|
2
|
+
// change the spec and run `cargo run -p runtime-front-spec-gen -- write`.
|
|
3
|
+
/* withruntime/modal: Modal Sandboxes on Runtime. Change the import from "modal" to "withruntime/modal"; RUNTIME_API_KEY replaces MODAL_TOKEN_ID and MODAL_TOKEN_SECRET. An app is a label; an image is a Runtime image or snapshot, or a registry image made into one once. */
|
|
4
|
+
import { Process, Refused, client, portUrl, refuseOthers, registryImage } from "./runtime.js";
|
|
5
|
+
export { AlreadyExistsError, NotFoundError } from "../index.js";
|
|
6
|
+
export class App {
|
|
7
|
+
name;
|
|
8
|
+
constructor(_k, v) {
|
|
9
|
+
this.name = v.name ?? "";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class Tunnel {
|
|
13
|
+
url;
|
|
14
|
+
port;
|
|
15
|
+
constructor(_k, v) {
|
|
16
|
+
this.url = v.url ?? "";
|
|
17
|
+
this.port = v.port ?? 0;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class FileInfo {
|
|
21
|
+
path;
|
|
22
|
+
name;
|
|
23
|
+
type;
|
|
24
|
+
size;
|
|
25
|
+
permissions;
|
|
26
|
+
constructor(_k, v) {
|
|
27
|
+
this.path = v.path ?? "";
|
|
28
|
+
this.name = v.name ?? "";
|
|
29
|
+
this.type = v.type ?? "";
|
|
30
|
+
this.size = v.size ?? 0;
|
|
31
|
+
this.permissions = v.permissions ?? "";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class Image {
|
|
35
|
+
#k;
|
|
36
|
+
tag;
|
|
37
|
+
imageId;
|
|
38
|
+
constructor(k, v) {
|
|
39
|
+
this.#k = k;
|
|
40
|
+
this.tag = v.tag ?? "";
|
|
41
|
+
this.imageId = v.imageId ?? "";
|
|
42
|
+
}
|
|
43
|
+
async build(_app) {
|
|
44
|
+
return new Image(this.#k, {
|
|
45
|
+
...(this.tag !== undefined ? { tag: this.tag } : {}),
|
|
46
|
+
imageId: await registryImage(this.#k, this),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** @deprecated Not served on Runtime: an image's build steps are images.create's dockerfile. */
|
|
50
|
+
dockerfileCommands(..._) {
|
|
51
|
+
throw new Refused("modal", "Image.dockerfileCommands", "an image's build steps are images.create's dockerfile");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export class ContainerProcess {
|
|
55
|
+
#p;
|
|
56
|
+
constructor(p) {
|
|
57
|
+
this.#p = p;
|
|
58
|
+
}
|
|
59
|
+
get stdout() {
|
|
60
|
+
const p = this.#p;
|
|
61
|
+
return { readText: () => p.readText("stdout") };
|
|
62
|
+
}
|
|
63
|
+
get stderr() {
|
|
64
|
+
const p = this.#p;
|
|
65
|
+
return { readText: () => p.readText("stderr") };
|
|
66
|
+
}
|
|
67
|
+
get stdin() {
|
|
68
|
+
const p = this.#p;
|
|
69
|
+
return { writeText: (data) => p.writeText(data), close: () => p.close() };
|
|
70
|
+
}
|
|
71
|
+
async wait() {
|
|
72
|
+
return this.#p.wait();
|
|
73
|
+
}
|
|
74
|
+
async poll() {
|
|
75
|
+
return this.#p.poll();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export class Sandbox {
|
|
79
|
+
sandboxId;
|
|
80
|
+
#k;
|
|
81
|
+
#command;
|
|
82
|
+
constructor(k, sandboxId, command) {
|
|
83
|
+
this.sandboxId = sandboxId;
|
|
84
|
+
this.#k = k;
|
|
85
|
+
this.#command = new Process(k, sandboxId, command);
|
|
86
|
+
}
|
|
87
|
+
get id() {
|
|
88
|
+
return this.sandboxId;
|
|
89
|
+
}
|
|
90
|
+
get stdout() {
|
|
91
|
+
const p = this.#command;
|
|
92
|
+
return { readText: () => p.readText("stdout") };
|
|
93
|
+
}
|
|
94
|
+
get stderr() {
|
|
95
|
+
const p = this.#command;
|
|
96
|
+
return { readText: () => p.readText("stderr") };
|
|
97
|
+
}
|
|
98
|
+
get stdin() {
|
|
99
|
+
const p = this.#command;
|
|
100
|
+
return { writeText: (data) => p.writeText(data), close: () => p.close() };
|
|
101
|
+
}
|
|
102
|
+
get filesystem() {
|
|
103
|
+
return new SandboxFilesystem(this.#k, this.id);
|
|
104
|
+
}
|
|
105
|
+
async wait() {
|
|
106
|
+
return this.#command.wait();
|
|
107
|
+
}
|
|
108
|
+
async poll() {
|
|
109
|
+
return this.#command.poll();
|
|
110
|
+
}
|
|
111
|
+
async exec(command, o = {}) {
|
|
112
|
+
refuseOthers("modal", "Sandbox.exec", o, ["env", "stderr", "stdout", "timeoutMs", "workdir"]);
|
|
113
|
+
const answer = await this.#k.sandboxes.exec(this.id, {
|
|
114
|
+
cmd: command,
|
|
115
|
+
...(o.workdir !== undefined ? { cwd: o.workdir } : {}),
|
|
116
|
+
...(o.env !== undefined ? { env: o.env } : {}),
|
|
117
|
+
...(o.timeoutMs !== undefined ? { timeoutSeconds: Math.ceil(o.timeoutMs / 1000) } : {}),
|
|
118
|
+
wait: false,
|
|
119
|
+
stdinOpen: true,
|
|
120
|
+
});
|
|
121
|
+
return new ContainerProcess(new Process(this.#k, this.id, answer.id));
|
|
122
|
+
}
|
|
123
|
+
async terminate() {
|
|
124
|
+
await this.#k.sandboxes.delete(this.id);
|
|
125
|
+
}
|
|
126
|
+
async snapshotFilesystem() {
|
|
127
|
+
const answer = await this.#k.sandboxes.snapshot(this.id, {});
|
|
128
|
+
return new Image(this.#k, {
|
|
129
|
+
...(answer.id !== undefined ? { imageId: answer.id } : {}),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async updateNetworkPolicy(p) {
|
|
133
|
+
refuseOthers("modal", "Sandbox.updateNetworkPolicy", p, ["outboundCidrAllowlist", "outboundDomainAllowlist"]);
|
|
134
|
+
await this.#k.sandboxes.setNetwork(this.id, {
|
|
135
|
+
...((p.outboundDomainAllowlist !== undefined || p.outboundCidrAllowlist !== undefined) ? { allowOutbound: [...(p.outboundDomainAllowlist ?? []), ...(p.outboundCidrAllowlist ?? [])] } : {}),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** @deprecated Not served on Runtime: a directory is snapshotted with its whole disk (snapshotFilesystem). */
|
|
139
|
+
snapshotDirectory(..._) {
|
|
140
|
+
throw new Refused("modal", "Sandbox.snapshotDirectory", "a directory is snapshotted with its whole disk (snapshotFilesystem)");
|
|
141
|
+
}
|
|
142
|
+
/** @deprecated Not served on Runtime: an image cannot be mounted at a path in a running sandbox. */
|
|
143
|
+
mountImage(..._) {
|
|
144
|
+
throw new Refused("modal", "Sandbox.mountImage", "an image cannot be mounted at a path in a running sandbox");
|
|
145
|
+
}
|
|
146
|
+
/** @deprecated Not served on Runtime: an image cannot be mounted at a path in a running sandbox. */
|
|
147
|
+
unmountImage(..._) {
|
|
148
|
+
throw new Refused("modal", "Sandbox.unmountImage", "an image cannot be mounted at a path in a running sandbox");
|
|
149
|
+
}
|
|
150
|
+
async tunnels() {
|
|
151
|
+
const answer = await this.#k.previews.list({
|
|
152
|
+
sandboxId: this.id,
|
|
153
|
+
});
|
|
154
|
+
return Object.fromEntries(await Promise.all(answer.items.map(async (item) => [item.port, new Tunnel(this.#k, {
|
|
155
|
+
...(item.port !== undefined ? { url: await portUrl(this.#k, this.id, item.port) } : {}),
|
|
156
|
+
...(item.port !== undefined ? { port: item.port } : {}),
|
|
157
|
+
})])));
|
|
158
|
+
}
|
|
159
|
+
/** @deprecated Not served on Runtime: a preview's token is sent as x-runtime-preview-token. */
|
|
160
|
+
createConnectToken(..._) {
|
|
161
|
+
throw new Refused("modal", "Sandbox.createConnectToken", "a preview's token is sent as x-runtime-preview-token");
|
|
162
|
+
}
|
|
163
|
+
/** @deprecated Not served on Runtime: a sidecar is a second sandbox. */
|
|
164
|
+
get experimentalSidecars() {
|
|
165
|
+
throw new Refused("modal", "Sandbox.experimentalSidecars", "a sidecar is a second sandbox");
|
|
166
|
+
}
|
|
167
|
+
/** @deprecated Not served on Runtime: a sandbox is snapshotted with snapshotFilesystem before it ends. */
|
|
168
|
+
experimentalGetExitSnapshot(..._) {
|
|
169
|
+
throw new Refused("modal", "Sandbox.experimentalGetExitSnapshot", "a sandbox is snapshotted with snapshotFilesystem before it ends");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
export class SandboxFilesystem {
|
|
173
|
+
id;
|
|
174
|
+
#k;
|
|
175
|
+
constructor(k, id) {
|
|
176
|
+
this.id = id;
|
|
177
|
+
this.#k = k;
|
|
178
|
+
}
|
|
179
|
+
async writeText(text, path) {
|
|
180
|
+
await this.#k.sandboxes.write(this.id, text, {
|
|
181
|
+
path: path,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async writeBytes(bytes, path) {
|
|
185
|
+
await this.#k.sandboxes.write(this.id, bytes, {
|
|
186
|
+
path: path,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async readText(path) {
|
|
190
|
+
const answer = await this.#k.sandboxes.read(this.id, {
|
|
191
|
+
path: path,
|
|
192
|
+
});
|
|
193
|
+
return new TextDecoder().decode(answer);
|
|
194
|
+
}
|
|
195
|
+
async readBytes(path) {
|
|
196
|
+
const answer = await this.#k.sandboxes.read(this.id, {
|
|
197
|
+
path: path,
|
|
198
|
+
});
|
|
199
|
+
return answer;
|
|
200
|
+
}
|
|
201
|
+
async stat(path) {
|
|
202
|
+
const answer = await this.#k.sandboxes.stat(this.id, {
|
|
203
|
+
path: path,
|
|
204
|
+
});
|
|
205
|
+
return new FileInfo(this.#k, {
|
|
206
|
+
...(answer.path !== undefined ? { path: answer.path } : {}),
|
|
207
|
+
...(answer.name !== undefined ? { name: answer.name } : {}),
|
|
208
|
+
...(answer.type !== undefined ? { type: answer.type } : {}),
|
|
209
|
+
...(answer.size !== undefined ? { size: answer.size } : {}),
|
|
210
|
+
...(answer.mode !== undefined ? { permissions: answer.mode } : {}),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
async listFiles(path) {
|
|
214
|
+
const answer = await this.#k.sandboxes.listFiles(this.id, {
|
|
215
|
+
path: path,
|
|
216
|
+
});
|
|
217
|
+
return answer.items.map((item) => new FileInfo(this.#k, {
|
|
218
|
+
...(item.path !== undefined ? { path: item.path } : {}),
|
|
219
|
+
...(item.name !== undefined ? { name: item.name } : {}),
|
|
220
|
+
...(item.type !== undefined ? { type: item.type } : {}),
|
|
221
|
+
...(item.size !== undefined ? { size: item.size } : {}),
|
|
222
|
+
...(item.mode !== undefined ? { permissions: item.mode } : {}),
|
|
223
|
+
}));
|
|
224
|
+
}
|
|
225
|
+
async makeDirectory(path) {
|
|
226
|
+
await this.#k.sandboxes.mkdir(this.id, {
|
|
227
|
+
path: path,
|
|
228
|
+
parents: true,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
async remove(path, o = {}) {
|
|
232
|
+
refuseOthers("modal", "Sandbox.filesystem.remove", o, ["recursive"]);
|
|
233
|
+
await this.#k.sandboxes.remove(this.id, {
|
|
234
|
+
path: path,
|
|
235
|
+
...(o.recursive !== undefined ? { recursive: o.recursive } : {}),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/** @deprecated Not served on Runtime: a local file is read and written by the program itself, then sent with writeBytes or read with readBytes. */
|
|
239
|
+
copyFromLocal(..._) {
|
|
240
|
+
throw new Refused("modal", "Sandbox.filesystem.copyFromLocal", "a local file is read and written by the program itself, then sent with writeBytes or read with readBytes");
|
|
241
|
+
}
|
|
242
|
+
/** @deprecated Not served on Runtime: a local file is read and written by the program itself, then sent with writeBytes or read with readBytes. */
|
|
243
|
+
copyToLocal(..._) {
|
|
244
|
+
throw new Refused("modal", "Sandbox.filesystem.copyToLocal", "a local file is read and written by the program itself, then sent with writeBytes or read with readBytes");
|
|
245
|
+
}
|
|
246
|
+
/** @deprecated Not served on Runtime: a directory is not watched; read it again with listFiles. */
|
|
247
|
+
watch(..._) {
|
|
248
|
+
throw new Refused("modal", "Sandbox.filesystem.watch", "a directory is not watched; read it again with listFiles");
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
export class ModalClient {
|
|
252
|
+
#k;
|
|
253
|
+
apps;
|
|
254
|
+
images;
|
|
255
|
+
secrets;
|
|
256
|
+
sandboxes;
|
|
257
|
+
volumes;
|
|
258
|
+
constructor(o = {}) {
|
|
259
|
+
this.#k = client(o);
|
|
260
|
+
this.apps = new ModalClientApps(this.#k);
|
|
261
|
+
this.images = new ModalClientImages(this.#k);
|
|
262
|
+
this.secrets = new ModalClientSecrets(this.#k);
|
|
263
|
+
this.sandboxes = new ModalClientSandboxes(this.#k);
|
|
264
|
+
this.volumes = new ModalClientVolumes(this.#k);
|
|
265
|
+
}
|
|
266
|
+
/** @deprecated Not served on Runtime: a cloud bucket is not mounted into a sandbox. */
|
|
267
|
+
get cloudBucketMounts() {
|
|
268
|
+
throw new Refused("modal", "ModalClient.cloudBucketMounts", "a cloud bucket is not mounted into a sandbox");
|
|
269
|
+
}
|
|
270
|
+
/** @deprecated Not served on Runtime: a sandbox's outbound proxy is not chosen by name. */
|
|
271
|
+
get proxies() {
|
|
272
|
+
throw new Refused("modal", "ModalClient.proxies", "a sandbox's outbound proxy is not chosen by name");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
export class ModalClientApps {
|
|
276
|
+
#k;
|
|
277
|
+
constructor(k) {
|
|
278
|
+
this.#k = k;
|
|
279
|
+
}
|
|
280
|
+
fromName(name, _o = {}) {
|
|
281
|
+
refuseOthers("modal", "ModalClient.apps.fromName", _o, ["createIfMissing"]);
|
|
282
|
+
return new App(this.#k, {
|
|
283
|
+
name: name,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
export class ModalClientImages {
|
|
288
|
+
#k;
|
|
289
|
+
constructor(k) {
|
|
290
|
+
this.#k = k;
|
|
291
|
+
}
|
|
292
|
+
fromRegistry(tag, _secret) {
|
|
293
|
+
return new Image(this.#k, {
|
|
294
|
+
tag: tag,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
async fromId(imageId) {
|
|
298
|
+
const answer = await this.#k.images.get(imageId, {});
|
|
299
|
+
return new Image(this.#k, {
|
|
300
|
+
...(answer.id !== undefined ? { imageId: answer.id } : {}),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
async delete(imageId) {
|
|
304
|
+
await this.#k.images.delete(imageId);
|
|
305
|
+
}
|
|
306
|
+
/** @deprecated Not served on Runtime: an image from Amazon ECR is not pulled. */
|
|
307
|
+
fromAwsEcr(..._) {
|
|
308
|
+
throw new Refused("modal", "ModalClient.images.fromAwsEcr", "an image from Amazon ECR is not pulled");
|
|
309
|
+
}
|
|
310
|
+
/** @deprecated Not served on Runtime: an image from Google Artifact Registry is not pulled. */
|
|
311
|
+
fromGcpArtifactRegistry(..._) {
|
|
312
|
+
throw new Refused("modal", "ModalClient.images.fromGcpArtifactRegistry", "an image from Google Artifact Registry is not pulled");
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
export class ModalClientSecrets {
|
|
316
|
+
constructor(..._) { }
|
|
317
|
+
/** @deprecated Not served on Runtime: a Runtime secret is sent only to the hosts it names, never put in a sandbox's environment; pass env. */
|
|
318
|
+
fromName(..._) {
|
|
319
|
+
throw new Refused("modal", "ModalClient.secrets.fromName", "a Runtime secret is sent only to the hosts it names, never put in a sandbox's environment; pass env");
|
|
320
|
+
}
|
|
321
|
+
/** @deprecated Not served on Runtime: a Runtime secret is sent only to the hosts it names, never put in a sandbox's environment; pass env. */
|
|
322
|
+
fromObject(..._) {
|
|
323
|
+
throw new Refused("modal", "ModalClient.secrets.fromObject", "a Runtime secret is sent only to the hosts it names, never put in a sandbox's environment; pass env");
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
export class ModalClientSandboxes {
|
|
327
|
+
#k;
|
|
328
|
+
constructor(k) {
|
|
329
|
+
this.#k = k;
|
|
330
|
+
}
|
|
331
|
+
async create(app, image, o = {}) {
|
|
332
|
+
refuseOthers("modal", "ModalClient.sandboxes.create", o, ["command", "cpu", "encryptedPorts", "idleTimeoutMs", "memoryMiB", "name", "outboundCidrAllowlist", "outboundDomainAllowlist", "timeoutMs"]);
|
|
333
|
+
const answer = await this.#k.sandboxes.create({
|
|
334
|
+
from: await registryImage(this.#k, image),
|
|
335
|
+
user: "root",
|
|
336
|
+
...(app.name !== undefined ? { labels: { "modal-app": app.name } } : {}),
|
|
337
|
+
...(o.cpu !== undefined ? { vcpu: Math.max(1, Math.ceil(o.cpu)) } : {}),
|
|
338
|
+
...(o.memoryMiB !== undefined ? { memoryGB: o.memoryMiB / 1024 } : {}),
|
|
339
|
+
...(o.timeoutMs !== undefined ? { deleteAfterSeconds: Math.ceil(o.timeoutMs / 1000) } : {}),
|
|
340
|
+
...(o.idleTimeoutMs !== undefined ? { idlePauseSeconds: Math.max(60, Math.ceil(o.idleTimeoutMs / 1000)) } : {}),
|
|
341
|
+
...(o.name !== undefined ? { name: o.name } : {}),
|
|
342
|
+
...((o.outboundDomainAllowlist !== undefined || o.outboundCidrAllowlist !== undefined) ? { network: { allowOutbound: [...(o.outboundDomainAllowlist ?? []), ...(o.outboundCidrAllowlist ?? [])] } } : {}),
|
|
343
|
+
});
|
|
344
|
+
const command = o.command !== undefined ? await Process.start(this.#k, answer, o.command) : null;
|
|
345
|
+
for (const each of o.encryptedPorts ?? [])
|
|
346
|
+
await this.#k.previews.create(answer.id, {
|
|
347
|
+
port: each,
|
|
348
|
+
});
|
|
349
|
+
return new Sandbox(this.#k, answer.id, command);
|
|
350
|
+
}
|
|
351
|
+
async fromId(id) {
|
|
352
|
+
const answer = await this.#k.sandboxes.get(id, {});
|
|
353
|
+
return new Sandbox(this.#k, answer.id, Process.of(answer));
|
|
354
|
+
}
|
|
355
|
+
async fromName(_appName, name) {
|
|
356
|
+
const answer = await this.#k.sandboxes.get(name, {});
|
|
357
|
+
return new Sandbox(this.#k, answer.id, Process.of(answer));
|
|
358
|
+
}
|
|
359
|
+
/** @deprecated Not served on Runtime: a sandbox's experimental settings are not served. */
|
|
360
|
+
experimentalCreate(..._) {
|
|
361
|
+
throw new Refused("modal", "ModalClient.sandboxes.experimentalCreate", "a sandbox's experimental settings are not served");
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
export class ModalClientVolumes {
|
|
365
|
+
constructor(..._) { }
|
|
366
|
+
/** @deprecated Not served on Runtime: a volume is created by volumes.create; get-or-create by name is not yet served. */
|
|
367
|
+
fromName(..._) {
|
|
368
|
+
throw new Refused("modal", "ModalClient.volumes.fromName", "a volume is created by volumes.create; get-or-create by name is not yet served");
|
|
369
|
+
}
|
|
370
|
+
/** @deprecated Not served on Runtime: volumes are not yet mounted read-only or by sub-path. */
|
|
371
|
+
ephemeral(..._) {
|
|
372
|
+
throw new Refused("modal", "ModalClient.volumes.ephemeral", "volumes are not yet mounted read-only or by sub-path");
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
export default ModalClient;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Runtime } from "../index.js";
|
|
2
|
+
export interface RuntimeOptions {
|
|
3
|
+
/** Defaults to RUNTIME_API_KEY. */
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/** Defaults to RUNTIME_API_URL, then https://api.withruntime.com. */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
}
|
|
8
|
+
/** A rival's method Runtime does not serve: its name and why, thrown before anything is sent. */
|
|
9
|
+
export declare class Refused extends Error {
|
|
10
|
+
readonly rival: string;
|
|
11
|
+
readonly method: string;
|
|
12
|
+
readonly why: string;
|
|
13
|
+
constructor(rival: string, method: string, why: string);
|
|
14
|
+
}
|
|
15
|
+
/** Refuses, by name, an option of `bag` that `known` does not hold: a rival's option is mapped or refused, never
|
|
16
|
+
* dropped. */
|
|
17
|
+
export declare function refuseOthers(rival: string, method: string, bag: unknown, known: string[], under?: string): void;
|
|
18
|
+
/** The Runtime client the rival's options name. */
|
|
19
|
+
export declare function client(o?: RuntimeOptions): Runtime;
|
|
20
|
+
/** What a machine starts from for a rival's image: its `imageId` when Runtime made it (an image or a snapshot), else
|
|
21
|
+
* its `tag`, a Runtime image of that name as it is, or an OCI reference (`alpine:3.21`) made once into the image
|
|
22
|
+
* `oci-<ref>` (images.create's `image`, about 11 s) and kept, so a second sandbox from it starts at once. Waits for it
|
|
23
|
+
* to be ready. */
|
|
24
|
+
export declare function registryImage(k: Runtime, image: string | {
|
|
25
|
+
imageId?: string | undefined;
|
|
26
|
+
tag?: string | undefined;
|
|
27
|
+
}): Promise<string>;
|
|
28
|
+
/** A URL that opens a machine's port, as a rival's tunnel or host link is used (a plain fetch): its preview, shared
|
|
29
|
+
* when it is not (previews.create, asked again, answers the same preview), with its token in the query when it is
|
|
30
|
+
* private. */
|
|
31
|
+
export declare function portUrl(k: Runtime, sandbox: string, port: number): Promise<string>;
|
|
32
|
+
/** The functions a process's output is given to as it is written, by stream. */
|
|
33
|
+
type Feed = {
|
|
34
|
+
stdout?: ((text: string) => unknown) | undefined;
|
|
35
|
+
stderr?: ((text: string) => unknown) | undefined;
|
|
36
|
+
};
|
|
37
|
+
/** The verbs a process is read, written and signalled through: its machine's product's (a sandbox's, a notebook's). */
|
|
38
|
+
type Machines = Pick<Runtime["sandboxes"], "processOutput" | "writeProcess" | "signalProcess">;
|
|
39
|
+
/** A process in a sandbox, as a rival's process object reads it: its output in full once it ends, its input, and its
|
|
40
|
+
* exit. A sandbox's own command is the process its label `dropin-command` names; null when it has none. */
|
|
41
|
+
export declare class Process {
|
|
42
|
+
#private;
|
|
43
|
+
static readonly LABEL = "dropin-command";
|
|
44
|
+
/** Its exit code once a wait or a poll has seen it end; null before. */
|
|
45
|
+
exitCode: number | null;
|
|
46
|
+
constructor(k: Runtime, sandbox: string, id: string | null, machines?: Machines);
|
|
47
|
+
/** Starts `cmd` in the sandbox as its own command, named by its label, and gives the process's id. */
|
|
48
|
+
static start(k: Runtime, sandbox: {
|
|
49
|
+
id: string;
|
|
50
|
+
labels: Record<string, string>;
|
|
51
|
+
}, cmd: string | string[]): Promise<string>;
|
|
52
|
+
/** The id of a sandbox's own command, from its label. */
|
|
53
|
+
static of(sandbox: {
|
|
54
|
+
labels: Record<string, string>;
|
|
55
|
+
}): string | null;
|
|
56
|
+
/** Its id, null for a sandbox made with no command of its own. */
|
|
57
|
+
get id(): string | null;
|
|
58
|
+
/** One stream's text, or both streams' when none is named, once it ends. */
|
|
59
|
+
readText(stream?: "stdout" | "stderr"): Promise<string>;
|
|
60
|
+
wait(): Promise<number>;
|
|
61
|
+
/** Its end, read at once, each piece fed as it is written: both streams' text and its exit code, as exec's answer
|
|
62
|
+
* names them. */
|
|
63
|
+
ran(feed?: Feed): Promise<{
|
|
64
|
+
stdout: string;
|
|
65
|
+
stderr: string;
|
|
66
|
+
exitCode: number;
|
|
67
|
+
}>;
|
|
68
|
+
signal(signal?: string): Promise<void>;
|
|
69
|
+
/** Its exit code, or null while it runs. */
|
|
70
|
+
poll(): Promise<number | null>;
|
|
71
|
+
writeText(text: string): Promise<void>;
|
|
72
|
+
close(): Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Generated by spec-gen from engines/front/crates/spec. Do not edit:
|
|
2
|
+
// change the spec and run `cargo run -p runtime-front-spec-gen -- write`.
|
|
3
|
+
/* What every drop-in shares (spec-gen's dropins): a Runtime from the rival's options or the environment, and the
|
|
4
|
+
refusal a method Runtime does not serve throws. The helpers here are a closed list of at most six; anything a
|
|
5
|
+
rival needs beyond them is a spec verb or a refusal (spec-gen dropins/mod.rs). */
|
|
6
|
+
import { AlreadyExistsError, NotFoundError, Runtime } from "../index.js";
|
|
7
|
+
/** A rival's method Runtime does not serve: its name and why, thrown before anything is sent. */
|
|
8
|
+
export class Refused extends Error {
|
|
9
|
+
rival;
|
|
10
|
+
method;
|
|
11
|
+
why;
|
|
12
|
+
constructor(rival, method, why) {
|
|
13
|
+
super(`${rival}'s ${method} is not served on Runtime: ${why}`);
|
|
14
|
+
this.rival = rival;
|
|
15
|
+
this.method = method;
|
|
16
|
+
this.why = why;
|
|
17
|
+
this.name = "Refused";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Refuses, by name, an option of `bag` that `known` does not hold: a rival's option is mapped or refused, never
|
|
21
|
+
* dropped. */
|
|
22
|
+
export function refuseOthers(rival, method, bag, known, under = "") {
|
|
23
|
+
if (typeof bag !== "object" || bag === null || Array.isArray(bag))
|
|
24
|
+
return;
|
|
25
|
+
for (const [key, value] of Object.entries(bag)) {
|
|
26
|
+
const at = under + key;
|
|
27
|
+
if (value === undefined || known.includes(at))
|
|
28
|
+
continue;
|
|
29
|
+
// An object some of whose keys are mapped (`source.snapshotId`): its other keys are refused one by one.
|
|
30
|
+
if (known.some((k) => k.startsWith(`${at}.`)))
|
|
31
|
+
refuseOthers(rival, method, value, known, `${at}.`);
|
|
32
|
+
else
|
|
33
|
+
throw new Refused(rival, `${method} option ${at}`, "it has no Runtime equivalent yet");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** The Runtime client the rival's options name. */
|
|
37
|
+
export function client(o = {}) {
|
|
38
|
+
return new Runtime({ ...(o.apiKey ? { apiKey: o.apiKey } : {}), ...(o.baseUrl ? { baseUrl: o.baseUrl } : {}) });
|
|
39
|
+
}
|
|
40
|
+
/** What a machine starts from for a rival's image: its `imageId` when Runtime made it (an image or a snapshot), else
|
|
41
|
+
* its `tag`, a Runtime image of that name as it is, or an OCI reference (`alpine:3.21`) made once into the image
|
|
42
|
+
* `oci-<ref>` (images.create's `image`, about 11 s) and kept, so a second sandbox from it starts at once. Waits for it
|
|
43
|
+
* to be ready. */
|
|
44
|
+
export async function registryImage(k, image) {
|
|
45
|
+
if (typeof image !== "string" && image.imageId)
|
|
46
|
+
return image.imageId;
|
|
47
|
+
const ref = typeof image === "string" ? image : (image.tag ?? "");
|
|
48
|
+
const ready = async (name) => {
|
|
49
|
+
let i = await k.images.get(name);
|
|
50
|
+
while (i.state === "making")
|
|
51
|
+
i = await k.images.get(name, { wait: 30, ifState: "making" });
|
|
52
|
+
if (i.state === "failed")
|
|
53
|
+
throw new Error(`The image ${ref} failed to build on Runtime (images.get ${name}).`);
|
|
54
|
+
return i.state === "ready";
|
|
55
|
+
};
|
|
56
|
+
try {
|
|
57
|
+
if (await ready(ref))
|
|
58
|
+
return ref;
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
if (!(e instanceof NotFoundError))
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
const name = `oci-${ref.toLowerCase().replace(/[^a-z0-9._-]+/g, "-")}`.slice(0, 64);
|
|
65
|
+
try {
|
|
66
|
+
if (await ready(name))
|
|
67
|
+
return name;
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
if (!(e instanceof NotFoundError))
|
|
71
|
+
throw e;
|
|
72
|
+
// Another sandbox starting from the same reference may make it first: then this one waits for that one.
|
|
73
|
+
const made = await k.images.create({ name, image: ref }).catch((c) => {
|
|
74
|
+
if (!(c instanceof AlreadyExistsError))
|
|
75
|
+
throw c;
|
|
76
|
+
return undefined;
|
|
77
|
+
});
|
|
78
|
+
if (await ready(made?.id ?? name))
|
|
79
|
+
return name;
|
|
80
|
+
}
|
|
81
|
+
throw new Error(`The image ${ref} is not ready on Runtime.`);
|
|
82
|
+
}
|
|
83
|
+
/** A URL that opens a machine's port, as a rival's tunnel or host link is used (a plain fetch): its preview, shared
|
|
84
|
+
* when it is not (previews.create, asked again, answers the same preview), with its token in the query when it is
|
|
85
|
+
* private. */
|
|
86
|
+
export async function portUrl(k, sandbox, port) {
|
|
87
|
+
const { info } = await k.previews.create(sandbox, { port });
|
|
88
|
+
return info.token ? `${info.url}?runtime_preview_token=${encodeURIComponent(info.token)}` : info.url;
|
|
89
|
+
}
|
|
90
|
+
/** A process in a sandbox, as a rival's process object reads it: its output in full once it ends, its input, and its
|
|
91
|
+
* exit. A sandbox's own command is the process its label `dropin-command` names; null when it has none. */
|
|
92
|
+
export class Process {
|
|
93
|
+
static LABEL = "dropin-command";
|
|
94
|
+
#m;
|
|
95
|
+
#sandbox;
|
|
96
|
+
#id;
|
|
97
|
+
/** Its exit code once a wait or a poll has seen it end; null before. */
|
|
98
|
+
exitCode = null;
|
|
99
|
+
constructor(k, sandbox, id, machines = k.sandboxes) {
|
|
100
|
+
this.#m = machines;
|
|
101
|
+
this.#sandbox = sandbox;
|
|
102
|
+
this.#id = id;
|
|
103
|
+
}
|
|
104
|
+
/** Starts `cmd` in the sandbox as its own command, named by its label, and gives the process's id. */
|
|
105
|
+
static async start(k, sandbox, cmd) {
|
|
106
|
+
const p = await k.sandboxes.exec(sandbox.id, { cmd, wait: false, stdinOpen: true });
|
|
107
|
+
const labels = Object.fromEntries(Object.entries(sandbox.labels).filter(([key]) => !key.startsWith("runtime.")));
|
|
108
|
+
await k.sandboxes.update(sandbox.id, { labels: { ...labels, [Process.LABEL]: p.id } });
|
|
109
|
+
return p.id;
|
|
110
|
+
}
|
|
111
|
+
/** The id of a sandbox's own command, from its label. */
|
|
112
|
+
static of(sandbox) {
|
|
113
|
+
return sandbox.labels[Process.LABEL] ?? null;
|
|
114
|
+
}
|
|
115
|
+
async #process() {
|
|
116
|
+
if (this.#id === null)
|
|
117
|
+
throw new Error(`Sandbox ${this.#sandbox} was made with no command of its own.`);
|
|
118
|
+
return this.#id;
|
|
119
|
+
}
|
|
120
|
+
/** Its id, null for a sandbox made with no command of its own. */
|
|
121
|
+
get id() {
|
|
122
|
+
return this.#id;
|
|
123
|
+
}
|
|
124
|
+
#end;
|
|
125
|
+
/** Its output read once, to its end, each piece given to its stream's function in `feed` as it is written (a rival's
|
|
126
|
+
* `onStdout`); output Runtime dropped is an error, never a gap. */
|
|
127
|
+
#read(feed = {}) {
|
|
128
|
+
this.#end ??= (async () => {
|
|
129
|
+
const id = await this.#process();
|
|
130
|
+
const seen = { text: "", stdout: "", stderr: "" };
|
|
131
|
+
for (let cursor = 0;;) {
|
|
132
|
+
const out = await this.#m.processOutput(this.#sandbox, id, { cursor, waitSeconds: 8 });
|
|
133
|
+
if (out.truncated)
|
|
134
|
+
throw new Error(`Process ${id} wrote ${out.droppedBytes} bytes more than Runtime keeps unread.`);
|
|
135
|
+
for (const c of out.chunks) {
|
|
136
|
+
const stream = c.stream === "stderr" ? "stderr" : "stdout";
|
|
137
|
+
seen.text += c.text;
|
|
138
|
+
seen[stream] += c.text;
|
|
139
|
+
feed[stream]?.(c.text);
|
|
140
|
+
}
|
|
141
|
+
cursor = out.nextCursor;
|
|
142
|
+
if (out.chunks.length === 0 && out.process && out.process.state !== "running")
|
|
143
|
+
return { ...seen, exitCode: (this.exitCode = out.process.exitCode ?? -1) };
|
|
144
|
+
}
|
|
145
|
+
})();
|
|
146
|
+
// Read in the background (a rival's background command): its failure reaches whoever awaits it, never the process.
|
|
147
|
+
this.#end.catch(() => undefined);
|
|
148
|
+
return this.#end;
|
|
149
|
+
}
|
|
150
|
+
/** One stream's text, or both streams' when none is named, once it ends. */
|
|
151
|
+
async readText(stream) {
|
|
152
|
+
return (await this.#read())[stream ?? "text"];
|
|
153
|
+
}
|
|
154
|
+
async wait() {
|
|
155
|
+
return (await this.#read()).exitCode;
|
|
156
|
+
}
|
|
157
|
+
/** Its end, read at once, each piece fed as it is written: both streams' text and its exit code, as exec's answer
|
|
158
|
+
* names them. */
|
|
159
|
+
ran(feed = {}) {
|
|
160
|
+
return this.#read(feed);
|
|
161
|
+
}
|
|
162
|
+
async signal(signal = "SIGTERM") {
|
|
163
|
+
await this.#m.signalProcess(this.#sandbox, await this.#process(), { signal });
|
|
164
|
+
}
|
|
165
|
+
/** Its exit code, or null while it runs. */
|
|
166
|
+
async poll() {
|
|
167
|
+
const id = await this.#process();
|
|
168
|
+
const p = (await this.#m.processOutput(this.#sandbox, id, { maxBytes: 4096 })).process;
|
|
169
|
+
return p && p.state !== "running" ? (this.exitCode = p.exitCode ?? -1) : null;
|
|
170
|
+
}
|
|
171
|
+
async writeText(text) {
|
|
172
|
+
let binary = "";
|
|
173
|
+
for (const b of new TextEncoder().encode(text))
|
|
174
|
+
binary += String.fromCharCode(b);
|
|
175
|
+
await this.#m.writeProcess(this.#sandbox, await this.#process(), { data: btoa(binary) });
|
|
176
|
+
}
|
|
177
|
+
async close() {
|
|
178
|
+
await this.#m.writeProcess(this.#sandbox, await this.#process(), { eof: true });
|
|
179
|
+
}
|
|
180
|
+
}
|