Skip to content

Commit

Permalink
Fix private urls using colon separator (yarnpkg#2519)
Browse files Browse the repository at this point in the history
* Fix private urls using colon separator

Closes yarnpkg#573, closes yarnpkg#2416.

Related to yarnpkg#2384, yarnpkg#573.

* Remove unused suppression

* Move to dedicated method & add tests
  • Loading branch information
apercu authored and mnsn committed Feb 15, 2017
1 parent a28dcbb commit 1569f4c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
45 changes: 45 additions & 0 deletions __tests__/resolvers/exotics/git-resolver.js
Original file line number Diff line number Diff line change
@@ -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://[email protected]/user/repo.git',
'git+ssh://[email protected]/user/repo.git',
'git+ssh://[email protected]/sub/right-pad',
'https://github.com/npm-ml/re',
'https://github.com/npm-ml/ocaml.git#npm-4.02.3',
'https://[email protected]/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@[email protected]: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://[email protected]: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'));
});

});
22 changes: 17 additions & 5 deletions src/resolvers/exotics/git-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:'];
Expand Down Expand Up @@ -60,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<Manifest> {
const {url} = this;

Expand Down Expand Up @@ -92,7 +101,10 @@ export default class GitResolver extends ExoticResolver {
}

const {config} = this;
const client = new Git(config, url, this.hash);

const transformedUrl = GitResolver.transformUrl(parts);

const client = new Git(config, transformedUrl, this.hash);
const commit = await client.init();

async function tryRegistry(registry): Promise<?Manifest> {
Expand All @@ -103,12 +115,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,
};
Expand All @@ -131,6 +143,6 @@ export default class GitResolver extends ExoticResolver {
}
}

throw new MessageError(this.reporter.lang('couldntFindManifestIn', url));
throw new MessageError(this.reporter.lang('couldntFindManifestIn', transformedUrl));
}
}

0 comments on commit 1569f4c

Please sign in to comment.