Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « libnpmsearch « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb6b50783d35e8c25d0931a3ebe207a333f93cf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'

const npmFetch = require('npm-registry-fetch')

module.exports = search
function search (query, opts) {
  return search.stream(query, opts).collect()
}
search.stream = searchStream
function searchStream (query, opts = {}) {
  opts = {
    detailed: false,
    limit: 20,
    from: 0,
    quality: 0.65,
    popularity: 0.98,
    maintenance: 0.5,
    ...opts.opts, // this is to support the cli's --searchopts parameter
    ...opts
  }

  switch (opts.sortBy) {
    case 'optimal': {
      opts.quality = 0.65
      opts.popularity = 0.98
      opts.maintenance = 0.5
      break
    }
    case 'quality': {
      opts.quality = 1
      opts.popularity = 0
      opts.maintenance = 0
      break
    }
    case 'popularity': {
      opts.quality = 0
      opts.popularity = 1
      opts.maintenance = 0
      break
    }
    case 'maintenance': {
      opts.quality = 0
      opts.popularity = 0
      opts.maintenance = 1
      break
    }
  }
  return npmFetch.json.stream('/-/v1/search', 'objects.*',
    {
      ...opts,
      query: {
        text: Array.isArray(query) ? query.join(' ') : query,
        size: opts.limit,
        from: opts.from,
        quality: opts.quality,
        popularity: opts.popularity,
        maintenance: opts.maintenance
      },
      mapJSON: (obj) => {
        if (obj.package.date) {
          obj.package.date = new Date(obj.package.date)
        }
        if (opts.detailed) {
          return obj
        } else {
          return obj.package
        }
      }
    }
  )
}