{
  "name": "peer-wire-protocol",
  "version": "0.7.0",
  "description": "Stream implementing the peer wire protocol used in bittorrent",
  "author": {
    "name": "Mathias Buus Madsen",
    "email": "mathiasbuus@gmail.com"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/mafintosh/peer-wire-protocol.git"
  },
  "keywords": [
    "bittorrent",
    "torrent",
    "protocol",
    "stream",
    "peer",
    "wire"
  ],
  "scripts": {
    "test": "node tests"
  },
  "dependencies": {
    "bitfield": "^0.1.0",
    "readable-stream": "^1.0.2",
    "bncode": "^0.2.3",
    "speedometer": "^0.1.2"
  },
  "readme": "# Peer Wire Protocol\n\npeer-wire-protocol is a node stream implementation of the [peer wire protocol specification](https://wiki.theory.org/BitTorrentSpecification#Peer_wire_protocol_.28TCP.29).\nThe protocol is the main communication layer when transferring files using BitTorrent and is used by [peerflix](https://github.com/mafintosh/peerflix).\n\nIt is available through npm:\n\n\tnpm install peer-wire-protocol\n\n[![build status](https://secure.travis-ci.org/mafintosh/peer-wire-protocol.png)](http://travis-ci.org/mafintosh/peer-wire-protocol)\n\n## Usage is simple\n\nSince the protocol is implemented as a stream all you have to do is pipe some to and from it\n\n``` js\nvar pwp = require('peer-wire-protocol');\nvar net = require('net');\n\nnet.createServer(function(socket) {\n\tvar wire = pwp();\n\n\t// pipe to and from the protocol\n\tsocket.pipe(wire).pipe(socket);\n\n\twire.on('handshake', function(infoHash, peerId) {\n\t\t// lets emit a handshake of our own as well\n\t\twire.handshake(new Buffer('my info hash'), new Buffer('my peer id'));\n\t});\n\n\twire.on('unchoke', function() {\n\t\tconsole.log('peer is no longer choking us: '+wire.peerChoking);\n\t});\n}).listen(6881);\n```\n\n## Full API\n\n### Handshaking\n\nSend and receive a handshake from the peer. This is the first message.\n\n``` js\n// send a handshake to the peer\nwire.handshake(infoHash, peerId, {dht:true});\nwire.on('handshake', function(infoHash, peerId, extensions) {\n\t// receive a handshake\n});\n```\n\nBoth the `infoHash` and the `peerId` should be 20 bytes\n\n### Choking\n\nCheck if you or the peer is choking\n\n``` js\nwire.peerChoking; // is the peer choking us?\nwire.amChoking; // are we choking the peer?\n\nwire.on('choke', function() {\n\t// the peer is now choking us\n});\nwire.on('unchoke', function() {\n\t// peer is no longer choking us\n});\n```\n\n### Interested\n\nSee if you or the peer is interested\n\n``` js\nwire.peerInterested; // is the peer interested in us?\nwire.amInterested; // are we interested in the peer?\n\nwire.on('interested', function() {\n\t// peer is now interested\n});\nwire.on('uninterested', function() {\n\t// peer is no longer interested\n});\n```\n\n### Bitfield\n\nExchange piece information with the peer\n\n``` js\n// send a bitfield to the peer\nwire.bitfield(buffer);\nwire.on('bitfield', function(bitfield) {\n\t// bitfield received from the peer\n});\n\n// send a have message indicating that you have a piece\nwire.have(pieceIndex);\nwire.on('have', function(pieceIndex) {\n\t// peer has sent you a have message\n});\n```\n\nYou can always see which pieces the peer have\n\n``` js\nwire.peerPieces[i]; // returns true if peer has piece i\n```\n\n### Requests\n\nSend and respond to requests for pieces\n\n``` js\n// request a piece from a peer\nwire.request(pieceIndex, offset, length, function(err, block) {\n\tif (err) {\n\t\t// there was an error (peer has started choking us etc)\n\t\treturn;\n\t}\n\t// got piece\n});\n\n// cancel a request to a peer\nwire.cancel(pieceIndex, offset, length);\n\n// receive a request from a peer\nwire.on('request', function(pieceIndex, offset, length, callback) {\n\t// ... read piece ...\n\tcallback(null, piece); // respond back to the peer\n});\n\nwire.requests;     // list of requests we currently have pending {piece, offset, length}\nwire.peerRequests; // list of requests the peer currently have pending {piece, offset, length}\n```\n\nYou can set a request timeout if you want to\n\n``` js\nwire.setTimeout(5000); // head request should take a most 5s to finish\n```\n\nIf the timeout is triggered the request callback is called with an error and a `timeout` event is emitted.\n\n### DHT and port\n\nYou can set the extensions flag `dht` in the handshake to `true` if you participate in the torrent dht.\nAfterwards you can send your dht port\n\n``` js\n// send your port to the peer\nwire.port(dhtPort);\nwire.on('port', function(dhtPort) {\n\t// peer has sent a port to us\n});\n```\n\n### Keep-Alive\n\nYou can enable the keep-alive ping (triggered every 60s)\n\n``` js\n// starts the keep alive\nwire.setKeepAlive(true);\nwire.on('keep-alive', function() {\n\t// peer sent a keep alive - just ignore it\n});\n```\n\n### Transfer stats\n\nCheck how many bytes you have uploaded and download\n\n``` js\nwire.uploaded; // number of bytes uploaded\nwire.downloaded; // number of bytes downloaded\n\nwire.on('download', function(numberOfBytes) {\n\t...\n});\nwire.on('upload', function(numberOfBytes) {\n\t...\n});\n```\n\n## License\n\nMIT\n",
  "readmeFilename": "README.md",
  "bugs": {
    "url": "https://github.com/mafintosh/peer-wire-protocol/issues"
  },
  "homepage": "https://github.com/mafintosh/peer-wire-protocol",
  "_id": "peer-wire-protocol@0.7.0",
  "_shasum": "6c015abf24b4877ed9eca3822b22d996078011da",
  "_from": "peer-wire-protocol@^0.7.0",
  "_resolved": "https://registry.npmjs.org/peer-wire-protocol/-/peer-wire-protocol-0.7.0.tgz"
}
