Compare commits

...

9 Commits

Author SHA1 Message Date
Akhilesh Rangani
ba0fab6856 patch: remove excess code 2024-06-16 19:18:19 -04:00
Akhilesh Rangani
186d765682 Merge branch 'feat/e2b' of https://github.com/jamesmurdza/sandbox into feat/e2b 2024-06-16 15:19:13 -04:00
Akhilesh Rangani
2994a4d291 fix: sandboxes now linked to specific users 2024-06-16 15:18:56 -04:00
James Murdza
85ebfe4161 fix: use the container URL for the preview panel 2024-06-15 21:04:33 -04:00
James Murdza
cfed8a225e fix: set project file permissions so that they belong to the terminal user 2024-06-15 21:00:43 -04:00
James Murdza
e7dd3238df fix: sync files to container instead of local file system 2024-06-14 15:43:22 -04:00
James Murdza
530aa2ff53 fix: ensure container remains open until all owner connections are closed 2024-06-14 13:32:55 -04:00
James Murdza
3c4850ee72 fix: wait until terminals are killed to close the container 2024-06-14 12:28:19 -04:00
Akhilesh Rangani
e3b2d882dd patch: requests 2024-06-14 12:10:01 -04:00
5 changed files with 111 additions and 82 deletions

View File

@ -110,12 +110,17 @@ export default {
const body = await request.json() const body = await request.json()
const { type, name, userId, visibility } = initSchema.parse(body) const { type, name, userId, visibility } = initSchema.parse(body)
const allSandboxes = await db.select().from(sandbox).all() const userSandboxes = await db
if (allSandboxes.length >= 8) { .select()
return new Response("You reached the maximum # of sandboxes.", { .from(sandbox)
status: 400, .where(eq(sandbox.userId, userId))
}) .all();
}
if (userSandboxes.length >= 8) {
return new Response("You reached the maximum # of sandboxes.", {
status: 400,
});
}
const sb = await db const sb = await db
.insert(sandbox) .insert(sandbox)

View File

@ -31,8 +31,8 @@ export const sandbox = sqliteTable("sandbox", {
createdAt: integer("createdAt", { mode: "timestamp_ms" }), createdAt: integer("createdAt", { mode: "timestamp_ms" }),
userId: text("user_id") userId: text("user_id")
.notNull() .notNull()
.references(() => user.id), .references(() => user.id, { onDelete: "cascade" }),
}); });
export type Sandbox = typeof sandbox.$inferSelect; export type Sandbox = typeof sandbox.$inferSelect;

View File

