Debian Codegraph @

debian/tests/test_modules/@apidevtools/json-schema-ref-parser/lib/parsers/yaml.js

raw
1 "use strict";
2
3 const { ParserError } = require("../util/errors");
4 const yaml = require("js-yaml");
5
6 module.exports = {
7 /**
8 * The order that this parser will run, in relation to other parsers.
9 *
10 * @type {number}
11 */
12 order: 200,
13
14 /**
15 * Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
16 *
17 * @type {boolean}
18 */
19 allowEmpty: true,
20
21 /**
22 * Determines whether this parser can parse a given file reference.
23 * Parsers that match will be tried, in order, until one successfully parses the file.
24 * Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
25 * every parser will be tried.
26 *
27 * @type {RegExp|string[]|function}
28 */
29 canParse: [".yaml", ".yml", ".json"], // JSON is valid YAML
30
31 /**
32 * Parses the given file as YAML
33 *
34 * @param {object} file - An object containing information about the referenced file
35 * @param {string} file.url - The full URL of the referenced file
36 * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
37 * @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
38 * @returns {Promise}
39 */
40 async parse (file) { // eslint-disable-line require-await
41 let data = file.data;
42 if (Buffer.isBuffer(data)) {
43 data = data.toString();
44 }
45
46 if (typeof data === "string") {
47 try {
48 return yaml.load(data);
49 }
50 catch (e) {
51 throw new ParserError(e.message, file.url);
52 }
53 }
54 else {
55 // data is already a JavaScript value (object, array, number, null, NaN, etc.)
56 return data;
57 }
58 }
59 };