-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use hosted-git-info to parse registry urls (#5761)
Previously this was using `new URL` which would fail on some urls that `hosted-git-info` is able to parse. But if we still get a url that can't be parsed, we now set it to be removed from the tree instead of erroring. Fixes: #5278
- Loading branch information
1 parent
292156c
commit 26f3d0b
Showing
9 changed files
with
229 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const url = require('url') | ||
|
||
const lastIndexOfBefore = (str, char, beforeChar) => { | ||
const startPosition = str.indexOf(beforeChar) | ||
return str.lastIndexOf(char, startPosition > -1 ? startPosition : Infinity) | ||
} | ||
|
||
const safeUrl = (u) => { | ||
try { | ||
return new url.URL(u) | ||
} catch { | ||
// this fn should never throw | ||
} | ||
} | ||
|
||
// accepts input like git:github.com:user/repo and inserts the // after the first : | ||
const correctProtocol = (arg, protocols) => { | ||
const firstColon = arg.indexOf(':') | ||
const proto = arg.slice(0, firstColon + 1) | ||
if (Object.prototype.hasOwnProperty.call(protocols, proto)) { | ||
return arg | ||
} | ||
|
||
const firstAt = arg.indexOf('@') | ||
if (firstAt > -1) { | ||
if (firstAt > firstColon) { | ||
return `git+ssh://${arg}` | ||
} else { | ||
return arg | ||
} | ||
} | ||
|
||
const doubleSlash = arg.indexOf('//') | ||
if (doubleSlash === firstColon + 1) { | ||
return arg | ||
} | ||
|
||
return `${arg.slice(0, firstColon + 1)}//${arg.slice(firstColon + 1)}` | ||
} | ||
|
||
// attempt to correct an scp style url so that it will parse with `new URL()` | ||
const correctUrl = (giturl) => { | ||
// ignore @ that come after the first hash since the denotes the start | ||
// of a committish which can contain @ characters | ||
const firstAt = lastIndexOfBefore(giturl, '@', '#') | ||
// ignore colons that come after the hash since that could include colons such as: | ||
// git@github.com:user/package-2#semver:^1.0.0 | ||
const lastColonBeforeHash = lastIndexOfBefore(giturl, ':', '#') | ||
|
||
if (lastColonBeforeHash > firstAt) { | ||
// the last : comes after the first @ (or there is no @) | ||
// like it would in: | ||
// proto://hostname.com:user/repo | ||
// username@hostname.com:user/repo | ||
// :password@hostname.com:user/repo | ||
// username:password@hostname.com:user/repo | ||
// proto://username@hostname.com:user/repo | ||
// proto://:password@hostname.com:user/repo | ||
// proto://username:password@hostname.com:user/repo | ||
// then we replace the last : with a / to create a valid path | ||
giturl = giturl.slice(0, lastColonBeforeHash) + '/' + giturl.slice(lastColonBeforeHash + 1) | ||
} | ||
|
||
if (lastIndexOfBefore(giturl, ':', '#') === -1 && giturl.indexOf('//') === -1) { | ||
// we have no : at all | ||
// as it would be in: | ||
// username@hostname.com/user/repo | ||
// then we prepend a protocol | ||
giturl = `git+ssh://${giturl}` | ||
} | ||
|
||
return giturl | ||
} | ||
|
||
module.exports = (giturl, protocols) => { | ||
const withProtocol = protocols ? correctProtocol(giturl, protocols) : giturl | ||
return safeUrl(withProtocol) || safeUrl(correctUrl(withProtocol)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.