Skip to content

Commit b97a852

Browse files
authored
feat: bump puppeteer support to 17.x (#1519)
1 parent 1b4d54c commit b97a852

File tree

6 files changed

+60
-37
lines changed

6 files changed

+60
-37
lines changed

package-lock.json

Lines changed: 26 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"playwright": "1.25.1",
9494
"portastic": "^1.0.1",
9595
"proxy": "^1.0.2",
96-
"puppeteer": "15.1.1",
96+
"puppeteer": "17.0.0",
9797
"ts-jest": "^28.0.8",
9898
"ts-node": "^10.9.1",
9999
"turbo": "1.4.5",

packages/browser-pool/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
},
5353
"peerDependencies": {
5454
"playwright": "^1.21.1",
55-
"puppeteer": ">= 9.x <= 15.1"
55+
"puppeteer": ">= 9.x <= 17.0"
5656
},
5757
"peerDependenciesMeta": {
5858
"playwright": {

packages/crawlee/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
},
7171
"peerDependencies": {
7272
"playwright": "^1.21.1",
73-
"puppeteer": ">= 9.x <= 15.1"
73+
"puppeteer": ">= 9.x <= 17.0"
7474
},
7575
"peerDependenciesMeta": {
7676
"playwright": {

packages/puppeteer-crawler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"ow": "^0.28.1"
6565
},
6666
"peerDependencies": {
67-
"puppeteer": ">= 9.x <= 15.1"
67+
"puppeteer": ">= 9.x <= 17.0"
6868
},
6969
"peerDependenciesMeta": {
7070
"puppeteer": {

packages/puppeteer-crawler/src/internals/utils/puppeteer_utils.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { readFile } from 'fs/promises';
2222
import ow from 'ow';
2323
import vm from 'vm';
2424
import { LruCache } from '@apify/datastructures';
25+
import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
2526
import type { Page, HTTPResponse, ResponseForRequest, HTTPRequest as PuppeteerRequest } from 'puppeteer';
2627
import log_ from '@apify/log';
2728
import type { Request } from '@crawlee/core';
@@ -237,12 +238,36 @@ export async function blockRequests(page: Page, options: BlockRequestsOptions =
237238

238239
const patternsToBlock = [...urlPatterns, ...extraUrlPatterns];
239240

240-
if (page._client instanceof Function) {
241-
await page._client().send('Network.setBlockedURLs', { urls: patternsToBlock });
242-
} else {
243-
// @ts-expect-error for older puppeteer (<14.4)
244-
await page._client.send('Network.setBlockedURLs', { urls: patternsToBlock });
241+
// We use CDP commands instead of request interception as the latter disables caching, which is not ideal
242+
await sendCDPCommand(page, 'Network.setBlockedURLs', { urls: patternsToBlock });
243+
}
244+
245+
/**
246+
* @internal
247+
*/
248+
export async function sendCDPCommand<T extends keyof ProtocolMapping.Commands>(
249+
page: Page,
250+
command: T,
251+
...args: ProtocolMapping.Commands[T]['paramsType']
252+
): Promise<ProtocolMapping.Commands[T]['returnType']> {
253+
// In puppeteer 16.x and 17.x, the `_client` method is completely omitted from the types. It's still there and works the same way, but it is hidden.
254+
255+
// Puppeteer <= 17
256+
if (Reflect.has(page, '_client')) {
257+
const client = Reflect.get(page, '_client');
258+
259+
if (typeof client === 'function') {
260+
return client.call(page).send(command, ...args);
261+
}
262+
263+
return client.send(command, ...args);
245264
}
265+
266+
const jsonPath = require.resolve('puppeteer/package.json');
267+
const parsed = JSON.parse(await readFile(jsonPath, 'utf-8'));
268+
269+
// eslint-disable-next-line max-len
270+
throw new Error(`Cannot detect CDP client for Puppeteer ${parsed.version}. You should report this to Crawlee, mentioning the puppeteer version you are using.`);
246271
}
247272

248273
/**

0 commit comments

Comments
 (0)