webtorrent / webtorrent

⚡️ Streaming torrent client for the web

Home Page:https://webtorrent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems with createServer

mitra42 opened this issue · comments

What version of this package are you using?
2.0.18

What operating system, Node.js, and npm version?
OSX - node 18.2.1 but not being used npm is 9.2.0 though I'm using yarn 1.22.19 to insteall
Testing in Chrome 112.0.5615.137 and Firefox 112.0.2

What happened?
I'm trying to switch over from WebTorrent 1.x.x to WebTorrent 2.x.x which is non trivial due to a number of breaking changes.

WebTorrent is being run inside a webComponent, and was working fine in this way with WT 1.x.x

I couldn't get the import WebTorrent from 'webtorrent' to work because of the challenge of all the dependencies and importMaps. (waiting on response on the Discord), but in the meantime am loading with
import WebTorrent from '/node_modules/webtorrent/dist/webtorrent.min.js';

The problem comes with the change to how WT renders - I was using client.renderTo which calls render-media, however render-media is no longer in webTorrent as far as I can see, and can't be easily added because its not ESM. If I read the docs correctly it now requires client.createServer before file.streamTo

I see two examples of how to call createServer, both of which are failing

METHOD A:

      navigator.serviceWorker.register('/node_modules/webtorrent/dist/sw.min.js')
      console.log("XXX loadContent checking if ready");
      navigator.serviceWorker.ready.then(
        (controller) => {
          console.log("XXX loadContent SW ready");
          WTclient.createServer({controller})
        },

The register is fetching the sw.min.js from the server, but the Promise never resolves - to confirm this, I gave it a bad URL and got the expected error.

METHOD B:

      const instance = WTclient.createServer(); // No arguments
      instance.server.listen(0);

But this fails with `

Uncaught Error: Invalid worker registration
    at Tt.createServer (index.js:181:78)

All I'm trying to do is the equivalent of the old renderTo (which goes to render.render in render-media), so I'm open to alternative ways to pass the torrent to the video element, but I think this experience above suggests some kind of bug.

Are you willing to submit a pull request to fix this bug?
Would be if I could figure out what was happening !