From 65c54681022a5848ebc4efc4cb78aac7d5cedc95 Mon Sep 17 00:00:00 2001 From: Marko Schulz Date: Mon, 13 Jan 2025 13:17:44 +0100 Subject: [PATCH] Simplify parameters in low-level net functions --- Changelog.minor.md | 1 + src/main/apps/appChanges.ts | 5 ++++- src/main/apps/apps.ts | 2 +- src/main/apps/sources.ts | 2 +- src/main/net.ts | 41 +++++++++++++++---------------------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Changelog.minor.md b/Changelog.minor.md index 5c4ba64c..b2cec456 100644 --- a/Changelog.minor.md +++ b/Changelog.minor.md @@ -18,6 +18,7 @@ release the new version. - #1057: Show J-Link warning also for local apps. - #1063: Download J-Link from Artifactory. - #1070: Use `ExternalLink` from shared. +- #1070: Simplified option passing in low-level net functions. ## 5.1.0 diff --git a/src/main/apps/appChanges.ts b/src/main/apps/appChanges.ts index 98bddaf5..6315a176 100644 --- a/src/main/apps/appChanges.ts +++ b/src/main/apps/appChanges.ts @@ -239,7 +239,10 @@ const download = async (app: AppSpec, version?: string) => { const packageFilePath = path.join(getAppsRootDir(app.source), fileName); await Promise.all([ - downloadToFile(tarballUrl, packageFilePath, true, app), + downloadToFile(tarballUrl, packageFilePath, { + enableProxyLogin: true, + app, + }), ...assertPreparedNrfutilModules(app, versionToInstall.nrfutilModules), ]); await verifyShasum(packageFilePath, versionToInstall.shasum); diff --git a/src/main/apps/apps.ts b/src/main/apps/apps.ts index 32fb01d6..4dd37b64 100644 --- a/src/main/apps/apps.ts +++ b/src/main/apps/apps.ts @@ -167,7 +167,7 @@ export const downloadLatestAppInfos = async () => { export const downloadAppInfos = async (source: Source) => { const downloadableApps = await Promise.all( getAppUrls(source).map(async appUrl => { - const appInfo = await downloadToJson(appUrl, true); + const appInfo = await downloadToJson(appUrl); if (path.basename(appUrl) !== `${appInfo.name}.json`) { inRenderer.showErrorDialog( diff --git a/src/main/apps/sources.ts b/src/main/apps/sources.ts index 3e8d0402..50b58349 100644 --- a/src/main/apps/sources.ts +++ b/src/main/apps/sources.ts @@ -130,7 +130,7 @@ const getSourceJsonPath = (source: Source) => path.join(getAppsRootDir(source.name), 'source.json'); export const downloadSourceJson = (sourceUrl: SourceUrl) => - downloadToJson(sourceUrl, true); + downloadToJson(sourceUrl); const downloadSourceJsonToFile = async (source: Source) => { const sourceJson = await downloadSourceJson(source.url); diff --git a/src/main/net.ts b/src/main/net.ts index 6505a9c0..34585f64 100644 --- a/src/main/net.ts +++ b/src/main/net.ts @@ -38,18 +38,21 @@ const determineBearer = (url: string) => export type NetError = Error & { statusCode?: number }; -const downloadToBuffer = ( - url: string, - enableProxyLogin: boolean, - app: AppSpec | undefined = undefined, - bearer = determineBearer(url) -) => +type DownloadOptions = { + enableProxyLogin: boolean; + app?: AppSpec; + bearer?: string; +}; + +const downloadToBuffer = (url: string, options: DownloadOptions) => new Promise((resolve, reject) => { const request = net.request({ url, session: session.fromPartition(NET_SESSION_NAME), }); request.setHeader('pragma', 'no-cache'); + + const bearer = options.bearer ?? determineBearer(url); if (bearer) { request.setHeader('Authorization', `Bearer ${bearer}`); } @@ -76,8 +79,8 @@ const downloadToBuffer = ( response.on('data', data => { addToBuffer(data); progress += data.length; - if (app) { - reportInstallProgress(app, progress, downloadSize); + if (options.app != null) { + reportInstallProgress(options.app, progress, downloadSize); } }); response.on('end', () => resolve(Buffer.concat(buffer))); @@ -85,7 +88,7 @@ const downloadToBuffer = ( reject(new Error(`Error when reading ${url}: ${error.message}`)) ); }); - if (enableProxyLogin) { + if (options.enableProxyLogin) { request.on('login', (authInfo, callback) => { if ( authInfo.isProxy === false && @@ -107,24 +110,15 @@ const downloadToBuffer = ( export const downloadToJson = async ( url: string, - enableProxyLogin: boolean, - bearer?: string -) => - ( - JSON.parse( - ( - await downloadToBuffer(url, enableProxyLogin, undefined, bearer) - ).toString() - ) - ); + options: DownloadOptions = { enableProxyLogin: true } +) => JSON.parse((await downloadToBuffer(url, options)).toString()); export const downloadToFile = async ( url: string, filePath: string, - enableProxyLogin: boolean | undefined = false, - app: AppSpec | undefined = undefined + options: DownloadOptions = { enableProxyLogin: false } ) => { - const buffer = await downloadToBuffer(url, enableProxyLogin, app); + const buffer = await downloadToBuffer(url, options); await fs.writeFile(filePath, buffer); }; @@ -138,7 +132,6 @@ export const getArtifactoryTokenInformation = async (token: string) => tokenInformationSchema.parse( await downloadToJson( 'https://files.nordicsemi.com/access/api/v1/tokens/me', - true, - token + { enableProxyLogin: true, bearer: token } ) );