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 306e817 commit f03bfbd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
11 changes: 9 additions & 2 deletions lib/index.js
Expand Up @@ -4,7 +4,6 @@ const LRU = require('lru-cache')
const hosts = require('./hosts.js')
const fromUrl = require('./from-url.js')
const parseUrl = require('./parse-url.js')
const getProtocols = require('./protocols.js')

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

Expand All @@ -22,7 +21,15 @@ class GitHost {
}

static #gitHosts = { byShortcut: {}, byDomain: {} }
static #protocols = getProtocols()
static #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 },
}

static addHost (name, host) {
GitHost.#gitHosts[name] = host
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))
}
9 changes: 0 additions & 9 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 f03bfbd

Please sign in to comment.