diff --git a/lib/index.js b/lib/index.js index 50f3501..f6c66ff 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 diff --git a/lib/parse-url.js b/lib/parse-url.js index 5f5ac4d..7d5489c 100644 --- a/lib/parse-url.js +++ b/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) @@ -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)) } diff --git a/lib/protocols.js b/lib/protocols.js deleted file mode 100644 index df6aea1..0000000 --- a/lib/protocols.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = (byShortcut = {}) => ({ - '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(byShortcut).reduce((acc, key) => { - acc[key] = { name: byShortcut[key] } - return acc - }, {}), -}) diff --git a/test/parse-url.js b/test/parse-url.js index aab57a2..54e7d16 100644 --- a/test/parse-url.js +++ b/test/parse-url.js @@ -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)) +})