Dashboard sipadu mbip
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Alias.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  9. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  10. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  11. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  12. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  13. var _constants = require("../constants");
  14. var _errors = require("../errors");
  15. var _toJSON2 = _interopRequireDefault(require("../toJSON"));
  16. var _Collection = _interopRequireDefault(require("./Collection"));
  17. var _Node2 = _interopRequireDefault(require("./Node"));
  18. var _Pair = _interopRequireDefault(require("./Pair"));
  19. var getAliasCount = function getAliasCount(node, anchors) {
  20. if (node instanceof Alias) {
  21. var anchor = anchors.find(function (a) {
  22. return a.node === node.source;
  23. });
  24. return anchor.count * anchor.aliasCount;
  25. } else if (node instanceof _Collection.default) {
  26. var count = 0;
  27. var _iteratorNormalCompletion = true;
  28. var _didIteratorError = false;
  29. var _iteratorError = undefined;
  30. try {
  31. for (var _iterator = node.items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  32. var item = _step.value;
  33. var c = getAliasCount(item, anchors);
  34. if (c > count) count = c;
  35. }
  36. } catch (err) {
  37. _didIteratorError = true;
  38. _iteratorError = err;
  39. } finally {
  40. try {
  41. if (!_iteratorNormalCompletion && _iterator.return != null) {
  42. _iterator.return();
  43. }
  44. } finally {
  45. if (_didIteratorError) {
  46. throw _iteratorError;
  47. }
  48. }
  49. }
  50. return count;
  51. } else if (node instanceof _Pair.default) {
  52. var kc = getAliasCount(node.key, anchors);
  53. var vc = getAliasCount(node.value, anchors);
  54. return Math.max(kc, vc);
  55. }
  56. return 1;
  57. };
  58. var Alias =
  59. /*#__PURE__*/
  60. function (_Node) {
  61. (0, _inherits2.default)(Alias, _Node);
  62. (0, _createClass2.default)(Alias, null, [{
  63. key: "stringify",
  64. value: function stringify(_ref, _ref2) {
  65. var range = _ref.range,
  66. source = _ref.source;
  67. var anchors = _ref2.anchors,
  68. doc = _ref2.doc,
  69. implicitKey = _ref2.implicitKey,
  70. inStringifyKey = _ref2.inStringifyKey;
  71. var anchor = Object.keys(anchors).find(function (a) {
  72. return anchors[a] === source;
  73. });
  74. if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName();
  75. if (anchor) return "*".concat(anchor).concat(implicitKey ? ' ' : '');
  76. var msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node';
  77. throw new Error("".concat(msg, " [").concat(range, "]"));
  78. }
  79. }]);
  80. function Alias(source) {
  81. var _this;
  82. (0, _classCallCheck2.default)(this, Alias);
  83. _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(Alias).call(this));
  84. _this.source = source;
  85. _this.type = _constants.Type.ALIAS;
  86. return _this;
  87. }
  88. (0, _createClass2.default)(Alias, [{
  89. key: "toJSON",
  90. value: function toJSON(arg, ctx) {
  91. var _this2 = this;
  92. if (!ctx) return (0, _toJSON2.default)(this.source, arg, ctx);
  93. var anchors = ctx.anchors,
  94. maxAliasCount = ctx.maxAliasCount;
  95. var anchor = anchors.find(function (a) {
  96. return a.node === _this2.source;
  97. });
  98. if (!anchor || anchor.res === undefined) {
  99. var msg = 'This should not happen: Alias anchor was not resolved?';
  100. if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
  101. }
  102. if (maxAliasCount >= 0) {
  103. anchor.count += 1;
  104. if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors);
  105. if (anchor.count * anchor.aliasCount > maxAliasCount) {
  106. var _msg = 'Excessive alias count indicates a resource exhaustion attack';
  107. if (this.cstNode) throw new _errors.YAMLReferenceError(this.cstNode, _msg);else throw new ReferenceError(_msg);
  108. }
  109. }
  110. return anchor.res;
  111. } // Only called when stringifying an alias mapping key while constructing
  112. // Object output.
  113. }, {
  114. key: "toString",
  115. value: function toString(ctx) {
  116. return Alias.stringify(this, ctx);
  117. }
  118. }, {
  119. key: "tag",
  120. set: function set(t) {
  121. throw new Error('Alias nodes cannot have tags');
  122. }
  123. }]);
  124. return Alias;
  125. }(_Node2.default);
  126. exports.default = Alias;
  127. (0, _defineProperty2.default)(Alias, "default", true);