File size: 4,663 Bytes
062a991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const productionMode = process.argv.includes("--production") || process.env.APP_ENV === "production" || process.env.NODE_ENV === "production";
const errors = [];
const warnings = [];
const checked = [];

function value(name) {
  return process.env[name]?.trim() ?? "";
}

function addError(key, message) {
  errors.push({ key, message });
}

function addWarning(key, message) {
  warnings.push({ key, message });
}

function checkRequired(name, description = "is required") {
  checked.push(name);
  const current = value(name);
  if (!current) addError(name, `${name} ${description}.`);
  return current;
}

function looksPlaceholder(current) {
  return /replace|placeholder|change-me|changeme|example\.com|example\.test/i.test(current);
}

function checkSecret(name) {
  const current = checkRequired(name, "must be configured with at least 32 characters");
  if (!current) return;
  if (current.length < 32) addError(name, `${name} must be at least 32 characters.`);
  if (looksPlaceholder(current)) addError(name, `${name} still looks like a placeholder.`);
}

function checkUrl(name, { httpsRequired = false } = {}) {
  const current = checkRequired(name, "must be a valid URL");
  if (!current) return;
  try {
    const parsed = new URL(current);
    if (httpsRequired && parsed.protocol !== "https:") addError(name, `${name} must use https in production.`);
  } catch {
    addError(name, `${name} must be a valid URL.`);
  }
}

function checkD1Storage() {
  const proxyUrl = value("LINKEDIN_D1_PROXY_URL");
  const sqlitePath = value("LINKEDIN_SQLITE_PATH");
  checked.push("LINKEDIN_D1_PROXY_URL", "LINKEDIN_SQLITE_PATH");
  if (productionMode && !proxyUrl) {
    addError("LINKEDIN_D1_PROXY_URL", "LINKEDIN_D1_PROXY_URL is required in production.");
    return;
  }
  if (!proxyUrl && !sqlitePath) {
    addError("LINKEDIN_D1_PROXY_URL", "Configure LINKEDIN_D1_PROXY_URL or LINKEDIN_SQLITE_PATH.");
    return;
  }
  if (proxyUrl) {
    try {
      const parsed = new URL(proxyUrl);
      if (!/^https?:$/.test(parsed.protocol)) addError("LINKEDIN_D1_PROXY_URL", "LINKEDIN_D1_PROXY_URL must use http or https.");
      if (parsed.username || parsed.password) addError("LINKEDIN_D1_PROXY_URL", "LINKEDIN_D1_PROXY_URL must not contain credentials.");
    } catch {
      addError("LINKEDIN_D1_PROXY_URL", "LINKEDIN_D1_PROXY_URL must be a valid URL.");
    }
  }
}

function checkStripeTestMode() {
  const secret = checkRequired("STRIPE_SECRET_KEY", "must be a Stripe Test Mode secret key");
  if (secret && !secret.startsWith("sk_test_")) addError("STRIPE_SECRET_KEY", "STRIPE_SECRET_KEY must start with sk_test_.");
  if (secret.startsWith("sk-production-")) addError("STRIPE_SECRET_KEY", "Live Stripe keys are not allowed for LinkedIn.");

  const priceId = checkRequired("STRIPE_PREMIUM_PRICE_ID", "must be a Stripe price id");
  if (priceId && !priceId.startsWith("price_")) addError("STRIPE_PREMIUM_PRICE_ID", "STRIPE_PREMIUM_PRICE_ID must start with price_.");

  const webhookSecret = checkRequired("STRIPE_WEBHOOK_SECRET", "must be configured for signed webhooks");
  if (webhookSecret && !webhookSecret.startsWith("whsec_")) addError("STRIPE_WEBHOOK_SECRET", "STRIPE_WEBHOOK_SECRET must start with whsec_.");
}

function checkEmail() {
  const mode = checkRequired("EMAIL_DELIVERY_MODE", "must be smtp in production");
  if (productionMode && mode !== "smtp") addError("EMAIL_DELIVERY_MODE", "EMAIL_DELIVERY_MODE must be smtp in production.");
  if (mode === "smtp" || productionMode) {
    checkRequired("MAIL_FROM");
    checkRequired("SMTP_HOST");
    checkRequired("SMTP_PORT");
    const user = value("SMTP_USER");
    const pass = value("SMTP_PASS");
    checked.push("SMTP_USER", "SMTP_PASS");
    if (user && !pass) addError("SMTP_PASS", "SMTP_PASS is required when SMTP_USER is set.");
  }
}

checkD1Storage();
checkSecret("SESSION_SECRET");
checkSecret("EMAIL_VERIFICATION_SECRET");
checkUrl("APP_URL", { httpsRequired: productionMode });
checkRequired("DEFAULT_TENANT_SLUG");
checkEmail();
checkStripeTestMode();

if (productionMode) {
  checked.push("SESSION_COOKIE_SECURE", "APP_ENV");
  if (value("SESSION_COOKIE_SECURE") !== "true") addError("SESSION_COOKIE_SECURE", "SESSION_COOKIE_SECURE must be true in production.");
  if (value("APP_ENV") !== "production") addWarning("APP_ENV", "APP_ENV is not production; --production made this a strict production preflight.");
}

const result = {
  ok: errors.length === 0,
  mode: productionMode ? "production" : "development",
  checked: Array.from(new Set(checked)).sort(),
  errors,
  warnings,
};

console.log(JSON.stringify(result, null, 2));
process.exitCode = result.ok ? 0 : 1;