diff --git a/resources/cf-url-rewriter/cf-url-rewriter.ts b/resources/cf-url-rewriter/cf-url-rewriter.ts index 499ae1d0..e30676ae 100644 --- a/resources/cf-url-rewriter/cf-url-rewriter.ts +++ b/resources/cf-url-rewriter/cf-url-rewriter.ts @@ -20,10 +20,8 @@ export async function handler(event: CloudFrontRequestEvent, context: Context, c if (request.uri.includes("/latest/")) { const urisplit = request.uri.split("/latest"); - const index = urisplit[0].lastIndexOf("/"); - const versionNumber = urisplit[0].substring(index + 1).replace(/\./g, ''); - const replaceIndexPath = getIndexFilePath(versionNumber, urisplit); + const replaceIndexPath = getIndexFilePath(urisplit); const indexUri = request.uri.replace(/\/latest\/.*/, replaceIndexPath); @@ -74,11 +72,19 @@ function errorResponse() { }; } -function getIndexFilePath(versionNumber: string, urisplit: string[]): string { - if (versionNumber <= "2111") { - return '/index.json'; - } else { +function getIndexFilePath(urisplit: string[]): string { + const version = urisplit[0].substring(urisplit[0].lastIndexOf("/") + 1); + const [major, minor, patch] = version.split('.').map(Number); + + if ( + (major == 1 && minor == 3 && patch >= 15) || + (major == 2 && minor >= 12) || + (major >= 3) + ) { const latestPath = urisplit[1].split("/").slice(0, 4).join("/"); return `/index${latestPath}/index.json`; + + } else { + return '/index.json'; } } diff --git a/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts b/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts index ab5af38d..61817d70 100644 --- a/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts +++ b/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts @@ -109,6 +109,30 @@ test('handler with latest url and /fool(latest)bar/ keyword and ci keyword', asy ); }); +test('handler with latest url and ci keyword for 1.3.x', async () => { + const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.3.15/latest/linux/x64/tar/opensearch.tar.gz'); + const context = {} as Context; + const callback = jest.fn() as CloudFrontRequestCallback; + + (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '4678' }); + + await handler(event, context, callback); + + expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.3.15/index/linux/x64/tar/index.json'); + + expect(callback).toHaveBeenCalledWith( + null, + { + headers: { + 'cache-control': [{ key: 'Cache-Control', value: 'max-age=3600' }], + location: [{ key: 'Location', value: '/ci/dbc/bundle-build-dashboards/1.3.15/4678/linux/x64/tar/opensearch.tar.gz' }], + }, + status: '302', + statusDescription: 'Moved temporarily', + }, + ); +}); + test('handler without latest url and without ci keyword', async () => { const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/'); const context = {} as Context;