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 });
|
|
|
|
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-28 20:06:47 -04:00
|
|
|
const terminal_1 = require("./terminal");
|
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-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;
|
|
|
|
}
|
2024-04-26 21:57:30 -04:00
|
|
|
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-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-18 16:40:08 -04:00
|
|
|
}
|
2024-04-26 02:10:37 -04:00
|
|
|
socket.data = {
|
|
|
|
id: sandboxId,
|
|
|
|
userId,
|
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;
|
2024-04-27 14:23:09 -04:00
|
|
|
const sandboxFiles = yield (0, utils_1.getSandboxFiles)(data.id);
|
2024-04-26 21:57:30 -04:00
|
|
|
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;
|
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", (fileId, body) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
file.data = body;
|
|
|
|
console.log("save file " + file.id + ": ", file.data);
|
|
|
|
yield (0, utils_1.saveFile)(fileId, body);
|
|
|
|
}));
|
2024-04-27 14:23:09 -04:00
|
|
|
socket.on("renameFile", (fileId, newName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
file.id = newName;
|
2024-04-27 19:12:25 -04:00
|
|
|
yield (0, utils_1.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 }) => {
|
|
|
|
console.log("creating terminal (" + id + ")");
|
|
|
|
terminals[id] = new terminal_1.Pty(socket, id);
|
|
|
|
});
|
|
|
|
socket.on("terminalData", ({ id, data }) => {
|
|
|
|
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}`);
|
|
|
|
});
|