@ -1,4 +1,3 @@
import fs from "fs";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import cors from "cors"; import cors from "cors";
@ -19,7 +18,7 @@ import {
saveFile, saveFile,
} from "./fileoperations"; } from "./fileoperations";
import { LockManager } from "./utils"; import { LockManager } from "./utils";
import { Sandbox, Terminal } from "e2b"; import { Sandbox, Terminal, FilesystemManager } from "e2b";
import { import {
MAX_BODY_SIZE, MAX_BODY_SIZE,
createFileRL, createFileRL,
@ -45,9 +44,16 @@ let inactivityTimeout: NodeJS.Timeout | null = null;
let isOwnerConnected = false; let isOwnerConnected = false;
const containers: Record<string, Sandbox> = {}; const containers: Record<string, Sandbox> = {};
const connections: Record<string, number> = {};
const terminals: Record<string, Terminal> = {}; const terminals: Record<string, Terminal> = {};
const dirName = path.join(__dirname, ".."); const dirName = "/home/user";
const moveFile = async (filesystem: FilesystemManager, filePath: string, newFilePath: string) => {
const fileContents = await filesystem.readBytes(filePath)
await filesystem.writeBytes(newFilePath, fileContents);
await filesystem.remove(filePath);
}
io.use(async (socket, next) => { io.use(async (socket, next) => {
const handshakeSchema = z.object({ const handshakeSchema = z.object({
@ -113,6 +119,7 @@ io.on("connection", async (socket) => {
if (data.isOwner) { if (data.isOwner) {
isOwnerConnected = true; isOwnerConnected = true;
connections[data.sandboxId] = (connections[data.sandboxId] ?? 0) + 1;
} else { } else {
if (!isOwnerConnected) { if (!isOwnerConnected) {
socket.emit("disableAccess", "The sandbox owner is not connected."); socket.emit("disableAccess", "The sandbox owner is not connected.");
@ -125,20 +132,27 @@ io.on("connection", async (socket) => {
if (!containers[data.sandboxId]) { if (!containers[data.sandboxId]) {
containers[data.sandboxId] = await Sandbox.create(); containers[data.sandboxId] = await Sandbox.create();
console.log("Created container ", data.sandboxId); console.log("Created container ", data.sandboxId);
io.emit("previewURL", "https://" + containers[data.sandboxId].getHostname(5173));
} }
} catch (error) { } catch (error) {
console.error("Error creating container ", data.sandboxId, error); console.error("Error creating container ", data.sandboxId, error);
} }
}); });
// Change the owner of the project directory to user
const fixPermissions = async () => {
await containers[data.sandboxId].process.startAndWait(
`sudo chown -R user "${path.join(dirName, "projects", data.sandboxId)}"`
);
}
const sandboxFiles = await getSandboxFiles(data.sandboxId); const sandboxFiles = await getSandboxFiles(data.sandboxId);
sandboxFiles.fileData.forEach((file) => { sandboxFiles.fileData.forEach(async (file) => {
const filePath = path.join(dirName, file.id); const filePath = path.join(dirName, file.id);
fs.mkdirSync(path.dirname(filePath), { recursive: true }); await containers[data.sandboxId].filesystem.makeDir(path.dirname(filePath));
fs.writeFile(filePath, file.data, function (err) { await containers[data.sandboxId].filesystem.write(filePath, file.data);
if (err) throw err;
});
}); });
fixPermissions();
socket.emit("loaded", sandboxFiles.files); socket.emit("loaded", sandboxFiles.files);
@ -171,9 +185,8 @@ io.on("connection", async (socket) => {
if (!file) return; if (!file) return;
file.data = body; file.data = body;
fs.writeFile(path.join(dirName, file.id), body, function (err) { await containers[data.sandboxId].filesystem.write(path.join(dirName, file.id), body);
if (err) throw err; fixPermissions();
});
await saveFile(fileId, body); await saveFile(fileId, body);
} catch (e) { } catch (e) {
io.emit("rateLimit", "Rate limited: file saving. Please slow down."); io.emit("rateLimit", "Rate limited: file saving. Please slow down.");
@ -187,13 +200,12 @@ io.on("connection", async (socket) => {
const parts = fileId.split("/"); const parts = fileId.split("/");
const newFileId = folderId + "/" + parts.pop(); const newFileId = folderId + "/" + parts.pop();
fs.rename( await moveFile(
containers[data.sandboxId].filesystem,
path.join(dirName, fileId), path.join(dirName, fileId),
path.join(dirName, newFileId), path.join(dirName, newFileId)
function (err) { )
if (err) throw err; fixPermissions();
}
);
file.id = newFileId; file.id = newFileId;
@ -219,9 +231,8 @@ io.on("connection", async (socket) => {
const id = `projects/${data.sandboxId}/${name}`; const id = `projects/${data.sandboxId}/${name}`;
fs.writeFile(path.join(dirName, id), "", function (err) { await containers[data.sandboxId].filesystem.write(path.join(dirName, id), "");
if (err) throw err; fixPermissions();
});
sandboxFiles.files.push({ sandboxFiles.files.push({
id, id,
@ -248,9 +259,7 @@ io.on("connection", async (socket) => {
const id = `projects/${data.sandboxId}/${name}`; const id = `projects/${data.sandboxId}/${name}`;
fs.mkdir(path.join(dirName, id), { recursive: true }, function (err) { await containers[data.sandboxId].filesystem.makeDir(path.join(dirName, id));
if (err) throw err;
});
callback(); callback();
} catch (e) { } catch (e) {
@ -270,13 +279,13 @@ io.on("connection", async (socket) => {
const newFileId = const newFileId =
parts.slice(0, parts.length - 1).join("/") + "/" + newName; parts.slice(0, parts.length - 1).join("/") + "/" + newName;
fs.rename(
await moveFile(
containers[data.sandboxId].filesystem,
path.join(dirName, fileId), path.join(dirName, fileId),
path.join(dirName, newFileId), path.join(dirName, newFileId)
function (err) { )
if (err) throw err; fixPermissions();
}
);
await renameFile(fileId, newFileId, file.data); await renameFile(fileId, newFileId, file.data);
} catch (e) { } catch (e) {
io.emit("rateLimit", "Rate limited: file renaming. Please slow down."); io.emit("rateLimit", "Rate limited: file renaming. Please slow down.");
@ -290,9 +299,7 @@ io.on("connection", async (socket) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId); const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file) return; if (!file) return;
fs.unlink(path.join(dirName, fileId), function (err) { await containers[data.sandboxId].filesystem.remove(path.join(dirName, fileId));
if (err) throw err;
});
sandboxFiles.fileData = sandboxFiles.fileData.filter( sandboxFiles.fileData = sandboxFiles.fileData.filter(
(f) => f.id !== fileId (f) => f.id !== fileId
); );
@ -315,9 +322,7 @@ io.on("connection", async (socket) => {
await Promise.all( await Promise.all(
files.map(async (file) => { files.map(async (file) => {
fs.unlink(path.join(dirName, file), function (err) { await containers[data.sandboxId].filesystem.remove(path.join(dirName, file));
if (err) throw err;
});
sandboxFiles.fileData = sandboxFiles.fileData.filter( sandboxFiles.fileData = sandboxFiles.fileData.filter(
(f) => f.id !== file (f) => f.id !== file
@ -346,6 +351,7 @@ io.on("connection", async (socket) => {
size: { cols: 80, rows: 20 }, size: { cols: 80, rows: 20 },
onExit: () => console.log("Terminal exited", id), onExit: () => console.log("Terminal exited", id),
}); });
await terminals[id].sendData(`cd "${path.join(dirName, "projects", data.sandboxId)}"\r`)
await terminals[id].sendData("export PS1='user> '\rclear\r"); await terminals[id].sendData("export PS1='user> '\rclear\r");
console.log("Created terminal", id); console.log("Created terminal", id);
} catch (error) { } catch (error) {
@ -432,11 +438,16 @@ io.on("connection", async (socket) => {
socket.on("disconnect", async () => { socket.on("disconnect", async () => {
if (data.isOwner) { if (data.isOwner) {
Object.entries(terminals).forEach((t) => { connections[data.sandboxId]--;
const terminal = t[1]; }
terminal.kill();
delete terminals[t[0]]; if (data.isOwner && connections[data.sandboxId] <= 0) {
}); await Promise.all(
Object.entries(terminals).map(async ([key, terminal]) => {
await terminal.kill();
delete terminals[key];
})
);
await lockManager.acquireLock(data.sandboxId, async () => { await lockManager.acquireLock(data.sandboxId, async () => {
try { try {

View File

@ -3,7 +3,7 @@
import { SetStateAction, useCallback, useEffect, useRef, useState } from "react" import { SetStateAction, useCallback, useEffect, useRef, useState } from "react"
import monaco from "monaco-editor" import monaco from "monaco-editor"
import Editor, { BeforeMount, OnMount } from "@monaco-editor/react" import Editor, { BeforeMount, OnMount } from "@monaco-editor/react"
import { io } from "socket.io-client" import { Socket, io } from "socket.io-client"
import { toast } from "sonner" import { toast } from "sonner"
import { useClerk } from "@clerk/nextjs" import { useClerk } from "@clerk/nextjs"
@ -41,12 +41,16 @@ export default function CodeEditor({
sandboxData: Sandbox sandboxData: Sandbox
reactDefinitionFile: string reactDefinitionFile: string
}) { }) {
const socket = io( const socketRef = useRef<Socket | null>(null);
`http://localhost:${process.env.NEXT_PUBLIC_SERVER_PORT}?userId=${userData.id}&sandboxId=${sandboxData.id}`,
{ // Initialize socket connection if it doesn't exist
timeout: 2000, if (!socketRef.current) {
} socketRef.current = io(
) `http://localhost:${process.env.NEXT_PUBLIC_SERVER_PORT}?userId=${userData.id}&sandboxId=${sandboxData.id}`,
{
timeout: 2000,
}
);}
const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(true) const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(true)
const [disableAccess, setDisableAccess] = useState({ const [disableAccess, setDisableAccess] = useState({
@ -90,6 +94,9 @@ export default function CodeEditor({
}[] }[]
>([]) >([])
// Preview state
const [previewURL, setPreviewURL] = useState<string>("");
const isOwner = sandboxData.userId === userData.id const isOwner = sandboxData.userId === userData.id
const clerk = useClerk() const clerk = useClerk()
@ -298,9 +305,10 @@ export default function CodeEditor({
) )
); );
console.log(`Saving file...${activeFileId}`); console.log(`Saving file...${activeFileId}`);
socket.emit("saveFile", activeFileId, value); console.log(`Saving file...${value}`);
socketRef.current?.emit("saveFile", activeFileId, value);
}, Number(process.env.FILE_SAVE_DEBOUNCE_DELAY)||1000), }, Number(process.env.FILE_SAVE_DEBOUNCE_DELAY)||1000),
[socket] [socketRef]
); );
useEffect(() => { useEffect(() => {
@ -364,10 +372,10 @@ export default function CodeEditor({
// Connection/disconnection effect // Connection/disconnection effect
useEffect(() => { useEffect(() => {
socket.connect() socketRef.current?.connect()
return () => { return () => {
socket.disconnect() socketRef.current?.disconnect()
} }
}, []) }, [])
@ -402,20 +410,22 @@ export default function CodeEditor({
}) })
} }
socket.on("connect", onConnect) socketRef.current?.on("connect", onConnect)
socket.on("disconnect", onDisconnect) socketRef.current?.on("disconnect", onDisconnect)
socket.on("loaded", onLoadedEvent) socketRef.current?.on("loaded", onLoadedEvent)
socket.on("rateLimit", onRateLimit) socketRef.current?.on("rateLimit", onRateLimit)
socket.on("terminalResponse", onTerminalResponse) socketRef.current?.on("terminalResponse", onTerminalResponse)
socket.on("disableAccess", onDisableAccess) socketRef.current?.on("disableAccess", onDisableAccess)
socketRef.current?.on("previewURL", setPreviewURL)
return () => { return () => {
socket.off("connect", onConnect) socketRef.current?.off("connect", onConnect)
socket.off("disconnect", onDisconnect) socketRef.current?.off("disconnect", onDisconnect)
socket.off("loaded", onLoadedEvent) socketRef.current?.off("loaded", onLoadedEvent)
socket.off("rateLimit", onRateLimit) socketRef.current?.off("rateLimit", onRateLimit)
socket.off("terminalResponse", onTerminalResponse) socketRef.current?.off("terminalResponse", onTerminalResponse)
socket.off("disableAccess", onDisableAccess) socketRef.current?.off("disableAccess", onDisableAccess)
socketRef.current?.off("previewURL", setPreviewURL)
} }
// }, []); // }, []);
}, [terminals]) }, [terminals])
@ -430,7 +440,7 @@ export default function CodeEditor({
// Debounced function to get file content // Debounced function to get file content
const debouncedGetFile = useCallback( const debouncedGetFile = useCallback(
debounce((tabId, callback) => { debounce((tabId, callback) => {
socket.emit('getFile', tabId, callback); socketRef.current?.emit('getFile', tabId, callback);
}, 300), // 300ms debounce delay, adjust as needed }, 300), // 300ms debounce delay, adjust as needed
[] []
); );
@ -534,7 +544,7 @@ export default function CodeEditor({
return false return false
} }
socket.emit("renameFile", id, newName) socketRef.current?.emit("renameFile", id, newName)
setTabs((prev) => setTabs((prev) =>
prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab)) prev.map((tab) => (tab.id === id ? { ...tab, name: newName } : tab))
) )
@ -543,7 +553,7 @@ export default function CodeEditor({
} }
const handleDeleteFile = (file: TFile) => { const handleDeleteFile = (file: TFile) => {
socket.emit("deleteFile", file.id, (response: (TFolder | TFile)[]) => { socketRef.current?.emit("deleteFile", file.id, (response: (TFolder | TFile)[]) => {
setFiles(response) setFiles(response)
}) })
closeTab(file.id) closeTab(file.id)
@ -553,11 +563,11 @@ export default function CodeEditor({
setDeletingFolderId(folder.id) setDeletingFolderId(folder.id)
console.log("deleting folder", folder.id) console.log("deleting folder", folder.id)
socket.emit("getFolder", folder.id, (response: string[]) => socketRef.current?.emit("getFolder", folder.id, (response: string[]) =>
closeTabs(response) closeTabs(response)
) )
socket.emit("deleteFolder", folder.id, (response: (TFolder | TFile)[]) => { socketRef.current?.emit("deleteFolder", folder.id, (response: (TFolder | TFile)[]) => {
setFiles(response) setFiles(response)
setDeletingFolderId("") setDeletingFolderId("")
}) })
@ -584,7 +594,7 @@ export default function CodeEditor({
{generate.show && ai ? ( {generate.show && ai ? (
<GenerateInput <GenerateInput
user={userData} user={userData}
socket={socket} socket={socketRef.current}
width={generate.width - 90} width={generate.width - 90}
data={{ data={{
fileName: tabs.find((t) => t.id === activeFileId)?.name ?? "", fileName: tabs.find((t) => t.id === activeFileId)?.name ?? "",
@ -644,7 +654,7 @@ export default function CodeEditor({
handleRename={handleRename} handleRename={handleRename}
handleDeleteFile={handleDeleteFile} handleDeleteFile={handleDeleteFile}
handleDeleteFolder={handleDeleteFolder} handleDeleteFolder={handleDeleteFolder}
socket={socket} socket={socketRef.current}
setFiles={setFiles} setFiles={setFiles}
addNew={(name, type) => addNew(name, type, setFiles, sandboxData)} addNew={(name, type) => addNew(name, type, setFiles, sandboxData)}
deletingFolderId={deletingFolderId} deletingFolderId={deletingFolderId}
@ -764,6 +774,7 @@ export default function CodeEditor({
previewPanelRef.current?.expand() previewPanelRef.current?.expand()
setIsPreviewCollapsed(false) setIsPreviewCollapsed(false)
}} }}
src={previewURL}
/> />
</ResizablePanel> </ResizablePanel>
<ResizableHandle /> <ResizableHandle />
@ -776,7 +787,7 @@ export default function CodeEditor({
<Terminals <Terminals
terminals={terminals} terminals={terminals}
setTerminals={setTerminals} setTerminals={setTerminals}
socket={socket} socket={socketRef.current}
/> />
) : ( ) : (
<div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none"> <div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none">

View File

@ -15,9 +15,11 @@ import { toast } from "sonner"
export default function PreviewWindow({ export default function PreviewWindow({
collapsed, collapsed,
open, open,
src
}: { }: {
collapsed: boolean collapsed: boolean
open: () => void open: () => void
src: string
}) { }) {
const ref = useRef<HTMLIFrameElement>(null) const ref = useRef<HTMLIFrameElement>(null)
const [iframeKey, setIframeKey] = useState(0) const [iframeKey, setIframeKey] = useState(0)
@ -45,7 +47,7 @@ export default function PreviewWindow({
<PreviewButton <PreviewButton
onClick={() => { onClick={() => {
navigator.clipboard.writeText(`http://localhost:5173`) navigator.clipboard.writeText(src)
toast.info("Copied preview link to clipboard") toast.info("Copied preview link to clipboard")
}} }}
> >
@ -73,7 +75,7 @@ export default function PreviewWindow({
ref={ref} ref={ref}
width={"100%"} width={"100%"}
height={"100%"} height={"100%"}
src={`http://localhost:5173`} src={src}
/> />
</div> </div>
)} )}