@blamejs/core 0.7.1 → 0.7.18
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/CHANGELOG.md +423 -389
- package/README.md +150 -149
- package/bin/blamejs.js +0 -0
- package/index.js +308 -282
- package/lib/api-key.js +660 -672
- package/lib/api-snapshot.js +338 -338
- package/lib/app-shutdown.js +385 -385
- package/lib/app.js +365 -365
- package/lib/archive.js +250 -250
- package/lib/atomic-file.js +544 -544
- package/lib/audit-chain.js +177 -177
- package/lib/audit-sign.js +344 -344
- package/lib/audit-tools.js +677 -677
- package/lib/audit.js +766 -766
- package/lib/auth/jwt.js +311 -311
- package/lib/auth/lockout.js +436 -436
- package/lib/auth/oauth.js +721 -721
- package/lib/auth/passkey.js +181 -181
- package/lib/auth/password.js +594 -594
- package/lib/backup/bundle.js +217 -217
- package/lib/backup/crypto.js +176 -176
- package/lib/backup/index.js +515 -515
- package/lib/backup/manifest.js +282 -282
- package/lib/break-glass.js +1338 -1338
- package/lib/bundler.js +441 -441
- package/lib/cache-redis.js +256 -266
- package/lib/cache.js +1206 -1211
- package/lib/canonical-json.js +115 -115
- package/lib/chain-writer.js +234 -234
- package/lib/cli-helpers.js +206 -206
- package/lib/cli.js +2334 -2334
- package/lib/cluster-provider-db.js +317 -317
- package/lib/cluster-storage.js +226 -226
- package/lib/cluster.js +703 -703
- package/lib/codepoint-class.js +196 -0
- package/lib/config-drift.js +301 -301
- package/lib/consent.js +222 -222
- package/lib/constants.js +191 -191
- package/lib/cookies.js +315 -315
- package/lib/credential-hash.js +322 -322
- package/lib/crypto.js +266 -266
- package/lib/csv.js +275 -286
- package/lib/db-declare-row-policy.js +267 -267
- package/lib/db-declare-view.js +420 -421
- package/lib/db-query.js +406 -406
- package/lib/db-schema.js +319 -319
- package/lib/db.js +1288 -1288
- package/lib/deprecate.js +222 -222
- package/lib/dev.js +335 -335
- package/lib/dual-control.js +473 -473
- package/lib/error-page.js +420 -420
- package/lib/external-db-migrate.js +441 -441
- package/lib/external-db.js +1061 -1061
- package/lib/file-type.js +273 -273
- package/lib/file-upload.js +1136 -0
- package/lib/forms.js +422 -422
- package/lib/framework-error.js +293 -202
- package/lib/framework-schema.js +717 -717
- package/lib/gate-contract.js +971 -0
- package/lib/guard-all.js +405 -0
- package/lib/guard-archive.js +739 -0
- package/lib/guard-csv.js +816 -0
- package/lib/guard-email.js +744 -0
- package/lib/guard-filename.js +724 -0
- package/lib/guard-html.js +976 -0
- package/lib/guard-json.js +729 -0
- package/lib/guard-markdown.js +586 -0
- package/lib/guard-svg.js +976 -0
- package/lib/guard-xml.js +405 -0
- package/lib/guard-yaml.js +529 -0
- package/lib/handlers.js +350 -350
- package/lib/http-client-cookie-jar.js +508 -508
- package/lib/http-client.js +1195 -1195
- package/lib/i18n.js +878 -878
- package/lib/jobs.js +185 -185
- package/lib/log-stream-cloudwatch.js +369 -369
- package/lib/log-stream-local.js +146 -146
- package/lib/log-stream-otlp-grpc.js +410 -410
- package/lib/log-stream-otlp.js +286 -286
- package/lib/log-stream-syslog.js +302 -302
- package/lib/log-stream-webhook.js +199 -199
- package/lib/log-stream.js +330 -330
- package/lib/log.js +500 -500
- package/lib/mail-bounce.js +528 -528
- package/lib/mail-dkim.js +369 -362
- package/lib/mail.js +981 -962
- package/lib/metrics.js +683 -683
- package/lib/middleware/api-encrypt.js +936 -573
- package/lib/middleware/attach-user.js +157 -157
- package/lib/middleware/body-parser.js +1170 -1091
- package/lib/middleware/bot-guard.js +178 -178
- package/lib/middleware/compression.js +452 -452
- package/lib/middleware/cors.js +314 -314
- package/lib/middleware/csp-nonce.js +348 -348
- package/lib/middleware/csrf-protect.js +316 -316
- package/lib/middleware/db-role-for.js +264 -269
- package/lib/middleware/health.js +392 -392
- package/lib/middleware/index.js +79 -79
- package/lib/middleware/rate-limit.js +358 -358
- package/lib/middleware/request-id.js +61 -61
- package/lib/middleware/request-log.js +168 -168
- package/lib/middleware/require-auth.js +104 -104
- package/lib/middleware/security-headers.js +116 -116
- package/lib/middleware/sse.js +166 -166
- package/lib/migrations.js +383 -383
- package/lib/mtls-ca.js +518 -518
- package/lib/mtls-engine-default.js +481 -481
- package/lib/network-dns.js +632 -632
- package/lib/network-heartbeat.js +290 -290
- package/lib/network-nts.js +574 -574
- package/lib/network-proxy.js +265 -265
- package/lib/network-tls.js +328 -328
- package/lib/network.js +233 -233
- package/lib/notify.js +612 -614
- package/lib/ntp-check.js +229 -229
- package/lib/numeric-bounds.js +111 -91
- package/lib/object-store/azure-blob-bucket-ops.js +349 -349
- package/lib/object-store/azure-blob.js +488 -451
- package/lib/object-store/gcs-bucket-ops.js +351 -351
- package/lib/object-store/gcs.js +519 -479
- package/lib/object-store/http-put.js +153 -153
- package/lib/object-store/index.js +197 -197
- package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
- package/lib/object-store/sigv4.js +903 -855
- package/lib/observability.js +151 -151
- package/lib/otel-export.js +269 -269
- package/lib/pagination.js +464 -464
- package/lib/parsers/index.js +80 -80
- package/lib/parsers/safe-env.js +642 -642
- package/lib/parsers/safe-ini.js +292 -292
- package/lib/parsers/safe-toml.js +784 -784
- package/lib/parsers/safe-xml.js +390 -390
- package/lib/parsers/safe-yaml.js +1015 -1015
- package/lib/permissions.js +708 -708
- package/lib/pqc-agent.js +87 -87
- package/lib/pqc-gate.js +279 -279
- package/lib/protobuf-encoder.js +190 -190
- package/lib/protocol-dispatcher.js +161 -161
- package/lib/pubsub-redis.js +167 -177
- package/lib/pubsub.js +429 -429
- package/lib/queue-local.js +476 -476
- package/lib/queue-redis.js +745 -752
- package/lib/queue-sqs.js +319 -319
- package/lib/queue.js +695 -695
- package/lib/redis-client.js +519 -489
- package/lib/request-helpers.js +340 -336
- package/lib/restore-bundle.js +237 -237
- package/lib/restore-rollback.js +259 -259
- package/lib/restore.js +409 -409
- package/lib/retry.js +376 -376
- package/lib/router.js +748 -748
- package/lib/safe-async.js +735 -735
- package/lib/safe-buffer.js +237 -237
- package/lib/safe-json.js +541 -541
- package/lib/safe-schema.js +1266 -1266
- package/lib/safe-url.js +159 -159
- package/lib/scheduler.js +706 -706
- package/lib/security-assert.js +373 -373
- package/lib/seeders.js +618 -630
- package/lib/session.js +478 -478
- package/lib/slug.js +269 -269
- package/lib/ssrf-guard.js +401 -401
- package/lib/static.js +879 -114
- package/lib/storage.js +471 -471
- package/lib/subject.js +281 -281
- package/lib/template.js +791 -791
- package/lib/testing.js +798 -798
- package/lib/time.js +310 -310
- package/lib/totp.js +302 -302
- package/lib/tracing.js +494 -494
- package/lib/uuid.js +132 -132
- package/lib/validate-opts.js +340 -270
- package/lib/vault/index.js +308 -308
- package/lib/vault/rotate.js +784 -784
- package/lib/vault/wrap.js +296 -296
- package/lib/vendor/noble-ciphers.cjs +9 -9
- package/lib/webhook.js +595 -598
- package/lib/websocket.js +1048 -1048
- package/package.json +77 -77
- package/sbom.cyclonedx.json +7 -7
package/lib/parsers/safe-toml.js
CHANGED
|
@@ -1,784 +1,784 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Security-focused TOML 1.0 parser.
|
|
4
|
-
*
|
|
5
|
-
* Implements TOML 1.0 (toml.io/en/v1.0.0) with the same defaults the
|
|
6
|
-
* framework's other parsers apply:
|
|
7
|
-
*
|
|
8
|
-
* - Size + depth + element-count limits (DoS prevention)
|
|
9
|
-
* - BOM stripping
|
|
10
|
-
* - Prototype-pollution rejection (__proto__ / constructor / prototype
|
|
11
|
-
* forbidden as bare keys, quoted keys, or dotted-key path segments)
|
|
12
|
-
* - Strict same-key redefinition rules per TOML spec (silent overwrite
|
|
13
|
-
* would mask config errors operators DO want surfaced)
|
|
14
|
-
*
|
|
15
|
-
* Output shape:
|
|
16
|
-
* - Strings: JS string (escape-decoded for basic/multi-line basic;
|
|
17
|
-
* verbatim for literal/multi-line literal)
|
|
18
|
-
* - Integers: JS Number when |n| <= Number.MAX_SAFE_INTEGER; throws
|
|
19
|
-
* toml/integer-overflow otherwise (operators get a clear failure
|
|
20
|
-
* instead of silent precision loss). Operators who legitimately
|
|
21
|
-
* need 64-bit integers should encode them as quoted strings.
|
|
22
|
-
* - Floats: JS Number; nan / inf / -inf preserved as JS NaN / Infinity
|
|
23
|
-
* - Booleans: true / false
|
|
24
|
-
* - Offset Date-Time: JS Date
|
|
25
|
-
* - Local Date-Time / Local Date / Local Time: ISO-formatted string
|
|
26
|
-
* (no implicit offset assumption)
|
|
27
|
-
* - Arrays: JS arrays
|
|
28
|
-
* - Tables / inline tables: plain JS objects
|
|
29
|
-
*
|
|
30
|
-
* What is REJECTED:
|
|
31
|
-
* - Same-key reassignment inside any table
|
|
32
|
-
* - Inline-table mutation (`x = { a = 1 }\nx.b = 2` is an error)
|
|
33
|
-
* - Defining a table whose name collides with an existing key
|
|
34
|
-
* - __proto__ / constructor / prototype as key names anywhere
|
|
35
|
-
* - Unterminated strings, arrays, or inline tables
|
|
36
|
-
*
|
|
37
|
-
* Public API:
|
|
38
|
-
* toml.parse(input, opts?) object | throws SafeTomlError
|
|
39
|
-
* toml.SafeTomlError error class
|
|
40
|
-
*
|
|
41
|
-
* Defaults:
|
|
42
|
-
* maxBytes: 1 MiB
|
|
43
|
-
* maxDepth: 100
|
|
44
|
-
* maxKeys: 50000
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
var C = require("../constants");
|
|
48
|
-
var numericBounds = require("../numeric-bounds");
|
|
49
|
-
var safeBuffer = require("../safe-buffer");
|
|
50
|
-
var { FrameworkError } = require("../framework-error");
|
|
51
|
-
|
|
52
|
-
class SafeTomlError extends FrameworkError {
|
|
53
|
-
constructor(message, code, line, col) {
|
|
54
|
-
super(line != null ? message + " at line " + line + ":" + col : message);
|
|
55
|
-
this.name = "SafeTomlError";
|
|
56
|
-
this.code = code || "toml/invalid";
|
|
57
|
-
this.line = line == null ? null : line;
|
|
58
|
-
this.col = col == null ? null : col;
|
|
59
|
-
this.isSafeTomlError = true;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// parseInt radices and TOML radix-prefix tokens — naming these constants
|
|
64
|
-
// keeps the literal `8` / `16` byte-shaped numbers off the call sites.
|
|
65
|
-
var RADIX_BIN = 0x2;
|
|
66
|
-
var RADIX_OCTAL = 0x8;
|
|
67
|
-
var RADIX_HEX = 0x10;
|
|
68
|
-
|
|
69
|
-
// Date-time literal character widths (per TOML / RFC 3339).
|
|
70
|
-
var TIME_CHARS = 0x8; // "HH:MM:SS"
|
|
71
|
-
var OFFSET_CHARS = 0x6; // "+HH:MM"
|
|
72
|
-
|
|
73
|
-
var DEFAULTS = {
|
|
74
|
-
maxBytes: C.BYTES.mib(1),
|
|
75
|
-
maxDepth: 100,
|
|
76
|
-
maxKeys: 50_000,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
var POISONED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
80
|
-
|
|
81
|
-
function parse(input, opts) {
|
|
82
|
-
opts = opts || {};
|
|
83
|
-
// Validate operator-supplied numeric opts via lib/numeric-bounds —
|
|
84
|
-
// Infinity / NaN / negative / non-integer all bypass the `> 0` shape
|
|
85
|
-
// and silently lift the DoS cap they were meant to enforce.
|
|
86
|
-
if (opts.maxBytes !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxBytes)) {
|
|
87
|
-
throw new SafeTomlError("toml.parse: maxBytes must be a positive finite integer; got " +
|
|
88
|
-
numericBounds.shape(opts.maxBytes), "toml/bad-opt");
|
|
89
|
-
}
|
|
90
|
-
if (opts.maxDepth !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxDepth)) {
|
|
91
|
-
throw new SafeTomlError("toml.parse: maxDepth must be a positive finite integer; got " +
|
|
92
|
-
numericBounds.shape(opts.maxDepth), "toml/bad-opt");
|
|
93
|
-
}
|
|
94
|
-
if (opts.maxKeys !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxKeys)) {
|
|
95
|
-
throw new SafeTomlError("toml.parse: maxKeys must be a positive finite integer; got " +
|
|
96
|
-
numericBounds.shape(opts.maxKeys), "toml/bad-opt");
|
|
97
|
-
}
|
|
98
|
-
var maxBytes = opts.maxBytes !== undefined
|
|
99
|
-
? Math.min(opts.maxBytes, C.BYTES.mib(64))
|
|
100
|
-
: DEFAULTS.maxBytes;
|
|
101
|
-
var maxDepth = opts.maxDepth !== undefined
|
|
102
|
-
? Math.min(opts.maxDepth, 1_000)
|
|
103
|
-
: DEFAULTS.maxDepth;
|
|
104
|
-
var maxKeys = opts.maxKeys !== undefined
|
|
105
|
-
? Math.min(opts.maxKeys, 1_000_000)
|
|
106
|
-
: DEFAULTS.maxKeys;
|
|
107
|
-
|
|
108
|
-
input = safeBuffer.normalizeText(input, {
|
|
109
|
-
maxBytes: maxBytes,
|
|
110
|
-
errorClass: SafeTomlError,
|
|
111
|
-
typeCode: "toml/wrong-input-type",
|
|
112
|
-
sizeCode: "toml/too-large",
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
var pos = 0;
|
|
116
|
-
var line = 1;
|
|
117
|
-
var col = 1;
|
|
118
|
-
var len = input.length;
|
|
119
|
-
var root = Object.create(null);
|
|
120
|
-
var definedTables = new Set(); // tables created by an explicit [a.b] header
|
|
121
|
-
var inlineTablesSet = new WeakSet();
|
|
122
|
-
var keyCount = 0;
|
|
123
|
-
var current = root;
|
|
124
|
-
|
|
125
|
-
function _err(msg, code) {
|
|
126
|
-
return new SafeTomlError(msg, code || "toml/invalid", line, col);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function _bumpKeys() {
|
|
130
|
-
keyCount += 1;
|
|
131
|
-
if (keyCount > maxKeys) throw _err("input exceeds maxKeys", "toml/too-many-keys");
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function _advance(n) {
|
|
135
|
-
n = n == null ? 1 : n;
|
|
136
|
-
for (var i = 0; i < n; i++) {
|
|
137
|
-
if (pos >= len) return;
|
|
138
|
-
if (input.charCodeAt(pos) === 0x0A) { line += 1; col = 1; }
|
|
139
|
-
else col += 1;
|
|
140
|
-
pos += 1;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function _eof() { return pos >= len; }
|
|
145
|
-
function _peek(off) { return input.charAt(pos + (off || 0)); }
|
|
146
|
-
function _peekCode(off) { return input.charCodeAt(pos + (off || 0)); }
|
|
147
|
-
|
|
148
|
-
function _skipSpacesAndTabs() {
|
|
149
|
-
while (!_eof()) {
|
|
150
|
-
var c = _peekCode();
|
|
151
|
-
if (c === 0x20 || c === 0x09) _advance();
|
|
152
|
-
else break;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function _skipNewline() {
|
|
157
|
-
if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
158
|
-
else if (_peek() === "\n") _advance(1);
|
|
159
|
-
else throw _err("expected newline", "toml/expected-newline");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function _skipCommentAndNewline() {
|
|
163
|
-
if (_peek() === "#") {
|
|
164
|
-
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
165
|
-
}
|
|
166
|
-
if (_eof()) return;
|
|
167
|
-
_skipNewline();
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function _markInlineTable(obj) { inlineTablesSet.add(obj); }
|
|
171
|
-
function _isInlineTable(obj) { return inlineTablesSet.has(obj); }
|
|
172
|
-
|
|
173
|
-
// ---- Key parsing ----
|
|
174
|
-
|
|
175
|
-
function _parseBareKey() {
|
|
176
|
-
var start = pos;
|
|
177
|
-
while (!_eof()) {
|
|
178
|
-
var c = _peekCode();
|
|
179
|
-
var ok =
|
|
180
|
-
(c >= 0x41 && c <= 0x5A) || // A-Z
|
|
181
|
-
(c >= 0x61 && c <= 0x7A) || // a-z
|
|
182
|
-
(c >= 0x30 && c <= 0x39) || // 0-9
|
|
183
|
-
c === 0x5F || c === 0x2D; // _ -
|
|
184
|
-
if (!ok) break;
|
|
185
|
-
_advance();
|
|
186
|
-
}
|
|
187
|
-
if (pos === start) throw _err("expected key", "toml/expected-key");
|
|
188
|
-
return input.substring(start, pos);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function _parseQuotedKey() {
|
|
192
|
-
var quote = _peek();
|
|
193
|
-
var basic = (quote === '"');
|
|
194
|
-
_advance();
|
|
195
|
-
var start = pos;
|
|
196
|
-
var out = "";
|
|
197
|
-
while (!_eof()) {
|
|
198
|
-
var c = _peek();
|
|
199
|
-
if (c === quote) {
|
|
200
|
-
if (!basic) out = input.substring(start, pos);
|
|
201
|
-
_advance();
|
|
202
|
-
return out;
|
|
203
|
-
}
|
|
204
|
-
if (c === "\n") throw _err("newline in quoted key", "toml/bad-key");
|
|
205
|
-
if (basic && c === "\\") {
|
|
206
|
-
out += input.substring(start, pos);
|
|
207
|
-
_advance();
|
|
208
|
-
out += _decodeEscape();
|
|
209
|
-
start = pos;
|
|
210
|
-
} else {
|
|
211
|
-
_advance();
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
throw _err("unterminated quoted key", "toml/bad-key");
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function _parseSingleKeySegment() {
|
|
218
|
-
var c = _peek();
|
|
219
|
-
if (c === '"' || c === "'") return _parseQuotedKey();
|
|
220
|
-
return _parseBareKey();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function _parseDottedKey() {
|
|
224
|
-
var segments = [_parseSingleKeySegment()];
|
|
225
|
-
if (POISONED_KEYS.has(segments[0])) {
|
|
226
|
-
throw _err("forbidden key '" + segments[0] + "'", "toml/poisoned-key");
|
|
227
|
-
}
|
|
228
|
-
while (true) {
|
|
229
|
-
_skipSpacesAndTabs();
|
|
230
|
-
if (_peek() !== ".") return segments;
|
|
231
|
-
_advance();
|
|
232
|
-
_skipSpacesAndTabs();
|
|
233
|
-
var seg = _parseSingleKeySegment();
|
|
234
|
-
if (POISONED_KEYS.has(seg)) {
|
|
235
|
-
throw _err("forbidden key '" + seg + "'", "toml/poisoned-key");
|
|
236
|
-
}
|
|
237
|
-
segments.push(seg);
|
|
238
|
-
// Cap dotted-key depth at maxDepth — same bound as `_parseValue`.
|
|
239
|
-
// Without this, a table header `[a.b.c.d…]` with thousands of
|
|
240
|
-
// segments builds a tree deep enough to stack-overflow the
|
|
241
|
-
// post-parse `_normalize` walker (which is recursive). The check
|
|
242
|
-
// sits at +1 over depth so a path of exactly maxDepth segments is
|
|
243
|
-
// still accepted (matches the inclusive bound in _parseValue).
|
|
244
|
-
if (segments.length > maxDepth) {
|
|
245
|
-
throw _err("dotted-key path exceeds maxDepth (" + maxDepth + ")", "toml/too-deep");
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// ---- Value parsing ----
|
|
251
|
-
|
|
252
|
-
function _decodeEscape() {
|
|
253
|
-
var c = _peek();
|
|
254
|
-
_advance();
|
|
255
|
-
switch (c) {
|
|
256
|
-
case '"': return '"';
|
|
257
|
-
case '\\': return "\\";
|
|
258
|
-
case "/": return "/";
|
|
259
|
-
case "b": return "\b";
|
|
260
|
-
case "f": return "\f";
|
|
261
|
-
case "n": return "\n";
|
|
262
|
-
case "r": return "\r";
|
|
263
|
-
case "t": return "\t";
|
|
264
|
-
case "u": {
|
|
265
|
-
var hex = input.substring(pos, pos + 4);
|
|
266
|
-
if (!safeBuffer.isHex(hex, 4)) {
|
|
267
|
-
throw _err("bad \\u escape", "toml/bad-escape");
|
|
268
|
-
}
|
|
269
|
-
_advance(4);
|
|
270
|
-
return String.fromCharCode(parseInt(hex, RADIX_HEX));
|
|
271
|
-
}
|
|
272
|
-
case "U": {
|
|
273
|
-
var hexLen = 0x8;
|
|
274
|
-
var hex8 = input.substring(pos, pos + hexLen);
|
|
275
|
-
if (hex8.length < hexLen || !/^[0-9a-fA-F]{8}$/.test(hex8)) {
|
|
276
|
-
throw _err("bad \\U escape", "toml/bad-escape");
|
|
277
|
-
}
|
|
278
|
-
_advance(hexLen);
|
|
279
|
-
var code = parseInt(hex8, RADIX_HEX);
|
|
280
|
-
if (code > 0x10FFFF) throw _err("\\U code point > U+10FFFF", "toml/bad-escape");
|
|
281
|
-
return String.fromCodePoint(code);
|
|
282
|
-
}
|
|
283
|
-
default:
|
|
284
|
-
throw _err("unknown escape '\\" + c + "'", "toml/bad-escape");
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function _parseBasicString() {
|
|
289
|
-
_advance();
|
|
290
|
-
var start = pos;
|
|
291
|
-
var out = "";
|
|
292
|
-
while (!_eof()) {
|
|
293
|
-
var c = _peek();
|
|
294
|
-
if (c === '"') {
|
|
295
|
-
out += input.substring(start, pos);
|
|
296
|
-
_advance();
|
|
297
|
-
return out;
|
|
298
|
-
}
|
|
299
|
-
if (c === "\n") throw _err("newline in basic string (use triple-quote for multi-line)", "toml/bad-string");
|
|
300
|
-
if (c === "\\") {
|
|
301
|
-
out += input.substring(start, pos);
|
|
302
|
-
_advance();
|
|
303
|
-
out += _decodeEscape();
|
|
304
|
-
start = pos;
|
|
305
|
-
continue;
|
|
306
|
-
}
|
|
307
|
-
var cc = _peekCode();
|
|
308
|
-
if ((cc < 0x20 && cc !== 0x09) || cc === 0x7F) {
|
|
309
|
-
throw _err("unescaped control char in string", "toml/bad-string");
|
|
310
|
-
}
|
|
311
|
-
_advance();
|
|
312
|
-
}
|
|
313
|
-
throw _err("unterminated basic string", "toml/unterminated-string");
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function _parseMultilineBasicString() {
|
|
317
|
-
_advance(3);
|
|
318
|
-
if (_peek() === "\n") _advance(1);
|
|
319
|
-
else if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
320
|
-
var start = pos;
|
|
321
|
-
var out = "";
|
|
322
|
-
while (!_eof()) {
|
|
323
|
-
if (_peek() === '"' && _peek(1) === '"' && _peek(2) === '"') {
|
|
324
|
-
out += input.substring(start, pos);
|
|
325
|
-
_advance(3);
|
|
326
|
-
var trailing = 0;
|
|
327
|
-
while (trailing < 2 && _peek() === '"') { out += '"'; _advance(); trailing += 1; }
|
|
328
|
-
return out;
|
|
329
|
-
}
|
|
330
|
-
var c = _peek();
|
|
331
|
-
if (c === "\\") {
|
|
332
|
-
out += input.substring(start, pos);
|
|
333
|
-
_advance();
|
|
334
|
-
if (_peek() === "\n" || (_peek() === "\r" && _peek(1) === "\n")) {
|
|
335
|
-
if (_peek() === "\r") _advance();
|
|
336
|
-
_advance();
|
|
337
|
-
_skipSpacesAndTabs();
|
|
338
|
-
while (_peek() === "\n" || (_peek() === "\r" && _peek(1) === "\n")) {
|
|
339
|
-
if (_peek() === "\r") _advance();
|
|
340
|
-
_advance();
|
|
341
|
-
_skipSpacesAndTabs();
|
|
342
|
-
}
|
|
343
|
-
start = pos;
|
|
344
|
-
continue;
|
|
345
|
-
}
|
|
346
|
-
out += _decodeEscape();
|
|
347
|
-
start = pos;
|
|
348
|
-
continue;
|
|
349
|
-
}
|
|
350
|
-
_advance();
|
|
351
|
-
}
|
|
352
|
-
throw _err("unterminated multi-line basic string", "toml/unterminated-string");
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
function _parseLiteralString() {
|
|
356
|
-
_advance();
|
|
357
|
-
var start = pos;
|
|
358
|
-
while (!_eof()) {
|
|
359
|
-
if (_peek() === "'") {
|
|
360
|
-
var s = input.substring(start, pos);
|
|
361
|
-
_advance();
|
|
362
|
-
return s;
|
|
363
|
-
}
|
|
364
|
-
if (_peek() === "\n") throw _err("newline in literal string", "toml/bad-string");
|
|
365
|
-
_advance();
|
|
366
|
-
}
|
|
367
|
-
throw _err("unterminated literal string", "toml/unterminated-string");
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function _parseMultilineLiteralString() {
|
|
371
|
-
_advance(3);
|
|
372
|
-
if (_peek() === "\n") _advance(1);
|
|
373
|
-
else if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
374
|
-
var start = pos;
|
|
375
|
-
while (!_eof()) {
|
|
376
|
-
if (_peek() === "'" && _peek(1) === "'" && _peek(2) === "'") {
|
|
377
|
-
var s = input.substring(start, pos);
|
|
378
|
-
_advance(3);
|
|
379
|
-
var trailing = 0;
|
|
380
|
-
while (trailing < 2 && _peek() === "'") { s += "'"; _advance(); trailing += 1; }
|
|
381
|
-
return s;
|
|
382
|
-
}
|
|
383
|
-
_advance();
|
|
384
|
-
}
|
|
385
|
-
throw _err("unterminated multi-line literal string", "toml/unterminated-string");
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Date-time, date-only, or time-only.
|
|
389
|
-
// Returns { kind, value } where for "offset" value is a JS Date and for
|
|
390
|
-
// the others value is the canonical ISO string. Returns null if the
|
|
391
|
-
// current position doesn't start a date-time literal.
|
|
392
|
-
function _tryParseDateTime() {
|
|
393
|
-
// Date-time form: YYYY-MM-DD followed by 'T'/' ' followed by time
|
|
394
|
-
if (pos + 10 <= len && /^\d{4}-\d{2}-\d{2}/.test(input.substr(pos, 10))) {
|
|
395
|
-
var sep = _peek(10);
|
|
396
|
-
if (sep === "T" || sep === "t" || sep === " ") {
|
|
397
|
-
// Look ahead for time portion (HH:MM:SS = 8 chars)
|
|
398
|
-
var timeStart = pos + 11;
|
|
399
|
-
var timeChars = TIME_CHARS;
|
|
400
|
-
if (/^\d{2}:\d{2}:\d{2}/.test(input.substr(timeStart, timeChars))) {
|
|
401
|
-
var datePart = input.substr(pos, 10);
|
|
402
|
-
var timeEnd = timeStart + timeChars;
|
|
403
|
-
// Optional fractional
|
|
404
|
-
if (_peek(timeEnd - pos) === ".") {
|
|
405
|
-
timeEnd += 1;
|
|
406
|
-
while (timeEnd < len && /\d/.test(input.charAt(timeEnd))) timeEnd += 1;
|
|
407
|
-
}
|
|
408
|
-
var timePart = input.substring(timeStart, timeEnd);
|
|
409
|
-
// Optional offset
|
|
410
|
-
var offsetStr = "";
|
|
411
|
-
var nextChar = input.charAt(timeEnd);
|
|
412
|
-
if (nextChar === "Z" || nextChar === "z") { offsetStr = "Z"; timeEnd += 1; }
|
|
413
|
-
else if (nextChar === "+" || nextChar === "-") {
|
|
414
|
-
// Expect ±HH:MM (6 chars)
|
|
415
|
-
var offsetChars = OFFSET_CHARS;
|
|
416
|
-
var off = input.substr(timeEnd, offsetChars);
|
|
417
|
-
if (/^[+-]\d{2}:\d{2}$/.test(off)) {
|
|
418
|
-
offsetStr = off;
|
|
419
|
-
timeEnd += offsetChars;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
_advance(timeEnd - pos);
|
|
423
|
-
if (offsetStr) {
|
|
424
|
-
var iso = datePart + "T" + timePart + offsetStr;
|
|
425
|
-
var d = new Date(iso);
|
|
426
|
-
if (isNaN(d.getTime())) throw _err("invalid offset date-time", "toml/bad-datetime");
|
|
427
|
-
return { kind: "offset", value: d };
|
|
428
|
-
}
|
|
429
|
-
return { kind: "local-dt", value: datePart + "T" + timePart };
|
|
430
|
-
}
|
|
431
|
-
// 'T' / ' ' followed by non-time → fall through to date-only check below
|
|
432
|
-
}
|
|
433
|
-
// Date-only — but only if followed by something OTHER than digit/colon
|
|
434
|
-
var after = _peek(10);
|
|
435
|
-
if (!after || !/[0-9:.]/.test(after)) {
|
|
436
|
-
var ds = input.substr(pos, 10);
|
|
437
|
-
_advance(10);
|
|
438
|
-
return { kind: "local-date", value: ds };
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
// Time-only: HH:MM:SS (8 chars)
|
|
442
|
-
var timeOnlyChars = TIME_CHARS;
|
|
443
|
-
if (pos + timeOnlyChars <= len && /^\d{2}:\d{2}:\d{2}/.test(input.substr(pos, timeOnlyChars))) {
|
|
444
|
-
var teEnd = pos + timeOnlyChars;
|
|
445
|
-
if (input.charAt(teEnd) === ".") {
|
|
446
|
-
teEnd += 1;
|
|
447
|
-
while (teEnd < len && /\d/.test(input.charAt(teEnd))) teEnd += 1;
|
|
448
|
-
}
|
|
449
|
-
var ts = input.substring(pos, teEnd);
|
|
450
|
-
_advance(teEnd - pos);
|
|
451
|
-
return { kind: "local-time", value: ts };
|
|
452
|
-
}
|
|
453
|
-
return null;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
function _parseNumber(firstChar) {
|
|
457
|
-
var startPos = pos;
|
|
458
|
-
var startLine = line;
|
|
459
|
-
var startCol = col;
|
|
460
|
-
|
|
461
|
-
// Special floats: inf, nan with optional sign
|
|
462
|
-
if (firstChar === "+" || firstChar === "-") {
|
|
463
|
-
var rest = input.substring(pos + 1, pos + 4);
|
|
464
|
-
if (rest === "inf") { _advance(4); return firstChar === "-" ? -Infinity : Infinity; }
|
|
465
|
-
if (rest === "nan") { _advance(4); return NaN; }
|
|
466
|
-
} else if (firstChar === "i" || firstChar === "n") {
|
|
467
|
-
var w = input.substring(pos, pos + 3);
|
|
468
|
-
if (w === "inf") { _advance(3); return Infinity; }
|
|
469
|
-
if (w === "nan") { _advance(3); return NaN; }
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
var sign = 1;
|
|
473
|
-
if (_peek() === "+") _advance();
|
|
474
|
-
else if (_peek() === "-") { sign = -1; _advance(); }
|
|
475
|
-
|
|
476
|
-
if (_peek() === "0") {
|
|
477
|
-
var prefix = _peek(1);
|
|
478
|
-
if (prefix === "x" || prefix === "o" || prefix === "b") {
|
|
479
|
-
if (sign !== 1) throw _err("sign not allowed on hex/oct/bin literal", "toml/bad-number");
|
|
480
|
-
_advance(2);
|
|
481
|
-
var radix = prefix === "x" ? RADIX_HEX : prefix === "o" ? RADIX_OCTAL : RADIX_BIN;
|
|
482
|
-
var digitsStart = pos;
|
|
483
|
-
while (!_eof()) {
|
|
484
|
-
var ch = _peek();
|
|
485
|
-
if (ch === "_") { _advance(); continue; }
|
|
486
|
-
if (radix === RADIX_HEX && /[0-9a-fA-F]/.test(ch)) { _advance(); continue; }
|
|
487
|
-
if (radix === RADIX_OCTAL && /[0-7]/.test(ch)) { _advance(); continue; }
|
|
488
|
-
if (radix === RADIX_BIN && /[01]/.test(ch)) { _advance(); continue; }
|
|
489
|
-
break;
|
|
490
|
-
}
|
|
491
|
-
var digits = input.substring(digitsStart, pos).replace(/_/g, "");
|
|
492
|
-
if (digits.length === 0) throw _err("expected digits after radix prefix", "toml/bad-number");
|
|
493
|
-
var n = parseInt(digits, radix);
|
|
494
|
-
if (!Number.isSafeInteger(n)) {
|
|
495
|
-
throw new SafeTomlError("integer overflow (use a quoted string for 64-bit values)",
|
|
496
|
-
"toml/integer-overflow", startLine, startCol);
|
|
497
|
-
}
|
|
498
|
-
return n;
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
var hasDot = false;
|
|
503
|
-
var hasExp = false;
|
|
504
|
-
while (!_eof()) {
|
|
505
|
-
var ch2 = _peek();
|
|
506
|
-
if (ch2 === "_") { _advance(); continue; }
|
|
507
|
-
if (ch2 >= "0" && ch2 <= "9") { _advance(); continue; }
|
|
508
|
-
if (ch2 === "." && !hasDot && !hasExp) { hasDot = true; _advance(); continue; }
|
|
509
|
-
if ((ch2 === "e" || ch2 === "E") && !hasExp) {
|
|
510
|
-
hasExp = true;
|
|
511
|
-
_advance();
|
|
512
|
-
if (_peek() === "+" || _peek() === "-") _advance();
|
|
513
|
-
continue;
|
|
514
|
-
}
|
|
515
|
-
break;
|
|
516
|
-
}
|
|
517
|
-
var raw = input.substring(startPos, pos).replace(/_/g, "");
|
|
518
|
-
if (raw === "" || raw === "-" || raw === "+") {
|
|
519
|
-
throw new SafeTomlError("invalid number", "toml/bad-number", startLine, startCol);
|
|
520
|
-
}
|
|
521
|
-
if (hasDot || hasExp) {
|
|
522
|
-
var f = parseFloat(raw);
|
|
523
|
-
if (isNaN(f)) {
|
|
524
|
-
throw new SafeTomlError("invalid float", "toml/bad-number", startLine, startCol);
|
|
525
|
-
}
|
|
526
|
-
return f;
|
|
527
|
-
}
|
|
528
|
-
var i = parseInt(raw, 10);
|
|
529
|
-
if (!Number.isSafeInteger(i)) {
|
|
530
|
-
throw new SafeTomlError("integer overflow (use a quoted string for 64-bit values)",
|
|
531
|
-
"toml/integer-overflow", startLine, startCol);
|
|
532
|
-
}
|
|
533
|
-
return i;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
function _parseValue(depth) {
|
|
537
|
-
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
538
|
-
_skipSpacesAndTabs();
|
|
539
|
-
if (_eof()) throw _err("expected value, got EOF", "toml/expected-value");
|
|
540
|
-
|
|
541
|
-
var c = _peek();
|
|
542
|
-
|
|
543
|
-
if (c === '"' && _peek(1) === '"' && _peek(2) === '"') return _parseMultilineBasicString();
|
|
544
|
-
if (c === "'" && _peek(1) === "'" && _peek(2) === "'") return _parseMultilineLiteralString();
|
|
545
|
-
if (c === '"') return _parseBasicString();
|
|
546
|
-
if (c === "'") return _parseLiteralString();
|
|
547
|
-
if (c === "[") return _parseArray(depth + 1);
|
|
548
|
-
if (c === "{") return _parseInlineTable(depth + 1);
|
|
549
|
-
|
|
550
|
-
if (input.substr(pos, 4) === "true" && !/[A-Za-z0-9_]/.test(input.charAt(pos + 4) || "")) {
|
|
551
|
-
_advance(4); return true;
|
|
552
|
-
}
|
|
553
|
-
if (input.substr(pos, 5) === "false" && !/[A-Za-z0-9_]/.test(input.charAt(pos + 5) || "")) {
|
|
554
|
-
_advance(5); return false;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
var dt = _tryParseDateTime();
|
|
558
|
-
if (dt !== null) return dt.value;
|
|
559
|
-
|
|
560
|
-
if (/[0-9+\-i n]/.test(c)) return _parseNumber(c);
|
|
561
|
-
|
|
562
|
-
throw _err("unexpected character '" + c + "'", "toml/expected-value");
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
function _parseArray(depth) {
|
|
566
|
-
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
567
|
-
_advance();
|
|
568
|
-
var arr = [];
|
|
569
|
-
while (true) {
|
|
570
|
-
_skipArrayWhitespace();
|
|
571
|
-
if (_peek() === "]") { _advance(); return arr; }
|
|
572
|
-
arr.push(_parseValue(depth));
|
|
573
|
-
_skipArrayWhitespace();
|
|
574
|
-
if (_peek() === ",") { _advance(); continue; }
|
|
575
|
-
_skipArrayWhitespace();
|
|
576
|
-
if (_peek() === "]") { _advance(); return arr; }
|
|
577
|
-
throw _err("expected ',' or ']' in array", "toml/bad-array");
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
function _skipArrayWhitespace() {
|
|
582
|
-
while (!_eof()) {
|
|
583
|
-
var c = _peek();
|
|
584
|
-
if (c === " " || c === "\t" || c === "\n" || c === "\r") { _advance(); continue; }
|
|
585
|
-
if (c === "#") {
|
|
586
|
-
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
587
|
-
continue;
|
|
588
|
-
}
|
|
589
|
-
break;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
function _parseInlineTable(depth) {
|
|
594
|
-
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
595
|
-
_advance();
|
|
596
|
-
var tbl = Object.create(null);
|
|
597
|
-
_markInlineTable(tbl);
|
|
598
|
-
_skipSpacesAndTabs();
|
|
599
|
-
if (_peek() === "}") { _advance(); return tbl; }
|
|
600
|
-
while (true) {
|
|
601
|
-
_skipSpacesAndTabs();
|
|
602
|
-
var pathSegs = _parseDottedKey();
|
|
603
|
-
_skipSpacesAndTabs();
|
|
604
|
-
if (_peek() !== "=") throw _err("expected '=' in inline table", "toml/bad-inline-table");
|
|
605
|
-
_advance();
|
|
606
|
-
_skipSpacesAndTabs();
|
|
607
|
-
var v = _parseValue(depth);
|
|
608
|
-
_bumpKeys();
|
|
609
|
-
_setNested(tbl, pathSegs, v);
|
|
610
|
-
_skipSpacesAndTabs();
|
|
611
|
-
if (_peek() === ",") {
|
|
612
|
-
_advance();
|
|
613
|
-
if (_peek() === "}") throw _err("trailing comma in inline table", "toml/bad-inline-table");
|
|
614
|
-
continue;
|
|
615
|
-
}
|
|
616
|
-
if (_peek() === "}") { _advance(); return tbl; }
|
|
617
|
-
throw _err("expected ',' or '}' in inline table", "toml/bad-inline-table");
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
function _setNested(table, segments, value) {
|
|
622
|
-
var t = table;
|
|
623
|
-
for (var i = 0; i < segments.length - 1; i++) {
|
|
624
|
-
var seg = segments[i];
|
|
625
|
-
if (POISONED_KEYS.has(seg)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
626
|
-
if (Object.prototype.hasOwnProperty.call(t, seg)) {
|
|
627
|
-
var sub = t[seg];
|
|
628
|
-
if (sub == null || typeof sub !== "object" || Array.isArray(sub)) {
|
|
629
|
-
throw _err("cannot redefine '" + seg + "' as a sub-table", "toml/redefine");
|
|
630
|
-
}
|
|
631
|
-
if (_isInlineTable(sub)) {
|
|
632
|
-
throw _err("cannot extend inline table '" + seg + "'", "toml/inline-table-mutated");
|
|
633
|
-
}
|
|
634
|
-
t = sub;
|
|
635
|
-
} else {
|
|
636
|
-
var fresh = Object.create(null);
|
|
637
|
-
t[seg] = fresh;
|
|
638
|
-
t = fresh;
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
var last = segments[segments.length - 1];
|
|
642
|
-
if (POISONED_KEYS.has(last)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
643
|
-
if (Object.prototype.hasOwnProperty.call(t, last)) {
|
|
644
|
-
throw _err("duplicate key '" + last + "'", "toml/duplicate-key");
|
|
645
|
-
}
|
|
646
|
-
t[last] = value;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
function _parseTableHeader() {
|
|
650
|
-
var isAoT = _peek(1) === "[";
|
|
651
|
-
_advance(isAoT ? 2 : 1);
|
|
652
|
-
_skipSpacesAndTabs();
|
|
653
|
-
var segments = _parseDottedKey();
|
|
654
|
-
_skipSpacesAndTabs();
|
|
655
|
-
if (isAoT) {
|
|
656
|
-
if (_peek() !== "]" || _peek(1) !== "]") throw _err("expected ']]'", "toml/bad-table-header");
|
|
657
|
-
_advance(2);
|
|
658
|
-
} else {
|
|
659
|
-
if (_peek() !== "]") throw _err("expected ']'", "toml/bad-table-header");
|
|
660
|
-
_advance();
|
|
661
|
-
}
|
|
662
|
-
_skipSpacesAndTabs();
|
|
663
|
-
_skipCommentAndNewline();
|
|
664
|
-
|
|
665
|
-
var t = root;
|
|
666
|
-
for (var i = 0; i < segments.length; i++) {
|
|
667
|
-
var seg = segments[i];
|
|
668
|
-
if (POISONED_KEYS.has(seg)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
669
|
-
var isLast = (i === segments.length - 1);
|
|
670
|
-
if (Object.prototype.hasOwnProperty.call(t, seg)) {
|
|
671
|
-
var sub = t[seg];
|
|
672
|
-
if (Array.isArray(sub)) {
|
|
673
|
-
if (isLast && isAoT) {
|
|
674
|
-
var fresh = Object.create(null);
|
|
675
|
-
sub.push(fresh);
|
|
676
|
-
current = fresh;
|
|
677
|
-
return;
|
|
678
|
-
}
|
|
679
|
-
if (isLast && !isAoT) {
|
|
680
|
-
throw _err("table '" + seg + "' previously defined as array of tables",
|
|
681
|
-
"toml/redefine");
|
|
682
|
-
}
|
|
683
|
-
t = sub[sub.length - 1];
|
|
684
|
-
continue;
|
|
685
|
-
}
|
|
686
|
-
if (typeof sub !== "object" || sub === null) {
|
|
687
|
-
throw _err("cannot redefine '" + seg + "' as a sub-table", "toml/redefine");
|
|
688
|
-
}
|
|
689
|
-
if (_isInlineTable(sub)) {
|
|
690
|
-
throw _err("cannot extend inline table '" + seg + "'", "toml/inline-table-mutated");
|
|
691
|
-
}
|
|
692
|
-
if (isLast && !isAoT) {
|
|
693
|
-
var fullPath = segments.slice(0, i + 1).join(".");
|
|
694
|
-
if (definedTables.has(fullPath)) {
|
|
695
|
-
throw _err("table '" + fullPath + "' defined twice", "toml/redefine");
|
|
696
|
-
}
|
|
697
|
-
definedTables.add(fullPath);
|
|
698
|
-
current = sub;
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
|
-
if (isLast && isAoT) {
|
|
702
|
-
throw _err("'" + seg + "' was a table; cannot redefine as array of tables",
|
|
703
|
-
"toml/redefine");
|
|
704
|
-
}
|
|
705
|
-
t = sub;
|
|
706
|
-
} else {
|
|
707
|
-
if (isLast && isAoT) {
|
|
708
|
-
var arr = [];
|
|
709
|
-
t[seg] = arr;
|
|
710
|
-
var freshT = Object.create(null);
|
|
711
|
-
arr.push(freshT);
|
|
712
|
-
current = freshT;
|
|
713
|
-
return;
|
|
714
|
-
}
|
|
715
|
-
if (isLast && !isAoT) {
|
|
716
|
-
var freshT2 = Object.create(null);
|
|
717
|
-
t[seg] = freshT2;
|
|
718
|
-
var fullPath2 = segments.slice(0, i + 1).join(".");
|
|
719
|
-
definedTables.add(fullPath2);
|
|
720
|
-
current = freshT2;
|
|
721
|
-
return;
|
|
722
|
-
}
|
|
723
|
-
var freshParent = Object.create(null);
|
|
724
|
-
t[seg] = freshParent;
|
|
725
|
-
t = freshParent;
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
|
|
730
|
-
function _parseKeyValueLine() {
|
|
731
|
-
var pathSegs = _parseDottedKey();
|
|
732
|
-
_skipSpacesAndTabs();
|
|
733
|
-
if (_peek() !== "=") throw _err("expected '=' after key", "toml/bad-kv");
|
|
734
|
-
_advance();
|
|
735
|
-
_skipSpacesAndTabs();
|
|
736
|
-
var v = _parseValue(1);
|
|
737
|
-
_bumpKeys();
|
|
738
|
-
_setNested(current, pathSegs, v);
|
|
739
|
-
_skipSpacesAndTabs();
|
|
740
|
-
_skipCommentAndNewline();
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
while (!_eof()) {
|
|
744
|
-
_skipSpacesAndTabs();
|
|
745
|
-
if (_eof()) break;
|
|
746
|
-
var c = _peek();
|
|
747
|
-
if (c === "#") {
|
|
748
|
-
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
749
|
-
if (!_eof()) _skipNewline();
|
|
750
|
-
continue;
|
|
751
|
-
}
|
|
752
|
-
if (c === "\n" || (c === "\r" && _peek(1) === "\n")) {
|
|
753
|
-
_skipNewline();
|
|
754
|
-
continue;
|
|
755
|
-
}
|
|
756
|
-
if (c === "[") {
|
|
757
|
-
_parseTableHeader();
|
|
758
|
-
continue;
|
|
759
|
-
}
|
|
760
|
-
_parseKeyValueLine();
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
// Strip null-prototype objects to plain {}-prototype for the operator;
|
|
764
|
-
// null-prototype is only needed during build to block prototype pollution.
|
|
765
|
-
function _normalize(value) {
|
|
766
|
-
if (Array.isArray(value)) return value.map(_normalize);
|
|
767
|
-
if (value && typeof value === "object" && !(value instanceof Date)) {
|
|
768
|
-
var out = {};
|
|
769
|
-
for (var k in value) {
|
|
770
|
-
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
|
771
|
-
out[k] = _normalize(value[k]);
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
return out;
|
|
775
|
-
}
|
|
776
|
-
return value;
|
|
777
|
-
}
|
|
778
|
-
return _normalize(root);
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
module.exports = {
|
|
782
|
-
parse: parse,
|
|
783
|
-
SafeTomlError: SafeTomlError,
|
|
784
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Security-focused TOML 1.0 parser.
|
|
4
|
+
*
|
|
5
|
+
* Implements TOML 1.0 (toml.io/en/v1.0.0) with the same defaults the
|
|
6
|
+
* framework's other parsers apply:
|
|
7
|
+
*
|
|
8
|
+
* - Size + depth + element-count limits (DoS prevention)
|
|
9
|
+
* - BOM stripping
|
|
10
|
+
* - Prototype-pollution rejection (__proto__ / constructor / prototype
|
|
11
|
+
* forbidden as bare keys, quoted keys, or dotted-key path segments)
|
|
12
|
+
* - Strict same-key redefinition rules per TOML spec (silent overwrite
|
|
13
|
+
* would mask config errors operators DO want surfaced)
|
|
14
|
+
*
|
|
15
|
+
* Output shape:
|
|
16
|
+
* - Strings: JS string (escape-decoded for basic/multi-line basic;
|
|
17
|
+
* verbatim for literal/multi-line literal)
|
|
18
|
+
* - Integers: JS Number when |n| <= Number.MAX_SAFE_INTEGER; throws
|
|
19
|
+
* toml/integer-overflow otherwise (operators get a clear failure
|
|
20
|
+
* instead of silent precision loss). Operators who legitimately
|
|
21
|
+
* need 64-bit integers should encode them as quoted strings.
|
|
22
|
+
* - Floats: JS Number; nan / inf / -inf preserved as JS NaN / Infinity
|
|
23
|
+
* - Booleans: true / false
|
|
24
|
+
* - Offset Date-Time: JS Date
|
|
25
|
+
* - Local Date-Time / Local Date / Local Time: ISO-formatted string
|
|
26
|
+
* (no implicit offset assumption)
|
|
27
|
+
* - Arrays: JS arrays
|
|
28
|
+
* - Tables / inline tables: plain JS objects
|
|
29
|
+
*
|
|
30
|
+
* What is REJECTED:
|
|
31
|
+
* - Same-key reassignment inside any table
|
|
32
|
+
* - Inline-table mutation (`x = { a = 1 }\nx.b = 2` is an error)
|
|
33
|
+
* - Defining a table whose name collides with an existing key
|
|
34
|
+
* - __proto__ / constructor / prototype as key names anywhere
|
|
35
|
+
* - Unterminated strings, arrays, or inline tables
|
|
36
|
+
*
|
|
37
|
+
* Public API:
|
|
38
|
+
* toml.parse(input, opts?) object | throws SafeTomlError
|
|
39
|
+
* toml.SafeTomlError error class
|
|
40
|
+
*
|
|
41
|
+
* Defaults:
|
|
42
|
+
* maxBytes: 1 MiB
|
|
43
|
+
* maxDepth: 100
|
|
44
|
+
* maxKeys: 50000
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
var C = require("../constants");
|
|
48
|
+
var numericBounds = require("../numeric-bounds");
|
|
49
|
+
var safeBuffer = require("../safe-buffer");
|
|
50
|
+
var { FrameworkError } = require("../framework-error");
|
|
51
|
+
|
|
52
|
+
class SafeTomlError extends FrameworkError {
|
|
53
|
+
constructor(message, code, line, col) {
|
|
54
|
+
super(line != null ? message + " at line " + line + ":" + col : message);
|
|
55
|
+
this.name = "SafeTomlError";
|
|
56
|
+
this.code = code || "toml/invalid";
|
|
57
|
+
this.line = line == null ? null : line;
|
|
58
|
+
this.col = col == null ? null : col;
|
|
59
|
+
this.isSafeTomlError = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// parseInt radices and TOML radix-prefix tokens — naming these constants
|
|
64
|
+
// keeps the literal `8` / `16` byte-shaped numbers off the call sites.
|
|
65
|
+
var RADIX_BIN = 0x2;
|
|
66
|
+
var RADIX_OCTAL = 0x8;
|
|
67
|
+
var RADIX_HEX = 0x10;
|
|
68
|
+
|
|
69
|
+
// Date-time literal character widths (per TOML / RFC 3339).
|
|
70
|
+
var TIME_CHARS = 0x8; // "HH:MM:SS"
|
|
71
|
+
var OFFSET_CHARS = 0x6; // "+HH:MM"
|
|
72
|
+
|
|
73
|
+
var DEFAULTS = {
|
|
74
|
+
maxBytes: C.BYTES.mib(1),
|
|
75
|
+
maxDepth: 100,
|
|
76
|
+
maxKeys: 50_000,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
var POISONED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
80
|
+
|
|
81
|
+
function parse(input, opts) {
|
|
82
|
+
opts = opts || {};
|
|
83
|
+
// Validate operator-supplied numeric opts via lib/numeric-bounds —
|
|
84
|
+
// Infinity / NaN / negative / non-integer all bypass the `> 0` shape
|
|
85
|
+
// and silently lift the DoS cap they were meant to enforce.
|
|
86
|
+
if (opts.maxBytes !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxBytes)) {
|
|
87
|
+
throw new SafeTomlError("toml.parse: maxBytes must be a positive finite integer; got " +
|
|
88
|
+
numericBounds.shape(opts.maxBytes), "toml/bad-opt");
|
|
89
|
+
}
|
|
90
|
+
if (opts.maxDepth !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxDepth)) {
|
|
91
|
+
throw new SafeTomlError("toml.parse: maxDepth must be a positive finite integer; got " +
|
|
92
|
+
numericBounds.shape(opts.maxDepth), "toml/bad-opt");
|
|
93
|
+
}
|
|
94
|
+
if (opts.maxKeys !== undefined && !numericBounds.isPositiveFiniteInt(opts.maxKeys)) {
|
|
95
|
+
throw new SafeTomlError("toml.parse: maxKeys must be a positive finite integer; got " +
|
|
96
|
+
numericBounds.shape(opts.maxKeys), "toml/bad-opt");
|
|
97
|
+
}
|
|
98
|
+
var maxBytes = opts.maxBytes !== undefined
|
|
99
|
+
? Math.min(opts.maxBytes, C.BYTES.mib(64))
|
|
100
|
+
: DEFAULTS.maxBytes;
|
|
101
|
+
var maxDepth = opts.maxDepth !== undefined
|
|
102
|
+
? Math.min(opts.maxDepth, 1_000)
|
|
103
|
+
: DEFAULTS.maxDepth;
|
|
104
|
+
var maxKeys = opts.maxKeys !== undefined
|
|
105
|
+
? Math.min(opts.maxKeys, 1_000_000)
|
|
106
|
+
: DEFAULTS.maxKeys;
|
|
107
|
+
|
|
108
|
+
input = safeBuffer.normalizeText(input, {
|
|
109
|
+
maxBytes: maxBytes,
|
|
110
|
+
errorClass: SafeTomlError,
|
|
111
|
+
typeCode: "toml/wrong-input-type",
|
|
112
|
+
sizeCode: "toml/too-large",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
var pos = 0;
|
|
116
|
+
var line = 1;
|
|
117
|
+
var col = 1;
|
|
118
|
+
var len = input.length;
|
|
119
|
+
var root = Object.create(null);
|
|
120
|
+
var definedTables = new Set(); // tables created by an explicit [a.b] header
|
|
121
|
+
var inlineTablesSet = new WeakSet();
|
|
122
|
+
var keyCount = 0;
|
|
123
|
+
var current = root;
|
|
124
|
+
|
|
125
|
+
function _err(msg, code) {
|
|
126
|
+
return new SafeTomlError(msg, code || "toml/invalid", line, col);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function _bumpKeys() {
|
|
130
|
+
keyCount += 1;
|
|
131
|
+
if (keyCount > maxKeys) throw _err("input exceeds maxKeys", "toml/too-many-keys");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function _advance(n) {
|
|
135
|
+
n = n == null ? 1 : n;
|
|
136
|
+
for (var i = 0; i < n; i++) {
|
|
137
|
+
if (pos >= len) return;
|
|
138
|
+
if (input.charCodeAt(pos) === 0x0A) { line += 1; col = 1; }
|
|
139
|
+
else col += 1;
|
|
140
|
+
pos += 1;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function _eof() { return pos >= len; }
|
|
145
|
+
function _peek(off) { return input.charAt(pos + (off || 0)); }
|
|
146
|
+
function _peekCode(off) { return input.charCodeAt(pos + (off || 0)); }
|
|
147
|
+
|
|
148
|
+
function _skipSpacesAndTabs() {
|
|
149
|
+
while (!_eof()) {
|
|
150
|
+
var c = _peekCode();
|
|
151
|
+
if (c === 0x20 || c === 0x09) _advance();
|
|
152
|
+
else break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function _skipNewline() {
|
|
157
|
+
if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
158
|
+
else if (_peek() === "\n") _advance(1);
|
|
159
|
+
else throw _err("expected newline", "toml/expected-newline");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function _skipCommentAndNewline() {
|
|
163
|
+
if (_peek() === "#") {
|
|
164
|
+
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
165
|
+
}
|
|
166
|
+
if (_eof()) return;
|
|
167
|
+
_skipNewline();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function _markInlineTable(obj) { inlineTablesSet.add(obj); }
|
|
171
|
+
function _isInlineTable(obj) { return inlineTablesSet.has(obj); }
|
|
172
|
+
|
|
173
|
+
// ---- Key parsing ----
|
|
174
|
+
|
|
175
|
+
function _parseBareKey() {
|
|
176
|
+
var start = pos;
|
|
177
|
+
while (!_eof()) {
|
|
178
|
+
var c = _peekCode();
|
|
179
|
+
var ok =
|
|
180
|
+
(c >= 0x41 && c <= 0x5A) || // A-Z
|
|
181
|
+
(c >= 0x61 && c <= 0x7A) || // a-z
|
|
182
|
+
(c >= 0x30 && c <= 0x39) || // 0-9
|
|
183
|
+
c === 0x5F || c === 0x2D; // _ -
|
|
184
|
+
if (!ok) break;
|
|
185
|
+
_advance();
|
|
186
|
+
}
|
|
187
|
+
if (pos === start) throw _err("expected key", "toml/expected-key");
|
|
188
|
+
return input.substring(start, pos);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function _parseQuotedKey() {
|
|
192
|
+
var quote = _peek();
|
|
193
|
+
var basic = (quote === '"');
|
|
194
|
+
_advance();
|
|
195
|
+
var start = pos;
|
|
196
|
+
var out = "";
|
|
197
|
+
while (!_eof()) {
|
|
198
|
+
var c = _peek();
|
|
199
|
+
if (c === quote) {
|
|
200
|
+
if (!basic) out = input.substring(start, pos);
|
|
201
|
+
_advance();
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
if (c === "\n") throw _err("newline in quoted key", "toml/bad-key");
|
|
205
|
+
if (basic && c === "\\") {
|
|
206
|
+
out += input.substring(start, pos);
|
|
207
|
+
_advance();
|
|
208
|
+
out += _decodeEscape();
|
|
209
|
+
start = pos;
|
|
210
|
+
} else {
|
|
211
|
+
_advance();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
throw _err("unterminated quoted key", "toml/bad-key");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function _parseSingleKeySegment() {
|
|
218
|
+
var c = _peek();
|
|
219
|
+
if (c === '"' || c === "'") return _parseQuotedKey();
|
|
220
|
+
return _parseBareKey();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function _parseDottedKey() {
|
|
224
|
+
var segments = [_parseSingleKeySegment()];
|
|
225
|
+
if (POISONED_KEYS.has(segments[0])) {
|
|
226
|
+
throw _err("forbidden key '" + segments[0] + "'", "toml/poisoned-key");
|
|
227
|
+
}
|
|
228
|
+
while (true) {
|
|
229
|
+
_skipSpacesAndTabs();
|
|
230
|
+
if (_peek() !== ".") return segments;
|
|
231
|
+
_advance();
|
|
232
|
+
_skipSpacesAndTabs();
|
|
233
|
+
var seg = _parseSingleKeySegment();
|
|
234
|
+
if (POISONED_KEYS.has(seg)) {
|
|
235
|
+
throw _err("forbidden key '" + seg + "'", "toml/poisoned-key");
|
|
236
|
+
}
|
|
237
|
+
segments.push(seg);
|
|
238
|
+
// Cap dotted-key depth at maxDepth — same bound as `_parseValue`.
|
|
239
|
+
// Without this, a table header `[a.b.c.d…]` with thousands of
|
|
240
|
+
// segments builds a tree deep enough to stack-overflow the
|
|
241
|
+
// post-parse `_normalize` walker (which is recursive). The check
|
|
242
|
+
// sits at +1 over depth so a path of exactly maxDepth segments is
|
|
243
|
+
// still accepted (matches the inclusive bound in _parseValue).
|
|
244
|
+
if (segments.length > maxDepth) {
|
|
245
|
+
throw _err("dotted-key path exceeds maxDepth (" + maxDepth + ")", "toml/too-deep");
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ---- Value parsing ----
|
|
251
|
+
|
|
252
|
+
function _decodeEscape() {
|
|
253
|
+
var c = _peek();
|
|
254
|
+
_advance();
|
|
255
|
+
switch (c) {
|
|
256
|
+
case '"': return '"';
|
|
257
|
+
case '\\': return "\\";
|
|
258
|
+
case "/": return "/";
|
|
259
|
+
case "b": return "\b";
|
|
260
|
+
case "f": return "\f";
|
|
261
|
+
case "n": return "\n";
|
|
262
|
+
case "r": return "\r";
|
|
263
|
+
case "t": return "\t";
|
|
264
|
+
case "u": {
|
|
265
|
+
var hex = input.substring(pos, pos + 4);
|
|
266
|
+
if (!safeBuffer.isHex(hex, 4)) {
|
|
267
|
+
throw _err("bad \\u escape", "toml/bad-escape");
|
|
268
|
+
}
|
|
269
|
+
_advance(4);
|
|
270
|
+
return String.fromCharCode(parseInt(hex, RADIX_HEX));
|
|
271
|
+
}
|
|
272
|
+
case "U": {
|
|
273
|
+
var hexLen = 0x8;
|
|
274
|
+
var hex8 = input.substring(pos, pos + hexLen);
|
|
275
|
+
if (hex8.length < hexLen || !/^[0-9a-fA-F]{8}$/.test(hex8)) {
|
|
276
|
+
throw _err("bad \\U escape", "toml/bad-escape");
|
|
277
|
+
}
|
|
278
|
+
_advance(hexLen);
|
|
279
|
+
var code = parseInt(hex8, RADIX_HEX);
|
|
280
|
+
if (code > 0x10FFFF) throw _err("\\U code point > U+10FFFF", "toml/bad-escape");
|
|
281
|
+
return String.fromCodePoint(code);
|
|
282
|
+
}
|
|
283
|
+
default:
|
|
284
|
+
throw _err("unknown escape '\\" + c + "'", "toml/bad-escape");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function _parseBasicString() {
|
|
289
|
+
_advance();
|
|
290
|
+
var start = pos;
|
|
291
|
+
var out = "";
|
|
292
|
+
while (!_eof()) {
|
|
293
|
+
var c = _peek();
|
|
294
|
+
if (c === '"') {
|
|
295
|
+
out += input.substring(start, pos);
|
|
296
|
+
_advance();
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
if (c === "\n") throw _err("newline in basic string (use triple-quote for multi-line)", "toml/bad-string");
|
|
300
|
+
if (c === "\\") {
|
|
301
|
+
out += input.substring(start, pos);
|
|
302
|
+
_advance();
|
|
303
|
+
out += _decodeEscape();
|
|
304
|
+
start = pos;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
var cc = _peekCode();
|
|
308
|
+
if ((cc < 0x20 && cc !== 0x09) || cc === 0x7F) {
|
|
309
|
+
throw _err("unescaped control char in string", "toml/bad-string");
|
|
310
|
+
}
|
|
311
|
+
_advance();
|
|
312
|
+
}
|
|
313
|
+
throw _err("unterminated basic string", "toml/unterminated-string");
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function _parseMultilineBasicString() {
|
|
317
|
+
_advance(3);
|
|
318
|
+
if (_peek() === "\n") _advance(1);
|
|
319
|
+
else if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
320
|
+
var start = pos;
|
|
321
|
+
var out = "";
|
|
322
|
+
while (!_eof()) {
|
|
323
|
+
if (_peek() === '"' && _peek(1) === '"' && _peek(2) === '"') {
|
|
324
|
+
out += input.substring(start, pos);
|
|
325
|
+
_advance(3);
|
|
326
|
+
var trailing = 0;
|
|
327
|
+
while (trailing < 2 && _peek() === '"') { out += '"'; _advance(); trailing += 1; }
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
var c = _peek();
|
|
331
|
+
if (c === "\\") {
|
|
332
|
+
out += input.substring(start, pos);
|
|
333
|
+
_advance();
|
|
334
|
+
if (_peek() === "\n" || (_peek() === "\r" && _peek(1) === "\n")) {
|
|
335
|
+
if (_peek() === "\r") _advance();
|
|
336
|
+
_advance();
|
|
337
|
+
_skipSpacesAndTabs();
|
|
338
|
+
while (_peek() === "\n" || (_peek() === "\r" && _peek(1) === "\n")) {
|
|
339
|
+
if (_peek() === "\r") _advance();
|
|
340
|
+
_advance();
|
|
341
|
+
_skipSpacesAndTabs();
|
|
342
|
+
}
|
|
343
|
+
start = pos;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
out += _decodeEscape();
|
|
347
|
+
start = pos;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
_advance();
|
|
351
|
+
}
|
|
352
|
+
throw _err("unterminated multi-line basic string", "toml/unterminated-string");
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function _parseLiteralString() {
|
|
356
|
+
_advance();
|
|
357
|
+
var start = pos;
|
|
358
|
+
while (!_eof()) {
|
|
359
|
+
if (_peek() === "'") {
|
|
360
|
+
var s = input.substring(start, pos);
|
|
361
|
+
_advance();
|
|
362
|
+
return s;
|
|
363
|
+
}
|
|
364
|
+
if (_peek() === "\n") throw _err("newline in literal string", "toml/bad-string");
|
|
365
|
+
_advance();
|
|
366
|
+
}
|
|
367
|
+
throw _err("unterminated literal string", "toml/unterminated-string");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function _parseMultilineLiteralString() {
|
|
371
|
+
_advance(3);
|
|
372
|
+
if (_peek() === "\n") _advance(1);
|
|
373
|
+
else if (_peek() === "\r" && _peek(1) === "\n") _advance(2);
|
|
374
|
+
var start = pos;
|
|
375
|
+
while (!_eof()) {
|
|
376
|
+
if (_peek() === "'" && _peek(1) === "'" && _peek(2) === "'") {
|
|
377
|
+
var s = input.substring(start, pos);
|
|
378
|
+
_advance(3);
|
|
379
|
+
var trailing = 0;
|
|
380
|
+
while (trailing < 2 && _peek() === "'") { s += "'"; _advance(); trailing += 1; }
|
|
381
|
+
return s;
|
|
382
|
+
}
|
|
383
|
+
_advance();
|
|
384
|
+
}
|
|
385
|
+
throw _err("unterminated multi-line literal string", "toml/unterminated-string");
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Date-time, date-only, or time-only.
|
|
389
|
+
// Returns { kind, value } where for "offset" value is a JS Date and for
|
|
390
|
+
// the others value is the canonical ISO string. Returns null if the
|
|
391
|
+
// current position doesn't start a date-time literal.
|
|
392
|
+
function _tryParseDateTime() {
|
|
393
|
+
// Date-time form: YYYY-MM-DD followed by 'T'/' ' followed by time
|
|
394
|
+
if (pos + 10 <= len && /^\d{4}-\d{2}-\d{2}/.test(input.substr(pos, 10))) {
|
|
395
|
+
var sep = _peek(10);
|
|
396
|
+
if (sep === "T" || sep === "t" || sep === " ") {
|
|
397
|
+
// Look ahead for time portion (HH:MM:SS = 8 chars)
|
|
398
|
+
var timeStart = pos + 11;
|
|
399
|
+
var timeChars = TIME_CHARS;
|
|
400
|
+
if (/^\d{2}:\d{2}:\d{2}/.test(input.substr(timeStart, timeChars))) {
|
|
401
|
+
var datePart = input.substr(pos, 10);
|
|
402
|
+
var timeEnd = timeStart + timeChars;
|
|
403
|
+
// Optional fractional
|
|
404
|
+
if (_peek(timeEnd - pos) === ".") {
|
|
405
|
+
timeEnd += 1;
|
|
406
|
+
while (timeEnd < len && /\d/.test(input.charAt(timeEnd))) timeEnd += 1;
|
|
407
|
+
}
|
|
408
|
+
var timePart = input.substring(timeStart, timeEnd);
|
|
409
|
+
// Optional offset
|
|
410
|
+
var offsetStr = "";
|
|
411
|
+
var nextChar = input.charAt(timeEnd);
|
|
412
|
+
if (nextChar === "Z" || nextChar === "z") { offsetStr = "Z"; timeEnd += 1; }
|
|
413
|
+
else if (nextChar === "+" || nextChar === "-") {
|
|
414
|
+
// Expect ±HH:MM (6 chars)
|
|
415
|
+
var offsetChars = OFFSET_CHARS;
|
|
416
|
+
var off = input.substr(timeEnd, offsetChars);
|
|
417
|
+
if (/^[+-]\d{2}:\d{2}$/.test(off)) {
|
|
418
|
+
offsetStr = off;
|
|
419
|
+
timeEnd += offsetChars;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
_advance(timeEnd - pos);
|
|
423
|
+
if (offsetStr) {
|
|
424
|
+
var iso = datePart + "T" + timePart + offsetStr;
|
|
425
|
+
var d = new Date(iso);
|
|
426
|
+
if (isNaN(d.getTime())) throw _err("invalid offset date-time", "toml/bad-datetime");
|
|
427
|
+
return { kind: "offset", value: d };
|
|
428
|
+
}
|
|
429
|
+
return { kind: "local-dt", value: datePart + "T" + timePart };
|
|
430
|
+
}
|
|
431
|
+
// 'T' / ' ' followed by non-time → fall through to date-only check below
|
|
432
|
+
}
|
|
433
|
+
// Date-only — but only if followed by something OTHER than digit/colon
|
|
434
|
+
var after = _peek(10);
|
|
435
|
+
if (!after || !/[0-9:.]/.test(after)) {
|
|
436
|
+
var ds = input.substr(pos, 10);
|
|
437
|
+
_advance(10);
|
|
438
|
+
return { kind: "local-date", value: ds };
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
// Time-only: HH:MM:SS (8 chars)
|
|
442
|
+
var timeOnlyChars = TIME_CHARS;
|
|
443
|
+
if (pos + timeOnlyChars <= len && /^\d{2}:\d{2}:\d{2}/.test(input.substr(pos, timeOnlyChars))) {
|
|
444
|
+
var teEnd = pos + timeOnlyChars;
|
|
445
|
+
if (input.charAt(teEnd) === ".") {
|
|
446
|
+
teEnd += 1;
|
|
447
|
+
while (teEnd < len && /\d/.test(input.charAt(teEnd))) teEnd += 1;
|
|
448
|
+
}
|
|
449
|
+
var ts = input.substring(pos, teEnd);
|
|
450
|
+
_advance(teEnd - pos);
|
|
451
|
+
return { kind: "local-time", value: ts };
|
|
452
|
+
}
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function _parseNumber(firstChar) {
|
|
457
|
+
var startPos = pos;
|
|
458
|
+
var startLine = line;
|
|
459
|
+
var startCol = col;
|
|
460
|
+
|
|
461
|
+
// Special floats: inf, nan with optional sign
|
|
462
|
+
if (firstChar === "+" || firstChar === "-") {
|
|
463
|
+
var rest = input.substring(pos + 1, pos + 4);
|
|
464
|
+
if (rest === "inf") { _advance(4); return firstChar === "-" ? -Infinity : Infinity; }
|
|
465
|
+
if (rest === "nan") { _advance(4); return NaN; }
|
|
466
|
+
} else if (firstChar === "i" || firstChar === "n") {
|
|
467
|
+
var w = input.substring(pos, pos + 3);
|
|
468
|
+
if (w === "inf") { _advance(3); return Infinity; }
|
|
469
|
+
if (w === "nan") { _advance(3); return NaN; }
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
var sign = 1;
|
|
473
|
+
if (_peek() === "+") _advance();
|
|
474
|
+
else if (_peek() === "-") { sign = -1; _advance(); }
|
|
475
|
+
|
|
476
|
+
if (_peek() === "0") {
|
|
477
|
+
var prefix = _peek(1);
|
|
478
|
+
if (prefix === "x" || prefix === "o" || prefix === "b") {
|
|
479
|
+
if (sign !== 1) throw _err("sign not allowed on hex/oct/bin literal", "toml/bad-number");
|
|
480
|
+
_advance(2);
|
|
481
|
+
var radix = prefix === "x" ? RADIX_HEX : prefix === "o" ? RADIX_OCTAL : RADIX_BIN;
|
|
482
|
+
var digitsStart = pos;
|
|
483
|
+
while (!_eof()) {
|
|
484
|
+
var ch = _peek();
|
|
485
|
+
if (ch === "_") { _advance(); continue; }
|
|
486
|
+
if (radix === RADIX_HEX && /[0-9a-fA-F]/.test(ch)) { _advance(); continue; }
|
|
487
|
+
if (radix === RADIX_OCTAL && /[0-7]/.test(ch)) { _advance(); continue; }
|
|
488
|
+
if (radix === RADIX_BIN && /[01]/.test(ch)) { _advance(); continue; }
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
var digits = input.substring(digitsStart, pos).replace(/_/g, "");
|
|
492
|
+
if (digits.length === 0) throw _err("expected digits after radix prefix", "toml/bad-number");
|
|
493
|
+
var n = parseInt(digits, radix);
|
|
494
|
+
if (!Number.isSafeInteger(n)) {
|
|
495
|
+
throw new SafeTomlError("integer overflow (use a quoted string for 64-bit values)",
|
|
496
|
+
"toml/integer-overflow", startLine, startCol);
|
|
497
|
+
}
|
|
498
|
+
return n;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
var hasDot = false;
|
|
503
|
+
var hasExp = false;
|
|
504
|
+
while (!_eof()) {
|
|
505
|
+
var ch2 = _peek();
|
|
506
|
+
if (ch2 === "_") { _advance(); continue; }
|
|
507
|
+
if (ch2 >= "0" && ch2 <= "9") { _advance(); continue; }
|
|
508
|
+
if (ch2 === "." && !hasDot && !hasExp) { hasDot = true; _advance(); continue; }
|
|
509
|
+
if ((ch2 === "e" || ch2 === "E") && !hasExp) {
|
|
510
|
+
hasExp = true;
|
|
511
|
+
_advance();
|
|
512
|
+
if (_peek() === "+" || _peek() === "-") _advance();
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
var raw = input.substring(startPos, pos).replace(/_/g, "");
|
|
518
|
+
if (raw === "" || raw === "-" || raw === "+") {
|
|
519
|
+
throw new SafeTomlError("invalid number", "toml/bad-number", startLine, startCol);
|
|
520
|
+
}
|
|
521
|
+
if (hasDot || hasExp) {
|
|
522
|
+
var f = parseFloat(raw);
|
|
523
|
+
if (isNaN(f)) {
|
|
524
|
+
throw new SafeTomlError("invalid float", "toml/bad-number", startLine, startCol);
|
|
525
|
+
}
|
|
526
|
+
return f;
|
|
527
|
+
}
|
|
528
|
+
var i = parseInt(raw, 10);
|
|
529
|
+
if (!Number.isSafeInteger(i)) {
|
|
530
|
+
throw new SafeTomlError("integer overflow (use a quoted string for 64-bit values)",
|
|
531
|
+
"toml/integer-overflow", startLine, startCol);
|
|
532
|
+
}
|
|
533
|
+
return i;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function _parseValue(depth) {
|
|
537
|
+
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
538
|
+
_skipSpacesAndTabs();
|
|
539
|
+
if (_eof()) throw _err("expected value, got EOF", "toml/expected-value");
|
|
540
|
+
|
|
541
|
+
var c = _peek();
|
|
542
|
+
|
|
543
|
+
if (c === '"' && _peek(1) === '"' && _peek(2) === '"') return _parseMultilineBasicString();
|
|
544
|
+
if (c === "'" && _peek(1) === "'" && _peek(2) === "'") return _parseMultilineLiteralString();
|
|
545
|
+
if (c === '"') return _parseBasicString();
|
|
546
|
+
if (c === "'") return _parseLiteralString();
|
|
547
|
+
if (c === "[") return _parseArray(depth + 1);
|
|
548
|
+
if (c === "{") return _parseInlineTable(depth + 1);
|
|
549
|
+
|
|
550
|
+
if (input.substr(pos, 4) === "true" && !/[A-Za-z0-9_]/.test(input.charAt(pos + 4) || "")) {
|
|
551
|
+
_advance(4); return true;
|
|
552
|
+
}
|
|
553
|
+
if (input.substr(pos, 5) === "false" && !/[A-Za-z0-9_]/.test(input.charAt(pos + 5) || "")) {
|
|
554
|
+
_advance(5); return false;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
var dt = _tryParseDateTime();
|
|
558
|
+
if (dt !== null) return dt.value;
|
|
559
|
+
|
|
560
|
+
if (/[0-9+\-i n]/.test(c)) return _parseNumber(c);
|
|
561
|
+
|
|
562
|
+
throw _err("unexpected character '" + c + "'", "toml/expected-value");
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function _parseArray(depth) {
|
|
566
|
+
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
567
|
+
_advance();
|
|
568
|
+
var arr = [];
|
|
569
|
+
while (true) {
|
|
570
|
+
_skipArrayWhitespace();
|
|
571
|
+
if (_peek() === "]") { _advance(); return arr; }
|
|
572
|
+
arr.push(_parseValue(depth));
|
|
573
|
+
_skipArrayWhitespace();
|
|
574
|
+
if (_peek() === ",") { _advance(); continue; }
|
|
575
|
+
_skipArrayWhitespace();
|
|
576
|
+
if (_peek() === "]") { _advance(); return arr; }
|
|
577
|
+
throw _err("expected ',' or ']' in array", "toml/bad-array");
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function _skipArrayWhitespace() {
|
|
582
|
+
while (!_eof()) {
|
|
583
|
+
var c = _peek();
|
|
584
|
+
if (c === " " || c === "\t" || c === "\n" || c === "\r") { _advance(); continue; }
|
|
585
|
+
if (c === "#") {
|
|
586
|
+
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function _parseInlineTable(depth) {
|
|
594
|
+
if (depth > maxDepth) throw _err("input exceeds maxDepth", "toml/too-deep");
|
|
595
|
+
_advance();
|
|
596
|
+
var tbl = Object.create(null);
|
|
597
|
+
_markInlineTable(tbl);
|
|
598
|
+
_skipSpacesAndTabs();
|
|
599
|
+
if (_peek() === "}") { _advance(); return tbl; }
|
|
600
|
+
while (true) {
|
|
601
|
+
_skipSpacesAndTabs();
|
|
602
|
+
var pathSegs = _parseDottedKey();
|
|
603
|
+
_skipSpacesAndTabs();
|
|
604
|
+
if (_peek() !== "=") throw _err("expected '=' in inline table", "toml/bad-inline-table");
|
|
605
|
+
_advance();
|
|
606
|
+
_skipSpacesAndTabs();
|
|
607
|
+
var v = _parseValue(depth);
|
|
608
|
+
_bumpKeys();
|
|
609
|
+
_setNested(tbl, pathSegs, v);
|
|
610
|
+
_skipSpacesAndTabs();
|
|
611
|
+
if (_peek() === ",") {
|
|
612
|
+
_advance();
|
|
613
|
+
if (_peek() === "}") throw _err("trailing comma in inline table", "toml/bad-inline-table");
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (_peek() === "}") { _advance(); return tbl; }
|
|
617
|
+
throw _err("expected ',' or '}' in inline table", "toml/bad-inline-table");
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function _setNested(table, segments, value) {
|
|
622
|
+
var t = table;
|
|
623
|
+
for (var i = 0; i < segments.length - 1; i++) {
|
|
624
|
+
var seg = segments[i];
|
|
625
|
+
if (POISONED_KEYS.has(seg)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
626
|
+
if (Object.prototype.hasOwnProperty.call(t, seg)) {
|
|
627
|
+
var sub = t[seg];
|
|
628
|
+
if (sub == null || typeof sub !== "object" || Array.isArray(sub)) {
|
|
629
|
+
throw _err("cannot redefine '" + seg + "' as a sub-table", "toml/redefine");
|
|
630
|
+
}
|
|
631
|
+
if (_isInlineTable(sub)) {
|
|
632
|
+
throw _err("cannot extend inline table '" + seg + "'", "toml/inline-table-mutated");
|
|
633
|
+
}
|
|
634
|
+
t = sub;
|
|
635
|
+
} else {
|
|
636
|
+
var fresh = Object.create(null);
|
|
637
|
+
t[seg] = fresh;
|
|
638
|
+
t = fresh;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
var last = segments[segments.length - 1];
|
|
642
|
+
if (POISONED_KEYS.has(last)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
643
|
+
if (Object.prototype.hasOwnProperty.call(t, last)) {
|
|
644
|
+
throw _err("duplicate key '" + last + "'", "toml/duplicate-key");
|
|
645
|
+
}
|
|
646
|
+
t[last] = value;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function _parseTableHeader() {
|
|
650
|
+
var isAoT = _peek(1) === "[";
|
|
651
|
+
_advance(isAoT ? 2 : 1);
|
|
652
|
+
_skipSpacesAndTabs();
|
|
653
|
+
var segments = _parseDottedKey();
|
|
654
|
+
_skipSpacesAndTabs();
|
|
655
|
+
if (isAoT) {
|
|
656
|
+
if (_peek() !== "]" || _peek(1) !== "]") throw _err("expected ']]'", "toml/bad-table-header");
|
|
657
|
+
_advance(2);
|
|
658
|
+
} else {
|
|
659
|
+
if (_peek() !== "]") throw _err("expected ']'", "toml/bad-table-header");
|
|
660
|
+
_advance();
|
|
661
|
+
}
|
|
662
|
+
_skipSpacesAndTabs();
|
|
663
|
+
_skipCommentAndNewline();
|
|
664
|
+
|
|
665
|
+
var t = root;
|
|
666
|
+
for (var i = 0; i < segments.length; i++) {
|
|
667
|
+
var seg = segments[i];
|
|
668
|
+
if (POISONED_KEYS.has(seg)) throw _err("forbidden key segment", "toml/poisoned-key");
|
|
669
|
+
var isLast = (i === segments.length - 1);
|
|
670
|
+
if (Object.prototype.hasOwnProperty.call(t, seg)) {
|
|
671
|
+
var sub = t[seg];
|
|
672
|
+
if (Array.isArray(sub)) {
|
|
673
|
+
if (isLast && isAoT) {
|
|
674
|
+
var fresh = Object.create(null);
|
|
675
|
+
sub.push(fresh);
|
|
676
|
+
current = fresh;
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (isLast && !isAoT) {
|
|
680
|
+
throw _err("table '" + seg + "' previously defined as array of tables",
|
|
681
|
+
"toml/redefine");
|
|
682
|
+
}
|
|
683
|
+
t = sub[sub.length - 1];
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
if (typeof sub !== "object" || sub === null) {
|
|
687
|
+
throw _err("cannot redefine '" + seg + "' as a sub-table", "toml/redefine");
|
|
688
|
+
}
|
|
689
|
+
if (_isInlineTable(sub)) {
|
|
690
|
+
throw _err("cannot extend inline table '" + seg + "'", "toml/inline-table-mutated");
|
|
691
|
+
}
|
|
692
|
+
if (isLast && !isAoT) {
|
|
693
|
+
var fullPath = segments.slice(0, i + 1).join(".");
|
|
694
|
+
if (definedTables.has(fullPath)) {
|
|
695
|
+
throw _err("table '" + fullPath + "' defined twice", "toml/redefine");
|
|
696
|
+
}
|
|
697
|
+
definedTables.add(fullPath);
|
|
698
|
+
current = sub;
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (isLast && isAoT) {
|
|
702
|
+
throw _err("'" + seg + "' was a table; cannot redefine as array of tables",
|
|
703
|
+
"toml/redefine");
|
|
704
|
+
}
|
|
705
|
+
t = sub;
|
|
706
|
+
} else {
|
|
707
|
+
if (isLast && isAoT) {
|
|
708
|
+
var arr = [];
|
|
709
|
+
t[seg] = arr;
|
|
710
|
+
var freshT = Object.create(null);
|
|
711
|
+
arr.push(freshT);
|
|
712
|
+
current = freshT;
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
if (isLast && !isAoT) {
|
|
716
|
+
var freshT2 = Object.create(null);
|
|
717
|
+
t[seg] = freshT2;
|
|
718
|
+
var fullPath2 = segments.slice(0, i + 1).join(".");
|
|
719
|
+
definedTables.add(fullPath2);
|
|
720
|
+
current = freshT2;
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
var freshParent = Object.create(null);
|
|
724
|
+
t[seg] = freshParent;
|
|
725
|
+
t = freshParent;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function _parseKeyValueLine() {
|
|
731
|
+
var pathSegs = _parseDottedKey();
|
|
732
|
+
_skipSpacesAndTabs();
|
|
733
|
+
if (_peek() !== "=") throw _err("expected '=' after key", "toml/bad-kv");
|
|
734
|
+
_advance();
|
|
735
|
+
_skipSpacesAndTabs();
|
|
736
|
+
var v = _parseValue(1);
|
|
737
|
+
_bumpKeys();
|
|
738
|
+
_setNested(current, pathSegs, v);
|
|
739
|
+
_skipSpacesAndTabs();
|
|
740
|
+
_skipCommentAndNewline();
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
while (!_eof()) {
|
|
744
|
+
_skipSpacesAndTabs();
|
|
745
|
+
if (_eof()) break;
|
|
746
|
+
var c = _peek();
|
|
747
|
+
if (c === "#") {
|
|
748
|
+
while (!_eof() && _peekCode() !== 0x0A) _advance();
|
|
749
|
+
if (!_eof()) _skipNewline();
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
if (c === "\n" || (c === "\r" && _peek(1) === "\n")) {
|
|
753
|
+
_skipNewline();
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
if (c === "[") {
|
|
757
|
+
_parseTableHeader();
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
_parseKeyValueLine();
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// Strip null-prototype objects to plain {}-prototype for the operator;
|
|
764
|
+
// null-prototype is only needed during build to block prototype pollution.
|
|
765
|
+
function _normalize(value) {
|
|
766
|
+
if (Array.isArray(value)) return value.map(_normalize);
|
|
767
|
+
if (value && typeof value === "object" && !(value instanceof Date)) {
|
|
768
|
+
var out = {};
|
|
769
|
+
for (var k in value) {
|
|
770
|
+
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
|
771
|
+
out[k] = _normalize(value[k]);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
return out;
|
|
775
|
+
}
|
|
776
|
+
return value;
|
|
777
|
+
}
|
|
778
|
+
return _normalize(root);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
module.exports = {
|
|
782
|
+
parse: parse,
|
|
783
|
+
SafeTomlError: SafeTomlError,
|
|
784
|
+
};
|