@browserless/goto 9.3.0-beta.9 → 9.3.3

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.3.0-beta.9",
5
+ "version": "9.3.3",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -29,22 +29,22 @@
29
29
  "puppeteer"
30
30
  ],
31
31
  "dependencies": {
32
- "@browserless/devices": "^9.2.22",
33
- "@cliqz/adblocker-puppeteer": "~1.23.0",
32
+ "@browserless/devices": "^9.3.3",
33
+ "@cliqz/adblocker-puppeteer": "~1.23.3",
34
34
  "debug-logfmt": "~1.0.4",
35
35
  "got": "~11.8.3",
36
- "is-url-http": "~2.2.5",
36
+ "is-url-http": "~2.2.7",
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.39",
42
+ "top-user-agents": "~1.0.40",
43
43
  "tough-cookie": "~4.0.0",
44
44
  "unique-random-array": "~2.0.0"
45
45
  },
46
46
  "devDependencies": {
47
- "ava": "latest",
47
+ "ava": "3",
48
48
  "browserless": "latest",
49
49
  "puppeteer": "latest",
50
50
  "signal-exit": "latest"
@@ -68,5 +68,5 @@
68
68
  "timeout": "2m",
69
69
  "verbose": true
70
70
  },
71
- "gitHead": "5917cbb975fba39b208fe7820d16a5655fa75420"
71
+ "gitHead": "e15c8a9fea38ad134170d52c9b8058010a3c7cd1"
72
72
  }
package/src/engine.bin CHANGED
Binary file
package/src/index.js CHANGED
@@ -24,7 +24,6 @@ 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)
28
27
 
29
28
  const isEmpty = val => val == null || !(Object.keys(val) || val).length
30
29
 
@@ -32,7 +31,7 @@ const castArray = value => [].concat(value).filter(Boolean)
32
31
 
