refactor: export all event handlers as one object
This commit is contained in:
parent
09ab81f5bd
commit
fcc7a836a6
@ -34,39 +34,39 @@ function extractPortNumber(inputString: string): number | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle heartbeat from a socket connection
|
// Handle heartbeat from a socket connection
|
||||||
export const handleHeartbeat: SocketHandler = (_: any, context: HandlerContext) => {
|
const handleHeartbeat: SocketHandler = (_: any, context: HandlerContext) => {
|
||||||
context.sandboxManager.setTimeout(CONTAINER_TIMEOUT)
|
context.sandboxManager.setTimeout(CONTAINER_TIMEOUT)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle getting a file
|
// Handle getting a file
|
||||||
export const handleGetFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => {
|
const handleGetFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => {
|
||||||
return context.fileManager.getFile(fileId)
|
return context.fileManager.getFile(fileId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle getting a folder
|
// Handle getting a folder
|
||||||
export const handleGetFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
const handleGetFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
||||||
return context.fileManager.getFolder(folderId)
|
return context.fileManager.getFolder(folderId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle saving a file
|
// 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);
|
await saveFileRL.consume(userId, 1);
|
||||||
return context.fileManager.saveFile(fileId, body)
|
return context.fileManager.saveFile(fileId, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle moving a file
|
// 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)
|
return context.fileManager.moveFile(fileId, folderId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle listing apps
|
// 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")
|
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() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle deploying code
|
// 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")
|
if (!context.gitClient) throw Error("Failed to retrieve apps list: No git client")
|
||||||
const fixedFilePaths = context.fileManager.sandboxFiles.fileData.map((file) => ({
|
const fixedFilePaths = context.fileManager.sandboxFiles.fileData.map((file) => ({
|
||||||
...file,
|
...file,
|
||||||
@ -77,36 +77,36 @@ export const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: H
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle creating a file
|
// 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);
|
await createFileRL.consume(userId, 1);
|
||||||
return { "success": await context.fileManager.createFile(name) }
|
return { "success": await context.fileManager.createFile(name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle creating a folder
|
// 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);
|
await createFolderRL.consume(userId, 1);
|
||||||
return { "success": await context.fileManager.createFolder(name) }
|
return { "success": await context.fileManager.createFolder(name) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle renaming a file
|
// 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)
|
await renameFileRL.consume(userId, 1)
|
||||||
return context.fileManager.renameFile(fileId, newName)
|
return context.fileManager.renameFile(fileId, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle deleting a file
|
// 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)
|
await deleteFileRL.consume(userId, 1)
|
||||||
return context.fileManager.deleteFile(fileId)
|
return context.fileManager.deleteFile(fileId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle deleting a folder
|
// Handle deleting a folder
|
||||||
export const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
||||||
return context.fileManager.deleteFolder(folderId)
|
return context.fileManager.deleteFolder(folderId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle creating a terminal session
|
// 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.lockManager.acquireLock(sandboxId, async () => {
|
||||||
await context.terminalManager.createTerminal(id, (responseString: string) => {
|
await context.terminalManager.createTerminal(id, (responseString: string) => {
|
||||||
context.socket.emit("terminalResponse", { id, data: responseString })
|
context.socket.emit("terminalResponse", { id, data: responseString })
|
||||||
@ -122,24 +122,44 @@ export const handleCreateTerminal: SocketHandler = async ({ id, sandboxId }: any
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle resizing a terminal
|
// Handle resizing a terminal
|
||||||
export const handleResizeTerminal: SocketHandler = ({ dimensions }: any, context: HandlerContext) => {
|
const handleResizeTerminal: SocketHandler = ({ dimensions }: any, context: HandlerContext) => {
|
||||||
context.terminalManager.resizeTerminal(dimensions)
|
context.terminalManager.resizeTerminal(dimensions)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle sending data to a terminal
|
// 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)
|
return context.terminalManager.sendTerminalData(id, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle closing a terminal
|
// Handle closing a terminal
|
||||||
export const handleCloseTerminal: SocketHandler = ({ id }: any, context: HandlerContext) => {
|
const handleCloseTerminal: SocketHandler = ({ id }: any, context: HandlerContext) => {
|
||||||
return context.terminalManager.closeTerminal(id)
|
return context.terminalManager.closeTerminal(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle generating code
|
// 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)
|
return context.aiWorker.generateCode(userId, fileName, code, line, instructions)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a type for SocketHandler functions
|
// Define a type for SocketHandler functions
|
||||||
type SocketHandler<T = Record<string, any>> = (args: T, context: HandlerContext) => any;
|
type SocketHandler<T = Record<string, any>> = (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,
|
||||||
|
};
|
||||||
|
@ -11,7 +11,7 @@ import { DokkuClient } from "./DokkuClient"
|
|||||||
import { FileManager, SandboxFiles } from "./FileManager"
|
import { FileManager, SandboxFiles } from "./FileManager"
|
||||||
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
|
||||||
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 { TerminalManager } from "./TerminalManager"
|
||||||
import { LockManager } from "./utils"
|
import { LockManager } from "./utils"
|
||||||
|
|
||||||
@ -191,24 +191,9 @@ io.on("connection", async (socket) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Register socket events with optional rate limiters
|
Object.entries(eventHandlers).forEach(([event, handler]) => {
|
||||||
handleSocketEvent("heartbeat", handleHeartbeat);
|
handleSocketEvent(event, handler);
|
||||||
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);
|
|
||||||
|
|
||||||
socket.on("disconnect", async () => {
|
socket.on("disconnect", async () => {
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user