@fedify/vocab-runtime 2.4.0-dev.1508 → 2.4.0-dev.1531

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.
Files changed (40) hide show
  1. package/deno.json +2 -1
  2. package/dist/docloader-DnUMWHaJ.d.cts +202 -0
  3. package/dist/docloader-xRGn1azD.d.ts +202 -0
  4. package/dist/internal/jsonld-cache.cjs +279 -0
  5. package/dist/internal/jsonld-cache.d.cts +48 -0
  6. package/dist/internal/jsonld-cache.d.ts +48 -0
  7. package/dist/internal/jsonld-cache.js +275 -0
  8. package/dist/mod.cjs +85 -190
  9. package/dist/mod.d.cts +18 -200
  10. package/dist/mod.d.ts +18 -200
  11. package/dist/mod.js +75 -184
  12. package/dist/tests/decimal.test.cjs +2 -2
  13. package/dist/tests/decimal.test.mjs +2 -2
  14. package/dist/tests/{docloader-0m6FCm4u.mjs → docloader-Bfj7NaT_.mjs} +75 -10
  15. package/dist/tests/{docloader-60uMNsd3.cjs → docloader-Cvdl8PIZ.cjs} +80 -9
  16. package/dist/tests/docloader.test.cjs +58 -6
  17. package/dist/tests/docloader.test.mjs +58 -6
  18. package/dist/tests/jsonld-cache.test.cjs +652 -0
  19. package/dist/tests/jsonld-cache.test.d.cts +1 -0
  20. package/dist/tests/jsonld-cache.test.d.mts +1 -0
  21. package/dist/tests/jsonld-cache.test.mjs +651 -0
  22. package/dist/tests/{request-CO5esooK.mjs → request-B5Su2gl0.mjs} +1 -1
  23. package/dist/tests/{request-CcBmdgDa.cjs → request-DJvOZdo7.cjs} +1 -1
  24. package/dist/tests/request.test.cjs +1 -1
  25. package/dist/tests/request.test.mjs +1 -1
  26. package/dist/tests/{url-C20FhC7p.cjs → url-2XwVbUS_.cjs} +108 -0
  27. package/dist/tests/{url-m9Qzxy-Y.mjs → url-YWJbnRlf.mjs} +85 -1
  28. package/dist/tests/url.test.cjs +104 -1
  29. package/dist/tests/url.test.mjs +105 -2
  30. package/dist/url-BAdyyqAa.cjs +315 -0
  31. package/dist/url-BuxPHxK2.js +261 -0
  32. package/package.json +11 -1
  33. package/src/docloader.test.ts +102 -6
  34. package/src/docloader.ts +76 -8
  35. package/src/internal/jsonld-cache.ts +538 -0
  36. package/src/jsonld-cache.test.ts +554 -0
  37. package/src/mod.ts +4 -0
  38. package/src/url.test.ts +252 -1
  39. package/src/url.ts +141 -0
  40. package/tsdown.config.ts +6 -1
