disk 0.8.21 → 0.8.23

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.
@@ -111,7 +111,7 @@ function deriveS3BaseUrl(controlBaseUrl) {
111
111
  }
112
112
  //#endregion
113
113
  //#region src/version.ts
114
- const VERSION = "0.8.21";
114
+ const VERSION = "0.8.23";
115
115
  const USER_AGENT = `archil-js/${VERSION}`;
116
116
  //#endregion
117
117
  //#region src/client.ts
@@ -252,6 +252,41 @@ var Disk = class {
252
252
  async removeTokenUser(identifier) {
253
253
  await this.removeUser("token", identifier);
254
254
  }
255
+ /**
256
+ * List all delegations currently held on this disk. A delegation grants a
257
+ * client exclusive write access to an inode; it has no ID of its own and is
258
+ * identified by the (clientId, inodeId) pair — pass those to
259
+ * `revokeDelegation` to release one. Entries with `isOrphaned: true` are
260
+ * held by clients no longer connected to the disk (disconnected without
261
+ * checking in). `path` is resolved best-effort by the server and may be
262
+ * absent.
263
+ */
264
+ async listDelegations() {
265
+ return (await unwrap(this._client.GET("/api/disks/{id}/delegations", { params: { path: { id: this.id } } }))).delegations;
266
+ }
267
+ /**
268
+ * Forcibly revoke a delegation, reclaiming write access from a client that
269
+ * is unreachable or crashed without checking its delegations in.
270
+ *
271
+ * Takes the `(clientId, inodeId)` pair identifying the delegation — pass an
272
+ * entry from `listDelegations` directly:
273
+ *
274
+ * ```ts
275
+ * for (const d of await disk.listDelegations()) {
276
+ * if (d.isOrphaned) await disk.revokeDelegation(d);
277
+ * }
278
+ * ```
279
+ */
280
+ async revokeDelegation(delegation) {
281
+ const { clientId, inodeId } = delegation;
282
+ await unwrap(this._client.POST("/api/disks/{id}/revoke-delegation", {
283
+ params: { path: { id: this.id } },
284
+ body: {
285
+ clientId,
286
+ inodeId
287
+ }
288
+ }));
289
+ }
255
290
  async getAllowedIPs() {
256
291
  return (await unwrap(this._client.GET("/api/disks/{id}/allowed-ips", { params: { path: { id: this.id } } }))).allowedIps;
257
292
  }
@@ -1099,6 +1134,319 @@ var Disks = class {
1099
1134
  };
1100
1135
  }
1101
1136
  };
