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

fix(headerinterpreter): use custom interpreter #774

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"bcrypt": "^5.1.0",
"bluebird": "^3.7.2",
"body-parser": "^1.19.2",
"cache-parser": "^1.2.4",
"cloudmersive-virus-api-client": "^1.2.7",
"connect-session-sequelize": "^7.1.5",
"convict": "^6.2.4",
Expand Down
3 changes: 3 additions & 0 deletions src/services/api/AxiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import logger from "@logger/logger"
import { getAccessToken } from "@utils/token-retrieval-utils"
import tracer from "@utils/tracer"

import { customHeaderInterpreter } from "@root/utils/headerInterpreter"

// Env vars
const GITHUB_ORG_NAME = config.get("github.orgName")

Expand Down Expand Up @@ -79,6 +81,7 @@ const isomerRepoAxiosInstance = setupCache(
{
interpretHeader: true,
etag: true,
headerInterpreter: customHeaderInterpreter,
}
)
isomerRepoAxiosInstance.interceptors.request.use(requestFormatter)
Expand Down
47 changes: 47 additions & 0 deletions src/utils/headerInterpreter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Header, HeadersInterpreter } from "axios-cache-interceptor"
import { parse } from "cache-parser"

// NOTE: Taken with reference to https://github.com/arthurfiorette/axios-cache-interceptor/blob/v0.9.2/src/header/interpreter.ts
// The change made is to remove the `maxAge` condition, which is not suitable because
// we want to revalidate on update.
// An alternative is to invalidate the cache on `update` but
// this requires an exhaustive search through our codebase.
// This incurs an extra network call on every update, which is not ideal but in 304
// this is an empty request.
// eslint-disable-next-line import/prefer-default-export
export const customHeaderInterpreter: HeadersInterpreter = (headers) => {
if (!headers) return "not enough headers"

const cacheControl = headers[Header.CacheControl]

if (cacheControl) {
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(
String(cacheControl)
)

// Header told that this response should not be cached.
if (noCache || noStore) {
return "dont cache"
}

if (immutable) {
// 1 year is sufficient, as Infinity may cause more problems.
// It might not be the best way, but a year is better than none.
return 1000 * 60 * 60 * 24 * 365
}

// Already out of date, for cache can be saved, but must be requested again
if (mustRevalidate || maxAge) {
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
return 0
}
}

const expires = headers[Header.Expires]

if (expires) {
const milliseconds = Date.parse(String(expires)) - Date.now()
return milliseconds >= 0 ? milliseconds : "dont cache"
}

return "not enough headers"
}