in packages/synthetics-sdk-broken-links/src/navigation_func.ts [105:157]
export async function checkLinks(
browser: Browser,
links: LinkIntermediate[],
options: BrokenLinksResultV1_BrokenLinkCheckerOptions,
startTime: string,
total_timeout_millis: number,
storageParams: StorageParameters
): Promise<BrokenLinksResultV1_SyntheticLinkResult[]> {
let timeLimitReached = false;
const followed_links: BrokenLinksResultV1_SyntheticLinkResult[] = [];
// Create Promise and variables used to set and resolve the time limit
const [timeLimitPromise, timeLimitTimeout, timeLimitresolver] =
getTimeLimitPromise(startTime, total_timeout_millis, 500);
const sequentialPromises = async () => {
const page = await openNewPage(browser);
for (const link of links) {
// prevents links from being checked after timeout is hit
if (timeLimitReached) return false;
try {
followed_links.push(
await checkLink(page, link, options, storageParams)
);
/** In the case of a single page app, network requests can hang and cause
* timeout issues in following links. To ensure this does not happen we
* need to reset the page in between every link checked
*/
await gotoBlankPage(page);
} catch (err) {
if (err instanceof Error) process.stderr.write(err.message);
throw new Error(
`An error occurred while checking ${link.target_uri}. Please reference server logs for further information.`
);
}
}
return true;
};
return Promise.race([timeLimitPromise, sequentialPromises()]).then(
(sequentialPromisesfinished) => {
// set timeLimitReached so that `sequentialPromises()` stops executing
timeLimitReached = !sequentialPromisesfinished;
// clear timer and resolve (safe regardless of which promise finishes first)
clearTimeout(timeLimitTimeout);
timeLimitresolver();
return followed_links;
}
);
}