Skip to content

Commit

Permalink
Simplify parameters in low-level net functions
Browse files Browse the repository at this point in the history
  • Loading branch information
datenreisender committed Jan 13, 2025
1 parent 418c421 commit 4196660
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions Changelog.minor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/main/apps/appChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/apps/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppInfo>(appUrl, true);
const appInfo = await downloadToJson<AppInfo>(appUrl);

if (path.basename(appUrl) !== `${appInfo.name}.json`) {
inRenderer.showErrorDialog(
Expand Down
2 changes: 1 addition & 1 deletion src/main/apps/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const getSourceJsonPath = (source: Source) =>
path.join(getAppsRootDir(source.name), 'source.json');

export const downloadSourceJson = (sourceUrl: SourceUrl) =>
downloadToJson<SourceJson>(sourceUrl, true);
downloadToJson<SourceJson>(sourceUrl);

const downloadSourceJsonToFile = async (source: Source) => {
const sourceJson = await downloadSourceJson(source.url);
Expand Down
41 changes: 17 additions & 24 deletions src/main/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer>((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}`);
}
Expand All @@ -76,16 +79,16 @@ 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)));
response.on('error', (error: Error) =>
reject(new Error(`Error when reading ${url}: ${error.message}`))
);
});
if (enableProxyLogin) {
if (options.enableProxyLogin) {
request.on('login', (authInfo, callback) => {
if (
authInfo.isProxy === false &&
Expand All @@ -107,24 +110,15 @@ const downloadToBuffer = (

export const downloadToJson = async <T>(
url: string,
enableProxyLogin: boolean,
bearer?: string
) =>
<T>(
JSON.parse(
(
await downloadToBuffer(url, enableProxyLogin, undefined, bearer)
).toString()
)
);
options: DownloadOptions = { enableProxyLogin: true }
) => <T>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);
};

Expand All @@ -138,7 +132,6 @@ export const getArtifactoryTokenInformation = async (token: string) =>
tokenInformationSchema.parse(
await downloadToJson<TokenInformation>(
'https://files.nordicsemi.com/access/api/v1/tokens/me',
true,
token
{ enableProxyLogin: true, bearer: token }
)
);

0 comments on commit 4196660

Please sign in to comment.