From b01934bd20fdfcf6202d712879607b72ad7f4b96 Mon Sep 17 00:00:00 2001 From: James Murdza Date: Thu, 5 Sep 2024 14:23:14 -0700 Subject: [PATCH] fix: change to non-streaming input method for E2B terminals --- backend/server/package-lock.json | 8 ++++---- backend/server/package.json | 2 +- backend/server/src/Terminal.ts | 23 +++++++---------------- backend/server/src/index.ts | 2 +- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/backend/server/package-lock.json b/backend/server/package-lock.json index f65063c..dd284cb 100644 --- a/backend/server/package-lock.json +++ b/backend/server/package-lock.json @@ -12,7 +12,7 @@ "concurrently": "^8.2.2", "cors": "^2.8.5", "dotenv": "^16.4.5", - "e2b": "^0.16.2-beta.46", + "e2b": "^0.16.2-beta.47", "express": "^4.19.2", "rate-limiter-flexible": "^5.0.3", "simple-git": "^3.25.0", @@ -765,9 +765,9 @@ } }, "node_modules/e2b": { - "version": "0.16.2-beta.46", - "resolved": "https://registry.npmjs.org/e2b/-/e2b-0.16.2-beta.46.tgz", - "integrity": "sha512-Hfh1dRmTzroFFvN5GNMj5x5xfZLN4iiMwdkXcUEky6yq7AkFsD/1ljKoddL/BzFYtSvKJpJ9iKXwsVI3Z28stA==", + "version": "0.16.2-beta.47", + "resolved": "https://registry.npmjs.org/e2b/-/e2b-0.16.2-beta.47.tgz", + "integrity": "sha512-tMPDYLMD+8+JyLPrsWft3NHBhK5YKOFOXzKMwpOKR5KvXOkd1silkArDwplmBUzN/eG/uRzWdtHZs9mHUQ5b9g==", "dependencies": { "@bufbuild/protobuf": "^1.10.0", "@connectrpc/connect": "^1.4.0", diff --git a/backend/server/package.json b/backend/server/package.json index 6117480..40c9c18 100644 --- a/backend/server/package.json +++ b/backend/server/package.json @@ -14,7 +14,7 @@ "concurrently": "^8.2.2", "cors": "^2.8.5", "dotenv": "^16.4.5", - "e2b": "^0.16.2-beta.46", + "e2b": "^0.16.2-beta.47", "express": "^4.19.2", "rate-limiter-flexible": "^5.0.3", "simple-git": "^3.25.0", diff --git a/backend/server/src/Terminal.ts b/backend/server/src/Terminal.ts index 14b7d2f..018ad9b 100644 --- a/backend/server/src/Terminal.ts +++ b/backend/server/src/Terminal.ts @@ -3,9 +3,6 @@ import { Sandbox, ProcessHandle } from "e2b"; // Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment export class Terminal { private pty: ProcessHandle | undefined; // Holds the PTY process handle - private input: - | { stop: () => void; sendData: (data: Uint8Array) => void } - | undefined; // Holds input stream controls private sandbox: Sandbox; // Reference to the sandbox environment // Constructor initializes the Terminal with a sandbox @@ -29,19 +26,18 @@ export class Terminal { cols, timeout: 0, onData: (data: Uint8Array) => { - onData(data.toString()); // Convert received data to string and pass to handler + onData(new TextDecoder().decode(data)); // Convert received data to string and pass to handler }, }); - // Set up input stream for the PTY - this.input = await this.sandbox.pty.streamInput(this.pty.pid); } // Send data to the terminal - sendData(data: string): void { - if (this.input) { - this.input.sendData(Buffer.from(data)); // Convert string to Buffer and send + async sendData(data: string) { + if (this.pty) { + await this.sandbox.pty.sendInput(this.pty.pid, new TextEncoder().encode(data)); + await this.pty.wait(); } else { - console.log("Cannot send data because input is not initialized."); + console.log("Cannot send data because pty is not initialized."); } } @@ -50,7 +46,7 @@ export class Terminal { if (this.pty) { await this.sandbox.pty.resize(this.pty.pid, size); } else { - console.log("Cannot send data because pty is not initialized."); + console.log("Cannot resize terminal because pty is not initialized."); } } @@ -61,11 +57,6 @@ export class Terminal { } else { console.log("Cannot kill pty because it is not initialized."); } - if (this.input) { - this.input.stop(); - } else { - console.log("Cannot stop input because it is not initialized."); - } } } diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index bb83465..548d23e 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -526,7 +526,7 @@ io.on("connection", async (socket) => { rows: 20, //onExit: () => console.log("Terminal exited", id), }); - terminals[id].sendData( + await terminals[id].sendData( `cd "${path.join(dirName, "projects", data.sandboxId)}"\rexport PS1='user> '\rclear\r` ); console.log("Created terminal", id);