| 85 | }; |
| 86 | |
| 87 | export const updateVariableFile = async ( |
| 88 | tag: string, |
| 89 | file: string, |
| 90 | env: Env, |
| 91 | ) => { |
| 92 | // Try calling download worker |
| 93 | const req = new Request(`https://download.fontsource.org/v/${tag}/${file}`, { |
| 94 | method: 'POST', |
| 95 | headers: { |
| 96 | Authorization: `Bearer ${env.UPLOAD_KEY}`, |
| 97 | }, |
| 98 | }); |
| 99 | |
| 100 | const resp = await fetch(req); |
| 101 | if (resp.status !== 201) { |
| 102 | if (resp.status === 404) { |
| 103 | throw new StatusError(resp.status, 'Not Found. File does not exist.'); |
| 104 | } |
| 105 | const error = await resp.text(); |
| 106 | |
| 107 | throw new StatusError( |
| 108 | resp.status, |
| 109 | `Bad response from download worker. ${error}`, |
| 110 | ); |
| 111 | } |
| 112 | // Check again if file exists in bucket |
| 113 | let retries = 0; |
| 114 | let font = await env.FONTS.get(`${tag}/variable/${file}`); |
| 115 | while (!font && retries < 3) { |
| 116 | font = await env.FONTS.get(`${tag}/variable/${file}`); |
| 117 | retries++; |
| 118 | // Exponential backoff |
| 119 | await new Promise((resolve) => setTimeout(resolve, retries * 300)); |
| 120 | } |
| 121 | |
| 122 | if (!font) { |
| 123 | throw new StatusError(500, 'Internal Server Error. Failed to update file.'); |
| 124 | } |
| 125 | |
| 126 | return font; |
| 127 | }; |