Skip to content

Commit d3d77b2

Browse files
committed
fixup
1 parent 8ed6901 commit d3d77b2

File tree

2 files changed

+36
-87
lines changed

2 files changed

+36
-87
lines changed

lib/cache/memory-cache-store.js

Lines changed: 35 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -64,100 +64,64 @@ 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
*/
7571
get ({ origin, path, method, headers }) {
7672
const now = Date.now()
77-
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]))
73+
return this.#arr.find((entry) => (
74+
entry.deleteAt > now &&
75+
entry.method === method &&
76+
entry.origin === origin &&
77+
entry.path === path &&
78+
(!entry.vary || Object.keys(entry.vary).every(headerName => entry.vary[headerName] === headers?.[headerName]))
8479
))
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
9680
}
9781

9882
/**
99-
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} req
100-
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} opts
83+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
84+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} val
10185
* @returns {Writable | undefined}
10286
*/
103-
createWriteStream (req, opts) {
104-
if (typeof req !== 'object') {
105-
throw new TypeError(`expected key to be object, got ${typeof req}`)
87+
createWriteStream (key, val) {
88+
if (typeof key !== 'object') {
89+
throw new TypeError(`expected key to be object, got ${typeof key}`)
10690
}
107-
if (typeof opts !== 'object') {
108-
throw new TypeError(`expected value to be object, got ${typeof opts}`)
91+
if (typeof val !== 'object') {
92+
throw new TypeError(`expected value to be object, got ${typeof val}`)
10993
}
11094

111-
if (this.isFull) {
112-
this.#prune()
113-
}
114-
115-
if (this.isFull) {
116-
return undefined
117-
}
118-
119-
let currentSize = 0
120-
12195
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-
}
96+
const entry = { ...key, ...val, body: [], size: 0 }
13997

14098
return new Writable({
14199
write (chunk, encoding, callback) {
142100
if (typeof chunk === 'string') {
143101
chunk = Buffer.from(chunk, encoding)
144102
}
145103

146-
currentSize += chunk.byteLength
104+
entry.size += chunk.byteLength
147105

148-
if (currentSize >= store.#maxEntrySize) {
106+
if (entry.size >= store.#maxEntrySize) {
149107
this.destroy()
150108
} else {
151-
val.body.push(chunk)
109+
entry.body.push(chunk)
152110
}
153111

154112
callback(null)
155113
},
156114
final (callback) {
157-
store.#arr.push(val)
158-
for (const buf of val.body) {
159-
store.#size += buf.byteLength
115+
store.#arr.push(entry)
116+
store.#size += entry.size
117+
118+
while (store.#arr.length >= store.#maxCount || store.#size >= store.#maxSize) {
119+
const count = Math.max(0, store.#arr.length - store.#maxCount / 2)
120+
for (const entry of store.#arr.splice(0, count)) {
121+
store.#size -= entry.size
122+
}
160123
}
124+
161125
callback(null)
162126
}
163127
})
@@ -166,29 +130,21 @@ class MemoryCacheStore {
166130
/**
167131
* @param {CacheKey} key
168132
*/
169-
delete ({ origin, path }) {
170-
// https://www.rfc-editor.org/rfc/rfc9111.html#section-2-3
133+
delete (key) {
134+
if (typeof key !== 'object') {
135+
throw new TypeError(`expected key to be object, got ${typeof key}`)
136+
}
137+
171138
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-
}
139+
for (const entry of this.#arr) {
140+
if (entry.path === key.path && entry.origin === key.origin) {
141+
this.#size -= entry.size
177142
} else {
178-
arr.push(value)
143+
arr.push(entry)
179144
}
180145
}
181146
this.#arr = arr
182147
}
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-
}
192148
}
193149

194150
module.exports = MemoryCacheStore

types/cache-interceptor.d.ts

Lines changed: 1 addition & 8 deletions
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)