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

Function validateVariableFontFileName

api/cdn/src/validate.ts:41–79  ·  view source on GitHub ↗
(
	file: string,
	metadata: IDResponse,
	variableMeta?: VariableMetadata,
)

Source from the content-addressed store, hash-verified

39};
40
41export const validateVariableFontFileName = (
42 file: string,
43 metadata: IDResponse,
44 variableMeta?: VariableMetadata,
45) => {
46 if (!variableMeta) {
47 throw new StatusError(400, 'Bad Request. Variable font not found.');
48 }
49
50 const [filename, extension] = file.split('.');
51 if (!extension || extension !== 'woff2') {
52 throw new StatusError(400, 'Bad Request. Invalid file extension.');
53 }
54
55 const { subsets, styles } = metadata;
56 const { axes } = variableMeta;
57
58 const filenameArr = filename.split('-');
59 const style = filenameArr.pop();
60 const axesKey = filenameArr.pop();
61 const subset = filenameArr.join('-');
62
63 const isValidAxesKey =
64 Boolean(axesKey && axes[axesKey]) ||
65 axesKey === 'standard' ||
66 axesKey === 'full';
67
68 // Accept id-subset-axes-style
69 if (
70 (subsets.includes(subset) || isNumeric(subset)) &&
71 isValidAxesKey &&
72 style &&
73 styles.includes(style)
74 ) {
75 return;
76 }
77
78 throw new StatusError(404, 'Not Found. Invalid filename.');
79};
80
81export const validateCSSFilename = (file: string, metadata: IDResponse) => {
82 const [filename, extension] = file.split('.');

Callers 1

router.tsFile · 0.90

Calls 1

isNumericFunction · 0.85

Tested by

no test coverage detected