@o3co/ts.hocon 1.3.0 → 1.4.0
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/README.md +55 -6
- package/dist/config-BwjvKeZ5.d.cts +271 -0
- package/dist/config-BwjvKeZ5.d.ts +271 -0
- package/dist/index.cjs +1105 -360
- package/dist/index.d.cts +45 -2
- package/dist/index.d.ts +45 -2
- package/dist/index.js +1094 -359
- package/dist/zod.cjs +823 -198
- package/dist/zod.d.cts +1 -1
- package/dist/zod.d.ts +1 -1
- package/dist/zod.js +822 -198
- package/package.json +3 -1
- package/dist/parse-nNbyqnOh.d.cts +0 -53
- package/dist/parse-nNbyqnOh.d.ts +0 -53
package/dist/index.js
CHANGED
|
@@ -215,6 +215,14 @@ var ResolveError = class extends Error {
|
|
|
215
215
|
this.name = "ResolveError";
|
|
216
216
|
}
|
|
217
217
|
};
|
|
218
|
+
var PackageLookupError = class extends ResolveError {
|
|
219
|
+
constructor(message, identifier, packageFile, line, col) {
|
|
220
|
+
super(message, `${identifier}/${packageFile}`, line, col);
|
|
221
|
+
this.identifier = identifier;
|
|
222
|
+
this.packageFile = packageFile;
|
|
223
|
+
this.name = "PackageLookupError";
|
|
224
|
+
}
|
|
225
|
+
};
|
|
218
226
|
var ConfigError = class extends Error {
|
|
219
227
|
constructor(message, path2) {
|
|
220
228
|
super(message);
|
|
@@ -222,191 +230,155 @@ var ConfigError = class extends Error {
|
|
|
222
230
|
this.name = "ConfigError";
|
|
223
231
|
}
|
|
224
232
|
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (value.fields.size === 0) return null;
|
|
230
|
-
const canonicalInt = /^(0|[1-9][0-9]*)$/;
|
|
231
|
-
const eligible = [];
|
|
232
|
-
for (const [key, val] of value.fields) {
|
|
233
|
-
if (!canonicalInt.test(key)) continue;
|
|
234
|
-
const n = Number(key);
|
|
235
|
-
if (!Number.isInteger(n) || n > 2147483647) continue;
|
|
236
|
-
eligible.push({ n, v: val });
|
|
233
|
+
var NotResolvedError = class extends ConfigError {
|
|
234
|
+
constructor(path2) {
|
|
235
|
+
super(`value at path "${path2}" is not resolved (call resolve() first)`, path2);
|
|
236
|
+
this.name = "NotResolvedError";
|
|
237
237
|
}
|
|
238
|
-
|
|
239
|
-
eligible.sort((a, b) => a.n - b.n);
|
|
240
|
-
return eligible.map((e) => e.v);
|
|
241
|
-
}
|
|
238
|
+
};
|
|
242
239
|
|
|
243
|
-
// src/
|
|
244
|
-
var
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const v = this.requireScalar(path2);
|
|
265
|
-
const coerced = coerceBoolean(v.raw);
|
|
266
|
-
if (coerced !== void 0) return coerced;
|
|
267
|
-
throw new ConfigError(`expected boolean at ${path2}, got ${v.valueType}`, path2);
|
|
240
|
+
// src/internal/resolver/types.ts
|
|
241
|
+
var separatorValues = /* @__PURE__ */ new WeakSet();
|
|
242
|
+
function isSubst(v) {
|
|
243
|
+
return v._kind === "subst-placeholder";
|
|
244
|
+
}
|
|
245
|
+
function isConcat(v) {
|
|
246
|
+
return v._kind === "concat-placeholder";
|
|
247
|
+
}
|
|
248
|
+
function isAppend(v) {
|
|
249
|
+
return v._kind === "append-placeholder";
|
|
250
|
+
}
|
|
251
|
+
function isResObj(v) {
|
|
252
|
+
return v._kind === "res-obj";
|
|
253
|
+
}
|
|
254
|
+
function makeResObj() {
|
|
255
|
+
return { _kind: "res-obj", fields: /* @__PURE__ */ new Map(), priorValues: /* @__PURE__ */ new Map() };
|
|
256
|
+
}
|
|
257
|
+
function mergeUnresolved(receiver, fallback) {
|
|
258
|
+
const result = makeResObj();
|
|
259
|
+
for (const [k, v] of fallback.fields) {
|
|
260
|
+
result.fields.set(k, v);
|
|
268
261
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (v.valueType !== "string" && v.valueType !== "number") {
|
|
272
|
-
throw new ConfigError(`expected duration at ${path2}, got ${v.valueType}`, path2);
|
|
273
|
-
}
|
|
274
|
-
const result = parseDuration(v.raw, unit);
|
|
275
|
-
if (Number.isNaN(result)) throw new ConfigError(`invalid duration at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
276
|
-
return result;
|
|
262
|
+
for (const [k, v] of fallback.priorValues) {
|
|
263
|
+
result.priorValues.set(k, v);
|
|
277
264
|
}
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
if (
|
|
281
|
-
|
|
265
|
+
for (const [k, rv] of receiver.fields) {
|
|
266
|
+
const existing = result.fields.get(k);
|
|
267
|
+
if (existing !== void 0) {
|
|
268
|
+
const recPrior = receiver.priorValues.get(k);
|
|
269
|
+
if (recPrior !== void 0 && !isResObj(recPrior)) {
|
|
270
|
+
result.fields.set(k, rv);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (isResObj(rv) && isResObj(existing)) {
|
|
274
|
+
result.fields.set(k, mergeUnresolved(rv, existing));
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
result.priorValues.set(k, existing);
|
|
282
278
|
}
|
|
283
|
-
|
|
284
|
-
if (Number.isNaN(result)) throw new ConfigError(`invalid byte size at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
285
|
-
if (result < 0) throw new ConfigError(`byte size must be non-negative at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
286
|
-
return result;
|
|
287
|
-
}
|
|
288
|
-
getConfig(path2) {
|
|
289
|
-
const v = this.lookupNode(path2);
|
|
290
|
-
if (v === void 0) throw new ConfigError(`path not found: ${path2}`, path2);
|
|
291
|
-
if (v.kind !== "object") throw new ConfigError(`expected object at ${path2}`, path2);
|
|
292
|
-
return new _Config(v);
|
|
279
|
+
result.fields.set(k, rv);
|
|
293
280
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if (v.kind === "object") {
|
|
298
|
-
const converted = numericObjectToArray(v);
|
|
299
|
-
if (converted !== null) return converted.map(hoconToJs);
|
|
281
|
+
for (const [k, v] of receiver.priorValues) {
|
|
282
|
+
if (receiver.fields.has(k)) {
|
|
283
|
+
result.priorValues.set(k, v);
|
|
300
284
|
}
|
|
301
|
-
if (v.kind !== "array") throw new ConfigError(`expected array at ${path2}`, path2);
|
|
302
|
-
return v.items.map(hoconToJs);
|
|
303
|
-
}
|
|
304
|
-
has(path2) {
|
|
305
|
-
return this.lookupNode(path2) !== void 0;
|
|
306
|
-
}
|
|
307
|
-
keys() {
|
|
308
|
-
return [...this.root.fields.keys()];
|
|
309
|
-
}
|
|
310
|
-
withFallback(fallback) {
|
|
311
|
-
const merged = deepMergeHocon(this.root, fallback.root);
|
|
312
|
-
return new _Config(merged);
|
|
313
285
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (next === void 0) return void 0;
|
|
324
|
-
current = next;
|
|
286
|
+
return result;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/internal/resolver/utils.ts
|
|
290
|
+
function segmentsToKey(segments) {
|
|
291
|
+
return segments.map((s) => {
|
|
292
|
+
const t = s.text;
|
|
293
|
+
if (t === "" || /[^a-zA-Z0-9\-_]/.test(t)) {
|
|
294
|
+
return `"${t.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
325
295
|
}
|
|
326
|
-
return
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
296
|
+
return t;
|
|
297
|
+
}).join(".");
|
|
298
|
+
}
|
|
299
|
+
function lookupPath(root, segments) {
|
|
300
|
+
const [head, ...tail] = segments;
|
|
301
|
+
if (head === void 0) return void 0;
|
|
302
|
+
const key = head.text;
|
|
303
|
+
const val = root.fields.get(key);
|
|
304
|
+
if (val === void 0) return void 0;
|
|
305
|
+
if (tail.length === 0) return val;
|
|
306
|
+
if (isResObj(val)) return lookupPath(val, tail);
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
function lookupResObj(root, segments) {
|
|
310
|
+
let cur = root;
|
|
311
|
+
for (const seg of segments) {
|
|
312
|
+
const val = cur.fields.get(seg.text);
|
|
313
|
+
if (val === void 0 || !isResObj(val)) return void 0;
|
|
314
|
+
cur = val;
|
|
333
315
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
let closed = false;
|
|
343
|
-
while (i < path2.length) {
|
|
344
|
-
const ch = path2[i];
|
|
345
|
-
if (ch === "\\" && i + 1 < path2.length) {
|
|
346
|
-
const next = path2[i + 1];
|
|
347
|
-
segment += next;
|
|
348
|
-
i += 2;
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
if (ch === '"') {
|
|
352
|
-
closed = true;
|
|
353
|
-
i++;
|
|
354
|
-
break;
|
|
355
|
-
}
|
|
356
|
-
segment += ch;
|
|
357
|
-
i++;
|
|
358
|
-
}
|
|
359
|
-
if (!closed) throw new ConfigError(`unterminated quoted path segment: ${path2}`, path2);
|
|
360
|
-
segments.push(segment);
|
|
361
|
-
if (i < path2.length && path2[i] === ".") i++;
|
|
316
|
+
return cur;
|
|
317
|
+
}
|
|
318
|
+
function deepMergeHoconValues(base, overlay) {
|
|
319
|
+
const merged = new Map(base.fields);
|
|
320
|
+
for (const [k, v] of overlay.fields) {
|
|
321
|
+
const existing = merged.get(k);
|
|
322
|
+
if (existing?.kind === "object" && v.kind === "object") {
|
|
323
|
+
merged.set(k, deepMergeHoconValues(existing, v));
|
|
362
324
|
} else {
|
|
363
|
-
|
|
364
|
-
if (dot === -1) {
|
|
365
|
-
segments.push(path2.slice(i));
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
segments.push(path2.slice(i, dot));
|
|
369
|
-
i = dot + 1;
|
|
325
|
+
merged.set(k, v);
|
|
370
326
|
}
|
|
371
327
|
}
|
|
372
|
-
return
|
|
328
|
+
return { kind: "object", fields: merged };
|
|
373
329
|
}
|
|
374
|
-
function
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return raw;
|
|
330
|
+
function deepMergeResObjInto(dst, src) {
|
|
331
|
+
for (const [k, srcVal] of src.fields) {
|
|
332
|
+
const dstVal = dst.fields.get(k);
|
|
333
|
+
if (dstVal !== void 0 && isResObj(dstVal) && isResObj(srcVal)) {
|
|
334
|
+
deepMergeResObjInto(dstVal, srcVal);
|
|
335
|
+
} else {
|
|
336
|
+
if (dstVal !== void 0) dst.priorValues.set(k, dstVal);
|
|
337
|
+
dst.fields.set(k, srcVal);
|
|
338
|
+
}
|
|
384
339
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
case "scalar":
|
|
389
|
-
return scalarToJs(v.raw, v.valueType);
|
|
390
|
-
case "array":
|
|
391
|
-
return v.items.map(hoconToJs);
|
|
392
|
-
case "object": {
|
|
393
|
-
const obj = {};
|
|
394
|
-
for (const [k, val] of v.fields) obj[k] = hoconToJs(val);
|
|
395
|
-
return obj;
|
|
340
|
+
for (const [k, srcPrior] of src.priorValues) {
|
|
341
|
+
if (!dst.priorValues.has(k)) {
|
|
342
|
+
dst.priorValues.set(k, srcPrior);
|
|
396
343
|
}
|
|
397
344
|
}
|
|
398
345
|
}
|
|
399
|
-
function
|
|
400
|
-
const
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if (
|
|
404
|
-
|
|
346
|
+
function hoconValueToResObj(hv) {
|
|
347
|
+
const obj = makeResObj();
|
|
348
|
+
if (hv.kind !== "object") return obj;
|
|
349
|
+
for (const [key, val] of hv.fields) {
|
|
350
|
+
if (val.kind === "object") {
|
|
351
|
+
obj.fields.set(key, hoconValueToResObj(val));
|
|
405
352
|
} else {
|
|
406
|
-
|
|
353
|
+
obj.fields.set(key, val);
|
|
407
354
|
}
|
|
408
355
|
}
|
|
409
|
-
return
|
|
356
|
+
return obj;
|
|
357
|
+
}
|
|
358
|
+
function isFileNotFoundError(e) {
|
|
359
|
+
if (!(e instanceof Error)) return false;
|
|
360
|
+
const code = e.code;
|
|
361
|
+
if (code === "ENOENT" || code === "MODULE_NOT_FOUND") return true;
|
|
362
|
+
const msg = e.message.toLowerCase();
|
|
363
|
+
return msg.includes("not found") || msg.includes("no such file") || msg.includes("enoent");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/internal/resolver/include-loader.ts
|
|
367
|
+
import { createRequire } from "module";
|
|
368
|
+
import * as nodePath from "path";
|
|
369
|
+
|
|
370
|
+
// src/internal/parser/empty-check.ts
|
|
371
|
+
function hasContentTokens(tokens) {
|
|
372
|
+
return tokens.some((t) => t.kind !== "eof" && t.kind !== "newline");
|
|
373
|
+
}
|
|
374
|
+
function assertNonEmptyDocument(tokens, sourceDescriptor) {
|
|
375
|
+
if (!hasContentTokens(tokens)) {
|
|
376
|
+
throw new ParseError(
|
|
377
|
+
`empty file is not a valid HOCON document (HOCON.md L130): ${sourceDescriptor}`,
|
|
378
|
+
1,
|
|
379
|
+
1
|
|
380
|
+
);
|
|
381
|
+
}
|
|
410
382
|
}
|
|
411
383
|
|
|
412
384
|
// src/internal/lexer/lexer.ts
|
|
@@ -972,58 +944,137 @@ var Parser = class {
|
|
|
972
944
|
const p = this.currentPos();
|
|
973
945
|
this.skip("newline");
|
|
974
946
|
const t = this.peek();
|
|
975
|
-
let path2;
|
|
976
|
-
let required = false;
|
|
977
|
-
let isFile = false;
|
|
978
947
|
if (t.kind === "unquoted" && (t.value === "required(" || t.value === "required" || t.value.startsWith("required("))) {
|
|
979
948
|
if (t.value === "required") {
|
|
980
|
-
const
|
|
981
|
-
if (
|
|
949
|
+
const next = this.peek(1);
|
|
950
|
+
if (next.kind !== "unquoted" || !next.value.startsWith("(")) {
|
|
982
951
|
throw new ParseError("include required must be followed by (", t.line, t.col);
|
|
983
952
|
}
|
|
984
953
|
}
|
|
985
|
-
required = true;
|
|
986
954
|
this.advance();
|
|
987
955
|
const innerPrefix = t.value.startsWith("required(") ? t.value.slice("required(".length) : "";
|
|
988
|
-
if (innerPrefix.startsWith("url") || innerPrefix.startsWith("classpath")) {
|
|
956
|
+
if (innerPrefix === "url" || innerPrefix.startsWith("url(") || innerPrefix === "classpath" || innerPrefix.startsWith("classpath(")) {
|
|
989
957
|
throw new ParseError("include url(...) and classpath(...) are not supported", t.line, t.col);
|
|
990
958
|
}
|
|
991
|
-
|
|
992
|
-
|
|
959
|
+
const nextTok = this.peek();
|
|
960
|
+
if (innerPrefix === "package" || innerPrefix.startsWith("package(") || innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "package(" || nextTok.value.startsWith("package("))) {
|
|
961
|
+
const consumePackageToken = innerPrefix === "";
|
|
962
|
+
const { qualifier, path: path3 } = this.parsePackageArgs(consumePackageToken);
|
|
963
|
+
return this.makeIncludeField(qualifier, path3, true, p);
|
|
993
964
|
}
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
965
|
+
if (innerPrefix === "file" || innerPrefix.startsWith("file(") || innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "file(" || nextTok.value === "file")) {
|
|
966
|
+
if (innerPrefix === "") {
|
|
967
|
+
this.advance();
|
|
968
|
+
}
|
|
969
|
+
const path3 = this.parseQuotedPathSkipWrapper(t);
|
|
970
|
+
return this.makeIncludeField({ kind: "file" }, path3, true, p);
|
|
971
|
+
}
|
|
972
|
+
if (innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "url" || nextTok.value.startsWith("url(") || nextTok.value === "classpath" || nextTok.value.startsWith("classpath("))) {
|
|
973
|
+
throw new ParseError("include url(...) and classpath(...) are not supported", nextTok.line, nextTok.col);
|
|
974
|
+
}
|
|
975
|
+
if (innerPrefix !== "") {
|
|
976
|
+
if (innerPrefix === ")") {
|
|
977
|
+
throw new ParseError("include required(...) must contain a path", t.line, t.col);
|
|
978
|
+
}
|
|
979
|
+
const name = innerPrefix.replace(/\(.*$/, "").replace(/\).*$/, "");
|
|
980
|
+
throw new ParseError(`unknown include qualifier inside required(): "${name}"`, t.line, t.col);
|
|
981
|
+
}
|
|
982
|
+
const path2 = this.parseQuotedPathSkipWrapper(t);
|
|
983
|
+
return this.makeIncludeField({ kind: "bare" }, path2, true, p);
|
|
984
|
+
}
|
|
985
|
+
if (t.kind === "string") {
|
|
986
|
+
const path2 = this.advance().value;
|
|
987
|
+
return this.makeIncludeField({ kind: "bare" }, path2, false, p);
|
|
988
|
+
}
|
|
989
|
+
if (t.kind === "unquoted" && (t.value === "file(" || t.value === "file")) {
|
|
1009
990
|
this.advance();
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
991
|
+
const path2 = this.parseQuotedPathSkipWrapper(t);
|
|
992
|
+
return this.makeIncludeField({ kind: "file" }, path2, false, p);
|
|
993
|
+
}
|
|
994
|
+
if (t.kind === "unquoted" && (t.value === "package(" || t.value.startsWith("package("))) {
|
|
995
|
+
const { qualifier, path: path2 } = this.parsePackageArgs();
|
|
996
|
+
return this.makeIncludeField(qualifier, path2, false, p);
|
|
997
|
+
}
|
|
998
|
+
if (t.kind === "unquoted" && (t.value === "url" || t.value.startsWith("url("))) {
|
|
1015
999
|
throw new ParseError("include url(...) is not supported", t.line, t.col);
|
|
1016
|
-
}
|
|
1000
|
+
}
|
|
1001
|
+
if (t.kind === "unquoted" && (t.value === "classpath" || t.value.startsWith("classpath("))) {
|
|
1017
1002
|
throw new ParseError("include classpath(...) is not supported", t.line, t.col);
|
|
1018
|
-
} else {
|
|
1019
|
-
throw new ParseError(`expected include path, got ${t.kind}`, t.line, t.col);
|
|
1020
1003
|
}
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1004
|
+
throw new ParseError(`expected include path, got ${t.kind}`, t.line, t.col);
|
|
1005
|
+
}
|
|
1006
|
+
makeIncludeField(qualifier, path2, required, pos) {
|
|
1007
|
+
return { key: [], value: { kind: "include", qualifier, path: path2, required, pos }, append: false, pos };
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Skip past include-syntax wrapper tokens to find the quoted path string.
|
|
1011
|
+
* Strict: only allows `(`, `)`, and bare qualifier keywords (file / file( /
|
|
1012
|
+
* package / package( / url / url( / classpath / classpath() between the
|
|
1013
|
+
* directive and the quoted path. Anything else is a parse error rather than
|
|
1014
|
+
* silently swallowed (see ts#113).
|
|
1015
|
+
*/
|
|
1016
|
+
parseQuotedPathSkipWrapper(errTok) {
|
|
1017
|
+
while (this.isIncludeWrapperToken(this.peek())) this.advance();
|
|
1018
|
+
if (this.peek().kind !== "string") {
|
|
1019
|
+
const tok = this.peek();
|
|
1020
|
+
if (tok.kind === "eof") throw new ParseError("expected include path", errTok.line, errTok.col);
|
|
1021
|
+
const desc = tok.kind === "unquoted" ? `unquoted "${tok.value}"` : tok.kind;
|
|
1022
|
+
throw new ParseError(`expected quoted include path, got ${desc}`, tok.line, tok.col);
|
|
1023
|
+
}
|
|
1024
|
+
const path2 = this.advance().value;
|
|
1025
|
+
while (this.peek().kind === "unquoted" && (this.peek().value === ")" || this.peek().value === "))")) {
|
|
1026
|
+
this.advance();
|
|
1027
|
+
}
|
|
1028
|
+
const after = this.peek();
|
|
1029
|
+
if (after.kind !== "newline" && after.kind !== "rbrace" && after.kind !== "eof" && after.kind !== "comma") {
|
|
1030
|
+
const desc = after.kind === "unquoted" ? `unquoted "${after.value}"` : after.kind;
|
|
1031
|
+
throw new ParseError(`unexpected token after include path: ${desc}`, after.line, after.col);
|
|
1032
|
+
}
|
|
1033
|
+
return path2;
|
|
1034
|
+
}
|
|
1035
|
+
isIncludeWrapperToken(tok) {
|
|
1036
|
+
return tok.kind === "unquoted" && tok.value === "(";
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Parse the body of `package("identifier", "file")`.
|
|
1040
|
+
*
|
|
1041
|
+
* @param consumePackageToken - When true (default), the current token is `package(` and
|
|
1042
|
+
* must be consumed first. When false, the lexer already bundled `package(` into the
|
|
1043
|
+
* preceding token (e.g. `required(package("`), so we are already positioned at the
|
|
1044
|
+
* first string argument.
|
|
1045
|
+
*/
|
|
1046
|
+
parsePackageArgs(consumePackageToken = true) {
|
|
1047
|
+
const pkgTok = consumePackageToken ? this.advance() : this.peek();
|
|
1048
|
+
this.skip("newline");
|
|
1049
|
+
const idTok = this.peek();
|
|
1050
|
+
if (idTok.kind !== "string") {
|
|
1051
|
+
throw new ParseError("include package: expected quoted identifier as first argument", pkgTok.line, pkgTok.col);
|
|
1052
|
+
}
|
|
1053
|
+
const identifier = this.advance().value;
|
|
1054
|
+
this.skip("newline");
|
|
1055
|
+
if (this.peek().kind !== "comma") {
|
|
1056
|
+
const after = this.peek();
|
|
1057
|
+
throw new ParseError(
|
|
1058
|
+
after.kind === "string" ? "include package: missing comma between identifier and file arguments" : "include package: requires exactly two arguments (identifier, file) \u2014 one-arg form is not supported",
|
|
1059
|
+
pkgTok.line,
|
|
1060
|
+
pkgTok.col
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
this.advance();
|
|
1064
|
+
this.skip("newline");
|
|
1065
|
+
const fileTok = this.peek();
|
|
1066
|
+
if (fileTok.kind !== "string") {
|
|
1067
|
+
throw new ParseError(
|
|
1068
|
+
"include package: requires exactly two arguments (identifier, file) \u2014 one-arg form is not supported",
|
|
1069
|
+
pkgTok.line,
|
|
1070
|
+
pkgTok.col
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
const filePath = this.advance().value;
|
|
1074
|
+
while (this.peek().kind !== "newline" && this.peek().kind !== "rbrace" && this.peek().kind !== "eof" && this.peek().kind !== "comma") {
|
|
1075
|
+
this.advance();
|
|
1076
|
+
}
|
|
1077
|
+
return { qualifier: { kind: "package", identifier }, path: filePath };
|
|
1027
1078
|
}
|
|
1028
1079
|
parseValue() {
|
|
1029
1080
|
const p = this.currentPos();
|
|
@@ -1064,7 +1115,9 @@ var Parser = class {
|
|
|
1064
1115
|
parts.push(node);
|
|
1065
1116
|
}
|
|
1066
1117
|
if (parts.length === 0) throw new ParseError("expected value", this.peek().line, this.peek().col);
|
|
1067
|
-
if (parts.length === 1)
|
|
1118
|
+
if (parts.length === 1) {
|
|
1119
|
+
return parts[0];
|
|
1120
|
+
}
|
|
1068
1121
|
return { kind: "concat", nodes: parts, pos: p };
|
|
1069
1122
|
}
|
|
1070
1123
|
parseArray() {
|
|
@@ -1096,118 +1149,6 @@ function parseTokens(tokens) {
|
|
|
1096
1149
|
return new Parser(tokens).parse();
|
|
1097
1150
|
}
|
|
1098
1151
|
|
|
1099
|
-
// src/internal/parser/empty-check.ts
|
|
1100
|
-
function hasContentTokens(tokens) {
|
|
1101
|
-
return tokens.some((t) => t.kind !== "eof" && t.kind !== "newline");
|
|
1102
|
-
}
|
|
1103
|
-
function assertNonEmptyDocument(tokens, sourceDescriptor) {
|
|
1104
|
-
if (!hasContentTokens(tokens)) {
|
|
1105
|
-
throw new ParseError(
|
|
1106
|
-
`empty file is not a valid HOCON document (HOCON.md L130): ${sourceDescriptor}`,
|
|
1107
|
-
1,
|
|
1108
|
-
1
|
|
1109
|
-
);
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
// src/internal/resolver/types.ts
|
|
1114
|
-
var separatorValues = /* @__PURE__ */ new WeakSet();
|
|
1115
|
-
function isSubst(v) {
|
|
1116
|
-
return v._kind === "subst-placeholder";
|
|
1117
|
-
}
|
|
1118
|
-
function isConcat(v) {
|
|
1119
|
-
return v._kind === "concat-placeholder";
|
|
1120
|
-
}
|
|
1121
|
-
function isAppend(v) {
|
|
1122
|
-
return v._kind === "append-placeholder";
|
|
1123
|
-
}
|
|
1124
|
-
function isResObj(v) {
|
|
1125
|
-
return v._kind === "res-obj";
|
|
1126
|
-
}
|
|
1127
|
-
function makeResObj() {
|
|
1128
|
-
return { _kind: "res-obj", fields: /* @__PURE__ */ new Map(), priorValues: /* @__PURE__ */ new Map() };
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
// src/internal/resolver/utils.ts
|
|
1132
|
-
function segmentsToKey(segments) {
|
|
1133
|
-
return segments.map((s) => {
|
|
1134
|
-
const t = s.text;
|
|
1135
|
-
if (t === "" || /[^a-zA-Z0-9\-_]/.test(t)) {
|
|
1136
|
-
return `"${t.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
1137
|
-
}
|
|
1138
|
-
return t;
|
|
1139
|
-
}).join(".");
|
|
1140
|
-
}
|
|
1141
|
-
function lookupPath(root, segments) {
|
|
1142
|
-
const [head, ...tail] = segments;
|
|
1143
|
-
if (head === void 0) return void 0;
|
|
1144
|
-
const key = head.text;
|
|
1145
|
-
const val = root.fields.get(key);
|
|
1146
|
-
if (val === void 0) return void 0;
|
|
1147
|
-
if (tail.length === 0) return val;
|
|
1148
|
-
if (isResObj(val)) return lookupPath(val, tail);
|
|
1149
|
-
return void 0;
|
|
1150
|
-
}
|
|
1151
|
-
function lookupResObj(root, segments) {
|
|
1152
|
-
let cur = root;
|
|
1153
|
-
for (const seg of segments) {
|
|
1154
|
-
const val = cur.fields.get(seg.text);
|
|
1155
|
-
if (val === void 0 || !isResObj(val)) return void 0;
|
|
1156
|
-
cur = val;
|
|
1157
|
-
}
|
|
1158
|
-
return cur;
|
|
1159
|
-
}
|
|
1160
|
-
function deepMergeHoconValues(base, overlay) {
|
|
1161
|
-
const merged = new Map(base.fields);
|
|
1162
|
-
for (const [k, v] of overlay.fields) {
|
|
1163
|
-
const existing = merged.get(k);
|
|
1164
|
-
if (existing?.kind === "object" && v.kind === "object") {
|
|
1165
|
-
merged.set(k, deepMergeHoconValues(existing, v));
|
|
1166
|
-
} else {
|
|
1167
|
-
merged.set(k, v);
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
return { kind: "object", fields: merged };
|
|
1171
|
-
}
|
|
1172
|
-
function deepMergeResObjInto(dst, src) {
|
|
1173
|
-
for (const [k, srcVal] of src.fields) {
|
|
1174
|
-
const dstVal = dst.fields.get(k);
|
|
1175
|
-
if (dstVal !== void 0 && isResObj(dstVal) && isResObj(srcVal)) {
|
|
1176
|
-
deepMergeResObjInto(dstVal, srcVal);
|
|
1177
|
-
} else {
|
|
1178
|
-
if (dstVal !== void 0) dst.priorValues.set(k, dstVal);
|
|
1179
|
-
dst.fields.set(k, srcVal);
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
for (const [k, srcPrior] of src.priorValues) {
|
|
1183
|
-
if (!dst.priorValues.has(k)) {
|
|
1184
|
-
dst.priorValues.set(k, srcPrior);
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1188
|
-
function hoconValueToResObj(hv) {
|
|
1189
|
-
const obj = makeResObj();
|
|
1190
|
-
if (hv.kind !== "object") return obj;
|
|
1191
|
-
for (const [key, val] of hv.fields) {
|
|
1192
|
-
if (val.kind === "object") {
|
|
1193
|
-
obj.fields.set(key, hoconValueToResObj(val));
|
|
1194
|
-
} else {
|
|
1195
|
-
obj.fields.set(key, val);
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
return obj;
|
|
1199
|
-
}
|
|
1200
|
-
function isFileNotFoundError(e) {
|
|
1201
|
-
if (!(e instanceof Error)) return false;
|
|
1202
|
-
const code = e.code;
|
|
1203
|
-
if (code === "ENOENT" || code === "MODULE_NOT_FOUND") return true;
|
|
1204
|
-
const msg = e.message.toLowerCase();
|
|
1205
|
-
return msg.includes("not found") || msg.includes("no such file") || msg.includes("enoent");
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
// src/internal/resolver/include-loader.ts
|
|
1209
|
-
import * as nodePath from "path";
|
|
1210
|
-
|
|
1211
1152
|
// src/internal/properties/properties.ts
|
|
1212
1153
|
function parseProperties(input) {
|
|
1213
1154
|
const root = /* @__PURE__ */ Object.create(null);
|
|
@@ -1267,6 +1208,84 @@ function recordToHoconValue(obj) {
|
|
|
1267
1208
|
}
|
|
1268
1209
|
|
|
1269
1210
|
// src/internal/resolver/include-loader.ts
|
|
1211
|
+
var _require = createRequire(import.meta.url);
|
|
1212
|
+
function isPnpPath(p) {
|
|
1213
|
+
return p.includes(".yarn/cache") || p.includes(".pnp.cjs") || p.includes(".pnp.loader");
|
|
1214
|
+
}
|
|
1215
|
+
function validatePackageFile(file, identifier) {
|
|
1216
|
+
if (file.length === 0) {
|
|
1217
|
+
throw new ParseError(`include package("${identifier}", ...): file argument must be non-empty`, 0, 0);
|
|
1218
|
+
}
|
|
1219
|
+
if (file.includes("\\")) {
|
|
1220
|
+
throw new ParseError(
|
|
1221
|
+
`include package("${identifier}", "${file}"): backslash not allowed in file argument (use forward slash)`,
|
|
1222
|
+
0,
|
|
1223
|
+
0
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
if (file.startsWith("/")) {
|
|
1227
|
+
throw new ParseError(
|
|
1228
|
+
`include package("${identifier}", "${file}"): absolute path not allowed in file argument`,
|
|
1229
|
+
0,
|
|
1230
|
+
0
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
if (/(?:^|\/)\.\.?(?:\/|$)/u.test(file)) {
|
|
1234
|
+
throw new ParseError(
|
|
1235
|
+
`include package("${identifier}", "${file}"): path traversal (. or ..) not allowed in file argument`,
|
|
1236
|
+
0,
|
|
1237
|
+
0
|
|
1238
|
+
);
|
|
1239
|
+
}
|
|
1240
|
+
if (file.includes("//")) {
|
|
1241
|
+
throw new ParseError(
|
|
1242
|
+
`include package("${identifier}", "${file}"): consecutive slashes not allowed in file argument`,
|
|
1243
|
+
0,
|
|
1244
|
+
0
|
|
1245
|
+
);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
function validatePackageIdentifier(identifier) {
|
|
1249
|
+
if (identifier.length === 0) {
|
|
1250
|
+
throw new ParseError("include package: identifier argument must be non-empty", 0, 0);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
function defaultPackageResolver(identifier, file, includingFile, baseDir, resolveFrom) {
|
|
1254
|
+
const from = resolveFrom ? Array.isArray(resolveFrom) ? resolveFrom : [resolveFrom] : baseDir ? [baseDir] : includingFile ? [nodePath.dirname(includingFile)] : [process.cwd()];
|
|
1255
|
+
let resolved;
|
|
1256
|
+
try {
|
|
1257
|
+
resolved = _require.resolve(`${identifier}/${file}`, { paths: from });
|
|
1258
|
+
} catch {
|
|
1259
|
+
throw new PackageLookupError(
|
|
1260
|
+
`include package("${identifier}", "${file}"): module not found (starting from: ${from.join(", ")})`,
|
|
1261
|
+
identifier,
|
|
1262
|
+
file,
|
|
1263
|
+
0,
|
|
1264
|
+
0
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
if (isPnpPath(resolved)) {
|
|
1268
|
+
throw new ResolveError(
|
|
1269
|
+
`include package("${identifier}", "${file}"): Yarn Berry PnP store paths are not supported. Provide a custom packageResolver option to handle PnP resolution.`,
|
|
1270
|
+
`${identifier}/${file}`,
|
|
1271
|
+
0,
|
|
1272
|
+
0
|
|
1273
|
+
);
|
|
1274
|
+
}
|
|
1275
|
+
const fileParts = file.split("/");
|
|
1276
|
+
const expectedBasename = fileParts[fileParts.length - 1] ?? file;
|
|
1277
|
+
const resolvedBasename = nodePath.basename(resolved);
|
|
1278
|
+
if (resolvedBasename !== expectedBasename) {
|
|
1279
|
+
throw new PackageLookupError(
|
|
1280
|
+
`include package("${identifier}", "${file}"): case mismatch \u2014 resolved "${resolvedBasename}" but expected "${expectedBasename}" (E11 decision 5: case-sensitive)`,
|
|
1281
|
+
identifier,
|
|
1282
|
+
file,
|
|
1283
|
+
0,
|
|
1284
|
+
0
|
|
1285
|
+
);
|
|
1286
|
+
}
|
|
1287
|
+
return resolved;
|
|
1288
|
+
}
|
|
1270
1289
|
var IncludeLoader = class {
|
|
1271
1290
|
constructor(opts) {
|
|
1272
1291
|
this.opts = opts;
|
|
@@ -1346,8 +1365,77 @@ var IncludeLoader = class {
|
|
|
1346
1365
|
}
|
|
1347
1366
|
return merged;
|
|
1348
1367
|
}
|
|
1368
|
+
/**
|
|
1369
|
+
* Resolve and load `include package("identifier", "file")`.
|
|
1370
|
+
* Validates the identifier and file arguments, resolves via the package resolver,
|
|
1371
|
+
* performs cycle detection using a JSON-serialised cycle key, and parses the content.
|
|
1372
|
+
*/
|
|
1373
|
+
// `required` is reserved for future optional-package semantics (E11 decision 7);
|
|
1374
|
+
// currently all package lookup failures throw regardless of this flag.
|
|
1375
|
+
loadPackage(identifier, file, _required) {
|
|
1376
|
+
validatePackageIdentifier(identifier);
|
|
1377
|
+
validatePackageFile(file, identifier);
|
|
1378
|
+
const { includeStack = [], baseDir, resolveFrom, packageResolver, readFileSync: readFileSync2 } = this.opts;
|
|
1379
|
+
const cycleKey = JSON.stringify(["package", identifier, file]);
|
|
1380
|
+
if (includeStack.includes(cycleKey)) {
|
|
1381
|
+
throw new ResolveError(
|
|
1382
|
+
`circular include: package("${identifier}", "${file}")`,
|
|
1383
|
+
`${identifier}/${file}`,
|
|
1384
|
+
0,
|
|
1385
|
+
0
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
if (includeStack.length >= 50) {
|
|
1389
|
+
throw new ResolveError(`include depth limit exceeded (max 50)`, `${identifier}/${file}`, 0, 0);
|
|
1390
|
+
}
|
|
1391
|
+
const resolver = packageResolver ?? ((id, f, includingFile, bDir) => defaultPackageResolver(id, f, includingFile, bDir, resolveFrom));
|
|
1392
|
+
const resolvedPath = resolver(identifier, file, void 0, baseDir);
|
|
1393
|
+
const content = readFileSync2(resolvedPath);
|
|
1394
|
+
if (content.length === 0) return makeResObj();
|
|
1395
|
+
const tokens = tokenize(content);
|
|
1396
|
+
assertNonEmptyDocument(tokens, resolvedPath);
|
|
1397
|
+
const ast = parseTokens(tokens);
|
|
1398
|
+
return this.onBuildResObj(ast, {
|
|
1399
|
+
...this.opts,
|
|
1400
|
+
baseDir: nodePath.dirname(resolvedPath),
|
|
1401
|
+
includeStack: [...includeStack, cycleKey]
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Async variant of `loadPackage`.
|
|
1406
|
+
*/
|
|
1407
|
+
async loadPackageAsync(identifier, file, _required) {
|
|
1408
|
+
validatePackageIdentifier(identifier);
|
|
1409
|
+
validatePackageFile(file, identifier);
|
|
1410
|
+
const { includeStack = [], baseDir, resolveFrom, packageResolver, readFile, readFileSync: readFileSync2 } = this.opts;
|
|
1411
|
+
const cycleKey = JSON.stringify(["package", identifier, file]);
|
|
1412
|
+
if (includeStack.includes(cycleKey)) {
|
|
1413
|
+
throw new ResolveError(
|
|
1414
|
+
`circular include: package("${identifier}", "${file}")`,
|
|
1415
|
+
`${identifier}/${file}`,
|
|
1416
|
+
0,
|
|
1417
|
+
0
|
|
1418
|
+
);
|
|
1419
|
+
}
|
|
1420
|
+
if (includeStack.length >= 50) {
|
|
1421
|
+
throw new ResolveError(`include depth limit exceeded (max 50)`, `${identifier}/${file}`, 0, 0);
|
|
1422
|
+
}
|
|
1423
|
+
const resolver = packageResolver ?? ((id, f, includingFile, bDir) => defaultPackageResolver(id, f, includingFile, bDir, resolveFrom));
|
|
1424
|
+
const resolvedPath = resolver(identifier, file, void 0, baseDir);
|
|
1425
|
+
const read = readFile ? async (p) => readFile(p) : async (p) => readFileSync2(p);
|
|
1426
|
+
const content = await read(resolvedPath);
|
|
1427
|
+
if (content.length === 0) return makeResObj();
|
|
1428
|
+
const tokens = tokenize(content);
|
|
1429
|
+
assertNonEmptyDocument(tokens, resolvedPath);
|
|
1430
|
+
const ast = parseTokens(tokens);
|
|
1431
|
+
return this.onBuildResObjAsync(ast, {
|
|
1432
|
+
...this.opts,
|
|
1433
|
+
baseDir: nodePath.dirname(resolvedPath),
|
|
1434
|
+
includeStack: [...includeStack, cycleKey]
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1349
1437
|
loadSingle(candidate) {
|
|
1350
|
-
const { readFileSync: readFileSync2, includeStack = []
|
|
1438
|
+
const { readFileSync: readFileSync2, includeStack = [] } = this.opts;
|
|
1351
1439
|
if (includeStack.includes(candidate)) {
|
|
1352
1440
|
throw new ResolveError(`circular include: ${candidate}`, candidate, 0, 0);
|
|
1353
1441
|
}
|
|
@@ -1365,14 +1453,13 @@ var IncludeLoader = class {
|
|
|
1365
1453
|
assertNonEmptyDocument(tokens, candidate);
|
|
1366
1454
|
const ast = parseTokens(tokens);
|
|
1367
1455
|
return this.onBuildResObj(ast, {
|
|
1368
|
-
|
|
1456
|
+
...this.opts,
|
|
1369
1457
|
baseDir: nodePath.dirname(candidate),
|
|
1370
|
-
readFileSync: readFileSync2,
|
|
1371
1458
|
includeStack: [...includeStack, candidate]
|
|
1372
1459
|
});
|
|
1373
1460
|
}
|
|
1374
1461
|
async loadSingleAsync(candidate) {
|
|
1375
|
-
const { readFile, readFileSync: readFileSync2, includeStack = []
|
|
1462
|
+
const { readFile, readFileSync: readFileSync2, includeStack = [] } = this.opts;
|
|
1376
1463
|
const read = readFile ? async (p) => readFile(p) : async (p) => readFileSync2(p);
|
|
1377
1464
|
if (includeStack.includes(candidate)) {
|
|
1378
1465
|
throw new ResolveError(`circular include: ${candidate}`, candidate, 0, 0);
|
|
@@ -1391,10 +1478,8 @@ var IncludeLoader = class {
|
|
|
1391
1478
|
assertNonEmptyDocument(tokens, candidate);
|
|
1392
1479
|
const ast = parseTokens(tokens);
|
|
1393
1480
|
return this.onBuildResObjAsync(ast, {
|
|
1394
|
-
|
|
1481
|
+
...this.opts,
|
|
1395
1482
|
baseDir: nodePath.dirname(candidate),
|
|
1396
|
-
readFileSync: readFileSync2,
|
|
1397
|
-
readFile,
|
|
1398
1483
|
includeStack: [...includeStack, candidate]
|
|
1399
1484
|
});
|
|
1400
1485
|
}
|
|
@@ -1403,7 +1488,6 @@ var IncludeLoader = class {
|
|
|
1403
1488
|
// src/internal/resolver/structure-builder.ts
|
|
1404
1489
|
var StructureBuilder = class _StructureBuilder {
|
|
1405
1490
|
constructor(opts) {
|
|
1406
|
-
this.opts = opts;
|
|
1407
1491
|
this.loader = new IncludeLoader(opts);
|
|
1408
1492
|
this.loader.onBuildResObj = (a, o) => new _StructureBuilder(o).build(a);
|
|
1409
1493
|
this.loader.onBuildResObjAsync = async (a, o) => new _StructureBuilder(o).buildAsync(a);
|
|
@@ -1430,7 +1514,13 @@ var StructureBuilder = class _StructureBuilder {
|
|
|
1430
1514
|
}
|
|
1431
1515
|
applyField(obj, field, pathPrefix) {
|
|
1432
1516
|
if (field.key.length === 0 && field.value.kind === "include") {
|
|
1433
|
-
const
|
|
1517
|
+
const { qualifier, path: path2, required } = field.value;
|
|
1518
|
+
let included;
|
|
1519
|
+
if (qualifier.kind === "package") {
|
|
1520
|
+
included = this.loader.loadPackage(qualifier.identifier, path2, required);
|
|
1521
|
+
} else {
|
|
1522
|
+
included = this.loader.load(path2, required, qualifier.kind === "file");
|
|
1523
|
+
}
|
|
1434
1524
|
if (pathPrefix.length > 0) {
|
|
1435
1525
|
this.relativizeResObj(included, pathPrefix);
|
|
1436
1526
|
}
|
|
@@ -1472,7 +1562,13 @@ var StructureBuilder = class _StructureBuilder {
|
|
|
1472
1562
|
}
|
|
1473
1563
|
async applyFieldAsync(obj, field, pathPrefix) {
|
|
1474
1564
|
if (field.key.length === 0 && field.value.kind === "include") {
|
|
1475
|
-
const
|
|
1565
|
+
const { qualifier, path: path2, required } = field.value;
|
|
1566
|
+
let included;
|
|
1567
|
+
if (qualifier.kind === "package") {
|
|
1568
|
+
included = await this.loader.loadPackageAsync(qualifier.identifier, path2, required);
|
|
1569
|
+
} else {
|
|
1570
|
+
included = await this.loader.loadAsync(path2, required, qualifier.kind === "file");
|
|
1571
|
+
}
|
|
1476
1572
|
if (pathPrefix.length > 0) {
|
|
1477
1573
|
this.relativizeResObj(included, pathPrefix);
|
|
1478
1574
|
}
|
|
@@ -1602,6 +1698,23 @@ var StructureBuilder = class _StructureBuilder {
|
|
|
1602
1698
|
}
|
|
1603
1699
|
};
|
|
1604
1700
|
|
|
1701
|
+
// src/value/numeric-array.ts
|
|
1702
|
+
function numericObjectToArray(value) {
|
|
1703
|
+
if (value.kind !== "object") return null;
|
|
1704
|
+
if (value.fields.size === 0) return null;
|
|
1705
|
+
const canonicalInt = /^(0|[1-9][0-9]*)$/;
|
|
1706
|
+
const eligible = [];
|
|
1707
|
+
for (const [key, val] of value.fields) {
|
|
1708
|
+
if (!canonicalInt.test(key)) continue;
|
|
1709
|
+
const n = Number(key);
|
|
1710
|
+
if (!Number.isInteger(n) || n > 2147483647) continue;
|
|
1711
|
+
eligible.push({ n, v: val });
|
|
1712
|
+
}
|
|
1713
|
+
if (eligible.length === 0) return null;
|
|
1714
|
+
eligible.sort((a, b) => a.n - b.n);
|
|
1715
|
+
return eligible.map((e) => e.v);
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1605
1718
|
// src/internal/resolver/substitution-resolver.ts
|
|
1606
1719
|
var SubstitutionResolver = class {
|
|
1607
1720
|
constructor(root, opts) {
|
|
@@ -1623,6 +1736,10 @@ var SubstitutionResolver = class {
|
|
|
1623
1736
|
// xx.hocon PR (see Phase 6 #3f close-out notes).
|
|
1624
1737
|
this.resolvingConcats = /* @__PURE__ */ new WeakSet();
|
|
1625
1738
|
}
|
|
1739
|
+
/** Returns a prefix for error messages, including origin info when set. */
|
|
1740
|
+
originPrefix() {
|
|
1741
|
+
return this.opts.originDescription ? `${this.opts.originDescription}: ` : "";
|
|
1742
|
+
}
|
|
1626
1743
|
resolve() {
|
|
1627
1744
|
return this.resolveResObj(this.root);
|
|
1628
1745
|
}
|
|
@@ -1663,7 +1780,8 @@ var SubstitutionResolver = class {
|
|
|
1663
1780
|
if (isConcat(v)) {
|
|
1664
1781
|
this.resolvingConcats.add(v);
|
|
1665
1782
|
try {
|
|
1666
|
-
|
|
1783
|
+
const concatResult = this.resolveConcat(v.nodes, scope, v.line, v.col);
|
|
1784
|
+
return concatResult;
|
|
1667
1785
|
} finally {
|
|
1668
1786
|
this.resolvingConcats.delete(v);
|
|
1669
1787
|
}
|
|
@@ -1708,7 +1826,7 @@ var SubstitutionResolver = class {
|
|
|
1708
1826
|
}
|
|
1709
1827
|
if (s.optional) return void 0;
|
|
1710
1828
|
throw new ResolveError(
|
|
1711
|
-
`circular substitution: ${key}`,
|
|
1829
|
+
this.originPrefix() + `circular substitution: ${key}`,
|
|
1712
1830
|
key,
|
|
1713
1831
|
s.line,
|
|
1714
1832
|
s.col
|
|
@@ -1738,7 +1856,7 @@ var SubstitutionResolver = class {
|
|
|
1738
1856
|
}
|
|
1739
1857
|
if (s.optional) return void 0;
|
|
1740
1858
|
throw new ResolveError(
|
|
1741
|
-
|
|
1859
|
+
`${this.originPrefix()}could not resolve substitution: \${${key}}`,
|
|
1742
1860
|
key,
|
|
1743
1861
|
s.line,
|
|
1744
1862
|
s.col
|
|
@@ -1771,7 +1889,7 @@ var SubstitutionResolver = class {
|
|
|
1771
1889
|
if (result !== void 0) this.cache.set(key, result);
|
|
1772
1890
|
return result;
|
|
1773
1891
|
}
|
|
1774
|
-
if (s.listSuffix) {
|
|
1892
|
+
if (s.listSuffix && this.opts.useSystemEnvironment !== false) {
|
|
1775
1893
|
const result = this.resolveEnvList(s);
|
|
1776
1894
|
if (result !== void 0) {
|
|
1777
1895
|
this.cache.set(key, result);
|
|
@@ -1780,22 +1898,36 @@ var SubstitutionResolver = class {
|
|
|
1780
1898
|
if (s.optional) return void 0;
|
|
1781
1899
|
const envBase = s.segments.map((seg) => seg.text).join(".");
|
|
1782
1900
|
throw new ResolveError(
|
|
1783
|
-
|
|
1901
|
+
`${this.originPrefix()}could not resolve substitution: \${${key}} (no environment variables ${envBase}_0, ${envBase}_1, \u2026 set)`,
|
|
1902
|
+
key,
|
|
1903
|
+
s.line,
|
|
1904
|
+
s.col
|
|
1905
|
+
);
|
|
1906
|
+
} else if (s.listSuffix) {
|
|
1907
|
+
if (s.optional) return void 0;
|
|
1908
|
+
if (this.opts.allowUnresolved) return s;
|
|
1909
|
+
throw new ResolveError(
|
|
1910
|
+
`${this.originPrefix()}could not resolve substitution: \${${key}}`,
|
|
1784
1911
|
key,
|
|
1785
1912
|
s.line,
|
|
1786
1913
|
s.col
|
|
1787
1914
|
);
|
|
1788
1915
|
}
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1916
|
+
if (this.opts.useSystemEnvironment !== false) {
|
|
1917
|
+
const envKey = s.segments.map((seg) => seg.text).join(".");
|
|
1918
|
+
const envVal = this.opts.env[envKey] ?? (s.prefixLen > 0 ? this.opts.env[s.segments.slice(s.prefixLen).map((seg) => seg.text).join(".")] : void 0);
|
|
1919
|
+
if (envVal !== void 0) {
|
|
1920
|
+
const result = { kind: "scalar", raw: envVal, valueType: "string" };
|
|
1921
|
+
this.cache.set(key, result);
|
|
1922
|
+
return result;
|
|
1923
|
+
}
|
|
1795
1924
|
}
|
|
1796
1925
|
if (s.optional) return void 0;
|
|
1926
|
+
if (this.opts.allowUnresolved) {
|
|
1927
|
+
return s;
|
|
1928
|
+
}
|
|
1797
1929
|
throw new ResolveError(
|
|
1798
|
-
|
|
1930
|
+
`${this.originPrefix()}could not resolve substitution: \${${key}}`,
|
|
1799
1931
|
key,
|
|
1800
1932
|
s.line,
|
|
1801
1933
|
s.col
|
|
@@ -1840,8 +1972,16 @@ var SubstitutionResolver = class {
|
|
|
1840
1972
|
return [full, bare];
|
|
1841
1973
|
}
|
|
1842
1974
|
resolveConcat(nodes, scope, line = 0, col = 0) {
|
|
1843
|
-
const
|
|
1844
|
-
if (
|
|
1975
|
+
const rawResolved = nodes.map((n) => this.resolveVal(n, scope));
|
|
1976
|
+
if (this.opts.allowUnresolved) {
|
|
1977
|
+
for (const v of rawResolved) {
|
|
1978
|
+
if (v !== void 0 && isSubst(v)) {
|
|
1979
|
+
return v;
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
const resolved = rawResolved.filter((v) => v !== void 0);
|
|
1984
|
+
if (resolved.length === 0) return void 0;
|
|
1845
1985
|
if (resolved.length === 1) return resolved[0];
|
|
1846
1986
|
const nonSep = resolved.filter((v) => !separatorValues.has(v));
|
|
1847
1987
|
if (nonSep.length === 0) {
|
|
@@ -1915,27 +2055,512 @@ var SubstitutionResolver = class {
|
|
|
1915
2055
|
};
|
|
1916
2056
|
|
|
1917
2057
|
// src/internal/resolver/resolver.ts
|
|
1918
|
-
function
|
|
1919
|
-
|
|
1920
|
-
return new SubstitutionResolver(root, opts).resolve();
|
|
2058
|
+
function buildTree(ast, opts) {
|
|
2059
|
+
return new StructureBuilder(opts).build(ast);
|
|
1921
2060
|
}
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
return new SubstitutionResolver(root, opts).resolve();
|
|
2061
|
+
function resolveTree(tree, opts) {
|
|
2062
|
+
return new SubstitutionResolver(tree, opts).resolve();
|
|
1925
2063
|
}
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
if (opts.env !== void 0) return opts.env;
|
|
1932
|
-
if (typeof process !== "undefined" && process.env) return process.env;
|
|
1933
|
-
return {};
|
|
2064
|
+
function containsPlaceholders(tree) {
|
|
2065
|
+
for (const val of tree.fields.values()) {
|
|
2066
|
+
if (valContainsPlaceholders(val)) return true;
|
|
2067
|
+
}
|
|
2068
|
+
return false;
|
|
1934
2069
|
}
|
|
1935
|
-
function
|
|
1936
|
-
|
|
2070
|
+
function valContainsPlaceholders(v) {
|
|
2071
|
+
const rv = v;
|
|
2072
|
+
if (isSubst(rv) || isConcat(rv) || isAppend(rv)) return true;
|
|
2073
|
+
if (isResObj(rv)) return containsPlaceholders(rv);
|
|
2074
|
+
const hv = v;
|
|
2075
|
+
if (hv?.kind === "array") {
|
|
2076
|
+
return hv.items.some((item) => valContainsPlaceholders(item));
|
|
2077
|
+
}
|
|
2078
|
+
return false;
|
|
1937
2079
|
}
|
|
1938
|
-
|
|
2080
|
+
function buildPartialHoconFromResObj(tree) {
|
|
2081
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2082
|
+
for (const [k, v] of tree.fields) {
|
|
2083
|
+
if (!isSubst(v) && !isConcat(v) && !isAppend(v)) {
|
|
2084
|
+
if (isResObj(v)) {
|
|
2085
|
+
fields.set(k, buildPartialHoconFromResObj(v));
|
|
2086
|
+
} else {
|
|
2087
|
+
if (!valContainsPlaceholders(v)) {
|
|
2088
|
+
fields.set(k, v);
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
return { kind: "object", fields };
|
|
2094
|
+
}
|
|
2095
|
+
function hoconValueToResObj2(value) {
|
|
2096
|
+
const obj = makeResObj();
|
|
2097
|
+
for (const [k, v] of value.fields) {
|
|
2098
|
+
if (v.kind === "object") {
|
|
2099
|
+
obj.fields.set(k, hoconValueToResObj2(v));
|
|
2100
|
+
} else {
|
|
2101
|
+
obj.fields.set(k, v);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
return obj;
|
|
2105
|
+
}
|
|
2106
|
+
function resolve2(ast, opts) {
|
|
2107
|
+
const tree = buildTree(ast, opts);
|
|
2108
|
+
return resolveTree(tree, { ...opts, useSystemEnvironment: true, allowUnresolved: false });
|
|
2109
|
+
}
|
|
2110
|
+
async function resolveAsync(ast, opts) {
|
|
2111
|
+
const root = await new StructureBuilder(opts).buildAsync(ast);
|
|
2112
|
+
return resolveTree(root, { ...opts, useSystemEnvironment: true, allowUnresolved: false });
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
// src/config.ts
|
|
2116
|
+
var Config = class _Config {
|
|
2117
|
+
constructor(root, opts) {
|
|
2118
|
+
this.root = root;
|
|
2119
|
+
this._resolved = opts?.resolved ?? true;
|
|
2120
|
+
this._parseBaseDir = opts?.parseBaseDir;
|
|
2121
|
+
this._originDescription = opts?.originDescription;
|
|
2122
|
+
this._resObjRoot = opts?.resObjRoot;
|
|
2123
|
+
this._resolveOpts = opts?.resolveOpts;
|
|
2124
|
+
}
|
|
2125
|
+
/** Returns true when the Config's value tree contains no unresolved
|
|
2126
|
+
* substitution placeholders. Whole-config granularity (E12 decision 11). */
|
|
2127
|
+
isResolved() {
|
|
2128
|
+
return this._resolved;
|
|
2129
|
+
}
|
|
2130
|
+
/** @internal Test-only: construct a resolved Config from a HoconValue. */
|
|
2131
|
+
static _fromResolvedValue(root, opts) {
|
|
2132
|
+
return new _Config(root, { resolved: true, originDescription: opts?.originDescription });
|
|
2133
|
+
}
|
|
2134
|
+
/** @internal Construct an unresolved Config carrying a ResObj tree.
|
|
2135
|
+
* Used by parseStringWithOptions (deferred path) in T6. */
|
|
2136
|
+
static _fromUnresolvedResObj(tree, opts) {
|
|
2137
|
+
const partialRoot = buildPartialHoconFromResObj(tree);
|
|
2138
|
+
return new _Config(partialRoot, {
|
|
2139
|
+
resolved: opts.resolved,
|
|
2140
|
+
parseBaseDir: opts.parseBaseDir,
|
|
2141
|
+
originDescription: opts.originDescription,
|
|
2142
|
+
resObjRoot: tree,
|
|
2143
|
+
resolveOpts: opts.resolveOpts
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
get(path2) {
|
|
2147
|
+
const v = this.lookupNode(path2);
|
|
2148
|
+
if (v === void 0) return void 0;
|
|
2149
|
+
return hoconToJs(v);
|
|
2150
|
+
}
|
|
2151
|
+
getString(path2) {
|
|
2152
|
+
const v = this.requireScalar(path2);
|
|
2153
|
+
return v.raw;
|
|
2154
|
+
}
|
|
2155
|
+
getNumber(path2) {
|
|
2156
|
+
const v = this.requireScalar(path2);
|
|
2157
|
+
const coerced = coerceNumber(v.raw);
|
|
2158
|
+
if (coerced !== void 0) return coerced;
|
|
2159
|
+
throw new ConfigError(`expected number at ${path2}, got ${v.valueType}`, path2);
|
|
2160
|
+
}
|
|
2161
|
+
getBoolean(path2) {
|
|
2162
|
+
const v = this.requireScalar(path2);
|
|
2163
|
+
const coerced = coerceBoolean(v.raw);
|
|
2164
|
+
if (coerced !== void 0) return coerced;
|
|
2165
|
+
throw new ConfigError(`expected boolean at ${path2}, got ${v.valueType}`, path2);
|
|
2166
|
+
}
|
|
2167
|
+
getDuration(path2, unit) {
|
|
2168
|
+
const v = this.requireScalar(path2);
|
|
2169
|
+
if (v.valueType !== "string" && v.valueType !== "number") {
|
|
2170
|
+
throw new ConfigError(`expected duration at ${path2}, got ${v.valueType}`, path2);
|
|
2171
|
+
}
|
|
2172
|
+
const result = parseDuration(v.raw, unit);
|
|
2173
|
+
if (Number.isNaN(result)) throw new ConfigError(`invalid duration at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
2174
|
+
return result;
|
|
2175
|
+
}
|
|
2176
|
+
getBytes(path2, unit) {
|
|
2177
|
+
const v = this.requireScalar(path2);
|
|
2178
|
+
if (v.valueType !== "string" && v.valueType !== "number") {
|
|
2179
|
+
throw new ConfigError(`expected byte size at ${path2}, got ${v.valueType}`, path2);
|
|
2180
|
+
}
|
|
2181
|
+
const result = parseBytes(v.raw, unit);
|
|
2182
|
+
if (Number.isNaN(result)) throw new ConfigError(`invalid byte size at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
2183
|
+
if (result < 0) throw new ConfigError(`byte size must be non-negative at ${path2}: ${JSON.stringify(v.raw)}`, path2);
|
|
2184
|
+
return result;
|
|
2185
|
+
}
|
|
2186
|
+
getConfig(path2) {
|
|
2187
|
+
const v = this.lookupNode(path2);
|
|
2188
|
+
if (v === void 0) {
|
|
2189
|
+
if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
|
|
2190
|
+
throw new NotResolvedError(path2);
|
|
2191
|
+
}
|
|
2192
|
+
throw new ConfigError(`path not found: ${path2}`, path2);
|
|
2193
|
+
}
|
|
2194
|
+
if (v.kind !== "object") throw new ConfigError(`expected object at ${path2}`, path2);
|
|
2195
|
+
if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
|
|
2196
|
+
throw new NotResolvedError(path2);
|
|
2197
|
+
}
|
|
2198
|
+
return new _Config(v, { resolved: this._resolved });
|
|
2199
|
+
}
|
|
2200
|
+
getList(path2) {
|
|
2201
|
+
const v = this.lookupNode(path2);
|
|
2202
|
+
if (v === void 0) {
|
|
2203
|
+
if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
|
|
2204
|
+
throw new NotResolvedError(path2);
|
|
2205
|
+
}
|
|
2206
|
+
throw new ConfigError(`path not found: ${path2}`, path2);
|
|
2207
|
+
}
|
|
2208
|
+
if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
|
|
2209
|
+
throw new NotResolvedError(path2);
|
|
2210
|
+
}
|
|
2211
|
+
if (v.kind === "object") {
|
|
2212
|
+
const converted = numericObjectToArray(v);
|
|
2213
|
+
if (converted !== null) return converted.map(hoconToJs);
|
|
2214
|
+
}
|
|
2215
|
+
if (v.kind !== "array") throw new ConfigError(`expected array at ${path2}`, path2);
|
|
2216
|
+
return v.items.map(hoconToJs);
|
|
2217
|
+
}
|
|
2218
|
+
has(path2) {
|
|
2219
|
+
return this.lookupNode(path2) !== void 0;
|
|
2220
|
+
}
|
|
2221
|
+
keys() {
|
|
2222
|
+
return [...this.root.fields.keys()];
|
|
2223
|
+
}
|
|
2224
|
+
withFallback(fallback) {
|
|
2225
|
+
if (fallback == null) return this;
|
|
2226
|
+
const selfTree = this._resObjRoot ?? hoconValueToResObj2(this.root);
|
|
2227
|
+
const fbTree = fallback._resObjRoot ?? hoconValueToResObj2(fallback.root);
|
|
2228
|
+
const merged = mergeUnresolved(selfTree, fbTree);
|
|
2229
|
+
const hasPlaceholders = containsPlaceholders(merged);
|
|
2230
|
+
const partialRoot = buildPartialHoconFromResObj(merged);
|
|
2231
|
+
return new _Config(partialRoot, {
|
|
2232
|
+
resolved: !hasPlaceholders,
|
|
2233
|
+
parseBaseDir: this._parseBaseDir,
|
|
2234
|
+
originDescription: this._originDescription,
|
|
2235
|
+
resObjRoot: merged,
|
|
2236
|
+
resolveOpts: this._resolveOpts ?? fallback._resolveOpts
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Runs substitution resolution (phase 2) on the stored unresolved tree.
|
|
2241
|
+
* Idempotent: calling resolve() on an already-resolved Config returns an
|
|
2242
|
+
* equivalent resolved Config.
|
|
2243
|
+
*
|
|
2244
|
+
* opts.allowUnresolved (default false) — when true, leaves unresolved
|
|
2245
|
+
* non-optional placeholders in place rather than throwing.
|
|
2246
|
+
* opts.useSystemEnvironment (default true) — when false, env var lookups
|
|
2247
|
+
* are suppressed (hermetic resolution).
|
|
2248
|
+
*
|
|
2249
|
+
* E12 decision 3.
|
|
2250
|
+
*/
|
|
2251
|
+
resolve(opts = {}) {
|
|
2252
|
+
if (this._resolved) {
|
|
2253
|
+
return new _Config(this.root, {
|
|
2254
|
+
resolved: true,
|
|
2255
|
+
parseBaseDir: this._parseBaseDir,
|
|
2256
|
+
originDescription: this._originDescription
|
|
2257
|
+
});
|
|
2258
|
+
}
|
|
2259
|
+
const tree = this._resObjRoot;
|
|
2260
|
+
if (!tree) {
|
|
2261
|
+
return new _Config(this.root, { resolved: true });
|
|
2262
|
+
}
|
|
2263
|
+
const resolveOpts = {
|
|
2264
|
+
...this._resolveOpts ?? { env: {}, baseDir: void 0, readFileSync: () => {
|
|
2265
|
+
throw new Error("no files");
|
|
2266
|
+
} },
|
|
2267
|
+
allowUnresolved: opts.allowUnresolved ?? false,
|
|
2268
|
+
useSystemEnvironment: opts.useSystemEnvironment ?? true,
|
|
2269
|
+
originDescription: this._originDescription
|
|
2270
|
+
};
|
|
2271
|
+
const resolved = resolveTree(tree, resolveOpts);
|
|
2272
|
+
if (resolved.kind !== "object") throw new Error("resolve: expected object root");
|
|
2273
|
+
if (opts.allowUnresolved) {
|
|
2274
|
+
const { stripped, hadPlaceholders } = stripPlaceholderFields(resolved);
|
|
2275
|
+
return new _Config(stripped, {
|
|
2276
|
+
resolved: !hadPlaceholders,
|
|
2277
|
+
parseBaseDir: this._parseBaseDir,
|
|
2278
|
+
originDescription: this._originDescription,
|
|
2279
|
+
resObjRoot: hadPlaceholders ? tree : void 0,
|
|
2280
|
+
resolveOpts: hadPlaceholders ? resolveOpts : void 0
|
|
2281
|
+
});
|
|
2282
|
+
}
|
|
2283
|
+
return new _Config(resolved, {
|
|
2284
|
+
resolved: true,
|
|
2285
|
+
parseBaseDir: this._parseBaseDir,
|
|
2286
|
+
originDescription: this._originDescription
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
/**
|
|
2290
|
+
* Resolves receiver substitutions using source as lookup context.
|
|
2291
|
+
* Source's keys are NOT merged into the result — differs from
|
|
2292
|
+
* `this.withFallback(source).resolve()` which includes source keys.
|
|
2293
|
+
*
|
|
2294
|
+
* Precondition (E12 decision 10): source must be resolved. If
|
|
2295
|
+
* source.isResolved() is false, throws NotResolvedError.
|
|
2296
|
+
*
|
|
2297
|
+
* On an already-resolved receiver, resolveWith is a no-op (returns an
|
|
2298
|
+
* equivalent resolved Config, source keys not included).
|
|
2299
|
+
*
|
|
2300
|
+
* E12 decisions 9, 10.
|
|
2301
|
+
*/
|
|
2302
|
+
resolveWith(source, opts = {}) {
|
|
2303
|
+
if (!source.isResolved()) {
|
|
2304
|
+
throw new NotResolvedError("source");
|
|
2305
|
+
}
|
|
2306
|
+
if (this._resolved) {
|
|
2307
|
+
return new _Config(this.root, {
|
|
2308
|
+
resolved: true,
|
|
2309
|
+
parseBaseDir: this._parseBaseDir,
|
|
2310
|
+
originDescription: this._originDescription
|
|
2311
|
+
});
|
|
2312
|
+
}
|
|
2313
|
+
const receiverTree = this._resObjRoot ?? hoconValueToResObj2(this.root);
|
|
2314
|
+
const srcTree = hoconValueToResObj2(source.root);
|
|
2315
|
+
const merged = mergeUnresolved(receiverTree, srcTree);
|
|
2316
|
+
const resolveOpts = {
|
|
2317
|
+
...this._resolveOpts ?? { env: {}, baseDir: void 0, readFileSync: () => {
|
|
2318
|
+
throw new Error("no files");
|
|
2319
|
+
} },
|
|
2320
|
+
allowUnresolved: opts.allowUnresolved ?? false,
|
|
2321
|
+
useSystemEnvironment: opts.useSystemEnvironment ?? true,
|
|
2322
|
+
originDescription: this._originDescription
|
|
2323
|
+
};
|
|
2324
|
+
const resolved = resolveTree(merged, resolveOpts);
|
|
2325
|
+
if (resolved.kind !== "object") throw new Error("resolveWith: expected object root");
|
|
2326
|
+
const receiverShapeForFilter = resObjToKeyShape(receiverTree);
|
|
2327
|
+
const filtered = filterByReceiverShape(resolved, receiverShapeForFilter);
|
|
2328
|
+
if (opts.allowUnresolved) {
|
|
2329
|
+
const { stripped, hadPlaceholders } = stripPlaceholderFields(filtered);
|
|
2330
|
+
return new _Config(stripped, {
|
|
2331
|
+
resolved: !hadPlaceholders,
|
|
2332
|
+
parseBaseDir: this._parseBaseDir,
|
|
2333
|
+
originDescription: this._originDescription,
|
|
2334
|
+
resObjRoot: hadPlaceholders ? receiverTree : void 0,
|
|
2335
|
+
resolveOpts: hadPlaceholders ? resolveOpts : void 0
|
|
2336
|
+
});
|
|
2337
|
+
}
|
|
2338
|
+
return new _Config(filtered, {
|
|
2339
|
+
resolved: true,
|
|
2340
|
+
parseBaseDir: this._parseBaseDir,
|
|
2341
|
+
originDescription: this._originDescription
|
|
2342
|
+
});
|
|
2343
|
+
}
|
|
2344
|
+
toObject() {
|
|
2345
|
+
return hoconToJs(this.root);
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* @internal Test-only: renders this resolved Config's value tree as canonical
|
|
2349
|
+
* JSON (sorted keys, no whitespace). Used by Layer-2 fixture tests to compare
|
|
2350
|
+
* against Lightbend ground truth. NOT part of the public API.
|
|
2351
|
+
*
|
|
2352
|
+
* Throws if the Config is unresolved (contains placeholders).
|
|
2353
|
+
*/
|
|
2354
|
+
_renderJSONForTest() {
|
|
2355
|
+
return renderHoconAsJSON(this.root);
|
|
2356
|
+
}
|
|
2357
|
+
lookupNode(path2) {
|
|
2358
|
+
const segments = splitConfigPath(path2);
|
|
2359
|
+
let current = this.root;
|
|
2360
|
+
for (const seg of segments) {
|
|
2361
|
+
if (current.kind !== "object") return void 0;
|
|
2362
|
+
const next = current.fields.get(seg);
|
|
2363
|
+
if (next === void 0) return void 0;
|
|
2364
|
+
current = next;
|
|
2365
|
+
}
|
|
2366
|
+
return current;
|
|
2367
|
+
}
|
|
2368
|
+
/**
|
|
2369
|
+
* _resObjRootSubtreeHasPlaceholders — walk _resObjRoot to the given path and
|
|
2370
|
+
* return true if the subtree there contains any unresolved placeholder.
|
|
2371
|
+
* Used by getConfig / getList (T2) to detect transitive unresolved placeholders
|
|
2372
|
+
* in object / array subtrees that would otherwise appear to be "resolved" in the
|
|
2373
|
+
* partial HoconValue root (because individual leaf fields are omitted rather than
|
|
2374
|
+
* the whole subtree when their value is a direct placeholder scalar, but object
|
|
2375
|
+
* sub-containers are retained in the partial root with the placeholder-keyed
|
|
2376
|
+
* fields omitted from them — so the object appears "present but empty-ish").
|
|
2377
|
+
*/
|
|
2378
|
+
_resObjRootSubtreeHasPlaceholders(path2) {
|
|
2379
|
+
const tree = this._resObjRoot;
|
|
2380
|
+
if (!tree) return false;
|
|
2381
|
+
const segments = splitConfigPath(path2);
|
|
2382
|
+
let cur = tree;
|
|
2383
|
+
for (const seg of segments) {
|
|
2384
|
+
const val = cur.fields.get(seg);
|
|
2385
|
+
if (val === void 0) return false;
|
|
2386
|
+
if (isResObj(val)) {
|
|
2387
|
+
cur = val;
|
|
2388
|
+
} else {
|
|
2389
|
+
return valContainsPlaceholders(val);
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
return containsPlaceholders(cur);
|
|
2393
|
+
}
|
|
2394
|
+
requireScalar(path2) {
|
|
2395
|
+
const v = this.lookupNode(path2);
|
|
2396
|
+
if (v === void 0) {
|
|
2397
|
+
if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
|
|
2398
|
+
throw new NotResolvedError(path2);
|
|
2399
|
+
}
|
|
2400
|
+
throw new ConfigError(`path not found: ${path2}`, path2);
|
|
2401
|
+
}
|
|
2402
|
+
if (v.kind !== "scalar") throw new ConfigError(`expected scalar at ${path2}, got ${v.kind}`, path2);
|
|
2403
|
+
return v;
|
|
2404
|
+
}
|
|
2405
|
+
};
|
|
2406
|
+
function splitConfigPath(path2) {
|
|
2407
|
+
const segments = [];
|
|
2408
|
+
let i = 0;
|
|
2409
|
+
while (i < path2.length) {
|
|
2410
|
+
if (path2[i] === '"') {
|
|
2411
|
+
i++;
|
|
2412
|
+
let segment = "";
|
|
2413
|
+
let closed = false;
|
|
2414
|
+
while (i < path2.length) {
|
|
2415
|
+
const ch = path2[i];
|
|
2416
|
+
if (ch === "\\" && i + 1 < path2.length) {
|
|
2417
|
+
const next = path2[i + 1];
|
|
2418
|
+
segment += next;
|
|
2419
|
+
i += 2;
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
if (ch === '"') {
|
|
2423
|
+
closed = true;
|
|
2424
|
+
i++;
|
|
2425
|
+
break;
|
|
2426
|
+
}
|
|
2427
|
+
segment += ch;
|
|
2428
|
+
i++;
|
|
2429
|
+
}
|
|
2430
|
+
if (!closed) throw new ConfigError(`unterminated quoted path segment: ${path2}`, path2);
|
|
2431
|
+
segments.push(segment);
|
|
2432
|
+
if (i < path2.length && path2[i] === ".") i++;
|
|
2433
|
+
} else {
|
|
2434
|
+
const dot = path2.indexOf(".", i);
|
|
2435
|
+
if (dot === -1) {
|
|
2436
|
+
segments.push(path2.slice(i));
|
|
2437
|
+
break;
|
|
2438
|
+
}
|
|
2439
|
+
segments.push(path2.slice(i, dot));
|
|
2440
|
+
i = dot + 1;
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
return segments;
|
|
2444
|
+
}
|
|
2445
|
+
function scalarToJs(raw, valueType) {
|
|
2446
|
+
switch (valueType) {
|
|
2447
|
+
case "null":
|
|
2448
|
+
return null;
|
|
2449
|
+
case "boolean":
|
|
2450
|
+
return raw === "true";
|
|
2451
|
+
case "number":
|
|
2452
|
+
return Number(raw);
|
|
2453
|
+
case "string":
|
|
2454
|
+
return raw;
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
function hoconToJs(v) {
|
|
2458
|
+
switch (v.kind) {
|
|
2459
|
+
case "scalar":
|
|
2460
|
+
return scalarToJs(v.raw, v.valueType);
|
|
2461
|
+
case "array":
|
|
2462
|
+
return v.items.map(hoconToJs);
|
|
2463
|
+
case "object": {
|
|
2464
|
+
const obj = /* @__PURE__ */ Object.create(null);
|
|
2465
|
+
for (const [k, val] of v.fields) obj[k] = hoconToJs(val);
|
|
2466
|
+
return Object.assign({}, obj);
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
function resObjToKeyShape(tree) {
|
|
2471
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2472
|
+
for (const [k, v] of tree.fields) {
|
|
2473
|
+
if (isSubst(v) || isConcat(v) || isAppend(v)) {
|
|
2474
|
+
fields.set(k, { kind: "scalar", raw: "", valueType: "null" });
|
|
2475
|
+
} else if (isResObj(v)) {
|
|
2476
|
+
fields.set(k, resObjToKeyShape(v));
|
|
2477
|
+
} else {
|
|
2478
|
+
fields.set(k, v);
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
return { kind: "object", fields };
|
|
2482
|
+
}
|
|
2483
|
+
function stripPlaceholderFields(v) {
|
|
2484
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2485
|
+
let hadPlaceholders = false;
|
|
2486
|
+
for (const [k, val] of v.fields) {
|
|
2487
|
+
if (val._kind === "subst-placeholder" || val._kind === "concat-placeholder" || val._kind === "append-placeholder") {
|
|
2488
|
+
hadPlaceholders = true;
|
|
2489
|
+
} else if (val.kind === "object") {
|
|
2490
|
+
const inner = stripPlaceholderFields(val);
|
|
2491
|
+
if (inner.hadPlaceholders) hadPlaceholders = true;
|
|
2492
|
+
fields.set(k, inner.stripped);
|
|
2493
|
+
} else {
|
|
2494
|
+
fields.set(k, val);
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
return { stripped: { kind: "object", fields }, hadPlaceholders };
|
|
2498
|
+
}
|
|
2499
|
+
function filterByReceiverShape(resolved, receiverShape) {
|
|
2500
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2501
|
+
for (const [k, rv] of resolved.fields) {
|
|
2502
|
+
if (!receiverShape.fields.has(k)) continue;
|
|
2503
|
+
const receiverVal = receiverShape.fields.get(k);
|
|
2504
|
+
if (rv.kind === "object" && receiverVal.kind === "object") {
|
|
2505
|
+
fields.set(k, filterByReceiverShape(rv, receiverVal));
|
|
2506
|
+
} else {
|
|
2507
|
+
fields.set(k, rv);
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
return { kind: "object", fields };
|
|
2511
|
+
}
|
|
2512
|
+
function renderHoconAsJSON(v) {
|
|
2513
|
+
switch (v.kind) {
|
|
2514
|
+
case "scalar": {
|
|
2515
|
+
switch (v.valueType) {
|
|
2516
|
+
case "null":
|
|
2517
|
+
return "null";
|
|
2518
|
+
case "boolean":
|
|
2519
|
+
return v.raw;
|
|
2520
|
+
case "number": {
|
|
2521
|
+
const n = Number(v.raw);
|
|
2522
|
+
if (Number.isFinite(n)) return v.raw;
|
|
2523
|
+
return JSON.stringify(v.raw);
|
|
2524
|
+
}
|
|
2525
|
+
case "string":
|
|
2526
|
+
return JSON.stringify(v.raw);
|
|
2527
|
+
}
|
|
2528
|
+
break;
|
|
2529
|
+
}
|
|
2530
|
+
case "array": {
|
|
2531
|
+
const items = v.items.map(renderHoconAsJSON);
|
|
2532
|
+
return `[${items.join(",")}]`;
|
|
2533
|
+
}
|
|
2534
|
+
case "object": {
|
|
2535
|
+
const keys = [...v.fields.keys()].sort();
|
|
2536
|
+
const pairs = keys.map((k) => {
|
|
2537
|
+
const val = v.fields.get(k);
|
|
2538
|
+
return `${JSON.stringify(k)}:${renderHoconAsJSON(val)}`;
|
|
2539
|
+
});
|
|
2540
|
+
return `{${pairs.join(",")}}`;
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
throw new Error(`renderHoconAsJSON: unsupported value kind (unresolved placeholder?)`);
|
|
2544
|
+
}
|
|
2545
|
+
|
|
2546
|
+
// src/parse.ts
|
|
2547
|
+
import * as fs from "fs";
|
|
2548
|
+
import * as path from "path";
|
|
2549
|
+
function defaultParseOptions() {
|
|
2550
|
+
return { resolveSubstitutions: true };
|
|
2551
|
+
}
|
|
2552
|
+
function defaultResolveOptions() {
|
|
2553
|
+
return { useSystemEnvironment: true, allowUnresolved: false };
|
|
2554
|
+
}
|
|
2555
|
+
function getEnv(opts) {
|
|
2556
|
+
if (opts.env !== void 0) return opts.env;
|
|
2557
|
+
if (typeof process !== "undefined" && process.env) return process.env;
|
|
2558
|
+
return {};
|
|
2559
|
+
}
|
|
2560
|
+
function defaultReadFileSync(filePath) {
|
|
2561
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
2562
|
+
}
|
|
2563
|
+
async function defaultReadFile(filePath) {
|
|
1939
2564
|
return fs.promises.readFile(filePath, "utf-8");
|
|
1940
2565
|
}
|
|
1941
2566
|
function buildResolveContext(input, opts) {
|
|
@@ -1946,7 +2571,10 @@ function buildResolveContext(input, opts) {
|
|
|
1946
2571
|
env: getEnv(opts),
|
|
1947
2572
|
baseDir: opts.baseDir,
|
|
1948
2573
|
readFileSync: opts.readFileSync ?? defaultReadFileSync,
|
|
1949
|
-
readFile: opts.readFile
|
|
2574
|
+
readFile: opts.readFile,
|
|
2575
|
+
originDescription: opts.originDescription,
|
|
2576
|
+
resolveFrom: opts.resolveFrom,
|
|
2577
|
+
packageResolver: opts.packageResolver
|
|
1950
2578
|
};
|
|
1951
2579
|
return { ast, resolveOpts };
|
|
1952
2580
|
}
|
|
@@ -1954,7 +2582,38 @@ function parse(input, opts = {}) {
|
|
|
1954
2582
|
const { ast, resolveOpts } = buildResolveContext(input, opts);
|
|
1955
2583
|
const value = resolve2(ast, resolveOpts);
|
|
1956
2584
|
if (value.kind !== "object") throw new Error("resolved value is not an object");
|
|
1957
|
-
return new Config(value);
|
|
2585
|
+
return new Config(value, { resolved: true, originDescription: opts.originDescription });
|
|
2586
|
+
}
|
|
2587
|
+
var parseString = parse;
|
|
2588
|
+
function parseStringWithOptions(input, opts = {}) {
|
|
2589
|
+
const resolveSubstitutions = opts.resolveSubstitutions ?? true;
|
|
2590
|
+
const { ast, resolveOpts } = buildResolveContext(input, opts);
|
|
2591
|
+
if (resolveSubstitutions) {
|
|
2592
|
+
const value = resolve2(ast, resolveOpts);
|
|
2593
|
+
if (value.kind !== "object") throw new Error("resolved value is not an object");
|
|
2594
|
+
return new Config(value, { resolved: true, originDescription: opts.originDescription });
|
|
2595
|
+
}
|
|
2596
|
+
const tree = buildTree(ast, resolveOpts);
|
|
2597
|
+
const hasPlaceholders = containsPlaceholders(tree);
|
|
2598
|
+
return Config._fromUnresolvedResObj(tree, {
|
|
2599
|
+
parseBaseDir: opts.baseDir,
|
|
2600
|
+
originDescription: opts.originDescription,
|
|
2601
|
+
resolved: !hasPlaceholders,
|
|
2602
|
+
resolveOpts
|
|
2603
|
+
});
|
|
2604
|
+
}
|
|
2605
|
+
function parseFileWithOptions(filePath, opts = {}) {
|
|
2606
|
+
if (typeof process === "undefined" && !opts.readFileSync) {
|
|
2607
|
+
throw new Error("parseFileWithOptions is not supported in browser environments without opts.readFileSync.");
|
|
2608
|
+
}
|
|
2609
|
+
const resolvedPath = path.resolve(filePath);
|
|
2610
|
+
const readFileSync2 = opts.readFileSync ?? defaultReadFileSync;
|
|
2611
|
+
const input = readFileSync2(resolvedPath);
|
|
2612
|
+
return parseStringWithOptions(input, {
|
|
2613
|
+
...opts,
|
|
2614
|
+
baseDir: opts.baseDir ?? path.dirname(resolvedPath),
|
|
2615
|
+
readFileSync: readFileSync2
|
|
2616
|
+
});
|
|
1958
2617
|
}
|
|
1959
2618
|
async function parseAsync(input, opts = {}) {
|
|
1960
2619
|
const { ast, resolveOpts } = buildResolveContext(input, opts);
|
|
@@ -1989,13 +2648,89 @@ async function parseFileAsync(filePath, opts = {}) {
|
|
|
1989
2648
|
readFileSync: opts.readFileSync ?? defaultReadFileSync
|
|
1990
2649
|
});
|
|
1991
2650
|
}
|
|
2651
|
+
|
|
2652
|
+
// src/value-factory.ts
|
|
2653
|
+
function fromMap(values, originDescription) {
|
|
2654
|
+
const fields = coerceObject(values, "");
|
|
2655
|
+
const root = { kind: "object", fields };
|
|
2656
|
+
return Config._fromResolvedValue(root, { originDescription });
|
|
2657
|
+
}
|
|
2658
|
+
function empty(originDescription) {
|
|
2659
|
+
const root = { kind: "object", fields: /* @__PURE__ */ new Map() };
|
|
2660
|
+
return Config._fromResolvedValue(root, { originDescription });
|
|
2661
|
+
}
|
|
2662
|
+
function coerceObject(obj, atPath) {
|
|
2663
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2664
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
2665
|
+
const childPath = atPath ? `${atPath}.${k}` : k;
|
|
2666
|
+
fields.set(k, coerceValue(v, childPath));
|
|
2667
|
+
}
|
|
2668
|
+
return fields;
|
|
2669
|
+
}
|
|
2670
|
+
function coerceValue(v, atPath) {
|
|
2671
|
+
if (v === null) {
|
|
2672
|
+
return { kind: "scalar", raw: "null", valueType: "null" };
|
|
2673
|
+
}
|
|
2674
|
+
if (v === void 0) {
|
|
2675
|
+
throw new ConfigError(
|
|
2676
|
+
`fromMap: undefined is not a valid HOCON value at path "${atPath}"`,
|
|
2677
|
+
atPath
|
|
2678
|
+
);
|
|
2679
|
+
}
|
|
2680
|
+
switch (typeof v) {
|
|
2681
|
+
case "string":
|
|
2682
|
+
return { kind: "scalar", raw: v, valueType: "string" };
|
|
2683
|
+
case "number": {
|
|
2684
|
+
if (Number.isNaN(v) || !Number.isFinite(v)) {
|
|
2685
|
+
throw new ConfigError(
|
|
2686
|
+
`fromMap: ${v} is not representable in HOCON (NaN/Infinity not allowed) at path "${atPath}"`,
|
|
2687
|
+
atPath
|
|
2688
|
+
);
|
|
2689
|
+
}
|
|
2690
|
+
return { kind: "scalar", raw: String(v), valueType: "number" };
|
|
2691
|
+
}
|
|
2692
|
+
case "boolean":
|
|
2693
|
+
return { kind: "scalar", raw: v ? "true" : "false", valueType: "boolean" };
|
|
2694
|
+
case "bigint": {
|
|
2695
|
+
if (v > BigInt(Number.MAX_SAFE_INTEGER) || v < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
2696
|
+
throw new ConfigError(
|
|
2697
|
+
`fromMap: bigint ${v} exceeds Number.MAX_SAFE_INTEGER (not representable as HOCON number) at path "${atPath}"`,
|
|
2698
|
+
atPath
|
|
2699
|
+
);
|
|
2700
|
+
}
|
|
2701
|
+
return { kind: "scalar", raw: String(v), valueType: "number" };
|
|
2702
|
+
}
|
|
2703
|
+
case "object": {
|
|
2704
|
+
if (Array.isArray(v)) {
|
|
2705
|
+
const items = v.map((item, i) => coerceValue(item, `${atPath}[${i}]`));
|
|
2706
|
+
return { kind: "array", items };
|
|
2707
|
+
}
|
|
2708
|
+
const fields = coerceObject(v, atPath);
|
|
2709
|
+
return { kind: "object", fields };
|
|
2710
|
+
}
|
|
2711
|
+
default:
|
|
2712
|
+
throw new ConfigError(
|
|
2713
|
+
`fromMap: unsupported value type "${typeof v}" at path "${atPath}"`,
|
|
2714
|
+
atPath
|
|
2715
|
+
);
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
1992
2718
|
export {
|
|
1993
2719
|
Config,
|
|
1994
2720
|
ConfigError,
|
|
2721
|
+
NotResolvedError,
|
|
2722
|
+
PackageLookupError,
|
|
1995
2723
|
ParseError,
|
|
1996
2724
|
ResolveError,
|
|
2725
|
+
defaultParseOptions,
|
|
2726
|
+
defaultResolveOptions,
|
|
2727
|
+
empty,
|
|
2728
|
+
fromMap,
|
|
1997
2729
|
parse,
|
|
1998
2730
|
parseAsync,
|
|
1999
2731
|
parseFile,
|
|
2000
|
-
parseFileAsync
|
|
2732
|
+
parseFileAsync,
|
|
2733
|
+
parseFileWithOptions,
|
|
2734
|
+
parseString,
|
|
2735
|
+
parseStringWithOptions
|
|
2001
2736
|
};
|