npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loadjs-torrent

v3.1.0

Published

An extension of loadjs that supports torrents

Downloads

17

Readme

TorrentJs

Greenkeeper badge

Build Status JavaScript Style Guide npm

An extension of LoadJS that uses torrents instead of http links.

Users will download assets via WebTorrent, and seed those files immediately. This allows for easy and automatic peer to peer load balancing of a site's JS and CSS files.

Installation

npm install loadjs-torrent

Usage

let torrentJs = require('loadjs-torrent')

// magnet link to individual css/js file or torrent folder
let magnetLink = 'magnet:?xt=urn:btih:0067a9ede2fa07e9d0374713bd4621b447292c62&dn=windowString.js&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openWebTorrent.com'

// all JS and CSS files will be loaded from the torrent, including all subdirectories
// other file extensions are ignored
torrentJs(magnetLink)
  .then(() => {
    console.log('torrent loaded successfully!')
    // logic for after the script loads
  })
  .catch((err) => {
    console.log('Error: ' + err)
    // error handling
  })

Or use the minified file from this repository. Be sure to use the cdn link on rawgit for production apps.

<script src="https://rawgit.com/KayleePop/loadjs-torrent/master/loadjs-torrent.min.js"></script>
<script>
// magnet link to individual css/js file or torrent folder
let magnetLink = 'magnet:?xt=urn:btih:0067a9ede2fa07e9d0374713bd4621b447292c62&dn=windowString.js&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openWebTorrent.com'

// all JS and CSS files will be loaded from the torrent, including all subdirectories
// other file extensions are ignored
torrentJs(magnetLink)
  .then(() => {
    console.log('torrent loaded successfully!')
    // logic for after the script loads
  })
  .catch((err) => {
    console.log('Error: ' + err)
    // error handling
  })
</script>

API

async torrentJs(magnetLink, opts)

Returns:

A Promise that resolves on successful loading of all files

Options

opts contains these defaults:

{
  path: '/**', // a glob to specify a subset of files to load
  async: true, // whether to load files asynchronously
  client: new WebTorrent(), // WebTorrent client to use
  before(path, scriptEl) {} // called before DOM insertion of files
}

Path

Default: '/**'

A glob pattern matching the files you wish to load from the torrent. Only .js and .css files that match the glob will be loaded, other filetypes and un-matched files will be ignored.

Matched files will be highly prioritized, but other files in the torrent will also be downloaded and seeded.

Examples:
torrentJs(magnetLink, {path: '/folder/otherFolder/*.js'})

torrentJs(magnetLink, {path: '**/*.js'}) // loads all javascript files

torrentJs(magnetlink, {path: '/dir/**'}) // loads all JS and CSS from dir

Async

Default: true

A boolean stating whether execution order matters for the loaded scripts.

If true, scripts will be loaded in the order that they finish downloading, and the async attribute will be applied, like this:

<script src="..." async></script>

If false, scripts will be loaded in order as each one finishes downloading and will execute in order without the async attribute. If one script takes a very long time to load, then it will block the scripts after it but not before it.

The order is based on the torrent, and it can be customized on creation with a Bittorrent client. Custom ordering is possible with Vuze.

Client

Default: new WebTorrent()

The WebTorrent client instance to use.

Creating a new WebTorrent client for every file creates unnecessary overhead, so you can pass in an existing one for torrentJs to use instead.

Example:
let WebTorrent = require('webtorrent')
let torrentClient = new WebTorrent()

torrentJs(magnetLink, {path: 'script.js', client: torrentClient})

torrentJs(magnetLink, {path: 'otherScript.js', client: torrentClient})

torrentJs(otherMagnetLink, {client: torrentClient})

Before

Default: (path, scriptEl) => {}

A function that is passed directly into loadjs. It executes just before the script or style tag is created. DOM insertion can be customized using the before callback as well.

See the loadjs docs for more info

The path param will be a blobUrl pointing to the downloaded file.

Feel free to open an issue with questions

Development

Tests

Run tests like this:

npm test

Make sure you are seeding the test fixtures before testing. Use a WebTorrent compatible torrent client.

Build

Build and minify like so:

npm run build