From 98353d910a11e5cc90b4ed86bca558586cb787d5 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Fri, 11 Nov 2022 00:18:29 +0100 Subject: feat: esm --- .gitignore | 1 + README.md | 24 ++-- docs/api.md | 2 +- docs/faq.md | 2 +- docs/get-started.md | 31 ++--- docs/tutorials.md | 52 +++---- index.js | 46 +++---- lib/conn-pool.js | 18 ++- lib/file-stream.js | 8 +- lib/file.js | 24 ++-- lib/peer.js | 250 +++++++++++++++++----------------- lib/rarity-map.js | 4 +- lib/server.js | 16 +-- lib/torrent.js | 80 +++++------ lib/utp.js | 4 +- lib/webconn.js | 20 +-- lib/worker-server.js | 4 +- lib/worker.js | 2 +- package.json | 16 ++- sw.min.js | 2 +- test/browser/basic.js | 4 +- test/client-add-duplicate-trackers.js | 6 +- test/client-add.js | 6 +- test/client-destroy.js | 6 +- test/client-remove.js | 6 +- test/client-seed.js | 6 +- test/common.js | 10 +- test/duplicate.js | 6 +- test/node/basic.js | 12 +- test/node/blocklist-dht.js | 12 +- test/node/blocklist-tracker.js | 12 +- test/node/blocklist.js | 12 +- test/node/conn-pool.js | 10 +- test/node/download-dht-magnet.js | 16 +-- test/node/download-dht-torrent.js | 14 +- test/node/download-from-ip.js | 10 +- test/node/download-lsd-magnet.js | 8 +- test/node/download-lsd-torrent.js | 8 +- test/node/download-metadata.js | 10 +- test/node/download-private-dht.js | 12 +- test/node/download-tracker-magnet.js | 14 +- test/node/download-tracker-torrent.js | 14 +- test/node/download-webseed-magnet.js | 18 +-- test/node/download-webseed-torrent.js | 18 +-- test/node/extensions.js | 6 +- test/node/limit-download-upload.js | 8 +- test/node/limit-methods.js | 8 +- test/node/metadata.js | 6 +- test/node/seed-stream.js | 10 +- test/node/seed-while-download.js | 14 +- test/node/server.js | 10 +- test/node/torrent-events.js | 10 +- test/rarity-map.js | 10 +- test/swarm.js | 6 +- test/torrent-destroy.js | 6 +- webtorrent.chromeapp.js | 76 +++-------- webtorrent.min.js | 53 ++++--- 57 files changed, 527 insertions(+), 552 deletions(-) diff --git a/.gitignore b/.gitignore index d829fed..2c7a4c8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ package-lock.json webtorrent.debug.js sw.debug.js +pnpm-lock.yaml diff --git a/README.md b/README.md index e310294..5804ea9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ JavaScript™. Note: WebTorrent does **not** support UDP/TCP peers in browse Simply include the [`webtorrent.min.js`](https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js) script on your page to start fetching files over WebRTC using the BitTorrent protocol, or -`require('webtorrent')` with [browserify](http://browserify.org/). See [demo apps +`import Webtorrent from 'webtorrent'` with [browserify](http://browserify.org/). See [demo apps ](#who-is-using-webtorrent-today) and [code examples](#usage) below. [![jsdelivr download count](https://data.jsdelivr.com/v1/package/npm/webtorrent/badge)](https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js) @@ -91,7 +91,7 @@ they can connect to both normal *and* web peers. We hope other clients will foll ### Install -To install WebTorrent for use in node or the browser with `require('webtorrent')`, run: +To install WebTorrent for use in node or the browser with `import Webtorrent from 'webtorrent'`, run: ```bash npm install webtorrent @@ -133,7 +133,7 @@ standards (no plugins, just HTML5 and WebRTC)! It's easy to get started! ##### Downloading a file is simple: ```js -const WebTorrent = require('webtorrent') +import WebTorrent from 'webtorrent' const client = new WebTorrent() const magnetURI = '...' @@ -151,8 +151,8 @@ client.add(magnetURI, function (torrent) { ##### Seeding a file is simple, too: ```js -const dragDrop = require('drag-drop') -const WebTorrent = require('webtorrent') +import dragDrop from 'drag-drop' +import WebTorrent from 'webtorrent' const client = new WebTorrent() @@ -186,7 +186,7 @@ bundler. However, webpack requires the following extra configuration: ``` Or, you can just use the pre-built version via -`require('webtorrent/webtorrent.min.js')` and skip the webpack configuration. +`import 'webtorrent/webtorrent.min.js'` and skip the webpack configuration. ##### Script tag @@ -195,14 +195,18 @@ WebTorrent is also available as a standalone script object, so it can be used with just a script tag: ```html - + ``` The WebTorrent script is also hosted on fast, reliable CDN infrastructure (Cloudflare and MaxCDN) for easy inclusion on your site: ```html - + ``` ##### Chrome App @@ -212,7 +216,9 @@ If you want to use WebTorrent in a following script: ```html - + ``` Be sure to enable the `chrome.sockets.udp` and `chrome.sockets.tcp` permissions! diff --git a/docs/api.md b/docs/api.md index 78e98e2..6bd20c9 100644 --- a/docs/api.md +++ b/docs/api.md @@ -599,7 +599,7 @@ node.js-style duplex stream to the remote peer. This event can be used to specif Here is a usage example: ```js -const MyExtension = require('./my-extension') +import MyExtension from './my-extension' torrent1.on('wire', function (wire, addr) { console.log('connected to peer with address ' + addr) diff --git a/docs/faq.md b/docs/faq.md index 2b791cb..3b7075a 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -250,7 +250,7 @@ clients like Transmission, and uTorrent to add support for WebTorrent. **Vuze** To start using WebTorrent, simply include the [`webtorrent.min.js`](https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js) script on your page. If you use [browserify](http://browserify.org/), you can -`npm install webtorrent` and `require('webtorrent')`. +`npm install webtorrent` and `import Webtorrent from 'webtorrent'`. It's easy to download a torrent and add it to the page. diff --git a/docs/get-started.md b/docs/get-started.md index fbf20e8..c9bc2b7 100644 --- a/docs/get-started.md +++ b/docs/get-started.md @@ -6,15 +6,15 @@ to get started! ## Install To start using WebTorrent, simply include the -[`webtorrent.min.js`](https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js) +[`webtorrent`](https://esm.sh/webtorrent) script on your page. ```html - + ``` -This provides a `WebTorrent` function on the `window` object. - ### Browserify WebTorrent also works great with [browserify](http://browserify.org/), which lets @@ -28,7 +28,7 @@ npm install webtorrent Then use `WebTorrent` like this: ```js -const WebTorrent = require('webtorrent') +import Webtorrent from 'webtorrent' ``` ## Quick Examples @@ -36,7 +36,7 @@ const WebTorrent = require('webtorrent') ### Downloading a torrent (in the browser) ```js -const WebTorrent = require('webtorrent') +import Webtorrent from 'webtorrent' const client = new WebTorrent() @@ -58,9 +58,7 @@ client.add(torrentId, function (torrent) { }) ``` -This supports video, audio, images, PDFs, Markdown, [and more][render-media], right -out of the box. There are additional ways to access file content directly, including -as a node-style stream, Buffer, or Blob URL. +This supports video, audio, images, PDFs, Markdown, right out of the box. There are additional ways to access file content directly, including as a node-style stream, Buffer, or Blob URL. Video and audio content can be streamed, i.e. playback will start before the full file is downloaded. Seeking works too – WebTorrent dynamically fetches @@ -71,8 +69,8 @@ the needed torrent pieces from the network on-demand. ### Creating a new torrent and seed it (in the browser) ```js -const dragDrop = require('drag-drop') -const WebTorrent = require('webtorrent') +import dragDrop from 'drag-drop' +import Webtorrent from 'webtorrent' const client = new WebTorrent() @@ -94,7 +92,7 @@ This exports a `DragDrop` function on `window`. ### Download and save a torrent (in Node.js) ```js -const WebTorrent = require('webtorrent') +import WebTorrent from 'webtorrent' const client = new WebTorrent() @@ -148,10 +146,10 @@ downloaded.