0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-17 23:45:29 -05:00

fix: updated combine url fix method (#1647)

* test: added more combineBaseUrl tests

* fix: optimized and updated combineBaseUrl method logic
This commit is contained in:
Giedrius Grabauskas 2020-01-08 10:30:23 +02:00 committed by Juan Picado @jotadeveloper
parent 32aabca641
commit 4f43347b50
2 changed files with 14 additions and 4 deletions

View file

@ -141,12 +141,19 @@ export function validateMetadata(object: Package, name: string): Package {
* @return {String} base registry url
*/
export function combineBaseUrl(protocol: string, host: string | void, prefix?: string | void): string {
let result = `${protocol}://${host}`;
const result = `${protocol}://${host}`;
if (prefix) {
prefix = prefix.replace(/\/$/, '');
const prefixOnlySlash = prefix === '/';
if (prefix && !prefixOnlySlash) {
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
result = prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix;
if (prefix.startsWith('/')) {
return `${result}${prefix}`;
}
return prefix;
}
return result;

View file

@ -212,7 +212,10 @@ describe('Utilities', () => {
});
test('should create a base url for registry', () => {
expect(combineBaseUrl("http", 'domain', '')).toEqual('http://domain');
expect(combineBaseUrl("http", 'domain', '/')).toEqual('http://domain');
expect(combineBaseUrl("http", 'domain', '/prefix/')).toEqual('http://domain/prefix');
expect(combineBaseUrl("http", 'domain', '/prefix/deep')).toEqual('http://domain/prefix/deep');
expect(combineBaseUrl("http", 'domain', 'only-prefix')).toEqual('only-prefix');
});