Skip to content

Commit 2a2fbc8

Browse files
author
Marvin Zhang
committed
refactor: enhance SSL configuration handling for Vercel deployment
1 parent d8cb9cc commit 2a2fbc8

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ POSTGRES_URL="postgresql://username:password@host:5432/database"
2323

2424
# PostgreSQL-specific options
2525
# POSTGRES_SSL="false" # SSL configuration: false, true, or JSON object
26+
# # false = no SSL
27+
# # true = SSL with self-signed cert support (Vercel compatible)
28+
# # '{"rejectUnauthorized":false}' = custom SSL config
2629
# POSTGRES_CONNECTION_TIMEOUT="15000" # Connection timeout in milliseconds
2730
# POSTGRES_IDLE_TIMEOUT="30000" # Idle connection timeout in milliseconds
2831
# POSTGRES_MAX_CONNECTIONS="20" # Maximum number of connections in pool
@@ -172,6 +175,9 @@ NODE_ENV="development"
172175
# POSTGRES_PRISMA_URL="postgres://default:[email protected]:5432/verceldb?pgbouncer=true&connect_timeout=15"
173176
# POSTGRES_URL_NON_POOLING="postgres://default:[email protected]:5432/verceldb"
174177

178+
# For Vercel deployment, SSL is automatically configured to handle self-signed certificates
179+
# No need to set POSTGRES_SSL explicitly - it defaults to secure settings for production
180+
175181
## ======== LOCAL DEVELOPMENT ========
176182
# For local development with Vercel Postgres, run:
177183
# vercel env pull .env.local

docs/guides/VERCEL_DEPLOYMENT.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ No configuration files needed! 🎉
9393
- Check that environment variable is properly formatted
9494
- For local testing, ensure `.env.local` contains the database URL
9595

96+
### SSL Certificate Errors (self-signed certificate in certificate chain)
97+
If you encounter SSL certificate errors in Vercel deployment:
98+
- This is automatically handled by the updated SSL configuration
99+
- The system defaults to SSL with self-signed certificate support in production
100+
- If needed, you can override by setting `POSTGRES_SSL="false"` in Vercel environment variables
101+
- For custom SSL configuration, set `POSTGRES_SSL='{"rejectUnauthorized":false,"ca":"..."}'`
102+
96103
### Auto-Detection Not Working
97104
- Check console logs for database detection messages
98105
- Ensure environment variable names match exactly: `POSTGRES_URL`, `MYSQL_URL`, `SQLITE_URL`

packages/core/src/utils/typeorm-config.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,40 @@ export interface TypeORMStorageOptions {
3131
// General options
3232
synchronize?: boolean;
3333
logging?: boolean;
34-
ssl?: boolean;
34+
ssl?: boolean | object;
3535
}
3636

3737
// Singleton DataSource instance
3838
let singletonDataSource: DataSource | null = null;
3939
let initializationPromise: Promise<DataSource> | null = null;
4040

41+
/**
42+
* Parse SSL configuration from environment variable
43+
*/
44+
function parseSSLConfig(sslEnvVar?: string): boolean | object {
45+
if (!sslEnvVar) {
46+
// Default SSL config for production (Vercel-compatible)
47+
return process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false;
48+
}
49+
50+
// Handle boolean strings
51+
if (sslEnvVar.toLowerCase() === 'false') {
52+
return false;
53+
}
54+
if (sslEnvVar.toLowerCase() === 'true') {
55+
// Use Vercel-compatible SSL config for true
56+
return { rejectUnauthorized: false };
57+
}
58+
59+
// Try to parse as JSON object
60+
try {
61+
return JSON.parse(sslEnvVar);
62+
} catch {
63+
// Fallback to Vercel-compatible SSL config
64+
return { rejectUnauthorized: false };
65+
}
66+
}
67+
4168
/**
4269
* Get or create the singleton DataSource instance
4370
* All services should use this to ensure they share the same database connection
@@ -175,7 +202,7 @@ export function parseTypeORMConfig(): TypeORMStorageOptions {
175202
url: postgresUrl,
176203
synchronize: process.env.NODE_ENV === 'development',
177204
logging: process.env.NODE_ENV === 'development',
178-
ssl: process.env.NODE_ENV === 'production',
205+
ssl: parseSSLConfig(process.env.POSTGRES_SSL),
179206
};
180207
}
181208

@@ -218,7 +245,7 @@ export function parseTypeORMConfig(): TypeORMStorageOptions {
218245
url: postgresUrl,
219246
synchronize: process.env.NODE_ENV === 'development',
220247
logging: process.env.NODE_ENV === 'development',
221-
ssl: process.env.NODE_ENV === 'production',
248+
ssl: parseSSLConfig(process.env.POSTGRES_SSL),
222249
};
223250
}
224251

packages/web/vercel.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"NODE_ENV": "production"
4+
}
5+
}

0 commit comments

Comments
 (0)