@@ -199,7 +199,32 @@ class CoCreateFileSystem {
199199
200200 let contentType = file [ "content-type" ] || "text/html" ;
201201
202- // Add handling for font files
202+ try {
203+ if ( typeof src === "string" ) {
204+ // Decode the file content based on its MIME type
205+ if ( / ^ ( i m a g e | a u d i o | v i d e o | f o n t | a p p l i c a t i o n \/ o c t e t - s t r e a m | a p p l i c a t i o n \/ x - f o n t - t t f | a p p l i c a t i o n \/ x - f o n t - w o f f | a p p l i c a t i o n \/ x - f o n t - w o f f 2 | a p p l i c a t i o n \/ x - f o n t - o p e n t y p e | a p p l i c a t i o n \/ x - f o n t - t r u e t y p e | a p p l i c a t i o n \/ x - f o n t - e o t ) / . test ( contentType ) ) {
206+ src = Buffer . from ( src , "base64" ) ;
207+ } else if ( / ^ ( a p p l i c a t i o n \/ z i p | a p p l i c a t i o n \/ x - 7 z - c o m p r e s s e d | a p p l i c a t i o n \/ x - r a r - c o m p r e s s e d | a p p l i c a t i o n \/ p d f ) / . test ( contentType ) ) {
208+ src = Buffer . from ( src , "binary" ) ;
209+ } else {
210+ src = Buffer . from ( src , "utf8" ) ;
211+ }
212+ } else {
213+ throw new Error ( "File content is not in a valid format" ) ;
214+ }
215+ } catch ( err ) {
216+ console . error ( "Error decoding file content:" , {
217+ message : err . message ,
218+ contentType,
219+ srcType : typeof src
220+ } ) ;
221+ let pageNotFound = await getDefaultFile ( "/404.html" ) ;
222+ return sendResponse ( pageNotFound . object [ 0 ] . src , 404 , {
223+ "Content-Type" : "text/html"
224+ } ) ;
225+ }
226+
227+ // Remove redundant handling for `src.src` in font file processing
203228 if ( contentType . startsWith ( "font/" ) || / \. ( w o f f 2 ? | t t f | o t f ) $ / i. test ( pathname ) ) {
204229 try {
205230 if ( typeof src === "string" ) {
@@ -209,22 +234,14 @@ class CoCreateFileSystem {
209234 } else {
210235 throw new Error ( "Font data is not valid base64" ) ;
211236 }
212- } else if ( typeof src === "object" && src . src ) {
213- // Handle case where src is an object containing base64 data
214- if ( / ^ ( [ A - Z a - z 0 - 9 + / ] + = { 0 , 2 } ) $ / . test ( src . src ) ) {
215- src = Buffer . from ( src . src , "base64" ) ;
216- } else {
217- throw new Error ( "Font data inside object is not valid base64" ) ;
218- }
219237 } else {
220- throw new Error ( "Font data is not a valid base64 string or object " ) ;
238+ throw new Error ( "Font data is not a valid base64 string" ) ;
221239 }
222240 } catch ( err ) {
223241 console . error ( "Error processing font file:" , {
224242 message : err . message ,
225243 contentType,
226- srcType : typeof src ,
227- srcPreview : typeof src === "string" ? src . slice ( 0 , 50 ) : null
244+ srcType : typeof src
228245 } ) ;
229246 let pageNotFound = await getDefaultFile ( "/404.html" ) ;
230247 return sendResponse ( pageNotFound . object [ 0 ] . src , 404 , {
@@ -260,6 +277,9 @@ class CoCreateFileSystem {
260277 ) {
261278 const protocol = "https://" ; // || req.headers['x-forwarded-proto'] || req.protocol;
262279 src = src . replaceAll ( "{{$host}}" , `${ protocol } ${ hostname } ` ) ;
280+ } else {
281+ // Log unknown file types
282+ console . warn ( `Unknown content type: ${ contentType } ` ) ;
263283 }
264284
265285 sendResponse ( src , 200 , { "Content-Type" : contentType } ) ;
0 commit comments