@apidevtools/json-schema-ref-parser 10.1.0 → 11.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.
- package/README.md +22 -0
- package/dist/lib/bundle.js +1 -0
- package/dist/lib/dereference.js +3 -1
- package/dist/lib/index.js +92 -102
- package/dist/lib/options.d.ts +6 -0
- package/dist/lib/options.js +1 -0
- 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 +5 -4
- package/dist/lib/util/url.js +62 -17
- package/dist/vite.config.d.ts +1 -1
- package/dist/vite.config.js +0 -4
- package/lib/bundle.ts +1 -0
- package/lib/dereference.ts +3 -1
- package/lib/index.ts +7 -0
- package/lib/options.ts +9 -1
- package/lib/ref.ts +1 -2
- package/lib/refs.ts +6 -8
- package/lib/resolve-external.ts +13 -14
- package/lib/resolvers/file.ts +1 -1
- package/lib/resolvers/http.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 +42 -21
- 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/dereference.js
CHANGED
|
@@ -130,7 +130,9 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
130
130
|
*/
|
|
131
131
|
function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
|
|
132
132
|
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
|
|
133
|
-
const
|
|
133
|
+
const isExternalRef = ref_js_1.default.isExternal$Ref($ref);
|
|
134
|
+
const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
|
|
135
|
+
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
|
|
134
136
|
const cache = dereferencedCache.get($refPath);
|
|
135
137
|
if (cache) {
|
|
136
138
|
const refKeys = Object.keys($ref);
|
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,71 @@ 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
|
+
else if (!args.path && args.schema && args.schema.$id) {
|
|
92
|
+
// when schema id has defined an URL should use that hostname to request the references,
|
|
93
|
+
// instead of using the current page URL
|
|
94
|
+
const params = url.parse(args.schema.$id);
|
|
95
|
+
const port = params.protocol === "https:" ? 443 : 80;
|
|
96
|
+
args.path = `${params.protocol}//${params.hostname}:${port}`;
|
|
97
|
+
}
|
|
98
|
+
// Resolve the absolute path of the schema
|
|
99
|
+
args.path = url.resolve(url.cwd(), args.path);
|
|
100
|
+
if (args.schema && typeof args.schema === "object") {
|
|
101
|
+
// A schema object was passed-in.
|
|
102
|
+
// So immediately add a new $Ref with the schema object as its value
|
|
103
|
+
const $ref = this.$refs._add(args.path);
|
|
104
|
+
$ref.value = args.schema;
|
|
105
|
+
$ref.pathType = pathType;
|
|
106
|
+
promise = Promise.resolve(args.schema);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// Parse the schema file/url
|
|
110
|
+
promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const result = await promise;
|
|
114
|
+
if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
|
|
115
|
+
this.schema = result;
|
|
116
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
100
117
|
}
|
|
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);
|
|
118
|
+
else if (args.options.continueOnError) {
|
|
119
|
+
this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
|
|
120
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
110
121
|
}
|
|
111
122
|
else {
|
|
112
|
-
|
|
113
|
-
promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
|
|
123
|
+
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
|
|
114
124
|
}
|
|
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
|
-
}
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
if (!args.options.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
|
|
128
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
128
129
|
}
|
|
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));
|
|
130
|
+
if (this.$refs._$refs[url.stripHash(args.path)]) {
|
|
131
|
+
this.$refs._$refs[url.stripHash(args.path)].addError(err);
|
|
137
132
|
}
|
|
138
|
-
|
|
133
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(null));
|
|
134
|
+
}
|
|
139
135
|
}
|
|
140
136
|
static parse() {
|
|
141
137
|
const parser = new $RefParser();
|
|
@@ -154,19 +150,17 @@ class $RefParser {
|
|
|
154
150
|
* @returns
|
|
155
151
|
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
|
|
156
152
|
*/
|
|
157
|
-
resolve() {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
});
|
|
153
|
+
async resolve() {
|
|
154
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
155
|
+
try {
|
|
156
|
+
await this.parse(args.path, args.schema, args.options);
|
|
157
|
+
await (0, resolve_external_js_1.default)(this, args.options);
|
|
158
|
+
finalize(this);
|
|
159
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.$refs));
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
163
|
+
}
|
|
170
164
|
}
|
|
171
165
|
static resolve() {
|
|
172
166
|
const instance = new $RefParser();
|
|
@@ -176,37 +170,33 @@ class $RefParser {
|
|
|
176
170
|
const instance = new $RefParser();
|
|
177
171
|
return instance.bundle.apply(instance, arguments);
|
|
178
172
|
}
|
|
179
|
-
bundle() {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
});
|
|
173
|
+
async bundle() {
|
|
174
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
175
|
+
try {
|
|
176
|
+
await this.resolve(args.path, args.schema, args.options);
|
|
177
|
+
(0, bundle_js_1.default)(this, args.options);
|
|
178
|
+
finalize(this);
|
|
179
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
183
|
+
}
|
|
192
184
|
}
|
|
193
185
|
static dereference() {
|
|
194
186
|
const instance = new $RefParser();
|
|
195
187
|
return instance.dereference.apply(instance, arguments);
|
|
196
188
|
}
|
|
197
|
-
dereference() {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
});
|
|
189
|
+
async dereference() {
|
|
190
|
+
const args = (0, normalize_args_js_1.default)(arguments);
|
|
191
|
+
try {
|
|
192
|
+
await this.resolve(args.path, args.schema, args.options);
|
|
193
|
+
(0, dereference_js_1.default)(this, args.options);
|
|
194
|
+
finalize(this);
|
|
195
|
+
return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
|
|
199
|
+
}
|
|
210
200
|
}
|
|
211
201
|
}
|
|
212
202
|
exports.$RefParser = $RefParser;
|
package/dist/lib/options.d.ts
CHANGED
|
@@ -69,6 +69,12 @@ interface $RefParserOptions {
|
|
|
69
69
|
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
|
|
70
70
|
*/
|
|
71
71
|
onDereference?(path: string, value: JSONSchemaObject): void;
|
|
72
|
+
/**
|
|
73
|
+
* Whether a reference should resolve relative to its directory/path, or from the cwd
|
|
74
|
+
*
|
|
75
|
+
* Default: `relative`
|
|
76
|
+
*/
|
|
77
|
+
externalReferenceResolution?: "relative" | "root";
|
|
72
78
|
};
|
|
73
79
|
}
|
|
74
80
|
export declare const getNewOptions: (options: DeepPartial<$RefParserOptions>) => $RefParserOptions;
|
package/dist/lib/options.js
CHANGED
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
|
};
|