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

Function validateCSSFilename

api/cdn/src/validate.ts:81–135  ·  view source on GitHub ↗
(file: string, metadata: IDResponse)

Source from the content-addressed store, hash-verified

79};
80
81export const validateCSSFilename = (file: string, metadata: IDResponse) => {
82 const [filename, extension] = file.split('.');
83 if (!extension || extension !== 'css') {
84 throw new StatusError(400, 'Bad Request. Invalid file extension.');
85 }
86
87 // Accept index.css
88 if (filename === 'index') {
89 return;
90 }
91
92 const { weights, styles, subsets } = metadata;
93
94 // Accept weight.css
95 if (weights.includes(Number(filename))) {
96 return;
97 }
98
99 // Accept weight-italic.css
100 let weight: string | undefined;
101 let style: string | undefined;
102 let subset: string | undefined;
103 [weight, style] = filename.split('-');
104 if (
105 weights.includes(Number(weight)) &&
106 style === 'italic' &&
107 styles.includes(style)
108 ) {
109 return;
110 }
111
112 // Accept subset-weight.css
113 const subsetWeight = filename.split('-');
114 weight = subsetWeight.pop();
115 subset = subsetWeight.join('-');
116 if (subsets.includes(subset) && weights.includes(Number(weight))) {
117 return;
118 }
119
120 // Accept subset-weight-style.css
121 const subsetWeightStyle = filename.split('-');
122 style = subsetWeightStyle.pop();
123 weight = subsetWeightStyle.pop();
124 subset = subsetWeightStyle.join('-');
125 if (
126 style &&
127 subsets.includes(subset) &&
128 weights.includes(Number(weight)) &&
129 styles.includes(style)
130 ) {
131 return;
132 }
133
134 throw new StatusError(404, 'Not Found. Invalid filename.');
135};
136
137export const validateVCSSFilename = (
138 file: string,

Callers 1

router.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected