@typhons/sandbox-tools 0.4.1 → 0.4.3

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/clone.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import u from"fs";import E from"crypto";import{setTimeout as m}from"timers/promises";var l="",n="",i=process.env.TYPHONS_SERVER||"https://insta.d.typhons.dev:3000",r="",t=process.argv.slice(2).map(e=>e.replace(/[\x00-\x1f\x7f]/g,""));for(let e=0;e<t.length;e++)switch(t[e]){case"--name":case"-n":l=t[++e];break;case"--box":case"-b":n=t[++e];break;case"--server":case"-s":i=t[++e];break;case"--api-key":case"-k":r=t[++e];break;case"-h":case"--help":console.log("Usage: clone [--name NAME] [--box SOURCE_BOX] [--server URL] [--api-key KEY]"),console.log(""),console.log("Clone a sandbox. If --box is omitted, detects the current sandbox."),console.log("If --name is omitted, auto-generates a name."),process.exit(0);default:t[e].startsWith("-")||(l=t[e])}if(!r)for(let e of[`${process.env.HOME}/.config/typhons/api-key`,`${process.env.HOME}/.config/remote-claude/api-key`])try{r=u.readFileSync(e,"utf8").trim();break}catch{}!r&&process.env.TYPHONS_API_KEY&&(r=process.env.TYPHONS_API_KEY);r||(console.error("Error: No API key found."),console.error(" Set TYPHONS_API_KEY, pass --api-key, or save to ~/.config/typhons/api-key"),process.exit(1));if(!n){let e="";try{e=u.readFileSync("/run/e2b/.E2B_SANDBOX_ID","utf8").trim()}catch{}if(e)try{let a=(await(await fetch(`${i}/api/machines`,{headers:{Authorization:`Bearer ${r}`},signal:AbortSignal.timeout(1e4)})).json()).find(d=>d.machine?.sandboxId===e||d.sandboxId===e);a&&(n=a.app?.name||a.devboxName||"")}catch{}n||(console.error("Error: Could not detect current sandbox."),console.error(` sandbox_id=${e}, server=${i}`),console.error(" Use --box to specify."),process.exit(1))}var S=process.env.USER||"root",b=i.match(/https?:\/\/[^.]*\.(.+?)(?::\d+)?$/),g=b?b[1]:"d.typhons.dev",_=g.replace(/^d\./,"");function $(){try{return u.readFileSync("/run/e2b/.E2B_SANDBOX_ID","utf8").trim()}catch{}return""}async function f(e,s,p){try{let a={method:e,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},signal:AbortSignal.timeout(5e3)};return p&&(a.body=JSON.stringify(p)),await(await fetch(`${i}${s}`,a)).json()}catch{return{}}}function h(e){console.log(`Clone ready: ${e}`),console.log(` ssh ${S}.${e}@ssh.${_}`),console.log(` https://${e}.${g}:3000`)}console.log(`Cloning ${n}...`);var I=E.randomBytes(24).toString("base64url").slice(0,32);function x(){let e={idempotencyKey:I},s=$();return s&&(e.sandboxId=s),l&&(e.name=l),e}var o=await f("POST",`/api/machines/${n}/clone`,x()),c=o.jobId||"";c||(o.error&&(console.error(`Error: ${o.error}`),process.exit(1)),o.name&&(h(o.name),process.exit(0)));var y="";for(let e=0;e<120;e++){if(!c&&(o=await f("POST",`/api/machines/${n}/clone`,x()),c=o.jobId||"",o.status==="ready"&&(h(o.name),process.exit(0)),o.status==="you_are_the_clone"&&(console.log(`You are the clone (${o.name}).`),process.exit(0)),!c)){await m(1e3);continue}o=await f("GET",`/api/clone-jobs/${c}`);let s=o.status||"";if(s!==y){switch(s){case"snapshotting":console.log("Snapshotting... (the current devbox will briefly freeze)");break;case"creating":console.log("Snapshot done, cloning to new machine...");break;case"registering":console.log("Waiting for clone to be ready...");break;case"auth":break;case"ready":h(o.name),process.exit(0);case"failed":console.error(`Error: ${o.error||"Clone failed"}`),process.exit(1)}y=s}await m(1e3)}console.error("Error: Clone did not complete within 2 minutes");process.exit(1);
2
+ import m from"fs";import E from"crypto";import{setTimeout as f}from"timers/promises";var l="",n="",i=process.env.TYPHONS_SERVER||"https://insta.d.typhons.dev:3000",r="",t=process.argv.slice(2).map(e=>e.replace(/[\x00-\x1f\x7f]/g,""));for(let e=0;e<t.length;e++)switch(t[e]){case"--name":case"-n":l=t[++e];break;case"--box":case"-b":n=t[++e];break;case"--server":case"-s":i=t[++e];break;case"--api-key":case"-k":r=t[++e];break;case"-h":case"--help":console.log("Usage: clone [--name NAME] [--box SOURCE_BOX] [--server URL] [--api-key KEY]"),console.log(""),console.log("Clone a sandbox. If --box is omitted, detects the current sandbox."),console.log("If --name is omitted, auto-generates a name."),process.exit(0);default:t[e].startsWith("-")||(l=t[e])}if(!r)for(let e of[`${process.env.HOME}/.config/typhons/api-key`,`${process.env.HOME}/.config/remote-claude/api-key`])try{r=m.readFileSync(e,"utf8").trim();break}catch{}!r&&process.env.TYPHONS_API_KEY&&(r=process.env.TYPHONS_API_KEY);r||(console.error("Error: No API key found."),console.error(" Set TYPHONS_API_KEY, pass --api-key, or save to ~/.config/typhons/api-key"),process.exit(1));if(!n){let e="";try{e=m.readFileSync("/run/e2b/.E2B_SANDBOX_ID","utf8").trim()}catch{}if(e)try{let a=(await(await fetch(`${i}/api/machines`,{headers:{Authorization:`Bearer ${r}`},signal:AbortSignal.timeout(1e4)})).json()).find(d=>d.machine?.sandboxId===e||d.sandboxId===e);a&&(n=a.app?.name||a.devboxName||"")}catch{}n||(console.error("Error: Could not detect current sandbox."),console.error(` sandbox_id=${e}, server=${i}`),console.error(" Use --box to specify."),process.exit(1))}var S=process.env.USER||"root",b=i.match(/https?:\/\/[^.]*\.(.+?)(?::\d+)?$/),g=b?b[1]:"d.typhons.dev",_=g.replace(/^d\./,"");function $(){try{return m.readFileSync("/run/e2b/.E2B_SANDBOX_ID","utf8").trim()}catch{}return""}async function h(e,s,p){try{let a={method:e,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},signal:AbortSignal.timeout(5e3)};return p&&(a.body=JSON.stringify(p)),await(await fetch(`${i}${s}`,a)).json()}catch{return{}}}function u(e){console.log(`Clone ready: ${e}`),console.log(` ssh ${S}.${e}@ssh.${_}`),console.log(` https://${e}.${g}:3000`)}console.log(`Cloning ${n}...`);var I=E.randomBytes(24).toString("base64url").slice(0,32);function x(){let e={idempotencyKey:I},s=$();return s&&(e.sandboxId=s),l&&(e.name=l),e}var o=await h("POST",`/api/machines/${n}/clone`,x()),c=o.jobId||"";c||(o.error&&(console.error(`Error: ${o.error}`),process.exit(1)),o.name&&(u(o.name),process.exit(0)));var y="";for(let e=0;e<120;e++){if(!c&&(o=await h("POST",`/api/machines/${n}/clone`,x()),c=o.jobId||"",o.status==="ready"&&(u(o.name),process.exit(0)),o.status==="you_are_the_clone"&&(console.log(`You are the clone (${o.name}).`),process.exit(0)),!c)){await f(1e3);continue}o=await h("GET",`/api/clone-jobs/${c}`);let s=o.status||"";if(s===""){await f(500);continue}if(s!==y){switch(s){case"snapshotting":console.log("Snapshotting... (the current devbox will briefly freeze)");break;case"creating":console.log("Snapshot done, cloning to new machine...");break;case"registering":console.log("Waiting for clone to be ready...");break;case"auth":break;case"ready":u(o.name),process.exit(0);case"failed":console.error(`Error: ${o.error||"Clone failed"}`),process.exit(1)}y=s}await f(1e3)}console.error("Error: Clone did not complete within 2 minutes");process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typhons/sandbox-tools",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "sandbox-relay-client": "./relay-client.js",
@@ -2,7 +2,7 @@
2
2
  # Start all sandbox services: sshd + watchdog + keepalive.
3
3
  # Called on sandbox boot/resume.
4
4
 
5
- BIN="$(cd "$(dirname "$0")" && pwd)"
5
+ BIN="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
6
6
 
7
7
  # Start sshd
8
8
  mkdir -p /run/sshd
@@ -1,7 +1,7 @@
1
1
  #!/bin/bash
2
2
  # Watchdog: keeps both session-proxy and relay-client running.
3
3
 
4
- BIN="$(cd "$(dirname "$0")" && pwd)"
4
+ BIN="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
5
5
  export SSH_PORT="${SSH_PORT:-2222}"
6
6
 
7
7
  log() { echo "[watchdog $(date +%H:%M:%S)] $*"; }