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 { Socket } from 'socket.io'
import { AIWorker } from "./AIWorker" import { AIWorker } from "./AIWorker"
import { CONTAINER_TIMEOUT } from "./constants" import { CONTAINER_TIMEOUT } from "./constants"
@ -29,17 +29,17 @@ function extractPortNumber(inputString: string): number | null {
return match ? parseInt(match[1]) : null return match ? parseInt(match[1]) : null
} }
type SandboxManagerContext = { type ServerContext = {
aiWorker: AIWorker; aiWorker: AIWorker;
dokkuClient: DokkuClient | null; dokkuClient: DokkuClient | null;
gitClient: SecureGitClient | null; gitClient: SecureGitClient | null;
socket: Socket; socket: Socket;
}; };
export class SandboxManager { export class Sandbox {
fileManager: FileManager | null; fileManager: FileManager | null;
terminalManager: TerminalManager | null; terminalManager: TerminalManager | null;
container: Sandbox | null; container: E2BSandbox | null;
dokkuClient: DokkuClient | null; dokkuClient: DokkuClient | null;
gitClient: SecureGitClient | null; gitClient: SecureGitClient | null;
aiWorker: AIWorker; aiWorker: AIWorker;
@ -47,7 +47,7 @@ export class SandboxManager {
sandboxId: string; sandboxId: string;
userId: 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.fileManager = null;
this.terminalManager = null; this.terminalManager = null;
this.container = null; this.container = null;
@ -66,7 +66,7 @@ export class SandboxManager {
console.log(`Found existing container ${this.sandboxId}`) console.log(`Found existing container ${this.sandboxId}`)
} else { } else {
console.log("Creating container", this.sandboxId) console.log("Creating container", this.sandboxId)
this.container = await Sandbox.create({ this.container = await E2BSandbox.create({
timeoutMs: CONTAINER_TIMEOUT, timeoutMs: CONTAINER_TIMEOUT,
}) })
} }

View File

@ -7,8 +7,8 @@ import { Server } from "socket.io"
import { AIWorker } from "./AIWorker" import { AIWorker } from "./AIWorker"
import { DokkuClient } from "./DokkuClient" import { DokkuClient } from "./DokkuClient"
import { OwnerConnectionManager } from "./OwnerConnectionManager" import { OwnerConnectionManager as ConnectionManager } from "./OwnerConnectionManager"
import { SandboxManager } from "./SandboxManager" import { Sandbox } from "./SandboxManager"
import { SecureGitClient } from "./SecureGitClient" import { SecureGitClient } from "./SecureGitClient"
import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware
@ -31,8 +31,8 @@ process.on("unhandledRejection", (reason, promise) => {
}) })
// Initialize containers and managers // Initialize containers and managers
const connectionManager = new OwnerConnectionManager() const connections = new ConnectionManager()
const sandboxManagers: Record<string, SandboxManager> = {} const sandboxes: Record<string, Sandbox> = {}
// Load environment variables // Load environment variables
dotenv.config() dotenv.config()
@ -99,9 +99,9 @@ io.on("connection", async (socket) => {
// Disable access unless the sandbox owner is connected // Disable access unless the sandbox owner is connected
if (data.isOwner) { if (data.isOwner) {
connectionManager.ownerConnected(data.sandboxId) connections.ownerConnected(data.sandboxId)
} else { } else {
if (!connectionManager.ownerIsConnected(data.sandboxId)) { if (!connections.ownerIsConnected(data.sandboxId)) {
socket.emit("disableAccess", "The sandbox owner is not connected.") socket.emit("disableAccess", "The sandbox owner is not connected.")
return return
} }
@ -109,7 +109,7 @@ io.on("connection", async (socket) => {
try { try {
// Create or retrieve the sandbox manager for the given sandbox ID // 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.sandboxId,
data.userId, data.userId,
{ aiWorker, dokkuClient, gitClient, socket } { aiWorker, dokkuClient, gitClient, socket }
@ -133,12 +133,12 @@ io.on("connection", async (socket) => {
socket.on("disconnect", async () => { socket.on("disconnect", async () => {
try { try {
if (data.isOwner) { if (data.isOwner) {
connectionManager.ownerDisconnected(data.sandboxId) connections.ownerDisconnected(data.sandboxId)
} }
await sandboxManager.disconnect() await sandboxManager.disconnect()
if (data.isOwner && !connectionManager.ownerIsConnected(data.sandboxId)) { if (data.isOwner && !connections.ownerIsConnected(data.sandboxId)) {
socket.broadcast.emit( socket.broadcast.emit(
"disableAccess", "disableAccess",
"The sandbox owner has disconnected." "The sandbox owner has disconnected."