summaryrefslogtreecommitdiff
path: root/deps/npm/lib/search/esearch.js
blob: f4beb7ade66b18f7415d8dfa9a4bd977458acbf5 (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
'use strict'

var npm = require('../npm.js')
var log = require('npmlog')
var mapToRegistry = require('../utils/map-to-registry.js')
var jsonstream = require('JSONStream')
var ms = require('mississippi')
var gunzip = require('../utils/gunzip-maybe')

module.exports = esearch

function esearch (opts) {
  var stream = ms.through.obj()

  mapToRegistry('-/v1/search', npm.config, function (er, uri, auth) {
    if (er) return stream.emit('error', er)
    createResultStream(uri, auth, opts, function (err, resultStream) {
      if (err) return stream.emit('error', err)
      ms.pipeline.obj(resultStream, stream)
    })
  })
  return stream
}

function createResultStream (uri, auth, opts, cb) {
  log.verbose('esearch', 'creating remote entry stream')
  var params = {
    timeout: 600,
    follow: true,
    staleOk: true,
    auth: auth,
    streaming: true
  }
  var q = buildQuery(opts)
  npm.registry.request(uri + '?text=' + encodeURIComponent(q) + '&size=' + opts.limit, params, function (err, res) {
    if (err) return cb(err)
    log.silly('esearch', 'request stream opened, code:', res.statusCode)
    // NOTE - The stream returned by `request` seems to be very persnickety
    //        and this is almost a magic incantation to get it to work.
    //        Modify how `res` is used here at your own risk.
    var entryStream = ms.pipeline.obj(
      res,
      ms.through(function (chunk, enc, cb) {
        cb(null, chunk)
      }),
      gunzip(),
      jsonstream.parse('objects.*.package', function (data) {
        return {
          name: data.name,
          description: data.description,
          maintainers: data.maintainers,
          keywords: data.keywords,
          version: data.version,
          date: data.date ? new Date(data.date) : null
        }
      })
    )
    return cb(null, entryStream)
  })
}

function buildQuery (opts) {
  return opts.include.join(' ')
}