55 lines
2.2 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");
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: "*",
},
});
io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
const q = socket.handshake.query;
console.log("middleware");
console.log(q);
if (!q.userId || !q.sandboxId) {
next(new Error("Invalid request."));
}
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${q.userId}`);
const dbUserJSON = yield dbUser.json();
if (!dbUserJSON || !dbUserJSON.sandbox.includes(q.sandboxId)) {
next(new Error("Invalid credentials."));
}
next();
}));
io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
console.log(`connection`);
const userId = socket.handshake.query.userId;
console.log(userId);
// socket.emit("loaded", {
// rootContent: await fetchDir("/workspace", "")
// });
// initHandlers(socket, replId);
}));
httpServer.listen(port, () => {
console.log(`Server running on port ${port}`);
});