Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extend utils file to include fetchwithtimeout fct #357

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 3 additions & 40 deletions src/backlinks/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,12 @@ import {
} from '@adobe/spacecat-shared-http-utils';
import { composeAuditURL } from '@adobe/spacecat-shared-utils';
import AhrefsAPIClient from '@adobe/spacecat-shared-ahrefs-client';
import { AbortController, AbortError } from '@adobe/fetch';
import { retrieveSiteBySiteId } from '../utils/data-access.js';
import { enhanceBacklinksWithFixes, fetch } from '../support/utils.js';

const TIMEOUT = 3000;
import { enhanceBacklinksWithFixes, isStillBrokenURL } from '../support/utils.js';

export async function filterOutValidBacklinks(backlinks, log) {
const fetchWithTimeout = async (url, timeout) => {
const controller = new AbortController();
const { signal } = controller;
const id = setTimeout(() => controller.abort(), timeout);

try {
const response = await fetch(url, { signal });
clearTimeout(id);
return response;
} catch (error) {
if (error instanceof AbortError) {
log.warn(`Request to ${url} timed out after ${timeout}ms`);
return { ok: false, status: 408 };
}
} finally {
clearTimeout(id);
}
return null;
};

const isStillBrokenBacklink = async (backlink) => {
try {
const response = await fetchWithTimeout(backlink.url_to, TIMEOUT);
if (!response.ok && response.status !== 404
&& response.status >= 400 && response.status < 500) {
log.warn(`Backlink ${backlink.url_to} returned status ${response.status}`);
}
return !response.ok;
} catch (error) {
log.error(`Failed to check backlink ${backlink.url_to}: ${error.message}`);
return true;
}
};

const backlinkStatuses = await Promise.all(backlinks.map(isStillBrokenBacklink));
return backlinks.filter((_, index) => backlinkStatuses[index]);
const backlinkStatuses = await Promise.allSettled(backlinks.map(async (backlink) => isStillBrokenURL(backlink.url_to, 'Backlink', log)));
return backlinks.filter((_, index) => backlinkStatuses[index].status === 'fulfilled' && backlinkStatuses[index].value);
}

export default async function auditBrokenBacklinks(message, context) {
Expand Down
48 changes: 47 additions & 1 deletion src/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
* governing permissions and limitations under the License.
*/

import { context as h2, h1 } from '@adobe/fetch';
import {
AbortController, AbortError, context as h2, h1,
} from '@adobe/fetch';
import { hasText, prependSchema, resolveCustomerSecretsName } from '@adobe/spacecat-shared-utils';
import URI from 'urijs';
import { JSDOM } from 'jsdom';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';

URI.preventInvalidHostname = true;
const TIMEOUT = 3000;

/* c8 ignore next 3 */
export const { fetch } = process.env.HELIX_FETCH_FORCE_HTTP1
Expand All @@ -32,6 +35,49 @@ export async function getRUMUrl(url) {
return finalUrl.endsWith('/') ? finalUrl.slice(0, -1) : /* c8 ignore next */ finalUrl;
}

/**
* Fetches a URL with a specified timeout.
*
* @async
* @param {string} url - The URL to fetch.
* @param {number} timeout - The timeout duration in milliseconds.
* @param {Object} log - The logging object to record information and errors.
* @returns {Promise<{ok: boolean, status: number}>} - A promise that resolves the response object
*/
export const fetchWithTimeout = async (url, timeout, log) => {
const controller = new AbortController();
const { signal } = controller;
const id = setTimeout(() => controller.abort(), timeout);

try {
const response = await fetch(url, { signal });
clearTimeout(id);
return response;
} catch (error) {
if (error instanceof AbortError) {
log.warn(`Request to ${url} timed out after ${timeout}ms`);
return { ok: false, status: 408 };
}
} finally {
clearTimeout(id);
}
return null;
};

export const isStillBrokenURL = async (url, label, log) => {
try {
const response = await fetchWithTimeout(url, TIMEOUT, log);
if (!response.ok && response.status !== 404
&& response.status >= 400 && response.status < 500) {
log.warn(`${label} ${url} returned status ${response.status}`);
}
return !response.ok;
} catch (error) {
log.error(`Failed to check ${label} ${url}: ${error.message}`);
return true;
}
};

/**
* Checks if a given URL contains a domain with a non-www subdomain.
*
Expand Down
Loading