Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. 'use strict'
  2. const url = require('url')
  3. const gitHosts = require('./git-host-info.js')
  4. const GitHost = module.exports = require('./git-host.js')
  5. const LRU = require('lru-cache')
  6. const cache = new LRU({ max: 1000 })
  7. const protocolToRepresentationMap = {
  8. 'git+ssh:': 'sshurl',
  9. 'git+https:': 'https',
  10. 'ssh:': 'sshurl',
  11. 'git:': 'git'
  12. }
  13. function protocolToRepresentation (protocol) {
  14. return protocolToRepresentationMap[protocol] || protocol.slice(0, -1)
  15. }
  16. const authProtocols = {
  17. 'git:': true,
  18. 'https:': true,
  19. 'git+https:': true,
  20. 'http:': true,
  21. 'git+http:': true
  22. }
  23. const knownProtocols = Object.keys(gitHosts.byShortcut).concat(['http:', 'https:', 'git:', 'git+ssh:', 'git+https:', 'ssh:'])
  24. module.exports.fromUrl = function (giturl, opts) {
  25. if (typeof giturl !== 'string') {
  26. return
  27. }
  28. const key = giturl + JSON.stringify(opts || {})
  29. if (!cache.has(key)) {
  30. cache.set(key, fromUrl(giturl, opts))
  31. }
  32. return cache.get(key)
  33. }
  34. function fromUrl (giturl, opts) {
  35. if (!giturl) {
  36. return
  37. }
  38. const url = isGitHubShorthand(giturl) ? 'github:' + giturl : correctProtocol(giturl)
  39. const parsed = parseGitUrl(url)
  40. if (!parsed) {
  41. return parsed
  42. }
  43. const gitHostShortcut = gitHosts.byShortcut[parsed.protocol]
  44. const gitHostDomain = gitHosts.byDomain[parsed.hostname.startsWith('www.') ? parsed.hostname.slice(4) : parsed.hostname]
  45. const gitHostName = gitHostShortcut || gitHostDomain
  46. if (!gitHostName) {
  47. return
  48. }
  49. const gitHostInfo = gitHosts[gitHostShortcut || gitHostDomain]
  50. let auth = null
  51. if (authProtocols[parsed.protocol] && (parsed.username || parsed.password)) {
  52. auth = `${parsed.username}${parsed.password ? ':' + parsed.password : ''}`
  53. }
  54. let committish = null
  55. let user = null
  56. let project = null
  57. let defaultRepresentation = null
  58. try {
  59. if (gitHostShortcut) {
  60. let pathname = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname
  61. const firstAt = pathname.indexOf('@')
  62. // we ignore auth for shortcuts, so just trim it out
  63. if (firstAt > -1) {
  64. pathname = pathname.slice(firstAt + 1)
  65. }
  66. const lastSlash = pathname.lastIndexOf('/')
  67. if (lastSlash > -1) {
  68. user = decodeURIComponent(pathname.slice(0, lastSlash))
  69. // we want nulls only, never empty strings
  70. if (!user) {
  71. user = null
  72. }
  73. project = decodeURIComponent(pathname.slice(lastSlash + 1))
  74. } else {
  75. project = decodeURIComponent(pathname)
  76. }
  77. if (project.endsWith('.git')) {
  78. project = project.slice(0, -4)
  79. }
  80. if (parsed.hash) {
  81. committish = decodeURIComponent(parsed.hash.slice(1))
  82. }
  83. defaultRepresentation = 'shortcut'
  84. } else {
  85. if (!gitHostInfo.protocols.includes(parsed.protocol)) {
  86. return
  87. }
  88. const segments = gitHostInfo.extract(parsed)
  89. if (!segments) {
  90. return
  91. }
  92. user = segments.user && decodeURIComponent(segments.user)
  93. project = decodeURIComponent(segments.project)
  94. committish = decodeURIComponent(segments.committish)
  95. defaultRepresentation = protocolToRepresentation(parsed.protocol)
  96. }
  97. } catch (err) {
  98. /* istanbul ignore else */
  99. if (err instanceof URIError) {
  100. return
  101. } else {
  102. throw err
  103. }
  104. }
  105. return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
  106. }
  107. // accepts input like git:github.com:user/repo and inserts the // after the first :
  108. const correctProtocol = (arg) => {
  109. const firstColon = arg.indexOf(':')
  110. const proto = arg.slice(0, firstColon + 1)
  111. if (knownProtocols.includes(proto)) {
  112. return arg
  113. }
  114. const firstAt = arg.indexOf('@')
  115. if (firstAt > -1) {
  116. if (firstAt > firstColon) {
  117. return `git+ssh://${arg}`
  118. } else {
  119. return arg
  120. }
  121. }
  122. const doubleSlash = arg.indexOf('//')
  123. if (doubleSlash === firstColon + 1) {
  124. return arg
  125. }
  126. return arg.slice(0, firstColon + 1) + '//' + arg.slice(firstColon + 1)
  127. }
  128. // look for github shorthand inputs, such as npm/cli
  129. const isGitHubShorthand = (arg) => {
  130. // it cannot contain whitespace before the first #
  131. // it cannot start with a / because that's probably an absolute file path
  132. // but it must include a slash since repos are username/repository
  133. // it cannot start with a . because that's probably a relative file path
  134. // it cannot start with an @ because that's a scoped package if it passes the other tests
  135. // it cannot contain a : before a # because that tells us that there's a protocol
  136. // a second / may not exist before a #
  137. const firstHash = arg.indexOf('#')
  138. const firstSlash = arg.indexOf('/')
  139. const secondSlash = arg.indexOf('/', firstSlash + 1)
  140. const firstColon = arg.indexOf(':')
  141. const firstSpace = /\s/.exec(arg)
  142. const firstAt = arg.indexOf('@')
  143. const spaceOnlyAfterHash = !firstSpace || (firstHash > -1 && firstSpace.index > firstHash)
  144. const atOnlyAfterHash = firstAt === -1 || (firstHash > -1 && firstAt > firstHash)
  145. const colonOnlyAfterHash = firstColon === -1 || (firstHash > -1 && firstColon > firstHash)
  146. const secondSlashOnlyAfterHash = secondSlash === -1 || (firstHash > -1 && secondSlash > firstHash)
  147. const hasSlash = firstSlash > 0
  148. // if a # is found, what we really want to know is that the character immediately before # is not a /
  149. const doesNotEndWithSlash = firstHash > -1 ? arg[firstHash - 1] !== '/' : !arg.endsWith('/')
  150. const doesNotStartWithDot = !arg.startsWith('.')
  151. return spaceOnlyAfterHash && hasSlash && doesNotEndWithSlash && doesNotStartWithDot && atOnlyAfterHash && colonOnlyAfterHash && secondSlashOnlyAfterHash
  152. }
  153. // attempt to correct an scp style url so that it will parse with `new URL()`
  154. const correctUrl = (giturl) => {
  155. const firstAt = giturl.indexOf('@')
  156. const lastHash = giturl.lastIndexOf('#')
  157. let firstColon = giturl.indexOf(':')
  158. let lastColon = giturl.lastIndexOf(':', lastHash > -1 ? lastHash : Infinity)
  159. let corrected
  160. if (lastColon > firstAt) {
  161. // the last : comes after the first @ (or there is no @)
  162. // like it would in:
  163. // proto://hostname.com:user/repo
  164. // username@hostname.com:user/repo
  165. // :password@hostname.com:user/repo
  166. // username:password@hostname.com:user/repo
  167. // proto://username@hostname.com:user/repo
  168. // proto://:password@hostname.com:user/repo
  169. // proto://username:password@hostname.com:user/repo
  170. // then we replace the last : with a / to create a valid path
  171. corrected = giturl.slice(0, lastColon) + '/' + giturl.slice(lastColon + 1)
  172. // // and we find our new : positions
  173. firstColon = corrected.indexOf(':')
  174. lastColon = corrected.lastIndexOf(':')
  175. }
  176. if (firstColon === -1 && giturl.indexOf('//') === -1) {
  177. // we have no : at all
  178. // as it would be in:
  179. // username@hostname.com/user/repo
  180. // then we prepend a protocol
  181. corrected = `git+ssh://${corrected}`
  182. }
  183. return corrected
  184. }
  185. // try to parse the url as its given to us, if that throws
  186. // then we try to clean the url and parse that result instead
  187. // THIS FUNCTION SHOULD NEVER THROW
  188. const parseGitUrl = (giturl) => {
  189. let result
  190. try {
  191. result = new url.URL(giturl)
  192. } catch (err) {}
  193. if (result) {
  194. return result
  195. }
  196. const correctedUrl = correctUrl(giturl)
  197. try {
  198. result = new url.URL(correctedUrl)
  199. } catch (err) {}
  200. return result
  201. }