@apidevtools/json-schema-ref-parser 10.1.0 → 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.
- package/README.md +22 -0
- package/dist/lib/bundle.js +1 -0
- package/dist/lib/index.js +85 -102
- package/dist/lib/parse.js +86 -101
- package/dist/lib/parsers/json.js +18 -29
- package/dist/lib/parsers/yaml.js +17 -28
- package/dist/lib/ref.d.ts +1 -2
- package/dist/lib/ref.js +1 -2
- package/dist/lib/refs.js +3 -4
- package/dist/lib/resolve-external.js +34 -49
- package/dist/lib/resolvers/file.js +16 -30
- package/dist/lib/resolvers/http.js +51 -64
- package/dist/lib/util/convert-path-to-posix.d.ts +1 -0
- package/dist/lib/util/convert-path-to-posix.js +14 -0
- package/dist/lib/util/is-windows.d.ts +1 -0
- package/dist/lib/util/is-windows.js +6 -0
- package/dist/lib/util/plugins.js +47 -58
- package/dist/lib/util/url.d.ts +3 -2
- package/dist/lib/util/url.js +60 -16
- package/dist/vite.config.d.ts +1 -1
- package/dist/vite.config.js +0 -4
- package/lib/bundle.ts +1 -0
- package/lib/ref.ts +1 -2
- package/lib/refs.ts +3 -5
- package/lib/resolve-external.ts +12 -12
- package/lib/resolvers/file.ts +1 -1
- package/lib/util/convert-path-to-posix.ts +11 -0
- package/lib/util/is-windows.ts +2 -0
- package/lib/util/url.ts +38 -18
- package/package.json +21 -22
package/README.md
CHANGED
|
@@ -124,6 +124,28 @@ JSON Schema $Ref Parser supports recent versions of every major web browser. Ol
|
|
|
124
124
|
To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve).
|
|
125
125
|
|
|
126
126
|
|
|
127
|
+
#### Webpack 5
|
|
128
|
+
Webpack 5 has dropped the default export of node core modules in favour of polyfills, you'll need to set them up yourself ( after npm-installing them )
|
|
129
|
+
Edit your `webpack.config.js` :
|
|
130
|
+
```js
|
|
131
|
+
config.resolve.fallback = {
|
|
132
|
+
"path": require.resolve("path-browserify"),
|
|
133
|
+
'util': require.resolve('util/'),
|
|
134
|
+
'fs': require.resolve('browserify-fs'),
|
|
135
|
+
"buffer": require.resolve("buffer/"),
|
|
136
|
+
"http": require.resolve("stream-http"),
|
|
137
|
+
"https": require.resolve("https-browserify"),
|
|
138
|
+
"url": require.resolve("url"),
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
config.plugins.push(
|
|
142
|
+
new webpack.ProvidePlugin({
|
|
143
|
+
Buffer: [ 'buffer', 'Buffer']
|
|
144
|
+
})
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
|
|
127
149
|
|
|
128
150
|
API Documentation
|
|
129
151
|
--------------------------
|
package/dist/lib/bundle.js
CHANGED
|
@@ -106,6 +106,7 @@ function crawl(parent, key, path, pathFromRoot, indirections, inventory, $refs,
|
|
|
106
106
|
* @param $refParent - The object that contains a JSON Reference as one of its keys
|
|
107
107
|
* @param $refKey - The key in `$refParent` that is a JSON Reference
|
|
108
108
|
* @param path - The full path of the JSON Reference at `$refKey`, possibly with a JSON Pointer in the hash
|
|
109
|
+
* @param indirections - unknown
|
|
109
110
|
* @param pathFromRoot - The path of the JSON Reference at `$refKey`, from the schema root
|
|
110
111
|
* @param inventory - An array of already-inventoried $ref pointers
|
|
111
112
|
* @param $refs
|
package/dist/lib/index.js
CHANGED
|
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
27
|
};
|
|
@@ -76,66 +67,64 @@ class $RefParser {
|
|
|
76
67
|
*/
|
|
77
68
|
this.$refs = new refs_js_1.default();
|
|
78
69
|
}
|
|
79
|
-
parse() {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
70
|
+
async parse() {
|
|
71
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
72
|
+
let promise;
|
|
73
|
+
if (!args.path && !args.schema) {
|
|
74
|
+
const err = (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${args.path || args.schema}`);
|
|
75
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
76
|
+
}
|
|
77
|
+
// Reset everything
|
|
78
|
+
this.schema = null;
|
|
79
|
+
this.$refs = new refs_js_1.default();
|
|
80
|
+
// If the path is a filesystem path, then convert it to a URL.
|
|
81
|
+
// NOTE: According to the JSON Reference spec, these should already be URLs,
|
|
82
|
+
// but, in practice, many people use local filesystem paths instead.
|
|
83
|
+
// So we're being generous here and doing the conversion automatically.
|
|
84
|
+
// This is not intended to be a 100% bulletproof solution.
|
|
85
|
+
// If it doesn't work for your use-case, then use a URL instead.
|
|
86
|
+
let pathType = "http";
|
|
87
|
+
if (url.isFileSystemPath(args.path)) {
|
|
88
|
+
args.path = url.fromFileSystemPath(args.path);
|
|
89
|
+
pathType = "file";
|
|
90
|
+
}
|
|
91
|
+
// Resolve the absolute path of the schema
|
|
92
|
+
args.path = url.resolve(url.cwd(), args.path);
|
|
93
|
+
if (args.schema && typeof args.schema === "object") {
|
|
94
|
+
// A schema object was passed-in.
|
|
95
|
+
// So immediately add a new $Ref with the schema object as its value
|
|
96
|
+
const $ref = this.$refs._add(args.path);
|
|
97
|
+
$ref.value = args.schema;
|
|
98
|
+
$ref.pathType = pathType;
|
|
99
|
+
promise = Promise.resolve(args.schema);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// Parse the schema file/url
|
|
103
|
+
promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const result = await promise;
|
|
107
|
+
if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
|
|
108
|
+
this.schema = result;
|
|
109
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
100
110
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
// A schema object was passed-in.
|
|
105
|
-
// So immediately add a new $Ref with the schema object as its value
|
|
106
|
-
const $ref = this.$refs._add(args.path);
|
|
107
|
-
$ref.value = args.schema;
|
|
108
|
-
$ref.pathType = pathType;
|
|
109
|
-
promise = Promise.resolve(args.schema);
|
|
111
|
+
else if (args.options.continueOnError) {
|
|
112
|
+
this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
|
|
113
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
110
114
|
}
|
|
111
115
|
else {
|
|
112
|
-
|
|
113
|
-
promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
|
|
116
|
+
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
|
|
114
117
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
120
|
-
}
|
|
121
|
-
else if (args.options.continueOnError) {
|
|
122
|
-
this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
|
|
123
|
-
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
|
|
127
|
-
}
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
if (!args.options.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
|
|
121
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
128
122
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
132
|
-
}
|
|
133
|
-
if (this.$refs._$refs[url.stripHash(args.path)]) {
|
|
134
|
-
this.$refs._$refs[url.stripHash(args.path)].addError(err);
|
|
135
|
-
}
|
|
136
|
-
return (0, maybe_js_1.default)(args.callback, Promise.resolve(null));
|
|
123
|
+
if (this.$refs._$refs[url.stripHash(args.path)]) {
|
|
124
|
+
this.$refs._$refs[url.stripHash(args.path)].addError(err);
|
|
137
125
|
}
|
|
138
|
-
|
|
126
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(null));
|
|
127
|
+
}
|
|
139
128
|
}
|
|
140
129
|
static parse() {
|
|
141
130
|
const parser = new $RefParser();
|
|
@@ -154,19 +143,17 @@ class $RefParser {
|
|
|
154
143
|
* @returns
|
|
155
144
|
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
|
|
156
145
|
*/
|
|
157
|
-
resolve() {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
});
|
|
146
|
+
async resolve() {
|
|
147
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
148
|
+
try {
|
|
149
|
+
await this.parse(args.path, args.schema, args.options);
|
|
150
|
+
await (0, resolve_external_js_1.default)(this, args.options);
|
|
151
|
+
finalize(this);
|
|
152
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.$refs));
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
156
|
+
}
|
|
170
157
|
}
|
|
171
158
|
static resolve() {
|
|
172
159
|
const instance = new $RefParser();
|
|
@@ -176,37 +163,33 @@ class $RefParser {
|
|
|
176
163
|
const instance = new $RefParser();
|
|
177
164
|
return instance.bundle.apply(instance, arguments);
|
|
178
165
|
}
|
|
179
|
-
bundle() {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
});
|
|
166
|
+
async bundle() {
|
|
167
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
168
|
+
try {
|
|
169
|
+
await this.resolve(args.path, args.schema, args.options);
|
|
170
|
+
(0, bundle_js_1.default)(this, args.options);
|
|
171
|
+
finalize(this);
|
|
172
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
176
|
+
}
|
|
192
177
|
}
|
|
193
178
|
static dereference() {
|
|
194
179
|
const instance = new $RefParser();
|
|
195
180
|
return instance.dereference.apply(instance, arguments);
|
|
196
181
|
}
|
|
197
|
-
dereference() {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
});
|
|
182
|
+
async dereference() {
|
|
183
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
184
|
+
try {
|
|
185
|
+
await this.resolve(args.path, args.schema, args.options);
|
|
186
|
+
(0, dereference_js_1.default)(this, args.options);
|
|
187
|
+
finalize(this);
|
|
188
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
192
|
+
}
|
|
210
193
|
}
|
|
211
194
|
}
|
|
212
195
|
exports.$RefParser = $RefParser;
|
package/dist/lib/parse.js
CHANGED
|
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
26
|
const ono_1 = require("@jsdevtools/ono");
|
|
36
27
|
const url = __importStar(require("./util/url.js"));
|
|
@@ -40,34 +31,32 @@ exports.default = parse;
|
|
|
40
31
|
/**
|
|
41
32
|
* Reads and parses the specified file path or URL.
|
|
42
33
|
*/
|
|
43
|
-
function parse(path, $refs, options) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
$ref.value = err;
|
|
67
|
-
}
|
|
68
|
-
throw err;
|
|
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;
|
|
69
57
|
}
|
|
70
|
-
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
71
60
|
}
|
|
72
61
|
/**
|
|
73
62
|
* Reads the given file, using the configured resolver plugins
|
|
@@ -80,36 +69,34 @@ function parse(path, $refs, options) {
|
|
|
80
69
|
* @returns
|
|
81
70
|
* The promise resolves with the raw file contents and the resolver that was used.
|
|
82
71
|
*/
|
|
83
|
-
function readFile(file, options, $refs) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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}"`);
|
|
94
91
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
throw new errors_js_1.UnmatchedResolverError(file.url);
|
|
99
|
-
}
|
|
100
|
-
else if (!err || !("error" in err)) {
|
|
101
|
-
// Throw a generic, friendly error.
|
|
102
|
-
throw ono_1.ono.syntax(`Unable to resolve $ref pointer "${file.url}"`);
|
|
103
|
-
}
|
|
104
|
-
// Throw the original error, if it's one of our own (user-friendly) errors.
|
|
105
|
-
else if (err.error instanceof errors_js_1.ResolverError) {
|
|
106
|
-
throw err.error;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
throw new errors_js_1.ResolverError(err, file.url);
|
|
110
|
-
}
|
|
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;
|
|
111
95
|
}
|
|
112
|
-
|
|
96
|
+
else {
|
|
97
|
+
throw new errors_js_1.ResolverError(err, file.url);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
113
100
|
}
|
|
114
101
|
/**
|
|
115
102
|
* Parses the given file's contents, using the configured parser plugins.
|
|
@@ -123,45 +110,43 @@ function readFile(file, options, $refs) {
|
|
|
123
110
|
* @returns
|
|
124
111
|
* The promise resolves with the parsed file contents and the parser that was used.
|
|
125
112
|
*/
|
|
126
|
-
function parseFile(file, options, $refs) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
throw ono_1.ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
return parser;
|
|
144
|
-
}
|
|
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`);
|
|
145
127
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// No resolver could be matched
|
|
149
|
-
throw new errors_js_1.UnmatchedParserError(file.url);
|
|
150
|
-
}
|
|
151
|
-
else if (err && err.message && err.message.startsWith("Error parsing")) {
|
|
152
|
-
throw err;
|
|
153
|
-
}
|
|
154
|
-
else if (!err || !("error" in err)) {
|
|
155
|
-
throw ono_1.ono.syntax(`Unable to parse ${file.url}`);
|
|
156
|
-
}
|
|
157
|
-
else if (err.error instanceof errors_js_1.ParserError) {
|
|
158
|
-
throw err.error;
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
throw new errors_js_1.ParserError(err.error.message, file.url);
|
|
162
|
-
}
|
|
128
|
+
else {
|
|
129
|
+
return parser;
|
|
163
130
|
}
|
|
164
|
-
}
|
|
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
|
+
}
|
|
165
150
|
}
|
|
166
151
|
/**
|
|
167
152
|
* Determines whether the parsed value is "empty".
|
package/dist/lib/parsers/json.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
const errors_js_1 = require("../util/errors.js");
|
|
13
4
|
exports.default = {
|
|
@@ -29,29 +20,27 @@ exports.default = {
|
|
|
29
20
|
/**
|
|
30
21
|
* Parses the given file as JSON
|
|
31
22
|
*/
|
|
32
|
-
parse(file) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
37
31
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return;
|
|
32
|
+
else {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(data);
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return JSON.parse(data);
|
|
45
|
-
}
|
|
46
|
-
catch (e) {
|
|
47
|
-
throw new errors_js_1.ParserError(e.message, file.url);
|
|
48
|
-
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw new errors_js_1.ParserError(e.message, file.url);
|
|
49
38
|
}
|
|
50
39
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
56
45
|
},
|
|
57
46
|
};
|
package/dist/lib/parsers/yaml.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -40,26 +31,24 @@ exports.default = {
|
|
|
40
31
|
* @param file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
41
32
|
* @returns
|
|
42
33
|
*/
|
|
43
|
-
parse(file) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return js_yaml_1.default.load(data, { schema: js_yaml_2.JSON_SCHEMA });
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
// @ts-expect-error TS(2571): Object is of type 'unknown'.
|
|
56
|
-
throw new errors_js_1.ParserError(e.message, file.url);
|
|
57
|
-
}
|
|
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 });
|
|
58
43
|
}
|
|
59
|
-
|
|
60
|
-
//
|
|
61
|
-
|
|
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);
|
|
62
47
|
}
|
|
63
|
-
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
64
53
|
},
|
|
65
54
|
};
|
package/dist/lib/ref.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ import Pointer from "./pointer.js";
|
|
|
2
2
|
import type { JSONParserError, MissingPointerError, ParserError, ResolverError } from "./util/errors.js";
|
|
3
3
|
import type $Refs from "./refs.js";
|
|
4
4
|
import type $RefParserOptions from "./options.js";
|
|
5
|
-
import type { JSONSchema } from "./types";
|
|
6
5
|
type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
|
|
7
6
|
/**
|
|
8
7
|
* This class represents a single JSON reference and its resolved value.
|
|
@@ -100,7 +99,7 @@ declare class $Ref {
|
|
|
100
99
|
* @param value - The value to inspect
|
|
101
100
|
* @returns
|
|
102
101
|
*/
|
|
103
|
-
static isExternal$Ref(value: any):
|
|
102
|
+
static isExternal$Ref(value: any): boolean;
|
|
104
103
|
/**
|
|
105
104
|
* Determines whether the given value is a JSON reference, and whether it is allowed by the options.
|
|
106
105
|
* For example, if it references an external file, then options.resolve.external must be true.
|
package/dist/lib/ref.js
CHANGED
|
@@ -64,8 +64,7 @@ class $Ref {
|
|
|
64
64
|
* @returns - Returns the resolved value
|
|
65
65
|
*/
|
|
66
66
|
get(path, options) {
|
|
67
|
-
|
|
68
|
-
return (_a = this.resolve(path, options)) === null || _a === void 0 ? void 0 : _a.value;
|
|
67
|
+
return this.resolve(path, options)?.value;
|
|
69
68
|
}
|
|
70
69
|
/**
|
|
71
70
|
* Resolves the given JSON reference within this {@link $Ref#value}.
|