<commit_before>'use strict'
module.exports = registry

const RegClient = require('npm-registry-client')
const semver = require('semver')

const regClient = new RegClient()

//
// Find tarballs on npm
//
function registry (opts) {
  opts = opts || {}

  if (!opts.url) {
    throw new Error('opts.url is required')
  }

  return { resolve }

  function resolve (opts) {
    opts = opts || {}

    if (!opts.name) {
      throw new Error('opts.name is required')
    }

    return new Promise((resolve, reject) => {
      versions(opts.name, opts.version, (err, v) => {
        if (err) return reject(err)
        resolve(v)
      })
    })
  }

  function getMatchedVersions (version, data) {
    try {
      if (version === 'latest') {
        return [data['dist-tags'].latest]
      }

      if (!semver.validRange(version)) {
        console.log('not a valid range ' + version)

        return Object.keys(data.versions).filter(v => v === version)
      }

      return Object.keys(data.versions)
        .filter(v => semver.satisfies(v, version))
        .sort((a, b) => semver.lte(a, b))
    } catch (e) {
      return null
    }
  }

  function versions (module, version, cb) {
    regClient.get(opts.url + module.replace('/', '%2f'), {
      auth: {
        token: opts.token,
      },
    }, (err, data) => {
      if (err) return cb(err)

      const v = getMatchedVersions(version, data)

      if (!v || !v.length) {
        const e = new Error('No match for semver `' + version + '` found')
        e.versions = Object.keys(data.versions)
        return cb(e)
      }

      cb(null, data.versions[v[0]])
    })
  }
}<commit_msg>encoding package name when requesting info from registry<commit_after>'use strict'
module.exports = registry

const RegClient = require('npm-registry-client')
const semver = require('semver')

const regClient = new RegClient()

//
// Find tarballs on npm
//
function registry (opts) {
  opts = opts || {}

  if (!opts.url) {
    throw new Error('opts.url is required')
  }

  return { resolve }

  function resolve (opts) {
    opts = opts || {}

    if (!opts.name) {
      throw new Error('opts.name is required')
    }

    return new Promise((resolve, reject) => {
      versions(opts.name, opts.version, (err, v) => {
        if (err) return reject(err)
        resolve(v)
      })
    })
  }

  function getMatchedVersions (version, data) {
    try {
      if (version === 'latest') {
        return [data['dist-tags'].latest]
      }

      if (!semver.validRange(version)) {
        console.log('not a valid range ' + version)

        return Object.keys(data.versions).filter(v => v === version)
      }

      return Object.keys(data.versions)
        .filter(v => semver.satisfies(v, version))
        .sort((a, b) => semver.lte(a, b))
    } catch (e) {
      return null
    }
  }

  function versions (module, version, cb) {
    const encodedPackageName = encodePackageName(module)
    regClient.get(opts.url + encodedPackageName, {
      auth: {
        token: opts.token,
      },
    }, (err, data) => {
      if (err) return cb(err)

      const v = getMatchedVersions(version, data)

      if (!v || !v.length) {
        const e = new Error('No match for semver `' + version + '` found')
        e.versions = Object.keys(data.versions)
        return cb(e)
      }

      cb(null, data.versions[v[0]])
    })
  }

  function encodePackageName (packageName) {
    if (packageName.charAt(0) === '@') {
      return `@${encodeURIComponent(packageName.substring(1))}`
    }
    return encodeURIComponent(packageName)
  }
}
{'fraction_non_alphanumeric': 0.1518456375838926, 'fraction_numerical': 0.0013982102908277406, 'mean_word_length': 2.390521327014218, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2350112', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1136, 'n_words': 278}