From 4649f0af70fa0c7332a0ce27b2a71f311470f633 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Wed, 18 Oct 2023 15:10:18 -0700 Subject: [PATCH] fix: make redirects work as intended Fixes #50 --- src/util.ts | 78 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/src/util.ts b/src/util.ts index 11b7766..61e4455 100644 --- a/src/util.ts +++ b/src/util.ts @@ -50,35 +50,77 @@ export function mapUrlPathToBucketPath( url: URL, env: Pick ): string | undefined { - const urlToBucketPathMap: Record = { - 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 = { + // 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 = { + 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; }