From d5e4649c4108e21db14763fa6c69bd635379560b Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:46:11 -0700 Subject: [PATCH] fix: don't throw a 401 when hitting /docs/ (#51) --- src/util.ts | 9 ++++++--- tests/e2e/directory.test.ts | 23 ++++++++++++++++++----- tests/unit/util.test.ts | 4 ++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/util.ts b/src/util.ts index 406e117..11b7766 100644 --- a/src/util.ts +++ b/src/util.ts @@ -51,9 +51,12 @@ export function mapUrlPathToBucketPath( env: Pick ): string | undefined { const urlToBucketPathMap: Record = { - dist: DIST_PATH_PREFIX + url.pathname.substring(5), - download: DOWNLOAD_PATH_PREFIX + url.pathname.substring(9), - api: API_PATH_PREFIX + (url.pathname.substring(4) || '/'), + 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 / }; diff --git a/tests/e2e/directory.test.ts b/tests/e2e/directory.test.ts index 62eae59..d5eb614 100644 --- a/tests/e2e/directory.test.ts +++ b/tests/e2e/directory.test.ts @@ -15,11 +15,14 @@ async function startS3Mock(): Promise { // later tests. If so, return a S3 response indicating that // the path exists. Otherwise return a S3 response indicating // that the path doesn't exist - if ( - ['nodejs/release/', 'nodejs/', 'metrics/'].includes( - url.searchParams.get('prefix')! - ) - ) { + const r2Prefix = url.searchParams.get('prefix')!; + + let doesFolderExist = + ['nodejs/release/', 'nodejs/', 'nodejs/docs/', 'metrics/'].includes( + r2Prefix + ) || r2Prefix.endsWith('/docs/api/'); + + if (doesFolderExist) { xmlFilePath += 'ListObjectsV2-exists.xml'; } else { xmlFilePath += 'ListObjectsV2-does-not-exist.xml'; @@ -107,6 +110,16 @@ describe('Directory Tests (Restricted Directory Listing)', () => { assert.strictEqual(res.status, 200); }); + it('allows `/docs/`', async () => { + const res = await mf.dispatchFetch(`${url}docs/`); + assert.strictEqual(res.status, 200); + }); + + it('allows `/api/`', async () => { + const res = await mf.dispatchFetch(`${url}api/`); + assert.strictEqual(res.status, 200); + }); + it('redirects `/metrics` to `/metrics/`', async () => { const originalRes = await mf.dispatchFetch(`${url}metrics`, { redirect: 'manual', diff --git a/tests/unit/util.test.ts b/tests/unit/util.test.ts index 8a92a8d..6f3b797 100644 --- a/tests/unit/util.test.ts +++ b/tests/unit/util.test.ts @@ -44,7 +44,7 @@ describe('mapUrlPathToBucketPath', () => { const result = mapUrlPathToBucketPath(new URL('http://localhost/dist'), { DIRECTORY_LISTING: 'restricted', }); - assert.strictEqual(result, 'nodejs/release'); + assert.strictEqual(result, 'nodejs/release/'); }); it('converts `/dist/latest` to `nodejs/release/latest`', () => { @@ -64,7 +64,7 @@ describe('mapUrlPathToBucketPath', () => { DIRECTORY_LISTING: 'restricted', } ); - assert.strictEqual(result, 'nodejs'); + assert.strictEqual(result, 'nodejs/'); }); it('converts `/download/releases` to `nodejs/releases`', () => {