@amritk/resolve-refs 0.4.2 → 0.4.4

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 ADDED
@@ -0,0 +1,45 @@
1
+ # @amritk/resolve-refs — notes for AI coding agents
2
+
3
+ Resolve and inline JSON Schema / OpenAPI `$ref`s (internal, cross-file, remote)
4
+ into a single dereferenced document, with session caching and a default-deny
5
+ SSRF guard. Full reference is [README.md](./README.md).
6
+
7
+ > Pre-alpha: APIs change in **minor** versions.
8
+
9
+ ## Minimal example
10
+
11
+ ```ts
12
+ import { resolveRefs, resolveRefsFromFile } from '@amritk/resolve-refs'
13
+
14
+ // In-memory, INTERNAL refs only:
15
+ const { resolved, errors } = resolveRefs(myDocument)
16
+
17
+ // From disk (cross-file + remote):
18
+ const result = await resolveRefsFromFile('./schema.json')
19
+
20
+ // Remote requires host allow-listing:
21
+ const remote = await resolveRefsFromFile('https://api.example.com/schema.json', {
22
+ allowedHosts: ['api.example.com'],
23
+ })
24
+ ```
25
+
26
+ ## Gotchas — where agents fail
27
+
28
+ 1. **`resolveRefs` is in-memory only.** It does NOT load other files/URLs —
29
+ external refs stay in place and are pushed to `errors` (the ref becomes `{}`).
30
+ Use `resolveRefsFromFile` for cross-file/remote.
31
+ 2. **Errors are collected, never thrown.** A missing file, refused host, or bad
32
+ URL lands on `result.errors` while the rest still resolves. Always check it.
33
+ 3. **Default-deny SSRF guard.** Remote refs to loopback / private / link-local /
34
+ `169.254.169.254` are refused unless `allowPrivateHosts: true` or an explicit
35
+ `allowedHosts` entry.
36
+ 4. **JSON-only by default** (`JSON.parse`). For YAML pass a custom
37
+ `parse: (content, location) => …` (e.g. wrapping `@amritk/yaml`).
38
+ 5. **`origins` exists only with `trackOrigins: true`.** Cycles are preserved (the
39
+ cycle point stays a `$ref`), so output is not always fully flat. The remote
40
+ cache is process-wide — `clearRemoteCache()` or `cache: false` when a schema
41
+ may have changed.
42
+
43
+ Exports: `resolveRefs`, `resolveRefsFromFile`, `clearRemoteCache`,
44
+ `getByPointer`, `pointerToPath`, `isPrivateHost` + types. Only the `.` entry.
45
+ Install: `bun add @amritk/resolve-refs`.
@@ -1,53 +1,33 @@
1
- /**
2
- * Decodes a single JSON Pointer segment (RFC 6901): percent-decodes it, then
3
- * unescapes `~1` → `/` and `~0` → `~`. Invalid percent-escapes are left as-is.
4
- */
5
1
  const decodeSegment = (segment) => {
6
- let decoded = segment;
7
- try {
8
- decoded = decodeURIComponent(segment);
9
- }
10
- catch {
11
- // leave invalid percent-escapes as-is
12
- }
13
- return decoded.replace(/~1/g, '/').replace(/~0/g, '~');
2
+ let decoded = segment;
3
+ try {
4
+ decoded = decodeURIComponent(segment);
5
+ } catch {
6
+ }
7
+ return decoded.replace(/~1/g, "/").replace(/~0/g, "~");
14
8
  };
