diff --git a/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.spec.ts b/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.spec.ts new file mode 100644 index 00000000..524c795b --- /dev/null +++ b/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.spec.ts @@ -0,0 +1,48 @@ +import { describe, it, expect } from 'vitest'; + +import { getDisconnectionFailuresForPage } from './get-disconnection-failures-for-page.js'; + +describe('getDisconnectionFailuresForPage', () => { + const mixed = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/style.css', + errorText: 'net::ERR_ABORTED', + }, + ]; + + it('defaults to same-host filtering using the page hostname', () => { + expect(getDisconnectionFailuresForPage(mixed, 'example.com')).toStrictEqual([ + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]); + }); + + it('with scope all, returns every disconnection failure', () => { + expect(getDisconnectionFailuresForPage(mixed, 'example.com', 'all')).toStrictEqual([ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]); + }); + + it('does not mutate the input failedRequests array', () => { + const input = [...mixed]; + getDisconnectionFailuresForPage(input, 'example.com'); + expect(input).toStrictEqual(mixed); + }); +}); diff --git a/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.ts b/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.ts new file mode 100644 index 00000000..0f600435 --- /dev/null +++ b/packages/@d-zero/beholder/src/get-disconnection-failures-for-page.ts @@ -0,0 +1,36 @@ +import type { NetworkDisconnectionScope } from './network-disconnection.js'; + +import { findDisconnectionFailures } from './network-disconnection.js'; + +/** + * Page-scrape entry for disconnection detection: applies the Scraper default + * of `'same-host'` and always passes the page hostname into the filter. + * + * WHY a wrapper over {@link findDisconnectionFailures}: the helper keeps a + * one-argument historical contract (no host filter) for existing unit callers. + * Scraper must not call that one-argument form, or third-party resets would + * again fail the whole page. This function is the only call shape `#fetchData` + * should use. + * @param failedRequests - All failed sub-resource requests collected so far + * @param pageHostname - Hostname of the page body URL + * @param scope - Override for the Scraper option; defaults to `'same-host'` + * @returns Disconnection failures that should trigger a page-level retry + * @example + * ```ts + * getDisconnectionFailuresForPage(failedRequests, 'example.com'); + * getDisconnectionFailuresForPage(failedRequests, 'example.com', 'all'); + * ``` + */ +export function getDisconnectionFailuresForPage( + failedRequests: ReadonlyArray<{ url: string; errorText: string }>, + pageHostname: string, + scope: NetworkDisconnectionScope = 'same-host', +): Array<{ url: string; errorText: string }> { + if (scope === 'all') { + return findDisconnectionFailures(failedRequests, { scope: 'all' }); + } + return findDisconnectionFailures(failedRequests, { + pageHostname, + scope: 'same-host', + }); +} diff --git a/packages/@d-zero/beholder/src/index.ts b/packages/@d-zero/beholder/src/index.ts index 92973da4..cd531c5c 100644 --- a/packages/@d-zero/beholder/src/index.ts +++ b/packages/@d-zero/beholder/src/index.ts @@ -26,6 +26,7 @@ export { IMAGE_SCAN_CODE } from './image-scan-code.js'; export type { ImageScanCode } from './image-scan-code.js'; export type { ScrapeResult, ResourceEntry, ConsoleLogEntry, PageData } from './types.js'; export type { ScraperOptions, ChangePhaseEvent, ScraperEventTypes } from './types.js'; +export type { NetworkDisconnectionScope } from './network-disconnection.js'; export type { Resource, AnchorData, diff --git a/packages/@d-zero/beholder/src/network-disconnection.spec.ts b/packages/@d-zero/beholder/src/network-disconnection.spec.ts index dae72dc9..fd986bdf 100644 --- a/packages/@d-zero/beholder/src/network-disconnection.spec.ts +++ b/packages/@d-zero/beholder/src/network-disconnection.spec.ts @@ -65,4 +65,159 @@ describe('findDisconnectionFailures', () => { const result = findDisconnectionFailures(failures); expect(result).toHaveLength(3); }); + + it('with scope same-host, returns only same-host disconnection failures', () => { + const failures = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://fonts.gstatic.com/s/font.woff2', + errorText: 'net::ERR_CONNECTION_TIMED_OUT', + }, + ]; + const result = findDisconnectionFailures(failures, { + pageHostname: 'example.com', + scope: 'same-host', + }); + expect(result).toStrictEqual([ + { url: 'https://example.com/app.js', errorText: 'net::ERR_CONNECTION_RESET' }, + ]); + }); + + it('with scope same-host, returns empty when only third-party disconnection failures exist', () => { + const failures = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://www.youtube.com/iframe_api', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + expect( + findDisconnectionFailures(failures, { + pageHostname: 'www.example.com', + scope: 'same-host', + }), + ).toStrictEqual([]); + }); + + it('with scope all, returns every disconnection failure regardless of host', () => { + const failures = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + const result = findDisconnectionFailures(failures, { + pageHostname: 'example.com', + scope: 'all', + }); + expect(result).toStrictEqual(failures); + }); + + it('with scope same-host, excludes hostless or unparsable failure URLs', () => { + const failures = [ + { url: 'not-a-url', errorText: 'net::ERR_CONNECTION_RESET' }, + { url: 'data:text/plain,hi', errorText: 'net::ERR_CONNECTION_RESET' }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + const result = findDisconnectionFailures(failures, { + pageHostname: 'example.com', + scope: 'same-host', + }); + expect(result).toStrictEqual([ + { url: 'https://example.com/app.js', errorText: 'net::ERR_CONNECTION_RESET' }, + ]); + }); + + it('with options but omitted scope, defaults to same-host filtering', () => { + const failures = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + const result = findDisconnectionFailures(failures, { + pageHostname: 'example.com', + }); + expect(result).toStrictEqual([ + { url: 'https://example.com/app.js', errorText: 'net::ERR_CONNECTION_RESET' }, + ]); + }); + + it('with scope same-host, still drops non-disconnection errors from mixed input', () => { + const failures = [ + { url: 'https://example.com/style.css', errorText: 'net::ERR_ABORTED' }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + expect( + findDisconnectionFailures(failures, { + pageHostname: 'example.com', + scope: 'same-host', + }), + ).toStrictEqual([ + { url: 'https://example.com/app.js', errorText: 'net::ERR_CONNECTION_RESET' }, + ]); + }); + + it('with scope same-host, requires exact hostname equality (not subdomain)', () => { + const failures = [ + { + url: 'https://www.example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://cdn.example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + { + url: 'https://example.com/app.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + expect( + findDisconnectionFailures(failures, { + pageHostname: 'example.com', + scope: 'same-host', + }), + ).toStrictEqual([ + { url: 'https://example.com/app.js', errorText: 'net::ERR_CONNECTION_RESET' }, + ]); + }); + + it('with scope all, pageHostname may be omitted', () => { + const failures = [ + { + url: 'https://www.googletagmanager.com/gtm.js', + errorText: 'net::ERR_CONNECTION_RESET', + }, + ]; + expect(findDisconnectionFailures(failures, { scope: 'all' })).toStrictEqual(failures); + }); }); diff --git a/packages/@d-zero/beholder/src/network-disconnection.ts b/packages/@d-zero/beholder/src/network-disconnection.ts index 9a04963c..025ab21a 100644 --- a/packages/@d-zero/beholder/src/network-disconnection.ts +++ b/packages/@d-zero/beholder/src/network-disconnection.ts @@ -21,13 +21,101 @@ export const NETWORK_DISCONNECTION_ERRORS: ReadonlySet = new Set([ 'net::ERR_CONNECTION_TIMED_OUT', ]); +/** + * Which failed requests count as a page-level network disconnection. + * + * - `'same-host'`: only failures whose URL hostname string equals the page hostname + * - `'all'`: any sub-resource disconnection failure (historical behavior) + * + * Why not eTLD+1 / subdomain matching: the scraper already classifies resources with + * exact `hostname` equality. Widening to registrable domain would retry on CDN hosts + * (`cdn.example.com`) that this option deliberately leaves as non-fatal under the + * default, and `www.example.com` ≠ `example.com` stays consistent with that axis. + * Why not same-origin: scheme/port differences are rare for sub-resources of the + * same page and would diverge from the existing hostname-based `isExternal` check. + */ +export type NetworkDisconnectionScope = 'same-host' | 'all'; + +/** + * Options for {@link findDisconnectionFailures}. + * + * `'same-host'` (including when `scope` is omitted) requires `pageHostname`. + * `'all'` does not. + * + * Why not a single default for both call shapes: omitting the whole `options` + * argument must keep the historical one-argument behavior (every disconnection + * error, no host filter) so existing unit fixtures stay valid without rewrite. + * When `options` is provided, omitted `scope` defaults to `'same-host'` so + * passing only `pageHostname` cannot silently ignore the host filter. + * Scraper code should call {@link getDisconnectionFailuresForPage} instead, + * which always uses the page-scrape default of `'same-host'`. + */ +export type FindDisconnectionFailuresOptions = + | { + scope?: 'same-host'; + pageHostname: string; + } + | { + scope: 'all'; + pageHostname?: string; + }; + +/** + * Returns the hostname of a request URL, or `null` when the URL cannot be + * parsed or has no hostname (data:/blob:/opaque). + * @param url - Failed request URL string from Puppeteer + */ +function requestHostname(url: string): string | null { + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return null; + } + return parsed.hostname || null; +} + /** * Checks whether any of the given failed requests were caused by a network disconnection. + * + * WHY optional host filter: third-party hosts (analytics, embeds, fonts CDNs) can + * fail with `ERR_CONNECTION_RESET` on restricted networks even when the page body + * loaded. Treating those as page-level disconnection discards reachable HTML. + * Callers that still want the historical all-hosts strictness pass `scope: 'all'`. + * + * When `options` is omitted, every disconnection failure is returned (historical + * one-argument contract). When `options` is provided, `scope` defaults to + * `'same-host'` and failures are limited to exact `pageHostname` string equality + * (www/apex/CDN subdomains do not match each other). * @param failedRequests - Array of failed request entries + * @param options - Optional scope / page hostname for filtering * @returns The subset of failed requests caused by network disconnection, empty array if none + * @example + * ```ts + * findDisconnectionFailures(failedRequests, { + * pageHostname: 'example.com', + * scope: 'same-host', + * }); + * ``` */ export function findDisconnectionFailures( failedRequests: ReadonlyArray<{ url: string; errorText: string }>, + options?: FindDisconnectionFailuresOptions, ): Array<{ url: string; errorText: string }> { - return failedRequests.filter((r) => NETWORK_DISCONNECTION_ERRORS.has(r.errorText)); + const disconnectionFailures = failedRequests.filter((r) => + NETWORK_DISCONNECTION_ERRORS.has(r.errorText), + ); + + // No options object: preserve the historical one-argument contract. + if (!options) { + return disconnectionFailures; + } + + const scope = options.scope ?? 'same-host'; + if (scope === 'all') { + return disconnectionFailures; + } + + const { pageHostname } = options; + return disconnectionFailures.filter((r) => requestHostname(r.url) === pageHostname); } diff --git a/packages/@d-zero/beholder/src/scraper.ts b/packages/@d-zero/beholder/src/scraper.ts index f6f2b102..f54c1159 100644 --- a/packages/@d-zero/beholder/src/scraper.ts +++ b/packages/@d-zero/beholder/src/scraper.ts @@ -40,7 +40,7 @@ import { isHtmlContentType } from './is-html-content-type.js'; import { keywordCheck } from './keyword-check.js'; import { measureScrollHeight } from './measure-scroll-height.js'; import { emptyMeta } from './meta/classify.js'; -import { findDisconnectionFailures } from './network-disconnection.js'; +import { getDisconnectionFailuresForPage } from './get-disconnection-failures-for-page.js'; import { parseUrl } from './parse-url.js'; import { toConsoleLogEntry } from './to-console-log-entry.js'; import { toPageErrorEntry } from './to-page-error-entry.js'; @@ -369,7 +369,8 @@ export default class Scraper extends EventEmitter { * 2. Navigate to URL via `page.goto()` and track redirect chain * 3. Emit `loadDOMContent` (marker only), then best-effort network idle, * then HTML snapshot (title/html) so later anchors/meta share a settled DOM - * 4. Check for network disconnection errors and throw to trigger retry + * 4. Check for same-host (by default) network disconnection errors and + * throw to trigger retry; override with `networkDisconnectionScope` * 5. Extract anchors, meta, and optionally images * 6. Check for keyword exclusion in HTML content * @param page - Puppeteer page instance @@ -704,8 +705,13 @@ export default class Scraper extends EventEmitter { }; } - // Check for network disconnection errors in failed requests - const disconnectionFailures = findDisconnectionFailures(failedRequests); + // Check for network disconnection errors in failed requests. + // Default same-host: third-party resets must not fail a loaded page body. + const disconnectionFailures = getDisconnectionFailuresForPage( + failedRequests, + url.hostname, + options?.networkDisconnectionScope ?? 'same-host', + ); if (disconnectionFailures.length > 0) { const errorSummary = disconnectionFailures .map((r) => `${r.url} (${r.errorText})`) diff --git a/packages/@d-zero/beholder/src/types.ts b/packages/@d-zero/beholder/src/types.ts index 9d82f2c2..7c56a9a4 100644 --- a/packages/@d-zero/beholder/src/types.ts +++ b/packages/@d-zero/beholder/src/types.ts @@ -58,6 +58,7 @@ export type { import type { ImageScanCode } from './image-scan-code.js'; import type { Meta } from './meta/types.js'; +import type { NetworkDisconnectionScope } from './network-disconnection.js'; import type { CDNType } from '@d-zero/shared/detect-cdn'; import type { CompressType } from '@d-zero/shared/detect-compress'; import type { ExURL } from '@d-zero/shared/parse-url'; @@ -689,4 +690,26 @@ export type ScraperOptions = { * (highest priority). When omitted or `null`, only the built-in selectors are used. */ mainContentSelector?: string | null; + /** + * Which failed sub-resource requests count as a page-level network + * disconnection that triggers retry. + * + * WHY default `'same-host'`: third-party hosts (tag managers, embeds, font + * CDNs) can fail with Chromium disconnection codes on restricted networks + * while the page body itself loaded. Treating those as page failure drops + * reachable anchors/HTML. Use `'all'` to restore the historical strictness + * that retries on any sub-resource disconnection error. + * + * Matching is exact hostname equality (not eTLD+1 / subdomain / origin). + * Defaults to `'same-host'` when omitted. `failedRequests` on the result + * still lists every failure regardless of this setting. + * @example + * ```ts + * // Historical strictness: retry when any sub-resource disconnects + * await scraper.scrapeStart(page, url, { + * networkDisconnectionScope: 'all', + * }); + * ``` + */ + networkDisconnectionScope?: NetworkDisconnectionScope; };