telemetry-policy.ts 731 B

1234567891011121314151617181920212223
  1. import { getConfig, type TelemetryConfig } from './config.js';
  2. export type TelemetryChannel = keyof TelemetryConfig;
  3. const TELEMETRY_OPT_OUT_ENV = ['MCODE_DISABLE_TELEMETRY', 'DO_NOT_TRACK'] as const;
  4. /**
  5. * True only when the channel is explicitly opted in and no global opt-out is set.
  6. * An unreadable config never authorizes an upload.
  7. */
  8. export function isTelemetryChannelEnabled(
  9. channel: TelemetryChannel,
  10. readConfigured: () => boolean | undefined = () => getConfig().telemetry[channel],
  11. ): boolean {
  12. for (const key of TELEMETRY_OPT_OUT_ENV) {
  13. if (/^(?:1|true|yes|on)$/iu.test(process.env[key]?.trim() ?? '')) return false;
  14. }
  15. try {
  16. return readConfigured() === true;
  17. } catch {
  18. return false;
  19. }
  20. }