Skip to content

Commit 7a3e041

Browse files
fix: fix caching logic
1 parent 08b9067 commit 7a3e041

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/lib/server/github-cache.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export type GitHubRelease = Awaited<
1818
>["data"][number];
1919

2020
type KeyType = "releases" | "descriptions" | "issue" | "pr";
21-
type CachedValue<T> = { value: T; cache: boolean };
2221

2322
export type ItemDetails = {
2423
comments: Awaited<ReturnType<Issues["listComments"]>>["data"];
@@ -144,10 +143,6 @@ export class GitHubCache {
144143
* @private
145144
*/
146145
#processCached<RType extends Parameters<InstanceType<typeof Redis>["json"]["set"]>[2]>() {
147-
function isCachedValue<T>(item: T | CachedValue<T>): item is CachedValue<T> {
148-
return typeof item === "object" && item !== null && "value" in item && "cache" in item;
149-
}
150-
151146
/**
152147
* Inner currying function to circumvent unsupported partial inference
153148
*
@@ -161,9 +156,7 @@ export class GitHubCache {
161156
return async <PromiseType>(
162157
cacheKey: string,
163158
promise: () => Promise<PromiseType>,
164-
transformer: (
165-
from: Awaited<PromiseType>
166-
) => RType | CachedValue<RType> | Promise<RType> | Promise<CachedValue<RType>>,
159+
transformer: (from: Awaited<PromiseType>) => RType | Promise<RType>,
167160
ttl: number | ((value: RType) => number | undefined) | undefined = undefined
168161
): Promise<RType> => {
169162
const cachedValue = await this.#redis.json.get<RType>(cacheKey);
@@ -174,24 +167,17 @@ export class GitHubCache {
174167

175168
console.log(`Cache miss for ${cacheKey}`);
176169

177-
let newValue = await transformer(await promise());
178-
let wantsCache = true;
179-
if (isCachedValue(newValue)) {
180-
wantsCache = newValue.cache;
181-
newValue = newValue.value;
182-
}
170+
const newValue = await transformer(await promise());
183171

184-
if (wantsCache) {
185-
await this.#redis.json.set(cacheKey, "$", newValue);
186-
if (ttl !== undefined) {
187-
if (typeof ttl === "function") {
188-
const ttlResult = ttl(newValue);
189-
if (ttlResult !== undefined) {
190-
await this.#redis.expire(cacheKey, ttlResult);
191-
}
192-
} else {
193-
await this.#redis.expire(cacheKey, ttl);
172+
await this.#redis.json.set(cacheKey, "$", newValue);
173+
if (ttl !== undefined) {
174+
if (typeof ttl === "function") {
175+
const ttlResult = ttl(newValue);
176+
if (ttlResult !== undefined) {
177+
await this.#redis.expire(cacheKey, ttlResult);
194178
}
179+
} else {
180+
await this.#redis.expire(cacheKey, ttl);
195181
}
196182
}
197183

@@ -633,10 +619,12 @@ export class GitHubCache {
633619
* @returns the deprecation status message if any, `false` otherwise
634620
*/
635621
async getPackageDeprecation(packageName: string) {
636-
return await this.#processCached<string | false>()(
622+
return await this.#processCached<{ value: string | false }>()(
637623
this.#getPackageKey(packageName, "deprecation"),
638624
async () => {
639625
try {
626+
// npmjs.org in a GitHub cache, I know, but hey, let's put that under the fact that
627+
// GitHub owns npmjs.org okay??
640628
const res = await fetch(`https://registry.npmjs.org/${packageName}/latest`);
641629
if (res.status !== 200) return {};
642630
return (await res.json()) as { deprecated?: boolean | string };
@@ -646,12 +634,12 @@ export class GitHubCache {
646634
}
647635
},
648636
({ deprecated }) => {
649-
if (deprecated === undefined) return false;
637+
if (deprecated === undefined) return { value: false };
650638
if (typeof deprecated === "boolean")
651-
return { value: "This package is deprecated", cache: false };
652-
return { value: deprecated || "This package is deprecated", cache: false };
639+
return { value: deprecated && "This package is deprecated" };
640+
return { value: deprecated || "This package is deprecated" };
653641
},
654-
item => (item === false ? DEPRECATIONS_TTL : undefined)
642+
item => (item.value === false ? DEPRECATIONS_TTL : undefined)
655643
);
656644
}
657645
}

src/lib/server/package-discoverer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class PackageDiscoverer {
6767
] ??
6868
descriptions["package.json"] ??
6969
"",
70-
deprecated: (await this.#cache.getPackageDeprecation(pkg)) || undefined
70+
deprecated: (await this.#cache.getPackageDeprecation(pkg)).value || undefined
7171
};
7272
})
7373
)

0 commit comments

Comments
 (0)