From 3bc555ca470c1c170ce2b581bc8f68b0980ee7d3 Mon Sep 17 00:00:00 2001 From: James Murdza Date: Thu, 24 Oct 2024 23:13:01 -0600 Subject: [PATCH] refactor: fix handler arguments --- backend/server/src/SocketHandlers.ts | 20 +++++++++++--------- backend/server/src/index.ts | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/backend/server/src/SocketHandlers.ts b/backend/server/src/SocketHandlers.ts index 64dc05a..9169dc8 100644 --- a/backend/server/src/SocketHandlers.ts +++ b/backend/server/src/SocketHandlers.ts @@ -1,3 +1,4 @@ +import { Socket } from 'socket.io' import { AIWorker } from "./AIWorker" import { CONTAINER_TIMEOUT } from "./constants" import { DokkuClient } from "./DokkuClient" @@ -14,6 +15,7 @@ export interface HandlerContext { dokkuClient: DokkuClient | null; gitClient: SecureGitClient | null; lockManager: LockManager + socket: Socket } // Extract port number from a string @@ -50,7 +52,7 @@ export const handleMoveFile: SocketHandler = ({ fileId, folderId }: any, context } // Handle listing apps -export const handleListApps: SocketHandler = async ({ }: any, context: HandlerContext) => { +export 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() } } @@ -67,13 +69,13 @@ export const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: H } // Handle creating a file -export const handleCreateFile: SocketHandler = ({ name }: any, context: HandlerContext) => { - return context.fileManager.createFile(name) +export const handleCreateFile: SocketHandler = async ({ name }: any, context: HandlerContext) => { + return { "success": await context.fileManager.createFile(name) } } // Handle creating a folder -export const handleCreateFolder: SocketHandler = ({ name }: any, context: HandlerContext) => { - return context.fileManager.createFolder(name) +export const handleCreateFolder: SocketHandler = async ({ name }: any, context: HandlerContext) => { + return { "success": await context.fileManager.createFolder(name) } } // Handle renaming a file @@ -92,13 +94,13 @@ export const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: Ha } // Handle creating a terminal session -export const handleCreateTerminal: SocketHandler = async ({ id, socket, data }: any, context: HandlerContext) => { - await context.lockManager.acquireLock(data.sandboxId, async () => { +export const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any, context: HandlerContext) => { + await context.lockManager.acquireLock(sandboxId, async () => { await context.terminalManager.createTerminal(id, (responseString: string) => { - socket.emit("terminalResponse", { id, data: responseString }) + context.socket.emit("terminalResponse", { id, data: responseString }) const port = extractPortNumber(responseString) if (port) { - socket.emit( + context.socket.emit( "previewURL", "https://" + context.sandboxManager.getHost(port) ) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index a496c96..4850077 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -178,6 +178,7 @@ io.on("connection", async (socket) => { gitClient, lockManager, sandboxManager: containers[data.sandboxId], + socket } // Helper function to handle socket events with error handling and optional rate limiting @@ -192,7 +193,7 @@ io.on("connection", async (socket) => { if (rateLimiter) { await rateLimiter.consume(data.userId, 1); // Adjust as needed for the specific rate limiter } - const response = await handler(options, handlerContext) + const response = await handler({ ...options, ...data }, handlerContext) callback?.(response); } catch (e: any) { console.error(`Error processing event "${event}":`, e); @@ -220,7 +221,6 @@ io.on("connection", async (socket) => { handleSocketEvent("closeTerminal", handleCloseTerminal); handleSocketEvent("generateCode", handleGenerateCode); - socket.on("disconnect", async () => { try { if (data.isOwner) {