@browserless/goto 9.2.20 → 9.3.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@browserless/goto",
3
3
  "description": "Go to a page aborting unnecessary requests",
4
4
  "homepage": "https://browserless.js.org/#/?id=gotopage-options",
5
- "version": "9.2.20",
5
+ "version": "9.3.0-beta.11",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -29,17 +29,17 @@
29
29
  "puppeteer"
30
30
  ],
31
31
  "dependencies": {
32
- "@browserless/devices": "^9.1.6",
32
+ "@browserless/devices": "^9.2.22",
33
33
  "@cliqz/adblocker-puppeteer": "~1.23.0",
34
34
  "debug-logfmt": "~1.0.4",
35
- "got": "~11.8.2",
36
- "is-url-http": "~2.2.4",
35
+ "got": "~11.8.3",
36
+ "is-url-http": "~2.2.5",
37
37
  "p-reflect": "~2.1.0",
38
38
  "p-timeout": "~4.1.0",
39
39
  "pretty-ms": "~7.0.1",
40
40
  "shallow-equal": "~1.2.1",
41
41
  "time-span": "~4.0.0",
42
- "top-user-agents": "~1.0.38",
42
+ "top-user-agents": "~1.0.39",
43
43
  "tough-cookie": "~4.0.0",
44
44
  "unique-random-array": "~2.0.0"
45
45
  },
@@ -68,5 +68,5 @@
68
68
  "timeout": "2m",
69
69
  "verbose": true
70
70
  },
71
- "gitHead": "58b992afb012bb270f3dfe27c2d57a1f91f8051f"
71
+ "gitHead": "5a93704c7fdb292fdf3a867299b3bfa4b095e701"
72
72
  }
