Fix promise timeouts in NPM driver

main
Elbert Alias 4 years ago
parent 675150ad76
commit f5270fb2cf

@ -225,27 +225,26 @@ class Site {
}
}
/**
* @param {Promise} promise
* @param {string} [errorMessage]
*
* @return {Promise}
*/
async promiseTimeout(promise, errorMessage = 'The website took too long to respond') {
let timerId = null;
promiseTimeout(
promise,
errorMessage = 'The website took too long to respond'
) {
let timeout = null
return Promise.race([
new Promise((resolve, reject) => {
timerId = setTimeout(() => {
clearTimeout(timerId);
reject(new Error(errorMessage));
}, this.options.maxWait);
timeout = setTimeout(() => {
clearTimeout(timeout)
reject(new Error(errorMessage))
}, this.options.maxWait)
}),
promise.then((value) => {
clearTimeout(timerId);
return value;
clearTimeout(timeout)
return value
}),
]);
])
}
async goto(url) {
@ -352,11 +351,15 @@ class Site {
)
try {
await this.promiseTimeout(page.goto(url.href, { waitUntil: 'domcontentloaded' }))
await this.promiseTimeout(
page.goto(url.href, { waitUntil: 'domcontentloaded' })
)
await sleep(1000)
// Links
const links = await (
const links = await this.promiseTimeout(
(
await this.promiseTimeout(
page.evaluateHandle(() =>
Array.from(document.getElementsByTagName('a')).map(
@ -369,12 +372,14 @@ class Site {
rel,
})
)
),
)
)
).jsonValue()
)
// CSS
const css = await (
const css = await this.promiseTimeout(
(
await this.promiseTimeout(
page.evaluateHandle((maxRows) => {
const css = []
@ -398,23 +403,27 @@ class Site {
}
return css.join('\n')
}, this.options.htmlMaxRows),
}, this.options.htmlMaxRows)
)
).jsonValue()
)
// Script tags
const scripts = await (
const scripts = await this.promiseTimeout(
(
await this.promiseTimeout(
page.evaluateHandle(() =>
Array.from(document.getElementsByTagName('script'))
.map(({ src }) => src)
.filter((src) => src)
),
)
)
).jsonValue()
)
// Meta tags
const meta = await (
const meta = await this.promiseTimeout(
(
await this.promiseTimeout(
page.evaluateHandle(() =>
Array.from(document.querySelectorAll('meta')).reduce(
@ -430,9 +439,10 @@ class Site {
},
{}
)
),
)
)
).jsonValue()
)
// JavaScript
const js = await this.promiseTimeout(
@ -467,7 +477,7 @@ class Site {
Wappalyzer.technologies
.filter(({ js }) => Object.keys(js).length)
.map(({ name, js }) => ({ name, chains: Object.keys(js) }))
),
)
)
// Cookies
@ -565,7 +575,11 @@ class Site {
return reducedLinks
} catch (error) {
this.error(error)
if (error.constructor.name === 'TimeoutError') {
throw new Error('The website took too long to respond')
}
throw new Error(error.message)
}
}

Loading…
Cancel
Save