File tree Expand file tree Collapse file tree 2 files changed +13
-8
lines changed Expand file tree Collapse file tree 2 files changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import * as grpc from "@grpc/grpc-js";
14
14
import { isFgaChecksEnabled , isFgaWritesEnabled } from "./authorizer" ;
15
15
import { base64decode } from "@jmondi/oauth2-server" ;
16
16
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" ;
18
18
import { ApplicationError , ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
19
19
20
20
async function tryThree < T > ( errMessage : string , code : ( attempt : number ) => Promise < T > ) : Promise < T > {
@@ -242,10 +242,10 @@ interface ZedTokenCache {
242
242
// "contribute" a cache shape to the request context
243
243
type ZedTokenCacheType = StoredZedToken ;
244
244
const ctxCacheSetZedToken = ( zedToken : StoredZedToken | undefined ) : void => {
245
- ctxSetCache < ZedTokenCacheType > ( "zedToken" , zedToken ) ;
245
+ ctxTrySetCache < ZedTokenCacheType > ( "zedToken" , zedToken ) ;
246
246
} ;
247
247
const ctxCacheGetZedToken = ( ) : StoredZedToken | undefined => {
248
- return ctxGetCache < ZedTokenCacheType > ( "zedToken" ) ;
248
+ return ctxTryGetCache < ZedTokenCacheType > ( "zedToken" ) ;
249
249
} ;
250
250
251
251
/**
Original file line number Diff line number Diff line change @@ -109,17 +109,22 @@ export function ctxCheckAborted() {
109
109
110
110
/** Encode cache keys in type to avoid clashes at compile time */
111
111
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 ;
114
114
}
115
115
116
116
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
+
118
123
if ( typeof value === "function" ) {
119
- const prev = ctxGetCache < T > ( key ) ;
124
+ const prev = ctxTryGetCache < T > ( key ) ;
120
125
value = value ( prev ) ;
121
126
}
122
- ctxGet ( ) . cache [ key ] = value ;
127
+ cache [ key ] = value ;
123
128
}
124
129
125
130
export type RequestContextSeed = Omit < RequestContext , "requestId" | "startTime" | "cache" > & {
You can’t perform that action at this time.
0 commit comments