refactor: reuse try...catch and rate limiting code across handlers
This commit is contained in:
parent
0fe652d873
commit
6f8bebe7dd
@ -25,38 +25,38 @@ function extractPortNumber(inputString: string): number | null {
|
||||
}
|
||||
|
||||
// Handle heartbeat from a socket connection
|
||||
export function handleHeartbeat({ data }: { data: any }, context: HandlerContext) {
|
||||
export const handleHeartbeat: SocketHandler = (_: any, context: HandlerContext) => {
|
||||
context.sandboxManager.setTimeout(CONTAINER_TIMEOUT)
|
||||
}
|
||||
|
||||
// Handle getting a file
|
||||
export function handleGetFile({ fileId }: { fileId: string }, context: HandlerContext) {
|
||||
export const handleGetFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => {
|
||||
return context.fileManager.getFile(fileId)
|
||||
}
|
||||
|
||||
// Handle getting a folder
|
||||
export function handleGetFolder({ folderId }: { folderId: string }, context: HandlerContext) {
|
||||
export const handleGetFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
||||
return context.fileManager.getFolder(folderId)
|
||||
}
|
||||
|
||||
// Handle saving a file
|
||||
export function handleSaveFile({ fileId, body }: { fileId: string, body: string }, context: HandlerContext) {
|
||||
export const handleSaveFile: SocketHandler = ({ fileId, body }: any, context: HandlerContext) => {
|
||||
return context.fileManager.saveFile(fileId, body)
|
||||
}
|
||||
|
||||
// Handle moving a file
|
||||
export function handleMoveFile({ fileId, folderId }: { fileId: string, folderId: string }, context: HandlerContext) {
|
||||
export const handleMoveFile: SocketHandler = ({ fileId, folderId }: any, context: HandlerContext) => {
|
||||
return context.fileManager.moveFile(fileId, folderId)
|
||||
}
|
||||
|
||||
// Handle listing apps
|
||||
export async function handleListApps({ }, 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() }
|
||||
}
|
||||
|
||||
// Handle deploying code
|
||||
export async function handleDeploy({ sandboxId }: { sandboxId: string }, context: HandlerContext) {
|
||||
export 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,
|
||||
@ -67,32 +67,32 @@ export async function handleDeploy({ sandboxId }: { sandboxId: string }, context
|
||||
}
|
||||
|
||||
// Handle creating a file
|
||||
export function handleCreateFile({ name }: { name: string }, context: HandlerContext) {
|
||||
export const handleCreateFile: SocketHandler = ({ name }: any, context: HandlerContext) => {
|
||||
return context.fileManager.createFile(name)
|
||||
}
|
||||
|
||||
// Handle creating a folder
|
||||
export function handleCreateFolder({ name }: { name: string }, context: HandlerContext) {
|
||||
export const handleCreateFolder: SocketHandler = ({ name }: any, context: HandlerContext) => {
|
||||
return context.fileManager.createFolder(name)
|
||||
}
|
||||
|
||||
// Handle renaming a file
|
||||
export function handleRenameFile({ fileId, newName }: { fileId: string, newName: string }, context: HandlerContext) {
|
||||
export const handleRenameFile: SocketHandler = ({ fileId, newName }: any, context: HandlerContext) => {
|
||||
return context.fileManager.renameFile(fileId, newName)
|
||||
}
|
||||
|
||||
// Handle deleting a file
|
||||
export function handleDeleteFile({ fileId }: { fileId: string }, context: HandlerContext) {
|
||||
export const handleDeleteFile: SocketHandler = ({ fileId }: any, context: HandlerContext) => {
|
||||
return context.fileManager.deleteFile(fileId)
|
||||
}
|
||||
|
||||
// Handle deleting a folder
|
||||
export function handleDeleteFolder({ folderId }: { folderId: string }, context: HandlerContext) {
|
||||
export const handleDeleteFolder: SocketHandler = ({ folderId }: any, context: HandlerContext) => {
|
||||
return context.fileManager.deleteFolder(folderId)
|
||||
}
|
||||
|
||||
// Handle creating a terminal session
|
||||
export async function handleCreateTerminal({ id, socket, data }: { id: string, socket: any, data: any }, context: HandlerContext) {
|
||||
export const handleCreateTerminal: SocketHandler = async ({ id, socket, data }: any, context: HandlerContext) => {
|
||||
await context.lockManager.acquireLock(data.sandboxId, async () => {
|
||||
await context.terminalManager.createTerminal(id, (responseString: string) => {
|
||||
socket.emit("terminalResponse", { id, data: responseString })
|
||||
@ -108,21 +108,24 @@ export async function handleCreateTerminal({ id, socket, data }: { id: string, s
|
||||
}
|
||||
|
||||
// Handle resizing a terminal
|
||||
export function handleResizeTerminal({ dimensions }: { dimensions: { cols: number; rows: number } }, context: HandlerContext) {
|
||||
export const handleResizeTerminal: SocketHandler = ({ dimensions }: any, context: HandlerContext) => {
|
||||
context.terminalManager.resizeTerminal(dimensions)
|
||||
}
|
||||
|
||||
// Handle sending data to a terminal
|
||||
export function handleTerminalData({ id, data }: { id: string, data: string }, context: HandlerContext) {
|
||||
export const handleTerminalData: SocketHandler = ({ id, data }: any, context: HandlerContext) => {
|
||||
return context.terminalManager.sendTerminalData(id, data)
|
||||
}
|
||||
|
||||
// Handle closing a terminal
|
||||
export function handleCloseTerminal({ id }: { id: string }, context: HandlerContext) {
|
||||
export const handleCloseTerminal: SocketHandler = ({ id }: any, context: HandlerContext) => {
|
||||
return context.terminalManager.closeTerminal(id)
|
||||
}
|
||||
|
||||
// Handle generating code
|
||||
export function handleGenerateCode({ userId, fileName, code, line, instructions }: { userId: string, fileName: string, code: string, line: number, instructions: string }, context: HandlerContext) {
|
||||
export 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<T = Record<string, any>> = (args: T, context: HandlerContext) => any;
|
||||
|
@ -180,165 +180,46 @@ io.on("connection", async (socket) => {
|
||||
sandboxManager: containers[data.sandboxId],
|
||||
}
|
||||
|
||||
// Handle various socket events (heartbeat, file operations, terminal operations, etc.)
|
||||
socket.on("heartbeat", async (options, callback) => {
|
||||
try {
|
||||
callback?.(handleHeartbeat(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error setting timeout:", e)
|
||||
socket.emit("error", `Error: set timeout. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
// Helper function to handle socket events with error handling and optional rate limiting
|
||||
const handleSocketEvent = (
|
||||
event: string,
|
||||
handler: any,
|
||||
rateLimiter: any | null = null
|
||||
) => {
|
||||
socket.on(event, async (options: any, callback?: (response: any) => void) => {
|
||||
try {
|
||||
// Consume rate limiter if provided
|
||||
if (rateLimiter) {
|
||||
await rateLimiter.consume(data.userId, 1); // Adjust as needed for the specific rate limiter
|
||||
}
|
||||
const response = await handler(options, handlerContext)
|
||||
callback?.(response);
|
||||
} catch (e: any) {
|
||||
console.error(`Error processing event "${event}":`, e);
|
||||
socket.emit("error", `Error: ${event}. ${e.message ?? e}`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
socket.on("getFile", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleGetFile(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error getting file:", e)
|
||||
socket.emit("error", `Error: get file. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
// Register socket events with optional rate limiters
|
||||
handleSocketEvent("heartbeat", handleHeartbeat);
|
||||
handleSocketEvent("getFile", handleGetFile);
|
||||
handleSocketEvent("getFolder", handleGetFolder);
|
||||
handleSocketEvent("saveFile", handleSaveFile, saveFileRL);
|
||||
handleSocketEvent("moveFile", handleMoveFile);
|
||||
handleSocketEvent("list", handleListApps);
|
||||
handleSocketEvent("deploy", handleDeploy);
|
||||
handleSocketEvent("createFile", handleCreateFile, createFileRL);
|
||||
handleSocketEvent("createFolder", handleCreateFolder, createFolderRL);
|
||||
handleSocketEvent("renameFile", handleRenameFile, renameFileRL);
|
||||
handleSocketEvent("deleteFile", handleDeleteFile, deleteFileRL);
|
||||
handleSocketEvent("deleteFolder", handleDeleteFolder);
|
||||
handleSocketEvent("createTerminal", handleCreateTerminal);
|
||||
handleSocketEvent("resizeTerminal", handleResizeTerminal);
|
||||
handleSocketEvent("terminalData", handleTerminalData);
|
||||
handleSocketEvent("closeTerminal", handleCloseTerminal);
|
||||
handleSocketEvent("generateCode", handleGenerateCode);
|
||||
|
||||
socket.on("getFolder", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleGetFolder(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error getting folder:", e)
|
||||
socket.emit("error", `Error: get folder. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("saveFile", async (options, callback) => {
|
||||
try {
|
||||
await saveFileRL.consume(data.userId, 1)
|
||||
callback?.(await handleSaveFile(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error saving file:", e)
|
||||
socket.emit("error", `Error: file saving. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("moveFile", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleMoveFile(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error moving file:", e)
|
||||
socket.emit("error", `Error: file moving. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("list", async (options, callback) => {
|
||||
console.log("Retrieving apps list...")
|
||||
try {
|
||||
callback?.(await handleListApps(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error retrieving apps list:", e)
|
||||
socket.emit("error", `Error: app list retrieval. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("deploy", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleDeploy(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error deploying project:", e)
|
||||
socket.emit("error", `Error: project deployment. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("createFile", async (options, callback) => {
|
||||
try {
|
||||
await createFileRL.consume(data.userId, 1)
|
||||
callback?.({ success: await handleCreateFile(options, handlerContext) })
|
||||
} catch (e: any) {
|
||||
console.error("Error creating file:", e)
|
||||
socket.emit("error", `Error: file creation. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("createFolder", async (options, callback) => {
|
||||
try {
|
||||
await createFolderRL.consume(data.userId, 1)
|
||||
callback?.(await handleCreateFolder(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error creating folder:", e)
|
||||
socket.emit("error", `Error: folder creation. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("renameFile", async (options, callback) => {
|
||||
try {
|
||||
await renameFileRL.consume(data.userId, 1)
|
||||
callback?.(await handleRenameFile(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error renaming file:", e)
|
||||
socket.emit("error", `Error: file renaming. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("deleteFile", async (options, callback) => {
|
||||
try {
|
||||
await deleteFileRL.consume(data.userId, 1)
|
||||
callback?.(await handleDeleteFile(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error deleting file:", e)
|
||||
socket.emit("error", `Error: file deletion. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("deleteFolder", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleDeleteFolder(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error deleting folder:", e)
|
||||
socket.emit("error", `Error: folder deletion. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("createTerminal", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleCreateTerminal(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error(`Error creating terminal ${options.id}:`, e)
|
||||
socket.emit("error", `Error: terminal creation. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("resizeTerminal", (options, callback) => {
|
||||
try {
|
||||
callback?.(handleResizeTerminal(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error resizing terminal:", e)
|
||||
socket.emit("error", `Error: terminal resizing. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("terminalData", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleTerminalData(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error writing to terminal:", e)
|
||||
socket.emit("error", `Error: writing to terminal. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("closeTerminal", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleCloseTerminal(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error closing terminal:", e)
|
||||
socket.emit("error", `Error: closing terminal. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("generateCode", async (options, callback) => {
|
||||
try {
|
||||
callback?.(await handleGenerateCode(options, handlerContext))
|
||||
} catch (e: any) {
|
||||
console.error("Error generating code:", e)
|
||||
socket.emit("error", `Error: code generation. ${e.message ?? e}`)
|
||||
}
|
||||
})
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user