Merge pull request #3346 from BR0kEN-/issues/3345

#3345: Close a timer after resolving/erroring the `Promise.race()`
main
Elbert Alias 4 years ago committed by GitHub
commit 6baa8fe7b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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