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

Function downloadFile

api/download/src/download.ts:23–64  ·  view source on GitHub ↗
(manifest: Manifest)

Source from the content-addressed store, hash-verified

21import type { IDResponse } from './types';
22
23export const downloadFile = async (manifest: Manifest) => {
24 const { id, subset, weight, style, extension, version, url } = manifest;
25
26 const res = await fetch(url);
27 info(`Downloading ${url}`);
28
29 if (!res.ok) {
30 throw new StatusError(500, `Could not fetch ${url}`);
31 }
32
33 const buffer = await res.arrayBuffer();
34
35 // Add to bucket
36 await putBucket(bucketPath(manifest), buffer);
37
38 // If woff, decompress and add to ttf folder
39 if (extension === 'woff') {
40 let ttfBuffer: ArrayBuffer | undefined;
41 try {
42 ttfBuffer = await woff2ttf.toSfnt(new Uint8Array(buffer));
43 } catch (error) {
44 throw new StatusError(
45 500,
46 `Could not convert woff to ttf ${String(error)}`,
47 );
48 }
49 if (!ttfBuffer) throw new StatusError(500, 'Could not convert woff to ttf');
50
51 // Add to bucket
52 await putBucket(
53 bucketPath({
54 id,
55 subset,
56 weight,
57 style,
58 extension: 'ttf',
59 version,
60 }),
61 ttfBuffer,
62 );
63 }
64};
65
66export const downloadVariableFile = async (manifest: ManifestVariable) => {
67 const { url } = manifest;

Callers 3

router.tsFile · 0.90
downloadManifestFunction · 0.85
useFontConverterFunction · 0.85

Calls 4

putBucketFunction · 0.90
bucketPathFunction · 0.90
arrayBufferMethod · 0.80
fetchFunction · 0.50

Tested by

no test coverage detected