252 lines
9.5 KiB
JavaScript
Raw Normal View History

2024-04-18 16:40:08 -04:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
2024-04-29 00:50:25 -04:00
const fs_1 = __importDefault(require("fs"));
2024-04-29 02:19:27 -04:00
const os_1 = __importDefault(require("os"));
2024-04-29 00:50:25 -04:00
const path_1 = __importDefault(require("path"));
2024-04-18 16:40:08 -04:00
const express_1 = __importDefault(require("express"));
const dotenv_1 = __importDefault(require("dotenv"));
const http_1 = require("http");
const socket_io_1 = require("socket.io");
2024-04-21 22:55:49 -04:00
const zod_1 = require("zod");
2024-04-27 14:23:09 -04:00
const utils_1 = require("./utils");
2024-04-29 02:19:27 -04:00
const node_pty_1 = require("node-pty");
2024-05-05 14:33:09 -07:00
const ratelimit_1 = require("./ratelimit");
2024-04-18 16:40:08 -04:00
dotenv_1.default.config();
const app = (0, express_1.default)();
const port = process.env.PORT || 4000;
// app.use(cors())
const httpServer = (0, http_1.createServer)(app);
const io = new socket_io_1.Server(httpServer, {
cors: {
origin: "*",
},
});
2024-04-28 20:06:47 -04:00
const terminals = {};
2024-04-29 00:50:25 -04:00
const dirName = path_1.default.join(__dirname, "..");
2024-04-21 22:55:49 -04:00
const handshakeSchema = zod_1.z.object({
userId: zod_1.z.string(),
sandboxId: zod_1.z.string(),
EIO: zod_1.z.string(),
transport: zod_1.z.string(),
});
2024-04-18 16:40:08 -04:00
io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
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 = yield fetch(`http://localhost:8787/api/user?id=${userId}`);
2024-04-21 22:55:49 -04:00
const dbUserJSON = (yield dbUser.json());
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-05-03 14:58:56 -07:00
const sharedSandboxes = dbUserJSON.usersToSandboxes.find((uts) => uts.sandboxId === sandboxId);
if (!sandbox && !sharedSandboxes) {
2024-04-21 22:55:49 -04:00
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-18 16:40:08 -04:00
}
2024-04-26 02:10:37 -04:00
socket.data = {
userId,
sandboxId: sandboxId,
2024-04-21 22:55:49 -04:00
};
2024-04-18 16:40:08 -04:00
next();
}));
io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
2024-04-21 22:55:49 -04:00
const data = socket.data;
const sandboxFiles = yield (0, utils_1.getSandboxFiles)(data.sandboxId);
2024-04-29 00:50:25 -04:00
sandboxFiles.fileData.forEach((file) => {
const filePath = path_1.default.join(dirName, file.id);
fs_1.default.mkdirSync(path_1.default.dirname(filePath), { recursive: true });
fs_1.default.writeFile(filePath, file.data, function (err) {
if (err)
throw err;
});
});
socket.emit("loaded", sandboxFiles.files);
2024-04-27 00:20:17 -04:00
socket.on("getFile", (fileId, callback) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
callback(file.data);
});
2024-04-27 19:12:25 -04:00
// todo: send diffs + debounce for efficiency
socket.on("saveFile", (fileId, body) => __awaiter(void 0, void 0, void 0, function* () {
2024-05-05 14:33:09 -07:00
try {
yield ratelimit_1.saveFileRL.consume(data.userId, 1);
if (Buffer.byteLength(body, "utf-8") > ratelimit_1.MAX_BODY_SIZE) {
socket.emit("rateLimit", "Rate limited: file size too large. Please reduce the file size.");
return;
}
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
file.data = body;
fs_1.default.writeFile(path_1.default.join(dirName, file.id), body, function (err) {
if (err)
throw err;
});
yield (0, utils_1.saveFile)(fileId, body);
}
catch (e) {
socket.emit("rateLimit", "Rate limited: file saving. Please slow down.");
}
2024-04-27 19:12:25 -04:00
}));
2024-04-29 00:50:25 -04:00
socket.on("createFile", (name) => __awaiter(void 0, void 0, void 0, function* () {
2024-05-05 14:33:09 -07:00
try {
yield ratelimit_1.createFileRL.consume(data.userId, 1);
const id = `projects/${data.sandboxId}/${name}`;
2024-05-05 14:33:09 -07:00
fs_1.default.writeFile(path_1.default.join(dirName, id), "", function (err) {
if (err)
throw err;
});
sandboxFiles.files.push({
id,
name,
type: "file",
});
sandboxFiles.fileData.push({
id,
data: "",
});
yield (0, utils_1.createFile)(id);
}
catch (e) {
socket.emit("rateLimit", "Rate limited: file creation. Please slow down.");
}
2024-04-29 00:50:25 -04:00
}));
2024-04-27 14:23:09 -04:00
socket.on("renameFile", (fileId, newName) => __awaiter(void 0, void 0, void 0, function* () {
2024-05-05 14:33:09 -07:00
try {
yield ratelimit_1.renameFileRL.consume(data.userId, 1);
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
file.id = newName;
const parts = fileId.split("/");
const newFileId = parts.slice(0, parts.length - 1).join("/") + "/" + newName;
fs_1.default.rename(path_1.default.join(dirName, fileId), path_1.default.join(dirName, newFileId), function (err) {
if (err)
throw err;
});
yield (0, utils_1.renameFile)(fileId, newFileId, file.data);
}
catch (e) {
socket.emit("rateLimit", "Rate limited: file renaming. Please slow down.");
2024-04-27 14:23:09 -04:00
return;
2024-05-05 14:33:09 -07:00
}
2024-04-27 14:23:09 -04:00
}));
2024-04-30 01:56:43 -04:00
socket.on("deleteFile", (fileId, callback) => __awaiter(void 0, void 0, void 0, function* () {
2024-05-05 14:33:09 -07:00
try {
yield ratelimit_1.deleteFileRL.consume(data.userId, 1);
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
fs_1.default.unlink(path_1.default.join(dirName, fileId), function (err) {
if (err)
throw err;
});
sandboxFiles.fileData = sandboxFiles.fileData.filter((f) => f.id !== fileId);
yield (0, utils_1.deleteFile)(fileId);
const newFiles = yield (0, utils_1.getSandboxFiles)(data.sandboxId);
2024-05-05 14:33:09 -07:00
callback(newFiles.files);
}
catch (e) {
socket.emit("rateLimit", "Rate limited: file deletion. Please slow down.");
}
2024-04-30 01:56:43 -04:00
}));
2024-04-28 20:06:47 -04:00
socket.on("createTerminal", ({ id }) => {
2024-05-05 14:33:09 -07:00
if (terminals[id]) {
console.log("Terminal already exists.");
return;
}
if (Object.keys(terminals).length >= 4) {
console.log("Too many terminals.");
return;
}
2024-04-29 02:19:27 -04:00
const pty = (0, node_pty_1.spawn)(os_1.default.platform() === "win32" ? "cmd.exe" : "bash", [], {
name: "xterm",
cols: 100,
cwd: path_1.default.join(dirName, "projects", data.sandboxId),
2024-04-29 02:19:27 -04:00
});
2024-04-29 21:36:33 -04:00
const onData = pty.onData((data) => {
2024-04-29 02:19:27 -04:00
socket.emit("terminalResponse", {
// data: Buffer.from(data, "utf-8").toString("base64"),
data,
});
});
2024-04-29 21:36:33 -04:00
const onExit = pty.onExit((code) => console.log("exit :(", code));
2024-04-30 22:48:36 -04:00
pty.write("clear\r");
2024-04-29 21:36:33 -04:00
terminals[id] = {
terminal: pty,
onData,
onExit,
};
2024-04-28 20:06:47 -04:00
});
2024-04-29 02:19:27 -04:00
socket.on("terminalData", (id, data) => {
2024-04-28 20:06:47 -04:00
if (!terminals[id]) {
console.log("terminals", terminals);
return;
}
2024-04-29 02:19:27 -04:00
try {
2024-04-29 21:36:33 -04:00
terminals[id].terminal.write(data);
2024-04-29 02:19:27 -04:00
}
catch (e) {
console.log("Error writing to terminal", e);
}
2024-04-28 20:06:47 -04:00
});
2024-05-03 00:52:01 -07:00
socket.on("generateCode", (fileName, code, line, instructions, callback) => __awaiter(void 0, void 0, void 0, function* () {
const fetchPromise = fetch(`http://localhost:8787/api/sandbox/generate`, {
2024-05-03 00:52:01 -07:00
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
userId: data.userId,
2024-05-03 00:52:01 -07:00
}),
});
const generateCodePromise = (0, utils_1.generateCode)({
fileName,
code,
line,
instructions,
});
const [fetchResponse, generateCodeResponse] = yield Promise.all([
fetchPromise,
generateCodePromise,
]);
const json = yield generateCodeResponse.json();
2024-05-03 00:52:01 -07:00
callback(json);
}));
2024-04-29 21:36:33 -04:00
socket.on("disconnect", () => {
Object.entries(terminals).forEach((t) => {
const { terminal, onData, onExit } = t[1];
if (os_1.default.platform() !== "win32")
terminal.kill();
onData.dispose();
onExit.dispose();
delete terminals[t[0]];
});
});
2024-04-18 16:40:08 -04:00
}));
httpServer.listen(port, () => {
console.log(`Server running on port ${port}`);
});