mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-12 11:16:09 +09:00
Clean up the oopsie. Rebuild the blank exercises.
This commit is contained in:
+42
-15
@@ -4,15 +4,14 @@ var gitHosts = require('./git-host-info.js')
|
||||
var GitHost = module.exports = require('./git-host.js')
|
||||
|
||||
var protocolToRepresentationMap = {
|
||||
'git+ssh': 'sshurl',
|
||||
'git+https': 'https',
|
||||
'ssh': 'sshurl',
|
||||
'git': 'git'
|
||||
'git+ssh:': 'sshurl',
|
||||
'git+https:': 'https',
|
||||
'ssh:': 'sshurl',
|
||||
'git:': 'git'
|
||||
}
|
||||
|
||||
function protocolToRepresentation (protocol) {
|
||||
if (protocol.substr(-1) === ':') protocol = protocol.slice(0, -1)
|
||||
return protocolToRepresentationMap[protocol] || protocol
|
||||
return protocolToRepresentationMap[protocol] || protocol.slice(0, -1)
|
||||
}
|
||||
|
||||
var authProtocols = {
|
||||
@@ -26,6 +25,7 @@ var authProtocols = {
|
||||
var cache = {}
|
||||
|
||||
module.exports.fromUrl = function (giturl, opts) {
|
||||
if (typeof giturl !== 'string') return
|
||||
var key = giturl + JSON.stringify(opts || {})
|
||||
|
||||
if (!(key in cache)) {
|
||||
@@ -41,13 +41,13 @@ function fromUrl (giturl, opts) {
|
||||
isGitHubShorthand(giturl) ? 'github:' + giturl : giturl
|
||||
)
|
||||
var parsed = parseGitUrl(url)
|
||||
var shortcutMatch = url.match(new RegExp('^([^:]+):(?:(?:[^@:]+(?:[^@]+)?@)?([^/]*))[/](.+?)(?:[.]git)?($|#)'))
|
||||
var shortcutMatch = url.match(/^([^:]+):(?:[^@]+@)?(?:([^/]*)\/)?([^#]+)/)
|
||||
var matches = Object.keys(gitHosts).map(function (gitHostName) {
|
||||
try {
|
||||
var gitHostInfo = gitHosts[gitHostName]
|
||||
var auth = null
|
||||
if (parsed.auth && authProtocols[parsed.protocol]) {
|
||||
auth = decodeURIComponent(parsed.auth)
|
||||
auth = parsed.auth
|
||||
}
|
||||
var committish = parsed.hash ? decodeURIComponent(parsed.hash.substr(1)) : null
|
||||
var user = null
|
||||
@@ -55,22 +55,27 @@ function fromUrl (giturl, opts) {
|
||||
var defaultRepresentation = null
|
||||
if (shortcutMatch && shortcutMatch[1] === gitHostName) {
|
||||
user = shortcutMatch[2] && decodeURIComponent(shortcutMatch[2])
|
||||
project = decodeURIComponent(shortcutMatch[3])
|
||||
project = decodeURIComponent(shortcutMatch[3].replace(/\.git$/, ''))
|
||||
defaultRepresentation = 'shortcut'
|
||||
} else {
|
||||
if (parsed.host !== gitHostInfo.domain) return
|
||||
if (parsed.host && parsed.host !== gitHostInfo.domain && parsed.host.replace(/^www[.]/, '') !== gitHostInfo.domain) return
|
||||
if (!gitHostInfo.protocols_re.test(parsed.protocol)) return
|
||||
if (!parsed.path) return
|
||||
var pathmatch = gitHostInfo.pathmatch
|
||||
var matched = parsed.path.match(pathmatch)
|
||||
if (!matched) return
|
||||
if (matched[1] != null) user = decodeURIComponent(matched[1].replace(/^:/, ''))
|
||||
if (matched[2] != null) project = decodeURIComponent(matched[2])
|
||||
/* istanbul ignore else */
|
||||
if (matched[1] !== null && matched[1] !== undefined) {
|
||||
user = decodeURIComponent(matched[1].replace(/^:/, ''))
|
||||
}
|
||||
project = decodeURIComponent(matched[2])
|
||||
defaultRepresentation = protocolToRepresentation(parsed.protocol)
|
||||
}
|
||||
return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
|
||||
} catch (ex) {
|
||||
if (!(ex instanceof URIError)) throw ex
|
||||
/* istanbul ignore else */
|
||||
if (ex instanceof URIError) {
|
||||
} else throw ex
|
||||
}
|
||||
}).filter(function (gitHostInfo) { return gitHostInfo })
|
||||
if (matches.length !== 1) return
|
||||
@@ -100,9 +105,31 @@ function fixupUnqualifiedGist (giturl) {
|
||||
}
|
||||
|
||||
function parseGitUrl (giturl) {
|
||||
if (typeof giturl !== 'string') giturl = '' + giturl
|
||||
var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
|
||||
if (!matched) return url.parse(giturl)
|
||||
if (!matched) {
|
||||
var legacy = url.parse(giturl)
|
||||
// If we don't have url.URL, then sorry, this is just not fixable.
|
||||
// This affects Node <= 6.12.
|
||||
if (legacy.auth && typeof url.URL === 'function') {
|
||||
// git urls can be in the form of scp-style/ssh-connect strings, like
|
||||
// git+ssh://user@host.com:some/path, which the legacy url parser
|
||||
// supports, but WhatWG url.URL class does not. However, the legacy
|
||||
// parser de-urlencodes the username and password, so something like
|
||||
// https://user%3An%40me:p%40ss%3Aword@x.com/ becomes
|
||||
// https://user:n@me:p@ss:word@x.com/ which is all kinds of wrong.
|
||||
// Pull off just the auth and host, so we dont' get the confusing
|
||||
// scp-style URL, then pass that to the WhatWG parser to get the
|
||||
// auth properly escaped.
|
||||
var authmatch = giturl.match(/[^@]+@[^:/]+/)
|
||||
/* istanbul ignore else - this should be impossible */
|
||||
if (authmatch) {
|
||||
var whatwg = new url.URL(authmatch[0])
|
||||
legacy.auth = whatwg.username || ''
|
||||
if (whatwg.password) legacy.auth += ':' + whatwg.password
|
||||
}
|
||||
}
|
||||
return legacy
|
||||
}
|
||||
return {
|
||||
protocol: 'git+ssh:',
|
||||
slashes: true,
|
||||
|
||||
Reference in New Issue
Block a user