ut_metadata

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

Github星跟踪图

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:

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

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

  // handle handshake
  wire.on('handshake', (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:

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

net.createServer(socket => {
  const 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', 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', err => {
    console.log(err.message)
  })

  // handle handshake
  wire.on('handshake', (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.

const 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', 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', err => {
  console.log(err.message)
})

license

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

主要指标

概览
名称与所有者webtorrent/ut_metadata
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2014-03-22 09:39:36
推送于2025-05-17 23:45:13
最后一次提交2024-12-07 18:47:18
发布数51
最新版本名称v4.0.3 (发布于 )
第一版名称v1.0.0 (发布于 2014-03-22 21:38:29)
用户参与
星数112
关注者数11
派生数34
提交数274
已启用问题?
问题数17
打开的问题数2
拉请求数93
打开的拉请求数4
关闭的拉请求数12
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?