( maskUrlBase: string, imageName: string )
| 173 | * @returns The fetched File or null if fetch failed |
| 174 | */ |
| 175 | export async function fetchUrlMask( |
| 176 | maskUrlBase: string, |
| 177 | imageName: string |
| 178 | ): Promise<File | null> { |
| 179 | const cached = urlMaskState.getCached(imageName); |
| 180 | if (cached) { |
| 181 | return cached; |
| 182 | } |
| 183 | |
| 184 | // The source has shipped no masks (enough consecutive misses, never a hit): |
| 185 | // stop probing instead of firing two more doomed 404s for every image. |
| 186 | if (!urlMaskFoundAny && urlMaskConsecutiveMisses >= URL_MASK_ABSENCE_THRESHOLD) { |
| 187 | return null; |
| 188 | } |
| 189 | |
| 190 | if (urlMaskState.isRequestPending(imageName)) { |
| 191 | return urlMaskState.waitForRequest(imageName); |
| 192 | } |
| 193 | |
| 194 | urlMaskState.startRequest(imageName); |
| 195 | let result: File | null = null; |
| 196 | |
| 197 | try { |
| 198 | for (const { url: maskUrl, filename } of buildMaskUrlCandidates(maskUrlBase, imageName)) { |
| 199 | try { |
| 200 | const response = await fetch(maskUrl); |
| 201 | if (response.ok) { |
| 202 | const blob = await response.blob(); |
| 203 | const file = new File([blob], filename, { type: blob.type || 'image/png' }); |
| 204 | urlMaskState.setCached(imageName, file); |
| 205 | result = file; |
| 206 | appLogger.debug(`[URL Mask] Found mask for ${imageName}`); |
| 207 | return file; |
| 208 | } |
| 209 | } catch (err) { |
| 210 | appLogger.debug(`[URL Mask] Error trying ${maskUrl}:`, err); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | appLogger.debug(`[URL Mask] No mask found for ${imageName}`); |
| 215 | return null; |
| 216 | } finally { |
| 217 | urlMaskState.completeRequest(imageName, result); |
| 218 | if (result) { |
| 219 | urlMaskFoundAny = true; |
| 220 | urlMaskConsecutiveMisses = 0; |
| 221 | } else { |
| 222 | urlMaskConsecutiveMisses += 1; |
| 223 | if (!urlMaskFoundAny && urlMaskConsecutiveMisses === URL_MASK_ABSENCE_THRESHOLD) { |
| 224 | appLogger.debug( |
| 225 | `[URL Mask] No masks found after ${URL_MASK_ABSENCE_THRESHOLD} images; skipping further mask probes for this source.` |
| 226 | ); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
no test coverage detected