smol-toml 1.3.0 → 1.3.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/dist/extract.d.ts CHANGED
@@ -26,4 +26,4 @@
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
  import { type TomlPrimitive } from './util.js';
29
- export declare function extractValue(str: string, ptr: number, end?: string): [TomlPrimitive, number];
29
+ export declare function extractValue(str: string, ptr: number, end: string | undefined, depth: number): [TomlPrimitive, number];
package/dist/extract.js CHANGED
@@ -50,12 +50,18 @@ function sliceAndTrimEndOf(str, startPtr, endPtr, allowNewLines) {
50
50
  }
51
51
  return [trimmed, commentIdx];
52
52
  }
53
- export function extractValue(str, ptr, end) {
53
+ export function extractValue(str, ptr, end, depth) {
54
+ if (depth === 0) {
55
+ throw new TomlError('document contains excessively nested structures. aborting.', {
56
+ toml: str,
57
+ ptr: ptr
58
+ });
59
+ }
54
60
  let c = str[ptr];
55
61
  if (c === '[' || c === '{') {
56
62
  let [value, endPtr] = c === '['
57
- ? parseArray(str, ptr)
58
- : parseInlineTable(str, ptr);
63
+ ? parseArray(str, ptr, depth)
64
+ : parseInlineTable(str, ptr, depth);
59
65
  let newPtr = skipUntil(str, endPtr, ',', end);
60
66
  if (end === '}') {
61
67
  let nextNewLine = indexOfNewline(str, endPtr, newPtr);
package/dist/index.cjs CHANGED
@@ -382,10 +382,16 @@ function sliceAndTrimEndOf(str, startPtr, endPtr, allowNewLines) {
382
382
  }
383
383
  return [trimmed, commentIdx];
384
384
  }
385
- function extractValue(str, ptr, end) {
385
+ function extractValue(str, ptr, end, depth) {
386
+ if (depth === 0) {
387
+ throw new TomlError("document contains excessively nested structures. aborting.", {
388
+ toml: str,
389
+ ptr
390
+ });
391
+ }
386
392
  let c = str[ptr];
387
393
  if (c === "[" || c === "{") {
388
- let [value, endPtr2] = c === "[" ? parseArray(str, ptr) : parseInlineTable(str, ptr);
394
+ let [value, endPtr2] = c === "[" ? parseArray(str, ptr, depth) : parseInlineTable(str, ptr, depth);
389
395
  let newPtr = skipUntil(str, endPtr2, ",", end);
390
396
  if (end === "}") {
391
397
  let nextNewLine = indexOfNewline(str, endPtr2, newPtr);
@@ -501,7 +507,7 @@ function parseKey(str, ptr, end = "=") {
501
507
  } while (dot + 1 && dot < endPtr);
502
508
  return [parsed, skipVoid(str, endPtr + 1, true, true)];
503
509
  }
504
- function parseInlineTable(str, ptr) {
510
+ function parseInlineTable(str, ptr, depth) {
505
511
  let res = {};
506
512
  let seen = /* @__PURE__ */ new Set();
507
513
  let c;
@@ -548,7 +554,7 @@ function parseInlineTable(str, ptr) {
548
554
  ptr
549
555
  });
550
556
  }
551
- let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}");
557
+ let [value, valueEndPtr] = extractValue(str, keyEndPtr, "}", depth - 1);
552
558
  seen.add(value);
553
559
  t[k] = value;
554
560
  ptr = valueEndPtr;
@@ -569,7 +575,7 @@ function parseInlineTable(str, ptr) {
569
575
  }
570
576
  return [res, ptr];
571
577
  }
572
- function parseArray(str, ptr) {
578
+ function parseArray(str, ptr, depth) {
573
579
  let res = [];
574
580
  let c;
575
581
  ptr++;
@@ -582,7 +588,7 @@ function parseArray(str, ptr) {
582
588
  } else if (c === "#")
583
589
  ptr = skipComment(str, ptr);
584
590
  else if (c !== " " && c !== " " && c !== "\n" && c !== "\r") {
585
- let e = extractValue(str, ptr - 1, "]");
591
+ let e = extractValue(str, ptr - 1, "]", depth - 1);
586
592
  res.push(e[0]);
587
593
  ptr = e[1];
588
594
  }
@@ -656,7 +662,8 @@ function peekTable(key, table, meta, type) {
656
662
  }
657
663
  return [k, t, state.c];
658
664
  }
659
- function parse(toml) {
665
+ function parse(toml, opts) {
666
+ let maxDepth = opts?.maxDepth ?? 1e3;
660
667
  let res = {};
661
668
  let meta = {};
662
669
  let tbl = res;
@@ -705,7 +712,7 @@ function parse(toml) {
705
712
  ptr
706
713
  });
707
714
  }
708
- let v = extractValue(toml, k[1]);
715
+ let v = extractValue(toml, k[1], void 0, maxDepth);
709
716
  p[1][p[0]] = v[0];
710
717
  ptr = v[1];
711
718
  }
@@ -743,7 +750,10 @@ function isArrayOfTables(obj) {
743
750
  function formatString(s) {
744
751
  return JSON.stringify(s).replace(/\x7f/g, "\\u007f");
745
752
  }
746
- function stringifyValue(val, type = extendedTypeOf(val)) {
753
+ function stringifyValue(val, type, depth) {
754
+ if (depth === 0) {
755
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
756
+ }
747
757
  if (type === "number") {
748
758
  if (isNaN(val))
749
759
  return "nan";
@@ -766,13 +776,13 @@ function stringifyValue(val, type = extendedTypeOf(val)) {
766
776
  return val.toISOString();
767
777
  }
768
778
  if (type === "object") {
769
- return stringifyInlineTable(val);
779
+ return stringifyInlineTable(val, depth);
770
780
  }
771
781
  if (type === "array") {
772
- return stringifyArray(val);
782
+ return stringifyArray(val, depth);
773
783
  }
774
784
  }
775
- function stringifyInlineTable(obj) {
785
+ function stringifyInlineTable(obj, depth) {
776
786
  let keys = Object.keys(obj);
777
787
  if (keys.length === 0)
778
788
  return "{}";
@@ -783,11 +793,11 @@ function stringifyInlineTable(obj) {
783
793
  res += ", ";
784
794
  res += BARE_KEY.test(k) ? k : formatString(k);
785
795
  res += " = ";
786
- res += stringifyValue(obj[k]);
796
+ res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1);
787
797
  }
788
798
  return res + " }";
789
799
  }
790
- function stringifyArray(array) {
800
+ function stringifyArray(array, depth) {
791
801
  if (array.length === 0)
792
802
  return "[]";
793
803
  let res = "[ ";
@@ -797,21 +807,27 @@ function stringifyArray(array) {
797
807
  if (array[i] === null || array[i] === void 0) {
798
808
  throw new TypeError("arrays cannot contain null or undefined values");
799
809
  }
800
- res += stringifyValue(array[i]);
810
+ res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1);
801
811
  }
802
812
  return res + " ]";
803
813
  }
804
- function stringifyArrayTable(array, key) {
814
+ function stringifyArrayTable(array, key, depth) {
815
+ if (depth === 0) {
816
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
817
+ }
805
818
  let res = "";
806
819
  for (let i = 0; i < array.length; i++) {
807
820
  res += `[[${key}]]
808
821
  `;
809
- res += stringifyTable(array[i], key);
822
+ res += stringifyTable(array[i], key, depth);
810
823
  res += "\n\n";
811
824
  }
812
825
  return res;
813
826
  }
814
- function stringifyTable(obj, prefix = "") {
827
+ function stringifyTable(obj, prefix, depth) {
828
+ if (depth === 0) {
829
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
830
+ }
815
831
  let preamble = "";
816
832
  let tables = "";
817
833
  let keys = Object.keys(obj);
@@ -824,17 +840,17 @@ function stringifyTable(obj, prefix = "") {
824
840
  }
825
841
  let key = BARE_KEY.test(k) ? k : formatString(k);
826
842
  if (type === "array" && isArrayOfTables(obj[k])) {
827
- tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key);
843
+ tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1);
828
844
  } else if (type === "object") {
829
845
  let tblKey = prefix ? `${prefix}.${key}` : key;
830
846
  tables += `[${tblKey}]
831
847
  `;
832
- tables += stringifyTable(obj[k], tblKey);
848
+ tables += stringifyTable(obj[k], tblKey, depth - 1);
833
849
  tables += "\n\n";
834
850
  } else {
835
851
  preamble += key;
836
852
  preamble += " = ";
837
- preamble += stringifyValue(obj[k], type);
853
+ preamble += stringifyValue(obj[k], type, depth);
838
854
  preamble += "\n";
839
855
  }
840
856
  }
@@ -842,11 +858,12 @@ function stringifyTable(obj, prefix = "") {
842
858
  return `${preamble}
843
859
  ${tables}`.trim();
844
860
  }
845
- function stringify(obj) {
861
+ function stringify(obj, opts) {
846
862
  if (extendedTypeOf(obj) !== "object") {
847
863
  throw new TypeError("stringify can only be called with an object");
848
864
  }
849
- return stringifyTable(obj);
865
+ let maxDepth = opts?.maxDepth ?? 1e3;
866
+ return stringifyTable(obj, "", maxDepth);
850
867
  }
851
868
 
852
869
  // dist/index.js
package/dist/parse.d.ts CHANGED
@@ -26,4 +26,6 @@
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
  import { type TomlPrimitive } from './util.js';
29
- export declare function parse(toml: string): Record<string, TomlPrimitive>;
29
+ export declare function parse(toml: string, opts?: {
30
+ maxDepth: number;
31
+ }): Record<string, TomlPrimitive>;
package/dist/parse.js CHANGED
@@ -93,7 +93,8 @@ function peekTable(key, table, meta, type) {
93
93
  }
94
94
  return [k, t, state.c];
95
95
  }
96
- export function parse(toml) {
96
+ export function parse(toml, opts) {
97
+ let maxDepth = opts?.maxDepth ?? 1000;
97
98
  let res = {};
98
99
  let meta = {};
99
100
  let tbl = res;
@@ -131,7 +132,7 @@ export function parse(toml) {
131
132
  ptr: ptr,
132
133
  });
133
134
  }
134
- let v = extractValue(toml, k[1]);
135
+ let v = extractValue(toml, k[1], void 0, maxDepth);
135
136
  p[1][p[0]] = v[0];
136
137
  ptr = v[1];
137
138
  }
@@ -25,4 +25,6 @@
25
25
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
- export declare function stringify(obj: any): string;
28
+ export declare function stringify(obj: any, opts?: {
29
+ maxDepth?: number;
30
+ }): string;
package/dist/stringify.js CHANGED
@@ -25,7 +25,7 @@
25
25
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
- const BARE_KEY = /^[a-z0-9-_]+$/i;
28
+ let BARE_KEY = /^[a-z0-9-_]+$/i;
29
29
  function extendedTypeOf(obj) {
30
30
  let type = typeof obj;
31
31
  if (type === 'object') {
@@ -46,7 +46,10 @@ function isArrayOfTables(obj) {
46
46
  function formatString(s) {
47
47
  return JSON.stringify(s).replace(/\x7f/g, '\\u007f');
48
48
  }
49
- function stringifyValue(val, type = extendedTypeOf(val)) {
49
+ function stringifyValue(val, type, depth) {
50
+ if (depth === 0) {
51
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
52
+ }
50
53
  if (type === 'number') {
51
54
  if (isNaN(val))
52
55
  return 'nan';
@@ -69,13 +72,13 @@ function stringifyValue(val, type = extendedTypeOf(val)) {
69
72
  return val.toISOString();
70
73
  }
71
74
  if (type === 'object') {
72
- return stringifyInlineTable(val);
75
+ return stringifyInlineTable(val, depth);
73
76
  }
74
77
  if (type === 'array') {
75
- return stringifyArray(val);
78
+ return stringifyArray(val, depth);
76
79
  }
77
80
  }
78
- function stringifyInlineTable(obj) {
81
+ function stringifyInlineTable(obj, depth) {
79
82
  let keys = Object.keys(obj);
80
83
  if (keys.length === 0)
81
84
  return '{}';
@@ -86,11 +89,11 @@ function stringifyInlineTable(obj) {
86
89
  res += ', ';
87
90
  res += BARE_KEY.test(k) ? k : formatString(k);
88
91
  res += ' = ';
89
- res += stringifyValue(obj[k]);
92
+ res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1);
90
93
  }
91
94
  return res + ' }';
92
95
  }
93
- function stringifyArray(array) {
96
+ function stringifyArray(array, depth) {
94
97
  if (array.length === 0)
95
98
  return '[]';
96
99
  let res = '[ ';
@@ -100,20 +103,26 @@ function stringifyArray(array) {
100
103
  if (array[i] === null || array[i] === void 0) {
101
104
  throw new TypeError('arrays cannot contain null or undefined values');
102
105
  }
103
- res += stringifyValue(array[i]);
106
+ res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1);
104
107
  }
105
108
  return res + ' ]';
106
109
  }
107
- function stringifyArrayTable(array, key) {
110
+ function stringifyArrayTable(array, key, depth) {
111
+ if (depth === 0) {
112
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
113
+ }
108
114
  let res = '';
109
115
  for (let i = 0; i < array.length; i++) {
110
116
  res += `[[${key}]]\n`;
111
- res += stringifyTable(array[i], key);
117
+ res += stringifyTable(array[i], key, depth);
112
118
  res += '\n\n';
113
119
  }
114
120
  return res;
115
121
  }
116
- function stringifyTable(obj, prefix = '') {
122
+ function stringifyTable(obj, prefix, depth) {
123
+ if (depth === 0) {
124
+ throw new Error("Could not stringify the object: maximum object depth exceeded");
125
+ }
117
126
  let preamble = '';
118
127
  let tables = '';
119
128
  let keys = Object.keys(obj);
@@ -126,27 +135,28 @@ function stringifyTable(obj, prefix = '') {
126
135
  }
127
136
  let key = BARE_KEY.test(k) ? k : formatString(k);
128
137
  if (type === 'array' && isArrayOfTables(obj[k])) {
129
- tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key);
138
+ tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1);
130
139
  }
131
140
  else if (type === 'object') {
132
141
  let tblKey = prefix ? `${prefix}.${key}` : key;
133
142
  tables += `[${tblKey}]\n`;
134
- tables += stringifyTable(obj[k], tblKey);
143
+ tables += stringifyTable(obj[k], tblKey, depth - 1);
135
144
  tables += '\n\n';
136
145
  }
137
146
  else {
138
147
  preamble += key;
139
148
  preamble += ' = ';
140
- preamble += stringifyValue(obj[k], type);
149
+ preamble += stringifyValue(obj[k], type, depth);
141
150
  preamble += '\n';
142
151
  }
143
152
  }
144
153
  }
145
154
  return `${preamble}\n${tables}`.trim();
146
155
  }
147
- export function stringify(obj) {
156
+ export function stringify(obj, opts) {
148
157
  if (extendedTypeOf(obj) !== 'object') {
149
158
  throw new TypeError('stringify can only be called with an object');
150
159
  }
151
- return stringifyTable(obj);
160
+ let maxDepth = opts?.maxDepth ?? 1000;
161
+ return stringifyTable(obj, '', maxDepth);
152
162
  }
package/dist/struct.d.ts CHANGED
@@ -27,5 +27,5 @@
27
27
  */
28
28
  import { type TomlPrimitive } from './util.js';
29
29
  export declare function parseKey(str: string, ptr: number, end?: string): [string[], number];
30
- export declare function parseInlineTable(str: string, ptr: number): [Record<string, TomlPrimitive>, number];
31
- export declare function parseArray(str: string, ptr: number): [TomlPrimitive[], number];
30
+ export declare function parseInlineTable(str: string, ptr: number, depth: number): [Record<string, TomlPrimitive>, number];
31
+ export declare function parseArray(str: string, ptr: number, depth: number): [TomlPrimitive[], number];
package/dist/struct.js CHANGED
@@ -102,7 +102,7 @@ export function parseKey(str, ptr, end = '=') {
102
102
  } while (dot + 1 && dot < endPtr);
103
103
  return [parsed, skipVoid(str, endPtr + 1, true, true)];
104
104
  }
105
- export function parseInlineTable(str, ptr) {
105
+ export function parseInlineTable(str, ptr, depth) {
106
106
  let res = {};
107
107
  let seen = new Set();
108
108
  let c;
@@ -152,7 +152,7 @@ export function parseInlineTable(str, ptr) {
152
152
  ptr: ptr
153
153
  });
154
154
  }
155
- let [value, valueEndPtr] = extractValue(str, keyEndPtr, '}');
155
+ let [value, valueEndPtr] = extractValue(str, keyEndPtr, '}', depth - 1);
156
156
  seen.add(value);
157
157
  t[k] = value;
158
158
  ptr = valueEndPtr;
@@ -173,7 +173,7 @@ export function parseInlineTable(str, ptr) {
173
173
  }
174
174
  return [res, ptr];
175
175
  }
176
- export function parseArray(str, ptr) {
176
+ export function parseArray(str, ptr, depth) {
177
177
  let res = [];
178
178
  let c;
179
179
  ptr++;
@@ -187,7 +187,7 @@ export function parseArray(str, ptr) {
187
187
  else if (c === '#')
188
188
  ptr = skipComment(str, ptr);
189
189
  else if (c !== ' ' && c !== '\t' && c !== '\n' && c !== '\r') {
190
- let e = extractValue(str, ptr - 1, ']');
190
+ let e = extractValue(str, ptr - 1, ']', depth - 1);
191
191
  res.push(e[0]);
192
192
  ptr = e[1];
193
193
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smol-toml",
3
3
  "license": "BSD-3-Clause",
4
- "version": "1.3.0",
4
+ "version": "1.3.1",
5
5
  "description": "A small, fast, and correct TOML parser/serializer",
6
6
  "author": "Cynthia <cyyynthia@borkenware.com>",
7
7
  "repository": "github:squirrelchat/smol-toml",
@@ -13,7 +13,7 @@
13
13
  "serializer"
14
14
  ],
15
15
  "type": "module",
16
- "packageManager": "pnpm@9.4.0",
16
+ "packageManager": "pnpm@9.12.3",
17
17
  "engines": {
18
18
  "node": ">= 18"
19
19
  },
@@ -28,14 +28,14 @@
28
28
  "devDependencies": {
29
29
  "@iarna/toml": "3.0.0",
30
30
  "@ltd/j-toml": "^1.38.0",
31
- "@tsconfig/node-lts": "^20.1.3",
31
+ "@tsconfig/node-lts": "^22.0.0",
32
32
  "@tsconfig/strictest": "^2.0.5",
33
- "@types/node": "^20.14.10",
34
- "@vitest/ui": "^2.0.3",
35
- "esbuild": "^0.23.0",
33
+ "@types/node": "^22.9.0",
34
+ "@vitest/ui": "^2.1.5",
35
+ "esbuild": "^0.24.0",
36
36
  "fast-toml": "^0.5.4",
37
- "typescript": "^5.5.3",
38
- "vitest": "^2.0.3"
37
+ "typescript": "^5.6.3",
38
+ "vitest": "^2.1.5"
39
39
  },
40
40
  "main": "./dist/index.cjs",
41
41
  "module": "./dist/index.js",