From 4daef28f0b9d76c42ffc28a7f9a5d87109013c60 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Thu, 26 Jan 2017 14:55:05 -0800 Subject: [PATCH 1/3] Fix private urls using colon separator Closes #573, closes #2416. Related to #2384, #573. --- src/resolvers/exotics/git-resolver.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/resolvers/exotics/git-resolver.js b/src/resolvers/exotics/git-resolver.js index bc2e32877a..83d19616d0 100644 --- a/src/resolvers/exotics/git-resolver.js +++ b/src/resolvers/exotics/git-resolver.js @@ -11,6 +11,7 @@ import ExoticResolver from './exotic-resolver.js'; import Git from '../../util/git.js'; const urlParse = require('url').parse; +const urlFormat = require('url').format; // we purposefully omit https and http as those are only valid if they end in the .git extension const GIT_PROTOCOLS = ['git:', 'git+ssh:', 'git+https:', 'ssh:']; @@ -92,7 +93,15 @@ export default class GitResolver extends ExoticResolver { } const {config} = this; - const client = new Git(config, url, this.hash); + + if (parts.pathname) { + parts.pathname = parts.pathname.replace(/^\/:/, '/'); + } + + // $FlowFixMe: https://github.com/facebook/flow/issues/908 + const transformedUrl = urlFormat(parts); + + const client = new Git(config, transformedUrl, this.hash); const commit = await client.init(); async function tryRegistry(registry): Promise { @@ -103,12 +112,12 @@ export default class GitResolver extends ExoticResolver { return null; } - const json = await config.readJson(`${url}/${filename}`, () => JSON.parse(file)); + const json = await config.readJson(`${transformedUrl}/${filename}`, () => JSON.parse(file)); json._uid = commit; json._remote = { - resolved: `${url}#${commit}`, + resolved: `${transformedUrl}#${commit}`, type: 'git', - reference: url, + reference: transformedUrl, hash: commit, registry, }; @@ -131,6 +140,6 @@ export default class GitResolver extends ExoticResolver { } } - throw new MessageError(this.reporter.lang('couldntFindManifestIn', url)); + throw new MessageError(this.reporter.lang('couldntFindManifestIn', transformedUrl)); } } From dfe0ada588d9fe647f62e2f8296f116bcd425ad4 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Thu, 26 Jan 2017 15:14:55 -0800 Subject: [PATCH 2/3] Remove unused suppression --- src/util/normalize-manifest/fix.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util/normalize-manifest/fix.js b/src/util/normalize-manifest/fix.js index 523058cf26..755d5604a8 100644 --- a/src/util/normalize-manifest/fix.js +++ b/src/util/normalize-manifest/fix.js @@ -145,7 +145,6 @@ export default async function ( parts.hostname = parts.pathname; parts.pathname = ''; } - // $FlowFixMe: https://github.com/facebook/flow/issues/908 info.homepage = url.format(parts); } From 8f4a18fc77aad5ded9a8c5e6d58242abf390b7b2 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Fri, 3 Feb 2017 14:22:04 -0800 Subject: [PATCH 3/3] Move to dedicated method & add tests --- __tests__/resolvers/exotics/git-resolver.js | 45 +++++++++++++++++++++ src/resolvers/exotics/git-resolver.js | 15 ++++--- src/util/normalize-manifest/fix.js | 1 + 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 __tests__/resolvers/exotics/git-resolver.js diff --git a/__tests__/resolvers/exotics/git-resolver.js b/__tests__/resolvers/exotics/git-resolver.js new file mode 100644 index 0000000000..e0ff5c1b8a --- /dev/null +++ b/__tests__/resolvers/exotics/git-resolver.js @@ -0,0 +1,45 @@ +/* @flow */ + +import {parse} from 'url'; + +import GitResolver from '../../../src/resolvers/exotics/git-resolver.js'; + +test('GitResolver transformUrl method is defined', () => { + expect(GitResolver.transformUrl).toBeDefined(); +}); + +test('GitResolver transformUrl does not affect normal urls', () => { + + const urls = [ + 'git+https://github.com/npm-ml/ocaml.git#npm-4.02.3', + 'git+ssh://git@gitlab.com/user/repo.git', + 'git+ssh://git@github.com/user/repo.git', + 'git+ssh://username@private.url/sub/right-pad', + 'https://github.com/npm-ml/re', + 'https://github.com/npm-ml/ocaml.git#npm-4.02.3', + 'https://git@github.com/stevemao/left-pad.git', + 'https://bitbucket.org/hgarcia/node-bitbucket-api.git', + 'https://github.com/yarnpkg/yarn/releases/download/v0.18.1/yarn-v0.18.1.tar.gz', + 'https://github.com/babel/babel-loader.git#greenkeeper/cross-env-3.1.4', + 'package@git@bitbucket.org:team/repo.git', + ]; + + urls.forEach((url) => { + expect(GitResolver.transformUrl(parse(url))).toBe(url); + }); + +}); + +test('GitResolver transformUrl affect host colon separated urls', () => { + + const urls = [ + 'git+ssh://username@private.url:sub/right-pad', + 'git+ssh://private.url:sub/right-pad', + 'https://private.url:sub/right-pad', + ]; + + urls.forEach((url) => { + expect(GitResolver.transformUrl(parse(url))).toBe(url.replace(':sub', '/sub')); + }); + +}); diff --git a/src/resolvers/exotics/git-resolver.js b/src/resolvers/exotics/git-resolver.js index 83d19616d0..ba91a4b8ff 100644 --- a/src/resolvers/exotics/git-resolver.js +++ b/src/resolvers/exotics/git-resolver.js @@ -61,6 +61,14 @@ export default class GitResolver extends ExoticResolver { return false; } + // This transformUrl util is here to replace colon separators in the pathname + // from private urls. It takes the url parts retrieved using urlFormat and + // returns the associated url. Related to #573, introduced in #2519. + static transformUrl(parts) : string { + const pathname = parts.pathname ? parts.pathname.replace(/^\/:/, '/') : ''; + return urlFormat({...parts, pathname}); + } + async resolve(forked?: true): Promise { const {url} = this; @@ -94,12 +102,7 @@ export default class GitResolver extends ExoticResolver { const {config} = this; - if (parts.pathname) { - parts.pathname = parts.pathname.replace(/^\/:/, '/'); - } - - // $FlowFixMe: https://github.com/facebook/flow/issues/908 - const transformedUrl = urlFormat(parts); + const transformedUrl = GitResolver.transformUrl(parts); const client = new Git(config, transformedUrl, this.hash); const commit = await client.init(); diff --git a/src/util/normalize-manifest/fix.js b/src/util/normalize-manifest/fix.js index 755d5604a8..523058cf26 100644 --- a/src/util/normalize-manifest/fix.js +++ b/src/util/normalize-manifest/fix.js @@ -145,6 +145,7 @@ export default async function ( parts.hostname = parts.pathname; parts.pathname = ''; } + // $FlowFixMe: https://github.com/facebook/flow/issues/908 info.homepage = url.format(parts); }