Dashboard sipadu mbip
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _warnings = require("../warnings");
  7. var _constants = require("../constants");
  8. var _errors = require("../errors");
  9. var _stringify = require("../stringify");
  10. var _tags = require("../tags");
  11. var _string = require("../tags/failsafe/string");
  12. var _Alias = _interopRequireDefault(require("./Alias"));
  13. var _Collection = _interopRequireDefault(require("./Collection"));
  14. var _Node = _interopRequireDefault(require("./Node"));
  15. var _Pair = _interopRequireDefault(require("./Pair"));
  16. var _Scalar = _interopRequireDefault(require("./Scalar"));
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  19. const isMap = ({
  20. type
  21. }) => type === _constants.Type.FLOW_MAP || type === _constants.Type.MAP;
  22. const isSeq = ({
  23. type
  24. }) => type === _constants.Type.FLOW_SEQ || type === _constants.Type.SEQ;
  25. class Schema {
  26. constructor({
  27. customTags,
  28. merge,
  29. schema,
  30. tags: deprecatedCustomTags
  31. }) {
  32. this.merge = !!merge;
  33. this.name = schema;
  34. this.tags = _tags.schemas[schema.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
  35. if (!this.tags) {
  36. const keys = Object.keys(_tags.schemas).map(key => JSON.stringify(key)).join(', ');
  37. throw new Error(`Unknown schema "${schema}"; use one of ${keys}`);
  38. }
  39. if (!customTags && deprecatedCustomTags) {
  40. customTags = deprecatedCustomTags;
  41. (0, _warnings.warnOptionDeprecation)('tags', 'customTags');
  42. }
  43. if (Array.isArray(customTags)) {
  44. for (const tag of customTags) this.tags = this.tags.concat(tag);
  45. } else if (typeof customTags === 'function') {
  46. this.tags = customTags(this.tags.slice());
  47. }
  48. for (let i = 0; i < this.tags.length; ++i) {
  49. const tag = this.tags[i];
  50. if (typeof tag === 'string') {
  51. const tagObj = _tags.tags[tag];
  52. if (!tagObj) {
  53. const keys = Object.keys(_tags.tags).map(key => JSON.stringify(key)).join(', ');
  54. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  55. }
  56. this.tags[i] = tagObj;
  57. }
  58. }
  59. }
  60. createNode(value, wrapScalars, tag, ctx) {
  61. if (value instanceof _Node.default) return value;
  62. let tagObj;
  63. if (tag) {
  64. if (tag.startsWith('!!')) tag = Schema.defaultPrefix + tag.slice(2);
  65. const match = this.tags.filter(t => t.tag === tag);
  66. tagObj = match.find(t => !t.format) || match[0];
  67. if (!tagObj) throw new Error(`Tag ${tag} not found`);
  68. } else {
  69. // TODO: deprecate/remove class check
  70. tagObj = this.tags.find(t => (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format);
  71. if (!tagObj) {
  72. if (typeof value.toJSON === 'function') value = value.toJSON();
  73. if (typeof value !== 'object') return wrapScalars ? new _Scalar.default(value) : value;
  74. tagObj = value instanceof Map ? _tags.tags.map : value[Symbol.iterator] ? _tags.tags.seq : _tags.tags.map;
  75. }
  76. }
  77. if (!ctx) ctx = {
  78. wrapScalars
  79. };else ctx.wrapScalars = wrapScalars;
  80. if (ctx.onTagObj) {
  81. ctx.onTagObj(tagObj);
  82. delete ctx.onTagObj;
  83. }
  84. const obj = {};
  85. if (value && typeof value === 'object' && ctx.prevObjects) {
  86. const prev = ctx.prevObjects.find(o => o.value === value);
  87. if (prev) {
  88. const alias = new _Alias.default(prev); // leaves source dirty; must be cleaned by caller
  89. ctx.aliasNodes.push(alias);
  90. return alias;
  91. }
  92. obj.value = value;
  93. ctx.prevObjects.push(obj);
  94. }
  95. obj.node = tagObj.createNode ? tagObj.createNode(this, value, ctx) : wrapScalars ? new _Scalar.default(value) : value;
  96. return obj.node;
  97. }
  98. createPair(key, value, ctx) {
  99. const k = this.createNode(key, ctx.wrapScalars, null, ctx);
  100. const v = this.createNode(value, ctx.wrapScalars, null, ctx);
  101. return new _Pair.default(k, v);
  102. } // falls back to string on no match
  103. resolveScalar(str, tags) {
  104. if (!tags) tags = this.tags;
  105. for (let i = 0; i < tags.length; ++i) {
  106. const {
  107. format,
  108. test,
  109. resolve
  110. } = tags[i];
  111. if (test) {
  112. const match = str.match(test);
  113. if (match) {
  114. let res = resolve.apply(null, match);
  115. if (!(res instanceof _Scalar.default)) res = new _Scalar.default(res);
  116. if (format) res.format = format;
  117. return res;
  118. }
  119. }
  120. }
  121. if (this.tags.scalarFallback) str = this.tags.scalarFallback(str);
  122. return new _Scalar.default(str);
  123. } // sets node.resolved on success
  124. resolveNode(doc, node, tagName) {
  125. const tags = this.tags.filter(({
  126. tag
  127. }) => tag === tagName);
  128. const generic = tags.find(({
  129. test
  130. }) => !test);
  131. if (node.error) doc.errors.push(node.error);
  132. try {
  133. if (generic) {
  134. let res = generic.resolve(doc, node);
  135. if (!(res instanceof _Collection.default)) res = new _Scalar.default(res);
  136. node.resolved = res;
  137. } else {
  138. const str = (0, _string.resolveString)(doc, node);
  139. if (typeof str === 'string' && tags.length > 0) {
  140. node.resolved = this.resolveScalar(str, tags);
  141. }
  142. }
  143. } catch (error) {
  144. if (!error.source) error.source = node;
  145. doc.errors.push(error);
  146. node.resolved = null;
  147. }
  148. if (!node.resolved) return null;
  149. if (tagName && node.tag) node.resolved.tag = tagName;
  150. return node.resolved;
  151. }
  152. resolveNodeWithFallback(doc, node, tagName) {
  153. const res = this.resolveNode(doc, node, tagName);
  154. if (Object.prototype.hasOwnProperty.call(node, 'resolved')) return res;
  155. const fallback = isMap(node) ? Schema.defaultTags.MAP : isSeq(node) ? Schema.defaultTags.SEQ : Schema.defaultTags.STR;
  156. if (fallback) {
  157. doc.warnings.push(new _errors.YAMLWarning(node, `The tag ${tagName} is unavailable, falling back to ${fallback}`));
  158. const res = this.resolveNode(doc, node, fallback);
  159. res.tag = tagName;
  160. return res;
  161. } else {
  162. doc.errors.push(new _errors.YAMLReferenceError(node, `The tag ${tagName} is unavailable`));
  163. }
  164. return null;
  165. }
  166. getTagObject(item) {
  167. if (item instanceof _Alias.default) return _Alias.default;
  168. if (item.tag) {
  169. const match = this.tags.filter(t => t.tag === item.tag);
  170. if (match.length > 0) return match.find(t => t.format === item.format) || match[0];
  171. }
  172. let tagObj, obj;
  173. if (item instanceof _Scalar.default) {
  174. obj = item.value; // TODO: deprecate/remove class check
  175. const match = this.tags.filter(t => t.identify && t.identify(obj) || t.class && obj instanceof t.class);
  176. tagObj = match.find(t => t.format === item.format) || match.find(t => !t.format);
  177. } else {
  178. obj = item;
  179. tagObj = this.tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
  180. }
  181. if (!tagObj) {
  182. const name = obj && obj.constructor ? obj.constructor.name : typeof obj;
  183. throw new Error(`Tag not resolved for ${name} value`);
  184. }
  185. return tagObj;
  186. } // needs to be called before stringifier to allow for circular anchor refs
  187. stringifyProps(node, tagObj, {
  188. anchors,
  189. doc
  190. }) {
  191. const props = [];
  192. const anchor = doc.anchors.getName(node);
  193. if (anchor) {
  194. anchors[anchor] = node;
  195. props.push(`&${anchor}`);
  196. }
  197. if (node.tag) {
  198. props.push(doc.stringifyTag(node.tag));
  199. } else if (!tagObj.default) {
  200. props.push(doc.stringifyTag(tagObj.tag));
  201. }
  202. return props.join(' ');
  203. }
  204. stringify(item, ctx, onComment, onChompKeep) {
  205. let tagObj;
  206. if (!(item instanceof _Node.default)) {
  207. const createCtx = {
  208. aliasNodes: [],
  209. onTagObj: o => tagObj = o,
  210. prevObjects: []
  211. };
  212. item = this.createNode(item, true, null, createCtx);
  213. const {
  214. anchors
  215. } = ctx.doc;
  216. for (const alias of createCtx.aliasNodes) {
  217. alias.source = alias.source.node;
  218. let name = anchors.getName(alias.source);
  219. if (!name) {
  220. name = anchors.newName();
  221. anchors.map[name] = alias.source;
  222. }
  223. }
  224. }
  225. ctx.tags = this;
  226. if (item instanceof _Pair.default) return item.toString(ctx, onComment, onChompKeep);
  227. if (!tagObj) tagObj = this.getTagObject(item);
  228. const props = this.stringifyProps(item, tagObj, ctx);
  229. const 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);
  230. return props ? item instanceof _Collection.default && str[0] !== '{' && str[0] !== '[' ? `${props}\n${ctx.indent}${str}` : `${props} ${str}` : str;
  231. }
  232. }
  233. exports.default = Schema;
  234. _defineProperty(Schema, "defaultPrefix", 'tag:yaml.org,2002:');
  235. _defineProperty(Schema, "defaultTags", {
  236. MAP: 'tag:yaml.org,2002:map',
  237. SEQ: 'tag:yaml.org,2002:seq',
  238. STR: 'tag:yaml.org,2002:str'
  239. });