@apidevtools/json-schema-ref-parser 10.0.0 → 10.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/lib/bundle.d.ts +10 -0
  2. package/dist/lib/bundle.js +265 -0
  3. package/dist/lib/dereference.d.ts +9 -0
  4. package/dist/lib/dereference.js +206 -0
  5. package/dist/lib/index.d.ts +206 -0
  6. package/dist/lib/index.js +223 -0
  7. package/dist/lib/normalize-args.d.ts +10 -0
  8. package/dist/lib/normalize-args.js +42 -0
  9. package/dist/lib/options.d.ts +77 -0
  10. package/dist/lib/options.js +119 -0
  11. package/dist/lib/parse.d.ts +8 -0
  12. package/dist/lib/parse.js +177 -0
  13. package/dist/lib/parsers/binary.d.ts +3 -0
  14. package/dist/lib/parsers/binary.js +35 -0
  15. package/dist/lib/parsers/json.d.ts +3 -0
  16. package/dist/lib/parsers/json.js +57 -0
  17. package/dist/lib/parsers/text.d.ts +3 -0
  18. package/dist/lib/parsers/text.js +42 -0
  19. package/dist/lib/parsers/yaml.d.ts +3 -0
  20. package/dist/lib/parsers/yaml.js +65 -0
  21. package/dist/lib/pointer.d.ts +87 -0
  22. package/dist/lib/pointer.js +256 -0
  23. package/dist/lib/ref.d.ts +181 -0
  24. package/dist/lib/ref.js +239 -0
  25. package/dist/lib/refs.d.ts +127 -0
  26. package/dist/lib/refs.js +223 -0
  27. package/dist/lib/resolve-external.d.ts +14 -0
  28. package/dist/lib/resolve-external.js +148 -0
  29. package/dist/lib/resolvers/file.d.ts +3 -0
  30. package/dist/lib/resolvers/file.js +76 -0
  31. package/dist/lib/resolvers/http.d.ts +3 -0
  32. package/dist/lib/resolvers/http.js +159 -0
  33. package/dist/lib/types/index.d.ts +104 -0
  34. package/dist/lib/types/index.js +2 -0
  35. package/dist/lib/util/errors.d.ts +50 -0
  36. package/dist/lib/util/errors.js +106 -0
  37. package/dist/lib/util/maybe.d.ts +3 -0
  38. package/dist/lib/util/maybe.js +24 -0
  39. package/dist/lib/util/next.d.ts +2 -0
  40. package/dist/lib/util/next.js +16 -0
  41. package/dist/lib/util/plugins.d.ts +36 -0
  42. package/dist/lib/util/plugins.js +144 -0
  43. package/dist/lib/util/url.d.ts +93 -0
  44. package/{cjs → dist/lib}/util/url.js +134 -102
  45. package/dist/vite.config.d.ts +2 -0
  46. package/dist/vite.config.js +23 -0
  47. package/lib/{bundle.js → bundle.ts} +105 -101
  48. package/lib/{dereference.js → dereference.ts} +113 -52
  49. package/lib/index.ts +413 -0
  50. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  51. package/lib/options.ts +202 -0
  52. package/lib/parse.ts +153 -0
  53. package/lib/parsers/binary.ts +39 -0
  54. package/lib/parsers/{json.js → json.ts} +9 -22
  55. package/lib/parsers/text.ts +46 -0
  56. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  57. package/lib/pointer.ts +296 -0
  58. package/lib/ref.ts +288 -0
  59. package/lib/refs.ts +238 -0
  60. package/lib/{resolve-external.js → resolve-external.ts} +39 -36
  61. package/lib/resolvers/file.ts +40 -0
  62. package/lib/resolvers/http.ts +136 -0
  63. package/lib/tsconfig.json +103 -0
  64. package/lib/types/index.ts +135 -0
  65. package/lib/util/errors.ts +141 -0
  66. package/lib/util/maybe.ts +22 -0
  67. package/lib/util/next.ts +13 -0
  68. package/lib/util/{plugins.js → plugins.ts} +58 -57
  69. package/lib/util/{url.js → url.ts} +64 -83
  70. package/package.json +44 -45
  71. package/cjs/bundle.js +0 -304
  72. package/cjs/dereference.js +0 -258
  73. package/cjs/index.js +0 -603
  74. package/cjs/normalize-args.js +0 -64
  75. package/cjs/options.js +0 -125
  76. package/cjs/package.json +0 -3
  77. package/cjs/parse.js +0 -338
  78. package/cjs/parsers/binary.js +0 -54
  79. package/cjs/parsers/json.js +0 -199
  80. package/cjs/parsers/text.js +0 -61
  81. package/cjs/parsers/yaml.js +0 -239
  82. package/cjs/pointer.js +0 -290
  83. package/cjs/ref.js +0 -333
  84. package/cjs/refs.js +0 -214
  85. package/cjs/resolve-external.js +0 -333
  86. package/cjs/resolvers/file.js +0 -106
  87. package/cjs/resolvers/http.js +0 -184
  88. package/cjs/util/errors.js +0 -401
  89. package/cjs/util/plugins.js +0 -159
  90. package/cjs/util/projectDir.cjs +0 -6
  91. package/lib/index.d.ts +0 -496
  92. package/lib/index.js +0 -290
  93. package/lib/options.js +0 -128
  94. package/lib/parse.js +0 -162
  95. package/lib/parsers/binary.js +0 -53
  96. package/lib/parsers/text.js +0 -64
  97. package/lib/pointer.js +0 -293
  98. package/lib/ref.js +0 -292
  99. package/lib/refs.js +0 -196
  100. package/lib/resolvers/file.js +0 -63
  101. package/lib/resolvers/http.js +0 -155
  102. package/lib/util/errors.js +0 -134
  103. package/lib/util/projectDir.cjs +0 -6
