@@ -64,100 +64,68 @@ class MemoryCacheStore {
6464    } 
6565  } 
6666
67-   get  isFull  ( )  { 
68-     return  this . #arr. length  >=  this . #maxCount ||  this . #size >=  this . #maxSize
69-   } 
70- 
7167  /** 
7268   * @param  {import('../../types/cache-interceptor.d.ts').default.CacheKey } req 
7369   * @returns  {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined } 
7470   */ 
75-   get  ( {  origin,  path,  method,  headers } )  { 
76-     const  now  =  Date . now ( ) 
71+   get  ( key )  { 
72+     if  ( typeof  key  !==  'object' )  { 
73+       throw  new  TypeError ( `expected key to be object, got ${ typeof  key }  ) 
74+     } 
7775
78-     const  value  =  this . #arr. find ( ( value )  =>  ( 
79-       value . method  ===  method  && 
80-       value . origin  ===  origin  && 
81-       value . path  ===  path  && 
82-       value . deleteAt  >  now  && 
83-       ( ! value . vary  ||  Object . keys ( value . vary ) . every ( headerName  =>  value . vary [ headerName ]  ===  headers ?. [ headerName ] ) ) 
76+     const  now  =  Date . now ( ) 
77+     return  this . #arr. find ( ( entry )  =>  ( 
78+       entry . deleteAt  >  now  && 
79+       entry . method  ===  key . method  && 
80+       entry . origin  ===  key . origin  && 
81+       entry . path  ===  key . path  && 
82+       ( ! entry . vary  ||  Object . keys ( entry . vary ) . every ( headerName  =>  entry . vary [ headerName ]  ===  key . headers ?. [ headerName ] ) ) 
8483    ) ) 
85- 
86-     return  value  !=  null 
87-       ? { 
88-           statusMessage : value . statusMessage , 
89-           statusCode : value . statusCode , 
90-           rawHeaders : value . rawHeaders , 
91-           cachedAt : value . cachedAt , 
92-           staleAt : value . staleAt , 
93-           body : value . body 
94-         } 
95-       : undefined 
9684  } 
9785
9886  /** 
99-    * @param  {import('../../types/cache-interceptor.d.ts').default.CacheKey } req  
100-    * @param  {import('../../types/cache-interceptor.d.ts').default.CacheValue } opts  
87+    * @param  {import('../../types/cache-interceptor.d.ts').default.CacheKey } key  
88+    * @param  {import('../../types/cache-interceptor.d.ts').default.CacheValue } val  
10189   * @returns  {Writable | undefined } 
10290   */ 
103-   createWriteStream  ( req ,  opts )  { 
104-     if  ( typeof  req  !==  'object' )  { 
105-       throw  new  TypeError ( `expected key to be object, got ${ typeof  req }  ) 
106-     } 
107-     if  ( typeof  opts  !==  'object' )  { 
108-       throw  new  TypeError ( `expected value to be object, got ${ typeof  opts }  ) 
91+   createWriteStream  ( key ,  val )  { 
92+     if  ( typeof  key  !==  'object' )  { 
93+       throw  new  TypeError ( `expected key to be object, got ${ typeof  key }  ) 
10994    } 
110- 
111-     if  ( this . isFull )  { 
112-       this . #prune( ) 
113-     } 
114- 
115-     if  ( this . isFull )  { 
116-       return  undefined 
95+     if  ( typeof  val  !==  'object' )  { 
96+       throw  new  TypeError ( `expected value to be object, got ${ typeof  val }  ) 
11797    } 
11898
119-     let  currentSize  =  0 
120- 
12199    const  store  =  this 
122- 
123-     // TODO (fix): Deep clone... 
124-     // TODO (perf): Faster with explicit props... 
125-     const  val  =  { 
126-       statusCode : opts . statusCode , 
127-       statusMessage : opts . statusMessage , 
128-       rawHeaders : opts . rawHeaders , 
129-       vary : opts . vary , 
130-       cachedAt : opts . cachedAt , 
131-       staleAt : opts . staleAt , 
132-       deleteAt : opts . deleteAt , 
133-       method : req . method , 
134-       origin : req . origin , 
135-       path : req . path , 
136-       /** @type  {Buffer[] } */ 
137-       body : [ ] 
138-     } 
100+     const  entry  =  {  ...key ,  ...val ,  body : [ ] ,  size : 0  } 
139101
140102    return  new  Writable ( { 
141103      write  ( chunk ,  encoding ,  callback )  { 
142104        if  ( typeof  chunk  ===  'string' )  { 
143105          chunk  =  Buffer . from ( chunk ,  encoding ) 
144106        } 
145107
146-         currentSize  +=  chunk . byteLength 
108+         entry . size  +=  chunk . byteLength 
147109
148-         if  ( currentSize  >=  store . #maxEntrySize)  { 
110+         if  ( entry . size  >=  store . #maxEntrySize)  { 
149111          this . destroy ( ) 
150112        }  else  { 
151-           val . body . push ( chunk ) 
113+           entry . body . push ( chunk ) 
152114        } 
153115
154116        callback ( null ) 
155117      } , 
156118      final  ( callback )  { 
157-         store . #arr. push ( val ) 
158-         for  ( const  buf  of  val . body )  { 
159-           store . #size +=  buf . byteLength 
119+         store . #arr. push ( entry ) 
120+         store . #size +=  entry . size 
121+ 
122+         while  ( store . #arr. length  >=  store . #maxCount ||  store . #size >=  store . #maxSize)  { 
123+           const  count  =  Math . max ( 0 ,  store . #arr. length  -  store . #maxCount /  2 ) 
124+           for  ( const  entry  of  store . #arr. splice ( 0 ,  count ) )  { 
125+             store . #size -=  entry . size 
126+           } 
160127        } 
128+ 
161129        callback ( null ) 
162130      } 
163131    } ) 
@@ -166,29 +134,21 @@ class MemoryCacheStore {
166134  /** 
167135   * @param  {CacheKey } key 
168136   */ 
169-   delete  ( {  origin,  path } )  { 
170-     // https://www.rfc-editor.org/rfc/rfc9111.html#section-2-3 
137+   delete  ( key )  { 
138+     if  ( typeof  key  !==  'object' )  { 
139+       throw  new  TypeError ( `expected key to be object, got ${ typeof  key }  ) 
140+     } 
141+ 
171142    const  arr  =  [ ] 
172-     for  ( const  value  of  this . #arr)  { 
173-       if  ( value . path  ===  path  &&  value . origin  ===  origin )  { 
174-         for  ( const  buf  of  value . body )  { 
175-           this . #size -=  buf . byteLength 
176-         } 
143+     for  ( const  entry  of  this . #arr)  { 
144+       if  ( entry . path  ===  key . path  &&  entry . origin  ===  key . origin )  { 
145+         this . #size -=  entry . size 
177146      }  else  { 
178-         arr . push ( value ) 
147+         arr . push ( entry ) 
179148      } 
180149    } 
181150    this . #arr =  arr 
182151  } 
183- 
184-   #prune ( )  { 
185-     const  count  =  Math . max ( 0 ,  this . #arr. length  -  this . #maxCount /  2 ) 
186-     for  ( const  value  of  this . #arr. splice ( 0 ,  count ) )  { 
187-       for  ( const  buf  of  value . body )  { 
188-         this . #size -=  buf . byteLength 
189-       } 
190-     } 
191-   } 
192152} 
193153
194154module . exports  =  MemoryCacheStore 
0 commit comments