From 47237ba54a63216969a82178e5785ff6b81db16b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 3 Jun 2014 21:17:25 -0700 Subject: [PATCH] expose `setMetadata` method --- index.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 7c524c6..85fefb7 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ module.exports = function (metadata) { } if (info) - this._gotMetadata(info) + this.setMetadata(info) } } @@ -96,7 +96,10 @@ module.exports = function (metadata) { } } - // Expose high-level, friendly API (fetch/cancel) + /** + * Ask the peer to send metadata. + * @public + */ ut_metadata.prototype.fetch = function () { if (this._metadataComplete) { return @@ -107,10 +110,24 @@ module.exports = function (metadata) { } } + /** + * Stop asking the peer to send metadata. + * @public + */ ut_metadata.prototype.cancel = function () { this._fetching = false } + ut_metadata.prototype.setMetadata = function (_metadata) { + if (this._metadataComplete) return + this._metadataComplete = true + this.metadata = _metadata + this._metadataSize = this.metadata.length + this.cancel() + this._wire.extendedHandshake.metadata_size = this._metadataSize + this.emit('metadata', bncode.encode({ info: bncode.decode(this.metadata) })) + } + ut_metadata.prototype._send = function (dict, trailer) { var buf = bncode.encode(dict) if (Buffer.isBuffer(trailer)) { @@ -195,21 +212,12 @@ module.exports = function (metadata) { // check hash if (sha1(this.metadata) === this._infoHash.toString('hex')) { - this._gotMetadata(this.metadata) + this.setMetadata(this.metadata) } else { this._failedMetadata() } } - ut_metadata.prototype._gotMetadata = function (_metadata) { - this.cancel() - this.metadata = _metadata - this._metadataComplete = true - this._metadataSize = this.metadata.length - this._wire.extendedHandshake.metadata_size = this._metadataSize - this.emit('metadata', bncode.encode({ info: bncode.decode(this.metadata) })) - } - ut_metadata.prototype._failedMetadata = function () { // reset bitfield & try again this._bitfield = new BitField(0, { grow: BITFIELD_GROW })