Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

perf: hoist fireIdentifyForUser outside onRequest hook closure

Devin AI89b0e55eec45
diff --git a/backend/src/server/plugins/auth/inject-identity.ts b/backend/src/server/plugins/auth/inject-identity.ts
index f07a70fecb..10b562231f 100644
--- a/backend/src/server/plugins/auth/inject-identity.ts
+++ b/backend/src/server/plugins/auth/inject-identity.ts
@@ -130,6 +130,19 @@ export const injectIdentity = fp(
async (server: FastifyZodProvider, opt: { shouldForwardWritesToPrimaryInstance?: boolean }) => {
server.decorateRequest("auth", null);
server.decorateRequest("shouldForwardWritesToPrimaryInstance", Boolean(opt.shouldForwardWritesToPrimaryInstance));
+
+ // Hoisted outside onRequest hook to avoid per-request function allocation on this hot path
+ const fireIdentifyForUser = (user: TUsers) => {
+ const distinctId = user.username ?? user.email ?? "";
+ if (distinctId) {
+ void server.services.telemetry.identifyUser(distinctId, {
+ email: user.email ?? undefined,
+ username: user.username,
+ userId: user.id
+ });
+ }
+ };
+
server.addHook("onRequest", async (req) => {
const appCfg = getConfig();
@@ -164,17 +177,6 @@ export const injectIdentity = fp(
if (!authMode) return;
- const fireIdentifyForUser = (user: TUsers) => {
- const distinctId = user.username ?? user.email ?? "";
- if (distinctId) {
- void server.services.telemetry.identifyUser(distinctId, {
- email: user.email ?? undefined,
- username: user.username,
- userId: user.id
- });
- }
- };
-
switch (authMode) {
case AuthMode.JWT: {
const { user, tokenVersionId, orgId, orgName, rootOrgId, parentOrgId } =