@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.
Files changed (46) hide show
  1. package/README.md +4 -8
  2. package/cjs/bundle.js +304 -0
  3. package/cjs/dereference.js +258 -0
  4. package/cjs/index.js +603 -0
  5. package/cjs/normalize-args.js +64 -0
  6. package/cjs/options.js +125 -0
  7. package/cjs/package.json +3 -0
  8. package/cjs/parse.js +338 -0
  9. package/cjs/parsers/binary.js +54 -0
  10. package/cjs/parsers/json.js +199 -0
  11. package/cjs/parsers/text.js +61 -0
  12. package/cjs/parsers/yaml.js +239 -0
  13. package/cjs/pointer.js +290 -0
  14. package/cjs/ref.js +333 -0
  15. package/cjs/refs.js +214 -0
  16. package/cjs/resolve-external.js +333 -0
  17. package/cjs/resolvers/file.js +106 -0
  18. package/cjs/resolvers/http.js +184 -0
  19. package/cjs/util/errors.js +401 -0
  20. package/cjs/util/plugins.js +159 -0
  21. package/cjs/util/projectDir.cjs +6 -0
  22. package/cjs/util/url.js +228 -0
  23. package/dist/lib/bundle.js +17 -7
  24. package/dist/lib/dereference.js +20 -8
  25. package/dist/lib/index.d.ts +6 -6
  26. package/dist/lib/index.js +17 -7
  27. package/dist/lib/options.d.ts +9 -2
  28. package/dist/lib/parse.d.ts +1 -1
  29. package/dist/lib/parse.js +17 -7
  30. package/dist/lib/pointer.js +25 -9
  31. package/dist/lib/refs.js +17 -7
  32. package/dist/lib/resolve-external.js +17 -7
  33. package/dist/lib/resolvers/file.js +17 -7
  34. package/dist/lib/resolvers/http.js +17 -7
  35. package/dist/lib/util/errors.d.ts +6 -2
  36. package/dist/lib/util/errors.js +7 -3
  37. package/dist/lib/util/plugins.d.ts +2 -2
  38. package/dist/lib/util/url.js +20 -9
  39. package/lib/dereference.ts +4 -1
  40. package/lib/index.ts +6 -12
  41. package/lib/options.ts +7 -0
  42. package/lib/pointer.ts +13 -2
  43. package/lib/util/errors.ts +14 -5
  44. package/lib/util/plugins.ts +6 -7
  45. package/lib/util/url.ts +3 -2
  46. package/package.json +31 -31
