mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-06 04:19:20 +00:00
Revert "perf: lazy load create telemetry (#5007)"
This reverts commit 0385c5e611.
This commit is contained in:
@@ -1,85 +0,0 @@
|
||||
import { ENV, getBooleanEnvVar, isTest } from "../utils/env";
|
||||
import { getProjectId } from "./project-id";
|
||||
import type { BetterAuthOptions } from "../types";
|
||||
import { detectEnvironment, detectRuntime } from "./detectors/detect-runtime";
|
||||
import { detectDatabase } from "./detectors/detect-database";
|
||||
import { detectFramework } from "./detectors/detect-framework";
|
||||
import { detectSystemInfo } from "./detectors/detect-system-info";
|
||||
import { detectPackageManager } from "./detectors/detect-project-info";
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import type { TelemetryContext, TelemetryEvent } from "./types";
|
||||
import { logger } from "../utils";
|
||||
import { getTelemetryAuthConfig } from "./detectors/detect-auth-config";
|
||||
|
||||
export async function createTelemetry(
|
||||
options: BetterAuthOptions,
|
||||
context?: TelemetryContext,
|
||||
) {
|
||||
const debugEnabled =
|
||||
options.telemetry?.debug ||
|
||||
getBooleanEnvVar("BETTER_AUTH_TELEMETRY_DEBUG", false);
|
||||
|
||||
const TELEMETRY_ENDPOINT = ENV.BETTER_AUTH_TELEMETRY_ENDPOINT;
|
||||
const track = async (event: TelemetryEvent) => {
|
||||
try {
|
||||
if (context?.customTrack) {
|
||||
await context.customTrack(event);
|
||||
} else {
|
||||
if (debugEnabled) {
|
||||
await Promise.resolve(
|
||||
logger.info("telemetry event", JSON.stringify(event, null, 2)),
|
||||
);
|
||||
} else {
|
||||
await betterFetch(TELEMETRY_ENDPOINT, {
|
||||
method: "POST",
|
||||
body: event,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
};
|
||||
|
||||
const isEnabled = async () => {
|
||||
const telemetryEnabled =
|
||||
options.telemetry?.enabled !== undefined
|
||||
? options.telemetry.enabled
|
||||
: false;
|
||||
const envEnabled = getBooleanEnvVar("BETTER_AUTH_TELEMETRY", false);
|
||||
return (
|
||||
(envEnabled || telemetryEnabled) && (context?.skipTestCheck || !isTest())
|
||||
);
|
||||
};
|
||||
|
||||
const enabled = await isEnabled();
|
||||
let anonymousId: string | undefined;
|
||||
|
||||
if (enabled) {
|
||||
anonymousId = await getProjectId(options.baseURL);
|
||||
|
||||
const payload = {
|
||||
config: getTelemetryAuthConfig(options),
|
||||
runtime: detectRuntime(),
|
||||
database: await detectDatabase(),
|
||||
framework: await detectFramework(),
|
||||
environment: detectEnvironment(),
|
||||
systemInfo: await detectSystemInfo(),
|
||||
packageManager: detectPackageManager(),
|
||||
};
|
||||
|
||||
void track({ type: "init", payload, anonymousId });
|
||||
}
|
||||
|
||||
return {
|
||||
publish: async (event: TelemetryEvent) => {
|
||||
if (!enabled) return;
|
||||
if (!anonymousId) {
|
||||
anonymousId = await getProjectId(options.baseURL);
|
||||
}
|
||||
await track({
|
||||
type: event.type,
|
||||
payload: event.payload,
|
||||
anonymousId,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,14 +1,85 @@
|
||||
let lazyImportCreateTelemetry: Promise<
|
||||
typeof import("./create-telemetry").createTelemetry
|
||||
> | null = null;
|
||||
// lazy load the telemetry module to split the bundle and avoid loading unnecessary code
|
||||
export const createTelemetry: typeof import("./create-telemetry").createTelemetry =
|
||||
async (...args) => {
|
||||
if (!lazyImportCreateTelemetry) {
|
||||
lazyImportCreateTelemetry = import("./create-telemetry").then(
|
||||
(mod) => mod.createTelemetry,
|
||||
);
|
||||
}
|
||||
const createTelemetry = await lazyImportCreateTelemetry;
|
||||
return createTelemetry(...args);
|
||||
import { ENV, getBooleanEnvVar, isTest } from "../utils/env";
|
||||
import { getProjectId } from "./project-id";
|
||||
import type { BetterAuthOptions } from "../types";
|
||||
import { detectEnvironment, detectRuntime } from "./detectors/detect-runtime";
|
||||
import { detectDatabase } from "./detectors/detect-database";
|
||||
import { detectFramework } from "./detectors/detect-framework";
|
||||
import { detectSystemInfo } from "./detectors/detect-system-info";
|
||||
import { detectPackageManager } from "./detectors/detect-project-info";
|
||||
import { betterFetch } from "@better-fetch/fetch";
|
||||
import type { TelemetryContext, TelemetryEvent } from "./types";
|
||||
import { logger } from "../utils";
|
||||
import { getTelemetryAuthConfig } from "./detectors/detect-auth-config";
|
||||
|
||||
export async function createTelemetry(
|
||||
options: BetterAuthOptions,
|
||||
context?: TelemetryContext,
|
||||
) {
|
||||
const debugEnabled =
|
||||
options.telemetry?.debug ||
|
||||
getBooleanEnvVar("BETTER_AUTH_TELEMETRY_DEBUG", false);
|
||||
|
||||
const TELEMETRY_ENDPOINT = ENV.BETTER_AUTH_TELEMETRY_ENDPOINT;
|
||||
const track = async (event: TelemetryEvent) => {
|
||||
try {
|
||||
if (context?.customTrack) {
|
||||
await context.customTrack(event);
|
||||
} else {
|
||||
if (debugEnabled) {
|
||||
await Promise.resolve(
|
||||
logger.info("telemetry event", JSON.stringify(event, null, 2)),
|
||||
);
|
||||
} else {
|
||||
await betterFetch(TELEMETRY_ENDPOINT, {
|
||||
method: "POST",
|
||||
body: event,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
};
|
||||
|
||||
const isEnabled = async () => {
|
||||
const telemetryEnabled =
|
||||
options.telemetry?.enabled !== undefined
|
||||
? options.telemetry.enabled
|
||||
: false;
|
||||
const envEnabled = getBooleanEnvVar("BETTER_AUTH_TELEMETRY", false);
|
||||
return (
|
||||
(envEnabled || telemetryEnabled) && (context?.skipTestCheck || !isTest())
|
||||
);
|
||||
};
|
||||
|
||||
const enabled = await isEnabled();
|
||||
let anonymousId: string | undefined;
|
||||
|
||||
if (enabled) {
|
||||
anonymousId = await getProjectId(options.baseURL);
|
||||
|
||||
const payload = {
|
||||
config: getTelemetryAuthConfig(options),
|
||||
runtime: detectRuntime(),
|
||||
database: await detectDatabase(),
|
||||
framework: await detectFramework(),
|
||||
environment: detectEnvironment(),
|
||||
systemInfo: await detectSystemInfo(),
|
||||
packageManager: detectPackageManager(),
|
||||
};
|
||||
|
||||
void track({ type: "init", payload, anonymousId });
|
||||
}
|
||||
|
||||
return {
|
||||
publish: async (event: TelemetryEvent) => {
|
||||
if (!enabled) return;
|
||||
if (!anonymousId) {
|
||||
anonymousId = await getProjectId(options.baseURL);
|
||||
}
|
||||
await track({
|
||||
type: event.type,
|
||||
payload: event.payload,
|
||||
anonymousId,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createTelemetry } from "./create-telemetry";
|
||||
import { createTelemetry } from "./index";
|
||||
import type { TelemetryEvent } from "./types";
|
||||
|
||||
vi.mock("@better-fetch/fetch", () => ({
|
||||
|
||||
Reference in New Issue
Block a user