Lazy import proxy-agent

This commit is contained in:
Fedor Indutny 2024-03-20 11:05:10 -07:00 committed by GitHub
parent 83e8f4b59d
commit 091b50c414
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 60 additions and 39 deletions

View file

@ -602,7 +602,7 @@ export abstract class Updater {
this.logger.info(`downloadUpdate: Downloading signature ${signatureUrl}`);
const signature = Buffer.from(
await got(signatureUrl, getGotOptions()).text(),
await got(signatureUrl, await getGotOptions()).text(),
'hex'
);
@ -614,7 +614,10 @@ export abstract class Updater {
this.logger.info(
`downloadUpdate: Downloading blockmap ${blockMapUrl}`
);
const blockMap = await got(blockMapUrl, getGotOptions()).buffer();
const blockMap = await got(
blockMapUrl,
await getGotOptions()
).buffer();
await writeFile(tempBlockMapPath, blockMap);
} catch (error) {
this.logger.warn(
@ -751,7 +754,7 @@ export abstract class Updater {
targetUpdatePath: string,
updateOnProgress = false
): Promise<void> {
const downloadStream = got.stream(updateFileUrl, getGotOptions());
const downloadStream = got.stream(updateFileUrl, await getGotOptions());
const writeStream = createWriteStream(targetUpdatePath);
await new Promise<void>((resolve, reject) => {
@ -930,7 +933,7 @@ export function parseYaml(yaml: string): JSONUpdateSchema {
async function getUpdateYaml(): Promise<string> {
const targetUrl = getUpdateCheckUrl();
const body = await got(targetUrl, getGotOptions()).text();
const body = await got(targetUrl, await getGotOptions()).text();
if (!body) {
throw new Error('Got unexpected response back from update check');

View file

@ -15,6 +15,7 @@ import { strictAssert } from '../util/assert';
import { wrapEventEmitterOnce } from '../util/wrapEventEmitterOnce';
import type { LoggerType } from '../types/Logging';
import { getGotOptions } from './got';
import type { GotOptions } from './got';
import { checkIntegrity } from './util';
const gunzip = promisify(nativeGunzip);
@ -74,7 +75,7 @@ export type DownloadOptionsType = Readonly<{
logger?: LoggerType;
// Testing
gotOptions?: ReturnType<typeof getGotOptions>;
gotOptions?: GotOptions;
}>;
export type DownloadRangesOptionsType = Readonly<{
@ -86,7 +87,7 @@ export type DownloadRangesOptionsType = Readonly<{
chunkStatusCallback: (chunkSize: number) => void;
// Testing
gotOptions?: ReturnType<typeof getGotOptions>;
gotOptions?: GotOptions;
}>;
export function getBlockMapFileName(fileName: string): string {
@ -212,7 +213,7 @@ export async function prepareDownload({
const newBlockMapData = await got(
getBlockMapFileName(newUrl),
getGotOptions()
await getGotOptions()
).buffer();
const newBlockMap = await parseBlockMap(newBlockMapData);
@ -343,7 +344,7 @@ export async function downloadRanges(
logger,
abortSignal,
chunkStatusCallback,
gotOptions = getGotOptions(),
gotOptions = await getGotOptions(),
} = options;
logger?.info('updater/downloadRanges: downloading ranges', ranges.length);

View file

@ -24,13 +24,15 @@ export function getCertificateAuthority(): string {
return config.get('certificateAuthority');
}
export function getGotOptions(): GotOptions {
export type { GotOptions };
export async function getGotOptions(): Promise<GotOptions> {
const certificateAuthority = getCertificateAuthority();
const proxyUrl = getProxyUrl();
const agent = proxyUrl
? {
http: createProxyAgent(proxyUrl),
https: createProxyAgent(proxyUrl),
http: await createProxyAgent(proxyUrl),
https: await createProxyAgent(proxyUrl),
}
: {
http: new HTTPAgent(),