UNPKG

2.95 kBTypeScriptView Raw
1import { EventEmitter } from 'node:events';
2import { type GaxiosResponse } from 'gaxios';
3import { type CheckOptions } from './options.js';
4import { Queue } from './queue.js';
5export { getConfig } from './config.js';
6export declare enum LinkState {
7 OK = "OK",
8 BROKEN = "BROKEN",
9 SKIPPED = "SKIPPED"
10}
11export type RetryInfo = {
12 url: string;
13 secondsUntilRetry: number;
14 status: number;
15};
16export type LinkResult = {
17 url: string;
18 status?: number;
19 state: LinkState;
20 parent?: string;
21 failureDetails?: Array<Error | GaxiosResponse>;
22};
23export type CrawlResult = {
24 passed: boolean;
25 links: LinkResult[];
26};
27type CrawlOptions = {
28 url: URL;
29 parent?: string;
30 crawl: boolean;
31 results: LinkResult[];
32 cache: Set<string>;
33 delayCache: Map<string, number>;
34 retryErrorsCache: Map<string, number>;
35 checkOptions: CheckOptions;
36 queue: Queue;
37 rootPath: string;
38 retry: boolean;
39 retryErrors: boolean;
40 retryErrorsCount: number;
41 retryErrorsJitter: number;
42};
43/**
44 * Instance class used to perform a crawl job.
45 */
46export declare class LinkChecker extends EventEmitter {
47 on(event: 'link', listener: (result: LinkResult) => void): this;
48 on(event: 'pagestart', listener: (link: string) => void): this;
49 on(event: 'retry', listener: (details: RetryInfo) => void): this;
50 /**
51 * Crawl a given url or path, and return a list of visited links along with
52 * status codes.
53 * @param options Options to use while checking for 404s
54 */
55 check(options_: CheckOptions): Promise<{
56 links: LinkResult[];
57 passed: boolean;
58 }>;
59 /**
60 * Crawl a given url with the provided options.
61 * @pram opts List of options used to do the crawl
62 * @private
63 * @returns A list of crawl results consisting of urls and status codes
64 */
65 crawl(options: CrawlOptions): Promise<void>;
66 /**
67 * Check the incoming response for a `retry-after` header. If present,
68 * and if the status was an HTTP 429, calculate the date at which this
69 * request should be retried. Ensure the delayCache knows that we're
70 * going to wait on requests for this entire host.
71 * @param response GaxiosResponse returned from the request
72 * @param opts CrawlOptions used during this request
73 */
74 shouldRetryAfter(response: GaxiosResponse, options: CrawlOptions): boolean;
75 /**
76 * If the response is a 5xx or synthetic 0 response retry N times.
77 * @param status Status returned by request or 0 if request threw.
78 * @param opts CrawlOptions used during this request
79 */
80 shouldRetryOnError(status: number, options: CrawlOptions): boolean;
81}
82/**
83 * Convenience method to perform a scan.
84 * @param options CheckOptions to be passed on
85 */
86export declare function check(options: CheckOptions): Promise<{
87 links: LinkResult[];
88 passed: boolean;
89}>;
90export type { CheckOptions } from './options.js';