@insforge/instabox-sdk 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -11
- package/dist/index.cjs +32 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +117 -1
- package/dist/index.d.ts +117 -1
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/instabox/SKILL.md +1 -1
package/README.md
CHANGED
|
@@ -30,14 +30,14 @@ The key is sent as the `X-API-Key` header.
|
|
|
30
30
|
|
|
31
31
|
### Scopes
|
|
32
32
|
|
|
33
|
-
| Scope | Allows
|
|
34
|
-
| ----------------- |
|
|
35
|
-
| `machines:read` | `Sandbox.list`, `getInfo`, `isRunning`, `vault`
|
|
36
|
-
| `machines:write` | `create`, `connect` (resume), `pause`, `resume`, `stop`, `setTimeout`, `setMetadata`, `disableVault`, `pushVault`, `commands`, `files`, `sshCredentials` |
|
|
37
|
-
| `machines:delete` | `kill`
|
|
38
|
-
| `images:read` | Resolve private snapshot names, `listSnapshots`, `getSnapshot`
|
|
39
|
-
| `images:write` | `createSnapshot`
|
|
40
|
-
| `images:delete` | `deleteSnapshot`
|
|
33
|
+
| Scope | Allows |
|
|
34
|
+
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
35
|
+
| `machines:read` | `Sandbox.list`, `getInfo`, `isRunning`, `vault` |
|
|
36
|
+
| `machines:write` | `create`, `connect` (resume), `pause`, `resume`, `stop`, `setTimeout`, `setAutoSuspend`, `setMetadata`, `disableVault`, `pushVault`, `commands`, `files`, `sshCredentials` |
|
|
37
|
+
| `machines:delete` | `kill` |
|
|
38
|
+
| `images:read` | Resolve private snapshot names, `listSnapshots`, `getSnapshot` |
|
|
39
|
+
| `images:write` | `createSnapshot` |
|
|
40
|
+
| `images:delete` | `deleteSnapshot` |
|
|
41
41
|
|
|
42
42
|
`:write` and `:delete` imply `:read`. Creating from a template or a snapshot ID needs only machine scopes. Creating from a snapshot name also needs `images:read`. Snapshot permissions are named `images:*` by the current API. Add `images:write` and `images:delete` if your code saves and deletes snapshots. A call outside the key's scopes throws `InstaboxAuthError` (`403 insufficient_scope`).
|
|
43
43
|
|
|
@@ -63,7 +63,7 @@ const sandbox = await Sandbox.create('universal', {
|
|
|
63
63
|
timeoutMs: 30 * 60_000, // server pauses it 30 min from now (60 s to 24 h)
|
|
64
64
|
archiveMs: 24 * 60 * 60_000, // stops it at this deadline, independent of timeout
|
|
65
65
|
onTimeout: 'pause', // or 'kill' to delete it instead
|
|
66
|
-
autoSuspendMs: 10 * 60_000, // pauses it after 10 idle minutes (default 30; 0: never)
|
|
66
|
+
autoSuspendMs: 10 * 60_000, // pauses it after 10 idle minutes (default 30; 0: never, see Manual lifecycle)
|
|
67
67
|
})
|
|
68
68
|
console.log(sandbox.sandboxId) // m_...
|
|
69
69
|
```
|
|
@@ -147,7 +147,50 @@ Every lifecycle call waits for its operation to finish (default limit 10 minutes
|
|
|
147
147
|
|
|
148
148
|
A timeout is enforced by the server: at `endAt` it suspends the sandbox (`onTimeout: 'pause'`, resume it with `Sandbox.connect`) or deletes it (`'kill'`). It fires once and is then cleared, so a resumed sandbox runs without a timeout until you set one again. Without `timeoutMs` there is no deadline.
|
|
149
149
|
|
|
150
|
-
Idle pause: a new sandbox pauses after 30 idle minutes (no agent working, no terminal use, little CPU) and resumes on the next `commands.run`, file read or SSH sign-in, processes and memory intact. `autoSuspendMs
|
|
150
|
+
Idle pause: a new sandbox pauses after 30 idle minutes (no agent working, no terminal use, little CPU) and resumes on the next `commands.run`, file read or SSH sign-in, processes and memory intact. `autoSuspendMs` sets another idle time or 0 (never), `setAutoSuspend(ms)` changes it later and the dashboard always uses 30 minutes. `getInfo().autoSuspendMs` shows what applies. Unlike a timeout the setting stays across pause and resume.
|
|
151
|
+
|
|
152
|
+
### Pause, stop or kill
|
|
153
|
+
|
|
154
|
+
| | `pause()` | `stop()` | `kill()` |
|
|
155
|
+
| --------------------------- | ------------------------------------------------ | -------------------------------------------- | ----------------------------------------- |
|
|
156
|
+
| Processes, memory | Kept: it picks up where it left off | Ended: the next start boots fresh | Gone |
|
|
157
|
+
| Disks (`/data`, root) | Kept | Kept | Deleted, cannot be undone |
|
|
158
|
+
| Back with | `resume()` or `Sandbox.connect(id)` | `resume()` or `Sandbox.connect(id)` (a boot) | Nothing: save a snapshot first to keep it |
|
|
159
|
+
| Counts toward `maxMachines` | Yes | Yes | No |
|
|
160
|
+
| Use it for | A break: the user comes back to the same session | Done for now: free memory, keep the files | Done for good |
|
|
161
|
+
|
|
162
|
+
The server's own pauses (idle pause, `onTimeout: 'pause'`) are the same as `pause()`; `archiveMs` is a `stop()`.
|
|
163
|
+
|
|
164
|
+
### Manual lifecycle: the sandbox pauses only when you pause it
|
|
165
|
+
|
|
166
|
+
For a product that decides itself when its sandboxes run, create them with `autoSuspendMs: 0`: the sandbox never pauses for idleness, and no timeout or archive deadline is set unless you pass one. It runs until your code pauses, stops or kills it, so keep its ID:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
// `db` is your own store, mapping your user to their sandbox
|
|
170
|
+
async function start(userId: string) {
|
|
171
|
+
const box = await Sandbox.create({ autoSuspendMs: 0, metadata: { userId } })
|
|
172
|
+
await db.saveSandbox(userId, box.sandboxId) // m_...
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// later, in any process with the same API key
|
|
176
|
+
async function pause(userId: string) {
|
|
177
|
+
const id = await db.sandboxOf(userId)
|
|
178
|
+
const box = await Sandbox.connect(id, { resume: false }) // attach without waking it
|
|
179
|
+
await box.pause() // or box.stop()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function remove(userId: string) {
|
|
183
|
+
await Sandbox.kill(await db.sandboxOf(userId)) // delete, no connect needed
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// lost the ID: find it by the metadata you set
|
|
187
|
+
async function findSandbox(userId: string) {
|
|
188
|
+
const [info] = await Sandbox.list({ metadata: { userId } })
|
|
189
|
+
return info?.sandboxId
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`Sandbox.connect(id)` without `resume: false` resumes a paused or stopped sandbox, so use it when the user comes back, not to pause. A sandbox your code paused stays paused: commands, file reads and SSH do not wake it, only `resume()` or `connect()`. An existing sandbox switches with `setAutoSuspend(0)`. If your process can die before it pauses a sandbox, add a backstop: `setTimeout(4 * 60 * 60_000)` pauses it 4 hours from now (`{ onTimeout: 'kill' }` deletes it), and calling it again pushes the deadline out.
|
|
151
194
|
|
|
152
195
|
## Vault
|
|
153
196
|
|
|
@@ -236,6 +279,64 @@ await sandbox.desktop.revokeStreamUrls() // every link stops working; open viewe
|
|
|
236
279
|
|
|
237
280
|
`streamUrl` needs no instabox account to open: anyone with the link can connect until `expiresAt` (60 s to 24 h, default 1 h). Expiry stops new connections; revoke ends open sessions: a viewer already watching stays until they leave or you call `revokeStreamUrls()`, which ends every link of the sandbox and disconnects its viewers within a few seconds. Revoke when someone's access should end, for example when a tenant's session or subscription ends. Links are view-only by default, enforced by the server; `viewOnly: false` lets the viewer use the mouse and keyboard. The sandbox and its desktop must be running when the link is opened.
|
|
238
281
|
|
|
282
|
+
### One sandbox per user, shown over VNC
|
|
283
|
+
|
|
284
|
+
A product that gives each of its users a computer they watch, or drive, in its own page:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
// Your server, after it has authenticated the user. Look the sandbox ID up in
|
|
288
|
+
// your own records, never take it from the browser: the API key controls
|
|
289
|
+
// every sandbox of the account.
|
|
290
|
+
|
|
291
|
+
// sign-up: one sandbox per user, without your account's Vault, paused only by you
|
|
292
|
+
async function onSignUp(userId: string) {
|
|
293
|
+
const box = await Sandbox.create({ vault: false, autoSuspendMs: 0, metadata: { userId } })
|
|
294
|
+
await db.saveSandbox(userId, box.sandboxId)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// the user opens your page: returns the URL for an <iframe>
|
|
298
|
+
async function onOpen(userId: string) {
|
|
299
|
+
const box = await Sandbox.connect(await db.sandboxOf(userId)) // resumes it if paused or stopped
|
|
300
|
+
await box.desktop.start()
|
|
301
|
+
const { url } = await box.desktop.streamUrl({ viewOnly: false, ttlSeconds: 3600 })
|
|
302
|
+
return url // mint a new one when it expires
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// the user leaves
|
|
306
|
+
async function onLeave(userId: string) {
|
|
307
|
+
const box = await Sandbox.connect(await db.sandboxOf(userId), { resume: false })
|
|
308
|
+
await box.desktop.revokeStreamUrls() // ends their viewer now
|
|
309
|
+
await box.pause()
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// the user cancels
|
|
313
|
+
async function onCancel(userId: string) {
|
|
314
|
+
await Sandbox.kill(await db.sandboxOf(userId))
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
The desktop must be running when the link is opened: a link to a paused sandbox does not wake it, so `connect()` before you hand it out. An open viewer does not count as activity, so with the default idle pause a sandbox someone only watches still pauses after 30 idle minutes. Each open viewer counts toward your account's limit of open terminals and desktops (8 unless we raised it for you). Keep `vault: false` for sandboxes your users can use: anything running in one can read what the Vault writes.
|
|
319
|
+
|
|
320
|
+
## Metrics and usage
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
// CPU and memory, one sample per 30 s, oldest first (default: the last 5 minutes, at most 24 h back)
|
|
324
|
+
const samples = await sandbox.getMetrics({ since: new Date(Date.now() - 15 * 60_000) })
|
|
325
|
+
// [{ timestamp: Date, cpuUsedPct: 12.5, cpuCount: 4, memUsed, memTotal, diskTotal }]
|
|
326
|
+
|
|
327
|
+
// What the sandbox used, for billing your own customers (default: this calendar month, UTC, to now)
|
|
328
|
+
const usage = await Sandbox.getUsage(sandbox.sandboxId, { from: new Date('2026-10-01T00:00:00Z') })
|
|
329
|
+
// { from, to, runningSeconds, vcpuSeconds, memGiBSeconds, diskGiBSeconds }
|
|
330
|
+
|
|
331
|
+
// Your account: counts, stored bytes (per sandbox in storage.machines, measured hourly) and limits
|
|
332
|
+
const account = await Sandbox.usage()
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
All three need `machines:read` (`Sandbox.usage()` also works with `images:read`).
|
|
336
|
+
|
|
337
|
+
- Metrics are measured on the host: `cpuUsedPct` is CPU time over the sandbox's vCPUs (0-100), `memUsed` the guest memory it has touched, in bytes. `diskUsed` is not reported yet. The list is empty while the sandbox is not running.
|
|
338
|
+
- `runningSeconds` counts time running, not paused or stopped; `vcpuSeconds` and `memGiBSeconds` multiply it by the sandbox's size. `diskGiBSeconds` is its disk size (system disk plus `/data`) times the time it existed in the window, paused or stopped included. Running time is recorded from the server release that added this route; earlier time is not counted. A deleted sandbox's usage stays readable by its ID.
|
|
339
|
+
|
|
239
340
|
## Errors
|
|
240
341
|
|
|
241
342
|
| Class | When |
|
|
@@ -276,7 +377,7 @@ INSTABOX_LIVE=1 INSTABOX_API_KEY=... npm run test:live # creates and deletes o
|
|
|
276
377
|
Bump `version` in `package.json` in a PR. After it merges, tag that commit and push the tag:
|
|
277
378
|
|
|
278
379
|
```bash
|
|
279
|
-
git tag v0.
|
|
380
|
+
git tag v0.4.0 && git push origin v0.4.0
|
|
280
381
|
```
|
|
281
382
|
|
|
282
383
|
The Release workflow checks that the tag is the package's version and that it is not on npm yet, runs the
|
package/dist/index.cjs
CHANGED
|
@@ -833,6 +833,18 @@ var Sandbox = class _Sandbox {
|
|
|
833
833
|
static async kill(sandboxId, opts = {}) {
|
|
834
834
|
return killMachine(new HttpClient(opts), sandboxId);
|
|
835
835
|
}
|
|
836
|
+
/** A sandbox's recent CPU and memory samples, oldest first (empty while it is not running). Requires machines:read. */
|
|
837
|
+
static async getMetrics(sandboxId, opts = {}) {
|
|
838
|
+
return getMetrics(new HttpClient(opts), sandboxId, opts);
|
|
839
|
+
}
|
|
840
|
+
/** What a sandbox used over a window (default: this calendar month); works for a deleted one by ID. Requires machines:read. */
|
|
841
|
+
static async getUsage(sandboxId, opts = {}) {
|
|
842
|
+
return getUsage(new HttpClient(opts), sandboxId, opts);
|
|
843
|
+
}
|
|
844
|
+
/** Your account's counts, stored bytes (per sandbox in `storage`) and limits. Requires machines:read or images:read. */
|
|
845
|
+
static async usage(opts = {}) {
|
|
846
|
+
return new HttpClient(opts).request("GET", "/v1/usage");
|
|
847
|
+
}
|
|
836
848
|
/** Lists the caller's private snapshots. Requires images:read. */
|
|
837
849
|
static async listSnapshots(opts = {}) {
|
|
838
850
|
const result = await new HttpClient(opts).request("GET", "/v1/snapshots");
|
|
@@ -862,6 +874,14 @@ var Sandbox = class _Sandbox {
|
|
|
862
874
|
await this.untilSynced();
|
|
863
875
|
return this.http.request("POST", `/v1/machines/${enc2(this.sandboxId)}/snapshots`, { body: opts });
|
|
864
876
|
}
|
|
877
|
+
/** This sandbox's recent CPU and memory samples; see `Sandbox.getMetrics`. */
|
|
878
|
+
async getMetrics(opts = {}) {
|
|
879
|
+
return getMetrics(this.http, this.sandboxId, opts);
|
|
880
|
+
}
|
|
881
|
+
/** What this sandbox used over a window; see `Sandbox.getUsage`. */
|
|
882
|
+
async getUsage(opts = {}) {
|
|
883
|
+
return getUsage(this.http, this.sandboxId, opts);
|
|
884
|
+
}
|
|
865
885
|
/** Fetches the current state of the sandbox. */
|
|
866
886
|
async getInfo() {
|
|
867
887
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
@@ -1013,6 +1033,18 @@ var Sandbox = class _Sandbox {
|
|
|
1013
1033
|
async function getMachine(http, id) {
|
|
1014
1034
|
return http.request("GET", `/v1/machines/${enc2(id)}`);
|
|
1015
1035
|
}
|
|
1036
|
+
async function getMetrics(http, id, opts) {
|
|
1037
|
+
const out = await http.request("GET", `/v1/machines/${enc2(id)}/metrics`, {
|
|
1038
|
+
query: { since: opts.since?.toISOString() }
|
|
1039
|
+
});
|
|
1040
|
+
return out.metrics.map((s) => ({ ...s, timestamp: new Date(s.timestamp) }));
|
|
1041
|
+
}
|
|
1042
|
+
async function getUsage(http, id, opts) {
|
|
1043
|
+
const u = await http.request("GET", `/v1/machines/${enc2(id)}/usage`, {
|
|
1044
|
+
query: { from: opts.from?.toISOString(), to: opts.to?.toISOString() }
|
|
1045
|
+
});
|
|
1046
|
+
return { ...u, from: new Date(u.from), to: new Date(u.to) };
|
|
1047
|
+
}
|
|
1016
1048
|
async function killMachine(http, id) {
|
|
1017
1049
|
try {
|
|
1018
1050
|
const op = await http.request("DELETE", `/v1/machines/${enc2(id)}`);
|