Skip to content

Commit e3ca487

Browse files
committed
fix: memory issue on error
1 parent b8af1f1 commit e3ca487

File tree

1 file changed

+111
-109
lines changed

1 file changed

+111
-109
lines changed

src/index.js

Lines changed: 111 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -60,119 +60,121 @@ async function analyze (originalUrl) {
6060
plugins: [], // vue-router, vuex, vue-apollo, etc
6161
ui: null // vuetify | bootstrap-vue | element-ui | tailwindcss
6262
}
63+
try {
64+
await page.setCacheEnabled(false) // disable cache for avoiding 304
65+
await page.setUserAgent('Vue-Telemetry')
66+
await page.setViewport({
67+
width: 1680,
68+
height: 1260,
69+
deviceScaleFactor: 1,
70+
})
71+
await page.setCookie({
72+
name: 'gtm_cookie_consent',
73+
value: 'functional:1|analytics:1|customization:1|advertising:1',
74+
domain: url.hostname
75+
})
76+
// https://github.com/puppeteer/puppeteer/blob/v2.1.0/docs/api.md#pagegotourl-options
77+
const response = await page.goto(originalUrl, {
78+
waitUntil: 'networkidle0',
79+
timeout: 5
80+
})
81+
if (!response.ok()) {
82+
const error = new Error(`Website responded with ${response.status()} status code`)
83+
error.code = ERROR_CODES.HTTP_ERROR
84+
error.statusCode = response.status()
85+
error.body = await response.text()
86+
throw error
87+
}
88+
// Get headers
89+
const headers = response.headers()
90+
91+
if (!(await isCrawlable(headers))) {
92+
const error = new Error(`Crawling is not allowed on ${originalUrl}`)
93+
error.code = ERROR_CODES.NOT_CRAWLABLE
94+
throw error
95+
}
96+
97+
// Get page scripts urls
98+
let scripts = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('script')).map(({ src }) => src))
99+
scripts = (await scripts.jsonValue()).filter(script => script)
100+
101+
// Original HTLM sent back
102+
const originalHtml = await response.text()
103+
104+
// Get page html
105+
const html = await page.content()
106+
107+
// Use for detection
108+
const context = { originalHtml, html, scripts, page }
109+
110+
if (!(await hasVue(context))) {
111+
const error = new Error(`Vue is not detected on ${originalUrl}`)
112+
error.code = ERROR_CODES.VUE_NOT_DETECTED
113+
throw error
114+
}
115+
116+
// Get page title
117+
infos.meta.title = await page.title()
118+
infos.meta.description = await page.$eval('head > meta[name="description"]', element => element.content).catch(() => '')
119+
if (!infos.meta.description) {
120+
infos.meta.description = await page.$eval('head > meta[property="og:description"]', element => element.content).catch(() => '')
121+
}
122+
infos.meta.siteName = await page.$eval('head > meta[property="og:site_name"]', element => element.content).catch(() => '')
123+
124+
const rtaLabel = await page.$eval('head > meta[name="RATING"]', element => element.content).catch(() => '')
125+
if (rtaLabel.trim() === 'RTA-5042-1996-1400-1577-RTA') {
126+
infos.meta.isAdultContent = true
127+
}
128+
129+
// Get page language
130+
const matches = html.match(new RegExp('<html[^>]*[: ]lang="([a-z]{2}((-|_)[A-Z]{2})?)"', 'i'));
131+
if (matches && matches.length) {
132+
infos.meta.language = matches[1].split('-')[0]
133+
}
134+
135+
// Get Vue version
136+
const version = await page.evaluate('(window.$nuxt && window.$nuxt.$root && window.$nuxt.$root.constructor.version) || (window.Vue && window.Vue.version) || [...document.querySelectorAll("*")].map((el) => el.__vue__ && el.__vue__.$root && el.__vue__.$root.constructor.version).filter(Boolean)[0]')
137+
if (version) {
138+
infos.vueVersion = version
139+
}
140+
141+
// Get Vue metas
142+
const { ssr } = await getVueMeta(context)
143+
infos.hasSSR = ssr
144+
145+
// Get Vue ecosystem infos
146+
infos.framework = await getFramework(context)
147+
infos.plugins = await getPlugins(context)
148+
infos.ui = await getUI(context)
149+
150+
// Get Nuxt modules if using NuxtJS
151+
if (infos.framework && infos.framework.name === 'NuxtJS') {
152+
const [meta, modules] = await Promise.all([
153+
getNuxtMeta(context),
154+
getNuxtModules(context)
155+
])
156+
infos.isStatic = meta.static
157+
infos.hasSSR = meta.ssr
158+
infos.frameworkModules = modules
159+
}
160+
161+
// Take screenshot
162+
const screenshotsDir = join(tmpdir(), 'vue-telemetry-analyzer')
163+
const filename = createHash('md5').update(originalUrl).digest('hex')
164+
infos.screenshot = join(screenshotsDir, `${filename}.jpg`)
165+
await makeDir(screenshotsDir)
166+
await page.screenshot({
167+
path: infos.screenshot,
168+
type: 'jpeg',
169+
quality: 90
170+
})
63171

