mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 16:02:26 +00:00
- Move from root publishing to clean `dist/` directory - Create `scripts/build-dist.mjs` to orchestrate build and copy assets - Update `package.json` build script - Update `netlify.toml` to publish `dist/` - Ignore `dist/` in `.gitignore` Co-authored-by: PR0M3TH3AN <[email protected]>
2.7 KiB
2.7 KiB
Embed Mode Notes and Recommendations
Current Behavior
The embed player (embed.html / js/embed.js) initializes a full NostrClient instance within its iframe context. This includes:
- Initializing
SimplePoolfromnostr-tools. - Connecting to default relays (or configured ones).
- Subscribing to metadata (Kind 0) for liveness check.
Duplicate Initialization Issue
When multiple embeds are present on a single parent page:
- Each iframe creates its own
NostrClientandSimplePool. - Each iframe opens its own WebSocket connections to the same set of relays.
- This results in
N \times Mconnections (N embeds, M relays), which can be resource-intensive and trigger rate limits.
Fixes Implemented
- Idempotent Initialization: Added guards in
js/embed.js(window.__bitvidEmbedStarted) andjs/nostr/client.js(this.isInitialized) to prevent accidental double-initialization within the same execution context. - Crash Fix: Added missing
resetStatsmethod toEmbedVideoModaland a safety guard inApplication.resetTorrentStatsto preventTypeError.
Recommended Long-Term Fixes
To solve the multi-iframe connection duplication, a mechanism to share the relay connection across Same-Origin iframes is required.
1. SharedWorker Relay Manager (Recommended)
Implement a SharedWorker (js/relayWorker.js) that acts as a singleton relay manager for the origin.
- Architecture:
- The worker holds the
SimplePooland WebSocket connections. - Embeds (and the main app) connect to the worker via
MessagePort. - The worker proxies subscription requests and events between the frames and the relays.
- The worker holds the
- Pros:
- Single connection per relay for the entire browser session/origin.
- Efficient resource usage.
- Cons:
- Requires refactoring
NostrClientto support a "Worker Relay Adapter" instead of directSimplePoolusage. - Browser support (SharedWorker not available in all contexts/browsers, fallback needed).
- Requires refactoring
2. BroadcastChannel Leader Election
Use BroadcastChannel to elect a "Leader" tab/frame that manages connections.
- Architecture:
- Frames join a channel.
- One frame becomes Leader.
- Followers request data via the channel.
- Pros:
- Works where SharedWorker might fail (though support is similar).
- Cons:
- Complex leader election/failover logic (what if leader frame closes?).
3. Embed-Specific Minimal Runtime
Create a stripped-down EmbedClient that only implements the subset of Nostr functionality needed for playback (fetch by ID/pointer), bypassing the heavier NostrClient if possible, or optimizing NostrClient to be lighter.