diff --git a/.release-please-manifest.json b/.release-please-manifest.json index b5e91573..684bdbed 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "5.2.0" + ".": "5.2.1" } diff --git a/CHANGELOG.md b/CHANGELOG.md index a3b3d261..80bb8355 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [5.2.1](https://github.com/npm/hosted-git-info/compare/v5.2.0...v5.2.1) (2022-10-27) + +### Bug Fixes + +* [`d2db548`](https://github.com/npm/hosted-git-info/commit/d2db5488ba372a12b642743cf07f7a88585130b0) [#177](https://github.com/npm/hosted-git-info/pull/177) only correct protocols when called from githost (@lukekarrys) + ## [5.2.0](https://github.com/npm/hosted-git-info/compare/v5.1.0...v5.2.0) (2022-10-26) ### Features diff --git a/lib/index.js b/lib/index.js index 50f35013..f6c66ff3 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 5f5ac4d3..7d5489c0 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 df6aea1e..00000000 --- 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/package.json b/package.json index 528d444e..ffd5c7d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hosted-git-info", - "version": "5.2.0", + "version": "5.2.1", "description": "Provides metadata and conversions from repository urls for GitHub, Bitbucket and GitLab", "main": "./lib/index.js", "repository": { diff --git a/test/parse-url.js b/test/parse-url.js index aab57a2b..54e7d169 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)) +})