Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: http caching #3562

Merged
merged 65 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f69a8b2
feat: http caching
flakey5 Aug 28, 2024
57c0f45
fixup! feat: http caching
flakey5 Sep 14, 2024
c88fb25
fixup! fixup! feat: http caching
flakey5 Sep 15, 2024
0a442b2
fixup! fixup! fixup! feat: http caching
flakey5 Sep 15, 2024
ae6edca
Update lib/handler/cache-handler.js
flakey5 Sep 15, 2024
26b2227
Apply suggestions from code review
flakey5 Sep 16, 2024
81cb021
fixup! fixup! fixup! fixup! feat: http caching
flakey5 Sep 16, 2024
6bff376
fixup! fixup! fixup! fixup! fixup! feat: http caching
flakey5 Sep 16, 2024
2edee29
clarify type for MemoryCacheStore
flakey5 Sep 17, 2024
f128e9a
Apply suggestions from code review
flakey5 Sep 17, 2024
807e764
Apply suggestions from code review
flakey5 Sep 17, 2024
4f8139a
tmp
flakey5 Sep 17, 2024
fabf558
fixup! tmp
flakey5 Sep 17, 2024
4546799
Apply suggestions from code review
flakey5 Sep 19, 2024
5a215d2
perf things, deleteByOrigin
flakey5 Sep 19, 2024
73564e8
incredibly messy and broken impl of streaming idea
flakey5 Sep 20, 2024
3a08528
Merge branch 'main' into flakey5/3231
metcoder95 Sep 20, 2024
cbe7b97
fix tests
flakey5 Sep 20, 2024
edc0772
check if the response is already cached again
flakey5 Sep 21, 2024
d7b24a4
backpressure patch
flakey5 Sep 21, 2024
0877f95
move body out of CacheStoreValue, remove size property
flakey5 Sep 22, 2024
e065a8e
Apply suggestions from code review
flakey5 Sep 24, 2024
230533a
add some comments on createWriteStream
flakey5 Sep 24, 2024
bcd7fa1
fix type tests, make staleAt and deleteAt absolute
flakey5 Sep 24, 2024
9ef03ef
empty the body when overwriting the response
flakey5 Sep 24, 2024
85a99d5
Merge branch 'main' into flakey5/3231
metcoder95 Sep 24, 2024
58839ee
update onError calls
flakey5 Sep 24, 2024
e49a32c
remove request deduplication for now
flakey5 Sep 25, 2024
6469aab
rename value -> opts, storedValue -> value
flakey5 Sep 25, 2024
969deb2
fix types
flakey5 Sep 25, 2024
3370f66
Apply suggestions from code review
flakey5 Sep 25, 2024
263718e
simplify parsing for qualified no-cache and private
flakey5 Sep 26, 2024
b5e483a
fix header omission, some cleanup
flakey5 Sep 26, 2024
7ea49d3
Merge branch 'main' into flakey5/3231
flakey5 Sep 29, 2024
ded27a1
Merge branch 'main' into flakey5/3231
metcoder95 Oct 1, 2024
d983b9c
running the tests in ci is probably a good idea
flakey5 Oct 1, 2024
6b1de1f
fix some testing values
flakey5 Oct 1, 2024
7e482e9
fixup! running the tests in ci is probably a good idea
flakey5 Oct 1, 2024
e433ec6
Update lib/interceptor/cache.js
flakey5 Oct 1, 2024
d8c7368
Update lib/util/cache.js
flakey5 Oct 2, 2024
0a7a99e
update from reviews
flakey5 Oct 2, 2024
43efed2
Update lib/interceptor/cache.js
flakey5 Oct 2, 2024
e765976
Apply suggestions from code review
flakey5 Oct 2, 2024
019f9b4
change from reviews
flakey5 Oct 2, 2024
1db72bb
add promise support back for createReadStream
flakey5 Oct 2, 2024
ab72d14
Apply suggestions from code review
flakey5 Oct 5, 2024
917b530
check if onError was called
flakey5 Oct 5, 2024
2adae09
add docs
flakey5 Oct 8, 2024
0ed33e4
Merge branch 'main' into flakey5/3231
flakey5 Oct 8, 2024
b951bd5
Merge branch 'main' into flakey5/3231
metcoder95 Oct 8, 2024
eda939d
add errorCallback
flakey5 Oct 9, 2024
a43112a
Update types/cache-interceptor.d.ts
flakey5 Oct 9, 2024
684d044
use fake timers and cleanup client
flakey5 Oct 10, 2024
12c2ded
lazy cache wellknown headers
flakey5 Oct 12, 2024
af0875e
Merge branch 'main' into flakey5/3231
metcoder95 Oct 13, 2024
29b1362
Apply suggestions from code review
flakey5 Oct 14, 2024
9e4f15b
Update lib/cache/memory-cache-store.js
flakey5 Oct 14, 2024
a5a3cf3
code review
flakey5 Oct 14, 2024
8bf9864
Apply suggestions from code review
flakey5 Oct 14, 2024
baaa914
Apply suggestions from code review
flakey5 Oct 14, 2024
8bf6b42
code review pt2
flakey5 Oct 14, 2024
05109b1
Update lib/handler/cache-revalidation-handler.js
mcollina Oct 14, 2024
092d9d4
Update lib/handler/cache-handler.js
mcollina Oct 14, 2024
1b91475
Apply suggestions from code review
Uzlopak Oct 14, 2024
e6aeda9
fix
Uzlopak Oct 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions docs/docs/api/CacheStore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Cache Store

