diff --git a/package-lock.json b/package-lock.json index 8a6cf7e..edfb3b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,7 @@ }, "devDependencies": { "@jitsi/eslint-config": "^4.1.0", + "@types/dotenv": "^8.2.0", "@typescript-eslint/eslint-plugin": "5.30.5", "@typescript-eslint/parser": "5.30.4", "eslint": "8.1.0", @@ -620,6 +621,16 @@ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz", "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==" }, + "node_modules/@types/dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==", + "deprecated": "This is a stub types definition. dotenv provides its own type definitions, so you do not need this installed.", + "dev": true, + "dependencies": { + "dotenv": "*" + } + }, "node_modules/@types/express": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", @@ -5282,6 +5293,15 @@ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz", "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==" }, + "@types/dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==", + "dev": true, + "requires": { + "dotenv": "*" + } + }, "@types/express": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.11.tgz", diff --git a/package.json b/package.json index cc56a38..6cb909c 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "devDependencies": { "@jitsi/eslint-config": "^4.1.0", + "@types/dotenv": "^8.2.0", "@typescript-eslint/eslint-plugin": "5.30.5", "@typescript-eslint/parser": "5.30.4", "eslint": "8.1.0", diff --git a/src/index.ts b/src/index.ts index 998beb4..e722496 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,100 +1,101 @@ -import debug from "debug"; -import express from "express"; -import http from "http"; -import socketIO from "socket.io"; -import * as prometheus from "socket.io-prometheus-metrics"; +import debug from 'debug'; +import dotenv from 'dotenv'; +import express from 'express'; +import http from 'http'; +import socketIO from 'socket.io'; +import * as prometheus from 'socket.io-prometheus-metrics'; -const serverDebug = debug("server"); -const ioDebug = debug("io"); -const socketDebug = debug("socket"); +const serverDebug = debug('server'); +const ioDebug = debug('io'); +const socketDebug = debug('socket'); -require("dotenv").config( - process.env.NODE_ENV !== "development" - ? { path: ".env.production" } - : { path: ".env.development" }, +dotenv.config( + process.env.NODE_ENV === 'development' + ? { path: '.env.development' } + : { path: '.env.production' } ); const app = express(); const port = process.env.PORT || 80; // default port to listen -app.use(express.static("public")); +app.use(express.static('public')); -app.get("/", (req, res) => { - res.send("Excalidraw backend is up :)"); +app.get('/', (req, res) => { + res.send('Excalidraw backend is up :)'); }); const server = http.createServer(app); server.listen(port, () => { - serverDebug(`listening on port: ${port}`); + serverDebug(`listening on port: ${port}`); }); const io = socketIO(server, { - handlePreflightRequest: (req, res) => { - const headers = { - "Access-Control-Allow-Headers": "Content-Type, Authorization", - "Access-Control-Allow-Origin": - (req.header && req.header.origin) || "https://meet.jit.si", - "Access-Control-Allow-Credentials": true, - }; - res.writeHead(200, headers); - res.end(); - }, + handlePreflightRequest: (req, res) => { + const headers = { + 'Access-Control-Allow-Headers': 'Content-Type, Authorization', + 'Access-Control-Allow-Origin': req.header?.origin ?? 'https://meet.jit.si', + 'Access-Control-Allow-Credentials': true + }; + + res.writeHead(200, headers); + res.end(); + } }); // listens on host:9090/metrics prometheus.metrics(io, { - collectDefaultMetrics: true + collectDefaultMetrics: true }); -io.on("connection", (socket) => { - ioDebug("connection established!"); - io.to(`${socket.id}`).emit("init-room"); - socket.on("join-room", (roomID) => { - socketDebug(`${socket.id} has joined ${roomID}`); - socket.join(roomID); - if (io.sockets.adapter.rooms[roomID].length <= 1) { - io.to(`${socket.id}`).emit("first-in-room"); - } else { - socket.broadcast.to(roomID).emit("new-user", socket.id); +io.on('connection', socket => { + ioDebug('connection established!'); + io.to(`${socket.id}`).emit('init-room'); + socket.on('join-room', roomID => { + socketDebug(`${socket.id} has joined ${roomID}`); + socket.join(roomID); + if (io.sockets.adapter.rooms[roomID].length <= 1) { + io.to(`${socket.id}`).emit('first-in-room'); + } else { + socket.broadcast.to(roomID).emit('new-user', socket.id); + } + io.in(roomID).emit( + 'room-user-change', + Object.keys(io.sockets.adapter.rooms[roomID].sockets) + ); + }); + + socket.on( + 'server-broadcast', + (roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => { + socketDebug(`${socket.id} sends update to ${roomID}`); + socket.broadcast.to(roomID).emit('client-broadcast', encryptedData, iv); } - io.in(roomID).emit( - "room-user-change", - Object.keys(io.sockets.adapter.rooms[roomID].sockets), ); - }); - socket.on( - "server-broadcast", + socket.on( + 'server-volatile-broadcast', (roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => { - socketDebug(`${socket.id} sends update to ${roomID}`); - socket.broadcast.to(roomID).emit("client-broadcast", encryptedData, iv); - }, - ); - - socket.on( - "server-volatile-broadcast", - (roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => { - socketDebug(`${socket.id} sends volatile update to ${roomID}`); - socket.volatile.broadcast + socketDebug(`${socket.id} sends volatile update to ${roomID}`); + socket.volatile.broadcast .to(roomID) - .emit("client-broadcast", encryptedData, iv); - }, - ); - - socket.on("disconnecting", () => { - const rooms = io.sockets.adapter.rooms; - for (const roomID in socket.rooms) { - const clients = Object.keys(rooms[roomID].sockets).filter( - (id) => id !== socket.id, - ); - if (clients.length > 0) { - socket.broadcast.to(roomID).emit("room-user-change", clients); - } + .emit('client-broadcast', encryptedData, iv); } - }); + ); - socket.on("disconnect", () => { - socket.removeAllListeners(); - }); + socket.on('disconnecting', () => { + const rooms = io.sockets.adapter.rooms; + + for (const roomID of Object.keys(socket.rooms)) { + const clients = Object.keys(rooms[roomID].sockets).filter(id => id !== socket.id); + + if (clients.length > 0) { + socket.broadcast.to(roomID).emit('room-user-change', clients); + } + } + }); + + socket.on('disconnect', () => { + socket.removeAllListeners(); + }); });