Skip to content

src,lib: add experimental node:formats/toml module - #67874

Open
aduh95 wants to merge 4 commits into
nodejs:mainfrom
aduh95:toml++
Open

aduh95 wants to merge 4 commits into
nodejs:mainfrom
aduh95:toml++

Conversation

@aduh95

@aduh95 aduh95 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Related to #67157 //cc @marcopiraccini

Opt-in with a configure flag, so it can be backported. The plan is to make it opt-out in a future semver-major.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/actions
  • @nodejs/gyp
  • @nodejs/loaders
  • @nodejs/security-wg
  • @nodejs/startup
  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Nov 29, 2026
@github-actions

This comment was marked as outdated.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Add the header-only toml++ library, its updater script and license
entry, for use by node:formats/toml.
Add a TOML parser and serializer backed by toml++, available behind
`--experimental-toml` as the scheme-only `node:formats/toml` builtin.

Assisted-by: Devin
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
This can be reverted once at least one serializer it documents is
enabled by default.

Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
@github-actions

Copy link
Copy Markdown
Contributor
Platform Number of requisites Proportion of new derivations
x86_64-linux 519 -> 520 => +1 1 / 520 = 0.1%
aarch64-linux 520 -> 521 => +1 1 / 521 = 0.1%
x86_64-darwin 447 -> 448 => +1 1 / 448 = 0.2%
aarch64-darwin 449 -> 450 => +1 1 / 450 = 0.2%
Total 1935 -> 1939 => +4 4 / 1939 = 0.2%
Version changes
Package Version Platforms
tomlplusplus added: 3.4.0 x86_64-darwin
tomlplusplus added: 3.5.1 aarch64-darwin, aarch64-linux, x86_64-linux

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 56.87500% with 69 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.56%. Comparing base (1611823) to head (83860ae).
⚠️ Report is 28 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/formats/toml.js 52.73% 69 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67874      +/-   ##
==========================================
- Coverage   90.58%   90.56%   -0.03%     
==========================================
  Files         802      804       +2     
  Lines      286656   286816     +160     
  Branches    55369    55378       +9     
==========================================
+ Hits       259668   259753      +85     
- Misses      17089    17184      +95     
+ Partials     9899     9879      -20     
Files with missing lines Coverage Δ
lib/formats/toml.js 100.00% <100.00%> (ø)
lib/internal/bootstrap/realm.js 97.06% <100.00%> (+<0.01%) ⬆️
lib/internal/errors.js 98.42% <100.00%> (+<0.01%) ⬆️
src/node_binding.cc 76.78% <ø> (ø)
src/node_binding.h 80.00% <ø> (ø)
src/node_builtins.cc 77.70% <100.00%> (+0.03%) ⬆️
src/node_external_reference.h 100.00% <ø> (ø)
lib/internal/formats/toml.js 52.73% <52.73%> (ø)

... and 34 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jsumners-nr jsumners-nr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love seeing this move so quickly. Just a bit concerned that the docs attempt to enforce the same API across all formats/ namespaced modules.

Comment thread doc/api/formats.md
Comment on lines +11 to +14
Every format module exports two functions:

* `parse(input[, options])` turns a document into JavaScript values.
* `stringify(value)` turns JavaScript values into a document.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be too restrictive. Each module should strive for such a simple API, but it should also be allowed to export an interface that makes sense for the module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs are not a spec, they only document the existing behavior. If another format is added, the docs will need to be updated anyway. In any case, happy to update if you have a suggestion

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the opening paragraph is sufficient. Lines 11 through 14 can be removed and everything will still make sense.

@jasnell jasnell added the large-pr PRs subject to the large-PR policy. label Dec 1, 2026
Comment thread doc/api/errors.md
An attempt was made to issue Server Name Indication from a TLS server-side
socket, which is only valid from a client.

<a id="ERR_TOML_PARSE"></a>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit... Let's just make sure the err codes with the various formats are consistently named.

Comment thread doc/api/formats.md
* `input` {string|ArrayBuffer|SharedArrayBuffer|TypedArray|DataView} The TOML
document. Binary input must be UTF-8. A leading byte order mark is ignored.
* `options` {Object}
* `bigint` {boolean} If `true`, every integer is returned as a {bigint}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also support a non-throwing parse mode, like the yaml case.


