2019-01-29 14:43:00 -08:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const npmFetch = require('npm-registry-fetch')
|
|
|
|
|
|
|
|
module.exports = search
|
|
|
|
function search (query, opts) {
|
2020-10-02 17:52:19 -04:00
|
|
|
return search.stream(query, opts).collect()
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
|
|
|
search.stream = searchStream
|
2020-10-02 17:52:19 -04:00
|
|
|
function searchStream (query, opts = {}) {
|
|
|
|
opts = {
|
|
|
|
detailed: false,
|
|
|
|
limit: 20,
|
|
|
|
from: 0,
|
|
|
|
quality: 0.65,
|
|
|
|
popularity: 0.98,
|
|
|
|
maintenance: 0.5,
|
2020-12-09 10:33:11 -05:00
|
|
|
...opts.opts, // this is to support the cli's --searchopts parameter
|
2022-01-14 19:42:48 +02:00
|
|
|
...opts,
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-29 14:43:00 -08:00
|
|
|
switch (opts.sortBy) {
|
|
|
|
case 'optimal': {
|
2020-10-02 17:52:19 -04:00
|
|
|
opts.quality = 0.65
|
|
|
|
opts.popularity = 0.98
|
|
|
|
opts.maintenance = 0.5
|
2019-01-29 14:43:00 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'quality': {
|
2020-10-02 17:52:19 -04:00
|
|
|
opts.quality = 1
|
|
|
|
opts.popularity = 0
|
|
|
|
opts.maintenance = 0
|
2019-01-29 14:43:00 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'popularity': {
|
2020-10-02 17:52:19 -04:00
|
|
|
opts.quality = 0
|
|
|
|
opts.popularity = 1
|
|
|
|
opts.maintenance = 0
|
2019-01-29 14:43:00 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'maintenance': {
|
2020-10-02 17:52:19 -04:00
|
|
|
opts.quality = 0
|
|
|
|
opts.popularity = 0
|
|
|
|
opts.maintenance = 1
|
2019-01-29 14:43:00 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return npmFetch.json.stream('/-/v1/search', 'objects.*',
|
2020-10-02 17:52:19 -04:00
|
|
|
{
|
|
|
|
...opts,
|
2019-01-29 14:43:00 -08:00
|
|
|
query: {
|
|
|
|
text: Array.isArray(query) ? query.join(' ') : query,
|
|
|
|
size: opts.limit,
|
2019-07-24 23:00:03 -07:00
|
|
|
from: opts.from,
|
2019-01-29 14:43:00 -08:00
|
|
|
quality: opts.quality,
|
|
|
|
popularity: opts.popularity,
|
2022-01-14 19:42:48 +02:00
|
|
|
maintenance: opts.maintenance,
|
2019-01-29 14:43:00 -08:00
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
mapJSON: (obj) => {
|
2019-01-29 14:43:00 -08:00
|
|
|
if (obj.package.date) {
|
|
|
|
obj.package.date = new Date(obj.package.date)
|
|
|
|
}
|
|
|
|
if (opts.detailed) {
|
|
|
|
return obj
|
|
|
|
} else {
|
|
|
|
return obj.package
|
|
|
|
}
|
2022-01-14 19:42:48 +02:00
|
|
|
},
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
2019-01-29 14:43:00 -08:00
|
|
|
)
|
|
|
|
}
|