Fix createProxyAgent on Electron 29

This commit is contained in:
Fedor Indutny 2024-03-15 09:42:13 -07:00 committed by GitHub
parent 1e275a917c
commit 73c0e0eb18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,7 @@
import { ProxyAgent } from 'proxy-agent';
import net from 'net';
import { URL } from 'url';
import type { LookupOneOptions, LookupAddress } from 'dns';
import type { LookupOptions, LookupAddress } from 'dns';
import { lookup } from 'dns/promises';
import * as log from '../logging/log';
@ -37,14 +37,7 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent {
}
const port = portStr ? parseInt(portStr, 10) : defaultPort;
async function happyLookup(
host: string,
opts: LookupOneOptions
): Promise<LookupAddress> {
if (opts.all) {
throw new Error('createProxyAgent: all=true lookup is not supported');
}
async function happyLookup(host: string): Promise<LookupAddress> {
const addresses = await lookup(host, { all: true });
// SOCKS 4/5 resolve target host before sending it to the proxy.
@ -79,18 +72,25 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent {
return address;
}
type CoercedCallbackType = (
err: NodeJS.ErrnoException | null,
address: string | Array<LookupAddress>,
family?: number
) => void;
async function happyLookupWithCallback(
host: string,
opts: LookupOneOptions,
callback: (
err: NodeJS.ErrnoException | null,
address: string,
family: number
) => void
opts: LookupOptions,
callback: CoercedCallbackType
): Promise<void> {
try {
const { address, family } = await happyLookup(host, opts);
callback(null, address, family);
const addr = await happyLookup(host);
if (opts.all) {
callback(null, [addr]);
} else {
const { address, family } = addr;
callback(null, address, family);
}
} catch (error) {
callback(error, '', -1);
}
@ -99,7 +99,14 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent {
return new ProxyAgent({
lookup:
port !== undefined
? (...args) => drop(happyLookupWithCallback(...args))
? (host, opts, callback) =>
drop(
happyLookupWithCallback(
host,
opts,
callback as CoercedCallbackType
)
)
: undefined,
getProxyForUrl() {
return proxyUrl;