Skip to content

Commit

Permalink
Merge pull request #108 from sebringrose/cascade-fix
Browse files Browse the repository at this point in the history
fix: stop recursive cascade at response
  • Loading branch information
sejori authored Dec 31, 2022
2 parents 6d1ee15 + c1824ec commit 4b1dd6d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
6 changes: 2 additions & 4 deletions middleware/cacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ export const cacher = (cache: ResponseCache): Middleware => async (ctx, next) =>
})
}

// else respond 200 clone of response - one-use original lives in cache
ctx.state.responseFromCache = true
return cacheItem.value.clone()
}

// update cache asynchronously to not block process before return
const response = await next()
if (!response) return

cache.set(key, response.clone())
return response.clone()
const newCacheItem = cache.set(key, response.clone())
return newCacheItem.value.clone()
}
6 changes: 4 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import Server from "./server.ts"
export default Server
export * from "./server.ts"
// alias for traditional Router setup
export const Router = Server

// Handlers
export * from "./handlers/static.ts"
Expand All @@ -20,5 +22,5 @@ export * from "./middleware/logger.ts"
// Utils
export * from "./utils/Crypto.ts"
export * from "./utils/ResponseCache.ts"
// export * from "./utils/Cascade.ts" // <-- not likely to be needed by users & clutters docs
// export * from "./utils/Promisify.ts" // <-- not likely to be needed by users & clutters docs
// export * from "./utils/Cascade.ts"
// ^ not needed by users & clutters docs
7 changes: 2 additions & 5 deletions utils/Cascade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ export class Cascade {

try {
const result = await fcnPromise(this.ctx, async () => await this.run(this.toCall[++this.called]))
if (result) {
this.response = result
} else {
await this.run(this.toCall[++this.called])
}
if (result) this.response = result
if (!this.response) await this.run(this.toCall[++this.called])
} catch (error) {
throw error
}
Expand Down

0 comments on commit 4b1dd6d

Please sign in to comment.