(
url: string,
options: ZipUrlValidationOptions = {}
)
| 26 | * Validate an archive URL by checking its size with a HEAD request. |
| 27 | */ |
| 28 | export async function validateZipUrl( |
| 29 | url: string, |
| 30 | options: ZipUrlValidationOptions = {} |
| 31 | ): Promise<ZipValidationResult> { |
| 32 | const fetchImpl = options.fetchImpl ?? fetch; |
| 33 | const sizeLimit = options.sizeLimit ?? ARCHIVE_SIZE_LIMIT; |
| 34 | const timeoutMs = options.timeoutMs ?? SIZE_CHECK_TIMEOUT; |
| 35 | const controller = new AbortController(); |
| 36 | const timeoutId = setTimeout(() => controller.abort(), timeoutMs); |
| 37 | |
| 38 | try { |
| 39 | const response = await fetchImpl(url, { |
| 40 | method: 'HEAD', |
| 41 | signal: controller.signal, |
| 42 | }); |
| 43 | |
| 44 | clearTimeout(timeoutId); |
| 45 | |
| 46 | if (!response.ok) { |
| 47 | return { |
| 48 | valid: false, |
| 49 | error: `Failed to access archive (${response.status})`, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | const contentLength = response.headers.get('content-length'); |
| 54 | if (!contentLength) { |
| 55 | return { valid: true }; |
| 56 | } |
| 57 | |
| 58 | const size = parseSafeIntegerString(contentLength); |
| 59 | if (size === null) { |
| 60 | return { valid: true }; |
| 61 | } |
| 62 | |
| 63 | return validateArchiveSize(size, sizeLimit); |
| 64 | } catch (err) { |
| 65 | clearTimeout(timeoutId); |
| 66 | |
| 67 | if (err instanceof Error && err.name === 'AbortError') { |
| 68 | return { valid: false, error: 'Size check timed out' }; |
| 69 | } |
| 70 | |
| 71 | // On HEAD failure (CORS, etc.), allow the download attempt. |
| 72 | return { valid: true }; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Validate a local archive file by checking its size. |
no test coverage detected