refactor: move rate limiting to handler functions
This commit is contained in:
parent
3bc555ca47
commit
d3e987b0ab
@ -3,6 +3,13 @@ import { AIWorker } from "./AIWorker"
|
|||||||
import { CONTAINER_TIMEOUT } from "./constants"
|
import { CONTAINER_TIMEOUT } from "./constants"
|
||||||
import { DokkuClient } from "./DokkuClient"
|
import { DokkuClient } from "./DokkuClient"
|
||||||
import { FileManager } from "./FileManager"
|
import { FileManager } from "./FileManager"
|
||||||
|
import {
|
||||||
|
createFileRL,
|
||||||
|
createFolderRL,
|
||||||
|
deleteFileRL,
|
||||||
|
renameFileRL,
|
||||||
|
saveFileRL,
|
||||||
|
} from "./ratelimit"
|
||||||
import { SecureGitClient } from "./SecureGitClient"
|
import { SecureGitClient } from "./SecureGitClient"
|
||||||
import { TerminalManager } from "./TerminalManager"
|
import { TerminalManager } from "./TerminalManager"
|
||||||
import { LockManager } from "./utils"
|
import { LockManager } from "./utils"
|
||||||
@ -42,7 +49,8 @@ export const handleGetFolder: SocketHandler = ({ folderId }: any, context: Handl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle saving a file
|
// Handle saving a file
|
||||||
export const handleSaveFile: SocketHandler = ({ fileId, body }: any, context: HandlerContext) => {
|
export const handleSaveFile: SocketHandler = async ({ fileId, body, userId }: any, context: HandlerContext) => {
|
||||||
|
await saveFileRL.consume(userId, 1);
|
||||||
return context.fileManager.saveFile(fileId, body)
|
return context.fileManager.saveFile(fileId, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,22 +77,26 @@ export const handleDeploy: SocketHandler = async ({ sandboxId }: any, context: H
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle creating a file
|
// Handle creating a file
|
||||||
export const handleCreateFile: SocketHandler = async ({ name }: any, context: HandlerContext) => {
|
export const handleCreateFile: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => {
|
||||||
|
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 }: any, context: HandlerContext) => {
|
export const handleCreateFolder: SocketHandler = async ({ name, userId }: any, context: HandlerContext) => {
|
||||||
|
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 = ({ fileId, newName }: any, context: HandlerContext) => {
|
export const handleRenameFile: SocketHandler = async ({ fileId, newName, userId }: any, context: HandlerContext) => {
|
||||||
|
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 = ({ fileId }: any, context: HandlerContext) => {
|
export const handleDeleteFile: SocketHandler = async ({ fileId, userId }: any, context: HandlerContext) => {
|
||||||
|
await deleteFileRL.consume(userId, 1)
|
||||||
return context.fileManager.deleteFile(fileId)
|
return context.fileManager.deleteFile(fileId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,13 +9,6 @@ import { AIWorker } from "./AIWorker"
|
|||||||
import { CONTAINER_TIMEOUT } from "./constants"
|
import { CONTAINER_TIMEOUT } from "./constants"
|
||||||
import { DokkuClient } from "./DokkuClient"
|
import { DokkuClient } from "./DokkuClient"
|
||||||
import { FileManager, SandboxFiles } from "./FileManager"
|
import { FileManager, SandboxFiles } from "./FileManager"
|
||||||
import {
|
|
||||||
createFileRL,
|
|
||||||
createFolderRL,
|
|
||||||
deleteFileRL,
|
|
||||||
renameFileRL,
|
|
||||||
saveFileRL,
|
|
||||||
} from "./ratelimit"
|
|
||||||
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 { handleCloseTerminal, handleCreateFile, handleCreateFolder, handleCreateTerminal, handleDeleteFile, handleDeleteFolder, handleDeploy, handleGenerateCode, handleGetFile, handleGetFolder, handleHeartbeat, handleListApps, handleMoveFile, HandlerContext, handleRenameFile, handleResizeTerminal, handleSaveFile, handleTerminalData } from "./SocketHandlers"
|
||||||
@ -184,15 +177,11 @@ io.on("connection", async (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
|
||||||
const handleSocketEvent = (
|
const handleSocketEvent = (
|
||||||
event: string,
|
event: string,
|
||||||
handler: any,
|
handler: any
|
||||||
rateLimiter: any | null = null
|
|
||||||
) => {
|
) => {
|
||||||
socket.on(event, async (options: any, callback?: (response: any) => void) => {
|
socket.on(event, async (options: any, callback?: (response: any) => void) => {
|
||||||
try {
|
try {
|
||||||
// Consume rate limiter if provided
|
// 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, ...data }, handlerContext)
|
const response = await handler({ ...options, ...data }, handlerContext)
|
||||||
callback?.(response);
|
callback?.(response);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@ -206,14 +195,14 @@ io.on("connection", async (socket) => {
|
|||||||
handleSocketEvent("heartbeat", handleHeartbeat);
|
handleSocketEvent("heartbeat", handleHeartbeat);
|
||||||
handleSocketEvent("getFile", handleGetFile);
|
handleSocketEvent("getFile", handleGetFile);
|
||||||
handleSocketEvent("getFolder", handleGetFolder);
|
handleSocketEvent("getFolder", handleGetFolder);
|
||||||
handleSocketEvent("saveFile", handleSaveFile, saveFileRL);
|
handleSocketEvent("saveFile", handleSaveFile);
|
||||||
handleSocketEvent("moveFile", handleMoveFile);
|
handleSocketEvent("moveFile", handleMoveFile);
|
||||||
handleSocketEvent("list", handleListApps);
|
handleSocketEvent("list", handleListApps);
|
||||||
handleSocketEvent("deploy", handleDeploy);
|
handleSocketEvent("deploy", handleDeploy);
|
||||||
handleSocketEvent("createFile", handleCreateFile, createFileRL);
|
handleSocketEvent("createFile", handleCreateFile);
|
||||||
handleSocketEvent("createFolder", handleCreateFolder, createFolderRL);
|
handleSocketEvent("createFolder", handleCreateFolder);
|
||||||
handleSocketEvent("renameFile", handleRenameFile, renameFileRL);
|
handleSocketEvent("renameFile", handleRenameFile);
|
||||||
handleSocketEvent("deleteFile", handleDeleteFile, deleteFileRL);
|
handleSocketEvent("deleteFile", handleDeleteFile);
|
||||||
handleSocketEvent("deleteFolder", handleDeleteFolder);
|
handleSocketEvent("deleteFolder", handleDeleteFolder);
|
||||||
handleSocketEvent("createTerminal", handleCreateTerminal);
|
handleSocketEvent("createTerminal", handleCreateTerminal);
|
||||||
handleSocketEvent("resizeTerminal", handleResizeTerminal);
|
handleSocketEvent("resizeTerminal", handleResizeTerminal);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user