yaml 2.8.4 → 2.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -94,8 +94,10 @@ class Composer {
94
94
  }
95
95
  }
96
96
  if (afterDoc) {
97
- Array.prototype.push.apply(doc.errors, this.errors);
98
- Array.prototype.push.apply(doc.warnings, this.warnings);
97
+ for (let i = 0; i < this.errors.length; ++i)
98
+ doc.errors.push(this.errors[i]);
99
+ for (let i = 0; i < this.warnings.length; ++i)
100
+ doc.warnings.push(this.warnings[i]);
99
101
  }
100
102
  else {
101
103
  doc.errors = this.errors;
@@ -64,46 +64,47 @@ function plainValue(source, onError) {
64
64
  }
65
65
  if (badChar)
66
66
  onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
67
- return foldLines(source);
67
+ return unfoldLines(source);
68
68
  }
69
69
  function singleQuotedValue(source, onError) {
70
70
  if (source[source.length - 1] !== "'" || source.length === 1)
71
71
  onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
72
- return foldLines(source.slice(1, -1)).replace(/''/g, "'");
72
+ return unfoldLines(source.slice(1, -1)).replace(/''/g, "'");
73
73
  }
74
- function foldLines(source) {
74
+ function unfoldLines(source) {
75
+ const line = /(.*?)\r?\n/sy;
76
+ let match = line.exec(source);
77
+ if (!match)
78
+ return source;
75
79
  /**
76
- * The negative lookbehind here and in the `re` RegExp is to
80
+ * The negative lookbehinds in these RegExps are to
77
81
  * prevent causing a polynomial search time in certain cases.
78
82
  *
79
- * The try-catch is for Safari, which doesn't support this yet:
83
+ * The try-catch is for Safari < 16.4 and other old browsers:
80
84
  * https://caniuse.com/js-regexp-lookbehind
81
85
  */
82
- let first, line;
86
+ let trimEnd, trimBoth;
83
87
  try {
84
- first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
85
- line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
88
+ trimEnd = new RegExp('(?<![ \t])[ \t]+$');
89
+ trimBoth = new RegExp('^[ \t]+|(?<![ \t])[ \t]+$', 'g');
86
90
  }
87
91
  catch {
88
- first = /(.*?)[ \t]*\r?\n/sy;
89
- line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
92
+ trimEnd = /[ \t]+$/;
93
+ trimBoth = /^[ \t]+|[ \t]+$/g;
90
94
  }
91
- let match = first.exec(source);
92
- if (!match)
93
- return source;
94
- let res = match[1];
95
+ let res = match[1].replace(trimEnd, '');
95
96
  let sep = ' ';
96
- let pos = first.lastIndex;
97
- line.lastIndex = pos;
97
+ let pos = line.lastIndex;
98
98
  while ((match = line.exec(source))) {
99
- if (match[1] === '') {
99
+ const lm = match[1].replace(trimBoth, '');
100
+ if (lm === '') {
100
101
  if (sep === '\n')
101
102
  res += sep;
102
103
  else
103
104
  sep = '\n';
104
105
  }
105
106
  else {
106
- res += sep + match[1];
107
+ res += sep + lm;
107
108
  sep = ' ';
108
109
  }
109
110
  pos = line.lastIndex;
@@ -43,38 +43,40 @@ class Alias extends NodeBase {
43
43
  if (node.anchor === this.source)
44
44
  found = node;
45
45
  }
46
+ if (found && ctx) {
47
+ const { anchors, doc, maxAliasCount } = ctx;
48
+ let data = anchors.get(found);
49
+ if (!data) {
50
+ // Resolve anchors for Node.prototype.toJS()
51
+ toJS(found, null, ctx);
52
+ data = anchors.get(found);
53
+ }
54
+ /* istanbul ignore if */
55
+ if (data?.res === undefined) {
56
+ const msg = 'This should not happen: Alias anchor was not resolved?';
57
+ throw new ReferenceError(msg);
58
+ }
59
+ if (maxAliasCount >= 0) {
60
+ data.count += 1;
61
+ if (data.aliasCount === 0)
62
+ data.aliasCount = getAliasCount(doc, found, anchors);
63
+ if (data.count * data.aliasCount > maxAliasCount) {
64
+ const msg = 'Excessive alias count indicates a resource exhaustion attack';
65
+ throw new ReferenceError(msg);
66
+ }
67
+ }
68
+ }
46
69
  return found;
47
70
  }
48
71
  toJSON(_arg, ctx) {
49
72
  if (!ctx)
50
73
  return { source: this.source };
51
- const { anchors, doc, maxAliasCount } = ctx;
52
- const source = this.resolve(doc, ctx);
74
+ const source = this.resolve(ctx.doc, ctx);
53
75
  if (!source) {
54
76
  const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
55
77
  throw new ReferenceError(msg);
56
78
  }
57
- let data = anchors.get(source);
58
- if (!data) {
59
- // Resolve anchors for Node.prototype.toJS()
60
- toJS(source, null, ctx);
61
- data = anchors.get(source);
62
- }
63
- /* istanbul ignore if */
64
- if (data?.res === undefined) {
65
- const msg = 'This should not happen: Alias anchor was not resolved?';
66
- throw new ReferenceError(msg);
67
- }
68
- if (maxAliasCount >= 0) {
69
- data.count += 1;
70
- if (data.aliasCount === 0)
71
- data.aliasCount = getAliasCount(doc, source, anchors);
72
- if (data.count * data.aliasCount > maxAliasCount) {
73
- const msg = 'Excessive alias count indicates a resource exhaustion attack';
74
- throw new ReferenceError(msg);
75
- }
76
- }
77
- return data.res;
79
+ return ctx.anchors.get(source).res;
78
80
  }
79
81
  toString(ctx, _onComment, _onChompKeep) {
80
82
  const src = `*${this.source}`;
@@ -310,7 +310,7 @@ class Lexer {
310
310
  const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
311
311
  this.indentNext = this.indentValue + 1;
312
312
  this.indentValue += n;
313
- return yield* this.parseBlockStart();
313
+ return 'block-start';
314
314
  }
315
315
  return 'doc';
316
316
  }
@@ -631,32 +631,36 @@ class Lexer {
631
631
  return 0;
632
632
  }
633
633
  *pushIndicators() {
634
- switch (this.charAt(0)) {
635
- case '!':
636
- return ((yield* this.pushTag()) +
637
- (yield* this.pushSpaces(true)) +
638
- (yield* this.pushIndicators()));
639
- case '&':
640
- return ((yield* this.pushUntil(isNotAnchorChar)) +
641
- (yield* this.pushSpaces(true)) +
642
- (yield* this.pushIndicators()));
643
- case '-': // this is an error
644
- case '?': // this is an error outside flow collections
645
- case ':': {
646
- const inFlow = this.flowLevel > 0;
647
- const ch1 = this.charAt(1);
648
- if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
649
- if (!inFlow)
650
- this.indentNext = this.indentValue + 1;
651
- else if (this.flowKey)
652
- this.flowKey = false;
653
- return ((yield* this.pushCount(1)) +
654
- (yield* this.pushSpaces(true)) +
655
- (yield* this.pushIndicators()));
634
+ let n = 0;
635
+ loop: while (true) {
636
+ switch (this.charAt(0)) {
637
+ case '!':
638
+ n += yield* this.pushTag();
639
+ n += yield* this.pushSpaces(true);
640
+ continue loop;
641
+ case '&':
642
+ n += yield* this.pushUntil(isNotAnchorChar);
643
+ n += yield* this.pushSpaces(true);
644
+ continue loop;
645
+ case '-': // this is an error
646
+ case '?': // this is an error outside flow collections
647
+ case ':': {
648
+ const inFlow = this.flowLevel > 0;
649
+ const ch1 = this.charAt(1);
650
+ if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
651
+ if (!inFlow)
652
+ this.indentNext = this.indentValue + 1;
653
+ else if (this.flowKey)
654
+ this.flowKey = false;
655
+ n += yield* this.pushCount(1);
656
+ n += yield* this.pushSpaces(true);
657
+ continue loop;
658
+ }
656
659
  }
657
660
  }
661
+ break loop;
658
662
  }
659
- return 0;
663
+ return n;
660
664
  }
661
665
  *pushTag() {
662
666
  if (this.charAt(1) === '<') {
@@ -67,6 +67,14 @@ function getFirstKeyStartProps(prev) {
67
67
  }
68
68
  return prev.splice(i, prev.length);
69
69
  }
70
+ function arrayPushArray(target, source) {
71
+ // May exhaust call stack with large `source` array
72
+ if (source.length < 1e5)
73
+ Array.prototype.push.apply(target, source);
74
+ else
75
+ for (let i = 0; i < source.length; ++i)
76
+ target.push(source[i]);
77
+ }
70
78
  function fixFlowSeqItems(fc) {
71
79
  if (fc.start.type === 'flow-seq-start') {
72
80
  for (const it of fc.items) {
@@ -79,12 +87,12 @@ function fixFlowSeqItems(fc) {
79
87
  delete it.key;
80
88
  if (isFlowToken(it.value)) {
81
89
  if (it.value.end)
82
- Array.prototype.push.apply(it.value.end, it.sep);
90
+ arrayPushArray(it.value.end, it.sep);
83
91
  else
84
92
  it.value.end = it.sep;
85
93
  }
86
94
  else
87
- Array.prototype.push.apply(it.start, it.sep);
95
+ arrayPushArray(it.start, it.sep);
88
96
  delete it.sep;
89
97
  }
90
98
  }
@@ -502,7 +510,7 @@ class Parser {
502
510
  const prev = map.items[map.items.length - 2];
503
511
  const end = prev?.value?.end;
504
512
  if (Array.isArray(end)) {
505
- Array.prototype.push.apply(end, it.start);
513
+ arrayPushArray(end, it.start);
506
514
  end.push(this.sourceToken);
507
515
  map.items.pop();
508
516
  return;
@@ -717,7 +725,7 @@ class Parser {
717
725
  const prev = seq.items[seq.items.length - 2];
718
726
  const end = prev?.value?.end;
719
727
  if (Array.isArray(end)) {
720
- Array.prototype.push.apply(end, it.start);
728
+ arrayPushArray(end, it.start);
721
729
  end.push(this.sourceToken);
722
730
  seq.items.pop();
723
731
  return;
@@ -97,8 +97,10 @@ class Composer {
97
97
  }
98
98
  }
99
99
  if (afterDoc) {
100
- Array.prototype.push.apply(doc.errors, this.errors);
101
- Array.prototype.push.apply(doc.warnings, this.warnings);
100
+ for (let i = 0; i < this.errors.length; ++i)
101
+ doc.errors.push(this.errors[i]);
102
+ for (let i = 0; i < this.warnings.length; ++i)
103
+ doc.warnings.push(this.warnings[i]);
102
104
  }
103
105
  else {
104
106
  doc.errors = this.errors;
@@ -66,46 +66,47 @@ function plainValue(source, onError) {
66
66
  }
67
67
  if (badChar)
68
68
  onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
69
- return foldLines(source);
69
+ return unfoldLines(source);
70
70
  }
71
71
  function singleQuotedValue(source, onError) {
72
72
  if (source[source.length - 1] !== "'" || source.length === 1)
73
73
  onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
74
- return foldLines(source.slice(1, -1)).replace(/''/g, "'");
74
+ return unfoldLines(source.slice(1, -1)).replace(/''/g, "'");
75
75
  }
76
- function foldLines(source) {
76
+ function unfoldLines(source) {
77
+ const line = /(.*?)\r?\n/sy;
78
+ let match = line.exec(source);
79
+ if (!match)
80
+ return source;
77
81
  /**
78
- * The negative lookbehind here and in the `re` RegExp is to
82
+ * The negative lookbehinds in these RegExps are to
79
83
  * prevent causing a polynomial search time in certain cases.
80
84
  *
81
- * The try-catch is for Safari, which doesn't support this yet:
85
+ * The try-catch is for Safari < 16.4 and other old browsers:
82
86
  * https://caniuse.com/js-regexp-lookbehind
83
87
  */
84
- let first, line;
88
+ let trimEnd, trimBoth;
85
89
  try {
86
- first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
87
- line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
90
+ trimEnd = new RegExp('(?<![ \t])[ \t]+$');
91
+ trimBoth = new RegExp('^[ \t]+|(?<![ \t])[ \t]+$', 'g');
88
92
  }
89
93
  catch {
90
- first = /(.*?)[ \t]*\r?\n/sy;
91
- line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
94
+ trimEnd = /[ \t]+$/;
95
+ trimBoth = /^[ \t]+|[ \t]+$/g;
92
96
  }
93
- let match = first.exec(source);
94
- if (!match)
95
- return source;
96
- let res = match[1];
97
+ let res = match[1].replace(trimEnd, '');
97
98
  let sep = ' ';
98
- let pos = first.lastIndex;
99
- line.lastIndex = pos;
99
+ let pos = line.lastIndex;
100
100
  while ((match = line.exec(source))) {
101
- if (match[1] === '') {
101
+ const lm = match[1].replace(trimBoth, '');
102
+ if (lm === '') {
102
103
  if (sep === '\n')
103
104
  res += sep;
104
105
  else
105
106
  sep = '\n';
106
107
  }
107
108
  else {
108
- res += sep + match[1];
109
+ res += sep + lm;
109
110
  sep = ' ';
110
111
  }
111
112
  pos = line.lastIndex;
@@ -45,38 +45,40 @@ class Alias extends Node.NodeBase {
45
45
  if (node.anchor === this.source)
46
46
  found = node;
47
47
  }
48
+ if (found && ctx) {
49
+ const { anchors, doc, maxAliasCount } = ctx;
50
+ let data = anchors.get(found);
51
+ if (!data) {
52
+ // Resolve anchors for Node.prototype.toJS()
53
+ toJS.toJS(found, null, ctx);
54
+ data = anchors.get(found);
55
+ }
56
+ /* istanbul ignore if */
57
+ if (data?.res === undefined) {
58
+ const msg = 'This should not happen: Alias anchor was not resolved?';
59
+ throw new ReferenceError(msg);
60
+ }
61
+ if (maxAliasCount >= 0) {
62
+ data.count += 1;
63
+ if (data.aliasCount === 0)
64
+ data.aliasCount = getAliasCount(doc, found, anchors);
65
+ if (data.count * data.aliasCount > maxAliasCount) {
66
+ const msg = 'Excessive alias count indicates a resource exhaustion attack';
67
+ throw new ReferenceError(msg);
68
+ }
69
+ }
70
+ }
48
71
  return found;
49
72
  }
50
73
  toJSON(_arg, ctx) {
51
74
  if (!ctx)
52
75
  return { source: this.source };
53
- const { anchors, doc, maxAliasCount } = ctx;
54
- const source = this.resolve(doc, ctx);
76
+ const source = this.resolve(ctx.doc, ctx);
55
77
  if (!source) {
56
78
  const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
57
79
  throw new ReferenceError(msg);
58
80
  }
59
- let data = anchors.get(source);
60
- if (!data) {
61
- // Resolve anchors for Node.prototype.toJS()
62
- toJS.toJS(source, null, ctx);
63
- data = anchors.get(source);
64
- }
65
- /* istanbul ignore if */
66
- if (data?.res === undefined) {
67
- const msg = 'This should not happen: Alias anchor was not resolved?';
68
- throw new ReferenceError(msg);
69
- }
70
- if (maxAliasCount >= 0) {
71
- data.count += 1;
72
- if (data.aliasCount === 0)
73
- data.aliasCount = getAliasCount(doc, source, anchors);
74
- if (data.count * data.aliasCount > maxAliasCount) {
75
- const msg = 'Excessive alias count indicates a resource exhaustion attack';
76
- throw new ReferenceError(msg);
77
- }
78
- }
79
- return data.res;
81
+ return ctx.anchors.get(source).res;
80
82
  }
81
83
  toString(ctx, _onComment, _onChompKeep) {
82
84
  const src = `*${this.source}`;
@@ -312,7 +312,7 @@ class Lexer {
312
312
  const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
313
313
  this.indentNext = this.indentValue + 1;
314
314
  this.indentValue += n;
315
- return yield* this.parseBlockStart();
315
+ return 'block-start';
316
316
  }
317
317
  return 'doc';
318
318
  }
@@ -633,32 +633,36 @@ class Lexer {
633
633
  return 0;
634
634
  }
635
635
  *pushIndicators() {
636
- switch (this.charAt(0)) {
637
- case '!':
638
- return ((yield* this.pushTag()) +
639
- (yield* this.pushSpaces(true)) +
640
- (yield* this.pushIndicators()));
641
- case '&':
642
- return ((yield* this.pushUntil(isNotAnchorChar)) +
643
- (yield* this.pushSpaces(true)) +
644
- (yield* this.pushIndicators()));
645
- case '-': // this is an error
646
- case '?': // this is an error outside flow collections
647
- case ':': {
648
- const inFlow = this.flowLevel > 0;
649
- const ch1 = this.charAt(1);
650
- if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
651
- if (!inFlow)
652
- this.indentNext = this.indentValue + 1;
653
- else if (this.flowKey)
654
- this.flowKey = false;
655
- return ((yield* this.pushCount(1)) +
656
- (yield* this.pushSpaces(true)) +
657
- (yield* this.pushIndicators()));
636
+ let n = 0;
637
+ loop: while (true) {
638
+ switch (this.charAt(0)) {
639
+ case '!':
640
+ n += yield* this.pushTag();
641
+ n += yield* this.pushSpaces(true);
642
+ continue loop;
643
+ case '&':
644
+ n += yield* this.pushUntil(isNotAnchorChar);
645
+ n += yield* this.pushSpaces(true);
646
+ continue loop;
647
+ case '-': // this is an error
648
+ case '?': // this is an error outside flow collections
649
+ case ':': {
650
+ const inFlow = this.flowLevel > 0;
651
+ const ch1 = this.charAt(1);
652
+ if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
653
+ if (!inFlow)
654
+ this.indentNext = this.indentValue + 1;
655
+ else if (this.flowKey)
656
+ this.flowKey = false;
657
+ n += yield* this.pushCount(1);
658
+ n += yield* this.pushSpaces(true);
659
+ continue loop;
660
+ }
658
661
  }
659
662
  }
663
+ break loop;
660
664
  }
661
- return 0;
665
+ return n;
662
666
  }
663
667
  *pushTag() {
664
668
  if (this.charAt(1) === '<') {
@@ -70,6 +70,14 @@ function getFirstKeyStartProps(prev) {
70
70
  }
71
71
  return prev.splice(i, prev.length);
72
72
  }
73
+ function arrayPushArray(target, source) {
74
+ // May exhaust call stack with large `source` array
75
+ if (source.length < 1e5)
76
+ Array.prototype.push.apply(target, source);
77
+ else
78
+ for (let i = 0; i < source.length; ++i)
79
+ target.push(source[i]);
80
+ }
73
81
  function fixFlowSeqItems(fc) {
74
82
  if (fc.start.type === 'flow-seq-start') {
75
83
  for (const it of fc.items) {
@@ -82,12 +90,12 @@ function fixFlowSeqItems(fc) {
82
90
  delete it.key;
83
91
  if (isFlowToken(it.value)) {
84
92
  if (it.value.end)
85
- Array.prototype.push.apply(it.value.end, it.sep);
93
+ arrayPushArray(it.value.end, it.sep);
86
94
  else
87
95
  it.value.end = it.sep;
88
96
  }
89
97
  else
90
- Array.prototype.push.apply(it.start, it.sep);
98
+ arrayPushArray(it.start, it.sep);
91
99
  delete it.sep;
92
100
  }
93
101
  }
@@ -507,7 +515,7 @@ class Parser {
507
515
  const prev = map.items[map.items.length - 2];
508
516
  const end = prev?.value?.end;
509
517
  if (Array.isArray(end)) {
510
- Array.prototype.push.apply(end, it.start);
518
+ arrayPushArray(end, it.start);
511
519
  end.push(this.sourceToken);
512
520
  map.items.pop();
513
521
  return;
@@ -722,7 +730,7 @@ class Parser {
722
730
  const prev = seq.items[seq.items.length - 2];
723
731
  const end = prev?.value?.end;
724
732
  if (Array.isArray(end)) {
725
- Array.prototype.push.apply(end, it.start);
733
+ arrayPushArray(end, it.start);
726
734
  end.push(this.sourceToken);
727
735
  seq.items.pop();
728
736
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.8.4",
3
+ "version": "2.9.1",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "funding": "https://github.com/sponsors/eemeli",