From 8705093aaa2e4e2eccd909a115b2c0001d5a1a3f Mon Sep 17 00:00:00 2001 From: Sandor Trombitas Date: Fri, 26 Jul 2024 12:00:07 +0300 Subject: [PATCH] refactor: consolidate download locations to single method --- ts-binary-wrapper/src/bootstrap.ts | 6 ++-- ts-binary-wrapper/src/common.ts | 22 +++++++------- ts-binary-wrapper/src/index.ts | 6 ++-- .../test/acceptance/basic.spec.ts | 2 ++ ts-binary-wrapper/test/unit/common.spec.ts | 30 +++++++++++-------- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/ts-binary-wrapper/src/bootstrap.ts b/ts-binary-wrapper/src/bootstrap.ts index ec2b5e0e38..ac35b210e5 100644 --- a/ts-binary-wrapper/src/bootstrap.ts +++ b/ts-binary-wrapper/src/bootstrap.ts @@ -10,8 +10,10 @@ const errorContextMessage = 'Download Error'; if (process.argv.includes('exec')) { const filenameShasum = config.getShasumFile(); - const downloadUrl = config.getDownloadLocation(); - const backupUrl = config.getBackupDownloadLocation(); + const { + downloadUrl, + backupUrl + } = config.getDownloadLocations(); const downloadError = await common.downloadWithBackup( downloadUrl, diff --git a/ts-binary-wrapper/src/common.ts b/ts-binary-wrapper/src/common.ts index d223fb3bd6..601c92884f 100644 --- a/ts-binary-wrapper/src/common.ts +++ b/ts-binary-wrapper/src/common.ts @@ -38,9 +38,9 @@ export class WrapperConfiguration { this.expectedSha256sum = expectedSha256sum; } - private buildDownloadUrl(baseUrl: string): string { - return `${baseUrl}/v${this.version}/${this.binaryName}`; - } + // private buildDownloadUrl(baseUrl: string): string { + // return `${baseUrl}/v${this.version}/${this.binaryName}`; + // } public getVersion(): string { return this.version; @@ -50,16 +50,14 @@ export class WrapperConfiguration { return this.binaryName; } - public getDownloadLocation(): string { + public getDownloadLocations(): { downloadUrl: string; backupUrl: string } { const baseUrl = 'https://downloads.snyk.io/cli'; + const backupUrl = 'https://static.snyk.io/cli'; - return this.buildDownloadUrl(baseUrl); - } - - public getBackupDownloadLocation(): string { - const baseUrl = 'https://static.snyk.io/cli'; - - return this.buildDownloadUrl(baseUrl); + return { + downloadUrl: `${baseUrl}/v${this.version}/${this.binaryName}`, + backupUrl: `${backupUrl}/v${this.version}/${this.binaryName}`, + }; } public getLocalLocation(): string { @@ -374,7 +372,7 @@ export async function downloadWithBackup( export async function logError( context: string, - err, + err: Error, printToConsole = true, ): Promise { if (isAnalyticsEnabled()) { diff --git a/ts-binary-wrapper/src/index.ts b/ts-binary-wrapper/src/index.ts index be7eb3cadd..2d3affc183 100644 --- a/ts-binary-wrapper/src/index.ts +++ b/ts-binary-wrapper/src/index.ts @@ -28,12 +28,14 @@ function run(executable: string): number { try { const config = common.getCurrentConfiguration(); const executable = config.getLocalLocation(); + const { downloadUrl, backupUrl } = config.getDownloadLocations(); if (!fs.existsSync(executable)) { console.error("Executable doesn't exist, trying to download."); - const downloadError = await common.downloadExecutable( - config.getDownloadLocation(), + const downloadError = await common.downloadWithBackup( + downloadUrl, + backupUrl, executable, config.getShasumFile(), ); diff --git a/ts-binary-wrapper/test/acceptance/basic.spec.ts b/ts-binary-wrapper/test/acceptance/basic.spec.ts index 1f4431c1b4..c58650f287 100644 --- a/ts-binary-wrapper/test/acceptance/basic.spec.ts +++ b/ts-binary-wrapper/test/acceptance/basic.spec.ts @@ -96,6 +96,8 @@ describe('Basic acceptance test', () => { { shell: true }, ); + console.log(resultIndex); + if (resultIndex.status != 0) { console.debug(resultIndex.stdout.toString()); console.debug(resultIndex.stderr.toString()); diff --git a/ts-binary-wrapper/test/unit/common.spec.ts b/ts-binary-wrapper/test/unit/common.spec.ts index 42d5eccfae..a3e4e271b4 100644 --- a/ts-binary-wrapper/test/unit/common.spec.ts +++ b/ts-binary-wrapper/test/unit/common.spec.ts @@ -146,7 +146,7 @@ describe('Configuration', () => { '1234abcdef', ); - const actualDownloadLocation = config.getDownloadLocation(); + const actualDownloadLocation = config.getDownloadLocations().downloadUrl; expect(actualDownloadLocation).toEqual(expectedDownloadLocation); const actualLocalLocation = config.getLocalLocation(); @@ -219,7 +219,7 @@ describe('Testing binary bootstrapper', () => { // download the shasum first, here we don't expect a shasum comparison const shasumDownload = await common.downloadExecutable( - config.getDownloadLocation() + shafileExtension, + config.getDownloadLocations().downloadUrl + shafileExtension, shasumFile, '', ); @@ -227,9 +227,10 @@ describe('Testing binary bootstrapper', () => { expect(fs.existsSync(shasumFile)).toBeTruthy(); const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile); + const { downloadUrl } = config.getDownloadLocations(); // download binary next and use previously downloaded shasum to check validity const binaryDownload = await common.downloadExecutable( - config.getDownloadLocation(), + downloadUrl, config.getLocalLocation(), expectedShasum, ); @@ -241,10 +242,9 @@ describe('Testing binary bootstrapper', () => { try { // check if the binary is executable - fs.accessSync(config.getLocalLocation(), fs.constants.X_OK); + expect(fs.accessSync(config.getLocalLocation(), fs.constants.X_OK)).not.toThrow(); } catch { // execution of binary not possible - expect(false).toBeTruthy(); } fs.unlinkSync(shasumFile); @@ -256,11 +256,12 @@ describe('Testing binary bootstrapper', () => { const config = new common.WrapperConfiguration('1.1080.0', binaryName, ''); const shasumFile = config.getLocalLocation() + Math.random() + shafileExtension; + const { downloadUrl } = config.getDownloadLocations(); // download the shasum first, here we don't expect a shasum comparison const shasumDownload = await common.downloadWithBackup( 'https://notdownloads.snyk.io/cli/v1.1080.0/snyk-macos.sha256', - config.getDownloadLocation() + shafileExtension, + downloadUrl + shafileExtension, shasumFile, '', ); @@ -269,8 +270,9 @@ describe('Testing binary bootstrapper', () => { const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile); // download binary next and use previously downloaded shasum to check validity - const binaryDownload = await common.downloadExecutable( - config.getDownloadLocation(), + const binaryDownload = await common.downloadWithBackup( + 'https://notdownloads.snyk.io/cli/v1.1080.0/snyk-macos', + downloadUrl, config.getLocalLocation(), expectedShasum, ); @@ -282,10 +284,9 @@ describe('Testing binary bootstrapper', () => { try { // check if the binary is executable - fs.accessSync(config.getLocalLocation(), fs.constants.X_OK); + expect(fs.accessSync(config.getLocalLocation(), fs.constants.X_OK)).not.toThrow(); } catch { // execution of binary not possible - expect(false).toBeTruthy(); } fs.unlinkSync(shasumFile); @@ -298,10 +299,12 @@ describe('Testing binary bootstrapper', () => { const config = new common.WrapperConfiguration('1.1080.0', binaryName, ''); const shasumFile = config.getLocalLocation() + Math.random() + shafileExtension; + const { downloadUrl } = config.getDownloadLocations(); + // download just any file and state a shasum expectation that never can be fullfilled const shasumDownload = await common.downloadExecutable( - config.getDownloadLocation() + shafileExtension, + downloadUrl + shafileExtension, shasumFile, 'incorrect-shasum', ); @@ -315,10 +318,11 @@ describe('Testing binary bootstrapper', () => { const config = new common.WrapperConfiguration('1.1080.0', binaryName, ''); const shasumFile = config.getLocalLocation() + Math.random() + shafileExtension; + const { downloadUrl } = config.getDownloadLocations(); // try to download a file that doesn't exis const shasumDownload = await common.downloadExecutable( - config.getDownloadLocation() + shafileExtension, + downloadUrl + shafileExtension, shasumFile, 'incorrect-shasum', ); @@ -339,7 +343,7 @@ describe('Testing binary bootstrapper', () => { }); }); -describe('isAnalyticsEnabled ', () => { +describe('isAnalyticsEnabled', () => { it('enabled', async () => { delete process.env.SNYK_DISABLE_ANALYTICS; expect(common.isAnalyticsEnabled()).toBeTruthy();