From 62ab0acec00fd7b259dd1c1868a0d23ed31e2e89 Mon Sep 17 00:00:00 2001 From: Lpmvb Date: Fri, 21 Dec 2018 19:53:29 +0800 Subject: [PATCH 1/2] feat: support host:port --- lib/giturl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/giturl.js b/lib/giturl.js index 8ddbbdc..1e6047a 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, From 494256bd6ee379c81003a731260f38b00aaac23e Mon Sep 17 00:00:00 2001 From: Lpmvb Date: Mon, 24 Dec 2018 22:30:20 +0800 Subject: [PATCH 2/2] fix support host:port --- lib/giturl.js | 15 ++++++++++++++- test/giturl.test.js | 3 +++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/giturl.js b/lib/giturl.js index 1e6047a..d0bd1b8 100644 --- a/lib/giturl.js +++ b/lib/giturl.js @@ -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 () {