disk 1.11.0 → 1.12.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/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 +240 -73
- 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-Cz_oVr-y.mjs} +375 -21
- package/dist/{src-BbjR-spO.mjs → src-V9HdQW-c.mjs} +240 -51
- package/package.json +12 -9
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-V9HdQW-c.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-Cz_oVr-y.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,11 +288,10 @@ function deriveS3BaseUrl(controlBaseUrl) {
|
|
|
163
288
|
}
|
|
164
289
|
//#endregion
|
|
165
290
|
//#region src/version.ts
|
|
166
|
-
const VERSION = "1.
|
|
291
|
+
const VERSION = "1.12.0";
|
|
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
|
}
|
|
@@ -198,10 +322,10 @@ const bufferRequestBodies = (dispatch) => (options, handler) => {
|
|
|
198
322
|
return true;
|
|
199
323
|
};
|
|
200
324
|
const sharedTransports = /* @__PURE__ */ new Map();
|
|
201
|
-
function dispatcherKey(baseUrl,
|
|
325
|
+
function dispatcherKey(baseUrl, authorization, ca) {
|
|
202
326
|
return JSON.stringify([
|
|
203
327
|
new URL(baseUrl).origin,
|
|
204
|
-
|
|
328
|
+
authorization,
|
|
205
329
|
ca?.map((cert) => cert.toString("base64"))
|
|
206
330
|
]);
|
|
207
331
|
}
|
|
@@ -227,13 +351,13 @@ function getSharedTransport(key, ca) {
|
|
|
227
351
|
});
|
|
228
352
|
return transport;
|
|
229
353
|
}
|
|
230
|
-
function createPooledFetch(baseUrl,
|
|
354
|
+
function createPooledFetch(baseUrl, authorization, tls) {
|
|
231
355
|
if (!isNodeRuntime()) {
|
|
232
356
|
if (tls?.ca !== void 0) throw new Error("Custom TLS CAs require Node.js");
|
|
233
357
|
return;
|
|
234
358
|
}
|
|
235
359
|
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,
|
|
360
|
+
const key = dispatcherKey(baseUrl, authorization, ca);
|
|
237
361
|
return async (request) => {
|
|
238
362
|
const { fetch, dispatcher } = await getSharedTransport(key, ca);
|
|
239
363
|
const body = request.body === null ? null : new Uint8Array(await request.arrayBuffer());
|
|
@@ -247,14 +371,23 @@ function createPooledFetch(baseUrl, apiKey, tls) {
|
|
|
247
371
|
});
|
|
248
372
|
};
|
|
249
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* The `Authorization` value for an API key: the key exactly as given, as a
|
|
376
|
+
* bearer token. Credential-injecting proxies substitute a placeholder only when
|
|
377
|
+
* it is the whole token, so the SDK must not add a prefix to it. The control
|
|
378
|
+
* plane accepts the key with or without its `key-` prefix after `Bearer`.
|
|
379
|
+
*/
|
|
380
|
+
function apiKeyAuthorization(apiKey) {
|
|
381
|
+
return `Bearer ${apiKey}`;
|
|
382
|
+
}
|
|
250
383
|
function createApiClient(opts) {
|
|
251
384
|
const baseUrl = opts.baseUrl ?? resolveBaseUrl(opts.region);
|
|
252
|
-
const
|
|
385
|
+
const authorization = apiKeyAuthorization(opts.apiKey);
|
|
253
386
|
return createClient({
|
|
254
387
|
baseUrl,
|
|
255
|
-
fetch: createPooledFetch(baseUrl,
|
|
388
|
+
fetch: createPooledFetch(baseUrl, authorization, opts.tls),
|
|
256
389
|
headers: {
|
|
257
|
-
Authorization:
|
|
390
|
+
Authorization: authorization,
|
|
258
391
|
"User-Agent": USER_AGENT
|
|
259
392
|
}
|
|
260
393
|
});
|
|
@@ -294,6 +427,22 @@ async function unwrapEmpty(promise) {
|
|
|
294
427
|
}
|
|
295
428
|
if (!body.success) throw new ArchilApiError(body.error ?? "Unknown API error", response.status, body.code);
|
|
296
429
|
}
|
|
430
|
+
//#endregion
|
|
431
|
+
//#region src/exec-options.ts
|
|
432
|
+
/**
|
|
433
|
+
* @internal Copies only the known option keys into a request body. Every key
|
|
434
|
+
* is required here, so an option added to the API fails to compile until it is
|
|
435
|
+
* copied.
|
|
436
|
+
*/
|
|
437
|
+
function execVmBody(options) {
|
|
438
|
+
return {
|
|
439
|
+
baseImage: options?.baseImage,
|
|
440
|
+
imageId: options?.imageId,
|
|
441
|
+
vcpuCount: options?.vcpuCount,
|
|
442
|
+
memSizeMib: options?.memSizeMib,
|
|
443
|
+
network: options?.network
|
|
444
|
+
};
|
|
445
|
+
}
|
|
297
446
|
const RETRY_BASE_MS = 100;
|
|
298
447
|
const RETRY_CAP_MS = 2e3;
|
|
299
448
|
const TRANSIENT_STATUSES = /* @__PURE__ */ new Set([
|
|
@@ -573,10 +722,13 @@ var Disk = class {
|
|
|
573
722
|
* Execute a command in a container with this disk mounted.
|
|
574
723
|
* Blocks until the command completes and returns stdout, stderr, and exit code.
|
|
575
724
|
*/
|
|
576
|
-
async exec(command) {
|
|
725
|
+
async exec(command, options) {
|
|
577
726
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
578
727
|
params: { path: { id: this.id } },
|
|
579
|
-
body: {
|
|
728
|
+
body: {
|
|
729
|
+
...execVmBody(options),
|
|
730
|
+
command
|
|
731
|
+
}
|
|
580
732
|
}), "connect"));
|
|
581
733
|
}
|
|
582
734
|
/**
|
|
@@ -923,29 +1075,22 @@ var Disk = class {
|
|
|
923
1075
|
*/
|
|
924
1076
|
async _s3Request(method, key, opts = {}) {
|
|
925
1077
|
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
1078
|
const encodedKey = key.replace(/^\//, "").split("/").map(encodeURIComponent).join("/");
|
|
928
1079
|
const path = encodedKey ? `/${this.id}/${encodedKey}` : `/${this.id}`;
|
|
1080
|
+
const url = `${this._s3BaseUrl.replace(/\/+$/, "")}${path}${buildQuery(opts.query)}`;
|
|
929
1081
|
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: {
|
|
1082
|
+
body: opts.body,
|
|
1083
|
+
headers: {
|
|
938
1084
|
...opts.contentType ? { "Content-Type": opts.contentType } : {},
|
|
939
1085
|
...opts.extraHeaders
|
|
940
|
-
}
|
|
1086
|
+
}
|
|
941
1087
|
};
|
|
942
1088
|
const hasBody = method === "GET" || method === "POST";
|
|
943
1089
|
const maxRetries = opts.retry === false ? 0 : 3;
|
|
944
1090
|
for (let attempt = 0;; attempt++) {
|
|
945
|
-
let error;
|
|
946
1091
|
let response;
|
|
947
1092
|
try {
|
|
948
|
-
|
|
1093
|
+
response = await this._client.raw(method, url, init);
|
|
949
1094
|
} catch (transportError) {
|
|
950
1095
|
if (attempt < maxRetries) {
|
|
951
1096
|
await retrySleep(attempt);
|
|
@@ -959,15 +1104,15 @@ var Disk = class {
|
|
|
959
1104
|
await retrySleep(attempt);
|
|
960
1105
|
continue;
|
|
961
1106
|
}
|
|
962
|
-
const message = typeof error === "string" ? error : error ? JSON.stringify(error) : "";
|
|
963
1107
|
return {
|
|
964
1108
|
ok: false,
|
|
965
1109
|
status: response.status,
|
|
966
1110
|
statusText: response.statusText,
|
|
967
1111
|
headers: response.headers,
|
|
968
|
-
body: new
|
|
1112
|
+
body: new Uint8Array(await response.arrayBuffer())
|
|
969
1113
|
};
|
|
970
1114
|
}
|
|
1115
|
+
if (!hasBody) await response.body?.cancel().catch(() => {});
|
|
971
1116
|
const bytes = hasBody ? new Uint8Array(await response.arrayBuffer()) : /* @__PURE__ */ new Uint8Array(0);
|
|
972
1117
|
return {
|
|
973
1118
|
ok: true,
|
|
@@ -1393,10 +1538,13 @@ var Disks = class {
|
|
|
1393
1538
|
* disk ID directly so callers that do not need its metadata can execute
|
|
1394
1539
|
* without fetching the disk first.
|
|
1395
1540
|
*/
|
|
1396
|
-
async exec(id, command) {
|
|
1541
|
+
async exec(id, command, options) {
|
|
1397
1542
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
1398
1543
|
params: { path: { id } },
|
|
1399
|
-
body: {
|
|
1544
|
+
body: {
|
|
1545
|
+
...execVmBody(options),
|
|
1546
|
+
command
|
|
1547
|
+
}
|
|
1400
1548
|
}), "connect"));
|
|
1401
1549
|
}
|
|
1402
1550
|
};
|
|
@@ -1474,6 +1622,15 @@ var Images = class {
|
|
|
1474
1622
|
//#endregion
|
|
1475
1623
|
//#region src/sandbox-process.ts
|
|
1476
1624
|
const PROCESS_STDIN_CHUNK_BYTES = 1024 * 1024;
|
|
1625
|
+
/** Close codes the runtime uses when it ends a connection to a running process. */
|
|
1626
|
+
const CLOSE_REASONS = {
|
|
1627
|
+
4001: "Process was taken over by another connection",
|
|
1628
|
+
4003: "Process owner disconnected"
|
|
1629
|
+
};
|
|
1630
|
+
/** The subprotocol the runtime selects for a process connection. */
|
|
1631
|
+
const CONNECT_SUBPROTOCOL = "archil.connect.v1";
|
|
1632
|
+
/** Prefix of the subprotocol that carries a connection token. */
|
|
1633
|
+
const TOKEN_SUBPROTOCOL_PREFIX = "archil.token.";
|
|
1477
1634
|
async function waitForSocketOpen(socket) {
|
|
1478
1635
|
if (socket.readyState === WebSocket.OPEN) return;
|
|
1479
1636
|
await new Promise((resolve, reject) => {
|
|
@@ -1499,12 +1656,13 @@ async function waitForSocketOpen(socket) {
|
|
|
1499
1656
|
socket.addEventListener("close", closed, { once: true });
|
|
1500
1657
|
});
|
|
1501
1658
|
}
|
|
1502
|
-
async function openProcessSocket(
|
|
1659
|
+
async function openProcessSocket(connectionEndpoint) {
|
|
1503
1660
|
for (let attempt = 0;; attempt++) {
|
|
1504
|
-
const
|
|
1661
|
+
const target = await connectionEndpoint();
|
|
1662
|
+
const endpoint = typeof target === "string" ? { url: target } : target;
|
|
1505
1663
|
let socket;
|
|
1506
1664
|
try {
|
|
1507
|
-
socket = new WebSocket(url);
|
|
1665
|
+
socket = endpoint.protocols ? new WebSocket(endpoint.url, endpoint.protocols) : new WebSocket(endpoint.url);
|
|
1508
1666
|
await waitForSocketOpen(socket);
|
|
1509
1667
|
return socket;
|
|
1510
1668
|
} catch (error) {
|
|
@@ -1540,7 +1698,7 @@ var SandboxProcess = class {
|
|
|
1540
1698
|
_cursor;
|
|
1541
1699
|
_onOutput;
|
|
1542
1700
|
_collectOutput;
|
|
1543
|
-
|
|
1701
|
+
_connectionEndpoint;
|
|
1544
1702
|
_controlProcess;
|
|
1545
1703
|
_stdoutDecoder = new TextDecoder();
|
|
1546
1704
|
_stderrDecoder = new TextDecoder();
|
|
@@ -1552,12 +1710,12 @@ var SandboxProcess = class {
|
|
|
1552
1710
|
_connectionError;
|
|
1553
1711
|
_stdinClosed = false;
|
|
1554
1712
|
/** @internal */
|
|
1555
|
-
constructor(processId, cursor, onOutput, collectOutput,
|
|
1713
|
+
constructor(processId, cursor, onOutput, collectOutput, connectionEndpoint, controlProcess) {
|
|
1556
1714
|
this._id = processId;
|
|
1557
1715
|
this._cursor = cursor;
|
|
1558
1716
|
this._onOutput = onOutput;
|
|
1559
1717
|
this._collectOutput = collectOutput;
|
|
1560
|
-
this.
|
|
1718
|
+
this._connectionEndpoint = connectionEndpoint;
|
|
1561
1719
|
this._controlProcess = controlProcess;
|
|
1562
1720
|
this._terminal = new Promise((resolve) => {
|
|
1563
1721
|
this._resolveTerminal = resolve;
|
|
@@ -1618,7 +1776,7 @@ var SandboxProcess = class {
|
|
|
1618
1776
|
}
|
|
1619
1777
|
/** @internal */
|
|
1620
1778
|
async _connect(request) {
|
|
1621
|
-
const socket = await openProcessSocket(this.
|
|
1779
|
+
const socket = await openProcessSocket(this._connectionEndpoint);
|
|
1622
1780
|
socket.binaryType = "arraybuffer";
|
|
1623
1781
|
this._socket = socket;
|
|
1624
1782
|
let resolveClosed;
|
|
@@ -1660,7 +1818,9 @@ var SandboxProcess = class {
|
|
|
1660
1818
|
this._connectionError = error;
|
|
1661
1819
|
fail(error);
|
|
1662
1820
|
});
|
|
1663
|
-
socket.addEventListener("close", () => {
|
|
1821
|
+
socket.addEventListener("close", (event) => {
|
|
1822
|
+
const reason = CLOSE_REASONS[event.code];
|
|
1823
|
+
if (reason && !this._result) this._connectionError = new Error(reason);
|
|
1664
1824
|
if (this._socket === socket) this._socket = void 0;
|
|
1665
1825
|
if (!readySettled) rejectReady(/* @__PURE__ */ new Error("Process connection closed before ready"));
|
|
1666
1826
|
resolveClosed();
|
|
@@ -2077,7 +2237,7 @@ var Sandbox = class Sandbox {
|
|
|
2077
2237
|
}
|
|
2078
2238
|
/** Start a process and return its handle without waiting for exit. */
|
|
2079
2239
|
async run(command, options = {}) {
|
|
2080
|
-
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2240
|
+
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2081
2241
|
const terminal = typeof options.terminal === "object" ? {
|
|
2082
2242
|
cols: options.terminal.cols ?? 80,
|
|
2083
2243
|
rows: options.terminal.rows ?? 24
|
|
@@ -2095,19 +2255,25 @@ var Sandbox = class Sandbox {
|
|
|
2095
2255
|
/** Reattach to a process, optionally resuming output from a cursor. */
|
|
2096
2256
|
async attach(processId, options = {}) {
|
|
2097
2257
|
const offset = options.offset ?? 0;
|
|
2098
|
-
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2258
|
+
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2099
2259
|
await process._connect({
|
|
2100
2260
|
type: "attach",
|
|
2101
2261
|
process_id: processId,
|
|
2102
|
-
offset
|
|
2262
|
+
offset,
|
|
2263
|
+
...options.shared ? { shared: true } : {}
|
|
2103
2264
|
});
|
|
2104
2265
|
return process;
|
|
2105
2266
|
}
|
|
2106
|
-
async
|
|
2107
|
-
|
|
2267
|
+
async _connectionEndpoint() {
|
|
2268
|
+
const data = await unwrap(retryApiRequest(() => this._client.POST("/api/sandboxes/{sid}/connections", { params: { path: { sid: this.id } } }), "transient"));
|
|
2269
|
+
if (data.endpoint && data.token) return {
|
|
2270
|
+
url: data.endpoint,
|
|
2271
|
+
protocols: [CONNECT_SUBPROTOCOL, `${TOKEN_SUBPROTOCOL_PREFIX}${data.token}`]
|
|
2272
|
+
};
|
|
2273
|
+
return { url: data.url };
|
|
2108
2274
|
}
|
|
2109
2275
|
async _control(request) {
|
|
2110
|
-
const socket = await openProcessSocket(() => this.
|
|
2276
|
+
const socket = await openProcessSocket(() => this._connectionEndpoint());
|
|
2111
2277
|
const expected = request.type === "kill" ? "killed" : "resized";
|
|
2112
2278
|
await new Promise((resolve, reject) => {
|
|
2113
2279
|
socket.addEventListener("message", (message) => {
|
|
@@ -2784,6 +2950,7 @@ var Archil = class {
|
|
|
2784
2950
|
disks[relPath] = entry;
|
|
2785
2951
|
} else disks[relPath] = diskIdFromMount(mount);
|
|
2786
2952
|
return unwrap(retryApiRequest(() => this._client.POST("/api/exec", { body: {
|
|
2953
|
+
...execVmBody(opts),
|
|
2787
2954
|
disks,
|
|
2788
2955
|
command: opts.command
|
|
2789
2956
|
} }), "connect"));
|