@insforge/instabox-sdk 0.7.0 → 0.8.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 +32 -10
- package/dist/index.cjs +150 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -1
- package/dist/index.d.ts +49 -1
- package/dist/index.js +150 -52
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/skills/instabox/SKILL.md +10 -8
package/dist/index.d.cts
CHANGED
|
@@ -851,6 +851,52 @@ declare class Desktop {
|
|
|
851
851
|
private run;
|
|
852
852
|
}
|
|
853
853
|
|
|
854
|
+
/** `public`: anyone with the URL reaches it. `private`: only requests that carry the port's token. */
|
|
855
|
+
type PortAccess = 'public' | 'private';
|
|
856
|
+
/** A sandbox port published at a URL. */
|
|
857
|
+
interface PublicPort {
|
|
858
|
+
port: number;
|
|
859
|
+
/** Shown on the dashboard: `Port <n>` unless one was given. */
|
|
860
|
+
name: string;
|
|
861
|
+
/** `http`, or `tcp` for a raw TCP port published with `instabox port publish --tcp`. */
|
|
862
|
+
protocol: 'http' | 'tcp';
|
|
863
|
+
/** `https://<port>-<id>.<domain>` (`tls://<host>:<port>` for tcp), on the server's own domain. */
|
|
864
|
+
url: string;
|
|
865
|
+
access: PortAccess;
|
|
866
|
+
/** A private port's token (`ibp_...`), returned only by the publish that created or rotated it. */
|
|
867
|
+
token?: string;
|
|
868
|
+
/** `url` with the token as its `_token` query parameter: the link to share. Set with `token`. */
|
|
869
|
+
tokenUrl?: string;
|
|
870
|
+
createdAt: Date;
|
|
871
|
+
}
|
|
872
|
+
interface PublishPortOpts {
|
|
873
|
+
/** Name shown on the dashboard. Omitted: `Port <n>` for a new port, the current name otherwise. */
|
|
874
|
+
name?: string;
|
|
875
|
+
/** Omitted: public for a new port, the current access otherwise. */
|
|
876
|
+
access?: PortAccess;
|
|
877
|
+
/** Give a private port a new token: the old one stops working and its connections close. */
|
|
878
|
+
rotateToken?: boolean;
|
|
879
|
+
}
|
|
880
|
+
/** URLs for servers listening in the sandbox, as `instabox port publish` makes them. */
|
|
881
|
+
declare class Ports {
|
|
882
|
+
private readonly http;
|
|
883
|
+
private readonly sandboxId;
|
|
884
|
+
/** @internal */
|
|
885
|
+
constructor(http: HttpClient, sandboxId: string);
|
|
886
|
+
/**
|
|
887
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
888
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
889
|
+
*/
|
|
890
|
+
publish(port: number, opts?: PublishPortOpts): Promise<PublicPort>;
|
|
891
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
892
|
+
list(): Promise<PublicPort[]>;
|
|
893
|
+
/**
|
|
894
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
895
|
+
* Requires machines:write.
|
|
896
|
+
*/
|
|
897
|
+
unpublish(port: number): Promise<boolean>;
|
|
898
|
+
}
|
|
899
|
+
|
|
854
900
|
declare const DEFAULT_TEMPLATE = "agent-browser";
|
|
855
901
|
/** The default when a server does not offer `DEFAULT_TEMPLATE` (an older or self-hosted one). */
|
|
856
902
|
declare const FALLBACK_TEMPLATE = "universal";
|
|
@@ -993,6 +1039,8 @@ declare class Sandbox {
|
|
|
993
1039
|
readonly files: Files;
|
|
994
1040
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
995
1041
|
readonly desktop: Desktop;
|
|
1042
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
1043
|
+
readonly ports: Ports;
|
|
996
1044
|
private machine;
|
|
997
1045
|
private constructor();
|
|
998
1046
|
/**
|
|
@@ -1101,4 +1149,4 @@ declare class Sandbox {
|
|
|
1101
1149
|
private mutate;
|
|
1102
1150
|
}
|
|
1103
1151
|
|
|
1104
|
-
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
|
1152
|
+
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type PortAccess, Ports, type PublicPort, type PublishPortOpts, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
package/dist/index.d.ts
CHANGED
|
@@ -851,6 +851,52 @@ declare class Desktop {
|
|
|
851
851
|
private run;
|
|
852
852
|
}
|
|
853
853
|
|
|
854
|
+
/** `public`: anyone with the URL reaches it. `private`: only requests that carry the port's token. */
|
|
855
|
+
type PortAccess = 'public' | 'private';
|
|
856
|
+
/** A sandbox port published at a URL. */
|
|
857
|
+
interface PublicPort {
|
|
858
|
+
port: number;
|
|
859
|
+
/** Shown on the dashboard: `Port <n>` unless one was given. */
|
|
860
|
+
name: string;
|
|
861
|
+
/** `http`, or `tcp` for a raw TCP port published with `instabox port publish --tcp`. */
|
|
862
|
+
protocol: 'http' | 'tcp';
|
|
863
|
+
/** `https://<port>-<id>.<domain>` (`tls://<host>:<port>` for tcp), on the server's own domain. */
|
|
864
|
+
url: string;
|
|
865
|
+
access: PortAccess;
|
|
866
|
+
/** A private port's token (`ibp_...`), returned only by the publish that created or rotated it. */
|
|
867
|
+
token?: string;
|
|
868
|
+
/** `url` with the token as its `_token` query parameter: the link to share. Set with `token`. */
|
|
869
|
+
tokenUrl?: string;
|
|
870
|
+
createdAt: Date;
|
|
871
|
+
}
|
|
872
|
+
interface PublishPortOpts {
|
|
873
|
+
/** Name shown on the dashboard. Omitted: `Port <n>` for a new port, the current name otherwise. */
|
|
874
|
+
name?: string;
|
|
875
|
+
/** Omitted: public for a new port, the current access otherwise. */
|
|
876
|
+
access?: PortAccess;
|
|
877
|
+
/** Give a private port a new token: the old one stops working and its connections close. */
|
|
878
|
+
rotateToken?: boolean;
|
|
879
|
+
}
|
|
880
|
+
/** URLs for servers listening in the sandbox, as `instabox port publish` makes them. */
|
|
881
|
+
declare class Ports {
|
|
882
|
+
private readonly http;
|
|
883
|
+
private readonly sandboxId;
|
|
884
|
+
/** @internal */
|
|
885
|
+
constructor(http: HttpClient, sandboxId: string);
|
|
886
|
+
/**
|
|
887
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
888
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
889
|
+
*/
|
|
890
|
+
publish(port: number, opts?: PublishPortOpts): Promise<PublicPort>;
|
|
891
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
892
|
+
list(): Promise<PublicPort[]>;
|
|
893
|
+
/**
|
|
894
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
895
|
+
* Requires machines:write.
|
|
896
|
+
*/
|
|
897
|
+
unpublish(port: number): Promise<boolean>;
|
|
898
|
+
}
|
|
899
|
+
|
|
854
900
|
declare const DEFAULT_TEMPLATE = "agent-browser";
|
|
855
901
|
/** The default when a server does not offer `DEFAULT_TEMPLATE` (an older or self-hosted one). */
|
|
856
902
|
declare const FALLBACK_TEMPLATE = "universal";
|
|
@@ -993,6 +1039,8 @@ declare class Sandbox {
|
|
|
993
1039
|
readonly files: Files;
|
|
994
1040
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
995
1041
|
readonly desktop: Desktop;
|
|
1042
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
1043
|
+
readonly ports: Ports;
|
|
996
1044
|
private machine;
|
|
997
1045
|
private constructor();
|
|
998
1046
|
/**
|
|
@@ -1101,4 +1149,4 @@ declare class Sandbox {
|
|
|
1101
1149
|
private mutate;
|
|
1102
1150
|
}
|
|
1103
1151
|
|
|
1104
|
-
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
|
1152
|
+
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type PortAccess, Ports, type PublicPort, type PublishPortOpts, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
package/dist/index.js
CHANGED
|
@@ -301,7 +301,7 @@ ${cmd}`;
|
|
|
301
301
|
try {
|
|
302
302
|
r = o.onStdout || o.onStderr ? await this.execStream(cmd, o) : await this.exec(cmd, o);
|
|
303
303
|
} finally {
|
|
304
|
-
if (staged
|
|
304
|
+
if (staged) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
|
|
305
305
|
});
|
|
306
306
|
}
|
|
307
307
|
if (r.exitCode !== 0) throw new CommandExitError(r);
|
|
@@ -1275,24 +1275,44 @@ function unsafe(name) {
|
|
|
1275
1275
|
code: "invalid_response"
|
|
1276
1276
|
});
|
|
1277
1277
|
}
|
|
1278
|
-
|
|
1278
|
+
var RESERVED = /* @__PURE__ */ new Set([
|
|
1279
|
+
"CON",
|
|
1280
|
+
"PRN",
|
|
1281
|
+
"AUX",
|
|
1282
|
+
"NUL",
|
|
1283
|
+
"CONIN$",
|
|
1284
|
+
"CONOUT$",
|
|
1285
|
+
...["COM", "LPT"].flatMap((d) => [1, 2, 3, 4, 5, 6, 7, 8, 9, "\xB9", "\xB2", "\xB3"].map((i) => `${d}${i}`))
|
|
1286
|
+
]);
|
|
1287
|
+
var WINDOWS = globalThis.process?.platform === "win32";
|
|
1288
|
+
function components(name, windows = WINDOWS) {
|
|
1279
1289
|
if (name.startsWith("/")) throw unsafe(name);
|
|
1280
1290
|
const parts = [];
|
|
1281
1291
|
for (const p of name.split("/")) {
|
|
1282
1292
|
if (p === "" || p === ".") continue;
|
|
1283
1293
|
if (p === ".." || p.includes("\\") || p.includes("\0")) throw unsafe(name);
|
|
1294
|
+
if (windows && (p.includes(":") || p.endsWith(".") || p.endsWith(" ") || RESERVED.has(p.split(".")[0].replace(/ +$/, "").toUpperCase()))) {
|
|
1295
|
+
throw unsafe(name);
|
|
1296
|
+
}
|
|
1284
1297
|
parts.push(p);
|
|
1285
1298
|
}
|
|
1286
1299
|
return parts;
|
|
1287
1300
|
}
|
|
1288
|
-
async function unpackLocal(n, src, dest) {
|
|
1301
|
+
async function unpackLocal(n, src, dest, windows = WINDOWS) {
|
|
1289
1302
|
await n.fsp.mkdir(dest, { recursive: true });
|
|
1290
1303
|
const root = await n.fsp.realpath(dest);
|
|
1291
1304
|
const gunzip = n.zlib.createGunzip();
|
|
1292
1305
|
n.stream.pipeline(n.stream.Readable.from(src, { objectMode: false }), gunzip, () => {
|
|
1293
1306
|
});
|
|
1294
|
-
const dirModes =
|
|
1307
|
+
const dirModes = /* @__PURE__ */ new Map();
|
|
1295
1308
|
let files = 0;
|
|
1309
|
+
const writable = async (abs, st) => {
|
|
1310
|
+
const mode = st.mode & 4095;
|
|
1311
|
+
if ((mode & 192) === 192 || dirModes.has(abs)) return;
|
|
1312
|
+
dirModes.set(abs, mode);
|
|
1313
|
+
await n.fsp.chmod(abs, mode | 192);
|
|
1314
|
+
};
|
|
1315
|
+
await writable(root, await n.fsp.stat(root));
|
|
1296
1316
|
const ensureDir = async (parts, name) => {
|
|
1297
1317
|
let cur = root;
|
|
1298
1318
|
for (const p of parts) {
|
|
@@ -1305,6 +1325,7 @@ async function unpackLocal(n, src, dest) {
|
|
|
1305
1325
|
}
|
|
1306
1326
|
if (!st) await n.fsp.mkdir(cur, { mode: 493 });
|
|
1307
1327
|
else if (!st.isDirectory()) throw unsafe(name);
|
|
1328
|
+
else await writable(cur, st);
|
|
1308
1329
|
}
|
|
1309
1330
|
return cur;
|
|
1310
1331
|
};
|
|
@@ -1322,39 +1343,49 @@ async function unpackLocal(n, src, dest) {
|
|
|
1322
1343
|
}
|
|
1323
1344
|
await n.fsp.unlink(abs);
|
|
1324
1345
|
};
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1346
|
+
let ok = false;
|
|
1347
|
+
try {
|
|
1348
|
+
for await (const e of untarStream(gunzip)) {
|
|
1349
|
+
const parts = components(e.name, windows);
|
|
1350
|
+
if (parts.length === 0) {
|
|
1351
|
+
for await (const _ of e.data) void _;
|
|
1352
|
+
continue;
|
|
1353
|
+
}
|
|
1354
|
+
const parent = await ensureDir(parts.slice(0, -1), e.name);
|
|
1355
|
+
const abs = n.path.join(parent, parts[parts.length - 1]);
|
|
1356
|
+
if (e.type === "dir") {
|
|
1357
|
+
await ensureDir(parts, e.name);
|
|
1358
|
+
dirModes.set(abs, e.mode);
|
|
1359
|
+
} else if (e.type === "file") {
|
|
1360
|
+
await clear(abs, e.name);
|
|
1361
|
+
await n.sp.pipeline(
|
|
1362
|
+
n.stream.Readable.from(e.data, { objectMode: false }),
|
|
1363
|
+
n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
|
|
1364
|
+
);
|
|
1365
|
+
await n.fsp.chmod(abs, e.mode);
|
|
1366
|
+
files++;
|
|
1367
|
+
} else if (e.type === "symlink") {
|
|
1368
|
+
await clear(abs, e.name);
|
|
1369
|
+
await n.fsp.symlink(e.linkname, abs);
|
|
1370
|
+
} else if (e.type === "link") {
|
|
1371
|
+
const tparts = components(e.linkname, windows);
|
|
1372
|
+
const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
|
|
1373
|
+
const st = await n.fsp.lstat(target).catch(() => void 0);
|
|
1374
|
+
if (!st?.isFile()) throw unsafe(e.linkname);
|
|
1375
|
+
await clear(abs, e.name);
|
|
1376
|
+
await n.fsp.copyFile(target, abs);
|
|
1377
|
+
files++;
|
|
1378
|
+
}
|
|
1330
1379
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
|
|
1341
|
-
);
|
|
1342
|
-
await n.fsp.chmod(abs, e.mode);
|
|
1343
|
-
files++;
|
|
1344
|
-
} else if (e.type === "symlink") {
|
|
1345
|
-
await clear(abs, e.name);
|
|
1346
|
-
await n.fsp.symlink(e.linkname, abs);
|
|
1347
|
-
} else if (e.type === "link") {
|
|
1348
|
-
const tparts = components(e.linkname);
|
|
1349
|
-
const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
|
|
1350
|
-
const st = await n.fsp.lstat(target).catch(() => void 0);
|
|
1351
|
-
if (!st?.isFile()) throw unsafe(e.linkname);
|
|
1352
|
-
await clear(abs, e.name);
|
|
1353
|
-
await n.fsp.copyFile(target, abs);
|
|
1354
|
-
files++;
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
|
-
for (const [abs, mode] of dirModes.reverse()) await n.fsp.chmod(abs, mode);
|
|
1380
|
+
ok = true;
|
|
1381
|
+
} finally {
|
|
1382
|
+
const depth = (p) => p.split(n.path.sep).length;
|
|
1383
|
+
for (const [abs, mode] of [...dirModes].sort((a, b) => depth(b[0]) - depth(a[0]))) {
|
|
1384
|
+
await n.fsp.chmod(abs, mode).catch((err) => {
|
|
1385
|
+
if (ok) throw err;
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1358
1389
|
return files;
|
|
1359
1390
|
}
|
|
1360
1391
|
|
|
@@ -2410,6 +2441,69 @@ var HttpClient = class {
|
|
|
2410
2441
|
}
|
|
2411
2442
|
};
|
|
2412
2443
|
|
|
2444
|
+
// src/ports.ts
|
|
2445
|
+
var enc3 = encodeURIComponent;
|
|
2446
|
+
function checkPort(port) {
|
|
2447
|
+
if (!Number.isInteger(port)) {
|
|
2448
|
+
throw new InstaboxError(`port must be a whole number; got ${port}`, { code: "bad_request" });
|
|
2449
|
+
}
|
|
2450
|
+
return port;
|
|
2451
|
+
}
|
|
2452
|
+
function fromServer(p) {
|
|
2453
|
+
const out = {
|
|
2454
|
+
port: p.port,
|
|
2455
|
+
name: p.name,
|
|
2456
|
+
protocol: p.protocol,
|
|
2457
|
+
url: p.url,
|
|
2458
|
+
access: p.access,
|
|
2459
|
+
createdAt: new Date(p.createdAt)
|
|
2460
|
+
};
|
|
2461
|
+
if (p.token) {
|
|
2462
|
+
out.token = p.token;
|
|
2463
|
+
out.tokenUrl = p.tokenUrl;
|
|
2464
|
+
}
|
|
2465
|
+
return out;
|
|
2466
|
+
}
|
|
2467
|
+
var Ports = class {
|
|
2468
|
+
/** @internal */
|
|
2469
|
+
constructor(http, sandboxId) {
|
|
2470
|
+
this.http = http;
|
|
2471
|
+
this.sandboxId = sandboxId;
|
|
2472
|
+
}
|
|
2473
|
+
http;
|
|
2474
|
+
sandboxId;
|
|
2475
|
+
/**
|
|
2476
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
2477
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
2478
|
+
*/
|
|
2479
|
+
async publish(port, opts = {}) {
|
|
2480
|
+
const body = { port: checkPort(port) };
|
|
2481
|
+
if (opts.name !== void 0) body.name = opts.name;
|
|
2482
|
+
if (opts.access !== void 0) body.access = opts.access;
|
|
2483
|
+
if (opts.rotateToken) body.rotateToken = true;
|
|
2484
|
+
const r = await this.http.request("POST", `/v1/machines/${enc3(this.sandboxId)}/ports`, { body });
|
|
2485
|
+
return fromServer(r);
|
|
2486
|
+
}
|
|
2487
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
2488
|
+
async list() {
|
|
2489
|
+
const r = await this.http.request("GET", `/v1/machines/${enc3(this.sandboxId)}/ports`);
|
|
2490
|
+
return r.ports.map(fromServer);
|
|
2491
|
+
}
|
|
2492
|
+
/**
|
|
2493
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
2494
|
+
* Requires machines:write.
|
|
2495
|
+
*/
|
|
2496
|
+
async unpublish(port) {
|
|
2497
|
+
try {
|
|
2498
|
+
await this.http.request("DELETE", `/v1/machines/${enc3(this.sandboxId)}/ports/${checkPort(port)}`);
|
|
2499
|
+
return true;
|
|
2500
|
+
} catch (err) {
|
|
2501
|
+
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
2502
|
+
throw err;
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
};
|
|
2506
|
+
|
|
2413
2507
|
// src/sandbox.ts
|
|
2414
2508
|
var DEFAULT_TEMPLATE = "agent-browser";
|
|
2415
2509
|
var FALLBACK_TEMPLATE = "universal";
|
|
@@ -2455,7 +2549,7 @@ function toSeconds(ms, field) {
|
|
|
2455
2549
|
return s;
|
|
2456
2550
|
}
|
|
2457
2551
|
var METADATA_KEY = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
2458
|
-
var
|
|
2552
|
+
var enc4 = encodeURIComponent;
|
|
2459
2553
|
var Sandbox = class _Sandbox {
|
|
2460
2554
|
constructor(http, machine, opts = {}) {
|
|
2461
2555
|
this.http = http;
|
|
@@ -2465,6 +2559,7 @@ var Sandbox = class _Sandbox {
|
|
|
2465
2559
|
this.files = new Files(this.commands, http, machine.id);
|
|
2466
2560
|
this.commands.stageStdin = (bytes, signal) => this.files.stageStdin(bytes, signal);
|
|
2467
2561
|
this.desktop = new Desktop(http, machine.id, this.commands, this.files);
|
|
2562
|
+
this.ports = new Ports(http, machine.id);
|
|
2468
2563
|
}
|
|
2469
2564
|
http;
|
|
2470
2565
|
/** The machine ID (`m_...`). */
|
|
@@ -2475,6 +2570,8 @@ var Sandbox = class _Sandbox {
|
|
|
2475
2570
|
files;
|
|
2476
2571
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
2477
2572
|
desktop;
|
|
2573
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
2574
|
+
ports;
|
|
2478
2575
|
machine;
|
|
2479
2576
|
static async create(a, b) {
|
|
2480
2577
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
@@ -2554,12 +2651,12 @@ var Sandbox = class _Sandbox {
|
|
|
2554
2651
|
}
|
|
2555
2652
|
/** Fetches a snapshot by ID. Requires images:read. */
|
|
2556
2653
|
static async getSnapshot(snapshotId, opts = {}) {
|
|
2557
|
-
return new HttpClient(opts).request("GET", `/v1/snapshots/${
|
|
2654
|
+
return new HttpClient(opts).request("GET", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2558
2655
|
}
|
|
2559
2656
|
/** Deletes a snapshot. Returns false if absent. Requires images:delete. */
|
|
2560
2657
|
static async deleteSnapshot(snapshotId, opts = {}) {
|
|
2561
2658
|
try {
|
|
2562
|
-
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${
|
|
2659
|
+
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2563
2660
|
return true;
|
|
2564
2661
|
} catch (err) {
|
|
2565
2662
|
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
@@ -2574,7 +2671,7 @@ var Sandbox = class _Sandbox {
|
|
|
2574
2671
|
async createSnapshot(opts) {
|
|
2575
2672
|
await this.settle();
|
|
2576
2673
|
await this.untilSynced();
|
|
2577
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2674
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/snapshots`, { body: opts });
|
|
2578
2675
|
}
|
|
2579
2676
|
/** This sandbox's recent CPU and memory samples; see `Sandbox.getMetrics`. */
|
|
2580
2677
|
async getMetrics(opts = {}) {
|
|
@@ -2594,7 +2691,7 @@ var Sandbox = class _Sandbox {
|
|
|
2594
2691
|
* pause off. The setting stays across pause and resume.
|
|
2595
2692
|
*/
|
|
2596
2693
|
async setAutoSuspend(idleMs) {
|
|
2597
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2694
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2598
2695
|
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
2599
2696
|
});
|
|
2600
2697
|
}
|
|
@@ -2608,7 +2705,7 @@ var Sandbox = class _Sandbox {
|
|
|
2608
2705
|
}
|
|
2609
2706
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
2610
2707
|
async setMetadata(metadata) {
|
|
2611
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2708
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2612
2709
|
body: { metadata }
|
|
2613
2710
|
});
|
|
2614
2711
|
}
|
|
@@ -2618,21 +2715,21 @@ var Sandbox = class _Sandbox {
|
|
|
2618
2715
|
* Throws `not_found` on a server without the Vault.
|
|
2619
2716
|
*/
|
|
2620
2717
|
async vault() {
|
|
2621
|
-
return this.http.request("GET", `/v1/machines/${
|
|
2718
|
+
return this.http.request("GET", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2622
2719
|
}
|
|
2623
2720
|
/**
|
|
2624
2721
|
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
2625
2722
|
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
2626
2723
|
*/
|
|
2627
2724
|
async pushVault() {
|
|
2628
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2725
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2629
2726
|
}
|
|
2630
2727
|
/**
|
|
2631
2728
|
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
2632
2729
|
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
2633
2730
|
*/
|
|
2634
2731
|
async disableVault() {
|
|
2635
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2732
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2636
2733
|
body: { vault: false }
|
|
2637
2734
|
});
|
|
2638
2735
|
}
|
|
@@ -2679,7 +2776,7 @@ var Sandbox = class _Sandbox {
|
|
|
2679
2776
|
}
|
|
2680
2777
|
body.ttlSeconds = opts.ttlSeconds;
|
|
2681
2778
|
}
|
|
2682
|
-
const c = await this.http.request("POST", `/v1/machines/${
|
|
2779
|
+
const c = await this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/connections`, { body });
|
|
2683
2780
|
return {
|
|
2684
2781
|
host: c.host,
|
|
2685
2782
|
port: c.port,
|
|
@@ -2698,7 +2795,7 @@ var Sandbox = class _Sandbox {
|
|
|
2698
2795
|
async settle(refresh = true) {
|
|
2699
2796
|
if (refresh) this.machine = await getMachine(this.http, this.sandboxId);
|
|
2700
2797
|
if (this.machine.operationId) {
|
|
2701
|
-
const op = await this.http.request("GET", `/v1/operations/${
|
|
2798
|
+
const op = await this.http.request("GET", `/v1/operations/${enc4(this.machine.operationId)}`);
|
|
2702
2799
|
await this.http.waitForOperation(op).catch(() => {
|
|
2703
2800
|
});
|
|
2704
2801
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
@@ -2726,29 +2823,29 @@ var Sandbox = class _Sandbox {
|
|
|
2726
2823
|
}
|
|
2727
2824
|
}
|
|
2728
2825
|
async mutate(method, action) {
|
|
2729
|
-
const op = await this.http.request(method, `/v1/machines/${
|
|
2826
|
+
const op = await this.http.request(method, `/v1/machines/${enc4(this.sandboxId)}/${action}`);
|
|
2730
2827
|
await this.http.waitForOperation(op);
|
|
2731
2828
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
2732
2829
|
}
|
|
2733
2830
|
};
|
|
2734
2831
|
async function getMachine(http, id) {
|
|
2735
|
-
return http.request("GET", `/v1/machines/${
|
|
2832
|
+
return http.request("GET", `/v1/machines/${enc4(id)}`);
|
|
2736
2833
|
}
|
|
2737
2834
|
async function getMetrics(http, id, opts) {
|
|
2738
|
-
const out = await http.request("GET", `/v1/machines/${
|
|
2835
|
+
const out = await http.request("GET", `/v1/machines/${enc4(id)}/metrics`, {
|
|
2739
2836
|
query: { since: opts.since?.toISOString() }
|
|
2740
2837
|
});
|
|
2741
2838
|
return out.metrics.map((s) => ({ ...s, timestamp: new Date(s.timestamp) }));
|
|
2742
2839
|
}
|
|
2743
2840
|
async function getUsage(http, id, opts) {
|
|
2744
|
-
const u = await http.request("GET", `/v1/machines/${
|
|
2841
|
+
const u = await http.request("GET", `/v1/machines/${enc4(id)}/usage`, {
|
|
2745
2842
|
query: { from: opts.from?.toISOString(), to: opts.to?.toISOString() }
|
|
2746
2843
|
});
|
|
2747
2844
|
return { ...u, from: new Date(u.from), to: new Date(u.to) };
|
|
2748
2845
|
}
|
|
2749
2846
|
async function killMachine(http, id) {
|
|
2750
2847
|
try {
|
|
2751
|
-
const op = await http.request("DELETE", `/v1/machines/${
|
|
2848
|
+
const op = await http.request("DELETE", `/v1/machines/${enc4(id)}`, {
|
|
2752
2849
|
headers: { "X-Confirm-Delete": id }
|
|
2753
2850
|
});
|
|
2754
2851
|
await http.waitForOperation(op);
|
|
@@ -2812,6 +2909,7 @@ export {
|
|
|
2812
2909
|
MAX_FILE_BYTES,
|
|
2813
2910
|
MAX_STDIN_BYTES,
|
|
2814
2911
|
MAX_UPLOAD_BYTES,
|
|
2912
|
+
Ports,
|
|
2815
2913
|
Sandbox,
|
|
2816
2914
|
isLimitCode
|
|
2817
2915
|
};
|