@browserless/goto 9.2.19 → 9.3.0-beta.10

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.19",
5
+ "version": "9.3.0-beta.10",
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",
33
- "@cliqz/adblocker-puppeteer": "~1.22.7",
32
+ "@browserless/devices": "^9.2.22",
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": "e44bfcf5f5ead8f42ad758c200b8fc5c1be4a761"
71
+ "gitHead": "195f7c7c39a4f04e74c2e4cd1b4c45bb4dec6166"
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 * (1 / 2),
106
+ action: (milliseconds = globalTimeout) => milliseconds * (1 / 8),
107
+ goto: (milliseconds = globalTimeout) => milliseconds * 0.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 = timeouts.base(globalTimeout),
145
147
  timezone,
146
148
  url,
147
149
  waitForFunction,
@@ -153,11 +155,27 @@ module.exports = ({
153
155
  ...args
154
156
  }
155
157
  ) => {
158
+ const actionTimeout = timeouts.action(timeout)
159
+ const gotoTimeout = timeouts.goto(timeout)
160
+
156
161
  const isWaitUntilAuto = waitUntil === 'auto'
157
162
  if (isWaitUntilAuto) waitUntil = 'load'
158
163
 
159
164
  const prePromises = []
160
165
 
166
+ if (abortTypes.length > 0) {
167
+ await page.setRequestInterception(true)
168
+ page.on('request', req => {
169
+ const resourceType = req.resourceType()
170
+ if (!abortTypes.includes(resourceType)) {
171
+ debug('continue', { url: req.url(), resourceType })
172
+ return req.continue(req.continueRequestOverrides(), 0)
173
+ }
174
+ debug('abort', { url: req.url(), resourceType })
175
+ return req.abort('blockedbyclient', 2)
176
+ })
177
+ }
178
+
161
179
  if (adblock) {
162
180
  prePromises.push(
163
181
  run({
@@ -227,16 +245,6 @@ module.exports = ({
227
245
  )
228
246
  }
229
247
 
230
- if (mediaType) {
231
- prePromises.push(
232
- run({
233
- fn: page.emulateMediaType(mediaType),
234
- timeout: actionTimeout,
235
- debug: { mediaType }
236
- })
237
- )
238
- }
239
-
240
248
  if (timezone) {
241
249
  prePromises.push(
242
250
  run({
@@ -271,19 +279,9 @@ module.exports = ({
271
279
 
272
280
  await Promise.all(prePromises.concat(applyEvasions))
273
281
 
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
282
  const { value } = await run({
285
283
  fn: html ? page.setContent(html, args) : page.goto(url, args),
286
- timeout,
284
+ timeout: gotoTimeout,
287
285
  debug: html ? 'html' : 'url'
288
286
  })
289
287
 
@@ -294,12 +292,22 @@ module.exports = ({
294
292
  waitForTimeout
295
293
  })) {
296
294
  if (value) {
297
- await run({ fn: page[key](value), timeout: actionTimeout, debug: { [key]: value } })
295
+ await run({ fn: page[key](value), timeout: gotoTimeout, debug: { [key]: value } })
298
296
  }
299
297
  }
300
298
 
301
299
  const postPromises = []
302
300
 
301
+ if (mediaType) {
302
+ postPromises.push(
303
+ run({
304
+ fn: page.emulateMediaType(mediaType),
305
+ timeout: actionTimeout,
306
+ debug: { mediaType }
307
+ })
308
+ )
309
+ }
310
+
303
311
  if (animations === false) {
304
312
  postPromises.push(
305
313
  run({
@@ -366,7 +374,11 @@ module.exports = ({
366
374
 
367
375
  if (click) {
368
376
  for (const selector of castArray(click)) {
369
- await run({ fn: page.click(selector), timeout: actionTimeout, debug: { click: selector } })
377
+ await run({
378
+ fn: page.click(selector),
379
+ timeout: actionTimeout,
380
+ debug: { click: selector }
381
+ })
370
382
  }
371
383
  }
372
384
 
@@ -391,8 +403,7 @@ module.exports = ({
391
403
  goto.deviceDescriptors = getDevice.deviceDescriptors
392
404
  goto.defaultViewport = defaultViewport
393
405
  goto.waitUntilAuto = _waitUntilAuto
394
- goto.timeout = baseTimeout
395
- goto.actionTimeout = actionTimeout
406
+ goto.timeouts = timeouts
396
407
  goto.run = run
397
408
 
398
409
  return goto