refactor: improve names of server variables

This commit is contained in:
James Murdza 2024-10-25 07:36:43 -06:00
parent 935c314357
commit 3ad7e5d9bc
2 changed files with 15 additions and 15 deletions

View File

@ -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,
})
}

View File

@ -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<string, SandboxManager> = {}
const connections = new ConnectionManager()
const sandboxes: Record<string, Sandbox> = {}
// 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."