Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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',
});
}
1 change: 1 addition & 0 deletions packages/@d-zero/beholder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
155 changes: 155 additions & 0 deletions packages/@d-zero/beholder/src/network-disconnection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
90 changes: 89 additions & 1 deletion packages/@d-zero/beholder/src/network-disconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,101 @@ export const NETWORK_DISCONNECTION_ERRORS: ReadonlySet<string> = 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);
}
14 changes: 10 additions & 4 deletions packages/@d-zero/beholder/src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -369,7 +369,8 @@ export default class Scraper extends EventEmitter<ScraperEventTypes> {
* 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
Expand Down Expand Up @@ -704,8 +705,13 @@ export default class Scraper extends EventEmitter<ScraperEventTypes> {
};
}

// 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})`)
Expand Down
Loading