<commit_before>// TODO: support blocklists

module.exports = WebTorrent

var Client = require('bittorrent-client')
var fs = require('fs')
var http = require('http')
var inherits = require('inherits')

inherits(WebTorrent, Client)

function WebTorrent (opts) {
  var self = this
  Client.call(self, opts)
  if (!opts) opts = {}

  if (opts.list) {
    return
  }

  self.on('torrent', function (torrent) {
    self._onTorrent(torrent)
  })

  // TODO: add event that signals that all files that are "interesting" to the user have
  // completed and handle it by stopping fetching additional data from the network
}

WebTorrent.prototype.add = function (torrentId, opts, cb) {
  var self = this
  if (!self.ready) {
    return self.once('ready', self.add.bind(self, torrentId, opts, cb))
  }

  if (typeof opts === 'function') {
    cb = opts
    opts = {}
  }
  if (typeof cb !== 'function') cb = function () {}

  // TODO: support passing in an index to file to download
  // self.index = opts.index

  // Called once we have a torrentId that bittorrent-client can handle
  function onTorrentId (torrentId) {
    Client.prototype.add.call(self, torrentId, opts, cb)
  }

  if (Client.toInfoHash(torrentId)) {
    // magnet uri, info hash, or torrent file can be handled by bittorrent-client
    process.nextTick(function () {
      onTorrentId(torrentId)
    })
  } else if (/^https?:/.test(torrentId)) {
    // http or https url to torrent file
    http.get(torrentId, function (res) {
      res.pipe(concat(function (torrent) {
        onTorrentId(torrent)
      }))
    }).on('error', function (err) {
      cb(new Error('Error downloading torrent from ' + torrentId + '\n' + err.message))
    })
  } else {
    // assume it's a filesystem path
    fs.readFile(torrentId, function (err, torrent) {
      if (err) {
        return cb(new Error('Cannot add torrent "' + torrentId + '". Torrent id must be one of: magnet uri, ' +
          'info hash, torrent file, http url, or filesystem path.'))
      }
      onTorrentId(torrent)
    })
  }

  return self
}

WebTorrent.prototype._onTorrent = function (torrent) {
  var self = this

  //torrent.swarm.on('wire', function () { })

  // if no index specified, use largest file
  // TODO: support torrent index selection correctly -- this doesn't work yet
  /*if (typeof torrent.index !== 'number') {
    var largestFile = torrent.files.reduce(function (a, b) {
      return a.length > b.length ? a : b
    })
    torrent.index = torrent.files.indexOf(largestFile)
  }

  // TODO
  torrent.files[torrent.index].select()*/
}
<commit_msg>removed extraneous comment<commit_after>// TODO: support blocklists

module.exports = WebTorrent

var Client = require('bittorrent-client')
var fs = require('fs')
var http = require('http')
var inherits = require('inherits')

inherits(WebTorrent, Client)

function WebTorrent (opts) {
  var self = this
  Client.call(self, opts)
  if (!opts) opts = {}

  if (opts.list) {
    return
  }

  self.on('torrent', function (torrent) {
    self._onTorrent(torrent)
  })

  // TODO: add event that signals that all files that are "interesting" to the user have
  // completed and handle it by stopping fetching additional data from the network
}

WebTorrent.prototype.add = function (torrentId, opts, cb) {
  var self = this
  if (!self.ready) {
    return self.once('ready', self.add.bind(self, torrentId, opts, cb))
  }

  if (typeof opts === 'function') {
    cb = opts
    opts = {}
  }
  if (typeof cb !== 'function') cb = function () {}

  // TODO: support passing in an index to file to download
  // self.index = opts.index

  // Called once we have a torrentId that bittorrent-client can handle
  function onTorrentId (torrentId) {
    Client.prototype.add.call(self, torrentId, opts, cb)
  }

  if (Client.toInfoHash(torrentId)) {
    // magnet uri, info hash, or torrent file can be handled by bittorrent-client
    process.nextTick(function () {
      onTorrentId(torrentId)
    })
  } else if (/^https?:/.test(torrentId)) {
    // http or https url to torrent file
    http.get(torrentId, function (res) {
      res.pipe(concat(function (torrent) {
        onTorrentId(torrent)
      }))
    }).on('error', function (err) {
      cb(new Error('Error downloading torrent from ' + torrentId + '\n' + err.message))
    })
  } else {
    // assume it's a filesystem path
    fs.readFile(torrentId, function (err, torrent) {
      if (err) {
        return cb(new Error('Cannot add torrent "' + torrentId + '". Torrent id must be one of: magnet uri, ' +
          'info hash, torrent file, http url, or filesystem path.'))
      }
      onTorrentId(torrent)
    })
  }

  return self
}

WebTorrent.prototype._onTorrent = function (torrent) {
  var self = this

  // if no index specified, use largest file
  // TODO: support torrent index selection correctly -- this doesn't work yet
  /*if (typeof torrent.index !== 'number') {
    var largestFile = torrent.files.reduce(function (a, b) {
      return a.length > b.length ? a : b
    })
    torrent.index = torrent.files.indexOf(largestFile)
  }

  // TODO
  torrent.files[torrent.index].select()*/
}

{'fraction_non_alphanumeric': 0.13054474708171207, 'fraction_numerical': 0.0, 'mean_word_length': 3.0007782101167315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6210711', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1611, 'n_words': 494}