Schema-88e323a7.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. 'use strict';
  2. var PlainValue = require('./PlainValue-ec8e588e.js');
  3. var resolveSeq = require('./resolveSeq-d03cb037.js');
  4. var warnings = require('./warnings-1000a372.js');
  5. function createMap(schema, obj, ctx) {
  6. const map = new resolveSeq.YAMLMap(schema);
  7. if (obj instanceof Map) {
  8. for (const [key, value] of obj) map.items.push(schema.createPair(key, value, ctx));
  9. } else if (obj && typeof obj === 'object') {
  10. for (const key of Object.keys(obj)) map.items.push(schema.createPair(key, obj[key], ctx));
  11. }
  12. if (typeof schema.sortMapEntries === 'function') {
  13. map.items.sort(schema.sortMapEntries);
  14. }
  15. return map;
  16. }
  17. const map = {
  18. createNode: createMap,
  19. default: true,
  20. nodeClass: resolveSeq.YAMLMap,
  21. tag: 'tag:yaml.org,2002:map',
  22. resolve: resolveSeq.resolveMap
  23. };
  24. function createSeq(schema, obj, ctx) {
  25. const seq = new resolveSeq.YAMLSeq(schema);
  26. if (obj && obj[Symbol.iterator]) {
  27. for (const it of obj) {
  28. const v = schema.createNode(it, ctx.wrapScalars, null, ctx);
  29. seq.items.push(v);
  30. }
  31. }
  32. return seq;
  33. }
  34. const seq = {
  35. createNode: createSeq,
  36. default: true,
  37. nodeClass: resolveSeq.YAMLSeq,
  38. tag: 'tag:yaml.org,2002:seq',
  39. resolve: resolveSeq.resolveSeq
  40. };
  41. const string = {
  42. identify: value => typeof value === 'string',
  43. default: true,
  44. tag: 'tag:yaml.org,2002:str',
  45. resolve: resolveSeq.resolveString,
  46. stringify(item, ctx, onComment, onChompKeep) {
  47. ctx = Object.assign({
  48. actualString: true
  49. }, ctx);
  50. return resolveSeq.stringifyString(item, ctx, onComment, onChompKeep);
  51. },
  52. options: resolveSeq.strOptions
  53. };
  54. const failsafe = [map, seq, string];
  55. /* global BigInt */
  56. const intIdentify$2 = value => typeof value === 'bigint' || Number.isInteger(value);
  57. const intResolve$1 = (src, part, radix) => resolveSeq.intOptions.asBigInt ? BigInt(src) : parseInt(part, radix);
  58. function intStringify$1(node, radix, prefix) {
  59. const {
  60. value
  61. } = node;
  62. if (intIdentify$2(value) && value >= 0) return prefix + value.toString(radix);
  63. return resolveSeq.stringifyNumber(node);
  64. }
  65. const nullObj = {
  66. identify: value => value == null,
  67. createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
  68. default: true,
  69. tag: 'tag:yaml.org,2002:null',
  70. test: /^(?:~|[Nn]ull|NULL)?$/,
  71. resolve: () => null,
  72. options: resolveSeq.nullOptions,
  73. stringify: () => resolveSeq.nullOptions.nullStr
  74. };
  75. const boolObj = {
  76. identify: value => typeof value === 'boolean',
  77. default: true,
  78. tag: 'tag:yaml.org,2002:bool',
  79. test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
  80. resolve: str => str[0] === 't' || str[0] === 'T',
  81. options: resolveSeq.boolOptions,
  82. stringify: ({
  83. value
  84. }) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr
  85. };
  86. const octObj = {
  87. identify: value => intIdentify$2(value) && value >= 0,
  88. default: true,
  89. tag: 'tag:yaml.org,2002:int',
  90. format: 'OCT',
  91. test: /^0o([0-7]+)$/,
  92. resolve: (str, oct) => intResolve$1(str, oct, 8),
  93. options: resolveSeq.intOptions,
  94. stringify: node => intStringify$1(node, 8, '0o')
  95. };
  96. const intObj = {
  97. identify: intIdentify$2,
  98. default: true,
  99. tag: 'tag:yaml.org,2002:int',
  100. test: /^[-+]?[0-9]+$/,
  101. resolve: str => intResolve$1(str, str, 10),
  102. options: resolveSeq.intOptions,
  103. stringify: resolveSeq.stringifyNumber
  104. };
  105. const hexObj = {
  106. identify: value => intIdentify$2(value) && value >= 0,
  107. default: true,
  108. tag: 'tag:yaml.org,2002:int',
  109. format: 'HEX',
  110. test: /^0x([0-9a-fA-F]+)$/,
  111. resolve: (str, hex) => intResolve$1(str, hex, 16),
  112. options: resolveSeq.intOptions,
  113. stringify: node => intStringify$1(node, 16, '0x')
  114. };
  115. const nanObj = {
  116. identify: value => typeof value === 'number',
  117. default: true,
  118. tag: 'tag:yaml.org,2002:float',
  119. test: /^(?:[-+]?\.inf|(\.nan))$/i,
  120. resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
  121. stringify: resolveSeq.stringifyNumber
  122. };
  123. const expObj = {
  124. identify: value => typeof value === 'number',
  125. default: true,
  126. tag: 'tag:yaml.org,2002:float',
  127. format: 'EXP',
  128. test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
  129. resolve: str => parseFloat(str),
  130. stringify: ({
  131. value
  132. }) => Number(value).toExponential()
  133. };
  134. const floatObj = {
  135. identify: value => typeof value === 'number',
  136. default: true,
  137. tag: 'tag:yaml.org,2002:float',
  138. test: /^[-+]?(?:\.([0-9]+)|[0-9]+\.([0-9]*))$/,
  139. resolve(str, frac1, frac2) {
  140. const frac = frac1 || frac2;
  141. const node = new resolveSeq.Scalar(parseFloat(str));
  142. if (frac && frac[frac.length - 1] === '0') node.minFractionDigits = frac.length;
  143. return node;
  144. },
  145. stringify: resolveSeq.stringifyNumber
  146. };
  147. const core = failsafe.concat([nullObj, boolObj, octObj, intObj, hexObj, nanObj, expObj, floatObj]);
  148. /* global BigInt */
  149. const intIdentify$1 = value => typeof value === 'bigint' || Number.isInteger(value);
  150. const stringifyJSON = ({
  151. value
  152. }) => JSON.stringify(value);
  153. const json = [map, seq, {
  154. identify: value => typeof value === 'string',
  155. default: true,
  156. tag: 'tag:yaml.org,2002:str',
  157. resolve: resolveSeq.resolveString,
  158. stringify: stringifyJSON
  159. }, {
  160. identify: value => value == null,
  161. createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
  162. default: true,
  163. tag: 'tag:yaml.org,2002:null',
  164. test: /^null$/,
  165. resolve: () => null,
  166. stringify: stringifyJSON
  167. }, {
  168. identify: value => typeof value === 'boolean',
  169. default: true,
  170. tag: 'tag:yaml.org,2002:bool',
  171. test: /^true|false$/,
  172. resolve: str => str === 'true',
  173. stringify: stringifyJSON
  174. }, {
  175. identify: intIdentify$1,
  176. default: true,
  177. tag: 'tag:yaml.org,2002:int',
  178. test: /^-?(?:0|[1-9][0-9]*)$/,
  179. resolve: str => resolveSeq.intOptions.asBigInt ? BigInt(str) : parseInt(str, 10),
  180. stringify: ({
  181. value
  182. }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
  183. }, {
  184. identify: value => typeof value === 'number',
  185. default: true,
  186. tag: 'tag:yaml.org,2002:float',
  187. test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
  188. resolve: str => parseFloat(str),
  189. stringify: stringifyJSON
  190. }];
  191. json.scalarFallback = str => {
  192. throw new SyntaxError(`Unresolved plain scalar ${JSON.stringify(str)}`);
  193. };
  194. /* global BigInt */
  195. const boolStringify = ({
  196. value
  197. }) => value ? resolveSeq.boolOptions.trueStr : resolveSeq.boolOptions.falseStr;
  198. const intIdentify = value => typeof value === 'bigint' || Number.isInteger(value);
  199. function intResolve(sign, src, radix) {
  200. let str = src.replace(/_/g, '');
  201. if (resolveSeq.intOptions.asBigInt) {
  202. switch (radix) {
  203. case 2:
  204. str = `0b${str}`;
  205. break;
  206. case 8:
  207. str = `0o${str}`;
  208. break;
  209. case 16:
  210. str = `0x${str}`;
  211. break;
  212. }
  213. const n = BigInt(str);
  214. return sign === '-' ? BigInt(-1) * n : n;
  215. }
  216. const n = parseInt(str, radix);
  217. return sign === '-' ? -1 * n : n;
  218. }
  219. function intStringify(node, radix, prefix) {
  220. const {
  221. value
  222. } = node;
  223. if (intIdentify(value)) {
  224. const str = value.toString(radix);
  225. return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
  226. }
  227. return resolveSeq.stringifyNumber(node);
  228. }
  229. const yaml11 = failsafe.concat([{
  230. identify: value => value == null,
  231. createNode: (schema, value, ctx) => ctx.wrapScalars ? new resolveSeq.Scalar(null) : null,
  232. default: true,
  233. tag: 'tag:yaml.org,2002:null',
  234. test: /^(?:~|[Nn]ull|NULL)?$/,
  235. resolve: () => null,
  236. options: resolveSeq.nullOptions,
  237. stringify: () => resolveSeq.nullOptions.nullStr
  238. }, {
  239. identify: value => typeof value === 'boolean',
  240. default: true,
  241. tag: 'tag:yaml.org,2002:bool',
  242. test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
  243. resolve: () => true,
  244. options: resolveSeq.boolOptions,
  245. stringify: boolStringify
  246. }, {
  247. identify: value => typeof value === 'boolean',
  248. default: true,
  249. tag: 'tag:yaml.org,2002:bool',
  250. test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
  251. resolve: () => false,
  252. options: resolveSeq.boolOptions,
  253. stringify: boolStringify
  254. }, {
  255. identify: intIdentify,
  256. default: true,
  257. tag: 'tag:yaml.org,2002:int',
  258. format: 'BIN',
  259. test: /^([-+]?)0b([0-1_]+)$/,
  260. resolve: (str, sign, bin) => intResolve(sign, bin, 2),
  261. stringify: node => intStringify(node, 2, '0b')
  262. }, {
  263. identify: intIdentify,
  264. default: true,
  265. tag: 'tag:yaml.org,2002:int',
  266. format: 'OCT',
  267. test: /^([-+]?)0([0-7_]+)$/,
  268. resolve: (str, sign, oct) => intResolve(sign, oct, 8),
  269. stringify: node => intStringify(node, 8, '0')
  270. }, {
  271. identify: intIdentify,
  272. default: true,
  273. tag: 'tag:yaml.org,2002:int',
  274. test: /^([-+]?)([0-9][0-9_]*)$/,
  275. resolve: (str, sign, abs) => intResolve(sign, abs, 10),
  276. stringify: resolveSeq.stringifyNumber
  277. }, {
  278. identify: intIdentify,
  279. default: true,
  280. tag: 'tag:yaml.org,2002:int',
  281. format: 'HEX',
  282. test: /^([-+]?)0x([0-9a-fA-F_]+)$/,
  283. resolve: (str, sign, hex) => intResolve(sign, hex, 16),
  284. stringify: node => intStringify(node, 16, '0x')
  285. }, {
  286. identify: value => typeof value === 'number',
  287. default: true,
  288. tag: 'tag:yaml.org,2002:float',
  289. test: /^(?:[-+]?\.inf|(\.nan))$/i,
  290. resolve: (str, nan) => nan ? NaN : str[0] === '-' ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY,
  291. stringify: resolveSeq.stringifyNumber
  292. }, {
  293. identify: value => typeof value === 'number',
  294. default: true,
  295. tag: 'tag:yaml.org,2002:float',
  296. format: 'EXP',
  297. test: /^[-+]?([0-9][0-9_]*)?(\.[0-9_]*)?[eE][-+]?[0-9]+$/,
  298. resolve: str => parseFloat(str.replace(/_/g, '')),
  299. stringify: ({
  300. value
  301. }) => Number(value).toExponential()
  302. }, {
  303. identify: value => typeof value === 'number',
  304. default: true,
  305. tag: 'tag:yaml.org,2002:float',
  306. test: /^[-+]?(?:[0-9][0-9_]*)?\.([0-9_]*)$/,
  307. resolve(str, frac) {
  308. const node = new resolveSeq.Scalar(parseFloat(str.replace(/_/g, '')));
  309. if (frac) {
  310. const f = frac.replace(/_/g, '');
  311. if (f[f.length - 1] === '0') node.minFractionDigits = f.length;
  312. }
  313. return node;
  314. },
  315. stringify: resolveSeq.stringifyNumber
  316. }], warnings.binary, warnings.omap, warnings.pairs, warnings.set, warnings.intTime, warnings.floatTime, warnings.timestamp);
  317. const schemas = {
  318. core,
  319. failsafe,
  320. json,
  321. yaml11
  322. };
  323. const tags = {
  324. binary: warnings.binary,
  325. bool: boolObj,
  326. float: floatObj,
  327. floatExp: expObj,
  328. floatNaN: nanObj,
  329. floatTime: warnings.floatTime,
  330. int: intObj,
  331. intHex: hexObj,
  332. intOct: octObj,
  333. intTime: warnings.intTime,
  334. map,
  335. null: nullObj,
  336. omap: warnings.omap,
  337. pairs: warnings.pairs,
  338. seq,
  339. set: warnings.set,
  340. timestamp: warnings.timestamp
  341. };
  342. function findTagObject(value, tagName, tags) {
  343. if (tagName) {
  344. const match = tags.filter(t => t.tag === tagName);
  345. const tagObj = match.find(t => !t.format) || match[0];
  346. if (!tagObj) throw new Error(`Tag ${tagName} not found`);
  347. return tagObj;
  348. } // TODO: deprecate/remove class check
  349. return tags.find(t => (t.identify && t.identify(value) || t.class && value instanceof t.class) && !t.format);
  350. }
  351. function createNode(value, tagName, ctx) {
  352. if (value instanceof resolveSeq.Node) return value;
  353. const {
  354. defaultPrefix,
  355. onTagObj,
  356. prevObjects,
  357. schema,
  358. wrapScalars
  359. } = ctx;
  360. if (tagName && tagName.startsWith('!!')) tagName = defaultPrefix + tagName.slice(2);
  361. let tagObj = findTagObject(value, tagName, schema.tags);
  362. if (!tagObj) {
  363. if (typeof value.toJSON === 'function') value = value.toJSON();
  364. if (!value || typeof value !== 'object') return wrapScalars ? new resolveSeq.Scalar(value) : value;
  365. tagObj = value instanceof Map ? map : value[Symbol.iterator] ? seq : map;
  366. }
  367. if (onTagObj) {
  368. onTagObj(tagObj);
  369. delete ctx.onTagObj;
  370. } // Detect duplicate references to the same object & use Alias nodes for all
  371. // after first. The `obj` wrapper allows for circular references to resolve.
  372. const obj = {
  373. value: undefined,
  374. node: undefined
  375. };
  376. if (value && typeof value === 'object' && prevObjects) {
  377. const prev = prevObjects.get(value);
  378. if (prev) {
  379. const alias = new resolveSeq.Alias(prev); // leaves source dirty; must be cleaned by caller
  380. ctx.aliasNodes.push(alias); // defined along with prevObjects
  381. return alias;
  382. }
  383. obj.value = value;
  384. prevObjects.set(value, obj);
  385. }
  386. obj.node = tagObj.createNode ? tagObj.createNode(ctx.schema, value, ctx) : wrapScalars ? new resolveSeq.Scalar(value) : value;
  387. if (tagName && obj.node instanceof resolveSeq.Node) obj.node.tag = tagName;
  388. return obj.node;
  389. }
  390. function getSchemaTags(schemas, knownTags, customTags, schemaId) {
  391. let tags = schemas[schemaId.replace(/\W/g, '')]; // 'yaml-1.1' -> 'yaml11'
  392. if (!tags) {
  393. const keys = Object.keys(schemas).map(key => JSON.stringify(key)).join(', ');
  394. throw new Error(`Unknown schema "${schemaId}"; use one of ${keys}`);
  395. }
  396. if (Array.isArray(customTags)) {
  397. for (const tag of customTags) tags = tags.concat(tag);
  398. } else if (typeof customTags === 'function') {
  399. tags = customTags(tags.slice());
  400. }
  401. for (let i = 0; i < tags.length; ++i) {
  402. const tag = tags[i];
  403. if (typeof tag === 'string') {
  404. const tagObj = knownTags[tag];
  405. if (!tagObj) {
  406. const keys = Object.keys(knownTags).map(key => JSON.stringify(key)).join(', ');
  407. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  408. }
  409. tags[i] = tagObj;
  410. }
  411. }
  412. return tags;
  413. }
  414. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  415. class Schema {
  416. // TODO: remove in v2
  417. // TODO: remove in v2
  418. constructor({
  419. customTags,
  420. merge,
  421. schema,
  422. sortMapEntries,
  423. tags: deprecatedCustomTags
  424. }) {
  425. this.merge = !!merge;
  426. this.name = schema;
  427. this.sortMapEntries = sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
  428. if (!customTags && deprecatedCustomTags) warnings.warnOptionDeprecation('tags', 'customTags');
  429. this.tags = getSchemaTags(schemas, tags, customTags || deprecatedCustomTags, schema);
  430. }
  431. createNode(value, wrapScalars, tagName, ctx) {
  432. const baseCtx = {
  433. defaultPrefix: Schema.defaultPrefix,
  434. schema: this,
  435. wrapScalars
  436. };
  437. const createCtx = ctx ? Object.assign(ctx, baseCtx) : baseCtx;
  438. return createNode(value, tagName, createCtx);
  439. }
  440. createPair(key, value, ctx) {
  441. if (!ctx) ctx = {
  442. wrapScalars: true
  443. };
  444. const k = this.createNode(key, ctx.wrapScalars, null, ctx);
  445. const v = this.createNode(value, ctx.wrapScalars, null, ctx);
  446. return new resolveSeq.Pair(k, v);
  447. }
  448. }
  449. PlainValue._defineProperty(Schema, "defaultPrefix", PlainValue.defaultTagPrefix);
  450. PlainValue._defineProperty(Schema, "defaultTags", PlainValue.defaultTags);
  451. exports.Schema = Schema;