hosts.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* eslint-disable max-len */
  2. 'use strict'
  3. const maybeJoin = (...args) => args.every(arg => arg) ? args.join('') : ''
  4. const maybeEncode = (arg) => arg ? encodeURIComponent(arg) : ''
  5. const formatHashFragment = (f) => f.toLowerCase()
  6. .replace(/^\W+/g, '') // strip leading non-characters
  7. .replace(/(?<!\W)\W+$/, '') // strip trailing non-characters
  8. .replace(/\//g, '') // strip all slashes
  9. .replace(/\W+/g, '-') // replace remaining non-characters with '-'
  10. const defaults = {
  11. sshtemplate: ({ domain, user, project, committish }) =>
  12. `git@${domain}:${user}/${project}.git${maybeJoin('#', committish)}`,
  13. sshurltemplate: ({ domain, user, project, committish }) =>
  14. `git+ssh://git@${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  15. edittemplate: ({ domain, user, project, committish, editpath, path }) =>
  16. `https://${domain}/${user}/${project}${maybeJoin('/', editpath, '/', maybeEncode(committish || 'HEAD'), '/', path)}`,
  17. browsetemplate: ({ domain, user, project, committish, treepath }) =>
  18. `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}`,
  19. browsetreetemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) =>
  20. `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'HEAD')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`,
  21. browseblobtemplate: ({ domain, user, project, committish, blobpath, path, fragment, hashformat }) =>
  22. `https://${domain}/${user}/${project}/${blobpath}/${maybeEncode(committish || 'HEAD')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`,
  23. docstemplate: ({ domain, user, project, treepath, committish }) =>
  24. `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`,
  25. httpstemplate: ({ auth, domain, user, project, committish }) =>
  26. `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  27. filetemplate: ({ domain, user, project, committish, path }) =>
  28. `https://${domain}/${user}/${project}/raw/${maybeEncode(committish || 'HEAD')}/${path}`,
  29. shortcuttemplate: ({ type, user, project, committish }) =>
  30. `${type}:${user}/${project}${maybeJoin('#', committish)}`,
  31. pathtemplate: ({ user, project, committish }) =>
  32. `${user}/${project}${maybeJoin('#', committish)}`,
  33. bugstemplate: ({ domain, user, project }) =>
  34. `https://${domain}/${user}/${project}/issues`,
  35. hashformat: formatHashFragment,
  36. }
  37. const hosts = {}
  38. hosts.github = {
  39. // First two are insecure and generally shouldn't be used any more, but
  40. // they are still supported.
  41. protocols: ['git:', 'http:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'],
  42. domain: 'github.com',
  43. treepath: 'tree',
  44. blobpath: 'blob',
  45. editpath: 'edit',
  46. filetemplate: ({ auth, user, project, committish, path }) =>
  47. `https://${maybeJoin(auth, '@')}raw.githubusercontent.com/${user}/${project}/${maybeEncode(committish || 'HEAD')}/${path}`,
  48. gittemplate: ({ auth, domain, user, project, committish }) =>
  49. `git://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  50. tarballtemplate: ({ domain, user, project, committish }) =>
  51. `https://codeload.${domain}/${user}/${project}/tar.gz/${maybeEncode(committish || 'HEAD')}`,
  52. extract: (url) => {
  53. let [, user, project, type, committish] = url.pathname.split('/', 5)
  54. if (type && type !== 'tree') {
  55. return
  56. }
  57. if (!type) {
  58. committish = url.hash.slice(1)
  59. }
  60. if (project && project.endsWith('.git')) {
  61. project = project.slice(0, -4)
  62. }
  63. if (!user || !project) {
  64. return
  65. }
  66. return { user, project, committish }
  67. },
  68. }
  69. hosts.bitbucket = {
  70. protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'],
  71. domain: 'bitbucket.org',
  72. treepath: 'src',
  73. blobpath: 'src',
  74. editpath: '?mode=edit',
  75. edittemplate: ({ domain, user, project, committish, treepath, path, editpath }) =>
  76. `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish || 'HEAD'), '/', path, editpath)}`,
  77. tarballtemplate: ({ domain, user, project, committish }) =>
  78. `https://${domain}/${user}/${project}/get/${maybeEncode(committish || 'HEAD')}.tar.gz`,
  79. extract: (url) => {
  80. let [, user, project, aux] = url.pathname.split('/', 4)
  81. if (['get'].includes(aux)) {
  82. return
  83. }
  84. if (project && project.endsWith('.git')) {
  85. project = project.slice(0, -4)
  86. }
  87. if (!user || !project) {
  88. return
  89. }
  90. return { user, project, committish: url.hash.slice(1) }
  91. },
  92. }
  93. hosts.gitlab = {
  94. protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'],
  95. domain: 'gitlab.com',
  96. treepath: 'tree',
  97. blobpath: 'tree',
  98. editpath: '-/edit',
  99. httpstemplate: ({ auth, domain, user, project, committish }) =>
  100. `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  101. tarballtemplate: ({ domain, user, project, committish }) =>
  102. `https://${domain}/${user}/${project}/repository/archive.tar.gz?ref=${maybeEncode(committish || 'HEAD')}`,
  103. extract: (url) => {
  104. const path = url.pathname.slice(1)
  105. if (path.includes('/-/') || path.includes('/archive.tar.gz')) {
  106. return
  107. }
  108. const segments = path.split('/')
  109. let project = segments.pop()
  110. if (project.endsWith('.git')) {
  111. project = project.slice(0, -4)
  112. }
  113. const user = segments.join('/')
  114. if (!user || !project) {
  115. return
  116. }
  117. return { user, project, committish: url.hash.slice(1) }
  118. },
  119. }
  120. hosts.gist = {
  121. protocols: ['git:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'],
  122. domain: 'gist.github.com',
  123. editpath: 'edit',
  124. sshtemplate: ({ domain, project, committish }) =>
  125. `git@${domain}:${project}.git${maybeJoin('#', committish)}`,
  126. sshurltemplate: ({ domain, project, committish }) =>
  127. `git+ssh://git@${domain}/${project}.git${maybeJoin('#', committish)}`,
  128. edittemplate: ({ domain, user, project, committish, editpath }) =>
  129. `https://${domain}/${user}/${project}${maybeJoin('/', maybeEncode(committish))}/${editpath}`,
  130. browsetemplate: ({ domain, project, committish }) =>
  131. `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`,
  132. browsetreetemplate: ({ domain, project, committish, path, hashformat }) =>
  133. `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}${maybeJoin('#', hashformat(path))}`,
  134. browseblobtemplate: ({ domain, project, committish, path, hashformat }) =>
  135. `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}${maybeJoin('#', hashformat(path))}`,
  136. docstemplate: ({ domain, project, committish }) =>
  137. `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`,
  138. httpstemplate: ({ domain, project, committish }) =>
  139. `git+https://${domain}/${project}.git${maybeJoin('#', committish)}`,
  140. filetemplate: ({ user, project, committish, path }) =>
  141. `https://gist.githubusercontent.com/${user}/${project}/raw${maybeJoin('/', maybeEncode(committish))}/${path}`,
  142. shortcuttemplate: ({ type, project, committish }) =>
  143. `${type}:${project}${maybeJoin('#', committish)}`,
  144. pathtemplate: ({ project, committish }) =>
  145. `${project}${maybeJoin('#', committish)}`,
  146. bugstemplate: ({ domain, project }) =>
  147. `https://${domain}/${project}`,
  148. gittemplate: ({ domain, project, committish }) =>
  149. `git://${domain}/${project}.git${maybeJoin('#', committish)}`,
  150. tarballtemplate: ({ project, committish }) =>
  151. `https://codeload.github.com/gist/${project}/tar.gz/${maybeEncode(committish || 'HEAD')}`,
  152. extract: (url) => {
  153. let [, user, project, aux] = url.pathname.split('/', 4)
  154. if (aux === 'raw') {
  155. return
  156. }
  157. if (!project) {
  158. if (!user) {
  159. return
  160. }
  161. project = user
  162. user = null
  163. }
  164. if (project.endsWith('.git')) {
  165. project = project.slice(0, -4)
  166. }
  167. return { user, project, committish: url.hash.slice(1) }
  168. },
  169. hashformat: function (fragment) {
  170. return fragment && 'file-' + formatHashFragment(fragment)
  171. },
  172. }
  173. hosts.sourcehut = {
  174. protocols: ['git+ssh:', 'https:'],
  175. domain: 'git.sr.ht',
  176. treepath: 'tree',
  177. blobpath: 'tree',
  178. filetemplate: ({ domain, user, project, committish, path }) =>
  179. `https://${domain}/${user}/${project}/blob/${maybeEncode(committish) || 'HEAD'}/${path}`,
  180. httpstemplate: ({ domain, user, project, committish }) =>
  181. `https://${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  182. tarballtemplate: ({ domain, user, project, committish }) =>
  183. `https://${domain}/${user}/${project}/archive/${maybeEncode(committish) || 'HEAD'}.tar.gz`,
  184. bugstemplate: () => null,
  185. extract: (url) => {
  186. let [, user, project, aux] = url.pathname.split('/', 4)
  187. // tarball url
  188. if (['archive'].includes(aux)) {
  189. return
  190. }
  191. if (project && project.endsWith('.git')) {
  192. project = project.slice(0, -4)
  193. }
  194. if (!user || !project) {
  195. return
  196. }
  197. return { user, project, committish: url.hash.slice(1) }
  198. },
  199. }
  200. for (const [name, host] of Object.entries(hosts)) {
  201. hosts[name] = Object.assign({}, defaults, host)
  202. }
  203. module.exports = hosts