* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} val * @returns {Writable | undefined}
(key, val)
| 122 | * @returns {Writable | undefined} |
| 123 | */ |
| 124 | createWriteStream (key, val) { |
| 125 | assertCacheKey(key) |
| 126 | assertCacheValue(val) |
| 127 | |
| 128 | const topLevelKey = `${key.origin}:${key.path}` |
| 129 | |
| 130 | const store = this |
| 131 | const entry = { ...key, ...val, body: [], size: 0 } |
| 132 | |
| 133 | return new Writable({ |
| 134 | write (chunk, encoding, callback) { |
| 135 | if (typeof chunk === 'string') { |
| 136 | chunk = Buffer.from(chunk, encoding) |
| 137 | } |
| 138 | |
| 139 | entry.size += chunk.byteLength |
| 140 | |
| 141 | if (entry.size > store.#maxEntrySize) { |
| 142 | this.destroy() |
| 143 | } else { |
| 144 | entry.body.push(chunk) |
| 145 | } |
| 146 | |
| 147 | callback(null) |
| 148 | }, |
| 149 | final (callback) { |
| 150 | let entries = store.#entries.get(topLevelKey) |
| 151 | if (!entries) { |
| 152 | entries = [] |
| 153 | store.#entries.set(topLevelKey, entries) |
| 154 | } |
| 155 | const previousEntry = findEntry(key, entries, Date.now()) |
| 156 | if (previousEntry) { |
| 157 | const index = entries.indexOf(previousEntry) |
| 158 | entries.splice(index, 1, entry) |
| 159 | store.#size -= previousEntry.size |
| 160 | } else { |
| 161 | entries.push(entry) |
| 162 | store.#count += 1 |
| 163 | } |
| 164 | |
| 165 | store.#size += entry.size |
| 166 | |
| 167 | // Check if cache is full and emit event if needed |
| 168 | if (store.#size > store.#maxSize || store.#count > store.#maxCount) { |
| 169 | // Emit maxSizeExceeded event if we haven't already |
| 170 | if (!store.#hasEmittedMaxSizeEvent) { |
| 171 | store.emit('maxSizeExceeded', { |
| 172 | size: store.#size, |
| 173 | maxSize: store.#maxSize, |
| 174 | count: store.#count, |
| 175 | maxCount: store.#maxCount |
| 176 | }) |
| 177 | store.#hasEmittedMaxSizeEvent = true |
| 178 | } |
| 179 | |
| 180 | // Perform eviction |
| 181 | for (const [key, entries] of store.#entries) { |
nothing calls this directly
no test coverage detected