(url: string, init?: RequestInit)
| 344 | * or on network error. |
| 345 | */ |
| 346 | export async function safeFetch(url: string, init?: RequestInit): Promise<Response | null> { |
| 347 | let current = url; |
| 348 | for (let hop = 0; hop <= MAX_FETCH_REDIRECTS; hop++) { |
| 349 | if (isPrivateUrl(current)) return null; |
| 350 | const res = await fetch(current, { ...init, redirect: "manual" }); |
| 351 | if (res.status >= 300 && res.status < 400) { |
| 352 | const loc = res.headers.get("location"); |
| 353 | if (!loc) return res; |
| 354 | try { |
| 355 | current = new URL(loc, current).toString(); |
| 356 | } catch { |
| 357 | return null; // malformed Location header |
| 358 | } |
| 359 | continue; |
| 360 | } |
| 361 | return res; |
| 362 | } |
| 363 | return null; // too many redirects |
| 364 | } |
| 365 | |
| 366 | async function fetchBuffer(url: string): Promise<Buffer | null> { |
| 367 | try { |
no test coverage detected