@ai-setting/roy-agent-core 1.4.12 → 1.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +7825 -0
- package/dist/index.js +13073 -19300
- package/dist/shared/chunk-0q6s9wm6.js +249 -0
- package/dist/shared/chunk-1aakcfp1.js +15 -0
- package/dist/shared/chunk-1d4rwms4.js +25 -0
- package/dist/shared/chunk-1pf5mfgd.js +82 -0
- package/dist/shared/chunk-1qwabsm0.js +154 -0
- package/dist/shared/chunk-25x2pdtp.js +107 -0
- package/dist/shared/chunk-2b5kbhx3.js +366 -0
- package/dist/shared/chunk-91bas8w5.js +20 -0
- package/dist/shared/chunk-9qzt1v1p.js +10 -0
- package/dist/shared/chunk-a9qmy3sc.js +296 -0
- package/dist/shared/chunk-g6j5n3gv.js +549 -0
- package/dist/shared/chunk-hs7tbmje.js +24 -0
- package/dist/shared/chunk-mf5xqbdh.js +14 -0
- package/dist/shared/chunk-q9j99fsm.js +368 -0
- package/dist/{env/workflow/engine/index.js → shared/chunk-rncy3rtd.js} +330 -1581
- package/dist/shared/chunk-t1rh6jtm.js +205 -0
- package/dist/{env/task/hooks/index.js → shared/chunk-wbkh7wat.js} +3 -18
- package/dist/shared/chunk-yqmx37vm.js +10 -0
- package/dist/shared/chunk-ze20rksg.js +102 -0
- package/package.json +7 -7
- package/dist/config/index.js +0 -1670
- package/dist/env/agent/index.js +0 -3035
- package/dist/env/commands/index.js +0 -1685
- package/dist/env/debug/formatters/index.js +0 -639
- package/dist/env/debug/index.js +0 -2300
- package/dist/env/hook/index.js +0 -273
- package/dist/env/index.js +0 -12591
- package/dist/env/llm/index.js +0 -2736
- package/dist/env/log-trace/index.js +0 -1779
- package/dist/env/mcp/index.js +0 -2173
- package/dist/env/mcp/tool/index.js +0 -1149
- package/dist/env/memory/built-in/index.js +0 -225
- package/dist/env/memory/index.js +0 -2171
- package/dist/env/memory/plugin/index.js +0 -1263
- package/dist/env/prompt/index.js +0 -2107
- package/dist/env/session/index.js +0 -3594
- package/dist/env/session/storage/index.js +0 -2049
- package/dist/env/skill/index.js +0 -1635
- package/dist/env/skill/tool/index.js +0 -114
- package/dist/env/task/delegate/index.js +0 -1844
- package/dist/env/task/index.js +0 -3578
- package/dist/env/task/plugins/index.js +0 -1626
- package/dist/env/task/storage/index.js +0 -1464
- package/dist/env/task/tools/index.js +0 -344
- package/dist/env/task/tools/operation/index.js +0 -270
- package/dist/env/tool/built-in/index.js +0 -1151
- package/dist/env/tool/index.js +0 -2284
- package/dist/env/workflow/decorators/index.js +0 -449
- package/dist/env/workflow/index.js +0 -6214
- package/dist/env/workflow/nodes/index.js +0 -650
- package/dist/env/workflow/service/index.js +0 -262
- package/dist/env/workflow/storage/index.js +0 -1236
- package/dist/env/workflow/tools/index.js +0 -1081
- package/dist/env/workflow/types/index.js +0 -479
- package/dist/env/workflow/utils/index.js +0 -1631
package/dist/config/index.js
DELETED
|
@@ -1,1670 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
function __accessProp(key) {
|
|
7
|
-
return this[key];
|
|
8
|
-
}
|
|
9
|
-
var __toCommonJS = (from) => {
|
|
10
|
-
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
11
|
-
if (entry)
|
|
12
|
-
return entry;
|
|
13
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
14
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
-
for (var key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(entry, key))
|
|
17
|
-
__defProp(entry, key, {
|
|
18
|
-
get: __accessProp.bind(from, key),
|
|
19
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
__moduleCache.set(from, entry);
|
|
23
|
-
return entry;
|
|
24
|
-
};
|
|
25
|
-
var __moduleCache;
|
|
26
|
-
var __returnValue = (v) => v;
|
|
27
|
-
function __exportSetter(name, newValue) {
|
|
28
|
-
this[name] = __returnValue.bind(null, newValue);
|
|
29
|
-
}
|
|
30
|
-
var __export = (target, all) => {
|
|
31
|
-
for (var name in all)
|
|
32
|
-
__defProp(target, name, {
|
|
33
|
-
get: all[name],
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
set: __exportSetter.bind(all, name)
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
40
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
41
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
42
|
-
r = Reflect.decorate(decorators, target, key, desc);
|
|
43
|
-
else
|
|
44
|
-
for (var i = decorators.length - 1;i >= 0; i--)
|
|
45
|
-
if (d = decorators[i])
|
|
46
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
47
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
48
|
-
};
|
|
49
|
-
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
50
|
-
var __require = import.meta.require;
|
|
51
|
-
|
|
52
|
-
// packages/core/src/env/hook/types.ts
|
|
53
|
-
function createHook(meta, fn) {
|
|
54
|
-
return {
|
|
55
|
-
...meta,
|
|
56
|
-
execute: fn
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function createPriorityHook(name, fn, priority) {
|
|
60
|
-
return createHook({ name, priority }, fn);
|
|
61
|
-
}
|
|
62
|
-
// packages/core/src/env/hook/hook-manager.ts
|
|
63
|
-
class HookManager {
|
|
64
|
-
_hooks = new Map;
|
|
65
|
-
componentName;
|
|
66
|
-
componentVersion;
|
|
67
|
-
defaultPriority;
|
|
68
|
-
constructor(options = {}) {
|
|
69
|
-
this.componentName = options.componentName ?? "unknown";
|
|
70
|
-
this.componentVersion = options.componentVersion ?? "0.0.0";
|
|
71
|
-
this.defaultPriority = options.defaultPriority ?? 0;
|
|
72
|
-
}
|
|
73
|
-
register(hookPoint, hook) {
|
|
74
|
-
const hooks = this.getOrCreateHooks(hookPoint);
|
|
75
|
-
hooks.push(hook);
|
|
76
|
-
}
|
|
77
|
-
registerMany(hookPoint, hooks) {
|
|
78
|
-
const hookList = this.getOrCreateHooks(hookPoint);
|
|
79
|
-
hookList.push(...hooks);
|
|
80
|
-
}
|
|
81
|
-
unregister(hookPoint, name) {
|
|
82
|
-
const hooks = this._hooks.get(hookPoint);
|
|
83
|
-
if (!hooks)
|
|
84
|
-
return false;
|
|
85
|
-
const index = hooks.findIndex((h) => h.name === name);
|
|
86
|
-
if (index === -1)
|
|
87
|
-
return false;
|
|
88
|
-
hooks.splice(index, 1);
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
unregisterAll(hookPoint) {
|
|
92
|
-
this._hooks.delete(hookPoint);
|
|
93
|
-
}
|
|
94
|
-
async execute(hookPoint, data, metadata = {}) {
|
|
95
|
-
const hooks = this._hooks.get(hookPoint);
|
|
96
|
-
if (!hooks || hooks.length === 0)
|
|
97
|
-
return;
|
|
98
|
-
const sortedHooks = this.sortHooks(hooks);
|
|
99
|
-
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
100
|
-
for (const hook of sortedHooks) {
|
|
101
|
-
await this.safeExecute(hook, ctx);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async executeAndCollect(hookPoint, data, metadata = {}) {
|
|
105
|
-
const hooks = this._hooks.get(hookPoint);
|
|
106
|
-
if (!hooks || hooks.length === 0)
|
|
107
|
-
return [];
|
|
108
|
-
const sortedHooks = this.sortHooks(hooks);
|
|
109
|
-
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
110
|
-
const results = [];
|
|
111
|
-
for (const hook of sortedHooks) {
|
|
112
|
-
const result = await this.safeExecuteAndReturn(hook, ctx);
|
|
113
|
-
if (result !== undefined) {
|
|
114
|
-
results.push(result);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return results;
|
|
118
|
-
}
|
|
119
|
-
count(hookPoint) {
|
|
120
|
-
return this._hooks.get(hookPoint)?.length ?? 0;
|
|
121
|
-
}
|
|
122
|
-
clear() {
|
|
123
|
-
this._hooks.clear();
|
|
124
|
-
}
|
|
125
|
-
getHookPoints() {
|
|
126
|
-
return Array.from(this._hooks.keys());
|
|
127
|
-
}
|
|
128
|
-
hasHooks(hookPoint) {
|
|
129
|
-
return this.count(hookPoint) > 0;
|
|
130
|
-
}
|
|
131
|
-
setComponentInfo(name, version) {
|
|
132
|
-
this.componentName = name;
|
|
133
|
-
this.componentVersion = version;
|
|
134
|
-
}
|
|
135
|
-
get hooks() {
|
|
136
|
-
return this._hooks;
|
|
137
|
-
}
|
|
138
|
-
async executeWithIntervention(hookPoint, data, metadata = {}) {
|
|
139
|
-
const hooks = this._hooks.get(hookPoint);
|
|
140
|
-
if (!hooks || hooks.length === 0) {
|
|
141
|
-
return { stopped: false, results: [] };
|
|
142
|
-
}
|
|
143
|
-
const sortedHooks = this.sortHooks(hooks);
|
|
144
|
-
const ctx = this.createContext(hookPoint, data, metadata, "before");
|
|
145
|
-
const results = [];
|
|
146
|
-
let stopped = false;
|
|
147
|
-
let action;
|
|
148
|
-
for (const hook of sortedHooks) {
|
|
149
|
-
if (stopped)
|
|
150
|
-
break;
|
|
151
|
-
const result = await this.safeExecuteAndReturn(hook, ctx);
|
|
152
|
-
results.push(result);
|
|
153
|
-
if (result && typeof result === "object" && "stopped" in result) {
|
|
154
|
-
const hookResult = result;
|
|
155
|
-
if (hookResult.stopped) {
|
|
156
|
-
stopped = true;
|
|
157
|
-
action = hookResult.action;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return { stopped, action, results };
|
|
162
|
-
}
|
|
163
|
-
getOrCreateHooks(hookPoint) {
|
|
164
|
-
let hooks = this._hooks.get(hookPoint);
|
|
165
|
-
if (!hooks) {
|
|
166
|
-
hooks = [];
|
|
167
|
-
this._hooks.set(hookPoint, hooks);
|
|
168
|
-
}
|
|
169
|
-
return hooks;
|
|
170
|
-
}
|
|
171
|
-
sortHooks(hooks) {
|
|
172
|
-
return [...hooks].sort((a, b) => {
|
|
173
|
-
const priorityA = a.priority ?? this.defaultPriority;
|
|
174
|
-
const priorityB = b.priority ?? this.defaultPriority;
|
|
175
|
-
return priorityA - priorityB;
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
createContext(hookPoint, data, metadata, phase) {
|
|
179
|
-
return {
|
|
180
|
-
component: {
|
|
181
|
-
name: this.componentName,
|
|
182
|
-
version: this.componentVersion
|
|
183
|
-
},
|
|
184
|
-
data,
|
|
185
|
-
metadata,
|
|
186
|
-
phase,
|
|
187
|
-
hookPoint
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
async safeExecute(hook, ctx) {
|
|
191
|
-
try {
|
|
192
|
-
await hook.execute(ctx);
|
|
193
|
-
} catch (error) {
|
|
194
|
-
console.error(`Hook "${hook.name}" failed:`, error);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
async safeExecuteAndReturn(hook, ctx) {
|
|
198
|
-
try {
|
|
199
|
-
return await hook.execute(ctx);
|
|
200
|
-
} catch (error) {
|
|
201
|
-
console.error(`Hook "${hook.name}" failed:`, error);
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
// packages/core/src/env/hook/global-hook-manager.ts
|
|
207
|
-
var globalHookManager = new HookManager;
|
|
208
|
-
var AgentHookPoints = {
|
|
209
|
-
BEFORE_START: "agent:before.start",
|
|
210
|
-
BEFORE_LLM: "agent:before.llm",
|
|
211
|
-
AFTER_LLM: "agent:after.llm",
|
|
212
|
-
BEFORE_TOOL: "agent:before.tool",
|
|
213
|
-
AFTER_TOOL: "agent:after.tool",
|
|
214
|
-
ON_ITERATION: "agent:on.iteration",
|
|
215
|
-
ON_THRESHOLD: "agent:on.threshold",
|
|
216
|
-
AFTER_COMPLETE: "agent:after.complete",
|
|
217
|
-
ON_ERROR: "agent:on.error"
|
|
218
|
-
};
|
|
219
|
-
var LLMHookPoints = {
|
|
220
|
-
BEFORE_INVOKE: "llm:before.invoke",
|
|
221
|
-
AFTER_INVOKE: "llm:after.invoke",
|
|
222
|
-
ON_STREAM: "llm:on.stream"
|
|
223
|
-
};
|
|
224
|
-
var ToolHookPoints = {
|
|
225
|
-
BEFORE_EXECUTE: "tool:before.execute",
|
|
226
|
-
AFTER_EXECUTE: "tool:after.execute",
|
|
227
|
-
BEFORE_REGISTER: "tool:before.register",
|
|
228
|
-
AFTER_REGISTER: "tool:after.register",
|
|
229
|
-
ON_ERROR: "tool:on.error"
|
|
230
|
-
};
|
|
231
|
-
var hookPointAliases = {
|
|
232
|
-
"before.tool": "tool:before.execute",
|
|
233
|
-
"after.tool": "tool:after.execute",
|
|
234
|
-
"llm.before-invoke": "llm:before.invoke",
|
|
235
|
-
"llm.after-invoke": "llm:after.invoke",
|
|
236
|
-
"llm.stream": "llm:on.stream"
|
|
237
|
-
};
|
|
238
|
-
function setupAliasHooks() {
|
|
239
|
-
const originalRegister = globalHookManager.register.bind(globalHookManager);
|
|
240
|
-
globalHookManager.register = function(hookPoint, hook) {
|
|
241
|
-
originalRegister(hookPoint, hook);
|
|
242
|
-
const alias = hookPointAliases[hookPoint];
|
|
243
|
-
if (alias) {
|
|
244
|
-
originalRegister(alias, hook);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
setupAliasHooks();
|
|
249
|
-
async function executeAgentHook(hookPoint, data, metadata = {}) {
|
|
250
|
-
await globalHookManager.execute(hookPoint, data, metadata);
|
|
251
|
-
}
|
|
252
|
-
async function executeAgentHookWithIntervention(hookPoint, data, metadata = {}) {
|
|
253
|
-
return globalHookManager.executeWithIntervention(hookPoint, data, metadata);
|
|
254
|
-
}
|
|
255
|
-
async function executeLLMHook(hookPoint, data, metadata = {}) {
|
|
256
|
-
await globalHookManager.execute(hookPoint, data, metadata);
|
|
257
|
-
}
|
|
258
|
-
async function executeToolHook(hookPoint, data, metadata = {}) {
|
|
259
|
-
await globalHookManager.execute(hookPoint, data, metadata);
|
|
260
|
-
}
|
|
261
|
-
// packages/core/src/env/component.ts
|
|
262
|
-
class BaseComponent {
|
|
263
|
-
_status = "created";
|
|
264
|
-
_enabled = true;
|
|
265
|
-
env;
|
|
266
|
-
hookManager;
|
|
267
|
-
constructor() {
|
|
268
|
-
this.hookManager = new HookManager;
|
|
269
|
-
}
|
|
270
|
-
getStatus() {
|
|
271
|
-
return this._status;
|
|
272
|
-
}
|
|
273
|
-
getConfig() {
|
|
274
|
-
return {
|
|
275
|
-
name: this.name,
|
|
276
|
-
version: this.version,
|
|
277
|
-
enabled: this._enabled,
|
|
278
|
-
env: this.env
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
isEnvInitialized() {
|
|
282
|
-
return this._status !== "created";
|
|
283
|
-
}
|
|
284
|
-
setStatus(status) {
|
|
285
|
-
this._status = status;
|
|
286
|
-
}
|
|
287
|
-
getEnv() {
|
|
288
|
-
return this.env;
|
|
289
|
-
}
|
|
290
|
-
async init(config) {
|
|
291
|
-
if (config?.env) {
|
|
292
|
-
this.env = config.env;
|
|
293
|
-
}
|
|
294
|
-
this.setStatus("initializing");
|
|
295
|
-
try {
|
|
296
|
-
if (config?.name)
|
|
297
|
-
Object.defineProperty(this, "name", { value: config.name, writable: false });
|
|
298
|
-
if (config?.version)
|
|
299
|
-
Object.defineProperty(this, "version", { value: config.version, writable: false });
|
|
300
|
-
} catch {}
|
|
301
|
-
if (config?.enabled !== undefined)
|
|
302
|
-
this._enabled = config.enabled;
|
|
303
|
-
this.hookManager.setComponentInfo(this.name, this.version);
|
|
304
|
-
await this.onInit();
|
|
305
|
-
this.setStatus("running");
|
|
306
|
-
}
|
|
307
|
-
async start() {
|
|
308
|
-
if (this._started)
|
|
309
|
-
return;
|
|
310
|
-
this._started = true;
|
|
311
|
-
await this.onStart();
|
|
312
|
-
this.setStatus("running");
|
|
313
|
-
}
|
|
314
|
-
async stop() {
|
|
315
|
-
this.setStatus("stopping");
|
|
316
|
-
this.hookManager.clear();
|
|
317
|
-
await this.onStop();
|
|
318
|
-
this.setStatus("stopped");
|
|
319
|
-
}
|
|
320
|
-
async onInit() {}
|
|
321
|
-
async onStart() {}
|
|
322
|
-
async onStop() {}
|
|
323
|
-
registerHook(hookPoint, hook) {
|
|
324
|
-
this.hookManager.register(hookPoint, hook);
|
|
325
|
-
}
|
|
326
|
-
addHook(hookPoint, name, fn, priority) {
|
|
327
|
-
this.hookManager.register(hookPoint, createHook({ name, priority }, fn));
|
|
328
|
-
}
|
|
329
|
-
removeHook(hookPoint, name) {
|
|
330
|
-
return this.hookManager.unregister(hookPoint, name);
|
|
331
|
-
}
|
|
332
|
-
async executeHooks(hookPoint, data, metadata) {
|
|
333
|
-
await this.hookManager.execute(hookPoint, data, metadata);
|
|
334
|
-
}
|
|
335
|
-
getConfigComponent() {
|
|
336
|
-
return this.env?.getComponent("config");
|
|
337
|
-
}
|
|
338
|
-
getRuntimeConfig(key, defaultValue) {
|
|
339
|
-
const configComponent = this.getConfigComponent();
|
|
340
|
-
if (configComponent) {
|
|
341
|
-
const value = configComponent.get(key);
|
|
342
|
-
if (value !== undefined) {
|
|
343
|
-
return value;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return defaultValue;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// packages/core/src/env/paths.ts
|
|
351
|
-
import os from "os";
|
|
352
|
-
import path from "path";
|
|
353
|
-
var APP_NAME = "roy-agent";
|
|
354
|
-
function getXdgBase() {
|
|
355
|
-
const home = os.homedir();
|
|
356
|
-
try {
|
|
357
|
-
const xdg = __require("xdg-basedir");
|
|
358
|
-
return {
|
|
359
|
-
config: xdg.xdgConfig || path.join(home, ".config"),
|
|
360
|
-
state: xdg.xdgState || path.join(home, ".local", "state"),
|
|
361
|
-
data: xdg.xdgData || path.join(home, ".local", "share"),
|
|
362
|
-
cache: xdg.xdgCache || path.join(home, ".cache")
|
|
363
|
-
};
|
|
364
|
-
} catch {
|
|
365
|
-
return {
|
|
366
|
-
config: path.join(home, ".config"),
|
|
367
|
-
state: path.join(home, ".local", "state"),
|
|
368
|
-
data: path.join(home, ".local", "share"),
|
|
369
|
-
cache: path.join(home, ".cache")
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
function computeXDGPaths() {
|
|
374
|
-
const home = os.homedir();
|
|
375
|
-
const xdg = getXdgBase();
|
|
376
|
-
return {
|
|
377
|
-
home,
|
|
378
|
-
config: path.join(xdg.config, APP_NAME),
|
|
379
|
-
state: path.join(xdg.state, APP_NAME),
|
|
380
|
-
data: path.join(xdg.data, APP_NAME),
|
|
381
|
-
cache: path.join(xdg.cache, APP_NAME)
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
class XDGPathsCalculator {
|
|
386
|
-
_paths;
|
|
387
|
-
get paths() {
|
|
388
|
-
if (!this._paths) {
|
|
389
|
-
this._paths = computeXDGPaths();
|
|
390
|
-
}
|
|
391
|
-
return this._paths;
|
|
392
|
-
}
|
|
393
|
-
getPath(type) {
|
|
394
|
-
return this.paths[type];
|
|
395
|
-
}
|
|
396
|
-
getComponentPath(base, subPath) {
|
|
397
|
-
return path.join(this.paths[base], subPath);
|
|
398
|
-
}
|
|
399
|
-
reset() {
|
|
400
|
-
this._paths = undefined;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
var XDG_PATHS = new XDGPathsCalculator;
|
|
404
|
-
function getXDGPaths() {
|
|
405
|
-
return XDG_PATHS.paths;
|
|
406
|
-
}
|
|
407
|
-
function getXDGPath(type) {
|
|
408
|
-
return XDG_PATHS.getPath(type);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// packages/core/src/config/protocol-resolver.ts
|
|
412
|
-
var PROTOCOL_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
413
|
-
|
|
414
|
-
class ProtocolResolver {
|
|
415
|
-
_xdgDataHome;
|
|
416
|
-
readFileFn;
|
|
417
|
-
constructor(options = {}) {
|
|
418
|
-
this._xdgDataHome = options.xdgDataHome ?? ProtocolResolver.getDefaultXDGDataHome();
|
|
419
|
-
if (options.readFileFn) {
|
|
420
|
-
this.readFileFn = options.readFileFn;
|
|
421
|
-
} else {
|
|
422
|
-
const fsSync = __require("fs");
|
|
423
|
-
this.readFileFn = (path2) => fsSync.readFileSync(path2, "utf-8");
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
get xdgDataHome() {
|
|
427
|
-
return this._xdgDataHome;
|
|
428
|
-
}
|
|
429
|
-
set xdgDataHome(value) {
|
|
430
|
-
this._xdgDataHome = value;
|
|
431
|
-
}
|
|
432
|
-
static getDefaultXDGDataHome() {
|
|
433
|
-
return process.env.XDG_DATA_HOME ?? `${process.env.HOME ?? "~"}/.local/share`;
|
|
434
|
-
}
|
|
435
|
-
parse(ref) {
|
|
436
|
-
const match = ref.match(PROTOCOL_PATTERN);
|
|
437
|
-
if (!match)
|
|
438
|
-
return null;
|
|
439
|
-
const [, protocol, path2, key, defaultValue] = match;
|
|
440
|
-
if (!["file", "env"].includes(protocol))
|
|
441
|
-
return null;
|
|
442
|
-
return {
|
|
443
|
-
protocol,
|
|
444
|
-
path: path2,
|
|
445
|
-
key: key ?? "",
|
|
446
|
-
defaultValue,
|
|
447
|
-
raw: ref
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
isProtocolRef(value) {
|
|
451
|
-
return PROTOCOL_PATTERN.test(value);
|
|
452
|
-
}
|
|
453
|
-
resolve(ref) {
|
|
454
|
-
const parsed = this.parse(ref);
|
|
455
|
-
if (!parsed)
|
|
456
|
-
return null;
|
|
457
|
-
const value = this.resolveInternal(parsed);
|
|
458
|
-
if (parsed.protocol === "file" && value === undefined) {
|
|
459
|
-
return null;
|
|
460
|
-
}
|
|
461
|
-
if (value === undefined && parsed.protocol === "env" && parsed.defaultValue === undefined) {
|
|
462
|
-
return null;
|
|
463
|
-
}
|
|
464
|
-
if (value === undefined && parsed.defaultValue !== undefined) {
|
|
465
|
-
return {
|
|
466
|
-
value: parsed.defaultValue,
|
|
467
|
-
source: parsed.protocol
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
return {
|
|
471
|
-
value,
|
|
472
|
-
source: parsed.protocol
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
resolveFileRef(filePath, key) {
|
|
476
|
-
const resolvedPath = this.resolvePath(filePath);
|
|
477
|
-
try {
|
|
478
|
-
const content = this.readFileFn(resolvedPath);
|
|
479
|
-
let data;
|
|
480
|
-
try {
|
|
481
|
-
data = JSON.parse(content);
|
|
482
|
-
} catch {
|
|
483
|
-
return content;
|
|
484
|
-
}
|
|
485
|
-
return this.getNestedValue(data, key);
|
|
486
|
-
} catch {
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
resolveInternal(ref) {
|
|
491
|
-
switch (ref.protocol) {
|
|
492
|
-
case "env":
|
|
493
|
-
return process.env[ref.path];
|
|
494
|
-
case "file":
|
|
495
|
-
return this.resolveFileRef(ref.path, ref.key);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
resolvePath(filePath) {
|
|
499
|
-
if (filePath.startsWith("/")) {
|
|
500
|
-
return filePath;
|
|
501
|
-
}
|
|
502
|
-
if (filePath.startsWith("~")) {
|
|
503
|
-
return filePath.replace("~", process.env.HOME ?? "");
|
|
504
|
-
}
|
|
505
|
-
return `${this._xdgDataHome}/${filePath}`;
|
|
506
|
-
}
|
|
507
|
-
getNestedValue(obj, key) {
|
|
508
|
-
if (key === "$" || key === "")
|
|
509
|
-
return obj;
|
|
510
|
-
const keys = key.split(".");
|
|
511
|
-
let current = obj;
|
|
512
|
-
for (const k of keys) {
|
|
513
|
-
if (current === null || current === undefined || typeof current !== "object") {
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
current = current[k];
|
|
517
|
-
}
|
|
518
|
-
return current;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// packages/core/src/config/file-source.ts
|
|
523
|
-
import * as fsSync from "fs";
|
|
524
|
-
import * as fsPromises from "fs/promises";
|
|
525
|
-
import path2 from "path";
|
|
526
|
-
|
|
527
|
-
// packages/core/src/config/config-parser.ts
|
|
528
|
-
import { parse as parseJSONCInternal, ParseErrorCode } from "jsonc-parser";
|
|
529
|
-
function parseJSONC(content) {
|
|
530
|
-
const errors = [];
|
|
531
|
-
const result = parseJSONCInternal(content, errors, {
|
|
532
|
-
allowTrailingComma: true,
|
|
533
|
-
disallowComments: false
|
|
534
|
-
});
|
|
535
|
-
if (errors.length > 0) {
|
|
536
|
-
const hasOnlyValueExpected = result === undefined && errors.length === 1 && errors[0].error === ParseErrorCode.ValueExpected;
|
|
537
|
-
if (!hasOnlyValueExpected) {
|
|
538
|
-
const error = errors[0];
|
|
539
|
-
const offset = error.offset;
|
|
540
|
-
const lines = content.substring(0, offset).split(`
|
|
541
|
-
`);
|
|
542
|
-
const line = lines.length;
|
|
543
|
-
const col = lines[lines.length - 1].length + 1;
|
|
544
|
-
const errorMessages = {
|
|
545
|
-
[ParseErrorCode.UnexpectedEndOfString]: "Unexpected end of string",
|
|
546
|
-
[ParseErrorCode.InvalidCharacter]: "Invalid character",
|
|
547
|
-
[ParseErrorCode.PropertyNameExpected]: "Property name expected",
|
|
548
|
-
[ParseErrorCode.ValueExpected]: "Value expected",
|
|
549
|
-
[ParseErrorCode.ColonExpected]: "Colon expected",
|
|
550
|
-
[ParseErrorCode.CommaExpected]: "Comma expected",
|
|
551
|
-
[ParseErrorCode.CloseBraceExpected]: "Close brace expected",
|
|
552
|
-
[ParseErrorCode.CloseBracketExpected]: "Close bracket expected",
|
|
553
|
-
[ParseErrorCode.EndOfFileExpected]: "Unexpected end of input",
|
|
554
|
-
[ParseErrorCode.InvalidSymbol]: "Invalid symbol"
|
|
555
|
-
};
|
|
556
|
-
throw new Error(`JSONC parse error at line ${line}, col ${col}: ${errorMessages[error.error] || "Unknown error"}`);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (result === undefined || result === null) {
|
|
560
|
-
return {};
|
|
561
|
-
}
|
|
562
|
-
return result;
|
|
563
|
-
}
|
|
564
|
-
function substituteEnvVars(value) {
|
|
565
|
-
if (typeof value === "string") {
|
|
566
|
-
return value.replace(/\$\{([^}]+)\}/g, (match, varExpr) => {
|
|
567
|
-
const [varName, defaultValue] = varExpr.split(":-");
|
|
568
|
-
const envValue = process.env[varName.trim()];
|
|
569
|
-
if (envValue !== undefined) {
|
|
570
|
-
return envValue;
|
|
571
|
-
}
|
|
572
|
-
if (defaultValue !== undefined) {
|
|
573
|
-
return defaultValue;
|
|
574
|
-
}
|
|
575
|
-
return "";
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
if (Array.isArray(value)) {
|
|
579
|
-
return value.map((item) => substituteEnvVars(item));
|
|
580
|
-
}
|
|
581
|
-
if (value !== null && typeof value === "object") {
|
|
582
|
-
const result = {};
|
|
583
|
-
for (const [key, val] of Object.entries(value)) {
|
|
584
|
-
result[key] = substituteEnvVars(val);
|
|
585
|
-
}
|
|
586
|
-
return result;
|
|
587
|
-
}
|
|
588
|
-
return value;
|
|
589
|
-
}
|
|
590
|
-
function parseJSONCWithEnv(content) {
|
|
591
|
-
const parsed = parseJSONC(content);
|
|
592
|
-
return substituteEnvVars(parsed);
|
|
593
|
-
}
|
|
594
|
-
var PROTOCOL_REF_PATTERN = /^\$\{([^:]+):\/\/[^}]+\}$/;
|
|
595
|
-
var PROTOCOL_REF_WITH_DEFAULT_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
596
|
-
function isProtocolRefString(value) {
|
|
597
|
-
return PROTOCOL_REF_PATTERN.test(value);
|
|
598
|
-
}
|
|
599
|
-
function extractDefaultValue(ref) {
|
|
600
|
-
const match = ref.match(PROTOCOL_REF_WITH_DEFAULT_PATTERN);
|
|
601
|
-
return match ? match[4] : undefined;
|
|
602
|
-
}
|
|
603
|
-
function substituteProtocolRefs(value, resolver) {
|
|
604
|
-
if (typeof value === "string") {
|
|
605
|
-
if (isProtocolRefString(value)) {
|
|
606
|
-
const result = resolver.resolve(value);
|
|
607
|
-
if (result !== null) {
|
|
608
|
-
return result.value;
|
|
609
|
-
}
|
|
610
|
-
const defaultValue = extractDefaultValue(value);
|
|
611
|
-
if (defaultValue !== undefined) {
|
|
612
|
-
return defaultValue;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
return value;
|
|
616
|
-
}
|
|
617
|
-
if (Array.isArray(value)) {
|
|
618
|
-
return value.map((item) => substituteProtocolRefs(item, resolver));
|
|
619
|
-
}
|
|
620
|
-
if (value !== null && typeof value === "object") {
|
|
621
|
-
const result = {};
|
|
622
|
-
for (const [key, val] of Object.entries(value)) {
|
|
623
|
-
result[key] = substituteProtocolRefs(val, resolver);
|
|
624
|
-
}
|
|
625
|
-
return result;
|
|
626
|
-
}
|
|
627
|
-
return value;
|
|
628
|
-
}
|
|
629
|
-
function parseJSONCWithProtocols(content, resolver) {
|
|
630
|
-
const parsed = parseJSONC(content);
|
|
631
|
-
const withProtocols = substituteProtocolRefs(parsed, resolver);
|
|
632
|
-
return substituteEnvVars(withProtocols);
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
// packages/core/src/config/file-source.ts
|
|
636
|
-
class FileSource {
|
|
637
|
-
name = "file";
|
|
638
|
-
priority = 10;
|
|
639
|
-
filePath;
|
|
640
|
-
data = {};
|
|
641
|
-
watchers = new Set;
|
|
642
|
-
watcherHandle;
|
|
643
|
-
debounceMs;
|
|
644
|
-
pendingWrite;
|
|
645
|
-
lastFileContent = "";
|
|
646
|
-
resolver;
|
|
647
|
-
optional = false;
|
|
648
|
-
watchEnabled = true;
|
|
649
|
-
constructor(options, resolver) {
|
|
650
|
-
if (typeof options === "string") {
|
|
651
|
-
this.filePath = options;
|
|
652
|
-
this.debounceMs = 100;
|
|
653
|
-
this.resolver = resolver;
|
|
654
|
-
this.optional = true;
|
|
655
|
-
} else {
|
|
656
|
-
this.filePath = options.filePath;
|
|
657
|
-
this.debounceMs = options.debounceMs ?? 100;
|
|
658
|
-
this.resolver = options.resolver ?? resolver;
|
|
659
|
-
this.optional = options.optional ?? true;
|
|
660
|
-
this.watchEnabled = options.watch ?? true;
|
|
661
|
-
}
|
|
662
|
-
this.initSync();
|
|
663
|
-
}
|
|
664
|
-
initSync() {
|
|
665
|
-
try {
|
|
666
|
-
if (fsSync.existsSync(this.filePath)) {
|
|
667
|
-
const content = fsSync.readFileSync(this.filePath, "utf-8");
|
|
668
|
-
if (this.resolver) {
|
|
669
|
-
this.data = parseJSONCWithProtocols(content, this.resolver);
|
|
670
|
-
} else {
|
|
671
|
-
this.data = parseJSONCWithEnv(content);
|
|
672
|
-
}
|
|
673
|
-
this.lastFileContent = content;
|
|
674
|
-
} else {
|
|
675
|
-
if (this.optional) {
|
|
676
|
-
this.data = {};
|
|
677
|
-
this.lastFileContent = "{}";
|
|
678
|
-
} else {
|
|
679
|
-
throw new Error(`Config file not found: ${this.filePath}`);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
} catch (error) {
|
|
683
|
-
if (this.optional) {
|
|
684
|
-
this.data = {};
|
|
685
|
-
this.lastFileContent = "{}";
|
|
686
|
-
} else {
|
|
687
|
-
throw error;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
async ensureInit() {
|
|
692
|
-
if (this.lastFileContent === "" && !fsSync.existsSync(this.filePath)) {
|
|
693
|
-
this.initSync();
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
getNestedValue(obj, key) {
|
|
697
|
-
const keys = key.split(".");
|
|
698
|
-
let current = obj;
|
|
699
|
-
for (const k of keys) {
|
|
700
|
-
if (current === null || current === undefined || typeof current !== "object") {
|
|
701
|
-
return;
|
|
702
|
-
}
|
|
703
|
-
current = current[k];
|
|
704
|
-
}
|
|
705
|
-
return current;
|
|
706
|
-
}
|
|
707
|
-
setNestedValue(obj, key, value) {
|
|
708
|
-
const keys = key.split(".");
|
|
709
|
-
let current = obj;
|
|
710
|
-
for (let i = 0;i < keys.length - 1; i++) {
|
|
711
|
-
const k = keys[i];
|
|
712
|
-
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
713
|
-
current[k] = {};
|
|
714
|
-
}
|
|
715
|
-
current = current[k];
|
|
716
|
-
}
|
|
717
|
-
current[keys[keys.length - 1]] = value;
|
|
718
|
-
}
|
|
719
|
-
deleteNestedValue(obj, key) {
|
|
720
|
-
const keys = key.split(".");
|
|
721
|
-
let current = obj;
|
|
722
|
-
for (let i = 0;i < keys.length - 1; i++) {
|
|
723
|
-
const k = keys[i];
|
|
724
|
-
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
725
|
-
return false;
|
|
726
|
-
}
|
|
727
|
-
current = current[k];
|
|
728
|
-
}
|
|
729
|
-
const lastKey = keys[keys.length - 1];
|
|
730
|
-
if (lastKey in current) {
|
|
731
|
-
delete current[lastKey];
|
|
732
|
-
return true;
|
|
733
|
-
}
|
|
734
|
-
return false;
|
|
735
|
-
}
|
|
736
|
-
flattenObject(obj, prefix = "") {
|
|
737
|
-
if (obj === null || obj === undefined) {
|
|
738
|
-
return [];
|
|
739
|
-
}
|
|
740
|
-
if (typeof obj !== "object") {
|
|
741
|
-
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
742
|
-
}
|
|
743
|
-
if (Array.isArray(obj)) {
|
|
744
|
-
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
745
|
-
}
|
|
746
|
-
const result = [];
|
|
747
|
-
const entries = Object.entries(obj);
|
|
748
|
-
for (const [k, v] of entries) {
|
|
749
|
-
const newKey = prefix ? `${prefix}.${k}` : k;
|
|
750
|
-
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
751
|
-
result.push(...this.flattenObject(v, newKey));
|
|
752
|
-
} else {
|
|
753
|
-
result.push({ key: newKey, value: v });
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
return result;
|
|
757
|
-
}
|
|
758
|
-
read(key) {
|
|
759
|
-
return this.getNestedValue(this.data, key);
|
|
760
|
-
}
|
|
761
|
-
async readAsync(key) {
|
|
762
|
-
await this.ensureInit();
|
|
763
|
-
return this.getNestedValue(this.data, key);
|
|
764
|
-
}
|
|
765
|
-
write(key, value) {
|
|
766
|
-
const oldValue = this.getNestedValue(this.data, key);
|
|
767
|
-
this.setNestedValue(this.data, key, value);
|
|
768
|
-
const event = {
|
|
769
|
-
type: oldValue === undefined ? "add" : "change",
|
|
770
|
-
key,
|
|
771
|
-
oldValue,
|
|
772
|
-
newValue: value,
|
|
773
|
-
source: this.name,
|
|
774
|
-
timestamp: Date.now()
|
|
775
|
-
};
|
|
776
|
-
this.notifyWatchers(event);
|
|
777
|
-
this.scheduleWrite();
|
|
778
|
-
return true;
|
|
779
|
-
}
|
|
780
|
-
scheduleWrite() {
|
|
781
|
-
if (this.pendingWrite) {
|
|
782
|
-
clearTimeout(this.pendingWrite);
|
|
783
|
-
}
|
|
784
|
-
this.pendingWrite = setTimeout(() => {
|
|
785
|
-
this.persistToFile();
|
|
786
|
-
}, this.debounceMs);
|
|
787
|
-
}
|
|
788
|
-
persistToFile() {
|
|
789
|
-
try {
|
|
790
|
-
const dir = path2.dirname(this.filePath);
|
|
791
|
-
if (!fsSync.existsSync(dir)) {
|
|
792
|
-
fsSync.mkdirSync(dir, { recursive: true });
|
|
793
|
-
}
|
|
794
|
-
const content = JSON.stringify(this.data, null, 2);
|
|
795
|
-
fsSync.writeFileSync(this.filePath, content, "utf-8");
|
|
796
|
-
this.lastFileContent = content;
|
|
797
|
-
} catch (error) {
|
|
798
|
-
console.error(`Failed to write config to ${this.filePath}:`, error);
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
delete(key) {
|
|
802
|
-
const oldValue = this.getNestedValue(this.data, key);
|
|
803
|
-
if (oldValue === undefined) {
|
|
804
|
-
return false;
|
|
805
|
-
}
|
|
806
|
-
this.deleteNestedValue(this.data, key);
|
|
807
|
-
const event = {
|
|
808
|
-
type: "delete",
|
|
809
|
-
key,
|
|
810
|
-
oldValue,
|
|
811
|
-
newValue: undefined,
|
|
812
|
-
source: this.name,
|
|
813
|
-
timestamp: Date.now()
|
|
814
|
-
};
|
|
815
|
-
this.notifyWatchers(event);
|
|
816
|
-
this.scheduleWrite();
|
|
817
|
-
return true;
|
|
818
|
-
}
|
|
819
|
-
list() {
|
|
820
|
-
return this.flattenObject(this.data);
|
|
821
|
-
}
|
|
822
|
-
async listAsync() {
|
|
823
|
-
await this.ensureInit();
|
|
824
|
-
return this.flattenObject(this.data);
|
|
825
|
-
}
|
|
826
|
-
watch(callback) {
|
|
827
|
-
this.watchers.add(callback);
|
|
828
|
-
if (!this.watchEnabled) {
|
|
829
|
-
return () => {
|
|
830
|
-
this.watchers.delete(callback);
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
this.ensureWatch();
|
|
834
|
-
return () => {
|
|
835
|
-
this.watchers.delete(callback);
|
|
836
|
-
if (this.watchers.size === 0) {
|
|
837
|
-
this.stopWatch();
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
}
|
|
841
|
-
async ensureWatch() {
|
|
842
|
-
if (this.watcherHandle !== undefined) {
|
|
843
|
-
return;
|
|
844
|
-
}
|
|
845
|
-
await this.ensureInit();
|
|
846
|
-
if (!fsSync.existsSync(this.filePath)) {
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
try {
|
|
850
|
-
this.watcherHandle = fsSync.watch(this.filePath, async (eventType) => {
|
|
851
|
-
if (eventType === "change") {
|
|
852
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
853
|
-
await this.reloadFromFile();
|
|
854
|
-
}
|
|
855
|
-
});
|
|
856
|
-
} catch (error) {
|
|
857
|
-
console.error(`Failed to watch file ${this.filePath}:`, error);
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
stopWatch() {
|
|
861
|
-
if (this.watcherHandle) {
|
|
862
|
-
this.watcherHandle.close();
|
|
863
|
-
this.watcherHandle = undefined;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
async reloadFromFile() {
|
|
867
|
-
try {
|
|
868
|
-
const content = await fsPromises.readFile(this.filePath, "utf-8");
|
|
869
|
-
if (content === this.lastFileContent) {
|
|
870
|
-
return;
|
|
871
|
-
}
|
|
872
|
-
const newData = parseJSONCWithEnv(content);
|
|
873
|
-
const oldData = this.data;
|
|
874
|
-
this.data = newData;
|
|
875
|
-
this.lastFileContent = content;
|
|
876
|
-
const oldFlat = this.flattenObject(oldData);
|
|
877
|
-
const newFlat = this.flattenObject(newData);
|
|
878
|
-
const oldKeys = new Set(oldFlat.map((e) => e.key));
|
|
879
|
-
const newKeys = new Set(newFlat.map((e) => e.key));
|
|
880
|
-
for (const key of oldKeys) {
|
|
881
|
-
if (!newKeys.has(key)) {
|
|
882
|
-
this.notifyWatchers({
|
|
883
|
-
type: "delete",
|
|
884
|
-
key,
|
|
885
|
-
oldValue: oldFlat.find((e) => e.key === key)?.value,
|
|
886
|
-
newValue: undefined,
|
|
887
|
-
source: this.name,
|
|
888
|
-
timestamp: Date.now()
|
|
889
|
-
});
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
for (const newEntry of newFlat) {
|
|
893
|
-
const oldEntry = oldFlat.find((e) => e.key === newEntry.key);
|
|
894
|
-
if (!oldEntry) {
|
|
895
|
-
this.notifyWatchers({
|
|
896
|
-
type: "add",
|
|
897
|
-
key: newEntry.key,
|
|
898
|
-
oldValue: undefined,
|
|
899
|
-
newValue: newEntry.value,
|
|
900
|
-
source: this.name,
|
|
901
|
-
timestamp: Date.now()
|
|
902
|
-
});
|
|
903
|
-
} else if (oldEntry.value !== newEntry.value) {
|
|
904
|
-
this.notifyWatchers({
|
|
905
|
-
type: "change",
|
|
906
|
-
key: newEntry.key,
|
|
907
|
-
oldValue: oldEntry.value,
|
|
908
|
-
newValue: newEntry.value,
|
|
909
|
-
source: this.name,
|
|
910
|
-
timestamp: Date.now()
|
|
911
|
-
});
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
} catch (error) {
|
|
915
|
-
console.error(`Failed to reload config from ${this.filePath}:`, error);
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
notifyWatchers(event) {
|
|
919
|
-
this.watchers.forEach((cb) => cb(event));
|
|
920
|
-
}
|
|
921
|
-
async refresh() {
|
|
922
|
-
await this.reloadFromFile();
|
|
923
|
-
}
|
|
924
|
-
async close() {
|
|
925
|
-
this.stopWatch();
|
|
926
|
-
if (this.pendingWrite) {
|
|
927
|
-
clearTimeout(this.pendingWrite);
|
|
928
|
-
this.pendingWrite = undefined;
|
|
929
|
-
}
|
|
930
|
-
this.persistToFile();
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
// packages/core/src/config/env-key.ts
|
|
935
|
-
function toEnvKey(key, prefix) {
|
|
936
|
-
let keyNormalized = key.replace(/[.-]/g, "_");
|
|
937
|
-
keyNormalized = keyNormalized.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/_+/g, "_").toUpperCase();
|
|
938
|
-
if (prefix) {
|
|
939
|
-
const separator = prefix.endsWith("_") ? "" : "_";
|
|
940
|
-
return `${prefix}${separator}${keyNormalized}`;
|
|
941
|
-
}
|
|
942
|
-
return keyNormalized;
|
|
943
|
-
}
|
|
944
|
-
function fromEnvKey(envKey, prefix) {
|
|
945
|
-
if (!prefix) {
|
|
946
|
-
return envKey;
|
|
947
|
-
}
|
|
948
|
-
if (envKey.startsWith(prefix)) {
|
|
949
|
-
const afterPrefix = envKey.slice(prefix.length);
|
|
950
|
-
if (afterPrefix.startsWith("_")) {
|
|
951
|
-
return afterPrefix.slice(1);
|
|
952
|
-
}
|
|
953
|
-
return afterPrefix;
|
|
954
|
-
}
|
|
955
|
-
return envKey;
|
|
956
|
-
}
|
|
957
|
-
function envKeyToConfigKey(envKey, prefix, componentName) {
|
|
958
|
-
const prefixUpper = prefix.toUpperCase();
|
|
959
|
-
if (!envKey.startsWith(prefixUpper)) {
|
|
960
|
-
return;
|
|
961
|
-
}
|
|
962
|
-
const componentUpperNormalized = componentName.replace(/-/g, "_").toUpperCase();
|
|
963
|
-
let keyPart = envKey.slice(prefixUpper.length);
|
|
964
|
-
if (keyPart.startsWith("_")) {
|
|
965
|
-
keyPart = keyPart.slice(1);
|
|
966
|
-
}
|
|
967
|
-
if (!keyPart) {
|
|
968
|
-
return;
|
|
969
|
-
}
|
|
970
|
-
const firstUnderscore = keyPart.indexOf("_");
|
|
971
|
-
let restPart;
|
|
972
|
-
if (firstUnderscore === -1) {
|
|
973
|
-
restPart = keyPart;
|
|
974
|
-
} else {
|
|
975
|
-
const firstPart = keyPart.slice(0, firstUnderscore);
|
|
976
|
-
const remaining = keyPart.slice(firstUnderscore + 1);
|
|
977
|
-
if (firstPart === componentUpperNormalized) {
|
|
978
|
-
restPart = remaining;
|
|
979
|
-
} else {
|
|
980
|
-
restPart = keyPart;
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
if (!restPart) {
|
|
984
|
-
return;
|
|
985
|
-
}
|
|
986
|
-
const restPartConverted = restPart.replace(/_/g, ".");
|
|
987
|
-
return `${componentName}.${restPartConverted.toLowerCase()}`;
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
// packages/core/src/config/env-source.ts
|
|
991
|
-
class EnvSource {
|
|
992
|
-
name = "env";
|
|
993
|
-
priority = 20;
|
|
994
|
-
prefix;
|
|
995
|
-
transform;
|
|
996
|
-
pollInterval;
|
|
997
|
-
watchers = new Set;
|
|
998
|
-
pollTimer;
|
|
999
|
-
lastValues = new Map;
|
|
1000
|
-
watchEnabled = true;
|
|
1001
|
-
constructor(options = {}) {
|
|
1002
|
-
this.prefix = options.prefix ?? "";
|
|
1003
|
-
this.transform = options.transform;
|
|
1004
|
-
this.pollInterval = options.pollInterval;
|
|
1005
|
-
this.watchEnabled = options.watch ?? true;
|
|
1006
|
-
}
|
|
1007
|
-
getEnvKey(key) {
|
|
1008
|
-
return toEnvKey(key, this.prefix);
|
|
1009
|
-
}
|
|
1010
|
-
getInternalKey(envKey) {
|
|
1011
|
-
return fromEnvKey(envKey, this.prefix);
|
|
1012
|
-
}
|
|
1013
|
-
read(key) {
|
|
1014
|
-
const envKey = this.getEnvKey(key);
|
|
1015
|
-
const value = process.env[envKey];
|
|
1016
|
-
if (value === undefined) {
|
|
1017
|
-
return;
|
|
1018
|
-
}
|
|
1019
|
-
if (this.transform) {
|
|
1020
|
-
return this.transform(value, key);
|
|
1021
|
-
}
|
|
1022
|
-
return value;
|
|
1023
|
-
}
|
|
1024
|
-
write(key, value) {
|
|
1025
|
-
const envKey = this.getEnvKey(key);
|
|
1026
|
-
const oldValue = process.env[envKey];
|
|
1027
|
-
const stringValue = String(value);
|
|
1028
|
-
process.env[envKey] = stringValue;
|
|
1029
|
-
const event = {
|
|
1030
|
-
type: oldValue === undefined ? "add" : "change",
|
|
1031
|
-
key,
|
|
1032
|
-
oldValue: oldValue !== undefined ? this.transformValue(oldValue, key) : undefined,
|
|
1033
|
-
newValue: this.transformValue(stringValue, key),
|
|
1034
|
-
source: this.name,
|
|
1035
|
-
timestamp: Date.now()
|
|
1036
|
-
};
|
|
1037
|
-
this.notifyWatchers(event);
|
|
1038
|
-
return true;
|
|
1039
|
-
}
|
|
1040
|
-
delete(key) {
|
|
1041
|
-
const envKey = this.getEnvKey(key);
|
|
1042
|
-
const oldValue = process.env[envKey];
|
|
1043
|
-
if (oldValue === undefined) {
|
|
1044
|
-
return false;
|
|
1045
|
-
}
|
|
1046
|
-
delete process.env[envKey];
|
|
1047
|
-
const event = {
|
|
1048
|
-
type: "delete",
|
|
1049
|
-
key,
|
|
1050
|
-
oldValue: this.transformValue(oldValue, key),
|
|
1051
|
-
newValue: undefined,
|
|
1052
|
-
source: this.name,
|
|
1053
|
-
timestamp: Date.now()
|
|
1054
|
-
};
|
|
1055
|
-
this.notifyWatchers(event);
|
|
1056
|
-
return true;
|
|
1057
|
-
}
|
|
1058
|
-
list() {
|
|
1059
|
-
const result = [];
|
|
1060
|
-
const prefix = this.prefix.toUpperCase();
|
|
1061
|
-
for (const envKey of Object.keys(process.env)) {
|
|
1062
|
-
if (prefix && !envKey.startsWith(prefix)) {
|
|
1063
|
-
continue;
|
|
1064
|
-
}
|
|
1065
|
-
const key = this.getInternalKey(envKey);
|
|
1066
|
-
const value = process.env[envKey];
|
|
1067
|
-
if (value !== undefined) {
|
|
1068
|
-
result.push({
|
|
1069
|
-
key,
|
|
1070
|
-
value: this.transformValue(value, key)
|
|
1071
|
-
});
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
return result;
|
|
1075
|
-
}
|
|
1076
|
-
watch(callback) {
|
|
1077
|
-
this.watchers.add(callback);
|
|
1078
|
-
if (!this.watchEnabled) {
|
|
1079
|
-
return () => {
|
|
1080
|
-
this.watchers.delete(callback);
|
|
1081
|
-
};
|
|
1082
|
-
}
|
|
1083
|
-
this.ensurePolling();
|
|
1084
|
-
this.recordCurrentValues();
|
|
1085
|
-
return () => {
|
|
1086
|
-
this.watchers.delete(callback);
|
|
1087
|
-
if (this.watchers.size === 0) {
|
|
1088
|
-
this.stopPolling();
|
|
1089
|
-
}
|
|
1090
|
-
};
|
|
1091
|
-
}
|
|
1092
|
-
ensurePolling() {
|
|
1093
|
-
if (this.pollTimer !== undefined) {
|
|
1094
|
-
return;
|
|
1095
|
-
}
|
|
1096
|
-
const interval = this.pollInterval ?? 1000;
|
|
1097
|
-
this.pollTimer = setInterval(() => {
|
|
1098
|
-
this.checkForChanges();
|
|
1099
|
-
}, interval);
|
|
1100
|
-
}
|
|
1101
|
-
stopPolling() {
|
|
1102
|
-
if (this.pollTimer) {
|
|
1103
|
-
clearInterval(this.pollTimer);
|
|
1104
|
-
this.pollTimer = undefined;
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
recordCurrentValues() {
|
|
1108
|
-
this.lastValues.clear();
|
|
1109
|
-
const entries = this.list();
|
|
1110
|
-
for (const entry of entries) {
|
|
1111
|
-
const envKey = this.getEnvKey(entry.key);
|
|
1112
|
-
const value = process.env[envKey];
|
|
1113
|
-
if (value !== undefined) {
|
|
1114
|
-
this.lastValues.set(envKey, value);
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
checkForChanges() {
|
|
1119
|
-
const currentEntries = this.list();
|
|
1120
|
-
const currentValues = new Map;
|
|
1121
|
-
for (const entry of currentEntries) {
|
|
1122
|
-
const envKey = this.getEnvKey(entry.key);
|
|
1123
|
-
const value = process.env[envKey];
|
|
1124
|
-
if (value !== undefined) {
|
|
1125
|
-
currentValues.set(envKey, value);
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
for (const [envKey, oldValue] of this.lastValues.entries()) {
|
|
1129
|
-
if (!currentValues.has(envKey)) {
|
|
1130
|
-
const key = this.getInternalKey(envKey);
|
|
1131
|
-
this.notifyWatchers({
|
|
1132
|
-
type: "delete",
|
|
1133
|
-
key,
|
|
1134
|
-
oldValue: this.transformValue(oldValue, key),
|
|
1135
|
-
newValue: undefined,
|
|
1136
|
-
source: this.name,
|
|
1137
|
-
timestamp: Date.now()
|
|
1138
|
-
});
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
for (const [envKey, newValue] of currentValues.entries()) {
|
|
1142
|
-
const key = this.getInternalKey(envKey);
|
|
1143
|
-
const oldValue = this.lastValues.get(envKey);
|
|
1144
|
-
if (oldValue === undefined) {
|
|
1145
|
-
this.notifyWatchers({
|
|
1146
|
-
type: "add",
|
|
1147
|
-
key,
|
|
1148
|
-
oldValue: undefined,
|
|
1149
|
-
newValue: this.transformValue(newValue, key),
|
|
1150
|
-
source: this.name,
|
|
1151
|
-
timestamp: Date.now()
|
|
1152
|
-
});
|
|
1153
|
-
} else if (oldValue !== newValue) {
|
|
1154
|
-
this.notifyWatchers({
|
|
1155
|
-
type: "change",
|
|
1156
|
-
key,
|
|
1157
|
-
oldValue: this.transformValue(oldValue, key),
|
|
1158
|
-
newValue: this.transformValue(newValue, key),
|
|
1159
|
-
source: this.name,
|
|
1160
|
-
timestamp: Date.now()
|
|
1161
|
-
});
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
this.lastValues = currentValues;
|
|
1165
|
-
}
|
|
1166
|
-
transformValue(value, key) {
|
|
1167
|
-
if (this.transform) {
|
|
1168
|
-
return this.transform(value, key);
|
|
1169
|
-
}
|
|
1170
|
-
return value;
|
|
1171
|
-
}
|
|
1172
|
-
notifyWatchers(event) {
|
|
1173
|
-
this.watchers.forEach((cb) => cb(event));
|
|
1174
|
-
}
|
|
1175
|
-
close() {
|
|
1176
|
-
this.stopPolling();
|
|
1177
|
-
this.watchers.clear();
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
// packages/core/src/config/config-component.ts
|
|
1182
|
-
import * as os2 from "os";
|
|
1183
|
-
import * as path3 from "path";
|
|
1184
|
-
|
|
1185
|
-
class ConfigSourceNotFoundError extends Error {
|
|
1186
|
-
constructor(sourceType, path4) {
|
|
1187
|
-
const message = path4 ? `Config source not found: ${sourceType} at ${path4}` : `Config source not found: ${sourceType}`;
|
|
1188
|
-
super(message);
|
|
1189
|
-
this.name = "ConfigSourceNotFoundError";
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
class ConfigFileNotFoundError extends Error {
|
|
1194
|
-
constructor(filePath) {
|
|
1195
|
-
super(`Config file not found: ${filePath}`);
|
|
1196
|
-
this.name = "ConfigFileNotFoundError";
|
|
1197
|
-
}
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
class MemorySource {
|
|
1201
|
-
name = "memory";
|
|
1202
|
-
priority = 0;
|
|
1203
|
-
store = new Map;
|
|
1204
|
-
watchers = new Set;
|
|
1205
|
-
read(key) {
|
|
1206
|
-
return this.store.get(key)?.value;
|
|
1207
|
-
}
|
|
1208
|
-
write(key, value) {
|
|
1209
|
-
const oldEntry = this.store.get(key);
|
|
1210
|
-
const entry = {
|
|
1211
|
-
value,
|
|
1212
|
-
timestamp: Date.now(),
|
|
1213
|
-
version: (oldEntry?.version ?? 0) + 1
|
|
1214
|
-
};
|
|
1215
|
-
this.store.set(key, entry);
|
|
1216
|
-
this.notifyWatchers({
|
|
1217
|
-
type: oldEntry ? "change" : "add",
|
|
1218
|
-
key,
|
|
1219
|
-
oldValue: oldEntry?.value,
|
|
1220
|
-
newValue: value,
|
|
1221
|
-
source: "memory",
|
|
1222
|
-
timestamp: entry.timestamp
|
|
1223
|
-
});
|
|
1224
|
-
return true;
|
|
1225
|
-
}
|
|
1226
|
-
delete(key) {
|
|
1227
|
-
const entry = this.store.get(key);
|
|
1228
|
-
if (!entry)
|
|
1229
|
-
return false;
|
|
1230
|
-
this.store.delete(key);
|
|
1231
|
-
this.notifyWatchers({
|
|
1232
|
-
type: "delete",
|
|
1233
|
-
key,
|
|
1234
|
-
oldValue: entry.value,
|
|
1235
|
-
newValue: undefined,
|
|
1236
|
-
source: "memory",
|
|
1237
|
-
timestamp: Date.now()
|
|
1238
|
-
});
|
|
1239
|
-
return true;
|
|
1240
|
-
}
|
|
1241
|
-
list() {
|
|
1242
|
-
return Array.from(this.store.entries()).map(([key, entry]) => ({
|
|
1243
|
-
key,
|
|
1244
|
-
value: entry.value
|
|
1245
|
-
}));
|
|
1246
|
-
}
|
|
1247
|
-
watch(callback) {
|
|
1248
|
-
this.watchers.add(callback);
|
|
1249
|
-
return () => this.watchers.delete(callback);
|
|
1250
|
-
}
|
|
1251
|
-
notifyWatchers(event) {
|
|
1252
|
-
this.watchers.forEach((cb) => cb(event));
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
|
|
1256
|
-
class AsyncPersistQueue {
|
|
1257
|
-
options;
|
|
1258
|
-
queue = new Map;
|
|
1259
|
-
timer;
|
|
1260
|
-
paused = false;
|
|
1261
|
-
constructor(options) {
|
|
1262
|
-
this.options = options;
|
|
1263
|
-
}
|
|
1264
|
-
add(key, value) {
|
|
1265
|
-
this.queue.set(key, { key, value, timestamp: Date.now() });
|
|
1266
|
-
this.scheduleFlush();
|
|
1267
|
-
}
|
|
1268
|
-
scheduleFlush() {
|
|
1269
|
-
if (this.paused || this.timer)
|
|
1270
|
-
return;
|
|
1271
|
-
this.timer = setTimeout(async () => {
|
|
1272
|
-
this.timer = undefined;
|
|
1273
|
-
await this.flush();
|
|
1274
|
-
}, this.options.debounceMs);
|
|
1275
|
-
}
|
|
1276
|
-
async flush() {
|
|
1277
|
-
if (this.queue.size === 0)
|
|
1278
|
-
return;
|
|
1279
|
-
const entries = Array.from(this.queue.values());
|
|
1280
|
-
this.queue.clear();
|
|
1281
|
-
try {
|
|
1282
|
-
await this.options.persistFn(entries);
|
|
1283
|
-
} catch (error) {
|
|
1284
|
-
console.error("Failed to persist config:", error);
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
clear() {
|
|
1288
|
-
this.queue.clear();
|
|
1289
|
-
if (this.timer) {
|
|
1290
|
-
clearTimeout(this.timer);
|
|
1291
|
-
this.timer = undefined;
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
size() {
|
|
1295
|
-
return this.queue.size;
|
|
1296
|
-
}
|
|
1297
|
-
pause() {
|
|
1298
|
-
this.paused = true;
|
|
1299
|
-
}
|
|
1300
|
-
resume() {
|
|
1301
|
-
this.paused = false;
|
|
1302
|
-
if (this.queue.size > 0) {
|
|
1303
|
-
this.scheduleFlush();
|
|
1304
|
-
}
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
class ConfigComponent extends BaseComponent {
|
|
1309
|
-
name = "config";
|
|
1310
|
-
version = "1.0.0";
|
|
1311
|
-
memorySource = new MemorySource;
|
|
1312
|
-
sources = [this.memorySource];
|
|
1313
|
-
componentSchemas = new Map;
|
|
1314
|
-
componentPaths = new Map;
|
|
1315
|
-
watchers = new Map;
|
|
1316
|
-
sourceUnwatchFns = new Map;
|
|
1317
|
-
persistQueue;
|
|
1318
|
-
persistFile;
|
|
1319
|
-
keyRegistry = new Map;
|
|
1320
|
-
componentRegistrations = new Map;
|
|
1321
|
-
protocolResolver;
|
|
1322
|
-
__xdgDataHome;
|
|
1323
|
-
pendingSources = [];
|
|
1324
|
-
sourceKeys = new Map;
|
|
1325
|
-
getXdgDataHome() {
|
|
1326
|
-
return this.__xdgDataHome ?? process.env.XDG_DATA_HOME ?? path3.join(os2.homedir(), ".local", "share");
|
|
1327
|
-
}
|
|
1328
|
-
setXdgDataHome(path4) {
|
|
1329
|
-
this.__xdgDataHome = path4;
|
|
1330
|
-
if (this.protocolResolver) {
|
|
1331
|
-
this.protocolResolver.xdgDataHome = path4;
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
constructor(options = {}) {
|
|
1335
|
-
super();
|
|
1336
|
-
this.persistFile = options.persistFile;
|
|
1337
|
-
this.persistQueue = new AsyncPersistQueue({
|
|
1338
|
-
debounceMs: options.debounceMs ?? 300,
|
|
1339
|
-
persistFn: this.persistToFile.bind(this)
|
|
1340
|
-
});
|
|
1341
|
-
this.protocolResolver = new ProtocolResolver;
|
|
1342
|
-
}
|
|
1343
|
-
register(schema) {
|
|
1344
|
-
this.componentSchemas.set(schema.name, schema);
|
|
1345
|
-
if (schema.paths) {
|
|
1346
|
-
this.componentPaths.set(schema.name, schema.paths);
|
|
1347
|
-
}
|
|
1348
|
-
for (const [key, value] of Object.entries(schema.defaults)) {
|
|
1349
|
-
const fullKey = `${schema.name}.${key}`;
|
|
1350
|
-
if (this.memorySource.read(fullKey) === undefined) {
|
|
1351
|
-
this.memorySource.write(fullKey, value);
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
unregister(name) {
|
|
1356
|
-
this.componentSchemas.delete(name);
|
|
1357
|
-
this.componentPaths.delete(name);
|
|
1358
|
-
return true;
|
|
1359
|
-
}
|
|
1360
|
-
isRegistered(name) {
|
|
1361
|
-
return this.componentSchemas.has(name);
|
|
1362
|
-
}
|
|
1363
|
-
listComponents() {
|
|
1364
|
-
return Array.from(this.componentSchemas.keys());
|
|
1365
|
-
}
|
|
1366
|
-
get(key) {
|
|
1367
|
-
const memoryValue = this.memorySource.read(key);
|
|
1368
|
-
if (memoryValue !== undefined) {
|
|
1369
|
-
return memoryValue;
|
|
1370
|
-
}
|
|
1371
|
-
if (key.includes(".")) {
|
|
1372
|
-
const parts = key.split(".");
|
|
1373
|
-
for (let i = parts.length - 1;i >= 1; i--) {
|
|
1374
|
-
const componentParts = parts.slice(0, i);
|
|
1375
|
-
const component = componentParts.join(".");
|
|
1376
|
-
const restParts = parts.slice(i);
|
|
1377
|
-
const restKey = restParts.join(".");
|
|
1378
|
-
const componentValue = this.memorySource.read(component);
|
|
1379
|
-
if (typeof componentValue === "object" && componentValue !== null) {
|
|
1380
|
-
const nestedValue = this.getNestedValue(componentValue, restKey);
|
|
1381
|
-
if (nestedValue !== undefined) {
|
|
1382
|
-
return nestedValue;
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
return;
|
|
1388
|
-
}
|
|
1389
|
-
getMany(keys) {
|
|
1390
|
-
const result = {};
|
|
1391
|
-
for (const key of keys) {
|
|
1392
|
-
result[key] = this.get(key);
|
|
1393
|
-
}
|
|
1394
|
-
return result;
|
|
1395
|
-
}
|
|
1396
|
-
async set(key, value) {
|
|
1397
|
-
const oldValue = this.get(key);
|
|
1398
|
-
this.memorySource.write(key, value);
|
|
1399
|
-
const event = {
|
|
1400
|
-
component: key.split(".")[0],
|
|
1401
|
-
key,
|
|
1402
|
-
oldValue,
|
|
1403
|
-
newValue: value,
|
|
1404
|
-
persisted: false,
|
|
1405
|
-
timestamp: Date.now()
|
|
1406
|
-
};
|
|
1407
|
-
this.notifyWatchers(key, event);
|
|
1408
|
-
this.persistQueue.add(key, value);
|
|
1409
|
-
}
|
|
1410
|
-
async setMany(config) {
|
|
1411
|
-
for (const [key, value] of Object.entries(config)) {
|
|
1412
|
-
await this.set(key, value);
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
getPath(component, subPath) {
|
|
1416
|
-
const paths = this.componentPaths.get(component);
|
|
1417
|
-
if (!paths) {
|
|
1418
|
-
throw new Error(`Component not registered: ${component}`);
|
|
1419
|
-
}
|
|
1420
|
-
if (subPath && paths[subPath]) {
|
|
1421
|
-
const { base: base2, subPath: relativePath2 } = paths[subPath];
|
|
1422
|
-
return XDG_PATHS.getComponentPath(base2, relativePath2);
|
|
1423
|
-
}
|
|
1424
|
-
const firstKey = Object.keys(paths)[0];
|
|
1425
|
-
if (!firstKey) {
|
|
1426
|
-
throw new Error(`No paths registered for component: ${component}`);
|
|
1427
|
-
}
|
|
1428
|
-
const { base, subPath: relativePath } = paths[firstKey];
|
|
1429
|
-
return XDG_PATHS.getComponentPath(base, relativePath);
|
|
1430
|
-
}
|
|
1431
|
-
async save(key) {
|
|
1432
|
-
await this.persistQueue.flush();
|
|
1433
|
-
}
|
|
1434
|
-
async reset(key) {
|
|
1435
|
-
const [component, ...rest] = key.split(".");
|
|
1436
|
-
const schema = this.componentSchemas.get(component);
|
|
1437
|
-
if (!schema)
|
|
1438
|
-
return;
|
|
1439
|
-
const partialKey = rest.join(".");
|
|
1440
|
-
const defaultValue = schema.defaults[partialKey] ?? schema.defaults[key];
|
|
1441
|
-
if (defaultValue !== undefined) {
|
|
1442
|
-
await this.set(key, defaultValue);
|
|
1443
|
-
}
|
|
1444
|
-
}
|
|
1445
|
-
watch(pattern, callback) {
|
|
1446
|
-
if (!this.watchers.has(pattern)) {
|
|
1447
|
-
this.watchers.set(pattern, new Set);
|
|
1448
|
-
}
|
|
1449
|
-
this.watchers.get(pattern).add(callback);
|
|
1450
|
-
return () => {
|
|
1451
|
-
this.watchers.get(pattern)?.delete(callback);
|
|
1452
|
-
};
|
|
1453
|
-
}
|
|
1454
|
-
addSource(source) {
|
|
1455
|
-
this.sources.push(source);
|
|
1456
|
-
this.sources.sort((a, b) => a.priority - b.priority);
|
|
1457
|
-
if (source.watch) {
|
|
1458
|
-
const callback = (event) => {
|
|
1459
|
-
this.notifyWatchers(event.key, {
|
|
1460
|
-
component: event.key.split(".")[0],
|
|
1461
|
-
key: event.key,
|
|
1462
|
-
oldValue: event.oldValue,
|
|
1463
|
-
newValue: event.newValue,
|
|
1464
|
-
persisted: true,
|
|
1465
|
-
timestamp: event.timestamp
|
|
1466
|
-
});
|
|
1467
|
-
};
|
|
1468
|
-
try {
|
|
1469
|
-
const unwatch = source.watch(callback);
|
|
1470
|
-
const sourceKey = this.getSourceKey(source);
|
|
1471
|
-
this.sourceUnwatchFns.set(sourceKey, unwatch);
|
|
1472
|
-
} catch {}
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
getSourceKey(source) {
|
|
1476
|
-
if ("filePath" in source) {
|
|
1477
|
-
return `file:${source.filePath}`;
|
|
1478
|
-
}
|
|
1479
|
-
if ("prefix" in source) {
|
|
1480
|
-
return `env:${source.prefix}`;
|
|
1481
|
-
}
|
|
1482
|
-
return source.name;
|
|
1483
|
-
}
|
|
1484
|
-
removeSource(name) {
|
|
1485
|
-
const index = this.sources.findIndex((s) => s.name === name);
|
|
1486
|
-
if (index > 0) {
|
|
1487
|
-
const source = this.sources[index];
|
|
1488
|
-
const sourceKey = this.getSourceKey(source);
|
|
1489
|
-
const unwatch = this.sourceUnwatchFns.get(sourceKey);
|
|
1490
|
-
if (unwatch) {
|
|
1491
|
-
unwatch();
|
|
1492
|
-
this.sourceUnwatchFns.delete(sourceKey);
|
|
1493
|
-
}
|
|
1494
|
-
this.sources.splice(index, 1);
|
|
1495
|
-
return true;
|
|
1496
|
-
}
|
|
1497
|
-
return false;
|
|
1498
|
-
}
|
|
1499
|
-
unwatchAll() {
|
|
1500
|
-
for (const unwatch of this.sourceUnwatchFns.values()) {
|
|
1501
|
-
unwatch();
|
|
1502
|
-
}
|
|
1503
|
-
this.sourceUnwatchFns.clear();
|
|
1504
|
-
}
|
|
1505
|
-
getSources() {
|
|
1506
|
-
return [...this.sources];
|
|
1507
|
-
}
|
|
1508
|
-
getNestedValue(obj, key) {
|
|
1509
|
-
const keys = key.split(".");
|
|
1510
|
-
let current = obj;
|
|
1511
|
-
for (const k of keys) {
|
|
1512
|
-
if (typeof current !== "object" || current === null)
|
|
1513
|
-
return;
|
|
1514
|
-
current = current[k];
|
|
1515
|
-
}
|
|
1516
|
-
return current;
|
|
1517
|
-
}
|
|
1518
|
-
notifyWatchers(key, event) {
|
|
1519
|
-
const exactWatchers = this.watchers.get(key);
|
|
1520
|
-
exactWatchers?.forEach((cb) => cb(event));
|
|
1521
|
-
const [component] = key.split(".");
|
|
1522
|
-
const wildcardWatchers = this.watchers.get(`${component}.*`);
|
|
1523
|
-
wildcardWatchers?.forEach((cb) => cb(event));
|
|
1524
|
-
const globalWatchers = this.watchers.get("*");
|
|
1525
|
-
globalWatchers?.forEach((cb) => cb(event));
|
|
1526
|
-
}
|
|
1527
|
-
async persistToFile(entries) {
|
|
1528
|
-
if (!this.persistFile)
|
|
1529
|
-
return;
|
|
1530
|
-
console.log(`[ConfigComponent] Persisting ${entries.length} entries to ${this.persistFile}`);
|
|
1531
|
-
}
|
|
1532
|
-
registerSource(registration) {
|
|
1533
|
-
const { type, relativePath, envPrefix } = registration;
|
|
1534
|
-
const sourceKey = type === "file" && relativePath ? `file:${relativePath}` : `${type}:${envPrefix ?? ""}`;
|
|
1535
|
-
if (this.sourceKeys.has(sourceKey)) {
|
|
1536
|
-
return;
|
|
1537
|
-
}
|
|
1538
|
-
this.pendingSources.push(registration);
|
|
1539
|
-
this.sourceKeys.set(sourceKey, true);
|
|
1540
|
-
}
|
|
1541
|
-
createSource(registration) {
|
|
1542
|
-
const { type, relativePath, envPrefix, priority, optional, watch: watch2 } = registration;
|
|
1543
|
-
switch (type) {
|
|
1544
|
-
case "memory": {
|
|
1545
|
-
return this.memorySource;
|
|
1546
|
-
}
|
|
1547
|
-
case "file": {
|
|
1548
|
-
if (!relativePath) {
|
|
1549
|
-
throw new Error("File source requires relativePath");
|
|
1550
|
-
}
|
|
1551
|
-
const isAbsolutePath = path3.isAbsolute(relativePath) || relativePath.startsWith("file://");
|
|
1552
|
-
let filePath;
|
|
1553
|
-
if (isAbsolutePath) {
|
|
1554
|
-
filePath = relativePath.startsWith("file://") ? relativePath.replace(/^file:\/\//, "") : relativePath;
|
|
1555
|
-
} else {
|
|
1556
|
-
if (this.protocolResolver) {
|
|
1557
|
-
this.protocolResolver.xdgDataHome = this.getXdgDataHome();
|
|
1558
|
-
}
|
|
1559
|
-
filePath = path3.join(this.getXdgDataHome(), relativePath);
|
|
1560
|
-
}
|
|
1561
|
-
return new FileSource({
|
|
1562
|
-
filePath,
|
|
1563
|
-
optional: optional ?? false,
|
|
1564
|
-
resolver: this.protocolResolver,
|
|
1565
|
-
watch: watch2
|
|
1566
|
-
});
|
|
1567
|
-
}
|
|
1568
|
-
case "env": {
|
|
1569
|
-
return new EnvSource({
|
|
1570
|
-
prefix: envPrefix ?? "",
|
|
1571
|
-
priority: priority ?? 20,
|
|
1572
|
-
watch: watch2
|
|
1573
|
-
});
|
|
1574
|
-
}
|
|
1575
|
-
default:
|
|
1576
|
-
throw new Error(`Unknown source type: ${type}`);
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
ensureSourcesCreated() {
|
|
1580
|
-
for (const pending of this.pendingSources) {
|
|
1581
|
-
const source = this.createSource(pending);
|
|
1582
|
-
this.addSource(source);
|
|
1583
|
-
}
|
|
1584
|
-
this.pendingSources = [];
|
|
1585
|
-
}
|
|
1586
|
-
registerKeys(keys) {
|
|
1587
|
-
for (const keyReg of keys) {
|
|
1588
|
-
if (this.keyRegistry.has(keyReg.key)) {
|
|
1589
|
-
continue;
|
|
1590
|
-
}
|
|
1591
|
-
this.keyRegistry.set(keyReg.key, keyReg);
|
|
1592
|
-
if (keyReg.default !== undefined) {
|
|
1593
|
-
if (this.memorySource.read(keyReg.key) === undefined) {
|
|
1594
|
-
this.memorySource.write(keyReg.key, keyReg.default);
|
|
1595
|
-
}
|
|
1596
|
-
}
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
registerComponent(registration) {
|
|
1600
|
-
const { name, sources, keys, defaults } = registration;
|
|
1601
|
-
this.componentRegistrations.set(name, registration);
|
|
1602
|
-
for (const sourceReg of sources) {
|
|
1603
|
-
this.registerSource(sourceReg);
|
|
1604
|
-
}
|
|
1605
|
-
this.registerKeys(keys);
|
|
1606
|
-
if (defaults) {
|
|
1607
|
-
for (const [key, value] of Object.entries(defaults)) {
|
|
1608
|
-
const fullKey = `${name}.${key}`;
|
|
1609
|
-
if (this.memorySource.read(fullKey) === undefined) {
|
|
1610
|
-
this.memorySource.write(fullKey, value);
|
|
1611
|
-
}
|
|
1612
|
-
}
|
|
1613
|
-
}
|
|
1614
|
-
}
|
|
1615
|
-
async load(componentName) {
|
|
1616
|
-
this.ensureSourcesCreated();
|
|
1617
|
-
let keysToLoad;
|
|
1618
|
-
if (componentName) {
|
|
1619
|
-
const registration = this.componentRegistrations.get(componentName);
|
|
1620
|
-
if (!registration) {
|
|
1621
|
-
return;
|
|
1622
|
-
}
|
|
1623
|
-
keysToLoad = registration.keys.map((k) => ({
|
|
1624
|
-
key: k.key,
|
|
1625
|
-
sources: k.sources
|
|
1626
|
-
}));
|
|
1627
|
-
} else {
|
|
1628
|
-
keysToLoad = Array.from(this.keyRegistry.values()).map((k) => ({
|
|
1629
|
-
key: k.key,
|
|
1630
|
-
sources: k.sources
|
|
1631
|
-
}));
|
|
1632
|
-
}
|
|
1633
|
-
const sourcesByType = new Map;
|
|
1634
|
-
for (const source of this.sources) {
|
|
1635
|
-
sourcesByType.set(source.name, source);
|
|
1636
|
-
}
|
|
1637
|
-
for (const { key, sources: sourceTypes } of keysToLoad) {
|
|
1638
|
-
if (this.memorySource.read(key) !== undefined) {
|
|
1639
|
-
continue;
|
|
1640
|
-
}
|
|
1641
|
-
for (const sourceType of sourceTypes) {
|
|
1642
|
-
const source = sourcesByType.get(sourceType);
|
|
1643
|
-
if (!source)
|
|
1644
|
-
continue;
|
|
1645
|
-
const value = source.read(key);
|
|
1646
|
-
if (value !== undefined) {
|
|
1647
|
-
this.memorySource.write(key, value);
|
|
1648
|
-
break;
|
|
1649
|
-
}
|
|
1650
|
-
}
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
getRegisteredSources() {
|
|
1654
|
-
this.ensureSourcesCreated();
|
|
1655
|
-
return [...this.sources];
|
|
1656
|
-
}
|
|
1657
|
-
}
|
|
1658
|
-
export {
|
|
1659
|
-
substituteProtocolRefs,
|
|
1660
|
-
substituteEnvVars,
|
|
1661
|
-
parseJSONCWithProtocols,
|
|
1662
|
-
parseJSONCWithEnv,
|
|
1663
|
-
parseJSONC,
|
|
1664
|
-
MemorySource,
|
|
1665
|
-
FileSource,
|
|
1666
|
-
EnvSource,
|
|
1667
|
-
ConfigSourceNotFoundError,
|
|
1668
|
-
ConfigFileNotFoundError,
|
|
1669
|
-
ConfigComponent
|
|
1670
|
-
};
|