33
32
  const run = async ({ fn, timeout, debug: props }) => {
34
33
  const debugProps = { duration: timeSpan() }
35
- const result = await pReflect(pTimeout(fn, timeout))
34
+ const result = await pReflect(timeout ? pTimeout(fn, timeout) : fn())
36
35
  debugProps.duration = prettyMs(debugProps.duration())
37
36
  if (result.isRejected) debugProps.error = result.reason.message || result.reason
38
37
  debug(props, debugProps)
@@ -92,22 +91,85 @@ const disableAnimations = `
92
91
  }
93
92
  `.trim()
94
93
 
95
- // related https://github.com/puppeteer/puppeteer/issues/1353
96
- const createWaitUntilAuto = defaultOpts => (page, opts) => {
97
- const { timeout } = { ...defaultOpts, ...opts }
98
-
99
- return Promise.all(
100
- [
101
- {
102
- fn: () => page.waitForNavigation({ waitUntil: 'networkidle2' }),
103
- debug: 'waitUntilAuto:waitForNavigation'
104
- },
105
- {
106
- fn: () => page.evaluate(() => window.history.pushState(null, null, null)),
107
- debug: 'waitUntilAuto:pushState'
108
- }
109
- ].map(({ fn, ...opts }) => run({ fn: fn(), timeout, ...opts }))
110
- )
94
+ const inject = async (
95
+ page,
96
+ { timeout, mediaType, animations, hide, remove, modules, scripts, styles }
97
+ ) => {
98
+ const postPromises = []
99
+
100
+ if (mediaType) {
101
+ postPromises.push(
102
+ run({
103
+ fn: page.emulateMediaType(mediaType),
104
+ timeout,
105
+ debug: { mediaType }
106
+ })
107
+ )
108
+ }
109
+
110
+ if (animations === false) {
111
+ postPromises.push(
112
+ run({
113
+ fn: injectStyle(page, disableAnimations),
114
+ timeout,
115
+ debug: 'disableAnimations'
116
+ })
117
+ )
118
+ }
119
+
120
+ if (hide) {
121
+ postPromises.push(
122
+ run({
123
+ fn: injectStyle(page, `${castArray(hide).join(', ')} { visibility: hidden !important; }`),
124
+ timeout,
125
+ debug: 'hide'
126
+ })
127
+ )
128
+ }
129
+
130
+ if (remove) {
131
+ postPromises.push(
132
+ run({
133
+ fn: injectStyle(page, `${castArray(remove).join(', ')} { display: none !important; }`),
134
+ timeout,
135
+ debug: 'remove'
136
+ })
137
+ )
138
+ }
139
+
140
+ if (modules) {
141
+ postPromises.push(
142
+ run({
143
+ fn: Promise.all(
144
+ castArray(modules).map(value => injectScript(page, value, { type: 'modules' }))
145
+ ),
146
+ timeout,
147
+ debug: 'modules'
148
+ })
149
+ )
150
+ }
151
+
152
+ if (scripts) {
153
+ postPromises.push(
154
+ run({
155
+ fn: Promise.all(castArray(scripts).map(value => injectScript(page, value))),
156
+ timeout,
157
+ debug: 'scripts'
158
+ })
159
+ )
160
+ }
161
+
162
+ if (styles) {
163
+ postPromises.push(
164
+ run({
165
+ fn: Promise.all(castArray(styles).map(style => injectStyle(page, style))),
166
+ timeout,
167
+ debug: 'styles'
168
+ })
169
+ )
170
+ }
171
+
172
+ return Promise.all(postPromises)
111
173
  }
112
174
 
113
175
  module.exports = ({
@@ -116,14 +178,32 @@ module.exports = ({
116
178
  timeout: globalTimeout,
117
179
  ...deviceOpts
118
180
  }) => {
119
- const baseTimeout = globalTimeout * (1 / 2)
120
- const gotoTimeout = timeout * 0.8
121
- const actionTimeout = baseTimeout * (1 / 8)
122
-
123
181
  const getDevice = createDevices(deviceOpts)
124
182
  const { viewport: defaultViewport } = getDevice.findDevice(defaultDevice)
125
183
 
126
- const _waitUntilAuto = createWaitUntilAuto({ timeout: actionTimeout })
184
+ const timeouts = {
185
+ base: (milliseconds = globalTimeout) => milliseconds * (2 / 3),
186
+ action: (milliseconds = globalTimeout) => milliseconds * (1 / 11),
187
+ goto: (milliseconds = globalTimeout) => milliseconds * (7 / 8)
188
+ }
189
+
190
+ // related https://github.com/puppeteer/puppeteer/issues/1353
191
+ const _waitUntilAuto = (page, opts = {}) => {
192
+ const timeout = timeouts.action(opts.timeout)
193
+
194
+ return Promise.all(
195
+ [
196
+ {
197
+ fn: () => page.waitForNavigation({ waitUntil: 'networkidle2' }),
198
+ debug: 'waitUntilAuto:waitForNavigation'
199
+ },
200
+ {
201
+ fn: () => page.evaluate(() => window.history.pushState(null, null, null)),
202
+ debug: 'waitUntilAuto:pushState'
203
+ }
204
+ ].map(({ fn, ...opts }) => run({ fn: fn(), timeout, ...opts }))
205
+ )
206
+ }
127
207
 
128
208
  const goto = async (
129
209
  page,
@@ -143,7 +223,7 @@ module.exports = ({
143
223
  scripts,
144
224
  scroll,
145
225
  styles,
146
- timeout = baseTimeout,
226
+ timeout = globalTimeout,
147
227
  timezone,
148
228
  url,
149
229
  waitForFunction,
@@ -155,6 +235,10 @@ module.exports = ({
155
235
  ...args
156
236
  }
157
237
  ) => {
238
+ const baseTimeout = timeouts.base(globalTimeout)
239
+ const actionTimeout = timeouts.action(baseTimeout)
240
+ const gotoTimeout = timeouts.goto(baseTimeout)
241
+
158
242
  const isWaitUntilAuto = waitUntil === 'auto'
159
243
  if (isWaitUntilAuto) waitUntil = 'load'
160
244
 
@@ -163,13 +247,15 @@ module.exports = ({
163
247
  if (abortTypes.length > 0) {
164
248
  await page.setRequestInterception(true)
165
249
  page.on('request', req => {
250
+ if (req.isInterceptResolutionHandled()) return
251
+
166
252
  const resourceType = req.resourceType()
167
253
  if (!abortTypes.includes(resourceType)) {
168
254
  debug('continue', { url: req.url(), resourceType })
169
255
  return req.continue(req.continueRequestOverrides(), 0)
170
256
  }
171
257
  debug('abort', { url: req.url(), resourceType })
172
- return req.abort('blockedbyclient', 2)
258
+ return req.abort('blockedbyclient', 0)
173
259
  })
174
260
  }
175
261
 
@@ -276,7 +362,7 @@ module.exports = ({
276
362
 
277
363
  await Promise.all(prePromises.concat(applyEvasions))
278
364
 
279
- const { value } = await run({
365
+ const { value: response } = await run({
280
366
  fn: html ? page.setContent(html, args) : page.goto(url, args),
281
367
  timeout: gotoTimeout,
282
368
  debug: html ? 'html' : 'url'
@@ -293,85 +379,24 @@ module.exports = ({
293
379
  }
294
380
  }
295
381
 
296
- const postPromises = []
297
-
298
- if (mediaType) {
299
- postPromises.push(
300
- run({
301
- fn: page.emulateMediaType(mediaType),
302
- timeout: actionTimeout,
303
- debug: { mediaType }
304
- })
305
- )
306
- }
307
-
308
- if (animations === false) {
309
- postPromises.push(
310
- run({
311
- fn: injectStyle(page, disableAnimations),
312
- timeout: actionTimeout,
313
- debug: 'disableAnimations'
314
- })
315
- )
316
- }
317
-
318
- if (hide) {
319
- postPromises.push(
320
- run({
321
- fn: injectStyle(page, `${castArray(hide).join(', ')} { visibility: hidden !important; }`),
322
- timeout: actionTimeout,
323
- debug: 'hide'
324
- })
325
- )
326
- }
327
-
328
- if (remove) {
329
- postPromises.push(
330
- run({
331
- fn: injectStyle(page, `${castArray(remove).join(', ')} { display: none !important; }`),
332
- timeout: actionTimeout,
333
- debug: 'remove'
334
- })
335
- )
336
- }
337
-
338
- if (modules) {
339
- postPromises.push(
340
- run({
341
- fn: Promise.all(
342
- castArray(modules).map(value => injectScript(page, value, { type: 'modules' }))
343
- ),
344
- timeout: actionTimeout,
345
- debug: 'modules'
346
- })
347
- )
348
- }
349
-
350
- if (scripts) {
351
- postPromises.push(
352
- run({
353
- fn: Promise.all(castArray(scripts).map(value => injectScript(page, value))),
354
- timeout: actionTimeout,
355
- debug: 'scripts'
356
- })
357
- )
358
- }
359
-
360
- if (styles) {
361
- postPromises.push(
362
- run({
363
- fn: Promise.all(castArray(styles).map(style => injectStyle(page, style))),
364
- timeout: actionTimeout,
365
- debug: 'styles'
366
- })
367
- )
368
- }
369
-
370
- await Promise.all(postPromises)
382
+ await inject(page, {
383
+ timeout: actionTimeout,
384
+ mediaType,
385
+ animations,
386
+ hide,
387
+ remove,
388
+ modules,
389
+ scripts,
390
+ styles
391
+ })
371
392
 
372
393
  if (click) {
373
394
  for (const selector of castArray(click)) {
374
- await run({ fn: page.click(selector), timeout: actionTimeout, debug: { click: selector } })
395
+ await run({
396
+ fn: page.click(selector),
397
+ timeout: actionTimeout,
398
+ debug: { click: selector }
399
+ })
375
400
  }
376
401
  }
377
402
 
@@ -384,10 +409,10 @@ module.exports = ({
384
409
  }
385
410
 
386
411
  if (isWaitUntilAuto) {
387
- await waitUntilAuto(page, { response: value, timeout: actionTimeout * 2 })
412
+ await waitUntilAuto(page, { response, timeout: actionTimeout * 2 })
388
413
  }
389
414
 
390
- return { response: value, device }
415
+ return { response, device }
391
416
  }
392
417
 
393
418
  goto.getDevice = getDevice
@@ -396,14 +421,12 @@ module.exports = ({
396
421
  goto.deviceDescriptors = getDevice.deviceDescriptors
397
422
  goto.defaultViewport = defaultViewport
398
423
  goto.waitUntilAuto = _waitUntilAuto
399
- goto.timeout = baseTimeout
400
- goto.actionTimeout = actionTimeout
424
+ goto.timeouts = timeouts
401
425
  goto.run = run
402
426
 
403
427
  return goto
404
428
  }
405
429
 
406
430
  module.exports.parseCookies = parseCookies
407
- module.exports.injectScript = injectScript
408
- module.exports.injectStyle = injectStyle
431
+ module.exports.inject = inject
409
432
  module.exports.evasions = ALL_EVASIONS_KEYS