A Cache Store is responsible for storing and retrieving cached responses.
It is also responsible for deciding which specific response to use based off of
a response's `Vary` header (if present).

## Pre-built Cache Stores

### `MemoryCacheStore`

The `MemoryCacheStore` stores the responses in-memory.

**Options**

- `maxEntries` - The maximum amount of responses to store. Default `Infinity`.
- `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached.

## Defining a Custom Cache Store

The store must implement the following functions:

### Getter: `isFull`

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: `createReadStream`

Parameters:

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

Returns: `CacheStoreReadable | Promise<CacheStoreReadable | undefined> | undefined` - If the request is cached, a readable for the body is returned. Otherwise, `undefined` is returned.

### Function: `createWriteStream`

Parameters:

* **req** `Dispatcher.RequestOptions` - Incoming request
* **value** `CacheStoreValue` - Response to store

Returns: `CacheStoreWriteable | undefined` - If the store is full, return `undefined`. Otherwise, return a writable so that the cache interceptor can stream the body and trailers to the store.

## `CacheStoreValue`

This is an interface containing the majority of a response's data (minus the body).

### Property `statusCode`

`number` - The response's HTTP status code.

### Property `statusMessage`

`string` - The response's HTTP status message.

### Property `rawHeaders`

`(Buffer | Buffer[])[]` - The response's headers.

### Property `rawTrailers`

`string[] | undefined` - The response's trailers.

### Property `vary`

`Record<string, string> | undefined` - The headers defined by the response's `Vary` header
and their respective values for later comparison

For example, for a response like
```
Vary: content-encoding, accepts
content-encoding: utf8
accepts: application/json
```

This would be
```js
{
'content-encoding': 'utf8',
accepts: 'application/json'
}
```

### Property `cachedAt`

`number` - Time in millis that this value was cached.

### Property `staleAt`

`number` - Time in millis that this value is considered stale.

### Property `deleteAt`

`number` - Time in millis that this value is to be deleted from the cache. This
is either the same sa staleAt or the `max-stale` caching directive.

The store must not return a response after the time defined in this property.

## `CacheStoreReadable`

This extends Node's [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable)
and defines extra properties relevant to the cache interceptor.

### Getter: `value`

The response's [`CacheStoreValue`](#cachestorevalue)

## `CacheStoreWriteable`

This extends Node's [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable)
and defines extra properties relevant to the cache interceptor.

### Setter: `rawTrailers`

If the response has trailers, the cache interceptor will pass them to the cache
interceptor through this method.
10 changes: 10 additions & 0 deletions docs/docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,16 @@ test('should not error if request status code is not in the specified error code

The Response Error Interceptor provides a robust mechanism for handling HTTP response errors by capturing detailed error information and propagating it through a structured `ResponseError` class. This enhancement improves error handling and debugging capabilities in applications using the interceptor.

##### `Cache Interceptor`

The `cache` interceptor implements client-side response caching as described in
[RFC9111](https://www.rfc-editor.org/rfc/rfc9111.html).

**Options**

- `store` - The [`CacheStore`](./CacheStore.md) to store and retrieve responses from. Default is [`MemoryCacheStore`](./CacheStore.md#memorycachestore).
- `methods` - The [**safe** HTTP methods](https://www.rfc-editor.org/rfc/rfc9110#section-9.2.1) to cache the response of.

## Instance Events

### Event: `'connect'`
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ module.exports.interceptors = {
redirect: require('./lib/interceptor/redirect'),
retry: require('./lib/interceptor/retry'),
dump: require('./lib/interceptor/dump'),
dns: require('./lib/interceptor/dns')
dns: require('./lib/interceptor/dns'),
cache: require('./lib/interceptor/cache')
}

module.exports.cacheStores = {
MemoryCacheStore: require('./lib/cache/memory-cache-store')
}

module.exports.buildConnector = buildConnector
Expand Down
Loading
Loading