| 47 | }; |
| 48 | |
| 49 | export const updateFile = async (tag: string, file: string, env: Env) => { |
| 50 | // Try calling download worker |
| 51 | const req = new Request(`https://download.fontsource.org/${tag}/${file}`, { |
| 52 | method: 'POST', |
| 53 | headers: { |
| 54 | Authorization: `Bearer ${env.UPLOAD_KEY}`, |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | const resp = await fetch(req); |
| 59 | if (resp.status !== 201) { |
| 60 | if (resp.status === 404) { |
| 61 | throw new StatusError(resp.status, 'Not Found. File does not exist.'); |
| 62 | } |
| 63 | const error = await resp.text(); |
| 64 | |
| 65 | throw new StatusError( |
| 66 | resp.status, |
| 67 | `Bad response from download worker. ${error}`, |
| 68 | ); |
| 69 | } |
| 70 | // Check again if file exists in bucket |
| 71 | let retries = 0; |
| 72 | let font = await env.FONTS.get(`${tag}/${file}`); |
| 73 | while (!font && retries < 3) { |
| 74 | font = await env.FONTS.get(`${tag}/${file}`); |
| 75 | retries++; |
| 76 | // Exponential backoff |
| 77 | await new Promise((resolve) => setTimeout(resolve, retries * 300)); |
| 78 | } |
| 79 | |
| 80 | if (!font) { |
| 81 | throw new StatusError(500, 'Internal Server Error. Failed to update file.'); |
| 82 | } |
| 83 | |
| 84 | return font; |
| 85 | }; |
| 86 | |
| 87 | export const updateVariableFile = async ( |
| 88 | tag: string, |