@insforge/instabox-sdk 0.5.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 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: 0` makes a sandbox that stays on (a server that must keep answering), another value changes the idle time, `setAutoSuspend(ms)` changes it later and `getInfo().autoSuspendMs` shows it. Unlike a timeout the setting stays across pause and resume.
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,44 @@ 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
+
239
320
  ## Metrics and usage
240
321
 
241
322
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@insforge/instabox-sdk",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "TypeScript SDK for instabox: create sandboxes on Firecracker dev machines, run commands, move files, pause, resume, snapshot and SSH in.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -52,7 +52,7 @@ try {
52
52
  }
53
53
  ```
54
54
 
55
- A new sandbox pauses itself after 30 idle minutes and resumes on the next command, file read or SSH sign-in; pass `autoSuspendMs: 0` for one that must stay on, or another idle time, and `setAutoSuspend(ms)` to change it later. `timeoutMs` is a hard deadline (`onTimeout: 'kill'` deletes the sandbox then), which the examples use so a forgotten sandbox is removed.
55
+ A new sandbox pauses itself after 30 idle minutes and resumes on the next command, file read or SSH sign-in. Pass another idle time as `autoSuspendMs` (or `setAutoSuspend(ms)` later); `autoSuspendMs: 0` means the sandbox runs until code calls `pause()`, `stop()` or `kill()`. Keep `sandboxId` for that, and reattach with `Sandbox.connect(id, { resume: false })` so attaching does not wake it. `pause()` keeps memory and processes; `stop()` ends processes and keeps disks; `kill()` deletes the sandbox and its disks. `timeoutMs` is a hard deadline (`onTimeout: 'kill'` deletes the sandbox then), which the examples use so a forgotten sandbox is removed.
56
56
 
57
57
  Connection options `{ apiKey, apiUrl }` override environment variables. `agent-browser` (universal plus a desktop and browser) is the default template; `universal` on a server without it. Create accepts a template ID, a private snapshot name, or a `sn_...` snapshot ID. Template IDs take precedence; duplicate snapshot names require an ID. There are no official snapshots. Template creation and snapshot-ID creation need only machine scopes; snapshot-name lookup also needs `images:read`.
58
58