2.2.4 • Published 5 years ago

@tauri-apps/toml v2.2.4

Weekly downloads
1,014
License
ISC
Repository
github
Last release
5 years ago

@tauri-apps/toml

This is a fork of iarna-toml so that we can make some needed modifations for tauri.

Better TOML parsing and stringifying all in that familiar JSON interface.

Coverage Status

TOML 0.5.0

TOML Spec Support

The most recent version as of 2018-07-26: v0.5.0

Example

const TOML = require('@tauri-apps/toml')
const obj = TOML.parse(`[abc]
foo = 123
bar = [1,2,3]`)
/* obj =
{abc: {foo: 123, bar: [1,2,3]}}
*/
const str = TOML.stringify(obj)
/* str =
[abc]
foo = 123
bar = [ 1, 2, 3 ]
*/

Visit the project github for more examples!

Why @tauri-apps/toml

  • See TOML-SPEC-SUPPORT for a comparison of which TOML features are supported by the various Node.js TOML parsers.
  • BigInt support on Node 10!
  • 100% test coverage.
  • Faster parsing, even if you only use TOML 0.4.0, it's as much as 100 times faster than toml and 3 times faster than toml-j0.4. However a recent newcomer @ltd/j-toml has appeared with 0.5 support and astoundingly fast parsing speeds for large text blocks. All I can say is you'll have to test your specific work loads if you want to know which of @tauri-apps/toml and @ltd/j-toml is faster for you, as we currently excell in different areas
  • Careful adherence to spec. Tests go beyond simple coverage.
  • Smallest parser bundle (if you use @tauri-apps/toml/parse-string).
  • No deps.
  • Detailed and easy to read error messages‼
> TOML.parse(src)
Error: Unexpected character, expecting string, number, datetime, boolean, inline array or inline table at row 6, col 5, pos 87:
5: "abc\"" = { abc=123,def="abc" }
6> foo=sdkfj
       ^
7:

TOML.parse(str) → Object (example)

Also available with: require('@tauri-apps/toml/parse-string')

Synchronously parse a TOML string and return an object.

TOML.stringify(obj) → String (example)

