From 18874405b921022f8df9d086f9b2da48f82dedee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Sun, 9 Feb 2025 21:56:58 +0100 Subject: [PATCH] fix(api): swift sdk has issues with double OpenPanel tags in user-agent, if so remove one --- apps/api/src/hooks/fix.hook.ts | 25 +++++++++++++++++++++++++ apps/api/src/index.ts | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 apps/api/src/hooks/fix.hook.ts diff --git a/apps/api/src/hooks/fix.hook.ts b/apps/api/src/hooks/fix.hook.ts new file mode 100644 index 00000000..31562e9e --- /dev/null +++ b/apps/api/src/hooks/fix.hook.ts @@ -0,0 +1,25 @@ +import type { + FastifyReply, + FastifyRequest, + HookHandlerDoneFunction, +} from 'fastify'; + +export function fixHook( + request: FastifyRequest, + reply: FastifyReply, + done: HookHandlerDoneFunction, +) { + const ua = request.headers['user-agent']; + // Swift SDK issue: https://github.com/Openpanel-dev/swift-sdk/commit/d588fa761a36a33f3b78eb79d83bfd524e3c7144 + if (ua) { + const regex = /OpenPanel\/(\d+\.\d+\.\d+)\sOpenPanel\/(\d+\.\d+\.\d+)/; + const match = ua.match(regex); + if (match) { + request.headers['user-agent'] = ua.replace( + regex, + `OpenPanel/${match[1]}`, + ); + } + } + done(); +} diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index f842084b..5de46c23 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -24,6 +24,7 @@ import { healthcheck, healthcheckQueue, } from './controllers/healthcheck.controller'; +import { fixHook } from './hooks/fix.hook'; import { ipHook } from './hooks/ip.hook'; import { requestIdHook } from './hooks/request-id.hook'; import { requestLoggingHook } from './hooks/request-logging.hook'; @@ -93,6 +94,7 @@ const startServer = async () => { fastify.addHook('preHandler', ipHook); fastify.addHook('preHandler', timestampHook); + fastify.addHook('preHandler', fixHook); fastify.addHook('onRequest', requestIdHook); fastify.addHook('onResponse', requestLoggingHook);