Skip to content

Commit

Permalink
Fix handling of a URL which has a non-hash component in the place of …
Browse files Browse the repository at this point in the history
…a hash (#37)

* Fix handling of a URL which has a non-hash component in the place of a hash

* Refactor to reuse the position of the hash in the file name
  • Loading branch information
danielgroves authored and XhmikosR committed Aug 30, 2019
1 parent f7d4cad commit 2e3d59e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ const staticify = (root, options) => {

const fileName = path.basename(p);
const fileNameParts = fileName.split('.');
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 &&
fileNameParts[fileNameParts.length - 2].length === HASH_LEN &&
re.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2]
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]);

Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2e3d59e

Please sign in to comment.