toon-parser 1.0.1 → 1.1.1
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/LICENSE +21 -0
- package/README.md +151 -149
- package/dist/index.cjs +874 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -4
- package/dist/index.js.map +1 -1
- package/package.json +55 -35
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Branislav Lang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,149 +1,151 @@
|
|
|
1
|
-
# toon-parser
|
|
2
|
-
|
|
3
|
-
Safe JSON ⇆ TOON encoder/decoder with strict validation and prototype-pollution guards.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install toon-parser
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
- `
|
|
49
|
-
- `
|
|
50
|
-
- `
|
|
51
|
-
- `
|
|
52
|
-
- `
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
rows[
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
- **
|
|
145
|
-
- **
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
1
|
+
# toon-parser
|
|
2
|
+
|
|
3
|
+
Safe JSON ⇆ TOON encoder/decoder with strict validation and prototype-pollution guards.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install toon-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Note: this package supports both ESM and CommonJS consumers (CJS builds are available as `dist/index.cjs`). The package requires Node >= 18 per `engines` in `package.json`.
|
|
12
|
+
|
|
13
|
+
## Why this library?
|
|
14
|
+
|
|
15
|
+
- Implements the TOON v2.1 spec features most useful for JSON round-trips: tabular arrays, inline primitive arrays, nested objects/arrays, deterministic quoting.
|
|
16
|
+
- Hardened for untrusted input: prototype-pollution guards, max depth/length/node caps, strict length/width enforcement, and finite-number checks.
|
|
17
|
+
- No dynamic code execution; parsing uses explicit token scanning and bounded state to resist resource exhaustion.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { jsonToToon, toonToJson } from 'toon-parser';
|
|
23
|
+
|
|
24
|
+
const data = {
|
|
25
|
+
context: { task: 'hike planning', year: 2025 },
|
|
26
|
+
friends: ['ana', 'luis', 'sam'],
|
|
27
|
+
hikes: [
|
|
28
|
+
{ id: 1, name: 'Blue Lake', distanceKm: 7.5, wasSunny: true },
|
|
29
|
+
{ id: 2, name: 'Ridge Overlook', distanceKm: 9.2, wasSunny: false }
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const toon = jsonToToon(data);
|
|
34
|
+
// TOON text with tabular hikes array and inline primitive friends array
|
|
35
|
+
console.log(toon);
|
|
36
|
+
|
|
37
|
+
const roundTrip = toonToJson(toon);
|
|
38
|
+
console.log(roundTrip); // back to the original JSON object
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
### `jsonToToon(value, options?) => string`
|
|
44
|
+
|
|
45
|
+
Encodes a JSON-compatible value into TOON text.
|
|
46
|
+
|
|
47
|
+
Options:
|
|
48
|
+
- `indent` (number, default `2`): spaces per indentation level.
|
|
49
|
+
- `delimiter` (`,` | `|` | `\t`, default `,`): delimiter for inline arrays and tabular rows.
|
|
50
|
+
- `sortKeys` (boolean, default `false`): sort object keys alphabetically instead of preserving encounter order.
|
|
51
|
+
- `maxDepth` (number, default `64`): maximum nesting depth (objects + arrays).
|
|
52
|
+
- `maxArrayLength` (number, default `50_000`): maximum allowed array length.
|
|
53
|
+
- `maxTotalNodes` (number, default `250_000`): cap on processed fields/items to limit resource use.
|
|
54
|
+
- `disallowedKeys` (string[], default `["__proto__", "constructor", "prototype"]`): keys rejected to prevent prototype pollution.
|
|
55
|
+
|
|
56
|
+
Throws `ToonError` if limits are hit or input is not encodable.
|
|
57
|
+
|
|
58
|
+
### `toonToJson(text, options?) => unknown`
|
|
59
|
+
|
|
60
|
+
Decodes TOON text back to JSON data.
|
|
61
|
+
|
|
62
|
+
Options:
|
|
63
|
+
- `strict` (boolean, default `true`): enforce declared array lengths, tabular row widths, and indentation consistency.
|
|
64
|
+
- Same security options as `jsonToToon`: `maxDepth`, `maxArrayLength`, `maxTotalNodes`, `disallowedKeys`.
|
|
65
|
+
|
|
66
|
+
Throws `ToonError` with line numbers when parsing fails or security limits are exceeded.
|
|
67
|
+
|
|
68
|
+
## Usage examples
|
|
69
|
+
|
|
70
|
+
### Control indentation and delimiter
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
const toon = jsonToToon(data, { indent: 4, delimiter: '|' });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Detect and emit tabular arrays
|
|
77
|
+
|
|
78
|
+
Uniform arrays of objects with primitive values are emitted in TOON’s table form automatically:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const toon = jsonToToon({ rows: [{ a: 1, b: 'x' }, { a: 2, b: 'y' }] });
|
|
82
|
+
/*
|
|
83
|
+
rows[2]{a,b}:
|
|
84
|
+
1,x
|
|
85
|
+
2,y
|
|
86
|
+
*/
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Non-uniform arrays fall back to list form with `-` entries.
|
|
90
|
+
|
|
91
|
+
### Handling unsafe keys
|
|
92
|
+
|
|
93
|
+
Prototype-polluting keys are rejected:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
toonToJson('__proto__: 1'); // throws ToonError: Disallowed key "__proto__"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
You can extend the blocklist:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
toonToJson('danger: 1', { disallowedKeys: ['danger'] }); // throws
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Enforcing strictness
|
|
106
|
+
|
|
107
|
+
Strict mode (default) ensures array lengths match headers and tabular rows match declared widths:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
toonToJson('nums[2]: 1'); // throws ToonError: length mismatch
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Disable strictness if you need best-effort parsing:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const result = toonToJson('nums[2]: 1', { strict: false });
|
|
117
|
+
// result: { nums: [1] }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Security limits
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const opts = { maxDepth: 10, maxArrayLength: 1000, maxTotalNodes: 10_000 };
|
|
124
|
+
jsonToToon(bigValue, opts); // throws if exceeded
|
|
125
|
+
toonToJson(bigToonText, opts); // throws if exceeded
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Error handling
|
|
129
|
+
|
|
130
|
+
All validation/parsing errors throw `ToonError`. When applicable, the error message includes a line number:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
try {
|
|
134
|
+
toonToJson('nums[2]: 1');
|
|
135
|
+
} catch (err) {
|
|
136
|
+
if (err instanceof ToonError) {
|
|
137
|
+
console.error(err.message); // "Line 1: Inline array length mismatch..."
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Design choices
|
|
143
|
+
|
|
144
|
+
- **Tabular detection** follows the spec: all elements must be objects, share identical keys, and contain only primitives.
|
|
145
|
+
- **String quoting** follows deterministic rules (quote numeric-looking strings, leading/trailing space, colon, delimiter, backslash, brackets, control chars, or leading hyphen).
|
|
146
|
+
- **Finite numbers only**: `NaN`, `Infinity`, and `-Infinity` are rejected.
|
|
147
|
+
- **No implicit path expansion**: dotted keys stay literal (e.g., `a.b` remains a single key).
|
|
148
|
+
|
|
149
|
+
## Project status
|
|
150
|
+
|
|
151
|
+
This library targets TOON spec v2.1 core behaviors commonly needed for JSON round-trips. It prioritizes correctness and safety over permissiveness; loosen validation via `strict: false` only when you fully trust the input source.***
|