Skip to content

Commit 659ecce

Browse files
committed
fix(connectors): keep Crawl-delay on page retries and never read a crawler-named valued directive as its value
1 parent fc1ae50 commit 659ecce

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎apps/sim/connectors/website/page.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ describe('X-Robots-Tag', () => {
5353
expect(robotsDirectives('none')).toEqual({ noindex: true, nofollow: true })
5454
expect(robotsDirectives('otherbot: none').nofollow).toBe(false)
5555
expect(robotsDirectives('simbot: noindex nofollow')).toEqual({ noindex: true, nofollow: true })
56+
expect(robotsDirectives('simbot: max-image-preview: none')).toEqual({
57+
noindex: false,
58+
nofollow: false,
59+
})
5660
})
5761
})
5862

‎apps/sim/connectors/website/page.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ export function robotsDirectives(value: string | null | undefined): RobotsDirect
156156
const named = /^([\w-]+)\s*:\s*(.*)$/.exec(directive)
157157
// Some pages separate directives with spaces (`noindex nofollow`) rather than commas.
158158
if (!named) return directive.split(/\s+/)
159-
if (VALUED_DIRECTIVES.has(named[1])) return []
160-
return named[1] === token ? named[2].split(/\s+/) : []
159+
if (VALUED_DIRECTIVES.has(named[1]) || named[1] !== token) return []
160+
// After this crawler's name, a valued directive (`SimBot: max-image-preview: none`) is still
161+
// never read as its value.
162+
const valued = /^([\w-]+)\s*:/.exec(named[2])
163+
return valued && VALUED_DIRECTIVES.has(valued[1]) ? [] : named[2].split(/\s+/)
161164
})
162165
return {
163166
noindex: directives.some((directive) => directive === 'noindex' || directive === 'none'),

‎apps/sim/connectors/website/website.integration.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let undeclared: 'html' | 'text' | undefined
3232
let crawlDelay = ''
3333
let lockedLink = false
3434
let extraLinks = ''
35+
let flakyFailures = 0
3536
const pageRequests: { path: string; at: number }[] = []
3637
let faqBody = '<p>Original answer.</p>'
3738

@@ -216,6 +217,9 @@ const siteServer = createServer((request, response) => {
216217
)
217218
case '/chain/next/next':
218219
return send(200, 'text/html', page('End', '<p>Published</p>'))
220+
case '/flaky':
221+
if (flakyFailures-- > 0) return send(503, 'text/plain', 'busy')
222+
return send(200, 'text/html', page('Flaky', '<p>Back</p>'))
219223
case '/stall':
220224
hung.push(response)
221225
return
@@ -305,6 +309,7 @@ beforeEach(() => {
305309
crawlDelay = ''
306310
lockedLink = false
307311
extraLinks = ''
312+
flakyFailures = 0
308313
pageRequests.length = 0
309314
})
310315

@@ -782,4 +787,18 @@ describe('website connector over real HTTP', () => {
782787
}
783788
expect(Date.now() - started).toBeLessThan(5_000)
784789
})
790+
791+
it('waits the Crawl-delay before retrying a page request', async () => {
792+
crawlDelay = '1.5'
793+
flakyFailures = 1
794+
const document = await websiteConnector.getDocument(
795+
`X-Api-Key: ${KEY}`,
796+
config(),
797+
`${site}/flaky`,
798+
{}
799+
)
800+
expect(document?.content).toContain('Back')
801+
const [first, second] = pageRequests.map((request) => request.at)
802+
expect((second ?? 0) - (first ?? 0)).toBeGreaterThanOrEqual(1_450)
803+
})
785804
})

‎apps/sim/connectors/website/website.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const CRAWL_DEADLINE_MS = 120_000
5959
interface WebsiteSession {
6060
scope: WebsiteScope
6161
headers: Record<string, string>
62+
/** The Crawl-delay robots.txt asks for, which every page request, retries included, keeps. */
63+
crawlDelayMs: number
6264
pace: (signal?: AbortSignal) => Promise<void>
6365
}
6466

@@ -151,6 +153,7 @@ async function openSession(
151153
const policy = robots === null ? ALLOW_ALL_ROBOTS : parseRobots(robots)
152154
const session: WebsiteSession = {
153155
headers,
156+
crawlDelayMs: policy.crawlDelayMs,
154157
pace: pacer(policy.crawlDelayMs),
155158
scope: {
156159
site,
@@ -274,7 +277,12 @@ async function readPage(
274277
site: scope.site,
275278
headers,
276279
accept: 'text/html, application/xhtml+xml;q=0.9',
277-
retryOptions,
280+
retryOptions: session.crawlDelayMs
281+
? {
282+
...retryOptions,
283+
initialDelayMs: Math.max(retryOptions?.initialDelayMs ?? 1000, session.crawlDelayMs),
284+
}
285+
: retryOptions,
278286
},
279287
PAGE_MAX_BYTES,
280288
(target) => {

0 commit comments

Comments
 (0)