umami/lib/session.ts

131 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-31 22:42:03 +01:00
import { Session, Team, Website } from '@prisma/client';
import cache from 'lib/cache';
2022-11-09 00:50:34 +01:00
import clickhouse from 'lib/clickhouse';
2022-12-31 22:42:03 +01:00
import { secret, uuid } from 'lib/crypto';
2023-01-02 08:20:14 +01:00
import { getClientInfo } from 'lib/detect';
2022-12-31 22:42:03 +01:00
import { parseToken } from 'next-basics';
import { createSession, getSession, getWebsite } from 'queries';
2020-08-09 08:48:43 +02:00
2023-01-02 08:20:14 +01:00
export async function findSession(
req,
payload,
): Promise<{
id: string;
websiteId: string;
hostname: string;
browser: string;
os: string;
device: string;
screen: string;
language: string;
country: string;
}> {
2022-11-01 07:42:37 +01:00
const { website: websiteId, hostname, screen, language } = payload;
2022-08-26 08:12:47 +02:00
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
2022-11-01 07:42:37 +01:00
const sessionId = uuid(websiteId, hostname, ip, userAgent);
2020-08-12 05:05:40 +02:00
2022-11-09 02:11:08 +01:00
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
2023-01-02 08:20:14 +01:00
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
2022-11-09 00:50:34 +01:00
};
}
2022-11-09 02:11:08 +01:00
// Find session
2022-12-31 22:42:03 +01:00
let session: Session;
2022-11-09 02:11:08 +01:00
if (cache.enabled) {
session = await cache.fetchSession(sessionId);
} else {
session = await getSession({ id: sessionId });
}
// Create a session if not found
if (!session) {
try {
session = await createSession({
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
});
2022-12-31 22:42:03 +01:00
} catch (e: any) {
2022-11-09 02:11:08 +01:00
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
2023-01-02 08:20:14 +01:00
return session;
}
export async function useSessionCache(req: any): Promise<{
id: string;
websiteId: string;
hostname: string;
browser: string;
os: string;
device: string;
screen: string;
language: string;
country: string;
}> {
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
if (result) {
return result;
}
}
return null;
}
export async function findWebsite(websiteId: string) {
let website: Website & { team?: Team } = null;
if (cache.enabled) {
website = await cache.fetchWebsite(websiteId);
} else {
website = await getWebsite({ id: websiteId }, true);
}
if (!website || website.deletedAt) {
throw new Error(`Website not found: ${websiteId}`);
}
return website;
}
export async function isOverApiLimit(website) {
const userId = website.userId ? website.userId : website.team.teamu.userId;
const limit = await cache.fetchCollectLimit(userId);
// To-do: Need to implement logic to find user-specific limit. Defaulted to 10k.
if (limit > 10000) {
return true;
}
await cache.incrementCollectLimit(userId);
return false;
2020-08-05 07:45:05 +02:00
}