Document.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import { Alias } from '../nodes/Alias.js';
  2. import { isEmptyPath, collectionFromPath } from '../nodes/Collection.js';
  3. import { NODE_TYPE, DOC, isNode, isCollection, isScalar } from '../nodes/identity.js';
  4. import { Pair } from '../nodes/Pair.js';
  5. import { toJS } from '../nodes/toJS.js';
  6. import { Schema } from '../schema/Schema.js';
  7. import { stringifyDocument } from '../stringify/stringifyDocument.js';
  8. import { anchorNames, findNewAnchor, createNodeAnchors } from './anchors.js';
  9. import { applyReviver } from './applyReviver.js';
  10. import { createNode } from './createNode.js';
  11. import { Directives } from './directives.js';
  12. class Document {
  13. constructor(value, replacer, options) {
  14. /** A comment before this Document */
  15. this.commentBefore = null;
  16. /** A comment immediately after this Document */
  17. this.comment = null;
  18. /** Errors encountered during parsing. */
  19. this.errors = [];
  20. /** Warnings encountered during parsing. */
  21. this.warnings = [];
  22. Object.defineProperty(this, NODE_TYPE, { value: DOC });
  23. let _replacer = null;
  24. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  25. _replacer = replacer;
  26. }
  27. else if (options === undefined && replacer) {
  28. options = replacer;
  29. replacer = undefined;
  30. }
  31. const opt = Object.assign({
  32. intAsBigInt: false,
  33. keepSourceTokens: false,
  34. logLevel: 'warn',
  35. prettyErrors: true,
  36. strict: true,
  37. stringKeys: false,
  38. uniqueKeys: true,
  39. version: '1.2'
  40. }, options);
  41. this.options = opt;
  42. let { version } = opt;
  43. if (options?._directives) {
  44. this.directives = options._directives.atDocument();
  45. if (this.directives.yaml.explicit)
  46. version = this.directives.yaml.version;
  47. }
  48. else
  49. this.directives = new Directives({ version });
  50. this.setSchema(version, options);
  51. // @ts-expect-error We can't really know that this matches Contents.
  52. this.contents =
  53. value === undefined ? null : this.createNode(value, _replacer, options);
  54. }
  55. /**
  56. * Create a deep copy of this Document and its contents.
  57. *
  58. * Custom Node values that inherit from `Object` still refer to their original instances.
  59. */
  60. clone() {
  61. const copy = Object.create(Document.prototype, {
  62. [NODE_TYPE]: { value: DOC }
  63. });
  64. copy.commentBefore = this.commentBefore;
  65. copy.comment = this.comment;
  66. copy.errors = this.errors.slice();
  67. copy.warnings = this.warnings.slice();
  68. copy.options = Object.assign({}, this.options);
  69. if (this.directives)
  70. copy.directives = this.directives.clone();
  71. copy.schema = this.schema.clone();
  72. // @ts-expect-error We can't really know that this matches Contents.
  73. copy.contents = isNode(this.contents)
  74. ? this.contents.clone(copy.schema)
  75. : this.contents;
  76. if (this.range)
  77. copy.range = this.range.slice();
  78. return copy;
  79. }
  80. /** Adds a value to the document. */
  81. add(value) {
  82. if (assertCollection(this.contents))
  83. this.contents.add(value);
  84. }
  85. /** Adds a value to the document. */
  86. addIn(path, value) {
  87. if (assertCollection(this.contents))
  88. this.contents.addIn(path, value);
  89. }
  90. /**
  91. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  92. *
  93. * If `node` already has an anchor, `name` is ignored.
  94. * Otherwise, the `node.anchor` value will be set to `name`,
  95. * or if an anchor with that name is already present in the document,
  96. * `name` will be used as a prefix for a new unique anchor.
  97. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  98. */
  99. createAlias(node, name) {
  100. if (!node.anchor) {
  101. const prev = anchorNames(this);
  102. node.anchor =
  103. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  104. !name || prev.has(name) ? findNewAnchor(name || 'a', prev) : name;
  105. }
  106. return new Alias(node.anchor);
  107. }
  108. createNode(value, replacer, options) {
  109. let _replacer = undefined;
  110. if (typeof replacer === 'function') {
  111. value = replacer.call({ '': value }, '', value);
  112. _replacer = replacer;
  113. }
  114. else if (Array.isArray(replacer)) {
  115. const keyToStr = (v) => typeof v === 'number' || v instanceof String || v instanceof Number;
  116. const asStr = replacer.filter(keyToStr).map(String);
  117. if (asStr.length > 0)
  118. replacer = replacer.concat(asStr);
  119. _replacer = replacer;
  120. }
  121. else if (options === undefined && replacer) {
  122. options = replacer;
  123. replacer = undefined;
  124. }
  125. const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
  126. const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this,
  127. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  128. anchorPrefix || 'a');
  129. const ctx = {
  130. aliasDuplicateObjects: aliasDuplicateObjects ?? true,
  131. keepUndefined: keepUndefined ?? false,
  132. onAnchor,
  133. onTagObj,
  134. replacer: _replacer,
  135. schema: this.schema,
  136. sourceObjects
  137. };
  138. const node = createNode(value, tag, ctx);
  139. if (flow && isCollection(node))
  140. node.flow = true;
  141. setAnchors();
  142. return node;
  143. }
  144. /**
  145. * Convert a key and a value into a `Pair` using the current schema,
  146. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  147. */
  148. createPair(key, value, options = {}) {
  149. const k = this.createNode(key, null, options);
  150. const v = this.createNode(value, null, options);
  151. return new Pair(k, v);
  152. }
  153. /**
  154. * Removes a value from the document.
  155. * @returns `true` if the item was found and removed.
  156. */
  157. delete(key) {
  158. return assertCollection(this.contents) ? this.contents.delete(key) : false;
  159. }
  160. /**
  161. * Removes a value from the document.
  162. * @returns `true` if the item was found and removed.
  163. */
  164. deleteIn(path) {
  165. if (isEmptyPath(path)) {
  166. if (this.contents == null)
  167. return false;
  168. // @ts-expect-error Presumed impossible if Strict extends false
  169. this.contents = null;
  170. return true;
  171. }
  172. return assertCollection(this.contents)
  173. ? this.contents.deleteIn(path)
  174. : false;
  175. }
  176. /**
  177. * Returns item at `key`, or `undefined` if not found. By default unwraps
  178. * scalar values from their surrounding node; to disable set `keepScalar` to
  179. * `true` (collections are always returned intact).
  180. */
  181. get(key, keepScalar) {
  182. return isCollection(this.contents)
  183. ? this.contents.get(key, keepScalar)
  184. : undefined;
  185. }
  186. /**
  187. * Returns item at `path`, or `undefined` if not found. By default unwraps
  188. * scalar values from their surrounding node; to disable set `keepScalar` to
  189. * `true` (collections are always returned intact).
  190. */
  191. getIn(path, keepScalar) {
  192. if (isEmptyPath(path))
  193. return !keepScalar && isScalar(this.contents)
  194. ? this.contents.value
  195. : this.contents;
  196. return isCollection(this.contents)
  197. ? this.contents.getIn(path, keepScalar)
  198. : undefined;
  199. }
  200. /**
  201. * Checks if the document includes a value with the key `key`.
  202. */
  203. has(key) {
  204. return isCollection(this.contents) ? this.contents.has(key) : false;
  205. }
  206. /**
  207. * Checks if the document includes a value at `path`.
  208. */
  209. hasIn(path) {
  210. if (isEmptyPath(path))
  211. return this.contents !== undefined;
  212. return isCollection(this.contents) ? this.contents.hasIn(path) : false;
  213. }
  214. /**
  215. * Sets a value in this document. For `!!set`, `value` needs to be a
  216. * boolean to add/remove the item from the set.
  217. */
  218. set(key, value) {
  219. if (this.contents == null) {
  220. // @ts-expect-error We can't really know that this matches Contents.
  221. this.contents = collectionFromPath(this.schema, [key], value);
  222. }
  223. else if (assertCollection(this.contents)) {
  224. this.contents.set(key, value);
  225. }
  226. }
  227. /**
  228. * Sets a value in this document. For `!!set`, `value` needs to be a
  229. * boolean to add/remove the item from the set.
  230. */
  231. setIn(path, value) {
  232. if (isEmptyPath(path)) {
  233. // @ts-expect-error We can't really know that this matches Contents.
  234. this.contents = value;
  235. }
  236. else if (this.contents == null) {
  237. // @ts-expect-error We can't really know that this matches Contents.
  238. this.contents = collectionFromPath(this.schema, Array.from(path), value);
  239. }
  240. else if (assertCollection(this.contents)) {
  241. this.contents.setIn(path, value);
  242. }
  243. }
  244. /**
  245. * Change the YAML version and schema used by the document.
  246. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  247. * It also requires the `schema` option to be given as a `Schema` instance value.
  248. *
  249. * Overrides all previously set schema options.
  250. */
  251. setSchema(version, options = {}) {
  252. if (typeof version === 'number')
  253. version = String(version);
  254. let opt;
  255. switch (version) {
  256. case '1.1':
  257. if (this.directives)
  258. this.directives.yaml.version = '1.1';
  259. else
  260. this.directives = new Directives({ version: '1.1' });
  261. opt = { resolveKnownTags: false, schema: 'yaml-1.1' };
  262. break;
  263. case '1.2':
  264. case 'next':
  265. if (this.directives)
  266. this.directives.yaml.version = version;
  267. else
  268. this.directives = new Directives({ version });
  269. opt = { resolveKnownTags: true, schema: 'core' };
  270. break;
  271. case null:
  272. if (this.directives)
  273. delete this.directives;
  274. opt = null;
  275. break;
  276. default: {
  277. const sv = JSON.stringify(version);
  278. throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
  279. }
  280. }
  281. // Not using `instanceof Schema` to allow for duck typing
  282. if (options.schema instanceof Object)
  283. this.schema = options.schema;
  284. else if (opt)
  285. this.schema = new Schema(Object.assign(opt, options));
  286. else
  287. throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
  288. }
  289. // json & jsonArg are only used from toJSON()
  290. toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  291. const ctx = {
  292. anchors: new Map(),
  293. doc: this,
  294. keep: !json,
  295. mapAsMap: mapAsMap === true,
  296. mapKeyWarned: false,
  297. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  298. };
  299. const res = toJS(this.contents, jsonArg ?? '', ctx);
  300. if (typeof onAnchor === 'function')
  301. for (const { count, res } of ctx.anchors.values())
  302. onAnchor(res, count);
  303. return typeof reviver === 'function'
  304. ? applyReviver(reviver, { '': res }, '', res)
  305. : res;
  306. }
  307. /**
  308. * A JSON representation of the document `contents`.
  309. *
  310. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  311. * property name.
  312. */
  313. toJSON(jsonArg, onAnchor) {
  314. return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor });
  315. }
  316. /** A YAML representation of the document. */
  317. toString(options = {}) {
  318. if (this.errors.length > 0)
  319. throw new Error('Document with errors cannot be stringified');
  320. if ('indent' in options &&
  321. (!Number.isInteger(options.indent) || Number(options.indent) <= 0)) {
  322. const s = JSON.stringify(options.indent);
  323. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  324. }
  325. return stringifyDocument(this, options);
  326. }
  327. }
  328. function assertCollection(contents) {
  329. if (isCollection(contents))
  330. return true;
  331. throw new Error('Expected a YAML collection as document contents');
  332. }
  333. export { Document };