@@ -3,6 +3,7 @@ import type { YogaInitialContext } from "graphql-yoga";
33import * as jose from "jose" ;
44import type { DbService } from "../db/db.service" ;
55import type { MetaEnvelope } from "../db/types" ;
6+ import { newTraceId , timed } from "../utils/timing" ;
67
78export type VaultContext = YogaInitialContext & {
89 currentUser : string | null ;
@@ -51,17 +52,26 @@ export class VaultAccessGuard {
5152 const now = Date . now ( ) ;
5253 let cached = jwksCache . get ( jwksUrl ) ;
5354 if ( ! cached || cached . expiresAt <= now ) {
54- const jwksResponse = await axios . get ( jwksUrl , {
55- timeout : JWKS_FETCH_TIMEOUT_MS ,
56- } ) ;
55+ const jwksResponse = await timed (
56+ "guard.validateToken.fetchJWKS" ,
57+ ( ) =>
58+ axios . get ( jwksUrl , {
59+ timeout : JWKS_FETCH_TIMEOUT_MS ,
60+ } ) ,
61+ ) ;
5762 cached = {
5863 jwks : jose . createLocalJWKSet ( jwksResponse . data ) ,
5964 expiresAt : now + JWKS_TTL_MS ,
6065 } ;
6166 jwksCache . set ( jwksUrl , cached ) ;
67+ } else {
68+ console . log ( "[timing] guard.validateToken.jwksCacheHit" ) ;
6269 }
6370
64- const { payload } = await jose . jwtVerify ( token , cached . jwks ) ;
71+ const { payload } = await timed (
72+ "guard.validateToken.jwtVerify" ,
73+ ( ) => jose . jwtVerify ( token , cached ! . jwks ) ,
74+ ) ;
6575
6676 return payload ;
6777 } catch ( error ) {
@@ -242,6 +252,13 @@ export class VaultAccessGuard {
242252 ) => Promise < any > ,
243253 ) {
244254 return async ( parent : T , args : Args , context : VaultContext ) => {
255+ const traceId = newTraceId ( "op" ) ;
256+ const opKind = args . id
257+ ? "id-targeted"
258+ : args . envelopeId
259+ ? "envelope-targeted"
260+ : "bulk" ;
261+ console . log ( `[timing] ${ traceId } guard.middleware.begin ${ opKind } ` ) ;
245262 // Check if this is storeMetaEnvelope operation (has input with ontology, payload, acl)
246263 const isStoreOperation =
247264 args . input &&
@@ -252,12 +269,20 @@ export class VaultAccessGuard {
252269 ! args . id ; // storeMetaEnvelope doesn't have id, updateMetaEnvelopeById does
253270
254271 // CRITICAL: Validate authentication BEFORE executing any resolver
255- await this . validateAuthentication ( context , isStoreOperation ) ;
272+ await timed (
273+ "guard.validateAuthentication" ,
274+ ( ) => this . validateAuthentication ( context , isStoreOperation ) ,
275+ traceId ,
276+ ) ;
256277
257278 // For operations that don't require a specific meta envelope ID (bulk queries)
258279 if ( ! args . id && ! args . envelopeId ) {
259280 // Authentication validated, now execute resolver
260- const result = await resolver ( parent , args , context ) ;
281+ const result = await timed (
282+ "guard.resolver(bulk)" ,
283+ ( ) => resolver ( parent , args , context ) ,
284+ traceId ,
285+ ) ;
261286
262287 // If the result is an array
263288 if ( Array . isArray ( result ) ) {
@@ -282,20 +307,29 @@ export class VaultAccessGuard {
282307 const metaEnvelopeId = args . id || args . envelopeId ;
283308 if ( ! metaEnvelopeId ) {
284309 // Authentication validated, now execute resolver
285- const result = await resolver ( parent , args , context ) ;
310+ const result = await timed (
311+ "guard.resolver(no-id)" ,
312+ ( ) => resolver ( parent , args , context ) ,
313+ traceId ,
314+ ) ;
286315 return this . filterACL ( result ) ;
287316 }
288317
289318 // Check if envelope exists and user has access
290- const { hasAccess, exists } = await this . checkAccess (
291- metaEnvelopeId ,
292- context ,
319+ const { hasAccess, exists } = await timed (
320+ "guard.checkAccess" ,
321+ ( ) => this . checkAccess ( metaEnvelopeId , context ) ,
322+ traceId ,
293323 ) ;
294324
295325 // For update operations with input, allow in-place creation if envelope doesn't exist
296326 if ( ! exists && args . input ) {
297327 // Envelope doesn't exist for this eName - allow in-place creation
298- const result = await resolver ( parent , args , context ) ;
328+ const result = await timed (
329+ "guard.resolver(in-place-create)" ,
330+ ( ) => resolver ( parent , args , context ) ,
331+ traceId ,
332+ ) ;
299333 return this . filterACL ( result ) ;
300334 }
301335
@@ -309,7 +343,11 @@ export class VaultAccessGuard {
309343 }
310344
311345 // Execute resolver and filter ACL
312- const result = await resolver ( parent , args , context ) ;
346+ const result = await timed (
347+ "guard.resolver(targeted)" ,
348+ ( ) => resolver ( parent , args , context ) ,
349+ traceId ,
350+ ) ;
313351
314352 // If result is null (envelope not found), return null
315353 if ( result === null ) {
0 commit comments