2018-01-29 11:09:40 +00:00
# ut_metadata [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
2017-09-29 06:23:28 +01:00
2017-04-14 06:44:24 +01:00
[travis-image]: https://img.shields.io/travis/webtorrent/ut_metadata/master.svg
[travis-url]: https://travis-ci.org/webtorrent/ut_metadata
2016-02-05 22:32:49 +00:00
[npm-image]: https://img.shields.io/npm/v/ut_metadata.svg
2015-01-22 23:10:49 +00:00
[npm-url]: https://npmjs.org/package/ut_metadata
2016-02-05 22:32:49 +00:00
[downloads-image]: https://img.shields.io/npm/dm/ut_metadata.svg
2015-01-22 23:10:49 +00:00
[downloads-url]: https://npmjs.org/package/ut_metadata
2017-03-21 03:52:14 +00:00
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com
2014-03-23 04:38:04 +00:00
2014-03-27 07:53:15 +00:00
### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
2014-03-23 04:38:04 +00:00
2017-04-14 16:55:08 +01:00
JavaScript implementation of the [Extension for Peers to Send Metadata Files (BEP 9) ](http://www.bittorrent.org/beps/bep_0009.html ). Use with [bittorrent-protocol ](https://www.npmjs.com/package/bittorrent-protocol ).
2014-03-23 04:38:04 +00:00
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 ](http://browserify.org/ )! This module is used by [WebTorrent ](http://webtorrent.io ).
2014-06-04 05:17:43 +01:00
### install
2014-03-23 04:38:04 +00:00
```
npm install ut_metadata
```
2014-06-04 05:17:43 +01:00
### usage
2014-03-23 04:38:04 +00:00
2017-04-14 06:44:24 +01:00
This package should be used with [bittorrent-protocol ](https://www.npmjs.com/package/bittorrent-protocol ), which supports a plugin-like system for extending the protocol with additional functionality.
2014-03-23 04:38:04 +00:00
Say you're already using `bittorrent-protocol` . Your code might look something like this:
```js
var Protocol = require('bittorrent-protocol')
var net = require('net')
net.createServer(function (socket) {
2014-03-23 04:41:45 +00:00
var wire = new Protocol()
socket.pipe(wire).pipe(socket)
2014-03-23 04:38:04 +00:00
// handle handshake
2014-03-23 04:41:45 +00:00
wire.on('handshake', function (infoHash, peerId) {
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
})
2014-03-23 04:38:04 +00:00
}).listen(6881)
```
To add support for BEP 9, simply modify your code like this:
```js
2014-03-23 06:48:26 +00:00
var Protocol = require('bittorrent-protocol')
var net = require('net')
var ut_metadata = require('ut_metadata')
2014-03-23 04:38:04 +00:00
net.createServer(function (socket) {
var wire = new Protocol()
socket.pipe(wire).pipe(socket)
// initialize the extension
wire.use(ut_metadata())
2014-03-26 09:03:13 +00:00
// all `ut_metadata` functionality can now be accessed at wire.ut_metadata
2014-03-23 04:38:04 +00:00
// ask the peer to send us metadata
2014-03-26 09:03:13 +00:00
wire.ut_metadata.fetch()
2014-03-23 04:38:04 +00:00
// 'metadata' event will fire when the metadata arrives and is verified to be correct!
2014-03-26 09:03:13 +00:00
wire.ut_metadata.on('metadata', function (metadata) {
2014-03-23 04:42:49 +00:00
// got metadata!
2014-03-23 04:38:04 +00:00
// 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.
2014-03-26 09:03:13 +00:00
wire.ut_metadata.on('warning', function (err) {
2014-03-23 04:38:04 +00:00
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)
```
2014-06-04 05:17:43 +01:00
### api
2014-03-23 04:38:04 +00:00
2014-06-04 05:17:43 +01:00
#### `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.
```js
var metadata = fs.readFileSync(__dirname + '/file.torrent')
wire.use(ut_metadata(metadata))
```
#### `ut_metadata.fetch()`
2014-03-23 04:38:04 +00:00
Ask the peer to send metadata.
2014-06-04 05:17:43 +01:00
#### `ut_metadata.cancel()`
2014-03-23 04:38:04 +00:00
Stop asking the peer to send metadata.
2014-06-04 05:17:43 +01:00
#### `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) {})`
2014-03-23 04:38:04 +00:00
Fired when metadata is available and verified to be correct. Called with a single
parameter of type Buffer.
```js
2014-03-26 09:03:13 +00:00
wire.ut_metadata.on('metadata', function (metadata) {
2014-03-23 04:38:04 +00:00
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.
2014-06-04 05:17:43 +01:00
#### `ut_metadata.on('warning', function (err) {})`
2014-03-23 04:38:04 +00:00
Fired if:
- the peer does not support ut_metadata
- the peer doesn't have metadata yet
2014-06-04 05:30:09 +01:00
- the peer repeatedly sent invalid data
2014-03-23 04:38:04 +00:00
```js
2014-03-26 09:03:13 +00:00
wire.ut_metadata.on('warning', function (err) {
2014-03-23 04:38:04 +00:00
console.log(err.message)
})
```
2014-06-04 05:17:43 +01:00
### license
2014-03-23 04:38:04 +00:00
2017-04-14 18:31:48 +01:00
MIT. Copyright (c) [Feross Aboukhadijeh ](https://feross.org ) and [WebTorrent, LLC ](https://webtorrent.io ).