package/src/engine.bin CHANGED
Binary file
package/src/index.js CHANGED
@@ -24,6 +24,7 @@ const engine = PuppeteerBlocker.deserialize(
24
24
 
25
25
  engine.on('request-blocked', ({ url }) => debugAdblock('block', url))
26
26
  engine.on('request-redirected', ({ url }) => debugAdblock('redirect', url))
27
+ engine.setRequestInterceptionPriority(1)
27
28
 
28
29
  const isEmpty = val => val == null || !(Object.keys(val) || val).length
29
30
 
@@ -91,37 +92,38 @@ const disableAnimations = `
91
92
  }
92
93
  `.trim()
93
94
 
94
- // related https://github.com/puppeteer/puppeteer/issues/1353
95
- const createWaitUntilAuto = defaultOpts => (page, opts) => {
96
- const { timeout } = { ...defaultOpts, ...opts }
97
-
98
- return Promise.all(
99
- [
100
- {
101
- fn: () => page.waitForNavigation({ waitUntil: 'networkidle2' }),
102
- debug: 'waitUntilAuto:waitForNavigation'
103
- },
104
- {
105
- fn: () => page.evaluate(() => window.history.pushState(null, null, null)),
106
- debug: 'waitUntilAuto:pushState'
107
- }
108
- ].map(({ fn, ...opts }) => run({ fn: fn(), timeout, ...opts }))
109
- )
110
- }
111
-
112
95
  module.exports = ({
113
96
  evasions = ALL_EVASIONS_KEYS,
114
97
  defaultDevice = 'Macbook Pro 13',
115
98
  timeout: globalTimeout,
116
99
  ...deviceOpts
117
100
  }) => {
118
- const baseTimeout = globalTimeout * (1 / 2)
119
- const actionTimeout = baseTimeout * (1 / 8)
120
-
121
101
  const getDevice = createDevices(deviceOpts)
122
102
  const { viewport: defaultViewport } = getDevice.findDevice(defaultDevice)
123
103
 
124
- const _waitUntilAuto = createWaitUntilAuto({ timeout: actionTimeout })
104
+ const timeouts = {
105
+ base: (milliseconds = globalTimeout) => milliseconds * (2 / 3),
106
+ action: (milliseconds = globalTimeout) => milliseconds * (1 / 11),
107
+ goto: (milliseconds = globalTimeout) => milliseconds * (7 / 8)
108
+ }
109
+
110
+ // related https://github.com/puppeteer/puppeteer/issues/1353
111
+ const _waitUntilAuto = (page, opts = {}) => {
112
+ const timeout = timeouts.action(opts.timeout)
113
+
114
+ return Promise.all(
115
+ [
116
+ {
117
+ fn: () => page.waitForNavigation({ waitUntil: 'networkidle2' }),
118
+ debug: 'waitUntilAuto:waitForNavigation'
119
+ },
120
+ {
121
+ fn: () => page.evaluate(() => window.history.pushState(null, null, null)),
122
+ debug: 'waitUntilAuto:pushState'
123
+ }
124
+ ].map(({ fn, ...opts }) => run({ fn: fn(), timeout, ...opts }))
125
+ )
126
+ }
125
127
 
126
128
  const goto = async (
127
129
  page,
@@ -141,7 +143,7 @@ module.exports = ({
141
143
  scripts,
142
144
  scroll,
143
145
  styles,
144
- timeout = baseTimeout,
146
+ timeout = globalTimeout,
145
147
  timezone,
146
148
  url,
147
149
  waitForFunction,
@@ -153,11 +155,28 @@ module.exports = ({
153
155
  ...args
154
156
  }
155
157
  ) => {
158
+ const baseTimeout = timeouts.base(globalTimeout)
159
+ const actionTimeout = timeouts.action(baseTimeout)
160
+ const gotoTimeout = timeouts.goto(baseTimeout)
161
+
156
162
  const isWaitUntilAuto = waitUntil === 'auto'
157
163
  if (isWaitUntilAuto) waitUntil = 'load'
158
164
 
159
165
  const prePromises = []
160
166
 
167
+ if (abortTypes.length > 0) {
168
+ await page.setRequestInterception(true)
169
+ page.on('request', req => {
170
+ const resourceType = req.resourceType()
171
+ if (!abortTypes.includes(resourceType)) {
172
+ debug('continue', { url: req.url(), resourceType })
173
+ return req.continue(req.continueRequestOverrides(), 0)
174
+ }
175
+ debug('abort', { url: req.url(), resourceType })
176
+ return req.abort('blockedbyclient', 2)
177
+ })
178
+ }
179
+
161
180
  if (adblock) {
162
181
  prePromises.push(
163
182
  run({
@@ -227,16 +246,6 @@ module.exports = ({
227
246
  )
228
247
  }
229
248
 
230
- if (mediaType) {
231
- prePromises.push(
232
- run({
233
- fn: page.emulateMediaType(mediaType),
234
- timeout: actionTimeout,
235
- debug: { mediaType }
236
- })
237
- )
238
- }
239
-
240
249
  if (timezone) {
241
250
  prePromises.push(
242
251
  run({
@@ -271,19 +280,9 @@ module.exports = ({
271
280
 
272
281
  await Promise.all(prePromises.concat(applyEvasions))
273
282
 
274
- if (abortTypes.length > 0) {
275
- await page.setRequestInterception(true)
276
- page.on('request', req => {
277
- const resourceType = req.resourceType()
278
- if (!abortTypes.includes(resourceType)) return req.continue()
279
- debug('abort', { url: req.url(), resourceType })
280
- return req.abort('blockedbyclient')
281
- })
282
- }
283
-
284
283
  const { value } = await run({
285
284
  fn: html ? page.setContent(html, args) : page.goto(url, args),
286
- timeout,
285
+ timeout: gotoTimeout,
287
286
  debug: html ? 'html' : 'url'
288
287
  })
289
288
 
@@ -294,12 +293,22 @@ module.exports = ({
294
293
  waitForTimeout
295
294
  })) {
296
295
  if (value) {
297
- await run({ fn: page[key](value), timeout: actionTimeout, debug: { [key]: value } })
296
+ await run({ fn: page[key](value), timeout: gotoTimeout, debug: { [key]: value } })
298
297
  }
299
298
  }
300
299
 
301
300
  const postPromises = []
302
301
 
302
+ if (mediaType) {
303
+ postPromises.push(
304
+ run({
305
+ fn: page.emulateMediaType(mediaType),
306
+ timeout: actionTimeout,
307
+ debug: { mediaType }
308
+ })
309
+ )
310
+ }
311
+
303
312
  if (animations === false) {
304
313
  postPromises.push(
305
314
  run({
@@ -366,7 +375,11 @@ module.exports = ({
366
375
 
367
376
  if (click) {
368
377
  for (const selector of castArray(click)) {
369
- await run({ fn: page.click(selector), timeout: actionTimeout, debug: { click: selector } })
378
+ await run({
379
+ fn: page.click(selector),
380
+ timeout: actionTimeout,
381
+ debug: { click: selector }
382
+ })
370
383
  }
371
384
  }
372
385
 
@@ -391,8 +404,7 @@ module.exports = ({
391
404
  goto.deviceDescriptors = getDevice.deviceDescriptors
392
405
  goto.defaultViewport = defaultViewport
393
406
  goto.waitUntilAuto = _waitUntilAuto
394
- goto.timeout = baseTimeout
395
- goto.actionTimeout = actionTimeout
407
+ goto.timeouts = timeouts
396
408
  goto.run = run
397
409
 
398
410
  return goto