@@ -42,6 +42,10 @@ function makeAssetCachePath(cacheDir, cacheKey) {
42
42
} ;
43
43
}
44
44
45
+ function makeAssetCacheName ( contentType , contentLength ) {
46
+ return Buffer . from ( `${ contentType } :${ contentLength } ` ) . toString ( "base64" ) ;
47
+ }
48
+
45
49
const middleWare = ( module . exports = function ( options ) {
46
50
return async function ( req , res , next ) {
47
51
options = options || { } ;
@@ -62,15 +66,20 @@ const middleWare = (module.exports = function(options) {
62
66
middleWare . makeDirIfNotExists ( path . join ( options . cacheDir , dir1 , dir2 ) ) ;
63
67
64
68
if ( fs . existsSync ( assetCachePath ) ) {
65
- const response = await fetch ( res . locals . fetchUrl , { method : "HEAD" } ) ;
66
-
67
- res . locals . contentLength = response . headers . get ( "content-length" ) ;
68
- res . locals . contentType = response . headers . get ( "content-type" ) ;
69
-
70
69
if ( options . logger )
71
70
options . logger . debug ( `Reading buffer from path ${ assetCachePath } ` ) ;
72
71
73
- res . locals . buffer = fs . readFileSync ( assetCachePath ) ;
72
+ const firstFile = fs . readdirSync ( assetCachePath ) [ 0 ] ;
73
+ const decodedFileName = Buffer . from ( "firstFile" , "base64" ) . toString (
74
+ "ascii"
75
+ ) ;
76
+
77
+ const [ contentType , contentLength ] = decodedFileName . split ( ":" ) ;
78
+
79
+ res . locals . contentLength = contentLength ;
80
+ res . locals . contentType = contentType ;
81
+
82
+ res . locals . buffer = fs . readFileSync ( `${ assetCachePath } /${ firstFile } ` ) ;
74
83
} else {
75
84
const blob = await ( await fetch ( res . locals . fetchUrl ) ) . blob ( ) ;
76
85
@@ -82,7 +91,13 @@ const middleWare = (module.exports = function(options) {
82
91
res . locals . contentType = blob . type ;
83
92
res . locals . contentLength = blob . size ;
84
93
85
- fs . writeFileSync ( assetCachePath , res . locals . buffer ) ;
94
+ fs . writeFileSync (
95
+ `${ assetCachePath } /${ middleWare . makeAssetCacheName (
96
+ blob . type ,
97
+ blob . size
98
+ ) } `,
99
+ res . locals . buffer
100
+ ) ;
86
101
}
87
102
88
103
next ( ) ;
@@ -105,3 +120,4 @@ const middleWare = (module.exports = function(options) {
105
120
106
121
middleWare . makeAssetCachePath = makeAssetCachePath ;
107
122
middleWare . makeDirIfNotExists = makeDirIfNotExists ;
123
+ middleWare . makeAssetCacheName = makeAssetCacheName ;
0 commit comments