···
mkdirSync(fileDir, { recursive: true });
299
+
// Determine if this is a web asset that should remain compressed
300
+
const webAssetTypes = [
301
+
'text/html', 'text/css', 'application/javascript', 'text/javascript',
302
+
'application/json', 'text/xml', 'application/xml'
305
+
const isWebAsset = mimeType && webAssetTypes.some(type =>
306
+
mimeType.toLowerCase().startsWith(type) || mimeType.toLowerCase() === type
309
+
// Decompress non-web assets that are gzipped
310
+
if (encoding === 'gzip' && !isWebAsset && content.length >= 2 &&
311
+
content[0] === 0x1f && content[1] === 0x8b) {
312
+
console.log(`[DEBUG] ${filePath}: decompressing non-web asset (${mimeType}) before caching`);
314
+
const { gunzipSync } = await import('zlib');
315
+
const decompressed = gunzipSync(content);
316
+
console.log(`[DEBUG] ${filePath}: decompressed from ${content.length} to ${decompressed.length} bytes`);
317
+
content = decompressed;
318
+
// Clear the encoding flag since we're storing decompressed
319
+
encoding = undefined;
321
+
console.log(`[DEBUG] ${filePath}: failed to decompress, storing original gzipped content`);
await writeFile(cacheFile, content);
301
-
// Store metadata if file is compressed
327
+
// Store metadata only if file is still compressed (web assets)
if (encoding === 'gzip' && mimeType) {
const metaFile = `${cacheFile}.meta`;
await writeFile(metaFile, JSON.stringify({ encoding, mimeType }));
console.log('Cached file', filePath, content.length, 'bytes (gzipped,', mimeType + ')');
307
-
console.log('Cached file', filePath, content.length, 'bytes');
333
+
console.log('Cached file', filePath, content.length, 'bytes (decompressed)');