diff --git a/writer-pipeline/src/http-retry.ts b/writer-pipeline/src/http-retry.ts index ecc6da9..10b9e7b 100644 --- a/writer-pipeline/src/http-retry.ts +++ b/writer-pipeline/src/http-retry.ts @@ -7,6 +7,13 @@ export interface FetchTextOptions { accept?: string; } +export interface FetchBinaryOptions { + timeoutMs?: number; + retries?: number; + userAgent?: string; + headers?: Record; +} + function sleep(ms: number): Promise { return new Promise((r) => setTimeout(r, ms)); } @@ -43,3 +50,36 @@ export async function fetchTextWithRetry( } throw lastErr instanceof Error ? lastErr : new Error(String(lastErr)); } + +/** + * GET url as binary with bounded retries (transient network / 5xx). + * Caller validates status, content-type, and size. + */ +export async function fetchBinaryWithRetry( + url: string, + opts: FetchBinaryOptions = {}, +): Promise { + const timeoutMs = opts.timeoutMs ?? 20_000; + const retries = Math.max(0, opts.retries ?? 2); + const ua = opts.userAgent ?? DEFAULT_UA; + const extraHeaders = opts.headers ?? {}; + let lastErr: unknown; + for (let attempt = 0; attempt <= retries; attempt += 1) { + try { + const res = await fetch(url, { + redirect: 'follow', + headers: { 'User-Agent': ua, ...extraHeaders }, + signal: AbortSignal.timeout(timeoutMs), + }); + if (res.status >= 500 && attempt < retries) { + await sleep(400 * (attempt + 1)); + continue; + } + return res; + } catch (e) { + lastErr = e; + if (attempt < retries) await sleep(500 * (attempt + 1)); + } + } + throw lastErr instanceof Error ? lastErr : new Error(String(lastErr)); +} diff --git a/writer-pipeline/src/legacy-fetch.ts b/writer-pipeline/src/legacy-fetch.ts index ed89c33..29fb24f 100644 --- a/writer-pipeline/src/legacy-fetch.ts +++ b/writer-pipeline/src/legacy-fetch.ts @@ -1,3 +1,5 @@ +import { fetchTextWithRetry } from './http-retry.js'; + /** * Raw HTML (capped) for OpenGraph / featured image extraction before publish. */ @@ -7,12 +9,11 @@ export async function fetchArticleHtmlForMeta( maxBytes = 600_000, ): Promise { try { - const res = await fetch(url, { - headers: { 'User-Agent': 'BitReport-news-writer-pipeline/1.0 (featured-image)' }, - signal: AbortSignal.timeout(timeoutMs), + const html = await fetchTextWithRetry(url, { + timeoutMs, + retries: 2, + userAgent: 'BitReport-news-writer-pipeline/1.0 (featured-image)', }); - if (!res.ok) return null; - const html = await res.text(); return html.slice(0, maxBytes); } catch { return null; diff --git a/writer-pipeline/src/wp-publish.ts b/writer-pipeline/src/wp-publish.ts index 2fc4087..4f31c30 100644 --- a/writer-pipeline/src/wp-publish.ts +++ b/writer-pipeline/src/wp-publish.ts @@ -1,6 +1,7 @@ import { Buffer } from 'node:buffer'; import type { AggregatedItem } from './types.js'; import { fetchArticleHtmlForMeta } from './legacy-fetch.js'; +import { fetchBinaryWithRetry } from './http-retry.js'; import { absolutizeImageUrl, extractFeaturedImageFromHtml } from './html-meta.js'; import { readWebDavArchiveFromEnv, @@ -131,10 +132,11 @@ async function downloadBinaryImage( 'User-Agent': 'BitReport-news-writer-pipeline/1.0 (media)', ...(extraHeaders ?? {}), }; - const res = await fetch(imageUrl, { + const res = await fetchBinaryWithRetry(imageUrl, { + timeoutMs: 20_000, + retries: 2, + userAgent: headers['User-Agent'], headers, - signal: AbortSignal.timeout(20_000), - redirect: 'follow', }); if (!res.ok) { throw new Error(`Featured image fetch failed ${res.status} for ${imageUrl}`);