61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
import "dotenv/config";
|
|
import { pathToFileURL } from "node:url";
|
|
|
|
const LOCAL_DATABASE_HOSTS = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
const POSTGRES_PROTOCOLS = new Set(["postgres:", "postgresql:"]);
|
|
const PROTECTED_ENVIRONMENTS = new Set(["production", "staging"]);
|
|
|
|
export function assertPortalLegacyCommerceRecutEnvironment(env = process.env) {
|
|
if (env.HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH !== "1") {
|
|
throw new Error(
|
|
"Set HIVEGRID_ALLOW_PORTAL_LEGACY_RECUT_DB_PUSH=1 for the explicit local/CI feature-branch recut",
|
|
);
|
|
}
|
|
|
|
const nodeEnvironment = env.NODE_ENV?.trim().toLowerCase();
|
|
const sentryEnvironment = env.SENTRY_ENVIRONMENT?.trim().toLowerCase();
|
|
if (
|
|
(nodeEnvironment && PROTECTED_ENVIRONMENTS.has(nodeEnvironment)) ||
|
|
(sentryEnvironment && PROTECTED_ENVIRONMENTS.has(sentryEnvironment))
|
|
) {
|
|
throw new Error("The VA-P26a feature-branch recut is forbidden in staging and production");
|
|
}
|
|
|
|
const databaseUrl = env.DIRECT_URL || env.DATABASE_URL;
|
|
if (!databaseUrl) {
|
|
throw new Error("DIRECT_URL or DATABASE_URL is required for the VA-P26a recut");
|
|
}
|
|
|
|
let parsedUrl;
|
|
try {
|
|
parsedUrl = new URL(databaseUrl);
|
|
} catch {
|
|
throw new Error("The VA-P26a recut database URL is invalid");
|
|
}
|
|
|
|
if (!POSTGRES_PROTOCOLS.has(parsedUrl.protocol)) {
|
|
throw new Error("The VA-P26a recut requires a PostgreSQL database URL");
|
|
}
|
|
|
|
if (!LOCAL_DATABASE_HOSTS.has(parsedUrl.hostname)) {
|
|
throw new Error("The VA-P26a recut is restricted to a loopback local/CI database");
|
|
}
|
|
|
|
return Object.freeze({ hostname: parsedUrl.hostname });
|
|
}
|
|
|
|
function isDirectExecution() {
|
|
return Boolean(process.argv[1]) && import.meta.url === pathToFileURL(process.argv[1]).href;
|
|
}
|
|
|
|
if (isDirectExecution()) {
|
|
try {
|
|
assertPortalLegacyCommerceRecutEnvironment();
|
|
process.stdout.write("OK VA-P26a local/CI legacy-commerce recut guard\n");
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "unknown recut guard failure";
|
|
process.stderr.write(`FAIL ${message}\n`);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|