One build per environment: PROD_* variables for the production artifact, TEST_* for the test build and the CSP check. The build fails when a variable is empty. Tests use the production values from config/monitoringTestEnv.ts. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
97 lines
3.5 KiB
TypeScript
97 lines
3.5 KiB
TypeScript
// Instellingen voor Sentry en Umami (monitoring-plan M1/M2). De waarden zijn
|
|
// openbaar (ze staan in de browser van elke bezoeker) maar komen niet meer uit
|
|
// de code: de pipeline geeft ze per omgeving mee aan de build, uit de Gitea-
|
|
// variabelen {TEST|PROD}_SENTRY_DSN, _UMAMI_SCRIPT_URL en _UMAMI_WEBSITE_ID,
|
|
// als VITE_SENTRY_DSN, VITE_UMAMI_SCRIPT_URL, VITE_UMAMI_WEBSITE_ID en
|
|
// VITE_APP_ENV ('test' of 'production'). Eén build hoort dus bij één omgeving.
|
|
// Geen imports uit Vite: config/securityHeaders.ts leest dit bestand ook in Node.
|
|
|
|
export type MonitoringEnvironment = 'test' | 'production';
|
|
|
|
export interface MonitoringConfig {
|
|
environment: MonitoringEnvironment;
|
|
sentryDsn: string;
|
|
umamiScriptUrl: string;
|
|
umamiWebsiteId: string;
|
|
}
|
|
|
|
export type Monitoring = MonitoringConfig;
|
|
|
|
// De host waarop de build van een omgeving meet
|
|
const hostnameByEnvironment: Record<MonitoringEnvironment, string> = {
|
|
production: 'andreaslop.nl',
|
|
test: 'test.andreaslop.nl',
|
|
};
|
|
|
|
// In de Vite-build staan de waarden in import.meta.env (statisch vervangen,
|
|
// daarom elke naam los); de buildscripts onder Node lezen process.env.
|
|
const fromProcess = (name: string): unknown =>
|
|
typeof process === 'undefined' ? undefined : process.env[name];
|
|
|
|
function rawSettings(): Record<keyof MonitoringConfig, unknown> {
|
|
const env = import.meta.env;
|
|
return {
|
|
environment: env?.VITE_APP_ENV ?? fromProcess('VITE_APP_ENV'),
|
|
sentryDsn: env?.VITE_SENTRY_DSN ?? fromProcess('VITE_SENTRY_DSN'),
|
|
umamiScriptUrl:
|
|
env?.VITE_UMAMI_SCRIPT_URL ?? fromProcess('VITE_UMAMI_SCRIPT_URL'),
|
|
umamiWebsiteId:
|
|
env?.VITE_UMAMI_WEBSITE_ID ?? fromProcess('VITE_UMAMI_WEBSITE_ID'),
|
|
};
|
|
}
|
|
|
|
const isHttps = (value: string): boolean => {
|
|
try {
|
|
return new URL(value).protocol === 'https:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// De instellingen van deze build, of null als er iets ontbreekt of ongeldig is
|
|
// (dan meet de site niets: liever geen meting dan een kapotte)
|
|
export function monitoringConfig(
|
|
raw: Record<keyof MonitoringConfig, unknown> = rawSettings(),
|
|
): MonitoringConfig | null {
|
|
const { environment, sentryDsn, umamiScriptUrl, umamiWebsiteId } = raw;
|
|
if (environment !== 'test' && environment !== 'production') return null;
|
|
const text = (value: unknown) =>
|
|
typeof value === 'string' ? value.trim() : '';
|
|
const dsn = text(sentryDsn);
|
|
const script = text(umamiScriptUrl);
|
|
const website = text(umamiWebsiteId);
|
|
if (!isHttps(dsn) || !isHttps(script) || website === '') return null;
|
|
return {
|
|
environment,
|
|
sentryDsn: dsn,
|
|
umamiScriptUrl: script,
|
|
umamiWebsiteId: website,
|
|
};
|
|
}
|
|
|
|
// Onbekende host (localhost, Lighthouse CI, previews, de andere omgeving):
|
|
// niets meten (M1)
|
|
export function monitoringFor(
|
|
hostname: string,
|
|
config: MonitoringConfig | null = monitoringConfig(),
|
|
): Monitoring | null {
|
|
if (!config || hostname !== hostnameByEnvironment[config.environment]) {
|
|
return null;
|
|
}
|
|
return { ...config };
|
|
}
|
|
|
|
// De herkomsten voor de CSP: Umami voor script-src en connect-src, de Sentry-
|
|
// ingest voor connect-src. Leeg zonder meting.
|
|
export function monitoringOrigins(
|
|
config: MonitoringConfig | null = monitoringConfig(),
|
|
): { umami: string[]; sentry: string[] } {
|
|
if (!config) return { umami: [], sentry: [] };
|
|
return {
|
|
umami: [new URL(config.umamiScriptUrl).origin],
|
|
sentry: [new URL(config.sentryDsn).origin],
|
|
};
|
|
}
|
|
|
|
export const monitoredHostnames = Object.values(hostnameByEnvironment);
|