Skip to content

Commit c1d4633

Browse files
authored
feat!: remove chromium and use puppeteer (#24)
* feat: remove chromium and use puppeteer * docs: update readme
1 parent 787b4c3 commit c1d4633

File tree

6 files changed

+298
-402
lines changed

6 files changed

+298
-402
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ You can use `vue-telemetry-analyzer` locally on your project:
8282
npm install vue-telemetry-analyzer # Or yarn add vue-telemetry
8383
```
8484

85-
Since this module is made to work on serverless environement by using [puppeteer-core](https://www.npmjs.com/package/puppeteer-core) and [chrome-aws-lambda](http://npmjs.com/package/chrome-aws-lambda), you need to install [puppeteer](https://www.npmjs.com/package/puppeteer) locally as a dev dependency:
86-
87-
```bash
88-
npm install --save-dev puppeteer # Or yarn add --dev puppeteer
89-
```
90-
9185
Then you can use the module in your project:
9286

9387
```js

bin/vta.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const ora = require('ora')
44
const consola = require('consola')
55
const yargs = require('yargs')
6-
const { setRootDir, install } = require('lmify')
7-
const { join } = require('path')
86

97
yargs
108
.usage('$0 [url]', 'analyze an url', (yargs) => {
@@ -17,12 +15,6 @@ yargs
1715
consola.error('Please provide an url to analyze')
1816
return yargs.showHelp()
1917
}
20-
// Check if puppeteer is installed
21-
try { require('puppeteer') }
22-
catch(err) {
23-
setRootDir(join(__dirname, '..'))
24-
await install('puppeteer')
25-
}
2618

2719
const spinner = ora(`Detecting Vue on ${argv.url}`).start()
2820
setTimeout(() => spinner.color = 'magenta', 2500)

package.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@
1919
"author": "NuxtJS Company",
2020
"license": "MIT",
2121
"dependencies": {
22-
"chrome-aws-lambda": "^3.1.1",
23-
"consola": "^2.13.0",
24-
"lmify": "^0.3.0",
22+
"consola": "^2.15.0",
2523
"make-dir": "^3.1.0",
26-
"ora": "^4.0.4",
27-
"puppeteer-core": "^3.3.0",
28-
"tld-extract": "^1.1.2",
29-
"yargs": "^15.3.1"
24+
"ora": "^5.1.0",
25+
"puppeteer": "^5.3.0",
26+
"tld-extract": "^2.0.0",
27+
"yargs": "^16.0.3"
3028
},
3129
"devDependencies": {
32-
"puppeteer": "^3.3.0",
33-
"standard-version": "^8.0.0"
30+
"standard-version": "^9.0.0"
3431
}
3532
}

src/index.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const chromium = require('chrome-aws-lambda')
1+
const puppeteer = require('puppeteer')
22
const tldParser = require('tld-extract')
33
const makeDir = require('make-dir')
44
const { createHash } = require('crypto')
55
const { URL } = require('url')
66
const { join } = require('path')
77
const { tmpdir } = require('os')
8-
const { isCrawlable } = require('./utils')
8+
const { isCrawlable, puppeteerArgs, puppeteerViewport } = require('./utils')
99
const { hasVue, getVueMeta, getFramework, getPlugins, getUI, getNuxtMeta, getNuxtModules } = require('./detectors')
1010
const consola = require('consola')
1111

@@ -15,25 +15,9 @@ const ERROR_CODES = Object.freeze({
1515
NOT_CRAWLABLE: 3,
1616
VUE_NOT_DETECTED: 4
1717
})
18+
let browser = null
1819

19-
async function getPuppeteerPath() {
20-
let executablePath = await chromium.executablePath
21-
22-
if (process.env.NETLIFY_DEV) {
23-
executablePath = null // forces to use local puppeteer
24-
}
25-
26-
return executablePath
27-
}
28-
29-
module.exports = async function (originalUrl) {
30-
const executablePath = await getPuppeteerPath()
31-
const browser = await chromium.puppeteer.launch({
32-
executablePath,
33-
args: chromium.args,
34-
defaultViewport: chromium.defaultViewport,
35-
headless: true
36-
})
20+
async function analyze (originalUrl) {
3721
// Parse url
3822
let url
3923
try {
@@ -43,6 +27,17 @@ module.exports = async function (originalUrl) {
4327
error.code = ERROR_CODES.INVALID_URL
4428
throw error
4529
}
30+
// Start browser if not launched
31+
if (!browser) {
32+
browser = await puppeteer.launch({
33+
args: puppeteerArgs,
34+
defaultViewport: puppeteerViewport,
35+
headless: true
36+
})
37+
browser.on('disconnected', () => {
38+
browser = null
39+
})
40+
}
4641
// Force https
4742
originalUrl = 'https://' + url.hostname + url.pathname
4843
const domain = tldParser(url.origin).domain
@@ -172,9 +167,15 @@ module.exports = async function (originalUrl) {
172167
quality: 90
173168
})
174169

175-
// Close browser
176-
await browser.close()
170+
await page.close()
177171

178172
return infos
179173
}
174+
175+
module.exports = analyze
180176
module.exports.ERROR_CODES = ERROR_CODES
177+
module.exports.close = async function () {
178+
if (browser) {
179+
await browser.close()
180+
}
181+
}

src/utils.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,51 @@ exports.isCrawlable = async function (htmlHeaders) {
6161

6262
return tag ? !patterns.test(tag) : true
6363
}
64+
65+
// From https://github.com/alixaxel/chrome-aws-lambda/blob/master/source/index.js#L83
66+
exports.puppeteerArgs = [
67+
'--autoplay-policy=user-gesture-required',
68+
'--disable-background-networking',
69+
'--disable-background-timer-throttling',
70+
'--disable-backgrounding-occluded-windows',
71+
'--disable-breakpad',
72+
'--disable-client-side-phishing-detection',
73+
'--disable-component-update',
74+
'--disable-default-apps',
75+
'--disable-dev-shm-usage',
76+
'--disable-domain-reliability',
77+
'--disable-extensions',
78+
'--disable-features=AudioServiceOutOfProcess',
79+
'--disable-hang-monitor',
80+
'--disable-ipc-flooding-protection',
81+
'--disable-notifications',
82+
'--disable-offer-store-unmasked-wallet-cards',
83+
'--disable-popup-blocking',
84+
'--disable-print-preview',
85+
'--disable-prompt-on-repost',
86+
'--disable-renderer-backgrounding',
87+
'--disable-setuid-sandbox',
88+
'--disable-speech-api',
89+
'--disable-sync',
90+
'--disk-cache-size=33554432',
91+
'--hide-scrollbars',
92+
'--ignore-gpu-blacklist',
93+
'--metrics-recording-only',
94+
'--mute-audio',
95+
'--no-default-browser-check',
96+
'--no-first-run',
97+
'--no-pings',
98+
'--no-sandbox',
99+
'--no-zygote',
100+
'--password-store=basic',
101+
'--use-gl=swiftshader',
102+
'--use-mock-keychain'
103+
]
104+
exports.puppeteerViewport = {
105+
deviceScaleFactor: 1,
106+
hasTouch: false,
107+
height: 1080,
108+
isLandscape: true,
109+
isMobile: false,
110+
width: 1920,
111+
}

0 commit comments

Comments
 (0)