(url: string, mimeType?: string, origin?: string)
| 1 | import {isFirefox} from './platform'; |
| 2 | |
| 3 | async function getOKResponse(url: string, mimeType?: string, origin?: string): Promise<Response> { |
| 4 | const credentials = origin && url.startsWith(`${origin}/`) ? undefined : 'omit'; |
| 5 | const response = await fetch( |
| 6 | url, |
| 7 | { |
| 8 | cache: 'force-cache', |
| 9 | credentials, |
| 10 | referrer: origin, |
| 11 | }, |
| 12 | ); |
| 13 | |
| 14 | // Firefox bug, content type is "application/x-unknown-content-type" |
| 15 | if (isFirefox && mimeType === 'text/css' && url.startsWith('moz-extension://') && url.endsWith('.css')) { |
| 16 | return response; |
| 17 | } |
| 18 | |
| 19 | if (mimeType && !(response.headers.get('Content-Type') === mimeType || response.headers.get('Content-Type')!.startsWith(`${mimeType};`))) { |
| 20 | throw new Error(`Mime type mismatch when loading ${url}`); |
| 21 | } |
| 22 | |
| 23 | if (!response.ok) { |
| 24 | throw new Error(`Unable to load ${url} ${response.status} ${response.statusText}`); |
| 25 | } |
| 26 | |
| 27 | return response; |
| 28 | } |
| 29 | |
| 30 | export async function loadAsDataURL(url: string, mimeType?: string): Promise<string> { |
| 31 | const response = await getOKResponse(url, mimeType); |
no test coverage detected