64-
await page.setCacheEnabled(false) // disable cache for avoiding 304
65-
await page.setUserAgent('Vue-Telemetry')
66-
await page.setViewport({
67-
width: 1680,
68-
height: 1260,
69-
deviceScaleFactor: 1,
70-
})
71-
await page.setCookie({
72-
name: 'gtm_cookie_consent',
73-
value: 'functional:1|analytics:1|customization:1|advertising:1',
74-
domain: url.hostname
75-
})
76-
// https://github.com/puppeteer/puppeteer/blob/v2.1.0/docs/api.md#pagegotourl-options
77-
const response = await page.goto(originalUrl, {
78-
waitUntil: 'networkidle0'
79-
})
80-
if (!response.ok()) {
81-
const error = new Error(`Website responded with ${response.status()} status code`)
82-
error.code = ERROR_CODES.HTTP_ERROR
83-
error.statusCode = response.status()
84-
error.body = await response.text()
85-
await page.close()
86-
throw error
87-
}
88-
// Get headers
89-
const headers = response.headers()
90-
91-
if (!(await isCrawlable(headers))) {
92-
const error = new Error(`Crawling is not allowed on ${originalUrl}`)
93-
error.code = ERROR_CODES.NOT_CRAWLABLE
94172
await page.close()
95-
throw error
96-
}
97-
98-
// Get page scripts urls
99-
let scripts = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('script')).map(({ src }) => src))
100-
scripts = (await scripts.jsonValue()).filter(script => script)
101-
102-
// Original HTLM sent back
103-
const originalHtml = await response.text()
104-
105-
// Get page html
106-
const html = await page.content()
107-
108-
// Use for detection
109-
const context = { originalHtml, html, scripts, page }
110-
111-
if (!(await hasVue(context))) {
112-
const error = new Error(`Vue is not detected on ${originalUrl}`)
113-
error.code = ERROR_CODES.VUE_NOT_DETECTED
173+
return infos
174+
} catch (err) {
114175
await page.close()
115-
throw error
116-
}
117-
118-
// Get page title
119-
infos.meta.title = await page.title()
120-
infos.meta.description = await page.$eval('head > meta[name="description"]', element => element.content).catch(() => '')
121-
if (!infos.meta.description) {
122-
infos.meta.description = await page.$eval('head > meta[property="og:description"]', element => element.content).catch(() => '')
123-
}
124-
infos.meta.siteName = await page.$eval('head > meta[property="og:site_name"]', element => element.content).catch(() => '')
125-
126-
const rtaLabel = await page.$eval('head > meta[name="RATING"]', element => element.content).catch(() => '')
127-
if (rtaLabel.trim() === 'RTA-5042-1996-1400-1577-RTA') {
128-
infos.meta.isAdultContent = true
176+
throw err
129177
}
130-
131-
// Get page language
132-
const matches = html.match(new RegExp('<html[^>]*[: ]lang="([a-z]{2}((-|_)[A-Z]{2})?)"', 'i'));
133-
if (matches && matches.length) {
134-
infos.meta.language = matches[1].split('-')[0]
135-
}
136-
137-
// Get Vue version
138-
const version = await page.evaluate('(window.$nuxt && window.$nuxt.$root && window.$nuxt.$root.constructor.version) || (window.Vue && window.Vue.version) || [...document.querySelectorAll("*")].map((el) => el.__vue__ && el.__vue__.$root && el.__vue__.$root.constructor.version).filter(Boolean)[0]')
139-
if (version) {
140-
infos.vueVersion = version
141-
}
142-
143-
// Get Vue metas
144-
const { ssr } = await getVueMeta(context)
145-
infos.hasSSR = ssr
146-
147-
// Get Vue ecosystem infos
148-
infos.framework = await getFramework(context)
149-
infos.plugins = await getPlugins(context)
150-
infos.ui = await getUI(context)
151-
152-
// Get Nuxt modules if using NuxtJS
153-
if (infos.framework && infos.framework.name === 'NuxtJS') {
154-
const [meta, modules] = await Promise.all([
155-
getNuxtMeta(context),
156-
getNuxtModules(context)
157-
])
158-
infos.isStatic = meta.static
159-
infos.hasSSR = meta.ssr
160-
infos.frameworkModules = modules
161-
}
162-
163-
// Take screenshot
164-
const screenshotsDir = join(tmpdir(), 'vue-telemetry-analyzer')
165-
const filename = createHash('md5').update(originalUrl).digest('hex')
166-
infos.screenshot = join(screenshotsDir, `${filename}.jpg`)
167-
await makeDir(screenshotsDir)
168-
await page.screenshot({
169-
path: infos.screenshot,
170-
type: 'jpeg',
171-
quality: 90
172-
})
173-
174-
await page.close()
175-
return infos
176178
}
177179

178180
module.exports = analyze

0 commit comments

Comments
 (0)