Skip to content

Commit 04c23c3

Browse files
committed
fix
1 parent 96b7a02 commit 04c23c3

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

components/server/src/authorization/spicedb-authorizer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as grpc from "@grpc/grpc-js";
1414
import { isFgaChecksEnabled, isFgaWritesEnabled } from "./authorizer";
1515
import { base64decode } from "@jmondi/oauth2-server";
1616
import { DecodedZedToken } from "@gitpod/spicedb-impl/lib/impl/v1/impl.pb";
17-
import { ctxGetCache, ctxSetCache } from "../util/request-context";
17+
import { ctxTryGetCache, ctxTrySetCache } from "../util/request-context";
1818
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1919

2020
async function tryThree<T>(errMessage: string, code: (attempt: number) => Promise<T>): Promise<T> {
@@ -242,10 +242,10 @@ interface ZedTokenCache {
242242
// "contribute" a cache shape to the request context
243243
type ZedTokenCacheType = StoredZedToken;
244244
const ctxCacheSetZedToken = (zedToken: StoredZedToken | undefined): void => {
245-
ctxSetCache<ZedTokenCacheType>("zedToken", zedToken);
245+
ctxTrySetCache<ZedTokenCacheType>("zedToken", zedToken);
246246
};
247247
const ctxCacheGetZedToken = (): StoredZedToken | undefined => {
248-
return ctxGetCache<ZedTokenCacheType>("zedToken");
248+
return ctxTryGetCache<ZedTokenCacheType>("zedToken");
249249
};
250250

251251
/**

components/server/src/util/request-context.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,22 @@ export function ctxCheckAborted() {
109109

110110
/** Encode cache keys in type to avoid clashes at compile time */
111111
type CacheKey = "zedToken";
112-
export function ctxGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
113-
return ctxGet().cache[key] || d;
112+
export function ctxTryGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
113+
return ctxTryGet()?.cache[key] || d;
114114
}
115115

116116
type UpdateCache<T> = (prev: T | undefined) => T | undefined;
117-
export function ctxSetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
117+
export function ctxTrySetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
118+
const cache = ctxTryGet()?.cache;
119+
if (!cache) {
120+
return;
121+
}
122+
118123
if (typeof value === "function") {
119-
const prev = ctxGetCache<T>(key);
124+
const prev = ctxTryGetCache<T>(key);
120125
value = value(prev);
121126
}
122-
ctxGet().cache[key] = value;
127+
cache[key] = value;
123128
}
124129

125130
export type RequestContextSeed = Omit<RequestContext, "requestId" | "startTime" | "cache"> & {

0 commit comments

Comments
 (0)