Skip to content

Commit 551e79d

Browse files
committed
fixup
1 parent 8ed6901 commit 551e79d

File tree

2 files changed

+41
-88
lines changed

2 files changed

+41
-88
lines changed

lib/cache/memory-cache-store.js

+40-80
Original file line numberDiff line numberDiff line change
@@ -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 == null || 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

194154
module.exports = MemoryCacheStore

types/cache-interceptor.d.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ declare namespace CacheHandler {
4545
statusCode: number
4646
statusMessage: string
4747
rawHeaders: Buffer[]
48-
body: null | Readable | Iterable<Buffer> | Buffer | Iterable<string> | string
48+
body: null | Readable | Iterable<Buffer> | AsyncIterable<Buffer> | Buffer | Iterable<string> | AsyncIterable<string> | string
4949
cachedAt: number
5050
staleAt: number
5151
}
@@ -54,11 +54,6 @@ declare namespace CacheHandler {
5454
* Underlying storage provider for cached responses
5555
*/
5656
export interface CacheStore {
57-
/**
58-
* Whether or not the cache is full and can not store any more responses
59-
*/
60-
get isFull(): boolean | undefined
61-
6257
get(key: CacheKey): GetResult | Promise<GetResult | undefined> | undefined
6358

6459
createWriteStream(key: CacheKey, val: CacheValue): Writable | undefined
@@ -88,8 +83,6 @@ declare namespace CacheHandler {
8883
export class MemoryCacheStore implements CacheStore {
8984
constructor (opts?: MemoryCacheStoreOpts)
9085

91-
get isFull (): boolean | undefined
92-
9386
get (key: CacheKey): GetResult | Promise<GetResult | undefined> | undefined
9487

9588
createWriteStream (key: CacheKey, value: CachedResponse): Writable | undefined

0 commit comments

Comments
 (0)