package/cjs/refs.js ADDED
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, /**
6
+ * This class is a map of JSON references and their resolved values.
7
+ */ "default", {
8
+ enumerable: true,
9
+ get: function() {
10
+ return $Refs;
11
+ }
12
+ });
13
+ var _ono = require("@jsdevtools/ono");
14
+ var _refJs = /*#__PURE__*/ _interopRequireDefault(require("./ref.js"));
15
+ var _urlJs = /*#__PURE__*/ _interopRequireWildcard(require("./util/url.js"));
16
+ function _interopRequireDefault(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ function _getRequireWildcardCache(nodeInterop) {
22
+ if (typeof WeakMap !== "function") return null;
23
+ var cacheBabelInterop = new WeakMap();
24
+ var cacheNodeInterop = new WeakMap();
25
+ return (_getRequireWildcardCache = function(nodeInterop) {
26
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
27
+ })(nodeInterop);
28
+ }
29
+ function _interopRequireWildcard(obj, nodeInterop) {
30
+ if (!nodeInterop && obj && obj.__esModule) {
31
+ return obj;
32
+ }
33
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
34
+ return {
35
+ default: obj
36
+ };
37
+ }
38
+ var cache = _getRequireWildcardCache(nodeInterop);
39
+ if (cache && cache.has(obj)) {
40
+ return cache.get(obj);
41
+ }
42
+ var newObj = {};
43
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
44
+ for(var key in obj){
45
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
46
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
47
+ if (desc && (desc.get || desc.set)) {
48
+ Object.defineProperty(newObj, key, desc);
49
+ } else {
50
+ newObj[key] = obj[key];
51
+ }
52
+ }
53
+ }
54
+ newObj.default = obj;
55
+ if (cache) {
56
+ cache.set(obj, newObj);
57
+ }
58
+ return newObj;
59
+ }
60
+ var isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
61
+ var getPathFromOs = function(filePath) {
62
+ return isWindows ? filePath.replace(/\\/g, "/") : filePath;
63
+ };
64
+ function $Refs() {
65
+ /**
66
+ * Indicates whether the schema contains any circular references.
67
+ *
68
+ * @type {boolean}
69
+ */ this.circular = false;
70
+ /**
71
+ * A map of paths/urls to {@link $Ref} objects
72
+ *
73
+ * @type {object}
74
+ * @protected
75
+ */ this._$refs = {};
76
+ /**
77
+ * The {@link $Ref} object that is the root of the JSON schema.
78
+ *
79
+ * @type {$Ref}
80
+ * @protected
81
+ */ this._root$Ref = null;
82
+ }
83
+ /**
84
+ * Returns the paths of all the files/URLs that are referenced by the JSON schema,
85
+ * including the schema itself.
86
+ *
87
+ * @param {...string|string[]} [types] - Only return paths of the given types ("file", "http", etc.)
88
+ * @returns {string[]}
89
+ */ $Refs.prototype.paths = function(types) {
90
+ var paths = getPaths(this._$refs, arguments);
91
+ return paths.map(function(path) {
92
+ return getPathFromOs(path.decoded);
93
+ });
94
+ };
95
+ /**
96
+ * Returns the map of JSON references and their resolved values.
97
+ *
98
+ * @param {...string|string[]} [types] - Only return references of the given types ("file", "http", etc.)
99
+ * @returns {object}
100
+ */ $Refs.prototype.values = function(types) {
101
+ var $refs = this._$refs;
102
+ var paths = getPaths($refs, arguments);
103
+ return paths.reduce(function(obj, path) {
104
+ obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
105
+ return obj;
106
+ }, {});
107
+ };
108
+ /**
109
+ * Returns a POJO (plain old JavaScript object) for serialization as JSON.
110
+ *
111
+ * @returns {object}
112
+ */ $Refs.prototype.toJSON = $Refs.prototype.values;
113
+ /**
114
+ * Determines whether the given JSON reference exists.
115
+ *
116
+ * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
117
+ * @param {$RefParserOptions} [options]
118
+ * @returns {boolean}
119
+ */ $Refs.prototype.exists = function(path, options) {
120
+ try {
121
+ this._resolve(path, "", options);
122
+ return true;
123
+ } catch (e) {
124
+ return false;
125
+ }
126
+ };
127
+ /**
128
+ * Resolves the given JSON reference and returns the resolved value.
129
+ *
130
+ * @param {string} path - The path being resolved, with a JSON pointer in the hash
131
+ * @param {$RefParserOptions} [options]
132
+ * @returns {*} - Returns the resolved value
133
+ */ $Refs.prototype.get = function(path, options) {
134
+ return this._resolve(path, "", options).value;
135
+ };
136
+ /**
137
+ * Sets the value of a nested property within this {@link $Ref#value}.
138
+ * If the property, or any of its parents don't exist, they will be created.
139
+ *
140
+ * @param {string} path - The path of the property to set, optionally with a JSON pointer in the hash
141
+ * @param {*} value - The value to assign
142
+ */ $Refs.prototype.set = function(path, value) {
143
+ var absPath = _urlJs.resolve(this._root$Ref.path, path);
144
+ var withoutHash = _urlJs.stripHash(absPath);
145
+ var $ref = this._$refs[withoutHash];
146
+ if (!$ref) {
147
+ throw (0, _ono.ono)('Error resolving $ref pointer "'.concat(path, '". \n"').concat(withoutHash, '" not found.'));
148
+ }
149
+ $ref.set(absPath, value);
150
+ };
151
+ /**
152
+ * Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
153
+ *
154
+ * @param {string} path - The file path or URL of the referenced file
155
+ */ $Refs.prototype._add = function(path) {
156
+ var withoutHash = _urlJs.stripHash(path);
157
+ var $ref = new _refJs.default();
158
+ $ref.path = withoutHash;
159
+ $ref.$refs = this;
160
+ this._$refs[withoutHash] = $ref;
161
+ this._root$Ref = this._root$Ref || $ref;
162
+ return $ref;
163
+ };
164
+ /**
165
+ * Resolves the given JSON reference.
166
+ *
167
+ * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
168
+ * @param {string} pathFromRoot - The path of `obj` from the schema root
169
+ * @param {$RefParserOptions} [options]
170
+ * @returns {Pointer}
171
+ * @protected
172
+ */ $Refs.prototype._resolve = function(path, pathFromRoot, options) {
173
+ var absPath = _urlJs.resolve(this._root$Ref.path, path);
174
+ var withoutHash = _urlJs.stripHash(absPath);
175
+ var $ref = this._$refs[withoutHash];
176
+ if (!$ref) {
177
+ throw (0, _ono.ono)('Error resolving $ref pointer "'.concat(path, '". \n"').concat(withoutHash, '" not found.'));
178
+ }
179
+ return $ref.resolve(absPath, options, path, pathFromRoot);
180
+ };
181
+ /**
182
+ * Returns the specified {@link $Ref} object, or undefined.
183
+ *
184
+ * @param {string} path - The path being resolved, optionally with a JSON pointer in the hash
185
+ * @returns {$Ref|undefined}
186
+ * @protected
187
+ */ $Refs.prototype._get$Ref = function(path) {
188
+ path = _urlJs.resolve(this._root$Ref.path, path);
189
+ var withoutHash = _urlJs.stripHash(path);
190
+ return this._$refs[withoutHash];
191
+ };
192
+ /**
193
+ * Returns the encoded and decoded paths keys of the given object.
194
+ *
195
+ * @param {object} $refs - The object whose keys are URL-encoded paths
196
+ * @param {...string|string[]} [types] - Only return paths of the given types ("file", "http", etc.)
197
+ * @returns {object[]}
198
+ */ function getPaths($refs, types) {
199
+ var paths = Object.keys($refs);
200
+ // Filter the paths by type
201
+ types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
202
+ if (types.length > 0 && types[0]) {
203
+ paths = paths.filter(function(key) {
204
+ return types.indexOf($refs[key].pathType) !== -1;
205
+ });
206
+ }
207
+ // Decode local filesystem paths
208
+ return paths.map(function(path) {
209
+ return {
210
+ encoded: path,
211
+ decoded: $refs[path].pathType === "file" ? _urlJs.toFileSystemPath(path, true) : path
212
+ };
213
+ });
214
+ }
@@ -0,0 +1,333 @@
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 _refJs = /*#__PURE__*/ _interopRequireDefault(require("./ref.js"));
12
+ var _pointerJs = /*#__PURE__*/ _interopRequireDefault(require("./pointer.js"));
13
+ var _parseJs = /*#__PURE__*/ _interopRequireDefault(require("./parse.js"));
14
+ var _urlJs = /*#__PURE__*/ _interopRequireWildcard(require("./util/url.js"));
15
+ var _errorsJs = require("./util/errors.js");
16
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
17
+ try {
18
+ var info = gen[key](arg);
19
+ var value = info.value;
20
+ } catch (error) {
21
+ reject(error);
22
+ return;
23
+ }
24
+ if (info.done) {
25
+ resolve(value);
26
+ } else {
27
+ Promise.resolve(value).then(_next, _throw);
28
+ }
29
+ }
30
+ function _asyncToGenerator(fn) {
31
+ return function() {
32
+ var self = this, args = arguments;
33
+ return new Promise(function(resolve, reject) {
34
+ var gen = fn.apply(self, args);
35
+ function _next(value) {
36
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
37
+ }
38
+ function _throw(err) {
39
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
40
+ }
41
+ _next(undefined);
42
+ });
43
+ };
44
+ }
45
+ function _interopRequireDefault(obj) {
46
+ return obj && obj.__esModule ? obj : {
47
+ default: obj
48
+ };
49
+ }
50
+ function _getRequireWildcardCache(nodeInterop) {
51
+ if (typeof WeakMap !== "function") return null;
52
+ var cacheBabelInterop = new WeakMap();
53
+ var cacheNodeInterop = new WeakMap();
54
+ return (_getRequireWildcardCache = function(nodeInterop) {
55
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
56
+ })(nodeInterop);
57
+ }
58
+ function _interopRequireWildcard(obj, nodeInterop) {
59
+ if (!nodeInterop && obj && obj.__esModule) {
60
+ return obj;
61
+ }
62
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
63
+ return {
64
+ default: obj
65
+ };
66
+ }
67
+ var cache = _getRequireWildcardCache(nodeInterop);
68
+ if (cache && cache.has(obj)) {
69
+ return cache.get(obj);
70
+ }
71
+ var newObj = {};
72
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
73
+ for(var key in obj){
74
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
75
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
76
+ if (desc && (desc.get || desc.set)) {
77
+ Object.defineProperty(newObj, key, desc);
78
+ } else {
79
+ newObj[key] = obj[key];
80
+ }
81
+ }
82
+ }
83
+ newObj.default = obj;
84
+ if (cache) {
85
+ cache.set(obj, newObj);
86
+ }
87
+ return newObj;
88
+ }
89
+ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
90
+ var f, y, t, g, _ = {
91
+ label: 0,
92
+ sent: function() {
93
+ if (t[0] & 1) throw t[1];
94
+ return t[1];
95
+ },
96
+ trys: [],
97
+ ops: []
98
+ };
99
+ return g = {
100
+ next: verb(0),
101
+ "throw": verb(1),
102
+ "return": verb(2)
103
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
104
+ return this;
105
+ }), g;
106
+ function verb(n) {
107
+ return function(v) {
108
+ return step([
109
+ n,
110
+ v
111
+ ]);
112
+ };
113
+ }
114
+ function step(op) {
115
+ if (f) throw new TypeError("Generator is already executing.");
116
+ while(_)try {
117
+ 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;
118
+ if (y = 0, t) op = [
119
+ op[0] & 2,
120
+ t.value
121
+ ];
122
+ switch(op[0]){
123
+ case 0:
124
+ case 1:
125
+ t = op;
126
+ break;
127
+ case 4:
128
+ _.label++;
129
+ return {
130
+ value: op[1],
131
+ done: false
132
+ };
133
+ case 5:
134
+ _.label++;
135
+ y = op[1];
136
+ op = [
137
+ 0
138
+ ];
139
+ continue;
140
+ case 7:
141
+ op = _.ops.pop();
142
+ _.trys.pop();
143
+ continue;
144
+ default:
145
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
146
+ _ = 0;
147
+ continue;
148
+ }
149
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
150
+ _.label = op[1];
151
+ break;
152
+ }
153
+ if (op[0] === 6 && _.label < t[1]) {
154
+ _.label = t[1];
155
+ t = op;
156
+ break;
157
+ }
158
+ if (t && _.label < t[2]) {
159
+ _.label = t[2];
160
+ _.ops.push(op);
161
+ break;
162
+ }
163
+ if (t[2]) _.ops.pop();
164
+ _.trys.pop();
165
+ continue;
166
+ }
167
+ op = body.call(thisArg, _);
168
+ } catch (e) {
169
+ op = [
170
+ 6,
171
+ e
172
+ ];
173
+ y = 0;
174
+ } finally{
175
+ f = t = 0;
176
+ }
177
+ if (op[0] & 5) throw op[1];
178
+ return {
179
+ value: op[0] ? op[1] : void 0,
180
+ done: true
181
+ };
182
+ }
183
+ };
184
+ var _default = resolveExternal;
185
+ /**
186
+ * Crawls the JSON schema, finds all external JSON references, and resolves their values.
187
+ * This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
188
+ *
189
+ * NOTE: We only care about EXTERNAL references here. INTERNAL references are only relevant when dereferencing.
190
+ *
191
+ * @param {$RefParser} parser
192
+ * @param {$RefParserOptions} options
193
+ *
194
+ * @returns {Promise}
195
+ * The promise resolves once all JSON references in the schema have been resolved,
196
+ * including nested references that are contained in externally-referenced files.
197
+ */ function resolveExternal(parser, options) {
198
+ if (!options.resolve.external) {
199
+ // Nothing to resolve, so exit early
200
+ return Promise.resolve();
201
+ }
202
+ try {
203
+ // console.log('Resolving $ref pointers in %s', parser.$refs._root$Ref.path);
204
+ var promises = crawl(parser.schema, parser.$refs._root$Ref.path + "#", parser.$refs, options);
205
+ return Promise.all(promises);
206
+ } catch (e) {
207
+ return Promise.reject(e);
208
+ }
209
+ }
210
+ /**
211
+ * Recursively crawls the given value, and resolves any external JSON references.
212
+ *
213
+ * @param {*} obj - The value to crawl. If it's not an object or array, it will be ignored.
214
+ * @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
215
+ * @param {$Refs} $refs
216
+ * @param {$RefParserOptions} options
217
+ * @param {Set} seen - Internal.
218
+ *
219
+ * @returns {Promise[]}
220
+ * Returns an array of promises. There will be one promise for each JSON reference in `obj`.
221
+ * If `obj` does not contain any JSON references, then the array will be empty.
222
+ * If any of the JSON references point to files that contain additional JSON references,
223
+ * then the corresponding promise will internally reference an array of promises.
224
+ */ function crawl(obj, path, $refs, options, seen) {
225
+ seen = seen || new Set();
226
+ var promises = [];
227
+ if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
228
+ seen.add(obj); // Track previously seen objects to avoid infinite recursion
229
+ if (_refJs.default.isExternal$Ref(obj)) {
230
+ promises.push(resolve$Ref(obj, path, $refs, options));
231
+ } else {
232
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
233
+ try {
234
+ for(var _iterator = Object.keys(obj)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
235
+ var key = _step.value;
236
+ var keyPath = _pointerJs.default.join(path, key);
237
+ var value = obj[key];
238
+ if (_refJs.default.isExternal$Ref(value)) {
239
+ promises.push(resolve$Ref(value, keyPath, $refs, options));
240
+ } else {
241
+ promises = promises.concat(crawl(value, keyPath, $refs, options, seen));
242
+ }
243
+ }
244
+ } catch (err) {
245
+ _didIteratorError = true;
246
+ _iteratorError = err;
247
+ } finally{
248
+ try {
249
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
250
+ _iterator.return();
251
+ }
252
+ } finally{
253
+ if (_didIteratorError) {
254
+ throw _iteratorError;
255
+ }
256
+ }
257
+ }
258
+ }
259
+ }
260
+ return promises;
261
+ }
262
+ function resolve$Ref($ref, path, $refs, options) {
263
+ return _resolve$Ref.apply(this, arguments);
264
+ }
265
+ function _resolve$Ref() {
266
+ _resolve$Ref = /**
267
+ * Resolves the given JSON Reference, and then crawls the resulting value.
268
+ *
269
+ * @param {{$ref: string}} $ref - The JSON Reference to resolve
270
+ * @param {string} path - The full path of `$ref`, possibly with a JSON Pointer in the hash
271
+ * @param {$Refs} $refs
272
+ * @param {$RefParserOptions} options
273
+ *
274
+ * @returns {Promise}
275
+ * The promise resolves once all JSON references in the object have been resolved,
276
+ * including nested references that are contained in externally-referenced files.
277
+ */ _asyncToGenerator(function($ref, path, $refs, options) {
278
+ var resolvedPath, withoutHash, result, promises, err;
279
+ return __generator(this, function(_state) {
280
+ switch(_state.label){
281
+ case 0:
282
+ resolvedPath = _urlJs.resolve(path, $ref.$ref);
283
+ withoutHash = _urlJs.stripHash(resolvedPath);
284
+ // Do we already have this $ref?
285
+ $ref = $refs._$refs[withoutHash];
286
+ if ($ref) {
287
+ // We've already parsed this $ref, so use the existing value
288
+ return [
289
+ 2,
290
+ Promise.resolve($ref.value)
291
+ ];
292
+ }
293
+ _state.label = 1;
294
+ case 1:
295
+ _state.trys.push([
296
+ 1,
297
+ 3,
298
+ ,
299
+ 4
300
+ ]);
301
+ return [
302
+ 4,
303
+ (0, _parseJs.default)(resolvedPath, $refs, options)
304
+ ];
305
+ case 2:
306
+ result = _state.sent();
307
+ promises = crawl(result, withoutHash + "#", $refs, options);
308
+ return [
309
+ 2,
310
+ Promise.all(promises)
311
+ ];
312
+ case 3:
313
+ err = _state.sent();
314
+ if (!options.continueOnError || !(0, _errorsJs.isHandledError)(err)) {
315
+ throw err;
316
+ }
317
+ if ($refs._$refs[withoutHash]) {
318
+ err.source = decodeURI(_urlJs.stripHash(path));
319
+ err.path = _urlJs.safePointerToPath(_urlJs.getHash(path));
320
+ }
321
+ return [
322
+ 2,
323
+ []
324
+ ];
325
+ case 4:
326
+ return [
327
+ 2
328
+ ];
329
+ }
330
+ });
331
+ });
332
+ return _resolve$Ref.apply(this, arguments);
333
+ }
@@ -0,0 +1,106 @@
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 _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
12
+ var _ono = require("@jsdevtools/ono");
13
+ var _urlJs = /*#__PURE__*/ _interopRequireWildcard(require("../util/url.js"));
14
+ var _errorsJs = require("../util/errors.js");
15
+ function _interopRequireDefault(obj) {
16
+ return obj && obj.__esModule ? obj : {
17
+ default: obj
18
+ };
19
+ }
20
+ function _getRequireWildcardCache(nodeInterop) {
21
+ if (typeof WeakMap !== "function") return null;
22
+ var cacheBabelInterop = new WeakMap();
23
+ var cacheNodeInterop = new WeakMap();
24
+ return (_getRequireWildcardCache = function(nodeInterop) {
25
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
26
+ })(nodeInterop);
27
+ }
28
+ function _interopRequireWildcard(obj, nodeInterop) {
29
+ if (!nodeInterop && obj && obj.__esModule) {
30
+ return obj;
31
+ }
32
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
33
+ return {
34
+ default: obj
35
+ };
36
+ }
37
+ var cache = _getRequireWildcardCache(nodeInterop);
38
+ if (cache && cache.has(obj)) {
39
+ return cache.get(obj);
40
+ }
41
+ var newObj = {};
42
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
43
+ for(var key in obj){
44
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
45
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
46
+ if (desc && (desc.get || desc.set)) {
47
+ Object.defineProperty(newObj, key, desc);
48
+ } else {
49
+ newObj[key] = obj[key];
50
+ }
51
+ }
52
+ }
53
+ newObj.default = obj;
54
+ if (cache) {
55
+ cache.set(obj, newObj);
56
+ }
57
+ return newObj;
58
+ }
59
+ var _default = {
60
+ /**
61
+ * The order that this resolver will run, in relation to other resolvers.
62
+ *
63
+ * @type {number}
64
+ */ order: 100,
65
+ /**
66
+ * Determines whether this resolver can read a given file reference.
67
+ * Resolvers that return true will be tried, in order, until one successfully resolves the file.
68
+ * Resolvers that return false will not be given a chance to resolve the file.
69
+ *
70
+ * @param {object} file - An object containing information about the referenced file
71
+ * @param {string} file.url - The full URL of the referenced file
72
+ * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
73
+ * @returns {boolean}
74
+ */ canRead: function canRead(file) {
75
+ return _urlJs.isFileSystemPath(file.url);
76
+ },
77
+ /**
78
+ * Reads the given file and returns its raw contents as a Buffer.
79
+ *
80
+ * @param {object} file - An object containing information about the referenced file
81
+ * @param {string} file.url - The full URL of the referenced file
82
+ * @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
83
+ * @returns {Promise<Buffer>}
84
+ */ read: function read(file) {
85
+ return new Promise(function(resolve, reject) {
86
+ var path;
87
+ try {
88
+ path = _urlJs.toFileSystemPath(file.url);
89
+ } catch (err) {
90
+ reject(new _errorsJs.ResolverError(_ono.ono.uri(err, "Malformed URI: ".concat(file.url)), file.url));
91
+ }
92
+ // console.log('Opening file: %s', path);
93
+ try {
94
+ _fs.default.readFile(path, function(err, data) {
95
+ if (err) {
96
+ reject(new _errorsJs.ResolverError((0, _ono.ono)(err, 'Error opening file "'.concat(path, '"')), path));
97
+ } else {
98
+ resolve(data);
99
+ }
100
+ });
101
+ } catch (err1) {
102
+ reject(new _errorsJs.ResolverError((0, _ono.ono)(err1, 'Error opening file "'.concat(path, '"')), path));
103
+ }
104
+ });
105
+ }
106
+ };