Fix getEnvArray to parse JSON when '[' present, CSV otherwise

Simplifies the comma-separated parsing logic to:
- If value contains '[', parse as JSON array
- Otherwise, parse as comma-separated values

This prevents incorrect splitting of arguments containing internal commas
when there's only one argument. For arguments with commas, users should
use JSON format: CHROME_ARGS='["--arg1,val", "--arg2"]'

Also exports getEnvArray in module.exports for consistency.

Co-authored-by: Nick Sweeting <pirate@users.noreply.github.com>
This commit is contained in:
claude[bot]
2025-12-31 07:39:49 +00:00
parent f7b186d7c8
commit 4285a05d19

View File

@@ -58,6 +58,15 @@ function getEnvInt(name, defaultValue = 0) {
/** /**
* Get array environment variable (JSON array or comma-separated string). * Get array environment variable (JSON array or comma-separated string).
*
* Parsing strategy:
* - If value contains '[' anywhere, parse as JSON array
* - Otherwise, parse as comma-separated values
*
* This prevents incorrect splitting of arguments that contain internal commas.
* For arguments with commas, use JSON format:
* CHROME_ARGS='["--user-data-dir=/path/with,comma", "--window-size=1440,900"]'
*
* @param {string} name - Environment variable name * @param {string} name - Environment variable name
* @param {string[]} [defaultValue=[]] - Default value if not set * @param {string[]} [defaultValue=[]] - Default value if not set
* @returns {string[]} - Array of strings * @returns {string[]} - Array of strings
@@ -66,23 +75,18 @@ function getEnvArray(name, defaultValue = []) {
const val = getEnv(name, ''); const val = getEnv(name, '');
if (!val) return defaultValue; if (!val) return defaultValue;
// Try parsing as JSON array first // If contains '[', parse as JSON array
if (val.startsWith('[')) { if (val.includes('[')) {
try { try {
const parsed = JSON.parse(val); const parsed = JSON.parse(val);
if (Array.isArray(parsed)) return parsed; if (Array.isArray(parsed)) return parsed;
} catch (e) { } catch (e) {
console.error(`[!] Failed to parse ${name} as JSON array: ${e.message}`);
// Fall through to comma-separated parsing // Fall through to comma-separated parsing
} }
} }
// Parse as comma-separated (but be careful with args that contain commas) // Parse as comma-separated values
// For Chrome args, we split on comma followed by '--' to be safe
if (val.includes(',--')) {
return val.split(/,(?=--)/).map(s => s.trim()).filter(Boolean);
}
// Simple comma-separated
return val.split(',').map(s => s.trim()).filter(Boolean); return val.split(',').map(s => s.trim()).filter(Boolean);
} }
@@ -1314,6 +1318,7 @@ module.exports = {
getEnv, getEnv,
getEnvBool, getEnvBool,
getEnvInt, getEnvInt,
getEnvArray,
parseResolution, parseResolution,
// PID file management // PID file management
writePidWithMtime, writePidWithMtime,