Skip to content

Commit

Permalink
search: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 10, 2018
1 parent b37a665 commit d2af077
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 820 deletions.
70 changes: 38 additions & 32 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

module.exports = exports = search

var npm = require('./npm.js')
var allPackageSearch = require('./search/all-package-search')
var esearch = require('./search/esearch.js')
var formatPackageStream = require('./search/format-package-stream.js')
var usage = require('./utils/usage')
var output = require('./utils/output.js')
var log = require('npmlog')
var ms = require('mississippi')
const npm = require('./npm.js')
const allPackageSearch = require('./search/all-package-search')
const figgyPudding = require('figgy-pudding')
const formatPackageStream = require('./search/format-package-stream.js')
const libSearch = require('libnpm/search')
const log = require('npmlog')
const ms = require('mississippi')
const npmConfig = require('./config/figgy-config.js')
const output = require('./utils/output.js')
const usage = require('./utils/usage')

search.usage = usage(
'search',
Expand All @@ -20,46 +22,50 @@ search.completion = function (opts, cb) {
cb(null, [])
}

const SearchOpts = figgyPudding({
description: {},
exclude: {},
include: {},
limit: {},
log: {},
staleness: {},
unicode: {}
})

function search (args, cb) {
var searchOpts = {
const opts = SearchOpts(npmConfig()).concat({
description: npm.config.get('description'),
exclude: prepareExcludes(npm.config.get('searchexclude')),
include: prepareIncludes(args, npm.config.get('searchopts')),
limit: npm.config.get('searchlimit'),
limit: npm.config.get('searchlimit') || 20,
log: log,
staleness: npm.config.get('searchstaleness'),
unicode: npm.config.get('unicode')
}

if (searchOpts.include.length === 0) {
})
if (opts.include.length === 0) {
return cb(new Error('search must be called with arguments'))
}

// Used later to figure out whether we had any packages go out
var anyOutput = false
let anyOutput = false

var entriesStream = ms.through.obj()
const entriesStream = ms.through.obj()

var esearchWritten = false
esearch(searchOpts).on('data', function (pkg) {
let esearchWritten = false
libSearch.stream(opts.include, opts).on('data', pkg => {
entriesStream.write(pkg)
!esearchWritten && (esearchWritten = true)
}).on('error', function (e) {
}).on('error', err => {
if (esearchWritten) {
// If esearch errored after already starting output, we can't fall back.
return entriesStream.emit('error', e)
return entriesStream.emit('error', err)
}
log.warn('search', 'fast search endpoint errored. Using old search.')
allPackageSearch(searchOpts).on('data', function (pkg) {
entriesStream.write(pkg)
}).on('error', function (e) {
entriesStream.emit('error', e)
}).on('end', function () {
entriesStream.end()
})
}).on('end', function () {
entriesStream.end()
})
allPackageSearch(opts)
.on('data', pkg => entriesStream.write(pkg))
.on('error', err => entriesStream.emit('error', err))
.on('end', () => entriesStream.end())
}).on('end', () => entriesStream.end())

// Grab a configured output stream that will spit out packages in the
// desired format.
Expand All @@ -71,14 +77,14 @@ function search (args, cb) {
parseable: npm.config.get('parseable'),
color: npm.color
})
outputStream.on('data', function (chunk) {
outputStream.on('data', chunk => {
if (!anyOutput) { anyOutput = true }
output(chunk.toString('utf8'))
})

log.silly('search', 'searching packages')
ms.pipe(entriesStream, outputStream, function (er) {
if (er) return cb(er)
ms.pipe(entriesStream, outputStream, err => {
if (err) return cb(err)
if (!anyOutput && !npm.config.get('json') && !npm.config.get('parseable')) {
output('No matches found for ' + (args.map(JSON.stringify).join(' ')))
}
Expand Down
Loading

0 comments on commit d2af077

Please sign in to comment.