Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dependencies": {
"@apify/log": "^1.0.0",
"fs-extra": "^9.1.0",
"lodash": "^4.17.21",
"lodash.merge": "^4.6.2",
"nanoid": "^3.1.22",
"ow": "^0.23.0",
Expand All @@ -54,7 +53,6 @@
"@apify/eslint-config-ts": "^0.1.1",
"@types/fs-extra": "^9.0.11",
"@types/jest": "^26.0.22",
"@types/lodash": "^4.14.172",
"@types/lodash.merge": "^4.6.6",
"@types/node": "^14.17.7",
"@typescript-eslint/eslint-plugin": "^4.22.0",
Expand Down
2 changes: 1 addition & 1 deletion src/abstract-classes/browser-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface Cookie {
* @hideconstructor
*/
export abstract class BrowserController<
Library extends CommonLibrary,
Library extends CommonLibrary = CommonLibrary,
LibraryOptions = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
Expand Down
21 changes: 13 additions & 8 deletions src/abstract-classes/browser-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ export interface CommonLibrary {
name?: () => string;
}

/**
* @internal
*/
/** @internal */
export interface CommonBrowser {
newPage(...args: unknown[]): unknown;
newPage(...args: unknown[]): Promise<CommonPage>;
}

/** @internal */
export interface CommonPage {
Comment thread
vladfrangu marked this conversation as resolved.
close(...args: unknown[]): Promise<unknown>;
}

export interface BrowserPluginOptions<LibraryOptions> {
Expand Down Expand Up @@ -57,7 +60,7 @@ export interface BrowserPluginOptions<LibraryOptions> {

export type CreateLaunchContextOptions<
Library extends CommonLibrary,
LibraryOptions = Parameters<Library['launch']>[0],
LibraryOptions extends unknown = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
NewPageResult = UnwrapPromise<ReturnType<LaunchResult['newPage']>>,
Expand All @@ -70,8 +73,8 @@ export type CreateLaunchContextOptions<
* feed them to {@link BrowserPool} for use.
*/
export abstract class BrowserPlugin<
Library extends CommonLibrary,
LibraryOptions = Parameters<Library['launch']>[0],
Library extends CommonLibrary = CommonLibrary,
LibraryOptions extends unknown = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
NewPageResult = UnwrapPromise<ReturnType<LaunchResult['newPage']>>,
Expand Down Expand Up @@ -137,7 +140,9 @@ export abstract class BrowserPlugin<
/**
* Launches the browser using provided launch context.
*/
async launch(launchContext = this.createLaunchContext()): Promise<LaunchResult> {
async launch(
launchContext: LaunchContext<Library, LibraryOptions, LaunchResult, NewPageOptions, NewPageResult> = this.createLaunchContext(),
): Promise<LaunchResult> {
const { proxyUrl, useIncognitoPages, userDataDir } = launchContext;

if (proxyUrl) {
Expand Down
589 changes: 357 additions & 232 deletions src/browser-pool.js → src/browser-pool.ts

Large diffs are not rendered by default.

17 changes: 3 additions & 14 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const BrowserPool = require('./browser-pool');
// eslint-disable-next-line import/extensions
const { PuppeteerPlugin } = require('./puppeteer/puppeteer-plugin');
// eslint-disable-next-line import/extensions
const { PlaywrightPlugin } = require('./playwright/playwright-plugin');

/**
* The `browser-pool` module exports three constructors. One for `BrowserPool`
* itself and two for the included Puppeteer and Playwright plugins.
Expand All @@ -26,13 +20,8 @@ const { PlaywrightPlugin } = require('./playwright/playwright-plugin');
* });
* ```
*
* @property {BrowserPool} BrowserPool
* @property {PuppeteerPlugin} PuppeteerPlugin
* @property {PlaywrightPlugin} PlaywrightPlugin
* @module browser-pool
*/
module.exports = {
BrowserPool,
PuppeteerPlugin,
PlaywrightPlugin,
};
export * from './browser-pool';
export * from './playwright/playwright-plugin';
export * from './puppeteer/puppeteer-plugin';
4 changes: 2 additions & 2 deletions src/launch-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { UnwrapPromise } from './utils';
* values, such as session IDs.
*/
export interface LaunchContextOptions<
Library extends CommonLibrary,
Library extends CommonLibrary = CommonLibrary,
LibraryOptions = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface LaunchContextOptions<
}

export class LaunchContext<
Library extends CommonLibrary,
Library extends CommonLibrary = CommonLibrary,
LibraryOptions = Parameters<Library['launch']>[0],
LaunchResult extends CommonBrowser = UnwrapPromise<ReturnType<Library['launch']>>,
NewPageOptions = Parameters<LaunchResult['newPage']>[0],
Expand Down
36 changes: 36 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { PlaywrightPlugin, PuppeteerPlugin } from '.';
import type { BrowserPlugin } from './abstract-classes/browser-plugin';

export function addTimeoutToPromise<T>(promise: Promise<T>, timeoutMillis: number, errorMessage: string): Promise<T> {
return new Promise(async (resolve, reject) => { // eslint-disable-line
const timeout = setTimeout(() => reject(new Error(errorMessage)), timeoutMillis);
Expand All @@ -16,3 +19,36 @@ export type UnwrapPromise<T> = T extends PromiseLike<infer R> ? UnwrapPromise<R>

// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
export function noop(..._args: unknown[]): void {}

export type InferBrowserPluginArray<
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is considered art 🤩
what is the current impact on the language server when unwrapping the array?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That... I don't know, I've never actually measured that before 😓
Any tips on how to do it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't have a significant impact imo. Many other packages do unwrapping as well.

// The original array input
Input extends readonly unknown[],
// The results of this type
Result extends BrowserPlugin[] = []
> =
// If the input is a tuple or a readonly array (`[] as const`), get the first and the rest of the values
Input extends readonly [infer FirstValue, ...infer Rest] | [infer FirstValue, ...infer Rest]
// If the first value is a PlaywrightPlugin
? FirstValue extends PlaywrightPlugin
// Add it to the result, and continue parsing
? InferBrowserPluginArray<Rest, [...Result, PlaywrightPlugin]>
// Else if the first value is a PuppeteerPlugin
: FirstValue extends PuppeteerPlugin
// Add it to the result, and continue parsing
? InferBrowserPluginArray<Rest, [...Result, PuppeteerPlugin]>
// Return never as it isn't a valid type
: never
// If there's no more inputs to parse
: Input extends []
// Return the results
? Result
// If the input is a general array of elements (not a tuple), infer it's values type
: Input extends Array<infer U>
// If the values are a union of the plugins
? [U] extends [PuppeteerPlugin | PlaywrightPlugin]
// Return an array of the union
? U[]
// Return never as it isn't a valid type
: never
// Return the result
: Result;
Loading