-
Notifications
You must be signed in to change notification settings - Fork 565
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
[http-cache] follow up #3733
[http-cache] follow up #3733
Changes from all commits
c65844d
10755d3
9a8b0b4
bc5cfe7
310c2e5
a5fd832
85727cf
bff7c42
6a099bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,48 +4,40 @@ const util = require('../core/util') | |
const CacheHandler = require('../handler/cache-handler') | ||
const MemoryCacheStore = require('../cache/memory-cache-store') | ||
const CacheRevalidationHandler = require('../handler/cache-revalidation-handler') | ||
const { UNSAFE_METHODS, assertCacheStoreType } = require('../util/cache.js') | ||
const { assertCacheStore, assertCacheMethods } = require('../util/cache.js') | ||
|
||
const AGE_HEADER = Buffer.from('age') | ||
|
||
/** | ||
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions | undefined} globalOpts | ||
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [opts] | ||
* @returns {import('../../types/dispatcher.d.ts').default.DispatcherComposeInterceptor} | ||
*/ | ||
module.exports = globalOpts => { | ||
if (!globalOpts) { | ||
globalOpts = {} | ||
module.exports = (opts = {}) => { | ||
const { | ||
store = new MemoryCacheStore(), | ||
methods = ['GET'] | ||
} = opts | ||
|
||
if (typeof opts !== 'object' || opts === null) { | ||
throw new TypeError(`expected type of opts to be an Object, got ${opts === null ? 'null' : typeof opts}`) | ||
} | ||
|
||
if (globalOpts.store) { | ||
assertCacheStoreType(globalOpts.store) | ||
} else { | ||
globalOpts.store = new MemoryCacheStore() | ||
} | ||
|
||
if (globalOpts.methods) { | ||
if (!Array.isArray(globalOpts.methods)) { | ||
throw new TypeError(`methods needs to be an array, got ${typeof globalOpts.methods}`) | ||
} | ||
assertCacheStore(store, 'opts.store') | ||
assertCacheMethods(methods, 'opts.methods') | ||
|
||
if (globalOpts.methods.length === 0) { | ||
throw new Error('methods must have at least one method in it') | ||
} | ||
} else { | ||
globalOpts.methods = ['GET'] | ||
const globalOpts = { | ||
store, | ||
methods | ||
} | ||
|
||
// Safe methods the user wants and unsafe methods | ||
const methods = [...globalOpts.methods, ...UNSAFE_METHODS] | ||
Comment on lines
-38
to
-39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did we "mixed" with unsafe methods? this would result in caching unsafe requests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this allows requests with unsafe methods to get to the cache handler, where they'll cause a cache purge if the response is a success |
||
|
||
return dispatch => { | ||
return (opts, handler) => { | ||
if (!opts.origin || !methods.includes(opts.method)) { | ||
// Not a method we want to cache or we don't have the origin, skip | ||
return dispatch(opts, handler) | ||
} | ||
|
||
const stream = globalOpts.store.createReadStream(opts) | ||
const stream = store.createReadStream(opts) | ||
if (!stream) { | ||
// Request isn't cached | ||
return dispatch(opts, new CacheHandler(globalOpts, opts, handler)) | ||
|
@@ -169,7 +161,7 @@ module.exports = globalOpts => { | |
respondWithCachedValue(stream, value) | ||
} | ||
|
||
Promise.resolve(stream).then(handleStream).catch(err => handler.onError(err)) | ||
Promise.resolve(stream).then(handleStream).catch(handler.onError) | ||
|
||
return true | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WHy did we check for unsafe methods? this whole if condition seems actually kind of dubious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flakey5
The invalidation logic is kind of strange. Also what is the point of passing methods, when they are ignored anyway? So I guess actually methods would have been an array of safe methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I guess my proposed change should be right, as the passed methods will be considered safe methods. Especially in regard of the last sentence in the spec, that methods whose safety is unknown is also covered.