This commit is contained in:
2025-04-13 15:10:07 -07:00
parent 5cd701ca2b
commit aade48a7b8
18 changed files with 3185 additions and 299 deletions

View File

@@ -7,6 +7,8 @@
// import { serveDir } from "jsr:@std/http/file-server"
import { brotli } from "jsr:@deno-library/compress";
const memoryCache = true;
const filepathResponseCache: Map<string, Response> = new Map();
@@ -30,9 +32,15 @@ async function serveFile(filename: string) {
const file = await Deno.readFile("../" + filename);
const newResponse = new Response(file);
const compressed = await brotli.compress(file);
const newResponse = await new Response(compressed);
// const newResponse = await new Response(file);
newResponse.headers.set('Cache-Control', 'no-transform');
newResponse.headers.set('Content-Encoding', 'br');
if (filename.endsWith('.js')) {
newResponse.headers.set('content-type', 'application/javascript')
@@ -89,6 +97,15 @@ interface HelloMessage {
known_users: string[]
}
interface Hello2Message {
type: string
user_id: string
user_name: string
peer_id: string
peer_name: string
is_bootstrap_peer: boolean
// peer_description:RTCSessionDescription
}
// interface PeerState {
// socket:WebSocket;
@@ -96,6 +113,7 @@ interface HelloMessage {
// }
// const peerStates:Map<string, PeerState> = new Map();
const bootstrapPeers: Set<string> = new Set();
const userPeers: Map<string, Set<string>> = new Map();
const peerSockets: Map<string, WebSocket> = new Map();
const socketPeers: Map<WebSocket, string> = new Map();
@@ -104,6 +122,31 @@ const socketPeers: Map<WebSocket, string> = new Map();
// }
function hello2Handler(m:Hello2Message, socket:WebSocket) {
// bootstrapPeers.add(m.)
console.log(`[>hello2]: peer ${colorID(m.peer_id)}:${m.peer_name}, user ${colorID(m.user_id)}:${m.user_name}`);
if (m.is_bootstrap_peer) {
bootstrapPeers.add(m.peer_id);
console.log(`This is a bootstrap peer. bootstrap peers:${[...bootstrapPeers.values()]}`);
}
if (!userPeers.has(m.user_id)) {
userPeers.set(m.user_id, new Set());
}
userPeers.get(m.user_id)?.add(m.peer_id);
peerSockets.set(m.peer_id, socket); // TODO:MAYBEBUG - what happens with multiple windows each with their own websocket?
socketPeers.set(socket, m.peer_id);
if (!m.is_bootstrap_peer) {
return JSON.stringify({ type: 'hello2', bootstrapPeers: [...bootstrapPeers.values()] });
}
return JSON.stringify({type:'hello2', message:'hello2 bootstrap peer!'});
}
function helloHandler(m: HelloMessage, socket: WebSocket) {
console.log(`Received hello from peer ${colorID(m.peer_id)}:${m.peer_name}, user ${colorID(m.user_id)}:${m.user_name}`);
@@ -233,7 +276,8 @@ function connectWebsocket(request: Request) {
return;
}
console.log("Websocket close:", colorID(peerID), `code:${event.code} reason:${event.reason} wasClean: ${event.wasClean}`);
bootstrapPeers.delete(peerID);
peerSockets.delete(peerID);
deletePeerFromUserPeers(peerID);
});
@@ -303,6 +347,7 @@ async function main() {
messageDispatch.set('ping', pingHandler);
messageDispatch.set('hello', helloHandler);
messageDispatch.set('hello2', hello2Handler);
messageDispatch.set('peer_message', peerMessageHandler);
Deno.serve({