Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing redirect handling #53

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading