Peer Wire Protocol
peer-wire-protocol is a node stream implementation of the peer wire protocol specification. The protocol is the main communication layer when transferring files using BitTorrent and is used by peerflix.
It is available through npm:
npm install peer-wire-protocol
Usage is simple
Since the protocol is implemented as a stream all you have to do is pipe some to and from it
var pwp = ;var net = ;net;
Full API
Handshaking
Send and receive a handshake from the peer. This is the first message.
// send a handshake to the peerwire;wire;
Both the infoHash
and the peerId
should be 20 bytes
Choking
Check if you or the peer is choking
wirepeerChoking; // is the peer choking us?wireamChoking; // are we choking the peer?wire;wire;
Interested
See if you or the peer is interested
wirepeerInterested; // is the peer interested in us?wireamInterested; // are we interested in the peer?wire;wire;
Bitfield
Exchange piece information with the peer
// send a bitfield to the peerwire;wire;// send a have message indicating that you have a piecewire;wire;
You can always see which pieces the peer have
wirepeerPiecesi; // returns true if peer has piece i
Requests
Send and respond to requests for pieces
// request a piece from a peerwire;// cancel a request to a peerwire;// receive a request from a peerwire;wirerequests; // list of requests we currently have pending {piece, offset, length}wirepeerRequests; // list of requests the peer currently have pending {piece, offset, length}
You can set a request timeout if you want to
wire; // head request should take a most 5s to finish
If the timeout is triggered the request callback is called with an error and a timeout
event is emitted.
DHT and port
You can set the extensions flag dht
in the handshake to true
if you participate in the torrent dht.
Afterwards you can send your dht port
// send your port to the peerwireportdhtPort;wire;
Keep-Alive
You can enable the keep-alive ping (triggered every 60s)
// starts the keep alivewire;wire;
Transfer stats
Check how many bytes you have uploaded and download
wireuploaded; // number of bytes uploadedwiredownloaded; // number of bytes downloadedwire;wire;
License
MIT