Make PROJECT_CREDENTIALS and ROUTE_MAP parsing robust against wrapping quotes from hosting platforms; add clearer debug output

This commit is contained in:
oonyeje 2026-07-07 11:51:54 +00:00
parent bfe7051262
commit 4d01664787

View File

@ -38,6 +38,32 @@ interface RouteEntry {
type RouteMap = Record<string, RouteEntry>; type RouteMap = Record<string, RouteEntry>;
// ----- Helpers --------------------------------------------------------------
/**
* Normalize a raw env-var value. Some hosting platforms (Coolify included)
* wrap pasted JSON values in extra quotes when the user doesn't quote-escape
* properly, producing strings like:
* "{\"square\":{...}}"
* which JSON.parse cannot read at position 1.
*
* This helper:
* 1. Trims whitespace
* 2. Strips a single surrounding layer of `"` or `'`
* 3. Unescapes `\"` -> `"`, `\'` -> `'`, and `\\` -> `\`
*/
function normalizeEnvValue(raw: string): string {
let value = raw.trim();
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
value = value.replace(/\\"/g, '"').replace(/\\'/g, "'").replace(/\\\\/g, '\\');
}
return value.trim();
}
// ----- Config --------------------------------------------------------------- // ----- Config ---------------------------------------------------------------
const PORT = parseInt(process.env.PORT ?? '3012', 10); const PORT = parseInt(process.env.PORT ?? '3012', 10);
@ -45,17 +71,25 @@ const APPWRITE_BASE_URL = (process.env.APPWRITE_BASE_URL ?? 'https://appwrite.bs
let PROJECT_CREDENTIALS: CredentialsMap = {}; let PROJECT_CREDENTIALS: CredentialsMap = {};
try { try {
PROJECT_CREDENTIALS = JSON.parse(process.env.PROJECT_CREDENTIALS ?? '{}') as CredentialsMap; const normalized = normalizeEnvValue(process.env.PROJECT_CREDENTIALS ?? '');
if (!normalized) {
throw new Error('PROJECT_CREDENTIALS is empty');
}
PROJECT_CREDENTIALS = JSON.parse(normalized) as CredentialsMap;
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : String(err); const message = err instanceof Error ? err.message : String(err);
const raw = (process.env.PROJECT_CREDENTIALS ?? '').slice(0, 300);
console.error('[startup] PROJECT_CREDENTIALS is not valid JSON:', message); console.error('[startup] PROJECT_CREDENTIALS is not valid JSON:', message);
console.error('[startup] Received value (first 300 chars):', raw);
console.error('[startup] Hint: in Coolify, paste the JSON without surrounding quotes. If the platform wraps it, set the env var to a single-line value with no leading/trailing whitespace.');
process.exit(1); process.exit(1);
} }
// Parse ROUTE_MAP. Format: "/path=>{json};/path2=>{json2}" // Parse ROUTE_MAP. Format: "/path=>{json};/path2=>{json2}"
const ROUTE_MAP: RouteMap = {}; const ROUTE_MAP: RouteMap = {};
if (process.env.ROUTE_MAP) { if (process.env.ROUTE_MAP) {
for (const entry of process.env.ROUTE_MAP.split(';')) { const normalizedRouteMap = normalizeEnvValue(process.env.ROUTE_MAP);
for (const entry of normalizedRouteMap.split(';')) {
const trimmed = entry.trim(); const trimmed = entry.trim();
if (!trimmed) continue; if (!trimmed) continue;
const arrowIdx = trimmed.indexOf('=>'); const arrowIdx = trimmed.indexOf('=>');