@o3co/ts.hocon 1.12.0 → 1.14.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.ja.md +256 -32
- package/README.md +166 -32
- package/dist/adapters/env.cjs +26 -1678
- package/dist/adapters/env.d.cts +1 -1
- package/dist/adapters/env.d.ts +1 -1
- package/dist/adapters/env.js +11 -1625
- package/dist/adapters/json5.cjs +496 -0
- package/dist/adapters/json5.d.cts +47 -0
- package/dist/adapters/json5.d.ts +47 -0
- package/dist/adapters/json5.js +496 -0
- package/dist/adapters/jsonc.cjs +17 -1632
- package/dist/adapters/jsonc.d.cts +1 -1
- package/dist/adapters/jsonc.d.ts +1 -1
- package/dist/adapters/jsonc.js +9 -1586
- package/dist/adapters/properties.cjs +7 -1773
- package/dist/adapters/properties.d.cts +1 -1
- package/dist/adapters/properties.d.ts +1 -1
- package/dist/adapters/properties.js +6 -1735
- package/dist/adapters/toml.cjs +12 -1626
- package/dist/adapters/toml.d.cts +1 -1
- package/dist/adapters/toml.d.ts +1 -1
- package/dist/adapters/toml.js +10 -1589
- package/dist/adapters/yaml.cjs +20 -1635
- package/dist/adapters/yaml.d.cts +1 -1
- package/dist/adapters/yaml.d.ts +1 -1
- package/dist/adapters/yaml.js +10 -1589
- package/dist/chunk-GBNQADHT.cjs +83 -0
- package/dist/chunk-IMJ7QHIK.cjs +158 -0
- package/dist/chunk-QY7ECGS6.js +83 -0
- package/dist/chunk-RO4D6BH7.cjs +3792 -0
- package/dist/chunk-WTXULUZS.js +3792 -0
- package/dist/chunk-XWKO4JM3.js +158 -0
- package/dist/{config-Dlq6rF3w.d.cts → config-OccaKqXu.d.cts} +148 -4
- package/dist/{config-Dlq6rF3w.d.ts → config-OccaKqXu.d.ts} +148 -4
- package/dist/index.cjs +35 -3449
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +25 -3383
- package/dist/zod.cjs +107 -3291
- package/dist/zod.d.cts +83 -3
- package/dist/zod.d.ts +83 -3
- package/dist/zod.js +95 -3245
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -10,9 +10,11 @@ A [Lightbend HOCON](https://github.com/lightbend/config/blob/main/HOCON.md) pars
|
|
|
10
10
|
> **Implemented by [Claude](https://claude.ai/) (Anthropic)** — designed and built end-to-end with Claude Code.
|
|
11
11
|
> Reviewed by [GitHub Copilot](https://github.com/features/copilot) and [OpenAI Codex](https://openai.com/index/openai-codex/).
|
|
12
12
|
|
|
13
|
+
**Docs:** [o3co.github.io/hocon](https://o3co.github.io/hocon/) — the cross-language hub for all four implementations, the hocon2 CLI and the shared conformance corpus.
|
|
14
|
+
|
|
13
15
|
[日本語](README.ja.md)
|
|
14
16
|
|
|
15
|
-
> **Library stance:** ts.hocon is a HOCON config loader — its purpose is reading `.hocon` config files and providing typed access via the Config API (`getString`, `getNumber`, `getBoolean`, `getDuration`, `getBytes`, `toObject`). It is not a low-level parser API. Internal types like `HoconValue` may change between minor versions.
|
|
17
|
+
> **Library stance:** ts.hocon is a HOCON config loader — its purpose is reading `.hocon` config files and providing typed access via the Config API (`getString`, `getNumber`, `getBoolean`, `getDuration`, `getBytes`, `getPeriod`, `toObject`). It is not a low-level parser API. Internal types like `HoconValue` may change between minor versions.
|
|
16
18
|
>
|
|
17
19
|
> **Cross-language conformance:** This implementation is tested against shared expected-JSON fixtures from [o3co/xx.hocon](https://github.com/o3co/xx.hocon) alongside [go.hocon](https://github.com/o3co/go.hocon), [rs.hocon](https://github.com/o3co/rs.hocon), and [py.hocon](https://github.com/o3co/py.hocon) to ensure all four implementations meet the same Lightbend HOCON specification.
|
|
18
20
|
|
|
@@ -36,12 +38,13 @@ import { parse } from '@o3co/ts.hocon' // ESM
|
|
|
36
38
|
const { parse } = require('@o3co/ts.hocon') // CJS
|
|
37
39
|
```
|
|
38
40
|
|
|
39
|
-
On every PR and every release, CI loads all
|
|
41
|
+
On every PR and every release, CI loads all eight exports both ways against the
|
|
40
42
|
built artifact, calls each one (including a package-scoped `include`, which
|
|
41
|
-
behaves differently per bundle format),
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
behaves differently per bundle format), checks that what a subpath returns or
|
|
44
|
+
throws is an instance of the classes the root exports, and checks that each
|
|
45
|
+
condition declares its own type declarations. That gate exists because 1.10.0
|
|
46
|
+
shipped with all seven of its CJS entrypoints throwing at load; it catches that
|
|
47
|
+
class of defect rather than promising none can exist.
|
|
45
48
|
|
|
46
49
|
### 2. Use
|
|
47
50
|
|
|
@@ -77,6 +80,49 @@ HOCON isn't just a serialization format — it's a **config-injection language**
|
|
|
77
80
|
|
|
78
81
|
On top of that, HOCON combines the readability of YAML with the structure of JSON — making it a strong fit for anything beyond flat key-value config.
|
|
79
82
|
|
|
83
|
+
## Configuration that stays inside its module
|
|
84
|
+
|
|
85
|
+
In a large codebase, configuration tends to leak: one big file that every team edits, defaults copied into each service, and modules that read each other's keys. HOCON lets each module own its configuration the way it owns its code.
|
|
86
|
+
|
|
87
|
+
- **Each module owns a namespace and ships its own defaults.** The `auth` module keeps `auth { … }` next to its code, with every default filled in. The application never copies those defaults; it brings them in with one `include`.
|
|
88
|
+
- **The application overrides only what differs.** Later layers deep-merge over earlier ones, so the application states two keys, not the whole block.
|
|
89
|
+
- **Each module reads only its own subtree, through its own schema.** The module asks for `auth` and validates it against the schema it defines. The shape of that section is the module's contract; nothing else in the file is visible to it.
|
|
90
|
+
- **Wiring between modules lives in the application.** The module only declares the keys it reads (`auth.db`) and leaves them without a default when the application must supply them. The application connects them to shared values (`auth.db = ${shared.db}`). Modules never reference each other or the application's keys, so dependencies run one way.
|
|
91
|
+
|
|
92
|
+
```hocon
|
|
93
|
+
# auth/reference.conf: shipped inside the auth package, owned by the auth team
|
|
94
|
+
auth {
|
|
95
|
+
session { lifetime = 30m, max-body = 64KiB }
|
|
96
|
+
# db: no default; the application supplies auth.db
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```hocon
|
|
101
|
+
# application.conf: owned by the application
|
|
102
|
+
include package("github.com/acme/auth", "reference.conf")
|
|
103
|
+
|
|
104
|
+
shared.db { host = db.internal, port = 5432 }
|
|
105
|
+
auth.db = ${shared.db} # wiring lives in the application
|
|
106
|
+
auth.session.lifetime = 2h # override what differs; the rest stays the module's default
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
// inside the auth module
|
|
111
|
+
import { getValidated, hocon } from '@o3co/ts.hocon/zod'
|
|
112
|
+
import { z } from 'zod'
|
|
113
|
+
|
|
114
|
+
const Auth = z.object({
|
|
115
|
+
session: z.object({
|
|
116
|
+
lifetime: hocon.duration('s').pipe(z.number().int().positive()),
|
|
117
|
+
'max-body': hocon.bytes().pipe(z.number().int().positive()),
|
|
118
|
+
}),
|
|
119
|
+
db: z.object({ host: z.string(), port: z.number().int() }),
|
|
120
|
+
})
|
|
121
|
+
const auth = getValidated(config, 'auth', Auth)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`include package(...)` is not part of HOCON.md: it is a convention shared by the o3co implementations ([E11](https://github.com/o3co/xx.hocon/blob/main/docs/extra-spec-conventions.md#e11)), a non-JVM counterpart to `include classpath(...)`. It is how a module publishes its defaults without the application knowing where its files live (in ts.hocon, the default resolver hands `<identifier>/<file>` to Node module resolution from the including file, so the identifier there is the npm package name, such as `@acme/auth`, which E11 allows for TS-only config; a portable Go-style identifier like the one above needs a `packageResolver` that maps it to the package; see [`ParseOptions`](#parse-functions)). On the JVM, Lightbend's `reference.conf` convention plays the same role.
|
|
125
|
+
|
|
80
126
|
## Features
|
|
81
127
|
|
|
82
128
|
- Full HOCON parsing: objects, arrays, scalars, substitutions (`${path}`, `${?path}`)
|
|
@@ -85,11 +131,12 @@ On top of that, HOCON combines the readability of YAML with the structure of JSO
|
|
|
85
131
|
- `+=` append operator
|
|
86
132
|
- `include "file.conf"` and `include file("file.conf")` directives
|
|
87
133
|
- Triple-quoted strings (`"""..."""`)
|
|
88
|
-
- Duration
|
|
134
|
+
- Duration, byte size and period parsing (`getDuration()`, `getBytes()`, `getPeriod()`)
|
|
135
|
+
- HOCON emitter (`renderHocon()`) — renders a resolved `Config` back to HOCON text; re-parsing yields the same value tree (E18 round-trip contract)
|
|
89
136
|
- Sync and async API (`parse` / `parseAsync` / `parseFile` / `parseFileAsync`)
|
|
90
137
|
- ESM + CJS dual package (every entrypoint smoke-tested in both formats on each CI run)
|
|
91
138
|
- Optional [Zod](https://zod.dev/) integration for schema validation
|
|
92
|
-
-
|
|
139
|
+
- Runs in browsers (`parse`/`parseAsync`; the bundler must stub Node built-ins, see [Browser Compatibility](#browser-compatibility))
|
|
93
140
|
|
|
94
141
|
## API
|
|
95
142
|
|
|
@@ -114,26 +161,35 @@ parseFileAsync(path: string, opts?: ParseOptions): Promise<Config>
|
|
|
114
161
|
| `env` | `Record<string, string>` | Environment variables for substitution (default: `process.env`) |
|
|
115
162
|
| `readFileSync` | `(path: string) => string` | Custom file reader (sync) |
|
|
116
163
|
| `readFile` | `(path: string) => Promise<string>` | Custom file reader (async) |
|
|
164
|
+
| `resolveSubstitutions` | `boolean` | `false` returns an unresolved Config, to resolve later (default `true`; see [Deferred resolution](#deferred-resolution-api-e12)) |
|
|
165
|
+
| `originDescription` | `string` | Source name for resolve-error messages when there is no file path |
|
|
166
|
+
| `resolveFrom` | `string \| string[]` | Where `include package(...)` starts Node module resolution (default: `baseDir`, which for `parseFile` is the file's directory; else `process.cwd()`) |
|
|
167
|
+
| `packageResolver` | `PackageResolver` | Custom resolver for `include package(...)` (Yarn PnP, bundlers, edge runtimes, tests); `resolveFrom` is then ignored |
|
|
117
168
|
|
|
118
169
|
### Config methods
|
|
119
170
|
|
|
120
171
|
| Method | Returns | Throws if |
|
|
121
172
|
|--------|---------|-----------|
|
|
122
173
|
| `get(path)` | `unknown \| undefined` | — |
|
|
174
|
+
| `getValue(path)` (the raw value tree, for introspection) | `ReadonlyHoconValue \| undefined` | unresolved |
|
|
123
175
|
| `getString(path)` | `string` | missing, wrong type, or unresolved |
|
|
124
176
|
| `getNumber(path)` | `number` | missing, wrong type, or unresolved |
|
|
125
177
|
| `getBoolean(path)` | `boolean` | missing, wrong type, or unresolved |
|
|
126
178
|
| `getConfig(path)` | `Config` | missing, not an object, or unresolved |
|
|
127
179
|
| `getList(path)` | `unknown[]` | missing, not an array, or unresolved |
|
|
128
|
-
| `getDuration(path, unit?)` | `number` | missing, not a string
|
|
129
|
-
| `getBytes(path, unit?)` | `number` | missing, not a string
|
|
180
|
+
| `getDuration(path, unit?)` | `number` | missing, not a string or number, invalid duration format, or not a finite number (E21) |
|
|
181
|
+
| `getBytes(path, unit?)` | `number` | missing, not a string or number, invalid byte size format, negative, or beyond 2^53-1 bytes |
|
|
182
|
+
| `getPeriod(path)` | `Period` | missing, not a string or number, or invalid period format |
|
|
130
183
|
| `has(path)` | `boolean` | — |
|
|
131
184
|
| `keys()` | `string[]` | — |
|
|
132
185
|
| `withFallback(fallback)` | `Config` | — |
|
|
133
186
|
| `resolve(opts?)` | `Config` | unresolvable substitution (unless `allowUnresolved: true`) |
|
|
134
187
|
| `resolveWith(source, opts?)` | `Config` | source unresolved, or unresolvable substitution |
|
|
135
188
|
| `isResolved()` | `boolean` | — |
|
|
136
|
-
| `toObject()` | `unknown` | — |
|
|
189
|
+
| `toObject(opts?)` | `unknown` | — (`{ nullPrototype: true }` returns objects that inherit nothing; see [Security Considerations](#security-considerations)) |
|
|
190
|
+
| `renderHocon()` | `string` | unresolved |
|
|
191
|
+
|
|
192
|
+
Every "Throws if" above is a `ConfigError` with `.path`, with two exceptions. An unresolved value read by `getValue` or a getter, or an unresolved `resolveWith` source, throws `NotResolvedError` (a `ConfigError` subclass). An unresolvable substitution in `resolve` / `resolveWith` throws `ResolveError`.
|
|
137
193
|
|
|
138
194
|
### Deferred resolution API (E12)
|
|
139
195
|
|
|
@@ -171,7 +227,7 @@ result.getString('r') // "val"
|
|
|
171
227
|
|
|
172
228
|
| Option | Type | Default | Description |
|
|
173
229
|
|--------|------|---------|-------------|
|
|
174
|
-
| `useSystemEnvironment` | `boolean` | `true` | Consult `process.env` for substitution fallback |
|
|
230
|
+
| `useSystemEnvironment` | `boolean` | `true` | Consult the environment (`ParseOptions.env`, default `process.env`) for substitution fallback |
|
|
175
231
|
| `allowUnresolved` | `boolean` | `false` | Leave unresolvable substitutions in place (no error) |
|
|
176
232
|
|
|
177
233
|
### Zod integration
|
|
@@ -194,21 +250,56 @@ const app = validate(cfg, Schema)
|
|
|
194
250
|
const port = getValidated(cfg, 'server.port', z.number().int())
|
|
195
251
|
```
|
|
196
252
|
|
|
253
|
+
Durations and byte sizes are strings in HOCON (`30m`, `64KiB`), so `z.number()` refuses them. The `hocon.duration(unit?)` and `hocon.bytes(unit?)` markers read them with the same rules as `getDuration()` / `getBytes()` and give a number in the requested unit (default `ms` / `B`; a plain number keeps the getters' meaning). Pipe the marker into a number schema for range and integer checks:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { hocon, getValidated } from '@o3co/ts.hocon/zod'
|
|
257
|
+
|
|
258
|
+
const Session = z.object({
|
|
259
|
+
lifetime: hocon.duration('s').pipe(z.number().int().min(60).max(86_400)),
|
|
260
|
+
maxBody: hocon.bytes().pipe(z.number().int().positive()),
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
// session { lifetime = 30m, maxBody = 64KiB }
|
|
264
|
+
getValidated(cfg, 'session', Session) // { lifetime: 1800, maxBody: 65536 }
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
`hocon.period()` reads a period the way `getPeriod()` does and gives a `Period` (`{ years, months, days }`); refine it in the schema:
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
const Backup = z.object({
|
|
271
|
+
keep: hocon.period().refine((p) => p.years === 0, 'use months or days'),
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
// backup { keep = 2w }
|
|
275
|
+
getValidated(cfg, 'backup', Backup) // { keep: { years: 0, months: 0, days: 14 } }
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
An invalid duration, size or period fails validation with the key's path. The markers are ordinary Zod schemas, so they work in `validate`, `getValidated`, `parseWithSchema`, `parseFileWithSchema` and anywhere else Zod runs them. Getters and markers both read exponent forms (`1e3`, `"1.5e2s"`). A marker reads a number through its JavaScript value, though, so a number literal with more significant digits than a double holds can read differently from the getters, which read the literal's text exactly (`1.99999999999999999` B is 1 through `getBytes()`, 2 through the marker); a period marker also reads a literal with a decimal point or an exponent (`7.0`, `1e2`) as an integer, which `getPeriod()` refuses. Write unit values with a unit (`30s`, `7d`) to avoid both.
|
|
279
|
+
|
|
280
|
+
On an unresolved Config (parsed with `resolveSubstitutions: false`, a fallback stack not yet `resolve()`d, or the result of `resolve({ allowUnresolved: true })` with something left), `validate` throws `NotResolvedError`, as the getters do, rather than validating a tree with the pending values missing; `getValidated` throws it for a path whose value is unresolved and validates a resolved one. Call `resolve()` first.
|
|
281
|
+
|
|
197
282
|
Install Zod as a peer dependency:
|
|
198
283
|
```bash
|
|
199
284
|
npm install zod
|
|
200
285
|
```
|
|
201
286
|
|
|
287
|
+
The Zod entry points (`validate`, `getValidated`, `parseWithSchema`, `parseFileWithSchema`) refuse an object key named `__proto__`, `constructor` or `prototype` anywhere in the value: Zod drops a `__proto__` key from a record without an error, and the three are refused alike so the result does not depend on the schema. The `ZodError` has one issue per key, at the object that holds it, with the key in the message and in `params.key` (`key "__proto__" names an object's prototype → at limits`). Rename the key, or read it with the plain getters (`cfg.getNumber('limits."constructor"')`), which are unaffected.
|
|
288
|
+
|
|
202
289
|
### Error types
|
|
203
290
|
|
|
204
291
|
```ts
|
|
205
|
-
import { ParseError, ResolveError, ConfigError } from '@o3co/ts.hocon'
|
|
292
|
+
import { ParseError, ResolveError, PackageLookupError, ConfigError, NotResolvedError } from '@o3co/ts.hocon'
|
|
206
293
|
|
|
207
|
-
// ParseError
|
|
208
|
-
// ResolveError
|
|
209
|
-
//
|
|
294
|
+
// ParseError — lexing/parsing failure: .line, .col, .file?
|
|
295
|
+
// ResolveError — substitution/include failure: .path, .line, .col, .file?
|
|
296
|
+
// PackageLookupError — include package(...) not found (a ResolveError): .identifier, .packageFile
|
|
297
|
+
// ConfigError — wrong type, missing path, invalid value: .path
|
|
298
|
+
// NotResolvedError — getter or Zod entry point on an unresolved value (a ConfigError): .path
|
|
210
299
|
```
|
|
211
300
|
|
|
301
|
+
Every entrypoint (`/zod`, `/adapters/*`) throws these same classes, so `instanceof` against the root's exports works across entrypoints within one module system. The Zod entry points throw `ZodError` for a value that does not match the schema.
|
|
302
|
+
|
|
212
303
|
## HOCON Examples
|
|
213
304
|
|
|
214
305
|
```hocon
|
|
@@ -260,8 +351,36 @@ c.getBytes('max-size') // 536870912 (bytes)
|
|
|
260
351
|
c.getBytes('max-size', 'MiB') // 512
|
|
261
352
|
```
|
|
262
353
|
|
|
263
|
-
Supported duration units: `ns`, `us`, `ms`, `s`, `m`, `h`, `d` (and long forms like `seconds`, `minutes`). Duration unit names are case-sensitive and must be lowercase (HOCON spec).
|
|
264
|
-
|
|
354
|
+
Supported duration units: `ns`, `us`, `ms`, `s`, `m`, `h`, `d` (and long forms like `seconds`, `minutes`). Duration unit names are case-sensitive and must be lowercase (HOCON spec). A number or a string with no unit is milliseconds for a duration and bytes for a byte size.
|
|
355
|
+
|
|
356
|
+
The number is read from its text as an exact decimal and multiplied by the unit exactly (E20). A duration is then rounded once in the requested unit: `1.001s` is `1001` ms (`1001000000` ns), where Lightbend's double arithmetic gives `1000999999` ns. A byte size is truncated toward zero to whole bytes (as Lightbend does) before it is converted: `1.0009kB` is `1000`. A duration whose value is not a finite number is an error (E21). A byte size must be non-negative and at most 2^53-1 bytes (the largest integer a JS number holds exactly), so 1 or more of any exa-, zetta- or yotta- unit (`EB`, `Ei`/`EiB`, `E`/`e`, `exabyte`, … through `Y`) is an error; use fractions (`0.001EiB`) or a smaller unit.
|
|
357
|
+
|
|
358
|
+
Supported byte units, the same set as Lightbend and case-sensitive like it:
|
|
359
|
+
|
|
360
|
+
- bytes: `B`, `b`, `byte`, `bytes`
|
|
361
|
+
- powers of 1000: `kB`, `MB`, `GB`, `TB`, `PB`, `EB`, `ZB`, `YB`, and `kilobyte(s)` … `yottabyte(s)`
|
|
362
|
+
- powers of 1024: `Ki`/`KiB`, `Mi`/`MiB`, … `Yi`/`YiB`, and `kibibyte(s)` … `yobibyte(s)`
|
|
363
|
+
- single letters, powers of 1024 in either case (java `-Xmx` style): `K`/`k`, `M`/`m`, `G`/`g`, `T`/`t`, `P`/`p`, `E`/`e`, `Z`/`z`, `Y`/`y`
|
|
364
|
+
|
|
365
|
+
Other spellings are errors: `KB`, `kb`, `kib`, `Kilobytes`, `MEGABYTES`. (`'KB'` and the other names in `getBytes(path, unit)` are output units, a separate list.)
|
|
366
|
+
|
|
367
|
+
### Periods
|
|
368
|
+
|
|
369
|
+
```ts
|
|
370
|
+
const c = parse(`
|
|
371
|
+
retention = 2w
|
|
372
|
+
billing = "1 month"
|
|
373
|
+
archive = 1y
|
|
374
|
+
grace = 3 // a bare integer is days
|
|
375
|
+
`)
|
|
376
|
+
|
|
377
|
+
c.getPeriod('retention') // { years: 0, months: 0, days: 14 }
|
|
378
|
+
c.getPeriod('billing') // { years: 0, months: 1, days: 0 }
|
|
379
|
+
c.getPeriod('archive') // { years: 1, months: 0, days: 0 }
|
|
380
|
+
c.getPeriod('grace') // { years: 0, months: 0, days: 3 }
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Supported period units: `d`/`day`/`days`, `w`/`week`/`weeks` (folded into days), `m`/`mo`/`month`/`months`, `y`/`year`/`years`. Unit names are case-sensitive and must be lowercase (`m` is months here, not minutes). A period is integer only (`7.5` is an error, unlike durations and sizes), may be negative, and each component must fit in a 32-bit signed integer, as in Lightbend (`java.time.Period`), rs.hocon and py.hocon.
|
|
265
384
|
|
|
266
385
|
## Spec Compliance
|
|
267
386
|
|
|
@@ -269,23 +388,29 @@ Conformance against the [Lightbend HOCON specification](https://github.com/light
|
|
|
269
388
|
|
|
270
389
|
| Metric | Status |
|
|
271
390
|
| ------------------------------------- | ------------- |
|
|
272
|
-
| Spec total (incl. out-of-scope) | **
|
|
273
|
-
| In-scope only | **
|
|
391
|
+
| Spec total (incl. out-of-scope) | **91.9%** |
|
|
392
|
+
| In-scope only | **100.0%** |
|
|
274
393
|
| Lightbend `test01`–`test13` suite | 13/13 passing |
|
|
275
394
|
|
|
276
395
|
**Extra-spec conventions (E-series) — implementation status:**
|
|
277
396
|
|
|
278
397
|
| Item | Description | Status |
|
|
279
398
|
|------|-------------|--------|
|
|
280
|
-
| E11 | `include package(...)` service-locator includes | ✅ v1.
|
|
399
|
+
| E11 | `include package(...)` service-locator includes | ✅ v1.4.0 |
|
|
281
400
|
| E12 | Deferred substitution resolution (`parse → withFallback → resolve()` lifecycle) | ✅ v1.4.0 |
|
|
401
|
+
| E18 | HOCON emitter round-trip contract (`renderHocon()`) | ✅ v1.13.0 |
|
|
402
|
+
| E19 | A float literal that overflows the double range (`1e999`) is a parse error | ✅ v1.14.0 |
|
|
403
|
+
| E20 | Unit arithmetic is exact decimal (durations: a deliberate divergence from Lightbend) | ✅ v1.14.0 |
|
|
404
|
+
| E21 | A duration the implementation cannot represent is an error | ✅ v1.14.0 |
|
|
405
|
+
|
|
406
|
+
The E-series items are conventions shared by the o3co implementations beyond HOCON.md; see [`xx.hocon/docs/extra-spec-conventions.md`](https://github.com/o3co/xx.hocon/blob/main/docs/extra-spec-conventions.md).
|
|
282
407
|
|
|
283
408
|
Not supported:
|
|
284
409
|
|
|
285
410
|
- `include url(...)`
|
|
286
411
|
- `include classpath(...)`
|
|
287
412
|
|
|
288
|
-
Supported since
|
|
413
|
+
Supported since v1.0.0 (P1):
|
|
289
414
|
|
|
290
415
|
- `.properties` file parsing
|
|
291
416
|
|
|
@@ -321,26 +446,33 @@ For typical application configs (loaded once at startup), the parsing cost is ne
|
|
|
321
446
|
|
|
322
447
|
### Feature Comparison with node-config
|
|
323
448
|
|
|
324
|
-
Feature-level comparison with [node-config](https://github.com/node-config/node-config) **
|
|
449
|
+
Feature-level comparison with [node-config](https://github.com/node-config/node-config) **v5.0.1** reading JSON config files, as of 2026-10-16:
|
|
325
450
|
|
|
326
451
|
| Feature | ts.hocon | node-config (JSON) |
|
|
327
452
|
|---|---|---|
|
|
328
|
-
| Comments | `//` `#` |
|
|
453
|
+
| Comments | `//` `#` | Yes (`//`, `/* */`; `.json` is parsed as JSON5) |
|
|
329
454
|
| Multi-line strings | `"""..."""` | No |
|
|
330
455
|
| Substitution (`${path}`) | Yes | No |
|
|
331
456
|
| Optional substitution (`${?path}`) | Yes | No |
|
|
332
457
|
| Environment variable reference | Yes (via substitution) | Partial (`custom-environment-variables` file) |
|
|
333
458
|
| Include | Yes | No |
|
|
334
|
-
| Deep merge | Yes (arrays
|
|
459
|
+
| Deep merge | Yes (objects; arrays replaced, `+=` / concatenation to extend) | Yes (objects; arrays replaced) |
|
|
335
460
|
| Append operator (`+=`) | Yes | No |
|
|
336
461
|
| Environment-based config | Configurable via HOCON | Yes (filename convention) |
|
|
337
462
|
| Schema validation | Zod integration | No |
|
|
338
463
|
| Programmatic API | `parse(string)` | File-based initialization, then `get()` |
|
|
339
|
-
| Typed getters | `getString`, `getNumber`, etc. | `get()` (
|
|
464
|
+
| Typed getters | `getString`, `getNumber`, etc. (checked at runtime) | `get<T>()` (type parameter only, no runtime check) |
|
|
340
465
|
|
|
341
466
|
## Browser Compatibility
|
|
342
467
|
|
|
343
|
-
`parse()` and `parseAsync()` work in browsers. `parseFile()` and `parseFileAsync()` require Node.js (or a custom `readFileSync`/`readFile` option).
|
|
468
|
+
`parse()` and `parseAsync()` work in browsers, as do the adapters and the Zod integration. `parseFile()` and `parseFileAsync()` require Node.js (or a custom `readFileSync`/`readFile` option).
|
|
469
|
+
|
|
470
|
+
The package still imports the Node built-ins `fs`, `path`, `module` and `url` (for file reading and `include package(...)`) and uses them only on those paths. A browser bundle needs them replaced by empty modules:
|
|
471
|
+
|
|
472
|
+
- **Vite** (7 and 8) does this by default, so there is nothing to configure.
|
|
473
|
+
- **Other bundlers:** map the four modules to an empty module (`export default {}`), for example with an esbuild plugin or webpack 5's `resolve.fallback: { fs: false, path: false, module: false, url: false }`. Marking them external is not enough, because the browser cannot load `fs`.
|
|
474
|
+
|
|
475
|
+
That covers parsing without file includes. Includes read through your own `readFile` / `readFileSync` (below) also resolve paths, so they need a browser implementation of `path` instead of the empty module, such as the `path-browserify` package. `include package(...)` needs a custom `packageResolver` there (see `ParseOptions`).
|
|
344
476
|
|
|
345
477
|
```ts
|
|
346
478
|
// Browser usage with custom file loader
|
|
@@ -407,7 +539,7 @@ const config = parseWithSchema(hoconInput, schema) // fails fast on startup
|
|
|
407
539
|
| Project | Language | Registry | Description |
|
|
408
540
|
|---------|----------|----------|-------------|
|
|
409
541
|
| [go.hocon](https://github.com/o3co/go.hocon) | Go | [pkg.go.dev](https://pkg.go.dev/github.com/o3co/go.hocon) | HOCON parser for Go |
|
|
410
|
-
| [rs.hocon](https://github.com/o3co/rs.hocon) | Rust | [crates.io](https://crates.io/crates/
|
|
542
|
+
| [rs.hocon](https://github.com/o3co/rs.hocon) | Rust | [crates.io](https://crates.io/crates/hocon-parser) | HOCON parser for Rust |
|
|
411
543
|
| [py.hocon](https://github.com/o3co/py.hocon) | Python | [PyPI](https://pypi.org/project/hocon-parser/) | HOCON parser for Python |
|
|
412
544
|
| [hocon2](https://github.com/o3co/hocon2) | Go | [pkg.go.dev](https://pkg.go.dev/github.com/o3co/hocon2) | HOCON → JSON/YAML/TOML/Properties CLI |
|
|
413
545
|
|
|
@@ -435,8 +567,9 @@ Deferring resolution matters: the plain `parse` resolves as it goes, so a
|
|
|
435
567
|
| `@o3co/ts.hocon/adapters/properties` | — | `java.util.Properties`, sharing the `include` syntax layer |
|
|
436
568
|
| `@o3co/ts.hocon/adapters/env` | — | Bulk-mounts a prefixed namespace; also reads `.env` |
|
|
437
569
|
| `@o3co/ts.hocon/adapters/jsonc` | — | JSON with comments and trailing commas |
|
|
438
|
-
| `@o3co/ts.hocon/adapters/
|
|
439
|
-
| `@o3co/ts.hocon/adapters/
|
|
570
|
+
| `@o3co/ts.hocon/adapters/json5` | — | JSON5 1.0.0, hand-rolled scanner, zero dependencies |
|
|
571
|
+
| `@o3co/ts.hocon/adapters/toml` | `smol-toml` (>= 1.0) | Optional peer dependency |
|
|
572
|
+
| `@o3co/ts.hocon/adapters/yaml` | `yaml` (>= 2.0) | Optional peer dependency; scalar resolution is that library's answer, with `version: '1.2'` declared so it cannot drift |
|
|
440
573
|
|
|
441
574
|
The TOML and YAML libraries are **optional peer dependencies**, so installing
|
|
442
575
|
this package still pulls in nothing — you add the one you actually use. Plain
|
|
@@ -458,11 +591,12 @@ cfg.getString('"foo.bar"') // "dotted" — one key whose name contains a dot
|
|
|
458
591
|
cfg.getString('foo.bar') // "nested" — foo -> bar
|
|
459
592
|
```
|
|
460
593
|
|
|
461
|
-
### Numbers from JSONC and YAML
|
|
594
|
+
### Numbers from JSONC, JSON5 and YAML
|
|
462
595
|
|
|
463
596
|
Integers are ingested losslessly: an integer literal too wide for a JS `number`
|
|
464
597
|
keeps its digits instead of being rounded, and one outside the int64 range is
|
|
465
|
-
refused rather than silently mangled
|
|
598
|
+
refused rather than silently mangled (JSON5's hex integers included). What you
|
|
599
|
+
get back depends on the getter:
|
|
466
600
|
|
|
467
601
|
```ts
|
|
468
602
|
const cfg = parseJsonc('{"id": 9007199254740993}')
|