MCPcopy Create free account
hub / github.com/fontsource/fontsource / getDownloadAsset

Function getDownloadAsset

api/worker/src/features/cdn/handler.ts:251–333  ·  view source on GitHub ↗
(
	c: Context<AppEnv>,
	id: string,
)

Source from the content-addressed store, hash-verified

249};
250
251export const getDownloadAsset = async (
252 c: Context<AppEnv>,
253 id: string,
254): Promise<Response> => {
255 const metadata = await getFontById(c, id);
256 if (!metadata) {
257 throw notFound('Not Found. Font does not exist.');
258 }
259
260 const axes = metadata.variable || undefined;
261 const [staticVersions, variableVersions] = await Promise.all([
262 getPublishedVersions(id, false),
263 axes ? getPublishedVersions(id, true) : undefined,
264 ]);
265 const staticVersion =
266 staticVersions.length > 0
267 ? resolveVersionTag('latest', staticVersions)
268 : undefined;
269 const variableVersion = variableVersions?.length
270 ? resolveVersionTag('latest', variableVersions)
271 : undefined;
272 const versions = staticVersion
273 ? { staticVersion, variableVersion }
274 : variableVersion
275 ? { variableVersion }
276 : undefined;
277 if (!versions) {
278 throw notFound(
279 'Unable to resolve download: no published versions available',
280 );
281 }
282 const downloadKey = getDownloadKey(
283 id,
284 versions.staticVersion,
285 versions.variableVersion,
286 );
287 const readDownload = () =>
288 getStoredBinaryObject(c, downloadKey, c.req.raw.headers);
289 const respond = (asset: StoredBinaryAsset) =>
290 respondWithBinary(asset, {
291 cachePolicy: {
292 ...CACHE_POLICIES.floating,
293 'Cache-Control': 'public, no-cache',
294 },
295 contentType: 'zip',
296 filename: `${id}.zip`,
297 });
298
299 const existing = await readDownload();
300 if (existing) {
301 return respond(existing);
302 }
303
304 const build = await startDownloadBuild(c, {
305 mode: 'download',
306 ...versions,
307 metadata,
308 });

Callers 1

handleMethod · 0.90

Calls 10

getFontByIdFunction · 0.90
notFoundFunction · 0.90
getPublishedVersionsFunction · 0.90
resolveVersionTagFunction · 0.90
getDownloadKeyFunction · 0.90
startDownloadBuildFunction · 0.90
badGatewayFunction · 0.90
readDownloadFunction · 0.85
respondFunction · 0.85
jsonMethod · 0.65

Tested by

no test coverage detected