recker 1.0.35 → 1.0.36-next.4f24ba8

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.
@@ -0,0 +1,730 @@
1
+ export const YAML_MEDIA_TYPES = [
2
+ 'application/yaml',
3
+ 'application/x-yaml',
4
+ 'text/yaml',
5
+ 'text/x-yaml',
6
+ ];
7
+ export const YAML_SUFFIX = '+yaml';
8
+ export function isYamlContentType(contentType) {
9
+ if (!contentType)
10
+ return false;
11
+ const ct = contentType.toLowerCase().split(';')[0].trim();
12
+ return YAML_MEDIA_TYPES.includes(ct) || ct.endsWith(YAML_SUFFIX);
13
+ }
14
+ export function parseYaml(yaml, options = {}) {
15
+ const opts = {
16
+ allowDuplicateKeys: options.allowDuplicateKeys ?? false,
17
+ parseDates: options.parseDates ?? true,
18
+ customTags: options.customTags ?? {},
19
+ maxDepth: options.maxDepth ?? 100,
20
+ maxKeys: options.maxKeys ?? 10000,
21
+ };
22
+ if (!yaml || !yaml.trim()) {
23
+ return null;
24
+ }
25
+ let content = yaml.replace(/^\uFEFF/, '');
26
+ content = content.replace(/^---\s*$/gm, '').replace(/^\.\.\.\s*$/gm, '');
27
+ const lines = content.split('\n');
28
+ const state = {
29
+ lines,
30
+ index: 0,
31
+ depth: 0,
32
+ keyCount: 0,
33
+ options: opts,
34
+ };
35
+ const anchors = new Map();
36
+ return parseValue(state, 0, anchors);
37
+ }
38
+ function parseValue(state, baseIndent, anchors) {
39
+ skipEmptyAndComments(state);
40
+ if (state.index >= state.lines.length) {
41
+ return null;
42
+ }
43
+ const line = state.lines[state.index];
44
+ const trimmed = line.trim();
45
+ if (trimmed === '~' || trimmed === 'null' || trimmed === 'Null' || trimmed === 'NULL') {
46
+ state.index++;
47
+ return null;
48
+ }
49
+ if (trimmed.startsWith('- ') || trimmed === '-') {
50
+ return parseSequence(state, baseIndent, anchors);
51
+ }
52
+ if (trimmed.includes(':') && !trimmed.startsWith('"') && !trimmed.startsWith("'")) {
53
+ return parseMapping(state, baseIndent, anchors);
54
+ }
55
+ state.index++;
56
+ return parseScalar(trimmed, state.options, anchors);
57
+ }
58
+ function parseMapping(state, baseIndent, anchors) {
59
+ const result = {};
60
+ state.depth++;
61
+ if (state.depth > state.options.maxDepth) {
62
+ throw new YamlError('Maximum nesting depth exceeded', state.index);
63
+ }
64
+ while (state.index < state.lines.length) {
65
+ skipEmptyAndComments(state);
66
+ if (state.index >= state.lines.length)
67
+ break;
68
+ const line = state.lines[state.index];
69
+ const currentIndent = getIndent(line);
70
+ if (currentIndent < baseIndent && line.trim()) {
71
+ break;
72
+ }
73
+ if (currentIndent < baseIndent) {
74
+ state.index++;
75
+ continue;
76
+ }
77
+ const trimmed = line.trim();
78
+ if (!trimmed || trimmed.startsWith('#')) {
79
+ state.index++;
80
+ continue;
81
+ }
82
+ if (trimmed.startsWith('- ') && currentIndent <= baseIndent) {
83
+ break;
84
+ }
85
+ const colonIndex = findKeyColon(trimmed);
86
+ if (colonIndex === -1) {
87
+ state.index++;
88
+ continue;
89
+ }
90
+ let key = trimmed.substring(0, colonIndex).trim();
91
+ let valueStr = trimmed.substring(colonIndex + 1).trim();
92
+ let anchor = null;
93
+ const anchorMatch = key.match(/^&(\w+)\s+/);
94
+ if (anchorMatch) {
95
+ anchor = anchorMatch[1];
96
+ key = key.substring(anchorMatch[0].length);
97
+ }
98
+ if (valueStr.startsWith('*')) {
99
+ const aliasName = valueStr.substring(1).split(/\s/)[0];
100
+ if (!anchors.has(aliasName)) {
101
+ throw new YamlError(`Unknown alias: ${aliasName}`, state.index);
102
+ }
103
+ result[key] = anchors.get(aliasName);
104
+ state.index++;
105
+ continue;
106
+ }
107
+ const valueAnchorMatch = valueStr.match(/^&(\w+)\s*/);
108
+ if (valueAnchorMatch) {
109
+ anchor = valueAnchorMatch[1];
110
+ valueStr = valueStr.substring(valueAnchorMatch[0].length);
111
+ }
112
+ key = unquote(key);
113
+ state.keyCount++;
114
+ if (state.keyCount > state.options.maxKeys) {
115
+ throw new YamlError('Maximum number of keys exceeded', state.index);
116
+ }
117
+ if (!state.options.allowDuplicateKeys && key in result) {
118
+ throw new YamlError(`Duplicate key: ${key}`, state.index);
119
+ }
120
+ state.index++;
121
+ let value;
122
+ if (valueStr === '' || valueStr === '|' || valueStr === '>' || valueStr === '|-' || valueStr === '>-') {
123
+ if (valueStr === '|' || valueStr === '|-') {
124
+ value = parseLiteralBlock(state, currentIndent, valueStr === '|-');
125
+ }
126
+ else if (valueStr === '>' || valueStr === '>-') {
127
+ value = parseFoldedBlock(state, currentIndent, valueStr === '>-');
128
+ }
129
+ else {
130
+ skipEmptyAndComments(state);
131
+ if (state.index < state.lines.length) {
132
+ const nextIndent = getIndent(state.lines[state.index]);
133
+ if (nextIndent > currentIndent) {
134
+ value = parseValue(state, nextIndent, anchors);
135
+ }
136
+ else {
137
+ value = null;
138
+ }
139
+ }
140
+ else {
141
+ value = null;
142
+ }
143
+ }
144
+ }
145
+ else {
146
+ value = parseScalar(valueStr, state.options, anchors);
147
+ }
148
+ if (anchor) {
149
+ anchors.set(anchor, value);
150
+ }
151
+ result[key] = value;
152
+ }
153
+ state.depth--;
154
+ return result;
155
+ }
156
+ function parseSequence(state, baseIndent, anchors) {
157
+ const result = [];
158
+ state.depth++;
159
+ if (state.depth > state.options.maxDepth) {
160
+ throw new YamlError('Maximum nesting depth exceeded', state.index);
161
+ }
162
+ while (state.index < state.lines.length) {
163
+ skipEmptyAndComments(state);
164
+ if (state.index >= state.lines.length)
165
+ break;
166
+ const line = state.lines[state.index];
167
+ const currentIndent = getIndent(line);
168
+ const trimmed = line.trim();
169
+ if (currentIndent < baseIndent && trimmed) {
170
+ break;
171
+ }
172
+ if (!trimmed.startsWith('-')) {
173
+ if (currentIndent <= baseIndent)
174
+ break;
175
+ state.index++;
176
+ continue;
177
+ }
178
+ let anchor = null;
179
+ let valueStr = trimmed.substring(1).trim();
180
+ const anchorMatch = valueStr.match(/^&(\w+)\s*/);
181
+ if (anchorMatch) {
182
+ anchor = anchorMatch[1];
183
+ valueStr = valueStr.substring(anchorMatch[0].length);
184
+ }
185
+ if (valueStr.startsWith('*')) {
186
+ const aliasName = valueStr.substring(1).split(/\s/)[0];
187
+ if (!anchors.has(aliasName)) {
188
+ throw new YamlError(`Unknown alias: ${aliasName}`, state.index);
189
+ }
190
+ result.push(anchors.get(aliasName));
191
+ state.index++;
192
+ continue;
193
+ }
194
+ state.index++;
195
+ let value;
196
+ if (valueStr === '') {
197
+ skipEmptyAndComments(state);
198
+ if (state.index < state.lines.length) {
199
+ const nextIndent = getIndent(state.lines[state.index]);
200
+ if (nextIndent > currentIndent) {
201
+ value = parseValue(state, nextIndent, anchors);
202
+ }
203
+ else {
204
+ value = null;
205
+ }
206
+ }
207
+ else {
208
+ value = null;
209
+ }
210
+ }
211
+ else if (valueStr.includes(':') && !valueStr.startsWith('"') && !valueStr.startsWith("'")) {
212
+ state.index--;
213
+ const fakeLine = ' '.repeat(currentIndent + 2) + valueStr;
214
+ const originalLine = state.lines[state.index];
215
+ state.lines[state.index] = fakeLine;
216
+ value = parseMapping(state, currentIndent + 2, anchors);
217
+ if (state.index < state.lines.length) {
218
+ state.lines[state.index - 1] = originalLine;
219
+ }
220
+ }
221
+ else {
222
+ value = parseScalar(valueStr, state.options, anchors);
223
+ }
224
+ if (anchor) {
225
+ anchors.set(anchor, value);
226
+ }
227
+ result.push(value);
228
+ }
229
+ state.depth--;
230
+ return result;
231
+ }
232
+ function parseLiteralBlock(state, baseIndent, chomp) {
233
+ const lines = [];
234
+ let contentIndent = -1;
235
+ while (state.index < state.lines.length) {
236
+ const line = state.lines[state.index];
237
+ const indent = getIndent(line);
238
+ const trimmed = line.trim();
239
+ if (contentIndent === -1 && trimmed) {
240
+ contentIndent = indent;
241
+ }
242
+ if (contentIndent !== -1 && indent < contentIndent && trimmed) {
243
+ break;
244
+ }
245
+ if (trimmed && indent <= baseIndent) {
246
+ break;
247
+ }
248
+ if (contentIndent !== -1) {
249
+ const relativeIndent = Math.max(0, indent - contentIndent);
250
+ lines.push(' '.repeat(relativeIndent) + trimmed);
251
+ }
252
+ else {
253
+ lines.push('');
254
+ }
255
+ state.index++;
256
+ }
257
+ if (chomp) {
258
+ while (lines.length > 0 && lines[lines.length - 1] === '') {
259
+ lines.pop();
260
+ }
261
+ }
262
+ return lines.join('\n');
263
+ }
264
+ function parseFoldedBlock(state, baseIndent, chomp) {
265
+ const paragraphs = [];
266
+ let currentParagraph = [];
267
+ let contentIndent = -1;
268
+ while (state.index < state.lines.length) {
269
+ const line = state.lines[state.index];
270
+ const indent = getIndent(line);
271
+ const trimmed = line.trim();
272
+ if (contentIndent === -1 && trimmed) {
273
+ contentIndent = indent;
274
+ }
275
+ if (contentIndent !== -1 && indent < contentIndent && trimmed) {
276
+ break;
277
+ }
278
+ if (trimmed && indent <= baseIndent) {
279
+ break;
280
+ }
281
+ if (contentIndent !== -1) {
282
+ if (trimmed === '') {
283
+ if (currentParagraph.length > 0) {
284
+ paragraphs.push(currentParagraph.join(' '));
285
+ currentParagraph = [];
286
+ }
287
+ paragraphs.push('');
288
+ }
289
+ else {
290
+ currentParagraph.push(trimmed);
291
+ }
292
+ }
293
+ state.index++;
294
+ }
295
+ if (currentParagraph.length > 0) {
296
+ paragraphs.push(currentParagraph.join(' '));
297
+ }
298
+ if (chomp) {
299
+ while (paragraphs.length > 0 && paragraphs[paragraphs.length - 1] === '') {
300
+ paragraphs.pop();
301
+ }
302
+ }
303
+ return paragraphs.join('\n');
304
+ }
305
+ function parseScalar(value, options, anchors) {
306
+ if (value.startsWith('*')) {
307
+ const aliasName = value.substring(1).split(/\s|#/)[0];
308
+ if (!anchors.has(aliasName)) {
309
+ throw new YamlError(`Unknown alias: ${aliasName}`);
310
+ }
311
+ return anchors.get(aliasName);
312
+ }
313
+ const commentIndex = findInlineComment(value);
314
+ if (commentIndex !== -1) {
315
+ value = value.substring(0, commentIndex).trim();
316
+ }
317
+ const tagMatch = value.match(/^!(\S+)\s*/);
318
+ if (tagMatch) {
319
+ const tag = tagMatch[1];
320
+ const tagValue = value.substring(tagMatch[0].length);
321
+ if (options.customTags[tag]) {
322
+ return options.customTags[tag](tagValue);
323
+ }
324
+ return tagValue;
325
+ }
326
+ if (value === '' || value === '~' || value === 'null' || value === 'Null' || value === 'NULL') {
327
+ return null;
328
+ }
329
+ if (value === 'true' || value === 'True' || value === 'TRUE' || value === 'yes' || value === 'Yes' || value === 'YES' || value === 'on' || value === 'On' || value === 'ON') {
330
+ return true;
331
+ }
332
+ if (value === 'false' || value === 'False' || value === 'FALSE' || value === 'no' || value === 'No' || value === 'NO' || value === 'off' || value === 'Off' || value === 'OFF') {
333
+ return false;
334
+ }
335
+ if (value === '.inf' || value === '.Inf' || value === '.INF' || value === '+.inf' || value === '+.Inf' || value === '+.INF') {
336
+ return Infinity;
337
+ }
338
+ if (value === '-.inf' || value === '-.Inf' || value === '-.INF') {
339
+ return -Infinity;
340
+ }
341
+ if (value === '.nan' || value === '.NaN' || value === '.NAN') {
342
+ return NaN;
343
+ }
344
+ if (/^0o[0-7]+$/.test(value)) {
345
+ return parseInt(value.substring(2), 8);
346
+ }
347
+ if (/^0x[0-9a-fA-F]+$/.test(value)) {
348
+ return parseInt(value, 16);
349
+ }
350
+ if (/^[-+]?\d+$/.test(value)) {
351
+ const num = parseInt(value, 10);
352
+ if (Number.isSafeInteger(num)) {
353
+ return num;
354
+ }
355
+ return num;
356
+ }
357
+ if (/^[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?$/.test(value)) {
358
+ return parseFloat(value);
359
+ }
360
+ if (/^\d+:\d{2}(:\d{2})?(\.\d+)?$/.test(value)) {
361
+ const parts = value.split(':').map(parseFloat);
362
+ let result = parts[0];
363
+ for (let i = 1; i < parts.length; i++) {
364
+ result = result * 60 + parts[i];
365
+ }
366
+ return result;
367
+ }
368
+ if (options.parseDates) {
369
+ if (/^\d{4}-\d{2}-\d{2}([T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?)?$/.test(value)) {
370
+ const date = new Date(value.replace(' ', 'T'));
371
+ if (!isNaN(date.getTime())) {
372
+ return date;
373
+ }
374
+ }
375
+ }
376
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
377
+ return unquote(value);
378
+ }
379
+ if (value.startsWith('[') && value.endsWith(']')) {
380
+ return parseFlowSequence(value, options, anchors);
381
+ }
382
+ if (value.startsWith('{') && value.endsWith('}')) {
383
+ return parseFlowMapping(value, options, anchors);
384
+ }
385
+ return value;
386
+ }
387
+ function parseFlowSequence(value, options, anchors) {
388
+ const inner = value.slice(1, -1).trim();
389
+ if (!inner)
390
+ return [];
391
+ const items = [];
392
+ let current = '';
393
+ let depth = 0;
394
+ let inQuote = false;
395
+ let quoteChar = '';
396
+ for (let i = 0; i < inner.length; i++) {
397
+ const char = inner[i];
398
+ if (inQuote) {
399
+ current += char;
400
+ if (char === quoteChar && inner[i - 1] !== '\\') {
401
+ inQuote = false;
402
+ }
403
+ continue;
404
+ }
405
+ if (char === '"' || char === "'") {
406
+ inQuote = true;
407
+ quoteChar = char;
408
+ current += char;
409
+ continue;
410
+ }
411
+ if (char === '[' || char === '{') {
412
+ depth++;
413
+ current += char;
414
+ continue;
415
+ }
416
+ if (char === ']' || char === '}') {
417
+ depth--;
418
+ current += char;
419
+ continue;
420
+ }
421
+ if (char === ',' && depth === 0) {
422
+ items.push(parseScalar(current.trim(), options, anchors));
423
+ current = '';
424
+ continue;
425
+ }
426
+ current += char;
427
+ }
428
+ if (current.trim()) {
429
+ items.push(parseScalar(current.trim(), options, anchors));
430
+ }
431
+ return items;
432
+ }
433
+ function parseFlowMapping(value, options, anchors) {
434
+ const inner = value.slice(1, -1).trim();
435
+ if (!inner)
436
+ return {};
437
+ const result = {};
438
+ let current = '';
439
+ let depth = 0;
440
+ let inQuote = false;
441
+ let quoteChar = '';
442
+ for (let i = 0; i < inner.length; i++) {
443
+ const char = inner[i];
444
+ if (inQuote) {
445
+ current += char;
446
+ if (char === quoteChar && inner[i - 1] !== '\\') {
447
+ inQuote = false;
448
+ }
449
+ continue;
450
+ }
451
+ if (char === '"' || char === "'") {
452
+ inQuote = true;
453
+ quoteChar = char;
454
+ current += char;
455
+ continue;
456
+ }
457
+ if (char === '[' || char === '{') {
458
+ depth++;
459
+ current += char;
460
+ continue;
461
+ }
462
+ if (char === ']' || char === '}') {
463
+ depth--;
464
+ current += char;
465
+ continue;
466
+ }
467
+ if (char === ',' && depth === 0) {
468
+ const colonIdx = findKeyColon(current);
469
+ if (colonIdx !== -1) {
470
+ const key = unquote(current.substring(0, colonIdx).trim());
471
+ const val = current.substring(colonIdx + 1).trim();
472
+ result[key] = parseScalar(val, options, anchors);
473
+ }
474
+ current = '';
475
+ continue;
476
+ }
477
+ current += char;
478
+ }
479
+ if (current.trim()) {
480
+ const colonIdx = findKeyColon(current);
481
+ if (colonIdx !== -1) {
482
+ const key = unquote(current.substring(0, colonIdx).trim());
483
+ const val = current.substring(colonIdx + 1).trim();
484
+ result[key] = parseScalar(val, options, anchors);
485
+ }
486
+ }
487
+ return result;
488
+ }
489
+ function getIndent(line) {
490
+ const match = line.match(/^(\s*)/);
491
+ return match ? match[1].length : 0;
492
+ }
493
+ function skipEmptyAndComments(state) {
494
+ while (state.index < state.lines.length) {
495
+ const trimmed = state.lines[state.index].trim();
496
+ if (trimmed && !trimmed.startsWith('#')) {
497
+ break;
498
+ }
499
+ state.index++;
500
+ }
501
+ }
502
+ function findKeyColon(str) {
503
+ let inQuote = false;
504
+ let quoteChar = '';
505
+ for (let i = 0; i < str.length; i++) {
506
+ const char = str[i];
507
+ if (inQuote) {
508
+ if (char === quoteChar && str[i - 1] !== '\\') {
509
+ inQuote = false;
510
+ }
511
+ continue;
512
+ }
513
+ if (char === '"' || char === "'") {
514
+ inQuote = true;
515
+ quoteChar = char;
516
+ continue;
517
+ }
518
+ if (char === ':') {
519
+ if (i === str.length - 1 || str[i + 1] === ' ' || str[i + 1] === '\n' || str[i + 1] === '\t') {
520
+ return i;
521
+ }
522
+ }
523
+ }
524
+ return -1;
525
+ }
526
+ function findInlineComment(str) {
527
+ let inQuote = false;
528
+ let quoteChar = '';
529
+ for (let i = 0; i < str.length; i++) {
530
+ const char = str[i];
531
+ if (inQuote) {
532
+ if (char === quoteChar && str[i - 1] !== '\\') {
533
+ inQuote = false;
534
+ }
535
+ continue;
536
+ }
537
+ if (char === '"' || char === "'") {
538
+ inQuote = true;
539
+ quoteChar = char;
540
+ continue;
541
+ }
542
+ if (char === '#' && (i === 0 || str[i - 1] === ' ' || str[i - 1] === '\t')) {
543
+ return i;
544
+ }
545
+ }
546
+ return -1;
547
+ }
548
+ function unquote(str) {
549
+ if (str.startsWith('"') && str.endsWith('"')) {
550
+ return str
551
+ .slice(1, -1)
552
+ .replace(/\\"/g, '"')
553
+ .replace(/\\\\/g, '\\')
554
+ .replace(/\\n/g, '\n')
555
+ .replace(/\\t/g, '\t')
556
+ .replace(/\\r/g, '\r')
557
+ .replace(/\\0/g, '\0')
558
+ .replace(/\\x([0-9a-fA-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
559
+ .replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
560
+ }
561
+ if (str.startsWith("'") && str.endsWith("'")) {
562
+ return str.slice(1, -1).replace(/''/g, "'");
563
+ }
564
+ return str;
565
+ }
566
+ export class YamlError extends Error {
567
+ line;
568
+ constructor(message, line) {
569
+ super(line !== undefined ? `${message} at line ${line + 1}` : message);
570
+ this.name = 'YamlError';
571
+ this.line = line;
572
+ }
573
+ }
574
+ export function serializeYaml(data, options = {}) {
575
+ const opts = {
576
+ indent: options.indent ?? 2,
577
+ lineWidth: options.lineWidth ?? 80,
578
+ forceQuotes: options.forceQuotes ?? false,
579
+ flowStyle: options.flowStyle ?? false,
580
+ sortKeys: options.sortKeys ?? false,
581
+ skipUndefined: options.skipUndefined ?? true,
582
+ documentMarkers: options.documentMarkers ?? false,
583
+ };
584
+ let result = '';
585
+ if (opts.documentMarkers) {
586
+ result += '---\n';
587
+ }
588
+ result += serializeValue(data, 0, opts);
589
+ if (opts.documentMarkers) {
590
+ result += '\n...';
591
+ }
592
+ return result;
593
+ }
594
+ function serializeValue(value, depth, opts) {
595
+ if (value === null || value === undefined) {
596
+ return 'null';
597
+ }
598
+ if (typeof value === 'boolean') {
599
+ return value ? 'true' : 'false';
600
+ }
601
+ if (typeof value === 'number') {
602
+ if (Number.isNaN(value))
603
+ return '.nan';
604
+ if (value === Infinity)
605
+ return '.inf';
606
+ if (value === -Infinity)
607
+ return '-.inf';
608
+ return String(value);
609
+ }
610
+ if (typeof value === 'string') {
611
+ return serializeString(value, depth, opts);
612
+ }
613
+ if (value instanceof Date) {
614
+ return value.toISOString();
615
+ }
616
+ if (Array.isArray(value)) {
617
+ return serializeArray(value, depth, opts);
618
+ }
619
+ if (typeof value === 'object') {
620
+ return serializeObject(value, depth, opts);
621
+ }
622
+ return String(value);
623
+ }
624
+ function serializeString(str, depth, opts) {
625
+ const needsQuotes = opts.forceQuotes ||
626
+ str === '' ||
627
+ str === 'null' ||
628
+ str === 'true' ||
629
+ str === 'false' ||
630
+ str === '~' ||
631
+ /^[\d\-.]+$/.test(str) ||
632
+ /^[&*!|>'"%@`]/.test(str) ||
633
+ /[:#\[\]{}]/.test(str) ||
634
+ str.includes('\n');
635
+ if (str.includes('\n')) {
636
+ const indent = ' '.repeat(opts.indent * (depth + 1));
637
+ const lines = str.split('\n');
638
+ return '|\n' + lines.map((line) => indent + line).join('\n');
639
+ }
640
+ if (needsQuotes) {
641
+ const escaped = str
642
+ .replace(/\\/g, '\\\\')
643
+ .replace(/"/g, '\\"')
644
+ .replace(/\n/g, '\\n')
645
+ .replace(/\t/g, '\\t')
646
+ .replace(/\r/g, '\\r');
647
+ return `"${escaped}"`;
648
+ }
649
+ return str;
650
+ }
651
+ function serializeArray(arr, depth, opts) {
652
+ if (arr.length === 0) {
653
+ return '[]';
654
+ }
655
+ if (opts.flowStyle && arr.length <= 5 && arr.every((v) => typeof v !== 'object' || v === null)) {
656
+ const items = arr.map((v) => serializeValue(v, depth, opts));
657
+ const flow = `[${items.join(', ')}]`;
658
+ if (flow.length <= opts.lineWidth) {
659
+ return flow;
660
+ }
661
+ }
662
+ const indent = ' '.repeat(opts.indent * depth);
663
+ const lines = [];
664
+ for (const item of arr) {
665
+ if (opts.skipUndefined && item === undefined) {
666
+ continue;
667
+ }
668
+ if (typeof item === 'object' && item !== null && !Array.isArray(item) && !(item instanceof Date)) {
669
+ const objStr = serializeObject(item, depth + 1, opts);
670
+ const firstLine = objStr.split('\n')[0];
671
+ const restLines = objStr.split('\n').slice(1);
672
+ lines.push(`${indent}- ${firstLine}`);
673
+ lines.push(...restLines.map((l) => `${indent} ${l.trimStart()}`));
674
+ }
675
+ else if (Array.isArray(item) && item.length > 0 && !opts.flowStyle) {
676
+ lines.push(`${indent}-`);
677
+ const nested = serializeArray(item, depth + 1, opts);
678
+ lines.push(...nested.split('\n').map((l) => `${indent} ${l.trimStart()}`));
679
+ }
680
+ else {
681
+ lines.push(`${indent}- ${serializeValue(item, depth + 1, opts)}`);
682
+ }
683
+ }
684
+ return lines.join('\n');
685
+ }
686
+ function serializeObject(obj, depth, opts) {
687
+ const keys = Object.keys(obj);
688
+ if (keys.length === 0) {
689
+ return '{}';
690
+ }
691
+ if (opts.sortKeys) {
692
+ keys.sort();
693
+ }
694
+ if (opts.flowStyle && keys.length <= 3 && keys.every((k) => typeof obj[k] !== 'object' || obj[k] === null)) {
695
+ const items = keys.map((k) => `${k}: ${serializeValue(obj[k], depth, opts)}`);
696
+ const flow = `{${items.join(', ')}}`;
697
+ if (flow.length <= opts.lineWidth) {
698
+ return flow;
699
+ }
700
+ }
701
+ const indent = ' '.repeat(opts.indent * depth);
702
+ const lines = [];
703
+ for (const key of keys) {
704
+ const value = obj[key];
705
+ if (opts.skipUndefined && value === undefined) {
706
+ continue;
707
+ }
708
+ const quotedKey = /^[\w-]+$/.test(key) && key !== 'true' && key !== 'false' && key !== 'null' ? key : `"${key}"`;
709
+ if (typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date)) {
710
+ lines.push(`${indent}${quotedKey}:`);
711
+ const nested = serializeObject(value, depth + 1, opts);
712
+ lines.push(...nested.split('\n'));
713
+ }
714
+ else if (Array.isArray(value) && value.length > 0 && !opts.flowStyle) {
715
+ lines.push(`${indent}${quotedKey}:`);
716
+ const nested = serializeArray(value, depth + 1, opts);
717
+ lines.push(...nested.split('\n'));
718
+ }
719
+ else {
720
+ lines.push(`${indent}${quotedKey}: ${serializeValue(value, depth + 1, opts)}`);
721
+ }
722
+ }
723
+ return lines.join('\n');
724
+ }
725
+ export async function yamlResponse(promise, options) {
726
+ const response = await promise;
727
+ const text = await response.text();
728
+ return parseYaml(text, options);
729
+ }
730
+ export { parseYaml as yamlParse, serializeYaml as yamlSerialize };