Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix private urls using colon separator #2519

Merged
merged 3 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this 'GitResolver transformUrl replaces the colon on 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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can probably come up with a more descriptive name than "transform."

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));
}
}