@@ -0,0 +1,48 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { n as DocumentLoader } from "../docloader-DnUMWHaJ.cjs";
3
+
4
+ //#region src/internal/jsonld-cache.d.ts
5
+ /**
6
+ * Options for deciding whether two IRIs should be treated as same-origin.
7
+ *
8
+ * @internal Technically exported for generated vocabulary classes, but not
9
+ * part of the public API contract. This is not considered public API for
10
+ * Semantic Versioning decisions.
11
+ */
12
+ interface IriTrustOptions {
13
+ crossOrigin?: "ignore" | "throw" | "trust";
14
+ }
15
+ /**
16
+ * Checks whether an IRI is trusted relative to another IRI.
17
+ *
18
+ * @internal Technically exported for generated vocabulary classes, but not
19
+ * part of the public API contract. This is not considered public API for
20
+ * Semantic Versioning decisions.
21
+ */
22
+ declare function isTrustedIriOrigin(options: IriTrustOptions, left: URL | null | undefined, right: URL | null | undefined): boolean;
23
+ /**
24
+ * Normalizes portable IRIs in JSON-LD cache data.
25
+ *
26
+ * @internal Technically exported for generated vocabulary classes, but not
27
+ * part of the public API contract. This is not considered public API for
28
+ * Semantic Versioning decisions.
29
+ */
30
+ declare function normalizeJsonLdIris(value: unknown, iriKeys: ReadonlySet<string>, iriPattern?: RegExp): unknown;
31
+ /**
32
+ * Finds the first JSON-LD context in a value.
33
+ *
34
+ * @internal Technically exported for generated vocabulary classes, but not
35
+ * part of the public API contract. This is not considered public API for
36
+ * Semantic Versioning decisions.
37
+ */
38
+ declare function getJsonLdContext(value: unknown, depth?: number): unknown;
39
+ /**
40
+ * Recompacts normalized JSON-LD cache data against the original context.
41
+ *
42
+ * @internal Technically exported for generated vocabulary classes, but not
43
+ * part of the public API contract. This is not considered public API for
44
+ * Semantic Versioning decisions.
45
+ */
46
+ declare function compactJsonLdCache(normalized: unknown, original: unknown, documentLoader?: DocumentLoader, depth?: number, inheritedContext?: unknown): Promise<unknown>;
47
+ //#endregion
48
+ export { IriTrustOptions, compactJsonLdCache, getJsonLdContext, isTrustedIriOrigin, normalizeJsonLdIris };
@@ -0,0 +1,48 @@
1
+ /// <reference lib="esnext.temporal" />
2
+ import { n as DocumentLoader } from "../docloader-xRGn1azD.js";
3
+
4
+ //#region src/internal/jsonld-cache.d.ts
5
+ /**
6
+ * Options for deciding whether two IRIs should be treated as same-origin.
7
+ *
8
+ * @internal Technically exported for generated vocabulary classes, but not
9
+ * part of the public API contract. This is not considered public API for
10
+ * Semantic Versioning decisions.
11
+ */
12
+ interface IriTrustOptions {
13
+ crossOrigin?: "ignore" | "throw" | "trust";
14
+ }
15
+ /**
16
+ * Checks whether an IRI is trusted relative to another IRI.
17
+ *
18
+ * @internal Technically exported for generated vocabulary classes, but not
19
+ * part of the public API contract. This is not considered public API for
20
+ * Semantic Versioning decisions.
21
+ */
22
+ declare function isTrustedIriOrigin(options: IriTrustOptions, left: URL | null | undefined, right: URL | null | undefined): boolean;
23
+ /**
24
+ * Normalizes portable IRIs in JSON-LD cache data.
25
+ *
26
+ * @internal Technically exported for generated vocabulary classes, but not
27
+ * part of the public API contract. This is not considered public API for
28
+ * Semantic Versioning decisions.
29
+ */
30
+ declare function normalizeJsonLdIris(value: unknown, iriKeys: ReadonlySet<string>, iriPattern?: RegExp): unknown;
31
+ /**
32
+ * Finds the first JSON-LD context in a value.
33
+ *
34
+ * @internal Technically exported for generated vocabulary classes, but not
35
+ * part of the public API contract. This is not considered public API for
36
+ * Semantic Versioning decisions.
37
+ */
38
+ declare function getJsonLdContext(value: unknown, depth?: number): unknown;
39
+ /**
40
+ * Recompacts normalized JSON-LD cache data against the original context.
41
+ *
42
+ * @internal Technically exported for generated vocabulary classes, but not
43
+ * part of the public API contract. This is not considered public API for
44
+ * Semantic Versioning decisions.
45
+ */
46
+ declare function compactJsonLdCache(normalized: unknown, original: unknown, documentLoader?: DocumentLoader, depth?: number, inheritedContext?: unknown): Promise<unknown>;
47
+ //#endregion
48
+ export { IriTrustOptions, compactJsonLdCache, getJsonLdContext, isTrustedIriOrigin, normalizeJsonLdIris };
@@ -0,0 +1,275 @@
1
+
2
+ import { i as haveSameIriOrigin, r as formatIri } from "../url-BuxPHxK2.js";
3
+ import jsonld_default from "../jsonld.js";
4
+ //#region src/internal/jsonld-cache.ts
5
+ const noJsonLdContext = Symbol("noJsonLdContext");
6
+ /**
7
+ * Checks whether an IRI is trusted relative to another IRI.
8
+ *
9
+ * @internal Technically exported for generated vocabulary classes, but not
10
+ * part of the public API contract. This is not considered public API for
11
+ * Semantic Versioning decisions.
12
+ */
13
+ function isTrustedIriOrigin(options, left, right) {
14
+ return options.crossOrigin === "trust" || left == null || right != null && haveSameIriOrigin(left, right);
15
+ }
16
+ /**
17
+ * Normalizes portable IRIs in JSON-LD cache data.
18
+ *
19
+ * @internal Technically exported for generated vocabulary classes, but not
20
+ * part of the public API contract. This is not considered public API for
21
+ * Semantic Versioning decisions.
22
+ */
23
+ function normalizeJsonLdIris(value, iriKeys, iriPattern = /^ap(?:\+ef61)?:\/\//i) {
24
+ return normalize(value, void 0, 0, void 0);
25
+ function isIriPosition(key, parentKey) {
26
+ return iriKeys.has(key) || (key === "@value" || key === "@list" || key === "@set") && parentKey != null && iriKeys.has(parentKey);
27
+ }
28
+ function arrayItemParentKey(key, parentKey) {
29
+ return key === "@list" || key === "@set" ? parentKey : key;
30
+ }
31
+ function entryParentKey(entryKey, key, parentKey) {
32
+ if (entryKey === "@list" || entryKey === "@set") return parentKey ?? key;
33
+ return key === "@list" || key === "@set" ? parentKey : key;
34
+ }
35
+ function normalize(value, key, depth = 0, parentKey) {
36
+ if (depth > 32 || key === "@context") return value;
37
+ if (typeof value === "string") {
38
+ if (key != null && isIriPosition(key, parentKey) && iriPattern.test(value)) try {
39
+ return formatIri(value);
40
+ } catch {
41
+ return value;
42
+ }
43
+ return value;
44
+ }
45
+ if (Array.isArray(value)) {
46
+ let clone;
47
+ const itemParentKey = arrayItemParentKey(key, parentKey);
48
+ for (let i = 0; i < value.length; i++) {
49
+ const result = normalize(value[i], key, depth + 1, itemParentKey);
50
+ if (result !== value[i]) {
51
+ clone ??= value.slice(0, i);
52
+ clone.push(result);
53
+ } else if (clone != null) clone.push(value[i]);
54
+ }
55
+ return clone ?? value;
56
+ }
57
+ if (value == null || typeof value !== "object") return value;
58
+ const object = value;
59
+ let clone;
60
+ for (const entryKey of globalThis.Object.keys(object)) {
61
+ const result = normalize(object[entryKey], entryKey, depth + 1, entryParentKey(entryKey, key, parentKey));
62
+ if (result !== object[entryKey]) {
63
+ clone ??= { ...object };
64
+ defineJsonLdProperty(clone, entryKey, result);
65
+ }
66
+ }
67
+ return clone ?? object;
68
+ }
69
+ }
70
+ /**
71
+ * Finds the first JSON-LD context in a value.
72
+ *
73
+ * @internal Technically exported for generated vocabulary classes, but not
74
+ * part of the public API contract. This is not considered public API for
75
+ * Semantic Versioning decisions.
76
+ */
77
+ function getJsonLdContext(value, depth = 0) {
78
+ if (depth > 32) return void 0;
79
+ if (Array.isArray(value)) {
80
+ for (const item of value) {
81
+ const context = getJsonLdContext(item, depth + 1);
82
+ if (context !== void 0) return context;
83
+ }
84
+ return;
85
+ }
86
+ if (value == null || typeof value !== "object" || !("@context" in value)) return;
87
+ return value["@context"];
88
+ }
89
+ function getOwnJsonLdContext(value) {
90
+ if (value == null || typeof value !== "object" || Array.isArray(value) || !("@context" in value)) return noJsonLdContext;
91
+ return value["@context"];
92
+ }
93
+ /**
94
+ * Recompacts normalized JSON-LD cache data against the original context.
95
+ *
96
+ * @internal Technically exported for generated vocabulary classes, but not
97
+ * part of the public API contract. This is not considered public API for
98
+ * Semantic Versioning decisions.
99
+ */
100
+ async function compactJsonLdCache(normalized, original, documentLoader, depth = 0, inheritedContext) {
101
+ if (depth > 32) return normalized;
102
+ if (Array.isArray(original)) {
103
+ const normalizedArray = Array.isArray(normalized) ? normalized : normalized != null && typeof normalized === "object" && "@graph" in normalized && Array.isArray(normalized["@graph"]) ? normalized["@graph"] : void 0;
104
+ if (normalizedArray == null) return normalized;
105
+ let clone;
106
+ for (let i = 0; i < normalizedArray.length; i++) {
107
+ const item = await compactJsonLdCache(normalizedArray[i], original[i], documentLoader, depth + 1, inheritedContext);
108
+ if (item !== normalizedArray[i]) {
109
+ clone ??= normalizedArray.slice(0, i);
110
+ clone.push(item);
111
+ } else if (clone != null) clone.push(normalizedArray[i]);
112
+ }
113
+ return clone ?? (Array.isArray(normalized) ? normalized : normalizedArray);
114
+ }
115
+ const ownContext = getOwnJsonLdContext(original);
116
+ const context = ownContext === noJsonLdContext ? inheritedContext : ownContext;
117
+ if (context == null) return preserveNoContextJsonLdShape(normalized, original, depth);
118
+ return await preserveJsonLdShape(await mergeUnmappedTerms(await jsonld_default.compact(Array.isArray(normalized) && normalized.length === 1 ? normalized[0] : normalized, context, { documentLoader }), original, context, documentLoader), original, context, documentLoader, depth);
119
+ }
120
+ function preserveNoContextJsonLdShape(normalized, original, depth = 0) {
121
+ if (depth > 32) return normalized;
122
+ if (original == null || typeof original !== "object" || Array.isArray(original)) return normalized;
123
+ const normalizedObject = Array.isArray(normalized) && normalized.length === 1 ? normalized[0] : normalized;
124
+ if (normalizedObject == null || typeof normalizedObject !== "object" || Array.isArray(normalizedObject)) return normalized;
125
+ const source = normalizedObject;
126
+ const originalObject = original;
127
+ let clone;
128
+ for (const key of globalThis.Object.keys(originalObject)) {
129
+ if (!globalThis.Object.prototype.hasOwnProperty.call(source, key)) continue;
130
+ const value = preserveNoContextValue(source[key], originalObject[key], depth + 1);
131
+ if (value !== originalObject[key]) {
132
+ clone ??= { ...originalObject };
133
+ defineJsonLdProperty(clone, key, value);
134
+ }
135
+ }
136
+ return clone ?? original;
137
+ }
138
+ function preserveNoContextValue(normalized, original, depth) {
139
+ if (depth > 32) return normalized;
140
+ if (Array.isArray(original)) {
141
+ const normalizedArray = Array.isArray(normalized) ? normalized : [normalized];
142
+ let clone;
143
+ for (let i = 0; i < original.length; i++) {
144
+ const value = preserveNoContextValue(normalizedArray[i], original[i], depth + 1);
145
+ if (value !== original[i]) {
146
+ clone ??= original.slice(0, i);
147
+ clone.push(value);
148
+ } else if (clone != null) clone.push(original[i]);
149
+ }
150
+ return clone ?? original;
151
+ }
152
+ const normalizedValue = Array.isArray(normalized) ? normalized[0] : normalized;
153
+ if (normalizedValue == null || typeof normalizedValue !== "object" || Array.isArray(normalizedValue)) return normalizedValue;
154
+ const normalizedObject = normalizedValue;
155
+ if (original != null && typeof original === "object" && !Array.isArray(original)) return preserveNoContextJsonLdShape(normalizedObject, original, depth + 1);
156
+ if (typeof normalizedObject["@id"] === "string") return normalizedObject["@id"];
157
+ if ("@value" in normalizedObject) return normalizedObject["@value"];
158
+ return normalizedValue;
159
+ }
160
+ function getTopLevelTerms(value) {
161
+ const terms = /* @__PURE__ */ new Set();
162
+ const nodes = Array.isArray(value) ? value : [value];
163
+ for (const node of nodes) {
164
+ if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
165
+ for (const key of globalThis.Object.keys(node)) {
166
+ if (key.startsWith("@")) continue;
167
+ terms.add(key);
168
+ }
169
+ }
170
+ return terms;
171
+ }
172
+ async function mergeUnmappedTerms(compacted, original, context, documentLoader) {
173
+ if (original == null || typeof original !== "object" || Array.isArray(original) || compacted == null || typeof compacted !== "object" || Array.isArray(compacted)) return compacted;
174
+ const unmappedKeys = globalThis.Object.keys(original).filter((key) => key !== "@context" && !globalThis.Object.prototype.hasOwnProperty.call(compacted, key));
175
+ if (unmappedKeys.length < 1) return compacted;
176
+ const result = { ...compacted };
177
+ const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
178
+ "@context": context,
179
+ ...compacted
180
+ } : compacted;
181
+ const compactedTerms = getTopLevelTerms(await jsonld_default.expand(compactedWithContext, { documentLoader }));
182
+ const dummyPrefix = "urn:fedify:dummy:";
183
+ const dummy = { "@context": context };
184
+ for (let i = 0; i < unmappedKeys.length; i++) defineJsonLdProperty(dummy, unmappedKeys[i], `${dummyPrefix}${i}`);
185
+ const expanded = await jsonld_default.expand(dummy, { documentLoader });
186
+ const representedKeys = /* @__PURE__ */ new Set();
187
+ const nodes = Array.isArray(expanded) ? expanded : [expanded];
188
+ for (const node of nodes) {
189
+ if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
190
+ for (const [term, termValue] of globalThis.Object.entries(node)) {
191
+ if (!compactedTerms.has(term)) continue;
192
+ for (let i = 0; i < unmappedKeys.length; i++) if (containsValue(termValue, `${dummyPrefix}${i}`)) representedKeys.add(unmappedKeys[i]);
193
+ }
194
+ }
195
+ for (const key of unmappedKeys) if (!representedKeys.has(key)) defineJsonLdProperty(result, key, original[key]);
196
+ return result;
197
+ }
198
+ function defineJsonLdProperty(object, key, value) {
199
+ globalThis.Object.defineProperty(object, key, {
200
+ value,
201
+ enumerable: true,
202
+ configurable: true,
203
+ writable: true
204
+ });
205
+ }
206
+ function containsValue(value, expected, depth = 0) {
207
+ if (depth > 32) return false;
208
+ if (value === expected) return true;
209
+ if (Array.isArray(value)) return value.some((item) => containsValue(item, expected, depth + 1));
210
+ if (value == null || typeof value !== "object") return false;
211
+ return globalThis.Object.entries(value).some(([key, item]) => key !== "@context" && containsValue(item, expected, depth + 1));
212
+ }
213
+ async function preserveJsonLdShape(compacted, original, context, documentLoader, depth = 0) {
214
+ if (depth > 32) return compacted;
215
+ if (compacted === original) return compacted;
216
+ if (original == null || typeof original !== "object" || compacted == null || typeof compacted !== "object") return compacted;
217
+ if (Array.isArray(original)) {
218
+ const compactedArray = Array.isArray(compacted) ? compacted : "@graph" in compacted && Array.isArray(compacted["@graph"]) ? compacted["@graph"] : void 0;
219
+ if (compactedArray == null) return compacted;
220
+ let clone;
221
+ for (let i = 0; i < compactedArray.length; i++) {
222
+ const value = await preserveJsonLdShape(compactedArray[i], original[i], context, documentLoader, depth + 1);
223
+ const originalContext = original[i] != null && typeof original[i] === "object" && !Array.isArray(original[i]) && "@context" in original[i] ? original[i]["@context"] : void 0;
224
+ const shaped = originalContext !== void 0 && value != null && typeof value === "object" && !Array.isArray(value) && !("@context" in value) ? {
225
+ "@context": originalContext,
226
+ ...value
227
+ } : value;
228
+ if (shaped !== compactedArray[i]) {
229
+ clone ??= compactedArray.slice(0, i);
230
+ clone.push(shaped);
231
+ } else if (clone != null) clone.push(compactedArray[i]);
232
+ }
233
+ return clone ?? (Array.isArray(compacted) ? compacted : compactedArray);
234
+ }
235
+ if (Array.isArray(compacted)) return compacted;
236
+ let clone;
237
+ const ownContext = getOwnJsonLdContext(original);
238
+ const objectContext = combineContexts(context, ownContext);
239
+ const objectCompacted = depth > 0 && ownContext === null ? await compactWithEmptyContext(compacted, context, documentLoader) : compacted;
240
+ const compactedObject = depth > 0 ? await mergeUnmappedTerms(objectCompacted, original, objectContext, documentLoader) : objectCompacted;
241
+ const originalObject = original;
242
+ for (const key of globalThis.Object.keys(compactedObject)) {
243
+ if (key === "@context") continue;
244
+ const value = await preserveJsonLdShape(compactedObject[key], originalObject[key], objectContext, documentLoader, depth + 1);
245
+ const shaped = Array.isArray(originalObject[key]) && !Array.isArray(value) ? [value] : value;
246
+ if (shaped !== compactedObject[key]) {
247
+ clone ??= { ...compactedObject };
248
+ defineJsonLdProperty(clone, key, shaped);
249
+ }
250
+ }
251
+ if (depth > 0) {
252
+ if ("@context" in originalObject && !("@context" in compactedObject)) {
253
+ clone ??= { ...compactedObject };
254
+ clone["@context"] = originalObject["@context"];
255
+ }
256
+ }
257
+ return clone ?? compactedObject;
258
+ }
259
+ async function compactWithEmptyContext(compacted, context, documentLoader) {
260
+ const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
261
+ "@context": context,
262
+ ...compacted
263
+ } : compacted;
264
+ const expanded = await jsonld_default.expand(compactedWithContext, { documentLoader });
265
+ return await jsonld_default.compact(expanded, {}, { documentLoader });
266
+ }
267
+ function combineContexts(inheritedContext, ownContext) {
268
+ if (ownContext === noJsonLdContext || ownContext === inheritedContext) return inheritedContext;
269
+ if (ownContext == null) return ownContext;
270
+ const inherited = Array.isArray(inheritedContext) ? inheritedContext : [inheritedContext];
271
+ const own = Array.isArray(ownContext) ? ownContext : [ownContext];
272
+ return [...inherited, ...own];
273
+ }
274
+ //#endregion
275
+ export { compactJsonLdCache, getJsonLdContext, isTrustedIriOrigin, normalizeJsonLdIris };