-
-
Notifications
You must be signed in to change notification settings - Fork 671
feat: http caching #3562
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
Merged
Merged
feat: http caching #3562
Changes from 2 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
f69a8b2
feat: http caching
flakey5 57c0f45
fixup! feat: http caching
flakey5 c88fb25
fixup! fixup! feat: http caching
flakey5 0a442b2
fixup! fixup! fixup! feat: http caching
flakey5 ae6edca
Update lib/handler/cache-handler.js
flakey5 26b2227
Apply suggestions from code review
flakey5 81cb021
fixup! fixup! fixup! fixup! feat: http caching
flakey5 6bff376
fixup! fixup! fixup! fixup! fixup! feat: http caching
flakey5 2edee29
clarify type for MemoryCacheStore
flakey5 f128e9a
Apply suggestions from code review
flakey5 807e764
Apply suggestions from code review
flakey5 4f8139a
tmp
flakey5 fabf558
fixup! tmp
flakey5 4546799
Apply suggestions from code review
flakey5 5a215d2
perf things, deleteByOrigin
flakey5 73564e8
incredibly messy and broken impl of streaming idea
flakey5 3a08528
Merge branch 'main' into flakey5/3231
metcoder95 cbe7b97
fix tests
flakey5 edc0772
check if the response is already cached again
flakey5 d7b24a4
backpressure patch
flakey5 0877f95
move body out of CacheStoreValue, remove size property
flakey5 e065a8e
Apply suggestions from code review
flakey5 230533a
add some comments on createWriteStream
flakey5 bcd7fa1
fix type tests, make staleAt and deleteAt absolute
flakey5 9ef03ef
empty the body when overwriting the response
flakey5 85a99d5
Merge branch 'main' into flakey5/3231
metcoder95 58839ee
update onError calls
flakey5 e49a32c
remove request deduplication for now
flakey5 6469aab
rename value -> opts, storedValue -> value
flakey5 969deb2
fix types
flakey5 3370f66
Apply suggestions from code review
flakey5 263718e
simplify parsing for qualified no-cache and private
flakey5 b5e483a
fix header omission, some cleanup
flakey5 7ea49d3
Merge branch 'main' into flakey5/3231
flakey5 ded27a1
Merge branch 'main' into flakey5/3231
metcoder95 d983b9c
running the tests in ci is probably a good idea
flakey5 6b1de1f
fix some testing values
flakey5 7e482e9
fixup! running the tests in ci is probably a good idea
flakey5 e433ec6
Update lib/interceptor/cache.js
flakey5 d8c7368
Update lib/util/cache.js
flakey5 0a7a99e
update from reviews
flakey5 43efed2
Update lib/interceptor/cache.js
flakey5 e765976
Apply suggestions from code review
flakey5 019f9b4
change from reviews
flakey5 1db72bb
add promise support back for createReadStream
flakey5 ab72d14
Apply suggestions from code review
flakey5 917b530
check if onError was called
flakey5 2adae09
add docs
flakey5 0ed33e4
Merge branch 'main' into flakey5/3231
flakey5 b951bd5
Merge branch 'main' into flakey5/3231
metcoder95 eda939d
add errorCallback
flakey5 a43112a
Update types/cache-interceptor.d.ts
flakey5 684d044
use fake timers and cleanup client
flakey5 12c2ded
lazy cache wellknown headers
flakey5 af0875e
Merge branch 'main' into flakey5/3231
metcoder95 29b1362
Apply suggestions from code review
flakey5 9e4f15b
Update lib/cache/memory-cache-store.js
flakey5 a5a3cf3
code review
flakey5 8bf9864
Apply suggestions from code review
flakey5 baaa914
Apply suggestions from code review
flakey5 8bf6b42
code review pt2
flakey5 05109b1
Update lib/handler/cache-revalidation-handler.js
mcollina 092d9d4
Update lib/handler/cache-handler.js
mcollina 1b91475
Apply suggestions from code review
Uzlopak e6aeda9
fix
Uzlopak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| 'use strict' | ||
|
|
||
| /** | ||
| * @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore | ||
| * @implements {CacheStore} | ||
| */ | ||
| class MemoryCacheStore { | ||
| /** | ||
| * @type {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts} opts | ||
| */ | ||
| #opts = {} | ||
flakey5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /** | ||
| * @type {Map<string, import('../../types/cache-interceptor.d.ts').default.CacheStoreValue[]>} | ||
| */ | ||
| #data = new Map() | ||
|
|
||
| /** | ||
| * @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined} opts | ||
| */ | ||
flakey5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor (opts) { | ||
| this.#opts = opts ?? {} | ||
|
|
||
| if (!this.#opts.maxEntrySize) { | ||
| this.#opts.maxEntrySize = Infinity | ||
flakey5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| get maxEntrySize () { | ||
| return this.#opts.maxEntrySize | ||
| } | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @param {import('../../types/dispatcher.d.ts').default.RequestOptions} req | ||
| * @returns {Promise<import('../../types/cache-interceptor.d.ts').default.CacheStoreValue | undefined>} | ||
| */ | ||
| get (req) { | ||
| const key = this.#makeKey(req) | ||
|
|
||
| const values = this.#data.get(key) | ||
| if (!values) { | ||
| return | ||
| } | ||
|
|
||
| let value | ||
| const now = Date.now() | ||
flakey5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (let i = values.length - 1; i >= 0; i--) { | ||
| const current = values[i] | ||
| if (now >= current.deleteAt) { | ||
| // Delete the expired ones | ||
| values.splice(i, 1) | ||
| continue | ||
| } | ||
|
|
||
| let matches = true | ||
|
|
||
| if (current.vary) { | ||
| for (const key in current.vary) { | ||
| if (current.vary[key] !== req.headers[key]) { | ||
flakey5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| matches = false | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (matches) { | ||
| value = current | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return value | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('../../types/dispatcher.d.ts').default.RequestOptions} req | ||
| * @param {import('../../types/cache-interceptor.d.ts').default.CacheStoreValue} value | ||
| */ | ||
| put (req, value) { | ||
| const key = this.#makeKey(req) | ||
|
|
||
| let values = this.#data.get(key) | ||
| if (!values) { | ||
| values = [] | ||
| this.#data.set(key, values) | ||
| } | ||
|
|
||
| values.push(value) | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('../../types/dispatcher.d.ts').default.RequestOptions} req | ||
| * @returns {string} | ||
| */ | ||
| #makeKey (req) { | ||
| // TODO origin is undefined | ||
flakey5 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // https://www.rfc-editor.org/rfc/rfc9111.html#section-2-3 | ||
| return `${req.origin}:${req.path}:${req.method}` | ||
| } | ||
| } | ||
|
|
||
| module.exports = MemoryCacheStore | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.