81 lines
3.0 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 });
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-26 02:10:37 -04:00
const getSandboxFiles_1 = __importDefault(require("./getSandboxFiles"));
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-21 22:55:49 -04:00
const handshakeSchema = zod_1.z.object({
userId: zod_1.z.string(),
sandboxId: zod_1.z.string(),
type: zod_1.z.enum(["node", "react"]),
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;
console.log("middleware");
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 02:10:37 -04:00
const { sandboxId, userId, type } = parseQuery.data;
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${userId}`);
2024-04-21 22:55:49 -04:00
const dbUserJSON = (yield dbUser.json());
console.log("dbUserJSON:", dbUserJSON);
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,
type,
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-26 02:10:37 -04:00
const sandboxFiles = yield (0, getSandboxFiles_1.default)(data.id);
// fetch all file data TODO
2024-04-18 16:40:08 -04:00
// socket.emit("loaded", {
// rootContent: await fetchDir("/workspace", "")
// });
// initHandlers(socket, replId);
}));
httpServer.listen(port, () => {
console.log(`Server running on port ${port}`);
});