Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  11. var _warnings = require("../warnings");
  12. var _constants = require("../constants");
  13. var _errors = require("../errors");
  14. var _stringify = require("../stringify");
  15. var _tags = require("../tags");
  16. var _string = require("../tags/failsafe/string");
  17. var _Alias = _interopRequireDefault(require("./Alias"));
  18. var _Collection = _interopRequireDefault(require("./Collection"));
  19. var _Node = _interopRequireDefault(require("./Node"));
  20. var _Pair = _interopRequireDefault(require("./Pair"));
  21. var _Scalar = _interopRequireDefault(require("./Scalar"));
  22. var isMap = function isMap(_ref) {
  23. var type = _ref.type;
  24. return type === _constants.Type.FLOW_MAP || type === _constants.Type.MAP;
  25. };
  26. var isSeq = function isSeq(_ref2) {
  27. var type = _ref2.type;
  28. return type === _constants.Type.FLOW_SEQ || type === _constants.Type.SEQ;
  29. };
  30. var Schema =
  31. /*#__PURE__*/
  32. function () {
  33. function Schema(_ref3) {
  34. var customTags = _ref3.customTags,
  35. merge = _ref3.merge,
  36. schema = _ref3.schema,
  37. deprecatedCustomTags = _ref3.tags;
  38. (0, _classCallCheck2.default)(this, Schema);
  39. this.merge = !!merge;
  40. this.name = schema;
  41. this.tags = _tags.schemas[schema.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
  42. if (!this.tags) {
  43. var keys = Object.keys(_tags.schemas).map(function (key) {
  44. return JSON.stringify(key);
  45. }).join(', ');
  46. throw new Error("Unknown schema \"".concat(schema, "\"; use one of ").concat(keys));
  47. }
  48. if (!customTags && deprecatedCustomTags) {
  49. customTags = deprecatedCustomTags;
  50. (0, _warnings.warnOptionDeprecation)('tags', 'customTags');
  51. }
  52. if (Array.isArray(customTags)) {
  53. var _iteratorNormalCompletion = true;
  54. var _didIteratorError = false;
  55. var _iteratorError = undefined;
  56. try {
  57. for (var _iterator = customTags[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  58. var tag = _step.value;
  59. this.tags = this.tags.concat(tag);
  60. }
  61. } catch (err) {
  62. _didIteratorError = true;
  63. _iteratorError = err;
  64. } finally {
  65. try {
  66. if (!_iteratorNormalCompletion && _iterator.return != null) {
  67. _iterator.return();
  68. }
  69. } finally {
  70. if (_didIteratorError) {
  71. throw _iteratorError;
  72. }
  73. }
  74. }
  75. } else if (typeof customTags === 'function') {
  76. this.tags = customTags(this.tags.slice());
  77. }
  78. for (var i = 0; i < this.tags.length; ++i) {
  79. var _tag = this.tags[i];
  80. if (typeof _tag === 'string') {
  81. var tagObj = _tags.tags[_tag];
  82. if (!tagObj) {
  83. var _keys = Object.keys(_tags.tags).map(function (key) {
  84. return JSON.stringify(key);
  85. }).join(', ');
  86. throw new Error("Unknown custom tag \"".concat(_tag, "\"; use one of ").concat(_keys));
  87. }
  88. this.tags[i] = tagObj;
  89. }
  90. }
  91. }
  92. (0, _createClass2.default)(Schema, [{
  93. key: "createNode",
  94. value: function createNode(value, wrapScalars, tag, ctx) {
  95. if (value instanceof _Node.default) return value;
  96. var tagObj;
  97. if (tag) {
  98. if (tag.startsWith('!!')) tag = Schema.defaultPrefix + tag.slice(2);
  99. var match = this.tags.filter(function (t) {
  100. return t.tag === tag;
  101. });
  102. tagObj = match.find(function (t) {
  103. return !t.format;
  104. }) || match[0];
  105. if (!tagObj) throw new Error("Tag ".concat(tag, " not found"));
  106. } else {
  107. // TODO: deprecate/remove class check
  108. tagObj = this.tags.find(function (t) {
  109. return (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format;
  110. });
  111. if (!tagObj) {
  112. if (typeof value.toJSON === 'function') value = value.toJSON();
  113. if ((0, _typeof2.default)(value) !== 'object') return wrapScalars ? new _Scalar.default(value) : value;
  114. tagObj = value instanceof Map ? _tags.tags.map : value[Symbol.iterator] ? _tags.tags.seq : _tags.tags.map;
  115. }
  116. }
  117. if (!ctx) ctx = {
  118. wrapScalars: wrapScalars
  119. };else ctx.wrapScalars = wrapScalars;
  120. if (ctx.onTagObj) {
  121. ctx.onTagObj(tagObj);
  122. delete ctx.onTagObj;
  123. }
  124. var obj = {};
  125. if (value && (0, _typeof2.default)(value) === 'object' && ctx.prevObjects) {
  126. var prev = ctx.prevObjects.find(function (o) {
  127. return o.value === value;
  128. });
  129. if (prev) {
  130. var alias = new _Alias.default(prev); // leaves source dirty; must be cleaned by caller
  131. ctx.aliasNodes.push(alias);
  132. return alias;
  133. }
  134. obj.value = value;
  135. ctx.prevObjects.push(obj);
  136. }
  137. obj.node = tagObj.createNode ? tagObj.createNode(this, value, ctx) : wrapScalars ? new _Scalar.default(value) : value;
  138. return obj.node;
  139. }
  140. }, {
  141. key: "createPair",
  142. value: function createPair(key, value, ctx) {
  143. var k = this.createNode(key, ctx.wrapScalars, null, ctx);
  144. var v = this.createNode(value, ctx.wrapScalars, null, ctx);
  145. return new _Pair.default(k, v);
  146. } // falls back to string on no match
  147. }, {
  148. key: "resolveScalar",
  149. value: function resolveScalar(str, tags) {
  150. if (!tags) tags = this.tags;
  151. for (var i = 0; i < tags.length; ++i) {
  152. var _tags$i = tags[i],
  153. format = _tags$i.format,
  154. test = _tags$i.test,
  155. resolve = _tags$i.resolve;
  156. if (test) {
  157. var match = str.match(test);
  158. if (match) {
  159. var res = resolve.apply(null, match);
  160. if (!(res instanceof _Scalar.default)) res = new _Scalar.default(res);
  161. if (format) res.format = format;
  162. return res;
  163. }
  164. }
  165. }
  166. if (this.tags.scalarFallback) str = this.tags.scalarFallback(str);
  167. return new _Scalar.default(str);
  168. } // sets node.resolved on success
  169. }, {
  170. key: "resolveNode",
  171. value: function resolveNode(doc, node, tagName) {
  172. var tags = this.tags.filter(function (_ref4) {
  173. var tag = _ref4.tag;
  174. return tag === tagName;
  175. });
  176. var generic = tags.find(function (_ref5) {
  177. var test = _ref5.test;
  178. return !test;
  179. });
  180. if (node.error) doc.errors.push(node.error);
  181. try {
  182. if (generic) {
  183. var res = generic.resolve(doc, node);
  184. if (!(res instanceof _Collection.default)) res = new _Scalar.default(res);
  185. node.resolved = res;
  186. } else {
  187. var str = (0, _string.resolveString)(doc, node);
  188. if (typeof str === 'string' && tags.length > 0) {
  189. node.resolved = this.resolveScalar(str, tags);
  190. }
  191. }
  192. } catch (error) {
  193. if (!error.source) error.source = node;
  194. doc.errors.push(error);
  195. node.resolved = null;
  196. }
  197. if (!node.resolved) return null;
  198. if (tagName && node.tag) node.resolved.tag = tagName;
  199. return node.resolved;
  200. }
  201. }, {
  202. key: "resolveNodeWithFallback",
  203. value: function resolveNodeWithFallback(doc, node, tagName) {
  204. var res = this.resolveNode(doc, node, tagName);
  205. if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res;
  206. var fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR;
  207. if (fallback) {
  208. doc.warnings.push(new _errors.YAMLWarning(node, "The tag ".concat(tagName, " is unavailable, falling back to ").concat(fallback)));
  209. var _res = this.resolveNode(doc, node, fallback);
  210. _res.tag = tagName;
  211. return _res;
  212. } else {
  213. doc.errors.push(new _errors.YAMLReferenceError(node, "The tag ".concat(tagName, " is unavailable")));
  214. }
  215. return null;
  216. }
  217. }, {
  218. key: "getTagObject",
  219. value: function getTagObject(item) {
  220. if (item instanceof _Alias.default) return _Alias.default;
  221. if (item.tag) {
  222. var match = this.tags.filter(function (t) {
  223. return t.tag === item.tag;
  224. });
  225. if (match.length > 0) return match.find(function (t) {
  226. return t.format === item.format;
  227. }) || match[0];
  228. }
  229. var tagObj, obj;
  230. if (item instanceof _Scalar.default) {
  231. obj = item.value; // TODO: deprecate/remove class check
  232. var _match = this.tags.filter(function (t) {
  233. return t.identify && t.identify(obj) || t.class && obj instanceof t.class;
  234. });
  235. tagObj = _match.find(function (t) {
  236. return t.format === item.format;
  237. }) || _match.find(function (t) {
  238. return !t.format;
  239. });
  240. } else {
  241. obj = item;
  242. tagObj = this.tags.find(function (t) {
  243. return t.nodeClass && obj instanceof t.nodeClass;
  244. });
  245. }
  246. if (!tagObj) {
  247. var name = obj && obj.constructor ? obj.constructor.name : (0, _typeof2.default)(obj);
  248. throw new Error("Tag not resolved for ".concat(name, " value"));
  249. }
  250. return tagObj;
  251. } // needs to be called before stringifier to allow for circular anchor refs
  252. }, {
  253. key: "stringifyProps",
  254. value: function stringifyProps(node, tagObj, _ref6) {
  255. var anchors = _ref6.anchors,
  256. doc = _ref6.doc;
  257. var props = [];
  258. var anchor = doc.anchors.getName(node);
  259. if (anchor) {
  260. anchors[anchor] = node;
  261. props.push("&".concat(anchor));
  262. }
  263. if (node.tag) {
  264. props.push(doc.stringifyTag(node.tag));
  265. } else if (!tagObj.default) {
  266. props.push(doc.stringifyTag(tagObj.tag));
  267. }
  268. return props.join(' ');
  269. }
  270. }, {
  271. key: "stringify",
  272. value: function stringify(item, ctx, onComment, onChompKeep) {
  273. var tagObj;
  274. if (!(item instanceof _Node.default)) {
  275. var createCtx = {
  276. aliasNodes: [],
  277. onTagObj: function onTagObj(o) {
  278. return tagObj = o;
  279. },
  280. prevObjects: []
  281. };
  282. item = this.createNode(item, true, null, createCtx);
  283. var anchors = ctx.doc.anchors;
  284. var _iteratorNormalCompletion2 = true;
  285. var _didIteratorError2 = false;
  286. var _iteratorError2 = undefined;
  287. try {
  288. for (var _iterator2 = createCtx.aliasNodes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  289. var alias = _step2.value;
  290. alias.source = alias.source.node;
  291. var name = anchors.getName(alias.source);
  292. if (!name) {
  293. name = anchors.newName();
  294. anchors.map[name] = alias.source;
  295. }
  296. }
  297. } catch (err) {
  298. _didIteratorError2 = true;
  299. _iteratorError2 = err;
  300. } finally {
  301. try {
  302. if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
  303. _iterator2.return();
  304. }
  305. } finally {
  306. if (_didIteratorError2) {
  307. throw _iteratorError2;
  308. }
  309. }
  310. }
  311. }
  312. ctx.tags = this;
  313. if (item instanceof _Pair.default) return item.toString(ctx, onComment, onChompKeep);
  314. if (!tagObj) tagObj = this.getTagObject(item);
  315. var props = this.stringifyProps(item, tagObj, ctx);
  316. var str = typeof tagObj.stringify === 'function' ? tagObj.stringify(item, ctx, onComment, onChompKeep) : item instanceof _Collection.default ? item.toString(ctx, onComment, onChompKeep) : (0, _stringify.stringifyString)(item, ctx, onComment, onChompKeep);
  317. return props ? item instanceof _Collection.default && str[0] !== '{' && str[0] !== '[' ? "".concat(props, "\n").concat(ctx.indent).concat(str) : "".concat(props, " ").concat(str) : str;
  318. }
  319. }]);
  320. return Schema;
  321. }();
  322. exports.default = Schema;
  323. (0, _defineProperty2.default)(Schema, "defaultPrefix", 'tag:yaml.org,2002:');
  324. (0, _defineProperty2.default)(Schema, "defaultTags", {
  325. MAP: 'tag:yaml.org,2002:map',
  326. SEQ: 'tag:yaml.org,2002:seq',
  327. STR: 'tag:yaml.org,2002:str'
  328. });