Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only correct protocols when called from githost #177

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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))
})