|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const url = require('url') |
4 |
| - |
5 |
| -const safeUrl = (u) => { |
6 |
| - try { |
7 |
| - return new url.URL(u) |
8 |
| - } catch { |
9 |
| - // this fn should never throw |
10 |
| - } |
11 |
| -} |
12 |
| - |
13 |
| -const lastIndexOfBefore = (str, char, beforeChar) => { |
14 |
| - const startPosition = str.indexOf(beforeChar) |
15 |
| - return str.lastIndexOf(char, startPosition > -1 ? startPosition : Infinity) |
16 |
| -} |
17 |
| - |
18 |
| -// accepts input like git:github.com:user/repo and inserts the // after the first : |
19 |
| -const correctProtocol = (arg, protocols) => { |
20 |
| - const firstColon = arg.indexOf(':') |
21 |
| - const proto = arg.slice(0, firstColon + 1) |
22 |
| - if (Object.prototype.hasOwnProperty.call(protocols, proto)) { |
23 |
| - return arg |
24 |
| - } |
25 |
| - |
26 |
| - const firstAt = arg.indexOf('@') |
27 |
| - if (firstAt > -1) { |
28 |
| - if (firstAt > firstColon) { |
29 |
| - return `git+ssh://${arg}` |
30 |
| - } else { |
31 |
| - return arg |
32 |
| - } |
33 |
| - } |
34 |
| - |
35 |
| - const doubleSlash = arg.indexOf('//') |
36 |
| - if (doubleSlash === firstColon + 1) { |
37 |
| - return arg |
38 |
| - } |
39 |
| - |
40 |
| - return `${arg.slice(0, firstColon + 1)}//${arg.slice(firstColon + 1)}` |
41 |
| -} |
| 3 | +const parseUrl = require('./parse-url') |
42 | 4 |
|
43 | 5 | // look for github shorthand inputs, such as npm/cli
|
44 | 6 | const isGitHubShorthand = (arg) => {
|
@@ -71,49 +33,13 @@ const isGitHubShorthand = (arg) => {
|
71 | 33 | secondSlashOnlyAfterHash
|
72 | 34 | }
|
73 | 35 |
|
74 |
| -// attempt to correct an scp style url so that it will parse with `new URL()` |
75 |
| -const correctUrl = (giturl) => { |
76 |
| - // ignore @ that come after the first hash since the denotes the start |
77 |
| - // of a committish which can contain @ characters |
78 |
| - const firstAt = lastIndexOfBefore(giturl, '@', '#') |
79 |
| - // ignore colons that come after the hash since that could include colons such as: |
80 |
| - // git@github.com:user/package-2#semver:^1.0.0 |
81 |
| - const lastColonBeforeHash = lastIndexOfBefore(giturl, ':', '#') |
82 |
| - |
83 |
| - if (lastColonBeforeHash > firstAt) { |
84 |
| - // the last : comes after the first @ (or there is no @) |
85 |
| - // like it would in: |
86 |
| - // proto://hostname.com:user/repo |
87 |
| - // username@hostname.com:user/repo |
88 |
| - // :password@hostname.com:user/repo |
89 |
| - // username:password@hostname.com:user/repo |
90 |
| - // proto://username@hostname.com:user/repo |
91 |
| - // proto://:password@hostname.com:user/repo |
92 |
| - // proto://username:password@hostname.com:user/repo |
93 |
| - // then we replace the last : with a / to create a valid path |
94 |
| - giturl = giturl.slice(0, lastColonBeforeHash) + '/' + giturl.slice(lastColonBeforeHash + 1) |
95 |
| - } |
96 |
| - |
97 |
| - if (lastIndexOfBefore(giturl, ':', '#') === -1 && giturl.indexOf('//') === -1) { |
98 |
| - // we have no : at all |
99 |
| - // as it would be in: |
100 |
| - // username@hostname.com/user/repo |
101 |
| - // then we prepend a protocol |
102 |
| - giturl = `git+ssh://${giturl}` |
103 |
| - } |
104 |
| - |
105 |
| - return giturl |
106 |
| -} |
107 |
| - |
108 | 36 | module.exports = (giturl, opts, { gitHosts, protocols }) => {
|
109 | 37 | if (!giturl) {
|
110 | 38 | return
|
111 | 39 | }
|
112 | 40 |
|
113 |
| - const correctedUrl = isGitHubShorthand(giturl) |
114 |
| - ? `github:${giturl}` |
115 |
| - : correctProtocol(giturl, protocols) |
116 |
| - const parsed = safeUrl(correctedUrl) || safeUrl(correctUrl(correctedUrl)) |
| 41 | + const correctedUrl = isGitHubShorthand(giturl) ? `github:${giturl}` : giturl |
| 42 | + const parsed = parseUrl(correctedUrl, protocols) |
117 | 43 | if (!parsed) {
|
118 | 44 | return
|
119 | 45 | }
|
|
0 commit comments