async function checkOriginLink()

in packages/synthetics-sdk-broken-links/src/broken_links.ts [229:293]


async function checkOriginLink(
  originPage: Page,
  options: BrokenLinksResultV1_BrokenLinkCheckerOptions,
  startTime: string,
  adjusted_synthetic_timeout_millis: number,
  storageParams: StorageParameters
): Promise<BrokenLinksResultV1_SyntheticLinkResult> {
  let originLinkResult: BrokenLinksResultV1_SyntheticLinkResult;

  // Create Promise and variables used to set and resolve the time limit
  const [timeLimitPromise, timeLimitTimeout, timeLimitresolver] =
    getTimeLimitPromise(
      startTime,
      adjusted_synthetic_timeout_millis,
      /*extraOffsetMillis=*/ 500
    );

  const originLinkResultPromise = async () => {
    originLinkResult = await checkLink(
      originPage,
      { target_uri: options.origin_uri, anchor_text: '', html_element: '' },
      options,
      storageParams,
      true
    );

    try {
      if (options.wait_for_selector) {
        await originPage.waitForSelector(options.wait_for_selector, {
          timeout: options.link_timeout_millis,
        });
      }
    } catch (err) {
      originLinkResult.link_passed = false;
      if (err instanceof Error) {
        originLinkResult.error_type = err.name;
        originLinkResult.error_message = err.message;
        process.stderr.write(err.message);
      }
    }
    return true;
  };

  return Promise.race([timeLimitPromise, originLinkResultPromise()]).then(
    (finished) => {
      // clear timer and resolve (safe regardless of which promise finishes first)
      clearTimeout(timeLimitTimeout);
      timeLimitresolver();

      // if the time limit occured during the wait_for_selector operation
      if (!finished && originLinkResult) {
        originLinkResult.link_passed = false;
        originLinkResult.error_type = 'TimeoutError';
        originLinkResult.error_message = `Total Synthetic Timeout of ${options.total_synthetic_timeout_millis} milliseconds hit while waiting for selector '${options.wait_for_selector}'`;
        process.stderr.write(originLinkResult.error_message);
      }

      // If initialized returns a copy otherwise theres a possibility that
      // originLinkResultPromise will finish executing before `runBrokenLinks`
      // finishes, and since objects are pass by reference the
      // SyntheticLinkResult could change values.
      return Object.assign({}, originLinkResult);
    }
  );
}