@apidevtools/json-schema-ref-parser 11.9.3 → 12.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.
- package/dist/lib/dereference.js +24 -6
- package/lib/dereference.ts +32 -6
- package/package.json +1 -1
package/dist/lib/dereference.js
CHANGED
|
@@ -76,11 +76,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
76
76
|
value: obj,
|
|
77
77
|
circular: false,
|
|
78
78
|
};
|
|
79
|
-
|
|
80
|
-
if (Date.now() - startTime > options.timeoutMs) {
|
|
81
|
-
throw new errors_1.TimeoutError(options.timeoutMs);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
79
|
+
checkDereferenceTimeout(startTime, options);
|
|
84
80
|
const derefOptions = (options.dereference || {});
|
|
85
81
|
const isExcludedPath = derefOptions.excludedPathMatcher || (() => false);
|
|
86
82
|
if (derefOptions?.circular === "ignore" || !processedObjects.has(obj)) {
|
|
@@ -94,6 +90,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
94
90
|
}
|
|
95
91
|
else {
|
|
96
92
|
for (const key of Object.keys(obj)) {
|
|
93
|
+
checkDereferenceTimeout(startTime, options);
|
|
97
94
|
const keyPath = pointer_js_1.default.join(path, key);
|
|
98
95
|
const keyPathFromRoot = pointer_js_1.default.join(pathFromRoot, key);
|
|
99
96
|
if (isExcludedPath(keyPathFromRoot)) {
|
|
@@ -171,7 +168,15 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
171
168
|
const shouldResolveOnCwd = isExternalRef && options?.dereference?.externalReferenceResolution === "root";
|
|
172
169
|
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
|
|
173
170
|
const cache = dereferencedCache.get($refPath);
|
|
174
|
-
if (cache
|
|
171
|
+
if (cache) {
|
|
172
|
+
// If the object we found is circular we can immediately return it because it would have been
|
|
173
|
+
// cached with everything we need already and we don't need to re-process anything inside it.
|
|
174
|
+
//
|
|
175
|
+
// If the cached object however is _not_ circular and there are additional keys alongside our
|
|
176
|
+
// `$ref` pointer here we should merge them back in and return that.
|
|
177
|
+
if (cache.circular) {
|
|
178
|
+
return cache;
|
|
179
|
+
}
|
|
175
180
|
const refKeys = Object.keys($ref);
|
|
176
181
|
if (refKeys.length > 1) {
|
|
177
182
|
const extraKeys = {};
|
|
@@ -229,6 +234,19 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
229
234
|
}
|
|
230
235
|
return dereferencedObject;
|
|
231
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Check if we've run past our allowed timeout and throw an error if we have.
|
|
239
|
+
*
|
|
240
|
+
* @param startTime - The time when the dereferencing started.
|
|
241
|
+
* @param options
|
|
242
|
+
*/
|
|
243
|
+
function checkDereferenceTimeout(startTime, options) {
|
|
244
|
+
if (options && options.timeoutMs) {
|
|
245
|
+
if (Date.now() - startTime > options.timeoutMs) {
|
|
246
|
+
throw new errors_1.TimeoutError(options.timeoutMs);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
232
250
|
/**
|
|
233
251
|
* Called when a circular reference is found.
|
|
234
252
|
* It sets the {@link $Refs#circular} flag, executes the options.dereference.onCircular callback,
|
package/lib/dereference.ts
CHANGED
|
@@ -69,11 +69,8 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
|
|
|
69
69
|
circular: false,
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
throw new TimeoutError(options.timeoutMs);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
72
|
+
checkDereferenceTimeout<S, O>(startTime, options);
|
|
73
|
+
|
|
77
74
|
const derefOptions = (options.dereference || {}) as DereferenceOptions;
|
|
78
75
|
const isExcludedPath = derefOptions.excludedPathMatcher || (() => false);
|
|
79
76
|
|
|
@@ -98,6 +95,8 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
|
|
|
98
95
|
result.value = dereferenced.value;
|
|
99
96
|
} else {
|
|
100
97
|
for (const key of Object.keys(obj)) {
|
|
98
|
+
checkDereferenceTimeout<S, O>(startTime, options);
|
|
99
|
+
|
|
101
100
|
const keyPath = Pointer.join(path, key);
|
|
102
101
|
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
|
|
103
102
|
|
|
@@ -214,7 +213,17 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
|
|
|
214
213
|
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
|
|
215
214
|
|
|
216
215
|
const cache = dereferencedCache.get($refPath);
|
|
217
|
-
|
|
216
|
+
|
|
217
|
+
if (cache) {
|
|
218
|
+
// If the object we found is circular we can immediately return it because it would have been
|
|
219
|
+
// cached with everything we need already and we don't need to re-process anything inside it.
|
|
220
|
+
//
|
|
221
|
+
// If the cached object however is _not_ circular and there are additional keys alongside our
|
|
222
|
+
// `$ref` pointer here we should merge them back in and return that.
|
|
223
|
+
if (cache.circular) {
|
|
224
|
+
return cache;
|
|
225
|
+
}
|
|
226
|
+
|
|
218
227
|
const refKeys = Object.keys($ref);
|
|
219
228
|
if (refKeys.length > 1) {
|
|
220
229
|
const extraKeys = {};
|
|
@@ -294,6 +303,23 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
|
|
|
294
303
|
return dereferencedObject;
|
|
295
304
|
}
|
|
296
305
|
|
|
306
|
+
/**
|
|
307
|
+
* Check if we've run past our allowed timeout and throw an error if we have.
|
|
308
|
+
*
|
|
309
|
+
* @param startTime - The time when the dereferencing started.
|
|
310
|
+
* @param options
|
|
311
|
+
*/
|
|
312
|
+
function checkDereferenceTimeout<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
|
|
313
|
+
startTime: number,
|
|
314
|
+
options: O,
|
|
315
|
+
): void {
|
|
316
|
+
if (options && options.timeoutMs) {
|
|
317
|
+
if (Date.now() - startTime > options.timeoutMs) {
|
|
318
|
+
throw new TimeoutError(options.timeoutMs);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
297
323
|
/**
|
|
298
324
|
* Called when a circular reference is found.
|
|
299
325
|
* It sets the {@link $Refs#circular} flag, executes the options.dereference.onCircular callback,
|