From 836dd51ccc0856e5def1430932234566aafbdc6a Mon Sep 17 00:00:00 2001 From: James Murdza Date: Fri, 25 Oct 2024 07:36:43 -0600 Subject: [PATCH] refactor: improve naming --- backend/server/src/SandboxManager.ts | 12 ++++++------ backend/server/src/index.ts | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/backend/server/src/SandboxManager.ts b/backend/server/src/SandboxManager.ts index 960d3c3..dd86434 100644 --- a/backend/server/src/SandboxManager.ts +++ b/backend/server/src/SandboxManager.ts @@ -1,4 +1,4 @@ -import { Sandbox } from "e2b" +import { Sandbox as E2BSandbox } from "e2b" import { Socket } from 'socket.io' import { AIWorker } from "./AIWorker" import { CONTAINER_TIMEOUT } from "./constants" @@ -29,17 +29,17 @@ function extractPortNumber(inputString: string): number | null { return match ? parseInt(match[1]) : null } -type SandboxManagerContext = { +type ServerContext = { aiWorker: AIWorker; dokkuClient: DokkuClient | null; gitClient: SecureGitClient | null; socket: Socket; }; -export class SandboxManager { +export class Sandbox { fileManager: FileManager | null; terminalManager: TerminalManager | null; - container: Sandbox | null; + container: E2BSandbox | null; dokkuClient: DokkuClient | null; gitClient: SecureGitClient | null; aiWorker: AIWorker; @@ -47,7 +47,7 @@ export class SandboxManager { sandboxId: string; userId: string; - constructor(sandboxId: string, userId: string, { aiWorker, dokkuClient, gitClient, socket }: SandboxManagerContext) { + constructor(sandboxId: string, userId: string, { aiWorker, dokkuClient, gitClient, socket }: ServerContext) { this.fileManager = null; this.terminalManager = null; this.container = null; @@ -66,7 +66,7 @@ export class SandboxManager { console.log(`Found existing container ${this.sandboxId}`) } else { console.log("Creating container", this.sandboxId) - this.container = await Sandbox.create({ + this.container = await E2BSandbox.create({ timeoutMs: CONTAINER_TIMEOUT, }) } diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index 025512a..56447b2 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -7,8 +7,8 @@ import { Server } from "socket.io" import { AIWorker } from "./AIWorker" import { DokkuClient } from "./DokkuClient" -import { OwnerConnectionManager } from "./OwnerConnectionManager" -import { SandboxManager } from "./SandboxManager" +import { OwnerConnectionManager as ConnectionManager } from "./OwnerConnectionManager" +import { Sandbox } from "./SandboxManager" import { SecureGitClient } from "./SecureGitClient" import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware @@ -31,8 +31,8 @@ process.on("unhandledRejection", (reason, promise) => { }) // Initialize containers and managers -const connectionManager = new OwnerConnectionManager() -const sandboxManagers: Record = {} +const connections = new ConnectionManager() +const sandboxes: Record = {} // Load environment variables dotenv.config() @@ -99,9 +99,9 @@ io.on("connection", async (socket) => { // Disable access unless the sandbox owner is connected if (data.isOwner) { - connectionManager.ownerConnected(data.sandboxId) + connections.ownerConnected(data.sandboxId) } else { - if (!connectionManager.ownerIsConnected(data.sandboxId)) { + if (!connections.ownerIsConnected(data.sandboxId)) { socket.emit("disableAccess", "The sandbox owner is not connected.") return } @@ -109,7 +109,7 @@ io.on("connection", async (socket) => { try { // Create or retrieve the sandbox manager for the given sandbox ID - const sandboxManager = sandboxManagers[data.sandboxId] ?? new SandboxManager( + const sandboxManager = sandboxes[data.sandboxId] ?? new Sandbox( data.sandboxId, data.userId, { aiWorker, dokkuClient, gitClient, socket } @@ -133,12 +133,12 @@ io.on("connection", async (socket) => { socket.on("disconnect", async () => { try { if (data.isOwner) { - connectionManager.ownerDisconnected(data.sandboxId) + connections.ownerDisconnected(data.sandboxId) } await sandboxManager.disconnect() - if (data.isOwner && !connectionManager.ownerIsConnected(data.sandboxId)) { + if (data.isOwner && !connections.ownerIsConnected(data.sandboxId)) { socket.broadcast.emit( "disableAccess", "The sandbox owner has disconnected."