@amritk/resolve-refs 0.4.0 → 0.4.1
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/README.md +27 -19
- package/dist/is-private-host.js +69 -7
- package/package.json +1 -1
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`
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
|
110
|
-
|
|
|
111
|
-
| cyclic tree | ~
|
|
112
|
-
| wide-distinct (60 defs, each used once) | ~
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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 **
|
|
123
|
-
output before either is timed.
|
|
131
|
+
roughly **0–20%** on top, within run-to-run noise on these small schemas.
|
package/dist/is-private-host.js
CHANGED
|
@@ -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
|
|
90
|
-
// rewrites
|
|
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
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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.
|
|
3
|
+
"version": "0.4.1",
|
|
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",
|