From f03bfbd3022c8f6283a991ff879ed97704ac35fa Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 26 Oct 2022 16:42:30 -0700 Subject: [PATCH] fix: only correct protocols when called from githost --- lib/index.js | 11 +++++++++-- lib/parse-url.js | 5 ++--- lib/protocols.js | 9 --------- test/parse-url.js | 9 ++++++++- 4 files changed, 19 insertions(+), 15 deletions(-) delete mode 100644 lib/protocols.js diff --git a/lib/index.js b/lib/index.js index 65d3d5f..a7339c2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 }) @@ -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 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 1299884..0000000 --- a/lib/protocols.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = () => ({ - '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 }, -}) 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)) +})