mirror of
https://github.com/ONLYOFFICE/server.git
synced 2026-02-10 18:05:07 +08:00
[bug] Refactor requireConfigWithRuntime to temp set/restore NODE_CONFIG during config reload to avoid E2BIG
This commit is contained in:
@ -51,43 +51,52 @@ function reloadNpmModule(moduleName) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Requires config module with runtime configuration support
|
||||
* Requires config module with runtime configuration support.
|
||||
* Temporarily sets NODE_CONFIG for reload, then restores environment to prevent E2BIG.
|
||||
* @param {Object} opt_additionalConfig - Additional configuration to merge
|
||||
* @returns {Object} config module
|
||||
*/
|
||||
function requireConfigWithRuntime(opt_additionalConfig) {
|
||||
let config = require('config');
|
||||
|
||||
// Backup original NODE_CONFIG to avoid growing environment
|
||||
const prevNodeConfig = process.env.NODE_CONFIG;
|
||||
let nodeConfigOverridden = false;
|
||||
|
||||
try {
|
||||
const configFilePath = config.get('runtimeConfig.filePath');
|
||||
if (configFilePath) {
|
||||
// Update NODE_CONFIG with runtime configuration
|
||||
if (configFilePath) {
|
||||
const configData = fs.readFileSync(configFilePath, 'utf8');
|
||||
|
||||
let curNodeConfig;
|
||||
if (process.env['NODE_CONFIG']) {
|
||||
curNodeConfig = JSON.parse(process.env['NODE_CONFIG']);
|
||||
} else {
|
||||
curNodeConfig = {};
|
||||
}
|
||||
|
||||
const fileConfig = JSON.parse(configData);
|
||||
|
||||
// Merge configurations: NODE_CONFIG -> runtime -> additional
|
||||
curNodeConfig = config.util.extendDeep(curNodeConfig, fileConfig);
|
||||
if (opt_additionalConfig) {
|
||||
curNodeConfig = config.util.extendDeep(curNodeConfig, opt_additionalConfig);
|
||||
}
|
||||
|
||||
process.env['NODE_CONFIG'] = JSON.stringify(curNodeConfig);
|
||||
const configData = fs.readFileSync(configFilePath, 'utf8');
|
||||
|
||||
// Parse existing NODE_CONFIG or start with empty object
|
||||
let curNodeConfig = JSON.parse(process.env.NODE_CONFIG ?? '{}');
|
||||
const fileConfig = JSON.parse(configData);
|
||||
|
||||
// Merge configurations: NODE_CONFIG -> runtime -> additional
|
||||
curNodeConfig = config.util.extendDeep(curNodeConfig, fileConfig);
|
||||
if (opt_additionalConfig) {
|
||||
curNodeConfig = config.util.extendDeep(curNodeConfig, opt_additionalConfig);
|
||||
}
|
||||
|
||||
// Temporarily set NODE_CONFIG only to reload the config module
|
||||
process.env.NODE_CONFIG = JSON.stringify(curNodeConfig);
|
||||
nodeConfigOverridden = true;
|
||||
|
||||
config = reloadNpmModule('config');
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code !== 'ENOENT') {
|
||||
console.error('Failed to load runtime config: %s', err.stack);
|
||||
}
|
||||
} finally {
|
||||
// Restore original NODE_CONFIG to keep env small and avoid E2BIG on Windows/pkg
|
||||
if (nodeConfigOverridden) {
|
||||
if (typeof prevNodeConfig === 'undefined') {
|
||||
delete process.env.NODE_CONFIG;
|
||||
} else {
|
||||
process.env.NODE_CONFIG = prevNodeConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user