1137
+ const POLL_INTERVAL_MS = 500;
1138
+ /** @internal */
1139
+ function validateTimeoutMs(timeoutMs) {
1140
+ if (!Number.isFinite(timeoutMs) || timeoutMs < 0) throw new RangeError("timeoutMs must be a finite, non-negative number");
1141
+ }
1142
+ /**
1143
+ * The requested SDK-side wait expired. The remote sandbox operation continues;
1144
+ * `latest` is the same `Sandbox`/`SandboxExec` the operation was called on,
1145
+ * updated in place to the last state observed before the deadline.
1146
+ */
1147
+ var SandboxWaitTimeoutError = class extends ArchilError {
1148
+ operation;
1149
+ timeoutMs;
1150
+ latest;
1151
+ constructor(operation, timeoutMs, latest) {
1152
+ const subject = operation === "exec" ? "sandbox exec to complete" : `sandbox to ${operation}`;
1153
+ super(`Timed out after ${timeoutMs}ms waiting for ${subject}`, 408, "SANDBOX_WAIT_TIMEOUT");
1154
+ this.name = "SandboxWaitTimeoutError";
1155
+ this.operation = operation;
1156
+ this.timeoutMs = timeoutMs;
1157
+ this.latest = latest;
1158
+ }
1159
+ };
1160
+ /** The sandbox entered an inactive state before startup completed. */
1161
+ var SandboxStartError = class extends ArchilError {
1162
+ latest;
1163
+ constructor(sandbox) {
1164
+ const detail = sandbox.exitReason ? `: ${sandbox.exitReason}` : "";
1165
+ super(`Sandbox entered ${sandbox.status} before it started${detail}`, 409, "SANDBOX_START_FAILED");
1166
+ this.name = "SandboxStartError";
1167
+ this.latest = sandbox;
1168
+ }
1169
+ };
1170
+ var SandboxExec = class {
1171
+ sandboxId;
1172
+ id;
1173
+ command;
1174
+ status;
1175
+ exitCode;
1176
+ stdout;
1177
+ stderr;
1178
+ exitReason;
1179
+ executeTimeMs;
1180
+ startedAt;
1181
+ finishedAt;
1182
+ /** @internal */
1183
+ _client;
1184
+ /** @internal */
1185
+ constructor(data, client) {
1186
+ this._client = client;
1187
+ this._apply(data);
1188
+ }
1189
+ /** @internal Overwrite this exec's fields in place from a fresh wire snapshot. */
1190
+ _apply(data) {
1191
+ this.sandboxId = data.sandbox_id;
1192
+ this.id = data.exec_id;
1193
+ this.command = data.command;
1194
+ this.status = data.status;
1195
+ this.exitCode = data.exit_code;
1196
+ this.stdout = data.stdout;
1197
+ this.stderr = data.stderr;
1198
+ this.exitReason = data.exit_reason;
1199
+ this.executeTimeMs = data.execute_time_ms;
1200
+ this.startedAt = new Date(data.started_at);
1201
+ this.finishedAt = data.finished_at ? new Date(data.finished_at) : void 0;
1202
+ return this;
1203
+ }
1204
+ toJSON() {
1205
+ return {
1206
+ sandboxId: this.sandboxId,
1207
+ id: this.id,
1208
+ command: this.command,
1209
+ status: this.status,
1210
+ exitCode: this.exitCode,
1211
+ stdout: this.stdout,
1212
+ stderr: this.stderr,
1213
+ exitReason: this.exitReason,
1214
+ executeTimeMs: this.executeTimeMs,
1215
+ startedAt: this.startedAt,
1216
+ finishedAt: this.finishedAt
1217
+ };
1218
+ }
1219
+ /** Re-fetch this exec and update it in place, returning the same object. */
1220
+ async refresh() {
1221
+ const data = await unwrap(this._client.GET("/api/sandboxes/{sid}/execs/{eid}", { params: { path: {
1222
+ sid: this.sandboxId,
1223
+ eid: this.id
1224
+ } } }));
1225
+ return this._apply(data);
1226
+ }
1227
+ /** Cancel this exec and update it in place, returning the same object. */
1228
+ async cancel() {
1229
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/execs/{eid}/cancel", { params: { path: {
1230
+ sid: this.sandboxId,
1231
+ eid: this.id
1232
+ } } }));
1233
+ return this._apply(data);
1234
+ }
1235
+ };
1236
+ var Sandbox = class {
1237
+ id;
1238
+ status;
1239
+ vcpuCount;
1240
+ memSizeMiB;
1241
+ maxTtlSeconds;
1242
+ maxConcurrentExecs;
1243
+ endpoints;
1244
+ createdAt;
1245
+ runningAt;
1246
+ finishedAt;
1247
+ lastActiveAt;
1248
+ expiresAt;
1249
+ exitReason;
1250
+ /** @internal */
1251
+ _client;
1252
+ /** @internal */
1253
+ constructor(data, client) {
1254
+ this._client = client;
1255
+ this._apply(data);
1256
+ }
1257
+ /** @internal Overwrite this sandbox's fields in place from a fresh wire snapshot. */
1258
+ _apply(data) {
1259
+ this.id = data.sandbox_id;
1260
+ this.status = data.status;
1261
+ this.vcpuCount = data.vcpu_count;
1262
+ this.memSizeMiB = data.mem_size_mib;
1263
+ this.maxTtlSeconds = data.max_ttl_seconds;
1264
+ this.maxConcurrentExecs = data.max_concurrent_execs;
1265
+ this.endpoints = data.endpoints?.map((endpoint) => ({ ...endpoint }));
1266
+ this.createdAt = new Date(data.created_at);
1267
+ this.runningAt = data.running_at ? new Date(data.running_at) : void 0;
1268
+ this.finishedAt = data.finished_at ? new Date(data.finished_at) : void 0;
1269
+ this.lastActiveAt = new Date(data.last_active_at);
1270
+ this.expiresAt = data.expires_at ? new Date(data.expires_at) : void 0;
1271
+ this.exitReason = data.exit_reason;
1272
+ return this;
1273
+ }
1274
+ toJSON() {
1275
+ return {
1276
+ id: this.id,
1277
+ status: this.status,
1278
+ vcpuCount: this.vcpuCount,
1279
+ memSizeMiB: this.memSizeMiB,
1280
+ maxTtlSeconds: this.maxTtlSeconds,
1281
+ maxConcurrentExecs: this.maxConcurrentExecs,
1282
+ endpoints: this.endpoints?.map((endpoint) => ({ ...endpoint })),
1283
+ createdAt: this.createdAt,
1284
+ runningAt: this.runningAt,
1285
+ finishedAt: this.finishedAt,
1286
+ lastActiveAt: this.lastActiveAt,
1287
+ expiresAt: this.expiresAt,
1288
+ exitReason: this.exitReason
1289
+ };
1290
+ }
1291
+ /** Re-fetch this sandbox. */
1292
+ async refresh() {
1293
+ const data = await unwrap(this._client.GET("/api/sandboxes/{sid}", { params: { path: { sid: this.id } } }));
1294
+ return this._apply(data);
1295
+ }
1296
+ /** Start this sandbox. */
1297
+ async start(options = {}) {
1298
+ const timeoutMs = options.timeoutMs ?? 3e4;
1299
+ validateTimeoutMs(timeoutMs);
1300
+ const deadline = Date.now() + timeoutMs;
1301
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/start", { params: {
1302
+ path: { sid: this.id },
1303
+ query: { wait: false }
1304
+ } }));
1305
+ this._apply(data);
1306
+ return waitForSandboxStart(this, deadline, timeoutMs);
1307
+ }
1308
+ /** Stop this sandbox. */
1309
+ async stop(options = {}) {
1310
+ const timeoutMs = options.timeoutMs ?? 3e4;
1311
+ validateTimeoutMs(timeoutMs);
1312
+ const deadline = Date.now() + timeoutMs;
1313
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/stop", { params: { path: { sid: this.id } } }));
1314
+ this._apply(data);
1315
+ for (;;) {
1316
+ if ([
1317
+ "stopped",
1318
+ "exited",
1319
+ "failed"
1320
+ ].includes(this.status)) return this;
1321
+ if (this.status !== "stopping") throw new ArchilError(`Sandbox entered ${this.status} before it stopped`, 409, "SANDBOX_STOP_FAILED");
1322
+ const remaining = deadline - Date.now();
1323
+ if (remaining <= 0) throw new SandboxWaitTimeoutError("stop", timeoutMs, this);
1324
+ await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1325
+ await this.refresh();
1326
+ }
1327
+ }
1328
+ /** Pause this sandbox, preserving its CPU and memory state. */
1329
+ async pause(options = {}) {
1330
+ const timeoutMs = options.timeoutMs ?? 3e4;
1331
+ validateTimeoutMs(timeoutMs);
1332
+ const deadline = Date.now() + timeoutMs;
1333
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/pause", { params: { path: { sid: this.id } } }));
1334
+ this._apply(data);
1335
+ for (;;) {
1336
+ if (this.status === "paused") return this;
1337
+ if (this.status !== "pausing") throw new ArchilError(`Sandbox entered ${this.status} before it paused`, 409, "SANDBOX_PAUSE_FAILED");
1338
+ const remaining = deadline - Date.now();
1339
+ if (remaining <= 0) throw new SandboxWaitTimeoutError("pause", timeoutMs, this);
1340
+ await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1341
+ await this.refresh();
1342
+ }
1343
+ }
1344
+ /** Resume this sandbox from its preserved CPU and memory state. */
1345
+ async resume(options = {}) {
1346
+ const timeoutMs = options.timeoutMs ?? 3e4;
1347
+ validateTimeoutMs(timeoutMs);
1348
+ const deadline = Date.now() + timeoutMs;
1349
+ const data = await unwrap(this._client.POST("/api/sandboxes/{sid}/resume", { params: {
1350
+ path: { sid: this.id },
1351
+ query: { wait: false }
1352
+ } }));
1353
+ this._apply(data);
1354
+ for (;;) {
1355
+ if (this.status === "running") return this;
1356
+ if (this.status !== "pending") throw new ArchilError(`Sandbox entered ${this.status} before it resumed`, 409, "SANDBOX_RESUME_FAILED");
1357
+ const remaining = deadline - Date.now();
1358
+ if (remaining <= 0) throw new SandboxWaitTimeoutError("resume", timeoutMs, this);
1359
+ await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1360
+ await this.refresh();
1361
+ }
1362
+ }
1363
+ async exec(command, options = {}) {
1364
+ const timeoutMs = options.timeoutMs ?? 3e4;
1365
+ validateTimeoutMs(timeoutMs);
1366
+ const deadline = Date.now() + timeoutMs;
1367
+ const exec = new SandboxExec(await unwrap(this._client.POST("/api/sandboxes/{sid}/execs", {
1368
+ params: {
1369
+ path: { sid: this.id },
1370
+ query: { wait: false }
1371
+ },
1372
+ body: {
1373
+ command,
1374
+ command_tty: options.commandTty,
1375
+ env: options.env,
1376
+ timeout_seconds: options.timeoutSeconds
1377
+ }
1378
+ })), this._client);
1379
+ if (exec.status !== "running") return exec;
1380
+ for (;;) {
1381
+ const remaining = deadline - Date.now();
1382
+ if (remaining <= 0) throw new SandboxWaitTimeoutError("exec", timeoutMs, exec);
1383
+ await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1384
+ await exec.refresh();
1385
+ if (exec.status !== "running") return exec;
1386
+ }
1387
+ }
1388
+ async listExecs() {
1389
+ return ((await unwrap(this._client.GET("/api/sandboxes/{sid}/execs", { params: { path: { sid: this.id } } })))?.execs ?? []).map((exec) => new SandboxExec(exec, this._client));
1390
+ }
1391
+ async getExec(execId) {
1392
+ return new SandboxExec(await unwrap(this._client.GET("/api/sandboxes/{sid}/execs/{eid}", { params: { path: {
1393
+ sid: this.id,
1394
+ eid: execId
1395
+ } } })), this._client);
1396
+ }
1397
+ async cancelExec(execId) {
1398
+ return new SandboxExec(await unwrap(this._client.POST("/api/sandboxes/{sid}/execs/{eid}/cancel", { params: { path: {
1399
+ sid: this.id,
1400
+ eid: execId
1401
+ } } })), this._client);
1402
+ }
1403
+ };
1404
+ /** @internal Poll `sandbox` in place until it is running (or the deadline passes). */
1405
+ async function waitForSandboxStart(sandbox, deadline, timeoutMs) {
1406
+ for (;;) {
1407
+ if (sandbox.status === "running") return sandbox;
1408
+ if (sandbox.status !== "pending") throw new SandboxStartError(sandbox);
1409
+ const remaining = deadline - Date.now();
1410
+ if (remaining <= 0) throw new SandboxWaitTimeoutError("start", timeoutMs, sandbox);
1411
+ await new Promise((resolve) => setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)));
1412
+ await sandbox.refresh();
1413
+ }
1414
+ }
1415
+ //#endregion
1416
+ //#region src/sandboxes.ts
1417
+ var Sandboxes = class {
1418
+ /** @internal */
1419
+ _client;
1420
+ /** @internal */
1421
+ constructor(client) {
1422
+ this._client = client;
1423
+ }
1424
+ /** List the account's sandboxes, oldest first. */
1425
+ async list(options = {}) {
1426
+ const filesystem = typeof options.disk === "string" ? options.disk : options.disk?.id;
1427
+ return ((await unwrap(this._client.GET("/api/sandboxes", { params: { query: { filesystem } } })))?.sandboxes ?? []).map((sandbox) => new Sandbox(sandbox, this._client));
1428
+ }
1429
+ async get(id) {
1430
+ return new Sandbox(await unwrap(this._client.GET("/api/sandboxes/{sid}", { params: { path: { sid: id } } })), this._client);
1431
+ }
1432
+ async create(request = {}, options = {}) {
1433
+ const timeoutMs = options.timeoutMs ?? 3e4;
1434
+ validateTimeoutMs(timeoutMs);
1435
+ const deadline = Date.now() + timeoutMs;
1436
+ const body = {
1437
+ vcpu_count: request.vcpuCount,
1438
+ mem_size_mib: request.memSizeMiB,
1439
+ base_image: request.baseImage,
1440
+ env: request.env,
1441
+ max_ttl_seconds: request.maxTtlSeconds,
1442
+ max_concurrent_execs: request.maxConcurrentExecs
1443
+ };
1444
+ return waitForSandboxStart(new Sandbox(await unwrap(this._client.POST("/api/sandboxes", {
1445
+ params: { query: { wait: false } },
1446
+ body
1447
+ })), this._client), deadline, timeoutMs);
1448
+ }
1449
+ };
1102
1450
  //#endregion
