Skip to content

Commit

Permalink
fix: don't treat vX.X.X folders as files
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Nov 11, 2023
1 parent 1a4ea10 commit 49a2053
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,35 @@ export function hasTrailingSlash(path: string): boolean {
}

export function isExtensionless(path: string): boolean {
// `path.lastIndexOf('.') == -1` is a Node-specific
// heuristic here. There aren't any files that don't
// have file extensions, so, if there are no file extensions
// specified in the url, treat it like a directory.
// One exception is `latest-vXX.x`, which is a directory
return path.lastIndexOf('.') === -1 || path.toLowerCase().endsWith('.x');
// `path.lastIndexOf('.') == -1` is a Node-specific heuristic here.
// There aren't any files in the bucket that don't have file extensions,
// so, if there is no file extension specified in the url, treat it
// like a directory.
//
// Two exceptions:
// - `latest-vXX.x` directories
// - `vX.X.X` directories

const extensionDelimiter = path.lastIndexOf('.');
if (extensionDelimiter === -1) {
return true;
}

const fileExtension = path.substring(extensionDelimiter + 1); // +1 to remove the `.`

// `latest-vXX.x` directory
if (fileExtension.toLowerCase() === 'x') {
return true;
}

// `vX.X.X` directory
// File extensions generally aren't numbers, so if we can parse this to
// one we can be pretty certain that it's a directory
if (!isNaN(Number.parseInt(fileExtension))) {
return true;
}

return false;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ describe('isDirectoryPath', () => {
assert.strictEqual(isDirectoryPath('/dist'), true);
});

it('returns true for `/dist/latest-v20.x`', () => {
assert.strictEqual(isDirectoryPath('/dist/latest-v20.x'), true);
});

it('returns true for `/dist/v20.20.2`', () => {
assert.strictEqual(isDirectoryPath('/dist/v20.20.2'), true);
});

it('returns false for `/dist/index.json`', () => {
assert.strictEqual(isDirectoryPath('/dist/index.json'), false);
});
Expand Down

0 comments on commit 49a2053

Please sign in to comment.