-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-loader.js
More file actions
197 lines (171 loc) · 5.16 KB
/
Copy pathconfig-loader.js
File metadata and controls
197 lines (171 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
/**
* config-loader.js — 配置文件加载器
*
* 支持从 YAML 文件动态加载数据源配置
* 用法:
* const { loadSourcesConfig } = require('./config-loader');
* const sources = loadSourcesConfig();
*/
const fs = require('fs');
const path = require('path');
// 尝试加载 js-yaml,如果不存在则使用内置的 JSON 解析
let yaml = null;
try {
yaml = require('js-yaml');
} catch (e) {
console.warn('[ConfigLoader] js-yaml not installed, YAML support disabled');
console.warn('[ConfigLoader] Run: npm install js-yaml');
}
/**
* 加载 YAML 配置文件
* @param {string} filePath - YAML 文件路径
* @returns {Object|null} - 解析后的配置对象
*/
function loadYamlConfig(filePath) {
if (!yaml) {
console.error('[ConfigLoader] Cannot load YAML without js-yaml package');
return null;
}
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
const config = yaml.load(fileContent);
console.log(`[ConfigLoader] Loaded YAML config from ${filePath}`);
return config;
} catch (err) {
console.error(`[ConfigLoader] Failed to load YAML: ${err.message}`);
return null;
}
}
/**
* 从 YAML 配置构建数据源映射
* @param {Object} yamlConfig - YAML 配置对象
* @returns {Object} - 数据源映射(name -> config)
*/
function buildSourceMap(yamlConfig) {
const sourceMap = {};
// 处理高频源
if (yamlConfig.high_frequency && Array.isArray(yamlConfig.high_frequency)) {
for (const source of yamlConfig.high_frequency) {
if (source.enabled !== false) {
sourceMap[source.name] = {
...source,
tier: 'high',
};
}
}
}
// 处理低频源
if (yamlConfig.low_frequency && Array.isArray(yamlConfig.low_frequency)) {
for (const source of yamlConfig.low_frequency) {
if (source.enabled !== false) {
sourceMap[source.name] = {
...source,
tier: 'low',
};
}
}
}
return sourceMap;
}
/**
* 应用默认配置到源配置
* @param {Object} sourceConfig - 单个源配置
* @param {Object} defaults - 默认配置
* @returns {Object} - 合并后的配置
*/
function applyDefaults(sourceConfig, defaults = {}) {
return {
maxAgeHours: sourceConfig.maxAgeHours || defaults.maxAgeHours || 48,
enableStrictTimestamp: sourceConfig.enableStrictTimestamp ?? defaults.enableStrictTimestamp ?? false,
dedupMode: sourceConfig.dedupMode || defaults.dedupMode || 'normal',
pushCooldownHours: sourceConfig.pushCooldownHours || defaults.pushCooldownHours || 24,
...sourceConfig,
};
}
/**
* 加载所有数据源配置
* @param {string} [configPath] - 可选的配置文件路径
* @returns {Object} - { sources: Object, settings: Object }
*/
function loadSourcesConfig(configPath) {
const defaultPath = path.join(__dirname, 'sources.yaml');
const filePath = configPath || defaultPath;
// 检查文件是否存在
if (!fs.existsSync(filePath)) {
console.warn(`[ConfigLoader] Config file not found: ${filePath}`);
console.warn('[ConfigLoader] Using built-in config from config.js');
return null;
}
// 加载 YAML 配置
const yamlConfig = loadYamlConfig(filePath);
if (!yamlConfig) {
return null;
}
// 构建源映射
const sourceMap = buildSourceMap(yamlConfig);
// 应用默认配置
const defaults = yamlConfig.settings?.defaults || {};
for (const [name, config] of Object.entries(sourceMap)) {
sourceMap[name] = applyDefaults(config, defaults);
}
console.log(`[ConfigLoader] Loaded ${Object.keys(sourceMap).length} sources from YAML`);
return {
sources: sourceMap,
settings: yamlConfig.settings || {},
rawConfig: yamlConfig,
};
}
/**
* 获取启用的数据源列表
* @param {string} [tier] - 可选的层级过滤 ('high' | 'low')
* @returns {Array<string>} - 启用的源名称数组
*/
function getEnabledSources(tier) {
const config = loadSourcesConfig();
if (!config) return [];
const sources = Object.entries(config.sources)
.filter(([_, conf]) => conf.enabled !== false)
.filter(([_, conf]) => !tier || conf.tier === tier)
.map(([name, _]) => name);
return sources;
}
/**
* 动态更新某个源的配置(运行时修改)
* @param {string} sourceName - 源名称
* @param {Object} updates - 要更新的配置项
*/
function updateSourceConfig(sourceName, updates) {
const config = loadSourcesConfig();
if (!config || !config.sources[sourceName]) {
throw new Error(`Source "${sourceName}" not found`);
}
config.sources[sourceName] = {
...config.sources[sourceName],
...updates,
};
console.log(`[ConfigLoader] Updated config for source: ${sourceName}`);
return config.sources[sourceName];
}
/**
* 导出为 JSON(用于前端展示)
* @returns {Object} - JSON 格式的配置
*/
function exportToJson() {
const config = loadSourcesConfig();
if (!config) return { sources: [], settings: {} };
return {
sources: Object.entries(config.sources).map(([name, conf]) => ({
name,
...conf,
})),
settings: config.settings,
};
}
module.exports = {
loadSourcesConfig,
getEnabledSources,
updateSourceConfig,
exportToJson,
loadYamlConfig,
};