Skip to content

Commit

Permalink
fix: make redirects work as intended
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
flakey5 committed Oct 18, 2023
1 parent d5e4649 commit 4649f0a
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,77 @@ export function mapUrlPathToBucketPath(
url: URL,
env: Pick<Env, 'DIRECTORY_LISTING'>
): string | undefined {
const urlToBucketPathMap: Record<string, string> = {
dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),
download:
DOWNLOAD_PATH_PREFIX +
(url.pathname.substring('/download'.length) || '/'),
docs: DOCS_PATH_PREFIX + (url.pathname.substring('/docs'.length) || '/'),
api: API_PATH_PREFIX + (url.pathname.substring('/api'.length) || '/'),
metrics: url.pathname.substring(1), // substring to cut off the /
};
// const urlToBucketPathMap: Record<string, string> = {
// dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),
// download:
// DOWNLOAD_PATH_PREFIX +
// (url.pathname.substring('/download'.length) || '/'),
// docs: DOCS_PATH_PREFIX + (url.pathname.substring('/docs'.length) || '/'),
// api: API_PATH_PREFIX + (url.pathname.substring('/api'.length) || '/'),
// metrics: url.pathname.substring(1), // substring to cut off the /
// };

// Example: /docs/asd/123
let bucketPath: string | undefined;

const splitPath = url.pathname.split('/'); // ['', 'docs', 'asd', '123']
console.log(' splitPath: ' + splitPath)
const basePath = splitPath[1]; // 'docs'

if (
REDIRECT_MAP.has(`${DOWNLOAD_PATH_PREFIX}/${splitPath[1]}/${splitPath[2]}`)
) {
// 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('/')}`;
} else if (basePath in urlToBucketPathMap) {
bucketPath = urlToBucketPathMap[basePath];
const urlToBucketPathMap: Record<string, string[]> = {
dist: ['nodejs', 'release', splitPath[2]],
download: ['nodejs', splitPath[2]],
docs: ['nodejs', 'docs', splitPath[2]],
api: ['nodejs', 'docs', 'latest', 'api', url.pathname.substring('/api/'.length)],
// There are no redirects under /metrics/, don't need to split it
metrics: [url.pathname.substring(1)],
};

if (basePath in urlToBucketPathMap) {
// ['nodejs', 'release', 'latest-v20.x', ...]
const bucketPathParts = urlToBucketPathMap[basePath];
console.log(' bucketPathParts: ' + bucketPathParts)

// Path we check for if it's redirected
// 'nodejs/release/latest-v20.x'
const redirectablePath = `${bucketPathParts[0]}/${bucketPathParts[1]}${bucketPathParts.length > 2 ? `/${bucketPathParts[2]}` : ''}`;
console.log(' redirectablePath: ' + redirectablePath)

if (REDIRECT_MAP.has(redirectablePath)) {
bucketPath = REDIRECT_MAP.get(redirectablePath) //+ '/' + bucketPath.substring(redirectablePath.length + 1);
console.log(' redirected: ' + bucketPath);
} else {
bucketPath = bucketPathParts.join('/');
}

console.log(' final bucket path: ' + bucketPath)
} else if (env.DIRECTORY_LISTING !== 'restricted') {
bucketPath = url.pathname.substring(1);
}

// if (basePath in urlToBucketPathMap) {
// const bucketPathParts = urlToBucketPathMap[basePath];
// console.log(' initial bucket path: ' + bucketPath)

// // TODO: have urlToBucketPathMap return an array of this so we don't need to split it
// // nodejs/release/latest-v20.x
// const bucketPathSplit = bucketPath.split('/'); // ['nodejs', 'release', 'latest-v20.x', ...]

// // Path we check for if it's redirected
// // 'nodejs/release/latest-v20.x'
// const redirectablePath = `${bucketPathSplit[0]}/${bucketPathSplit[1]}${bucketPathSplit.length >= 3 ? `/${bucketPathSplit[2]}` : ''}`;
// console.log(' redirectablePath: ' + redirectablePath)

// if (REDIRECT_MAP.has(redirectablePath)) {
// bucketPath = REDIRECT_MAP.get(redirectablePath) + '/' + bucketPath.substring(redirectablePath.length + 1);
// console.log(' redirected: ' + bucketPath);
// }

// console.log(' final bucket path: ' + bucketPath)
// } else if (env.DIRECTORY_LISTING !== 'restricted') {
// bucketPath = url.pathname.substring(1);
// }

return bucketPath !== undefined ? decodeURIComponent(bucketPath) : undefined;
}

Expand Down

0 comments on commit 4649f0a

Please sign in to comment.