@@ -13,96 +13,96 @@ const CACHE_CONTROL = 'cache-control'
1313const CACHE_IF_NONE_MATCH = 'if-none-match'
1414
1515const middleware = ( opts ) => async ( req , res , next ) => {
16- opts = Object . assign ( {
17- stores : [ CacheManager . caching ( { store : 'memory' , max : 1000 , ttl : 30 } ) ]
18- } , opts )
16+ try {
17+ opts = Object . assign ( {
18+ stores : [ CacheManager . caching ( { store : 'memory' , max : 1000 , ttl : 30 } ) ]
19+ } , opts )
1920
20- // creating multi-cache instance
21- const mcache = CacheManager . multiCaching ( opts . stores )
21+ // creating multi-cache instance
22+ const mcache = CacheManager . multiCaching ( opts . stores )
2223
23- if ( req . cacheDisabled ) return next ( )
24+ if ( req . cacheDisabled ) return next ( )
2425
25- let { url, cacheAppendKey = req => '' } = req
26- cacheAppendKey = await cacheAppendKey ( req )
26+ let { url, cacheAppendKey = req => '' } = req
27+ cacheAppendKey = await cacheAppendKey ( req )
2728
28- const key = req . method + url + cacheAppendKey
29- // ref cache key on req object
30- req . cacheKey = key
29+ const key = req . method + url + cacheAppendKey
30+ // ref cache key on req object
31+ req . cacheKey = key
3132
32- // try to retrieve cached response
33- const cached = await get ( mcache , key )
33+ // try to retrieve cached response
34+ const cached = await get ( mcache , key )
3435
35- if ( cached ) {
36- // respond from cache if there is a hit
37- let { status, headers, data } = JSON . parse ( cached )
36+ if ( cached ) {
37+ // respond from cache if there is a hit
38+ let { status, headers, data } = JSON . parse ( cached )
3839
39- // pre-checking If-None-Match header
40- if ( req . headers [ CACHE_IF_NONE_MATCH ] && req . headers [ CACHE_IF_NONE_MATCH ] === headers [ CACHE_ETAG ] ) {
41- res . setHeader ( 'content-length' , '0' )
42- res . statusCode = 304
43- res . end ( )
40+ // pre-checking If-None-Match header
41+ if ( req . headers [ CACHE_IF_NONE_MATCH ] && req . headers [ CACHE_IF_NONE_MATCH ] === headers [ CACHE_ETAG ] ) {
42+ res . setHeader ( 'content-length' , '0' )
43+ res . statusCode = 304
44+ res . end ( )
4445
45- return // exit because client cache state matches
46- }
46+ return // exit because client cache state matches
47+ }
4748
48- if ( typeof data === 'object' && data . type === 'Buffer' ) {
49- data = Buffer . from ( data . data )
50- }
51- headers [ X_CACHE_HIT ] = '1'
49+ if ( typeof data === 'object' && data . type === 'Buffer' ) {
50+ data = Buffer . from ( data . data )
51+ }
52+ headers [ X_CACHE_HIT ] = '1'
5253
53- // set cached response headers
54- Object . keys ( headers ) . forEach ( header => res . setHeader ( header , headers [ header ] ) )
54+ // set cached response headers
55+ Object . keys ( headers ) . forEach ( header => res . setHeader ( header , headers [ header ] ) )
5556
56- // send cached payload
57- req . cacheHit = true
58- res . statusCode = status
59- res . end ( data )
57+ // send cached payload
58+ req . cacheHit = true
59+ res . statusCode = status
60+ res . end ( data )
6061
61- return
62- }
62+ return
63+ }
6364
64- onEnd ( res , ( payload ) => {
65- if ( payload . headers [ X_CACHE_EXPIRE ] ) {
66- // support service level expiration
67- const keysPattern = payload . headers [ X_CACHE_EXPIRE ] . replace ( / \s / g, '' )
68- const patterns = keysPattern . split ( ',' )
69- // delete keys on all cache tiers
70- patterns . forEach ( pattern => opts . stores . forEach ( store => getKeys ( store , pattern ) . then ( keys => mcache . del ( keys ) ) ) )
71- } else if ( payload . headers [ X_CACHE_TIMEOUT ] || payload . headers [ CACHE_CONTROL ] ) {
72- // extract cache ttl
73- let ttl = 0
74- if ( payload . headers [ CACHE_CONTROL ] ) {
75- ttl = cacheControl ( payload . headers [ CACHE_CONTROL ] ) . maxAge
76- }
77- if ( ! ttl ) {
78- if ( payload . headers [ X_CACHE_TIMEOUT ] ) {
79- ttl = Math . max ( ms ( payload . headers [ X_CACHE_TIMEOUT ] ) , 1000 ) / 1000 // min value: 1 second
80- } else {
81- return // no TTL found, we don't cache
65+ onEnd ( res , ( payload ) => {
66+ if ( payload . headers [ X_CACHE_EXPIRE ] ) {
67+ // support service level expiration
68+ const keysPattern = payload . headers [ X_CACHE_EXPIRE ] . replace ( / \s / g, '' )
69+ const patterns = keysPattern . split ( ',' )
70+ // delete keys on all cache tiers
71+ patterns . forEach ( pattern => opts . stores . forEach ( store => getKeys ( store , pattern ) . then ( keys => mcache . del ( keys ) ) ) )
72+ } else if ( payload . headers [ X_CACHE_TIMEOUT ] || payload . headers [ CACHE_CONTROL ] ) {
73+ // extract cache ttl
74+ let ttl = 0
75+ if ( payload . headers [ CACHE_CONTROL ] ) {
76+ ttl = cacheControl ( payload . headers [ CACHE_CONTROL ] ) . maxAge
77+ }
78+ if ( ! ttl ) {
79+ if ( payload . headers [ X_CACHE_TIMEOUT ] ) {
80+ ttl = Math . max ( ms ( payload . headers [ X_CACHE_TIMEOUT ] ) , 1000 ) / 1000 // min value: 1 second
81+ } else {
82+ return // no TTL found, we don't cache
83+ }
8284 }
83- }
8485
85- // setting cache-control header if absent
86- if ( ! payload . headers [ CACHE_CONTROL ] ) {
87- payload . headers [ CACHE_CONTROL ] = `private, no-cache, max-age=${ ttl } `
88- }
89- // setting ETag if absent
90- if ( ! payload . headers [ CACHE_ETAG ] ) {
91- payload . headers [ CACHE_ETAG ] = Math . random ( ) . toString ( 36 ) . substring ( 2 , 16 )
92- }
86+ // setting cache-control header if absent
87+ if ( ! payload . headers [ CACHE_CONTROL ] ) {
88+ payload . headers [ CACHE_CONTROL ] = `private, no-cache, max-age=${ ttl } `
89+ }
90+ // setting ETag if absent
91+ if ( ! payload . headers [ CACHE_ETAG ] ) {
92+ payload . headers [ CACHE_ETAG ] = Math . random ( ) . toString ( 36 ) . substring ( 2 , 16 )
93+ }
9394
94- // cache response
95- mcache . set ( req . cacheKey , JSON . stringify ( payload ) , { ttl } )
96- }
97- } )
95+ // cache response
96+ mcache . set ( req . cacheKey , JSON . stringify ( payload ) , { ttl } )
97+ }
98+ } )
9899
99- return next ( )
100+ return next ( )
101+ } catch ( err ) {
102+ return next ( err )
103+ }
100104}
101105
102- const get = ( cache , key ) => new Promise ( ( resolve ) => {
103- cache . getAndPassUp ( key , ( _ , res ) => {
104- resolve ( res )
105- } )
106- } )
106+ const get = ( cache , key ) => cache . getAndPassUp ( key )
107107
108108module . exports = iu ( middleware )
0 commit comments