Skip to content

Commit 3624764

Browse files
committed
fix
1 parent 04a6032 commit 3624764

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
@@ -116,17 +116,22 @@ export function ctxSignal() {
116116

117117
/** Encode cache keys in type to avoid clashes at compile time */
118118
type CacheKey = "zedToken";
119-
export function ctxGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
120-
return ctxGet().cache[key] || d;
119+
export function ctxTryGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
120+
return ctxTryGet()?.cache[key] || d;
121121
}
122122

123123
type UpdateCache<T> = (prev: T | undefined) => T | undefined;
124-
export function ctxSetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
124+
export function ctxTrySetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
125+
const cache = ctxTryGet()?.cache;
126+
if (!cache) {
127+
return;
128+
}
129+
125130
if (typeof value === "function") {
126-
const prev = ctxGetCache<T>(key);
131+
const prev = ctxTryGetCache<T>(key);
127132
value = value(prev);
128133
}
129-
ctxGet().cache[key] = value;
134+
cache[key] = value;
130135
}
131136

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

0 commit comments

Comments
 (0)