@apidevtools/json-schema-ref-parser 11.7.2 → 11.8.2
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 +4 -8
- package/cjs/bundle.js +304 -0
- package/cjs/dereference.js +258 -0
- package/cjs/index.js +603 -0
- package/cjs/normalize-args.js +64 -0
- package/cjs/options.js +125 -0
- package/cjs/package.json +3 -0
- package/cjs/parse.js +338 -0
- package/cjs/parsers/binary.js +54 -0
- package/cjs/parsers/json.js +199 -0
- package/cjs/parsers/text.js +61 -0
- package/cjs/parsers/yaml.js +239 -0
- package/cjs/pointer.js +290 -0
- package/cjs/ref.js +333 -0
- package/cjs/refs.js +214 -0
- package/cjs/resolve-external.js +333 -0
- package/cjs/resolvers/file.js +106 -0
- package/cjs/resolvers/http.js +184 -0
- package/cjs/util/errors.js +401 -0
- package/cjs/util/plugins.js +159 -0
- package/cjs/util/projectDir.cjs +6 -0
- package/cjs/util/url.js +228 -0
- package/dist/lib/bundle.js +17 -7
- package/dist/lib/dereference.js +20 -8
- package/dist/lib/index.d.ts +6 -6
- package/dist/lib/index.js +17 -7
- package/dist/lib/options.d.ts +9 -2
- package/dist/lib/parse.d.ts +1 -1
- package/dist/lib/parse.js +17 -7
- package/dist/lib/pointer.js +25 -9
- package/dist/lib/refs.js +17 -7
- package/dist/lib/resolve-external.js +17 -7
- package/dist/lib/resolvers/file.js +17 -7
- package/dist/lib/resolvers/http.js +17 -7
- package/dist/lib/util/errors.d.ts +6 -2
- package/dist/lib/util/errors.js +7 -3
- package/dist/lib/util/plugins.d.ts +2 -2
- package/dist/lib/util/url.js +20 -9
- package/lib/dereference.ts +4 -1
- package/lib/index.ts +6 -12
- package/lib/options.ts +7 -0
- package/lib/pointer.ts +13 -2
- package/lib/util/errors.ts +14 -5
- package/lib/util/plugins.ts +6 -7
- package/lib/util/url.ts +3 -2
- package/package.json +31 -31
package/cjs/options.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */ "use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _jsonJs = /*#__PURE__*/ _interopRequireDefault(require("./parsers/json.js"));
|
|
12
|
+
var _yamlJs = /*#__PURE__*/ _interopRequireDefault(require("./parsers/yaml.js"));
|
|
13
|
+
var _textJs = /*#__PURE__*/ _interopRequireDefault(require("./parsers/text.js"));
|
|
14
|
+
var _binaryJs = /*#__PURE__*/ _interopRequireDefault(require("./parsers/binary.js"));
|
|
15
|
+
var _fileJs = /*#__PURE__*/ _interopRequireDefault(require("./resolvers/file.js"));
|
|
16
|
+
var _httpJs = /*#__PURE__*/ _interopRequireDefault(require("./resolvers/http.js"));
|
|
17
|
+
function _instanceof(left, right) {
|
|
18
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
19
|
+
return !!right[Symbol.hasInstance](left);
|
|
20
|
+
} else {
|
|
21
|
+
return left instanceof right;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function _interopRequireDefault(obj) {
|
|
25
|
+
return obj && obj.__esModule ? obj : {
|
|
26
|
+
default: obj
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var _default = $RefParserOptions;
|
|
30
|
+
/**
|
|
31
|
+
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
|
|
32
|
+
*
|
|
33
|
+
* @param {object|$RefParserOptions} [options] - Overridden options
|
|
34
|
+
* @constructor
|
|
35
|
+
*/ function $RefParserOptions(options) {
|
|
36
|
+
merge(this, $RefParserOptions.defaults);
|
|
37
|
+
merge(this, options);
|
|
38
|
+
}
|
|
39
|
+
$RefParserOptions.defaults = {
|
|
40
|
+
/**
|
|
41
|
+
* Determines how different types of files will be parsed.
|
|
42
|
+
*
|
|
43
|
+
* You can add additional parsers of your own, replace an existing one with
|
|
44
|
+
* your own implementation, or disable any parser by setting it to false.
|
|
45
|
+
*/ parse: {
|
|
46
|
+
json: _jsonJs.default,
|
|
47
|
+
yaml: _yamlJs.default,
|
|
48
|
+
text: _textJs.default,
|
|
49
|
+
binary: _binaryJs.default
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* Determines how JSON References will be resolved.
|
|
53
|
+
*
|
|
54
|
+
* You can add additional resolvers of your own, replace an existing one with
|
|
55
|
+
* your own implementation, or disable any resolver by setting it to false.
|
|
56
|
+
*/ resolve: {
|
|
57
|
+
file: _fileJs.default,
|
|
58
|
+
http: _httpJs.default,
|
|
59
|
+
/**
|
|
60
|
+
* Determines whether external $ref pointers will be resolved.
|
|
61
|
+
* If this option is disabled, then none of above resolvers will be called.
|
|
62
|
+
* Instead, external $ref pointers will simply be ignored.
|
|
63
|
+
*
|
|
64
|
+
* @type {boolean}
|
|
65
|
+
*/ external: true
|
|
66
|
+
},
|
|
67
|
+
/**
|
|
68
|
+
* By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
|
|
69
|
+
* causes it to keep processing as much as possible and then throw a single error that contains all errors
|
|
70
|
+
* that were encountered.
|
|
71
|
+
*/ continueOnError: false,
|
|
72
|
+
/**
|
|
73
|
+
* Determines the types of JSON references that are allowed.
|
|
74
|
+
*/ dereference: {
|
|
75
|
+
/**
|
|
76
|
+
* Dereference circular (recursive) JSON references?
|
|
77
|
+
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
|
|
78
|
+
* If "ignore", then circular references will not be dereferenced.
|
|
79
|
+
*
|
|
80
|
+
* @type {boolean|string}
|
|
81
|
+
*/ circular: true,
|
|
82
|
+
/**
|
|
83
|
+
* A function, called for each path, which can return true to stop this path and all
|
|
84
|
+
* subpaths from being dereferenced further. This is useful in schemas where some
|
|
85
|
+
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
86
|
+
*
|
|
87
|
+
* @type {function}
|
|
88
|
+
*/ excludedPathMatcher: function() {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Merges the properties of the source object into the target object.
|
|
95
|
+
*
|
|
96
|
+
* @param {object} target - The object that we're populating
|
|
97
|
+
* @param {?object} source - The options that are being merged
|
|
98
|
+
* @returns {object}
|
|
99
|
+
*/ function merge(target, source) {
|
|
100
|
+
if (isMergeable(source)) {
|
|
101
|
+
var keys = Object.keys(source);
|
|
102
|
+
for(var i = 0; i < keys.length; i++){
|
|
103
|
+
var key = keys[i];
|
|
104
|
+
var sourceSetting = source[key];
|
|
105
|
+
var targetSetting = target[key];
|
|
106
|
+
if (isMergeable(sourceSetting)) {
|
|
107
|
+
// It's a nested object, so merge it recursively
|
|
108
|
+
target[key] = merge(targetSetting || {}, sourceSetting);
|
|
109
|
+
} else if (sourceSetting !== undefined) {
|
|
110
|
+
// It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
|
|
111
|
+
target[key] = sourceSetting;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return target;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Determines whether the given value can be merged,
|
|
119
|
+
* or if it is a scalar value that should just override the target value.
|
|
120
|
+
*
|
|
121
|
+
* @param {*} val
|
|
122
|
+
* @returns {Boolean}
|
|
123
|
+
*/ function isMergeable(val) {
|
|
124
|
+
return val && typeof val === "object" && !Array.isArray(val) && !_instanceof(val, RegExp) && !_instanceof(val, Date);
|
|
125
|
+
}
|
package/cjs/package.json
ADDED
package/cjs/parse.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _ono = require("@jsdevtools/ono");
|
|
12
|
+
var _urlJs = /*#__PURE__*/ _interopRequireWildcard(require("./util/url.js"));
|
|
13
|
+
var _pluginsJs = /*#__PURE__*/ _interopRequireWildcard(require("./util/plugins.js"));
|
|
14
|
+
var _errorsJs = require("./util/errors.js");
|
|
15
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
16
|
+
try {
|
|
17
|
+
var info = gen[key](arg);
|
|
18
|
+
var value = info.value;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (info.done) {
|
|
24
|
+
resolve(value);
|
|
25
|
+
} else {
|
|
26
|
+
Promise.resolve(value).then(_next, _throw);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function _asyncToGenerator(fn) {
|
|
30
|
+
return function() {
|
|
31
|
+
var self = this, args = arguments;
|
|
32
|
+
return new Promise(function(resolve, reject) {
|
|
33
|
+
var gen = fn.apply(self, args);
|
|
34
|
+
function _next(value) {
|
|
35
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
36
|
+
}
|
|
37
|
+
function _throw(err) {
|
|
38
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
39
|
+
}
|
|
40
|
+
_next(undefined);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function _instanceof(left, right) {
|
|
45
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
46
|
+
return !!right[Symbol.hasInstance](left);
|
|
47
|
+
} else {
|
|
48
|
+
return left instanceof right;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
52
|
+
if (typeof WeakMap !== "function") return null;
|
|
53
|
+
var cacheBabelInterop = new WeakMap();
|
|
54
|
+
var cacheNodeInterop = new WeakMap();
|
|
55
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
56
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
57
|
+
})(nodeInterop);
|
|
58
|
+
}
|
|
59
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
60
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
61
|
+
return obj;
|
|
62
|
+
}
|
|
63
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
64
|
+
return {
|
|
65
|
+
default: obj
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
69
|
+
if (cache && cache.has(obj)) {
|
|
70
|
+
return cache.get(obj);
|
|
71
|
+
}
|
|
72
|
+
var newObj = {};
|
|
73
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
74
|
+
for(var key in obj){
|
|
75
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
76
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
77
|
+
if (desc && (desc.get || desc.set)) {
|
|
78
|
+
Object.defineProperty(newObj, key, desc);
|
|
79
|
+
} else {
|
|
80
|
+
newObj[key] = obj[key];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
newObj.default = obj;
|
|
85
|
+
if (cache) {
|
|
86
|
+
cache.set(obj, newObj);
|
|
87
|
+
}
|
|
88
|
+
return newObj;
|
|
89
|
+
}
|
|
90
|
+
var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
|
|
91
|
+
var f, y, t, g, _ = {
|
|
92
|
+
label: 0,
|
|
93
|
+
sent: function() {
|
|
94
|
+
if (t[0] & 1) throw t[1];
|
|
95
|
+
return t[1];
|
|
96
|
+
},
|
|
97
|
+
trys: [],
|
|
98
|
+
ops: []
|
|
99
|
+
};
|
|
100
|
+
return g = {
|
|
101
|
+
next: verb(0),
|
|
102
|
+
"throw": verb(1),
|
|
103
|
+
"return": verb(2)
|
|
104
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
105
|
+
return this;
|
|
106
|
+
}), g;
|
|
107
|
+
function verb(n) {
|
|
108
|
+
return function(v) {
|
|
109
|
+
return step([
|
|
110
|
+
n,
|
|
111
|
+
v
|
|
112
|
+
]);
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function step(op) {
|
|
116
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
117
|
+
while(_)try {
|
|
118
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
119
|
+
if (y = 0, t) op = [
|
|
120
|
+
op[0] & 2,
|
|
121
|
+
t.value
|
|
122
|
+
];
|
|
123
|
+
switch(op[0]){
|
|
124
|
+
case 0:
|
|
125
|
+
case 1:
|
|
126
|
+
t = op;
|
|
127
|
+
break;
|
|
128
|
+
case 4:
|
|
129
|
+
_.label++;
|
|
130
|
+
return {
|
|
131
|
+
value: op[1],
|
|
132
|
+
done: false
|
|
133
|
+
};
|
|
134
|
+
case 5:
|
|
135
|
+
_.label++;
|
|
136
|
+
y = op[1];
|
|
137
|
+
op = [
|
|
138
|
+
0
|
|
139
|
+
];
|
|
140
|
+
continue;
|
|
141
|
+
case 7:
|
|
142
|
+
op = _.ops.pop();
|
|
143
|
+
_.trys.pop();
|
|
144
|
+
continue;
|
|
145
|
+
default:
|
|
146
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
147
|
+
_ = 0;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
151
|
+
_.label = op[1];
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
155
|
+
_.label = t[1];
|
|
156
|
+
t = op;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
if (t && _.label < t[2]) {
|
|
160
|
+
_.label = t[2];
|
|
161
|
+
_.ops.push(op);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (t[2]) _.ops.pop();
|
|
165
|
+
_.trys.pop();
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
op = body.call(thisArg, _);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
op = [
|
|
171
|
+
6,
|
|
172
|
+
e
|
|
173
|
+
];
|
|
174
|
+
y = 0;
|
|
175
|
+
} finally{
|
|
176
|
+
f = t = 0;
|
|
177
|
+
}
|
|
178
|
+
if (op[0] & 5) throw op[1];
|
|
179
|
+
return {
|
|
180
|
+
value: op[0] ? op[1] : void 0,
|
|
181
|
+
done: true
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
var _default = parse;
|
|
186
|
+
function parse(path, $refs, options) {
|
|
187
|
+
return _parse.apply(this, arguments);
|
|
188
|
+
}
|
|
189
|
+
function _parse() {
|
|
190
|
+
_parse = /**
|
|
191
|
+
* Reads and parses the specified file path or URL.
|
|
192
|
+
*
|
|
193
|
+
* @param {string} path - This path MUST already be resolved, since `read` doesn't know the resolution context
|
|
194
|
+
* @param {$Refs} $refs
|
|
195
|
+
* @param {$RefParserOptions} options
|
|
196
|
+
*
|
|
197
|
+
* @returns {Promise}
|
|
198
|
+
* The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
|
|
199
|
+
*/ _asyncToGenerator(function(path, $refs, options) {
|
|
200
|
+
var $ref, file, resolver, parser, err;
|
|
201
|
+
return __generator(this, function(_state) {
|
|
202
|
+
switch(_state.label){
|
|
203
|
+
case 0:
|
|
204
|
+
// Remove the URL fragment, if any
|
|
205
|
+
path = _urlJs.stripHash(path);
|
|
206
|
+
$ref = $refs._add(path);
|
|
207
|
+
file = {
|
|
208
|
+
url: path,
|
|
209
|
+
extension: _urlJs.getExtension(path)
|
|
210
|
+
};
|
|
211
|
+
_state.label = 1;
|
|
212
|
+
case 1:
|
|
213
|
+
_state.trys.push([
|
|
214
|
+
1,
|
|
215
|
+
4,
|
|
216
|
+
,
|
|
217
|
+
5
|
|
218
|
+
]);
|
|
219
|
+
return [
|
|
220
|
+
4,
|
|
221
|
+
readFile(file, options, $refs)
|
|
222
|
+
];
|
|
223
|
+
case 2:
|
|
224
|
+
resolver = _state.sent();
|
|
225
|
+
$ref.pathType = resolver.plugin.name;
|
|
226
|
+
file.data = resolver.result;
|
|
227
|
+
return [
|
|
228
|
+
4,
|
|
229
|
+
parseFile(file, options, $refs)
|
|
230
|
+
];
|
|
231
|
+
case 3:
|
|
232
|
+
parser = _state.sent();
|
|
233
|
+
$ref.value = parser.result;
|
|
234
|
+
return [
|
|
235
|
+
2,
|
|
236
|
+
parser.result
|
|
237
|
+
];
|
|
238
|
+
case 4:
|
|
239
|
+
err = _state.sent();
|
|
240
|
+
if ((0, _errorsJs.isHandledError)(err)) {
|
|
241
|
+
$ref.value = err;
|
|
242
|
+
}
|
|
243
|
+
throw err;
|
|
244
|
+
case 5:
|
|
245
|
+
return [
|
|
246
|
+
2
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
return _parse.apply(this, arguments);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Reads the given file, using the configured resolver plugins
|
|
255
|
+
*
|
|
256
|
+
* @param {object} file - An object containing information about the referenced file
|
|
257
|
+
* @param {string} file.url - The full URL of the referenced file
|
|
258
|
+
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
259
|
+
* @param {$RefParserOptions} options
|
|
260
|
+
*
|
|
261
|
+
* @returns {Promise}
|
|
262
|
+
* The promise resolves with the raw file contents and the resolver that was used.
|
|
263
|
+
*/ function readFile(file, options, $refs) {
|
|
264
|
+
return new Promise(function(resolve, reject) {
|
|
265
|
+
var onError = function onError(err) {
|
|
266
|
+
if (!err && options.continueOnError) {
|
|
267
|
+
// No resolver could be matched
|
|
268
|
+
reject(new _errorsJs.UnmatchedResolverError(file.url));
|
|
269
|
+
} else if (!err || !("error" in err)) {
|
|
270
|
+
// Throw a generic, friendly error.
|
|
271
|
+
reject(_ono.ono.syntax('Unable to resolve $ref pointer "'.concat(file.url, '"')));
|
|
272
|
+
} else if (_instanceof(err.error, _errorsJs.ResolverError)) {
|
|
273
|
+
reject(err.error);
|
|
274
|
+
} else {
|
|
275
|
+
reject(new _errorsJs.ResolverError(err, file.url));
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
// console.log('Reading %s', file.url);
|
|
279
|
+
// Find the resolvers that can read this file
|
|
280
|
+
var resolvers = _pluginsJs.all(options.resolve);
|
|
281
|
+
resolvers = _pluginsJs.filter(resolvers, "canRead", file);
|
|
282
|
+
// Run the resolvers, in order, until one of them succeeds
|
|
283
|
+
_pluginsJs.sort(resolvers);
|
|
284
|
+
_pluginsJs.run(resolvers, "read", file, $refs).then(resolve, onError);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Parses the given file's contents, using the configured parser plugins.
|
|
289
|
+
*
|
|
290
|
+
* @param {object} file - An object containing information about the referenced file
|
|
291
|
+
* @param {string} file.url - The full URL of the referenced file
|
|
292
|
+
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
293
|
+
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
294
|
+
* @param {$RefParserOptions} options
|
|
295
|
+
*
|
|
296
|
+
* @returns {Promise}
|
|
297
|
+
* The promise resolves with the parsed file contents and the parser that was used.
|
|
298
|
+
*/ function parseFile(file, options, $refs) {
|
|
299
|
+
return new Promise(function(resolve, reject) {
|
|
300
|
+
var onParsed = function onParsed(parser) {
|
|
301
|
+
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
|
|
302
|
+
reject(_ono.ono.syntax('Error parsing "'.concat(file.url, '" as ').concat(parser.plugin.name, ". \nParsed value is empty")));
|
|
303
|
+
} else {
|
|
304
|
+
resolve(parser);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
var onError = function onError(err) {
|
|
308
|
+
if (!err && options.continueOnError) {
|
|
309
|
+
// No resolver could be matched
|
|
310
|
+
reject(new _errorsJs.UnmatchedParserError(file.url));
|
|
311
|
+
} else if (!err || !("error" in err)) {
|
|
312
|
+
reject(_ono.ono.syntax("Unable to parse ".concat(file.url)));
|
|
313
|
+
} else if (_instanceof(err.error, _errorsJs.ParserError)) {
|
|
314
|
+
reject(err.error);
|
|
315
|
+
} else {
|
|
316
|
+
reject(new _errorsJs.ParserError(err.error.message, file.url));
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
// console.log('Parsing %s', file.url);
|
|
320
|
+
// Find the parsers that can read this file type.
|
|
321
|
+
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
|
|
322
|
+
// This handles situations where the file IS a supported type, just with an unknown extension.
|
|
323
|
+
var allParsers = _pluginsJs.all(options.parse);
|
|
324
|
+
var filteredParsers = _pluginsJs.filter(allParsers, "canParse", file);
|
|
325
|
+
var parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
|
|
326
|
+
// Run the parsers, in order, until one of them succeeds
|
|
327
|
+
_pluginsJs.sort(parsers);
|
|
328
|
+
_pluginsJs.run(parsers, "parse", file, $refs).then(onParsed, onError);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Determines whether the parsed value is "empty".
|
|
333
|
+
*
|
|
334
|
+
* @param {*} value
|
|
335
|
+
* @returns {boolean}
|
|
336
|
+
*/ function isEmpty(value) {
|
|
337
|
+
return value === undefined || typeof value === "object" && Object.keys(value).length === 0 || typeof value === "string" && value.trim().length === 0 || Buffer.isBuffer(value) && value.length === 0;
|
|
338
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
|
|
12
|
+
var _default = {
|
|
13
|
+
/**
|
|
14
|
+
* The order that this parser will run, in relation to other parsers.
|
|
15
|
+
*
|
|
16
|
+
* @type {number}
|
|
17
|
+
*/ order: 400,
|
|
18
|
+
/**
|
|
19
|
+
* Whether to allow "empty" files (zero bytes).
|
|
20
|
+
*
|
|
21
|
+
* @type {boolean}
|
|
22
|
+
*/ allowEmpty: true,
|
|
23
|
+
/**
|
|
24
|
+
* Determines whether this parser can parse a given file reference.
|
|
25
|
+
* Parsers that return true will be tried, in order, until one successfully parses the file.
|
|
26
|
+
* Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
|
|
27
|
+
* every parser will be tried.
|
|
28
|
+
*
|
|
29
|
+
* @param {object} file - An object containing information about the referenced file
|
|
30
|
+
* @param {string} file.url - The full URL of the referenced file
|
|
31
|
+
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
32
|
+
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/ canParse: function canParse(file) {
|
|
35
|
+
// Use this parser if the file is a Buffer, and has a known binary extension
|
|
36
|
+
return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url);
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* Parses the given data as a Buffer (byte array).
|
|
40
|
+
*
|
|
41
|
+
* @param {object} file - An object containing information about the referenced file
|
|
42
|
+
* @param {string} file.url - The full URL of the referenced file
|
|
43
|
+
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
44
|
+
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
45
|
+
* @returns {Buffer}
|
|
46
|
+
*/ parse: function parse(file) {
|
|
47
|
+
if (Buffer.isBuffer(file.data)) {
|
|
48
|
+
return file.data;
|
|
49
|
+
} else {
|
|
50
|
+
// This will reject if data is anything other than a string or typed array
|
|
51
|
+
return Buffer.from(file.data);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|