( url: string, env: Env, )
| 171 | * @returns File content or null if not allowed or not found |
| 172 | */ |
| 173 | export async function fetchFileWithRobotsTxtCheck( |
| 174 | url: string, |
| 175 | env: Env, |
| 176 | ): Promise<{ content: string | null; blockedByRobots: boolean }> { |
| 177 | try { |
| 178 | const urlObj = new URL(url); |
| 179 | // Create path from URL path + filename |
| 180 | const path = urlObj.pathname; |
| 181 | |
| 182 | // Check robots.txt before attempting to fetch |
| 183 | const isAllowed = await checkRobotsTxt(urlObj.hostname, path, env); |
| 184 | |
| 185 | if (!isAllowed) { |
| 186 | console.log(`Access to ${url} disallowed by robots.txt`); |
| 187 | return { content: null, blockedByRobots: true }; |
| 188 | } |
| 189 | |
| 190 | // If allowed, use cached content or fetch |
| 191 | const content = await fetchUrlContent({ |
| 192 | url, |
| 193 | format: "text", |
| 194 | }); |
| 195 | |
| 196 | return { |
| 197 | content: content, |
| 198 | blockedByRobots: false, |
| 199 | }; |
| 200 | } catch (error) { |
| 201 | console.warn(`Error fetching ${url}: ${error}`); |
| 202 | return { content: null, blockedByRobots: false }; |
| 203 | } |
| 204 | } |
no test coverage detected