@browserless/goto 10.10.0 → 10.10.1

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": "Navigate to web pages with built-in ad blocking, device emulation, and optimized loading for faster automation.",
4
4
  "homepage": "https://browserless.js.org/#/?id=gotopage-options",
5
- "version": "10.10.0",
5
+ "version": "10.10.1",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -66,5 +66,5 @@
66
66
  "timeout": "2m",
67
67
  "workerThreads": false
68
68
  },
69
- "gitHead": "9b80e677418f2defb804d39043680fe65e3e277b"
69
+ "gitHead": "32f6e72bcb489a83ac9659520a3961aeb97c47b7"
70
70
  }
package/src/adblock.js CHANGED
@@ -6,25 +6,41 @@ const fs = require('fs')
6
6
 
7
7
  const debug = require('debug-logfmt')('browserless:goto:adblock')
8
8
 
9
- const engine = PuppeteerBlocker.deserialize(
10
- new Uint8Array(fs.readFileSync(path.resolve(__dirname, './engine.bin')))
11
- )
9
+ let enginePromise
12
10
 
13
- engine.on('request-blocked', ({ url }) => debug('block', url))
14
- engine.on('request-redirected', ({ url }) => debug('redirect', url))
11
+ const getEngine = () => {
12
+ if (enginePromise) return enginePromise
13
+
14
+ enginePromise = fs.promises.readFile(path.resolve(__dirname, './engine.bin')).then(buffer => {
15
+ const engine = PuppeteerBlocker.deserialize(new Uint8Array(buffer))
16
+ engine.on('request-blocked', ({ url }) => debug('block', url))
17
+ engine.on('request-redirected', ({ url }) => debug('redirect', url))
18
+ return engine
19
+ })
20
+
21
+ return enginePromise
22
+ }
15
23
 
16
24
  /**
17
25
  * autoconsent.playwright.js is the only browser-injectable IIFE bundle in the package.
18
26
  * It is not in the package's "exports" map, so pin @duckduckgo/autoconsent with ~ to
19
27
  * avoid breakage from internal restructuring on minor/patch bumps.
20
28
  */
21
- const autoconsentPlaywrightScript = fs.readFileSync(
22
- path.resolve(
23
- path.dirname(require.resolve('@duckduckgo/autoconsent')),
24
- 'autoconsent.playwright.js'
25
- ),
26
- 'utf8'
27
- )
29
+ let autoconsentPlaywrightScriptPromise
30
+
31
+ const getAutoconsentPlaywrightScript = () => {
32
+ if (autoconsentPlaywrightScriptPromise) return autoconsentPlaywrightScriptPromise
33
+
34
+ autoconsentPlaywrightScriptPromise = fs.promises.readFile(
35
+ path.resolve(
36
+ path.dirname(require.resolve('@duckduckgo/autoconsent')),
37
+ 'autoconsent.playwright.js'
38
+ ),
39
+ 'utf8'
40
+ )
41
+
42
+ return autoconsentPlaywrightScriptPromise
43
+ }
28
44
 
29
45
  /* Configuration passed to autoconsent's `initResp` message.
30
46
  See https://github.com/duckduckgo/autoconsent/blob/main/api.md */
@@ -66,6 +82,7 @@ const sendMessage = (page, message) =>
66
82
 
67
83
  const setupAutoConsent = async page => {
68
84
  if (page._autoconsentSetup) return
85
+ const autoconsentPlaywrightScript = await getAutoconsentPlaywrightScript()
69
86
 
70
87
  await page.exposeFunction('autoconsentSendMessage', async message => {
71
88
  if (!message || typeof message !== 'object') return
@@ -83,12 +100,12 @@ const setupAutoConsent = async page => {
83
100
  page._autoconsentSetup = true
84
101
  }
85
102
 
86
- const runAutoConsent = page => page.evaluate(autoconsentPlaywrightScript)
103
+ const runAutoConsent = async page => page.evaluate(await getAutoconsentPlaywrightScript())
87
104
 
88
105
  const enableBlockingInPage = (page, run, actionTimeout) => {
89
106
  page.disableAdblock = () =>
90
- engine
91
- .disableBlockingInPage(page, { keepRequestInterception: true })
107
+ getEngine()
108
+ .then(engine => engine.disableBlockingInPage(page, { keepRequestInterception: true }))
92
109
  .then(() => debug('disabled'))
93
110
  .catch(() => {})
94
111
 
@@ -99,7 +116,7 @@ const enableBlockingInPage = (page, run, actionTimeout) => {
99
116
  debug: 'autoconsent:setup'
100
117
  }),
101
118
  run({
102
- fn: engine.enableBlockingInPage(page),
119
+ fn: getEngine().then(engine => engine.enableBlockingInPage(page)),
103
120
  timeout: actionTimeout,
104
121
  debug: 'adblock'
105
122
  })
package/src/engine.bin CHANGED
Binary file
package/src/index.js CHANGED
@@ -30,15 +30,13 @@ const run = async ({ fn, timeout, debug: props }) => {
30
30
  return result
31
31
  }
32
32
 
33
- const parseCookies = (url, str) =>
34
- str.split(';').reduce((acc, cookieStr) => {
35
- const jar = new toughCookie.CookieJar(undefined, { rejectPublicSuffixes: false })
36
- jar.setCookieSync(cookieStr.trim(), url)
37
- const parsedCookie = jar.serializeSync().cookies[0]
33
+ const parseCookies = (url, str) => {
34
+ const jar = new toughCookie.CookieJar(undefined, { rejectPublicSuffixes: false })
38
35
 
39
- // Use this instead of the above when the following issue is fixed:
40
- // https://github.com/salesforce/tough-cookie/issues/149
41
- // const ret = toughCookie.parse(cookie).serializeSync();
36
+ return str.split(';').reduce((acc, cookieStr) => {
37
+ const cookie = jar.setCookieSync(cookieStr.trim(), url)
38
+ if (!cookie) return acc
39
+ const parsedCookie = cookie.toJSON()
42
40
 
43
41
  parsedCookie.name = parsedCookie.key
44
42
  delete parsedCookie.key
@@ -50,6 +48,7 @@ const parseCookies = (url, str) =>
50
48
  acc.push(parsedCookie)
51
49
  return acc
52
50
  }, [])
51
+ }
53
52
 
54
53
  const getMediaFeatures = ({ animations, colorScheme }) => {
55
54
  const prefers = []