Skip to content

Commit d2db548

Browse files
committedOct 27, 2022
fix: only correct protocols when called from githost
1 parent 6dfa346 commit d2db548

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed
 

‎lib/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@ const gitHosts = require('./git-host-info.js')
33
const GitHost = module.exports = require('./git-host.js')
44
const LRU = require('lru-cache')
55
const parseUrl = require('./parse-url.js')
6-
const protocols = require('./protocols')(gitHosts.byShortcut)
6+
77
const cache = new LRU({ max: 1000 })
88

9+
const protocols = {
10+
'git+ssh:': { name: 'sshurl' },
11+
'ssh:': { name: 'sshurl' },
12+
'git+https:': { name: 'https', auth: true },
13+
'git:': { auth: true },
14+
'http:': { auth: true },
15+
'https:': { auth: true },
16+
'git+http:': { auth: true },
17+
...Object.keys(gitHosts.byShortcut).reduce((acc, key) => {
18+
acc[key] = { name: gitHosts.byShortcut[key] }
19+
return acc
20+
}, {}),
21+
}
22+
923
module.exports.fromUrl = function (giturl, opts) {
1024
if (typeof giturl !== 'string') {
1125
return

‎lib/parse-url.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const url = require('url')
2-
const getProtocols = require('./protocols.js')
32

43
const lastIndexOfBefore = (str, char, beforeChar) => {
54
const startPosition = str.indexOf(beforeChar)
@@ -73,7 +72,7 @@ const correctUrl = (giturl) => {
7372
return giturl
7473
}
7574

76-
module.exports = (giturl, protocols = getProtocols()) => {
77-
const withProtocol = correctProtocol(giturl, protocols)
75+
module.exports = (giturl, protocols) => {
76+
const withProtocol = protocols ? correctProtocol(giturl, protocols) : giturl
7877
return safeUrl(withProtocol) || safeUrl(correctUrl(withProtocol))
7978
}

‎lib/protocols.js

-13
This file was deleted.

‎test/parse-url.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ const t = require('tap')
22
const HostedGit = require('..')
33
const parseUrl = require('../lib/parse-url.js')
44

5-
t.test('can parse git+ssh url by default', async t => {
5+
t.test('can parse git+ssh urls', async t => {
66
// https://github.com/npm/cli/issues/5278
77
const u = 'git+ssh://git@abc:frontend/utils.git#6d45447e0c5eb6cd2e3edf05a8c5a9bb81950c79'
88
t.ok(parseUrl(u))
99
t.ok(HostedGit.parseUrl(u))
1010
})
11+
12+
t.test('can parse file urls', async t => {
13+
// https://github.com/npm/cli/pull/5758#issuecomment-1292753331
14+
const u = 'file:../../../global-prefix/lib/node_modules/@myscope/bar'
15+
t.ok(parseUrl(u))
16+
t.ok(HostedGit.parseUrl(u))
17+
})

0 commit comments

Comments
 (0)
Failed to load comments.