Skip to content

Commit

Permalink
refactor!: make node deps lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Apr 23, 2024
1 parent 2416154 commit 9602765
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 118 deletions.
8 changes: 6 additions & 2 deletions docs/api/puppeteer.defaultargs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ sidebar_label: defaultArgs
#### Signature:

```typescript
defaultArgs: (options?: import("puppeteer-core/internal/puppeteer-core.js").BrowserLaunchArgumentOptions | undefined) => string[]
defaultArgs: (
options?:
| import('puppeteer-core/internal/puppeteer-core.js').BrowserLaunchArgumentOptions
| undefined
) => Promise<string[]>;
```

## Parameters
Expand Down Expand Up @@ -41,4 +45,4 @@ _(Optional)_
</tbody></table>
**Returns:**

string\[\]
Promise&lt;string\[\]&gt;
4 changes: 2 additions & 2 deletions docs/api/puppeteer.executablepath.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ executablePath: (
channel?:
| import('puppeteer-core/internal/puppeteer-core.js').ChromeReleaseChannel
| undefined
) => string;
) => Promise<string>;
```

## Parameters
Expand Down Expand Up @@ -45,4 +45,4 @@ _(Optional)_
</tbody></table>
**Returns:**

string
Promise&lt;string&gt;
4 changes: 2 additions & 2 deletions docs/api/puppeteer.puppeteer.defaultargs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sidebar_label: Puppeteer.defaultArgs

```typescript
class Puppeteer {
defaultArgs(options?: BrowserLaunchArgumentOptions): string[];
defaultArgs(options?: BrowserLaunchArgumentOptions): Promise<string[]>;
}
```

Expand Down Expand Up @@ -43,6 +43,6 @@ _(Optional)_ Set of configurable options to set on the browser.
</tbody></table>
**Returns:**

string\[\]
Promise&lt;string\[\]&gt;

The default flags that Chromium will be launched with.
4 changes: 2 additions & 2 deletions docs/api/puppeteer.puppeteer.executablepath.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The default executable path.

```typescript
class Puppeteer {
executablePath(channel?: ChromeReleaseChannel): string;
executablePath(channel?: ChromeReleaseChannel): Promise<string>;
}
```

Expand Down Expand Up @@ -45,4 +45,4 @@ _(Optional)_
</tbody></table>
**Returns:**

string
Promise&lt;string&gt;
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@ import {
} from '@puppeteer/browsers';

import type {Browser} from '../api/Browser.js';
import {_connectToBrowser} from '../common/BrowserConnector.js';
import type {Configuration} from '../common/Configuration.js';
import type {
ConnectOptions,
BrowserConnectOptions,
} from '../common/ConnectOptions.js';
import {
type CustomQueryHandler,
customQueryHandlers,
} from '../common/CustomQueryHandler.js';
import type {Product} from '../common/Product.js';
import {PUPPETEER_REVISIONS} from '../revisions.js';

import {ChromeLauncher} from './ChromeLauncher.js';
import {FirefoxLauncher} from './FirefoxLauncher.js';
import type {
BrowserLaunchArgumentOptions,
ChromeReleaseChannel,
LaunchOptions,
} from './LaunchOptions.js';
import type {ProductLauncher} from './ProductLauncher.js';
} from '../node/LaunchOptions.js';
import type {ProductLauncher} from '../node/ProductLauncher.js';
import {PUPPETEER_REVISIONS} from '../revisions.js';

import {_connectToBrowser} from './BrowserConnector.js';
import type {Configuration} from './Configuration.js';
import type {ConnectOptions, BrowserConnectOptions} from './ConnectOptions.js';
import {
type CustomQueryHandler,
customQueryHandlers,
} from './CustomQueryHandler.js';
import type {Product} from './Product.js';

/**
* @public
Expand Down Expand Up @@ -231,16 +226,16 @@ export class Puppeteer {
*
* @param options - Options to configure launching behavior.
*/
launch(options: PuppeteerLaunchOptions = {}): Promise<Browser> {
async launch(options: PuppeteerLaunchOptions = {}): Promise<Browser> {
const {product = this.defaultProduct} = options;
this.#lastLaunchedProduct = product;
return this.#launcher.launch(options);
return await (await this.#getLauncher()).launch(options);
}

/**
* @internal
*/
get #launcher(): ProductLauncher {
async #getLauncher(): Promise<ProductLauncher> {
if (
this.#_launcher &&
this.#_launcher.product === this.lastLaunchedProduct
Expand All @@ -250,10 +245,12 @@ export class Puppeteer {
switch (this.lastLaunchedProduct) {
case 'chrome':
this.defaultBrowserRevision = PUPPETEER_REVISIONS.chrome;
const {ChromeLauncher} = await import('../node/ChromeLauncher.js');
this.#_launcher = new ChromeLauncher(this);
break;
case 'firefox':
this.defaultBrowserRevision = PUPPETEER_REVISIONS.firefox;
const {FirefoxLauncher} = await import('../node/FirefoxLauncher.js');
this.#_launcher = new FirefoxLauncher(this);
break;
default:
Expand All @@ -265,8 +262,8 @@ export class Puppeteer {
/**
* The default executable path.
*/
executablePath(channel?: ChromeReleaseChannel): string {
return this.#launcher.executablePath(channel);
async executablePath(channel?: ChromeReleaseChannel): Promise<string> {
return (await this.#getLauncher()).executablePath(channel);
}

/**
Expand Down Expand Up @@ -306,25 +303,15 @@ export class Puppeteer {
return this.configuration.defaultProduct ?? 'chrome';
}

/**
* @deprecated Do not use as this field as it does not take into account
* multiple browsers of different types. Use
* {@link Puppeteer.defaultProduct | defaultProduct} or
* {@link Puppeteer.lastLaunchedProduct | lastLaunchedProduct}.
*
* @returns The name of the browser that is under automation.
*/
get product(): string {
return this.#launcher.product;
}

/**
* @param options - Set of configurable options to set on the browser.
*
* @returns The default flags that Chromium will be launched with.
*/
defaultArgs(options: BrowserLaunchArgumentOptions = {}): string[] {
return this.#launcher.defaultArgs(options);
async defaultArgs(
options: BrowserLaunchArgumentOptions = {}
): Promise<string[]> {
return (await this.#getLauncher()).defaultArgs(options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/node/ChromeLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@puppeteer/browsers';

import type {Browser} from '../api/Browser.js';
import type {Puppeteer, PuppeteerLaunchOptions} from '../common/Puppeteer.js';
import {debugError} from '../common/util.js';
import {assert} from '../util/assert.js';

Expand All @@ -23,7 +24,6 @@ import type {
ChromeReleaseChannel,
} from './LaunchOptions.js';
import {ProductLauncher, type ResolvedLaunchArgs} from './ProductLauncher.js';
import type {Puppeteer, PuppeteerLaunchOptions} from './Puppeteer.js';
import {rm} from './util/fs.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/node/FirefoxLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import {
Browser,
} from '@puppeteer/browsers';

import type {Puppeteer, PuppeteerLaunchOptions} from '../common/Puppeteer.js';
import {debugError} from '../common/util.js';
import {assert} from '../util/assert.js';

import type {BrowserLaunchArgumentOptions} from './LaunchOptions.js';
import {ProductLauncher, type ResolvedLaunchArgs} from './ProductLauncher.js';
import type {Puppeteer, PuppeteerLaunchOptions} from './Puppeteer.js';
import {rm} from './util/fs.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/node/ProductLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {CdpBrowser} from '../cdp/Browser.js';
import {Connection} from '../cdp/Connection.js';
import {TimeoutError} from '../common/Errors.js';
import type {Product} from '../common/Product.js';
import type {Puppeteer, PuppeteerLaunchOptions} from '../common/Puppeteer.js';
import {debugError, DEFAULT_VIEWPORT} from '../common/util.js';
import type {Viewport} from '../common/Viewport.js';

Expand All @@ -37,7 +38,6 @@ import type {
} from './LaunchOptions.js';
import {NodeWebSocketTransport as WebSocketTransport} from './NodeWebSocketTransport.js';
import {PipeTransport} from './PipeTransport.js';
import type {Puppeteer, PuppeteerLaunchOptions} from './Puppeteer.js';

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export * from './FirefoxLauncher.js';
export * from './LaunchOptions.js';
export * from './PipeTransport.js';
export * from './ProductLauncher.js';
export * from './Puppeteer.js';
export * from '../common/Puppeteer.js';
export * from './ScreenRecorder.js';
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/puppeteer-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export * from './util/util.js';
*/
export * from './common/CustomQueryHandler.js';

import {Puppeteer} from './node/Puppeteer.js';
import {Puppeteer} from './common/Puppeteer.js';

/**
* @public
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer/src/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type {Protocol} from 'puppeteer-core';

export * from 'puppeteer-core/internal/puppeteer-core.js';

import {Puppeteer} from 'puppeteer-core/internal/node/Puppeteer.js';
import {Puppeteer} from 'puppeteer-core/internal/common/Puppeteer.js';

import {getConfiguration} from './getConfiguration.js';

Expand Down
2 changes: 1 addition & 1 deletion test/src/cdp/devtools.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import expect from 'expect';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/node/Puppeteer.js';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/common/Puppeteer.js';

import {getTestState, launch} from '../mocha-utils.js';

Expand Down
2 changes: 1 addition & 1 deletion test/src/cdp/extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import path from 'path';

import expect from 'expect';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/node/Puppeteer.js';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/common/Puppeteer.js';

import {getTestState, launch} from '../mocha-utils.js';

Expand Down
2 changes: 1 addition & 1 deletion test/src/headful.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import os from 'os';
import path from 'path';

import expect from 'expect';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/node/Puppeteer.js';
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/common/Puppeteer.js';
import {rmSync} from 'puppeteer-core/internal/node/util/fs.js';

import {getTestState, launch} from './mocha-utils.js';
Expand Down
52 changes: 22 additions & 30 deletions test/src/launcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,52 +392,44 @@ describe('Launcher specs', function () {
});

if (isChrome) {
expect(puppeteer.defaultArgs()).toContain('--no-first-run');
expect(puppeteer.defaultArgs()).toContain('--headless=new');
expect(puppeteer.defaultArgs({headless: false})).not.toContain(
expect(await puppeteer.defaultArgs()).toContain('--no-first-run');
expect(await puppeteer.defaultArgs()).toContain('--headless=new');
expect(await puppeteer.defaultArgs({headless: false})).not.toContain(
'--headless=new'
);
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
expect(await puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
`--user-data-dir=${path.resolve('foo')}`
);
} else if (isFirefox) {
expect(puppeteer.defaultArgs()).toContain('--headless');
expect(puppeteer.defaultArgs()).toContain('--no-remote');
expect(await puppeteer.defaultArgs()).toContain('--headless');
expect(await puppeteer.defaultArgs()).toContain('--no-remote');
if (os.platform() === 'darwin') {
expect(puppeteer.defaultArgs()).toContain('--foreground');
expect(await puppeteer.defaultArgs()).toContain('--foreground');
} else {
expect(puppeteer.defaultArgs()).not.toContain('--foreground');
expect(await puppeteer.defaultArgs()).not.toContain('--foreground');
}
expect(puppeteer.defaultArgs({headless: false})).not.toContain(
expect(await puppeteer.defaultArgs({headless: false})).not.toContain(
'--headless'
);
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
expect(await puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
'--profile'
);
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain('foo');
expect(await puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
'foo'
);
} else {
expect(puppeteer.defaultArgs()).toContain('-headless');
expect(puppeteer.defaultArgs({headless: false})).not.toContain(
expect(await puppeteer.defaultArgs()).toContain('-headless');
expect(await puppeteer.defaultArgs({headless: false})).not.toContain(
'-headless'
);
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
expect(await puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
'-profile'
);
expect(puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
expect(await puppeteer.defaultArgs({userDataDir: 'foo'})).toContain(
path.resolve('foo')
);
}
});
it('should report the correct product', async () => {
const {isChrome, isFirefox, puppeteer} = await getTestState({
skipLaunch: true,
});
if (isChrome) {
expect(puppeteer.product).toBe('chrome');
} else if (isFirefox) {
expect(puppeteer.product).toBe('firefox');
}
});
it('should work with no default arguments', async () => {
const {context, close} = await launch({
ignoreDefaultArgs: true,
Expand All @@ -455,7 +447,7 @@ describe('Launcher specs', function () {
skipLaunch: true,
});
// Make sure we launch with `--enable-automation` by default.
const defaultArgs = puppeteer.defaultArgs();
const defaultArgs = await puppeteer.defaultArgs();
const {browser, close} = await launch(
Object.assign({}, defaultBrowserOptions, {
// Ignore first and third default argument.
Expand All @@ -479,7 +471,7 @@ describe('Launcher specs', function () {
skipLaunch: true,
});

const defaultArgs = puppeteer.defaultArgs();
const defaultArgs = await puppeteer.defaultArgs();
const {browser, close} = await launch(
Object.assign({}, defaultBrowserOptions, {
// Only the first argument is fixed, others are optional.
Expand Down Expand Up @@ -919,7 +911,7 @@ describe('Launcher specs', function () {
skipLaunch: true,
});

const executablePath = puppeteer.executablePath();
const executablePath = await puppeteer.executablePath();
expect(fs.existsSync(executablePath)).toBe(true);
expect(fs.realpathSync(executablePath)).toBe(executablePath);
});
Expand All @@ -928,7 +920,7 @@ describe('Launcher specs', function () {
skipLaunch: true,
});

const executablePath = puppeteer.executablePath('chrome');
const executablePath = await puppeteer.executablePath('chrome');
expect(executablePath).toBeTruthy();
});
describe('when executable path is configured', () => {
Expand All @@ -952,7 +944,7 @@ describe('Launcher specs', function () {
skipLaunch: true,
});
try {
puppeteer.executablePath();
await puppeteer.executablePath();
} catch (error) {
expect((error as Error).message).toContain(
'SOME_CUSTOM_EXECUTABLE'
Expand Down

0 comments on commit 9602765

Please sign in to comment.