15
- /**
16
- * Parses a JSON Pointer string (RFC 6901) into a path of keys: strips the leading
17
- * `/`, splits on `/`, decodes each segment, and coerces canonical array-index
18
- * tokens to numbers so indices and object keys read back the way a path consumer
19
- * expects. Only RFC 6901 array indices (`0` or a non-zero-leading run of digits)
20
- * are coerced; a numeric *object* key with a leading zero such as `"01"` is kept
21
- * as a string, since coercing it would alias to a different key (`obj["01"]` is
22
- * not `obj[1]`). A bare `''` or `'/'` is the empty path.
23
- */
24
- export const pointerToPath = (pointer) => {
25
- if (pointer === '' || pointer === '/')
26
- return [];
27
- return pointer
28
- .replace(/^\//, '')
29
- .split('/')
30
- .map((segment) => {
31
- const decoded = decodeSegment(segment);
32
- return /^(0|[1-9]\d*)$/.test(decoded) ? Number(decoded) : decoded;
33
- });
9
+ const pointerToPath = (pointer) => {
10
+ if (pointer === "" || pointer === "/")
11
+ return [];
12
+ return pointer.replace(/^\//, "").split("/").map((segment) => {
13
+ const decoded = decodeSegment(segment);
14
+ return /^(0|[1-9]\d*)$/.test(decoded) ? Number(decoded) : decoded;
15
+ });
34
16
  };
35
- /**
36
- * Walks a JSON Pointer string (RFC 6901) to the value it points to within
37
- * `root`. A bare `''` or `'/'` returns the root document. Segment escapes are
38
- * decoded (`~1` → `/`, `~0` → `~`). Returns `undefined` when any segment along
39
- * the path is missing or traverses a non-object.
40
- */
41
- export const getByPointer = (root, pointer) => {
42
- if (pointer === '' || pointer === '/')
43
- return root;
44
- const segments = pointer.replace(/^\//, '').split('/').map(decodeSegment);
45
- let current = root;
46
- for (const segment of segments) {
47
- if (current === null || typeof current !== 'object')
48
- return undefined;
49
- const key = Array.isArray(current) ? Number(segment) : segment;
50
- current = current[key];
51
- }
52
- return current;
17
+ const getByPointer = (root, pointer) => {
18
+ if (pointer === "" || pointer === "/")
19
+ return root;
20
+ const segments = pointer.replace(/^\//, "").split("/").map(decodeSegment);
21
+ let current = root;
22
+ for (const segment of segments) {
23
+ if (current === null || typeof current !== "object")
24
+ return void 0;
25
+ const key = Array.isArray(current) ? Number(segment) : segment;
26
+ current = current[key];
27
+ }
28
+ return current;
29
+ };
30
+ export {
31
+ getByPointer,
32
+ pointerToPath
53
33
  };
package/dist/index.js CHANGED
@@ -1,4 +1,12 @@
1
- export { getByPointer, pointerToPath } from './get-by-pointer.js';
2
- export { isPrivateHost } from './is-private-host.js';
3
- export { resolveRefs } from './resolve-refs.js';
4
- export { clearRemoteCache, resolveRefsFromFile } from './resolve-refs-from-file.js';
1
+ import { getByPointer, pointerToPath } from "./get-by-pointer.js";
2
+ import { isPrivateHost } from "./is-private-host.js";
3
+ import { resolveRefs } from "./resolve-refs.js";
4
+ import { clearRemoteCache, resolveRefsFromFile } from "./resolve-refs-from-file.js";
5
+ export {
6
+ clearRemoteCache,
7
+ getByPointer,
8
+ isPrivateHost,
9
+ pointerToPath,
10
+ resolveRefs,
11
+ resolveRefsFromFile
12
+ };
@@ -1,172 +1,126 @@
1
- /**
2
- * Best-effort check for a hostname that resolves to a non-public address —
3
- * loopback, private (RFC 1918 / ULA), link-local (incl. the `169.254.169.254`
4
- * cloud-metadata endpoint), or otherwise host-local. This is a syntactic guard
5
- * on the URL only; it does not perform DNS, so a public name that resolves to a
6
- * private IP is not caught here. It exists to stop the obvious SSRF footguns
7
- * when resolving remote `$ref`s.
8
- */
9
- /** True for an IPv4 address (given its first two octets) in a non-public range. */
10
1
  const isPrivateIpv4 = (a, b) => {
11
- if (a === 10 || a === 127 || a === 0)
12
- return true;
13
- if (a === 169 && b === 254)
14
- return true; // link-local + cloud metadata
15
- if (a === 172 && b >= 16 && b <= 31)
16
- return true;
17
- if (a === 192 && b === 168)
18
- return true;
19
- if (a === 100 && b >= 64 && b <= 127)
20
- return true; // CGNAT
21
- return false;
2
+ if (a === 10 || a === 127 || a === 0)
3
+ return true;
4
+ if (a === 169 && b === 254)
5
+ return true;
6
+ if (a === 172 && b >= 16 && b <= 31)
7
+ return true;
8
+ if (a === 192 && b === 168)
9
+ return true;
10
+ if (a === 100 && b >= 64 && b <= 127)
11
+ return true;
12
+ return false;
22
13
  };
23
- /** Parses a single IPv4 part in decimal, octal (`0…`), or hex (`0x…`) form. */
24
14
  const parseIpv4Part = (s) => {
25
- if (/^0x[0-9a-f]+$/i.test(s))
26
- return Number.parseInt(s, 16);
27
- if (/^0[0-7]*$/.test(s))
28
- return Number.parseInt(s, 8);
29
- if (/^[1-9][0-9]*$/.test(s))
30
- return Number.parseInt(s, 10);
31
- return null;
15
+ if (/^0x[0-9a-f]+$/i.test(s))
16
+ return Number.parseInt(s, 16);
17
+ if (/^0[0-7]*$/.test(s))
18
+ return Number.parseInt(s, 8);
19
+ if (/^[1-9][0-9]*$/.test(s))
20
+ return Number.parseInt(s, 10);
21
+ return null;
32
22
  };
33
- /**
34
- * Resolves the first two octets of an IPv4 host written in any inet_aton form
35
- * (dotted/decimal/octal/hex, 1–4 parts), or `null` if it is not an IPv4 literal.
36
- * The WHATWG URL parser normalizes these to dotted-decimal before they reach us,
37
- * but a direct caller (this is an exported guard) can pass the raw form.
38
- */
39
23
  const ipv4Octets = (host) => {
40
- if (!/^[0-9a-fx.]+$/i.test(host))
41
- return null;
42
- const parts = host.split('.');
43
- if (parts.length === 0 || parts.length > 4)
44
- return null;
45
- const nums = [];
46
- for (const part of parts) {
47
- const n = parseIpv4Part(part);
48
- if (n === null)
49
- return null;
50
- nums.push(n);
51
- }
52
- // inet_aton packing: every part but the last is one byte; the last fills the
53
- // remaining low bytes (so `127.1` is 127.0.0.1 and `2130706433` is too).
54
- let value = 0;
55
- for (let i = 0; i < nums.length - 1; i++) {
56
- const byte = nums[i];
57
- if (byte > 0xff)
58
- return null;
59
- value = value * 256 + byte;
60
- }
61
- const remaining = 4 - (nums.length - 1);
62
- const last = nums[nums.length - 1];
63
- if (last > 256 ** remaining - 1)
64
- return null;
65
- value = value * 256 ** remaining + last;
66
- if (value > 0xffffffff)
67
- return null;
68
- return [(value >>> 24) & 0xff, (value >>> 16) & 0xff];
24
+ if (!/^[0-9a-fx.]+$/i.test(host))
25
+ return null;
26
+ const parts = host.split(".");
27
+ if (parts.length === 0 || parts.length > 4)
28
+ return null;
29
+ const nums = [];
30
+ for (const part of parts) {
31
+ const n = parseIpv4Part(part);
32
+ if (n === null)
33
+ return null;
34
+ nums.push(n);
35
+ }
36
+ let value = 0;
37
+ for (let i = 0; i < nums.length - 1; i++) {
38
+ const byte = nums[i];
39
+ if (byte > 255)
40
+ return null;
41
+ value = value * 256 + byte;
42
+ }
43
+ const remaining = 4 - (nums.length - 1);
44
+ const last = nums[nums.length - 1];
45
+ if (last > 256 ** remaining - 1)
46
+ return null;
47
+ value = value * 256 ** remaining + last;
48
+ if (value > 4294967295)
49
+ return null;
50
+ return [value >>> 24 & 255, value >>> 16 & 255];
69
51
  };
70
- /**
71
- * Expands a hex IPv6 literal (no dotted-IPv4 tail — that is handled separately)
72
- * into its eight 16-bit groups, resolving `::` zero-compression. Returns null if
73
- * the string is not a well-formed hex IPv6 address.
74
- */
75
52
  const expandHexIpv6 = (host) => {
76
- const halves = host.split('::');
77
- if (halves.length > 2)
78
- return null;
79
- const toGroups = (part) => {
80
- if (part === '')
81
- return [];
82
- const groups = [];
83
- for (const g of part.split(':')) {
84
- if (!/^[0-9a-f]{1,4}$/.test(g))
85
- return null;
86
- groups.push(Number.parseInt(g, 16));
87
- }
88
- return groups;
89
- };
90
- const head = toGroups(halves[0]);
91
- if (head === null)
92
- return null;
93
- if (halves.length === 1)
94
- return head.length === 8 ? head : null;
95
- const tail = toGroups(halves[1]);
96
- if (tail === null)
97
- return null;
98
- const fill = 8 - head.length - tail.length;
99
- if (fill < 0)
53
+ const halves = host.split("::");
54
+ if (halves.length > 2)
55
+ return null;
56
+ const toGroups = (part) => {
57
+ if (part === "")
58
+ return [];
59
+ const groups = [];
60
+ for (const g of part.split(":")) {
61
+ if (!/^[0-9a-f]{1,4}$/.test(g))
100
62
  return null;
101
- return [...head, ...new Array(fill).fill(0), ...tail];
63
+ groups.push(Number.parseInt(g, 16));
64
+ }
65
+ return groups;
66
+ };
67
+ const head = toGroups(halves[0]);
68
+ if (head === null)
69
+ return null;
70
+ if (halves.length === 1)
71
+ return head.length === 8 ? head : null;
72
+ const tail = toGroups(halves[1]);
73
+ if (tail === null)
74
+ return null;
75
+ const fill = 8 - head.length - tail.length;
76
+ if (fill < 0)
77
+ return null;
78
+ return [...head, ...new Array(fill).fill(0), ...tail];
102
79
  };
103
- /**
104
- * When an expanded IPv6 address embeds an IPv4 address in its low 32 bits,
105
- * returns that IPv4 as a 32-bit number; otherwise null. Covers every embedding
106
- * the WHATWG URL parser can hand us as bare hex — the `ffff:`-only regex this
107
- * replaced let `::7f00:1` (`::127.0.0.1`) and `::a9fe:a9fe` (`::169.254.169.254`)
108
- * through, since those normalize away the `ffff:` marker:
109
- * - `::/96` compatible (`::X:Y`, also covers `::`/`::1`),
110
- * - IPv4-mapped (`::ffff:X:Y`),
111
- * - IPv4-translated (`::ffff:0:X:Y`),
112
- * - NAT64 (`64:ff9b::/96`).
113
- */
114
80
  const ipv4EmbeddedInIpv6 = (g) => {
115
- const [a, b, c, d, e, f, hi, lo] = g;
116
- const embedded = (hi * 0x10000 + lo) >>> 0;
117
- const zeros4 = a === 0 && b === 0 && c === 0 && d === 0;
118
- if (zeros4 && e === 0 && f === 0)
119
- return embedded; // ::/96 compatible
120
- if (zeros4 && e === 0 && f === 0xffff)
121
- return embedded; // ::ffff:X:Y mapped
122
- if (zeros4 && e === 0xffff && f === 0)
123
- return embedded; // ::ffff:0:X:Y translated
124
- if (a === 0x64 && b === 0xff9b && c === 0 && d === 0 && e === 0 && f === 0)
125
- return embedded; // 64:ff9b::/96 NAT64
126
- return null;
81
+ const [a, b, c, d, e, f, hi, lo] = g;
82
+ const embedded = hi * 65536 + lo >>> 0;
83
+ const zeros4 = a === 0 && b === 0 && c === 0 && d === 0;
84
+ if (zeros4 && e === 0 && f === 0)
85
+ return embedded;
86
+ if (zeros4 && e === 0 && f === 65535)
87
+ return embedded;
88
+ if (zeros4 && e === 65535 && f === 0)
89
+ return embedded;
90
+ if (a === 100 && b === 65435 && c === 0 && d === 0 && e === 0 && f === 0)
91
+ return embedded;
92
+ return null;
127
93
  };
128
- export const isPrivateHost = (hostname) => {
129
- // Strip IPv6 brackets and any trailing dot(s): `localhost.` is the FQDN-root
130
- // form of `localhost` and resolves to the same loopback address, so it must
131
- // not slip past the by-name checks below.
132
- const host = hostname
133
- .replace(/^\[|\]$/g, '')
134
- .replace(/\.+$/, '')
135
- .toLowerCase();
136
- if (host === 'localhost' || host.endsWith('.localhost'))
137
- return true;
138
- if (host.includes(':')) {
139
- // --- IPv6 (and IPv4-mapped IPv6) ---
140
- if (host === '::1' || host === '::')
141
- return true;
142
- // fe80::/10 link-local spans fe80–febf (third nibble 8–b).
143
- if (/^fe[89ab][0-9a-f]:/.test(host))
144
- return true;
145
- if (host.startsWith('fc') || host.startsWith('fd'))
146
- return true; // fc00::/7 unique-local
147
- // IPv4-mapped/compatible, dotted (`::ffff:127.0.0.1`, `::127.0.0.1`) — the URL
148
- // parser rewrites these to hex, but a direct caller may pass the dotted form.
149
- const dotted = /:((?:\d{1,3}\.){3}\d{1,3})$/.exec(host);
150
- if (dotted?.[1]) {
151
- const oct = ipv4Octets(dotted[1]);
152
- if (oct)
153
- return isPrivateIpv4(oct[0], oct[1]);
154
- }
155
- // Every hex IPv4-in-IPv6 embedding `new URL()` can produce (mapped,
156
- // compatible, translated, NAT64) — via a full expansion rather than a
157
- // single-form regex, which missed `::7f00:1` / `::a9fe:a9fe`. This also
158
- // catches the fully-expanded loopback `0:0:0:0:0:0:0:1`.
159
- const groups = expandHexIpv6(host);
160
- if (groups) {
161
- const embedded = ipv4EmbeddedInIpv6(groups);
162
- if (embedded !== null)
163
- return isPrivateIpv4((embedded >>> 24) & 0xff, (embedded >>> 16) & 0xff);
164
- }
165
- return false;
94
+ const isPrivateHost = (hostname) => {
95
+ const host = hostname.replace(/^\[|\]$/g, "").replace(/\.+$/, "").toLowerCase();
96
+ if (host === "localhost" || host.endsWith(".localhost"))
97
+ return true;
98
+ if (host.includes(":")) {
99
+ if (host === "::1" || host === "::")
100
+ return true;
101
+ if (/^fe[89ab][0-9a-f]:/.test(host))
102
+ return true;
103
+ if (host.startsWith("fc") || host.startsWith("fd"))
104
+ return true;
105
+ const dotted = /:((?:\d{1,3}\.){3}\d{1,3})$/.exec(host);
106
+ if (dotted?.[1]) {
107
+ const oct2 = ipv4Octets(dotted[1]);
108
+ if (oct2)
109
+ return isPrivateIpv4(oct2[0], oct2[1]);
110
+ }
111
+ const groups = expandHexIpv6(host);
112
+ if (groups) {
113
+ const embedded = ipv4EmbeddedInIpv6(groups);
114
+ if (embedded !== null)
115
+ return isPrivateIpv4(embedded >>> 24 & 255, embedded >>> 16 & 255);
166
116
  }
167
- // --- IPv4 (any inet_aton encoding) ---
168
- const oct = ipv4Octets(host);
169
- if (oct)
170
- return isPrivateIpv4(oct[0], oct[1]);
171
117
  return false;
118
+ }
119
+ const oct = ipv4Octets(host);
120
+ if (oct)
121
+ return isPrivateIpv4(oct[0], oct[1]);
122
+ return false;
123
+ };
124
+ export {
125
+ isPrivateHost
172
126
  };
package/dist/reference.js CHANGED
@@ -1,80 +1,54 @@
1
- import { getByPointer, pointerToPath } from './get-by-pointer.js';
2
- // `$ref` is listed first so that a node carrying several reference keywords
3
- // resolves through the static one, matching how validators treat `$ref`.
4
- const REF_KEYWORDS = ['$ref', '$dynamicRef', '$recursiveRef'];
5
- /** Returns the reference keyword `obj` carries (if any), preferring `$ref`. */
6
- export const readReference = (obj) => {
7
- for (const keyword of REF_KEYWORDS) {
8
- const value = obj[keyword];
9
- if (typeof value === 'string')
10
- return { keyword, value };
11
- }
12
- return undefined;
1
+ import { getByPointer, pointerToPath } from "./get-by-pointer.js";
2
+ const REF_KEYWORDS = ["$ref", "$dynamicRef", "$recursiveRef"];
3
+ const readReference = (obj) => {
4
+ for (const keyword of REF_KEYWORDS) {
5
+ const value = obj[keyword];
6
+ if (typeof value === "string")
7
+ return { keyword, value };
8
+ }
9
+ return void 0;
13
10
  };
14
- /** A fragment is a JSON Pointer when it is empty or begins with `/`; otherwise a plain-name anchor. */
15
- const isPointerFragment = (fragment) => fragment === '' || fragment.startsWith('/');
16
- /**
17
- * Depth-first search for the first object in `root` satisfying `predicate`,
18
- * returning it with the path to it. `seen` guards against cyclic inputs. Used to
19
- * locate `$anchor`/`$dynamicAnchor`/`$recursiveAnchor` targets.
20
- */
11
+ const isPointerFragment = (fragment) => fragment === "" || fragment.startsWith("/");
21
12
  const search = (root, predicate) => {
22
- const seen = new Set();
23
- const walk = (node, pointer) => {
24
- if (node === null || typeof node !== 'object' || seen.has(node))
25
- return undefined;
26
- seen.add(node);
27
- if (!Array.isArray(node) && predicate(node))
28
- return { value: node, pointer };
29
- if (Array.isArray(node)) {
30
- for (let i = 0; i < node.length; i++) {
31
- const found = walk(node[i], [...pointer, i]);
32
- if (found)
33
- return found;
34
- }
35
- }
36
- else {
37
- for (const key of Object.keys(node)) {
38
- const found = walk(node[key], [...pointer, key]);
39
- if (found)
40
- return found;
41
- }
42
- }
43
- return undefined;
44
- };
45
- return walk(root, []);
46
- };
47
- /**
48
- * Resolves a reference `fragment` (the part after `#`) within `root`, per its
49
- * `keyword`. Returns the target node and its in-document path, or `undefined`
50
- * when nothing matches.
51
- *
52
- * - **JSON Pointer** (`''`, `/a/b`) — a plain pointer lookup, for any keyword.
53
- * - **`$anchor` name** (`node`) — searches for a `$anchor`/`$dynamicAnchor` equal
54
- * to the name. A `$dynamicRef` prefers a `$dynamicAnchor`, then falls back to a
55
- * plain `$anchor`, so it degrades to `$ref` semantics when nothing dynamic
56
- * matches (2020-12).
57
- * - **`$recursiveRef`** (always `#`) — binds to the object carrying
58
- * `$recursiveAnchor: true`, falling back to the document root when there is
59
- * none (2019-09).
60
- *
61
- * Anchor search is document-global: we bind to the single matching anchor in
62
- * `root` rather than walking the dynamic scope. This is the *fallback* path —
63
- * the resolvers first try `$id`-scoped resolution via `resource-registry.ts`,
64
- * which binds duplicate anchor names to the resource they are declared in, and
65
- * only land here for anchors declared outside any registered scope.
66
- */
67
- export const resolveFragment = (root, keyword, fragment) => {
68
- if (keyword === '$recursiveRef') {
69
- const anchored = search(root, (obj) => obj['$recursiveAnchor'] === true);
70
- return anchored ?? { value: root, pointer: [] };
71
- }
72
- if (isPointerFragment(fragment)) {
73
- const value = getByPointer(root, fragment);
74
- return value === undefined ? undefined : { value, pointer: pointerToPath(fragment) };
13
+ const seen = /* @__PURE__ */ new Set();
14
+ const walk = (node, pointer) => {
15
+ if (node === null || typeof node !== "object" || seen.has(node))
16
+ return void 0;
17
+ seen.add(node);
18
+ if (!Array.isArray(node) && predicate(node))
19
+ return { value: node, pointer };
20
+ if (Array.isArray(node)) {
21
+ for (let i = 0; i < node.length; i++) {
22
+ const found = walk(node[i], [...pointer, i]);
23
+ if (found)
24
+ return found;
25
+ }
26
+ } else {
27
+ for (const key of Object.keys(node)) {
28
+ const found = walk(node[key], [...pointer, key]);
29
+ if (found)
30
+ return found;
31
+ }
75
32
  }
76
- if (keyword === '$dynamicRef') {
77
- return (search(root, (obj) => obj['$dynamicAnchor'] === fragment) ?? search(root, (obj) => obj['$anchor'] === fragment));
78
- }
79
- return search(root, (obj) => obj['$anchor'] === fragment || obj['$dynamicAnchor'] === fragment);
33
+ return void 0;
34
+ };
35
+ return walk(root, []);
36
+ };
37
+ const resolveFragment = (root, keyword, fragment) => {
38
+ if (keyword === "$recursiveRef") {
39
+ const anchored = search(root, (obj) => obj["$recursiveAnchor"] === true);
40
+ return anchored ?? { value: root, pointer: [] };
41
+ }
42
+ if (isPointerFragment(fragment)) {
43
+ const value = getByPointer(root, fragment);
44
+ return value === void 0 ? void 0 : { value, pointer: pointerToPath(fragment) };
45
+ }
46
+ if (keyword === "$dynamicRef") {
47
+ return search(root, (obj) => obj["$dynamicAnchor"] === fragment) ?? search(root, (obj) => obj["$anchor"] === fragment);
48
+ }
49
+ return search(root, (obj) => obj["$anchor"] === fragment || obj["$dynamicAnchor"] === fragment);
50
+ };
51
+ export {
52
+ readReference,
53
+ resolveFragment
80
54
  };