Add base code
This commit is contained in:
commit
b46bf94c21
9 changed files with 12059 additions and 0 deletions
1
.env.development
Normal file
1
.env.development
Normal file
|
@ -0,0 +1 @@
|
||||||
|
PORT=3002
|
2
.eslintignore
Normal file
2
.eslintignore
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
dist
|
||||||
|
node_modules
|
3
.eslintrc.json
Normal file
3
.eslintrc.json
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"extends": "@jitsi/eslint-config"
|
||||||
|
}
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.DS_Store
|
||||||
|
dist
|
||||||
|
node_modules
|
1
babel.config.json
Normal file
1
babel.config.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{}
|
11885
package-lock.json
generated
Normal file
11885
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
53
package.json
Normal file
53
package.json
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
{
|
||||||
|
"name": "excalidraw-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "src/index.js",
|
||||||
|
"description": "Excalidraw backend",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/jitsi/excalidraw-backend"
|
||||||
|
},
|
||||||
|
"private": true,
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/core": "7.16.0",
|
||||||
|
"@babel/eslint-parser": "7.16.0",
|
||||||
|
"@babel/plugin-proposal-export-default-from": "7.16.0",
|
||||||
|
"@babel/preset-env": "7.16.0",
|
||||||
|
"@babel/runtime": "7.16.0",
|
||||||
|
"@excalidraw/eslint-config": "1.0.1",
|
||||||
|
"@excalidraw/prettier-config": "1.0.2",
|
||||||
|
"@jitsi/eslint-config": "^4.1.0",
|
||||||
|
"@types/debug": "4.1.5",
|
||||||
|
"@types/express": "4.17.11",
|
||||||
|
"@types/node": "14.14.31",
|
||||||
|
"@types/socket.io": "2.1.4",
|
||||||
|
"@typescript-eslint/eslint-plugin": "5.30.5",
|
||||||
|
"@typescript-eslint/parser": "5.30.4",
|
||||||
|
"babel-loader": "8.2.3",
|
||||||
|
"babel-plugin-optional-require": "0.3.1",
|
||||||
|
"circular-dependency-plugin": "5.2.0",
|
||||||
|
"clean-css-cli": "4.3.0",
|
||||||
|
"copy-webpack-plugin": "^9.0.0",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"css-loader": "3.6.0",
|
||||||
|
"debug": "4.3.1",
|
||||||
|
"dotenv": "^10.0.0",
|
||||||
|
"eslint": "8.1.0",
|
||||||
|
"eslint-plugin-import": "2.25.2",
|
||||||
|
"eslint-plugin-jsdoc": "37.0.3",
|
||||||
|
"express": "4.17.1",
|
||||||
|
"socket.io": "^2.5.0",
|
||||||
|
"ts-node-dev": "^1.1.8",
|
||||||
|
"typescript": "4.2.3"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"start": "tsc && node dist/index.js",
|
||||||
|
"start:dev": "cross-env NODE_ENV=development ts-node-dev --respawn --transpile-only src/index.ts"
|
||||||
|
}
|
||||||
|
}
|
94
src/index.ts
Normal file
94
src/index.ts
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
import debug from "debug";
|
||||||
|
import express from "express";
|
||||||
|
import http from "http";
|
||||||
|
import socketIO from "socket.io";
|
||||||
|
|
||||||
|
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" },
|
||||||
|
);
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const port = process.env.PORT || 80; // default port to listen
|
||||||
|
|
||||||
|
app.use(express.static("public"));
|
||||||
|
|
||||||
|
app.get("/", (req, res) => {
|
||||||
|
res.send("Excalidraw backend is up :)");
|
||||||
|
});
|
||||||
|
|
||||||
|
const server = http.createServer(app);
|
||||||
|
|
||||||
|
server.listen(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();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
socket.on(
|
||||||
|
"server-volatile-broadcast",
|
||||||
|
(roomID: string, encryptedData: ArrayBuffer, iv: Uint8Array) => {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
socket.removeAllListeners();
|
||||||
|
});
|
||||||
|
});
|
17
tsconfig.json
Normal file
17
tsconfig.json
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"target": "es5",
|
||||||
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
|
"allowJs": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"strict": true,
|
||||||
|
"moduleResolution": "Node",
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"outDir": "dist"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue