bittorrent-protocol
Advanced tools
Comparing version 0.4.0 to 0.4.1
112
index.js
@@ -213,3 +213,3 @@ module.exports = Wire | ||
Wire.prototype.cancel = function (i, offset, length) { | ||
this._callback(this._pull(this.requests, i, offset, length), new Error('request was cancelled'), null) | ||
this._callback(pull(this.requests, i, offset, length), new Error('request was cancelled'), null) | ||
this._message(8, [i, offset, length], null) | ||
@@ -291,3 +291,3 @@ } | ||
var respond = function (err, buffer) { | ||
if (err || request !== this._pull(this.peerRequests, i, offset, length)) | ||
if (err || request !== pull(this.peerRequests, i, offset, length)) | ||
return | ||
@@ -303,3 +303,3 @@ this.piece(i, offset, buffer) | ||
Wire.prototype._onpiece = function (i, offset, buffer) { | ||
this._callback(this._pull(this.requests, i, offset, buffer.length), null, buffer) | ||
this._callback(pull(this.requests, i, offset, buffer.length), null, buffer) | ||
this.downloaded += buffer.length | ||
@@ -311,3 +311,3 @@ this.emit('download', buffer.length) | ||
Wire.prototype._oncancel = function (i, offset, length) { | ||
this._pull(this.peerRequests, i, offset, length) | ||
pull(this.peerRequests, i, offset, length) | ||
this.emit('cancel', i, offset, length) | ||
@@ -330,16 +330,50 @@ } | ||
// | ||
// HELPER METHODS | ||
// STREAM METHODS | ||
// | ||
Wire.prototype._pull = function (requests, piece, offset, length) { | ||
for (var i = 0; i < requests.length; i++) { | ||
var req = requests[i] | ||
if (req.piece !== piece || req.offset !== offset || req.length !== length) continue | ||
if (i === 0) requests.shift() | ||
else requests.splice(i, 1) | ||
return req | ||
/** | ||
* Push a message to the remote peer. | ||
* @param {Buffer} data | ||
*/ | ||
Wire.prototype._push = function (data) { | ||
if (this._finished) return | ||
return this.push(data) | ||
} | ||
/** | ||
* Duplex stream method. Called whenever the upstream has data for us. | ||
* @param {Buffer|string} data | ||
* @param {string} encoding | ||
* @param {function} cb | ||
*/ | ||
Wire.prototype._write = function (data, encoding, cb) { | ||
this._bufferSize += data.length | ||
this._buffer.push(data) | ||
while (this._bufferSize >= this._parserSize) { | ||
var buffer = (this._buffer.length === 1) | ||
? this._buffer[0] | ||
: Buffer.concat(this._buffer) | ||
this._bufferSize -= this._parserSize | ||
this._buffer = this._bufferSize | ||
? [buffer.slice(this._parserSize)] | ||
: [] | ||
this._parser(buffer.slice(0, this._parserSize)) | ||
} | ||
return null | ||
cb(null) // Signal that we're ready for more data | ||
} | ||
/** | ||
* Duplex stream method. Called whenever the downstream wants data. No-op | ||
* since we'll just push data whenever we get it. Extra data will be buffered | ||
* in memory (we don't want to apply backpressure to peers!). | ||
*/ | ||
Wire.prototype._read = function () {} | ||
// | ||
// HELPER METHODS | ||
// | ||
Wire.prototype._callback = function (request, err, buffer) { | ||
@@ -463,44 +497,14 @@ if (!request) | ||
// | ||
// STREAM METHODS | ||
// | ||
/** | ||
* Push a message to the remote peer. | ||
* @param {Buffer} data | ||
*/ | ||
Wire.prototype._push = function (data) { | ||
if (this._finished) return | ||
return this.push(data) | ||
} | ||
/** | ||
* Duplex stream method. Called whenever the upstream has data for us. | ||
* @param {Buffer|string} data | ||
* @param {string} encoding | ||
* @param {function} cb | ||
*/ | ||
Wire.prototype._write = function (data, encoding, cb) { | ||
this._bufferSize += data.length | ||
this._buffer.push(data) | ||
while (this._bufferSize >= this._parserSize) { | ||
var buffer = (this._buffer.length === 1) | ||
? this._buffer[0] | ||
: Buffer.concat(this._buffer) | ||
this._bufferSize -= this._parserSize | ||
this._buffer = this._bufferSize | ||
? [buffer.slice(this._parserSize)] | ||
: [] | ||
this._parser(buffer.slice(0, this._parserSize)) | ||
function pull (requests, piece, offset, length) { | ||
for (var i = 0; i < requests.length; i++) { | ||
var req = requests[i] | ||
if (req.piece !== piece || req.offset !== offset || req.length !== length) | ||
continue | ||
if (i === 0) | ||
requests.shift() | ||
else | ||
requests.splice(i, 1) | ||
return req | ||
} | ||
cb(null) // Signal that we're ready for more data | ||
return null | ||
} | ||
/** | ||
* Duplex stream method. Called whenever the downstream wants data. No-op | ||
* since we'll just push data whenever we get it. Extra data will be buffered | ||
* in memory (we don't want to apply backpressure to peers!). | ||
*/ | ||
Wire.prototype._read = function () {} |
{ | ||
"name": "bittorrent-protocol", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Simple, robust, BitTorrent peer wire protocol implementation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
579
24147