move all logic in event.controller to worker (speed up request)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-02 15:57:36 +02:00
parent aed62c59e4
commit a877c44324
12 changed files with 3145 additions and 359 deletions

View File

@@ -0,0 +1,58 @@
import { stripTrailingSlash } from '@openpanel/common';
import referrers from '../referrers';
function getHostname(url: string | undefined) {
if (!url) {
return '';
}
try {
return new URL(url).hostname;
} catch (e) {
return '';
}
}
export function parseReferrer(url: string | undefined) {
const hostname = getHostname(url);
const match = referrers[hostname] ?? referrers[hostname.replace('www.', '')];
return {
name: match?.name ?? '',
type: match?.type ?? 'unknown',
url: stripTrailingSlash(url ?? ''),
};
}
export function getReferrerWithQuery(
query: Record<string, string> | undefined
) {
if (!query) {
return null;
}
const source = query.utm_source ?? query.ref ?? query.utm_referrer ?? '';
if (source === '') {
return null;
}
const match = Object.values(referrers).find(
(referrer) => referrer.name.toLowerCase() === source.toLowerCase()
);
if (match) {
return {
name: match.name,
type: match.type,
url: '',
};
}
return {
name: source,
type: 'unknown',
url: '',
};
}

View File

@@ -1,54 +1,23 @@
export function getOS(ua?: string) {
if (!ua) {
return null;
}
if (/iPad/i.test(ua)) {
return 'iPad';
}
if (/iPhone/i.test(ua)) {
return 'iPhone';
}
if (/iPod/i.test(ua)) {
return 'iPod';
}
if (/Macintosh/i.test(ua)) {
return 'macOS';
}
if (/IEMobile|Windows/i.test(ua)) {
return 'Windows';
}
if (/Android/i.test(ua)) {
return 'Android';
}
if (/BlackBerry/i.test(ua)) {
return 'BlackBerry';
}
if (/EF500/i.test(ua)) {
return 'Bluebird';
}
if (/CrOS/i.test(ua)) {
return 'Chrome OS';
}
if (/DL-AXIS/i.test(ua)) {
return 'Datalogic';
}
if (/CT50/i.test(ua)) {
return 'Honeywell';
}
if (/TC70|TC55/i.test(ua)) {
return 'Zebra';
}
if (/Linux/i.test(ua)) {
return 'Generic Linux';
}
return 'Unknown';
import { UAParser } from 'ua-parser-js';
export function isUserAgentSet(ua: string) {
return ua !== 'node' && ua !== 'undici' && !!ua;
}
export function getDevice(ua?: string) {
if (!ua) {
return null;
}
export function parseUserAgent(ua: string) {
const res = new UAParser(ua).getResult();
return {
os: res.os.name,
osVersion: res.os.version,
browser: res.browser.name,
browserVersion: res.browser.version,
device: res.device.type ?? getDevice(ua),
brand: res.device.vendor,
model: res.device.model,
};
}
export function getDevice(ua: string) {
const t1 =
/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
ua

View File

@@ -0,0 +1,48 @@
export function parseSearchParams(
params: URLSearchParams
): Record<string, string> | undefined {
const result: Record<string, string> = {};
for (const [key, value] of params.entries()) {
result[key] = value;
}
return Object.keys(result).length ? result : undefined;
}
export function parsePath(path?: string): {
query?: Record<string, string>;
path: string;
hash?: string;
} {
if (!path) {
return {
path: '',
};
}
try {
const url = new URL(path);
return {
query: parseSearchParams(url.searchParams),
path: url.pathname,
hash: url.hash || undefined,
};
} catch (error) {
return {
path,
};
}
}
export function isSameDomain(
url1: string | undefined,
url2: string | undefined
) {
if (!url1 || !url2) {
return false;
}
try {
return new URL(url1).hostname === new URL(url2).hostname;
} catch (e) {
return false;
}
}