From 711e03043bb8b35db3fa0cde4b184b9e9648bde1 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 19 Oct 2023 19:34:27 +0300 Subject: [PATCH 1/2] fix: add missing redirect handling --- src/util.ts | 20 +++++++++++++------- tests/unit/util.test.ts | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/util.ts b/src/util.ts index a568d81..89778bc 100644 --- a/src/util.ts +++ b/src/util.ts @@ -66,13 +66,18 @@ 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') { @@ -194,7 +199,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'); } /** diff --git a/tests/unit/util.test.ts b/tests/unit/util.test.ts index 6a3fff8..036c454 100644 --- a/tests/unit/util.test.ts +++ b/tests/unit/util.test.ts @@ -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, @@ -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`', () => { @@ -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'), From 7cd8e1b14d7a834defe15f60007b3fca6de7e490 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 19 Oct 2023 19:56:56 +0300 Subject: [PATCH 2/2] format --- src/util.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/util.ts b/src/util.ts index 89778bc..11d10d3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -70,14 +70,20 @@ export function mapUrlPathToBucketPath( const mappedRelease = `${DIST_PATH_PREFIX}/${splitPath[3]}`; const mappedDist = `${DIST_PATH_PREFIX}/${splitPath[2]}`; - if (splitPath[1] === "dist" && REDIRECT_MAP.has(mappedDist)) { + 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)) { + 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(mappedDocs)}/${splitPath.slice(3).join('/')}`; - } else if (splitPath[2] === "release" && REDIRECT_MAP.has(mappedRelease)) { - bucketPath = `${REDIRECT_MAP.get(mappedRelease)}/${splitPath.slice(4).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') { @@ -199,7 +205,7 @@ 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. - // One exception is `latest-vXX.x`, which is a directory + // One exception is `latest-vXX.x`, which is a directory return path.lastIndexOf('.') === -1 || path.toLowerCase().endsWith('.x'); }