summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/get-agent.js
blob: 4bc074f29bf844365aa066ef97ba00c4b9264dd3 (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
// get an http/https agent
// This is necessary for the custom CA certs in http2,
// especially while juggling multiple different registries.
//
// When using http2, the agent key is just the CA setting,
// since it can manage socket pooling across different host:port
// options.  When using the older implementation, the
// key is ca:host:port combination.

module.exports = getAgent

var npm = require("../npm.js")
  , url = require("url")
  , agents = {}
  , isHttp2 = !!require("http").globalAgent
  , registry = url.parse(npm.config.get("registry") || "")
  , regCA = npm.config.get("ca")

function getAgent (remote) {
  // If not doing https, then there's no CA cert to manage.
  // on http2, this will use the default global agent.
  // on http1, this is undefined, so it'll spawn based on
  // host:port if necessary.
  if (remote.protocol !== "https:") {
    return require("http").globalAgent
  }

  if (typeof remote === "string") {
    remote = url.parse(remote)
  }

  var ca
  // if this is the registry, then use the configuration ca.
  // otherwise, just use the built-in CAs that node has.
  // todo: multi-registry support.
  if (remote.hostname === registry.hostname
      && remote.port === registry.port) {
    ca = regCA
  }

  // no CA, just use the default agent.
  if (!ca) {
    return require("https").globalAgent
  }

  var hostname = remote.hostname
    , port = remote.port
    , key = agentKey(hostname, port, ca)

  return agents[key] = agents[key] || getAgent_(hostname, port, ca)
}

function getAgent_ (hostname, port, ca) {
  var Agent = require("https").Agent
  return new Agent({ host: hostname
                   , port: port
                   , ca: ca })
}

function agentKey (hostname, port, ca) {
  return JSON.stringify(isHttp2 ? ca : [hostname, port, ca])
}