Skip to content

Commit c87a228

Browse files
authored
feat: allow skipping access checks when initializing ProxyConfiguration (#474)
Closes: #472
1 parent e158c0f commit c87a228

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

packages/apify/src/actor.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,10 @@ export class Actor<Data extends Dictionary = Dictionary> {
13741374
* ```
13751375
* { useApifyProxy: false }
13761376
* ```
1377+
*
1378+
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
1379+
* This can make the init slower, to opt-out of this, use `checkAccess: false` (defaults to `true`).
1380+
*
13771381
* @ignore
13781382
*/
13791383
async createProxyConfiguration(
@@ -1383,7 +1387,8 @@ export class Actor<Data extends Dictionary = Dictionary> {
13831387
): Promise<ProxyConfiguration | undefined> {
13841388
// Compatibility fix for Input UI where proxy: None returns { useApifyProxy: false }
13851389
// Without this, it would cause proxy to use the zero config / auto mode.
1386-
const { useApifyProxy, ...options } = proxyConfigurationOptions;
1390+
const { useApifyProxy, checkAccess, ...options } =
1391+
proxyConfigurationOptions;
13871392
const dontUseApifyProxy = useApifyProxy === false;
13881393
const dontUseCustomProxies = !proxyConfigurationOptions.proxyUrls;
13891394

@@ -1393,7 +1398,7 @@ export class Actor<Data extends Dictionary = Dictionary> {
13931398

13941399
const proxyConfiguration = new ProxyConfiguration(options, this.config);
13951400

1396-
if (await proxyConfiguration.initialize()) {
1401+
if (await proxyConfiguration.initialize({ checkAccess })) {
13971402
return proxyConfiguration;
13981403
}
13991404

@@ -2183,6 +2188,9 @@ export class Actor<Data extends Dictionary = Dictionary> {
21832188
* ```
21842189
* { useApifyProxy: false }
21852190
* ```
2191+
*
2192+
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
2193+
* This can make the init slower, to opt-out of this, use `checkAccess: false` (defaults to `true`).
21862194
*/
21872195
static async createProxyConfiguration(
21882196
proxyConfigurationOptions: ProxyConfigurationOptions & {

packages/apify/src/proxy_configuration.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export interface ProxyConfigurationOptions
6565
ProxyConfigurationOptions,
6666
keyof CoreProxyConfigurationOptions | 'tieredProxyConfig'
6767
>[];
68+
69+
/**
70+
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
71+
* This can make the init slower, to opt-out of this, use `checkAccess: false` (defaults to `true`).
72+
*/
73+
checkAccess?: boolean;
6874
}
6975

7076
/**
@@ -256,8 +262,11 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
256262
*
257263
* You should use the {@apilink createProxyConfiguration} function to create a pre-initialized
258264
* `ProxyConfiguration` instance instead of calling this manually.
265+
*
266+
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
267+
* This can make the init slower, to opt-out of this, use `checkAccess: false`.
259268
*/
260-
async initialize(): Promise<boolean> {
269+
async initialize(options?: { checkAccess?: boolean }): Promise<boolean> {
261270
if (this.usesApifyProxy) {
262271
if (!this.password) {
263272
await this._setPasswordIfToken();
@@ -280,7 +289,9 @@ export class ProxyConfiguration extends CoreProxyConfiguration {
280289
}
281290
}
282291

283-
return this._checkAccess();
292+
if (options?.checkAccess !== false) {
293+
return this._checkAccess();
294+
}
284295
}
285296

286297
return true;

test/apify/proxy_configuration.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ describe('Actor.createProxyConfiguration()', () => {
567567
const proxyUrl = proxyUrlNoSession;
568568
const url = 'http://proxy.apify.com/?format=json';
569569
gotScrapingSpy.mockResolvedValueOnce({ body: status } as any);
570+
const checkAccessSpy = vitest.spyOn(
571+
ProxyConfiguration.prototype,
572+
// @ts-ignore protected method
573+
'_checkAccess',
574+
);
570575

571576
const proxyConfiguration =
572577
await Actor.createProxyConfiguration(basicOpts);
@@ -582,6 +587,7 @@ describe('Actor.createProxyConfiguration()', () => {
582587
expect(proxyConfiguration.hostname).toBe(hostname);
583588
// @ts-expect-error private property
584589
expect(proxyConfiguration.port).toBe(port);
590+
expect(checkAccessSpy).toHaveBeenCalled();
585591

586592
expect(gotScrapingSpy).toBeCalledWith({
587593
url,
@@ -591,6 +597,37 @@ describe('Actor.createProxyConfiguration()', () => {
591597
});
592598
});
593599

600+
test('disabling `checkAccess`', async () => {
601+
const status = { connected: true };
602+
const proxyUrl = proxyUrlNoSession;
603+
const url = 'http://proxy.apify.com/?format=json';
604+
gotScrapingSpy.mockResolvedValueOnce({ body: status } as any);
605+
const checkAccessSpy = vitest.spyOn(
606+
ProxyConfiguration.prototype,
607+
// @ts-ignore protected method
608+
'_checkAccess',
609+
);
610+
611+
const proxyConfiguration = await Actor.createProxyConfiguration({
612+
...basicOpts,
613+
checkAccess: false,
614+
});
615+
616+
expect(proxyConfiguration).toBeInstanceOf(ProxyConfiguration);
617+
// @ts-expect-error private property
618+
expect(proxyConfiguration.groups).toBe(groups);
619+
// @ts-expect-error private property
620+
expect(proxyConfiguration.countryCode).toBe(countryCode);
621+
// @ts-expect-error private property
622+
expect(proxyConfiguration.password).toBe(password);
623+
// @ts-expect-error private property
624+
expect(proxyConfiguration.hostname).toBe(hostname);
625+
// @ts-expect-error private property
626+
expect(proxyConfiguration.port).toBe(port);
627+
expect(checkAccessSpy).not.toHaveBeenCalled();
628+
expect(gotScrapingSpy).not.toHaveBeenCalled();
629+
});
630+
594631
test('should work without password (with token)', async () => {
595632
process.env.APIFY_TOKEN = '123456789';
596633
const opts: Dictionary = { ...basicOpts };

0 commit comments

Comments
 (0)