Skip to content

Commit

Permalink
feat(cache): add shouldCache parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbonnet committed May 29, 2022
1 parent 75a97fa commit f156eee
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Buffer } from 'buffer'

const { isPromise } = types

export function cache({ store }) {
export function cache({ store, shouldCache = defaultShouldCache }) {
return async function cache(request, next) {
if (await store.has(request)) {
const cache = await store.get(request)
Expand All @@ -15,7 +15,7 @@ export function cache({ store }) {
return await request.respond(cache)
}
const response = await next(request)
if (response.cache) {
if (shouldCache(response)) {
const tube = new PassThrough()
response.tube = tube
await store.set(request, {
Expand All @@ -29,6 +29,10 @@ export function cache({ store }) {
}
}

function defaultShouldCache(request) {
return request.cache
}

function buffer(tube) {
return new Promise(function (resolve, reject) {
const chunks = []
Expand Down

0 comments on commit f156eee

Please sign in to comment.