]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/utils/map-to-registry.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / utils / map-to-registry.js
1 var url = require("url")
2
3 var log = require("npmlog")
4   , npa = require("npm-package-arg")
5
6 module.exports = mapToRegistry
7
8 function mapToRegistry(name, config, cb) {
9   log.silly("mapToRegistry", "name", name)
10   var registry
11
12   // the name itself takes precedence
13   var data = npa(name)
14   if (data.scope) {
15     // the name is definitely scoped, so escape now
16     name = name.replace("/", "%2f")
17
18     log.silly("mapToRegistry", "scope (from package name)", data.scope)
19
20     registry = config.get(data.scope + ":registry")
21     if (!registry) {
22       log.verbose("mapToRegistry", "no registry URL found in name for scope", data.scope)
23     }
24   }
25
26   // ...then --scope=@scope or --scope=scope
27   var scope = config.get("scope")
28   if (!registry && scope) {
29     // I'm an enabler, sorry
30     if (scope.charAt(0) !== "@") scope = "@" + scope
31
32     log.silly("mapToRegistry", "scope (from config)", scope)
33
34     registry = config.get(scope + ":registry")
35     if (!registry) {
36       log.verbose("mapToRegistry", "no registry URL found in config for scope", scope)
37     }
38   }
39
40   // ...and finally use the default registry
41   if (!registry) {
42     log.silly("mapToRegistry", "using default registry")
43     registry = config.get("registry")
44   }
45
46   log.silly("mapToRegistry", "registry", registry)
47
48   var auth = config.getCredentialsByURI(registry)
49
50   // normalize registry URL so resolution doesn't drop a piece of registry URL
51   var normalized = registry.slice(-1) !== '/' ? registry + '/' : registry
52   var uri
53   log.silly('mapToRegistry', 'data', data)
54   if (data.type === 'remote') {
55     uri = data.spec
56   } else {
57     uri = url.resolve(normalized, name)
58   }
59
60   log.silly('mapToRegistry', 'uri', uri)
61
62   cb(null, uri, scopeAuth(uri, registry, auth), normalized)
63 }
64
65 function scopeAuth (uri, registry, auth) {
66   var cleaned = {
67     scope: auth.scope,
68     email: auth.email,
69     alwaysAuth: auth.alwaysAuth,
70     token: undefined,
71     username: undefined,
72     password: undefined,
73     auth: undefined
74   }
75
76   var requestHost
77   var registryHost
78
79   if (auth.token || auth.auth || (auth.username && auth.password)) {
80     requestHost = url.parse(uri).hostname
81     registryHost = url.parse(registry).hostname
82
83     if (requestHost === registryHost) {
84       cleaned.token = auth.token
85       cleaned.auth = auth.auth
86       cleaned.username = auth.username
87       cleaned.password = auth.password
88     } else if (auth.alwaysAuth) {
89       log.verbose('scopeAuth', 'alwaysAuth set for', registry)
90       cleaned.token = auth.token
91       cleaned.auth = auth.auth
92       cleaned.username = auth.username
93       cleaned.password = auth.password
94     } else {
95       log.silly('scopeAuth', uri, "doesn't share host with registry", registry)
96     }
97   }
98
99   return cleaned
100 }