@amritk/resolve-refs 0.4.5 → 0.7.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/AI.md +17 -7
- package/README.md +93 -16
- package/dist/assert-public-host.d.ts +39 -0
- package/dist/assert-public-host.js +28 -0
- package/dist/child-role.d.ts +43 -0
- package/dist/child-role.js +45 -0
- package/dist/dynamic-scope.d.ts +70 -0
- package/dist/dynamic-scope.js +25 -0
- package/dist/get-by-pointer.d.ts +6 -0
- package/dist/get-by-pointer.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/is-contained-path.d.ts +12 -0
- package/dist/is-contained-path.js +32 -0
- package/dist/is-private-host.d.ts +11 -4
- package/dist/is-private-host.js +24 -10
- package/dist/max-depth.d.ts +24 -0
- package/dist/max-depth.js +9 -0
- package/dist/reference.js +11 -6
- package/dist/resolve-refs-from-file.d.ts +22 -3
- package/dist/resolve-refs-from-file.js +414 -43
- package/dist/resolve-refs.d.ts +31 -3
- package/dist/resolve-refs.js +55 -14
- package/dist/resource-registry.d.ts +15 -4
- package/dist/resource-registry.js +26 -12
- package/dist/role-at-path.d.ts +17 -0
- package/dist/role-at-path.js +5 -0
- package/dist/types.d.ts +75 -1
- package/package.json +7 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ResolveError } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* How deep either resolver walks before it gives up on a branch.
|
|
4
|
+
*
|
|
5
|
+
* Both walkers are recursive, so a pathologically nested document (`{"a":{"a":…`
|
|
6
|
+
* repeated thousands of times — trivial to produce, and JSON.parse happily
|
|
7
|
+
* accepts it) used to unwind the whole call stack with a `RangeError`. That
|
|
8
|
+
* broke the package's headline contract: errors are *collected* on the result,
|
|
9
|
+
* never thrown. Capping the walk turns that crash into an ordinary
|
|
10
|
+
* {@link ResolveError} on the result.
|
|
11
|
+
*
|
|
12
|
+
* 512 is far past anything a hand-written or generated schema reaches (deeply
|
|
13
|
+
* bundled OpenAPI documents live around 20–40 levels) while staying comfortably
|
|
14
|
+
* inside the default stack on every runtime we target.
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_MAX_DEPTH = 512;
|
|
17
|
+
/**
|
|
18
|
+
* The error recorded when a walk hits {@link DEFAULT_MAX_DEPTH} (or the
|
|
19
|
+
* caller's `maxDepth`). Reported once per resolve — a wide *and* deep document
|
|
20
|
+
* would otherwise push one error per branch and turn the error array into its
|
|
21
|
+
* own memory problem. The offending subtree is left unresolved rather than
|
|
22
|
+
* dropped, so no data disappears silently.
|
|
23
|
+
*/
|
|
24
|
+
export declare const depthLimitError: (maxDepth: number) => ResolveError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const DEFAULT_MAX_DEPTH = 512;
|
|
2
|
+
const depthLimitError = (maxDepth) => ({
|
|
3
|
+
message: `Document nesting exceeds the maximum depth of ${maxDepth}; the deeper subtrees were left unresolved (raise maxDepth to allow more)`,
|
|
4
|
+
path: []
|
|
5
|
+
});
|
|
6
|
+
export {
|
|
7
|
+
DEFAULT_MAX_DEPTH,
|
|
8
|
+
depthLimitError
|
|
9
|
+
};
|
package/dist/reference.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { childRole } from "./child-role.js";
|
|
1
2
|
import { getByPointer, pointerToPath } from "./get-by-pointer.js";
|
|
3
|
+
import { DEFAULT_MAX_DEPTH } from "./max-depth.js";
|
|
2
4
|
const REF_KEYWORDS = ["$ref", "$dynamicRef", "$recursiveRef"];
|
|
3
5
|
const readReference = (obj) => {
|
|
4
6
|
for (const keyword of REF_KEYWORDS) {
|
|
@@ -11,28 +13,31 @@ const readReference = (obj) => {
|
|
|
11
13
|
const isPointerFragment = (fragment) => fragment === "" || fragment.startsWith("/");
|
|
12
14
|
const search = (root, predicate) => {
|
|
13
15
|
const seen = /* @__PURE__ */ new Set();
|
|
14
|
-
const walk = (node, pointer) => {
|
|
15
|
-
if (node === null || typeof node !== "object" || seen.has(node))
|
|
16
|
+
const walk = (node, pointer, depth, role) => {
|
|
17
|
+
if (node === null || typeof node !== "object" || seen.has(node) || depth > DEFAULT_MAX_DEPTH)
|
|
18
|
+
return void 0;
|
|
19
|
+
if (role === "value")
|
|
16
20
|
return void 0;
|
|
17
21
|
seen.add(node);
|
|
18
|
-
if (!Array.isArray(node) && predicate(node))
|
|
22
|
+
if (!Array.isArray(node) && role !== "schemaMap" && predicate(node)) {
|
|
19
23
|
return { value: node, pointer };
|
|
24
|
+
}
|
|
20
25
|
if (Array.isArray(node)) {
|
|
21
26
|
for (let i = 0; i < node.length; i++) {
|
|
22
|
-
const found = walk(node[i], [...pointer, i]);
|
|
27
|
+
const found = walk(node[i], [...pointer, i], depth + 1, childRole(role, i));
|
|
23
28
|
if (found)
|
|
24
29
|
return found;
|
|
25
30
|
}
|
|
26
31
|
} else {
|
|
27
32
|
for (const key of Object.keys(node)) {
|
|
28
|
-
const found = walk(node[key], [...pointer, key]);
|
|
33
|
+
const found = walk(node[key], [...pointer, key], depth + 1, childRole(role, key));
|
|
29
34
|
if (found)
|
|
30
35
|
return found;
|
|
31
36
|
}
|
|
32
37
|
}
|
|
33
38
|
return void 0;
|
|
34
39
|
};
|
|
35
|
-
return walk(root, []);
|
|
40
|
+
return walk(root, [], 0, "schema");
|
|
36
41
|
};
|
|
37
42
|
const resolveFragment = (root, keyword, fragment) => {
|
|
38
43
|
if (keyword === "$recursiveRef") {
|
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
import type { ResolveOptions, ResolveResult } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Drops cached remote documents. Pass a URL to drop just that document (across
|
|
4
|
+
* every credential scope it was fetched under); pass nothing to drop them all.
|
|
5
|
+
* Mainly useful in tests, or when a remote schema is known to have changed.
|
|
6
|
+
*/
|
|
7
|
+
export declare const clearRemoteCache: (location?: string) => void;
|
|
4
8
|
/**
|
|
5
9
|
* Resolves `$ref`s in a document on disk (or at a URL), including cross-file
|
|
6
10
|
* and remote refs. Remote documents are fetched on the fly and cached in memory
|
|
7
11
|
* for the session; `options` governs whether/which remote hosts are allowed,
|
|
8
12
|
* how they are fetched (`headers`, `fetch`, `timeoutMs`, `maxRedirects`,
|
|
9
|
-
* `maxBytes`),
|
|
13
|
+
* `maxBytes`), which local directories may be read (`localRefs`,
|
|
14
|
+
* `allowedRoots`), the limits on the resolve as a whole (`maxDocuments`,
|
|
15
|
+
* `totalTimeoutMs`, `maxDepth`), and whether the session cache is used
|
|
16
|
+
* (`cache`).
|
|
17
|
+
*
|
|
18
|
+
* Only references in *schema* positions are followed: a `$ref`-shaped object
|
|
19
|
+
* inside `enum`, `const`, `default` or `examples` is instance data, so it is
|
|
20
|
+
* kept verbatim and the document it names is never loaded (see `child-role.ts`).
|
|
21
|
+
*
|
|
22
|
+
* As in {@link resolveRefs}, a reference the resolver cannot answer without
|
|
23
|
+
* changing what the document accepts is *kept* rather than guessed at, and
|
|
24
|
+
* resolves within the output: a cycle (rewritten to a root-relative `$ref`, its
|
|
25
|
+
* target hoisted into `$defs` when it came from another file), and — for a
|
|
26
|
+
* resolve that stayed inside a single document — a `$dynamicRef` whose anchor
|
|
27
|
+
* name is declared more than once. Neither is reported on `errors`; nothing
|
|
28
|
+
* failed.
|
|
10
29
|
*/
|
|
11
30
|
export declare const resolveRefsFromFile: (filename: string, options?: ResolveOptions) => Promise<ResolveResult>;
|