diff --git a/.changeset/nice-pillows-type.md b/.changeset/nice-pillows-type.md new file mode 100644 index 0000000..9d0045d --- /dev/null +++ b/.changeset/nice-pillows-type.md @@ -0,0 +1,5 @@ +--- +'proxy-agent': minor +--- + +Allow `getProxyForUrl()` option to return a Promise diff --git a/packages/proxy-agent/src/index.ts b/packages/proxy-agent/src/index.ts index c051f8a..496e185 100644 --- a/packages/proxy-agent/src/index.ts +++ b/packages/proxy-agent/src/index.ts @@ -22,7 +22,7 @@ type ValidProtocol = (typeof PROTOCOLS)[number]; type AgentConstructor = new (...args: never[]) => Agent; -type GetProxyForUrlCallback = (url: string) => string; +type GetProxyForUrlCallback = (url: string) => string | Promise; /** * Supported proxy types. @@ -115,7 +115,7 @@ export class ProxyAgent extends Agent { : 'http:'; const host = req.getHeader('host'); const url = new URL(req.path, `${protocol}//${host}`).href; - const proxy = this.getProxyForUrl(url); + const proxy = await this.getProxyForUrl(url); if (!proxy) { debug('Proxy not enabled for URL: %o', url); diff --git a/packages/proxy-agent/test/test.ts b/packages/proxy-agent/test/test.ts index 5f6de6e..2544d18 100644 --- a/packages/proxy-agent/test/test.ts +++ b/packages/proxy-agent/test/test.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as http from 'http'; import * as https from 'https'; import { URL } from 'url'; +import { promisify } from 'util'; import { once } from 'events'; import assert from 'assert'; import WebSocket, { WebSocketServer } from 'ws'; @@ -287,6 +288,33 @@ describe('ProxyAgent', () => { assert(gotCall); assert(requestUrl.href === urlParameter); }); + + it('should call provided function with asynchronous getProxyForUrl option', async () => { + let gotCall = false; + let urlParameter = ''; + httpsServer.once('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + const agent = new ProxyAgent({ + rejectUnauthorized: false, + getProxyForUrl: async(u) => { + gotCall = true; + urlParameter = u; + await promisify(setTimeout)(1); + return httpsProxyServerUrl.href; + }, + }); + const requestUrl = new URL('/test', httpsServerUrl); + const res = await req(requestUrl, { + agent, + rejectUnauthorized: false, + }); + const body = await json(res); + assert(httpsServerUrl.host === body.host); + assert(gotCall); + assert(requestUrl.href === urlParameter); + }); }); describe('"ws" module', () => {