diff --git a/lib/giturl.js b/lib/giturl.js index 8ddbbdc..d0bd1b8 100644 --- a/lib/giturl.js +++ b/lib/giturl.js @@ -14,8 +14,8 @@ * Module dependencies. */ -// host[:/]n1/n2 -var RE = /^([^:\/]+)[:\/](.+)$/i; +// host[:/]n1/n2 | host:port[:/]n1/n2 +var RE = /^([^:\/]+:\d{1,5}|[^:\/]+)[:\/](.+)$/i; var HTTPS_HOSTS = { 'github.com': 1, @@ -45,5 +45,18 @@ exports.parse = function parse(sourceURL) { // p1/p2/.../pn[.xxx] var isContainGit = /\.git$/.test(sourceURL); var url = isContainGit ? item[2] : item[2].split('/', 2).join('/'); - return protocol + '://' + host + '/' + url; + + var port = null; + if (host.indexOf(':') >= 0) { + port = host.split(':')[1]; + host = host.split(':')[0]; + if (parseInt(port) > 65535) { + url = port + '/' + url; + port = null; + } + } + + return port === null ? + protocol + '://' + host + '/' + url : + protocol + '://' + host + ':' + port + '/' + url; }; diff --git a/test/giturl.test.js b/test/giturl.test.js index e83abdb..209feb1 100644 --- a/test/giturl.test.js +++ b/test/giturl.test.js @@ -35,6 +35,9 @@ describe('giturl.test.js', function () { giturl.parse('git@github.com:cnpm/cnpm').should.equal('https://github.com/cnpm/cnpm'); giturl.parse('git@gitcafe.com:fengmk2/cnpm.git').should.equal('https://gitcafe.com/fengmk2/cnpm'); giturl.parse('git@gist.github.com:3135914.git').should.equal('https://gist.github.com/3135914'); + giturl.parse('git@gist.github.com:3135.git').should.equal('https://gist.github.com/3135'); + giturl.parse('git@gitlab.com:65535/logger.git').should.equal('http://gitlab.com:65535/logger'); + giturl.parse('git@gitlab.com:65536/logger.git').should.equal('http://gitlab.com/65536/logger'); }); it('should parse not git url', function () {