Split out the concept of headless and an archive peer. Add better logging for URL params

This commit is contained in:
2025-05-18 21:30:04 -07:00
parent b46c600d75
commit b49e760bf3
3 changed files with 31 additions and 159 deletions

View File

@@ -2,11 +2,13 @@ import { openDatabase, getData, addData, addDataArray, clearData, deleteData, me
import { log, logID } from "log"; import { log, logID } from "log";
export class Sync { export class Sync {
isArchivePeer:boolean = false;
userID: string = ""; userID: string = "";
userIDsToSync: Set<string> = new Set(); userIDsToSync: Set<string> = new Set();
constructor() { setArchive(isHeadless:boolean) {
this.isArchivePeer = isHeadless;
} }
setUserID(userID: string) { setUserID(userID: string) {
@@ -14,7 +16,12 @@ export class Sync {
} }
shouldSyncUserID(userID: string) { shouldSyncUserID(userID: string) {
return true; let shouldSyncAllUsers = this.isArchivePeer;
if (shouldSyncAllUsers) {
return true;
}
return this.userIDsToSync.has(userID);
} }
// shouldSyncUserID(userID: string) { // shouldSyncUserID(userID: string) {

View File

@@ -731,6 +731,7 @@ class App {
posts: StoragePost[] = []; posts: StoragePost[] = [];
isHeadless: boolean = false; isHeadless: boolean = false;
isBootstrapPeer: boolean = false; isBootstrapPeer: boolean = false;
isArchivePeer: boolean = false;
showLog: boolean = false; showLog: boolean = false;
markedAvailable = false; markedAvailable = false;
limitPosts = 50; limitPosts = 50;
@@ -740,7 +741,7 @@ class App {
connectURL: string = ""; connectURL: string = "";
firstRun = false; firstRun = false;
peerManager: PeerManager | null = null; peerManager: PeerManager | null = null;
sync = new Sync(); sync:Sync = new Sync();
async connect() { async connect() {
this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer); this.peerManager = new PeerManager(this.userID, this.peerID, this.isBootstrapPeer);
@@ -1648,10 +1649,15 @@ class App {
this.showInfo(); 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"); 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.peerID = this.getPeerID();
@@ -1659,90 +1665,11 @@ class App {
this.userID = this.getUserID(); this.userID = this.getUserID();
this.username = this.getUsername(); this.username = this.getUsername();
this.sync.setUserID(this.userID); this.sync.setUserID(this.userID)
this.sync.setArchive(this.isArchivePeer);
this.connect(); 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(); this.getRoute();
if (this.router.route === App.Route.CONNECT) { 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 time = 0;
let delta = 0; let delta = 0;

View File

@@ -546,6 +546,7 @@ class App {
this.posts = []; this.posts = [];
this.isHeadless = false; this.isHeadless = false;
this.isBootstrapPeer = false; this.isBootstrapPeer = false;
this.isArchivePeer = false;
this.showLog = false; this.showLog = false;
this.markedAvailable = false; this.markedAvailable = false;
this.limitPosts = 50; this.limitPosts = 50;
@@ -1247,73 +1248,21 @@ class App {
if (urlParams.has('log')) { if (urlParams.has('log')) {
this.showInfo(); 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"); this.isBootstrapPeer = urlParams.has("bootstrap");
if (this.isBootstrapPeer) { console.log(`[headless]${this.isHeadless} [archive] ${this.isArchivePeer} [bootstrap] ${this.isBootstrapPeer}`);
console.log.apply(null, log(`This is a bootstrap peer`)); let limitPostsParam = urlParams.get('limitPosts');
; if (limitPostsParam) {
this.limitPosts = parseInt(limitPostsParam);
} }
this.peerID = this.getPeerID(); this.peerID = this.getPeerID();
this.peername = this.getPeername(); this.peername = this.getPeername();
this.userID = this.getUserID(); this.userID = this.getUserID();
this.username = this.getUsername(); this.username = this.getUsername();
this.sync.setUserID(this.userID); this.sync.setUserID(this.userID);
this.sync.setArchive(this.isArchivePeer);
this.connect(); 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(); this.getRoute();
if (this.router.route === App.Route.CONNECT) { if (this.router.route === App.Route.CONNECT) {
console.log.apply(null, log('connect', this.router.userID)); console.log.apply(null, log('connect', this.router.userID));
@@ -1323,11 +1272,6 @@ class App {
await this.initDB(); await this.initDB();
this.connectURL = `${document.location.origin}/connect/${this.userID}`; this.connectURL = `${document.location.origin}/connect/${this.userID}`;
document.getElementById('connectURL').innerHTML = `<a href="${this.connectURL}">connect</a>`; document.getElementById('connectURL').innerHTML = `<a href="${this.connectURL}">connect</a>`;
this.isHeadless = urlParams.has('headless');
let limitPostsParam = urlParams.get('limitPosts');
if (limitPostsParam) {
this.limitPosts = parseInt(limitPostsParam);
}
let time = 0; let time = 0;
let delta = 0; let delta = 0;
// let isPersisted = await navigator?.storage?.persisted(); // let isPersisted = await navigator?.storage?.persisted();