smol-toml 1.2.2 → 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/date.js +1 -5
- package/dist/error.js +1 -5
- package/dist/extract.d.ts +1 -1
- package/dist/extract.js +26 -24
- package/dist/index.cjs +904 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.js +6 -11
- package/dist/parse.d.ts +3 -1
- package/dist/parse.js +16 -19
- package/dist/primitive.js +18 -23
- package/dist/stringify.d.ts +3 -1
- package/dist/stringify.js +26 -20
- package/dist/struct.d.ts +2 -2
- package/dist/struct.js +30 -36
- package/dist/util.js +8 -16
- package/package.json +26 -16
package/dist/index.d.ts
CHANGED
|
@@ -25,8 +25,16 @@
|
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
import { parse } from './parse.js';
|
|
29
|
+
import { stringify } from './stringify.js';
|
|
30
|
+
import { TomlDate } from './date.js';
|
|
31
|
+
import { TomlError } from './error.js';
|
|
32
32
|
export type { TomlPrimitive } from './util.js';
|
|
33
|
+
declare const _default: {
|
|
34
|
+
parse: typeof parse;
|
|
35
|
+
stringify: typeof stringify;
|
|
36
|
+
TomlDate: typeof TomlDate;
|
|
37
|
+
TomlError: typeof TomlError;
|
|
38
|
+
};
|
|
39
|
+
export default _default;
|
|
40
|
+
export { parse, stringify, TomlDate, TomlError };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*!
|
|
3
2
|
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
4
3
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -26,13 +25,9 @@
|
|
|
26
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var parse_js_1 = require("./parse.js");
|
|
36
|
-
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_js_1.parse; } });
|
|
37
|
-
var stringify_js_1 = require("./stringify.js");
|
|
38
|
-
Object.defineProperty(exports, "stringify", { enumerable: true, get: function () { return stringify_js_1.stringify; } });
|
|
28
|
+
import { parse } from './parse.js';
|
|
29
|
+
import { stringify } from './stringify.js';
|
|
30
|
+
import { TomlDate } from './date.js';
|
|
31
|
+
import { TomlError } from './error.js';
|
|
32
|
+
export default { parse, stringify, TomlDate, TomlError };
|
|
33
|
+
export { parse, stringify, TomlDate, TomlError };
|
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
|
|
29
|
+
export declare function parse(toml: string, opts?: {
|
|
30
|
+
maxDepth: number;
|
|
31
|
+
}): Record<string, TomlPrimitive>;
|
package/dist/parse.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*!
|
|
3
2
|
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
4
3
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -26,12 +25,10 @@
|
|
|
26
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const util_js_1 = require("./util.js");
|
|
34
|
-
const error_js_1 = require("./error.js");
|
|
28
|
+
import { parseKey } from './struct.js';
|
|
29
|
+
import { extractValue } from './extract.js';
|
|
30
|
+
import { skipVoid } from './util.js';
|
|
31
|
+
import { TomlError } from './error.js';
|
|
35
32
|
function peekTable(key, table, meta, type) {
|
|
36
33
|
let t = table;
|
|
37
34
|
let m = meta;
|
|
@@ -96,18 +93,19 @@ function peekTable(key, table, meta, type) {
|
|
|
96
93
|
}
|
|
97
94
|
return [k, t, state.c];
|
|
98
95
|
}
|
|
99
|
-
function parse(toml) {
|
|
96
|
+
export function parse(toml, opts) {
|
|
97
|
+
let maxDepth = opts?.maxDepth ?? 1000;
|
|
100
98
|
let res = {};
|
|
101
99
|
let meta = {};
|
|
102
100
|
let tbl = res;
|
|
103
101
|
let m = meta;
|
|
104
|
-
for (let ptr =
|
|
102
|
+
for (let ptr = skipVoid(toml, 0); ptr < toml.length;) {
|
|
105
103
|
if (toml[ptr] === '[') {
|
|
106
104
|
let isTableArray = toml[++ptr] === '[';
|
|
107
|
-
let k =
|
|
105
|
+
let k = parseKey(toml, ptr += +isTableArray, ']');
|
|
108
106
|
if (isTableArray) {
|
|
109
107
|
if (toml[k[1] - 1] !== ']') {
|
|
110
|
-
throw new
|
|
108
|
+
throw new TomlError('expected end of table declaration', {
|
|
111
109
|
toml: toml,
|
|
112
110
|
ptr: k[1] - 1,
|
|
113
111
|
});
|
|
@@ -116,7 +114,7 @@ function parse(toml) {
|
|
|
116
114
|
}
|
|
117
115
|
let p = peekTable(k[0], res, meta, isTableArray ? 2 /* Type.ARRAY */ : 1 /* Type.EXPLICIT */);
|
|
118
116
|
if (!p) {
|
|
119
|
-
throw new
|
|
117
|
+
throw new TomlError('trying to redefine an already defined table or value', {
|
|
120
118
|
toml: toml,
|
|
121
119
|
ptr: ptr,
|
|
122
120
|
});
|
|
@@ -126,27 +124,26 @@ function parse(toml) {
|
|
|
126
124
|
ptr = k[1];
|
|
127
125
|
}
|
|
128
126
|
else {
|
|
129
|
-
let k =
|
|
127
|
+
let k = parseKey(toml, ptr);
|
|
130
128
|
let p = peekTable(k[0], tbl, m, 0 /* Type.DOTTED */);
|
|
131
129
|
if (!p) {
|
|
132
|
-
throw new
|
|
130
|
+
throw new TomlError('trying to redefine an already defined table or value', {
|
|
133
131
|
toml: toml,
|
|
134
132
|
ptr: ptr,
|
|
135
133
|
});
|
|
136
134
|
}
|
|
137
|
-
let v =
|
|
135
|
+
let v = extractValue(toml, k[1], void 0, maxDepth);
|
|
138
136
|
p[1][p[0]] = v[0];
|
|
139
137
|
ptr = v[1];
|
|
140
138
|
}
|
|
141
|
-
ptr =
|
|
139
|
+
ptr = skipVoid(toml, ptr, true);
|
|
142
140
|
if (toml[ptr] && toml[ptr] !== '\n' && toml[ptr] !== '\r') {
|
|
143
|
-
throw new
|
|
141
|
+
throw new TomlError('each key-value declaration must be followed by an end-of-line', {
|
|
144
142
|
toml: toml,
|
|
145
143
|
ptr: ptr
|
|
146
144
|
});
|
|
147
145
|
}
|
|
148
|
-
ptr =
|
|
146
|
+
ptr = skipVoid(toml, ptr);
|
|
149
147
|
}
|
|
150
148
|
return res;
|
|
151
149
|
}
|
|
152
|
-
exports.parse = parse;
|
package/dist/primitive.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*!
|
|
3
2
|
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
4
3
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -26,11 +25,9 @@
|
|
|
26
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const date_js_1 = require("./date.js");
|
|
33
|
-
const error_js_1 = require("./error.js");
|
|
28
|
+
import { skipVoid } from './util.js';
|
|
29
|
+
import { TomlDate } from './date.js';
|
|
30
|
+
import { TomlError } from './error.js';
|
|
34
31
|
let INT_REGEX = /^((0x[0-9a-fA-F](_?[0-9a-fA-F])*)|(([+-]|0[ob])?\d(_?\d)*))$/;
|
|
35
32
|
let FLOAT_REGEX = /^[+-]?\d(_?\d)*(\.\d(_?\d)*)?([eE][+-]?\d(_?\d)*)?$/;
|
|
36
33
|
let LEADING_ZERO = /^[+-]?0[0-9_]/;
|
|
@@ -44,7 +41,7 @@ let ESC_MAP = {
|
|
|
44
41
|
'"': '"',
|
|
45
42
|
'\\': '\\',
|
|
46
43
|
};
|
|
47
|
-
function parseString(str, ptr = 0, endPtr = str.length) {
|
|
44
|
+
export function parseString(str, ptr = 0, endPtr = str.length) {
|
|
48
45
|
let isLiteral = str[ptr] === "'";
|
|
49
46
|
let isMultiline = str[ptr++] === str[ptr] && str[ptr] === str[ptr + 1];
|
|
50
47
|
if (isMultiline) {
|
|
@@ -62,14 +59,14 @@ function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
62
59
|
let c = str[ptr++];
|
|
63
60
|
if (c === '\n' || (c === '\r' && str[ptr] === '\n')) {
|
|
64
61
|
if (!isMultiline) {
|
|
65
|
-
throw new
|
|
62
|
+
throw new TomlError('newlines are not allowed in strings', {
|
|
66
63
|
toml: str,
|
|
67
64
|
ptr: ptr - 1
|
|
68
65
|
});
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
else if ((c < '\x20' && c !== '\t') || c === '\x7f') {
|
|
72
|
-
throw new
|
|
69
|
+
throw new TomlError('control characters are not allowed in strings', {
|
|
73
70
|
toml: str,
|
|
74
71
|
ptr: ptr - 1
|
|
75
72
|
});
|
|
@@ -80,7 +77,7 @@ function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
80
77
|
// Unicode escape
|
|
81
78
|
let code = str.slice(ptr, (ptr += (c === 'u' ? 4 : 8)));
|
|
82
79
|
if (!ESCAPE_REGEX.test(code)) {
|
|
83
|
-
throw new
|
|
80
|
+
throw new TomlError('invalid unicode escape', {
|
|
84
81
|
toml: str,
|
|
85
82
|
ptr: tmp
|
|
86
83
|
});
|
|
@@ -89,7 +86,7 @@ function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
89
86
|
parsed += String.fromCodePoint(parseInt(code, 16));
|
|
90
87
|
}
|
|
91
88
|
catch {
|
|
92
|
-
throw new
|
|
89
|
+
throw new TomlError('invalid unicode escape', {
|
|
93
90
|
toml: str,
|
|
94
91
|
ptr: tmp
|
|
95
92
|
});
|
|
@@ -97,21 +94,21 @@ function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
97
94
|
}
|
|
98
95
|
else if (isMultiline && (c === '\n' || c === ' ' || c === '\t' || c === '\r')) {
|
|
99
96
|
// Multiline escape
|
|
100
|
-
ptr =
|
|
97
|
+
ptr = skipVoid(str, ptr - 1, true);
|
|
101
98
|
if (str[ptr] !== '\n' && str[ptr] !== '\r') {
|
|
102
|
-
throw new
|
|
99
|
+
throw new TomlError('invalid escape: only line-ending whitespace may be escaped', {
|
|
103
100
|
toml: str,
|
|
104
101
|
ptr: tmp
|
|
105
102
|
});
|
|
106
103
|
}
|
|
107
|
-
ptr =
|
|
104
|
+
ptr = skipVoid(str, ptr);
|
|
108
105
|
}
|
|
109
106
|
else if (c in ESC_MAP) {
|
|
110
107
|
// Classic escape
|
|
111
108
|
parsed += ESC_MAP[c];
|
|
112
109
|
}
|
|
113
110
|
else {
|
|
114
|
-
throw new
|
|
111
|
+
throw new TomlError('unrecognized escape sequence', {
|
|
115
112
|
toml: str,
|
|
116
113
|
ptr: tmp
|
|
117
114
|
});
|
|
@@ -126,8 +123,7 @@ function parseString(str, ptr = 0, endPtr = str.length) {
|
|
|
126
123
|
}
|
|
127
124
|
return parsed + str.slice(sliceStart, endPtr - 1);
|
|
128
125
|
}
|
|
129
|
-
|
|
130
|
-
function parseValue(value, toml, ptr) {
|
|
126
|
+
export function parseValue(value, toml, ptr) {
|
|
131
127
|
// Constant values
|
|
132
128
|
if (value === 'true')
|
|
133
129
|
return true;
|
|
@@ -145,33 +141,32 @@ function parseValue(value, toml, ptr) {
|
|
|
145
141
|
let isInt;
|
|
146
142
|
if ((isInt = INT_REGEX.test(value)) || FLOAT_REGEX.test(value)) {
|
|
147
143
|
if (LEADING_ZERO.test(value)) {
|
|
148
|
-
throw new
|
|
144
|
+
throw new TomlError('leading zeroes are not allowed', {
|
|
149
145
|
toml: toml,
|
|
150
146
|
ptr: ptr
|
|
151
147
|
});
|
|
152
148
|
}
|
|
153
149
|
let numeric = +(value.replace(/_/g, ''));
|
|
154
150
|
if (isNaN(numeric)) {
|
|
155
|
-
throw new
|
|
151
|
+
throw new TomlError('invalid number', {
|
|
156
152
|
toml: toml,
|
|
157
153
|
ptr: ptr
|
|
158
154
|
});
|
|
159
155
|
}
|
|
160
156
|
if (isInt && !Number.isSafeInteger(numeric)) {
|
|
161
|
-
throw new
|
|
157
|
+
throw new TomlError('integer value cannot be represented losslessly', {
|
|
162
158
|
toml: toml,
|
|
163
159
|
ptr: ptr
|
|
164
160
|
});
|
|
165
161
|
}
|
|
166
162
|
return numeric;
|
|
167
163
|
}
|
|
168
|
-
let date = new
|
|
164
|
+
let date = new TomlDate(value);
|
|
169
165
|
if (!date.isValid()) {
|
|
170
|
-
throw new
|
|
166
|
+
throw new TomlError('invalid value', {
|
|
171
167
|
toml: toml,
|
|
172
168
|
ptr: ptr
|
|
173
169
|
});
|
|
174
170
|
}
|
|
175
171
|
return date;
|
|
176
172
|
}
|
|
177
|
-
exports.parseValue = parseValue;
|
package/dist/stringify.d.ts
CHANGED
|
@@ -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
|
|
28
|
+
export declare function stringify(obj: any, opts?: {
|
|
29
|
+
maxDepth?: number;
|
|
30
|
+
}): string;
|
package/dist/stringify.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*!
|
|
3
2
|
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
4
3
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -26,9 +25,7 @@
|
|
|
26
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
exports.stringify = void 0;
|
|
31
|
-
const BARE_KEY = /^[a-z0-9-_]+$/i;
|
|
28
|
+
let BARE_KEY = /^[a-z0-9-_]+$/i;
|
|
32
29
|
function extendedTypeOf(obj) {
|
|
33
30
|
let type = typeof obj;
|
|
34
31
|
if (type === 'object') {
|
|
@@ -49,7 +46,10 @@ function isArrayOfTables(obj) {
|
|
|
49
46
|
function formatString(s) {
|
|
50
47
|
return JSON.stringify(s).replace(/\x7f/g, '\\u007f');
|
|
51
48
|
}
|
|
52
|
-
function stringifyValue(val, type
|
|
49
|
+
function stringifyValue(val, type, depth) {
|
|
50
|
+
if (depth === 0) {
|
|
51
|
+
throw new Error("Could not stringify the object: maximum object depth exceeded");
|
|
52
|
+
}
|
|
53
53
|
if (type === 'number') {
|
|
54
54
|
if (isNaN(val))
|
|
55
55
|
return 'nan';
|
|
@@ -72,13 +72,13 @@ function stringifyValue(val, type = extendedTypeOf(val)) {
|
|
|
72
72
|
return val.toISOString();
|
|
73
73
|
}
|
|
74
74
|
if (type === 'object') {
|
|
75
|
-
return stringifyInlineTable(val);
|
|
75
|
+
return stringifyInlineTable(val, depth);
|
|
76
76
|
}
|
|
77
77
|
if (type === 'array') {
|
|
78
|
-
return stringifyArray(val);
|
|
78
|
+
return stringifyArray(val, depth);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
function stringifyInlineTable(obj) {
|
|
81
|
+
function stringifyInlineTable(obj, depth) {
|
|
82
82
|
let keys = Object.keys(obj);
|
|
83
83
|
if (keys.length === 0)
|
|
84
84
|
return '{}';
|
|
@@ -89,11 +89,11 @@ function stringifyInlineTable(obj) {
|
|
|
89
89
|
res += ', ';
|
|
90
90
|
res += BARE_KEY.test(k) ? k : formatString(k);
|
|
91
91
|
res += ' = ';
|
|
92
|
-
res += stringifyValue(obj[k]);
|
|
92
|
+
res += stringifyValue(obj[k], extendedTypeOf(obj[k]), depth - 1);
|
|
93
93
|
}
|
|
94
94
|
return res + ' }';
|
|
95
95
|
}
|
|
96
|
-
function stringifyArray(array) {
|
|
96
|
+
function stringifyArray(array, depth) {
|
|
97
97
|
if (array.length === 0)
|
|
98
98
|
return '[]';
|
|
99
99
|
let res = '[ ';
|
|
@@ -103,20 +103,26 @@ function stringifyArray(array) {
|
|
|
103
103
|
if (array[i] === null || array[i] === void 0) {
|
|
104
104
|
throw new TypeError('arrays cannot contain null or undefined values');
|
|
105
105
|
}
|
|
106
|
-
res += stringifyValue(array[i]);
|
|
106
|
+
res += stringifyValue(array[i], extendedTypeOf(array[i]), depth - 1);
|
|
107
107
|
}
|
|
108
108
|
return res + ' ]';
|
|
109
109
|
}
|
|
110
|
-
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
|
+
}
|
|
111
114
|
let res = '';
|
|
112
115
|
for (let i = 0; i < array.length; i++) {
|
|
113
116
|
res += `[[${key}]]\n`;
|
|
114
|
-
res += stringifyTable(array[i], key);
|
|
117
|
+
res += stringifyTable(array[i], key, depth);
|
|
115
118
|
res += '\n\n';
|
|
116
119
|
}
|
|
117
120
|
return res;
|
|
118
121
|
}
|
|
119
|
-
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
|
+
}
|
|
120
126
|
let preamble = '';
|
|
121
127
|
let tables = '';
|
|
122
128
|
let keys = Object.keys(obj);
|
|
@@ -129,28 +135,28 @@ function stringifyTable(obj, prefix = '') {
|
|
|
129
135
|
}
|
|
130
136
|
let key = BARE_KEY.test(k) ? k : formatString(k);
|
|
131
137
|
if (type === 'array' && isArrayOfTables(obj[k])) {
|
|
132
|
-
tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key);
|
|
138
|
+
tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1);
|
|
133
139
|
}
|
|
134
140
|
else if (type === 'object') {
|
|
135
141
|
let tblKey = prefix ? `${prefix}.${key}` : key;
|
|
136
142
|
tables += `[${tblKey}]\n`;
|
|
137
|
-
tables += stringifyTable(obj[k], tblKey);
|
|
143
|
+
tables += stringifyTable(obj[k], tblKey, depth - 1);
|
|
138
144
|
tables += '\n\n';
|
|
139
145
|
}
|
|
140
146
|
else {
|
|
141
147
|
preamble += key;
|
|
142
148
|
preamble += ' = ';
|
|
143
|
-
preamble += stringifyValue(obj[k], type);
|
|
149
|
+
preamble += stringifyValue(obj[k], type, depth);
|
|
144
150
|
preamble += '\n';
|
|
145
151
|
}
|
|
146
152
|
}
|
|
147
153
|
}
|
|
148
154
|
return `${preamble}\n${tables}`.trim();
|
|
149
155
|
}
|
|
150
|
-
function stringify(obj) {
|
|
156
|
+
export function stringify(obj, opts) {
|
|
151
157
|
if (extendedTypeOf(obj) !== 'object') {
|
|
152
158
|
throw new TypeError('stringify can only be called with an object');
|
|
153
159
|
}
|
|
154
|
-
|
|
160
|
+
let maxDepth = opts?.maxDepth ?? 1000;
|
|
161
|
+
return stringifyTable(obj, '', maxDepth);
|
|
155
162
|
}
|
|
156
|
-
exports.stringify = stringify;
|
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
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*!
|
|
3
2
|
* Copyright (c) Squirrel Chat et al., All rights reserved.
|
|
4
3
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
@@ -26,19 +25,17 @@
|
|
|
26
25
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
26
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
27
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const util_js_1 = require("./util.js");
|
|
34
|
-
const error_js_1 = require("./error.js");
|
|
28
|
+
import { parseString } from './primitive.js';
|
|
29
|
+
import { extractValue } from './extract.js';
|
|
30
|
+
import { skipComment, indexOfNewline, getStringEnd, skipVoid } from './util.js';
|
|
31
|
+
import { TomlError } from './error.js';
|
|
35
32
|
let KEY_PART_RE = /^[a-zA-Z0-9-_]+[ \t]*$/;
|
|
36
|
-
function parseKey(str, ptr, end = '=') {
|
|
33
|
+
export function parseKey(str, ptr, end = '=') {
|
|
37
34
|
let dot = ptr - 1;
|
|
38
35
|
let parsed = [];
|
|
39
36
|
let endPtr = str.indexOf(end, ptr);
|
|
40
37
|
if (endPtr < 0) {
|
|
41
|
-
throw new
|
|
38
|
+
throw new TomlError('incomplete key-value: cannot find end of key', {
|
|
42
39
|
toml: str,
|
|
43
40
|
ptr: ptr
|
|
44
41
|
});
|
|
@@ -50,29 +47,29 @@ function parseKey(str, ptr, end = '=') {
|
|
|
50
47
|
// If it's a string
|
|
51
48
|
if (c === '"' || c === "'") {
|
|
52
49
|
if (c === str[ptr + 1] && c === str[ptr + 2]) {
|
|
53
|
-
throw new
|
|
50
|
+
throw new TomlError('multiline strings are not allowed in keys', {
|
|
54
51
|
toml: str,
|
|
55
52
|
ptr: ptr,
|
|
56
53
|
});
|
|
57
54
|
}
|
|
58
|
-
let eos =
|
|
55
|
+
let eos = getStringEnd(str, ptr);
|
|
59
56
|
if (eos < 0) {
|
|
60
|
-
throw new
|
|
57
|
+
throw new TomlError('unfinished string encountered', {
|
|
61
58
|
toml: str,
|
|
62
59
|
ptr: ptr,
|
|
63
60
|
});
|
|
64
61
|
}
|
|
65
62
|
dot = str.indexOf('.', eos);
|
|
66
63
|
let strEnd = str.slice(eos, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
67
|
-
let newLine =
|
|
64
|
+
let newLine = indexOfNewline(strEnd);
|
|
68
65
|
if (newLine > -1) {
|
|
69
|
-
throw new
|
|
66
|
+
throw new TomlError('newlines are not allowed in keys', {
|
|
70
67
|
toml: str,
|
|
71
68
|
ptr: ptr + dot + newLine,
|
|
72
69
|
});
|
|
73
70
|
}
|
|
74
71
|
if (strEnd.trimStart()) {
|
|
75
|
-
throw new
|
|
72
|
+
throw new TomlError('found extra tokens after the string part', {
|
|
76
73
|
toml: str,
|
|
77
74
|
ptr: eos,
|
|
78
75
|
});
|
|
@@ -80,20 +77,20 @@ function parseKey(str, ptr, end = '=') {
|
|
|
80
77
|
if (endPtr < eos) {
|
|
81
78
|
endPtr = str.indexOf(end, eos);
|
|
82
79
|
if (endPtr < 0) {
|
|
83
|
-
throw new
|
|
80
|
+
throw new TomlError('incomplete key-value: cannot find end of key', {
|
|
84
81
|
toml: str,
|
|
85
82
|
ptr: ptr,
|
|
86
83
|
});
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
|
-
parsed.push(
|
|
86
|
+
parsed.push(parseString(str, ptr, eos));
|
|
90
87
|
}
|
|
91
88
|
else {
|
|
92
89
|
// Normal raw key part consumption and validation
|
|
93
90
|
dot = str.indexOf('.', ptr);
|
|
94
91
|
let part = str.slice(ptr, dot < 0 || dot > endPtr ? endPtr : dot);
|
|
95
92
|
if (!KEY_PART_RE.test(part)) {
|
|
96
|
-
throw new
|
|
93
|
+
throw new TomlError('only letter, numbers, dashes and underscores are allowed in keys', {
|
|
97
94
|
toml: str,
|
|
98
95
|
ptr: ptr,
|
|
99
96
|
});
|
|
@@ -103,10 +100,9 @@ function parseKey(str, ptr, end = '=') {
|
|
|
103
100
|
}
|
|
104
101
|
// Until there's no more dot
|
|
105
102
|
} while (dot + 1 && dot < endPtr);
|
|
106
|
-
return [parsed,
|
|
103
|
+
return [parsed, skipVoid(str, endPtr + 1, true, true)];
|
|
107
104
|
}
|
|
108
|
-
|
|
109
|
-
function parseInlineTable(str, ptr) {
|
|
105
|
+
export function parseInlineTable(str, ptr, depth) {
|
|
110
106
|
let res = {};
|
|
111
107
|
let seen = new Set();
|
|
112
108
|
let c;
|
|
@@ -114,19 +110,19 @@ function parseInlineTable(str, ptr) {
|
|
|
114
110
|
ptr++;
|
|
115
111
|
while ((c = str[ptr++]) !== '}' && c) {
|
|
116
112
|
if (c === '\n') {
|
|
117
|
-
throw new
|
|
113
|
+
throw new TomlError('newlines are not allowed in inline tables', {
|
|
118
114
|
toml: str,
|
|
119
115
|
ptr: ptr - 1
|
|
120
116
|
});
|
|
121
117
|
}
|
|
122
118
|
else if (c === '#') {
|
|
123
|
-
throw new
|
|
119
|
+
throw new TomlError('inline tables cannot contain comments', {
|
|
124
120
|
toml: str,
|
|
125
121
|
ptr: ptr - 1
|
|
126
122
|
});
|
|
127
123
|
}
|
|
128
124
|
else if (c === ',') {
|
|
129
|
-
throw new
|
|
125
|
+
throw new TomlError('expected key-value, found comma', {
|
|
130
126
|
toml: str,
|
|
131
127
|
ptr: ptr - 1
|
|
132
128
|
});
|
|
@@ -141,7 +137,7 @@ function parseInlineTable(str, ptr) {
|
|
|
141
137
|
t = hasOwn ? t[k] : (t[k] = {});
|
|
142
138
|
k = key[i];
|
|
143
139
|
if ((hasOwn = Object.hasOwn(t, k)) && (typeof t[k] !== 'object' || seen.has(t[k]))) {
|
|
144
|
-
throw new
|
|
140
|
+
throw new TomlError('trying to redefine an already defined value', {
|
|
145
141
|
toml: str,
|
|
146
142
|
ptr: ptr
|
|
147
143
|
});
|
|
@@ -151,12 +147,12 @@ function parseInlineTable(str, ptr) {
|
|
|
151
147
|
}
|
|
152
148
|
}
|
|
153
149
|
if (hasOwn) {
|
|
154
|
-
throw new
|
|
150
|
+
throw new TomlError('trying to redefine an already defined value', {
|
|
155
151
|
toml: str,
|
|
156
152
|
ptr: ptr
|
|
157
153
|
});
|
|
158
154
|
}
|
|
159
|
-
let [value, valueEndPtr] =
|
|
155
|
+
let [value, valueEndPtr] = extractValue(str, keyEndPtr, '}', depth - 1);
|
|
160
156
|
seen.add(value);
|
|
161
157
|
t[k] = value;
|
|
162
158
|
ptr = valueEndPtr;
|
|
@@ -164,45 +160,43 @@ function parseInlineTable(str, ptr) {
|
|
|
164
160
|
}
|
|
165
161
|
}
|
|
166
162
|
if (comma) {
|
|
167
|
-
throw new
|
|
163
|
+
throw new TomlError('trailing commas are not allowed in inline tables', {
|
|
168
164
|
toml: str,
|
|
169
165
|
ptr: comma
|
|
170
166
|
});
|
|
171
167
|
}
|
|
172
168
|
if (!c) {
|
|
173
|
-
throw new
|
|
169
|
+
throw new TomlError('unfinished table encountered', {
|
|
174
170
|
toml: str,
|
|
175
171
|
ptr: ptr
|
|
176
172
|
});
|
|
177
173
|
}
|
|
178
174
|
return [res, ptr];
|
|
179
175
|
}
|
|
180
|
-
|
|
181
|
-
function parseArray(str, ptr) {
|
|
176
|
+
export function parseArray(str, ptr, depth) {
|
|
182
177
|
let res = [];
|
|
183
178
|
let c;
|
|
184
179
|
ptr++;
|
|
185
180
|
while ((c = str[ptr++]) !== ']' && c) {
|
|
186
181
|
if (c === ',') {
|
|
187
|
-
throw new
|
|
182
|
+
throw new TomlError('expected value, found comma', {
|
|
188
183
|
toml: str,
|
|
189
184
|
ptr: ptr - 1
|
|
190
185
|
});
|
|
191
186
|
}
|
|
192
187
|
else if (c === '#')
|
|
193
|
-
ptr =
|
|
188
|
+
ptr = skipComment(str, ptr);
|
|
194
189
|
else if (c !== ' ' && c !== '\t' && c !== '\n' && c !== '\r') {
|
|
195
|
-
let e =
|
|
190
|
+
let e = extractValue(str, ptr - 1, ']', depth - 1);
|
|
196
191
|
res.push(e[0]);
|
|
197
192
|
ptr = e[1];
|
|
198
193
|
}
|
|
199
194
|
}
|
|
200
195
|
if (!c) {
|
|
201
|
-
throw new
|
|
196
|
+
throw new TomlError('unfinished array encountered', {
|
|
202
197
|
toml: str,
|
|
203
198
|
ptr: ptr
|
|
204
199
|
});
|
|
205
200
|
}
|
|
206
201
|
return [res, ptr];
|
|
207
202
|
}
|
|
208
|
-
exports.parseArray = parseArray;
|