mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-06 04:42:57 +00:00
fix(webtorrent): ensure magnet webseeds are used in stream
Extracts `ws` (web seed) parameters from the magnet link and merges them with the explicit `urlList` (e.g., from direct URL) when initializing the WebTorrent stream. This fixes a regression where magnet-embedded web seeds were ignored if a direct URL was provided. - Updates `js/magnets.js` to parse `ws` parameters and export `webSeedsFromMagnet`. - Updates `js/webtorrent.js` to merge magnet web seeds with `opts.urlList`. - Adds regression test `tests/webtorrent-webseeds.test.mjs`. Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
5b3db62513
commit
2bebaa6465
3 files changed
+113
-7
No files matched your search
@@ -0,0 +1,76 @@
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
// Setup globals required by webtorrent.js
|
||||
if (typeof self === 'undefined') global.self = global;
|
||||
if (typeof navigator === 'undefined') {
|
||||
global.navigator = {
|
||||
userAgent: 'test',
|
||||
serviceWorker: {
|
||||
register: async () => ({ active: true }),
|
||||
ready: Promise.resolve({ active: true }),
|
||||
controller: { postMessage: () => {} },
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
}
|
||||
};
|
||||
}
|
||||
if (typeof window === 'undefined') {
|
||||
global.window = {
|
||||
location: { hostname: 'localhost', origin: 'http://localhost' },
|
||||
isSecureContext: true,
|
||||
navigator: global.navigator
|
||||
}
|
||||
}
|
||||
if (typeof global.location === 'undefined') {
|
||||
global.location = global.window.location;
|
||||
}
|
||||
|
||||
// Import dynamic to ensure globals are set
|
||||
const { TorrentClient } = await import('../js/webtorrent.js');
|
||||
|
||||
class MockWebTorrent {
|
||||
constructor() {
|
||||
this.torrents = [];
|
||||
}
|
||||
add(magnet, opts, cb) {
|
||||
this.lastAdd = { magnet, opts };
|
||||
const torrent = {
|
||||
on: () => {},
|
||||
files: [{name: 'video.mp4', streamTo: () => {}}],
|
||||
once: () => {},
|
||||
destroy: () => {},
|
||||
};
|
||||
if (cb) cb(torrent);
|
||||
return torrent;
|
||||
}
|
||||
destroy() {}
|
||||
createServer() {}
|
||||
}
|
||||
|
||||
test("streamVideo merges magnet web seeds with explicit web seeds", async () => {
|
||||
const client = new TorrentClient({ webTorrentClass: MockWebTorrent });
|
||||
client.serviceWorkerDisabled = true;
|
||||
|
||||
const magnet = 'magnet:?xt=urn:btih:123&ws=http://magnet-seed.com/file.mp4';
|
||||
const urlList = ['http://direct-url.com/video.mp4'];
|
||||
|
||||
const videoElement = {
|
||||
play: async () => {},
|
||||
addEventListener: () => {},
|
||||
muted: false,
|
||||
crossOrigin: null // streamVideo sets this
|
||||
};
|
||||
|
||||
try {
|
||||
await client.streamVideo(magnet, videoElement, { urlList });
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
const lastAdd = client.client.lastAdd;
|
||||
|
||||
assert.ok(lastAdd.opts.urlList.includes('http://magnet-seed.com/file.mp4'), "Magnet web seed missing");
|
||||
assert.ok(lastAdd.opts.urlList.includes('http://direct-url.com/video.mp4'), "Explicit web seed missing");
|
||||
});
|
||||
Reference in new issue
Block a user