@apidevtools/json-schema-ref-parser 10.0.1 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +22 -0
  2. package/dist/lib/bundle.d.ts +10 -0
  3. package/dist/lib/bundle.js +266 -0
  4. package/dist/lib/dereference.d.ts +9 -0
  5. package/dist/lib/dereference.js +206 -0
  6. package/dist/lib/index.d.ts +206 -0
  7. package/dist/lib/index.js +206 -0
  8. package/dist/lib/normalize-args.d.ts +10 -0
  9. package/dist/lib/normalize-args.js +42 -0
  10. package/dist/lib/options.d.ts +77 -0
  11. package/dist/lib/options.js +119 -0
  12. package/dist/lib/parse.d.ts +8 -0
  13. package/dist/lib/parse.js +162 -0
  14. package/dist/lib/parsers/binary.d.ts +3 -0
  15. package/dist/lib/parsers/binary.js +35 -0
  16. package/dist/lib/parsers/json.d.ts +3 -0
  17. package/dist/lib/parsers/json.js +46 -0
  18. package/dist/lib/parsers/text.d.ts +3 -0
  19. package/dist/lib/parsers/text.js +42 -0
  20. package/dist/lib/parsers/yaml.d.ts +3 -0
  21. package/dist/lib/parsers/yaml.js +54 -0
  22. package/dist/lib/pointer.d.ts +87 -0
  23. package/dist/lib/pointer.js +256 -0
  24. package/dist/lib/ref.d.ts +180 -0
  25. package/dist/lib/ref.js +238 -0
  26. package/dist/lib/refs.d.ts +127 -0
  27. package/dist/lib/refs.js +222 -0
  28. package/dist/lib/resolve-external.d.ts +14 -0
  29. package/dist/lib/resolve-external.js +133 -0
  30. package/dist/lib/resolvers/file.d.ts +3 -0
  31. package/dist/lib/resolvers/file.js +62 -0
  32. package/dist/lib/resolvers/http.d.ts +3 -0
  33. package/dist/lib/resolvers/http.js +146 -0
  34. package/dist/lib/types/index.d.ts +104 -0
  35. package/dist/lib/types/index.js +2 -0
  36. package/dist/lib/util/convert-path-to-posix.d.ts +1 -0
  37. package/dist/lib/util/convert-path-to-posix.js +14 -0
  38. package/dist/lib/util/errors.d.ts +50 -0
  39. package/dist/lib/util/errors.js +106 -0
  40. package/dist/lib/util/is-windows.d.ts +1 -0
  41. package/dist/lib/util/is-windows.js +6 -0
  42. package/dist/lib/util/maybe.d.ts +3 -0
  43. package/dist/lib/util/maybe.js +24 -0
  44. package/dist/lib/util/next.d.ts +2 -0
  45. package/dist/lib/util/next.js +16 -0
  46. package/dist/lib/util/plugins.d.ts +36 -0
  47. package/dist/lib/util/plugins.js +133 -0
  48. package/dist/lib/util/url.d.ts +94 -0
  49. package/dist/lib/util/url.js +304 -0
  50. package/dist/vite.config.d.ts +2 -0
  51. package/dist/vite.config.js +19 -0
  52. package/lib/{bundle.js → bundle.ts} +106 -101
  53. package/lib/{dereference.js → dereference.ts} +113 -52
  54. package/lib/index.ts +413 -0
  55. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  56. package/lib/options.ts +202 -0
  57. package/lib/parse.ts +153 -0
  58. package/lib/parsers/binary.ts +39 -0
  59. package/lib/parsers/{json.js → json.ts} +9 -22
  60. package/lib/parsers/text.ts +46 -0
  61. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  62. package/lib/pointer.ts +296 -0
  63. package/lib/ref.ts +287 -0
  64. package/lib/refs.ts +236 -0
  65. package/lib/{resolve-external.js → resolve-external.ts} +44 -41
  66. package/lib/resolvers/file.ts +40 -0
  67. package/lib/resolvers/http.ts +136 -0
  68. package/lib/tsconfig.json +103 -0
  69. package/lib/types/index.ts +135 -0
  70. package/lib/util/convert-path-to-posix.ts +11 -0
  71. package/lib/util/errors.ts +141 -0
  72. package/lib/util/is-windows.ts +2 -0
  73. package/lib/util/maybe.ts +22 -0
  74. package/lib/util/next.ts +13 -0
  75. package/lib/util/{plugins.js → plugins.ts} +58 -57
  76. package/lib/util/{url.js → url.ts} +92 -91
  77. package/package.json +44 -46
  78. package/cjs/bundle.js +0 -304
  79. package/cjs/dereference.js +0 -258
  80. package/cjs/index.js +0 -603
  81. package/cjs/normalize-args.js +0 -64
  82. package/cjs/options.js +0 -125
  83. package/cjs/package.json +0 -3
  84. package/cjs/parse.js +0 -338
  85. package/cjs/parsers/binary.js +0 -54
  86. package/cjs/parsers/json.js +0 -199
  87. package/cjs/parsers/text.js +0 -61
  88. package/cjs/parsers/yaml.js +0 -239
  89. package/cjs/pointer.js +0 -290
  90. package/cjs/ref.js +0 -333
  91. package/cjs/refs.js +0 -214
  92. package/cjs/resolve-external.js +0 -333
  93. package/cjs/resolvers/file.js +0 -106
  94. package/cjs/resolvers/http.js +0 -184
  95. package/cjs/util/errors.js +0 -401
  96. package/cjs/util/plugins.js +0 -159
  97. package/cjs/util/projectDir.cjs +0 -6
  98. package/cjs/util/url.js +0 -228
  99. package/lib/index.d.ts +0 -496
  100. package/lib/index.js +0 -290
  101. package/lib/options.js +0 -128
  102. package/lib/parse.js +0 -162
  103. package/lib/parsers/binary.js +0 -53
  104. package/lib/parsers/text.js +0 -64
  105. package/lib/pointer.js +0 -293
  106. package/lib/ref.js +0 -292
  107. package/lib/refs.js +0 -196
  108. package/lib/resolvers/file.js +0 -63
  109. package/lib/resolvers/http.js +0 -155
  110. package/lib/util/errors.js +0 -134
  111. package/lib/util/projectDir.cjs +0 -6
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ import type $Refs from "./refs.js";
3
+ import type { Options } from "./options.js";
4
+ export default parse;
5
+ /**
6
+ * Reads and parses the specified file path or URL.
7
+ */
8
+ declare function parse(path: string, $refs: $Refs, options: Options): Promise<string | import("./types/index.js").JSONSchema | Buffer | undefined>;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const ono_1 = require("@jsdevtools/ono");
27
+ const url = __importStar(require("./util/url.js"));
28
+ const plugins = __importStar(require("./util/plugins.js"));
29
+ const errors_js_1 = require("./util/errors.js");
30
+ exports.default = parse;
31
+ /**
32
+ * Reads and parses the specified file path or URL.
33
+ */
34
+ async function parse(path, $refs, options) {
35
+ // Remove the URL fragment, if any
36
+ path = url.stripHash(path);
37
+ // Add a new $Ref for this file, even though we don't have the value yet.
38
+ // This ensures that we don't simultaneously read & parse the same file multiple times
39
+ const $ref = $refs._add(path);
40
+ // This "file object" will be passed to all resolvers and parsers.
41
+ const file = {
42
+ url: path,
43
+ extension: url.getExtension(path),
44
+ };
45
+ // Read the file and then parse the data
46
+ try {
47
+ const resolver = await readFile(file, options, $refs);
48
+ $ref.pathType = resolver.plugin.name;
49
+ file.data = resolver.result;
50
+ const parser = await parseFile(file, options, $refs);
51
+ $ref.value = parser.result;
52
+ return parser.result;
53
+ }
54
+ catch (err) {
55
+ if ((0, errors_js_1.isHandledError)(err)) {
56
+ $ref.value = err;
57
+ }
58
+ throw err;
59
+ }
60
+ }
61
+ /**
62
+ * Reads the given file, using the configured resolver plugins
63
+ *
64
+ * @param file - An object containing information about the referenced file
65
+ * @param file.url - The full URL of the referenced file
66
+ * @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
67
+ * @param options
68
+ *
69
+ * @returns
70
+ * The promise resolves with the raw file contents and the resolver that was used.
71
+ */
72
+ async function readFile(file, options, $refs) {
73
+ // console.log('Reading %s', file.url);
74
+ // Find the resolvers that can read this file
75
+ let resolvers = plugins.all(options.resolve);
76
+ resolvers = plugins.filter(resolvers, "canRead", file);
77
+ // Run the resolvers, in order, until one of them succeeds
78
+ plugins.sort(resolvers);
79
+ try {
80
+ const data = await plugins.run(resolvers, "read", file, $refs);
81
+ return data;
82
+ }
83
+ catch (err) {
84
+ if (!err && options.continueOnError) {
85
+ // No resolver could be matched
86
+ throw new errors_js_1.UnmatchedResolverError(file.url);
87
+ }
88
+ else if (!err || !("error" in err)) {
89
+ // Throw a generic, friendly error.
90
+ throw ono_1.ono.syntax(`Unable to resolve $ref pointer "${file.url}"`);
91
+ }
92
+ // Throw the original error, if it's one of our own (user-friendly) errors.
93
+ else if (err.error instanceof errors_js_1.ResolverError) {
94
+ throw err.error;
95
+ }
96
+ else {
97
+ throw new errors_js_1.ResolverError(err, file.url);
98
+ }
99
+ }
100
+ }
101
+ /**
102
+ * Parses the given file's contents, using the configured parser plugins.
103
+ *
104
+ * @param file - An object containing information about the referenced file
105
+ * @param file.url - The full URL of the referenced file
106
+ * @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
107
+ * @param file.data - The file contents. This will be whatever data type was returned by the resolver
108
+ * @param options
109
+ *
110
+ * @returns
111
+ * The promise resolves with the parsed file contents and the parser that was used.
112
+ */
113
+ async function parseFile(file, options, $refs) {
114
+ // console.log('Parsing %s', file.url);
115
+ // Find the parsers that can read this file type.
116
+ // If none of the parsers are an exact match for this file, then we'll try ALL of them.
117
+ // This handles situations where the file IS a supported type, just with an unknown extension.
118
+ const allParsers = plugins.all(options.parse);
119
+ const filteredParsers = plugins.filter(allParsers, "canParse", file);
120
+ const parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
121
+ // Run the parsers, in order, until one of them succeeds
122
+ plugins.sort(parsers);
123
+ try {
124
+ const parser = await plugins.run(parsers, "parse", file, $refs);
125
+ if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
126
+ throw ono_1.ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
127
+ }
128
+ else {
129
+ return parser;
130
+ }
131
+ }
132
+ catch (err) {
133
+ if (!err && options.continueOnError) {
134
+ // No resolver could be matched
135
+ throw new errors_js_1.UnmatchedParserError(file.url);
136
+ }
137
+ else if (err && err.message && err.message.startsWith("Error parsing")) {
138
+ throw err;
139
+ }
140
+ else if (!err || !("error" in err)) {
141
+ throw ono_1.ono.syntax(`Unable to parse ${file.url}`);
142
+ }
143
+ else if (err.error instanceof errors_js_1.ParserError) {
144
+ throw err.error;
145
+ }
146
+ else {
147
+ throw new errors_js_1.ParserError(err.error.message, file.url);
148
+ }
149
+ }
150
+ }
151
+ /**
152
+ * Determines whether the parsed value is "empty".
153
+ *
154
+ * @param value
155
+ * @returns
156
+ */
157
+ function isEmpty(value) {
158
+ return (value === undefined ||
159
+ (typeof value === "object" && Object.keys(value).length === 0) ||
160
+ (typeof value === "string" && value.trim().length === 0) ||
161
+ (Buffer.isBuffer(value) && value.length === 0));
162
+ }
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "../types/index.js";
2
+ declare const _default: Plugin;
3
+ export default _default;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
4
+ exports.default = {
5
+ /**
6
+ * The order that this parser will run, in relation to other parsers.
7
+ */
8
+ order: 400,
9
+ /**
10
+ * Whether to allow "empty" files (zero bytes).
11
+ */
12
+ allowEmpty: true,
13
+ /**
14
+ * Determines whether this parser can parse a given file reference.
15
+ * Parsers that return true will be tried, in order, until one successfully parses the file.
16
+ * Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
17
+ * every parser will be tried.
18
+ */
19
+ canParse(file) {
20
+ // Use this parser if the file is a Buffer, and has a known binary extension
21
+ return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url);
22
+ },
23
+ /**
24
+ * Parses the given data as a Buffer (byte array).
25
+ */
26
+ parse(file) {
27
+ if (Buffer.isBuffer(file.data)) {
28
+ return file.data;
29
+ }
30
+ else {
31
+ // This will reject if data is anything other than a string or typed array
32
+ return Buffer.from(file.data);
33
+ }
34
+ },
35
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "../types/index.js";
2
+ declare const _default: Plugin;
3
+ export default _default;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const errors_js_1 = require("../util/errors.js");
4
+ exports.default = {
5
+ /**
6
+ * The order that this parser will run, in relation to other parsers.
7
+ */
8
+ order: 100,
9
+ /**
10
+ * Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
11
+ */
12
+ allowEmpty: true,
13
+ /**
14
+ * Determines whether this parser can parse a given file reference.
15
+ * Parsers that match will be tried, in order, until one successfully parses the file.
16
+ * Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
17
+ * every parser will be tried.
18
+ */
19
+ canParse: ".json",
20
+ /**
21
+ * Parses the given file as JSON
22
+ */
23
+ async parse(file) {
24
+ let data = file.data;
25
+ if (Buffer.isBuffer(data)) {
26
+ data = data.toString();
27
+ }
28
+ if (typeof data === "string") {
29
+ if (data.trim().length === 0) {
30
+ return; // This mirrors the YAML behavior
31
+ }
32
+ else {
33
+ try {
34
+ return JSON.parse(data);
35
+ }
36
+ catch (e) {
37
+ throw new errors_js_1.ParserError(e.message, file.url);
38
+ }
39
+ }
40
+ }
41
+ else {
42
+ // data is already a JavaScript value (object, array, number, null, NaN, etc.)
43
+ return data;
44
+ }
45
+ },
46
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "../types/index.js";
2
+ declare const _default: Plugin;
3
+ export default _default;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const errors_js_1 = require("../util/errors.js");
4
+ const TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
5
+ exports.default = {
6
+ /**
7
+ * The order that this parser will run, in relation to other parsers.
8
+ */
9
+ order: 300,
10
+ /**
11
+ * Whether to allow "empty" files (zero bytes).
12
+ */
13
+ allowEmpty: true,
14
+ /**
15
+ * The encoding that the text is expected to be in.
16
+ */
17
+ encoding: "utf8",
18
+ /**
19
+ * Determines whether this parser can parse a given file reference.
20
+ * Parsers that return true will be tried, in order, until one successfully parses the file.
21
+ * Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
22
+ * every parser will be tried.
23
+ */
24
+ canParse(file) {
25
+ // Use this parser if the file is a string or Buffer, and has a known text-based extension
26
+ return (typeof file.data === "string" || Buffer.isBuffer(file.data)) && TEXT_REGEXP.test(file.url);
27
+ },
28
+ /**
29
+ * Parses the given file as text
30
+ */
31
+ parse(file) {
32
+ if (typeof file.data === "string") {
33
+ return file.data;
34
+ }
35
+ else if (Buffer.isBuffer(file.data)) {
36
+ return file.data.toString(this.encoding);
37
+ }
38
+ else {
39
+ throw new errors_js_1.ParserError("data is not text", file.url);
40
+ }
41
+ },
42
+ };
@@ -0,0 +1,3 @@
1
+ import type { Plugin } from "../types/index.js";
2
+ declare const _default: Plugin;
3
+ export default _default;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const errors_js_1 = require("../util/errors.js");
7
+ const js_yaml_1 = __importDefault(require("js-yaml"));
8
+ const js_yaml_2 = require("js-yaml");
9
+ exports.default = {
10
+ /**
11
+ * The order that this parser will run, in relation to other parsers.
12
+ */
13
+ order: 200,
14
+ /**
15
+ * Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
16
+ */
17
+ allowEmpty: true,
18
+ /**
19
+ * Determines whether this parser can parse a given file reference.
20
+ * Parsers that match will be tried, in order, until one successfully parses the file.
21
+ * Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
22
+ * every parser will be tried.
23
+ */
24
+ canParse: [".yaml", ".yml", ".json"],
25
+ /**
26
+ * Parses the given file as YAML
27
+ *
28
+ * @param file - An object containing information about the referenced file
29
+ * @param file.url - The full URL of the referenced file
30
+ * @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
31
+ * @param file.data - The file contents. This will be whatever data type was returned by the resolver
32
+ * @returns
33
+ */
34
+ async parse(file) {
35
+ // eslint-disable-line require-await
36
+ let data = file.data;
37
+ if (Buffer.isBuffer(data)) {
38
+ data = data.toString();
39
+ }
40
+ if (typeof data === "string") {
41
+ try {
42
+ return js_yaml_1.default.load(data, { schema: js_yaml_2.JSON_SCHEMA });
43
+ }
44
+ catch (e) {
45
+ // @ts-expect-error TS(2571): Object is of type 'unknown'.
46
+ throw new errors_js_1.ParserError(e.message, file.url);
47
+ }
48
+ }
49
+ else {
50
+ // data is already a JavaScript value (object, array, number, null, NaN, etc.)
51
+ return data;
52
+ }
53
+ },
54
+ };
@@ -0,0 +1,87 @@
1
+ import type $RefParserOptions from "./options.js";
2
+ import $Ref from "./ref.js";
3
+ /**
4
+ * This class represents a single JSON pointer and its resolved value.
5
+ *
6
+ * @param $ref
7
+ * @param path
8
+ * @param [friendlyPath] - The original user-specified path (used for error messages)
9
+ * @class
10
+ */
11
+ declare class Pointer {
12
+ /**
13
+ * The {@link $Ref} object that contains this {@link Pointer} object.
14
+ */
15
+ $ref: $Ref;
16
+ /**
17
+ * The file path or URL, containing the JSON pointer in the hash.
18
+ * This path is relative to the path of the main JSON schema file.
19
+ */
20
+ path: string;
21
+ /**
22
+ * The original path or URL, used for error messages.
23
+ */
24
+ originalPath: string;
25
+ /**
26
+ * The value of the JSON pointer.
27
+ * Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
28
+ */
29
+ value: any;
30
+ /**
31
+ * Indicates whether the pointer references itself.
32
+ */
33
+ circular: boolean;
34
+ /**
35
+ * The number of indirect references that were traversed to resolve the value.
36
+ * Resolving a single pointer may require resolving multiple $Refs.
37
+ */
38
+ indirections: number;
39
+ constructor($ref: any, path: any, friendlyPath: any);
40
+ /**
41
+ * Resolves the value of a nested property within the given object.
42
+ *
43
+ * @param obj - The object that will be crawled
44
+ * @param options
45
+ * @param pathFromRoot - the path of place that initiated resolving
46
+ *
47
+ * @returns
48
+ * Returns a JSON pointer whose {@link Pointer#value} is the resolved value.
49
+ * If resolving this value required resolving other JSON references, then
50
+ * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
51
+ * of the resolved value.
52
+ */
53
+ resolve(obj: any, options: any, pathFromRoot: any): this;
54
+ /**
55
+ * Sets the value of a nested property within the given object.
56
+ *
57
+ * @param obj - The object that will be crawled
58
+ * @param value - the value to assign
59
+ * @param options
60
+ *
61
+ * @returns
62
+ * Returns the modified object, or an entirely new object if the entire object is overwritten.
63
+ */
64
+ set(obj: any, value: any, options?: $RefParserOptions): any;
65
+ /**
66
+ * Parses a JSON pointer (or a path containing a JSON pointer in the hash)
67
+ * and returns an array of the pointer's tokens.
68
+ * (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
69
+ *
70
+ * The pointer is parsed according to RFC 6901
71
+ * {@link https://tools.ietf.org/html/rfc6901#section-3}
72
+ *
73
+ * @param path
74
+ * @param [originalPath]
75
+ * @returns
76
+ */
77
+ static parse(path: string, originalPath?: string): any;
78
+ /**
79
+ * Creates a JSON pointer path, by joining one or more tokens to a base path.
80
+ *
81
+ * @param base - The base path (e.g. "schema.json#/definitions/person")
82
+ * @param tokens - The token(s) to append (e.g. ["name", "first"])
83
+ * @returns
84
+ */
85
+ static join(base: any, tokens: any): any;
86
+ }
87
+ export default Pointer;