fix: fix problems with event handler arguments

This commit is contained in:
James Murdza 2024-10-24 23:13:01 -06:00
parent 1479d25d49
commit 5ba6bdba15
2 changed files with 13 additions and 11 deletions

View File

@ -1,3 +1,4 @@
import { Socket } from 'socket.io'
import { AIWorker } from "./AIWorker" import { AIWorker } from "./AIWorker"
import { CONTAINER_TIMEOUT } from "./constants" import { CONTAINER_TIMEOUT } from "./constants"
import { DokkuClient } from "./DokkuClient" import { DokkuClient } from "./DokkuClient"
@ -14,6 +15,7 @@ export interface HandlerContext {
dokkuClient: DokkuClient | null; dokkuClient: DokkuClient | null;
gitClient: SecureGitClient | null; gitClient: SecureGitClient | null;
lockManager: LockManager lockManager: LockManager
socket: Socket
} }
// Extract port number from a string // Extract port number from a string
@ -50,7 +52,7 @@ export const handleMoveFile: SocketHandler = ({ fileId, folderId }: any, context
} }
// Handle listing apps // 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") if (!context.dokkuClient) throw Error("Failed to retrieve apps list: No Dokku client")
return { success: true, apps: await context.dokkuClient.listApps() } 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 // Handle creating a file
export const handleCreateFile: SocketHandler = ({ name }: any, context: HandlerContext) => { export const handleCreateFile: SocketHandler = async ({ name }: any, context: HandlerContext) => {
return context.fileManager.createFile(name) return { "success": await context.fileManager.createFile(name) }
} }
// Handle creating a folder // Handle creating a folder
export const handleCreateFolder: SocketHandler = ({ name }: any, context: HandlerContext) => { export const handleCreateFolder: SocketHandler = async ({ name }: any, context: HandlerContext) => {
return context.fileManager.createFolder(name) return { "success": await context.fileManager.createFolder(name) }
} }
// Handle renaming a file // Handle renaming a file
@ -92,13 +94,13 @@ export const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: Ha
} }
// Handle creating a terminal session // Handle creating a terminal session
export const handleCreateTerminal: SocketHandler = async ({ id, socket, data }: any, context: HandlerContext) => { export const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any, context: HandlerContext) => {
await context.lockManager.acquireLock(data.sandboxId, async () => { await context.lockManager.acquireLock(sandboxId, async () => {
await context.terminalManager.createTerminal(id, (responseString: string) => { await context.terminalManager.createTerminal(id, (responseString: string) => {
socket.emit("terminalResponse", { id, data: responseString }) context.socket.emit("terminalResponse", { id, data: responseString })
const port = extractPortNumber(responseString) const port = extractPortNumber(responseString)
if (port) { if (port) {
socket.emit( context.socket.emit(
"previewURL", "previewURL",
"https://" + context.sandboxManager.getHost(port) "https://" + context.sandboxManager.getHost(port)
) )

View File

@ -178,6 +178,7 @@ io.on("connection", async (socket) => {
gitClient, gitClient,
lockManager, lockManager,
sandboxManager: containers[data.sandboxId], sandboxManager: containers[data.sandboxId],
socket
} }
// Helper function to handle socket events with error handling and optional rate limiting // Helper function to handle socket events with error handling and optional rate limiting
@ -192,7 +193,7 @@ io.on("connection", async (socket) => {
if (rateLimiter) { if (rateLimiter) {
await rateLimiter.consume(data.userId, 1); // Adjust as needed for the specific rate limiter 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); callback?.(response);
} catch (e: any) { } catch (e: any) {
console.error(`Error processing event "${event}":`, e); console.error(`Error processing event "${event}":`, e);
@ -220,7 +221,6 @@ io.on("connection", async (socket) => {
handleSocketEvent("closeTerminal", handleCloseTerminal); handleSocketEvent("closeTerminal", handleCloseTerminal);
handleSocketEvent("generateCode", handleGenerateCode); handleSocketEvent("generateCode", handleGenerateCode);
socket.on("disconnect", async () => { socket.on("disconnect", async () => {
try { try {
if (data.isOwner) { if (data.isOwner) {