@effected/toml

中文文档
0.20.0发布于 2026/11/10MITTypeScriptES Module构建来源已验证近 30 天下载 8806

TOML 1.1.0 parsing, editing and formatting as Effect schemas: typed diagnostics, a lossless CST and first-class date-time values.

原始使用文档
文档目录8 节
  1. Why @effected/toml
  2. Install
  3. Quick start
  4. Editing without losing comments
  5. Value model
  6. Features
  7. Conformance
  8. License

@effected/toml

npm License: MIT Node.js %3E%3D24.11.0 TypeScript 7.0

Zero-dependency TOML 1.1.0 parsing, editing and formatting expressed as Effect schemas and pure functions. Parse TOML into plain values or a byte-exact linear CST, compute comment-preserving edits, format, modify by path, walk a document as a Stream, and decode straight into a validated domain schema.

Pre-1.0.0. This package is part of the @effected/* kit, built on stable Effect v4 (effect ~4.1.0) and still in 0.x development. Stable Effect makes a kit 1.0.0 possible, not automatic. To keep your effect and @effect/* versions on the line the kit is built and tested against, install @effected/pnpm-plugin-effected.

Stability: unstable. This package's API surface is not yet considered complete and may change across 0.x releases. Pin an exact version — even a package marked stable before 1.0.0 can introduce a breaking change by accident, and an exact pin turns that into a type-check error rather than a runtime surprise. Full policy: release strategy.

Why @effected/toml

TOML is the config format behind Cargo.toml, pyproject.toml and a large share of the Rust and Python toolchains — files that people maintain by hand and comment heavily. Most JavaScript TOML libraries round-trip through a plain-object model, which loses every comment and blank line the moment you write the file back out.

The engine here is written from scratch against the TOML 1.1.0 spec, and it is the only format package in this repo that vendors no upstream code at all. Parsing accepts everything 1.1 added — newlines, comments and trailing commas inside inline tables, optional seconds in times, and the \e and \xHH string escapes — while Toml.stringify deliberately emits 1.0 spellings, which are valid 1.1, so documents this package writes stay readable by 1.0-only consumers. It parses into a lossless linear CST whose expression spans tile the source byte-exact, so TomlDocument.parse(text).stringify() reproduces the original text exactly for any valid document. There is no separate re-serialization path that can drift from the source. Edits are byte-minimal splices against that CST, so formatting and path-based modification preserve every comment and every byte you did not touch.

The value model is honest about TOML's types rather than flattening them into JavaScript's. Integers past ±(2^53 − 1) decode to bigint instead of silently losing precision, and TOML's four date-time types decode to four calendar-validated value classes instead of a Date that cannot represent a local time. TOML has no null, so Toml.stringify on a value containing null fails with a structured UnsupportedValue diagnostic naming the offending path rather than dropping the key. Every fallible entry point carries a typed error built from TomlDiagnostic, and nesting-depth guards on both the parse and the stringify side mean hostile input fails through that channel rather than as a stack overflow.

Install

npm install @effected/toml effect
pnpm add @effected/toml effect

Requires Node.js >=24.11.0. effect v4 is a peer dependency; the package itself adds no other runtime dependencies.

All @effected/* packages are ESM-only: the exports maps publish only import conditions, so require() — including tools that resolve in CJS mode — fails with Node's ERR_PACKAGE_PATH_NOT_EXPORTED rather than loading a CJS build that does not exist. Import from an ES module.

Quick start

Compose your schema with Toml.schema to decode TOML straight into a validated domain value, or reach for the pre-bound Toml.TomlFromString codec when you just want the plain value:

import { Toml } from "@effected/toml";
import { Effect, Schema } from "effect";

const Config = Schema.Struct({ name: Schema.String, port: Schema.Number });
const ConfigFromToml = Toml.schema(Config);

const program = Effect.gen(function* () {
  return yield* Schema.decodeUnknownEffect(ConfigFromToml)(`
    name = "api"
    port = 3000
  `);
});

Effect.runPromise(program).then(console.log);
// { name: "api", port: 3000 }

Toml.stringify goes the other way, emitting canonical TOML:

import { Toml } from "@effected/toml";
import { Effect } from "effect";

Effect.runPromise(Toml.stringify({ title: "app", server: { port: 8080 } })).then(console.log);
// title = "app"
//
// [server]
// port = 8080

Editing without losing comments

TomlFormat.modify computes a TomlEdit array against the parsed CST; modifyToString applies it in one step. Comments, blank lines and layout that an edit does not cover come through byte-identical:

import { TomlFormat } from "@effected/toml";
import { Effect } from "effect";

const source = `# server config
name = "api"
port = 3000 # dev default
`;

Effect.runPromise(TomlFormat.modifyToString(source, ["port"], 8080)).then(console.log);
// # server config
// name = "api"
// port = 8080 # dev default

Value model

TOML's types do not all have a JavaScript equivalent, so the ones that do not get a real one:

TOMLDecodes to
Integer within ±(2^53 − 1)number
Integer beyond ±(2^53 − 1), inside the 64-bit rangebigint — precision is never silently lost
Integer outside the 64-bit rangefails with an IntegerOutOfRange diagnostic
Offset date-time (1979-05-27T07:32:00Z)TomlOffsetDateTime
Local date-time (1979-05-27T07:32:00)TomlLocalDateTime
Local date (1979-05-27)TomlLocalDate
Local time (07:32:00)TomlLocalTime

The four date-time classes are Schema.Class value objects with real Gregorian-calendar validation and structural equality, not Date subclasses: JavaScript's Date has no way to represent a date with no time, or a time with no offset.

TOML cannot represent null at all. Rather than dropping the key or writing an empty string, Toml.stringify fails:

import { Toml } from "@effected/toml";
import { Effect } from "effect";

Effect.runPromise(Effect.result(Toml.stringify({ a: null }))).then(console.log);
// Failure with TomlStringifyError, whose `diagnostic` carries:
// { code: "UnsupportedValue", message: "unsupported null value at a", ... }

Features

  • Toml.parse / Toml.stringify — value-level parse and canonical stringify, both carrying typed TomlParseError / TomlStringifyError channels, including nesting-depth guards on adversarial input.
  • Toml.fromString / Toml.TomlFromString / Toml.schema — string→domain schema factories that decode TOML directly into a validated Effect Schema value.
  • TomlDocument — the lossless document: parse, schema, toValue and stringify, backed by the linear CST whose expression spans reconstruct the source byte-exact.
  • TomlEdit / TomlRange (with applyAll) — the non-mutating text-edit vocabulary shared by the formatter and the modifier, and identical in shape to @effected/jsonc's and @effected/yaml's.
  • TomlFormat — format and formatToString compute conservative, comment-preserving formatting edits; modify and modifyToString compute the edits to replace, delete or insert a value at a path.
  • TomlVisitor — walk a parsed document as a Stream of visitor events.
  • TomlDiagnostic — the structured diagnostic (code, message, offset, length, line, character) every typed error carries, across five staged error-code unions.
  • TomlLocalDate / TomlLocalTime / TomlLocalDateTime / TomlOffsetDateTime — TOML's date-time types as calendar-validated Schema.Class value objects.

Conformance

The engine runs the toml-test compliance corpus — its files-toml-1.1.0 subset, 214 valid cases and 467 invalid ones — to a 100% pass rate with no skip list, and a differential property suite cross-checks it against an independent TOML parser. Both oracles are devDependency-only; neither reaches your runtime.

License

MIT

版本与发布记录

@effected/toml 共发布 26 个版本,首次发布于 2026/07/17;过去 12 个月发布了 26 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.20.0latest2026/11/103 天前
0.19.12026/11/067 天前
0.19.02026/11/0310 天前
0.18.02026/11/0112 天前
0.17.02026/10/2915 天前
0.16.02026/10/2420 天前

关于 @effected/toml 的常见问题

@effected/toml 是什么?

TOML 1.1.0 parsing, editing and formatting as Effect schemas: typed diagnostics, a lossless CST and first-class date-time values.

如何安装 @effected/toml?

在项目目录运行 npm install @effected/toml,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effect(~4.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effected/toml 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effected/toml 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effected/toml 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.11.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@effected/toml 可以商用吗?

@effected/toml 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effected/toml 的下载量有多少?

根据 npm 官方下载统计,@effected/toml 在 2026/10/13 – 2026/11/11 这 30 天内被下载 8,806 次,日均约 294 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effected/toml 还在维护吗?

最新版本 0.20.0 发布于 2026/11/10(3 天前);至今共发布 26 个版本,其中过去 12 个月发布了 26 个;首次发布于 2026/07/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。