GithubHelp home page GithubHelp logo

jimmywarting / ut_metadata Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webtorrent/ut_metadata

0.0 3.0 0.0 123 KB

BitTorrent Extension for Peers to Send Metadata Files (BEP 9)

Home Page: https://webtorrent.io

License: MIT License

JavaScript 100.00%

ut_metadata's Introduction

ut_metadata travis npm downloads javascript style guide

BitTorrent Extension for Peers to Send Metadata Files (BEP 9)

JavaScript implementation of the Extension for Peers to Send Metadata Files (BEP 9). Use with bittorrent-protocol.

The purpose of this extension is to allow clients to join a swarm and complete a download without the need of downloading a .torrent file first. This extension instead allows clients to download the metadata from peers. It makes it possible to support magnet links, a link on a web page only containing enough information to join the swarm (the info hash).

Works in the browser with browserify! This module is used by WebTorrent.

install

npm install ut_metadata

usage

This package should be used with bittorrent-protocol, which supports a plugin-like system for extending the protocol with additional functionality.

Say you're already using bittorrent-protocol. Your code might look something like this:

var Protocol = require('bittorrent-protocol')
var net = require('net')

net.createServer(function (socket) {
  var wire = new Protocol()
  socket.pipe(wire).pipe(socket)

  // handle handshake
  wire.on('handshake', function (infoHash, peerId) {
    wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
  })

}).listen(6881)

To add support for BEP 9, simply modify your code like this:

var Protocol = require('bittorrent-protocol')
var net = require('net')
var ut_metadata = require('ut_metadata')

net.createServer(function (socket) {
  var wire = new Protocol()
  socket.pipe(wire).pipe(socket)

  // initialize the extension
  wire.use(ut_metadata())

  // all `ut_metadata` functionality can now be accessed at wire.ut_metadata

  // ask the peer to send us metadata
  wire.ut_metadata.fetch()

  // 'metadata' event will fire when the metadata arrives and is verified to be correct!
  wire.ut_metadata.on('metadata', function (metadata) {
    // got metadata!

    // Note: the event will not fire if the peer does not support ut_metadata, if they
    // don't have metadata yet either, if they repeatedly send invalid data, or if they
    // simply don't respond.
  })

  // optionally, listen to the 'warning' event if you want to know that metadata is
  // probably not going to arrive for one of the above reasons.
  wire.ut_metadata.on('warning', function (err) {
    console.log(err.message)
  })

  // handle handshake
  wire.on('handshake', function (infoHash, peerId) {
    wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
  })

}).listen(6881)

api

ut_metadata([metadata])

Initialize the extension. If you have the torrent metadata (Buffer), pass it into the ut_metadata constructor so it's made available to the peer.

var metadata = fs.readFileSync(__dirname + '/file.torrent')
wire.use(ut_metadata(metadata))

ut_metadata.fetch()

Ask the peer to send metadata.

ut_metadata.cancel()

Stop asking the peer to send metadata.

ut_metadata.setMetadata(metadata)

Set the metadata. If you didn't have the metadata at the time ut_metadata was initialized, but you end up getting it from another peer (or somewhere else), you should call setMetadata so the metadata will be available to the peer.

ut_metadata.on('metadata', function (metadata) {})

Fired when metadata is available and verified to be correct. Called with a single parameter of type Buffer.

wire.ut_metadata.on('metadata', function (metadata) {
  console.log(Buffer.isBuffer(metadata)) // true
})

Note: the event will not fire if the peer does not support ut_metadata, if they don't have metadata yet either, if they repeatedly send invalid data, or if they simply don't respond.

ut_metadata.on('warning', function (err) {})

Fired if:

  • the peer does not support ut_metadata
  • the peer doesn't have metadata yet
  • the peer repeatedly sent invalid data
wire.ut_metadata.on('warning', function (err) {
  console.log(err.message)
})

license

MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.

ut_metadata's People

Contributors

diegorbaquero avatar feross avatar greenkeeper[bot] avatar greenkeeperio-bot avatar transitive-bullshit avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.