diff --git a/lib/utils/index.js b/lib/utils/index.js index f3cb366c..8c7015c5 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -19,9 +19,9 @@ function isUrl (path) { } function getUrl (currentUrl, path) { - var pathObject = url.parse(path); + const pathObject = url.parse(path); if (isUrl(path) && !pathObject.protocol) { - var urlObject = url.parse(currentUrl); + const urlObject = url.parse(currentUrl); pathObject.protocol = urlObject.protocol; path = url.format(pathObject); } @@ -33,8 +33,8 @@ function getUnixPath (filepath) { } function getRelativePath (path1, path2) { - var dirname = path.dirname(path1); - var relativePath = path.relative(dirname, path2); + const dirname = path.dirname(path1); + const relativePath = path.relative(dirname, path2); return getUnixPath(relativePath); } @@ -45,8 +45,12 @@ function getRelativePath (path1, path2) { * @returns {string} decoded pathname */ function getPathnameFromUrl (u) { - var pathname = url.parse(u).pathname; - return decodeURI(pathname); + const pathname = url.parse(u).pathname; + try { + return decodeURI(pathname); + } catch (e) { + return pathname; + } } /** @@ -96,8 +100,8 @@ function getFilenameExtension (filepath) { function shortenFilename (filename) { if (filename.length >= MAX_FILENAME_LENGTH) { - var shortFilename = filename.substring(0, 20) + getFilenameExtension(filename); - logger.debug('shorten filename: ' + filename + ' -> ' + shortFilename); + const shortFilename = filename.substring(0, 20) + getFilenameExtension(filename); + logger.debug(`[utils] shorten filename: ${filename} -> ${shortFilename}`); return shortFilename; } return filename; @@ -110,7 +114,11 @@ function waitAllFulfilled (promises) { } function normalizeUrl (u, opts) { - return normalize(u, extend({removeTrailingSlash: false}, opts)); + try { + return normalize(u, extend({removeTrailingSlash: false}, opts)); + } catch (e) { + return u; + } } function urlsEqual (url1, url2) { @@ -118,7 +126,7 @@ function urlsEqual (url1, url2) { } function isUriSchemaSupported (path) { - var protocol = url.parse(path).protocol; + const protocol = url.parse(path).protocol; return !protocol || protocol && isUrl(path); } @@ -127,7 +135,7 @@ function getTypeByMime (mimeType) { } function getTypeByFilename (filename) { - var ext = getFilenameExtension(filename); + const ext = getFilenameExtension(filename); return typeByExt[ext]; } diff --git a/test/unit/utils/utils-test.js b/test/unit/utils/utils-test.js index d5c40520..6a49c964 100644 --- a/test/unit/utils/utils-test.js +++ b/test/unit/utils/utils-test.js @@ -91,6 +91,9 @@ describe('Utils', function () { it('should decode escaped chars', function () { utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equal('logo/logo-mobile (1).svg'); }); + it('should return path as is if url is malformed', () => { + utils.getFilepathFromUrl('https://example.co/%%IMAGE%%/logo.png').should.equal('%%IMAGE%%/logo.png'); + }); }); describe('#getHashFromUrl', function () { @@ -225,5 +228,12 @@ describe('Utils', function () { it('should return false for /path and /path/', function() { should(utils.urlsEqual('http://example.com/path', 'http://example.com/path/')).be.eql(false); }); - }) + }); + + describe('#normalizeUrl', () => { + it('should return original url if it is malformed', () => { + const malformedUrl = 'http://example.com/%%IMAGEURL%%/bar1q2blitz.png'; + should(utils.normalizeUrl(malformedUrl)).be.eql(malformedUrl); + }); + }); });