@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
224 lines • 6.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SSHConnection = void 0;
exports.deriveGatewayAddress = deriveGatewayAddress;
exports.discoverSSHGatewayUrl = discoverSSHGatewayUrl;
exports.gatewaySSHUrl = gatewaySSHUrl;
exports.connectSSH = connectSSH;
const ws_1 = __importDefault(require("ws"));
const auth_js_1 = require("./auth.js");
const errors_js_1 = require("./errors.js");
class SSHConnection {
ws;
closed = false;
readBuf = [];
readResolve = null;
constructor(ws) {
this.ws = ws;
ws.on("message", (data) => {
if (this.readResolve) {
const resolve = this.readResolve;
this.readResolve = null;
resolve(new Uint8Array(data));
}
else {
this.readBuf.push(data);
}
});
ws.on("close", () => {
this.closed = true;
if (this.readResolve) {
const resolve = this.readResolve;
this.readResolve = null;
resolve(null);
}
});
ws.on("error", () => {
this.closed = true;
if (this.readResolve) {
const resolve = this.readResolve;
this.readResolve = null;
resolve(null);
}
});
}
async read() {
if (this.readBuf.length > 0) {
return new Uint8Array(this.readBuf.shift());
}
if (this.closed)
return null;
return new Promise((resolve) => {
this.readResolve = resolve;
});
}
async write(data) {
return new Promise((resolve, reject) => {
this.ws.send(data, (err) => {
if (err)
reject(new errors_js_1.SSHUnavailableError(err.message, { cause: err }));
else
resolve();
});
});
}
close() {
if (this.closed)
return;
this.closed = true;
try {
this.ws.close(1000, "");
}
catch {
// ignore
}
}
}
exports.SSHConnection = SSHConnection;
function deriveGatewayAddress(baseUrl) {
let u;
try {
u = new URL(baseUrl.trim());
}
catch {
return "";
}
if (!u.hostname)
return "";
let host = u.hostname;
if (host.startsWith("api.")) {
host = "sandbox-gateway." + host.slice(4);
}
else if (host.startsWith("app.")) {
host = "sandbox-gateway." + host.slice(4);
}
else {
host = "sandbox-gateway." + host;
}
const port = u.port;
u.hostname = host;
if (port)
u.port = port;
u.pathname = "";
u.search = "";
u.hash = "";
return u.toString().replace(/\/$/, "");
}
// Returns the first healthy automatically selected URL, or "" to use the configured gateway.
async function discoverSSHGatewayUrl(gatewayClient, sessionId) {
const sid = sessionId.trim();
if (!sid)
return "";
let gateways;
try {
gateways = (await gatewayClient.listActiveSSHGateways({ sessionId: sid })).gateways ?? [];
}
catch {
return "";
}
for (const g of gateways) {
const bridge = g.wsBridgeEndpoint.trim();
if (!bridge || !g.healthy)
continue;
let u;
try {
u = new URL(bridge);
}
catch {
continue;
}
if (!u.host)
continue;
if (u.protocol === "http:")
u.protocol = "ws:";
else if (u.protocol === "https:")
u.protocol = "wss:";
u.pathname = `${u.pathname.replace(/\/+$/, "")}/v1/ssh/${sid}`;
u.search = "";
u.hash = "";
return u.toString();
}
return "";
}
function gatewaySSHUrl(gatewayAddress, sessionId) {
if (!gatewayAddress.trim() || !sessionId.trim())
return "";
let u;
try {
u = new URL(gatewayAddress.trim());
}
catch {
return "";
}
u.pathname = `/v1/ssh/${sessionId}`;
u.search = "";
u.hash = "";
if (u.protocol === "http:")
u.protocol = "ws:";
else if (u.protocol === "https:")
u.protocol = "wss:";
return u.toString();
}
async function connectSSH(gatewayAddress, sessionId, authToken, options) {
let url = "";
if (options?.discoverGateway) {
try {
url = (await options.discoverGateway(sessionId)).trim();
}
catch {
url = "";
}
}
if (!url)
url = gatewaySSHUrl(gatewayAddress, sessionId);
if (!url) {
throw new errors_js_1.SSHUnavailableError("gateway address not configured");
}
const headers = {};
const h = new Headers();
(0, auth_js_1.setAuthHeaders)(h, authToken);
h.forEach((value, key) => {
headers[key] = value;
});
return new Promise((resolve, reject) => {
const ws = new ws_1.default(url, {
headers,
ca: options?.rootCertificates,
});
ws.on("open", () => {
resolve(new SSHConnection(ws));
});
ws.on("unexpected-response", (_req, res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk.toString();
});
res.on("end", () => {
const reason = body.trim() || res.statusMessage || "unknown";
switch (res.statusCode) {
case 401:
reject(new errors_js_1.UnauthorizedError(reason));
break;
case 403:
reject(new errors_js_1.PermissionDeniedError(reason));
break;
case 404:
reject(new errors_js_1.SessionNotFoundError(reason));
break;
case 409:
reject(new errors_js_1.InvalidStateError(reason));
break;
default:
reject(new errors_js_1.SSHUnavailableError(reason));
}
});
});
ws.on("error", (err) => {
reject(new errors_js_1.SSHUnavailableError(err.message, { cause: err }));
});
});
}
//# sourceMappingURL=ssh.js.map