Fixes #3345: Close a timer after resolving/erroring the `Promise.race()`

main
Sergii Bondarenko 4 years ago
parent afbc55db94
commit 8e60696062
No known key found for this signature in database
GPG Key ID: 242150A50CD96A36

@ -225,12 +225,27 @@ class Site {
} }
} }
timeout() { /**
return new Promise((resolve, reject) => * @param {Promise} promise
setTimeout(() => { * @param {string} [errorMessage]
reject(new Error('The website took too long to respond')) *
}, this.options.maxWait) * @return {Promise}
) */
async promiseTimeout(promise, errorMessage = 'The website took too long to respond') {
let timerId = null;
return Promise.race([
new Promise((resolve, reject) => {
timerId = setTimeout(() => {
clearTimeout(timerId);
reject(new Error(errorMessage));
}, this.options.maxWait);
}),
promise.then((value) => {
clearTimeout(timerId);
return value;
}),
]);
} }
async goto(url) { async goto(url) {
@ -337,17 +352,12 @@ class Site {
) )
try { try {
await Promise.race([ await this.promiseTimeout(page.goto(url.href, { waitUntil: 'domcontentloaded' }))
this.timeout(),
page.goto(url.href, { waitUntil: 'domcontentloaded' }),
])
await sleep(1000) await sleep(1000)
// Links // Links
const links = await ( const links = await (
await Promise.race([ await this.promiseTimeout(
this.timeout(),
page.evaluateHandle(() => page.evaluateHandle(() =>
Array.from(document.getElementsByTagName('a')).map( Array.from(document.getElementsByTagName('a')).map(
({ hash, hostname, href, pathname, protocol, rel }) => ({ ({ hash, hostname, href, pathname, protocol, rel }) => ({
@ -360,13 +370,12 @@ class Site {
}) })
) )
), ),
]) )
).jsonValue() ).jsonValue()
// CSS // CSS
const css = await ( const css = await (
await Promise.race([ await this.promiseTimeout(
this.timeout(),
page.evaluateHandle((maxRows) => { page.evaluateHandle((maxRows) => {
const css = [] const css = []
@ -390,25 +399,23 @@ class Site {
return css.join('\n') return css.join('\n')
}, this.options.htmlMaxRows), }, this.options.htmlMaxRows),
]) )
).jsonValue() ).jsonValue()
// Script tags // Script tags
const scripts = await ( const scripts = await (
await Promise.race([ await this.promiseTimeout(
this.timeout(),
page.evaluateHandle(() => page.evaluateHandle(() =>
Array.from(document.getElementsByTagName('script')) Array.from(document.getElementsByTagName('script'))
.map(({ src }) => src) .map(({ src }) => src)
.filter((src) => src) .filter((src) => src)
), ),
]) )
).jsonValue() ).jsonValue()
// Meta tags // Meta tags
const meta = await ( const meta = await (
await Promise.race([ await this.promiseTimeout(
this.timeout(),
page.evaluateHandle(() => page.evaluateHandle(() =>
Array.from(document.querySelectorAll('meta')).reduce( Array.from(document.querySelectorAll('meta')).reduce(
(metas, meta) => { (metas, meta) => {
@ -424,12 +431,11 @@ class Site {
{} {}
) )
), ),
]) )
).jsonValue() ).jsonValue()
// JavaScript // JavaScript
const js = await Promise.race([ const js = await this.promiseTimeout(
this.timeout(),
page.evaluate( page.evaluate(
(technologies) => { (technologies) => {
return technologies.reduce((technologies, { name, chains }) => { return technologies.reduce((technologies, { name, chains }) => {
@ -462,7 +468,7 @@ class Site {
.filter(({ js }) => Object.keys(js).length) .filter(({ js }) => Object.keys(js).length)
.map(({ name, js }) => ({ name, chains: Object.keys(js) })) .map(({ name, js }) => ({ name, chains: Object.keys(js) }))
), ),
]) )
// Cookies // Cookies
const cookies = (await page.cookies()).reduce( const cookies = (await page.cookies()).reduce(

Loading…
Cancel
Save