mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +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<
|
import { ENV, getBooleanEnvVar, isTest } from "../utils/env";
|
||||||
typeof import("./create-telemetry").createTelemetry
|
import { getProjectId } from "./project-id";
|
||||||
> | null = null;
|
import type { BetterAuthOptions } from "../types";
|
||||||
// lazy load the telemetry module to split the bundle and avoid loading unnecessary code
|
import { detectEnvironment, detectRuntime } from "./detectors/detect-runtime";
|
||||||
export const createTelemetry: typeof import("./create-telemetry").createTelemetry =
|
import { detectDatabase } from "./detectors/detect-database";
|
||||||
async (...args) => {
|
import { detectFramework } from "./detectors/detect-framework";
|
||||||
if (!lazyImportCreateTelemetry) {
|
import { detectSystemInfo } from "./detectors/detect-system-info";
|
||||||
lazyImportCreateTelemetry = import("./create-telemetry").then(
|
import { detectPackageManager } from "./detectors/detect-project-info";
|
||||||
(mod) => mod.createTelemetry,
|
import { betterFetch } from "@better-fetch/fetch";
|
||||||
);
|
import type { TelemetryContext, TelemetryEvent } from "./types";
|
||||||
}
|
import { logger } from "../utils";
|
||||||
const createTelemetry = await lazyImportCreateTelemetry;
|
import { getTelemetryAuthConfig } from "./detectors/detect-auth-config";
|
||||||
return createTelemetry(...args);
|
|
||||||
|
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 { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { createTelemetry } from "./create-telemetry";
|
import { createTelemetry } from "./index";
|
||||||
import type { TelemetryEvent } from "./types";
|
import type { TelemetryEvent } from "./types";
|
||||||
|
|
||||||
vi.mock("@better-fetch/fetch", () => ({
|
vi.mock("@better-fetch/fetch", () => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user