@amritk/resolve-refs 0.4.2 → 0.4.3
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/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.js +114 -184
- package/dist/resource-registry.js +125 -170
- package/dist/safe-assign.js +9 -14
- package/package.json +2 -2
package/dist/resolve-refs.js
CHANGED
|
@@ -1,190 +1,120 @@
|
|
|
1
|
-
import { pointerToPath } from
|
|
2
|
-
import { readReference, resolveFragment } from
|
|
3
|
-
import { baseOfNode, buildResourceRegistry, resolveRefInScope } from
|
|
4
|
-
import { assignKey } from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const CYCLE = Symbol('cycle');
|
|
9
|
-
// A ref that resolved to nothing; cached so a repeated bad ref reports once.
|
|
10
|
-
const MISSING = Symbol('missing');
|
|
11
|
-
/**
|
|
12
|
-
* OpenAPI 3.1 Reference Objects allow only these annotation keywords beside a
|
|
13
|
-
* `$ref`, and they *override* the target's — an `allOf` wrapper is not valid in
|
|
14
|
-
* those positions (Path Item, Response, Parameter references). They carry no
|
|
15
|
-
* validation semantics in plain JSON Schema either, so overriding is safe there
|
|
16
|
-
* too; every other sibling keyword keeps the spec-correct `allOf` combination.
|
|
17
|
-
*/
|
|
18
|
-
const ANNOTATION_ONLY_SIBLINGS = new Set(['summary', 'description']);
|
|
19
|
-
/**
|
|
20
|
-
* Single-pass internal `$ref` resolver. Each unique ref string is resolved
|
|
21
|
-
* exactly once per scope (the `cache`); revisiting a ref that is still
|
|
22
|
-
* mid-resolution means a cycle, so the original reference node is kept — its
|
|
23
|
-
* target still exists in the resolved document, preserving the recursive
|
|
24
|
-
* branch instead of collapsing it to `{}`. Refs that resolve to nothing in
|
|
25
|
-
* this document (external files, HTTP) are left in place but recorded as an
|
|
26
|
-
* error — callers that need those should use `resolveRefsFromFile`.
|
|
27
|
-
*
|
|
28
|
-
* `base` is the current `$id` base URI, used to scope anchor lookups and to
|
|
29
|
-
* match refs against embedded resources (see `resource-registry.ts`).
|
|
30
|
-
*/
|
|
1
|
+
import { pointerToPath } from "./get-by-pointer.js";
|
|
2
|
+
import { readReference, resolveFragment } from "./reference.js";
|
|
3
|
+
import { baseOfNode, buildResourceRegistry, resolveRefInScope } from "./resource-registry.js";
|
|
4
|
+
import { assignKey } from "./safe-assign.js";
|
|
5
|
+
const CYCLE = /* @__PURE__ */ Symbol("cycle");
|
|
6
|
+
const MISSING = /* @__PURE__ */ Symbol("missing");
|
|
7
|
+
const ANNOTATION_ONLY_SIBLINGS = /* @__PURE__ */ new Set(["summary", "description"]);
|
|
31
8
|
const resolveInternal = (node, root, registry, base, cache, origins, errors) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
if (node === null || typeof node !== "object")
|
|
10
|
+
return node;
|
|
11
|
+
if (Array.isArray(node)) {
|
|
12
|
+
return node.map((item) => resolveInternal(item, root, registry, base, cache, origins, errors));
|
|
13
|
+
}
|
|
14
|
+
const obj = node;
|
|
15
|
+
const nodeBase = typeof obj["$id"] === "string" ? baseOfNode(registry, obj, base) : base;
|
|
16
|
+
const reference = readReference(obj);
|
|
17
|
+
if (reference) {
|
|
18
|
+
const { keyword, value: ref } = reference;
|
|
19
|
+
const externalRef = () => {
|
|
20
|
+
errors.push({
|
|
21
|
+
message: `Cannot resolve external ${keyword} "${ref}": external ref requires resolveRefsFromFile`,
|
|
22
|
+
path: []
|
|
23
|
+
});
|
|
24
|
+
return obj;
|
|
25
|
+
};
|
|
26
|
+
let found;
|
|
27
|
+
let targetBase = nodeBase;
|
|
28
|
+
if (keyword === "$recursiveRef") {
|
|
29
|
+
found = resolveFragment(root, keyword, ref.startsWith("#") ? ref.slice(1) : ref);
|
|
30
|
+
targetBase = registry.rootBase;
|
|
31
|
+
} else {
|
|
32
|
+
const scoped = resolveRefInScope(registry, keyword, ref, nodeBase);
|
|
33
|
+
if (scoped === "external")
|
|
34
|
+
return externalRef();
|
|
35
|
+
if (scoped !== void 0) {
|
|
36
|
+
found = scoped;
|
|
37
|
+
targetBase = scoped.base;
|
|
38
|
+
} else if (ref.startsWith("#")) {
|
|
39
|
+
found = resolveFragment(root, keyword, ref.slice(1));
|
|
40
|
+
targetBase = registry.rootBase;
|
|
41
|
+
} else {
|
|
42
|
+
return externalRef();
|
|
43
|
+
}
|
|
36
44
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const externalRef = () => {
|
|
50
|
-
errors.push({
|
|
51
|
-
message: `Cannot resolve external ${keyword} "${ref}": external ref requires resolveRefsFromFile`,
|
|
52
|
-
path: [],
|
|
53
|
-
});
|
|
54
|
-
return obj;
|
|
55
|
-
};
|
|
56
|
-
// Resolve within the document's `$id` scope. `$recursiveRef` ignores its
|
|
57
|
-
// fragment and binds document-globally, so it skips the scoped path.
|
|
58
|
-
let found;
|
|
59
|
-
let targetBase = nodeBase;
|
|
60
|
-
if (keyword === '$recursiveRef') {
|
|
61
|
-
found = resolveFragment(root, keyword, ref.startsWith('#') ? ref.slice(1) : ref);
|
|
62
|
-
targetBase = registry.rootBase;
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
const scoped = resolveRefInScope(registry, keyword, ref, nodeBase);
|
|
66
|
-
if (scoped === 'external')
|
|
67
|
-
return externalRef();
|
|
68
|
-
if (scoped !== undefined) {
|
|
69
|
-
found = scoped;
|
|
70
|
-
targetBase = scoped.base;
|
|
71
|
-
}
|
|
72
|
-
else if (ref.startsWith('#')) {
|
|
73
|
-
// Scope-aware lookup found nothing; fall back to the document-global
|
|
74
|
-
// search for compatibility with documents that reference an anchor
|
|
75
|
-
// declared in a sibling resource.
|
|
76
|
-
found = resolveFragment(root, keyword, ref.slice(1));
|
|
77
|
-
targetBase = registry.rootBase;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
return externalRef();
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
// Cache/cycle key includes the keyword (`$ref #x` and `$dynamicRef #x` can
|
|
84
|
-
// bind differently) and the base URI (the same anchor name can bind
|
|
85
|
-
// differently inside different embedded resources).
|
|
86
|
-
const cacheKey = `${keyword} ${nodeBase} ${ref}`;
|
|
87
|
-
let target;
|
|
88
|
-
let pointer;
|
|
89
|
-
const cached = cache.get(cacheKey);
|
|
90
|
-
if (cached === MISSING)
|
|
91
|
-
return obj;
|
|
92
|
-
if (cached === CYCLE) {
|
|
93
|
-
// Mid-resolution revisit — a reference cycle. Keep the reference node
|
|
94
|
-
// (siblings resolved, the ref itself verbatim): its target still exists
|
|
95
|
-
// in the resolved document, so consumers resolve the recursion locally
|
|
96
|
-
// rather than finding an empty `{}` where the recursive branch was.
|
|
97
|
-
const kept = {};
|
|
98
|
-
for (const key of Object.keys(obj)) {
|
|
99
|
-
assignKey(kept, key, key === keyword ? obj[key] : resolveInternal(obj[key], root, registry, nodeBase, cache, origins, errors));
|
|
100
|
-
}
|
|
101
|
-
return kept;
|
|
102
|
-
}
|
|
103
|
-
if (cached !== undefined) {
|
|
104
|
-
target = cached.target;
|
|
105
|
-
pointer = cached.pointer;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
cache.set(cacheKey, CYCLE);
|
|
109
|
-
if (found === undefined) {
|
|
110
|
-
// A reference that resolves to nothing (a typo'd pointer or a missing
|
|
111
|
-
// anchor) was previously inlined as literal `undefined` with no trace.
|
|
112
|
-
// Record it and keep the original node so the failure is visible.
|
|
113
|
-
const fragment = ref.startsWith('#') ? ref.slice(1) : ref;
|
|
114
|
-
errors.push({
|
|
115
|
-
message: `Cannot resolve internal ${keyword} "${ref}"`,
|
|
116
|
-
path: fragment === '' || fragment.startsWith('/') ? pointerToPath(fragment) : [],
|
|
117
|
-
});
|
|
118
|
-
cache.set(cacheKey, MISSING);
|
|
119
|
-
return obj;
|
|
120
|
-
}
|
|
121
|
-
pointer = found.pointer;
|
|
122
|
-
target = resolveInternal(found.value, root, registry, targetBase, cache, origins, errors);
|
|
123
|
-
cache.set(cacheKey, { target, pointer });
|
|
124
|
-
// Stamp the inlined node with the path it was defined at (see resolveAt).
|
|
125
|
-
// First-write-wins so the deepest definition stamps before any outer ref that
|
|
126
|
-
// transitively points at the same object. Primitives can't key the map.
|
|
127
|
-
if (origins && target !== null && typeof target === 'object' && !origins.has(target)) {
|
|
128
|
-
origins.set(target, { location: '', pointer });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
const siblingKeys = Object.keys(obj).filter((key) => key !== keyword);
|
|
132
|
-
if (siblingKeys.length === 0)
|
|
133
|
-
return target;
|
|
134
|
-
const siblings = {};
|
|
135
|
-
for (const key of siblingKeys) {
|
|
136
|
-
assignKey(siblings, key, resolveInternal(obj[key], root, registry, nodeBase, cache, origins, errors));
|
|
137
|
-
}
|
|
138
|
-
// Annotation-only siblings (OpenAPI Reference Objects): inline the target
|
|
139
|
-
// with the annotations overriding — never wrap in `allOf`, which is invalid
|
|
140
|
-
// where those references appear (see ANNOTATION_ONLY_SIBLINGS).
|
|
141
|
-
if (siblingKeys.every((key) => ANNOTATION_ONLY_SIBLINGS.has(key))) {
|
|
142
|
-
if (target !== null && typeof target === 'object' && !Array.isArray(target)) {
|
|
143
|
-
const overridden = {};
|
|
144
|
-
for (const key of Object.keys(target))
|
|
145
|
-
assignKey(overridden, key, target[key]);
|
|
146
|
-
for (const key of Object.keys(siblings))
|
|
147
|
-
assignKey(overridden, key, siblings[key]);
|
|
148
|
-
if (origins && !origins.has(overridden))
|
|
149
|
-
origins.set(overridden, { location: '', pointer });
|
|
150
|
-
return overridden;
|
|
151
|
-
}
|
|
152
|
-
// A non-object target (boolean schema, primitive) has no members to
|
|
153
|
-
// override; the annotations have nowhere to live, so return the target.
|
|
154
|
-
return target;
|
|
155
|
-
}
|
|
156
|
-
// Per JSON Schema 2020-12, keywords sibling to `$ref` are *not* ignored: they
|
|
157
|
-
// apply alongside the referenced schema. Preserve them by combining both in an
|
|
158
|
-
// `allOf` (so a constraint present on both sides is never silently dropped)
|
|
159
|
-
// rather than returning the bare target. The cache always stores the
|
|
160
|
-
// sibling-free target so each occurrence applies its own siblings.
|
|
161
|
-
const existingAllOf = Array.isArray(siblings['allOf']) ? siblings['allOf'] : [];
|
|
162
|
-
const merged = { ...siblings, allOf: [...existingAllOf, target] };
|
|
163
|
-
// Stamp the wrapper too, so a consumer mapping the resolved node back to its
|
|
164
|
-
// origin finds one for a `$ref`-with-siblings node (not only the inner target).
|
|
165
|
-
if (origins && !origins.has(merged))
|
|
166
|
-
origins.set(merged, { location: '', pointer });
|
|
167
|
-
return merged;
|
|
45
|
+
const cacheKey = `${keyword} ${nodeBase} ${ref}`;
|
|
46
|
+
let target;
|
|
47
|
+
let pointer;
|
|
48
|
+
const cached = cache.get(cacheKey);
|
|
49
|
+
if (cached === MISSING)
|
|
50
|
+
return obj;
|
|
51
|
+
if (cached === CYCLE) {
|
|
52
|
+
const kept = {};
|
|
53
|
+
for (const key of Object.keys(obj)) {
|
|
54
|
+
assignKey(kept, key, key === keyword ? obj[key] : resolveInternal(obj[key], root, registry, nodeBase, cache, origins, errors));
|
|
55
|
+
}
|
|
56
|
+
return kept;
|
|
168
57
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
58
|
+
if (cached !== void 0) {
|
|
59
|
+
target = cached.target;
|
|
60
|
+
pointer = cached.pointer;
|
|
61
|
+
} else {
|
|
62
|
+
cache.set(cacheKey, CYCLE);
|
|
63
|
+
if (found === void 0) {
|
|
64
|
+
const fragment = ref.startsWith("#") ? ref.slice(1) : ref;
|
|
65
|
+
errors.push({
|
|
66
|
+
message: `Cannot resolve internal ${keyword} "${ref}"`,
|
|
67
|
+
path: fragment === "" || fragment.startsWith("/") ? pointerToPath(fragment) : []
|
|
68
|
+
});
|
|
69
|
+
cache.set(cacheKey, MISSING);
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
pointer = found.pointer;
|
|
73
|
+
target = resolveInternal(found.value, root, registry, targetBase, cache, origins, errors);
|
|
74
|
+
cache.set(cacheKey, { target, pointer });
|
|
75
|
+
if (origins && target !== null && typeof target === "object" && !origins.has(target)) {
|
|
76
|
+
origins.set(target, { location: "", pointer });
|
|
77
|
+
}
|
|
172
78
|
}
|
|
173
|
-
|
|
79
|
+
const siblingKeys = Object.keys(obj).filter((key) => key !== keyword);
|
|
80
|
+
if (siblingKeys.length === 0)
|
|
81
|
+
return target;
|
|
82
|
+
const siblings = {};
|
|
83
|
+
for (const key of siblingKeys) {
|
|
84
|
+
assignKey(siblings, key, resolveInternal(obj[key], root, registry, nodeBase, cache, origins, errors));
|
|
85
|
+
}
|
|
86
|
+
if (siblingKeys.every((key) => ANNOTATION_ONLY_SIBLINGS.has(key))) {
|
|
87
|
+
if (target !== null && typeof target === "object" && !Array.isArray(target)) {
|
|
88
|
+
const overridden = {};
|
|
89
|
+
for (const key of Object.keys(target))
|
|
90
|
+
assignKey(overridden, key, target[key]);
|
|
91
|
+
for (const key of Object.keys(siblings))
|
|
92
|
+
assignKey(overridden, key, siblings[key]);
|
|
93
|
+
if (origins && !origins.has(overridden))
|
|
94
|
+
origins.set(overridden, { location: "", pointer });
|
|
95
|
+
return overridden;
|
|
96
|
+
}
|
|
97
|
+
return target;
|
|
98
|
+
}
|
|
99
|
+
const existingAllOf = Array.isArray(siblings["allOf"]) ? siblings["allOf"] : [];
|
|
100
|
+
const merged = { ...siblings, allOf: [...existingAllOf, target] };
|
|
101
|
+
if (origins && !origins.has(merged))
|
|
102
|
+
origins.set(merged, { location: "", pointer });
|
|
103
|
+
return merged;
|
|
104
|
+
}
|
|
105
|
+
const result = {};
|
|
106
|
+
for (const key of Object.keys(obj)) {
|
|
107
|
+
assignKey(result, key, resolveInternal(obj[key], root, registry, nodeBase, cache, origins, errors));
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
};
|
|
111
|
+
const resolveRefs = (data, options = {}) => {
|
|
112
|
+
const origins = options.trackOrigins ? /* @__PURE__ */ new Map() : void 0;
|
|
113
|
+
const errors = [];
|
|
114
|
+
const registry = buildResourceRegistry(data);
|
|
115
|
+
const resolved = resolveInternal(data, data, registry, registry.rootBase, /* @__PURE__ */ new Map(), origins, errors);
|
|
116
|
+
return origins ? { resolved, errors, origins } : { resolved, errors };
|
|
174
117
|
};
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
* pointers, `$anchor`/`$dynamicAnchor` names (scoped by `$id`), and refs whose
|
|
178
|
-
* URI matches an embedded resource's `$id` — inlining each target. Refs to
|
|
179
|
-
* other files/URLs are left in place and reported on `errors` (this resolver
|
|
180
|
-
* can't load other documents — use `resolveRefsFromFile` for those). Cycles are
|
|
181
|
-
* broken by keeping the original reference node, so recursive schemas keep
|
|
182
|
-
* their recursive branch.
|
|
183
|
-
*/
|
|
184
|
-
export const resolveRefs = (data, options = {}) => {
|
|
185
|
-
const origins = options.trackOrigins ? new Map() : undefined;
|
|
186
|
-
const errors = [];
|
|
187
|
-
const registry = buildResourceRegistry(data);
|
|
188
|
-
const resolved = resolveInternal(data, data, registry, registry.rootBase, new Map(), origins, errors);
|
|
189
|
-
return origins ? { resolved, errors, origins } : { resolved, errors };
|
|
118
|
+
export {
|
|
119
|
+
resolveRefs
|
|
190
120
|
};
|
|
@@ -1,186 +1,141 @@
|
|
|
1
|
-
import { getByPointer, pointerToPath } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// inside an enum member or example value is instance data, not a declaration.
|
|
11
|
-
const NON_SCHEMA_KEYWORDS = new Set(['enum', 'const', 'default', 'examples']);
|
|
12
|
-
/** `new URL(ref, base).href`, or `undefined` when the pair does not parse. */
|
|
13
|
-
export const resolveUri = (ref, base) => {
|
|
14
|
-
try {
|
|
15
|
-
return new URL(ref, base).href;
|
|
16
|
-
}
|
|
17
|
-
catch {
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
1
|
+
import { getByPointer, pointerToPath } from "./get-by-pointer.js";
|
|
2
|
+
const SYNTHETIC_BASE = "https://mjst-internal.invalid/document";
|
|
3
|
+
const NON_SCHEMA_KEYWORDS = /* @__PURE__ */ new Set(["enum", "const", "default", "examples"]);
|
|
4
|
+
const resolveUri = (ref, base) => {
|
|
5
|
+
try {
|
|
6
|
+
return new URL(ref, base).href;
|
|
7
|
+
} catch {
|
|
8
|
+
return void 0;
|
|
9
|
+
}
|
|
20
10
|
};
|
|
21
|
-
/** Strips the fragment from an absolute URI string. */
|
|
22
11
|
const withoutFragment = (uri) => {
|
|
23
|
-
|
|
24
|
-
|
|
12
|
+
const hashIdx = uri.indexOf("#");
|
|
13
|
+
return hashIdx === -1 ? uri : uri.slice(0, hashIdx);
|
|
25
14
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
15
|
+
const splitFragment = (ref) => {
|
|
16
|
+
const hashIdx = ref.indexOf("#");
|
|
17
|
+
return {
|
|
18
|
+
uriPart: hashIdx === -1 ? ref : ref.slice(0, hashIdx),
|
|
19
|
+
fragment: hashIdx === -1 ? "" : ref.slice(hashIdx + 1)
|
|
20
|
+
};
|
|
33
21
|
};
|
|
34
|
-
/** The base URI a subschema establishes via `$id`, or the enclosing base. */
|
|
35
22
|
const baseAfterId = (node, enclosingBase) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return bare === '' ? enclosingBase : bare;
|
|
23
|
+
const id = node["$id"];
|
|
24
|
+
if (typeof id !== "string" || id === "")
|
|
25
|
+
return enclosingBase;
|
|
26
|
+
const resolved = resolveUri(id, enclosingBase);
|
|
27
|
+
if (resolved === void 0)
|
|
28
|
+
return enclosingBase;
|
|
29
|
+
const bare = withoutFragment(resolved);
|
|
30
|
+
return bare === "" ? enclosingBase : bare;
|
|
45
31
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
: initialBase;
|
|
58
|
-
const registerAnchors = (node, base, pointer) => {
|
|
59
|
-
const anchor = node['$anchor'];
|
|
60
|
-
if (typeof anchor === 'string') {
|
|
61
|
-
const key = `${base}#${anchor}`;
|
|
62
|
-
if (!staticAnchors.has(key))
|
|
63
|
-
staticAnchors.set(key, { value: node, pointer });
|
|
64
|
-
}
|
|
65
|
-
const dynamicAnchor = node['$dynamicAnchor'];
|
|
66
|
-
if (typeof dynamicAnchor === 'string') {
|
|
67
|
-
const key = `${base}#${dynamicAnchor}`;
|
|
68
|
-
// Per 2020-12 a `$dynamicAnchor` also creates an ordinary anchor.
|
|
69
|
-
if (!dynamicAnchors.has(key))
|
|
70
|
-
dynamicAnchors.set(key, { value: node, pointer });
|
|
71
|
-
if (!staticAnchors.has(key))
|
|
72
|
-
staticAnchors.set(key, { value: node, pointer });
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const walk = (node, base, pointer) => {
|
|
76
|
-
if (node === null || typeof node !== 'object')
|
|
77
|
-
return;
|
|
78
|
-
if (Array.isArray(node)) {
|
|
79
|
-
for (let i = 0; i < node.length; i++)
|
|
80
|
-
walk(node[i], base, [...pointer, i]);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const record = node;
|
|
84
|
-
const nodeBase = baseAfterId(record, base);
|
|
85
|
-
if (nodeBase !== base && !resources.has(nodeBase))
|
|
86
|
-
resources.set(nodeBase, { value: node, pointer });
|
|
87
|
-
registerAnchors(record, nodeBase, pointer);
|
|
88
|
-
for (const key of Object.keys(record)) {
|
|
89
|
-
if (NON_SCHEMA_KEYWORDS.has(key))
|
|
90
|
-
continue;
|
|
91
|
-
walk(record[key], nodeBase, [...pointer, key]);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
// The root registers under its own `$id` (when it has one) and under the
|
|
95
|
-
// initial base, so both spellings of a self-reference resolve. `$id` first:
|
|
96
|
-
// `baseOfNode` scans in insertion order, and refs inside the root must
|
|
97
|
-
// resolve against its declared base, not the synthetic one.
|
|
98
|
-
if (rootBase !== initialBase)
|
|
99
|
-
resources.set(rootBase, { value: root, pointer: [] });
|
|
100
|
-
if (!resources.has(withoutFragment(initialBase))) {
|
|
101
|
-
resources.set(withoutFragment(initialBase), { value: root, pointer: [] });
|
|
32
|
+
const buildResourceRegistry = (root, initialBase = SYNTHETIC_BASE) => {
|
|
33
|
+
const resources = /* @__PURE__ */ new Map();
|
|
34
|
+
const staticAnchors = /* @__PURE__ */ new Map();
|
|
35
|
+
const dynamicAnchors = /* @__PURE__ */ new Map();
|
|
36
|
+
const rootBase = root !== null && typeof root === "object" && !Array.isArray(root) ? baseAfterId(root, initialBase) : initialBase;
|
|
37
|
+
const registerAnchors = (node, base, pointer) => {
|
|
38
|
+
const anchor = node["$anchor"];
|
|
39
|
+
if (typeof anchor === "string") {
|
|
40
|
+
const key = `${base}#${anchor}`;
|
|
41
|
+
if (!staticAnchors.has(key))
|
|
42
|
+
staticAnchors.set(key, { value: node, pointer });
|
|
102
43
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*/
|
|
111
|
-
const getByPointerWithBase = (start, startBase, startPointer, fragment) => {
|
|
112
|
-
const value = getByPointer(start, fragment);
|
|
113
|
-
if (value === undefined)
|
|
114
|
-
return undefined;
|
|
115
|
-
// Re-walk the same path for base tracking only (cheap: fragment paths are short).
|
|
116
|
-
let base = startBase;
|
|
117
|
-
let current = start;
|
|
118
|
-
for (const segment of pointerToPath(fragment)) {
|
|
119
|
-
if (current === null || typeof current !== 'object')
|
|
120
|
-
break;
|
|
121
|
-
if (!Array.isArray(current))
|
|
122
|
-
base = baseAfterId(current, base);
|
|
123
|
-
current = current[segment];
|
|
44
|
+
const dynamicAnchor = node["$dynamicAnchor"];
|
|
45
|
+
if (typeof dynamicAnchor === "string") {
|
|
46
|
+
const key = `${base}#${dynamicAnchor}`;
|
|
47
|
+
if (!dynamicAnchors.has(key))
|
|
48
|
+
dynamicAnchors.set(key, { value: node, pointer });
|
|
49
|
+
if (!staticAnchors.has(key))
|
|
50
|
+
staticAnchors.set(key, { value: node, pointer });
|
|
124
51
|
}
|
|
125
|
-
|
|
126
|
-
|
|
52
|
+
};
|
|
53
|
+
const walk = (node, base, pointer) => {
|
|
54
|
+
if (node === null || typeof node !== "object")
|
|
55
|
+
return;
|
|
56
|
+
if (Array.isArray(node)) {
|
|
57
|
+
for (let i = 0; i < node.length; i++)
|
|
58
|
+
walk(node[i], base, [...pointer, i]);
|
|
59
|
+
return;
|
|
127
60
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
*/
|
|
138
|
-
export const baseOfNode = (registry, node, enclosing) => {
|
|
139
|
-
for (const [uri, resource] of registry.resources) {
|
|
140
|
-
if (resource.value === node)
|
|
141
|
-
return uri;
|
|
61
|
+
const record = node;
|
|
62
|
+
const nodeBase = baseAfterId(record, base);
|
|
63
|
+
if (nodeBase !== base && !resources.has(nodeBase))
|
|
64
|
+
resources.set(nodeBase, { value: node, pointer });
|
|
65
|
+
registerAnchors(record, nodeBase, pointer);
|
|
66
|
+
for (const key of Object.keys(record)) {
|
|
67
|
+
if (NON_SCHEMA_KEYWORDS.has(key))
|
|
68
|
+
continue;
|
|
69
|
+
walk(record[key], nodeBase, [...pointer, key]);
|
|
142
70
|
}
|
|
143
|
-
|
|
71
|
+
};
|
|
72
|
+
if (rootBase !== initialBase)
|
|
73
|
+
resources.set(rootBase, { value: root, pointer: [] });
|
|
74
|
+
if (!resources.has(withoutFragment(initialBase))) {
|
|
75
|
+
resources.set(withoutFragment(initialBase), { value: root, pointer: [] });
|
|
76
|
+
}
|
|
77
|
+
walk(root, initialBase, []);
|
|
78
|
+
return { resources, staticAnchors, dynamicAnchors, rootBase, root };
|
|
79
|
+
};
|
|
80
|
+
const getByPointerWithBase = (start, startBase, startPointer, fragment) => {
|
|
81
|
+
const value = getByPointer(start, fragment);
|
|
82
|
+
if (value === void 0)
|
|
83
|
+
return void 0;
|
|
84
|
+
let base = startBase;
|
|
85
|
+
let current = start;
|
|
86
|
+
for (const segment of pointerToPath(fragment)) {
|
|
87
|
+
if (current === null || typeof current !== "object")
|
|
88
|
+
break;
|
|
89
|
+
if (!Array.isArray(current))
|
|
90
|
+
base = baseAfterId(current, base);
|
|
91
|
+
current = current[segment];
|
|
92
|
+
}
|
|
93
|
+
if (current !== null && typeof current === "object" && !Array.isArray(current)) {
|
|
94
|
+
base = baseAfterId(current, base);
|
|
95
|
+
}
|
|
96
|
+
return { value, pointer: [...startPointer, ...pointerToPath(fragment)], base };
|
|
97
|
+
};
|
|
98
|
+
const isPointerFragment = (fragment) => fragment === "" || fragment.startsWith("/");
|
|
99
|
+
const baseOfNode = (registry, node, enclosing) => {
|
|
100
|
+
for (const [uri, resource] of registry.resources) {
|
|
101
|
+
if (resource.value === node)
|
|
102
|
+
return uri;
|
|
103
|
+
}
|
|
104
|
+
return enclosing;
|
|
144
105
|
};
|
|
145
|
-
/** Looks up an anchor name under `base`, honoring `$dynamicRef`'s preference order. */
|
|
146
106
|
const lookupAnchor = (registry, keyword, base, name) => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
107
|
+
const key = `${base}#${name}`;
|
|
108
|
+
if (keyword === "$dynamicRef")
|
|
109
|
+
return registry.dynamicAnchors.get(key) ?? registry.staticAnchors.get(key);
|
|
110
|
+
return registry.staticAnchors.get(key);
|
|
151
111
|
};
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
* - a {@link ScopedTarget} when the ref resolves to something in this document
|
|
156
|
-
* (a root-relative pointer, an anchor in scope, or an embedded resource);
|
|
157
|
-
* - `'external'` when the ref names a URI that matches no embedded resource —
|
|
158
|
-
* the caller decides whether to fetch it or leave it untouched;
|
|
159
|
-
* - `undefined` when the ref *should* resolve here but nothing matches (a
|
|
160
|
-
* missing anchor, a bad pointer into an embedded resource) — the caller may
|
|
161
|
-
* fall back to a document-global search for compatibility.
|
|
162
|
-
*/
|
|
163
|
-
export const resolveRefInScope = (registry, keyword, ref, currentBase) => {
|
|
164
|
-
const { uriPart, fragment } = splitFragment(ref);
|
|
165
|
-
if (uriPart === '') {
|
|
166
|
-
// A plain `#/pointer` stays document-root-relative (see module doc).
|
|
167
|
-
if (isPointerFragment(fragment)) {
|
|
168
|
-
return getByPointerWithBase(registry.root, registry.rootBase, [], fragment);
|
|
169
|
-
}
|
|
170
|
-
// An anchor name resolves within the current resource's scope.
|
|
171
|
-
const anchored = lookupAnchor(registry, keyword, currentBase, fragment);
|
|
172
|
-
return anchored === undefined ? undefined : { ...anchored, base: currentBase };
|
|
173
|
-
}
|
|
174
|
-
const absolute = resolveUri(ref, currentBase);
|
|
175
|
-
if (absolute === undefined)
|
|
176
|
-
return 'external';
|
|
177
|
-
const resource = registry.resources.get(withoutFragment(absolute));
|
|
178
|
-
if (resource === undefined)
|
|
179
|
-
return 'external';
|
|
112
|
+
const resolveRefInScope = (registry, keyword, ref, currentBase) => {
|
|
113
|
+
const { uriPart, fragment } = splitFragment(ref);
|
|
114
|
+
if (uriPart === "") {
|
|
180
115
|
if (isPointerFragment(fragment)) {
|
|
181
|
-
|
|
182
|
-
return getByPointerWithBase(resource.value, resourceBase, resource.pointer, fragment);
|
|
116
|
+
return getByPointerWithBase(registry.root, registry.rootBase, [], fragment);
|
|
183
117
|
}
|
|
184
|
-
const
|
|
185
|
-
return
|
|
118
|
+
const anchored2 = lookupAnchor(registry, keyword, currentBase, fragment);
|
|
119
|
+
return anchored2 === void 0 ? void 0 : { ...anchored2, base: currentBase };
|
|
120
|
+
}
|
|
121
|
+
const absolute = resolveUri(ref, currentBase);
|
|
122
|
+
if (absolute === void 0)
|
|
123
|
+
return "external";
|
|
124
|
+
const resource = registry.resources.get(withoutFragment(absolute));
|
|
125
|
+
if (resource === void 0)
|
|
126
|
+
return "external";
|
|
127
|
+
if (isPointerFragment(fragment)) {
|
|
128
|
+
const resourceBase = withoutFragment(absolute);
|
|
129
|
+
return getByPointerWithBase(resource.value, resourceBase, resource.pointer, fragment);
|
|
130
|
+
}
|
|
131
|
+
const anchored = lookupAnchor(registry, keyword, withoutFragment(absolute), fragment);
|
|
132
|
+
return anchored === void 0 ? void 0 : { ...anchored, base: withoutFragment(absolute) };
|
|
133
|
+
};
|
|
134
|
+
export {
|
|
135
|
+
SYNTHETIC_BASE,
|
|
136
|
+
baseOfNode,
|
|
137
|
+
buildResourceRegistry,
|
|
138
|
+
resolveRefInScope,
|
|
139
|
+
resolveUri,
|
|
140
|
+
splitFragment
|
|
186
141
|
};
|
package/dist/safe-assign.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
Object.defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true });
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
target[key] = value;
|
|
14
|
-
}
|
|
1
|
+
const assignKey = (target, key, value) => {
|
|
2
|
+
if (key === "__proto__") {
|
|
3
|
+
Object.defineProperty(target, key, { value, writable: true, enumerable: true, configurable: true });
|
|
4
|
+
} else {
|
|
5
|
+
target[key] = value;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
assignKey
|
|
15
10
|
};
|