1103
1451
  //#region src/tokens.ts
1104
1452
  var Tokens = class {
@@ -1381,6 +1729,7 @@ function diskIdFromMount(m) {
1381
1729
  }
1382
1730
  var Archil = class {
1383
1731
  disks;
1732
+ sandboxes;
1384
1733
  tokens;
1385
1734
  /** @internal */
1386
1735
  _client;
@@ -1398,6 +1747,7 @@ var Archil = class {
1398
1747
  const s3BaseUrl = opts.s3BaseUrl ?? envVar("ARCHIL_S3_BASE_URL") ?? deriveS3BaseUrl(controlBaseUrl);
1399
1748
  this._client = client;
1400
1749
  this.disks = new Disks(client, region, s3BaseUrl);
1750
+ this.sandboxes = new Sandboxes(client);
1401
1751
  this.tokens = new Tokens(client);
1402
1752
  }
1403
1753
  /**
@@ -1455,6 +1805,15 @@ function listDisks(opts) {
1455
1805
  function getDisk(id) {
1456
1806
  return archil().disks.get(id);
1457
1807
  }
1808
+ function createSandbox(req = {}, opts = {}) {
1809
+ return archil().sandboxes.create(req, opts);
1810
+ }
1811
+ function listSandboxes(opts) {
1812
+ return archil().sandboxes.list(opts);
1813
+ }
1814
+ function getSandbox(id) {
1815
+ return archil().sandboxes.get(id);
1816
+ }
1458
1817
  function listApiKeys(opts) {
1459
1818
  return archil().tokens.list(opts);
1460
1819
  }
@@ -1480,4 +1839,4 @@ function workspace(mounts) {
1480
1839
  return archil().workspace(mounts);
1481
1840
  }
1482
1841
  //#endregion
1483
- export { USER_AGENT as _, exec as a, ArchilError as b, listDisks as c, Workspace as d, Tokens as f, effectiveUploadPartSize as g, DiskMultipart as h, deleteApiKey as i, workspace as l, Disk as m, createApiKey as n, getDisk as o, Disks as p, createDisk as r, listApiKeys as s, configure as t, Archil as u, VERSION as v, ArchilS3Error as x, ArchilApiError as y };
1842
+ export { DiskMultipart as C, ArchilApiError as D, VERSION as E, ArchilError as O, Disk as S, USER_AGENT as T, Sandbox as _, deleteApiKey as a, SandboxWaitTimeoutError as b, getSandbox as c, listSandboxes as d, workspace as f, Sandboxes as g, Tokens as h, createSandbox as i, ArchilS3Error as k, listApiKeys as l, Workspace as m, createApiKey as n, exec as o, Archil as p, createDisk as r, getDisk as s, configure as t, listDisks as u, SandboxExec as v, effectiveUploadPartSize as w, Disks as x, SandboxStartError as y };
package/package.json CHANGED
@@ -1,7 +1,18 @@
1
1
  {
2
2
  "name": "disk",
3
- "version": "0.8.21",
3
+ "version": "0.8.23",
4
4
  "description": "Pure-JS client and CLI for Archil disks",
5
+ "keywords": [
6
+ "archil",
7
+ "disk",
8
+ "filesystem",
9
+ "cli"
10
+ ],
11
+ "homepage": "https://archil.com",
12
+ "license": "MIT",
13
+ "repository": {
14
+ "url": "https://github.com/archil-data/archil-sdk"
15
+ },
5
16
  "type": "module",
6
17
  "main": "dist/index.cjs",
7
18
  "module": "dist/index.mjs",
@@ -27,7 +38,7 @@
27
38
  "LICENSE"
28
39
  ],
29
40
  "dependencies": {
30
- "@archildata/api-types": "^0.0.1",
41
+ "@archildata/api-types": "^0.0.9",
31
42
  "commander": "^14.0.3",
32
43
  "fast-xml-parser": "^5.7.0",
33
44
  "openapi-fetch": "^0.13.5",
@@ -42,13 +53,6 @@
42
53
  "engines": {
43
54
  "node": ">= 22"
44
55
  },
45
- "keywords": [
46
- "archil",
47
- "disk",
48
- "filesystem",
49
- "cli"
50
- ],
51
- "license": "MIT",
52
56
  "scripts": {
53
57
  "build": "tsdown",
54
58
  "dev": "tsdown --watch",