Skip to content

Commit dc39cc2

Browse files
authored
fix: proxyUrls list can contain null (#3142)
Closes #3136
1 parent 2a480fe commit dc39cc2

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

packages/core/src/proxy_configuration.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export interface ProxyConfigurationFunction {
1010
(sessionId: string | number, options?: { request?: Request }): string | null | Promise<string | null>;
1111
}
1212

13+
type UrlList = (string | null)[];
14+
1315
export interface ProxyConfigurationOptions {
1416
/**
1517
* An array of custom proxy URLs to be rotated.
1618
* Custom proxies are not compatible with Apify Proxy and an attempt to use both
1719
* configuration options will cause an error to be thrown on initialize.
1820
*/
19-
proxyUrls?: string[];
21+
proxyUrls?: UrlList;
2022

2123
/**
2224
* Custom function that allows you to generate the new proxy URL dynamically. It gets the `sessionId` as a parameter and an optional parameter with the `Request` object when applicable.
@@ -37,7 +39,7 @@ export interface ProxyConfigurationOptions {
3739
*
3840
* Use `null` as a proxy URL to disable the proxy for the given tier.
3941
*/
40-
tieredProxyUrls?: (string | null)[][];
42+
tieredProxyUrls?: UrlList[];
4143
}
4244

4345
export interface TieredProxy {
@@ -201,9 +203,9 @@ class ProxyTierTracker {
201203
export class ProxyConfiguration {
202204
isManInTheMiddle = false;
203205
protected nextCustomUrlIndex = 0;
204-
protected proxyUrls?: string[];
205-
protected tieredProxyUrls?: (string | null)[][];
206-
protected usedProxyUrls = new Map<string, string>();
206+
protected proxyUrls?: UrlList;
207+
protected tieredProxyUrls?: UrlList[];
208+
protected usedProxyUrls = new Map<string, string | null>();
207209
protected newUrlFunction?: ProxyConfigurationFunction;
208210
protected log = log.child({ prefix: 'ProxyConfiguration' });
209211
protected domainTiers = new Map<string, ProxyTierTracker>();
@@ -233,7 +235,7 @@ export class ProxyConfiguration {
233235
ow(
234236
rest,
235237
ow.object.exactShape({
236-
proxyUrls: ow.optional.array.nonEmpty.ofType(ow.string.url),
238+
proxyUrls: ow.optional.array.nonEmpty.ofType(ow.any(ow.string.url, ow.null)),
237239
newUrlFunction: ow.optional.function,
238240
tieredProxyUrls: ow.optional.array.nonEmpty.ofType(
239241
ow.array.nonEmpty.ofType(ow.any(ow.string.url, ow.null)),
@@ -389,14 +391,14 @@ export class ProxyConfiguration {
389391
return this._handleTieredUrl(sessionId ?? cryptoRandomObjectId(6), options).proxyUrl ?? undefined;
390392
}
391393

392-
return this._handleCustomUrl(sessionId);
394+
return this._handleCustomUrl(sessionId) ?? undefined;
393395
}
394396

395397
/**
396398
* Handles custom url rotation with session
397399
*/
398-
protected _handleCustomUrl(sessionId?: string): string {
399-
let customUrlToUse: string;
400+
protected _handleCustomUrl(sessionId?: string): string | null {
401+
let customUrlToUse: string | null;
400402

401403
if (!sessionId) {
402404
return this.proxyUrls![this.nextCustomUrlIndex++ % this.proxyUrls!.length];

test/core/crawlers/browser_crawler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ describe('BrowserCrawler', () => {
849849
// @ts-expect-error Accessing private property
850850
const proxiesToUse = proxyConfiguration.proxyUrls!;
851851
for (const proxyUrl of proxiesToUse) {
852-
expect(browserProxies.includes(new URL(proxyUrl).href.slice(0, -1))).toBeTruthy();
852+
expect(browserProxies.includes(new URL(proxyUrl!).href.slice(0, -1))).toBeTruthy();
853853
}
854854

855855
delete process.env[ENV_VARS.PROXY_PASSWORD];

0 commit comments

Comments
 (0)