128 lines
3.1 KiB
TypeScript
Raw Normal View History

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-27 19:12:25 -04:00
import { 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-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
}
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-18 16:40:08 -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-28 20:06:47 -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
console.log("save file " + file.id + ": ", file.data)
await saveFile(fileId, body)
2024-04-27 16:41:25 -04:00
})
2024-04-27 19:12:25 -04:00
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
await renameFile(fileId, newName, 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 + ")")
terminals[id] = new Pty(socket, id)
})
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}`)
})