@ai-setting/roy-agent-core 1.5.15-test → 1.5.16-test
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/config/index.d.ts +1250 -0
- package/dist/config/index.js +32 -0
- package/dist/env/agent/index.d.ts +2279 -0
- package/dist/env/agent/index.js +24 -0
- package/dist/env/commands/index.d.ts +1131 -0
- package/dist/env/commands/index.js +14 -0
- package/dist/env/debug/formatters/index.d.ts +236 -0
- package/dist/env/debug/formatters/index.js +11 -0
- package/dist/env/debug/index.d.ts +1652 -0
- package/dist/env/debug/index.js +26 -0
- package/dist/env/hook/index.d.ts +279 -0
- package/dist/env/hook/index.js +29 -0
- package/dist/env/index.d.ts +3481 -0
- package/dist/env/index.js +82 -0
- package/dist/env/llm/index.d.ts +1760 -0
- package/dist/env/llm/index.js +40 -0
- package/dist/env/log-trace/index.d.ts +1574 -0
- package/dist/env/log-trace/index.js +83 -0
- package/dist/env/mcp/index.d.ts +1331 -0
- package/dist/env/mcp/index.js +39 -0
- package/dist/env/mcp/tool/index.d.ts +183 -0
- package/dist/env/mcp/tool/index.js +14 -0
- package/dist/env/memory/built-in/index.d.ts +232 -0
- package/dist/env/memory/built-in/index.js +11 -0
- package/dist/env/memory/index.d.ts +1799 -0
- package/dist/env/memory/index.js +56 -0
- package/dist/env/memory/plugin/index.d.ts +747 -0
- package/dist/env/memory/plugin/index.js +36 -0
- package/dist/env/prompt/index.d.ts +1164 -0
- package/dist/env/prompt/index.js +20 -0
- package/dist/env/session/index.d.ts +1908 -0
- package/dist/env/session/index.js +25 -0
- package/dist/env/session/storage/index.d.ts +564 -0
- package/dist/env/session/storage/index.js +18 -0
- package/dist/env/skill/index.d.ts +1266 -0
- package/dist/env/skill/index.js +34 -0
- package/dist/env/skill/tool/index.d.ts +193 -0
- package/dist/env/skill/tool/index.js +9 -0
- package/dist/env/task/delegate/index.d.ts +1612 -0
- package/dist/env/task/delegate/index.js +18 -0
- package/dist/env/task/events/index.d.ts +171 -0
- package/dist/env/task/events/index.js +7 -0
- package/dist/env/task/hooks/index.d.ts +624 -0
- package/dist/env/task/hooks/index.js +7 -0
- package/dist/env/task/index.d.ts +1553 -0
- package/dist/env/task/index.js +34 -0
- package/dist/env/task/plugins/index.d.ts +466 -0
- package/dist/env/task/plugins/index.js +23 -0
- package/dist/env/task/storage/index.d.ts +241 -0
- package/dist/env/task/storage/index.js +14 -0
- package/dist/env/task/tools/index.d.ts +1485 -0
- package/dist/env/task/tools/index.js +17 -0
- package/dist/env/task/tools/operation/index.d.ts +1484 -0
- package/dist/env/task/tools/operation/index.js +15 -0
- package/dist/env/tool/built-in/index.d.ts +218 -0
- package/dist/env/tool/built-in/index.js +25 -0
- package/dist/env/tool/index.d.ts +1396 -0
- package/dist/env/tool/index.js +39 -0
- package/dist/env/workflow/decorators/index.d.ts +2161 -0
- package/dist/env/workflow/decorators/index.js +27 -0
- package/dist/env/workflow/engine/index.d.ts +3453 -0
- package/dist/env/workflow/engine/index.js +28 -0
- package/dist/env/workflow/index.d.ts +3546 -0
- package/dist/env/workflow/index.js +136 -0
- package/dist/env/workflow/nodes/index.d.ts +2092 -0
- package/dist/env/workflow/nodes/index.js +19 -0
- package/dist/env/workflow/service/index.d.ts +227 -0
- package/dist/env/workflow/service/index.js +13 -0
- package/dist/env/workflow/storage/index.d.ts +165 -0
- package/dist/env/workflow/storage/index.js +27 -0
- package/dist/env/workflow/tools/index.d.ts +416 -0
- package/dist/env/workflow/tools/index.js +159 -0
- package/dist/env/workflow/types/index.d.ts +2255 -0
- package/dist/env/workflow/types/index.js +98 -0
- package/dist/env/workflow/utils/index.d.ts +2031 -0
- package/dist/env/workflow/utils/index.js +637 -0
- package/dist/index.d.ts +7858 -0
- package/dist/index.js +399 -0
- package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +258 -0
- package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
- package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +349 -0
- package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +117 -0
- package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +11 -0
- package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +200 -0
- package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +299 -0
- package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
- package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +350 -0
- package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +180 -0
- package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +408 -0
- package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
- package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +762 -0
- package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +10 -0
- package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +922 -0
- package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +15 -0
- package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +1130 -0
- package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +653 -0
- package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
- package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +872 -0
- package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +11 -0
- package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
- package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +378 -0
- package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +492 -0
- package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +52 -0
- package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
- package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +862 -0
- package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +1387 -0
- package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +208 -0
- package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +139 -0
- package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +534 -0
- package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +204 -0
- package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +603 -0
- package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +97 -0
- package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +587 -0
- package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +176 -0
- package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
- package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +442 -0
- package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +57 -0
- package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +66 -0
- package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +10 -0
- package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +267 -0
- package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
- package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +102 -0
- package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +549 -0
- package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +15 -0
- package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
- package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +812 -0
- package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +1316 -0
- package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +1205 -0
- package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +328 -0
- package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +20 -0
- package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +2098 -0
- package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +419 -0
- package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +368 -0
- package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +377 -0
- package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +172 -0
- package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +103 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
XDG_PATHS
|
|
3
|
+
} from "./roy-agent-core-qxnbvgwe.js";
|
|
4
|
+
import {
|
|
5
|
+
fromEnvKey,
|
|
6
|
+
toEnvKey
|
|
7
|
+
} from "./roy-agent-core-qxhq8ven.js";
|
|
8
|
+
import {
|
|
9
|
+
BaseComponent
|
|
10
|
+
} from "./roy-agent-core-kkbwepqb.js";
|
|
11
|
+
import {
|
|
12
|
+
__require
|
|
13
|
+
} from "./roy-agent-core-fs0mn2jk.js";
|
|
14
|
+
|
|
15
|
+
// src/config/config-parser.ts
|
|
16
|
+
import { parse as parseJSONCInternal, ParseErrorCode } from "jsonc-parser";
|
|
17
|
+
function parseJSONC(content) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
const result = parseJSONCInternal(content, errors, {
|
|
20
|
+
allowTrailingComma: true,
|
|
21
|
+
disallowComments: false
|
|
22
|
+
});
|
|
23
|
+
if (errors.length > 0) {
|
|
24
|
+
const hasOnlyValueExpected = result === undefined && errors.length === 1 && errors[0].error === ParseErrorCode.ValueExpected;
|
|
25
|
+
if (!hasOnlyValueExpected) {
|
|
26
|
+
const error = errors[0];
|
|
27
|
+
const offset = error.offset;
|
|
28
|
+
const lines = content.substring(0, offset).split(`
|
|
29
|
+
`);
|
|
30
|
+
const line = lines.length;
|
|
31
|
+
const col = lines[lines.length - 1].length + 1;
|
|
32
|
+
const errorMessages = {
|
|
33
|
+
[ParseErrorCode.UnexpectedEndOfString]: "Unexpected end of string",
|
|
34
|
+
[ParseErrorCode.InvalidCharacter]: "Invalid character",
|
|
35
|
+
[ParseErrorCode.PropertyNameExpected]: "Property name expected",
|
|
36
|
+
[ParseErrorCode.ValueExpected]: "Value expected",
|
|
37
|
+
[ParseErrorCode.ColonExpected]: "Colon expected",
|
|
38
|
+
[ParseErrorCode.CommaExpected]: "Comma expected",
|
|
39
|
+
[ParseErrorCode.CloseBraceExpected]: "Close brace expected",
|
|
40
|
+
[ParseErrorCode.CloseBracketExpected]: "Close bracket expected",
|
|
41
|
+
[ParseErrorCode.EndOfFileExpected]: "Unexpected end of input",
|
|
42
|
+
[ParseErrorCode.InvalidSymbol]: "Invalid symbol"
|
|
43
|
+
};
|
|
44
|
+
throw new Error(`JSONC parse error at line ${line}, col ${col}: ${errorMessages[error.error] || "Unknown error"}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (result === undefined || result === null) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
function substituteEnvVars(value) {
|
|
53
|
+
if (typeof value === "string") {
|
|
54
|
+
return value.replace(/\$\{([^}]+)\}/g, (match, varExpr) => {
|
|
55
|
+
const [varName, defaultValue] = varExpr.split(":-");
|
|
56
|
+
const envValue = process.env[varName.trim()];
|
|
57
|
+
if (envValue !== undefined) {
|
|
58
|
+
return envValue;
|
|
59
|
+
}
|
|
60
|
+
if (defaultValue !== undefined) {
|
|
61
|
+
return defaultValue;
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return value.map((item) => substituteEnvVars(item));
|
|
68
|
+
}
|
|
69
|
+
if (value !== null && typeof value === "object") {
|
|
70
|
+
const result = {};
|
|
71
|
+
for (const [key, val] of Object.entries(value)) {
|
|
72
|
+
result[key] = substituteEnvVars(val);
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
function parseJSONCWithEnv(content) {
|
|
79
|
+
const parsed = parseJSONC(content);
|
|
80
|
+
return substituteEnvVars(parsed);
|
|
81
|
+
}
|
|
82
|
+
var PROTOCOL_REF_PATTERN = /^\$\{([^:]+):\/\/[^}]+\}$/;
|
|
83
|
+
var PROTOCOL_REF_WITH_DEFAULT_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
84
|
+
function isProtocolRefString(value) {
|
|
85
|
+
return PROTOCOL_REF_PATTERN.test(value);
|
|
86
|
+
}
|
|
87
|
+
function extractDefaultValue(ref) {
|
|
88
|
+
const match = ref.match(PROTOCOL_REF_WITH_DEFAULT_PATTERN);
|
|
89
|
+
return match ? match[4] : undefined;
|
|
90
|
+
}
|
|
91
|
+
function substituteProtocolRefs(value, resolver) {
|
|
92
|
+
if (typeof value === "string") {
|
|
93
|
+
if (isProtocolRefString(value)) {
|
|
94
|
+
const result = resolver.resolve(value);
|
|
95
|
+
if (result !== null) {
|
|
96
|
+
return result.value;
|
|
97
|
+
}
|
|
98
|
+
const defaultValue = extractDefaultValue(value);
|
|
99
|
+
if (defaultValue !== undefined) {
|
|
100
|
+
return defaultValue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
return value.map((item) => substituteProtocolRefs(item, resolver));
|
|
107
|
+
}
|
|
108
|
+
if (value !== null && typeof value === "object") {
|
|
109
|
+
const result = {};
|
|
110
|
+
for (const [key, val] of Object.entries(value)) {
|
|
111
|
+
result[key] = substituteProtocolRefs(val, resolver);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
function parseJSONCWithProtocols(content, resolver) {
|
|
118
|
+
const parsed = parseJSONC(content);
|
|
119
|
+
const withProtocols = substituteProtocolRefs(parsed, resolver);
|
|
120
|
+
return substituteEnvVars(withProtocols);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/config/file-source.ts
|
|
124
|
+
import * as fsSync from "fs";
|
|
125
|
+
import * as fsPromises from "fs/promises";
|
|
126
|
+
import path from "path";
|
|
127
|
+
class FileSource {
|
|
128
|
+
name = "file";
|
|
129
|
+
priority = 10;
|
|
130
|
+
filePath;
|
|
131
|
+
data = {};
|
|
132
|
+
watchers = new Set;
|
|
133
|
+
watcherHandle;
|
|
134
|
+
debounceMs;
|
|
135
|
+
pendingWrite;
|
|
136
|
+
lastFileContent = "";
|
|
137
|
+
resolver;
|
|
138
|
+
optional = false;
|
|
139
|
+
watchEnabled = true;
|
|
140
|
+
constructor(options, resolver) {
|
|
141
|
+
if (typeof options === "string") {
|
|
142
|
+
this.filePath = options;
|
|
143
|
+
this.debounceMs = 100;
|
|
144
|
+
this.resolver = resolver;
|
|
145
|
+
this.optional = true;
|
|
146
|
+
} else {
|
|
147
|
+
this.filePath = options.filePath;
|
|
148
|
+
this.debounceMs = options.debounceMs ?? 100;
|
|
149
|
+
this.resolver = options.resolver ?? resolver;
|
|
150
|
+
this.optional = options.optional ?? true;
|
|
151
|
+
this.watchEnabled = options.watch ?? true;
|
|
152
|
+
}
|
|
153
|
+
this.initSync();
|
|
154
|
+
}
|
|
155
|
+
initSync() {
|
|
156
|
+
try {
|
|
157
|
+
if (fsSync.existsSync(this.filePath)) {
|
|
158
|
+
const content = fsSync.readFileSync(this.filePath, "utf-8");
|
|
159
|
+
if (this.resolver) {
|
|
160
|
+
this.data = parseJSONCWithProtocols(content, this.resolver);
|
|
161
|
+
} else {
|
|
162
|
+
this.data = parseJSONCWithEnv(content);
|
|
163
|
+
}
|
|
164
|
+
this.lastFileContent = content;
|
|
165
|
+
} else {
|
|
166
|
+
if (this.optional) {
|
|
167
|
+
this.data = {};
|
|
168
|
+
this.lastFileContent = "{}";
|
|
169
|
+
} else {
|
|
170
|
+
throw new Error(`Config file not found: ${this.filePath}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (this.optional) {
|
|
175
|
+
this.data = {};
|
|
176
|
+
this.lastFileContent = "{}";
|
|
177
|
+
} else {
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async ensureInit() {
|
|
183
|
+
if (this.lastFileContent === "" && !fsSync.existsSync(this.filePath)) {
|
|
184
|
+
this.initSync();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
getNestedValue(obj, key) {
|
|
188
|
+
const keys = key.split(".");
|
|
189
|
+
let current = obj;
|
|
190
|
+
for (const k of keys) {
|
|
191
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
current = current[k];
|
|
195
|
+
}
|
|
196
|
+
return current;
|
|
197
|
+
}
|
|
198
|
+
setNestedValue(obj, key, value) {
|
|
199
|
+
const keys = key.split(".");
|
|
200
|
+
let current = obj;
|
|
201
|
+
for (let i = 0;i < keys.length - 1; i++) {
|
|
202
|
+
const k = keys[i];
|
|
203
|
+
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
204
|
+
current[k] = {};
|
|
205
|
+
}
|
|
206
|
+
current = current[k];
|
|
207
|
+
}
|
|
208
|
+
current[keys[keys.length - 1]] = value;
|
|
209
|
+
}
|
|
210
|
+
deleteNestedValue(obj, key) {
|
|
211
|
+
const keys = key.split(".");
|
|
212
|
+
let current = obj;
|
|
213
|
+
for (let i = 0;i < keys.length - 1; i++) {
|
|
214
|
+
const k = keys[i];
|
|
215
|
+
if (!(k in current) || typeof current[k] !== "object" || current[k] === null) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
current = current[k];
|
|
219
|
+
}
|
|
220
|
+
const lastKey = keys[keys.length - 1];
|
|
221
|
+
if (lastKey in current) {
|
|
222
|
+
delete current[lastKey];
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
flattenObject(obj, prefix = "") {
|
|
228
|
+
if (obj === null || obj === undefined) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
if (typeof obj !== "object") {
|
|
232
|
+
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
233
|
+
}
|
|
234
|
+
if (Array.isArray(obj)) {
|
|
235
|
+
return prefix ? [{ key: prefix, value: obj }] : [];
|
|
236
|
+
}
|
|
237
|
+
const result = [];
|
|
238
|
+
const entries = Object.entries(obj);
|
|
239
|
+
for (const [k, v] of entries) {
|
|
240
|
+
const newKey = prefix ? `${prefix}.${k}` : k;
|
|
241
|
+
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
|
|
242
|
+
result.push(...this.flattenObject(v, newKey));
|
|
243
|
+
} else {
|
|
244
|
+
result.push({ key: newKey, value: v });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
read(key) {
|
|
250
|
+
return this.getNestedValue(this.data, key);
|
|
251
|
+
}
|
|
252
|
+
async readAsync(key) {
|
|
253
|
+
await this.ensureInit();
|
|
254
|
+
return this.getNestedValue(this.data, key);
|
|
255
|
+
}
|
|
256
|
+
write(key, value) {
|
|
257
|
+
const oldValue = this.getNestedValue(this.data, key);
|
|
258
|
+
this.setNestedValue(this.data, key, value);
|
|
259
|
+
const event = {
|
|
260
|
+
type: oldValue === undefined ? "add" : "change",
|
|
261
|
+
key,
|
|
262
|
+
oldValue,
|
|
263
|
+
newValue: value,
|
|
264
|
+
source: this.name,
|
|
265
|
+
timestamp: Date.now()
|
|
266
|
+
};
|
|
267
|
+
this.notifyWatchers(event);
|
|
268
|
+
this.scheduleWrite();
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
scheduleWrite() {
|
|
272
|
+
if (this.pendingWrite) {
|
|
273
|
+
clearTimeout(this.pendingWrite);
|
|
274
|
+
}
|
|
275
|
+
this.pendingWrite = setTimeout(() => {
|
|
276
|
+
this.persistToFile();
|
|
277
|
+
}, this.debounceMs);
|
|
278
|
+
}
|
|
279
|
+
persistToFile() {
|
|
280
|
+
try {
|
|
281
|
+
const dir = path.dirname(this.filePath);
|
|
282
|
+
if (!fsSync.existsSync(dir)) {
|
|
283
|
+
fsSync.mkdirSync(dir, { recursive: true });
|
|
284
|
+
}
|
|
285
|
+
const content = JSON.stringify(this.data, null, 2);
|
|
286
|
+
fsSync.writeFileSync(this.filePath, content, "utf-8");
|
|
287
|
+
this.lastFileContent = content;
|
|
288
|
+
} catch (error) {
|
|
289
|
+
console.error(`Failed to write config to ${this.filePath}:`, error);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
delete(key) {
|
|
293
|
+
const oldValue = this.getNestedValue(this.data, key);
|
|
294
|
+
if (oldValue === undefined) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
this.deleteNestedValue(this.data, key);
|
|
298
|
+
const event = {
|
|
299
|
+
type: "delete",
|
|
300
|
+
key,
|
|
301
|
+
oldValue,
|
|
302
|
+
newValue: undefined,
|
|
303
|
+
source: this.name,
|
|
304
|
+
timestamp: Date.now()
|
|
305
|
+
};
|
|
306
|
+
this.notifyWatchers(event);
|
|
307
|
+
this.scheduleWrite();
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
list() {
|
|
311
|
+
return this.flattenObject(this.data);
|
|
312
|
+
}
|
|
313
|
+
async listAsync() {
|
|
314
|
+
await this.ensureInit();
|
|
315
|
+
return this.flattenObject(this.data);
|
|
316
|
+
}
|
|
317
|
+
watch(callback) {
|
|
318
|
+
this.watchers.add(callback);
|
|
319
|
+
if (!this.watchEnabled) {
|
|
320
|
+
return () => {
|
|
321
|
+
this.watchers.delete(callback);
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
this.ensureWatch();
|
|
325
|
+
return () => {
|
|
326
|
+
this.watchers.delete(callback);
|
|
327
|
+
if (this.watchers.size === 0) {
|
|
328
|
+
this.stopWatch();
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
async ensureWatch() {
|
|
333
|
+
if (this.watcherHandle !== undefined) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
await this.ensureInit();
|
|
337
|
+
if (!fsSync.existsSync(this.filePath)) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
this.watcherHandle = fsSync.watch(this.filePath, async (eventType) => {
|
|
342
|
+
if (eventType === "change") {
|
|
343
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
344
|
+
await this.reloadFromFile();
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
} catch (error) {
|
|
348
|
+
console.error(`Failed to watch file ${this.filePath}:`, error);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
stopWatch() {
|
|
352
|
+
if (this.watcherHandle) {
|
|
353
|
+
this.watcherHandle.close();
|
|
354
|
+
this.watcherHandle = undefined;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async reloadFromFile() {
|
|
358
|
+
try {
|
|
359
|
+
const content = await fsPromises.readFile(this.filePath, "utf-8");
|
|
360
|
+
if (content === this.lastFileContent) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const newData = parseJSONCWithEnv(content);
|
|
364
|
+
const oldData = this.data;
|
|
365
|
+
this.data = newData;
|
|
366
|
+
this.lastFileContent = content;
|
|
367
|
+
const oldFlat = this.flattenObject(oldData);
|
|
368
|
+
const newFlat = this.flattenObject(newData);
|
|
369
|
+
const oldKeys = new Set(oldFlat.map((e) => e.key));
|
|
370
|
+
const newKeys = new Set(newFlat.map((e) => e.key));
|
|
371
|
+
for (const key of oldKeys) {
|
|
372
|
+
if (!newKeys.has(key)) {
|
|
373
|
+
this.notifyWatchers({
|
|
374
|
+
type: "delete",
|
|
375
|
+
key,
|
|
376
|
+
oldValue: oldFlat.find((e) => e.key === key)?.value,
|
|
377
|
+
newValue: undefined,
|
|
378
|
+
source: this.name,
|
|
379
|
+
timestamp: Date.now()
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
for (const newEntry of newFlat) {
|
|
384
|
+
const oldEntry = oldFlat.find((e) => e.key === newEntry.key);
|
|
385
|
+
if (!oldEntry) {
|
|
386
|
+
this.notifyWatchers({
|
|
387
|
+
type: "add",
|
|
388
|
+
key: newEntry.key,
|
|
389
|
+
oldValue: undefined,
|
|
390
|
+
newValue: newEntry.value,
|
|
391
|
+
source: this.name,
|
|
392
|
+
timestamp: Date.now()
|
|
393
|
+
});
|
|
394
|
+
} else if (oldEntry.value !== newEntry.value) {
|
|
395
|
+
this.notifyWatchers({
|
|
396
|
+
type: "change",
|
|
397
|
+
key: newEntry.key,
|
|
398
|
+
oldValue: oldEntry.value,
|
|
399
|
+
newValue: newEntry.value,
|
|
400
|
+
source: this.name,
|
|
401
|
+
timestamp: Date.now()
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.error(`Failed to reload config from ${this.filePath}:`, error);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
notifyWatchers(event) {
|
|
410
|
+
this.watchers.forEach((cb) => cb(event));
|
|
411
|
+
}
|
|
412
|
+
async refresh() {
|
|
413
|
+
await this.reloadFromFile();
|
|
414
|
+
}
|
|
415
|
+
async close() {
|
|
416
|
+
this.stopWatch();
|
|
417
|
+
if (this.pendingWrite) {
|
|
418
|
+
clearTimeout(this.pendingWrite);
|
|
419
|
+
this.pendingWrite = undefined;
|
|
420
|
+
}
|
|
421
|
+
this.persistToFile();
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// src/config/env-source.ts
|
|
426
|
+
class EnvSource {
|
|
427
|
+
name = "env";
|
|
428
|
+
priority = 20;
|
|
429
|
+
prefix;
|
|
430
|
+
transform;
|
|
431
|
+
pollInterval;
|
|
432
|
+
watchers = new Set;
|
|
433
|
+
pollTimer;
|
|
434
|
+
lastValues = new Map;
|
|
435
|
+
watchEnabled = true;
|
|
436
|
+
constructor(options = {}) {
|
|
437
|
+
this.prefix = options.prefix ?? "";
|
|
438
|
+
this.transform = options.transform;
|
|
439
|
+
this.pollInterval = options.pollInterval;
|
|
440
|
+
this.watchEnabled = options.watch ?? true;
|
|
441
|
+
}
|
|
442
|
+
getEnvKey(key) {
|
|
443
|
+
return toEnvKey(key, this.prefix);
|
|
444
|
+
}
|
|
445
|
+
getInternalKey(envKey) {
|
|
446
|
+
return fromEnvKey(envKey, this.prefix);
|
|
447
|
+
}
|
|
448
|
+
read(key) {
|
|
449
|
+
const envKey = this.getEnvKey(key);
|
|
450
|
+
const value = process.env[envKey];
|
|
451
|
+
if (value === undefined) {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (this.transform) {
|
|
455
|
+
return this.transform(value, key);
|
|
456
|
+
}
|
|
457
|
+
return value;
|
|
458
|
+
}
|
|
459
|
+
write(key, value) {
|
|
460
|
+
const envKey = this.getEnvKey(key);
|
|
461
|
+
const oldValue = process.env[envKey];
|
|
462
|
+
const stringValue = String(value);
|
|
463
|
+
process.env[envKey] = stringValue;
|
|
464
|
+
const event = {
|
|
465
|
+
type: oldValue === undefined ? "add" : "change",
|
|
466
|
+
key,
|
|
467
|
+
oldValue: oldValue !== undefined ? this.transformValue(oldValue, key) : undefined,
|
|
468
|
+
newValue: this.transformValue(stringValue, key),
|
|
469
|
+
source: this.name,
|
|
470
|
+
timestamp: Date.now()
|
|
471
|
+
};
|
|
472
|
+
this.notifyWatchers(event);
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
delete(key) {
|
|
476
|
+
const envKey = this.getEnvKey(key);
|
|
477
|
+
const oldValue = process.env[envKey];
|
|
478
|
+
if (oldValue === undefined) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
delete process.env[envKey];
|
|
482
|
+
const event = {
|
|
483
|
+
type: "delete",
|
|
484
|
+
key,
|
|
485
|
+
oldValue: this.transformValue(oldValue, key),
|
|
486
|
+
newValue: undefined,
|
|
487
|
+
source: this.name,
|
|
488
|
+
timestamp: Date.now()
|
|
489
|
+
};
|
|
490
|
+
this.notifyWatchers(event);
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
list() {
|
|
494
|
+
const result = [];
|
|
495
|
+
const prefix = this.prefix.toUpperCase();
|
|
496
|
+
for (const envKey of Object.keys(process.env)) {
|
|
497
|
+
if (prefix && !envKey.startsWith(prefix)) {
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const key = this.getInternalKey(envKey);
|
|
501
|
+
const value = process.env[envKey];
|
|
502
|
+
if (value !== undefined) {
|
|
503
|
+
result.push({
|
|
504
|
+
key,
|
|
505
|
+
value: this.transformValue(value, key)
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return result;
|
|
510
|
+
}
|
|
511
|
+
watch(callback) {
|
|
512
|
+
this.watchers.add(callback);
|
|
513
|
+
if (!this.watchEnabled) {
|
|
514
|
+
return () => {
|
|
515
|
+
this.watchers.delete(callback);
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
this.ensurePolling();
|
|
519
|
+
this.recordCurrentValues();
|
|
520
|
+
return () => {
|
|
521
|
+
this.watchers.delete(callback);
|
|
522
|
+
if (this.watchers.size === 0) {
|
|
523
|
+
this.stopPolling();
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
ensurePolling() {
|
|
528
|
+
if (this.pollTimer !== undefined) {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
const interval = this.pollInterval ?? 1000;
|
|
532
|
+
this.pollTimer = setInterval(() => {
|
|
533
|
+
this.checkForChanges();
|
|
534
|
+
}, interval);
|
|
535
|
+
}
|
|
536
|
+
stopPolling() {
|
|
537
|
+
if (this.pollTimer) {
|
|
538
|
+
clearInterval(this.pollTimer);
|
|
539
|
+
this.pollTimer = undefined;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
recordCurrentValues() {
|
|
543
|
+
this.lastValues.clear();
|
|
544
|
+
const entries = this.list();
|
|
545
|
+
for (const entry of entries) {
|
|
546
|
+
const envKey = this.getEnvKey(entry.key);
|
|
547
|
+
const value = process.env[envKey];
|
|
548
|
+
if (value !== undefined) {
|
|
549
|
+
this.lastValues.set(envKey, value);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
checkForChanges() {
|
|
554
|
+
const currentEntries = this.list();
|
|
555
|
+
const currentValues = new Map;
|
|
556
|
+
for (const entry of currentEntries) {
|
|
557
|
+
const envKey = this.getEnvKey(entry.key);
|
|
558
|
+
const value = process.env[envKey];
|
|
559
|
+
if (value !== undefined) {
|
|
560
|
+
currentValues.set(envKey, value);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
for (const [envKey, oldValue] of this.lastValues.entries()) {
|
|
564
|
+
if (!currentValues.has(envKey)) {
|
|
565
|
+
const key = this.getInternalKey(envKey);
|
|
566
|
+
this.notifyWatchers({
|
|
567
|
+
type: "delete",
|
|
568
|
+
key,
|
|
569
|
+
oldValue: this.transformValue(oldValue, key),
|
|
570
|
+
newValue: undefined,
|
|
571
|
+
source: this.name,
|
|
572
|
+
timestamp: Date.now()
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
for (const [envKey, newValue] of currentValues.entries()) {
|
|
577
|
+
const key = this.getInternalKey(envKey);
|
|
578
|
+
const oldValue = this.lastValues.get(envKey);
|
|
579
|
+
if (oldValue === undefined) {
|
|
580
|
+
this.notifyWatchers({
|
|
581
|
+
type: "add",
|
|
582
|
+
key,
|
|
583
|
+
oldValue: undefined,
|
|
584
|
+
newValue: this.transformValue(newValue, key),
|
|
585
|
+
source: this.name,
|
|
586
|
+
timestamp: Date.now()
|
|
587
|
+
});
|
|
588
|
+
} else if (oldValue !== newValue) {
|
|
589
|
+
this.notifyWatchers({
|
|
590
|
+
type: "change",
|
|
591
|
+
key,
|
|
592
|
+
oldValue: this.transformValue(oldValue, key),
|
|
593
|
+
newValue: this.transformValue(newValue, key),
|
|
594
|
+
source: this.name,
|
|
595
|
+
timestamp: Date.now()
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
this.lastValues = currentValues;
|
|
600
|
+
}
|
|
601
|
+
transformValue(value, key) {
|
|
602
|
+
if (this.transform) {
|
|
603
|
+
return this.transform(value, key);
|
|
604
|
+
}
|
|
605
|
+
return value;
|
|
606
|
+
}
|
|
607
|
+
notifyWatchers(event) {
|
|
608
|
+
this.watchers.forEach((cb) => cb(event));
|
|
609
|
+
}
|
|
610
|
+
close() {
|
|
611
|
+
this.stopPolling();
|
|
612
|
+
this.watchers.clear();
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// src/config/protocol-resolver.ts
|
|
617
|
+
var PROTOCOL_PATTERN = /^\$\{([^:]+):\/\/([^:#]+)(?:#([^:-]+))?(?::-(.*))?\}$/;
|
|
618
|
+
|
|
619
|
+
class ProtocolResolver {
|
|
620
|
+
_xdgDataHome;
|
|
621
|
+
readFileFn;
|
|
622
|
+
constructor(options = {}) {
|
|
623
|
+
this._xdgDataHome = options.xdgDataHome ?? ProtocolResolver.getDefaultXDGDataHome();
|
|
624
|
+
if (options.readFileFn) {
|
|
625
|
+
this.readFileFn = options.readFileFn;
|
|
626
|
+
} else {
|
|
627
|
+
const fsSync2 = __require("fs");
|
|
628
|
+
this.readFileFn = (path2) => fsSync2.readFileSync(path2, "utf-8");
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
get xdgDataHome() {
|
|
632
|
+
return this._xdgDataHome;
|
|
633
|
+
}
|
|
634
|
+
set xdgDataHome(value) {
|
|
635
|
+
this._xdgDataHome = value;
|
|
636
|
+
}
|
|
637
|
+
static getDefaultXDGDataHome() {
|
|
638
|
+
return process.env.XDG_DATA_HOME ?? `${process.env.HOME ?? "~"}/.local/share`;
|
|
639
|
+
}
|
|
640
|
+
parse(ref) {
|
|
641
|
+
const match = ref.match(PROTOCOL_PATTERN);
|
|
642
|
+
if (!match)
|
|
643
|
+
return null;
|
|
644
|
+
const [, protocol, path2, key, defaultValue] = match;
|
|
645
|
+
if (!["file", "env"].includes(protocol))
|
|
646
|
+
return null;
|
|
647
|
+
return {
|
|
648
|
+
protocol,
|
|
649
|
+
path: path2,
|
|
650
|
+
key: key ?? "",
|
|
651
|
+
defaultValue,
|
|
652
|
+
raw: ref
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
isProtocolRef(value) {
|
|
656
|
+
return PROTOCOL_PATTERN.test(value);
|
|
657
|
+
}
|
|
658
|
+
resolve(ref) {
|
|
659
|
+
const parsed = this.parse(ref);
|
|
660
|
+
if (!parsed)
|
|
661
|
+
return null;
|
|
662
|
+
const value = this.resolveInternal(parsed);
|
|
663
|
+
if (parsed.protocol === "file" && value === undefined) {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
if (value === undefined && parsed.protocol === "env" && parsed.defaultValue === undefined) {
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
if (value === undefined && parsed.defaultValue !== undefined) {
|
|
670
|
+
return {
|
|
671
|
+
value: parsed.defaultValue,
|
|
672
|
+
source: parsed.protocol
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
return {
|
|
676
|
+
value,
|
|
677
|
+
source: parsed.protocol
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
resolveFileRef(filePath, key) {
|
|
681
|
+
const resolvedPath = this.resolvePath(filePath);
|
|
682
|
+
try {
|
|
683
|
+
const content = this.readFileFn(resolvedPath);
|
|
684
|
+
let data;
|
|
685
|
+
try {
|
|
686
|
+
data = JSON.parse(content);
|
|
687
|
+
} catch {
|
|
688
|
+
return content;
|
|
689
|
+
}
|
|
690
|
+
return this.getNestedValue(data, key);
|
|
691
|
+
} catch {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
resolveInternal(ref) {
|
|
696
|
+
switch (ref.protocol) {
|
|
697
|
+
case "env":
|
|
698
|
+
return process.env[ref.path];
|
|
699
|
+
case "file":
|
|
700
|
+
return this.resolveFileRef(ref.path, ref.key);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
resolvePath(filePath) {
|
|
704
|
+
if (filePath.startsWith("/")) {
|
|
705
|
+
return filePath;
|
|
706
|
+
}
|
|
707
|
+
if (filePath.startsWith("~")) {
|
|
708
|
+
return filePath.replace("~", process.env.HOME ?? "");
|
|
709
|
+
}
|
|
710
|
+
return `${this._xdgDataHome}/${filePath}`;
|
|
711
|
+
}
|
|
712
|
+
getNestedValue(obj, key) {
|
|
713
|
+
if (key === "$" || key === "")
|
|
714
|
+
return obj;
|
|
715
|
+
const keys = key.split(".");
|
|
716
|
+
let current = obj;
|
|
717
|
+
for (const k of keys) {
|
|
718
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
current = current[k];
|
|
722
|
+
}
|
|
723
|
+
return current;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// src/config/config-component.ts
|
|
728
|
+
import * as os from "os";
|
|
729
|
+
import * as path2 from "path";
|
|
730
|
+
|
|
731
|
+
class ConfigSourceNotFoundError extends Error {
|
|
732
|
+
constructor(sourceType, path3) {
|
|
733
|
+
const message = path3 ? `Config source not found: ${sourceType} at ${path3}` : `Config source not found: ${sourceType}`;
|
|
734
|
+
super(message);
|
|
735
|
+
this.name = "ConfigSourceNotFoundError";
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
class ConfigFileNotFoundError extends Error {
|
|
740
|
+
constructor(filePath) {
|
|
741
|
+
super(`Config file not found: ${filePath}`);
|
|
742
|
+
this.name = "ConfigFileNotFoundError";
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
class MemorySource {
|
|
747
|
+
name = "memory";
|
|
748
|
+
priority = 0;
|
|
749
|
+
store = new Map;
|
|
750
|
+
watchers = new Set;
|
|
751
|
+
read(key) {
|
|
752
|
+
return this.store.get(key)?.value;
|
|
753
|
+
}
|
|
754
|
+
write(key, value) {
|
|
755
|
+
const oldEntry = this.store.get(key);
|
|
756
|
+
const entry = {
|
|
757
|
+
value,
|
|
758
|
+
timestamp: Date.now(),
|
|
759
|
+
version: (oldEntry?.version ?? 0) + 1
|
|
760
|
+
};
|
|
761
|
+
this.store.set(key, entry);
|
|
762
|
+
this.notifyWatchers({
|
|
763
|
+
type: oldEntry ? "change" : "add",
|
|
764
|
+
key,
|
|
765
|
+
oldValue: oldEntry?.value,
|
|
766
|
+
newValue: value,
|
|
767
|
+
source: "memory",
|
|
768
|
+
timestamp: entry.timestamp
|
|
769
|
+
});
|
|
770
|
+
return true;
|
|
771
|
+
}
|
|
772
|
+
delete(key) {
|
|
773
|
+
const entry = this.store.get(key);
|
|
774
|
+
if (!entry)
|
|
775
|
+
return false;
|
|
776
|
+
this.store.delete(key);
|
|
777
|
+
this.notifyWatchers({
|
|
778
|
+
type: "delete",
|
|
779
|
+
key,
|
|
780
|
+
oldValue: entry.value,
|
|
781
|
+
newValue: undefined,
|
|
782
|
+
source: "memory",
|
|
783
|
+
timestamp: Date.now()
|
|
784
|
+
});
|
|
785
|
+
return true;
|
|
786
|
+
}
|
|
787
|
+
list() {
|
|
788
|
+
return Array.from(this.store.entries()).map(([key, entry]) => ({
|
|
789
|
+
key,
|
|
790
|
+
value: entry.value
|
|
791
|
+
}));
|
|
792
|
+
}
|
|
793
|
+
watch(callback) {
|
|
794
|
+
this.watchers.add(callback);
|
|
795
|
+
return () => this.watchers.delete(callback);
|
|
796
|
+
}
|
|
797
|
+
notifyWatchers(event) {
|
|
798
|
+
this.watchers.forEach((cb) => cb(event));
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
class AsyncPersistQueue {
|
|
803
|
+
options;
|
|
804
|
+
queue = new Map;
|
|
805
|
+
timer;
|
|
806
|
+
paused = false;
|
|
807
|
+
constructor(options) {
|
|
808
|
+
this.options = options;
|
|
809
|
+
}
|
|
810
|
+
add(key, value) {
|
|
811
|
+
this.queue.set(key, { key, value, timestamp: Date.now() });
|
|
812
|
+
this.scheduleFlush();
|
|
813
|
+
}
|
|
814
|
+
scheduleFlush() {
|
|
815
|
+
if (this.paused || this.timer)
|
|
816
|
+
return;
|
|
817
|
+
this.timer = setTimeout(async () => {
|
|
818
|
+
this.timer = undefined;
|
|
819
|
+
await this.flush();
|
|
820
|
+
}, this.options.debounceMs);
|
|
821
|
+
}
|
|
822
|
+
async flush() {
|
|
823
|
+
if (this.queue.size === 0)
|
|
824
|
+
return;
|
|
825
|
+
const entries = Array.from(this.queue.values());
|
|
826
|
+
this.queue.clear();
|
|
827
|
+
try {
|
|
828
|
+
await this.options.persistFn(entries);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
console.error("Failed to persist config:", error);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
clear() {
|
|
834
|
+
this.queue.clear();
|
|
835
|
+
if (this.timer) {
|
|
836
|
+
clearTimeout(this.timer);
|
|
837
|
+
this.timer = undefined;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
size() {
|
|
841
|
+
return this.queue.size;
|
|
842
|
+
}
|
|
843
|
+
pause() {
|
|
844
|
+
this.paused = true;
|
|
845
|
+
}
|
|
846
|
+
resume() {
|
|
847
|
+
this.paused = false;
|
|
848
|
+
if (this.queue.size > 0) {
|
|
849
|
+
this.scheduleFlush();
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
class ConfigComponent extends BaseComponent {
|
|
855
|
+
name = "config";
|
|
856
|
+
version = "1.0.0";
|
|
857
|
+
memorySource = new MemorySource;
|
|
858
|
+
sources = [this.memorySource];
|
|
859
|
+
componentSchemas = new Map;
|
|
860
|
+
componentPaths = new Map;
|
|
861
|
+
watchers = new Map;
|
|
862
|
+
sourceUnwatchFns = new Map;
|
|
863
|
+
persistQueue;
|
|
864
|
+
persistFile;
|
|
865
|
+
keyRegistry = new Map;
|
|
866
|
+
componentRegistrations = new Map;
|
|
867
|
+
protocolResolver;
|
|
868
|
+
__xdgDataHome;
|
|
869
|
+
pendingSources = [];
|
|
870
|
+
sourceKeys = new Map;
|
|
871
|
+
getXdgDataHome() {
|
|
872
|
+
return this.__xdgDataHome ?? process.env.XDG_DATA_HOME ?? path2.join(os.homedir(), ".local", "share");
|
|
873
|
+
}
|
|
874
|
+
setXdgDataHome(path3) {
|
|
875
|
+
this.__xdgDataHome = path3;
|
|
876
|
+
if (this.protocolResolver) {
|
|
877
|
+
this.protocolResolver.xdgDataHome = path3;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
constructor(options = {}) {
|
|
881
|
+
super();
|
|
882
|
+
this.persistFile = options.persistFile;
|
|
883
|
+
this.persistQueue = new AsyncPersistQueue({
|
|
884
|
+
debounceMs: options.debounceMs ?? 300,
|
|
885
|
+
persistFn: this.persistToFile.bind(this)
|
|
886
|
+
});
|
|
887
|
+
this.protocolResolver = new ProtocolResolver;
|
|
888
|
+
}
|
|
889
|
+
register(schema) {
|
|
890
|
+
this.componentSchemas.set(schema.name, schema);
|
|
891
|
+
if (schema.paths) {
|
|
892
|
+
this.componentPaths.set(schema.name, schema.paths);
|
|
893
|
+
}
|
|
894
|
+
for (const [key, value] of Object.entries(schema.defaults)) {
|
|
895
|
+
const fullKey = `${schema.name}.${key}`;
|
|
896
|
+
if (this.memorySource.read(fullKey) === undefined) {
|
|
897
|
+
this.memorySource.write(fullKey, value);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
unregister(name) {
|
|
902
|
+
this.componentSchemas.delete(name);
|
|
903
|
+
this.componentPaths.delete(name);
|
|
904
|
+
return true;
|
|
905
|
+
}
|
|
906
|
+
isRegistered(name) {
|
|
907
|
+
return this.componentSchemas.has(name);
|
|
908
|
+
}
|
|
909
|
+
listComponents() {
|
|
910
|
+
return Array.from(this.componentSchemas.keys());
|
|
911
|
+
}
|
|
912
|
+
get(key) {
|
|
913
|
+
const memoryValue = this.memorySource.read(key);
|
|
914
|
+
if (memoryValue !== undefined) {
|
|
915
|
+
return memoryValue;
|
|
916
|
+
}
|
|
917
|
+
if (key.includes(".")) {
|
|
918
|
+
const parts = key.split(".");
|
|
919
|
+
for (let i = parts.length - 1;i >= 1; i--) {
|
|
920
|
+
const componentParts = parts.slice(0, i);
|
|
921
|
+
const component = componentParts.join(".");
|
|
922
|
+
const restParts = parts.slice(i);
|
|
923
|
+
const restKey = restParts.join(".");
|
|
924
|
+
const componentValue = this.memorySource.read(component);
|
|
925
|
+
if (typeof componentValue === "object" && componentValue !== null) {
|
|
926
|
+
const nestedValue = this.getNestedValue(componentValue, restKey);
|
|
927
|
+
if (nestedValue !== undefined) {
|
|
928
|
+
return nestedValue;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
getMany(keys) {
|
|
936
|
+
const result = {};
|
|
937
|
+
for (const key of keys) {
|
|
938
|
+
result[key] = this.get(key);
|
|
939
|
+
}
|
|
940
|
+
return result;
|
|
941
|
+
}
|
|
942
|
+
async set(key, value) {
|
|
943
|
+
const oldValue = this.get(key);
|
|
944
|
+
this.memorySource.write(key, value);
|
|
945
|
+
const event = {
|
|
946
|
+
component: key.split(".")[0],
|
|
947
|
+
key,
|
|
948
|
+
oldValue,
|
|
949
|
+
newValue: value,
|
|
950
|
+
persisted: false,
|
|
951
|
+
timestamp: Date.now()
|
|
952
|
+
};
|
|
953
|
+
this.notifyWatchers(key, event);
|
|
954
|
+
this.persistQueue.add(key, value);
|
|
955
|
+
}
|
|
956
|
+
async setMany(config) {
|
|
957
|
+
for (const [key, value] of Object.entries(config)) {
|
|
958
|
+
await this.set(key, value);
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
getPath(component, subPath) {
|
|
962
|
+
const paths = this.componentPaths.get(component);
|
|
963
|
+
if (!paths) {
|
|
964
|
+
throw new Error(`Component not registered: ${component}`);
|
|
965
|
+
}
|
|
966
|
+
if (subPath && paths[subPath]) {
|
|
967
|
+
const { base: base2, subPath: relativePath2 } = paths[subPath];
|
|
968
|
+
return XDG_PATHS.getComponentPath(base2, relativePath2);
|
|
969
|
+
}
|
|
970
|
+
const firstKey = Object.keys(paths)[0];
|
|
971
|
+
if (!firstKey) {
|
|
972
|
+
throw new Error(`No paths registered for component: ${component}`);
|
|
973
|
+
}
|
|
974
|
+
const { base, subPath: relativePath } = paths[firstKey];
|
|
975
|
+
return XDG_PATHS.getComponentPath(base, relativePath);
|
|
976
|
+
}
|
|
977
|
+
async save(key) {
|
|
978
|
+
await this.persistQueue.flush();
|
|
979
|
+
}
|
|
980
|
+
async reset(key) {
|
|
981
|
+
const [component, ...rest] = key.split(".");
|
|
982
|
+
const schema = this.componentSchemas.get(component);
|
|
983
|
+
if (!schema)
|
|
984
|
+
return;
|
|
985
|
+
const partialKey = rest.join(".");
|
|
986
|
+
const defaultValue = schema.defaults[partialKey] ?? schema.defaults[key];
|
|
987
|
+
if (defaultValue !== undefined) {
|
|
988
|
+
await this.set(key, defaultValue);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
watch(pattern, callback) {
|
|
992
|
+
if (!this.watchers.has(pattern)) {
|
|
993
|
+
this.watchers.set(pattern, new Set);
|
|
994
|
+
}
|
|
995
|
+
this.watchers.get(pattern).add(callback);
|
|
996
|
+
return () => {
|
|
997
|
+
this.watchers.get(pattern)?.delete(callback);
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
addSource(source) {
|
|
1001
|
+
this.sources.push(source);
|
|
1002
|
+
this.sources.sort((a, b) => a.priority - b.priority);
|
|
1003
|
+
if (source.watch) {
|
|
1004
|
+
const callback = (event) => {
|
|
1005
|
+
this.notifyWatchers(event.key, {
|
|
1006
|
+
component: event.key.split(".")[0],
|
|
1007
|
+
key: event.key,
|
|
1008
|
+
oldValue: event.oldValue,
|
|
1009
|
+
newValue: event.newValue,
|
|
1010
|
+
persisted: true,
|
|
1011
|
+
timestamp: event.timestamp
|
|
1012
|
+
});
|
|
1013
|
+
};
|
|
1014
|
+
try {
|
|
1015
|
+
const unwatch = source.watch(callback);
|
|
1016
|
+
const sourceKey = this.getSourceKey(source);
|
|
1017
|
+
this.sourceUnwatchFns.set(sourceKey, unwatch);
|
|
1018
|
+
} catch {}
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
getSourceKey(source) {
|
|
1022
|
+
if ("filePath" in source) {
|
|
1023
|
+
return `file:${source.filePath}`;
|
|
1024
|
+
}
|
|
1025
|
+
if ("prefix" in source) {
|
|
1026
|
+
return `env:${source.prefix}`;
|
|
1027
|
+
}
|
|
1028
|
+
return source.name;
|
|
1029
|
+
}
|
|
1030
|
+
removeSource(name) {
|
|
1031
|
+
const index = this.sources.findIndex((s) => s.name === name);
|
|
1032
|
+
if (index > 0) {
|
|
1033
|
+
const source = this.sources[index];
|
|
1034
|
+
const sourceKey = this.getSourceKey(source);
|
|
1035
|
+
const unwatch = this.sourceUnwatchFns.get(sourceKey);
|
|
1036
|
+
if (unwatch) {
|
|
1037
|
+
unwatch();
|
|
1038
|
+
this.sourceUnwatchFns.delete(sourceKey);
|
|
1039
|
+
}
|
|
1040
|
+
this.sources.splice(index, 1);
|
|
1041
|
+
return true;
|
|
1042
|
+
}
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
unwatchAll() {
|
|
1046
|
+
for (const unwatch of this.sourceUnwatchFns.values()) {
|
|
1047
|
+
unwatch();
|
|
1048
|
+
}
|
|
1049
|
+
this.sourceUnwatchFns.clear();
|
|
1050
|
+
}
|
|
1051
|
+
getSources() {
|
|
1052
|
+
return [...this.sources];
|
|
1053
|
+
}
|
|
1054
|
+
getNestedValue(obj, key) {
|
|
1055
|
+
const keys = key.split(".");
|
|
1056
|
+
let current = obj;
|
|
1057
|
+
for (const k of keys) {
|
|
1058
|
+
if (typeof current !== "object" || current === null)
|
|
1059
|
+
return;
|
|
1060
|
+
current = current[k];
|
|
1061
|
+
}
|
|
1062
|
+
return current;
|
|
1063
|
+
}
|
|
1064
|
+
notifyWatchers(key, event) {
|
|
1065
|
+
const exactWatchers = this.watchers.get(key);
|
|
1066
|
+
exactWatchers?.forEach((cb) => cb(event));
|
|
1067
|
+
const [component] = key.split(".");
|
|
1068
|
+
const wildcardWatchers = this.watchers.get(`${component}.*`);
|
|
1069
|
+
wildcardWatchers?.forEach((cb) => cb(event));
|
|
1070
|
+
const globalWatchers = this.watchers.get("*");
|
|
1071
|
+
globalWatchers?.forEach((cb) => cb(event));
|
|
1072
|
+
}
|
|
1073
|
+
async persistToFile(entries) {
|
|
1074
|
+
if (!this.persistFile)
|
|
1075
|
+
return;
|
|
1076
|
+
console.log(`[ConfigComponent] Persisting ${entries.length} entries to ${this.persistFile}`);
|
|
1077
|
+
}
|
|
1078
|
+
registerSource(registration) {
|
|
1079
|
+
const { type, relativePath, envPrefix } = registration;
|
|
1080
|
+
const sourceKey = type === "file" && relativePath ? `file:${relativePath}` : `${type}:${envPrefix ?? ""}`;
|
|
1081
|
+
if (this.sourceKeys.has(sourceKey)) {
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
this.pendingSources.push(registration);
|
|
1085
|
+
this.sourceKeys.set(sourceKey, true);
|
|
1086
|
+
}
|
|
1087
|
+
createSource(registration) {
|
|
1088
|
+
const { type, relativePath, envPrefix, priority, optional, watch: watch2 } = registration;
|
|
1089
|
+
switch (type) {
|
|
1090
|
+
case "memory": {
|
|
1091
|
+
return this.memorySource;
|
|
1092
|
+
}
|
|
1093
|
+
case "file": {
|
|
1094
|
+
if (!relativePath) {
|
|
1095
|
+
throw new Error("File source requires relativePath");
|
|
1096
|
+
}
|
|
1097
|
+
const isAbsolutePath = path2.isAbsolute(relativePath) || relativePath.startsWith("file://");
|
|
1098
|
+
let filePath;
|
|
1099
|
+
if (isAbsolutePath) {
|
|
1100
|
+
filePath = relativePath.startsWith("file://") ? relativePath.replace(/^file:\/\//, "") : relativePath;
|
|
1101
|
+
} else {
|
|
1102
|
+
if (this.protocolResolver) {
|
|
1103
|
+
this.protocolResolver.xdgDataHome = this.getXdgDataHome();
|
|
1104
|
+
}
|
|
1105
|
+
filePath = path2.join(this.getXdgDataHome(), relativePath);
|
|
1106
|
+
}
|
|
1107
|
+
return new FileSource({
|
|
1108
|
+
filePath,
|
|
1109
|
+
optional: optional ?? false,
|
|
1110
|
+
resolver: this.protocolResolver,
|
|
1111
|
+
watch: watch2
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
case "env": {
|
|
1115
|
+
return new EnvSource({
|
|
1116
|
+
prefix: envPrefix ?? "",
|
|
1117
|
+
priority: priority ?? 20,
|
|
1118
|
+
watch: watch2
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
default:
|
|
1122
|
+
throw new Error(`Unknown source type: ${type}`);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
ensureSourcesCreated() {
|
|
1126
|
+
for (const pending of this.pendingSources) {
|
|
1127
|
+
const source = this.createSource(pending);
|
|
1128
|
+
this.addSource(source);
|
|
1129
|
+
}
|
|
1130
|
+
this.pendingSources = [];
|
|
1131
|
+
}
|
|
1132
|
+
registerKeys(keys) {
|
|
1133
|
+
for (const keyReg of keys) {
|
|
1134
|
+
if (this.keyRegistry.has(keyReg.key)) {
|
|
1135
|
+
continue;
|
|
1136
|
+
}
|
|
1137
|
+
this.keyRegistry.set(keyReg.key, keyReg);
|
|
1138
|
+
if (keyReg.default !== undefined) {
|
|
1139
|
+
if (this.memorySource.read(keyReg.key) === undefined) {
|
|
1140
|
+
this.memorySource.write(keyReg.key, keyReg.default);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
registerComponent(registration) {
|
|
1146
|
+
const { name, sources, keys, defaults } = registration;
|
|
1147
|
+
this.componentRegistrations.set(name, registration);
|
|
1148
|
+
for (const sourceReg of sources) {
|
|
1149
|
+
this.registerSource(sourceReg);
|
|
1150
|
+
}
|
|
1151
|
+
this.registerKeys(keys);
|
|
1152
|
+
if (defaults) {
|
|
1153
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
1154
|
+
const fullKey = `${name}.${key}`;
|
|
1155
|
+
if (this.memorySource.read(fullKey) === undefined) {
|
|
1156
|
+
this.memorySource.write(fullKey, value);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
async load(componentName) {
|
|
1162
|
+
this.ensureSourcesCreated();
|
|
1163
|
+
let keysToLoad;
|
|
1164
|
+
if (componentName) {
|
|
1165
|
+
const registration = this.componentRegistrations.get(componentName);
|
|
1166
|
+
if (!registration) {
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
keysToLoad = registration.keys.map((k) => ({
|
|
1170
|
+
key: k.key,
|
|
1171
|
+
sources: k.sources
|
|
1172
|
+
}));
|
|
1173
|
+
} else {
|
|
1174
|
+
keysToLoad = Array.from(this.keyRegistry.values()).map((k) => ({
|
|
1175
|
+
key: k.key,
|
|
1176
|
+
sources: k.sources
|
|
1177
|
+
}));
|
|
1178
|
+
}
|
|
1179
|
+
const sourcesByType = new Map;
|
|
1180
|
+
for (const source of this.sources) {
|
|
1181
|
+
sourcesByType.set(source.name, source);
|
|
1182
|
+
}
|
|
1183
|
+
for (const { key, sources: sourceTypes } of keysToLoad) {
|
|
1184
|
+
if (this.memorySource.read(key) !== undefined) {
|
|
1185
|
+
continue;
|
|
1186
|
+
}
|
|
1187
|
+
for (const sourceType of sourceTypes) {
|
|
1188
|
+
const source = sourcesByType.get(sourceType);
|
|
1189
|
+
if (!source)
|
|
1190
|
+
continue;
|
|
1191
|
+
const value = source.read(key);
|
|
1192
|
+
if (value !== undefined) {
|
|
1193
|
+
this.memorySource.write(key, value);
|
|
1194
|
+
break;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
getRegisteredSources() {
|
|
1200
|
+
this.ensureSourcesCreated();
|
|
1201
|
+
return [...this.sources];
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
export { parseJSONC, substituteEnvVars, parseJSONCWithEnv, substituteProtocolRefs, parseJSONCWithProtocols, FileSource, EnvSource, ConfigSourceNotFoundError, ConfigFileNotFoundError, MemorySource, ConfigComponent };
|