Skip to content

Commit

Permalink
Readd the custom-host-suffix feature
Browse files Browse the repository at this point in the history
  • Loading branch information
KidkArolis committed Jan 8, 2018
1 parent 9dcbc8f commit ea3fba0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ describe('request', () => {
const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});

test('should add authorization header if pathname does not match but custom-host-suffix is used', () => {
const url = 'https://some.other.registry/tarball/path/@testScope%2fyarn.tgz';
const config = {
'//some.other.registry/some/path/:_authToken': 'testScopedAuthToken',
'@testScope:registry': 'https://some.other.registry/some/path/',
'custom-host-suffix': 'some.other.registry',
};

const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});
});

const packageIdents = [
Expand Down
16 changes: 16 additions & 0 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import envReplace from '../util/env-replace.js';
import Registry from './base-registry.js';
import {addSuffix} from '../util/misc';
import {getPosixPath, resolveWithHome} from '../util/path';
import normalizeUrl from 'normalize-url';
import {default as userHome, home} from '../util/user-home-dir';
import path from 'path';
import url from 'url';
Expand Down Expand Up @@ -265,6 +266,21 @@ export default class NpmRegistry extends Registry {

let registry = this.getRegistry(packageIdent);

// Support for an existing feature: custom-host-suffix
// When custom-host-suffix global option is set, and request url matches
// this custom-host-suffix, we extract scope from the URL. We then use
// this scope to determine which auth token to use for that scope
if (packageIdent.match(REGEX_REGISTRY_PREFIX)) {
const customHostSuffix = this.getRegistryOrGlobalOption(registry || DEFAULT_REGISTRY, 'custom-host-suffix');
if (typeof customHostSuffix === 'string') {
const requestHost = url.parse(normalizeUrl(packageIdent)).host || '';
if (requestHost.endsWith(customHostSuffix)) {
const scope = this.getScope(packageIdent);
registry = this.getRegistry(scope + '/pkg');
}
}
}

if (!registry) {
return '';
}
Expand Down

0 comments on commit ea3fba0

Please sign in to comment.