disk 1.10.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/archil-CLQrudfP.d.cts +1581 -0
- package/dist/archil-CLQrudfP.d.mts +1581 -0
- package/dist/cli.mjs +2 -3
- package/dist/index.cjs +257 -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-DV7SSadf.mjs → sandbox-options-Cz_oVr-y.mjs} +375 -21
- package/dist/{src-BND_QqPx.mjs → src-V9HdQW-c.mjs} +257 -51
- package/package.json +12 -9
- package/dist/disk-DZkPOSQD.d.cts +0 -784
- package/dist/disk-DZkPOSQD.d.mts +0 -784
|
@@ -1,17 +1,164 @@
|
|
|
1
1
|
import { t as toSegments } from "./paths-Br9snUhy.mjs";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
//#region src/api-fetch.ts
|
|
3
|
+
function buildPath(template, params = {}) {
|
|
4
|
+
return template.replace(/\{([^}]+)\}/g, (_, name) => {
|
|
5
|
+
const value = params[name];
|
|
6
|
+
if (value === void 0) throw new Error(`Missing path parameter '${name}' for ${template}`);
|
|
7
|
+
return encodeURIComponent(String(value));
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
/** Serialize a query object; undefined/null values are omitted and arrays repeat the key. */
|
|
11
|
+
function buildQuery(query = {}) {
|
|
12
|
+
const parts = [];
|
|
13
|
+
for (const [name, value] of Object.entries(query)) for (const item of Array.isArray(value) ? value : [value]) {
|
|
14
|
+
if (item === void 0 || item === null) continue;
|
|
15
|
+
parts.push(`${encodeURIComponent(name)}=${encodeURIComponent(String(item))}`);
|
|
16
|
+
}
|
|
17
|
+
return parts.length > 0 ? `?${parts.join("&")}` : "";
|
|
18
|
+
}
|
|
19
|
+
function createClient(config) {
|
|
20
|
+
const baseUrl = config.baseUrl.replace(/\/+$/, "");
|
|
21
|
+
const send = (request) => (config.fetch ?? globalThis.fetch)(request);
|
|
22
|
+
const headersFor = (extra = {}) => {
|
|
23
|
+
const headers = new Headers(config.headers);
|
|
24
|
+
for (const [name, value] of Object.entries(extra)) if (value !== void 0) headers.set(name, value);
|
|
25
|
+
return headers;
|
|
26
|
+
};
|
|
27
|
+
const call = async (method, path, init = {}) => {
|
|
28
|
+
const url = baseUrl + buildPath(path, init.params?.path) + buildQuery(init.params?.query);
|
|
29
|
+
const headers = headersFor(init.params?.header);
|
|
30
|
+
let body;
|
|
31
|
+
if (init.body !== void 0) {
|
|
32
|
+
body = JSON.stringify(init.body);
|
|
33
|
+
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json");
|
|
34
|
+
}
|
|
35
|
+
const response = await send(new Request(url, {
|
|
36
|
+
method,
|
|
37
|
+
headers,
|
|
38
|
+
body
|
|
39
|
+
}));
|
|
40
|
+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
|
|
41
|
+
await response.body?.cancel().catch(() => {});
|
|
42
|
+
return response.ok ? {
|
|
43
|
+
data: void 0,
|
|
44
|
+
response
|
|
45
|
+
} : {
|
|
46
|
+
error: void 0,
|
|
47
|
+
response
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (response.ok) return {
|
|
51
|
+
data: await response.json(),
|
|
52
|
+
response
|
|
53
|
+
};
|
|
54
|
+
const text = await response.text();
|
|
55
|
+
let error = text;
|
|
56
|
+
try {
|
|
57
|
+
error = JSON.parse(text);
|
|
58
|
+
} catch {}
|
|
59
|
+
return {
|
|
60
|
+
error,
|
|
61
|
+
response
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const method = (name) => ((path, init) => call(name, path, init));
|
|
65
|
+
return {
|
|
66
|
+
GET: method("GET"),
|
|
67
|
+
PUT: method("PUT"),
|
|
68
|
+
POST: method("POST"),
|
|
69
|
+
DELETE: method("DELETE"),
|
|
70
|
+
PATCH: method("PATCH"),
|
|
71
|
+
raw: (methodName, url, init = {}) => send(new Request(url, {
|
|
72
|
+
method: methodName,
|
|
73
|
+
headers: headersFor(init.headers),
|
|
74
|
+
body: init.body
|
|
75
|
+
}))
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
4
79
|
//#region src/s3xml.ts
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
80
|
+
const ALWAYS_ARRAY = /* @__PURE__ */ new Set(["Contents", "CommonPrefixes"]);
|
|
81
|
+
const NAMED_ENTITIES = {
|
|
82
|
+
amp: "&",
|
|
83
|
+
lt: "<",
|
|
84
|
+
gt: ">",
|
|
85
|
+
quot: "\"",
|
|
86
|
+
apos: "'"
|
|
87
|
+
};
|
|
88
|
+
function decodeEntities(text) {
|
|
89
|
+
if (!text.includes("&")) return text;
|
|
90
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (match, ref) => {
|
|
91
|
+
if (ref[0] === "#") {
|
|
92
|
+
const code = ref[1] === "x" || ref[1] === "X" ? parseInt(ref.slice(2), 16) : parseInt(ref.slice(1), 10);
|
|
93
|
+
return code > 1114111 ? match : String.fromCodePoint(code);
|
|
94
|
+
}
|
|
95
|
+
return Object.hasOwn(NAMED_ENTITIES, ref) ? NAMED_ENTITIES[ref] : match;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function setOwn(target, name, value) {
|
|
99
|
+
Object.defineProperty(target, name, {
|
|
100
|
+
value,
|
|
101
|
+
enumerable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
configurable: true
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function addChild(parent, name, value) {
|
|
107
|
+
parent.hasChildren = true;
|
|
108
|
+
const existing = Object.hasOwn(parent.children, name) ? parent.children[name] : void 0;
|
|
109
|
+
if (existing === void 0) setOwn(parent.children, name, ALWAYS_ARRAY.has(name) ? [value] : value);
|
|
110
|
+
else if (Array.isArray(existing)) existing.push(value);
|
|
111
|
+
else setOwn(parent.children, name, [existing, value]);
|
|
112
|
+
}
|
|
113
|
+
/** Index just past `terminator`, searching from `from`; throws if it never appears. */
|
|
114
|
+
function skipPast(xml, terminator, from) {
|
|
115
|
+
const end = xml.indexOf(terminator, from);
|
|
116
|
+
if (end === -1) throw new Error(`Malformed XML: missing '${terminator}'`);
|
|
117
|
+
return end + terminator.length;
|
|
118
|
+
}
|
|
11
119
|
/** Parse an XML document into a plain object. Returns {} for empty/blank input. */
|
|
12
120
|
function parseXml(xml) {
|
|
13
|
-
|
|
14
|
-
|
|
121
|
+
const root = {
|
|
122
|
+
name: "",
|
|
123
|
+
children: {},
|
|
124
|
+
hasChildren: false,
|
|
125
|
+
text: ""
|
|
126
|
+
};
|
|
127
|
+
const stack = [root];
|
|
128
|
+
let position = 0;
|
|
129
|
+
while (position < xml.length) {
|
|
130
|
+
const current = stack[stack.length - 1];
|
|
131
|
+
const open = xml.indexOf("<", position);
|
|
132
|
+
current.text += decodeEntities(xml.slice(position, open === -1 ? xml.length : open));
|
|
133
|
+
if (open === -1) break;
|
|
134
|
+
if (xml.startsWith("<!--", open)) position = skipPast(xml, "-->", open + 4);
|
|
135
|
+
else if (xml.startsWith("<![CDATA[", open)) {
|
|
136
|
+
position = skipPast(xml, "]]>", open + 9);
|
|
137
|
+
current.text += xml.slice(open + 9, position - 3);
|
|
138
|
+
} else if (xml.startsWith("<?", open)) position = skipPast(xml, "?>", open + 2);
|
|
139
|
+
else if (xml.startsWith("<!", open)) throw new Error("Malformed XML: DOCTYPE and other declarations are not supported");
|
|
140
|
+
else {
|
|
141
|
+
position = skipPast(xml, ">", open + 1);
|
|
142
|
+
const tag = xml.slice(open + 1, position - 1);
|
|
143
|
+
const closing = tag.startsWith("/");
|
|
144
|
+
const selfClosing = !closing && tag.endsWith("/");
|
|
145
|
+
const name = /^[^\s/]+/.exec(closing ? tag.slice(1) : tag)?.[0];
|
|
146
|
+
if (!name) throw new Error(`Malformed XML: invalid tag <${tag}>`);
|
|
147
|
+
if (closing) {
|
|
148
|
+
if (current.name !== name || stack.length === 1) throw new Error(`Malformed XML: unexpected </${name}>`);
|
|
149
|
+
stack.pop();
|
|
150
|
+
addChild(stack[stack.length - 1], name, current.hasChildren ? current.children : current.text);
|
|
151
|
+
} else if (selfClosing) addChild(current, name, "");
|
|
152
|
+
else stack.push({
|
|
153
|
+
name,
|
|
154
|
+
children: {},
|
|
155
|
+
hasChildren: false,
|
|
156
|
+
text: ""
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (stack.length !== 1) throw new Error(`Malformed XML: unclosed <${stack[stack.length - 1].name}>`);
|
|
161
|
+
return root.children;
|
|
15
162
|
}
|
|
16
163
|
//#endregion
|
|
17
164
|
//#region src/errors.ts
|
|
@@ -140,11 +287,10 @@ function deriveS3BaseUrl(controlBaseUrl) {
|
|
|
140
287
|
}
|
|
141
288
|
//#endregion
|
|
142
289
|
//#region src/version.ts
|
|
143
|
-
const VERSION = "1.
|
|
290
|
+
const VERSION = "1.12.0";
|
|
144
291
|
const USER_AGENT = `archil-js/${VERSION}`;
|
|
145
292
|
//#endregion
|
|
146
293
|
//#region src/client.ts
|
|
147
|
-
const createClient = typeof createClientDefault === "function" ? createClientDefault : createClientDefault.default;
|
|
148
294
|
function isNodeRuntime() {
|
|
149
295
|
return typeof process !== "undefined" && typeof process.versions?.node === "string";
|
|
150
296
|
}
|
|
@@ -175,10 +321,10 @@ const bufferRequestBodies = (dispatch) => (options, handler) => {
|
|
|
175
321
|
return true;
|
|
176
322
|
};
|
|
177
323
|
const sharedTransports = /* @__PURE__ */ new Map();
|
|
178
|
-
function dispatcherKey(baseUrl,
|
|
324
|
+
function dispatcherKey(baseUrl, authorization, ca) {
|
|
179
325
|
return JSON.stringify([
|
|
180
326
|
new URL(baseUrl).origin,
|
|
181
|
-
|
|
327
|
+
authorization,
|
|
182
328
|
ca?.map((cert) => cert.toString("base64"))
|
|
183
329
|
]);
|
|
184
330
|
}
|
|
@@ -204,13 +350,13 @@ function getSharedTransport(key, ca) {
|
|
|
204
350
|
});
|
|
205
351
|
return transport;
|
|
206
352
|
}
|
|
207
|
-
function createPooledFetch(baseUrl,
|
|
353
|
+
function createPooledFetch(baseUrl, authorization, tls) {
|
|
208
354
|
if (!isNodeRuntime()) {
|
|
209
355
|
if (tls?.ca !== void 0) throw new Error("Custom TLS CAs require Node.js");
|
|
210
356
|
return;
|
|
211
357
|
}
|
|
212
358
|
const ca = tls?.ca === void 0 ? void 0 : (Array.isArray(tls.ca) ? tls.ca : [tls.ca]).map((cert) => Buffer.from(cert));
|
|
213
|
-
const key = dispatcherKey(baseUrl,
|
|
359
|
+
const key = dispatcherKey(baseUrl, authorization, ca);
|
|
214
360
|
return async (request) => {
|
|
215
361
|
const { fetch, dispatcher } = await getSharedTransport(key, ca);
|
|
216
362
|
const body = request.body === null ? null : new Uint8Array(await request.arrayBuffer());
|
|
@@ -224,14 +370,23 @@ function createPooledFetch(baseUrl, apiKey, tls) {
|
|
|
224
370
|
});
|
|
225
371
|
};
|
|
226
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* The `Authorization` value for an API key: the key exactly as given, as a
|
|
375
|
+
* bearer token. Credential-injecting proxies substitute a placeholder only when
|
|
376
|
+
* it is the whole token, so the SDK must not add a prefix to it. The control
|
|
377
|
+
* plane accepts the key with or without its `key-` prefix after `Bearer`.
|
|
378
|
+
*/
|
|
379
|
+
function apiKeyAuthorization(apiKey) {
|
|
380
|
+
return `Bearer ${apiKey}`;
|
|
381
|
+
}
|
|
227
382
|
function createApiClient(opts) {
|
|
228
383
|
const baseUrl = opts.baseUrl ?? resolveBaseUrl(opts.region);
|
|
229
|
-
const
|
|
384
|
+
const authorization = apiKeyAuthorization(opts.apiKey);
|
|
230
385
|
return createClient({
|
|
231
386
|
baseUrl,
|
|
232
|
-
fetch: createPooledFetch(baseUrl,
|
|
387
|
+
fetch: createPooledFetch(baseUrl, authorization, opts.tls),
|
|
233
388
|
headers: {
|
|
234
|
-
Authorization:
|
|
389
|
+
Authorization: authorization,
|
|
235
390
|
"User-Agent": USER_AGENT
|
|
236
391
|
}
|
|
237
392
|
});
|
|
@@ -271,6 +426,22 @@ async function unwrapEmpty(promise) {
|
|
|
271
426
|
}
|
|
272
427
|
if (!body.success) throw new ArchilApiError(body.error ?? "Unknown API error", response.status, body.code);
|
|
273
428
|
}
|
|
429
|
+
//#endregion
|
|
430
|
+
//#region src/exec-options.ts
|
|
431
|
+
/**
|
|
432
|
+
* @internal Copies only the known option keys into a request body. Every key
|
|
433
|
+
* is required here, so an option added to the API fails to compile until it is
|
|
434
|
+
* copied.
|
|
435
|
+
*/
|
|
436
|
+
function execVmBody(options) {
|
|
437
|
+
return {
|
|
438
|
+
baseImage: options?.baseImage,
|
|
439
|
+
imageId: options?.imageId,
|
|
440
|
+
vcpuCount: options?.vcpuCount,
|
|
441
|
+
memSizeMib: options?.memSizeMib,
|
|
442
|
+
network: options?.network
|
|
443
|
+
};
|
|
444
|
+
}
|
|
274
445
|
const RETRY_BASE_MS = 100;
|
|
275
446
|
const RETRY_CAP_MS = 2e3;
|
|
276
447
|
const TRANSIENT_STATUSES = /* @__PURE__ */ new Set([
|
|
@@ -442,6 +613,23 @@ var Disk = class {
|
|
|
442
613
|
await this.removeUser("token", identifier);
|
|
443
614
|
}
|
|
444
615
|
/**
|
|
616
|
+
* Create an S3 access key pair for this disk's S3 API. The secret key is
|
|
617
|
+
* returned only here. The keys reach the whole disk, read-write unless
|
|
618
|
+
* `readOnly` is set, so keep them server-side and give browsers presigned
|
|
619
|
+
* URLs instead.
|
|
620
|
+
*/
|
|
621
|
+
async createS3Credentials(options = {}) {
|
|
622
|
+
const user = await this.addUser({
|
|
623
|
+
...options,
|
|
624
|
+
type: "s3"
|
|
625
|
+
});
|
|
626
|
+
if (!user.s3AccessKeyId || !user.s3SecretKey || !user.identifier) throw new Error("Server did not return generated S3 credentials");
|
|
627
|
+
return user;
|
|
628
|
+
}
|
|
629
|
+
async removeS3Credentials(accessKeyId) {
|
|
630
|
+
await this.removeUser("s3", accessKeyId);
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
445
633
|
* List all delegations currently held on this disk. A delegation grants a
|
|
446
634
|
* client exclusive write access to an inode; it has no ID of its own and is
|
|
447
635
|
* identified by the (clientId, inodeId) pair — pass those to
|
|
@@ -533,10 +721,13 @@ var Disk = class {
|
|
|
533
721
|
* Execute a command in a container with this disk mounted.
|
|
534
722
|
* Blocks until the command completes and returns stdout, stderr, and exit code.
|
|
535
723
|
*/
|
|
536
|
-
async exec(command) {
|
|
724
|
+
async exec(command, options) {
|
|
537
725
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
538
726
|
params: { path: { id: this.id } },
|
|
539
|
-
body: {
|
|
727
|
+
body: {
|
|
728
|
+
...execVmBody(options),
|
|
729
|
+
command
|
|
730
|
+
}
|
|
540
731
|
}), "connect"));
|
|
541
732
|
}
|
|
542
733
|
/**
|
|
@@ -883,29 +1074,22 @@ var Disk = class {
|
|
|
883
1074
|
*/
|
|
884
1075
|
async _s3Request(method, key, opts = {}) {
|
|
885
1076
|
if (!this._s3BaseUrl) throw new Error("S3 base URL not configured. Pass s3BaseUrl to new Archil({...}) or set ARCHIL_S3_BASE_URL.");
|
|
886
|
-
const call = this._client[method];
|
|
887
1077
|
const encodedKey = key.replace(/^\//, "").split("/").map(encodeURIComponent).join("/");
|
|
888
1078
|
const path = encodedKey ? `/${this.id}/${encodedKey}` : `/${this.id}`;
|
|
1079
|
+
const url = `${this._s3BaseUrl.replace(/\/+$/, "")}${path}${buildQuery(opts.query)}`;
|
|
889
1080
|
const init = {
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
...opts.query ? { params: { query: opts.query } } : {},
|
|
893
|
-
...opts.body !== void 0 ? {
|
|
894
|
-
body: opts.body,
|
|
895
|
-
bodySerializer: (b) => b
|
|
896
|
-
} : {},
|
|
897
|
-
...opts.contentType || opts.extraHeaders ? { headers: {
|
|
1081
|
+
body: opts.body,
|
|
1082
|
+
headers: {
|
|
898
1083
|
...opts.contentType ? { "Content-Type": opts.contentType } : {},
|
|
899
1084
|
...opts.extraHeaders
|
|
900
|
-
}
|
|
1085
|
+
}
|
|
901
1086
|
};
|
|
902
1087
|
const hasBody = method === "GET" || method === "POST";
|
|
903
1088
|
const maxRetries = opts.retry === false ? 0 : 3;
|
|
904
1089
|
for (let attempt = 0;; attempt++) {
|
|
905
|
-
let error;
|
|
906
1090
|
let response;
|
|
907
1091
|
try {
|
|
908
|
-
|
|
1092
|
+
response = await this._client.raw(method, url, init);
|
|
909
1093
|
} catch (transportError) {
|
|
910
1094
|
if (attempt < maxRetries) {
|
|
911
1095
|
await retrySleep(attempt);
|
|
@@ -919,15 +1103,15 @@ var Disk = class {
|
|
|
919
1103
|
await retrySleep(attempt);
|
|
920
1104
|
continue;
|
|
921
1105
|
}
|
|
922
|
-
const message = typeof error === "string" ? error : error ? JSON.stringify(error) : "";
|
|
923
1106
|
return {
|
|
924
1107
|
ok: false,
|
|
925
1108
|
status: response.status,
|
|
926
1109
|
statusText: response.statusText,
|
|
927
1110
|
headers: response.headers,
|
|
928
|
-
body: new
|
|
1111
|
+
body: new Uint8Array(await response.arrayBuffer())
|
|
929
1112
|
};
|
|
930
1113
|
}
|
|
1114
|
+
if (!hasBody) await response.body?.cancel().catch(() => {});
|
|
931
1115
|
const bytes = hasBody ? new Uint8Array(await response.arrayBuffer()) : /* @__PURE__ */ new Uint8Array(0);
|
|
932
1116
|
return {
|
|
933
1117
|
ok: true,
|
|
@@ -1353,10 +1537,13 @@ var Disks = class {
|
|
|
1353
1537
|
* disk ID directly so callers that do not need its metadata can execute
|
|
1354
1538
|
* without fetching the disk first.
|
|
1355
1539
|
*/
|
|
1356
|
-
async exec(id, command) {
|
|
1540
|
+
async exec(id, command, options) {
|
|
1357
1541
|
return unwrap(retryApiRequest(() => this._client.POST("/api/disks/{id}/exec", {
|
|
1358
1542
|
params: { path: { id } },
|
|
1359
|
-
body: {
|
|
1543
|
+
body: {
|
|
1544
|
+
...execVmBody(options),
|
|
1545
|
+
command
|
|
1546
|
+
}
|
|
1360
1547
|
}), "connect"));
|
|
1361
1548
|
}
|
|
1362
1549
|
};
|
|
@@ -1434,6 +1621,15 @@ var Images = class {
|
|
|
1434
1621
|
//#endregion
|
|
1435
1622
|
//#region src/sandbox-process.ts
|
|
1436
1623
|
const PROCESS_STDIN_CHUNK_BYTES = 1024 * 1024;
|
|
1624
|
+
/** Close codes the runtime uses when it ends a connection to a running process. */
|
|
1625
|
+
const CLOSE_REASONS = {
|
|
1626
|
+
4001: "Process was taken over by another connection",
|
|
1627
|
+
4003: "Process owner disconnected"
|
|
1628
|
+
};
|
|
1629
|
+
/** The subprotocol the runtime selects for a process connection. */
|
|
1630
|
+
const CONNECT_SUBPROTOCOL = "archil.connect.v1";
|
|
1631
|
+
/** Prefix of the subprotocol that carries a connection token. */
|
|
1632
|
+
const TOKEN_SUBPROTOCOL_PREFIX = "archil.token.";
|
|
1437
1633
|
async function waitForSocketOpen(socket) {
|
|
1438
1634
|
if (socket.readyState === WebSocket.OPEN) return;
|
|
1439
1635
|
await new Promise((resolve, reject) => {
|
|
@@ -1459,12 +1655,13 @@ async function waitForSocketOpen(socket) {
|
|
|
1459
1655
|
socket.addEventListener("close", closed, { once: true });
|
|
1460
1656
|
});
|
|
1461
1657
|
}
|
|
1462
|
-
async function openProcessSocket(
|
|
1658
|
+
async function openProcessSocket(connectionEndpoint) {
|
|
1463
1659
|
for (let attempt = 0;; attempt++) {
|
|
1464
|
-
const
|
|
1660
|
+
const target = await connectionEndpoint();
|
|
1661
|
+
const endpoint = typeof target === "string" ? { url: target } : target;
|
|
1465
1662
|
let socket;
|
|
1466
1663
|
try {
|
|
1467
|
-
socket = new WebSocket(url);
|
|
1664
|
+
socket = endpoint.protocols ? new WebSocket(endpoint.url, endpoint.protocols) : new WebSocket(endpoint.url);
|
|
1468
1665
|
await waitForSocketOpen(socket);
|
|
1469
1666
|
return socket;
|
|
1470
1667
|
} catch (error) {
|
|
@@ -1500,7 +1697,7 @@ var SandboxProcess = class {
|
|
|
1500
1697
|
_cursor;
|
|
1501
1698
|
_onOutput;
|
|
1502
1699
|
_collectOutput;
|
|
1503
|
-
|
|
1700
|
+
_connectionEndpoint;
|
|
1504
1701
|
_controlProcess;
|
|
1505
1702
|
_stdoutDecoder = new TextDecoder();
|
|
1506
1703
|
_stderrDecoder = new TextDecoder();
|
|
@@ -1512,12 +1709,12 @@ var SandboxProcess = class {
|
|
|
1512
1709
|
_connectionError;
|
|
1513
1710
|
_stdinClosed = false;
|
|
1514
1711
|
/** @internal */
|
|
1515
|
-
constructor(processId, cursor, onOutput, collectOutput,
|
|
1712
|
+
constructor(processId, cursor, onOutput, collectOutput, connectionEndpoint, controlProcess) {
|
|
1516
1713
|
this._id = processId;
|
|
1517
1714
|
this._cursor = cursor;
|
|
1518
1715
|
this._onOutput = onOutput;
|
|
1519
1716
|
this._collectOutput = collectOutput;
|
|
1520
|
-
this.
|
|
1717
|
+
this._connectionEndpoint = connectionEndpoint;
|
|
1521
1718
|
this._controlProcess = controlProcess;
|
|
1522
1719
|
this._terminal = new Promise((resolve) => {
|
|
1523
1720
|
this._resolveTerminal = resolve;
|
|
@@ -1578,7 +1775,7 @@ var SandboxProcess = class {
|
|
|
1578
1775
|
}
|
|
1579
1776
|
/** @internal */
|
|
1580
1777
|
async _connect(request) {
|
|
1581
|
-
const socket = await openProcessSocket(this.
|
|
1778
|
+
const socket = await openProcessSocket(this._connectionEndpoint);
|
|
1582
1779
|
socket.binaryType = "arraybuffer";
|
|
1583
1780
|
this._socket = socket;
|
|
1584
1781
|
let resolveClosed;
|
|
@@ -1620,7 +1817,9 @@ var SandboxProcess = class {
|
|
|
1620
1817
|
this._connectionError = error;
|
|
1621
1818
|
fail(error);
|
|
1622
1819
|
});
|
|
1623
|
-
socket.addEventListener("close", () => {
|
|
1820
|
+
socket.addEventListener("close", (event) => {
|
|
1821
|
+
const reason = CLOSE_REASONS[event.code];
|
|
1822
|
+
if (reason && !this._result) this._connectionError = new Error(reason);
|
|
1624
1823
|
if (this._socket === socket) this._socket = void 0;
|
|
1625
1824
|
if (!readySettled) rejectReady(/* @__PURE__ */ new Error("Process connection closed before ready"));
|
|
1626
1825
|
resolveClosed();
|
|
@@ -2037,7 +2236,7 @@ var Sandbox = class Sandbox {
|
|
|
2037
2236
|
}
|
|
2038
2237
|
/** Start a process and return its handle without waiting for exit. */
|
|
2039
2238
|
async run(command, options = {}) {
|
|
2040
|
-
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2239
|
+
const process = new SandboxProcess("", 0, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2041
2240
|
const terminal = typeof options.terminal === "object" ? {
|
|
2042
2241
|
cols: options.terminal.cols ?? 80,
|
|
2043
2242
|
rows: options.terminal.rows ?? 24
|
|
@@ -2055,19 +2254,25 @@ var Sandbox = class Sandbox {
|
|
|
2055
2254
|
/** Reattach to a process, optionally resuming output from a cursor. */
|
|
2056
2255
|
async attach(processId, options = {}) {
|
|
2057
2256
|
const offset = options.offset ?? 0;
|
|
2058
|
-
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this.
|
|
2257
|
+
const process = new SandboxProcess(processId, offset, options.onOutput, options.collectOutput ?? true, () => this._connectionEndpoint(), (request) => this._control(request));
|
|
2059
2258
|
await process._connect({
|
|
2060
2259
|
type: "attach",
|
|
2061
2260
|
process_id: processId,
|
|
2062
|
-
offset
|
|
2261
|
+
offset,
|
|
2262
|
+
...options.shared ? { shared: true } : {}
|
|
2063
2263
|
});
|
|
2064
2264
|
return process;
|
|
2065
2265
|
}
|
|
2066
|
-
async
|
|
2067
|
-
|
|
2266
|
+
async _connectionEndpoint() {
|
|
2267
|
+
const data = await unwrap(retryApiRequest(() => this._client.POST("/api/sandboxes/{sid}/connections", { params: { path: { sid: this.id } } }), "transient"));
|
|
2268
|
+
if (data.endpoint && data.token) return {
|
|
2269
|
+
url: data.endpoint,
|
|
2270
|
+
protocols: [CONNECT_SUBPROTOCOL, `${TOKEN_SUBPROTOCOL_PREFIX}${data.token}`]
|
|
2271
|
+
};
|
|
2272
|
+
return { url: data.url };
|
|
2068
2273
|
}
|
|
2069
2274
|
async _control(request) {
|
|
2070
|
-
const socket = await openProcessSocket(() => this.
|
|
2275
|
+
const socket = await openProcessSocket(() => this._connectionEndpoint());
|
|
2071
2276
|
const expected = request.type === "kill" ? "killed" : "resized";
|
|
2072
2277
|
await new Promise((resolve, reject) => {
|
|
2073
2278
|
socket.addEventListener("message", (message) => {
|
|
@@ -2744,6 +2949,7 @@ var Archil = class {
|
|
|
2744
2949
|
disks[relPath] = entry;
|
|
2745
2950
|
} else disks[relPath] = diskIdFromMount(mount);
|
|
2746
2951
|
return unwrap(retryApiRequest(() => this._client.POST("/api/exec", { body: {
|
|
2952
|
+
...execVmBody(opts),
|
|
2747
2953
|
disks,
|
|
2748
2954
|
command: opts.command
|
|
2749
2955
|
} }), "connect"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "disk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Pure-JS client and CLIs for Archil disks and sandboxes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"archil",
|
|
@@ -40,20 +40,23 @@
|
|
|
40
40
|
"LICENSE"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@archildata/api-types": "^0.0.
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"openapi-fetch": "^0.13.5",
|
|
48
|
-
"undici": "8.10.2",
|
|
49
|
-
"uuid": "^11.1.1",
|
|
43
|
+
"@archildata/api-types": "^0.0.81",
|
|
44
|
+
"undici": "^8.12.3"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
50
47
|
"zod": "^4.4.3"
|
|
51
48
|
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"zod": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
52
54
|
"devDependencies": {
|
|
53
55
|
"@types/node": "^26.0.1",
|
|
54
56
|
"tsdown": "^0.22.3",
|
|
55
57
|
"typescript": "^6.0.3",
|
|
56
|
-
"vitest": "^4.1.9"
|
|
58
|
+
"vitest": "^4.1.9",
|
|
59
|
+
"zod": "^4.4.3"
|
|
57
60
|
},
|
|
58
61
|
"engines": {
|
|
59
62
|
"node": ">= 22.19.0"
|