diff --git a/backend/server/src/SocketHandlers.ts b/backend/server/src/SocketHandlers.ts index 5b2f663..4627d5f 100644 --- a/backend/server/src/SocketHandlers.ts +++ b/backend/server/src/SocketHandlers.ts @@ -34,39 +34,39 @@ function extractPortNumber(inputString: string): number | null { } // Handle heartbeat from a socket connection -export const handleHeartbeat: SocketHandler = (_: any, context: HandlerContext) => { +const handleHeartbeat: SocketHandler = (_: any, context: HandlerContext) => { context.sandboxManager.setTimeout(CONTAINER_TIMEOUT) } // Handle getting a file -export const handleGetFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => { +const handleGetFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => { return context.fileManager.getFile(fileId) } // Handle getting a folder -export const handleGetFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => { +const handleGetFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => { return context.fileManager.getFolder(folderId) } // Handle saving a file -export const handleSaveFile: SocketHandler = async ({ fileId, body, userId }: any, context: HandlerContext) => { +const handleSaveFile: SocketHandler = async ({ fileId, body, userId }: any, context: HandlerContext) => { await saveFileRL.consume(userId, 1); return context.fileManager.saveFile(fileId, body) } // Handle moving a file -export const handleMoveFile: SocketHandler = ({ fileId, folderId }: any, context: HandlerContext) => { +const handleMoveFile: SocketHandler = ({ fileId, folderId }: any, context: HandlerContext) => { return context.fileManager.moveFile(fileId, folderId) } // Handle listing apps -export const handleListApps: SocketHandler = async (_: any, context: HandlerContext) => { +const handleListApps: SocketHandler = async (_: any, context: HandlerContext) => { if (!context.dokkuClient) throw Error("Failed to retrieve apps list: No Dokku client") return { success: true, apps: await context.dokkuClient.listApps() } } // Handle deploying code -export const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: HandlerContext) => { +const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: HandlerContext) => { if (!context.gitClient) throw Error("Failed to retrieve apps list: No git client") const fixedFilePaths = context.fileManager.sandboxFiles.fileData.map((file) => ({ ...file, @@ -77,36 +77,36 @@ export const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: H } // Handle creating a file -export const handleCreateFile: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => { +const handleCreateFile: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => { await createFileRL.consume(userId, 1); return { "success": await context.fileManager.createFile(name) } } // Handle creating a folder -export const handleCreateFolder: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => { +const handleCreateFolder: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => { await createFolderRL.consume(userId, 1); return { "success": await context.fileManager.createFolder(name) } } // Handle renaming a file -export const handleRenameFile: SocketHandler = async ({ fileId, newName, userId }: any, context: HandlerContext) => { +const handleRenameFile: SocketHandler = async ({ fileId, newName, userId }: any, context: HandlerContext) => { await renameFileRL.consume(userId, 1) return context.fileManager.renameFile(fileId, newName) } // Handle deleting a file -export const handleDeleteFile: SocketHandler = async ({ fileId, userId }: any, context: HandlerContext) => { +const handleDeleteFile: SocketHandler = async ({ fileId, userId }: any, context: HandlerContext) => { await deleteFileRL.consume(userId, 1) return context.fileManager.deleteFile(fileId) } // Handle deleting a folder -export const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => { +const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => { return context.fileManager.deleteFolder(folderId) } // Handle creating a terminal session -export const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any, context: HandlerContext) => { +const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any, context: HandlerContext) => { await context.lockManager.acquireLock(sandboxId, async () => { await context.terminalManager.createTerminal(id, (responseString: string) => { context.socket.emit("terminalResponse", { id, data: responseString }) @@ -122,24 +122,44 @@ export const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any } // Handle resizing a terminal -export const handleResizeTerminal: SocketHandler = ({ dimensions }: any, context: HandlerContext) => { +const handleResizeTerminal: SocketHandler = ({ dimensions }: any, context: HandlerContext) => { context.terminalManager.resizeTerminal(dimensions) } // Handle sending data to a terminal -export const handleTerminalData: SocketHandler = ({ id, data }: any, context: HandlerContext) => { +const handleTerminalData: SocketHandler = ({ id, data }: any, context: HandlerContext) => { return context.terminalManager.sendTerminalData(id, data) } // Handle closing a terminal -export const handleCloseTerminal: SocketHandler = ({ id }: any, context: HandlerContext) => { +const handleCloseTerminal: SocketHandler = ({ id }: any, context: HandlerContext) => { return context.terminalManager.closeTerminal(id) } // Handle generating code -export const handleGenerateCode: SocketHandler = ({ userId, fileName, code, line, instructions }: any, context: HandlerContext) => { +const handleGenerateCode: SocketHandler = ({ userId, fileName, code, line, instructions }: any, context: HandlerContext) => { return context.aiWorker.generateCode(userId, fileName, code, line, instructions) } // Define a type for SocketHandler functions type SocketHandler> = (args: T, context: HandlerContext) => any; + +export const eventHandlers = { + "heartbeat": handleHeartbeat, + "getFile": handleGetFile, + "getFolder": handleGetFolder, + "saveFile": handleSaveFile, + "moveFile": handleMoveFile, + "list": handleListApps, + "deploy": handleDeploy, + "createFile": handleCreateFile, + "createFolder": handleCreateFolder, + "renameFile": handleRenameFile, + "deleteFile": handleDeleteFile, + "deleteFolder": handleDeleteFolder, + "createTerminal": handleCreateTerminal, + "resizeTerminal": handleResizeTerminal, + "terminalData": handleTerminalData, + "closeTerminal": handleCloseTerminal, + "generateCode": handleGenerateCode, +}; diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index bda426d..f944684 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -11,7 +11,7 @@ import { DokkuClient } from "./DokkuClient" import { FileManager, SandboxFiles } from "./FileManager" import { SecureGitClient } from "./SecureGitClient" import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware -import { handleCloseTerminal, handleCreateFile, handleCreateFolder, handleCreateTerminal, handleDeleteFile, handleDeleteFolder, handleDeploy, handleGenerateCode, handleGetFile, handleGetFolder, handleHeartbeat, handleListApps, handleMoveFile, HandlerContext, handleRenameFile, handleResizeTerminal, handleSaveFile, handleTerminalData } from "./SocketHandlers" +import { eventHandlers, HandlerContext } from "./SocketHandlers" import { TerminalManager } from "./TerminalManager" import { LockManager } from "./utils" @@ -191,24 +191,9 @@ io.on("connection", async (socket) => { }); }; - // Register socket events with optional rate limiters - handleSocketEvent("heartbeat", handleHeartbeat); - handleSocketEvent("getFile", handleGetFile); - handleSocketEvent("getFolder", handleGetFolder); - handleSocketEvent("saveFile", handleSaveFile); - handleSocketEvent("moveFile", handleMoveFile); - handleSocketEvent("list", handleListApps); - handleSocketEvent("deploy", handleDeploy); - handleSocketEvent("createFile", handleCreateFile); - handleSocketEvent("createFolder", handleCreateFolder); - handleSocketEvent("renameFile", handleRenameFile); - handleSocketEvent("deleteFile", handleDeleteFile); - handleSocketEvent("deleteFolder", handleDeleteFolder); - handleSocketEvent("createTerminal", handleCreateTerminal); - handleSocketEvent("resizeTerminal", handleResizeTerminal); - handleSocketEvent("terminalData", handleTerminalData); - handleSocketEvent("closeTerminal", handleCloseTerminal); - handleSocketEvent("generateCode", handleGenerateCode); + Object.entries(eventHandlers).forEach(([event, handler]) => { + handleSocketEvent(event, handler); + }); socket.on("disconnect", async () => { try {