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 @@ -116,17 +116,22 @@ export function ctxSignal() {
116
116
117
117
/** Encode cache keys in type to avoid clashes at compile time */
118
118
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 ;
121
121
}
122
122
123
123
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
+
125
130
if ( typeof value === "function" ) {
126
- const prev = ctxGetCache < T > ( key ) ;
131
+ const prev = ctxTryGetCache < T > ( key ) ;
127
132
value = value ( prev ) ;
128
133
}
129
- ctxGet ( ) . cache [ key ] = value ;
134
+ cache [ key ] = value ;
130
135
}
131
136
132
137
export type RequestContextSeed = Omit < RequestContext , "requestId" | "startTime" | "cache" > & {
You can’t perform that action at this time.
0 commit comments