// Basic document.
{
const doc = TOML.parse(`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worthwhile using fixtures here to help the tests be more readable

Comment thread src/node_toml.cc
for (const auto& [key, node] : entries) {
Local<String> name;
Local<Value> value;
if (!String::NewFromUtf8(isolate_,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToV8Value?

Comment thread src/node_toml.cc
KeyCollectionMode::kOwnOnly,
static_cast<PropertyFilter>(PropertyFilter::ONLY_ENUMERABLE |
PropertyFilter::SKIP_SYMBOLS),
v8::IndexFilter::kIncludeIndices,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using decl at top?

Comment thread src/node_toml.cc
out->push_back(std::move(*node));
}
Leave();
return Just(true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never returns Just(false)?

Would MaybeVoid be better?

Comment thread src/node_toml.cc
return std::make_unique<::toml::value<int64_t>>(integer);
}
if (value->IsArray()) {
auto array = std::make_unique<::toml::array>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if this could accept sequences/sync iterators

Comment thread src/node_toml.cc
Local<Array> info = args[3].As<Array>();
Local<String> message;
if (!String::NewFromUtf8(isolate,
description.data(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToV8Value?

Comment thread src/node_toml.cc

Local<String> result;
if (!String::NewFromUtf8(isolate,
output.data(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToV8Value

case kLocalDate:
return Temporal.PlainDate.from(text);
default:
return Temporal.PlainTime.from(text);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar yet with the primordial captures for Temporal yet... Are these safe captures?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be mutated by userland so indeed we would need a better solution to remove any temptation of relying on monkey-patching. I should at least add a TODO

@TheOneTheOnlyJJ

TheOneTheOnlyJJ commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Note that if #67157 gets merged, it may be possible to use tomlplusplus's yaml_formatter for converting between formats more performantly than in pure JS.
It may not be feasible, but I'm just noting this down here for potential future reference.

Comment thread src/node_toml.cc

// The JS layer returns an RFC 3339 string. Let toml++ parse it so that it
// ends up with the right datetime type.
std::unique_ptr<::toml::node> ParseDatetime(Local<Value> text) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really has a fair amount of unnecessary overhead. Every datetime in stringify runs a full TOML parse... First it's serialized to an ISO 8601 string. Then we put "v = " in front and call toml::parse() (which builds a whole table), pull v back out and copy it. That's a parser run per timestamp. Could we just pass the numeric fields from JS and build toml::date/time/date_time directly? Or parse the fixed-format string by hand?

Comment thread src/node_toml.cc
return Number::New(isolate_, node.as_floating_point()->get());
case ::toml::node_type::boolean:
return Boolean::New(isolate_, node.as_boolean()->get());
case ::toml::node_type::date: {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snprintfs the fields into a std::string (which allocates once offsets or nanoseconds are present), then make a JS string, add [offset], then have Temporal parse it all again. Pass the numbers and call the Temporal constructors directly. Point is this feels very heavyweight and non-performant for what it's doing.

Comment thread src/node_toml.cc
}

// Returns nullptr when a JavaScript exception is pending.
std::unique_ptr<::toml::node> BuildValue(Local<Value> value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to allocate everything twice, and strings get copied twice.

Comment thread src/node_toml.cc
: isolate_(isolate),
context_(context),
format_datetime_(format_datetime),
object_prototype_(Object::New(isolate)->GetPrototype()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will allocate a throw away object on every parse. Why is this necessary?

Comment thread src/node_toml.cc
static_cast<unsigned>(date.month),
static_cast<unsigned>(date.day));
out->append(buf);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snprintf and copy here just add unnecessary overhead. I would drop snprintf and write the digits by hand.

Something like... (had an agent whip this up quickly)

inline char* Write2(char* p, unsigned v) {
  p[0] = '0' + v / 10;
  p[1] = '0' + v % 10;
  return p + 2;
}

inline char* Write4(char* p, unsigned v) {
  p = Write2(p, v / 100);
  return Write2(p, v % 100);
}

char* WriteDate(char* p, const ::toml::date& d) {
  DCHECK_LE(d.year, 9999);  // TOML only allows 4-digit years
  p = Write4(p, d.year);
  *p++ = '-';
  p = Write2(p, d.month);
  *p++ = '-';
  return Write2(p, d.day);
}

char* WriteTime(char* p, const ::toml::time& t) {
  p = Write2(p, t.hour);
  *p++ = ':';
  p = Write2(p, t.minute);
  *p++ = ':';
  p = Write2(p, t.second);
  if (uint32_t ns = t.nanosecond) {
    *p++ = '.';
    int digits = 9;
    while (ns % 10 == 0) { ns /= 10; digits--; }
    for (int i = digits - 1; i >= 0; i--, ns /= 10) p[i] = '0' + ns % 10;
    p += digits;
  }
  return p;
}

But we really don't need the std::string either. Give Convert a char buf[48] on the stack, chain the writers, and hand OneByteString(isolate_, buf, end - buf) to CreateDatetime. No heap allocation, no scratch copy, and no snprintf.

That said, all of this still feels unnecessary. If we passed the numbers to JS and called the Temporal constructors directly, this formatting code and Temporal's re-parse could both go away.

@jasnell

jasnell commented Dec 2, 2026

Copy link
Copy Markdown
Member

Some performance related concerns, but consider them non-blocking for landing this PR. I'd like to see perf improvements and benchmarks landed before it graduates from experimental.

@jasnell jasnell added the experimental Issues and PRs related to experimental features. label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Issues and PRs related to experimental features. large-pr PRs subject to the large-PR policy. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants