2024-04-29 00:50:25 -04:00
|
|
|
import fs from "fs"
|
|
|
|
import path from "path"
|
2024-04-18 16:40:08 -04:00
|
|
|
import express, { Express, NextFunction, Request, Response } from "express"
|
|
|
|
import dotenv from "dotenv"
|
|
|
|
import { createServer } from "http"
|
|
|
|
import { Server } from "socket.io"
|
|
|
|
|
|
|
|
import { z } from "zod"
|
2024-04-21 22:55:49 -04:00
|
|
|
import { User } from "./types"
|
2024-04-29 00:50:25 -04:00
|
|
|
import { createFile, getSandboxFiles, renameFile, saveFile } from "./utils"
|
2024-04-28 20:06:47 -04:00
|
|
|
import { Pty } from "./terminal"
|
2024-04-18 16:40:08 -04:00
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
const app: Express = express()
|
|
|
|
const port = process.env.PORT || 4000
|
|
|
|
// app.use(cors())
|
|
|
|
const httpServer = createServer(app)
|
|
|
|
const io = new Server(httpServer, {
|
|
|
|
cors: {
|
|
|
|
origin: "*",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-04-28 20:06:47 -04:00
|
|
|
const terminals: { [id: string]: Pty } = {}
|
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
const dirName = path.join(__dirname, "..")
|
|
|
|
|
2024-04-21 22:55:49 -04:00
|
|
|
const handshakeSchema = z.object({
|
|
|
|
userId: z.string(),
|
|
|
|
sandboxId: z.string(),
|
|
|
|
EIO: z.string(),
|
|
|
|
transport: z.string(),
|
|
|
|
})
|
|
|
|
|
2024-04-18 16:40:08 -04:00
|
|
|
io.use(async (socket, next) => {
|
|
|
|
const q = socket.handshake.query
|
2024-04-21 22:55:49 -04:00
|
|
|
const parseQuery = handshakeSchema.safeParse(q)
|
|
|
|
|
|
|
|
if (!parseQuery.success) {
|
|
|
|
console.log("Invalid request.")
|
2024-04-18 16:40:08 -04:00
|
|
|
next(new Error("Invalid request."))
|
2024-04-21 22:55:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
const { sandboxId, userId } = parseQuery.data
|
2024-04-26 02:10:37 -04:00
|
|
|
const dbUser = await fetch(`http://localhost:8787/api/user?id=${userId}`)
|
2024-04-21 22:55:49 -04:00
|
|
|
const dbUserJSON = (await dbUser.json()) as User
|
|
|
|
|
|
|
|
if (!dbUserJSON) {
|
|
|
|
console.log("DB error.")
|
|
|
|
next(new Error("DB error."))
|
|
|
|
return
|
2024-04-18 16:40:08 -04:00
|
|
|
}
|
|
|
|
|
2024-04-26 02:10:37 -04:00
|
|
|
const sandbox = dbUserJSON.sandbox.find((s) => s.id === sandboxId)
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-04-21 22:55:49 -04:00
|
|
|
if (!sandbox) {
|
|
|
|
console.log("Invalid credentials.")
|
2024-04-18 16:40:08 -04:00
|
|
|
next(new Error("Invalid credentials."))
|
2024-04-21 22:55:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-26 02:10:37 -04:00
|
|
|
socket.data = {
|
|
|
|
id: sandboxId,
|
|
|
|
userId,
|
2024-04-18 16:40:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
io.on("connection", async (socket) => {
|
2024-04-21 22:55:49 -04:00
|
|
|
const data = socket.data as {
|
|
|
|
userId: string
|
2024-04-26 02:10:37 -04:00
|
|
|
id: string
|
2024-04-21 22:55:49 -04:00
|
|
|
}
|
|
|
|
|
2024-04-26 02:10:37 -04:00
|
|
|
const sandboxFiles = await getSandboxFiles(data.id)
|
2024-04-29 00:50:25 -04:00
|
|
|
sandboxFiles.fileData.forEach((file) => {
|
|
|
|
const filePath = path.join(dirName, file.id)
|
|
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
|
|
|
fs.writeFile(filePath, file.data, function (err) {
|
|
|
|
if (err) throw err
|
|
|
|
// console.log("Saved File:", file.id)
|
|
|
|
})
|
|
|
|
})
|
2024-04-18 16:40:08 -04:00
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
socket.emit("loaded", sandboxFiles.files)
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-04-27 00:20:17 -04:00
|
|
|
socket.on("getFile", (fileId: string, callback) => {
|
|
|
|
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
|
|
|
|
if (!file) return
|
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
// console.log("get file " + file.id + ": ", file.data.slice(0, 10) + "...")
|
2024-04-27 00:20:17 -04:00
|
|
|
callback(file.data)
|
|
|
|
})
|
2024-04-27 19:12:25 -04:00
|
|
|
|
|
|
|
// todo: send diffs + debounce for efficiency
|
|
|
|
socket.on("saveFile", async (fileId: string, body: string) => {
|
|
|
|
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
|
|
|
|
if (!file) return
|
|
|
|
file.data = body
|
2024-04-29 00:50:25 -04:00
|
|
|
// console.log("save file " + file.id + ": ", file.data)
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
fs.writeFile(path.join(dirName, file.id), body, function (err) {
|
|
|
|
if (err) throw err
|
|
|
|
})
|
2024-04-27 19:12:25 -04:00
|
|
|
await saveFile(fileId, body)
|
2024-04-27 16:41:25 -04:00
|
|
|
})
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
socket.on("createFile", async (name: string) => {
|
|
|
|
const id = `projects/${data.id}/${name}`
|
|
|
|
console.log("create file", id, name)
|
|
|
|
|
|
|
|
fs.writeFile(path.join(dirName, id), "", function (err) {
|
|
|
|
if (err) throw err
|
|
|
|
})
|
|
|
|
|
|
|
|
sandboxFiles.files.push({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type: "file",
|
|
|
|
})
|
|
|
|
|
|
|
|
sandboxFiles.fileData.push({
|
|
|
|
id,
|
|
|
|
data: "",
|
|
|
|
})
|
|
|
|
|
|
|
|
await createFile(id)
|
|
|
|
})
|
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
socket.on("renameFile", async (fileId: string, newName: string) => {
|
|
|
|
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
|
|
|
|
if (!file) return
|
|
|
|
file.id = newName
|
2024-04-27 19:12:25 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
const parts = fileId.split("/")
|
|
|
|
const newFileId = parts.slice(0, parts.length - 1).join("/") + "/" + newName
|
|
|
|
|
|
|
|
fs.rename(
|
|
|
|
path.join(dirName, fileId),
|
|
|
|
path.join(dirName, newFileId),
|
|
|
|
function (err) {
|
|
|
|
if (err) throw err
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await renameFile(fileId, newFileId, file.data)
|
2024-04-27 14:23:09 -04:00
|
|
|
})
|
2024-04-28 20:06:47 -04:00
|
|
|
|
|
|
|
socket.on("createTerminal", ({ id }: { id: string }) => {
|
|
|
|
console.log("creating terminal (" + id + ")")
|
2024-04-29 00:50:25 -04:00
|
|
|
terminals[id] = new Pty(socket, id, `/projects/${data.id}`)
|
2024-04-28 20:06:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("terminalData", ({ id, data }: { id: string; data: string }) => {
|
|
|
|
console.log(`Received data for terminal ${id}: ${data}`)
|
|
|
|
|
|
|
|
if (!terminals[id]) {
|
|
|
|
console.log("terminal not found")
|
|
|
|
console.log("terminals", terminals)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Writing to terminal ${id}`)
|
|
|
|
terminals[id].write(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("disconnect", () => {})
|
2024-04-18 16:40:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
httpServer.listen(port, () => {
|
|
|
|
console.log(`Server running on port ${port}`)
|
|
|
|
})
|