Skip to content

Commit

Permalink
Fix: support protocol-relative registry URLs (#4347)
Browse files Browse the repository at this point in the history
**Summary**

Fixes matching protocol-relative registry URLs from config.
Reported here: #3987 (comment)

**Test plan**

Added one new test.
  • Loading branch information
BYK authored Sep 8, 2017
1 parent d3f223c commit 161f97e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ describe('isRequestToRegistry functional test', () => {
expect(npmRegistry.isRequestToRegistry('http://pkgs.host.com:80/foo/bar/baz', 'http://pkgs.host.com/bar/baz')).toBe(
true,
);
expect(npmRegistry.isRequestToRegistry('http://pkgs.host.com:80/foo/bar/baz', '//pkgs.host.com/bar/baz')).toBe(
true,
);
});
});

Expand Down
13 changes: 7 additions & 6 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import url from 'url';
import ini from 'ini';

const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
const REGEX_REGISTRY_PREFIX = /^https?:/;
const REGEX_REGISTRY_HTTP_PROTOCOL = /^https?:/i;
const REGEX_REGISTRY_PREFIX = /^(https?:)?\/\//i;
const REGEX_REGISTRY_SUFFIX = /registry\/?$/;

export const SCOPE_SEPARATOR = '%2f';
Expand Down Expand Up @@ -85,7 +86,7 @@ export default class NpmRegistry extends Registry {
}

getRequestUrl(registry: string, pathname: string): string {
const isUrl = /^https?:/i.test(pathname);
const isUrl = REGEX_REGISTRY_PREFIX.test(pathname);

if (isUrl) {
return pathname;
Expand Down Expand Up @@ -241,7 +242,7 @@ export default class NpmRegistry extends Registry {

getRegistry(packageIdent: string): string {
// Try extracting registry from the url, then scoped registry, and default registry
if (packageIdent.match(/^https?:/)) {
if (packageIdent.match(REGEX_REGISTRY_PREFIX)) {
const availableRegistries = this.getAvailableRegistries();
const registry = availableRegistries.find(registry => packageIdent.startsWith(registry));
if (registry) {
Expand Down Expand Up @@ -303,7 +304,7 @@ export default class NpmRegistry extends Registry {
}

getRegistryOption(registry: string, option: string): mixed {
const pre = REGEX_REGISTRY_PREFIX;
const pre = REGEX_REGISTRY_HTTP_PROTOCOL;
const suf = REGEX_REGISTRY_SUFFIX;

// When registry is used config scope, the trailing '/' is required
Expand All @@ -314,8 +315,8 @@ export default class NpmRegistry extends Registry {
// 3nd attempt, remove the 'registry/?' suffix of the registry URL
return (
this.getScopedOption(reg, option) ||
(reg.match(pre) && this.getRegistryOption(reg.replace(pre, ''), option)) ||
(reg.match(suf) && this.getRegistryOption(reg.replace(suf, ''), option))
(pre.test(reg) && this.getRegistryOption(reg.replace(pre, ''), option)) ||
(suf.test(reg) && this.getRegistryOption(reg.replace(suf, ''), option))
);
}

Expand Down

0 comments on commit 161f97e

Please sign in to comment.