Also available with: require('@tauri-apps/toml/stringify)

Serialize an object as TOML.

your-object.toJSON

If an object TOML.stringify is serializing has a toJSON method then it will call it to transform the object before serializing it. This matches the behavior of JSON.stringify.

The one exception to this is that toJSON is not called for Date objects because JSON represents dates as strings and TOML can represent them natively.

moment objects are treated the same as native Date objects, in this respect.

TOML.stringify.value(obj) -> String

Also available with: require('@tauri-apps/toml/stringify').value

Serialize a value as TOML would. This is a fragment and not a complete valid TOML document.

Promises and Streaming

The parser provides alternative async and streaming interfaces, for times that you're working with really absurdly big TOML files and don't want to tie-up the event loop while it parses.

TOML.parse.async(str, opts) → Promise(Object) (example)

Also available with: require('@tauri-apps/toml/parse-async')

opts.blocksize is the amount text to parser per pass through the event loop. Defaults to 40kb.

Asynchronously parse a TOML string and return a promise of the resulting object.

TOML.parse.stream(readable) → Promise(Object) (example)

Also available with: require('@tauri-apps/toml/parse-stream')

Given a readable stream, parse it as it feeds us data. Return a promise of the resulting object.

readable.pipe(TOML.parse.stream()) → Transform (example)

Also available with: require('@tauri-apps/toml/parse-stream')

Returns a transform stream in object mode. When it completes, emit the resulting object. Only one object will ever be emitted.

Lowlevel Interface (example) (example w/ parser debugging)

You construct a parser object, per TOML file you want to process:

const TOMLParser = require('@tauri-apps/toml/lib/toml-parser.js')
const parser = new TOMLParser()

Then you call the parse method for each chunk as you read them, or in a single call:

parser.parse(`hello = 'world'`)

And finally, you call the finish method to complete parsing and retrieve the resulting object.

const data = parser.finish()

Both the parse method and finish method will throw if they find a problem with the string they were given. Error objects thrown from the parser have pos, line and col attributes. TOML.parse adds a visual summary of where in the source string there were issues using parse-pretty-error and you can too:

const prettyError = require('./parse-pretty-error.js')
const newErr = prettyError(err, sourceString)

What's Different

Version 2 of this module supports TOML 0.5.0. Other modules currently published to the npm registry support 0.4.0. 0.5.0 is mostly backwards compatible with 0.4.0, but if you have need, you can install @tauri-apps/toml@1 to get a version of this module that supports 0.4.0. Please see the CHANGELOG for details on exactly whats changed.

TOML we can't do

  • -nan is a valid TOML value and is converted into NaN. There is no way to produce -nan when stringifying. Stringification will produce positive nan.
  • Detecting and erroring on invalid utf8 documents: This is because Node's UTF8 processing converts invalid sequences into the placeholder character and does not have facilities for reporting these as errors instead. We can detect the placeholder character, but it's valid to intentionally include them in documents, so erroring on them is not great.
  • On versions of Node < 10, very large Integer values will lose precision. On Node >=10, bigints are used.
  • Floating/local dates and times are still represented by JavaScript Date objects, which don't actually support these concepts. The objects returned have been modified so that you can determine what kind of thing they are (with isFloating, isDate, isTime properties) and that their ISO representation (via toISOString) is representative of their TOML value. They will correctly round trip if you pass them to TOML.stringify.
  • Binary, hexadecimal and octal values are converted to ordinary integers and will be decimal if you stringify them.

Changes

I write a by hand, honest-to-god, CHANGELOG for this project. It's a description of what went into a release that you the consumer of the module could care about, not a list of git commits, so please check it out!

Benchmarks

You can run them yourself with:

$ npm run benchmark

The results below are from my laptop using Node 11.10.0. The library versions tested were @tauri-apps/toml@2.2.2, toml-j0.4@1.1.1, toml@3.0.0, @sgarciac/bombadil@2.1.0 and @ltd/j-toml@0.5.47. The speed value is megabytes-per-second that the parser can process of that document type. Bigger is better. The percentage after average results is the margin of error.

As this table is getting a little wide, with how npm and github display it, you can also view it seperately in the BENCHMARK document.

@tauri-apps/tomltoml-j0.4toml@sgarciac/bombadil@ltd/j-toml
Overall25MB/sec0.55%7MB/sec1.39%0.2MB/sec3.47%--38MB/sec1.37%
Spec Example: v0.4.023MB/sec0.87%10MB/sec0.62%1MB/sec1.89%1.7MB/sec1.03%35MB/sec1.32%
Spec Example: Hard Unicode57MB/sec1.46%16MB/sec0.66%2MB/sec2.25%0.8MB/sec0.57%93MB/sec1.79%
Types: Array, Inline7.2MB/sec1.60%3.2MB/sec0.77%0.1MB/sec1.84%1.7MB/sec0.56%4.1MB/sec14.48%
Types: Array6.9MB/sec0.47%5.8MB/sec0.46%0.1MB/sec3.67%1.4MB/sec0.76%2.5MB/sec8.19%
Types: Boolean,22MB/sec0.85%8.5MB/sec0.55%0.2MB/sec1.83%2.1MB/sec1.29%5.6MB/sec0.58%
Types: Datetime18MB/sec0.56%11MB/sec0.80%0.3MB/sec1.55%0.8MB/sec0.51%4.5MB/sec0.66%
Types: Float9.2MB/sec0.71%5.2MB/sec1.12%0.3MB/sec2.04%2.6MB/sec0.86%3.7MB/sec0.61%
Types: Int6.4MB/sec0.44%3.9MB/sec0.56%0.1MB/sec1.65%1.7MB/sec1.15%1.5MB/sec4.06%
Types: Literal String, 7 char26MB/sec0.62%8.1MB/sec1.00%0.3MB/sec1.48%2.9MB/sec0.58%6MB/sec0.52%
Types: Literal String, 92 char41MB/sec0.80%11MB/sec1.20%0.4MB/sec2.38%15MB/sec0.84%23MB/sec0.58%
Types: Literal String, Multiline, 1079 char21MB/sec0.28%7.2MB/sec1.62%1.3MB/sec3.05%55MB/sec0.53%332MB/sec0.46%
Types: Basic String, 7 char26MB/sec0.56%6.6MB/sec0.61%0.2MB/sec4.70%2.7MB/sec0.68%3.3MB/sec0.47%
Types: Basic String, 92 char41MB/sec0.63%8MB/sec0.51%0.1MB/sec1.57%14MB/sec0.66%21MB/sec0.43%
Types: Basic String, 1079 char21MB/sec0.36%6MB/sec0.81%0.1MB/sec1.81%51MB/sec0.53%13MB/sec0.62%
Types: Table, Inline9.8MB/sec0.47%4.6MB/sec0.81%0.1MB/sec1.82%1.7MB/sec0.75%2.9MB/sec4.82%
Types: Table6.9MB/sec0.43%4.9MB/sec0.46%0.1MB/sec3.59%1.6MB/sec0.88%4.4MB/sec0.53%
Scaling: Array, Inline, 1000 elements33MB/sec2.15%2.5MB/sec1.07%0.1MB/sec3.57%1.8MB/sec0.64%8.7MB/sec4.12%
Scaling: Array, Nested, 1000 deep1.6MB/sec2.50%1.2MB/sec0.49%0.1MB/sec3.62%--1MB/sec3.79%
Scaling: Literal String, 40kb56MB/sec0.58%12MB/sec1.03%3.6MB/sec4.00%17MB/sec0.54%498MB/sec0.52%
Scaling: Literal String, Multiline, 40kb58MB/sec0.38%6.4MB/sec0.54%0.2MB/sec1.72%15MB/sec0.74%197MB/sec0.54%
Scaling: Basic String, Multiline, 40kb57MB/sec1.03%7.2MB/sec1.22%3.4MB/sec4.24%15MB/sec0.75%840MB/sec0.52%
Scaling: Basic String, 40kb57MB/sec0.43%8.6MB/sec0.57%0.2MB/sec1.71%17MB/sec0.51%394MB/sec0.54%
Scaling: Table, Inline, 1000 elements27MB/sec0.46%7.5MB/sec0.71%0.3MB/sec2.24%3MB/sec0.74%2.3MB/sec0.81%
Scaling: Table, Inline, Nested, 1000 deep7.8MB/sec0.61%4.3MB/sec0.83%0.1MB/sec2.93%--1.2MB/sec13.45%

Tests

The test suite is maintained at 100% coverage: Coverage Status

The spec was carefully hand converted into a series of test framework independent (and mostly language independent) assertions, as pairs of TOML and YAML files. You can find those files here: spec-test. A number of examples of invalid Unicode were also written, but are difficult to make use of in Node.js where Unicode errors are silently hidden. You can find those here: spec-test-disabled.

Further tests were written to increase coverage to 100%, these may be more implementation specific, but they can be found in coverage and coverage-error.

I've also written some quality assurance style tests, which don't contribute to coverage but do cover scenarios that could easily be problematic for some implementations can be found in: test/qa.js and test/qa-error.js.

All of the official example files from the TOML spec are run through this parser and compared to the official YAML files when available. These files are from the TOML spec as of: 357a4ba6 and specifically are:

The stringifier is tested by round-tripping these same files, asserting that TOML.parse(sourcefile) deepEqual TOML.parse(TOML.stringify(TOML.parse(sourcefile)). This is done in test/roundtrip-examples.js There are also some tests written to complete coverage from stringification in: test/stringify.js

Tests for the async and streaming interfaces are in test/async.js and test/stream.js respectively.

Tests for the parsers debugging mode live in test/devel.js.

And finally, many more stringification tests were borrowed from @othiym23's toml-stream module. They were fetched as of b6f1e26b572d49742d49fa6a6d11524d003441fa and live in test/toml-stream.

Improvements to make

  • In stringify:
    • Any way to produce comments. As a JSON stand-in I'm not too worried about this. That said, a document orientated fork is something I'd like to look at eventually…
    • Stringification could use some work on its error reporting. It reports what's wrong, but not where in your data structure it was.
  • Further optimize the parser:
    • There are some debugging assertions left in the main parser, these should be moved to a subclass.
    • Make the whole debugging parser thing work as a mixin instead of as a superclass.