|
| 1 | +import { existsSync, readFileSync } from 'node:fs'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +const ENV_PATH = resolve(process.cwd(), '.env'); |
| 5 | + |
| 6 | +const parseEnvLine = (line) => { |
| 7 | + const trimmed = line.trim(); |
| 8 | + if (!trimmed || trimmed.startsWith('#')) { |
| 9 | + return null; |
| 10 | + } |
| 11 | + |
| 12 | + const separatorIndex = trimmed.indexOf('='); |
| 13 | + if (separatorIndex < 0) { |
| 14 | + return null; |
| 15 | + } |
| 16 | + |
| 17 | + const key = trimmed.slice(0, separatorIndex).trim(); |
| 18 | + let value = trimmed.slice(separatorIndex + 1).trim(); |
| 19 | + |
| 20 | + if ( |
| 21 | + (value.startsWith('"') && value.endsWith('"')) || |
| 22 | + (value.startsWith("'") && value.endsWith("'")) |
| 23 | + ) { |
| 24 | + value = value.slice(1, -1); |
| 25 | + } |
| 26 | + |
| 27 | + return { key, value }; |
| 28 | +}; |
| 29 | + |
| 30 | +const loadDotEnv = () => { |
| 31 | + if (!existsSync(ENV_PATH)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const contents = readFileSync(ENV_PATH, 'utf-8'); |
| 36 | + contents.split(/\r?\n/).forEach((line) => { |
| 37 | + const parsed = parseEnvLine(line); |
| 38 | + if (!parsed || process.env[parsed.key] !== undefined) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + process.env[parsed.key] = parsed.value; |
1 | 43 | import dotenv from 'dotenv'; |
2 | 44 | import { z } from 'zod'; |
3 | 45 |
|
@@ -28,8 +70,55 @@ if (!result.success) { |
28 | 70 | result.error.errors.forEach((err) => { |
29 | 71 | console.error(`- ${err.path.join('.')}: ${err.message}`); |
30 | 72 | }); |
| 73 | +}; |
| 74 | + |
| 75 | +const isIntegerString = (value) => typeof value === 'string' && /^\d+$/.test(value); |
| 76 | + |
| 77 | +const validateEnv = () => { |
| 78 | + const errors = []; |
| 79 | + const { VITE_SUPABASE_URL, VITE_SUPABASE_ANON_KEY } = process.env; |
| 80 | + |
| 81 | + if (!VITE_SUPABASE_URL) { |
| 82 | + errors.push('VITE_SUPABASE_URL is required'); |
| 83 | + } else { |
| 84 | + try { |
| 85 | + new URL(VITE_SUPABASE_URL); |
| 86 | + } catch { |
| 87 | + errors.push('VITE_SUPABASE_URL must be a valid URL'); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (!VITE_SUPABASE_ANON_KEY || VITE_SUPABASE_ANON_KEY.length < 10) { |
| 92 | + errors.push('VITE_SUPABASE_ANON_KEY is required and must be at least 10 characters'); |
| 93 | + } |
| 94 | + |
| 95 | + const numericKeys = [ |
| 96 | + 'AUTH_TOKEN_TTL_SECONDS', |
| 97 | + 'LOGIN_RATE_LIMIT_MAX_ATTEMPTS', |
| 98 | + 'LOGIN_RATE_LIMIT_WINDOW_MS', |
| 99 | + 'LOGIN_RATE_LIMIT_BLOCK_MS', |
| 100 | + 'REDIS_PORT', |
| 101 | + ]; |
| 102 | + |
| 103 | + numericKeys.forEach((key) => { |
| 104 | + const value = process.env[key]; |
| 105 | + if (value !== undefined && !isIntegerString(value)) { |
| 106 | + errors.push(`${key} must be an integer string`); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + if (process.env.AUTH_TOKEN_SECRET && process.env.AUTH_TOKEN_SECRET.length < 16) { |
| 111 | + errors.push('AUTH_TOKEN_SECRET must be at least 16 characters when provided'); |
| 112 | + } |
| 113 | + |
| 114 | + if (errors.length > 0) { |
| 115 | + console.error('\n❌ Invalid environment configuration:\n'); |
| 116 | + errors.forEach((error) => console.error(`- ${error}`)); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | +}; |
31 | 120 |
|
32 | | - process.exit(1); |
33 | | -} |
| 121 | +loadDotEnv(); |
| 122 | +validateEnv(); |
34 | 123 |
|
35 | | -export default result.data; |
| 124 | +export default process.env; |
0 commit comments