MCPcopy
hub / github.com/nodejs/undici / createWriteStream

Method createWriteStream

lib/cache/memory-cache-store.js:124–200  ·  view source on GitHub ↗

* @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)

Source from the content-addressed store, hash-verified

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) {

Callers

nothing calls this directly

Calls 2

assertCacheKeyFunction · 0.85
assertCacheValueFunction · 0.85

Tested by

no test coverage detected