From b49e760bf3d6d755bed65d21673cf78fc52d8977 Mon Sep 17 00:00:00 2001 From: bobbydigitales Date: Sun, 18 May 2025 21:30:04 -0700 Subject: [PATCH] Split out the concept of headless and an archive peer. Add better logging for URL params --- src/Sync.ts | 11 ++++- src/main2.ts | 107 +++++++----------------------------------------- static/main2.js | 72 ++++---------------------------- 3 files changed, 31 insertions(+), 159 deletions(-) diff --git a/src/Sync.ts b/src/Sync.ts index 5cada9b..9580660 100644 --- a/src/Sync.ts +++ b/src/Sync.ts @@ -2,11 +2,13 @@ import { openDatabase, getData, addData, addDataArray, clearData, deleteData, me import { log, logID } from "log"; export class Sync { + isArchivePeer:boolean = false; userID: string = ""; userIDsToSync: Set = new Set(); - constructor() { + setArchive(isHeadless:boolean) { + this.isArchivePeer = isHeadless; } setUserID(userID: string) { @@ -14,7 +16,12 @@ export class Sync { } shouldSyncUserID(userID: string) { - return true; + let shouldSyncAllUsers = this.isArchivePeer; + if (shouldSyncAllUsers) { + return true; + } + + return this.userIDsToSync.has(userID); } // shouldSyncUserID(userID: string) { diff --git a/src/main2.ts b/src/main2.ts index a9c1fca..aaa6105 100644 --- a/src/main2.ts +++ b/src/main2.ts @@ -731,6 +731,7 @@ class App { posts: StoragePost[] = []; isHeadless: boolean = false; isBootstrapPeer: boolean = false; + isArchivePeer: boolean = false; showLog: boolean = false; markedAvailable = false; limitPosts = 50; @@ -740,7 +741,7 @@ class App { connectURL: string = ""; firstRun = false; peerManager: PeerManager | null = null; - sync = new Sync(); + sync:Sync = new Sync(); async connect() { this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer); @@ -1646,103 +1647,29 @@ class App { let urlParams = (new URL(window.location.href)).searchParams; if (urlParams.has('log')) { this.showInfo(); - } + } - this.isHeadless = /\bHeadlessChrome\//.test(navigator.userAgent); + this.isHeadless = /\bHeadlessChrome\//.test(navigator.userAgent) || urlParams.has('headless'); + this.isArchivePeer = urlParams.has('archive'); this.isBootstrapPeer = urlParams.has("bootstrap"); - if (this.isBootstrapPeer) { - console.log.apply(null, log(`This is a bootstrap peer`));; - } + console.log(`[headless]${this.isHeadless} [archive] ${this.isArchivePeer} [bootstrap] ${this.isBootstrapPeer}`); + + let limitPostsParam = urlParams.get('limitPosts'); + if (limitPostsParam) { + this.limitPosts = parseInt(limitPostsParam); + } + this.peerID = this.getPeerID(); this.peername = this.getPeername(); this.userID = this.getUserID(); this.username = this.getUsername(); - this.sync.setUserID(this.userID); + this.sync.setUserID(this.userID) + this.sync.setArchive(this.isArchivePeer); this.connect(); - // this.registerRPCs(); - // this.testPeerManager(); - - // let peer: RTCPeerConnection | null = null; - // // if (window.RTCPeerConnection) { - // peer = new RTCPeerConnection({ - // iceServers: [ - // { urls: "stun:ddln.app" }, - // // { urls: "turn:ddln.app", username: "a", credential: "b" }, - // { urls: "stun:stun.l.google.com" }, // keeping this for now as my STUN server is not return ipv6 - // // { urls: "stun:stun1.l.google.com" }, - // // { urls: "stun:stun2.l.google.com" }, - // // { urls: "stun:stun3.l.google.com" }, - // // { urls: "stun:stun4.l.google.com" }, - // ] - // }); - - // peer.createDataChannel('boop'); - - // peer.onicecandidate = ({ candidate }) => { log(`WRTC:${candidate?.address} ${candidate?.protocol} ${candidate?.type} ${(candidate as any)?.url}`) }; - // peer.onnegotiationneeded = async (event) => { - // console.log.apply(null, log("on negotiation needed fired"));; - - // let makingOffer = false; - - // try { - // makingOffer = true; - // await peer.setLocalDescription(); - - // let IDsToSync = this.following; - // if (this.router.route === App.Route.USER) { - // IDsToSync = new Set([this.router.userID]); - // } - - // if (!peer.localDescription) { - // return; - // } - - // // this.websocket = new wsConnection(this.userID, this.peerID, IDsToSync, peer.localDescription); - // // log(peer.localDescription.type + ":" + peer.localDescription.sdp); - // // this.initOffline(this.websocket); - - // // this.websocket?.sendWebRTCDescription(peer.localDescription); - // } catch (err) { - // console.error(err); - // } finally { - // makingOffer = false; - // } - - // } - - - - - // peer.createOffer().then((description)=>{ - // peer.setLocalDescription(description) - // console.log.apply(null, log("RTC: " + description.sdp + description.type));; - // }); - - // } - - // await this.exportPostsForUser('b38b623c-c3fa-4351-9cab-50233c99fa4e'); - - // Get initial state and route from URL and user agent etc - - // Set local state (userid etc) based on that. - - // Init libraries - - // Render - // Load all images async - // Start the process of figuring out what posts we need - // Download posts once all current images are loaded - - - // window.resizeTo(645, 900); - - // this.initLogo() - - this.getRoute(); if (this.router.route === App.Route.CONNECT) { @@ -1759,12 +1686,6 @@ class App { - this.isHeadless = urlParams.has('headless'); - - let limitPostsParam = urlParams.get('limitPosts'); - if (limitPostsParam) { - this.limitPosts = parseInt(limitPostsParam); - } let time = 0; let delta = 0; diff --git a/static/main2.js b/static/main2.js index b1c4dc5..1e2fb86 100644 --- a/static/main2.js +++ b/static/main2.js @@ -546,6 +546,7 @@ class App { this.posts = []; this.isHeadless = false; this.isBootstrapPeer = false; + this.isArchivePeer = false; this.showLog = false; this.markedAvailable = false; this.limitPosts = 50; @@ -1247,73 +1248,21 @@ class App { if (urlParams.has('log')) { this.showInfo(); } - this.isHeadless = /\bHeadlessChrome\//.test(navigator.userAgent); + this.isHeadless = /\bHeadlessChrome\//.test(navigator.userAgent) || urlParams.has('headless'); + this.isArchivePeer = urlParams.has('archive'); this.isBootstrapPeer = urlParams.has("bootstrap"); - if (this.isBootstrapPeer) { - console.log.apply(null, log(`This is a bootstrap peer`)); - ; + console.log(`[headless]${this.isHeadless} [archive] ${this.isArchivePeer} [bootstrap] ${this.isBootstrapPeer}`); + let limitPostsParam = urlParams.get('limitPosts'); + if (limitPostsParam) { + this.limitPosts = parseInt(limitPostsParam); } this.peerID = this.getPeerID(); this.peername = this.getPeername(); this.userID = this.getUserID(); this.username = this.getUsername(); this.sync.setUserID(this.userID); + this.sync.setArchive(this.isArchivePeer); this.connect(); - // this.registerRPCs(); - // this.testPeerManager(); - // let peer: RTCPeerConnection | null = null; - // // if (window.RTCPeerConnection) { - // peer = new RTCPeerConnection({ - // iceServers: [ - // { urls: "stun:ddln.app" }, - // // { urls: "turn:ddln.app", username: "a", credential: "b" }, - // { urls: "stun:stun.l.google.com" }, // keeping this for now as my STUN server is not return ipv6 - // // { urls: "stun:stun1.l.google.com" }, - // // { urls: "stun:stun2.l.google.com" }, - // // { urls: "stun:stun3.l.google.com" }, - // // { urls: "stun:stun4.l.google.com" }, - // ] - // }); - // peer.createDataChannel('boop'); - // peer.onicecandidate = ({ candidate }) => { log(`WRTC:${candidate?.address} ${candidate?.protocol} ${candidate?.type} ${(candidate as any)?.url}`) }; - // peer.onnegotiationneeded = async (event) => { - // console.log.apply(null, log("on negotiation needed fired"));; - // let makingOffer = false; - // try { - // makingOffer = true; - // await peer.setLocalDescription(); - // let IDsToSync = this.following; - // if (this.router.route === App.Route.USER) { - // IDsToSync = new Set([this.router.userID]); - // } - // if (!peer.localDescription) { - // return; - // } - // // this.websocket = new wsConnection(this.userID, this.peerID, IDsToSync, peer.localDescription); - // // log(peer.localDescription.type + ":" + peer.localDescription.sdp); - // // this.initOffline(this.websocket); - // // this.websocket?.sendWebRTCDescription(peer.localDescription); - // } catch (err) { - // console.error(err); - // } finally { - // makingOffer = false; - // } - // } - // peer.createOffer().then((description)=>{ - // peer.setLocalDescription(description) - // console.log.apply(null, log("RTC: " + description.sdp + description.type));; - // }); - // } - // await this.exportPostsForUser('b38b623c-c3fa-4351-9cab-50233c99fa4e'); - // Get initial state and route from URL and user agent etc - // Set local state (userid etc) based on that. - // Init libraries - // Render - // Load all images async - // Start the process of figuring out what posts we need - // Download posts once all current images are loaded - // window.resizeTo(645, 900); - // this.initLogo() this.getRoute(); if (this.router.route === App.Route.CONNECT) { console.log.apply(null, log('connect', this.router.userID)); @@ -1323,11 +1272,6 @@ class App { await this.initDB(); this.connectURL = `${document.location.origin}/connect/${this.userID}`; document.getElementById('connectURL').innerHTML = `connect`; - this.isHeadless = urlParams.has('headless'); - let limitPostsParam = urlParams.get('limitPosts'); - if (limitPostsParam) { - this.limitPosts = parseInt(limitPostsParam); - } let time = 0; let delta = 0; // let isPersisted = await navigator?.storage?.persisted();