@@ -64,100 +64,68 @@ class MemoryCacheStore {
64
64
}
65
65
}
66
66
67
- get isFull ( ) {
68
- return this . #arr. length >= this . #maxCount || this . #size >= this . #maxSize
69
- }
70
-
71
67
/**
72
68
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } req
73
69
* @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined }
74
70
*/
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
+ }
77
75
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 == null || Object . keys ( entry . vary ) . every ( headerName => entry . vary [ headerName ] === key . headers ?. [ headerName ] ) )
84
83
) )
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
96
84
}
97
85
98
86
/**
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
101
89
* @returns {Writable | undefined }
102
90
*/
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 } ` )
109
94
}
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 } ` )
117
97
}
118
98
119
- let currentSize = 0
120
-
121
99
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 }
139
101
140
102
return new Writable ( {
141
103
write ( chunk , encoding , callback ) {
142
104
if ( typeof chunk === 'string' ) {
143
105
chunk = Buffer . from ( chunk , encoding )
144
106
}
145
107
146
- currentSize += chunk . byteLength
108
+ entry . size += chunk . byteLength
147
109
148
- if ( currentSize >= store . #maxEntrySize) {
110
+ if ( entry . size >= store . #maxEntrySize) {
149
111
this . destroy ( )
150
112
} else {
151
- val . body . push ( chunk )
113
+ entry . body . push ( chunk )
152
114
}
153
115
154
116
callback ( null )
155
117
} ,
156
118
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
+ }
160
127
}
128
+
161
129
callback ( null )
162
130
}
163
131
} )
@@ -166,29 +134,21 @@ class MemoryCacheStore {
166
134
/**
167
135
* @param {CacheKey } key
168
136
*/
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
+
171
142
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
177
146
} else {
178
- arr . push ( value )
147
+ arr . push ( entry )
179
148
}
180
149
}
181
150
this . #arr = arr
182
151
}
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
- }
192
152
}
193
153
194
154
module . exports = MemoryCacheStore
0 commit comments