@amritk/resolve-refs 0.4.0 → 0.4.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 amritk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -97,27 +97,35 @@ JSON Schema documents only, so this resolver stays JSON-only and dependency-free
97
97
 
98
98
  ## Benchmarks
99
99
 
100
- `resolveRefs` is single-pass: every unique `$ref` string is resolved exactly
101
- once and memoized, with a sentinel that breaks cycles. The `bench/` suite isolates
102
- what that memoization buys by pitting it against a naive inliner that re-resolves
103
- each ref every time it is encountered — same inlined output, the cache is the only
104
- difference. Representative numbers (Bun 1.3, Linux x64 — your hardware will
105
- differ, run `bun run bench` yourself):
100
+ `resolveRefs` memoizes: every unique `$ref` string is resolved once per scope,
101
+ with a sentinel that breaks cycles by keeping the reference node in place. But it
102
+ is no longer *only* a memoized inliner — before resolving anything it walks the
103
+ whole document once to build a resource registry (`$id`/`$anchor` scoping), keeps
104
+ recursive cycles intact, and records a diagnostic for every external ref. The
105
+ `bench/` suite pits it against a bare naive inliner that does none of that — no
106
+ registry, no scoping, no diagnostics — and re-resolves each ref on every
107
+ encounter, so the gap is the production resolver's *total* per-call cost against
108
+ the cheapest thing that produces the same inlined shape. Both are asserted to
109
+ produce byte-identical output before either is timed. Representative numbers
110
+ (Bun 1.3, Linux x64 — your hardware will differ, run `bun run bench` yourself):
106
111
 
107
112
  | schema | cached | naive | speedup |
108
113
  |:---|---:|---:|---:|
109
- | reuse-heavy (50 refs → 1 def) | ~35k ops/s | ~6k ops/s | **~5.8×** |
110
- | chain (40 `$ref` → `$ref` links) | ~5k ops/s | ~0.6k ops/s | **~8.2×** |
111
- | cyclic tree | ~122k ops/s | ~117k ops/s | ~1.05× |
112
- | wide-distinct (60 defs, each used once) | ~3.3k ops/s | ~6.1k ops/s | ~0.54× |
113
-
114
- The cache earns its keep exactly where you'd expect: a `$def` referenced from
115
- many sites, or a long indirection chain, is resolved once instead of re-walked
116
- every time. On a document where every ref is distinct — so the cache never hits —
117
- its bookkeeping is pure overhead and the naive walk is faster; that `wide-distinct`
118
- row is kept in the table precisely to show the trade honestly. Real API schemas
119
- lean heavily on shared `$def`s, which is the case the resolver is tuned for.
114
+ | chain (40 `$ref` → `$ref` links) | ~2.6k ops/s | ~0.8k ops/s | **~3.2×** |
115
+ | reuse-heavy (50 refs → 1 def) | ~4.5k ops/s | ~8k ops/s | ~0.5× |
116
+ | cyclic tree | ~27k ops/s | ~74k ops/s | ~0.37× |
117
+ | wide-distinct (60 defs, each used once) | ~2.3k ops/s | ~7.3k ops/s | ~0.32× |
118
+
119
+ Memoization overtakes the naive walk only on the **chain** shape, where a long
120
+ indirection path is expensive to re-resolve and the cache collapses it to one
121
+ pass. On every other shape here the fixed cost of the up-front registry walk (paid
122
+ once per call, no matter how the refs reuse) outweighs what memoization saves, and
123
+ the bare inliner is faster — these are small schemas where that one document walk
124
+ dominates. The takeaway is practical: the resolver's per-call floor is a full
125
+ document traversal, so in a hot loop resolve a document **once** and reuse the
126
+ result rather than re-resolving. The `reuse-heavy`, `cyclic`, and `wide-distinct`
127
+ rows are kept in the table precisely to show that trade honestly rather than
128
+ cherry-picking the one shape the cache wins.
120
129
 
121
130
  Opting into `trackOrigins` (which records where each inlined value came from) adds
122
- roughly **5–20%** on top. Both strategies are asserted to produce byte-identical
123
- output before either is timed.
131
+ roughly **0–20%** on top, within run-to-run noise on these small schemas.
@@ -67,6 +67,64 @@ const ipv4Octets = (host) => {
67
67
  return null;
68
68
  return [(value >>> 24) & 0xff, (value >>> 16) & 0xff];
69
69
  };
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
+ 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)
100
+ return null;
101
+ return [...head, ...new Array(fill).fill(0), ...tail];
102
+ };
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
+ 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;
127
+ };
70
128
  export const isPrivateHost = (hostname) => {
71
129
  // Strip IPv6 brackets and any trailing dot(s): `localhost.` is the FQDN-root
72
130
  // form of `localhost` and resolves to the same loopback address, so it must
@@ -86,19 +144,23 @@ export const isPrivateHost = (hostname) => {
86
144
  return true;
87
145
  if (host.startsWith('fc') || host.startsWith('fd'))
88
146
  return true; // fc00::/7 unique-local
89
- // IPv4-mapped/compatible, dotted (`::ffff:127.0.0.1`) — the URL parser
90
- // rewrites this to hex, but a direct caller may pass the dotted form.
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.
91
149
  const dotted = /:((?:\d{1,3}\.){3}\d{1,3})$/.exec(host);
92
150
  if (dotted?.[1]) {
93
151
  const oct = ipv4Octets(dotted[1]);
94
152
  if (oct)
95
153
  return isPrivateIpv4(oct[0], oct[1]);
96
154
  }
97
- // IPv4-mapped, hex (`::ffff:7f00:1`) — what `new URL()` produces.
98
- const hex = /:ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(host);
99
- if (hex?.[1] && hex[2]) {
100
- const hi = Number.parseInt(hex[1], 16);
101
- return isPrivateIpv4((hi >> 8) & 0xff, hi & 0xff);
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);
102
164
  }
103
165
  return false;
104
166
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/resolve-refs",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Resolve and inline JSON Schema / OpenAPI $refs — internal, cross-file, and remote — with session caching and a default-deny SSRF guard.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -39,8 +39,8 @@
39
39
  },
40
40
  "exports": {
41
41
  ".": {
42
- "default": "./dist/index.js",
43
- "types": "./dist/index.d.ts"
42
+ "types": "./dist/index.d.ts",
43
+ "default": "./dist/index.js"
44
44
  }
45
45
  }
46
46
  }