@@ -0,0 +1,127 @@
1
+ import $Ref from "./ref.js";
2
+ import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
3
+ import type { JSONSchema } from "./types/index.js";
4
+ import type $RefParserOptions from "./options.js";
5
+ interface $RefsMap {
6
+ [url: string]: $Ref;
7
+ }
8
+ /**
9
+ * When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
10
+ *
11
+ * This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
12
+ *
13
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html
14
+ */
15
+ export default class $Refs {
16
+ /**
17
+ * This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default.
18
+ *
19
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#circular
20
+ */
21
+ circular: boolean;
22
+ /**
23
+ * Returns the paths/URLs of all the files in your schema (including the main schema file).
24
+ *
25
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#pathstypes
26
+ *
27
+ * @param types (optional) Optionally only return certain types of paths ("file", "http", etc.)
28
+ */
29
+ paths(...types: string[]): string[];
30
+ /**
31
+ * Returns a map of paths/URLs and their correspond values.
32
+ *
33
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#valuestypes
34
+ *
35
+ * @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
36
+ */
37
+ values(...types: string[]): JSONSchema;
38
+ /**
39
+ * Returns `true` if the given path exists in the schema; otherwise, returns `false`
40
+ *
41
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#existsref
42
+ *
43
+ * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
44
+ */
45
+ /**
46
+ * Determines whether the given JSON reference exists.
47
+ *
48
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
49
+ * @param [options]
50
+ * @returns
51
+ */
52
+ exists(path: string, options: any): boolean;
53
+ /**
54
+ * Resolves the given JSON reference and returns the resolved value.
55
+ *
56
+ * @param path - The path being resolved, with a JSON pointer in the hash
57
+ * @param [options]
58
+ * @returns - Returns the resolved value
59
+ */
60
+ get(path: string, options?: $RefParserOptions): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type;
61
+ /**
62
+ * Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
63
+ *
64
+ * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
65
+ * @param value The value to assign. Can be anything (object, string, number, etc.)
66
+ */
67
+ set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type): void;
68
+ /**
69
+ * Returns the specified {@link $Ref} object, or undefined.
70
+ *
71
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
72
+ * @returns
73
+ * @protected
74
+ */
75
+ _get$Ref(path: any): $Ref;
76
+ /**
77
+ * Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
78
+ *
79
+ * @param path - The file path or URL of the referenced file
80
+ */
81
+ _add(path: string): $Ref;
82
+ /**
83
+ * Resolves the given JSON reference.
84
+ *
85
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
86
+ * @param pathFromRoot - The path of `obj` from the schema root
87
+ * @param [options]
88
+ * @returns
89
+ * @protected
90
+ */
91
+ _resolve(path: string, pathFromRoot: string, options?: any): import("./pointer.js").default | null;
92
+ /**
93
+ * A map of paths/urls to {@link $Ref} objects
94
+ *
95
+ * @type {object}
96
+ * @protected
97
+ */
98
+ _$refs: $RefsMap;
99
+ /**
100
+ * The {@link $Ref} object that is the root of the JSON schema.
101
+ *
102
+ * @type {$Ref}
103
+ * @protected
104
+ */
105
+ _root$Ref: $Ref;
106
+ constructor();
107
+ /**
108
+ * Returns the paths of all the files/URLs that are referenced by the JSON schema,
109
+ * including the schema itself.
110
+ *
111
+ * @param [types] - Only return paths of the given types ("file", "http", etc.)
112
+ * @returns
113
+ */
114
+ /**
115
+ * Returns the map of JSON references and their resolved values.
116
+ *
117
+ * @param [types] - Only return references of the given types ("file", "http", etc.)
118
+ * @returns
119
+ */
120
+ /**
121
+ * Returns a POJO (plain old JavaScript object) for serialization as JSON.
122
+ *
123
+ * @returns {object}
124
+ */
125
+ toJSON: (...types: string[]) => JSONSchema;
126
+ }
127
+ export {};
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const ono_1 = require("@jsdevtools/ono");
30
+ const ref_js_1 = __importDefault(require("./ref.js"));
31
+ const url = __importStar(require("./util/url.js"));
32
+ const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : "");
33
+ const getPathFromOs = (filePath) => (isWindows ? filePath.replace(/\\/g, "/") : filePath);
34
+ /**
35
+ * When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
36
+ *
37
+ * This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
38
+ *
39
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html
40
+ */
41
+ class $Refs {
42
+ /**
43
+ * Returns the paths/URLs of all the files in your schema (including the main schema file).
44
+ *
45
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#pathstypes
46
+ *
47
+ * @param types (optional) Optionally only return certain types of paths ("file", "http", etc.)
48
+ */
49
+ paths(...types) {
50
+ const paths = getPaths(this._$refs, types);
51
+ return paths.map((path) => {
52
+ return getPathFromOs(path.decoded);
53
+ });
54
+ }
55
+ /**
56
+ * Returns a map of paths/URLs and their correspond values.
57
+ *
58
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#valuestypes
59
+ *
60
+ * @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
61
+ */
62
+ values(...types) {
63
+ const $refs = this._$refs;
64
+ const paths = getPaths($refs, types);
65
+ return paths.reduce((obj, path) => {
66
+ obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
67
+ return obj;
68
+ }, {});
69
+ }
70
+ /**
71
+ * Returns `true` if the given path exists in the schema; otherwise, returns `false`
72
+ *
73
+ * See https://apitools.dev/json-schema-ref-parser/docs/refs.html#existsref
74
+ *
75
+ * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
76
+ */
77
+ /**
78
+ * Determines whether the given JSON reference exists.
79
+ *
80
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
81
+ * @param [options]
82
+ * @returns
83
+ */
84
+ exists(path, options) {
85
+ try {
86
+ this._resolve(path, "", options);
87
+ return true;
88
+ }
89
+ catch (e) {
90
+ return false;
91
+ }
92
+ }
93
+ /**
94
+ * Resolves the given JSON reference and returns the resolved value.
95
+ *
96
+ * @param path - The path being resolved, with a JSON pointer in the hash
97
+ * @param [options]
98
+ * @returns - Returns the resolved value
99
+ */
100
+ get(path, options) {
101
+ return this._resolve(path, "", options).value;
102
+ }
103
+ /**
104
+ * Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
105
+ *
106
+ * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
107
+ * @param value The value to assign. Can be anything (object, string, number, etc.)
108
+ */
109
+ set(path, value) {
110
+ const absPath = url.resolve(this._root$Ref.path, path);
111
+ const withoutHash = url.stripHash(absPath);
112
+ const $ref = this._$refs[withoutHash];
113
+ if (!$ref) {
114
+ throw (0, ono_1.ono)(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
115
+ }
116
+ $ref.set(absPath, value);
117
+ }
118
+ /**
119
+ * Returns the specified {@link $Ref} object, or undefined.
120
+ *
121
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
122
+ * @returns
123
+ * @protected
124
+ */
125
+ _get$Ref(path) {
126
+ path = url.resolve(this._root$Ref.path, path);
127
+ const withoutHash = url.stripHash(path);
128
+ return this._$refs[withoutHash];
129
+ }
130
+ /**
131
+ * Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
132
+ *
133
+ * @param path - The file path or URL of the referenced file
134
+ */
135
+ _add(path) {
136
+ const withoutHash = url.stripHash(path);
137
+ const $ref = new ref_js_1.default(this);
138
+ $ref.path = withoutHash;
139
+ this._$refs[withoutHash] = $ref;
140
+ this._root$Ref = this._root$Ref || $ref;
141
+ return $ref;
142
+ }
143
+ /**
144
+ * Resolves the given JSON reference.
145
+ *
146
+ * @param path - The path being resolved, optionally with a JSON pointer in the hash
147
+ * @param pathFromRoot - The path of `obj` from the schema root
148
+ * @param [options]
149
+ * @returns
150
+ * @protected
151
+ */
152
+ _resolve(path, pathFromRoot, options) {
153
+ const absPath = url.resolve(this._root$Ref.path, path);
154
+ const withoutHash = url.stripHash(absPath);
155
+ const $ref = this._$refs[withoutHash];
156
+ if (!$ref) {
157
+ throw (0, ono_1.ono)(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
158
+ }
159
+ return $ref.resolve(absPath, options, path, pathFromRoot);
160
+ }
161
+ constructor() {
162
+ /**
163
+ * A map of paths/urls to {@link $Ref} objects
164
+ *
165
+ * @type {object}
166
+ * @protected
167
+ */
168
+ this._$refs = {};
169
+ /**
170
+ * Returns the paths of all the files/URLs that are referenced by the JSON schema,
171
+ * including the schema itself.
172
+ *
173
+ * @param [types] - Only return paths of the given types ("file", "http", etc.)
174
+ * @returns
175
+ */
176
+ /**
177
+ * Returns the map of JSON references and their resolved values.
178
+ *
179
+ * @param [types] - Only return references of the given types ("file", "http", etc.)
180
+ * @returns
181
+ */
182
+ /**
183
+ * Returns a POJO (plain old JavaScript object) for serialization as JSON.
184
+ *
185
+ * @returns {object}
186
+ */
187
+ this.toJSON = this.values;
188
+ /**
189
+ * Indicates whether the schema contains any circular references.
190
+ *
191
+ * @type {boolean}
192
+ */
193
+ this.circular = false;
194
+ this._$refs = {};
195
+ // @ts-ignore
196
+ this._root$Ref = null;
197
+ }
198
+ }
199
+ exports.default = $Refs;
200
+ /**
201
+ * Returns the encoded and decoded paths keys of the given object.
202
+ *
203
+ * @param $refs - The object whose keys are URL-encoded paths
204
+ * @param [types] - Only return paths of the given types ("file", "http", etc.)
205
+ * @returns
206
+ */
207
+ function getPaths($refs, types) {
208
+ let paths = Object.keys($refs);
209
+ // Filter the paths by type
210
+ types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
211
+ if (types.length > 0 && types[0]) {
212
+ paths = paths.filter((key) => {
213
+ return types.includes($refs[key].pathType);
214
+ });
215
+ }
216
+ // Decode local filesystem paths
217
+ return paths.map((path) => {
218
+ return {
219
+ encoded: path,
220
+ decoded: $refs[path].pathType === "file" ? url.toFileSystemPath(path, true) : path,
221
+ };
222
+ });
223
+ }
@@ -0,0 +1,14 @@
1
+ import type { Options } from "./options.js";
2
+ import type $RefParser from "./index.js";
3
+ export default resolveExternal;
4
+ /**
5
+ * Crawls the JSON schema, finds all external JSON references, and resolves their values.
6
+ * This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
7
+ *
8
+ * NOTE: We only care about EXTERNAL references here. INTERNAL references are only relevant when dereferencing.
9
+ *
10
+ * @returns
11
+ * The promise resolves once all JSON references in the schema have been resolved,
12
+ * including nested references that are contained in externally-referenced files.
13
+ */
14
+ declare function resolveExternal(parser: $RefParser, options: Options): Promise<void> | Promise<any[]>;
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const ref_js_1 = __importDefault(require("./ref.js"));
39
+ const pointer_js_1 = __importDefault(require("./pointer.js"));
40
+ const parse_js_1 = __importDefault(require("./parse.js"));
41
+ const url = __importStar(require("./util/url.js"));
42
+ const errors_js_1 = require("./util/errors.js");
43
+ exports.default = resolveExternal;
44
+ /**
45
+ * Crawls the JSON schema, finds all external JSON references, and resolves their values.
46
+ * This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
47
+ *
48
+ * NOTE: We only care about EXTERNAL references here. INTERNAL references are only relevant when dereferencing.
49
+ *
50
+ * @returns
51
+ * The promise resolves once all JSON references in the schema have been resolved,
52
+ * including nested references that are contained in externally-referenced files.
53
+ */
54
+ function resolveExternal(parser, options) {
55
+ if (!options.resolve.external) {
56
+ // Nothing to resolve, so exit early
57
+ return Promise.resolve();
58
+ }
59
+ try {
60
+ // console.log('Resolving $ref pointers in %s', parser.$refs._root$Ref.path);
61
+ const promises = crawl(parser.schema, parser.$refs._root$Ref.path + "#", parser.$refs, options);
62
+ return Promise.all(promises);
63
+ }
64
+ catch (e) {
65
+ return Promise.reject(e);
66
+ }
67
+ }
68
+ /**
69
+ * Recursively crawls the given value, and resolves any external JSON references.
70
+ *
71
+ * @param obj - The value to crawl. If it's not an object or array, it will be ignored.
72
+ * @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
73
+ * @param $refs
74
+ * @param options
75
+ * @param seen - Internal.
76
+ *
77
+ * @returns
78
+ * Returns an array of promises. There will be one promise for each JSON reference in `obj`.
79
+ * If `obj` does not contain any JSON references, then the array will be empty.
80
+ * If any of the JSON references point to files that contain additional JSON references,
81
+ * then the corresponding promise will internally reference an array of promises.
82
+ */
83
+ function crawl(obj, path, $refs, options, seen) {
84
+ seen || (seen = new Set());
85
+ let promises = [];
86
+ if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
87
+ seen.add(obj); // Track previously seen objects to avoid infinite recursion
88
+ if (ref_js_1.default.isExternal$Ref(obj)) {
89
+ promises.push(resolve$Ref(obj, path, $refs, options));
90
+ }
91
+ else {
92
+ for (const key of Object.keys(obj)) {
93
+ const keyPath = pointer_js_1.default.join(path, key);
94
+ const value = obj[key];
95
+ if (ref_js_1.default.isExternal$Ref(value)) {
96
+ promises.push(resolve$Ref(value, keyPath, $refs, options));
97
+ }
98
+ else {
99
+ promises = promises.concat(crawl(value, keyPath, $refs, options, seen));
100
+ }
101
+ }
102
+ }
103
+ }
104
+ return promises;
105
+ }
106
+ /**
107
+ * Resolves the given JSON Reference, and then crawls the resulting value.
108
+ *
109
+ * @param $ref - The JSON Reference to resolve
110
+ * @param path - The full path of `$ref`, possibly with a JSON Pointer in the hash
111
+ * @param $refs
112
+ * @param options
113
+ *
114
+ * @returns
115
+ * The promise resolves once all JSON references in the object have been resolved,
116
+ * including nested references that are contained in externally-referenced files.
117
+ */
118
+ function resolve$Ref($ref, path, $refs, options) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ // console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
121
+ const resolvedPath = url.resolve(path, $ref.$ref);
122
+ const withoutHash = url.stripHash(resolvedPath);
123
+ // Do we already have this $ref?
124
+ $ref = $refs._$refs[withoutHash];
125
+ if ($ref) {
126
+ // We've already parsed this $ref, so use the existing value
127
+ return Promise.resolve($ref.value);
128
+ }
129
+ // Parse the $referenced file/url
130
+ try {
131
+ const result = yield (0, parse_js_1.default)(resolvedPath, $refs, options);
132
+ // Crawl the parsed value
133
+ // console.log('Resolving $ref pointers in %s', withoutHash);
134
+ const promises = crawl(result, withoutHash + "#", $refs, options);
135
+ return Promise.all(promises);
136
+ }
137
+ catch (err) {
138
+ if (!(options === null || options === void 0 ? void 0 : options.continueOnError) || !(0, errors_js_1.isHandledError)(err)) {
139
+ throw err;
140
+ }
141
+ if ($refs._$refs[withoutHash]) {
142
+ err.source = decodeURI(url.stripHash(path));
143
+ err.path = url.safePointerToPath(url.getHash(path));
144
+ }
145
+ return [];
146
+ }
147
+ });
148
+ }
@@ -0,0 +1,3 @@
1
+ import type { ResolverOptions } from "../types/index.js";
2
+ declare const _default: ResolverOptions;
3
+ export default _default;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const promises_1 = __importDefault(require("fs/promises"));
39
+ const ono_1 = require("@jsdevtools/ono");
40
+ const url = __importStar(require("../util/url.js"));
41
+ const errors_js_1 = require("../util/errors.js");
42
+ exports.default = {
43
+ /**
44
+ * The order that this resolver will run, in relation to other resolvers.
45
+ */
46
+ order: 100,
47
+ /**
48
+ * Determines whether this resolver can read a given file reference.
49
+ * Resolvers that return true will be tried, in order, until one successfully resolves the file.
50
+ * Resolvers that return false will not be given a chance to resolve the file.
51
+ */
52
+ canRead(file) {
53
+ return url.isFileSystemPath(file.url);
54
+ },
55
+ /**
56
+ * Reads the given file and returns its raw contents as a Buffer.
57
+ */
58
+ read(file) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ let path;
61
+ try {
62
+ path = url.toFileSystemPath(file.url);
63
+ }
64
+ catch (err) {
65
+ throw new errors_js_1.ResolverError(ono_1.ono.uri(err, `Malformed URI: ${file.url}`), file.url);
66
+ }
67
+ try {
68
+ const data = yield promises_1.default.readFile(path);
69
+ return data;
70
+ }
71
+ catch (err) {
72
+ throw new errors_js_1.ResolverError((0, ono_1.ono)(err, `Error opening file "${path}"`), path);
73
+ }
74
+ });
75
+ },
76
+ };
@@ -0,0 +1,3 @@
1
+ import type { HTTPResolverOptions } from "../types/index.js";
2
+ declare const _default: HTTPResolverOptions;
3
+ export default _default;