@detiq/crawler 1.1.9 → 1.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/accessibility-auditor.js +54 -74
- package/dist/ai-login.js +227 -249
- package/dist/auth-token-rotator.js +57 -67
- package/dist/auth.js +299 -336
- package/dist/browser-extension.js +104 -142
- package/dist/captcha-solver.js +98 -125
- package/dist/color-contrast.js +77 -89
- package/dist/content-chunker.js +126 -126
- package/dist/content-dedup.js +54 -71
- package/dist/cookie-consent.js +68 -83
- package/dist/cookie-jar.js +53 -60
- package/dist/crawler.js +4494 -5353
- package/dist/custom-js-injector.js +45 -61
- package/dist/dark-mode-capture.js +67 -81
- package/dist/dead-link-detector.js +47 -55
- package/dist/element-hash.js +8 -12
- package/dist/email-otp.js +154 -195
- package/dist/engine-router.js +87 -98
- package/dist/export/bulk-export.js +70 -55
- package/dist/export/vector-export.js +77 -105
- package/dist/extractors/advanced.js +249 -266
- package/dist/extractors/artifact-envelope.js +59 -58
- package/dist/failure-analysis-agent.js +37 -33
- package/dist/feature-flag-detector.js +51 -66
- package/dist/form-detector.js +55 -75
- package/dist/frontier.js +76 -101
- package/dist/gap-pass.js +68 -89
- package/dist/graphql-detector.js +62 -72
- package/dist/http-engine.js +282 -330
- package/dist/incremental-diff.js +54 -87
- package/dist/index.js +67 -28
- package/dist/input-fuzzer.js +93 -97
- package/dist/interactive-probe.js +715 -827
- package/dist/job-queue.js +2151 -2169
- package/dist/js-coverage.js +52 -56
- package/dist/js-router-detector.js +86 -117
- package/dist/language-detector.js +51 -63
- package/dist/link-classifier.js +32 -50
- package/dist/live-view.js +91 -108
- package/dist/locator-extractor.js +151 -187
- package/dist/logger.js +9 -7
- package/dist/markdown-extract.js +48 -61
- package/dist/native-crawler.js +780 -946
- package/dist/network-observer.js +135 -156
- package/dist/network-throttle.js +29 -40
- package/dist/openapi-generator.js +77 -92
- package/dist/pdf-extractor.js +72 -90
- package/dist/performance-metrics.js +80 -102
- package/dist/phash.js +125 -144
- package/dist/pii-scrubber.js +65 -68
- package/dist/playwright-recorder.js +70 -58
- package/dist/policies/crawl-safety.js +31 -34
- package/dist/profiles/domain-profile.js +130 -125
- package/dist/proxy.js +146 -172
- package/dist/rate-limiter.js +58 -65
- package/dist/redirect-tracker.js +60 -64
- package/dist/redis-frontier.js +116 -136
- package/dist/redis-queue.js +157 -144
- package/dist/response-cache.js +127 -140
- package/dist/responsive-detector.js +42 -46
- package/dist/retry-policy.js +46 -50
- package/dist/robots-txt.js +69 -83
- package/dist/screen-reader.js +45 -70
- package/dist/screenshot-masker.js +59 -77
- package/dist/seo-checker.js +88 -107
- package/dist/session-capture.js +192 -212
- package/dist/session-export.js +107 -133
- package/dist/sitemap-writer.js +38 -41
- package/dist/sitemap.js +141 -202
- package/dist/skyvern.js +123 -148
- package/dist/sqs-queue.js +42 -42
- package/dist/sso-token.js +74 -90
- package/dist/stealth/human-behavior.js +156 -173
- package/dist/telemetry.js +68 -82
- package/dist/test-executor.js +309 -335
- package/dist/tls-fingerprint.js +77 -88
- package/dist/url-pattern-completer.js +71 -90
- package/dist/visual-diff-verdict.js +23 -38
- package/dist/webhook-notifier.js +71 -69
- package/dist/websocket-detector.js +56 -68
- package/dist/worker.js +29 -38
- package/package.json +1 -1
- package/src/job-queue.ts +136 -0
package/dist/response-cache.js
CHANGED
|
@@ -1,153 +1,140 @@
|
|
|
1
|
-
import { createHash } from
|
|
2
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync, statSync, unlinkSync } from
|
|
3
|
-
import { join } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync, statSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
class ResponseCache {
|
|
5
|
+
dir;
|
|
6
|
+
totalHits = 0;
|
|
7
|
+
totalRequests = 0;
|
|
8
|
+
constructor(cacheDir) {
|
|
9
|
+
this.dir = cacheDir;
|
|
10
|
+
if (!existsSync(cacheDir)) mkdirSync(cacheDir, { recursive: true });
|
|
11
|
+
}
|
|
12
|
+
key(url) {
|
|
13
|
+
return createHash("sha256").update(url).digest("hex").slice(0, 16);
|
|
14
|
+
}
|
|
15
|
+
path(url) {
|
|
16
|
+
return join(this.dir, `${this.key(url)}.json`);
|
|
17
|
+
}
|
|
18
|
+
get(url) {
|
|
19
|
+
this.totalRequests++;
|
|
20
|
+
try {
|
|
21
|
+
const p = this.path(url);
|
|
22
|
+
if (!existsSync(p)) return null;
|
|
23
|
+
const entry = JSON.parse(readFileSync(p, "utf-8"));
|
|
24
|
+
this.totalHits++;
|
|
25
|
+
return entry;
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
12
28
|
}
|
|
13
|
-
|
|
14
|
-
|
|
29
|
+
}
|
|
30
|
+
read(url) {
|
|
31
|
+
try {
|
|
32
|
+
const p = this.path(url);
|
|
33
|
+
if (!existsSync(p)) return null;
|
|
34
|
+
return JSON.parse(readFileSync(p, "utf-8"));
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
15
37
|
}
|
|
16
|
-
|
|
17
|
-
|
|
38
|
+
}
|
|
39
|
+
isFresh(url) {
|
|
40
|
+
const entry = this.read(url);
|
|
41
|
+
if (!entry?.cacheControl) return false;
|
|
42
|
+
const m = /max-age=(\d+)/i.exec(entry.cacheControl);
|
|
43
|
+
if (!m) return false;
|
|
44
|
+
const maxAgeMs = parseInt(m[1], 10) * 1e3;
|
|
45
|
+
return entry.cachedAt + maxAgeMs > Date.now();
|
|
46
|
+
}
|
|
47
|
+
staleIfError(url) {
|
|
48
|
+
const entry = this.read(url);
|
|
49
|
+
if (!entry?.cacheControl) return false;
|
|
50
|
+
const m = /stale-if-error=(\d+)/i.exec(entry.cacheControl);
|
|
51
|
+
if (!m) return false;
|
|
52
|
+
const staleIfErrorMs = parseInt(m[1], 10) * 1e3;
|
|
53
|
+
return entry.cachedAt + staleIfErrorMs >= Date.now();
|
|
54
|
+
}
|
|
55
|
+
set(url, entry) {
|
|
56
|
+
try {
|
|
57
|
+
const full = { ...entry, cachedAt: Date.now(), hitCount: 0 };
|
|
58
|
+
writeFileSync(this.path(url), JSON.stringify(full), "utf-8");
|
|
59
|
+
} catch {
|
|
18
60
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!existsSync(p))
|
|
37
|
-
return null;
|
|
38
|
-
return JSON.parse(readFileSync(p, 'utf-8'));
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
isFresh(url) {
|
|
45
|
-
const entry = this.read(url);
|
|
46
|
-
if (!entry?.cacheControl)
|
|
47
|
-
return false;
|
|
48
|
-
const m = /max-age=(\d+)/i.exec(entry.cacheControl);
|
|
49
|
-
if (!m)
|
|
50
|
-
return false;
|
|
51
|
-
const maxAgeMs = parseInt(m[1], 10) * 1000;
|
|
52
|
-
return entry.cachedAt + maxAgeMs > Date.now();
|
|
53
|
-
}
|
|
54
|
-
staleIfError(url) {
|
|
55
|
-
const entry = this.read(url);
|
|
56
|
-
if (!entry?.cacheControl)
|
|
57
|
-
return false;
|
|
58
|
-
const m = /stale-if-error=(\d+)/i.exec(entry.cacheControl);
|
|
59
|
-
if (!m)
|
|
60
|
-
return false;
|
|
61
|
-
const staleIfErrorMs = parseInt(m[1], 10) * 1000;
|
|
62
|
-
return entry.cachedAt + staleIfErrorMs >= Date.now();
|
|
63
|
-
}
|
|
64
|
-
set(url, entry) {
|
|
65
|
-
try {
|
|
66
|
-
const full = { ...entry, cachedAt: Date.now(), hitCount: 0 };
|
|
67
|
-
writeFileSync(this.path(url), JSON.stringify(full), 'utf-8');
|
|
68
|
-
}
|
|
69
|
-
catch { /* non-fatal */ }
|
|
70
|
-
}
|
|
71
|
-
getValidationHeaders(url) {
|
|
72
|
-
const entry = this.get(url);
|
|
73
|
-
if (!entry)
|
|
74
|
-
return {};
|
|
75
|
-
const headers = {};
|
|
76
|
-
if (entry.etag)
|
|
77
|
-
headers['If-None-Match'] = entry.etag;
|
|
78
|
-
if (entry.lastModified)
|
|
79
|
-
headers['If-Modified-Since'] = entry.lastModified;
|
|
80
|
-
return headers;
|
|
81
|
-
}
|
|
82
|
-
markHit(url) {
|
|
83
|
-
try {
|
|
84
|
-
const p = this.path(url);
|
|
85
|
-
if (!existsSync(p))
|
|
86
|
-
return;
|
|
87
|
-
const entry = JSON.parse(readFileSync(p, 'utf-8'));
|
|
88
|
-
entry.hitCount++;
|
|
89
|
-
writeFileSync(p, JSON.stringify(entry), 'utf-8');
|
|
90
|
-
}
|
|
91
|
-
catch { /* non-fatal */ }
|
|
61
|
+
}
|
|
62
|
+
getValidationHeaders(url) {
|
|
63
|
+
const entry = this.get(url);
|
|
64
|
+
if (!entry) return {};
|
|
65
|
+
const headers = {};
|
|
66
|
+
if (entry.etag) headers["If-None-Match"] = entry.etag;
|
|
67
|
+
if (entry.lastModified) headers["If-Modified-Since"] = entry.lastModified;
|
|
68
|
+
return headers;
|
|
69
|
+
}
|
|
70
|
+
markHit(url) {
|
|
71
|
+
try {
|
|
72
|
+
const p = this.path(url);
|
|
73
|
+
if (!existsSync(p)) return;
|
|
74
|
+
const entry = JSON.parse(readFileSync(p, "utf-8"));
|
|
75
|
+
entry.hitCount++;
|
|
76
|
+
writeFileSync(p, JSON.stringify(entry), "utf-8");
|
|
77
|
+
} catch {
|
|
92
78
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
catch { /* non-fatal */ }
|
|
79
|
+
}
|
|
80
|
+
invalidate(url) {
|
|
81
|
+
try {
|
|
82
|
+
const p = this.path(url);
|
|
83
|
+
if (existsSync(p)) unlinkSync(p);
|
|
84
|
+
} catch {
|
|
100
85
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
catch { /* non-fatal */ }
|
|
86
|
+
}
|
|
87
|
+
clear() {
|
|
88
|
+
try {
|
|
89
|
+
for (const f of readdirSync(this.dir)) {
|
|
90
|
+
if (f.endsWith(".json")) unlinkSync(join(this.dir, f));
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
109
93
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
94
|
+
}
|
|
95
|
+
stats() {
|
|
96
|
+
let entries = 0;
|
|
97
|
+
let totalSizeBytes = 0;
|
|
98
|
+
try {
|
|
99
|
+
for (const f of readdirSync(this.dir)) {
|
|
100
|
+
if (!f.endsWith(".json")) continue;
|
|
101
|
+
entries++;
|
|
113
102
|
try {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
continue;
|
|
117
|
-
entries++;
|
|
118
|
-
try {
|
|
119
|
-
totalSizeBytes += statSync(join(this.dir, f)).size;
|
|
120
|
-
}
|
|
121
|
-
catch { /* skip */ }
|
|
122
|
-
}
|
|
103
|
+
totalSizeBytes += statSync(join(this.dir, f)).size;
|
|
104
|
+
} catch {
|
|
123
105
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
entries,
|
|
127
|
-
totalSizeBytes,
|
|
128
|
-
hitRate: this.totalRequests > 0 ? this.totalHits / this.totalRequests : 0,
|
|
129
|
-
totalHits: this.totalHits,
|
|
130
|
-
};
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
131
108
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
109
|
+
return {
|
|
110
|
+
entries,
|
|
111
|
+
totalSizeBytes,
|
|
112
|
+
hitRate: this.totalRequests > 0 ? this.totalHits / this.totalRequests : 0,
|
|
113
|
+
totalHits: this.totalHits
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
prune(maxAgeMs) {
|
|
117
|
+
let deleted = 0;
|
|
118
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
119
|
+
try {
|
|
120
|
+
for (const f of readdirSync(this.dir)) {
|
|
121
|
+
if (!f.endsWith(".json")) continue;
|
|
135
122
|
try {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
unlinkSync(p);
|
|
144
|
-
deleted++;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch { /* skip */ }
|
|
148
|
-
}
|
|
123
|
+
const p = join(this.dir, f);
|
|
124
|
+
const entry = JSON.parse(readFileSync(p, "utf-8"));
|
|
125
|
+
if (entry.cachedAt < cutoff) {
|
|
126
|
+
unlinkSync(p);
|
|
127
|
+
deleted++;
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
149
130
|
}
|
|
150
|
-
|
|
151
|
-
|
|
131
|
+
}
|
|
132
|
+
} catch {
|
|
152
133
|
}
|
|
134
|
+
return deleted;
|
|
135
|
+
}
|
|
153
136
|
}
|
|
137
|
+
export {
|
|
138
|
+
ResponseCache
|
|
139
|
+
};
|
|
140
|
+
//# sourceMappingURL=response-cache.js.map
|
|
@@ -1,50 +1,46 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Responsive design detector — captures page at multiple breakpoints
|
|
3
|
-
* and detects layout differences between desktop and mobile viewports.
|
|
4
|
-
*
|
|
5
|
-
* Breakpoints: mobile(375), tablet(768), desktop(1280), wide(1920)
|
|
6
|
-
* Detects: viewport-specific content visibility, menu collapse state,
|
|
7
|
-
* font size changes, hidden/shown elements.
|
|
8
|
-
*/
|
|
9
1
|
const BREAKPOINTS = [
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
{ width: 375, height: 812, label: "mobile" },
|
|
3
|
+
{ width: 768, height: 1024, label: "tablet" },
|
|
4
|
+
{ width: 1280, height: 800, label: "desktop" }
|
|
13
5
|
];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
6
|
+
async function detectResponsive(page, url, opts = {}) {
|
|
7
|
+
const captures = [];
|
|
8
|
+
const issues = [];
|
|
9
|
+
for (const bp of BREAKPOINTS) {
|
|
10
|
+
try {
|
|
11
|
+
await page.setViewportSize({ width: bp.width, height: bp.height });
|
|
12
|
+
await page.waitForTimeout(500);
|
|
13
|
+
const data = await page.evaluate(() => ({
|
|
14
|
+
visibleTextLength: (document.body.innerText ?? "").length,
|
|
15
|
+
hasHamburgerMenu: !!document.querySelector('[class*="hamburger"],[class*="menu-toggle"],[class*="nav-toggle"],[aria-label*="menu"],[aria-label*="Menu"]'),
|
|
16
|
+
hiddenElements: Array.from(document.querySelectorAll("*")).filter(
|
|
17
|
+
(el) => window.getComputedStyle(el).display === "none"
|
|
18
|
+
).length,
|
|
19
|
+
scrollHeight: document.body.scrollHeight
|
|
20
|
+
})).catch(() => ({ visibleTextLength: 0, hasHamburgerMenu: false, hiddenElements: 0, scrollHeight: 0 }));
|
|
21
|
+
let screenshotBuffer;
|
|
22
|
+
if (opts.captureScreenshots) {
|
|
23
|
+
screenshotBuffer = await page.screenshot({ type: "jpeg", quality: 60 }).catch(() => void 0);
|
|
24
|
+
}
|
|
25
|
+
captures.push({ ...bp, ...data, screenshotBuffer });
|
|
26
|
+
} catch {
|
|
34
27
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return { url, captures, isResponsive, hasMobileMenu, issues };
|
|
28
|
+
}
|
|
29
|
+
await page.setViewportSize({ width: 1280, height: 800 }).catch(() => {
|
|
30
|
+
});
|
|
31
|
+
const mobile = captures.find((c) => c.label === "mobile");
|
|
32
|
+
const desktop = captures.find((c) => c.label === "desktop");
|
|
33
|
+
const isResponsive = !!(mobile && desktop && (mobile.hiddenElements !== desktop.hiddenElements || mobile.scrollHeight !== desktop.scrollHeight || mobile.hasHamburgerMenu));
|
|
34
|
+
const hasMobileMenu = captures.some((c) => c.label === "mobile" && c.hasHamburgerMenu);
|
|
35
|
+
if (mobile && desktop && mobile.visibleTextLength > desktop.visibleTextLength * 1.3) {
|
|
36
|
+
issues.push("Mobile shows significantly more text than desktop \u2014 possible responsive issue");
|
|
37
|
+
}
|
|
38
|
+
if (!isResponsive && captures.length > 1) {
|
|
39
|
+
issues.push("Layout appears identical across breakpoints \u2014 may not be responsive");
|
|
40
|
+
}
|
|
41
|
+
return { url, captures, isResponsive, hasMobileMenu, issues };
|
|
50
42
|
}
|
|
43
|
+
export {
|
|
44
|
+
detectResponsive
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=responsive-detector.js.map
|
package/dist/retry-policy.js
CHANGED
|
@@ -1,58 +1,54 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retry policy — configurable exponential backoff with jitter.
|
|
3
|
-
* Covers: 5xx server errors, network timeouts, ECONNREFUSED, ECONNRESET.
|
|
4
|
-
* NOT retried: 4xx client errors (except 429 — handled separately), redirects.
|
|
5
|
-
*/
|
|
6
1
|
const DEFAULT_RETRY_STATUSES = [429, 500, 502, 503, 504];
|
|
7
2
|
function shouldRetry(error, retryOn) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (error?.message && /timeout|net::ERR_/i.test(error.message))
|
|
13
|
-
return true;
|
|
14
|
-
// HTTP status errors
|
|
15
|
-
if (typeof error?.status === 'number')
|
|
16
|
-
return retryOn.includes(error.status);
|
|
17
|
-
return false;
|
|
3
|
+
if (error?.code && ["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND", "EPIPE"].includes(error.code)) return true;
|
|
4
|
+
if (error?.message && /timeout|net::ERR_/i.test(error.message)) return true;
|
|
5
|
+
if (typeof error?.status === "number") return retryOn.includes(error.status);
|
|
6
|
+
return false;
|
|
18
7
|
}
|
|
19
8
|
function delay(ms) {
|
|
20
|
-
|
|
9
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
21
10
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
11
|
+
async function withRetry(fn, opts = {}) {
|
|
12
|
+
const {
|
|
13
|
+
maxRetries = 3,
|
|
14
|
+
baseDelayMs = 1e3,
|
|
15
|
+
maxDelayMs = 3e4,
|
|
16
|
+
jitterMs = 500,
|
|
17
|
+
retryOn = DEFAULT_RETRY_STATUSES
|
|
18
|
+
} = opts;
|
|
19
|
+
let lastError;
|
|
20
|
+
let totalDelayMs = 0;
|
|
21
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
22
|
+
try {
|
|
23
|
+
const value = await fn(attempt);
|
|
24
|
+
return { value, attempts: attempt + 1, totalDelayMs };
|
|
25
|
+
} catch (err) {
|
|
26
|
+
lastError = err;
|
|
27
|
+
if (attempt === maxRetries) break;
|
|
28
|
+
if (!shouldRetry(err, retryOn)) throw err;
|
|
29
|
+
const backoff = Math.min(maxDelayMs, baseDelayMs * Math.pow(2, attempt));
|
|
30
|
+
const jitter = Math.random() * jitterMs;
|
|
31
|
+
const waitMs = Math.round(backoff + jitter);
|
|
32
|
+
totalDelayMs += waitMs;
|
|
33
|
+
await delay(waitMs);
|
|
43
34
|
}
|
|
44
|
-
|
|
35
|
+
}
|
|
36
|
+
throw lastError;
|
|
45
37
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return result.value;
|
|
38
|
+
async function fetchWithRetry(url, init = {}, retryOpts = {}) {
|
|
39
|
+
const result = await withRetry(async () => {
|
|
40
|
+
const res = await fetch(url, init);
|
|
41
|
+
if (retryOpts.retryOn?.includes(res.status) ?? DEFAULT_RETRY_STATUSES.includes(res.status)) {
|
|
42
|
+
const err = new Error(`HTTP ${res.status}`);
|
|
43
|
+
err.status = res.status;
|
|
44
|
+
throw err;
|
|
45
|
+
}
|
|
46
|
+
return res;
|
|
47
|
+
}, retryOpts);
|
|
48
|
+
return result.value;
|
|
58
49
|
}
|
|
50
|
+
export {
|
|
51
|
+
fetchWithRetry,
|
|
52
|
+
withRetry
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=retry-policy.js.map
|
package/dist/robots-txt.js
CHANGED
|
@@ -1,91 +1,77 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal robots.txt fetcher + parser. No external deps. Caches per origin.
|
|
3
|
-
* Implements the subset of robots.txt spec used in practice:
|
|
4
|
-
* User-agent, Disallow, Allow — longest match wins (RFC 9309).
|
|
5
|
-
*/
|
|
6
1
|
function parseRobots(text) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const colonIdx = line.indexOf(':');
|
|
16
|
-
if (colonIdx === -1)
|
|
17
|
-
continue;
|
|
18
|
-
const key = line.slice(0, colonIdx).trim().toLowerCase();
|
|
19
|
-
const value = line.slice(colonIdx + 1).trim();
|
|
20
|
-
if (key === 'user-agent') {
|
|
21
|
-
// Start new block only when current block already has rules, otherwise
|
|
22
|
-
// accumulate multiple User-agent lines into one block.
|
|
23
|
-
if (!current || current.disallow.length > 0 || current.allow.length > 0) {
|
|
24
|
-
current = { userAgent: value.toLowerCase(), disallow: [], allow: [] };
|
|
25
|
-
rules.push(current);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
current.userAgent = value.toLowerCase();
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
else if (key === 'disallow' && current && value) {
|
|
32
|
-
current.disallow.push(value);
|
|
33
|
-
}
|
|
34
|
-
else if (key === 'allow' && current && value) {
|
|
35
|
-
current.allow.push(value);
|
|
36
|
-
}
|
|
2
|
+
const rules = [];
|
|
3
|
+
let current = null;
|
|
4
|
+
for (const rawLine of text.split("\n")) {
|
|
5
|
+
const line = rawLine.split("#")[0].trim();
|
|
6
|
+
if (!line) {
|
|
7
|
+
current = null;
|
|
8
|
+
continue;
|
|
37
9
|
}
|
|
38
|
-
|
|
10
|
+
const colonIdx = line.indexOf(":");
|
|
11
|
+
if (colonIdx === -1) continue;
|
|
12
|
+
const key = line.slice(0, colonIdx).trim().toLowerCase();
|
|
13
|
+
const value = line.slice(colonIdx + 1).trim();
|
|
14
|
+
if (key === "user-agent") {
|
|
15
|
+
if (!current || current.disallow.length > 0 || current.allow.length > 0) {
|
|
16
|
+
current = { userAgent: value.toLowerCase(), disallow: [], allow: [] };
|
|
17
|
+
rules.push(current);
|
|
18
|
+
} else {
|
|
19
|
+
current.userAgent = value.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
} else if (key === "disallow" && current && value) {
|
|
22
|
+
current.disallow.push(value);
|
|
23
|
+
} else if (key === "allow" && current && value) {
|
|
24
|
+
current.allow.push(value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return rules;
|
|
39
28
|
}
|
|
40
29
|
function isAllowedByRules(rules, userAgent, pathname) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
for (const p of rule.disallow) {
|
|
55
|
-
if (pathname.startsWith(p))
|
|
56
|
-
matched.push({ path: p, allow: false });
|
|
57
|
-
}
|
|
30
|
+
const ua = userAgent.toLowerCase();
|
|
31
|
+
const exactRules = rules.filter((r) => r.userAgent === ua);
|
|
32
|
+
const wildcardRules = rules.filter((r) => r.userAgent === "*");
|
|
33
|
+
const applicable = exactRules.length > 0 ? exactRules : wildcardRules;
|
|
34
|
+
if (applicable.length === 0) return true;
|
|
35
|
+
const matched = [];
|
|
36
|
+
for (const rule of applicable) {
|
|
37
|
+
for (const p of rule.allow) {
|
|
38
|
+
if (pathname.startsWith(p)) matched.push({ path: p, allow: true });
|
|
39
|
+
}
|
|
40
|
+
for (const p of rule.disallow) {
|
|
41
|
+
if (pathname.startsWith(p)) matched.push({ path: p, allow: false });
|
|
58
42
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
}
|
|
44
|
+
if (matched.length === 0) return true;
|
|
45
|
+
matched.sort((a, b) => b.path.length - a.path.length);
|
|
46
|
+
return matched[0].allow;
|
|
63
47
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.cache.set(origin, []); // network error or timeout = no restrictions
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return isAllowedByRules(this.cache.get(origin), userAgent, pathname);
|
|
48
|
+
class RobotsCache {
|
|
49
|
+
cache = /* @__PURE__ */ new Map();
|
|
50
|
+
async isAllowed(url, userAgent = "ZeTa-Crawler") {
|
|
51
|
+
let origin;
|
|
52
|
+
let pathname;
|
|
53
|
+
try {
|
|
54
|
+
const u = new URL(url);
|
|
55
|
+
origin = u.origin;
|
|
56
|
+
pathname = u.pathname || "/";
|
|
57
|
+
} catch {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (!this.cache.has(origin)) {
|
|
61
|
+
try {
|
|
62
|
+
const res = await fetch(`${origin}/robots.txt`, {
|
|
63
|
+
headers: { "User-Agent": userAgent },
|
|
64
|
+
signal: AbortSignal.timeout(5e3)
|
|
65
|
+
});
|
|
66
|
+
this.cache.set(origin, res.ok ? parseRobots(await res.text()) : []);
|
|
67
|
+
} catch {
|
|
68
|
+
this.cache.set(origin, []);
|
|
69
|
+
}
|
|
90
70
|
}
|
|
71
|
+
return isAllowedByRules(this.cache.get(origin), userAgent, pathname);
|
|
72
|
+
}
|
|
91
73
|
}
|
|
74
|
+
export {
|
|
75
|
+
RobotsCache
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=robots-txt.js.map
|