disk 1.11.0 → 1.12.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/README.md +8 -0
- package/dist/{disk-BmBSYT8h.d.cts → archil-CLQrudfP.d.cts} +789 -5
- package/dist/{disk-BmBSYT8h.d.mts → archil-CLQrudfP.d.mts} +789 -5
- package/dist/cli.mjs +2 -3
- package/dist/index.cjs +242 -74
- package/dist/index.d.cts +2 -695
- package/dist/index.d.mts +2 -695
- package/dist/index.mjs +1 -1
- package/dist/internal/tools.d.cts +1 -1
- package/dist/internal/tools.d.mts +1 -1
- package/dist/sandbox-cli.mjs +5 -10
- package/dist/{sandbox-options-VBQ-AsOP.mjs → sandbox-options-Df_pRGJW.mjs} +375 -21
- package/dist/{src-BbjR-spO.mjs → src-CsxpFPq3.mjs} +242 -52
- package/package.json +14 -10
package/dist/cli.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { a as deleteApiKey, k as ArchilApiError, l as listApiKeys, n as createApiKey, r as createDisk, s as getDisk, t as configure, u as listDisks } from "./src-
|
|
3
|
-
import { a as addGlobalOptions, c as resolveProgramCredentials, l as renderTable, o as addProfileCommands, s as isProfileCommand, t as optionalInteger } from "./sandbox-options-
|
|
2
|
+
import { a as deleteApiKey, k as ArchilApiError, l as listApiKeys, n as createApiKey, r as createDisk, s as getDisk, t as configure, u as listDisks } from "./src-CsxpFPq3.mjs";
|
|
3
|
+
import { a as addGlobalOptions, c as resolveProgramCredentials, d as Command, l as renderTable, o as addProfileCommands, s as isProfileCommand, t as optionalInteger } from "./sandbox-options-Df_pRGJW.mjs";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
|
-
import { Command } from "commander";
|
|
6
5
|
//#region bin/cli.ts
|
|
7
6
|
const pkg = createRequire(import.meta.url)("../package.json");
|
|
8
7
|
const program = new Command();
|
package/dist/index.cjs
CHANGED
|
@@ -1,40 +1,165 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
//#region \0rolldown/runtime.js
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
23
|
-
//#endregion
|
|
24
2
|
const require_paths = require("./paths-1c4QqbOB.cjs");
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
//#region src/api-fetch.ts
|
|
4
|
+
function buildPath(template, params = {}) {
|
|
5
|
+
return template.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
6
|
+
const value = params[name];
|
|
7
|
+
if (value === void 0) throw new Error(`Missing path parameter '${name}' for ${template}`);
|
|
8
|
+
return encodeURIComponent(String(value));
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
/** Serialize a query object; undefined/null values are omitted and arrays repeat the key. */
|
|
12
|
+
function buildQuery(query = {}) {
|
|
13
|
+
const parts = [];
|
|
14
|
+
for (const [name, value] of Object.entries(query)) for (const item of Array.isArray(value) ? value : [value]) {
|
|
15
|
+
if (item === void 0 || item === null) continue;
|
|
16
|
+
parts.push(`${encodeURIComponent(name)}=${encodeURIComponent(String(item))}`);
|
|
17
|
+
}
|
|
18
|
+
return parts.length > 0 ? `?${parts.join("&")}` : "";
|
|
19
|
+
}
|
|
20
|
+
function createClient(config) {
|
|
21
|
+
const baseUrl = config.baseUrl.replace(/\/+$/, "");
|
|
22
|
+
const send = (request) => (config.fetch ?? globalThis.fetch)(request);
|
|
23
|
+
const headersFor = (extra = {}) => {
|
|
24
|
+
const headers = new Headers(config.headers);
|
|
25
|
+
for (const [name, value] of Object.entries(extra)) if (value !== void 0) headers.set(name, value);
|
|
26
|
+
return headers;
|
|
27
|
+
};
|
|
28
|
+
const call = async (method, path, init = {}) => {
|
|
29
|
+
const url = baseUrl + buildPath(path, init.params?.path) + buildQuery(init.params?.query);
|
|
30
|
+
const headers = headersFor(init.params?.header);
|
|
31
|
+
let body;
|
|
32
|
+
if (init.body !== void 0) {
|
|
33
|
+
body = JSON.stringify(init.body);
|
|
34
|
+
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json");
|
|
35
|
+
}
|
|
36
|
+
const response = await send(new Request(url, {
|
|
37
|
+
method,
|
|
38
|
+
headers,
|
|
39
|
+
body
|
|
40
|
+
}));
|
|
41
|
+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
|
|
42
|
+
await response.body?.cancel().catch(() => {});
|
|
43
|
+
return response.ok ? {
|
|
44
|
+
data: void 0,
|
|
45
|
+
response
|
|
46
|
+
} : {
|
|
47
|
+
error: void 0,
|
|
48
|
+
response
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (response.ok) return {
|
|
52
|
+
data: await response.json(),
|
|
53
|
+
response
|
|
54
|
+
};
|
|
55
|
+
const text = await response.text();
|
|
56
|
+
let error = text;
|
|
57
|
+
try {
|
|
58
|
+
error = JSON.parse(text);
|
|
59
|
+
} catch {}
|
|
60
|
+
return {
|
|
61
|
+
error,
|
|
62
|
+
response
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
const method = (name) => ((path, init) => call(name, path, init));
|
|
66
|
+
return {
|
|
67
|
+
GET: method("GET"),
|
|
68
|
+
PUT: method("PUT"),
|
|
69
|
+
POST: method("POST"),
|
|
70
|
+
DELETE: method("DELETE"),
|
|
71
|
+
PATCH: method("PATCH"),
|
|
72
|
+
raw: (methodName, url, init = {}) => send(new Request(url, {
|
|
73
|
+
method: methodName,
|
|
74
|
+
headers: headersFor(init.headers),
|
|
75
|
+
body: init.body
|
|
76
|
+
}))
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
27
80
|
//#region src/s3xml.ts
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
81
|
+
const ALWAYS_ARRAY = /* @__PURE__ */ new Set(["Contents", "CommonPrefixes"]);
|
|
82
|
+
const NAMED_ENTITIES = {
|
|
83
|
+
amp: "&",
|
|
84
|
+
lt: "<",
|
|
85
|
+
gt: ">",
|
|
86
|
+
quot: "\"",
|
|
87
|
+
apos: "'"
|
|
88
|
+
};
|
|
89
|
+
function decodeEntities(text) {
|
|
90
|
+
if (!text.includes("&")) return text;
|
|
91
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (match, ref) => {
|
|
92
|
+
if (ref[0] === "#") {
|
|
93
|
+
const code = ref[1] === "x" || ref[1] === "X" ? parseInt(ref.slice(2), 16) : parseInt(ref.slice(1), 10);
|
|
94
|
+
return code > 1114111 ? match : String.fromCodePoint(code);
|
|
95
|
+
}
|
|
96
|
+
return Object.hasOwn(NAMED_ENTITIES, ref) ? NAMED_ENTITIES[ref] : match;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function setOwn(target, name, value) {
|
|
100
|
+
Object.defineProperty(target, name, {
|
|
101
|
+
value,
|
|
102
|
+
enumerable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
configurable: true
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function addChild(parent, name, value) {
|
|
108
|
+
parent.hasChildren = true;
|
|
109
|
+
const existing = Object.hasOwn(parent.children, name) ? parent.children[name] : void 0;
|
|
110
|
+
if (existing === void 0) setOwn(parent.children, name, ALWAYS_ARRAY.has(name) ? [value] : value);
|
|
111
|
+
else if (Array.isArray(existing)) existing.push(value);
|
|
112
|
+
else setOwn(parent.children, name, [existing, value]);
|
|
113
|
+
}
|
|
114
|
+
/** Index just past `terminator`, searching from `from`; throws if it never appears. */
|
|
115
|
+
function skipPast(xml, terminator, from) {
|
|
116
|
+
const end = xml.indexOf(terminator, from);
|
|
117
|
+
if (end === -1) throw new Error(`Malformed XML: missing '${terminator}'`);
|
|
118
|
+
return end + terminator.length;
|
|
119
|
+
}
|
|
34
120
|
/** Parse an XML document into a plain object. Returns {} for empty/blank input. */
|
|
35
121
|
function parseXml(xml) {
|
|
36
|
-
|
|
37
|
-
|
|
122
|
+
const root = {
|
|
123
|
+
name: "",
|
|
124
|
+
children: {},
|
|
125
|
+
hasChildren: false,
|
|
126
|
+
text: ""
|
|
127
|
+
};
|
|
128
|
+
const stack = [root];
|
|
129
|
+
let position = 0;
|
|
130
|
+
while (position < xml.length) {
|
|
131
|
+
const current = stack[stack.length - 1];
|
|
132
|
+
const open = xml.indexOf("<", position);
|
|
133
|
+
current.text += decodeEntities(xml.slice(position, open === -1 ? xml.length : open));
|
|
134
|
+
if (open === -1) break;
|
|
135
|
+
if (xml.startsWith("<!--", open)) position = skipPast(xml, "-->", open + 4);
|
|
136
|
+
else if (xml.startsWith("<![CDATA[", open)) {
|
|
137
|
+
position = skipPast(xml, "]]>", open + 9);
|
|
138
|
+
current.text += xml.slice(open + 9, position - 3);
|
|
139
|
+
} else if (xml.startsWith("<?", open)) position = skipPast(xml, "?>", open + 2);
|
|
140
|
+
else if (xml.startsWith("<!", open)) throw new Error("Malformed XML: DOCTYPE and other declarations are not supported");
|
|
141
|
+
else {
|
|
142
|
+
position = skipPast(xml, ">", open + 1);
|
|
143
|
+
const tag = xml.slice(open + 1, position - 1);
|
|
144
|
+
const closing = tag.startsWith("/");
|
|
145
|
+
const selfClosing = !closing && tag.endsWith("/");
|
|
146
|
+
const name = /^[^\s/]+/.exec(closing ? tag.slice(1) : tag)?.[0];
|
|
147
|
+
if (!name) throw new Error(`Malformed XML: invalid tag <${tag}>`);
|
|
148
|
+
if (closing) {
|
|
149
|
+
if (current.name !== name || stack.length === 1) throw new Error(`Malformed XML: unexpected </${name}>`);
|
|
150
|
+
stack.pop();
|
|
151
|
+
addChild(stack[stack.length - 1], name, current.hasChildren ? current.children : current.text);
|
|
152
|
+
} else if (selfClosing) addChild(current, name, "");
|
|
153
|
+
else stack.push({
|
|
154
|
+
name,
|
|
155
|
+
children: {},
|
|
156
|
+
hasChildren: false,
|
|
157
|
+
text: ""
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (stack.length !== 1) throw new Error(`Malformed XML: unclosed <${stack[stack.length - 1].name}>`);
|
|
162
|
+
return root.children;
|
|
38
163
|
}
|
|
39
164
|
//#endregion
|
|
40
165
|
//#region src/errors.ts
|
|
@@ -163,15 +288,15 @@ function deriveS3BaseUrl(controlBaseUrl) {
|
|
|
163
288
|
}
|
|
164
289
|
//#endregion
|
|
165
290
|
//#region src/version.ts
|
|
166
|
-
const VERSION = "1.
|
|
291
|
+
const VERSION = "1.12.1";
|
|
167
292
|
const USER_AGENT = `archil-js/${VERSION}`;
|
|
168
293
|
//#endregion
|
|
169
294
|
//#region src/client.ts
|
|
170
|
-
const createClient = typeof openapi_fetch.default === "function" ? openapi_fetch.default : openapi_fetch.default.default;
|
|
171
295
|
function isNodeRuntime() {
|
|
172
296
|
return typeof process !== "undefined" && typeof process.versions?.node === "string";
|
|
173
297
|
}
|
|
174
|
-
const
|
|
298
|
+
const undiciSpecifier = isNodeRuntime() && process.versions.bun !== void 0 ? "undici/index.js" : "undici";
|
|
299
|
+
const undiciModule = isNodeRuntime() ? import(undiciSpecifier) : void 0;
|
|
175
300
|
const CONTROL_PLANE_CONNECTIONS = 1;
|
|
176
301
|
const IDLE_SESSION_TIMEOUT_MS = 3e4;
|
|
177
302
|
function isAsyncIterableBody(body) {
|
|
@@ -198,10 +323,10 @@ const bufferRequestBodies = (dispatch) => (options, handler) => {
|
|
|
198
323
|
return true;
|
|
199
324
|
};
|
|
200
325
|
const sharedTransports = /* @__PURE__ */ new Map();
|
|
201
|
-
function dispatcherKey(baseUrl,
|
|
326
|
+
function dispatcherKey(baseUrl, authorization, ca) {
|
|
202
327
|
return JSON.stringify([
|
|
203
328
|
new URL(baseUrl).origin,
|
|
204
|
-
|
|
329
|
+
authorization,
|
|
205
330
|
ca?.map((cert) => cert.toString("base64"))
|
|
206
331
|
]);
|
|
207
332
|
}
|
|
@@ -227,13 +352,13 @@ function getSharedTransport(key, ca) {
|
|
|
227
352
|
});
|
|
228
353
|
return transport;
|
|
229
354
|
}
|
|
230
|
-
function createPooledFetch(baseUrl,
|
|
355
|
+
function createPooledFetch(baseUrl, authorization, tls) {
|
|
231
356
|
if (!isNodeRuntime()) {
|
|
232
357
|
if (tls?.ca !== void 0) throw new Error("Custom TLS CAs require Node.js");
|
|
233
358
|
return;
|
|
234
359
|
}
|
|
235
360
|
const ca = tls?.ca === void 0 ? void 0 : (Array.isArray(tls.ca) ? tls.ca : [tls.ca]).map((cert) => Buffer.from(cert));
|
|
236
|
-
const key = dispatcherKey(baseUrl,
|
|
361
|
+
const key = dispatcherKey(baseUrl, authorization, ca);
|
|
237
362
|
return async (request) => {
|
|
238
363
|
const { fetch, dispatcher } = await getSharedTransport(key, ca);
|
|
239
364
|
const body = request.body === null ? null : new Uint8Array(await request.arrayBuffer());
|
|
@@ -247,14 +372,23 @@ function createPooledFetch(baseUrl, apiKey, tls) {
|
|
|
247
372
|
});
|
|
248
373
|
};
|
|
249
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* The `Authorization` value for an API key: the key exactly as given, as a
|
|
377
|
+
* bearer token. Credential-injecting proxies substitute a placeholder only when
|
|
378
|
+
* it is the whole token, so the SDK must not add a prefix to it. The control
|
|
379
|
+
* plane accepts the key with or without its `key-` prefix after `Bearer`.
|
|
380
|
+
*/
|
|
381
|
+
function apiKeyAuthorization(apiKey) {
|
|
382
|
+
return `Bearer ${apiKey}`;
|
|
383
|
+
}
|
|
250
384
|
function createApiClient(opts) {
|
|
251
385
|
const baseUrl = opts.baseUrl ?? resolveBaseUrl(opts.region);
|
|
252
|
-
const
|
|
386
|
+
const authorization = apiKeyAuthorization(opts.apiKey);
|
|
253
387
|
return createClient({
|
|
254
388
|
baseUrl,
|
|
255
|
-
fetch: createPooledFetch(baseUrl,
|
|
389
|
+
fetch: createPooledFetch(baseUrl, authorization, opts.tls),
|
|
256
390
|
headers: {
|
|
257
|
-
Authorization:
|
|
391
|
+
Authorization: authorization,
|
|
258
392
|
"User-Agent": USER_AGENT
|
|
259
393
|
}
|
|
260
394
|
});
|
|
@@ -294,6 +428,22 @@ async function unwrapEmpty(promise) {
|
|
|
294
428
|
}
|
|
295
429
|
if (!body.success) throw new ArchilApiError(body.error ?? "Unknown API error", response.status, body.code);
|
|
296
430
|
}
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region src/exec-options.ts
|
|
433
|
+
/**
|
|
434
|
+
* @internal Copies only the known option keys into a request body. Every key
|
|
435
|
+
* is required here, so an option added to the API fails to compile until it is
|
|
436
|
+
* copied.
|
|
437
|
+
*/
|
|
438
|
+
function execVmBody(options) {
|
|
439
|
+
return {
|
|
440
|
+
baseImage: options?.baseImage,
|
|
441
|
+
imageId: options?.imageId,
|
|
442
|
+
vcpuCount: options?.vcpuCount,
|
|
443
|
+
memSizeMib: options?.memSizeMib,
|
|
444
|
+
network: options?.network
|
|
445
|
+
};
|
|
446
|
+
}
|
|
297
447
|
const RETRY_BASE_MS = 100;
|
|
298
448
|
const RETRY_CAP_MS = 2e3;
|
|
299
449
|
const TRANSIENT_STATUSES = /* @__PURE__ */ new Set([
|
|
@@ -573,10 +723,13 @@ var Disk = class {
|
|
|
573
723
|
* Execute a command in a container with this disk mounted.
|
|
574
724
|
* Blocks until the command completes and returns stdout, stderr, and exit code.
|
|
575
725
|
*/
|
|
576
|
-
async exec(command) {
|
|
726
|
+
async exec(command, options) {
|
|
577
727
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
578
728
|
params: { path: { id: this.id } },
|
|
579
|
-
body: {
|
|
729
|
+
body: {
|
|
730
|
+
...execVmBody(options),
|
|
731
|
+
command
|
|
732
|
+
}
|
|
580
733
|
}), "connect"));
|
|
581
734
|
}
|
|
582
735
|
/**
|
|
@@ -923,29 +1076,22 @@ var Disk = class {
|
|
|
923
1076
|
*/
|
|
924
1077
|
async _s3Request(method, key, opts = {}) {
|
|
925
1078
|
if (!this._s3BaseUrl) throw new Error("S3 base URL not configured. Pass s3BaseUrl to new Archil({...}) or set ARCHIL_S3_BASE_URL.");
|
|
926
|
-
const call = this._client[method];
|
|
927
1079
|
const encodedKey = key.replace(/^\//, "").split("/").map(encodeURIComponent).join("/");
|
|
928
1080
|
const path = encodedKey ? `/${this.id}/${encodedKey}` : `/${this.id}`;
|
|
1081
|
+
const url = `${this._s3BaseUrl.replace(/\/+$/, "")}${path}${buildQuery(opts.query)}`;
|
|
929
1082
|
const init = {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
...opts.query ? { params: { query: opts.query } } : {},
|
|
933
|
-
...opts.body !== void 0 ? {
|
|
934
|
-
body: opts.body,
|
|
935
|
-
bodySerializer: (b) => b
|
|
936
|
-
} : {},
|
|
937
|
-
...opts.contentType || opts.extraHeaders ? { headers: {
|
|
1083
|
+
body: opts.body,
|
|
1084
|
+
headers: {
|
|
938
1085
|
...opts.contentType ? { "Content-Type": opts.contentType } : {},
|
|
939
1086
|
...opts.extraHeaders
|
|
940
|
-
}
|
|
1087
|
+
}
|
|
941
1088
|
};
|
|
942
1089
|
const hasBody = method === "GET" || method === "POST";
|
|
943
1090
|
const maxRetries = opts.retry === false ? 0 : 3;
|
|
944
1091
|
for (let attempt = 0;; attempt++) {
|
|
945
|
-
let error;
|
|
946
1092
|
let response;
|
|
947
1093
|
try {
|
|
948
|
-
|
|
1094
|
+
response = await this._client.raw(method, url, init);
|
|
949
1095
|
} catch (transportError) {
|
|
950
1096
|
if (attempt < maxRetries) {
|
|
951
1097
|
await retrySleep(attempt);
|
|
@@ -959,15 +1105,15 @@ var Disk = class {
|
|
|
959
1105
|
await retrySleep(attempt);
|
|
960
1106
|
continue;
|
|
961
1107
|
}
|
|
962
|
-
const message = typeof error === "string" ? error : error ? JSON.stringify(error) : "";
|
|
963
1108
|
return {
|
|
964
1109
|
ok: false,
|
|
965
1110
|
status: response.status,
|
|
966
1111
|
statusText: response.statusText,
|
|
967
1112
|
headers: response.headers,
|
|
968
|
-
body: new
|
|
1113
|
+
body: new Uint8Array(await response.arrayBuffer())
|
|
969
1114
|
};
|
|
970
1115
|
}
|
|
1116
|
+
if (!hasBody) await response.body?.cancel().catch(() => {});
|
|
971
1117
|
const bytes = hasBody ? new Uint8Array(await response.arrayBuffer()) : /* @__PURE__ */ new Uint8Array(0);
|
|
972
1118
|
return {
|
|
973
1119
|
ok: true,
|
|
@@ -1393,10 +1539,13 @@ var Disks = class {
|
|
|
1393
1539
|
* disk ID directly so callers that do not need its metadata can execute
|
|
1394
1540
|
* without fetching the disk first.
|
|
1395
1541
|
*/
|
|
1396
|
-
async exec(id, command) {
|
|
1542
|
+
async exec(id, command, options) {
|
|
1397
1543
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
1398
1544
|
params: { path: { id } },
|
|
1399
|
-
body: {
|
|
1545
|
+
body: {
|
|
1546
|
+
...execVmBody(options),
|
|
1547
|
+
command
|
|
1548
|
+
}
|
|
1400
1549
|
}), "connect"));
|
|
1401
1550
|
}
|
|
1402
1551
|
};
|
|
@@ -1474,6 +1623,15 @@ var Images = class {
|
|
|
1474
1623
|
//#endregion
|
|
1475
1624
|
//#region src/sandbox-process.ts
|
|
1476
1625
|
const PROCESS_STDIN_CHUNK_BYTES = 1024 * 1024;
|
|
1626
|
+
/** Close codes the runtime uses when it ends a connection to a running process. */
|
|
1627
|
+
const CLOSE_REASONS = {
|
|
1628
|
+
4001: "Process was taken over by another connection",
|
|
1629
|
+
4003: "Process owner disconnected"
|
|
1630
|
+
};
|
|
1631
|
+
/** The subprotocol the runtime selects for a process connection. */
|
|
1632
|
+
const CONNECT_SUBPROTOCOL = "archil.connect.v1";
|
|
1633
|
+
/** Prefix of the subprotocol that carries a connection token. */
|
|
1634
|
+
const TOKEN_SUBPROTOCOL_PREFIX = "archil.token.";
|
|
1477
1635
|
async function waitForSocketOpen(socket) {
|
|
1478
1636
|
if (socket.readyState === WebSocket.OPEN) return;
|
|
1479
1637
|
await new Promise((resolve, reject) => {
|
|
@@ -1499,12 +1657,13 @@ async function waitForSocketOpen(socket) {
|
|
|
1499
1657
|
socket.addEventListener("close", closed, { once: true });
|
|
1500
1658
|
});
|
|
1501
1659
|
}
|
|
1502
|
-
async function openProcessSocket(
|
|
1660
|
+
async function openProcessSocket(connectionEndpoint) {
|
|
1503
1661
|
for (let attempt = 0;; attempt++) {
|
|
1504
|
-
const
|
|
1662
|
+
const target = await connectionEndpoint();
|
|
1663
|
+
const endpoint = typeof target === "string" ? { url: target } : target;
|
|
1505
1664
|
let socket;
|
|
1506
1665
|
try {
|
|
1507
|
-
socket = new WebSocket(url);
|
|
1666
|
+
socket = endpoint.protocols ? new WebSocket(endpoint.url, endpoint.protocols) : new WebSocket(endpoint.url);
|
|
1508
1667
|
await waitForSocketOpen(socket);
|
|
1509
1668
|
return socket;
|
|
1510
1669
|
} catch (error) {
|
|
@@ -1540,7 +1699,7 @@ var SandboxProcess = class {
|
|
|
1540
1699
|
_cursor;
|
|
1541
1700
|
_onOutput;
|
|
1542
1701
|
_collectOutput;
|
|
1543
|
-
|
|
1702
|
+
_connectionEndpoint;
|
|
1544
1703
|
_controlProcess;
|
|
1545
1704
|
_stdoutDecoder = new TextDecoder();
|
|
1546
1705
|
_stderrDecoder = new TextDecoder();
|
|
@@ -1552,12 +1711,12 @@ var SandboxProcess = class {
|
|
|
1552
1711
|
_connectionError;
|
|
1553
1712
|
_stdinClosed = false;
|
|
1554
1713
|
/** @internal */
|
|
1555
|
-
constructor(processId, cursor, onOutput, collectOutput,
|
|
1714
|
+
constructor(processId, cursor, onOutput, collectOutput, connectionEndpoint, controlProcess) {
|
|
1556
1715
|
this._id = processId;
|
|
1557
1716
|
this._cursor = cursor;
|
|
1558
1717
|
this._onOutput = onOutput;
|
|
1559
1718
|
this._collectOutput = collectOutput;
|
|
1560
|
-
this.
|
|
1719
|
+
this._connectionEndpoint = connectionEndpoint;
|
|
1561
1720
|
this._controlProcess = controlProcess;
|
|
1562
1721
|
this._terminal = new Promise((resolve) => {
|
|
1563
1722
|
this._resolveTerminal = resolve;
|
|
@@ -1618,7 +1777,7 @@ var SandboxProcess = class {
|
|
|
1618
1777
|
}
|
|
1619
1778
|
/** @internal */
|
|
1620
1779
|
async _connect(request) {
|
|
1621
|
-
const socket = await openProcessSocket(this.
|
|
1780
|
+
const socket = await openProcessSocket(this._connectionEndpoint);
|
|
1622
1781
|
socket.binaryType = "arraybuffer";
|
|
1623
1782
|
this._socket = socket;
|
|
1624
1783
|
let resolveClosed;
|
|
@@ -1660,7 +1819,9 @@ var SandboxProcess = class {
|
|
|
1660
1819
|
this._connectionError = error;
|
|
1661
1820
|
fail(error);
|
|
1662
1821
|
});
|
|
1663
|
-
socket.addEventListener("close", () => {
|
|
1822
|
+
socket.addEventListener("close", (event) => {
|
|
1823
|
+
const reason = CLOSE_REASONS[event.code];
|
|
1824
|
+
if (reason && !this._result) this._connectionError = new Error(reason);
|
|
1664
1825
|
if (this._socket === socket) this._socket = void 0;
|
|
1665
1826
|
if (!readySettled) rejectReady(/* @__PURE__ */ new Error("Process connection closed before ready"));
|
|
1666
1827
|
resolveClosed();
|
|
@@ -2077,7 +2238,7 @@ var Sandbox = class Sandbox {
|
|
|
2077
2238
|
}
|
|
2078
2239
|
/** Start a process and return its handle without waiting for exit. */
|
|
2079
2240
|
async run(command, options = {}) {
|
|
2080
|
-
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2241
|
+
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2081
2242
|
const terminal = typeof options.terminal === "object" ? {
|
|
2082
2243
|
cols: options.terminal.cols ?? 80,
|
|
2083
2244
|
rows: options.terminal.rows ?? 24
|
|
@@ -2095,19 +2256,25 @@ var Sandbox = class Sandbox {
|
|
|
2095
2256
|
/** Reattach to a process, optionally resuming output from a cursor. */
|
|
2096
2257
|
async attach(processId, options = {}) {
|
|
2097
2258
|
const offset = options.offset ?? 0;
|
|
2098
|
-
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2259
|
+
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2099
2260
|
await process._connect({
|
|
2100
2261
|
type: "attach",
|
|
2101
2262
|
process_id: processId,
|
|
2102
|
-
offset
|
|
2263
|
+
offset,
|
|
2264
|
+
...options.shared ? { shared: true } : {}
|
|
2103
2265
|
});
|
|
2104
2266
|
return process;
|
|
2105
2267
|
}
|
|
2106
|
-
async
|
|
2107
|
-
|
|
2268
|
+
async _connectionEndpoint() {
|
|
2269
|
+
const data = await unwrap(retryApiRequest(() => this._client.POST("/api/sandboxes/{sid}/connections", { params: { path: { sid: this.id } } }), "transient"));
|
|
2270
|
+
if (data.endpoint && data.token) return {
|
|
2271
|
+
url: data.endpoint,
|
|
2272
|
+
protocols: [CONNECT_SUBPROTOCOL, `${TOKEN_SUBPROTOCOL_PREFIX}${data.token}`]
|
|
2273
|
+
};
|
|
2274
|
+
return { url: data.url };
|
|
2108
2275
|
}
|
|
2109
2276
|
async _control(request) {
|
|
2110
|
-
const socket = await openProcessSocket(() => this.
|
|
2277
|
+
const socket = await openProcessSocket(() => this._connectionEndpoint());
|
|
2111
2278
|
const expected = request.type === "kill" ? "killed" : "resized";
|
|
2112
2279
|
await new Promise((resolve, reject) => {
|
|
2113
2280
|
socket.addEventListener("message", (message) => {
|
|
@@ -2784,6 +2951,7 @@ var Archil = class {
|
|
|
2784
2951
|
disks[relPath] = entry;
|
|
2785
2952
|
} else disks[relPath] = diskIdFromMount(mount);
|
|
2786
2953
|
return unwrap(retryApiRequest(() => this._client.POST("/api/exec", { body: {
|
|
2954
|
+
...execVmBody(opts),
|
|
2787
2955
|
disks,
|
|
2788
2956
|
command: opts.command
|
|
2789
2957
|
} }), "connect"));
|