a zip code crypto-currency system good for red ONLY

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict'
  2. var url = require('url')
  3. var gitHosts = require('./git-host-info.js')
  4. var GitHost = module.exports = require('./git-host.js')
  5. var protocolToRepresentationMap = {
  6. 'git+ssh': 'sshurl',
  7. 'git+https': 'https',
  8. 'ssh': 'sshurl',
  9. 'git': 'git'
  10. }
  11. function protocolToRepresentation (protocol) {
  12. if (protocol.substr(-1) === ':') protocol = protocol.slice(0, -1)
  13. return protocolToRepresentationMap[protocol] || protocol
  14. }
  15. var authProtocols = {
  16. 'git:': true,
  17. 'https:': true,
  18. 'git+https:': true,
  19. 'http:': true,
  20. 'git+http:': true
  21. }
  22. var cache = {}
  23. module.exports.fromUrl = function (giturl, opts) {
  24. if (typeof giturl !== 'string') return
  25. var key = giturl + JSON.stringify(opts || {})
  26. if (!(key in cache)) {
  27. cache[key] = fromUrl(giturl, opts)
  28. }
  29. return cache[key]
  30. }
  31. function fromUrl (giturl, opts) {
  32. if (giturl == null || giturl === '') return
  33. var url = fixupUnqualifiedGist(
  34. isGitHubShorthand(giturl) ? 'github:' + giturl : giturl
  35. )
  36. var parsed = parseGitUrl(url)
  37. var shortcutMatch = url.match(new RegExp('^([^:]+):(?:(?:[^@:]+(?:[^@]+)?@)?([^/]*))[/](.+?)(?:[.]git)?($|#)'))
  38. var matches = Object.keys(gitHosts).map(function (gitHostName) {
  39. try {
  40. var gitHostInfo = gitHosts[gitHostName]
  41. var auth = null
  42. if (parsed.auth && authProtocols[parsed.protocol]) {
  43. auth = decodeURIComponent(parsed.auth)
  44. }
  45. var committish = parsed.hash ? decodeURIComponent(parsed.hash.substr(1)) : null
  46. var user = null
  47. var project = null
  48. var defaultRepresentation = null
  49. if (shortcutMatch && shortcutMatch[1] === gitHostName) {
  50. user = shortcutMatch[2] && decodeURIComponent(shortcutMatch[2])
  51. project = decodeURIComponent(shortcutMatch[3])
  52. defaultRepresentation = 'shortcut'
  53. } else {
  54. if (parsed.host && parsed.host !== gitHostInfo.domain && parsed.host.replace(/^www[.]/, '') !== gitHostInfo.domain) return
  55. if (!gitHostInfo.protocols_re.test(parsed.protocol)) return
  56. if (!parsed.path) return
  57. var pathmatch = gitHostInfo.pathmatch
  58. var matched = parsed.path.match(pathmatch)
  59. if (!matched) return
  60. if (matched[1] != null) user = decodeURIComponent(matched[1].replace(/^:/, ''))
  61. if (matched[2] != null) project = decodeURIComponent(matched[2])
  62. defaultRepresentation = protocolToRepresentation(parsed.protocol)
  63. }
  64. return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
  65. } catch (ex) {
  66. if (!(ex instanceof URIError)) throw ex
  67. }
  68. }).filter(function (gitHostInfo) { return gitHostInfo })
  69. if (matches.length !== 1) return
  70. return matches[0]
  71. }
  72. function isGitHubShorthand (arg) {
  73. // Note: This does not fully test the git ref format.
  74. // See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  75. //
  76. // The only way to do this properly would be to shell out to
  77. // git-check-ref-format, and as this is a fast sync function,
  78. // we don't want to do that. Just let git fail if it turns
  79. // out that the commit-ish is invalid.
  80. // GH usernames cannot start with . or -
  81. return /^[^:@%/\s.-][^:@%/\s]*[/][^:@\s/%]+(?:#.*)?$/.test(arg)
  82. }
  83. function fixupUnqualifiedGist (giturl) {
  84. // necessary for round-tripping gists
  85. var parsed = url.parse(giturl)
  86. if (parsed.protocol === 'gist:' && parsed.host && !parsed.path) {
  87. return parsed.protocol + '/' + parsed.host
  88. } else {
  89. return giturl
  90. }
  91. }
  92. function parseGitUrl (giturl) {
  93. if (typeof giturl !== 'string') giturl = '' + giturl
  94. var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
  95. if (!matched) return url.parse(giturl)
  96. return {
  97. protocol: 'git+ssh:',
  98. slashes: true,
  99. auth: matched[1],
  100. host: matched[2],
  101. port: null,
  102. hostname: matched[2],
  103. hash: matched[4],
  104. search: null,
  105. query: null,
  106. pathname: '/' + matched[3],
  107. path: '/' + matched[3],
  108. href: 'git+ssh://' + matched[1] + '@' + matched[2] +
  109. '/' + matched[3] + (matched[4] || '')
  110. }
  111. }