@apidevtools/json-schema-ref-parser 10.0.1 → 11.0.0

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