@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/dist/index.cjs CHANGED
@@ -32,12 +32,21 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  Config: () => Config,
34
34
  ConfigError: () => ConfigError,
35
+ NotResolvedError: () => NotResolvedError,
36
+ PackageLookupError: () => PackageLookupError,
35
37
  ParseError: () => ParseError,
36
38
  ResolveError: () => ResolveError,
39
+ defaultParseOptions: () => defaultParseOptions,
40
+ defaultResolveOptions: () => defaultResolveOptions,
41
+ empty: () => empty,
42
+ fromMap: () => fromMap,
37
43
  parse: () => parse,
38
44
  parseAsync: () => parseAsync,
39
45
  parseFile: () => parseFile,
40
- parseFileAsync: () => parseFileAsync
46
+ parseFileAsync: () => parseFileAsync,
47
+ parseFileWithOptions: () => parseFileWithOptions,
48
+ parseString: () => parseString,
49
+ parseStringWithOptions: () => parseStringWithOptions
41
50
  });
42
51
  module.exports = __toCommonJS(src_exports);
43
52
 
@@ -258,6 +267,14 @@ var ResolveError = class extends Error {
258
267
  this.name = "ResolveError";
259
268
  }
260
269
  };
270
+ var PackageLookupError = class extends ResolveError {
271
+ constructor(message, identifier, packageFile, line, col) {
272
+ super(message, `${identifier}/${packageFile}`, line, col);
273
+ this.identifier = identifier;
274
+ this.packageFile = packageFile;
275
+ this.name = "PackageLookupError";
276
+ }
277
+ };
261
278
  var ConfigError = class extends Error {
262
279
  constructor(message, path2) {
263
280
  super(message);
@@ -265,191 +282,155 @@ var ConfigError = class extends Error {
265
282
  this.name = "ConfigError";
266
283
  }
267
284
  };
268
-
269
- // src/value/numeric-array.ts
270
- function numericObjectToArray(value) {
271
- if (value.kind !== "object") return null;
272
- if (value.fields.size === 0) return null;
273
- const canonicalInt = /^(0|[1-9][0-9]*)$/;
274
- const eligible = [];
275
- for (const [key, val] of value.fields) {
276
- if (!canonicalInt.test(key)) continue;
277
- const n = Number(key);
278
- if (!Number.isInteger(n) || n > 2147483647) continue;
279
- eligible.push({ n, v: val });
285
+ var NotResolvedError = class extends ConfigError {
286
+ constructor(path2) {
287
+ super(`value at path "${path2}" is not resolved (call resolve() first)`, path2);
288
+ this.name = "NotResolvedError";
280
289
  }
281
- if (eligible.length === 0) return null;
282
- eligible.sort((a, b) => a.n - b.n);
283
- return eligible.map((e) => e.v);
284
- }
290
+ };
285
291
 
286
- // src/config.ts
287
- var Config = class _Config {
288
- constructor(root) {
289
- this.root = root;
290
- }
291
- get(path2) {
292
- const v = this.lookupNode(path2);
293
- if (v === void 0) return void 0;
294
- return hoconToJs(v);
295
- }
296
- getString(path2) {
297
- const v = this.requireScalar(path2);
298
- return v.raw;
299
- }
300
- getNumber(path2) {
301
- const v = this.requireScalar(path2);
302
- const coerced = coerceNumber(v.raw);
303
- if (coerced !== void 0) return coerced;
304
- throw new ConfigError(`expected number at ${path2}, got ${v.valueType}`, path2);
305
- }
306
- getBoolean(path2) {
307
- const v = this.requireScalar(path2);
308
- const coerced = coerceBoolean(v.raw);
309
- if (coerced !== void 0) return coerced;
310
- throw new ConfigError(`expected boolean at ${path2}, got ${v.valueType}`, path2);
292
+ // src/internal/resolver/types.ts
293
+ var separatorValues = /* @__PURE__ */ new WeakSet();
294
+ function isSubst(v) {
295
+ return v._kind === "subst-placeholder";
296
+ }
297
+ function isConcat(v) {
298
+ return v._kind === "concat-placeholder";
299
+ }
300
+ function isAppend(v) {
301
+ return v._kind === "append-placeholder";
302
+ }
303
+ function isResObj(v) {
304
+ return v._kind === "res-obj";
305
+ }
306
+ function makeResObj() {
307
+ return { _kind: "res-obj", fields: /* @__PURE__ */ new Map(), priorValues: /* @__PURE__ */ new Map() };
308
+ }
309
+ function mergeUnresolved(receiver, fallback) {
310
+ const result = makeResObj();
311
+ for (const [k, v] of fallback.fields) {
312
+ result.fields.set(k, v);
311
313
  }
312
- getDuration(path2, unit) {
313
- const v = this.requireScalar(path2);
314
- if (v.valueType !== "string" && v.valueType !== "number") {
315
- throw new ConfigError(`expected duration at ${path2}, got ${v.valueType}`, path2);
316
- }
317
- const result = parseDuration(v.raw, unit);
318
- if (Number.isNaN(result)) throw new ConfigError(`invalid duration at ${path2}: ${JSON.stringify(v.raw)}`, path2);
319
- return result;
314
+ for (const [k, v] of fallback.priorValues) {
315
+ result.priorValues.set(k, v);
320
316
  }
321
- getBytes(path2, unit) {
322
- const v = this.requireScalar(path2);
323
- if (v.valueType !== "string" && v.valueType !== "number") {
324
- throw new ConfigError(`expected byte size at ${path2}, got ${v.valueType}`, path2);
317
+ for (const [k, rv] of receiver.fields) {
318
+ const existing = result.fields.get(k);
319
+ if (existing !== void 0) {
320
+ const recPrior = receiver.priorValues.get(k);
321
+ if (recPrior !== void 0 && !isResObj(recPrior)) {
322
+ result.fields.set(k, rv);
323
+ continue;
324
+ }
325
+ if (isResObj(rv) && isResObj(existing)) {
326
+ result.fields.set(k, mergeUnresolved(rv, existing));
327
+ continue;
328
+ }
329
+ result.priorValues.set(k, existing);
325
330
  }
326
- const result = parseBytes(v.raw, unit);
327
- if (Number.isNaN(result)) throw new ConfigError(`invalid byte size at ${path2}: ${JSON.stringify(v.raw)}`, path2);
328
- if (result < 0) throw new ConfigError(`byte size must be non-negative at ${path2}: ${JSON.stringify(v.raw)}`, path2);
329
- return result;
330
- }
331
- getConfig(path2) {
332
- const v = this.lookupNode(path2);
333
- if (v === void 0) throw new ConfigError(`path not found: ${path2}`, path2);
334
- if (v.kind !== "object") throw new ConfigError(`expected object at ${path2}`, path2);
335
- return new _Config(v);
331
+ result.fields.set(k, rv);
336
332
  }
337
- getList(path2) {
338
- const v = this.lookupNode(path2);
339
- if (v === void 0) throw new ConfigError(`path not found: ${path2}`, path2);
340
- if (v.kind === "object") {
341
- const converted = numericObjectToArray(v);
342
- if (converted !== null) return converted.map(hoconToJs);
333
+ for (const [k, v] of receiver.priorValues) {
334
+ if (receiver.fields.has(k)) {
335
+ result.priorValues.set(k, v);
343
336
  }
344
- if (v.kind !== "array") throw new ConfigError(`expected array at ${path2}`, path2);
345
- return v.items.map(hoconToJs);
346
- }
347
- has(path2) {
348
- return this.lookupNode(path2) !== void 0;
349
- }
350
- keys() {
351
- return [...this.root.fields.keys()];
352
- }
353
- withFallback(fallback) {
354
- const merged = deepMergeHocon(this.root, fallback.root);
355
- return new _Config(merged);
356
337
  }
357
- toObject() {
358
- return hoconToJs(this.root);
359
- }
360
- lookupNode(path2) {
361
- const segments = splitConfigPath(path2);
362
- let current = this.root;
363
- for (const seg of segments) {
364
- if (current.kind !== "object") return void 0;
365
- const next = current.fields.get(seg);
366
- if (next === void 0) return void 0;
367
- current = next;
338
+ return result;
339
+ }
340
+
341
+ // src/internal/resolver/utils.ts
342
+ function segmentsToKey(segments) {
343
+ return segments.map((s) => {
344
+ const t = s.text;
345
+ if (t === "" || /[^a-zA-Z0-9\-_]/.test(t)) {
346
+ return `"${t.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
368
347
  }
369
- return current;
370
- }
371
- requireScalar(path2) {
372
- const v = this.lookupNode(path2);
373
- if (v === void 0) throw new ConfigError(`path not found: ${path2}`, path2);
374
- if (v.kind !== "scalar") throw new ConfigError(`expected scalar at ${path2}, got ${v.kind}`, path2);
375
- return v;
348
+ return t;
349
+ }).join(".");
350
+ }
351
+ function lookupPath(root, segments) {
352
+ const [head, ...tail] = segments;
353
+ if (head === void 0) return void 0;
354
+ const key = head.text;
355
+ const val = root.fields.get(key);
356
+ if (val === void 0) return void 0;
357
+ if (tail.length === 0) return val;
358
+ if (isResObj(val)) return lookupPath(val, tail);
359
+ return void 0;
360
+ }
361
+ function lookupResObj(root, segments) {
362
+ let cur = root;
363
+ for (const seg of segments) {
364
+ const val = cur.fields.get(seg.text);
365
+ if (val === void 0 || !isResObj(val)) return void 0;
366
+ cur = val;
376
367
  }
377
- };
378
- function splitConfigPath(path2) {
379
- const segments = [];
380
- let i = 0;
381
- while (i < path2.length) {
382
- if (path2[i] === '"') {
383
- i++;
384
- let segment = "";
385
- let closed = false;
386
- while (i < path2.length) {
387
- const ch = path2[i];
388
- if (ch === "\\" && i + 1 < path2.length) {
389
- const next = path2[i + 1];
390
- segment += next;
391
- i += 2;
392
- continue;
393
- }
394
- if (ch === '"') {
395
- closed = true;
396
- i++;
397
- break;
398
- }
399
- segment += ch;
400
- i++;
401
- }
402
- if (!closed) throw new ConfigError(`unterminated quoted path segment: ${path2}`, path2);
403
- segments.push(segment);
404
- if (i < path2.length && path2[i] === ".") i++;
368
+ return cur;
369
+ }
370
+ function deepMergeHoconValues(base, overlay) {
371
+ const merged = new Map(base.fields);
372
+ for (const [k, v] of overlay.fields) {
373
+ const existing = merged.get(k);
374
+ if (existing?.kind === "object" && v.kind === "object") {
375
+ merged.set(k, deepMergeHoconValues(existing, v));
405
376
  } else {
406
- const dot = path2.indexOf(".", i);
407
- if (dot === -1) {
408
- segments.push(path2.slice(i));
409
- break;
410
- }
411
- segments.push(path2.slice(i, dot));
412
- i = dot + 1;
377
+ merged.set(k, v);
413
378
  }
414
379
  }
415
- return segments;
380
+ return { kind: "object", fields: merged };
416
381
  }
417
- function scalarToJs(raw, valueType) {
418
- switch (valueType) {
419
- case "null":
420
- return null;
421
- case "boolean":
422
- return raw === "true";
423
- case "number":
424
- return Number(raw);
425
- case "string":
426
- return raw;
382
+ function deepMergeResObjInto(dst, src) {
383
+ for (const [k, srcVal] of src.fields) {
384
+ const dstVal = dst.fields.get(k);
385
+ if (dstVal !== void 0 && isResObj(dstVal) && isResObj(srcVal)) {
386
+ deepMergeResObjInto(dstVal, srcVal);
387
+ } else {
388
+ if (dstVal !== void 0) dst.priorValues.set(k, dstVal);
389
+ dst.fields.set(k, srcVal);
390
+ }
427
391
  }
428
- }
429
- function hoconToJs(v) {
430
- switch (v.kind) {
431
- case "scalar":
432
- return scalarToJs(v.raw, v.valueType);
433
- case "array":
434
- return v.items.map(hoconToJs);
435
- case "object": {
436
- const obj = {};
437
- for (const [k, val] of v.fields) obj[k] = hoconToJs(val);
438
- return obj;
392
+ for (const [k, srcPrior] of src.priorValues) {
393
+ if (!dst.priorValues.has(k)) {
394
+ dst.priorValues.set(k, srcPrior);
439
395
  }
440
396
  }
441
397
  }
442
- function deepMergeHocon(receiver, fallback) {
443
- const merged = new Map(fallback.fields);
444
- for (const [k, v] of receiver.fields) {
445
- const fb = merged.get(k);
446
- if (fb?.kind === "object" && v.kind === "object") {
447
- merged.set(k, deepMergeHocon(v, fb));
398
+ function hoconValueToResObj(hv) {
399
+ const obj = makeResObj();
400
+ if (hv.kind !== "object") return obj;
401
+ for (const [key, val] of hv.fields) {
402
+ if (val.kind === "object") {
403
+ obj.fields.set(key, hoconValueToResObj(val));
448
404
  } else {
449
- merged.set(k, v);
405
+ obj.fields.set(key, val);
450
406
  }
451
407
  }
452
- return { kind: "object", fields: merged };
408
+ return obj;
409
+ }
410
+ function isFileNotFoundError(e) {
411
+ if (!(e instanceof Error)) return false;
412
+ const code = e.code;
413
+ if (code === "ENOENT" || code === "MODULE_NOT_FOUND") return true;
414
+ const msg = e.message.toLowerCase();
415
+ return msg.includes("not found") || msg.includes("no such file") || msg.includes("enoent");
416
+ }
417
+
418
+ // src/internal/resolver/include-loader.ts
419
+ var import_node_module = require("module");
420
+ var nodePath = __toESM(require("path"), 1);
421
+
422
+ // src/internal/parser/empty-check.ts
423
+ function hasContentTokens(tokens) {
424
+ return tokens.some((t) => t.kind !== "eof" && t.kind !== "newline");
425
+ }
426
+ function assertNonEmptyDocument(tokens, sourceDescriptor) {
427
+ if (!hasContentTokens(tokens)) {
428
+ throw new ParseError(
429
+ `empty file is not a valid HOCON document (HOCON.md L130): ${sourceDescriptor}`,
430
+ 1,
431
+ 1
432
+ );
433
+ }
453
434
  }
454
435
 
455
436
  // src/internal/lexer/lexer.ts
@@ -1015,58 +996,137 @@ var Parser = class {
1015
996
  const p = this.currentPos();
1016
997
  this.skip("newline");
1017
998
  const t = this.peek();
1018
- let path2;
1019
- let required = false;
1020
- let isFile = false;
1021
999
  if (t.kind === "unquoted" && (t.value === "required(" || t.value === "required" || t.value.startsWith("required("))) {
1022
1000
  if (t.value === "required") {
1023
- const next2 = this.peek(1);
1024
- if (next2.kind !== "unquoted" || !next2.value.startsWith("(")) {
1001
+ const next = this.peek(1);
1002
+ if (next.kind !== "unquoted" || !next.value.startsWith("(")) {
1025
1003
  throw new ParseError("include required must be followed by (", t.line, t.col);
1026
1004
  }
1027
1005
  }
1028
- required = true;
1029
1006
  this.advance();
1030
1007
  const innerPrefix = t.value.startsWith("required(") ? t.value.slice("required(".length) : "";
1031
- if (innerPrefix.startsWith("url") || innerPrefix.startsWith("classpath")) {
1008
+ if (innerPrefix === "url" || innerPrefix.startsWith("url(") || innerPrefix === "classpath" || innerPrefix.startsWith("classpath(")) {
1032
1009
  throw new ParseError("include url(...) and classpath(...) are not supported", t.line, t.col);
1033
1010
  }
1034
- if (innerPrefix.startsWith("file")) {
1035
- isFile = true;
1011
+ const nextTok = this.peek();
1012
+ if (innerPrefix === "package" || innerPrefix.startsWith("package(") || innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "package(" || nextTok.value.startsWith("package("))) {
1013
+ const consumePackageToken = innerPrefix === "";
1014
+ const { qualifier, path: path3 } = this.parsePackageArgs(consumePackageToken);
1015
+ return this.makeIncludeField(qualifier, path3, true, p);
1036
1016
  }
1037
- const next = this.peek();
1038
- if (next.kind === "unquoted" && (next.value === "url" || next.value.startsWith("url(") || next.value === "classpath" || next.value.startsWith("classpath("))) {
1039
- throw new ParseError("include url(...) and classpath(...) are not supported", next.line, next.col);
1040
- }
1041
- if (next.kind === "unquoted" && (next.value === "file(" || next.value === "file" || next.value.startsWith("file("))) {
1042
- isFile = true;
1043
- }
1044
- while (this.peek().kind !== "string" && this.peek().kind !== "eof") this.advance();
1045
- if (this.peek().kind === "eof") throw new ParseError("expected include path", t.line, t.col);
1046
- path2 = this.advance().value;
1047
- while (this.peek().kind !== "newline" && this.peek().kind !== "rbrace" && this.peek().kind !== "eof" && this.peek().kind !== "comma") this.advance();
1048
- } else if (t.kind === "string") {
1049
- path2 = this.advance().value;
1050
- } else if (t.kind === "unquoted" && (t.value === "file(" || t.value === "file")) {
1051
- isFile = true;
1017
+ if (innerPrefix === "file" || innerPrefix.startsWith("file(") || innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "file(" || nextTok.value === "file")) {
1018
+ if (innerPrefix === "") {
1019
+ this.advance();
1020
+ }
1021
+ const path3 = this.parseQuotedPathSkipWrapper(t);
1022
+ return this.makeIncludeField({ kind: "file" }, path3, true, p);
1023
+ }
1024
+ if (innerPrefix === "" && nextTok.kind === "unquoted" && (nextTok.value === "url" || nextTok.value.startsWith("url(") || nextTok.value === "classpath" || nextTok.value.startsWith("classpath("))) {
1025
+ throw new ParseError("include url(...) and classpath(...) are not supported", nextTok.line, nextTok.col);
1026
+ }
1027
+ if (innerPrefix !== "") {
1028
+ if (innerPrefix === ")") {
1029
+ throw new ParseError("include required(...) must contain a path", t.line, t.col);
1030
+ }
1031
+ const name = innerPrefix.replace(/\(.*$/, "").replace(/\).*$/, "");
1032
+ throw new ParseError(`unknown include qualifier inside required(): "${name}"`, t.line, t.col);
1033
+ }
1034
+ const path2 = this.parseQuotedPathSkipWrapper(t);
1035
+ return this.makeIncludeField({ kind: "bare" }, path2, true, p);
1036
+ }
1037
+ if (t.kind === "string") {
1038
+ const path2 = this.advance().value;
1039
+ return this.makeIncludeField({ kind: "bare" }, path2, false, p);
1040
+ }
1041
+ if (t.kind === "unquoted" && (t.value === "file(" || t.value === "file")) {
1052
1042
  this.advance();
1053
- while (this.peek().kind !== "string" && this.peek().kind !== "eof") this.advance();
1054
- if (this.peek().kind === "eof") throw new ParseError("expected include path", t.line, t.col);
1055
- path2 = this.advance().value;
1056
- while (this.peek().kind !== "newline" && this.peek().kind !== "rbrace" && this.peek().kind !== "eof" && this.peek().kind !== "comma") this.advance();
1057
- } else if (t.kind === "unquoted" && (t.value === "url" || t.value.startsWith("url("))) {
1043
+ const path2 = this.parseQuotedPathSkipWrapper(t);
1044
+ return this.makeIncludeField({ kind: "file" }, path2, false, p);
1045
+ }
1046
+ if (t.kind === "unquoted" && (t.value === "package(" || t.value.startsWith("package("))) {
1047
+ const { qualifier, path: path2 } = this.parsePackageArgs();
1048
+ return this.makeIncludeField(qualifier, path2, false, p);
1049
+ }
1050
+ if (t.kind === "unquoted" && (t.value === "url" || t.value.startsWith("url("))) {
1058
1051
  throw new ParseError("include url(...) is not supported", t.line, t.col);
1059
- } else if (t.kind === "unquoted" && (t.value === "classpath" || t.value.startsWith("classpath("))) {
1052
+ }
1053
+ if (t.kind === "unquoted" && (t.value === "classpath" || t.value.startsWith("classpath("))) {
1060
1054
  throw new ParseError("include classpath(...) is not supported", t.line, t.col);
1061
- } else {
1062
- throw new ParseError(`expected include path, got ${t.kind}`, t.line, t.col);
1063
1055
  }
1064
- return {
1065
- key: [],
1066
- value: { kind: "include", path: path2, required, ...isFile ? { isFile: true } : {}, pos: p },
1067
- append: false,
1068
- pos: p
1069
- };
1056
+ throw new ParseError(`expected include path, got ${t.kind}`, t.line, t.col);
1057
+ }
1058
+ makeIncludeField(qualifier, path2, required, pos) {
1059
+ return { key: [], value: { kind: "include", qualifier, path: path2, required, pos }, append: false, pos };
1060
+ }
1061
+ /**
1062
+ * Skip past include-syntax wrapper tokens to find the quoted path string.
1063
+ * Strict: only allows `(`, `)`, and bare qualifier keywords (file / file( /
1064
+ * package / package( / url / url( / classpath / classpath() between the
1065
+ * directive and the quoted path. Anything else is a parse error rather than
1066
+ * silently swallowed (see ts#113).
1067
+ */
1068
+ parseQuotedPathSkipWrapper(errTok) {
1069
+ while (this.isIncludeWrapperToken(this.peek())) this.advance();
1070
+ if (this.peek().kind !== "string") {
1071
+ const tok = this.peek();
1072
+ if (tok.kind === "eof") throw new ParseError("expected include path", errTok.line, errTok.col);
1073
+ const desc = tok.kind === "unquoted" ? `unquoted "${tok.value}"` : tok.kind;
1074
+ throw new ParseError(`expected quoted include path, got ${desc}`, tok.line, tok.col);
1075
+ }
1076
+ const path2 = this.advance().value;
1077
+ while (this.peek().kind === "unquoted" && (this.peek().value === ")" || this.peek().value === "))")) {
1078
+ this.advance();
1079
+ }
1080
+ const after = this.peek();
1081
+ if (after.kind !== "newline" && after.kind !== "rbrace" && after.kind !== "eof" && after.kind !== "comma") {
1082
+ const desc = after.kind === "unquoted" ? `unquoted "${after.value}"` : after.kind;
1083
+ throw new ParseError(`unexpected token after include path: ${desc}`, after.line, after.col);
1084
+ }
1085
+ return path2;
1086
+ }
1087
+ isIncludeWrapperToken(tok) {
1088
+ return tok.kind === "unquoted" && tok.value === "(";
1089
+ }
1090
+ /**
1091
+ * Parse the body of `package("identifier", "file")`.
1092
+ *
1093
+ * @param consumePackageToken - When true (default), the current token is `package(` and
1094
+ * must be consumed first. When false, the lexer already bundled `package(` into the
1095
+ * preceding token (e.g. `required(package("`), so we are already positioned at the
1096
+ * first string argument.
1097
+ */
1098
+ parsePackageArgs(consumePackageToken = true) {
1099
+ const pkgTok = consumePackageToken ? this.advance() : this.peek();
1100
+ this.skip("newline");
1101
+ const idTok = this.peek();
1102
+ if (idTok.kind !== "string") {
1103
+ throw new ParseError("include package: expected quoted identifier as first argument", pkgTok.line, pkgTok.col);
1104
+ }
1105
+ const identifier = this.advance().value;
1106
+ this.skip("newline");
1107
+ if (this.peek().kind !== "comma") {
1108
+ const after = this.peek();
1109
+ throw new ParseError(
1110
+ 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",
1111
+ pkgTok.line,
1112
+ pkgTok.col
1113
+ );
1114
+ }
1115
+ this.advance();
1116
+ this.skip("newline");
1117
+ const fileTok = this.peek();
1118
+ if (fileTok.kind !== "string") {
1119
+ throw new ParseError(
1120
+ "include package: requires exactly two arguments (identifier, file) \u2014 one-arg form is not supported",
1121
+ pkgTok.line,
1122
+ pkgTok.col
1123
+ );
1124
+ }
1125
+ const filePath = this.advance().value;
1126
+ while (this.peek().kind !== "newline" && this.peek().kind !== "rbrace" && this.peek().kind !== "eof" && this.peek().kind !== "comma") {
1127
+ this.advance();
1128
+ }
1129
+ return { qualifier: { kind: "package", identifier }, path: filePath };
1070
1130
  }
1071
1131
  parseValue() {
1072
1132
  const p = this.currentPos();
@@ -1107,7 +1167,9 @@ var Parser = class {
1107
1167
  parts.push(node);
1108
1168
  }
1109
1169
  if (parts.length === 0) throw new ParseError("expected value", this.peek().line, this.peek().col);
1110
- if (parts.length === 1) return parts[0];
1170
+ if (parts.length === 1) {
1171
+ return parts[0];
1172
+ }
1111
1173
  return { kind: "concat", nodes: parts, pos: p };
1112
1174
  }
1113
1175
  parseArray() {
@@ -1139,118 +1201,6 @@ function parseTokens(tokens) {
1139
1201
  return new Parser(tokens).parse();
1140
1202
  }
1141
1203
 
1142
- // src/internal/parser/empty-check.ts
1143
- function hasContentTokens(tokens) {
1144
- return tokens.some((t) => t.kind !== "eof" && t.kind !== "newline");
1145
- }
1146
- function assertNonEmptyDocument(tokens, sourceDescriptor) {
1147
- if (!hasContentTokens(tokens)) {
1148
- throw new ParseError(
1149
- `empty file is not a valid HOCON document (HOCON.md L130): ${sourceDescriptor}`,
1150
- 1,
1151
- 1
1152
- );
1153
- }
1154
- }
1155
-
1156
- // src/internal/resolver/types.ts
1157
- var separatorValues = /* @__PURE__ */ new WeakSet();
1158
- function isSubst(v) {
1159
- return v._kind === "subst-placeholder";
1160
- }
1161
- function isConcat(v) {
1162
- return v._kind === "concat-placeholder";
1163
- }
1164
- function isAppend(v) {
1165
- return v._kind === "append-placeholder";
1166
- }
1167
- function isResObj(v) {
1168
- return v._kind === "res-obj";
1169
- }
1170
- function makeResObj() {
1171
- return { _kind: "res-obj", fields: /* @__PURE__ */ new Map(), priorValues: /* @__PURE__ */ new Map() };
1172
- }
1173
-
1174
- // src/internal/resolver/utils.ts
1175
- function segmentsToKey(segments) {
1176
- return segments.map((s) => {
1177
- const t = s.text;
1178
- if (t === "" || /[^a-zA-Z0-9\-_]/.test(t)) {
1179
- return `"${t.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
1180
- }
1181
- return t;
1182
- }).join(".");
1183
- }
1184
- function lookupPath(root, segments) {
1185
- const [head, ...tail] = segments;
1186
- if (head === void 0) return void 0;
1187
- const key = head.text;
1188
- const val = root.fields.get(key);
1189
- if (val === void 0) return void 0;
1190
- if (tail.length === 0) return val;
1191
- if (isResObj(val)) return lookupPath(val, tail);
1192
- return void 0;
1193
- }
1194
- function lookupResObj(root, segments) {
1195
- let cur = root;
1196
- for (const seg of segments) {
1197
- const val = cur.fields.get(seg.text);
1198
- if (val === void 0 || !isResObj(val)) return void 0;
1199
- cur = val;
1200
- }
1201
- return cur;
1202
- }
1203
- function deepMergeHoconValues(base, overlay) {
1204
- const merged = new Map(base.fields);
1205
- for (const [k, v] of overlay.fields) {
1206
- const existing = merged.get(k);
1207
- if (existing?.kind === "object" && v.kind === "object") {
1208
- merged.set(k, deepMergeHoconValues(existing, v));
1209
- } else {
1210
- merged.set(k, v);
1211
- }
1212
- }
1213
- return { kind: "object", fields: merged };
1214
- }
1215
- function deepMergeResObjInto(dst, src) {
1216
- for (const [k, srcVal] of src.fields) {
1217
- const dstVal = dst.fields.get(k);
1218
- if (dstVal !== void 0 && isResObj(dstVal) && isResObj(srcVal)) {
1219
- deepMergeResObjInto(dstVal, srcVal);
1220
- } else {
1221
- if (dstVal !== void 0) dst.priorValues.set(k, dstVal);
1222
- dst.fields.set(k, srcVal);
1223
- }
1224
- }
1225
- for (const [k, srcPrior] of src.priorValues) {
1226
- if (!dst.priorValues.has(k)) {
1227
- dst.priorValues.set(k, srcPrior);
1228
- }
1229
- }
1230
- }
1231
- function hoconValueToResObj(hv) {
1232
- const obj = makeResObj();
1233
- if (hv.kind !== "object") return obj;
1234
- for (const [key, val] of hv.fields) {
1235
- if (val.kind === "object") {
1236
- obj.fields.set(key, hoconValueToResObj(val));
1237
- } else {
1238
- obj.fields.set(key, val);
1239
- }
1240
- }
1241
- return obj;
1242
- }
1243
- function isFileNotFoundError(e) {
1244
- if (!(e instanceof Error)) return false;
1245
- const code = e.code;
1246
- if (code === "ENOENT" || code === "MODULE_NOT_FOUND") return true;
1247
- const msg = e.message.toLowerCase();
1248
- return msg.includes("not found") || msg.includes("no such file") || msg.includes("enoent");
1249
- }
1250
-
1251
- // src/internal/resolver/include-loader.ts
1252
- var nodePath = __toESM(require("path"), 1);
1253
-
1254
1204
  // src/internal/properties/properties.ts
1255
1205
  function parseProperties(input) {
1256
1206
  const root = /* @__PURE__ */ Object.create(null);
@@ -1310,6 +1260,85 @@ function recordToHoconValue(obj) {
1310
1260
  }
1311
1261
 
1312
1262
  // src/internal/resolver/include-loader.ts
1263
+ var import_meta = {};
1264
+ var _require = (0, import_node_module.createRequire)(import_meta.url);
1265
+ function isPnpPath(p) {
1266
+ return p.includes(".yarn/cache") || p.includes(".pnp.cjs") || p.includes(".pnp.loader");
1267
+ }
1268
+ function validatePackageFile(file, identifier) {
1269
+ if (file.length === 0) {
1270
+ throw new ParseError(`include package("${identifier}", ...): file argument must be non-empty`, 0, 0);
1271
+ }
1272
+ if (file.includes("\\")) {
1273
+ throw new ParseError(
1274
+ `include package("${identifier}", "${file}"): backslash not allowed in file argument (use forward slash)`,
1275
+ 0,
1276
+ 0
1277
+ );
1278
+ }
1279
+ if (file.startsWith("/")) {
1280
+ throw new ParseError(
1281
+ `include package("${identifier}", "${file}"): absolute path not allowed in file argument`,
1282
+ 0,
1283
+ 0
1284
+ );
1285
+ }
1286
+ if (/(?:^|\/)\.\.?(?:\/|$)/u.test(file)) {
1287
+ throw new ParseError(
1288
+ `include package("${identifier}", "${file}"): path traversal (. or ..) not allowed in file argument`,
1289
+ 0,
1290
+ 0
1291
+ );
1292
+ }
1293
+ if (file.includes("//")) {
1294
+ throw new ParseError(
1295
+ `include package("${identifier}", "${file}"): consecutive slashes not allowed in file argument`,
1296
+ 0,
1297
+ 0
1298
+ );
1299
+ }
1300
+ }
1301
+ function validatePackageIdentifier(identifier) {
1302
+ if (identifier.length === 0) {
1303
+ throw new ParseError("include package: identifier argument must be non-empty", 0, 0);
1304
+ }
1305
+ }
1306
+ function defaultPackageResolver(identifier, file, includingFile, baseDir, resolveFrom) {
1307
+ const from = resolveFrom ? Array.isArray(resolveFrom) ? resolveFrom : [resolveFrom] : baseDir ? [baseDir] : includingFile ? [nodePath.dirname(includingFile)] : [process.cwd()];
1308
+ let resolved;
1309
+ try {
1310
+ resolved = _require.resolve(`${identifier}/${file}`, { paths: from });
1311
+ } catch {
1312
+ throw new PackageLookupError(
1313
+ `include package("${identifier}", "${file}"): module not found (starting from: ${from.join(", ")})`,
1314
+ identifier,
1315
+ file,
1316
+ 0,
1317
+ 0
1318
+ );
1319
+ }
1320
+ if (isPnpPath(resolved)) {
1321
+ throw new ResolveError(
1322
+ `include package("${identifier}", "${file}"): Yarn Berry PnP store paths are not supported. Provide a custom packageResolver option to handle PnP resolution.`,
1323
+ `${identifier}/${file}`,
1324
+ 0,
1325
+ 0
1326
+ );
1327
+ }
1328
+ const fileParts = file.split("/");
1329
+ const expectedBasename = fileParts[fileParts.length - 1] ?? file;
1330
+ const resolvedBasename = nodePath.basename(resolved);
1331
+ if (resolvedBasename !== expectedBasename) {
1332
+ throw new PackageLookupError(
1333
+ `include package("${identifier}", "${file}"): case mismatch \u2014 resolved "${resolvedBasename}" but expected "${expectedBasename}" (E11 decision 5: case-sensitive)`,
1334
+ identifier,
1335
+ file,
1336
+ 0,
1337
+ 0
1338
+ );
1339
+ }
1340
+ return resolved;
1341
+ }
1313
1342
  var IncludeLoader = class {
1314
1343
  constructor(opts) {
1315
1344
  this.opts = opts;
@@ -1389,8 +1418,77 @@ var IncludeLoader = class {
1389
1418
  }
1390
1419
  return merged;
1391
1420
  }
1421
+ /**
1422
+ * Resolve and load `include package("identifier", "file")`.
1423
+ * Validates the identifier and file arguments, resolves via the package resolver,
1424
+ * performs cycle detection using a JSON-serialised cycle key, and parses the content.
1425
+ */
1426
+ // `required` is reserved for future optional-package semantics (E11 decision 7);
1427
+ // currently all package lookup failures throw regardless of this flag.
1428
+ loadPackage(identifier, file, _required) {
1429
+ validatePackageIdentifier(identifier);
1430
+ validatePackageFile(file, identifier);
1431
+ const { includeStack = [], baseDir, resolveFrom, packageResolver, readFileSync: readFileSync2 } = this.opts;
1432
+ const cycleKey = JSON.stringify(["package", identifier, file]);
1433
+ if (includeStack.includes(cycleKey)) {
1434
+ throw new ResolveError(
1435
+ `circular include: package("${identifier}", "${file}")`,
1436
+ `${identifier}/${file}`,
1437
+ 0,
1438
+ 0
1439
+ );
1440
+ }
1441
+ if (includeStack.length >= 50) {
1442
+ throw new ResolveError(`include depth limit exceeded (max 50)`, `${identifier}/${file}`, 0, 0);
1443
+ }
1444
+ const resolver = packageResolver ?? ((id, f, includingFile, bDir) => defaultPackageResolver(id, f, includingFile, bDir, resolveFrom));
1445
+ const resolvedPath = resolver(identifier, file, void 0, baseDir);
1446
+ const content = readFileSync2(resolvedPath);
1447
+ if (content.length === 0) return makeResObj();
1448
+ const tokens = tokenize(content);
1449
+ assertNonEmptyDocument(tokens, resolvedPath);
1450
+ const ast = parseTokens(tokens);
1451
+ return this.onBuildResObj(ast, {
1452
+ ...this.opts,
1453
+ baseDir: nodePath.dirname(resolvedPath),
1454
+ includeStack: [...includeStack, cycleKey]
1455
+ });
1456
+ }
1457
+ /**
1458
+ * Async variant of `loadPackage`.
1459
+ */
1460
+ async loadPackageAsync(identifier, file, _required) {
1461
+ validatePackageIdentifier(identifier);
1462
+ validatePackageFile(file, identifier);
1463
+ const { includeStack = [], baseDir, resolveFrom, packageResolver, readFile, readFileSync: readFileSync2 } = this.opts;
1464
+ const cycleKey = JSON.stringify(["package", identifier, file]);
1465
+ if (includeStack.includes(cycleKey)) {
1466
+ throw new ResolveError(
1467
+ `circular include: package("${identifier}", "${file}")`,
1468
+ `${identifier}/${file}`,
1469
+ 0,
1470
+ 0
1471
+ );
1472
+ }
1473
+ if (includeStack.length >= 50) {
1474
+ throw new ResolveError(`include depth limit exceeded (max 50)`, `${identifier}/${file}`, 0, 0);
1475
+ }
1476
+ const resolver = packageResolver ?? ((id, f, includingFile, bDir) => defaultPackageResolver(id, f, includingFile, bDir, resolveFrom));
1477
+ const resolvedPath = resolver(identifier, file, void 0, baseDir);
1478
+ const read = readFile ? async (p) => readFile(p) : async (p) => readFileSync2(p);
1479
+ const content = await read(resolvedPath);
1480
+ if (content.length === 0) return makeResObj();
1481
+ const tokens = tokenize(content);
1482
+ assertNonEmptyDocument(tokens, resolvedPath);
1483
+ const ast = parseTokens(tokens);
1484
+ return this.onBuildResObjAsync(ast, {
1485
+ ...this.opts,
1486
+ baseDir: nodePath.dirname(resolvedPath),
1487
+ includeStack: [...includeStack, cycleKey]
1488
+ });
1489
+ }
1392
1490
  loadSingle(candidate) {
1393
- const { readFileSync: readFileSync2, includeStack = [], env } = this.opts;
1491
+ const { readFileSync: readFileSync2, includeStack = [] } = this.opts;
1394
1492
  if (includeStack.includes(candidate)) {
1395
1493
  throw new ResolveError(`circular include: ${candidate}`, candidate, 0, 0);
1396
1494
  }
@@ -1408,14 +1506,13 @@ var IncludeLoader = class {
1408
1506
  assertNonEmptyDocument(tokens, candidate);
1409
1507
  const ast = parseTokens(tokens);
1410
1508
  return this.onBuildResObj(ast, {
1411
- env,
1509
+ ...this.opts,
1412
1510
  baseDir: nodePath.dirname(candidate),
1413
- readFileSync: readFileSync2,
1414
1511
  includeStack: [...includeStack, candidate]
1415
1512
  });
1416
1513
  }
1417
1514
  async loadSingleAsync(candidate) {
1418
- const { readFile, readFileSync: readFileSync2, includeStack = [], env } = this.opts;
1515
+ const { readFile, readFileSync: readFileSync2, includeStack = [] } = this.opts;
1419
1516
  const read = readFile ? async (p) => readFile(p) : async (p) => readFileSync2(p);
1420
1517
  if (includeStack.includes(candidate)) {
1421
1518
  throw new ResolveError(`circular include: ${candidate}`, candidate, 0, 0);
@@ -1434,10 +1531,8 @@ var IncludeLoader = class {
1434
1531
  assertNonEmptyDocument(tokens, candidate);
1435
1532
  const ast = parseTokens(tokens);
1436
1533
  return this.onBuildResObjAsync(ast, {
1437
- env,
1534
+ ...this.opts,
1438
1535
  baseDir: nodePath.dirname(candidate),
1439
- readFileSync: readFileSync2,
1440
- readFile,
1441
1536
  includeStack: [...includeStack, candidate]
1442
1537
  });
1443
1538
  }
@@ -1446,7 +1541,6 @@ var IncludeLoader = class {
1446
1541
  // src/internal/resolver/structure-builder.ts
1447
1542
  var StructureBuilder = class _StructureBuilder {
1448
1543
  constructor(opts) {
1449
- this.opts = opts;
1450
1544
  this.loader = new IncludeLoader(opts);
1451
1545
  this.loader.onBuildResObj = (a, o) => new _StructureBuilder(o).build(a);
1452
1546
  this.loader.onBuildResObjAsync = async (a, o) => new _StructureBuilder(o).buildAsync(a);
@@ -1473,7 +1567,13 @@ var StructureBuilder = class _StructureBuilder {
1473
1567
  }
1474
1568
  applyField(obj, field, pathPrefix) {
1475
1569
  if (field.key.length === 0 && field.value.kind === "include") {
1476
- const included = this.loader.load(field.value.path, field.value.required, field.value.isFile);
1570
+ const { qualifier, path: path2, required } = field.value;
1571
+ let included;
1572
+ if (qualifier.kind === "package") {
1573
+ included = this.loader.loadPackage(qualifier.identifier, path2, required);
1574
+ } else {
1575
+ included = this.loader.load(path2, required, qualifier.kind === "file");
1576
+ }
1477
1577
  if (pathPrefix.length > 0) {
1478
1578
  this.relativizeResObj(included, pathPrefix);
1479
1579
  }
@@ -1515,7 +1615,13 @@ var StructureBuilder = class _StructureBuilder {
1515
1615
  }
1516
1616
  async applyFieldAsync(obj, field, pathPrefix) {
1517
1617
  if (field.key.length === 0 && field.value.kind === "include") {
1518
- const included = await this.loader.loadAsync(field.value.path, field.value.required, field.value.isFile);
1618
+ const { qualifier, path: path2, required } = field.value;
1619
+ let included;
1620
+ if (qualifier.kind === "package") {
1621
+ included = await this.loader.loadPackageAsync(qualifier.identifier, path2, required);
1622
+ } else {
1623
+ included = await this.loader.loadAsync(path2, required, qualifier.kind === "file");
1624
+ }
1519
1625
  if (pathPrefix.length > 0) {
1520
1626
  this.relativizeResObj(included, pathPrefix);
1521
1627
  }
@@ -1645,6 +1751,23 @@ var StructureBuilder = class _StructureBuilder {
1645
1751
  }
1646
1752
  };
1647
1753
 
1754
+ // src/value/numeric-array.ts
1755
+ function numericObjectToArray(value) {
1756
+ if (value.kind !== "object") return null;
1757
+ if (value.fields.size === 0) return null;
1758
+ const canonicalInt = /^(0|[1-9][0-9]*)$/;
1759
+ const eligible = [];
1760
+ for (const [key, val] of value.fields) {
1761
+ if (!canonicalInt.test(key)) continue;
1762
+ const n = Number(key);
1763
+ if (!Number.isInteger(n) || n > 2147483647) continue;
1764
+ eligible.push({ n, v: val });
1765
+ }
1766
+ if (eligible.length === 0) return null;
1767
+ eligible.sort((a, b) => a.n - b.n);
1768
+ return eligible.map((e) => e.v);
1769
+ }
1770
+
1648
1771
  // src/internal/resolver/substitution-resolver.ts
1649
1772
  var SubstitutionResolver = class {
1650
1773
  constructor(root, opts) {
@@ -1666,6 +1789,10 @@ var SubstitutionResolver = class {
1666
1789
  // xx.hocon PR (see Phase 6 #3f close-out notes).
1667
1790
  this.resolvingConcats = /* @__PURE__ */ new WeakSet();
1668
1791
  }
1792
+ /** Returns a prefix for error messages, including origin info when set. */
1793
+ originPrefix() {
1794
+ return this.opts.originDescription ? `${this.opts.originDescription}: ` : "";
1795
+ }
1669
1796
  resolve() {
1670
1797
  return this.resolveResObj(this.root);
1671
1798
  }
@@ -1706,7 +1833,8 @@ var SubstitutionResolver = class {
1706
1833
  if (isConcat(v)) {
1707
1834
  this.resolvingConcats.add(v);
1708
1835
  try {
1709
- return this.resolveConcat(v.nodes, scope, v.line, v.col);
1836
+ const concatResult = this.resolveConcat(v.nodes, scope, v.line, v.col);
1837
+ return concatResult;
1710
1838
  } finally {
1711
1839
  this.resolvingConcats.delete(v);
1712
1840
  }
@@ -1751,7 +1879,7 @@ var SubstitutionResolver = class {
1751
1879
  }
1752
1880
  if (s.optional) return void 0;
1753
1881
  throw new ResolveError(
1754
- `circular substitution: ${key}`,
1882
+ this.originPrefix() + `circular substitution: ${key}`,
1755
1883
  key,
1756
1884
  s.line,
1757
1885
  s.col
@@ -1781,7 +1909,7 @@ var SubstitutionResolver = class {
1781
1909
  }
1782
1910
  if (s.optional) return void 0;
1783
1911
  throw new ResolveError(
1784
- `could not resolve substitution: \${${key}}`,
1912
+ `${this.originPrefix()}could not resolve substitution: \${${key}}`,
1785
1913
  key,
1786
1914
  s.line,
1787
1915
  s.col
@@ -1814,7 +1942,7 @@ var SubstitutionResolver = class {
1814
1942
  if (result !== void 0) this.cache.set(key, result);
1815
1943
  return result;
1816
1944
  }
1817
- if (s.listSuffix) {
1945
+ if (s.listSuffix && this.opts.useSystemEnvironment !== false) {
1818
1946
  const result = this.resolveEnvList(s);
1819
1947
  if (result !== void 0) {
1820
1948
  this.cache.set(key, result);
@@ -1823,22 +1951,36 @@ var SubstitutionResolver = class {
1823
1951
  if (s.optional) return void 0;
1824
1952
  const envBase = s.segments.map((seg) => seg.text).join(".");
1825
1953
  throw new ResolveError(
1826
- `could not resolve substitution: \${${key}} (no environment variables ${envBase}_0, ${envBase}_1, \u2026 set)`,
1954
+ `${this.originPrefix()}could not resolve substitution: \${${key}} (no environment variables ${envBase}_0, ${envBase}_1, \u2026 set)`,
1955
+ key,
1956
+ s.line,
1957
+ s.col
1958
+ );
1959
+ } else if (s.listSuffix) {
1960
+ if (s.optional) return void 0;
1961
+ if (this.opts.allowUnresolved) return s;
1962
+ throw new ResolveError(
1963
+ `${this.originPrefix()}could not resolve substitution: \${${key}}`,
1827
1964
  key,
1828
1965
  s.line,
1829
1966
  s.col
1830
1967
  );
1831
1968
  }
1832
- const envKey = s.segments.map((seg) => seg.text).join(".");
1833
- const envVal = this.opts.env[envKey] ?? (s.prefixLen > 0 ? this.opts.env[s.segments.slice(s.prefixLen).map((seg) => seg.text).join(".")] : void 0);
1834
- if (envVal !== void 0) {
1835
- const result = { kind: "scalar", raw: envVal, valueType: "string" };
1836
- this.cache.set(key, result);
1837
- return result;
1969
+ if (this.opts.useSystemEnvironment !== false) {
1970
+ const envKey = s.segments.map((seg) => seg.text).join(".");
1971
+ const envVal = this.opts.env[envKey] ?? (s.prefixLen > 0 ? this.opts.env[s.segments.slice(s.prefixLen).map((seg) => seg.text).join(".")] : void 0);
1972
+ if (envVal !== void 0) {
1973
+ const result = { kind: "scalar", raw: envVal, valueType: "string" };
1974
+ this.cache.set(key, result);
1975
+ return result;
1976
+ }
1838
1977
  }
1839
1978
  if (s.optional) return void 0;
1979
+ if (this.opts.allowUnresolved) {
1980
+ return s;
1981
+ }
1840
1982
  throw new ResolveError(
1841
- `could not resolve substitution: \${${key}}`,
1983
+ `${this.originPrefix()}could not resolve substitution: \${${key}}`,
1842
1984
  key,
1843
1985
  s.line,
1844
1986
  s.col
@@ -1883,8 +2025,16 @@ var SubstitutionResolver = class {
1883
2025
  return [full, bare];
1884
2026
  }
1885
2027
  resolveConcat(nodes, scope, line = 0, col = 0) {
1886
- const resolved = nodes.map((n) => this.resolveVal(n, scope)).filter((v) => v !== void 0);
1887
- if (resolved.length === 0) return { kind: "scalar", raw: "null", valueType: "null" };
2028
+ const rawResolved = nodes.map((n) => this.resolveVal(n, scope));
2029
+ if (this.opts.allowUnresolved) {
2030
+ for (const v of rawResolved) {
2031
+ if (v !== void 0 && isSubst(v)) {
2032
+ return v;
2033
+ }
2034
+ }
2035
+ }
2036
+ const resolved = rawResolved.filter((v) => v !== void 0);
2037
+ if (resolved.length === 0) return void 0;
1888
2038
  if (resolved.length === 1) return resolved[0];
1889
2039
  const nonSep = resolved.filter((v) => !separatorValues.has(v));
1890
2040
  if (nonSep.length === 0) {
@@ -1958,27 +2108,512 @@ var SubstitutionResolver = class {
1958
2108
  };
1959
2109
 
1960
2110
  // src/internal/resolver/resolver.ts
1961
- function resolve2(ast, opts) {
1962
- const root = new StructureBuilder(opts).build(ast);
1963
- return new SubstitutionResolver(root, opts).resolve();
2111
+ function buildTree(ast, opts) {
2112
+ return new StructureBuilder(opts).build(ast);
1964
2113
  }
1965
- async function resolveAsync(ast, opts) {
1966
- const root = await new StructureBuilder(opts).buildAsync(ast);
1967
- return new SubstitutionResolver(root, opts).resolve();
2114
+ function resolveTree(tree, opts) {
2115
+ return new SubstitutionResolver(tree, opts).resolve();
1968
2116
  }
1969
-
1970
- // src/parse.ts
1971
- var fs = __toESM(require("fs"), 1);
1972
- var path = __toESM(require("path"), 1);
1973
- function getEnv(opts) {
1974
- if (opts.env !== void 0) return opts.env;
1975
- if (typeof process !== "undefined" && process.env) return process.env;
1976
- return {};
2117
+ function containsPlaceholders(tree) {
2118
+ for (const val of tree.fields.values()) {
2119
+ if (valContainsPlaceholders(val)) return true;
2120
+ }
2121
+ return false;
1977
2122
  }
1978
- function defaultReadFileSync(filePath) {
1979
- return fs.readFileSync(filePath, "utf-8");
2123
+ function valContainsPlaceholders(v) {
2124
+ const rv = v;
2125
+ if (isSubst(rv) || isConcat(rv) || isAppend(rv)) return true;
2126
+ if (isResObj(rv)) return containsPlaceholders(rv);
2127
+ const hv = v;
2128
+ if (hv?.kind === "array") {
2129
+ return hv.items.some((item) => valContainsPlaceholders(item));
2130
+ }
2131
+ return false;
1980
2132
  }
1981
- async function defaultReadFile(filePath) {
2133
+ function buildPartialHoconFromResObj(tree) {
2134
+ const fields = /* @__PURE__ */ new Map();
2135
+ for (const [k, v] of tree.fields) {
2136
+ if (!isSubst(v) && !isConcat(v) && !isAppend(v)) {
2137
+ if (isResObj(v)) {
2138
+ fields.set(k, buildPartialHoconFromResObj(v));
2139
+ } else {
2140
+ if (!valContainsPlaceholders(v)) {
2141
+ fields.set(k, v);
2142
+ }
2143
+ }
2144
+ }
2145
+ }
2146
+ return { kind: "object", fields };
2147
+ }
2148
+ function hoconValueToResObj2(value) {
2149
+ const obj = makeResObj();
2150
+ for (const [k, v] of value.fields) {
2151
+ if (v.kind === "object") {
2152
+ obj.fields.set(k, hoconValueToResObj2(v));
2153
+ } else {
2154
+ obj.fields.set(k, v);
2155
+ }
2156
+ }
2157
+ return obj;
2158
+ }
2159
+ function resolve2(ast, opts) {
2160
+ const tree = buildTree(ast, opts);
2161
+ return resolveTree(tree, { ...opts, useSystemEnvironment: true, allowUnresolved: false });
2162
+ }
2163
+ async function resolveAsync(ast, opts) {
2164
+ const root = await new StructureBuilder(opts).buildAsync(ast);
2165
+ return resolveTree(root, { ...opts, useSystemEnvironment: true, allowUnresolved: false });
2166
+ }
2167
+
2168
+ // src/config.ts
2169
+ var Config = class _Config {
2170
+ constructor(root, opts) {
2171
+ this.root = root;
2172
+ this._resolved = opts?.resolved ?? true;
2173
+ this._parseBaseDir = opts?.parseBaseDir;
2174
+ this._originDescription = opts?.originDescription;
2175
+ this._resObjRoot = opts?.resObjRoot;
2176
+ this._resolveOpts = opts?.resolveOpts;
2177
+ }
2178
+ /** Returns true when the Config's value tree contains no unresolved
2179
+ * substitution placeholders. Whole-config granularity (E12 decision 11). */
2180
+ isResolved() {
2181
+ return this._resolved;
2182
+ }
2183
+ /** @internal Test-only: construct a resolved Config from a HoconValue. */
2184
+ static _fromResolvedValue(root, opts) {
2185
+ return new _Config(root, { resolved: true, originDescription: opts?.originDescription });
2186
+ }
2187
+ /** @internal Construct an unresolved Config carrying a ResObj tree.
2188
+ * Used by parseStringWithOptions (deferred path) in T6. */
2189
+ static _fromUnresolvedResObj(tree, opts) {
2190
+ const partialRoot = buildPartialHoconFromResObj(tree);
2191
+ return new _Config(partialRoot, {
2192
+ resolved: opts.resolved,
2193
+ parseBaseDir: opts.parseBaseDir,
2194
+ originDescription: opts.originDescription,
2195
+ resObjRoot: tree,
2196
+ resolveOpts: opts.resolveOpts
2197
+ });
2198
+ }
2199
+ get(path2) {
2200
+ const v = this.lookupNode(path2);
2201
+ if (v === void 0) return void 0;
2202
+ return hoconToJs(v);
2203
+ }
2204
+ getString(path2) {
2205
+ const v = this.requireScalar(path2);
2206
+ return v.raw;
2207
+ }
2208
+ getNumber(path2) {
2209
+ const v = this.requireScalar(path2);
2210
+ const coerced = coerceNumber(v.raw);
2211
+ if (coerced !== void 0) return coerced;
2212
+ throw new ConfigError(`expected number at ${path2}, got ${v.valueType}`, path2);
2213
+ }
2214
+ getBoolean(path2) {
2215
+ const v = this.requireScalar(path2);
2216
+ const coerced = coerceBoolean(v.raw);
2217
+ if (coerced !== void 0) return coerced;
2218
+ throw new ConfigError(`expected boolean at ${path2}, got ${v.valueType}`, path2);
2219
+ }
2220
+ getDuration(path2, unit) {
2221
+ const v = this.requireScalar(path2);
2222
+ if (v.valueType !== "string" && v.valueType !== "number") {
2223
+ throw new ConfigError(`expected duration at ${path2}, got ${v.valueType}`, path2);
2224
+ }
2225
+ const result = parseDuration(v.raw, unit);
2226
+ if (Number.isNaN(result)) throw new ConfigError(`invalid duration at ${path2}: ${JSON.stringify(v.raw)}`, path2);
2227
+ return result;
2228
+ }
2229
+ getBytes(path2, unit) {
2230
+ const v = this.requireScalar(path2);
2231
+ if (v.valueType !== "string" && v.valueType !== "number") {
2232
+ throw new ConfigError(`expected byte size at ${path2}, got ${v.valueType}`, path2);
2233
+ }
2234
+ const result = parseBytes(v.raw, unit);
2235
+ if (Number.isNaN(result)) throw new ConfigError(`invalid byte size at ${path2}: ${JSON.stringify(v.raw)}`, path2);
2236
+ if (result < 0) throw new ConfigError(`byte size must be non-negative at ${path2}: ${JSON.stringify(v.raw)}`, path2);
2237
+ return result;
2238
+ }
2239
+ getConfig(path2) {
2240
+ const v = this.lookupNode(path2);
2241
+ if (v === void 0) {
2242
+ if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
2243
+ throw new NotResolvedError(path2);
2244
+ }
2245
+ throw new ConfigError(`path not found: ${path2}`, path2);
2246
+ }
2247
+ if (v.kind !== "object") throw new ConfigError(`expected object at ${path2}`, path2);
2248
+ if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
2249
+ throw new NotResolvedError(path2);
2250
+ }
2251
+ return new _Config(v, { resolved: this._resolved });
2252
+ }
2253
+ getList(path2) {
2254
+ const v = this.lookupNode(path2);
2255
+ if (v === void 0) {
2256
+ if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
2257
+ throw new NotResolvedError(path2);
2258
+ }
2259
+ throw new ConfigError(`path not found: ${path2}`, path2);
2260
+ }
2261
+ if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
2262
+ throw new NotResolvedError(path2);
2263
+ }
2264
+ if (v.kind === "object") {
2265
+ const converted = numericObjectToArray(v);
2266
+ if (converted !== null) return converted.map(hoconToJs);
2267
+ }
2268
+ if (v.kind !== "array") throw new ConfigError(`expected array at ${path2}`, path2);
2269
+ return v.items.map(hoconToJs);
2270
+ }
2271
+ has(path2) {
2272
+ return this.lookupNode(path2) !== void 0;
2273
+ }
2274
+ keys() {
2275
+ return [...this.root.fields.keys()];
2276
+ }
2277
+ withFallback(fallback) {
2278
+ if (fallback == null) return this;
2279
+ const selfTree = this._resObjRoot ?? hoconValueToResObj2(this.root);
2280
+ const fbTree = fallback._resObjRoot ?? hoconValueToResObj2(fallback.root);
2281
+ const merged = mergeUnresolved(selfTree, fbTree);
2282
+ const hasPlaceholders = containsPlaceholders(merged);
2283
+ const partialRoot = buildPartialHoconFromResObj(merged);
2284
+ return new _Config(partialRoot, {
2285
+ resolved: !hasPlaceholders,
2286
+ parseBaseDir: this._parseBaseDir,
2287
+ originDescription: this._originDescription,
2288
+ resObjRoot: merged,
2289
+ resolveOpts: this._resolveOpts ?? fallback._resolveOpts
2290
+ });
2291
+ }
2292
+ /**
2293
+ * Runs substitution resolution (phase 2) on the stored unresolved tree.
2294
+ * Idempotent: calling resolve() on an already-resolved Config returns an
2295
+ * equivalent resolved Config.
2296
+ *
2297
+ * opts.allowUnresolved (default false) — when true, leaves unresolved
2298
+ * non-optional placeholders in place rather than throwing.
2299
+ * opts.useSystemEnvironment (default true) — when false, env var lookups
2300
+ * are suppressed (hermetic resolution).
2301
+ *
2302
+ * E12 decision 3.
2303
+ */
2304
+ resolve(opts = {}) {
2305
+ if (this._resolved) {
2306
+ return new _Config(this.root, {
2307
+ resolved: true,
2308
+ parseBaseDir: this._parseBaseDir,
2309
+ originDescription: this._originDescription
2310
+ });
2311
+ }
2312
+ const tree = this._resObjRoot;
2313
+ if (!tree) {
2314
+ return new _Config(this.root, { resolved: true });
2315
+ }
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(tree, resolveOpts);
2325
+ if (resolved.kind !== "object") throw new Error("resolve: expected object root");
2326
+ if (opts.allowUnresolved) {
2327
+ const { stripped, hadPlaceholders } = stripPlaceholderFields(resolved);
2328
+ return new _Config(stripped, {
2329
+ resolved: !hadPlaceholders,
2330
+ parseBaseDir: this._parseBaseDir,
2331
+ originDescription: this._originDescription,
2332
+ resObjRoot: hadPlaceholders ? tree : void 0,
2333
+ resolveOpts: hadPlaceholders ? resolveOpts : void 0
2334
+ });
2335
+ }
2336
+ return new _Config(resolved, {
2337
+ resolved: true,
2338
+ parseBaseDir: this._parseBaseDir,
2339
+ originDescription: this._originDescription
2340
+ });
2341
+ }
2342
+ /**
2343
+ * Resolves receiver substitutions using source as lookup context.
2344
+ * Source's keys are NOT merged into the result — differs from
2345
+ * `this.withFallback(source).resolve()` which includes source keys.
2346
+ *
2347
+ * Precondition (E12 decision 10): source must be resolved. If
2348
+ * source.isResolved() is false, throws NotResolvedError.
2349
+ *
2350
+ * On an already-resolved receiver, resolveWith is a no-op (returns an
2351
+ * equivalent resolved Config, source keys not included).
2352
+ *
2353
+ * E12 decisions 9, 10.
2354
+ */
2355
+ resolveWith(source, opts = {}) {
2356
+ if (!source.isResolved()) {
2357
+ throw new NotResolvedError("source");
2358
+ }
2359
+ if (this._resolved) {
2360
+ return new _Config(this.root, {
2361
+ resolved: true,
2362
+ parseBaseDir: this._parseBaseDir,
2363
+ originDescription: this._originDescription
2364
+ });
2365
+ }
2366
+ const receiverTree = this._resObjRoot ?? hoconValueToResObj2(this.root);
2367
+ const srcTree = hoconValueToResObj2(source.root);
2368
+ const merged = mergeUnresolved(receiverTree, srcTree);
2369
+ const resolveOpts = {
2370
+ ...this._resolveOpts ?? { env: {}, baseDir: void 0, readFileSync: () => {
2371
+ throw new Error("no files");
2372
+ } },
2373
+ allowUnresolved: opts.allowUnresolved ?? false,
2374
+ useSystemEnvironment: opts.useSystemEnvironment ?? true,
2375
+ originDescription: this._originDescription
2376
+ };
2377
+ const resolved = resolveTree(merged, resolveOpts);
2378
+ if (resolved.kind !== "object") throw new Error("resolveWith: expected object root");
2379
+ const receiverShapeForFilter = resObjToKeyShape(receiverTree);
2380
+ const filtered = filterByReceiverShape(resolved, receiverShapeForFilter);
2381
+ if (opts.allowUnresolved) {
2382
+ const { stripped, hadPlaceholders } = stripPlaceholderFields(filtered);
2383
+ return new _Config(stripped, {
2384
+ resolved: !hadPlaceholders,
2385
+ parseBaseDir: this._parseBaseDir,
2386
+ originDescription: this._originDescription,
2387
+ resObjRoot: hadPlaceholders ? receiverTree : void 0,
2388
+ resolveOpts: hadPlaceholders ? resolveOpts : void 0
2389
+ });
2390
+ }
2391
+ return new _Config(filtered, {
2392
+ resolved: true,
2393
+ parseBaseDir: this._parseBaseDir,
2394
+ originDescription: this._originDescription
2395
+ });
2396
+ }
2397
+ toObject() {
2398
+ return hoconToJs(this.root);
2399
+ }
2400
+ /**
2401
+ * @internal Test-only: renders this resolved Config's value tree as canonical
2402
+ * JSON (sorted keys, no whitespace). Used by Layer-2 fixture tests to compare
2403
+ * against Lightbend ground truth. NOT part of the public API.
2404
+ *
2405
+ * Throws if the Config is unresolved (contains placeholders).
2406
+ */
2407
+ _renderJSONForTest() {
2408
+ return renderHoconAsJSON(this.root);
2409
+ }
2410
+ lookupNode(path2) {
2411
+ const segments = splitConfigPath(path2);
2412
+ let current = this.root;
2413
+ for (const seg of segments) {
2414
+ if (current.kind !== "object") return void 0;
2415
+ const next = current.fields.get(seg);
2416
+ if (next === void 0) return void 0;
2417
+ current = next;
2418
+ }
2419
+ return current;
2420
+ }
2421
+ /**
2422
+ * _resObjRootSubtreeHasPlaceholders — walk _resObjRoot to the given path and
2423
+ * return true if the subtree there contains any unresolved placeholder.
2424
+ * Used by getConfig / getList (T2) to detect transitive unresolved placeholders
2425
+ * in object / array subtrees that would otherwise appear to be "resolved" in the
2426
+ * partial HoconValue root (because individual leaf fields are omitted rather than
2427
+ * the whole subtree when their value is a direct placeholder scalar, but object
2428
+ * sub-containers are retained in the partial root with the placeholder-keyed
2429
+ * fields omitted from them — so the object appears "present but empty-ish").
2430
+ */
2431
+ _resObjRootSubtreeHasPlaceholders(path2) {
2432
+ const tree = this._resObjRoot;
2433
+ if (!tree) return false;
2434
+ const segments = splitConfigPath(path2);
2435
+ let cur = tree;
2436
+ for (const seg of segments) {
2437
+ const val = cur.fields.get(seg);
2438
+ if (val === void 0) return false;
2439
+ if (isResObj(val)) {
2440
+ cur = val;
2441
+ } else {
2442
+ return valContainsPlaceholders(val);
2443
+ }
2444
+ }
2445
+ return containsPlaceholders(cur);
2446
+ }
2447
+ requireScalar(path2) {
2448
+ const v = this.lookupNode(path2);
2449
+ if (v === void 0) {
2450
+ if (!this._resolved && this._resObjRootSubtreeHasPlaceholders(path2)) {
2451
+ throw new NotResolvedError(path2);
2452
+ }
2453
+ throw new ConfigError(`path not found: ${path2}`, path2);
2454
+ }
2455
+ if (v.kind !== "scalar") throw new ConfigError(`expected scalar at ${path2}, got ${v.kind}`, path2);
2456
+ return v;
2457
+ }
2458
+ };
2459
+ function splitConfigPath(path2) {
2460
+ const segments = [];
2461
+ let i = 0;
2462
+ while (i < path2.length) {
2463
+ if (path2[i] === '"') {
2464
+ i++;
2465
+ let segment = "";
2466
+ let closed = false;
2467
+ while (i < path2.length) {
2468
+ const ch = path2[i];
2469
+ if (ch === "\\" && i + 1 < path2.length) {
2470
+ const next = path2[i + 1];
2471
+ segment += next;
2472
+ i += 2;
2473
+ continue;
2474
+ }
2475
+ if (ch === '"') {
2476
+ closed = true;
2477
+ i++;
2478
+ break;
2479
+ }
2480
+ segment += ch;
2481
+ i++;
2482
+ }
2483
+ if (!closed) throw new ConfigError(`unterminated quoted path segment: ${path2}`, path2);
2484
+ segments.push(segment);
2485
+ if (i < path2.length && path2[i] === ".") i++;
2486
+ } else {
2487
+ const dot = path2.indexOf(".", i);
2488
+ if (dot === -1) {
2489
+ segments.push(path2.slice(i));
2490
+ break;
2491
+ }
2492
+ segments.push(path2.slice(i, dot));
2493
+ i = dot + 1;
2494
+ }
2495
+ }
2496
+ return segments;
2497
+ }
2498
+ function scalarToJs(raw, valueType) {
2499
+ switch (valueType) {
2500
+ case "null":
2501
+ return null;
2502
+ case "boolean":
2503
+ return raw === "true";
2504
+ case "number":
2505
+ return Number(raw);
2506
+ case "string":
2507
+ return raw;
2508
+ }
2509
+ }
2510
+ function hoconToJs(v) {
2511
+ switch (v.kind) {
2512
+ case "scalar":
2513
+ return scalarToJs(v.raw, v.valueType);
2514
+ case "array":
2515
+ return v.items.map(hoconToJs);
2516
+ case "object": {
2517
+ const obj = /* @__PURE__ */ Object.create(null);
2518
+ for (const [k, val] of v.fields) obj[k] = hoconToJs(val);
2519
+ return Object.assign({}, obj);
2520
+ }
2521
+ }
2522
+ }
2523
+ function resObjToKeyShape(tree) {
2524
+ const fields = /* @__PURE__ */ new Map();
2525
+ for (const [k, v] of tree.fields) {
2526
+ if (isSubst(v) || isConcat(v) || isAppend(v)) {
2527
+ fields.set(k, { kind: "scalar", raw: "", valueType: "null" });
2528
+ } else if (isResObj(v)) {
2529
+ fields.set(k, resObjToKeyShape(v));
2530
+ } else {
2531
+ fields.set(k, v);
2532
+ }
2533
+ }
2534
+ return { kind: "object", fields };
2535
+ }
2536
+ function stripPlaceholderFields(v) {
2537
+ const fields = /* @__PURE__ */ new Map();
2538
+ let hadPlaceholders = false;
2539
+ for (const [k, val] of v.fields) {
2540
+ if (val._kind === "subst-placeholder" || val._kind === "concat-placeholder" || val._kind === "append-placeholder") {
2541
+ hadPlaceholders = true;
2542
+ } else if (val.kind === "object") {
2543
+ const inner = stripPlaceholderFields(val);
2544
+ if (inner.hadPlaceholders) hadPlaceholders = true;
2545
+ fields.set(k, inner.stripped);
2546
+ } else {
2547
+ fields.set(k, val);
2548
+ }
2549
+ }
2550
+ return { stripped: { kind: "object", fields }, hadPlaceholders };
2551
+ }
2552
+ function filterByReceiverShape(resolved, receiverShape) {
2553
+ const fields = /* @__PURE__ */ new Map();
2554
+ for (const [k, rv] of resolved.fields) {
2555
+ if (!receiverShape.fields.has(k)) continue;
2556
+ const receiverVal = receiverShape.fields.get(k);
2557
+ if (rv.kind === "object" && receiverVal.kind === "object") {
2558
+ fields.set(k, filterByReceiverShape(rv, receiverVal));
2559
+ } else {
2560
+ fields.set(k, rv);
2561
+ }
2562
+ }
2563
+ return { kind: "object", fields };
2564
+ }
2565
+ function renderHoconAsJSON(v) {
2566
+ switch (v.kind) {
2567
+ case "scalar": {
2568
+ switch (v.valueType) {
2569
+ case "null":
2570
+ return "null";
2571
+ case "boolean":
2572
+ return v.raw;
2573
+ case "number": {
2574
+ const n = Number(v.raw);
2575
+ if (Number.isFinite(n)) return v.raw;
2576
+ return JSON.stringify(v.raw);
2577
+ }
2578
+ case "string":
2579
+ return JSON.stringify(v.raw);
2580
+ }
2581
+ break;
2582
+ }
2583
+ case "array": {
2584
+ const items = v.items.map(renderHoconAsJSON);
2585
+ return `[${items.join(",")}]`;
2586
+ }
2587
+ case "object": {
2588
+ const keys = [...v.fields.keys()].sort();
2589
+ const pairs = keys.map((k) => {
2590
+ const val = v.fields.get(k);
2591
+ return `${JSON.stringify(k)}:${renderHoconAsJSON(val)}`;
2592
+ });
2593
+ return `{${pairs.join(",")}}`;
2594
+ }
2595
+ }
2596
+ throw new Error(`renderHoconAsJSON: unsupported value kind (unresolved placeholder?)`);
2597
+ }
2598
+
2599
+ // src/parse.ts
2600
+ var fs = __toESM(require("fs"), 1);
2601
+ var path = __toESM(require("path"), 1);
2602
+ function defaultParseOptions() {
2603
+ return { resolveSubstitutions: true };
2604
+ }
2605
+ function defaultResolveOptions() {
2606
+ return { useSystemEnvironment: true, allowUnresolved: false };
2607
+ }
2608
+ function getEnv(opts) {
2609
+ if (opts.env !== void 0) return opts.env;
2610
+ if (typeof process !== "undefined" && process.env) return process.env;
2611
+ return {};
2612
+ }
2613
+ function defaultReadFileSync(filePath) {
2614
+ return fs.readFileSync(filePath, "utf-8");
2615
+ }
2616
+ async function defaultReadFile(filePath) {
1982
2617
  return fs.promises.readFile(filePath, "utf-8");
1983
2618
  }
1984
2619
  function buildResolveContext(input, opts) {
@@ -1989,7 +2624,10 @@ function buildResolveContext(input, opts) {
1989
2624
  env: getEnv(opts),
1990
2625
  baseDir: opts.baseDir,
1991
2626
  readFileSync: opts.readFileSync ?? defaultReadFileSync,
1992
- readFile: opts.readFile
2627
+ readFile: opts.readFile,
2628
+ originDescription: opts.originDescription,
2629
+ resolveFrom: opts.resolveFrom,
2630
+ packageResolver: opts.packageResolver
1993
2631
  };
1994
2632
  return { ast, resolveOpts };
1995
2633
  }
@@ -1997,7 +2635,38 @@ function parse(input, opts = {}) {
1997
2635
  const { ast, resolveOpts } = buildResolveContext(input, opts);
1998
2636
  const value = resolve2(ast, resolveOpts);
1999
2637
  if (value.kind !== "object") throw new Error("resolved value is not an object");
2000
- return new Config(value);
2638
+ return new Config(value, { resolved: true, originDescription: opts.originDescription });
2639
+ }
2640
+ var parseString = parse;
2641
+ function parseStringWithOptions(input, opts = {}) {
2642
+ const resolveSubstitutions = opts.resolveSubstitutions ?? true;
2643
+ const { ast, resolveOpts } = buildResolveContext(input, opts);
2644
+ if (resolveSubstitutions) {
2645
+ const value = resolve2(ast, resolveOpts);
2646
+ if (value.kind !== "object") throw new Error("resolved value is not an object");
2647
+ return new Config(value, { resolved: true, originDescription: opts.originDescription });
2648
+ }
2649
+ const tree = buildTree(ast, resolveOpts);
2650
+ const hasPlaceholders = containsPlaceholders(tree);
2651
+ return Config._fromUnresolvedResObj(tree, {
2652
+ parseBaseDir: opts.baseDir,
2653
+ originDescription: opts.originDescription,
2654
+ resolved: !hasPlaceholders,
2655
+ resolveOpts
2656
+ });
2657
+ }
2658
+ function parseFileWithOptions(filePath, opts = {}) {
2659
+ if (typeof process === "undefined" && !opts.readFileSync) {
2660
+ throw new Error("parseFileWithOptions is not supported in browser environments without opts.readFileSync.");
2661
+ }
2662
+ const resolvedPath = path.resolve(filePath);
2663
+ const readFileSync2 = opts.readFileSync ?? defaultReadFileSync;
2664
+ const input = readFileSync2(resolvedPath);
2665
+ return parseStringWithOptions(input, {
2666
+ ...opts,
2667
+ baseDir: opts.baseDir ?? path.dirname(resolvedPath),
2668
+ readFileSync: readFileSync2
2669
+ });
2001
2670
  }
2002
2671
  async function parseAsync(input, opts = {}) {
2003
2672
  const { ast, resolveOpts } = buildResolveContext(input, opts);
@@ -2032,14 +2701,90 @@ async function parseFileAsync(filePath, opts = {}) {
2032
2701
  readFileSync: opts.readFileSync ?? defaultReadFileSync
2033
2702
  });
2034
2703
  }
2704
+
2705
+ // src/value-factory.ts
2706
+ function fromMap(values, originDescription) {
2707
+ const fields = coerceObject(values, "");
2708
+ const root = { kind: "object", fields };
2709
+ return Config._fromResolvedValue(root, { originDescription });
2710
+ }
2711
+ function empty(originDescription) {
2712
+ const root = { kind: "object", fields: /* @__PURE__ */ new Map() };
2713
+ return Config._fromResolvedValue(root, { originDescription });
2714
+ }
2715
+ function coerceObject(obj, atPath) {
2716
+ const fields = /* @__PURE__ */ new Map();
2717
+ for (const [k, v] of Object.entries(obj)) {
2718
+ const childPath = atPath ? `${atPath}.${k}` : k;
2719
+ fields.set(k, coerceValue(v, childPath));
2720
+ }
2721
+ return fields;
2722
+ }
2723
+ function coerceValue(v, atPath) {
2724
+ if (v === null) {
2725
+ return { kind: "scalar", raw: "null", valueType: "null" };
2726
+ }
2727
+ if (v === void 0) {
2728
+ throw new ConfigError(
2729
+ `fromMap: undefined is not a valid HOCON value at path "${atPath}"`,
2730
+ atPath
2731
+ );
2732
+ }
2733
+ switch (typeof v) {
2734
+ case "string":
2735
+ return { kind: "scalar", raw: v, valueType: "string" };
2736
+ case "number": {
2737
+ if (Number.isNaN(v) || !Number.isFinite(v)) {
2738
+ throw new ConfigError(
2739
+ `fromMap: ${v} is not representable in HOCON (NaN/Infinity not allowed) at path "${atPath}"`,
2740
+ atPath
2741
+ );
2742
+ }
2743
+ return { kind: "scalar", raw: String(v), valueType: "number" };
2744
+ }
2745
+ case "boolean":
2746
+ return { kind: "scalar", raw: v ? "true" : "false", valueType: "boolean" };
2747
+ case "bigint": {
2748
+ if (v > BigInt(Number.MAX_SAFE_INTEGER) || v < BigInt(Number.MIN_SAFE_INTEGER)) {
2749
+ throw new ConfigError(
2750
+ `fromMap: bigint ${v} exceeds Number.MAX_SAFE_INTEGER (not representable as HOCON number) at path "${atPath}"`,
2751
+ atPath
2752
+ );
2753
+ }
2754
+ return { kind: "scalar", raw: String(v), valueType: "number" };
2755
+ }
2756
+ case "object": {
2757
+ if (Array.isArray(v)) {
2758
+ const items = v.map((item, i) => coerceValue(item, `${atPath}[${i}]`));
2759
+ return { kind: "array", items };
2760
+ }
2761
+ const fields = coerceObject(v, atPath);
2762
+ return { kind: "object", fields };
2763
+ }
2764
+ default:
2765
+ throw new ConfigError(
2766
+ `fromMap: unsupported value type "${typeof v}" at path "${atPath}"`,
2767
+ atPath
2768
+ );
2769
+ }
2770
+ }
2035
2771
  // Annotate the CommonJS export names for ESM import in node:
2036
2772
  0 && (module.exports = {
2037
2773
  Config,
2038
2774
  ConfigError,
2775
+ NotResolvedError,
2776
+ PackageLookupError,
2039
2777
  ParseError,
2040
2778
  ResolveError,
2779
+ defaultParseOptions,
2780
+ defaultResolveOptions,
2781
+ empty,
2782
+ fromMap,
2041
2783
  parse,
2042
2784
  parseAsync,
2043
2785
  parseFile,
2044
- parseFileAsync
2786
+ parseFileAsync,
2787
+ parseFileWithOptions,
2788
+ parseString,
2789
+ parseStringWithOptions
2045
2790
  });