"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-package-arg/npa.js" (11 Apr 2017, 4954 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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