devanvil 2.1.0 → 2.1.2
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/README.ar.md +48 -23
- package/README.md +12 -8
- package/SECURITY.md +36 -16
- package/dist/cli.js +72 -5
- package/dist/cli.js.map +1 -0
- package/dist/core/contract-drift.js +16 -5
- package/dist/core/contract-drift.js.map +1 -0
- package/dist/core/converters.d.ts +3 -1
- package/dist/core/converters.js +317 -37
- package/dist/core/converters.js.map +1 -0
- package/dist/core/cron.js +38 -4
- package/dist/core/cron.js.map +1 -0
- package/dist/core/crypto.js +12 -1
- package/dist/core/crypto.js.map +1 -0
- package/dist/core/curl.js +6 -1
- package/dist/core/curl.js.map +1 -0
- package/dist/core/diff.js +3 -2
- package/dist/core/diff.js.map +1 -0
- package/dist/core/encoding.js +2 -1
- package/dist/core/encoding.js.map +1 -0
- package/dist/core/importers.js +1 -0
- package/dist/core/importers.js.map +1 -0
- package/dist/core/jwt.js +22 -8
- package/dist/core/jwt.js.map +1 -0
- package/dist/core/llm-tokens.js +1 -0
- package/dist/core/llm-tokens.js.map +1 -0
- package/dist/core/log-analyzer.js +1 -0
- package/dist/core/log-analyzer.js.map +1 -0
- package/dist/core/network.js +1 -0
- package/dist/core/network.js.map +1 -0
- package/dist/core/oauth-protobuf.js +17 -10
- package/dist/core/oauth-protobuf.js.map +1 -0
- package/dist/core/openapi.js +1 -0
- package/dist/core/openapi.js.map +1 -0
- package/dist/core/pipeline.js +1 -0
- package/dist/core/pipeline.js.map +1 -0
- package/dist/core/regex.js +14 -4
- package/dist/core/regex.js.map +1 -0
- package/dist/core/runner.js +1 -0
- package/dist/core/runner.js.map +1 -0
- package/dist/core/secrets.js +12 -7
- package/dist/core/secrets.js.map +1 -0
- package/dist/core/security.d.ts +24 -1
- package/dist/core/security.js +122 -4
- package/dist/core/security.js.map +1 -0
- package/dist/core/sql-explain.js +1 -0
- package/dist/core/sql-explain.js.map +1 -0
- package/dist/core/tls.js +6 -2
- package/dist/core/tls.js.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/server/app.d.ts +5 -1
- package/dist/server/app.js +157 -13
- package/dist/server/app.js.map +1 -0
- package/dist/server/http-proxy.js +20 -10
- package/dist/server/http-proxy.js.map +1 -0
- package/dist/server/http-utils.d.ts +9 -0
- package/dist/server/http-utils.js +36 -3
- package/dist/server/http-utils.js.map +1 -0
- package/dist/server/load-tester.js +4 -0
- package/dist/server/load-tester.js.map +1 -0
- package/dist/server/mcp.d.ts +1 -1
- package/dist/server/mcp.js +12 -7
- package/dist/server/mcp.js.map +1 -0
- package/dist/server/mock-store.js +55 -13
- package/dist/server/mock-store.js.map +1 -0
- package/dist/server/plugin-loader.d.ts +2 -1
- package/dist/server/plugin-loader.js +22 -1
- package/dist/server/plugin-loader.js.map +1 -0
- package/dist/server/recorder-store.js +51 -2
- package/dist/server/recorder-store.js.map +1 -0
- package/dist/server/routes/network-routes.js +65 -10
- package/dist/server/routes/network-routes.js.map +1 -0
- package/dist/server/routes/tools-routes.js +10 -2
- package/dist/server/routes/tools-routes.js.map +1 -0
- package/dist/server/routes/workspace-routes.js +29 -6
- package/dist/server/routes/workspace-routes.js.map +1 -0
- package/dist/server/webhook-store.d.ts +1 -0
- package/dist/server/webhook-store.js +16 -2
- package/dist/server/webhook-store.js.map +1 -0
- package/dist/server/workspace-store.d.ts +4 -1
- package/dist/server/workspace-store.js +32 -7
- package/dist/server/workspace-store.js.map +1 -0
- package/dist/server/ws-probe.js +9 -0
- package/dist/server/ws-probe.js.map +1 -0
- package/dist/ui/app.js +83 -7
- package/dist/ui/styles.css +2 -2
- package/dist/ui/tools-data.js +47 -30
- package/dist/ui/tools-network.js +102 -28
- package/dist/ui/tools-ops.js +16 -12
- package/dist/ui/tools-pro.js +1 -0
- package/dist/ui/tools-security.js +30 -22
- package/dist/ui/tools-v12.js +41 -14
- package/dist/ui/tools-v21.js +26 -10
- package/package.json +4 -3
package/dist/core/converters.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isDangerousKey,
|
|
1
|
+
import { isDangerousKey, isProtoKey, safeDataJsonParse, sanitizeDataObject } from './security.js';
|
|
2
2
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
3
3
|
// YAML Parser & Serializer (Zero-dependency, supports objects, arrays, quotes,
|
|
4
4
|
// inline collections, comments, booleans, numbers, null, multiline | / > blocks)
|
|
@@ -24,7 +24,7 @@ function parseYamlScalar(raw) {
|
|
|
24
24
|
}
|
|
25
25
|
if ((s.startsWith('[') && s.endsWith(']')) || (s.startsWith('{') && s.endsWith('}'))) {
|
|
26
26
|
try {
|
|
27
|
-
return
|
|
27
|
+
return safeDataJsonParse(s);
|
|
28
28
|
}
|
|
29
29
|
catch {
|
|
30
30
|
if (s.startsWith('[') && s.endsWith(']')) {
|
|
@@ -42,13 +42,13 @@ function parseYamlScalar(raw) {
|
|
|
42
42
|
const colonIdx = findUnquotedChar(pair, ':');
|
|
43
43
|
if (colonIdx > 0) {
|
|
44
44
|
const k = stripQuotes(pair.slice(0, colonIdx).trim());
|
|
45
|
-
if (
|
|
45
|
+
if (isProtoKey(k))
|
|
46
46
|
continue;
|
|
47
47
|
const v = parseYamlScalar(pair.slice(colonIdx + 1));
|
|
48
48
|
obj[k] = v;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
-
return
|
|
51
|
+
return sanitizeDataObject(obj);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -146,20 +146,24 @@ export function parseYaml(input) {
|
|
|
146
146
|
if (lines.length === 0)
|
|
147
147
|
return {};
|
|
148
148
|
let idx = 0;
|
|
149
|
-
function parseBlock(minIndent) {
|
|
149
|
+
function parseBlock(minIndent, depth = 0) {
|
|
150
|
+
if (depth > 64)
|
|
151
|
+
throw new Error('YAML nesting depth limit exceeded (max 64)');
|
|
150
152
|
if (idx >= lines.length)
|
|
151
153
|
return null;
|
|
152
154
|
const first = lines[idx];
|
|
153
155
|
if (first.text.startsWith('- ') || first.text === '-') {
|
|
154
|
-
return parseSequence(first.indent);
|
|
156
|
+
return parseSequence(first.indent, depth + 1);
|
|
155
157
|
}
|
|
156
158
|
if (findUnquotedChar(first.text, ':') !== -1) {
|
|
157
|
-
return parseMapping(first.indent);
|
|
159
|
+
return parseMapping(first.indent, depth + 1);
|
|
158
160
|
}
|
|
159
161
|
idx++;
|
|
160
162
|
return parseYamlScalar(first.text);
|
|
161
163
|
}
|
|
162
|
-
function parseSequence(seqIndent) {
|
|
164
|
+
function parseSequence(seqIndent, depth = 0) {
|
|
165
|
+
if (depth > 64)
|
|
166
|
+
throw new Error('YAML nesting depth limit exceeded (max 64)');
|
|
163
167
|
const arr = [];
|
|
164
168
|
while (idx < lines.length) {
|
|
165
169
|
const line = lines[idx];
|
|
@@ -171,7 +175,7 @@ export function parseYaml(input) {
|
|
|
171
175
|
idx++;
|
|
172
176
|
if (!rest) {
|
|
173
177
|
if (idx < lines.length && lines[idx].indent > seqIndent) {
|
|
174
|
-
arr.push(parseBlock(lines[idx].indent));
|
|
178
|
+
arr.push(parseBlock(lines[idx].indent, depth + 1));
|
|
175
179
|
}
|
|
176
180
|
else {
|
|
177
181
|
arr.push(null);
|
|
@@ -190,12 +194,12 @@ export function parseYaml(input) {
|
|
|
190
194
|
firstVal = parseYamlScalar(vRaw);
|
|
191
195
|
}
|
|
192
196
|
else if (idx < lines.length && lines[idx].indent > itemIndent) {
|
|
193
|
-
firstVal = parseBlock(lines[idx].indent);
|
|
197
|
+
firstVal = parseBlock(lines[idx].indent, depth + 1);
|
|
194
198
|
}
|
|
195
199
|
else {
|
|
196
200
|
firstVal = null;
|
|
197
201
|
}
|
|
198
|
-
if (!
|
|
202
|
+
if (!isProtoKey(k))
|
|
199
203
|
itemObj[k] = firstVal;
|
|
200
204
|
// Gather sibling keys belonging to this sequence item's object
|
|
201
205
|
while (idx < lines.length && lines[idx].indent >= itemIndent) {
|
|
@@ -216,12 +220,12 @@ export function parseYaml(input) {
|
|
|
216
220
|
parsedSubVal = parseYamlScalar(subVal);
|
|
217
221
|
}
|
|
218
222
|
else if (idx < lines.length && lines[idx].indent > sub.indent) {
|
|
219
|
-
parsedSubVal = parseBlock(lines[idx].indent);
|
|
223
|
+
parsedSubVal = parseBlock(lines[idx].indent, depth + 1);
|
|
220
224
|
}
|
|
221
225
|
else {
|
|
222
226
|
parsedSubVal = null;
|
|
223
227
|
}
|
|
224
|
-
if (!
|
|
228
|
+
if (!isProtoKey(subKey))
|
|
225
229
|
itemObj[subKey] = parsedSubVal;
|
|
226
230
|
}
|
|
227
231
|
arr.push(itemObj);
|
|
@@ -241,7 +245,9 @@ export function parseYaml(input) {
|
|
|
241
245
|
}
|
|
242
246
|
return chunks.join(folded ? ' ' : '\n');
|
|
243
247
|
}
|
|
244
|
-
function parseMapping(mapIndent) {
|
|
248
|
+
function parseMapping(mapIndent, depth = 0) {
|
|
249
|
+
if (depth > 64)
|
|
250
|
+
throw new Error('YAML nesting depth limit exceeded (max 64)');
|
|
245
251
|
const obj = Object.create(null);
|
|
246
252
|
while (idx < lines.length) {
|
|
247
253
|
const line = lines[idx];
|
|
@@ -266,21 +272,21 @@ export function parseYaml(input) {
|
|
|
266
272
|
parsedVal = parseYamlScalar(valRaw);
|
|
267
273
|
}
|
|
268
274
|
else if (idx < lines.length && lines[idx].indent > mapIndent) {
|
|
269
|
-
parsedVal = parseBlock(lines[idx].indent);
|
|
275
|
+
parsedVal = parseBlock(lines[idx].indent, depth + 1);
|
|
270
276
|
}
|
|
271
277
|
else if (idx < lines.length && lines[idx].indent === mapIndent && lines[idx].text.startsWith('-')) {
|
|
272
|
-
parsedVal = parseSequence(mapIndent);
|
|
278
|
+
parsedVal = parseSequence(mapIndent, depth + 1);
|
|
273
279
|
}
|
|
274
280
|
else {
|
|
275
281
|
parsedVal = null;
|
|
276
282
|
}
|
|
277
|
-
if (!
|
|
283
|
+
if (!isProtoKey(key)) {
|
|
278
284
|
obj[key] = parsedVal;
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
287
|
return obj;
|
|
282
288
|
}
|
|
283
|
-
return
|
|
289
|
+
return sanitizeDataObject(parseBlock(lines[0].indent));
|
|
284
290
|
}
|
|
285
291
|
function needsYamlQuotes(str) {
|
|
286
292
|
if (str === '')
|
|
@@ -347,7 +353,9 @@ export function stringifyYaml(value, indent = 0) {
|
|
|
347
353
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
348
354
|
// TOML Parser & Serializer
|
|
349
355
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
350
|
-
function parseTomlValue(raw) {
|
|
356
|
+
function parseTomlValue(raw, depth = 0) {
|
|
357
|
+
if (depth > 32)
|
|
358
|
+
throw new Error('TOML inline nesting depth limit exceeded (max 32)');
|
|
351
359
|
const s = raw.trim();
|
|
352
360
|
if (s === 'true')
|
|
353
361
|
return true;
|
|
@@ -362,7 +370,7 @@ function parseTomlValue(raw) {
|
|
|
362
370
|
const inner = s.slice(1, -1).trim();
|
|
363
371
|
if (!inner)
|
|
364
372
|
return [];
|
|
365
|
-
return splitTopLevel(inner, ',').map((part) => parseTomlValue(part));
|
|
373
|
+
return splitTopLevel(inner, ',').map((part) => parseTomlValue(part, depth + 1));
|
|
366
374
|
}
|
|
367
375
|
if (s.startsWith('{') && s.endsWith('}')) {
|
|
368
376
|
const inner = s.slice(1, -1).trim();
|
|
@@ -373,12 +381,12 @@ function parseTomlValue(raw) {
|
|
|
373
381
|
const eq = findUnquotedChar(pair, '=');
|
|
374
382
|
if (eq > 0) {
|
|
375
383
|
const k = stripQuotes(pair.slice(0, eq).trim());
|
|
376
|
-
if (!
|
|
377
|
-
obj[k] = parseTomlValue(pair.slice(eq + 1));
|
|
384
|
+
if (!isProtoKey(k)) {
|
|
385
|
+
obj[k] = parseTomlValue(pair.slice(eq + 1), depth + 1);
|
|
378
386
|
}
|
|
379
387
|
}
|
|
380
388
|
}
|
|
381
|
-
return
|
|
389
|
+
return sanitizeDataObject(obj);
|
|
382
390
|
}
|
|
383
391
|
const numCandidate = s.replace(/_/g, '');
|
|
384
392
|
if (/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(numCandidate)) {
|
|
@@ -400,7 +408,7 @@ export function parseToml(input) {
|
|
|
400
408
|
.slice(2, -2)
|
|
401
409
|
.split('.')
|
|
402
410
|
.map((p) => stripQuotes(p.trim()));
|
|
403
|
-
if (pathParts.some(
|
|
411
|
+
if (pathParts.some(isProtoKey)) {
|
|
404
412
|
ignoreCurrentTable = true;
|
|
405
413
|
continue;
|
|
406
414
|
}
|
|
@@ -426,7 +434,7 @@ export function parseToml(input) {
|
|
|
426
434
|
.slice(1, -1)
|
|
427
435
|
.split('.')
|
|
428
436
|
.map((p) => stripQuotes(p.trim()));
|
|
429
|
-
if (pathParts.some(
|
|
437
|
+
if (pathParts.some(isProtoKey)) {
|
|
430
438
|
ignoreCurrentTable = true;
|
|
431
439
|
continue;
|
|
432
440
|
}
|
|
@@ -451,7 +459,7 @@ export function parseToml(input) {
|
|
|
451
459
|
.slice(0, eqIdx)
|
|
452
460
|
.split('.')
|
|
453
461
|
.map((p) => stripQuotes(p.trim()));
|
|
454
|
-
if (keyPath.some(
|
|
462
|
+
if (keyPath.some(isProtoKey))
|
|
455
463
|
continue;
|
|
456
464
|
const val = parseTomlValue(line.slice(eqIdx + 1));
|
|
457
465
|
let target = current;
|
|
@@ -463,7 +471,7 @@ export function parseToml(input) {
|
|
|
463
471
|
}
|
|
464
472
|
target[keyPath[keyPath.length - 1]] = val;
|
|
465
473
|
}
|
|
466
|
-
return
|
|
474
|
+
return sanitizeDataObject(root);
|
|
467
475
|
}
|
|
468
476
|
function formatTomlScalar(v) {
|
|
469
477
|
if (v === null || v === undefined)
|
|
@@ -571,7 +579,7 @@ export function parseCsv(input, delimiter = ',') {
|
|
|
571
579
|
const obj = Object.create(null);
|
|
572
580
|
for (let c = 0; c < headers.length; c++) {
|
|
573
581
|
const header = headers[c] || `col_${c}`;
|
|
574
|
-
if (
|
|
582
|
+
if (isProtoKey(header))
|
|
575
583
|
continue;
|
|
576
584
|
const rawVal = currentRow[c] ?? '';
|
|
577
585
|
if (rawVal === 'true')
|
|
@@ -583,7 +591,7 @@ export function parseCsv(input, delimiter = ',') {
|
|
|
583
591
|
else
|
|
584
592
|
obj[header] = rawVal;
|
|
585
593
|
}
|
|
586
|
-
records.push(
|
|
594
|
+
records.push(sanitizeDataObject(obj));
|
|
587
595
|
}
|
|
588
596
|
return records;
|
|
589
597
|
}
|
|
@@ -619,7 +627,7 @@ export function parseQueryString(input) {
|
|
|
619
627
|
const params = new URLSearchParams(cleaned);
|
|
620
628
|
const out = Object.create(null);
|
|
621
629
|
for (const [k, v] of params.entries()) {
|
|
622
|
-
if (
|
|
630
|
+
if (isProtoKey(k))
|
|
623
631
|
continue;
|
|
624
632
|
if (k in out) {
|
|
625
633
|
const existing = out[k];
|
|
@@ -629,7 +637,7 @@ export function parseQueryString(input) {
|
|
|
629
637
|
out[k] = v;
|
|
630
638
|
}
|
|
631
639
|
}
|
|
632
|
-
return
|
|
640
|
+
return sanitizeDataObject(out);
|
|
633
641
|
}
|
|
634
642
|
export function stringifyQueryString(value) {
|
|
635
643
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
@@ -745,13 +753,272 @@ export function generateJsonSchema(value, title = 'GeneratedSchema') {
|
|
|
745
753
|
};
|
|
746
754
|
}
|
|
747
755
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
756
|
+
// XML Parser & Serializer (Zero-dependency, XXE/DTD-safe, depth-limited)
|
|
757
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
758
|
+
function decodeXmlEntities(text) {
|
|
759
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (_m, ent) => {
|
|
760
|
+
if (ent === 'amp')
|
|
761
|
+
return '&';
|
|
762
|
+
if (ent === 'lt')
|
|
763
|
+
return '<';
|
|
764
|
+
if (ent === 'gt')
|
|
765
|
+
return '>';
|
|
766
|
+
if (ent === 'quot')
|
|
767
|
+
return '"';
|
|
768
|
+
if (ent === 'apos')
|
|
769
|
+
return "'";
|
|
770
|
+
if (ent.startsWith('#x')) {
|
|
771
|
+
const cp = parseInt(ent.slice(2), 16);
|
|
772
|
+
return Number.isFinite(cp) && cp >= 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : _m;
|
|
773
|
+
}
|
|
774
|
+
if (ent.startsWith('#')) {
|
|
775
|
+
const cp = parseInt(ent.slice(1), 10);
|
|
776
|
+
return Number.isFinite(cp) && cp >= 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : _m;
|
|
777
|
+
}
|
|
778
|
+
return _m;
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
function escapeXmlText(text) {
|
|
782
|
+
return text
|
|
783
|
+
.replace(/&/g, '&')
|
|
784
|
+
.replace(/</g, '<')
|
|
785
|
+
.replace(/>/g, '>')
|
|
786
|
+
.replace(/"/g, '"')
|
|
787
|
+
.replace(/'/g, ''');
|
|
788
|
+
}
|
|
789
|
+
function coerceXmlScalar(raw) {
|
|
790
|
+
if (raw === 'true')
|
|
791
|
+
return true;
|
|
792
|
+
if (raw === 'false')
|
|
793
|
+
return false;
|
|
794
|
+
if (raw === 'null')
|
|
795
|
+
return null;
|
|
796
|
+
if (/^-?(?:0|[1-9]\d*)(?:\.\d+)?$/.test(raw)) {
|
|
797
|
+
const n = Number(raw);
|
|
798
|
+
if (Number.isFinite(n))
|
|
799
|
+
return n;
|
|
800
|
+
}
|
|
801
|
+
return raw;
|
|
802
|
+
}
|
|
803
|
+
export function parseXml(xmlInput) {
|
|
804
|
+
const MAX_XML_DEPTH = 32;
|
|
805
|
+
if (/<!(?:DOCTYPE|ENTITY)\b/i.test(xmlInput)) {
|
|
806
|
+
throw new Error('XML DOCTYPE/ENTITY declarations are blocked for XXE safety');
|
|
807
|
+
}
|
|
808
|
+
// Strip XML declaration and comments cleanly
|
|
809
|
+
const src = xmlInput
|
|
810
|
+
.replace(/^\uFEFF/, '')
|
|
811
|
+
.replace(/<\?[\s\S]*?\?>/g, '')
|
|
812
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
813
|
+
.trim();
|
|
814
|
+
if (!src)
|
|
815
|
+
throw new Error('XML input is empty');
|
|
816
|
+
let pos = 0;
|
|
817
|
+
function skipWs() {
|
|
818
|
+
while (pos < src.length && /\s/.test(src[pos]))
|
|
819
|
+
pos++;
|
|
820
|
+
}
|
|
821
|
+
function readName() {
|
|
822
|
+
const start = pos;
|
|
823
|
+
while (pos < src.length && /[A-Za-z0-9_.:-]/.test(src[pos]))
|
|
824
|
+
pos++;
|
|
825
|
+
const name = src.slice(start, pos);
|
|
826
|
+
if (!name)
|
|
827
|
+
throw new Error(`Expected XML name at offset ${pos}`);
|
|
828
|
+
return name;
|
|
829
|
+
}
|
|
830
|
+
function parseElement(depth) {
|
|
831
|
+
if (depth > MAX_XML_DEPTH) {
|
|
832
|
+
throw new Error(`XML nesting depth limit exceeded (${MAX_XML_DEPTH})`);
|
|
833
|
+
}
|
|
834
|
+
skipWs();
|
|
835
|
+
if (src[pos] !== '<')
|
|
836
|
+
throw new Error(`Expected '<' at offset ${pos}`);
|
|
837
|
+
pos++;
|
|
838
|
+
const tagName = readName();
|
|
839
|
+
const attrs = Object.create(null);
|
|
840
|
+
let hasAttrs = false;
|
|
841
|
+
while (pos < src.length) {
|
|
842
|
+
skipWs();
|
|
843
|
+
if (src.startsWith('/>', pos)) {
|
|
844
|
+
pos += 2;
|
|
845
|
+
return { name: tagName, value: hasAttrs ? sanitizeDataObject(attrs) : '' };
|
|
846
|
+
}
|
|
847
|
+
if (src[pos] === '>') {
|
|
848
|
+
pos++;
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
const attrName = readName();
|
|
852
|
+
skipWs();
|
|
853
|
+
if (src[pos] !== '=')
|
|
854
|
+
throw new Error(`Expected '=' after attribute "${attrName}" at offset ${pos}`);
|
|
855
|
+
pos++;
|
|
856
|
+
skipWs();
|
|
857
|
+
const quote = src[pos];
|
|
858
|
+
if (quote !== '"' && quote !== "'")
|
|
859
|
+
throw new Error(`Expected quoted value for attribute "${attrName}"`);
|
|
860
|
+
pos++;
|
|
861
|
+
const valStart = pos;
|
|
862
|
+
while (pos < src.length && src[pos] !== quote)
|
|
863
|
+
pos++;
|
|
864
|
+
if (pos >= src.length)
|
|
865
|
+
throw new Error(`Unterminated attribute value for "${attrName}"`);
|
|
866
|
+
const attrVal = decodeXmlEntities(src.slice(valStart, pos));
|
|
867
|
+
pos++;
|
|
868
|
+
if (!isProtoKey(attrName)) {
|
|
869
|
+
attrs[`@${attrName}`] = attrVal;
|
|
870
|
+
hasAttrs = true;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
const children = [];
|
|
874
|
+
let textParts = '';
|
|
875
|
+
while (pos < src.length) {
|
|
876
|
+
if (src.startsWith('</', pos)) {
|
|
877
|
+
pos += 2;
|
|
878
|
+
const closeName = readName();
|
|
879
|
+
skipWs();
|
|
880
|
+
if (src[pos] !== '>')
|
|
881
|
+
throw new Error(`Expected '>' after closing tag </${closeName}`);
|
|
882
|
+
pos++;
|
|
883
|
+
if (closeName !== tagName) {
|
|
884
|
+
throw new Error(`Mismatched XML closing tag: expected </${tagName}>, got </${closeName}>`);
|
|
885
|
+
}
|
|
886
|
+
break;
|
|
887
|
+
}
|
|
888
|
+
if (src.startsWith('<![CDATA[', pos)) {
|
|
889
|
+
const endCdata = src.indexOf(']]>', pos + 9);
|
|
890
|
+
if (endCdata === -1)
|
|
891
|
+
throw new Error('Unterminated CDATA section');
|
|
892
|
+
textParts += src.slice(pos + 9, endCdata);
|
|
893
|
+
pos = endCdata + 3;
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
if (src[pos] === '<') {
|
|
897
|
+
children.push(parseElement(depth + 1));
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
const nextTag = src.indexOf('<', pos);
|
|
901
|
+
if (nextTag === -1)
|
|
902
|
+
throw new Error(`Unclosed XML tag <${tagName}>`);
|
|
903
|
+
textParts += decodeXmlEntities(src.slice(pos, nextTag));
|
|
904
|
+
pos = nextTag;
|
|
905
|
+
}
|
|
906
|
+
const trimmedText = textParts.trim();
|
|
907
|
+
if (children.length === 0 && !hasAttrs) {
|
|
908
|
+
return { name: tagName, value: coerceXmlScalar(trimmedText) };
|
|
909
|
+
}
|
|
910
|
+
const nodeObj = Object.create(null);
|
|
911
|
+
if (hasAttrs) {
|
|
912
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
913
|
+
nodeObj[k] = v;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
for (const ch of children) {
|
|
917
|
+
if (isProtoKey(ch.name))
|
|
918
|
+
continue;
|
|
919
|
+
if (Object.prototype.hasOwnProperty.call(nodeObj, ch.name)) {
|
|
920
|
+
const existing = nodeObj[ch.name];
|
|
921
|
+
if (Array.isArray(existing))
|
|
922
|
+
existing.push(ch.value);
|
|
923
|
+
else
|
|
924
|
+
nodeObj[ch.name] = [existing, ch.value];
|
|
925
|
+
}
|
|
926
|
+
else {
|
|
927
|
+
nodeObj[ch.name] = ch.value;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (trimmedText) {
|
|
931
|
+
nodeObj['#text'] = coerceXmlScalar(trimmedText);
|
|
932
|
+
}
|
|
933
|
+
return { name: tagName, value: sanitizeDataObject(nodeObj) };
|
|
934
|
+
}
|
|
935
|
+
const root = parseElement(0);
|
|
936
|
+
if (isProtoKey(root.name))
|
|
937
|
+
throw new Error(`Invalid XML root tag: ${root.name}`);
|
|
938
|
+
const rootObj = Object.create(null);
|
|
939
|
+
rootObj[root.name] = root.value;
|
|
940
|
+
return sanitizeDataObject(rootObj);
|
|
941
|
+
}
|
|
942
|
+
function sanitizeXmlTagName(key) {
|
|
943
|
+
const clean = key.replace(/[^A-Za-z0-9_.-]/g, '_');
|
|
944
|
+
if (!clean || /^[^A-Za-z_]/.test(clean))
|
|
945
|
+
return `n_${clean || 'item'}`;
|
|
946
|
+
return clean;
|
|
947
|
+
}
|
|
948
|
+
export function stringifyXml(value, rootName = 'root', indentSize = 2) {
|
|
949
|
+
const lines = ['<?xml version="1.0" encoding="UTF-8"?>'];
|
|
950
|
+
function emit(tagRaw, val, depth) {
|
|
951
|
+
const tag = sanitizeXmlTagName(tagRaw);
|
|
952
|
+
const pad = indentSize > 0 ? ' '.repeat(depth * indentSize) : '';
|
|
953
|
+
if (val === null || val === undefined) {
|
|
954
|
+
lines.push(`${pad}<${tag} />`);
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (Array.isArray(val)) {
|
|
958
|
+
if (val.length === 0) {
|
|
959
|
+
lines.push(`${pad}<${tag} />`);
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
for (const item of val)
|
|
963
|
+
emit(tag, item, depth);
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (typeof val !== 'object') {
|
|
967
|
+
lines.push(`${pad}<${tag}>${escapeXmlText(String(val))}</${tag}>`);
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
const rec = val;
|
|
971
|
+
const attrPairs = [];
|
|
972
|
+
const childEntries = [];
|
|
973
|
+
let textVal;
|
|
974
|
+
for (const [k, v] of Object.entries(rec)) {
|
|
975
|
+
if (k.startsWith('@') && k.length > 1) {
|
|
976
|
+
attrPairs.push(`${sanitizeXmlTagName(k.slice(1))}="${escapeXmlText(String(v ?? ''))}"`);
|
|
977
|
+
}
|
|
978
|
+
else if (k === '#text') {
|
|
979
|
+
textVal = v;
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
childEntries.push([k, v]);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
const attrStr = attrPairs.length ? ` ${attrPairs.join(' ')}` : '';
|
|
986
|
+
if (childEntries.length === 0 && textVal === undefined) {
|
|
987
|
+
lines.push(`${pad}<${tag}${attrStr} />`);
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
if (childEntries.length === 0 && textVal !== undefined) {
|
|
991
|
+
lines.push(`${pad}<${tag}${attrStr}>${escapeXmlText(String(textVal))}</${tag}>`);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
lines.push(`${pad}<${tag}${attrStr}>`);
|
|
995
|
+
if (textVal !== undefined) {
|
|
996
|
+
lines.push(`${indentSize > 0 ? ' '.repeat((depth + 1) * indentSize) : ''}${escapeXmlText(String(textVal))}`);
|
|
997
|
+
}
|
|
998
|
+
for (const [ck, cv] of childEntries) {
|
|
999
|
+
emit(ck, cv, depth + 1);
|
|
1000
|
+
}
|
|
1001
|
+
lines.push(`${pad}</${tag}>`);
|
|
1002
|
+
}
|
|
1003
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
1004
|
+
const keys = Object.keys(value);
|
|
1005
|
+
if (keys.length === 1 && !keys[0].startsWith('@') && keys[0] !== '#text') {
|
|
1006
|
+
const onlyKey = keys[0];
|
|
1007
|
+
emit(onlyKey, value[onlyKey], 0);
|
|
1008
|
+
return lines.join(indentSize > 0 ? '\n' : '');
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
emit(rootName, value, 0);
|
|
1012
|
+
return lines.join(indentSize > 0 ? '\n' : '');
|
|
1013
|
+
}
|
|
1014
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
748
1015
|
// Unified Data Converter
|
|
749
1016
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
750
1017
|
export function convertData(input, from, to, options = {}) {
|
|
751
1018
|
let data;
|
|
752
1019
|
switch (from) {
|
|
753
1020
|
case 'json':
|
|
754
|
-
data =
|
|
1021
|
+
data = safeDataJsonParse(input);
|
|
755
1022
|
break;
|
|
756
1023
|
case 'yaml':
|
|
757
1024
|
data = parseYaml(input);
|
|
@@ -762,6 +1029,9 @@ export function convertData(input, from, to, options = {}) {
|
|
|
762
1029
|
case 'csv':
|
|
763
1030
|
data = parseCsv(input);
|
|
764
1031
|
break;
|
|
1032
|
+
case 'xml':
|
|
1033
|
+
data = parseXml(input);
|
|
1034
|
+
break;
|
|
765
1035
|
case 'query':
|
|
766
1036
|
data = parseQueryString(input);
|
|
767
1037
|
break;
|
|
@@ -781,6 +1051,8 @@ export function convertData(input, from, to, options = {}) {
|
|
|
781
1051
|
return stringifyToml(data);
|
|
782
1052
|
case 'csv':
|
|
783
1053
|
return stringifyCsv(data);
|
|
1054
|
+
case 'xml':
|
|
1055
|
+
return stringifyXml(data, 'root', indent);
|
|
784
1056
|
case 'query':
|
|
785
1057
|
return stringifyQueryString(data);
|
|
786
1058
|
case 'typescript':
|
|
@@ -797,7 +1069,14 @@ function sortKeysDeep(val) {
|
|
|
797
1069
|
if (val && typeof val === 'object') {
|
|
798
1070
|
const sorted = {};
|
|
799
1071
|
for (const k of Object.keys(val).sort()) {
|
|
800
|
-
|
|
1072
|
+
if (isProtoKey(k))
|
|
1073
|
+
continue;
|
|
1074
|
+
Object.defineProperty(sorted, k, {
|
|
1075
|
+
value: sortKeysDeep(val[k]),
|
|
1076
|
+
enumerable: true,
|
|
1077
|
+
writable: true,
|
|
1078
|
+
configurable: true,
|
|
1079
|
+
});
|
|
801
1080
|
}
|
|
802
1081
|
return sorted;
|
|
803
1082
|
}
|
|
@@ -901,19 +1180,19 @@ export function queryJsonPath(data, expression) {
|
|
|
901
1180
|
}
|
|
902
1181
|
return results;
|
|
903
1182
|
}
|
|
904
|
-
function collectRecursive(node, key, acc) {
|
|
905
|
-
if (!node || typeof node !== 'object' || isDangerousKey(key))
|
|
1183
|
+
function collectRecursive(node, key, acc, depth = 0) {
|
|
1184
|
+
if (depth > 32 || !node || typeof node !== 'object' || isDangerousKey(key))
|
|
906
1185
|
return;
|
|
907
1186
|
if (Array.isArray(node)) {
|
|
908
1187
|
for (const el of node)
|
|
909
|
-
collectRecursive(el, key, acc);
|
|
1188
|
+
collectRecursive(el, key, acc, depth + 1);
|
|
910
1189
|
return;
|
|
911
1190
|
}
|
|
912
1191
|
const obj = node;
|
|
913
1192
|
if (Object.prototype.hasOwnProperty.call(obj, key))
|
|
914
1193
|
acc.push(obj[key]);
|
|
915
1194
|
for (const val of Object.values(obj)) {
|
|
916
|
-
collectRecursive(val, key, acc);
|
|
1195
|
+
collectRecursive(val, key, acc, depth + 1);
|
|
917
1196
|
}
|
|
918
1197
|
}
|
|
919
1198
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -979,3 +1258,4 @@ export function formatSql(sql) {
|
|
|
979
1258
|
// Restore literals
|
|
980
1259
|
return normalized.replace(/__SQL_STR_(\d+)__/g, (_, i) => placeholders[Number(i)] ?? '');
|
|
981
1260
|
}
|
|
1261
|
+
//# sourceMappingURL=converters.js.map
|