autotel
Version:
Write Once, Observe Anywhere
86 lines (84 loc) • 3.19 kB
JavaScript
import './chunk-DGUM43GV.js';
// src/enrichers.ts
function get(headers, key) {
const lower = key.toLowerCase();
for (const [k, v] of Object.entries(headers)) {
if (k.toLowerCase() === lower) {
return Array.isArray(v) ? v[0] : v;
}
}
return void 0;
}
var BROWSER_RE = /(Firefox|OPR|Edg|Chrome|Safari|MSIE|Trident)[\s/]?([\d.]*)/;
var OS_RE = /(Windows NT|Mac OS X|Linux|Android|iPhone OS|iPad|CrOS)[\s]?([\d._]*)/;
function parseBrowser(ua) {
const m = BROWSER_RE.exec(ua);
if (!m) return void 0;
const name = m[1] === "OPR" ? "Opera" : m[1] === "Edg" ? "Edge" : m[1] === "Trident" ? "IE" : m[1];
return m[2] ? `${name} ${m[2]}` : name;
}
function parseOS(ua) {
const m = OS_RE.exec(ua);
if (!m) return void 0;
const name = m[1] === "iPhone OS" ? "iOS" : m[1] === "Windows NT" ? "Windows" : m[1] === "Mac OS X" ? "macOS" : m[1];
const ver = m[2]?.replaceAll("_", ".") || void 0;
return ver ? `${name} ${ver}` : name;
}
function parseDevice(ua) {
if (/Mobi|Android.*Mobile|iPhone/.test(ua)) return "mobile";
if (/iPad|Android(?!.*Mobile)|Tablet/.test(ua)) return "tablet";
if (/Bot|Crawler|Spider|Lighthouse/i.test(ua)) return "bot";
return "desktop";
}
function userAgent(headers) {
const raw = get(headers, "user-agent");
if (!raw) return void 0;
const attrs = { "user_agent.raw": raw };
const browser = parseBrowser(raw);
if (browser) attrs["user_agent.browser"] = browser;
const os = parseOS(raw);
if (os) attrs["user_agent.os"] = os;
const device = parseDevice(raw);
if (device) attrs["user_agent.device"] = device;
return attrs;
}
function geo(headers) {
const country = get(headers, "x-vercel-ip-country") ?? get(headers, "cf-ipcountry");
const region = get(headers, "x-vercel-ip-country-region");
const city = get(headers, "x-vercel-ip-city");
const latitude = get(headers, "x-vercel-ip-latitude");
const longitude = get(headers, "x-vercel-ip-longitude");
if (!country && !region && !city && !latitude && !longitude) return void 0;
const attrs = {};
if (country) attrs["geo.country"] = country;
if (region) attrs["geo.region"] = region;
if (city) {
try {
attrs["geo.city"] = decodeURIComponent(city);
} catch {
attrs["geo.city"] = city;
}
}
if (latitude) attrs["geo.latitude"] = latitude;
if (longitude) attrs["geo.longitude"] = longitude;
return attrs;
}
var DIGITS_RE = /^\d+$/;
function parseContentLength(value) {
if (!value || !DIGITS_RE.test(value)) return void 0;
return Number(value);
}
function requestSize(requestHeaders, responseHeaders) {
const reqLen = get(requestHeaders, "content-length");
const resLen = responseHeaders ? get(responseHeaders, "content-length") : void 0;
if (!reqLen && !resLen) return void 0;
const attrs = {};
const reqBytes = parseContentLength(reqLen);
if (reqBytes !== void 0) attrs["http.request.body.size"] = reqBytes;
const resBytes = parseContentLength(resLen);
if (resBytes !== void 0) attrs["http.response.body.size"] = resBytes;
return Object.keys(attrs).length > 0 ? attrs : void 0;
}
export { geo, requestSize, userAgent };
//# sourceMappingURL=enrichers.js.map
//# sourceMappingURL=enrichers.js.map