🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor GitHubStarButton and enhance server utilities
Bill Yangf0440f62be69
diff --git a/docs/src/components/GitHubStarButton.jsx b/docs/src/components/GitHubStarButton.jsxindex afee354b1..8f9d8702a 100644--- a/docs/src/components/GitHubStarButton.jsx+++ b/docs/src/components/GitHubStarButton.jsx@@ -10,9 +10,7 @@ export function GitHubStarButton() {useEffect(() => {const fetchStarCount = async () => {try {- const response = await fetch(- "https://api.github.com/repos/rybbit-io/rybbit"- );+ const response = await fetch("https://api.github.com/repos/rybbit-io/rybbit");const data = await response.json();if (data.stargazers_count) {@@ -37,15 +35,11 @@ export function GitHubStarButton() {rel="noopener noreferrer"className="inline-flex items-center gap-1 bg-neutral-800/70 hover:bg-neutral-700/70 border border-neutral-600/70 hover:border-neutral-700/80 text-white px-3 py-1.5 rounded-full text-xs font-semibold transition-all duration-300 hover:-translate-y-1 shadow-xl shadow-neutral-900/50 hover:shadow-2xl hover:shadow-neutral-800/60 backdrop-blur-sm ring-1 ring-white/10 hover:ring-white/20">- <svg- className="w-3.5 h-3.5 mr-1"- fill="currentColor"- viewBox="0 0 24 24"- >+ <svg className="w-3.5 h-3.5 mr-1" fill="currentColor" viewBox="0 0 24 24"><path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.30.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" /></svg><span>Star us!</span>- <div className="flex items-center gap-1 ml-1 mr-0.5 py-0.5 bg-neutral-700/50 rounded-full text-xs">+ <div className="flex items-center gap-1 ml-1 mr-0.5 py-0.5 rounded-full text-xs"><Star className="w-3 h-3 text-yellow-400 fill-current" />{isLoading ? (<div className="w-7.5 h-3.5 bg-neutral-600 rounded-sm animate-pulse"></div>diff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.tsindex be5a34970..65ff3489d 100644--- a/server/src/api/sessionReplay/recordSessionReplay.ts+++ b/server/src/api/sessionReplay/recordSessionReplay.ts@@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from "fastify";import { z } from "zod";import { SessionReplayIngestService } from "../../services/replay/sessionReplayIngestService.js";import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";+import { getIpAddress } from "../../utils.js";const recordSessionReplaySchema = z.object({userId: z.string(),@@ -10,7 +11,7 @@ const recordSessionReplaySchema = z.object({type: z.union([z.string(), z.number()]),data: z.any(),timestamp: z.number(),- })+ }),),metadata: z.object({@@ -27,13 +28,11 @@ export async function recordSessionReplay(Params: { site: string };Body: RecordSessionReplayRequest;}>,- reply: FastifyReply+ reply: FastifyReply,) {try {const siteId = Number(request.params.site);- const body = recordSessionReplaySchema.parse(- request.body- ) as RecordSessionReplayRequest;+ const body = recordSessionReplaySchema.parse(request.body) as RecordSessionReplayRequest;// Extract request metadata for trackingconst userAgent = request.headers["user-agent"] || "";@@ -58,24 +57,3 @@ export async function recordSessionReplay(return reply.status(500).send({ error: "Internal server error" });}}--// Helper function to get IP address-const getIpAddress = (request: FastifyRequest): string => {- const cfConnectingIp = request.headers["cf-connecting-ip"];- if (cfConnectingIp && typeof cfConnectingIp === "string") {- return cfConnectingIp.trim();- }-- const forwardedFor = request.headers["x-forwarded-for"];- if (forwardedFor && typeof forwardedFor === "string") {- const ips = forwardedFor- .split(",")- .map((ip) => ip.trim())- .filter(Boolean);- if (ips.length > 0) {- return ips[ips.length - 1];- }- }-- return request.ip;-};diff --git a/server/src/tracker/utils.ts b/server/src/tracker/utils.tsindex 419f210b4..c4d5df935 100644--- a/server/src/tracker/utils.ts+++ b/server/src/tracker/utils.ts@@ -5,6 +5,7 @@ import { usageService } from "../services/usageService.js";import { userIdService } from "../services/userId/userIdService.js";import { trackingPayloadSchema } from "./trackEvent.js";import { TrackingPayload } from "./types.js";+import { getIpAddress } from "../utils.js";export type TotalTrackingPayload = TrackingPayload & {type?: string;@@ -49,9 +50,7 @@ export function getAllUrlParams(querystring: string): Record<string, string> {if (!querystring) return params;// If querystring starts with ?, remove it- const cleanQuerystring = querystring.startsWith("?")- ? querystring.substring(1)- : querystring;+ const cleanQuerystring = querystring.startsWith("?") ? querystring.substring(1) : querystring;try {const searchParams = new URLSearchParams(cleanQuerystring);@@ -92,11 +91,10 @@ export function isSiteOverLimit(siteId: number | string): boolean {export function createBasePayload(request: FastifyRequest,eventType: "pageview" | "custom_event" | "performance" | "error" = "pageview",- validatedBody: ValidatedTrackingPayload+ validatedBody: ValidatedTrackingPayload,): TotalTrackingPayload {// Use custom user agent if provided, otherwise fall back to header- const userAgent =- validatedBody.user_agent || request.headers["user-agent"] || "";+ const userAgent = validatedBody.user_agent || request.headers["user-agent"] || "";// Override IP if provided in payloadconst ipAddress = validatedBody.ip_address || getIpAddress(request);const siteId = validatedBody.site_id;@@ -123,27 +121,3 @@ export function createBasePayload(userId: userId,};}--// Helper function to get IP address-const getIpAddress = (request: FastifyRequest): string => {- // Priority 1: Cloudflare header (already validated by CF)- const cfConnectingIp = request.headers["cf-connecting-ip"];- if (cfConnectingIp && typeof cfConnectingIp === "string") {- return cfConnectingIp.trim();- }-- // Priority 2: X-Forwarded-For - just use the first IP- const forwardedFor = request.headers["x-forwarded-for"];- if (forwardedFor && typeof forwardedFor === "string") {- const ips = forwardedFor- .split(",")- .map((ip) => ip.trim())- .filter(Boolean);- if (ips.length > 0) {- // Always use the first IP - the original client- return ips[0];- }- }-- return request.ip;-};diff --git a/server/src/utils.ts b/server/src/utils.tsindex 1e3d24c90..a7df8b32a 100644--- a/server/src/utils.ts+++ b/server/src/utils.ts@@ -1,3 +1,4 @@+import { FastifyRequest } from "fastify";import { siteConfig } from "./lib/siteConfig.js";import * as psl from "psl";@@ -93,20 +94,9 @@ const tvOS = new Set([const gamingOS = new Set(["PlayStation", "Xbox", "Nintendo"]);-const embeddedOS = new Set([- "Windows IoT",- "Contiki",- "Raspbian",- "Morph OS",- "Pico",- "NetRange",-]);+const embeddedOS = new Set(["Windows IoT", "Contiki", "Raspbian", "Morph OS", "Pico", "NetRange"]);-export function getDeviceType(- screenWidth: number,- screenHeight: number,- ua: UAParser.IResult-): string {+export function getDeviceType(screenWidth: number, screenHeight: number, ua: UAParser.IResult): string {if (ua.os.name) {if (desktopOS.has(ua.os.name)) {return "Desktop";@@ -213,3 +203,27 @@ export const normalizeOrigin = (input: string): string => {}}};++// Helper function to get IP address+export const getIpAddress = (request: FastifyRequest): string => {+ // Priority 1: Cloudflare header (already validated by CF)+ const cfConnectingIp = request.headers["cf-connecting-ip"];+ if (cfConnectingIp && typeof cfConnectingIp === "string") {+ return cfConnectingIp.trim();+ }++ // Priority 2: X-Forwarded-For - just use the first IP+ const forwardedFor = request.headers["x-forwarded-for"];+ if (forwardedFor && typeof forwardedFor === "string") {+ const ips = forwardedFor+ .split(",")+ .map((ip) => ip.trim())+ .filter(Boolean);+ if (ips.length > 0) {+ // Always use the first IP - the original client+ return ips[0];+ }+ }++ return request.ip;+};