@apidevtools/json-schema-ref-parser 10.0.0 → 10.1.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 (103) hide show
  1. package/dist/lib/bundle.d.ts +10 -0
  2. package/dist/lib/bundle.js +265 -0
  3. package/dist/lib/dereference.d.ts +9 -0
  4. package/dist/lib/dereference.js +206 -0
  5. package/dist/lib/index.d.ts +206 -0
  6. package/dist/lib/index.js +223 -0
  7. package/dist/lib/normalize-args.d.ts +10 -0
  8. package/dist/lib/normalize-args.js +42 -0
  9. package/dist/lib/options.d.ts +77 -0
  10. package/dist/lib/options.js +119 -0
  11. package/dist/lib/parse.d.ts +8 -0
  12. package/dist/lib/parse.js +177 -0
  13. package/dist/lib/parsers/binary.d.ts +3 -0
  14. package/dist/lib/parsers/binary.js +35 -0
  15. package/dist/lib/parsers/json.d.ts +3 -0
  16. package/dist/lib/parsers/json.js +57 -0
  17. package/dist/lib/parsers/text.d.ts +3 -0
  18. package/dist/lib/parsers/text.js +42 -0
  19. package/dist/lib/parsers/yaml.d.ts +3 -0
  20. package/dist/lib/parsers/yaml.js +65 -0
  21. package/dist/lib/pointer.d.ts +87 -0
  22. package/dist/lib/pointer.js +256 -0
  23. package/dist/lib/ref.d.ts +181 -0
  24. package/dist/lib/ref.js +239 -0
  25. package/dist/lib/refs.d.ts +127 -0
  26. package/dist/lib/refs.js +223 -0
  27. package/dist/lib/resolve-external.d.ts +14 -0
  28. package/dist/lib/resolve-external.js +148 -0
  29. package/dist/lib/resolvers/file.d.ts +3 -0
  30. package/dist/lib/resolvers/file.js +76 -0
  31. package/dist/lib/resolvers/http.d.ts +3 -0
  32. package/dist/lib/resolvers/http.js +159 -0
  33. package/dist/lib/types/index.d.ts +104 -0
  34. package/dist/lib/types/index.js +2 -0
  35. package/dist/lib/util/errors.d.ts +50 -0
  36. package/dist/lib/util/errors.js +106 -0
  37. package/dist/lib/util/maybe.d.ts +3 -0
  38. package/dist/lib/util/maybe.js +24 -0
  39. package/dist/lib/util/next.d.ts +2 -0
  40. package/dist/lib/util/next.js +16 -0
  41. package/dist/lib/util/plugins.d.ts +36 -0
  42. package/dist/lib/util/plugins.js +144 -0
  43. package/dist/lib/util/url.d.ts +93 -0
  44. package/{cjs → dist/lib}/util/url.js +134 -102
  45. package/dist/vite.config.d.ts +2 -0
  46. package/dist/vite.config.js +23 -0
  47. package/lib/{bundle.js → bundle.ts} +105 -101
  48. package/lib/{dereference.js → dereference.ts} +113 -52
  49. package/lib/index.ts +413 -0
  50. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  51. package/lib/options.ts +202 -0
  52. package/lib/parse.ts +153 -0
  53. package/lib/parsers/binary.ts +39 -0
  54. package/lib/parsers/{json.js → json.ts} +9 -22
  55. package/lib/parsers/text.ts +46 -0
  56. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  57. package/lib/pointer.ts +296 -0
  58. package/lib/ref.ts +288 -0
  59. package/lib/refs.ts +238 -0
  60. package/lib/{resolve-external.js → resolve-external.ts} +39 -36
  61. package/lib/resolvers/file.ts +40 -0
  62. package/lib/resolvers/http.ts +136 -0
  63. package/lib/tsconfig.json +103 -0
  64. package/lib/types/index.ts +135 -0
  65. package/lib/util/errors.ts +141 -0
  66. package/lib/util/maybe.ts +22 -0
  67. package/lib/util/next.ts +13 -0
  68. package/lib/util/{plugins.js → plugins.ts} +58 -57
  69. package/lib/util/{url.js → url.ts} +64 -83
  70. package/package.json +44 -45
  71. package/cjs/bundle.js +0 -304
  72. package/cjs/dereference.js +0 -258
  73. package/cjs/index.js +0 -603
  74. package/cjs/normalize-args.js +0 -64
  75. package/cjs/options.js +0 -125
  76. package/cjs/package.json +0 -3
  77. package/cjs/parse.js +0 -338
  78. package/cjs/parsers/binary.js +0 -54
  79. package/cjs/parsers/json.js +0 -199
  80. package/cjs/parsers/text.js +0 -61
  81. package/cjs/parsers/yaml.js +0 -239
  82. package/cjs/pointer.js +0 -290
  83. package/cjs/ref.js +0 -333
  84. package/cjs/refs.js +0 -214
  85. package/cjs/resolve-external.js +0 -333
  86. package/cjs/resolvers/file.js +0 -106
  87. package/cjs/resolvers/http.js +0 -184
  88. package/cjs/util/errors.js +0 -401
  89. package/cjs/util/plugins.js +0 -159
  90. package/cjs/util/projectDir.cjs +0 -6
  91. package/lib/index.d.ts +0 -496
  92. package/lib/index.js +0 -290
  93. package/lib/options.js +0 -128
  94. package/lib/parse.js +0 -162
  95. package/lib/parsers/binary.js +0 -53
  96. package/lib/parsers/text.js +0 -64
  97. package/lib/pointer.js +0 -293
  98. package/lib/ref.js +0 -292
  99. package/lib/refs.js +0 -196
  100. package/lib/resolvers/file.js +0 -63
  101. package/lib/resolvers/http.js +0 -155
  102. package/lib/util/errors.js +0 -134
  103. package/lib/util/projectDir.cjs +0 -6
