Skip to content

Commit

Permalink
fix: use origin http protocol (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored Jun 15, 2023
1 parent be30f17 commit 3ba325d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/giturl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ exports.parse = function parse(sourceURL) {
}

var url = sourceURL;

var originProtocol;
try {
var uo = new URL(url);
originProtocol = uo.protocol;
} catch (_) {}

if (url.indexOf('@') >= 0) {
url = url.replace(/^[^@]+@/, ''); // `git@`` || `https://jpillora@` => ""
}
Expand All @@ -40,10 +47,18 @@ exports.parse = function parse(sourceURL) {
}

var host = item[1];
var protocol = HTTPS_HOSTS[host] ? 'https' : 'http';

var protocol;
if (HTTPS_HOSTS[host]) {
protocol = 'https:';
} else if ([ 'https:', 'http:' ].includes(originProtocol)) {
protocol = originProtocol;
} else {
protocol = 'http:';
}

// p1/p2/.../pn[.xxx]
var isContainGit = /\.git$/.test(sourceURL);
var url = isContainGit ? item[2] : item[2].split('/', 2).join('/');
return protocol + '://' + host + '/' + url;
return protocol + '//' + host + '/' + url;
};
6 changes: 6 additions & 0 deletions test/giturl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@ describe('giturl.test.js', function () {
giturl.parse('http://gist.github.com/10453258.git')
.should.equal('https://gist.github.com/10453258');
});

it('should parse protocol', function () {
giturl.parse('http://git.foo.com/foo/bar').should.equal('http://git.foo.com/foo/bar');
giturl.parse('https://git.foo.com/foo/bar').should.equal('https://git.foo.com/foo/bar');
giturl.parse('git://git.foo.com/foo/bar.git').should.equal('http://git.foo.com/foo/bar');
});
});
});

0 comments on commit 3ba325d

Please sign in to comment.