]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npm-package-arg/npa.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npm-package-arg / npa.js
1 var url = require("url")
2 var assert = require("assert")
3 var util = require("util")
4 var semver = require("semver")
5 var path = require("path")
6 var HostedGit = require("hosted-git-info")
7
8 module.exports = npa
9
10 var isWindows = process.platform === "win32" || global.FAKE_WINDOWS
11 var slashRe = isWindows ? /\\|[/]/ : /[/]/
12
13 var parseName = /^(?:@([^/]+?)[/])?([^/]+?)$/
14 var nameAt = /^(@([^/]+?)[/])?([^/]+?)@/
15 var debug = util.debuglog ? util.debuglog("npa")
16   : /\bnpa\b/i.test(process.env.NODE_DEBUG || "")
17   ? function () {
18     console.error("NPA: " + util.format.apply(util, arguments).split("\n").join("\nNPA: "))
19   } : function () {}
20
21 function validName (name) {
22   if (!name) {
23     debug("not a name %j", name)
24     return false
25   }
26   var n = name.trim()
27   if (!n || n.charAt(0) === "."
28       || !n.match(/^[a-zA-Z0-9]/)
29       || n.match(/[/()&?#|<>@:%\s\\*'"!~`]/)
30       || n.toLowerCase() === "node_modules"
31       || n !== encodeURIComponent(n)
32       || n.toLowerCase() === "favicon.ico") {
33     debug("not a valid name %j", name)
34     return false
35   }
36   return n
37 }
38
39 function npa (arg) {
40   assert.equal(typeof arg, "string")
41   arg = arg.trim()
42
43   var res = new Result
44   res.raw = arg
45   res.scope = null
46
47   // See if it's something like foo@...
48   var nameparse = arg.match(nameAt)
49   debug("nameparse", nameparse)
50   if (nameparse && validName(nameparse[3]) &&
51       (!nameparse[2] || validName(nameparse[2]))) {
52     res.name = (nameparse[1] || "") + nameparse[3]
53     if (nameparse[2])
54       res.scope = "@" + nameparse[2]
55     arg = arg.substr(nameparse[0].length)
56   } else {
57     res.name = null
58   }
59
60   res.rawSpec = arg
61   res.spec = arg
62
63   var urlparse = url.parse(arg)
64   debug("urlparse", urlparse)
65
66   // windows paths look like urls
67   // don't be fooled!
68   if (isWindows && urlparse && urlparse.protocol &&
69       urlparse.protocol.match(/^[a-zA-Z]:$/)) {
70     debug("windows url-ish local path", urlparse)
71     urlparse = {}
72   }
73
74   if (urlparse.protocol || HostedGit.fromUrl(arg)) {
75     return parseUrl(res, arg, urlparse)
76   }
77
78   // at this point, it's not a url, and not hosted
79   // If it's a valid name, and doesn't already have a name, then assume
80   // $name@"" range
81   //
82   // if it's got / chars in it, then assume that it's local.
83
84   if (res.name) {
85     if (arg == '') arg = 'latest'
86     var version = semver.valid(arg, true)
87     var range = semver.validRange(arg, true)
88     // foo@...
89     if (version) {
90       res.spec = version
91       res.type = "version"
92     } else if (range) {
93       res.spec = range
94       res.type = "range"
95     } else if (slashRe.test(arg)) {
96       parseLocal(res, arg)
97     } else {
98       res.type = "tag"
99       res.spec = arg
100     }
101   } else {
102     var p = arg.match(parseName)
103     if (p && validName(p[2]) &&
104         (!p[1] || validName(p[1]))) {
105       res.type = "tag"
106       res.spec = "latest"
107       res.rawSpec = ""
108       res.name = arg
109       if (p[1])
110         res.scope = "@" + p[1]
111     } else {
112       parseLocal(res, arg)
113     }
114   }
115
116   return res
117 }
118
119 function parseLocal (res, arg) {
120   // turns out nearly every character is allowed in fs paths
121   if (/\0/.test(arg)) {
122     throw new Error("Invalid Path: " + JSON.stringify(arg))
123   }
124   res.type = "local"
125   res.spec = path.resolve(arg)
126 }
127
128 function parseUrl (res, arg, urlparse) {
129   var gitHost = HostedGit.fromUrl(arg)
130   if (gitHost) {
131     res.type  = "hosted"
132     res.spec  = gitHost.toString(),
133     res.hosted = {
134       type:      gitHost.type,
135       ssh:       gitHost.ssh(),
136       sshUrl:    gitHost.sshurl(),
137       httpsUrl:  gitHost.https(),
138       gitUrl:    gitHost.git(),
139       shortcut:  gitHost.shortcut(),
140       directUrl: gitHost.file("package.json")
141     }
142     return res
143   }
144   // check the protocol, and then see if it's git or not
145   switch (urlparse.protocol) {
146     case "git:":
147     case "git+http:":
148     case "git+https:":
149     case "git+rsync:":
150     case "git+ftp:":
151     case "git+ssh:":
152     case "git+file:":
153       res.type = "git"
154       res.spec = arg.replace(/^git[+]/, "")
155       break
156
157     case "http:":
158     case "https:":
159       res.type = "remote"
160       res.spec = arg
161       break
162
163     case "file:":
164       res.type = "local"
165       res.spec = urlparse.pathname
166       break
167
168     default:
169       throw new Error("Unsupported URL Type: " + arg)
170       break
171   }
172
173   return res
174 }
175
176
177 function Result () {
178   if (!(this instanceof Result)) return new Result
179 }
180 Result.prototype.name   = null
181 Result.prototype.type   = null
182 Result.prototype.spec   = null
183 Result.prototype.raw    = null
184 Result.prototype.hosted = null