Skip to content

Commit f03bfbd

Browse files
committed
fix: only correct protocols when called from githost
1 parent 306e817 commit f03bfbd

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const LRU = require('lru-cache')
44
const hosts = require('./hosts.js')
55
const fromUrl = require('./from-url.js')
66
const parseUrl = require('./parse-url.js')
7-
const getProtocols = require('./protocols.js')
87

98
const cache = new LRU({ max: 1000 })
109

@@ -22,7 +21,15 @@ class GitHost {
2221
}
2322

2423
static #gitHosts = { byShortcut: {}, byDomain: {} }
25-
static #protocols = getProtocols()
24+
static #protocols = {
25+
'git+ssh:': { name: 'sshurl' },
26+
'ssh:': { name: 'sshurl' },
27+
'git+https:': { name: 'https', auth: true },
28+
'git:': { auth: true },
29+
'http:': { auth: true },
30+
'https:': { auth: true },
31+
'git+http:': { auth: true },
32+
}
2633

2734
static addHost (name, host) {
2835
GitHost.#gitHosts[name] = host

lib/parse-url.js

Lines changed: 2 additions & 3 deletions
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

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/parse-url.js

Lines changed: 8 additions & 1 deletion
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)