Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Nov 12, 2024
1 parent 9f467e7 commit bb502d9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 57 deletions.
5 changes: 3 additions & 2 deletions docs/docs/api/CacheStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ The store must implement the following functions:
Optional. This tells the cache interceptor if the store is full or not. If this is true,
the cache interceptor will not attempt to cache the response.

### Function: `getValueByKey`
### Function: `get`

Parameters:

* **req** `Dispatcher.RequestOptions` - Incoming request

Returns: `GetValueByKeyResult | Promise<GetValueByKeyResult | undefined> | undefined` - If the request is cached, a readable for the body is returned. Otherwise, `undefined` is returned.
Returns: `GetResult | Promise<GetResult | undefined> | undefined` - If the request is cached, the cached response is returned. If the request's method is anything other than HEAD, the response is also returned.
If the request isn't cached, `undefined` is returned.

Response properties:

Expand Down
31 changes: 25 additions & 6 deletions lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class MemoryCacheStore {

/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
* @returns {import('../../types/cache-interceptor.d.ts').default.GetValueByKeyResult | undefined}
* @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined}
*/
getValueByKey (key) {
get (key) {
if (typeof key !== 'object') {
throw new TypeError(`expected key to be object, got ${typeof key}`)
}
Expand Down Expand Up @@ -121,9 +121,10 @@ class MemoryCacheStore {
const values = this.#getValuesForRequest(key, true)

/**
* @type {number}
* @type {(MemoryStoreValue & { index: number }) | undefined}
*/
let value = this.#findValue(key, values)
let valueIndex = value?.index
if (!value) {
// The value doesn't already exist, meaning we haven't cached this
// response before. Let's assign it a value and insert it into our data
Expand All @@ -150,9 +151,11 @@ class MemoryCacheStore {
// Our value is either the only response for this path or our deleteAt
// time is sooner than all the other responses
values.push(value)
valueIndex = values.length - 1
} else if (opts.deleteAt >= values[0].deleteAt) {
// Our deleteAt is later than everyone elses
values.unshift(value)
valueIndex = 0
} else {
// We're neither in the front or the end, let's just binary search to
// find our stop we need to be in
Expand All @@ -168,6 +171,7 @@ class MemoryCacheStore {
const middleValue = values[middleIndex]
if (opts.deleteAt === middleIndex) {
values.splice(middleIndex, 0, value)
valueIndex = middleIndex
break
} else if (opts.deleteAt > middleValue.opts.deleteAt) {
endIndex = middleIndex
Expand Down Expand Up @@ -215,7 +219,7 @@ class MemoryCacheStore {
if (currentSize >= maxEntrySize) {
body = null
this.end()
// TODO remove element from values
shiftAtIndex(values, valueIndex)
return callback()
}

Expand Down Expand Up @@ -276,7 +280,7 @@ class MemoryCacheStore {
* to respond with.
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} req
* @param {MemoryStoreValue[]} values
* @returns {MemoryStoreValue | undefined}
* @returns {(MemoryStoreValue & { index: number }) | undefined}
*/
#findValue (req, values) {
/**
Expand Down Expand Up @@ -311,7 +315,10 @@ class MemoryCacheStore {
}

if (matches) {
value = current
value = {
current,
index: i
}
break
}
}
Expand All @@ -320,4 +327,16 @@ class MemoryCacheStore {
}
}

/**
* @param {any[]} array Array to modify
* @param {number} idx Index to delete
*/
function shiftAtIndex (array, idx) {
for (let i = idx + 1; idx < array.length; i++) {
array[i - 1] = array[i]
}

array.length--
}

module.exports = MemoryCacheStore
70 changes: 42 additions & 28 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,43 @@ class CacheHandler extends DecoratorHandler {
cacheControlDirectives
)

this.#writeStream = this.#store.createWriteStream(this.#cacheKey, {
statusCode,
statusMessage,
rawHeaders: strippedHeaders,
vary: varyDirectives,
cachedAt: now,
staleAt,
deleteAt
})

if (this.#writeStream) {
const handler = this
this.#writeStream
.on('drain', resume)
.on('error', function () {
if (this.#cacheKey.method === 'HEAD') {
this.#store.createWriteStream(this.#cacheKey, {
statusCode,
statusMessage,
rawHeaders: strippedHeaders,
vary: varyDirectives,
cachedAt: now,
staleAt,
deleteAt
})
} else {
this.#writeStream = this.#store.createWriteStream(this.#cacheKey, {
statusCode,
statusMessage,
rawHeaders: strippedHeaders,
vary: varyDirectives,
cachedAt: now,
staleAt,
deleteAt
})

if (this.#writeStream) {
const handler = this
this.#writeStream
.on('drain', resume)
.on('error', function () {
// TODO (fix): Make error somehow observable?
})
.on('close', function () {
if (handler.#writeStream === this) {
handler.#writeStream = undefined
}

// TODO (fix): Should we resume even if was paused downstream?
resume()
})
})
.on('close', function () {
if (handler.#writeStream === this) {
handler.#writeStream = undefined
}

// TODO (fix): Should we resume even if was paused downstream?
resume()
})
}
}
}

Expand All @@ -175,7 +187,7 @@ class CacheHandler extends DecoratorHandler {
onData (chunk) {
let paused = false

if (this.#writeStream && this.#cacheKey.method !== 'HEAD') {
if (this.#writeStream) {
paused ||= this.#writeStream.write(chunk) === false
}

Expand Down Expand Up @@ -234,7 +246,6 @@ function canCacheResponse (statusCode, headers, cacheControlDirectives) {
}

if (
!cacheControlDirectives.public ||
cacheControlDirectives.private === true ||
cacheControlDirectives['no-cache'] === true ||
cacheControlDirectives['no-store']
Expand All @@ -249,7 +260,7 @@ function canCacheResponse (statusCode, headers, cacheControlDirectives) {

// https://www.rfc-editor.org/rfc/rfc9111.html#name-storing-responses-to-authen
if (headers.authorization) {
if (typeof headers.authorization !== 'string') {
if (!cacheControlDirectives.public || typeof headers.authorization !== 'string') {
return false
}

Expand Down Expand Up @@ -299,7 +310,10 @@ function determineStaleAt (now, headers, cacheControlDirectives) {

if (headers.expire && typeof headers.expire === 'string') {
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
return now + (Date.now() - new Date(headers.expire).getTime())
const expiresDate = new Date(headers.expire)
if (expiresDate instanceof Date && !isNaN(expiresDate)) {
return now + (Date.now() - expiresDate.getTime())
}
}

return undefined
Expand Down
6 changes: 4 additions & 2 deletions lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = (opts = {}) => {
// TODO (perf): For small entries support returning a Buffer instead of a stream.
// Maybe store should return { staleAt, headers, body, etc... } instead of a stream + stream.value?
// Where body can be a Buffer, string, stream or blob?
const result = store.getValueByKey(cacheKey)
const result = store.get(cacheKey)
if (!result) {
// Request isn't cached
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
Expand Down Expand Up @@ -114,6 +114,8 @@ module.exports = (opts = {}) => {
if (typeof handler.onComplete === 'function') {
handler.onComplete([])
}

stream?.destroy()
} else {
stream.on('data', function (chunk) {
if (typeof handler.onData === 'function' && !handler.onData(chunk)) {
Expand All @@ -127,7 +129,7 @@ module.exports = (opts = {}) => {
}

/**
* @param {import('../../types/cache-interceptor.d.ts').default.GetValueByKeyResult} result
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
*/
const handleStream = (result) => {
const { response: value, body: stream } = result
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function assertCacheStore (store, name = 'CacheStore') {
throw new TypeError(`expected type of ${name} to be a CacheStore, got ${store === null ? 'null' : typeof store}`)
}

for (const fn of ['getValueByKey', 'createWriteStream', 'deleteByKey']) {
for (const fn of ['get', 'createWriteStream', 'deleteByKey']) {
if (typeof store[fn] !== 'function') {
throw new TypeError(`${name} needs to have a \`${fn}()\` function`)
}
Expand Down
28 changes: 14 additions & 14 deletions test/cache-interceptor/cache-stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function cacheStoreTests (CacheStore) {
test('matches interface', async () => {
const store = new CacheStore()
equal(typeof store.isFull, 'boolean')
equal(typeof store.getValueByKey, 'function')
equal(typeof store.get, 'function')
equal(typeof store.createWriteStream, 'function')
equal(typeof store.deleteByKey, 'function')
})
Expand Down Expand Up @@ -44,15 +44,15 @@ function cacheStoreTests (CacheStore) {
const store = new CacheStore()

// Sanity check
equal(store.getValueByKey(request), undefined)
equal(store.get(request), undefined)

// Write the response to the store
let writeStream = store.createWriteStream(request, requestValue)
notEqual(writeStream, undefined)
writeResponse(writeStream, requestBody)

// Now try fetching it with a deep copy of the original request
let readResult = store.getValueByKey(structuredClone(request))
let readResult = store.get(structuredClone(request))
notEqual(readResult, undefined)

deepStrictEqual(await readResponse(readResult), {
Expand All @@ -79,7 +79,7 @@ function cacheStoreTests (CacheStore) {

// We haven't cached this one yet, make sure it doesn't confuse it with
// another request
equal(store.getValueByKey(anotherRequest), undefined)
equal(store.get(anotherRequest), undefined)

// Now let's cache it
writeStream = store.createWriteStream(anotherRequest, {
Expand All @@ -89,7 +89,7 @@ function cacheStoreTests (CacheStore) {
notEqual(writeStream, undefined)
writeResponse(writeStream, anotherBody)

readResult = store.getValueByKey(anotherRequest)
readResult = store.get(anotherRequest)
notEqual(readResult, undefined)
deepStrictEqual(await readResponse(readResult), {
...anotherValue,
Expand Down Expand Up @@ -123,7 +123,7 @@ function cacheStoreTests (CacheStore) {
notEqual(writeStream, undefined)
writeResponse(writeStream, requestBody)

const readResult = store.getValueByKey(request)
const readResult = store.get(request)
deepStrictEqual(await readResponse(readResult), {
...requestValue,
body: requestBody
Expand Down Expand Up @@ -155,7 +155,7 @@ function cacheStoreTests (CacheStore) {
notEqual(writeStream, undefined)
writeResponse(writeStream, requestBody)

equal(store.getValueByKey(request), undefined)
equal(store.get(request), undefined)
})

test('respects vary directives', async () => {
Expand Down Expand Up @@ -186,13 +186,13 @@ function cacheStoreTests (CacheStore) {
const store = new CacheStore()

// Sanity check
equal(store.getValueByKey(request), undefined)
equal(store.get(request), undefined)

const writeStream = store.createWriteStream(request, requestValue)
notEqual(writeStream, undefined)
writeResponse(writeStream, requestBody)

const readStream = store.getValueByKey(structuredClone(request))
const readStream = store.get(structuredClone(request))
notEqual(readStream, undefined)
deepStrictEqual(await readResponse(readStream), {
...requestValue,
Expand All @@ -207,7 +207,7 @@ function cacheStoreTests (CacheStore) {
'some-header': 'another-value'
}
}
equal(store.getValueByKey(nonMatchingRequest), undefined)
equal(store.get(nonMatchingRequest), undefined)
})
})
}
Expand Down Expand Up @@ -236,14 +236,14 @@ test('MemoryCacheStore locks values properly', async () => {

// Value should now be locked, we shouldn't be able to create a readable or
// another writable to it until the first one finishes
equal(store.getValueByKey(request), undefined)
equal(store.get(request), undefined)

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:796:25) at Test.processPendingSubtests (node:internal/test_runner/test:527:18) at Suite.postRun (node:internal/test_runner/test:889:19) at Suite.run (node:internal/test_runner/test:1150:10) at async Test.processPendingSubtests (node:internal/test_runner/test:527:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:639:25) at Test.processPendingSubtests (node:internal/test_runner/test:382:18) at Suite.postRun (node:internal/test_runner/test:730:19) at Suite.run (node:internal/test_runner/test:979:10) at async startSubtest (node:internal/test_runner/harness:218:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at Test.processPendingSubtests (node:internal/test_runner/test:374:18) at Suite.postRun (node:internal/test_runner/test:715:19) at Suite.run (node:internal/test_runner/test:962:10) at async startSubtest (node:internal/test_runner/harness:214:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' }, code: 'ERR_TEST_FAILURE' }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (23, ubuntu-latest) / Test with Node.js 23 on ubuntu-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:297:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (22, ubuntu-latest) / Test with Node.js 22 on ubuntu-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:296:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (22, macos-latest) / Test with Node.js 22 on macos-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:296:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (23, macos-latest) / Test with Node.js 23 on macos-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:297:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:796:25) at Test.processPendingSubtests (node:internal/test_runner/test:527:18) at Suite.postRun (node:internal/test_runner/test:889:19) at Suite.run (node:internal/test_runner/test:1150:10) at async Test.processPendingSubtests (node:internal/test_runner/test:527:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at Test.processPendingSubtests (node:internal/test_runner/test:374:18) at Suite.postRun (node:internal/test_runner/test:715:19) at Suite.run (node:internal/test_runner/test:962:10) at async startSubtest (node:internal/test_runner/harness:214:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' }, code: 'ERR_TEST_FAILURE' }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:639:25) at Test.processPendingSubtests (node:internal/test_runner/test:382:18) at Suite.postRun (node:internal/test_runner/test:730:19) at Suite.run (node:internal/test_runner/test:979:10) at async startSubtest (node:internal/test_runner/harness:218:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:796:25) at Test.processPendingSubtests (node:internal/test_runner/test:527:18) at Suite.postRun (node:internal/test_runner/test:889:19) at Suite.run (node:internal/test_runner/test:1150:10) at async Test.processPendingSubtests (node:internal/test_runner/test:527:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 22 compiled --without-intl

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:296:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }

Check failure on line 239 in test/cache-interceptor/cache-stores.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 23 compiled --without-intl

MemoryCacheStore locks values properly

[Error [ERR_TEST_FAILURE]: { body: undefined, response: undefined } == undefined] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: AssertionError [ERR_ASSERTION]: { body: undefined, response: undefined } == undefined at TestContext.<anonymous> (/home/runner/work/undici/undici/test/cache-interceptor/cache-stores.js:239:3) at Test.runInAsyncScope (node:async_hooks:211:14) at Test.run (node:internal/test_runner/test:934:25) at Test.processPendingSubtests (node:internal/test_runner/test:633:18) at Suite.postRun (node:internal/test_runner/test:1045:19) at Suite.run (node:internal/test_runner/test:1335:10) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:297:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: { response: undefined, body: undefined }, expected: undefined, operator: '==' } }
equal(store.createWriteStream(request, requestValue), undefined)

// Close the writable, this should unlock it
writeResponse(writable, ['asd'])

// Stream is now closed, let's lock any new write streams
const result = store.getValueByKey(request)
const result = store.get(request)
notEqual(result, undefined)

// Consume & close the result, this should lift the write lock
Expand All @@ -265,8 +265,8 @@ function writeResponse (stream, body) {
}

/**
* @param {import('../../types/cache-interceptor.d.ts').default.GetValueByKeyResult} result
* @returns {Promise<import('../../types/cache-interceptor.d.ts').default.GetValueByKeyResult | { body: Buffer[] }>}
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
* @returns {Promise<import('../../types/cache-interceptor.d.ts').default.GetResult | { body: Buffer[] }>}
*/
async function readResponse ({ response, body: stream }) {
/**
Expand Down
2 changes: 1 addition & 1 deletion test/types/cache-interceptor.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CacheInterceptor from '../../types/cache-interceptor'
const store: CacheInterceptor.CacheStore = {
isFull: false,

getValue (_: CacheInterceptor.CacheKey): CacheInterceptor.GetValueByKeyResult | Promise<CacheInterceptor.GetValueByKeyResult | undefined> | undefined {
getValue (_: CacheInterceptor.CacheKey): CacheInterceptor.GetResult | Promise<CacheInterceptor.GetResult | undefined> | undefined {
throw new Error('stub')
},

Expand Down
Loading

0 comments on commit bb502d9

Please sign in to comment.