package/lib/refs.js DELETED
@@ -1,196 +0,0 @@
1
- import { ono } from "@jsdevtools/ono";
2
- import $Ref from "./ref.js";
3
- import * as url from "./util/url.js";
4
-
5
- const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
6
- const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
7
-
8
- /**
9
- * This class is a map of JSON references and their resolved values.
10
- */
11
- export default function $Refs () {
12
- /**
13
- * Indicates whether the schema contains any circular references.
14
- *
15
- * @type {boolean}
16
- */
17
- this.circular = false;
18
-
19
- /**
20
- * A map of paths/urls to {@link $Ref} objects
21
- *
22
- * @type {object}
23
- * @protected
24
- */
25
- this._$refs = {};
26
-
27
- /**
28
- * The {@link $Ref} object that is the root of the JSON schema.
29
- *
30
- * @type {$Ref}
31
- * @protected
32
- */
33
- this._root$Ref = null;
34
- }
35
-
36
- /**
37
- * Returns the paths of all the files/URLs that are referenced by the JSON schema,
38
- * including the schema itself.
39
- *
40
- * @param {...string|string[]} [types] - Only return paths of the given types ("file", "http", etc.)
41
- * @returns {string[]}
42
- */
43
- $Refs.prototype.paths = function (types) { // eslint-disable-line no-unused-vars
44
- let paths = getPaths(this._$refs, arguments);
45
- return paths.map((path) => {
46
- return getPathFromOs(path.decoded);
47
- });
48
- };
49
-
50
- /**
51
- * Returns the map of JSON references and their resolved values.
52
- *
53
- * @param {...string|string[]} [types] - Only return references of the given types ("file", "http", etc.)
54
- * @returns {object}
55
- */
56
- $Refs.prototype.values = function (types) { // eslint-disable-line no-unused-vars
57
- let $refs = this._$refs;
58
- let paths = getPaths($refs, arguments);
59
- return paths.reduce((obj, path) => {
60
- obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
61
- return obj;
62
- }, {});
63
- };
64
-
65
- /**
66
- * Returns a POJO (plain old JavaScript object) for serialization as JSON.
67
- *
68
- * @returns {object}
69
- */
70
- $Refs.prototype.toJSON = $Refs.prototype.values;
71
-
72
- /**
73
- * Determines whether the given JSON reference exists.
74
- *
75
- * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
76
- * @param {$RefParserOptions} [options]
77
- * @returns {boolean}
78
- */
79
- $Refs.prototype.exists = function (path, options) {
80
- try {
81
- this._resolve(path, "", options);
82
- return true;
83
- }
84
- catch (e) {
85
- return false;
86
- }
87
- };
88
-
89
- /**
90
- * Resolves the given JSON reference and returns the resolved value.
91
- *
92
- * @param {string} path - The path being resolved, with a JSON pointer in the hash
93
- * @param {$RefParserOptions} [options]
94
- * @returns {*} - Returns the resolved value
95
- */
96
- $Refs.prototype.get = function (path, options) {
97
- return this._resolve(path, "", options).value;
98
- };
99
-
100
- /**
101
- * Sets the value of a nested property within this {@link $Ref#value}.
102
- * If the property, or any of its parents don't exist, they will be created.
103
- *
104
- * @param {string} path - The path of the property to set, optionally with a JSON pointer in the hash
105
- * @param {*} value - The value to assign
106
- */
107
- $Refs.prototype.set = function (path, value) {
108
- let absPath = url.resolve(this._root$Ref.path, path);
109
- let withoutHash = url.stripHash(absPath);
110
- let $ref = this._$refs[withoutHash];
111
-
112
- if (!$ref) {
113
- throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
114
- }
115
-
116
- $ref.set(absPath, value);
117
- };
118
-
119
- /**
120
- * Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
121
- *
122
- * @param {string} path - The file path or URL of the referenced file
123
- */
124
- $Refs.prototype._add = function (path) {
125
- let withoutHash = url.stripHash(path);
126
-
127
- let $ref = new $Ref();
128
- $ref.path = withoutHash;
129
- $ref.$refs = this;
130
-
131
- this._$refs[withoutHash] = $ref;
132
- this._root$Ref = this._root$Ref || $ref;
133
-
134
- return $ref;
135
- };
136
-
137
- /**
138
- * Resolves the given JSON reference.
139
- *
140
- * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
141
- * @param {string} pathFromRoot - The path of `obj` from the schema root
142
- * @param {$RefParserOptions} [options]
143
- * @returns {Pointer}
144
- * @protected
145
- */
146
- $Refs.prototype._resolve = function (path, pathFromRoot, options) {
147
- let absPath = url.resolve(this._root$Ref.path, path);
148
- let withoutHash = url.stripHash(absPath);
149
- let $ref = this._$refs[withoutHash];
150
-
151
- if (!$ref) {
152
- throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
153
- }
154
-
155
- return $ref.resolve(absPath, options, path, pathFromRoot);
156
- };
157
-
158
- /**
159
- * Returns the specified {@link $Ref} object, or undefined.
160
- *
161
- * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
162
- * @returns {$Ref|undefined}
163
- * @protected
164
- */
165
- $Refs.prototype._get$Ref = function (path) {
166
- path = url.resolve(this._root$Ref.path, path);
167
- let withoutHash = url.stripHash(path);
168
- return this._$refs[withoutHash];
169
- };
170
-
171
- /**
172
- * Returns the encoded and decoded paths keys of the given object.
173
- *
174
- * @param {object} $refs - The object whose keys are URL-encoded paths
175
- * @param {...string|string[]} [types] - Only return paths of the given types ("file", "http", etc.)
176
- * @returns {object[]}
177
- */
178
- function getPaths ($refs, types) {
179
- let paths = Object.keys($refs);
180
-
181
- // Filter the paths by type
182
- types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
183
- if (types.length > 0 && types[0]) {
184
- paths = paths.filter((key) => {
185
- return types.indexOf($refs[key].pathType) !== -1;
186
- });
187
- }
188
-
189
- // Decode local filesystem paths
190
- return paths.map((path) => {
191
- return {
192
- encoded: path,
193
- decoded: $refs[path].pathType === "file" ? url.toFileSystemPath(path, true) : path
194
- };
195
- });
196
- }
@@ -1,63 +0,0 @@
1
- import fs from "fs";
2
- import { ono } from "@jsdevtools/ono";
3
- import * as url from "../util/url.js";
4
- import { ResolverError } from "../util/errors.js";
5
-
6
- export default {
7
- /**
8
- * The order that this resolver will run, in relation to other resolvers.
9
- *
10
- * @type {number}
11
- */
12
- order: 100,
13
-
14
- /**
15
- * Determines whether this resolver can read a given file reference.
16
- * Resolvers that return true will be tried, in order, until one successfully resolves the file.
17
- * Resolvers that return false will not be given a chance to resolve the file.
18
- *
19
- * @param {object} file - An object containing information about the referenced file
20
- * @param {string} file.url - The full URL of the referenced file
21
- * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
22
- * @returns {boolean}
23
- */
24
- canRead (file) {
25
- return url.isFileSystemPath(file.url);
26
- },
27
-
28
- /**
29
- * Reads the given file and returns its raw contents as a Buffer.
30
- *
31
- * @param {object} file - An object containing information about the referenced file
32
- * @param {string} file.url - The full URL of the referenced file
33
- * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
34
- * @returns {Promise<Buffer>}
35
- */
36
- read (file) {
37
- return new Promise(((resolve, reject) => {
38
- let path;
39
- try {
40
- path = url.toFileSystemPath(file.url);
41
- }
42
- catch (err) {
43
- reject(new ResolverError(ono.uri(err, `Malformed URI: ${file.url}`), file.url));
44
- }
45
-
46
- // console.log('Opening file: %s', path);
47
-
48
- try {
49
- fs.readFile(path, (err, data) => {
50
- if (err) {
51
- reject(new ResolverError(ono(err, `Error opening file "${path}"`), path));
52
- }
53
- else {
54
- resolve(data);
55
- }
56
- });
57
- }
58
- catch (err) {
59
- reject(new ResolverError(ono(err, `Error opening file "${path}"`), path));
60
- }
61
- }));
62
- }
63
- };
@@ -1,155 +0,0 @@
1
- import { ono } from "@jsdevtools/ono";
2
- import * as url from "../util/url.js";
3
- import { ResolverError } from "../util/errors.js";
4
-
5
- export default {
6
- /**
7
- * The order that this resolver will run, in relation to other resolvers.
8
- *
9
- * @type {number}
10
- */
11
- order: 200,
12
-
13
- /**
14
- * HTTP headers to send when downloading files.
15
- *
16
- * @example:
17
- * {
18
- * "User-Agent": "JSON Schema $Ref Parser",
19
- * Accept: "application/json"
20
- * }
21
- *
22
- * @type {object}
23
- */
24
- headers: null,
25
-
26
- /**
27
- * HTTP request timeout (in milliseconds).
28
- *
29
- * @type {number}
30
- */
31
- timeout: 5000, // 5 seconds
32
-
33
- /**
34
- * The maximum number of HTTP redirects to follow.
35
- * To disable automatic following of redirects, set this to zero.
36
- *
37
- * @type {number}
38
- */
39
- redirects: 5,
40
-
41
- /**
42
- * The `withCredentials` option of XMLHttpRequest.
43
- * Set this to `true` if you're downloading files from a CORS-enabled server that requires authentication
44
- *
45
- * @type {boolean}
46
- */
47
- withCredentials: false,
48
-
49
- /**
50
- * Determines whether this resolver can read a given file reference.
51
- * Resolvers that return true will be tried in order, until one successfully resolves the file.
52
- * Resolvers that return false will not be given a chance to resolve the file.
53
- *
54
- * @param {object} file - An object containing information about the referenced file
55
- * @param {string} file.url - The full URL of the referenced file
56
- * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
57
- * @returns {boolean}
58
- */
59
- canRead (file) {
60
- return url.isHttp(file.url);
61
- },
62
-
63
- /**
64
- * Reads the given URL and returns its raw contents as a Buffer.
65
- *
66
- * @param {object} file - An object containing information about the referenced file
67
- * @param {string} file.url - The full URL of the referenced file
68
- * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
69
- * @returns {Promise<Buffer>}
70
- */
71
- read (file) {
72
- let u = url.parse(file.url);
73
-
74
- if (typeof window !== "undefined" && !u.protocol) {
75
- // Use the protocol of the current page
76
- u.protocol = url.parse(location.href).protocol;
77
- }
78
-
79
- return download(u, this);
80
- }
81
- };
82
-
83
- /**
84
- * Downloads the given file.
85
- *
86
- * @param {Url|string} u - The url to download (can be a parsed {@link Url} object)
87
- * @param {object} httpOptions - The `options.resolve.http` object
88
- * @param {number} [redirects] - The redirect URLs that have already been followed
89
- *
90
- * @returns {Promise<Buffer>}
91
- * The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
92
- */
93
- function download (u, httpOptions, redirects) {
94
- u = url.parse(u);
95
- redirects = redirects || [];
96
- redirects.push(u.href);
97
-
98
- return get(u, httpOptions)
99
- .then((res) => {
100
- if (res.status >= 400) {
101
- throw ono({ status: res.statusCode }, `HTTP ERROR ${res.status}`);
102
- }
103
- else if (res.status >= 300) {
104
- if (redirects.length > httpOptions.redirects) {
105
- throw new ResolverError(ono({ status: res.status },
106
- `Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
107
- }
108
- else if (!res.headers.location) {
109
- throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
110
- }
111
- else {
112
- // console.log('HTTP %d redirect %s -> %s', res.status, u.href, res.headers.location);
113
- let redirectTo = url.resolve(u, res.headers.location);
114
- return download(redirectTo, httpOptions, redirects);
115
- }
116
- }
117
- else {
118
- return res.body ? res.arrayBuffer().then(buf => Buffer.from(buf)) : Buffer.alloc(0);
119
- }
120
- })
121
- .catch((err) => {
122
- throw new ResolverError(ono(err, `Error downloading ${u.href}`), u.href);
123
- });
124
- }
125
-
126
- /**
127
- * Sends an HTTP GET request.
128
- *
129
- * @param {Url} u - A parsed {@link Url} object
130
- * @param {object} httpOptions - The `options.resolve.http` object
131
- *
132
- * @returns {Promise<Response>}
133
- * The promise resolves with the HTTP Response object.
134
- */
135
- function get (u, httpOptions) {
136
- let controller;
137
- let timeoutId;
138
- if (httpOptions.timeout) {
139
- controller = new AbortController();
140
- timeoutId = setTimeout(() => controller.abort(), httpOptions.timeout);
141
- }
142
-
143
- return fetch(u, {
144
- method: "GET",
145
- headers: httpOptions.headers || {},
146
- credentials: httpOptions.withCredentials ? "include" : "same-origin",
147
- signal: controller ? controller.signal : null,
148
- }).then(response => {
149
- if (timeoutId) {
150
- clearTimeout(timeoutId);
151
- }
152
-
153
- return response;
154
- });
155
- }
@@ -1,134 +0,0 @@
1
- import { Ono } from "@jsdevtools/ono";
2
-
3
- import { stripHash, toFileSystemPath } from "./url.js";
4
-
5
- export const JSONParserError = class JSONParserError extends Error {
6
- constructor (message, source) {
7
- super();
8
-
9
- this.code = "EUNKNOWN";
10
- this.message = message;
11
- this.source = source;
12
- this.path = null;
13
-
14
- Ono.extend(this);
15
- }
16
-
17
- get footprint () {
18
- return `${this.path}+${this.source}+${this.code}+${this.message}`;
19
- }
20
- };
21
-
22
- setErrorName(JSONParserError);
23
-
24
- export const JSONParserErrorGroup = class JSONParserErrorGroup extends Error {
25
- constructor (parser) {
26
- super();
27
-
28
- this.files = parser;
29
- this.message = `${this.errors.length} error${this.errors.length > 1 ? "s" : ""} occurred while reading '${toFileSystemPath(parser.$refs._root$Ref.path)}'`;
30
-
31
- Ono.extend(this);
32
- }
33
-
34
- static getParserErrors (parser) {
35
- const errors = [];
36
-
37
- for (const $ref of Object.values(parser.$refs._$refs)) {
38
- if ($ref.errors) {
39
- errors.push(...$ref.errors);
40
- }
41
- }
42
-
43
- return errors;
44
- }
45
-
46
- get errors () {
47
- return JSONParserErrorGroup.getParserErrors(this.files);
48
- }
49
- };
50
-
51
- setErrorName(JSONParserErrorGroup);
52
-
53
- export const ParserError = class ParserError extends JSONParserError {
54
- constructor (message, source) {
55
- super(`Error parsing ${source}: ${message}`, source);
56
-
57
- this.code = "EPARSER";
58
- }
59
- };
60
-
61
- setErrorName(ParserError);
62
-
63
- export const UnmatchedParserError = class UnmatchedParserError extends JSONParserError {
64
- constructor (source) {
65
- super(`Could not find parser for "${source}"`, source);
66
-
67
- this.code = "EUNMATCHEDPARSER";
68
- }
69
- };
70
-
71
- setErrorName(UnmatchedParserError);
72
-
73
- export const ResolverError = class ResolverError extends JSONParserError {
74
- constructor (ex, source) {
75
- super(ex.message || `Error reading file "${source}"`, source);
76
-
77
- this.code = "ERESOLVER";
78
-
79
- if ("code" in ex) {
80
- this.ioErrorCode = String(ex.code);
81
- }
82
- }
83
- };
84
-
85
- setErrorName(ResolverError);
86
-
87
- export const UnmatchedResolverError = class UnmatchedResolverError extends JSONParserError {
88
- constructor (source) {
89
- super(`Could not find resolver for "${source}"`, source);
90
-
91
- this.code = "EUNMATCHEDRESOLVER";
92
- }
93
- };
94
-
95
- setErrorName(UnmatchedResolverError);
96
-
97
- export const MissingPointerError = class MissingPointerError extends JSONParserError {
98
- constructor (token, path) {
99
- super(`Token "${token}" does not exist.`, stripHash(path));
100
-
101
- this.code = "EMISSINGPOINTER";
102
- }
103
- };
104
-
105
- setErrorName(MissingPointerError);
106
-
107
- export const InvalidPointerError = class InvalidPointerError extends JSONParserError {
108
- constructor (pointer, path) {
109
- super(`Invalid $ref pointer "${pointer}". Pointers must begin with "#/"`, stripHash(path));
110
-
111
- this.code = "EINVALIDPOINTER";
112
- }
113
- };
114
-
115
- setErrorName(InvalidPointerError);
116
-
117
- function setErrorName (err) {
118
- Object.defineProperty(err.prototype, "name", {
119
- value: err.name,
120
- enumerable: true,
121
- });
122
- }
123
-
124
- export function isHandledError (err) {
125
- return err instanceof JSONParserError || err instanceof JSONParserErrorGroup;
126
- }
127
-
128
- export function normalizeError (err) {
129
- if (err.path === null) {
130
- err.path = [];
131
- }
132
-
133
- return err;
134
- }
@@ -1,6 +0,0 @@
1
- const nodePath = require("path");
2
-
3
- // Webpack 4 (used by browser tests) can't transpile import.meta.url
4
- // So export the project directory using __dirname from a .cjs module
5
- const projectDir = nodePath.resolve(__dirname, "..", "..");
6
- module.exports = projectDir