Skip to content

Commit

Permalink
fix: only correct protocols when called from githost
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 27, 2022
1 parent 6dfa346 commit d2db548
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
16 changes: 15 additions & 1 deletion lib/index.js
Expand Up @@ -3,9 +3,23 @@ const gitHosts = require('./git-host-info.js')
const GitHost = module.exports = require('./git-host.js')
const LRU = require('lru-cache')
const parseUrl = require('./parse-url.js')
const protocols = require('./protocols')(gitHosts.byShortcut)

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

const protocols = {
'git+ssh:': { name: 'sshurl' },
'ssh:': { name: 'sshurl' },
'git+https:': { name: 'https', auth: true },
'git:': { auth: true },
'http:': { auth: true },
'https:': { auth: true },
'git+http:': { auth: true },
...Object.keys(gitHosts.byShortcut).reduce((acc, key) => {
acc[key] = { name: gitHosts.byShortcut[key] }
return acc
}, {}),
}

module.exports.fromUrl = function (giturl, opts) {
if (typeof giturl !== 'string') {
return
Expand Down
5 changes: 2 additions & 3 deletions lib/parse-url.js
@@ -1,5 +1,4 @@
const url = require('url')
const getProtocols = require('./protocols.js')

const lastIndexOfBefore = (str, char, beforeChar) => {
const startPosition = str.indexOf(beforeChar)
Expand Down Expand Up @@ -73,7 +72,7 @@ const correctUrl = (giturl) => {
return giturl
}

module.exports = (giturl, protocols = getProtocols()) => {
const withProtocol = correctProtocol(giturl, protocols)
module.exports = (giturl, protocols) => {
const withProtocol = protocols ? correctProtocol(giturl, protocols) : giturl
return safeUrl(withProtocol) || safeUrl(correctUrl(withProtocol))
}
13 changes: 0 additions & 13 deletions lib/protocols.js

This file was deleted.

9 changes: 8 additions & 1 deletion test/parse-url.js
Expand Up @@ -2,9 +2,16 @@ const t = require('tap')
const HostedGit = require('..')
const parseUrl = require('../lib/parse-url.js')

t.test('can parse git+ssh url by default', async t => {
t.test('can parse git+ssh urls', async t => {
// https://github.com/npm/cli/issues/5278
const u = 'git+ssh://git@abc:frontend/utils.git#6d45447e0c5eb6cd2e3edf05a8c5a9bb81950c79'
t.ok(parseUrl(u))
t.ok(HostedGit.parseUrl(u))
})

t.test('can parse file urls', async t => {
// https://github.com/npm/cli/pull/5758#issuecomment-1292753331
const u = 'file:../../../global-prefix/lib/node_modules/@myscope/bar'
t.ok(parseUrl(u))
t.ok(HostedGit.parseUrl(u))
})

0 comments on commit d2db548

Please sign in to comment.