MCPcopy Index your code
hub / github.com/fontsource/fontsource / updateFile

Function updateFile

api/cdn/src/update.ts:49–85  ·  view source on GitHub ↗
(tag: string, file: string, env: Env)

Source from the content-addressed store, hash-verified

47};
48
49export 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
87export const updateVariableFile = async (
88 tag: string,

Callers 1

router.tsFile · 0.90

Calls 3

fetchFunction · 0.70
textMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected