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