Skip to content

Commit

Permalink
Always create new object if it's a top level context
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Jul 25, 2024
1 parent fab4914 commit cd327ea
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions library/agent/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,25 @@ export function runWithContext<T>(context: Context, fn: () => T) {
return fn();
}

// `attackDetected`, `consumedRateLimitForIP` and `consumedRateLimitForUser`
// are carried over if there's an existing context
// Only in case of a top level context, we should reset them first
// This is more for tests than for real usage
delete context.cache;
delete context.attackDetected;
delete context.consumedRateLimitForIP;
delete context.consumedRateLimitForUser;
// We need to create a new context without `attackDetected`, `consumedRateLimitForIP`, ...
const topLevelContext: Context = {
url: context.url,
method: context.method,
query: context.query,
headers: context.headers,
routeParams: context.routeParams,
remoteAddress: context.remoteAddress,
body: context.body,
cookies: context.cookies,
source: context.source,
route: context.route,
graphql: context.graphql,
xml: context.xml,
subdomains: context.subdomains,
};

// If there's no context yet, we create a new context and run the function with it
return ContextStorage.run(context, fn);
return ContextStorage.run(topLevelContext, fn);
}

/**
Expand Down

0 comments on commit cd327ea

Please sign in to comment.