webtorrent / webtorrent-hybrid

WebTorrent (with WebRTC support in Node.js)
https://webtorrent.io
MIT License
516 stars 98 forks source link

TypeScript support #148

Closed ForestJohnson closed 2 years ago

ForestJohnson commented 2 years ago

What version of this package are you using?

5.0.1

What problem do you want to solve?

I want to be able to use this package easily in a TypeScript node.js project (I am using the ts-node package)

What do you think is the correct solution to this problem?

I don't know enough about typescript and javascript modules to answer this fully.

Are you willing to submit a pull request to implement this change?

No, I am creating this issue for documentation purposes to help the next hapless person who tries to do this.

I ended up solving my issue this way:


// webtorrent-hybrid doesn't seem to work with typescript :(
//import WebTorrent from 'webtorrent-hybrid';

// This is a hack to get our webtorrent instance to be like webtorrent-hybrid 
// (support webRTC from inside node.js)
// I did this because I could not get webtorrent-hybrid to work with typescript.
import "./configureWebTorrentForNode.js"

import WebTorrent from 'webtorrent';

where configureWebTorrentForNode.js contains:

// this code was ripped from https://github.com/webtorrent/webtorrent-hybrid/blob/3b5d9bb0a21fceef76f25a113ebede40fce73338/lib/global.js

// original code was:
/*
  import { announceList } from 'create-torrent'
  import wrtc from 'wrtc'

  globalThis.WEBTORRENT_ANNOUNCE = announceList
    .map(arr => arr[0])
    .filter(url => url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0)

  globalThis.WRTC = wrtc
*/

globalThis.WRTC = require('wrtc');

globalThis.WEBTORRENT_ANNOUNCE = [
  ['wss://tracker.btorrent.xyz'],
  ['wss://tracker.openwebtorrent.com']
];
github-actions[bot] commented 2 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?