(context: Context, options: ContextFetchOptions)
| 40 | } |
| 41 | |
| 42 | export function contextFetch(context: Context, options: ContextFetchOptions): Promise<string> { |
| 43 | const { url: rawUrl, requestType = 'text', responseType = 'text', imageDom } = options |
| 44 | let url = rawUrl |
| 45 | |
| 46 | const { |
| 47 | timeout, |
| 48 | acceptOfImage, |
| 49 | requests, |
| 50 | fetchFn, |
| 51 | fetch: { |
| 52 | requestInit, |
| 53 | bypassingCache, |
| 54 | placeholderImage, |
| 55 | }, |
| 56 | font, |
| 57 | workers, |
| 58 | fontFamilies, |
| 59 | } = context |
| 60 | |
| 61 | if (requestType === 'image' && (IN_SAFARI || IN_FIREFOX)) { |
| 62 | context.drawImageCount++ |
| 63 | } |
| 64 | |
| 65 | let request = requests.get(rawUrl) |
| 66 | if (!request) { |
| 67 | // cache bypass so we dont have CORS issues with cached images |
| 68 | // ref: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Bypassing_the_cache |
| 69 | if (bypassingCache) { |
| 70 | if (bypassingCache instanceof RegExp && bypassingCache.test(url)) { |
| 71 | url += (/\?/.test(url) ? '&' : '?') + new Date().getTime() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Font minify |
| 76 | const canFontMinify = requestType.startsWith('font') && font && font.minify |
| 77 | const fontTexts = new Set() |
| 78 | if (canFontMinify) { |
| 79 | const families = requestType.split(';')[1].split(',') |
| 80 | families.forEach((family) => { |
| 81 | if (!fontFamilies.has(family)) |
| 82 | return |
| 83 | fontFamilies.get(family)!.forEach(text => fontTexts.add(text)) |
| 84 | }) |
| 85 | } |
| 86 | const needFontMinify = canFontMinify && fontTexts.size |
| 87 | |
| 88 | const baseFetchOptions: BaseFetchOptions = { |
| 89 | url, |
| 90 | timeout, |
| 91 | responseType: needFontMinify ? 'arrayBuffer' : responseType, |
| 92 | headers: requestType === 'image' ? { accept: acceptOfImage } : undefined, |
| 93 | ...requestInit, |
| 94 | } |
| 95 | |
| 96 | request = { |
| 97 | type: requestType, |
| 98 | resolve: undefined, |
| 99 | reject: undefined, |
no test coverage detected
searching dependent graphs…