@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 +45 -0
- package/dist/get-by-pointer.js +29 -49
- package/dist/index.js +12 -4
- package/dist/is-private-host.js +112 -158
- package/dist/reference.js +50 -76
- package/dist/resolve-refs-from-file.js +350 -507
- package/dist/resolve-refs.d.ts +8 -0
- package/dist/resolve-refs.js +114 -184
- package/dist/resource-registry.js +125 -170
- package/dist/safe-assign.js +9 -14
- package/package.json +4 -3
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`.
|
package/dist/get-by-pointer.js
CHANGED
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
*
|
|
21
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
};
|
package/dist/is-private-host.js
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
};
|