Skip to content

Commit

Permalink
fix: add missing redirect handling (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow authored Oct 19, 2023
1 parent 9a29544 commit 1a4ea10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 19 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,24 @@ export function mapUrlPathToBucketPath(
const splitPath = url.pathname.split('/'); // ['', 'docs', 'asd', '123']
const basePath = splitPath[1]; // 'docs'

if (
REDIRECT_MAP.has(`${DOWNLOAD_PATH_PREFIX}/${splitPath[1]}/${splitPath[2]}`)
) {
const mappedDocs = `${DOCS_PATH_PREFIX}/${splitPath[2]}`;
const mappedRelease = `${DIST_PATH_PREFIX}/${splitPath[3]}`;
const mappedDist = `${DIST_PATH_PREFIX}/${splitPath[2]}`;

if (splitPath[1] === 'dist' && REDIRECT_MAP.has(mappedDist)) {
// All items in REDIRECT_MAP are three levels deep, that is asserted in tests
bucketPath = `${REDIRECT_MAP.get(mappedDist)}/${splitPath
.slice(3)
.join('/')}`;
} else if (splitPath[1] === 'docs' && REDIRECT_MAP.has(mappedDocs)) {
// All items in REDIRECT_MAP are three levels deep, that is asserted in tests
bucketPath = `${REDIRECT_MAP.get(
`${DOWNLOAD_PATH_PREFIX}/${splitPath[1]}/${splitPath[2]}`
)}/${splitPath.slice(3).join('/')}`;
bucketPath = `${REDIRECT_MAP.get(mappedDocs)}/${splitPath
.slice(3)
.join('/')}`;
} else if (splitPath[2] === 'release' && REDIRECT_MAP.has(mappedRelease)) {
bucketPath = `${REDIRECT_MAP.get(mappedRelease)}/${splitPath
.slice(4)
.join('/')}`;
} else if (basePath in urlToBucketPathMap) {
bucketPath = urlToBucketPathMap[basePath];
} else if (env.DIRECTORY_LISTING !== 'restricted') {
Expand Down Expand Up @@ -194,7 +205,8 @@ export function isExtensionless(path: string): boolean {
// 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.
return path.lastIndexOf('.') === -1;
// One exception is `latest-vXX.x`, which is a directory
return path.lastIndexOf('.') === -1 || path.toLowerCase().endsWith('.x');
}

/**
Expand Down
17 changes: 13 additions & 4 deletions tests/unit/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import assert from 'node:assert';
import { before, describe, it, skip } from 'node:test';
import { readFile } from 'node:fs/promises';
import { describe, it } from 'node:test';
import {
isDirectoryPath,
mapBucketPathToUrlPath,
Expand Down Expand Up @@ -48,14 +47,14 @@ describe('mapUrlPathToBucketPath', () => {
assert.strictEqual(result, 'nodejs/release/');
});

it('converts `/dist/latest` to `nodejs/release/latest`', () => {
it('converts `/dist/latest` to `nodejs/release/v.X.X.X`', () => {
const result = mapUrlPathToBucketPath(
new URL('http://localhost/dist/latest'),
{
DIRECTORY_LISTING: 'restricted',
}
);
assert.strictEqual(result, 'nodejs/release/latest');
assert.match(result ?? '', /^nodejs\/release\/v.\d+\.\d+\.\d+\/$/);
});

it('converts `/download` to `nodejs`', () => {
Expand All @@ -78,6 +77,16 @@ describe('mapUrlPathToBucketPath', () => {
assert.strictEqual(result, 'nodejs/release');
});

it('converts `/download/release/latest` to `nodejs/release/v.X.X.X`', () => {
const result = mapUrlPathToBucketPath(
new URL('http://localhost/download/release/latest'),
{
DIRECTORY_LISTING: 'restricted',
}
);
assert.match(result ?? '', /^nodejs\/release\/v.\d+\.\d+\.\d+\/$/);
});

it('converts `/docs/latest` to `nodejs/release/v.X.X.X/docs/`', () => {
const result = mapUrlPathToBucketPath(
new URL('http://localhost/docs/latest'),
Expand Down

0 comments on commit 1a4ea10

Please sign in to comment.