From 3ba9cf2032088938390d143ecca0e5237dbd573f Mon Sep 17 00:00:00 2001 From: Daniel Groves Date: Fri, 30 Aug 2019 10:07:36 +0100 Subject: [PATCH 1/3] Fix handling of a URL which has a non-hash componenet in the place of a hash --- index.js | 6 ++++-- test/index.js | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index baf1549..b5ba670 100644 --- a/index.js +++ b/index.js @@ -92,11 +92,13 @@ const staticify = (root, options) => { const fileName = path.basename(p); const fileNameParts = fileName.split('.'); + const fileNameHash = fileNameParts[fileNameParts.length - 2]; const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i'); + const reResult = re.exec(fileNameHash); if (fileNameParts.length >= 3 && - fileNameParts[fileNameParts.length - 2].length === HASH_LEN && - re.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2] + fileNameHash.length === HASH_LEN && + (reResult && reResult[0] === fileNameHash) ) { const stripped = fileNameParts.slice(0, fileNameParts.length - 2); diff --git a/test/index.js b/test/index.js index 0f3fdce..5a4de81 100644 --- a/test/index.js +++ b/test/index.js @@ -32,10 +32,18 @@ describe('.stripVersion', () => { staticify(ROOT).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js')); }); + it('should not fail when the path contains a 7 character string that is not a hash', () => { + staticify(ROOT).stripVersion(path.normalize('/script.abcdefg.html')).should.equal(path.normalize('/script.abcdefg.html')); + }); + it('should strip the (long) hash from a path when necessary', () => { staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.4e2502b01a4c92b0a51b1a5a3271eab6.js')).should.equal(path.normalize('/script.js')); staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js')); }); + + it('should not fail when the path contains a 32 character string that is not a hash', () => { + staticify(ROOT).stripVersion(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')).should.equal(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')); + }); }); describe('.getVersionedPath', () => { From 1b82b75417af00e6f818725c73a0c0f4ef2e516b Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 30 Aug 2019 12:18:04 +0300 Subject: [PATCH 2/3] Update index.js --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index b5ba670..7095df2 100644 --- a/index.js +++ b/index.js @@ -96,8 +96,7 @@ const staticify = (root, options) => { const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i'); const reResult = re.exec(fileNameHash); - if (fileNameParts.length >= 3 && - fileNameHash.length === HASH_LEN && + if (fileNameParts.length >= 3 && fileNameHash.length === HASH_LEN && (reResult && reResult[0] === fileNameHash) ) { const stripped = fileNameParts.slice(0, fileNameParts.length - 2); From 7b65a1a20ed029769238675c59765976771b2328 Mon Sep 17 00:00:00 2001 From: Daniel Groves Date: Fri, 30 Aug 2019 10:28:48 +0100 Subject: [PATCH 3/3] Refactor to reuse the position of the hash in the file name --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7095df2..9229b86 100644 --- a/index.js +++ b/index.js @@ -92,14 +92,15 @@ const staticify = (root, options) => { const fileName = path.basename(p); const fileNameParts = fileName.split('.'); - const fileNameHash = fileNameParts[fileNameParts.length - 2]; + const fileNameHashPosition = fileNameParts.length - 2; + const fileNameHash = fileNameParts[fileNameHashPosition]; const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i'); const reResult = re.exec(fileNameHash); if (fileNameParts.length >= 3 && fileNameHash.length === HASH_LEN && (reResult && reResult[0] === fileNameHash) ) { - const stripped = fileNameParts.slice(0, fileNameParts.length - 2); + const stripped = fileNameParts.slice(0, fileNameHashPosition); stripped.push(fileNameParts[fileNameParts.length - 1]);