Skip to content

Commit

Permalink
fix: type errors when playwright is not installed (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Oct 30, 2022
1 parent c46351c commit de9db0c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/browser-pool/src/playwright/playwright-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type { Cookie } from '@crawlee/types';
import { BrowserController } from '../abstract-classes/browser-controller';
import { anonymizeProxySugar } from '../anonymize-proxy';
import type { PlaywrightPlugin } from './playwright-plugin';
import type { SafeParameters } from '../utils';

const tabIds = new WeakMap<Page, number>();
const keyFromTabId = (tabId: string | number) => `.${tabId}.`;

export class PlaywrightController extends BrowserController<BrowserType, Parameters<BrowserType['launch']>[0], Browser> {
export class PlaywrightController extends BrowserController<BrowserType, SafeParameters<BrowserType['launch']>[0], Browser> {
normalizeProxyOptions(proxyUrl: string | undefined, pageOptions: any): Record<string, unknown> {
if (!proxyUrl) {
return {};
Expand All @@ -28,7 +29,7 @@ export class PlaywrightController extends BrowserController<BrowserType, Paramet
};
}

protected async _newPage(contextOptions?: Parameters<Browser['newPage']>[0]): Promise<Page> {
protected async _newPage(contextOptions?: SafeParameters<Browser['newPage']>[0]): Promise<Page> {
if (contextOptions !== undefined && !this.launchContext.useIncognitoPages && !this.launchContext.experimentalContainers) {
throw new Error('A new page can be created with provided context only when using incognito pages or experimental containers.');
}
Expand Down
5 changes: 3 additions & 2 deletions packages/browser-pool/src/playwright/playwright-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getLocalProxyAddress } from '../proxy-server';
import { anonymizeProxySugar } from '../anonymize-proxy';
import { createProxyServerForContainers } from '../container-proxy-server';
import { loadFirefoxAddon } from './load-firefox-addon';
import type { SafeParameters } from '../utils';

const getFreePort = async () => {
return new Promise<number>((resolve, reject) => {
Expand All @@ -27,7 +28,7 @@ const getFreePort = async () => {
// taacPath = browser-pool/dist/tab-as-a-container
const taacPath = path.join(__dirname, '..', 'tab-as-a-container');

export class PlaywrightPlugin extends BrowserPlugin<BrowserType, Parameters<BrowserType['launch']>[0], PlaywrightBrowser> {
export class PlaywrightPlugin extends BrowserPlugin<BrowserType, SafeParameters<BrowserType['launch']>[0], PlaywrightBrowser> {
private _browserVersion?: string;
_containerProxyServer?: Awaited<ReturnType<typeof createProxyServerForContainers>>;

Expand Down Expand Up @@ -171,7 +172,7 @@ export class PlaywrightPlugin extends BrowserPlugin<BrowserType, Parameters<Brow
return browser;
}

protected _createController(): BrowserController<BrowserType, Parameters<BrowserType['launch']>[0], PlaywrightBrowser> {
protected _createController(): BrowserController<BrowserType, SafeParameters<BrowserType['launch']>[0], PlaywrightBrowser> {
return new PlaywrightController(this);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/browser-pool/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export type UnwrapPromise<T> = T extends PromiseLike<infer R> ? UnwrapPromise<R>

export function noop(..._args: unknown[]): void {}

/**
* This is required when using optional dependencies.
* Importing a type gives `any`, but `Parameters<any>` gives `unknown[]` instead of `any`
*/
export type SafeParameters<T extends (...args: any) => any> = unknown[] extends Parameters<T> ? any : Parameters<T>;

export type InferBrowserPluginArray<
// The original array input
Input extends readonly unknown[],
Expand Down

0 comments on commit de9db0c

Please sign in to comment.