MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / isBotHook

Function isBotHook

apps/api/src/hooks/is-bot.hook.ts:9–57  ·  view source on GitHub ↗
(
  req: FastifyRequest<{
    Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
  }>,
  reply: FastifyReply
)

Source from the content-addressed store, hash-verified

7import { isBot } from '@/bots';
8
9export async function isBotHook(
10 req: FastifyRequest<{
11 Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
12 }>,
13 reply: FastifyReply
14) {
15 // Requests authenticated with a client secret come from server-side SDKs
16 // (node, php, go, rust, java, python, …). That auth is a far stronger signal
17 // of legitimate first-party traffic than the user agent, so never treat them
18 // as bots — bot detection is for public/frontend (origin-authenticated)
19 // traffic.
20 if (req.clientSecretAuth) {
21 return;
22 }
23
24 const bot = req.headers['user-agent']
25 ? await isBot(req.headers['user-agent'])
26 : null;
27
28 if (bot && req.client?.projectId) {
29 if ('type' in req.body && req.body.type === 'track') {
30 const path = (req.body.payload.properties?.__path ||
31 req.body.payload.properties?.path) as string | undefined;
32 if (path) {
33 await createBotEvent({
34 ...bot,
35 projectId: req.client.projectId,
36 path: path ?? '',
37 createdAt: new Date(),
38 });
39 }
40 // Handle deprecated events (v1)
41 } else if ('name' in req.body && 'properties' in req.body) {
42 const path = (req.body.properties?.__path || req.body.properties?.path) as
43 | string
44 | undefined;
45 if (path) {
46 await createBotEvent({
47 ...bot,
48 projectId: req.client.projectId,
49 path: path ?? '',
50 createdAt: new Date(),
51 });
52 }
53 }
54
55 return reply.status(202).send({ bot });
56 }
57}

Callers 1

Calls 2

createBotEventFunction · 0.90
sendMethod · 0.45

Tested by

no test coverage detected