985 lines
30 KiB
TypeScript
Raw Normal View History

2024-10-19 05:25:26 -06:00
import cors from "cors"
import dotenv from "dotenv"
import express, { Express } from "express"
import fs from "fs"
import { createServer } from "http"
import path from "path"
import { Server } from "socket.io"
import { DokkuClient } from "./DokkuClient"
import { SecureGitClient } from "./SecureGitClient"
import { z } from "zod"
2024-04-30 01:56:43 -04:00
import {
createFile,
deleteFile,
2024-05-11 17:23:45 -07:00
getFolder,
2024-05-09 22:32:21 -07:00
getProjectSize,
2024-04-30 01:56:43 -04:00
getSandboxFiles,
renameFile,
saveFile,
2024-10-19 05:25:26 -06:00
} from "./fileoperations"
import { TFile, TFileData, TFolder, User } from "./types"
import { LockManager } from "./utils"
2024-09-05 12:32:32 -07:00
2024-10-19 05:25:26 -06:00
import {
EntryInfo,
Filesystem,
FilesystemEvent,
Sandbox,
WatchHandle,
} from "e2b"
2024-09-05 12:32:32 -07:00
import { Terminal } from "./Terminal"
2024-05-05 12:55:34 -07:00
import {
2024-05-05 12:58:45 -07:00
MAX_BODY_SIZE,
2024-05-05 12:55:34 -07:00
createFileRL,
2024-05-11 18:03:42 -07:00
createFolderRL,
2024-05-05 12:55:34 -07:00
deleteFileRL,
renameFileRL,
saveFileRL,
2024-10-19 05:25:26 -06:00
} from "./ratelimit"
2024-04-18 16:40:08 -04:00
2024-10-19 05:25:26 -06:00
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error)
// Do not exit the process
// You can add additional logging or recovery logic here
2024-10-19 05:25:26 -06:00
})
2024-10-19 05:25:26 -06:00
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason)
// Do not exit the process
// You can also handle the rejected promise here if needed
2024-10-19 05:25:26 -06:00
})
// The amount of time in ms that a container will stay alive without a hearbeat.
const CONTAINER_TIMEOUT = 120_000
2024-10-19 05:25:26 -06:00
dotenv.config()
2024-04-18 16:40:08 -04:00
2024-10-19 05:25:26 -06:00
const app: Express = express()
const port = process.env.PORT || 4000
app.use(cors())
const httpServer = createServer(app)
2024-04-18 16:40:08 -04:00
const io = new Server(httpServer, {
cors: {
origin: "*",
},
2024-10-19 05:25:26 -06:00
})
2024-04-18 16:40:08 -04:00
2024-10-19 05:25:26 -06:00
let inactivityTimeout: NodeJS.Timeout | null = null
let isOwnerConnected = false
2024-10-19 05:25:26 -06:00
const containers: Record<string, Sandbox> = {}
const connections: Record<string, number> = {}
2024-04-28 20:06:47 -04:00
2024-10-19 05:25:26 -06:00
const dirName = "/home/user"
2024-10-19 05:25:26 -06:00
const moveFile = async (
filesystem: Filesystem,
filePath: string,
newFilePath: string
) => {
2024-09-29 20:54:09 -07:00
try {
2024-10-19 05:25:26 -06:00
const fileContents = await filesystem.read(filePath)
await filesystem.write(newFilePath, fileContents)
await filesystem.remove(filePath)
2024-09-29 20:54:09 -07:00
} catch (e) {
2024-10-19 05:25:26 -06:00
console.error(`Error moving file from ${filePath} to ${newFilePath}:`, e)
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
}
2024-04-29 00:50:25 -04:00
2024-04-18 16:40:08 -04:00
io.use(async (socket, next) => {
2024-05-25 20:13:31 -07:00
const handshakeSchema = z.object({
userId: z.string(),
sandboxId: z.string(),
EIO: z.string(),
transport: z.string(),
2024-10-19 05:25:26 -06:00
})
2024-05-25 20:13:31 -07:00
2024-10-19 05:25:26 -06:00
const q = socket.handshake.query
const parseQuery = handshakeSchema.safeParse(q)
2024-04-21 22:55:49 -04:00
if (!parseQuery.success) {
2024-10-19 05:25:26 -06:00
next(new Error("Invalid request."))
return
2024-04-21 22:55:49 -04:00
}
2024-10-19 05:25:26 -06:00
const { sandboxId, userId } = parseQuery.data
2024-05-13 23:22:06 -07:00
const dbUser = await fetch(
2024-05-26 18:37:36 -07:00
`${process.env.DATABASE_WORKER_URL}/api/user?id=${userId}`,
{
headers: {
Authorization: `${process.env.WORKERS_KEY}`,
},
}
2024-10-19 05:25:26 -06:00
)
const dbUserJSON = (await dbUser.json()) as User
2024-04-21 22:55:49 -04:00
if (!dbUserJSON) {
2024-10-19 05:25:26 -06:00
next(new Error("DB error."))
return
2024-04-18 16:40:08 -04:00
}
2024-10-19 05:25:26 -06:00
const sandbox = dbUserJSON.sandbox.find((s) => s.id === sandboxId)
2024-05-03 14:58:56 -07:00
const sharedSandboxes = dbUserJSON.usersToSandboxes.find(
(uts) => uts.sandboxId === sandboxId
2024-10-19 05:25:26 -06:00
)
2024-04-18 16:40:08 -04:00
2024-05-03 14:58:56 -07:00
if (!sandbox && !sharedSandboxes) {
2024-10-19 05:25:26 -06:00
next(new Error("Invalid credentials."))
return
2024-04-21 22:55:49 -04:00
}
2024-04-26 02:10:37 -04:00
socket.data = {
userId,
sandboxId: sandboxId,
isOwner: sandbox !== undefined,
2024-10-19 05:25:26 -06:00
}
2024-04-18 16:40:08 -04:00
2024-10-19 05:25:26 -06:00
next()
})
2024-04-18 16:40:08 -04:00
2024-10-19 05:25:26 -06:00
const lockManager = new LockManager()
2024-10-19 05:25:26 -06:00
if (!process.env.DOKKU_HOST)
console.error("Environment variable DOKKU_HOST is not defined")
if (!process.env.DOKKU_USERNAME)
console.error("Environment variable DOKKU_USERNAME is not defined")
if (!process.env.DOKKU_KEY)
console.error("Environment variable DOKKU_KEY is not defined")
const client =
process.env.DOKKU_HOST && process.env.DOKKU_KEY && process.env.DOKKU_USERNAME
? new DokkuClient({
host: process.env.DOKKU_HOST,
username: process.env.DOKKU_USERNAME,
privateKey: fs.readFileSync(process.env.DOKKU_KEY),
})
2024-10-19 05:25:26 -06:00
: null
client?.connect()
2024-10-19 05:25:26 -06:00
const git =
process.env.DOKKU_HOST && process.env.DOKKU_KEY
? new SecureGitClient(
`dokku@${process.env.DOKKU_HOST}`,
process.env.DOKKU_KEY
)
: null
2024-04-18 16:40:08 -04:00
io.on("connection", async (socket) => {
try {
2024-10-19 05:25:26 -06:00
if (inactivityTimeout) clearTimeout(inactivityTimeout)
const data = socket.data as {
2024-10-19 05:25:26 -06:00
userId: string
sandboxId: string
isOwner: boolean
}
2024-04-21 22:55:49 -04:00
if (data.isOwner) {
2024-10-19 05:25:26 -06:00
isOwnerConnected = true
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1
} else {
if (!isOwnerConnected) {
2024-10-19 05:25:26 -06:00
socket.emit("disableAccess", "The sandbox owner is not connected.")
return
}
}
2024-10-19 05:25:26 -06:00
const createdContainer = await lockManager.acquireLock(
data.sandboxId,
async () => {
try {
// Start a new container if the container doesn't exist or it timed out.
if (
!containers[data.sandboxId] ||
!(await containers[data.sandboxId].isRunning())
) {
containers[data.sandboxId] = await Sandbox.create({
timeoutMs: CONTAINER_TIMEOUT,
})
console.log("Created container ", data.sandboxId)
return true
}
} catch (e: any) {
console.error(`Error creating container ${data.sandboxId}:`, e)
io.emit("error", `Error: container creation. ${e.message ?? e}`)
}
}
2024-10-19 05:25:26 -06:00
)
2024-04-27 19:12:25 -04:00
const terminals: Record<string, Terminal> = {}
2024-10-19 05:25:26 -06:00
const sandboxFiles = await getSandboxFiles(data.sandboxId)
const projectDirectory = path.posix.join(
dirName,
"projects",
data.sandboxId
)
const containerFiles = containers[data.sandboxId].files
const fileWatchers: WatchHandle[] = []
2024-09-29 20:54:09 -07:00
// Change the owner of the project directory to user
2024-09-15 13:11:59 -07:00
const fixPermissions = async (projectDirectory: string) => {
try {
await containers[data.sandboxId].commands.run(
`sudo chown -R user "${projectDirectory}"`
2024-10-19 05:25:26 -06:00
)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.log("Failed to fix permissions: " + e)
}
2024-10-19 05:25:26 -06:00
}
2024-04-27 00:20:17 -04:00
2024-09-29 20:54:09 -07:00
// Check if the given path is a directory
const isDirectory = async (projectDirectory: string): Promise<boolean> => {
2024-09-05 12:32:32 -07:00
try {
2024-09-29 20:54:09 -07:00
const result = await containers[data.sandboxId].commands.run(
`[ -d "${projectDirectory}" ] && echo "true" || echo "false"`
2024-10-19 05:25:26 -06:00
)
return result.stdout.trim() === "true"
2024-09-05 12:32:32 -07:00
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.log("Failed to check if directory: " + e)
return false
2024-09-05 12:32:32 -07:00
}
2024-10-19 05:25:26 -06:00
}
2024-09-29 20:54:09 -07:00
// Only continue to container setup if a new container was created
if (createdContainer) {
// Copy all files from the project to the container
const promises = sandboxFiles.fileData.map(async (file) => {
try {
2024-10-19 05:25:26 -06:00
const filePath = path.posix.join(dirName, file.id)
const parentDirectory = path.dirname(filePath)
2024-09-29 20:54:09 -07:00
if (!containerFiles.exists(parentDirectory)) {
2024-10-19 05:25:26 -06:00
await containerFiles.makeDir(parentDirectory)
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
await containerFiles.write(filePath, file.data)
2024-09-29 20:54:09 -07:00
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.log("Failed to create file: " + e)
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
})
await Promise.all(promises)
2024-09-29 20:54:09 -07:00
// Make the logged in user the owner of all project files
2024-10-19 05:25:26 -06:00
fixPermissions(projectDirectory)
2024-09-29 20:54:09 -07:00
}
2024-09-15 13:11:59 -07:00
2024-09-29 20:54:09 -07:00
// Start filesystem watcher for the project directory
2024-10-19 05:25:26 -06:00
const watchDirectory = async (
directory: string
): Promise<WatchHandle | undefined> => {
2024-09-29 20:54:09 -07:00
try {
2024-10-19 05:25:26 -06:00
return await containerFiles.watch(
directory,
async (event: FilesystemEvent) => {
try {
function removeDirName(path: string, dirName: string) {
return path.startsWith(dirName)
? path.slice(dirName.length)
: path
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
// This is the absolute file path in the container
const containerFilePath = path.posix.join(directory, event.name)
// This is the file path relative to the home directory
const sandboxFilePath = removeDirName(
containerFilePath,
dirName + "/"
)
// This is the directory being watched relative to the home directory
const sandboxDirectory = removeDirName(directory, dirName + "/")
// Helper function to find a folder by id
function findFolderById(
files: (TFolder | TFile)[],
folderId: string
) {
return files.find(
(file: TFolder | TFile) =>
file.type === "folder" && file.id === folderId
)
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
// A new file or directory was created.
if (event.type === "create") {
const folder = findFolderById(
sandboxFiles.files,
sandboxDirectory
) as TFolder
const isDir = await isDirectory(containerFilePath)
const newItem = isDir
? ({
id: sandboxFilePath,
name: event.name,
type: "folder",
children: [],
} as TFolder)
: ({
id: sandboxFilePath,
name: event.name,
type: "file",
} as TFile)
if (folder) {
// If the folder exists, add the new item (file/folder) as a child
folder.children.push(newItem)
} else {
// If folder doesn't exist, add the new item to the root
sandboxFiles.files.push(newItem)
}
2024-09-29 20:54:09 -07:00
2024-10-19 05:25:26 -06:00
if (!isDir) {
const fileData = await containers[data.sandboxId].files.read(
containerFilePath
)
const fileContents =
typeof fileData === "string" ? fileData : ""
sandboxFiles.fileData.push({
id: sandboxFilePath,
data: fileContents,
})
}
2024-09-29 20:54:09 -07:00
2024-10-19 05:25:26 -06:00
console.log(`Create ${sandboxFilePath}`)
2024-09-29 20:54:09 -07:00
}
2024-04-27 19:12:25 -04:00
2024-10-19 05:25:26 -06:00
// A file or directory was removed or renamed.
else if (event.type === "remove" || event.type == "rename") {
const folder = findFolderById(
sandboxFiles.files,
sandboxDirectory
) as TFolder
const isDir = await isDirectory(containerFilePath)
const isFileMatch = (file: TFolder | TFile | TFileData) =>
file.id === sandboxFilePath ||
file.id.startsWith(containerFilePath + "/")
if (folder) {
// Remove item from its parent folder
folder.children = folder.children.filter(
(file: TFolder | TFile) => !isFileMatch(file)
)
} else {
// Remove from the root if it's not inside a folder
sandboxFiles.files = sandboxFiles.files.filter(
(file: TFolder | TFile) => !isFileMatch(file)
)
}
2024-09-29 20:54:09 -07:00
2024-10-19 05:25:26 -06:00
// Also remove any corresponding file data
sandboxFiles.fileData = sandboxFiles.fileData.filter(
(file: TFileData) => !isFileMatch(file)
)
2024-09-29 20:54:09 -07:00
2024-10-19 05:25:26 -06:00
console.log(`Removed: ${sandboxFilePath}`)
2024-09-29 20:54:09 -07:00
}
2024-10-19 05:25:26 -06:00
// The contents of a file were changed.
else if (event.type === "write") {
const folder = findFolderById(
sandboxFiles.files,
sandboxDirectory
) as TFolder
const fileToWrite = sandboxFiles.fileData.find(
(file) => file.id === sandboxFilePath
)
if (fileToWrite) {
fileToWrite.data = await containers[
data.sandboxId
].files.read(containerFilePath)
console.log(`Write to ${sandboxFilePath}`)
} else {
// If the file is part of a folder structure, locate it and update its data
const fileInFolder = folder?.children.find(
(file) => file.id === sandboxFilePath
)
if (fileInFolder) {
const fileData = await containers[
data.sandboxId
].files.read(containerFilePath)
const fileContents =
typeof fileData === "string" ? fileData : ""
sandboxFiles.fileData.push({
id: sandboxFilePath,
data: fileContents,
})
console.log(`Write to ${sandboxFilePath}`)
}
}
}
2024-09-29 20:54:09 -07:00
2024-10-19 05:25:26 -06:00
// Tell the client to reload the file list
socket.emit("loaded", sandboxFiles.files)
} catch (error) {
console.error(
`Error handling ${event.type} event for ${event.name}:`,
error
)
}
},
{ timeout: 0 }
)
2024-09-29 20:54:09 -07:00
} catch (error) {
2024-10-19 05:25:26 -06:00
console.error(`Error watching filesystem:`, error)
2024-09-15 13:11:59 -07:00
}
2024-10-19 05:25:26 -06:00
}
2024-04-27 19:12:25 -04:00
2024-09-29 20:54:09 -07:00
// Watch the project directory
2024-10-19 05:25:26 -06:00
const handle = await watchDirectory(projectDirectory)
// Keep track of watch handlers to close later
2024-10-19 05:25:26 -06:00
if (handle) fileWatchers.push(handle)
2024-09-29 20:54:09 -07:00
// Watch all subdirectories of the project directory, but not deeper
// This also means directories created after the container is created won't be watched
2024-10-19 05:25:26 -06:00
const dirContent = await containerFiles.list(projectDirectory)
await Promise.all(
dirContent.map(async (item: EntryInfo) => {
if (item.type === "dir") {
console.log("Watching " + item.path)
// Keep track of watch handlers to close later
const handle = await watchDirectory(item.path)
if (handle) fileWatchers.push(handle)
}
})
)
socket.emit("loaded", sandboxFiles.files)
2024-05-11 17:23:45 -07:00
socket.on("heartbeat", async () => {
try {
// This keeps the container alive for another CONTAINER_TIMEOUT seconds.
2024-10-19 05:25:26 -06:00
// The E2B docs are unclear, but the timeout is relative to the time of this method call.
await containers[data.sandboxId].setTimeout(CONTAINER_TIMEOUT)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error setting timeout:", e)
io.emit("error", `Error: set timeout. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
socket.on("getFile", (fileId: string, callback) => {
2024-10-19 05:25:26 -06:00
console.log(fileId)
try {
2024-10-19 05:25:26 -06:00
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
2024-04-27 19:12:25 -04:00
2024-10-19 05:25:26 -06:00
callback(file.data)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error getting file:", e)
io.emit("error", `Error: get file. ${e.message ?? e}`)
2024-05-05 12:58:45 -07:00
}
2024-10-19 05:25:26 -06:00
})
2024-05-05 12:58:45 -07:00
socket.on("getFolder", async (folderId: string, callback) => {
try {
2024-10-19 05:25:26 -06:00
const files = await getFolder(folderId)
callback(files)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error getting folder:", e)
io.emit("error", `Error: get folder. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-05 12:55:34 -07:00
// todo: send diffs + debounce for efficiency
socket.on("saveFile", async (fileId: string, body: string) => {
2024-10-19 05:25:26 -06:00
if (!fileId) return // handles saving when no file is open
2024-06-27 23:43:18 -07:00
try {
if (Buffer.byteLength(body, "utf-8") > MAX_BODY_SIZE) {
socket.emit(
"error",
"Error: file size too large. Please reduce the file size."
2024-10-19 05:25:26 -06:00
)
return
}
try {
2024-10-19 05:25:26 -06:00
await saveFileRL.consume(data.userId, 1)
await saveFile(fileId, body)
} catch (e) {
2024-10-19 05:25:26 -06:00
io.emit("error", "Rate limited: file saving. Please slow down.")
return
}
2024-04-27 19:12:25 -04:00
2024-10-19 05:25:26 -06:00
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
file.data = body
2024-05-10 00:12:41 -07:00
2024-09-05 12:32:32 -07:00
await containers[data.sandboxId].files.write(
path.posix.join(dirName, file.id),
body
2024-10-19 05:25:26 -06:00
)
fixPermissions(projectDirectory)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error saving file:", e)
io.emit("error", `Error: file saving. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-10 00:12:41 -07:00
socket.on(
"moveFile",
async (fileId: string, folderId: string, callback) => {
try {
2024-10-19 05:25:26 -06:00
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
2024-10-19 05:25:26 -06:00
const parts = fileId.split("/")
const newFileId = folderId + "/" + parts.pop()
await moveFile(
2024-09-05 12:32:32 -07:00
containers[data.sandboxId].files,
path.posix.join(dirName, fileId),
path.posix.join(dirName, newFileId)
2024-10-19 05:25:26 -06:00
)
fixPermissions(projectDirectory)
2024-10-19 05:25:26 -06:00
file.id = newFileId
2024-10-19 05:25:26 -06:00
await renameFile(fileId, newFileId, file.data)
const newFiles = await getSandboxFiles(data.sandboxId)
callback(newFiles.files)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error moving file:", e)
io.emit("error", `Error: file moving. ${e.message ?? e}`)
}
2024-05-10 00:12:41 -07:00
}
2024-10-19 05:25:26 -06:00
)
2024-05-10 00:12:41 -07:00
2024-07-21 14:58:38 -04:00
interface CallbackResponse {
2024-10-19 05:25:26 -06:00
success: boolean
apps?: string[]
message?: string
2024-07-21 14:58:38 -04:00
}
socket.on(
"list",
async (callback: (response: CallbackResponse) => void) => {
2024-10-19 05:25:26 -06:00
console.log("Retrieving apps list...")
try {
2024-10-19 05:25:26 -06:00
if (!client)
throw Error("Failed to retrieve apps list: No Dokku client")
callback({
success: true,
2024-10-19 05:25:26 -06:00
apps: await client.listApps(),
})
} catch (error) {
callback({
success: false,
message: "Failed to retrieve apps list",
2024-10-19 05:25:26 -06:00
})
}
2024-07-21 14:58:38 -04:00
}
2024-10-19 05:25:26 -06:00
)
2024-07-21 14:58:38 -04:00
socket.on(
"deploy",
async (callback: (response: CallbackResponse) => void) => {
try {
// Push the project files to the Dokku server
2024-10-19 05:25:26 -06:00
console.log("Deploying project ${data.sandboxId}...")
if (!git) throw Error("Failed to retrieve apps list: No git client")
// Remove the /project/[id]/ component of each file path:
const fixedFilePaths = sandboxFiles.fileData.map((file) => {
return {
...file,
id: file.id.split("/").slice(2).join("/"),
2024-10-19 05:25:26 -06:00
}
})
// Push all files to Dokku.
2024-10-19 05:25:26 -06:00
await git.pushFiles(fixedFilePaths, data.sandboxId)
callback({
success: true,
2024-10-19 05:25:26 -06:00
})
} catch (error) {
callback({
success: false,
message: "Failed to deploy project: " + error,
2024-10-19 05:25:26 -06:00
})
}
}
2024-10-19 05:25:26 -06:00
)
socket.on("createFile", async (name: string, callback) => {
try {
2024-10-19 05:25:26 -06:00
const size: number = await getProjectSize(data.sandboxId)
// limit is 200mb
if (size > 200 * 1024 * 1024) {
io.emit(
"error",
"Rate limited: project size exceeded. Please delete some files."
2024-10-19 05:25:26 -06:00
)
callback({ success: false })
return
}
2024-05-10 00:12:41 -07:00
try {
2024-10-19 05:25:26 -06:00
await createFileRL.consume(data.userId, 1)
} catch (e) {
2024-10-19 05:25:26 -06:00
io.emit("error", "Rate limited: file creation. Please slow down.")
return
}
2024-05-10 00:12:41 -07:00
2024-10-19 05:25:26 -06:00
const id = `projects/${data.sandboxId}/${name}`
2024-05-10 00:12:41 -07:00
2024-09-05 12:32:32 -07:00
await containers[data.sandboxId].files.write(
path.posix.join(dirName, id),
""
2024-10-19 05:25:26 -06:00
)
fixPermissions(projectDirectory)
2024-04-29 00:50:25 -04:00
sandboxFiles.files.push({
id,
name,
type: "file",
2024-10-19 05:25:26 -06:00
})
2024-04-29 00:50:25 -04:00
sandboxFiles.fileData.push({
id,
data: "",
2024-10-19 05:25:26 -06:00
})
2024-05-05 12:55:34 -07:00
2024-10-19 05:25:26 -06:00
await createFile(id)
2024-04-29 00:50:25 -04:00
2024-10-19 05:25:26 -06:00
callback({ success: true })
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error creating file:", e)
io.emit("error", `Error: file creation. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-09 22:32:21 -07:00
socket.on("createFolder", async (name: string, callback) => {
try {
try {
2024-10-19 05:25:26 -06:00
await createFolderRL.consume(data.userId, 1)
} catch (e) {
2024-10-19 05:25:26 -06:00
io.emit("error", "Rate limited: folder creation. Please slow down.")
return
}
2024-04-29 00:50:25 -04:00
2024-10-19 05:25:26 -06:00
const id = `projects/${data.sandboxId}/${name}`
2024-05-11 18:03:42 -07:00
2024-09-05 12:32:32 -07:00
await containers[data.sandboxId].files.makeDir(
path.posix.join(dirName, id)
2024-10-19 05:25:26 -06:00
)
2024-05-11 18:03:42 -07:00
2024-10-19 05:25:26 -06:00
callback()
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error creating folder:", e)
io.emit("error", `Error: folder creation. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-11 18:03:42 -07:00
socket.on("renameFile", async (fileId: string, newName: string) => {
try {
try {
2024-10-19 05:25:26 -06:00
await renameFileRL.consume(data.userId, 1)
} catch (e) {
2024-10-19 05:25:26 -06:00
io.emit("error", "Rate limited: file renaming. Please slow down.")
return
}
2024-05-11 18:03:42 -07:00
2024-10-19 05:25:26 -06:00
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
file.id = newName
2024-05-05 12:55:34 -07:00
2024-10-19 05:25:26 -06:00
const parts = fileId.split("/")
const newFileId =
2024-10-19 05:25:26 -06:00
parts.slice(0, parts.length - 1).join("/") + "/" + newName
2024-05-05 12:55:34 -07:00
await moveFile(
2024-09-05 12:32:32 -07:00
containers[data.sandboxId].files,
path.posix.join(dirName, fileId),
path.posix.join(dirName, newFileId)
2024-10-19 05:25:26 -06:00
)
fixPermissions(projectDirectory)
await renameFile(fileId, newFileId, file.data)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error renaming folder:", e)
io.emit("error", `Error: folder renaming. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-05 12:55:34 -07:00
socket.on("deleteFile", async (fileId: string, callback) => {
try {
try {
2024-10-19 05:25:26 -06:00
await deleteFileRL.consume(data.userId, 1)
} catch (e) {
2024-10-19 05:25:26 -06:00
io.emit("error", "Rate limited: file deletion. Please slow down.")
2024-05-05 12:55:34 -07:00
}
2024-04-28 20:06:47 -04:00
2024-10-19 05:25:26 -06:00
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
2024-04-30 01:56:43 -04:00
2024-09-05 12:32:32 -07:00
await containers[data.sandboxId].files.remove(
path.posix.join(dirName, fileId)
2024-10-19 05:25:26 -06:00
)
sandboxFiles.fileData = sandboxFiles.fileData.filter(
(f) => f.id !== fileId
2024-10-19 05:25:26 -06:00
)
2024-04-30 01:56:43 -04:00
2024-10-19 05:25:26 -06:00
await deleteFile(fileId)
2024-05-11 17:23:45 -07:00
2024-10-19 05:25:26 -06:00
const newFiles = await getSandboxFiles(data.sandboxId)
callback(newFiles.files)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error deleting file:", e)
io.emit("error", `Error: file deletion. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-11 17:23:45 -07:00
// todo
// socket.on("renameFolder", async (folderId: string, newName: string) => {
// });
2024-05-11 17:23:45 -07:00
socket.on("deleteFolder", async (folderId: string, callback) => {
try {
2024-10-19 05:25:26 -06:00
const files = await getFolder(folderId)
2024-05-11 17:23:45 -07:00
await Promise.all(
files.map(async (file) => {
2024-09-05 12:32:32 -07:00
await containers[data.sandboxId].files.remove(
path.posix.join(dirName, file)
2024-10-19 05:25:26 -06:00
)
2024-05-11 17:23:45 -07:00
sandboxFiles.fileData = sandboxFiles.fileData.filter(
(f) => f.id !== file
2024-10-19 05:25:26 -06:00
)
2024-05-11 17:23:45 -07:00
2024-10-19 05:25:26 -06:00
await deleteFile(file)
})
2024-10-19 05:25:26 -06:00
)
2024-05-11 17:23:45 -07:00
2024-10-19 05:25:26 -06:00
const newFiles = await getSandboxFiles(data.sandboxId)
2024-05-05 12:55:34 -07:00
2024-10-19 05:25:26 -06:00
callback(newFiles.files)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error deleting folder:", e)
io.emit("error", `Error: folder deletion. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-04-29 02:19:27 -04:00
socket.on("createTerminal", async (id: string, callback) => {
try {
// Note: The number of terminals per window is limited on the frontend, but not backend
if (terminals[id]) {
2024-10-19 05:25:26 -06:00
return
}
2024-04-29 02:19:27 -04:00
await lockManager.acquireLock(data.sandboxId, async () => {
try {
2024-09-05 12:32:32 -07:00
terminals[id] = new Terminal(containers[data.sandboxId])
await terminals[id].init({
onData: (responseString: string) => {
2024-10-19 05:25:26 -06:00
io.emit("terminalResponse", { id, data: responseString })
function extractPortNumber(inputString: string) {
// Remove ANSI escape codes
2024-10-19 05:25:26 -06:00
const cleanedString = inputString.replace(
/\x1B\[[0-9;]*m/g,
""
)
2024-09-05 12:32:32 -07:00
// Regular expression to match port number
2024-10-19 05:25:26 -06:00
const regex = /http:\/\/localhost:(\d+)/
// If a match is found, return the port number
2024-10-19 05:25:26 -06:00
const match = cleanedString.match(regex)
return match ? match[1] : null
}
2024-10-19 05:25:26 -06:00
const port = parseInt(extractPortNumber(responseString) ?? "")
if (port) {
io.emit(
"previewURL",
2024-09-05 12:32:32 -07:00
"https://" + containers[data.sandboxId].getHost(port)
2024-10-19 05:25:26 -06:00
)
}
},
2024-09-05 12:32:32 -07:00
cols: 80,
rows: 20,
//onExit: () => console.log("Terminal exited", id),
2024-10-19 05:25:26 -06:00
})
2024-10-19 05:25:26 -06:00
const defaultDirectory = path.posix.join(
dirName,
"projects",
data.sandboxId
)
const defaultCommands = [
`cd "${defaultDirectory}"`,
"export PS1='user> '",
2024-10-19 05:25:26 -06:00
"clear",
]
2024-10-19 05:25:26 -06:00
for (const command of defaultCommands)
await terminals[id].sendData(command + "\r")
2024-10-19 05:25:26 -06:00
console.log("Created terminal", id)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error(`Error creating terminal ${id}:`, e)
io.emit("error", `Error: terminal creation. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-04-29 02:19:27 -04:00
2024-10-19 05:25:26 -06:00
callback()
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error(`Error creating terminal ${id}:`, e)
io.emit("error", `Error: terminal creation. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-04-30 22:48:36 -04:00
socket.on(
"resizeTerminal",
(dimensions: { cols: number; rows: number }) => {
try {
Object.values(terminals).forEach((t) => {
2024-10-19 05:25:26 -06:00
t.resize(dimensions)
})
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error resizing terminal:", e)
io.emit("error", `Error: terminal resizing. ${e.message ?? e}`)
}
}
2024-10-19 05:25:26 -06:00
)
2024-05-06 23:34:45 -07:00
2024-09-05 12:32:32 -07:00
socket.on("terminalData", async (id: string, data: string) => {
try {
if (!terminals[id]) {
2024-10-19 05:25:26 -06:00
return
}
2024-04-28 20:06:47 -04:00
2024-10-19 05:25:26 -06:00
await terminals[id].sendData(data)
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error writing to terminal:", e)
io.emit("error", `Error: writing to terminal. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-04-28 20:06:47 -04:00
socket.on("closeTerminal", async (id: string, callback) => {
try {
if (!terminals[id]) {
2024-10-19 05:25:26 -06:00
return
}
2024-05-06 23:34:45 -07:00
2024-10-19 05:25:26 -06:00
await terminals[id].close()
delete terminals[id]
2024-05-06 23:34:45 -07:00
2024-10-19 05:25:26 -06:00
callback()
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error closing terminal:", e)
io.emit("error", `Error: closing terminal. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-05-06 23:34:45 -07:00
socket.on(
"generateCode",
async (
fileName: string,
code: string,
line: number,
instructions: string,
callback
) => {
try {
const fetchPromise = fetch(
`${process.env.DATABASE_WORKER_URL}/api/sandbox/generate`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.WORKERS_KEY}`,
},
body: JSON.stringify({
userId: data.userId,
}),
}
2024-10-19 05:25:26 -06:00
)
// Generate code from cloudflare workers AI
const generateCodePromise = fetch(
2024-10-19 05:25:26 -06:00
`${process.env.AI_WORKER_URL}/api?fileName=${encodeURIComponent(
fileName
)}&code=${encodeURIComponent(code)}&line=${encodeURIComponent(
line
)}&instructions=${encodeURIComponent(instructions)}`,
{
headers: {
"Content-Type": "application/json",
Authorization: `${process.env.CF_AI_KEY}`,
},
}
2024-10-19 05:25:26 -06:00
)
const [fetchResponse, generateCodeResponse] = await Promise.all([
fetchPromise,
generateCodePromise,
2024-10-19 05:25:26 -06:00
])
2024-10-19 05:25:26 -06:00
const json = await generateCodeResponse.json()
2024-10-19 05:25:26 -06:00
callback({ response: json.response, success: true })
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error generating code:", e)
io.emit("error", `Error: code generation. ${e.message ?? e}`)
2024-05-13 23:22:06 -07:00
}
}
2024-10-19 05:25:26 -06:00
)
2024-05-13 23:22:06 -07:00
socket.on("disconnect", async () => {
try {
if (data.isOwner) {
2024-10-19 05:25:26 -06:00
connections[data.sandboxId]--
2024-05-13 23:22:06 -07:00
}
2024-05-03 00:52:01 -07:00
// Close all terminals for this connection
await Promise.all(
Object.entries(terminals).map(async ([key, terminal]) => {
await terminal.close()
delete terminals[key]
})
)
// Stop watching file changes in the container
2024-10-19 05:25:26 -06:00
Promise.all(
fileWatchers.map(async (handle: WatchHandle) => {
await handle.close()
})
)
if (data.isOwner && connections[data.sandboxId] <= 0) {
socket.broadcast.emit(
"disableAccess",
"The sandbox owner has disconnected."
2024-10-19 05:25:26 -06:00
)
}
// const sockets = await io.fetchSockets();
// if (inactivityTimeout) {
// clearTimeout(inactivityTimeout);
// }
// if (sockets.length === 0) {
// console.log("STARTING TIMER");
// inactivityTimeout = setTimeout(() => {
// io.fetchSockets().then(async (sockets) => {
// if (sockets.length === 0) {
// console.log("Server stopped", res);
// }
// });
// }, 20000);
// } else {
// console.log("number of sockets", sockets.length);
// }
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.log("Error disconnecting:", e)
io.emit("error", `Error: disconnecting. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
} catch (e: any) {
2024-10-19 05:25:26 -06:00
console.error("Error connecting:", e)
io.emit("error", `Error: connection. ${e.message ?? e}`)
}
2024-10-19 05:25:26 -06:00
})
2024-04-18 16:40:08 -04:00
httpServer.listen(port, () => {
2024-10-19 05:25:26 -06:00
console.log(`Server running on port ${port}`)
})