@fedify/vocab-runtime 2.4.0-dev.1564 → 2.4.0-dev.1570

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 (42) hide show
  1. package/deno.json +1 -2
  2. package/dist/mod.cjs +190 -107
  3. package/dist/mod.d.cts +200 -18
  4. package/dist/mod.d.ts +200 -18
  5. package/dist/mod.js +184 -97
  6. package/dist/tests/decimal.test.cjs +2 -2
  7. package/dist/tests/decimal.test.mjs +2 -2
  8. package/dist/tests/{docloader-CYQvKbtL.mjs → docloader-C76ldE5C.mjs} +10 -97
  9. package/dist/tests/{docloader-UAdXnDwt.cjs → docloader-mvgIWKI7.cjs} +9 -102
  10. package/dist/tests/docloader.test.cjs +6 -58
  11. package/dist/tests/docloader.test.mjs +6 -58
  12. package/dist/tests/{request-DgAlI7RF.mjs → request-BEXkv1ul.mjs} +1 -1
  13. package/dist/tests/{request-DnzhAfki.cjs → request-SworbvLc.cjs} +1 -1
  14. package/dist/tests/request.test.cjs +1 -1
  15. package/dist/tests/request.test.mjs +1 -1
  16. package/dist/tests/{url-2XwVbUS_.cjs → url-C20FhC7p.cjs} +0 -108
  17. package/dist/tests/{url-YWJbnRlf.mjs → url-m9Qzxy-Y.mjs} +1 -85
  18. package/dist/tests/url.test.cjs +1 -104
  19. package/dist/tests/url.test.mjs +2 -105
  20. package/package.json +1 -11
  21. package/src/contexts.ts +0 -2
  22. package/src/docloader.test.ts +6 -102
  23. package/src/docloader.ts +8 -76
  24. package/src/mod.ts +0 -4
  25. package/src/url.test.ts +1 -252
  26. package/src/url.ts +0 -141
  27. package/tsdown.config.ts +1 -6
  28. package/dist/docloader-DnUMWHaJ.d.cts +0 -202
  29. package/dist/docloader-xRGn1azD.d.ts +0 -202
  30. package/dist/internal/jsonld-cache.cjs +0 -279
  31. package/dist/internal/jsonld-cache.d.cts +0 -48
  32. package/dist/internal/jsonld-cache.d.ts +0 -48
  33. package/dist/internal/jsonld-cache.js +0 -275
  34. package/dist/tests/jsonld-cache.test.cjs +0 -652
  35. package/dist/tests/jsonld-cache.test.d.cts +0 -1
  36. package/dist/tests/jsonld-cache.test.d.mts +0 -1
  37. package/dist/tests/jsonld-cache.test.mjs +0 -651
  38. package/dist/url-BAdyyqAa.cjs +0 -315
  39. package/dist/url-BuxPHxK2.js +0 -261
  40. package/src/contexts/fep-7aa9.json +0 -24
  41. package/src/internal/jsonld-cache.ts +0 -538
  42. package/src/jsonld-cache.test.ts +0 -554
@@ -1,651 +0,0 @@
1
- import { i as haveSameIriOrigin, r as formatIri, s as parseIri } from "./url-YWJbnRlf.mjs";
2
- import { deepStrictEqual, ok, strictEqual } from "node:assert";
3
- import { test } from "node:test";
4
- import jsonld from "jsonld/dist/jsonld.esm.js";
5
- //#region src/jsonld.ts
6
- var jsonld_default = jsonld;
7
- //#endregion
8
- //#region src/internal/jsonld-cache.ts
9
- const noJsonLdContext = Symbol("noJsonLdContext");
10
- /**
11
- * Checks whether an IRI is trusted relative to another IRI.
12
- *
13
- * @internal Technically exported for generated vocabulary classes, but not
14
- * part of the public API contract. This is not considered public API for
15
- * Semantic Versioning decisions.
16
- */
17
- function isTrustedIriOrigin(options, left, right) {
18
- return options.crossOrigin === "trust" || left == null || right != null && haveSameIriOrigin(left, right);
19
- }
20
- /**
21
- * Normalizes portable IRIs in JSON-LD cache data.
22
- *
23
- * @internal Technically exported for generated vocabulary classes, but not
24
- * part of the public API contract. This is not considered public API for
25
- * Semantic Versioning decisions.
26
- */
27
- function normalizeJsonLdIris(value, iriKeys, iriPattern = /^ap(?:\+ef61)?:\/\//i) {
28
- return normalize(value, void 0, 0, void 0);
29
- function isIriPosition(key, parentKey) {
30
- return iriKeys.has(key) || (key === "@value" || key === "@list" || key === "@set") && parentKey != null && iriKeys.has(parentKey);
31
- }
32
- function arrayItemParentKey(key, parentKey) {
33
- return key === "@list" || key === "@set" ? parentKey : key;
34
- }
35
- function entryParentKey(entryKey, key, parentKey) {
36
- if (entryKey === "@list" || entryKey === "@set") return parentKey ?? key;
37
- return key === "@list" || key === "@set" ? parentKey : key;
38
- }
39
- function normalize(value, key, depth = 0, parentKey) {
40
- if (depth > 32 || key === "@context") return value;
41
- if (typeof value === "string") {
42
- if (key != null && isIriPosition(key, parentKey) && iriPattern.test(value)) try {
43
- return formatIri(value);
44
- } catch {
45
- return value;
46
- }
47
- return value;
48
- }
49
- if (Array.isArray(value)) {
50
- let clone;
51
- const itemParentKey = arrayItemParentKey(key, parentKey);
52
- for (let i = 0; i < value.length; i++) {
53
- const result = normalize(value[i], key, depth + 1, itemParentKey);
54
- if (result !== value[i]) {
55
- clone ??= value.slice(0, i);
56
- clone.push(result);
57
- } else if (clone != null) clone.push(value[i]);
58
- }
59
- return clone ?? value;
60
- }
61
- if (value == null || typeof value !== "object") return value;
62
- const object = value;
63
- let clone;
64
- for (const entryKey of globalThis.Object.keys(object)) {
65
- const result = normalize(object[entryKey], entryKey, depth + 1, entryParentKey(entryKey, key, parentKey));
66
- if (result !== object[entryKey]) {
67
- clone ??= { ...object };
68
- defineJsonLdProperty(clone, entryKey, result);
69
- }
70
- }
71
- return clone ?? object;
72
- }
73
- }
74
- /**
75
- * Finds the first JSON-LD context in a value.
76
- *
77
- * @internal Technically exported for generated vocabulary classes, but not
78
- * part of the public API contract. This is not considered public API for
79
- * Semantic Versioning decisions.
80
- */
81
- function getJsonLdContext(value, depth = 0) {
82
- if (depth > 32) return void 0;
83
- if (Array.isArray(value)) {
84
- for (const item of value) {
85
- const context = getJsonLdContext(item, depth + 1);
86
- if (context !== void 0) return context;
87
- }
88
- return;
89
- }
90
- if (value == null || typeof value !== "object" || !("@context" in value)) return;
91
- return value["@context"];
92
- }
93
- function getOwnJsonLdContext(value) {
94
- if (value == null || typeof value !== "object" || Array.isArray(value) || !("@context" in value)) return noJsonLdContext;
95
- return value["@context"];
96
- }
97
- /**
98
- * Recompacts normalized JSON-LD cache data against the original context.
99
- *
100
- * @internal Technically exported for generated vocabulary classes, but not
101
- * part of the public API contract. This is not considered public API for
102
- * Semantic Versioning decisions.
103
- */
104
- async function compactJsonLdCache(normalized, original, documentLoader, depth = 0, inheritedContext) {
105
- if (depth > 32) return normalized;
106
- if (Array.isArray(original)) {
107
- const normalizedArray = Array.isArray(normalized) ? normalized : normalized != null && typeof normalized === "object" && "@graph" in normalized && Array.isArray(normalized["@graph"]) ? normalized["@graph"] : void 0;
108
- if (normalizedArray == null) return normalized;
109
- let clone;
110
- for (let i = 0; i < normalizedArray.length; i++) {
111
- const item = await compactJsonLdCache(normalizedArray[i], original[i], documentLoader, depth + 1, inheritedContext);
112
- if (item !== normalizedArray[i]) {
113
- clone ??= normalizedArray.slice(0, i);
114
- clone.push(item);
115
- } else if (clone != null) clone.push(normalizedArray[i]);
116
- }
117
- return clone ?? (Array.isArray(normalized) ? normalized : normalizedArray);
118
- }
119
- const ownContext = getOwnJsonLdContext(original);
120
- const context = ownContext === noJsonLdContext ? inheritedContext : ownContext;
121
- if (context == null) return preserveNoContextJsonLdShape(normalized, original, depth);
122
- 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);
123
- }
124
- function preserveNoContextJsonLdShape(normalized, original, depth = 0) {
125
- if (depth > 32) return normalized;
126
- if (original == null || typeof original !== "object" || Array.isArray(original)) return normalized;
127
- const normalizedObject = Array.isArray(normalized) && normalized.length === 1 ? normalized[0] : normalized;
128
- if (normalizedObject == null || typeof normalizedObject !== "object" || Array.isArray(normalizedObject)) return normalized;
129
- const source = normalizedObject;
130
- const originalObject = original;
131
- let clone;
132
- for (const key of globalThis.Object.keys(originalObject)) {
133
- if (!globalThis.Object.prototype.hasOwnProperty.call(source, key)) continue;
134
- const value = preserveNoContextValue(source[key], originalObject[key], depth + 1);
135
- if (value !== originalObject[key]) {
136
- clone ??= { ...originalObject };
137
- defineJsonLdProperty(clone, key, value);
138
- }
139
- }
140
- return clone ?? original;
141
- }
142
- function preserveNoContextValue(normalized, original, depth) {
143
- if (depth > 32) return normalized;
144
- if (Array.isArray(original)) {
145
- const normalizedArray = Array.isArray(normalized) ? normalized : [normalized];
146
- let clone;
147
- for (let i = 0; i < original.length; i++) {
148
- const value = preserveNoContextValue(normalizedArray[i], original[i], depth + 1);
149
- if (value !== original[i]) {
150
- clone ??= original.slice(0, i);
151
- clone.push(value);
152
- } else if (clone != null) clone.push(original[i]);
153
- }
154
- return clone ?? original;
155
- }
156
- const normalizedValue = Array.isArray(normalized) ? normalized[0] : normalized;
157
- if (normalizedValue == null || typeof normalizedValue !== "object" || Array.isArray(normalizedValue)) return normalizedValue;
158
- const normalizedObject = normalizedValue;
159
- if (original != null && typeof original === "object" && !Array.isArray(original)) return preserveNoContextJsonLdShape(normalizedObject, original, depth + 1);
160
- if (typeof normalizedObject["@id"] === "string") return normalizedObject["@id"];
161
- if ("@value" in normalizedObject) return normalizedObject["@value"];
162
- return normalizedValue;
163
- }
164
- function getTopLevelTerms(value) {
165
- const terms = /* @__PURE__ */ new Set();
166
- const nodes = Array.isArray(value) ? value : [value];
167
- for (const node of nodes) {
168
- if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
169
- for (const key of globalThis.Object.keys(node)) {
170
- if (key.startsWith("@")) continue;
171
- terms.add(key);
172
- }
173
- }
174
- return terms;
175
- }
176
- async function mergeUnmappedTerms(compacted, original, context, documentLoader) {
177
- if (original == null || typeof original !== "object" || Array.isArray(original) || compacted == null || typeof compacted !== "object" || Array.isArray(compacted)) return compacted;
178
- const unmappedKeys = globalThis.Object.keys(original).filter((key) => key !== "@context" && !globalThis.Object.prototype.hasOwnProperty.call(compacted, key));
179
- if (unmappedKeys.length < 1) return compacted;
180
- const result = { ...compacted };
181
- const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
182
- "@context": context,
183
- ...compacted
184
- } : compacted;
185
- const compactedTerms = getTopLevelTerms(await jsonld_default.expand(compactedWithContext, { documentLoader }));
186
- const dummyPrefix = "urn:fedify:dummy:";
187
- const dummy = { "@context": context };
188
- for (let i = 0; i < unmappedKeys.length; i++) defineJsonLdProperty(dummy, unmappedKeys[i], `${dummyPrefix}${i}`);
189
- const expanded = await jsonld_default.expand(dummy, { documentLoader });
190
- const representedKeys = /* @__PURE__ */ new Set();
191
- const nodes = Array.isArray(expanded) ? expanded : [expanded];
192
- for (const node of nodes) {
193
- if (node == null || typeof node !== "object" || Array.isArray(node)) continue;
194
- for (const [term, termValue] of globalThis.Object.entries(node)) {
195
- if (!compactedTerms.has(term)) continue;
196
- for (let i = 0; i < unmappedKeys.length; i++) if (containsValue(termValue, `${dummyPrefix}${i}`)) representedKeys.add(unmappedKeys[i]);
197
- }
198
- }
199
- for (const key of unmappedKeys) if (!representedKeys.has(key)) defineJsonLdProperty(result, key, original[key]);
200
- return result;
201
- }
202
- function defineJsonLdProperty(object, key, value) {
203
- globalThis.Object.defineProperty(object, key, {
204
- value,
205
- enumerable: true,
206
- configurable: true,
207
- writable: true
208
- });
209
- }
210
- function containsValue(value, expected, depth = 0) {
211
- if (depth > 32) return false;
212
- if (value === expected) return true;
213
- if (Array.isArray(value)) return value.some((item) => containsValue(item, expected, depth + 1));
214
- if (value == null || typeof value !== "object") return false;
215
- return globalThis.Object.entries(value).some(([key, item]) => key !== "@context" && containsValue(item, expected, depth + 1));
216
- }
217
- async function preserveJsonLdShape(compacted, original, context, documentLoader, depth = 0) {
218
- if (depth > 32) return compacted;
219
- if (compacted === original) return compacted;
220
- if (original == null || typeof original !== "object" || compacted == null || typeof compacted !== "object") return compacted;
221
- if (Array.isArray(original)) {
222
- const compactedArray = Array.isArray(compacted) ? compacted : "@graph" in compacted && Array.isArray(compacted["@graph"]) ? compacted["@graph"] : void 0;
223
- if (compactedArray == null) return compacted;
224
- let clone;
225
- for (let i = 0; i < compactedArray.length; i++) {
226
- const value = await preserveJsonLdShape(compactedArray[i], original[i], context, documentLoader, depth + 1);
227
- const originalContext = original[i] != null && typeof original[i] === "object" && !Array.isArray(original[i]) && "@context" in original[i] ? original[i]["@context"] : void 0;
228
- const shaped = originalContext !== void 0 && value != null && typeof value === "object" && !Array.isArray(value) && !("@context" in value) ? {
229
- "@context": originalContext,
230
- ...value
231
- } : value;
232
- if (shaped !== compactedArray[i]) {
233
- clone ??= compactedArray.slice(0, i);
234
- clone.push(shaped);
235
- } else if (clone != null) clone.push(compactedArray[i]);
236
- }
237
- return clone ?? (Array.isArray(compacted) ? compacted : compactedArray);
238
- }
239
- if (Array.isArray(compacted)) return compacted;
240
- let clone;
241
- const ownContext = getOwnJsonLdContext(original);
242
- const objectContext = combineContexts(context, ownContext);
243
- const objectCompacted = depth > 0 && ownContext === null ? await compactWithEmptyContext(compacted, context, documentLoader) : compacted;
244
- const compactedObject = depth > 0 ? await mergeUnmappedTerms(objectCompacted, original, objectContext, documentLoader) : objectCompacted;
245
- const originalObject = original;
246
- for (const key of globalThis.Object.keys(compactedObject)) {
247
- if (key === "@context") continue;
248
- const value = await preserveJsonLdShape(compactedObject[key], originalObject[key], objectContext, documentLoader, depth + 1);
249
- const shaped = Array.isArray(originalObject[key]) && !Array.isArray(value) ? [value] : value;
250
- if (shaped !== compactedObject[key]) {
251
- clone ??= { ...compactedObject };
252
- defineJsonLdProperty(clone, key, shaped);
253
- }
254
- }
255
- if (depth > 0) {
256
- if ("@context" in originalObject && !("@context" in compactedObject)) {
257
- clone ??= { ...compactedObject };
258
- clone["@context"] = originalObject["@context"];
259
- }
260
- }
261
- return clone ?? compactedObject;
262
- }
263
- async function compactWithEmptyContext(compacted, context, documentLoader) {
264
- const compactedWithContext = compacted != null && typeof compacted === "object" && !Array.isArray(compacted) && !("@context" in compacted) ? {
265
- "@context": context,
266
- ...compacted
267
- } : compacted;
268
- const expanded = await jsonld_default.expand(compactedWithContext, { documentLoader });
269
- return await jsonld_default.compact(expanded, {}, { documentLoader });
270
- }
271
- function combineContexts(inheritedContext, ownContext) {
272
- if (ownContext === noJsonLdContext || ownContext === inheritedContext) return inheritedContext;
273
- if (ownContext == null) return ownContext;
274
- const inherited = Array.isArray(inheritedContext) ? inheritedContext : [inheritedContext];
275
- const own = Array.isArray(ownContext) ? ownContext : [ownContext];
276
- return [...inherited, ...own];
277
- }
278
- //#endregion
279
- //#region src/jsonld-cache.test.ts
280
- test("isTrustedIriOrigin() trusts same portable IRI origins", () => {
281
- ok(isTrustedIriOrigin({}, parseIri("ap://did:key:z6Mkabc/actor"), parseIri("ap+ef61://did:key:z6Mkabc/outbox")));
282
- ok(!isTrustedIriOrigin({}, parseIri("ap://did:key:z6Mkabc/actor"), parseIri("ap://did:key:z6Mkdef/outbox")));
283
- ok(isTrustedIriOrigin({ crossOrigin: "trust" }, parseIri("ap://did:key:z6Mkabc/actor"), parseIri("ap://did:key:z6Mkdef/outbox")));
284
- });
285
- test("normalizeJsonLdIris() normalizes selected JSON-LD IRI positions", () => {
286
- deepStrictEqual(normalizeJsonLdIris({
287
- "@id": "ap://did:key:z6Mkabc/object",
288
- "https://example.com/ns#ref": [{ "@value": "ap://did:key:z6Mkabc/ref" }],
289
- "https://example.com/ns#text": [{ "@value": "ap://did:key:z6Mkabc/not-an-iri-position" }]
290
- }, new Set(["@id", "https://example.com/ns#ref"])), {
291
- "@id": "ap+ef61://did:key:z6Mkabc/object",
292
- "https://example.com/ns#ref": [{ "@value": "ap+ef61://did:key:z6Mkabc/ref" }],
293
- "https://example.com/ns#text": [{ "@value": "ap://did:key:z6Mkabc/not-an-iri-position" }]
294
- });
295
- });
296
- test("normalizeJsonLdIris() preserves IRI context in list/set wrappers", () => {
297
- deepStrictEqual(normalizeJsonLdIris({ "https://example.com/ns#ref": [{ "@list": [{ "@value": "ap://did:key:z6Mkabc/listed" }] }, { "@set": [{ "@value": "ap://did:key:z6Mkabc/set" }] }] }, new Set(["https://example.com/ns#ref"])), { "https://example.com/ns#ref": [{ "@list": [{ "@value": "ap+ef61://did:key:z6Mkabc/listed" }] }, { "@set": [{ "@value": "ap+ef61://did:key:z6Mkabc/set" }] }] });
298
- });
299
- test("normalizeJsonLdIris() defines prototype-like keys safely", () => {
300
- const iriKeys = new Set(["__proto__"]);
301
- const value = {};
302
- globalThis.Object.defineProperty(value, "__proto__", {
303
- value: "ap://did:key:z6Mkabc/object",
304
- enumerable: true,
305
- configurable: true,
306
- writable: true
307
- });
308
- const normalized = normalizeJsonLdIris(value, iriKeys);
309
- strictEqual(globalThis.Object.getOwnPropertyDescriptor(normalized, "__proto__")?.value, "ap+ef61://did:key:z6Mkabc/object");
310
- strictEqual(globalThis.Object.getPrototypeOf(normalized), Object.prototype);
311
- });
312
- test("getJsonLdContext() finds nested contexts", () => {
313
- const context = { name: "https://example.com/ns#name" };
314
- deepStrictEqual(getJsonLdContext([{ type: "Note" }, { "@context": context }]), context);
315
- });
316
- test("compactJsonLdCache() preserves no-context object shape", async () => {
317
- const original = {
318
- "@id": "ap://did:key:z6Mkabc/objects/1",
319
- "@type": ["https://www.w3.org/ns/activitystreams#Note"],
320
- "https://www.w3.org/ns/activitystreams#attributedTo": [{ "@id": "ap://did:key:z6Mkabc/actor" }],
321
- "https://www.w3.org/ns/activitystreams#content": [{ "@value": "No-context object shape should stay cached." }]
322
- };
323
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id", "https://www.w3.org/ns/activitystreams#attributedTo"])), original), {
324
- "@id": "ap+ef61://did:key:z6Mkabc/objects/1",
325
- "@type": ["https://www.w3.org/ns/activitystreams#Note"],
326
- "https://www.w3.org/ns/activitystreams#attributedTo": [{ "@id": "ap+ef61://did:key:z6Mkabc/actor" }],
327
- "https://www.w3.org/ns/activitystreams#content": [{ "@value": "No-context object shape should stay cached." }]
328
- });
329
- });
330
- test("compactJsonLdCache() ignores nested contexts for no-context parents", async () => {
331
- const nestedContext = {
332
- type: "@type",
333
- name: "https://www.w3.org/ns/activitystreams#name"
334
- };
335
- const original = {
336
- "@id": "ap://did:key:z6Mkabc/objects/1",
337
- "@type": ["https://www.w3.org/ns/activitystreams#Note"],
338
- "https://www.w3.org/ns/activitystreams#attachment": [{
339
- "@context": nestedContext,
340
- type: "https://www.w3.org/ns/activitystreams#Object",
341
- name: "Nested object"
342
- }]
343
- };
344
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
345
- "@id": "ap+ef61://did:key:z6Mkabc/objects/1",
346
- "@type": ["https://www.w3.org/ns/activitystreams#Note"],
347
- "https://www.w3.org/ns/activitystreams#attachment": [{
348
- "@context": nestedContext,
349
- type: "https://www.w3.org/ns/activitystreams#Object",
350
- name: "Nested object"
351
- }]
352
- });
353
- });
354
- test("compactJsonLdCache() defines no-context prototype-like keys safely", async () => {
355
- const original = { "@id": "ap://did:key:z6Mkabc/objects/1" };
356
- globalThis.Object.defineProperty(original, "__proto__", {
357
- value: "ap://did:key:z6Mkabc/proto",
358
- enumerable: true,
359
- configurable: true,
360
- writable: true
361
- });
362
- const normalized = { "@id": "ap+ef61://did:key:z6Mkabc/objects/1" };
363
- globalThis.Object.defineProperty(normalized, "__proto__", {
364
- value: "ap+ef61://did:key:z6Mkabc/proto",
365
- enumerable: true,
366
- configurable: true,
367
- writable: true
368
- });
369
- const compacted = await compactJsonLdCache([normalized], original);
370
- strictEqual(globalThis.Object.getOwnPropertyDescriptor(compacted, "__proto__")?.value, "ap+ef61://did:key:z6Mkabc/proto");
371
- strictEqual(globalThis.Object.getPrototypeOf(compacted), Object.prototype);
372
- });
373
- test("compactJsonLdCache() preserves nested unmapped terms", async () => {
374
- const context = {
375
- as: "https://www.w3.org/ns/activitystreams#",
376
- id: "@id",
377
- type: "@type",
378
- attachment: { "@id": "as:attachment" },
379
- name: "as:name"
380
- };
381
- const original = {
382
- "@context": context,
383
- type: "as:Note",
384
- id: "ap://did:key:z6Mkabc/objects/1",
385
- attachment: {
386
- type: "as:Object",
387
- name: "Attachment with an unmapped extension.",
388
- extra: "This nested unmapped property should stay cached."
389
- }
390
- };
391
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
392
- "@context": context,
393
- type: "as:Note",
394
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
395
- attachment: {
396
- type: "as:Object",
397
- name: "Attachment with an unmapped extension.",
398
- extra: "This nested unmapped property should stay cached."
399
- }
400
- });
401
- });
402
- test("compactJsonLdCache() reuses unchanged unmapped values", async () => {
403
- const context = {
404
- as: "https://www.w3.org/ns/activitystreams#",
405
- id: "@id",
406
- type: "@type",
407
- attachment: { "@id": "as:attachment" },
408
- name: "as:name"
409
- };
410
- const extra = { source: "unchanged nested extension" };
411
- const rootExtra = { source: "unchanged root extension" };
412
- const original = {
413
- "@context": context,
414
- type: "as:Note",
415
- id: "ap://did:key:z6Mkabc/objects/1",
416
- rootExtra,
417
- attachment: {
418
- type: "as:Object",
419
- name: "Attachment with an unmapped extension.",
420
- extra
421
- }
422
- };
423
- const compacted = await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original);
424
- strictEqual(compacted.rootExtra, rootExtra);
425
- strictEqual(compacted.attachment.extra, extra);
426
- });
427
- test("compactJsonLdCache() defines prototype-like unmapped keys safely", async () => {
428
- const context = {
429
- id: "@id",
430
- type: "@type"
431
- };
432
- const protoValue = { source: "own __proto__ extension" };
433
- const original = {
434
- "@context": context,
435
- type: "https://example.com/ns#Object",
436
- id: "ap://did:key:z6Mkabc/objects/1"
437
- };
438
- globalThis.Object.defineProperty(original, "__proto__", {
439
- value: protoValue,
440
- enumerable: true,
441
- configurable: true,
442
- writable: true
443
- });
444
- const compacted = await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original);
445
- strictEqual(globalThis.Object.getOwnPropertyDescriptor(compacted, "__proto__")?.value, protoValue);
446
- strictEqual(globalThis.Object.getPrototypeOf(compacted), Object.prototype);
447
- });
448
- test("compactJsonLdCache() checks prototype-like aliases safely", async () => {
449
- const context = {
450
- ex: "https://example.com/ns#",
451
- id: "@id",
452
- represented: "ex:represented"
453
- };
454
- globalThis.Object.defineProperty(context, "__proto__", {
455
- value: "ex:represented",
456
- enumerable: true,
457
- configurable: true,
458
- writable: true
459
- });
460
- const original = {
461
- "@context": context,
462
- id: "ap://did:key:z6Mkabc/objects/1",
463
- represented: "Compacted term already present."
464
- };
465
- globalThis.Object.defineProperty(original, "__proto__", {
466
- value: "Alias represented by the compacted term.",
467
- enumerable: true,
468
- configurable: true,
469
- writable: true
470
- });
471
- const compacted = await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original);
472
- const protoDescriptor = globalThis.Object.getOwnPropertyDescriptor(compacted, "__proto__");
473
- ok(protoDescriptor?.value === "Alias represented by the compacted term." || Array.isArray(protoDescriptor?.value) && protoDescriptor.value.includes("Alias represented by the compacted term."));
474
- strictEqual(protoDescriptor?.enumerable, true);
475
- strictEqual(compacted.id, "ap+ef61://did:key:z6Mkabc/objects/1");
476
- strictEqual(globalThis.Object.getPrototypeOf(compacted), Object.prototype);
477
- });
478
- test("compactJsonLdCache() preserves nested contexts", async () => {
479
- const context = {
480
- as: "https://www.w3.org/ns/activitystreams#",
481
- id: "@id",
482
- type: "@type",
483
- attachment: { "@id": "as:attachment" },
484
- name: "as:name"
485
- };
486
- const nestedContext = {
487
- id: "@id",
488
- type: "@type",
489
- title: "https://example.com/ns#title"
490
- };
491
- const original = {
492
- "@context": context,
493
- type: "as:Note",
494
- id: "ap://did:key:z6Mkabc/objects/1",
495
- attachment: {
496
- "@context": nestedContext,
497
- type: "as:Object",
498
- title: "Nested title."
499
- }
500
- };
501
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
502
- "@context": context,
503
- type: "as:Note",
504
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
505
- attachment: {
506
- "@context": nestedContext,
507
- type: "as:Object",
508
- "https://example.com/ns#title": "Nested title."
509
- }
510
- });
511
- });
512
- test("compactJsonLdCache() preserves nested array contexts", async () => {
513
- const context = {
514
- as: "https://www.w3.org/ns/activitystreams#",
515
- id: "@id",
516
- type: "@type",
517
- attachment: { "@id": "as:attachment" }
518
- };
519
- const nestedContext = [{
520
- id: "@id",
521
- type: "@type"
522
- }, { title: "https://example.com/ns#title" }];
523
- const original = {
524
- "@context": context,
525
- type: "as:Note",
526
- id: "ap://did:key:z6Mkabc/objects/1",
527
- attachment: {
528
- "@context": nestedContext,
529
- type: "as:Object",
530
- title: "Nested title."
531
- }
532
- };
533
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
534
- "@context": context,
535
- type: "as:Note",
536
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
537
- attachment: {
538
- "@context": nestedContext,
539
- type: "as:Object",
540
- "https://example.com/ns#title": "Nested title."
541
- }
542
- });
543
- });
544
- test("compactJsonLdCache() preserves nested null contexts", async () => {
545
- const context = {
546
- as: "https://www.w3.org/ns/activitystreams#",
547
- id: "@id",
548
- type: "@type",
549
- attachment: { "@id": "as:attachment" },
550
- title: "https://example.com/ns#title"
551
- };
552
- const original = {
553
- "@context": context,
554
- type: "as:Note",
555
- id: "ap://did:key:z6Mkabc/objects/1",
556
- attachment: {
557
- "@context": null,
558
- "https://example.com/ns#title": "Absolute title."
559
- }
560
- };
561
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
562
- "@context": context,
563
- type: "as:Note",
564
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
565
- attachment: {
566
- "@context": null,
567
- "https://example.com/ns#title": "Absolute title."
568
- }
569
- });
570
- });
571
- test("compactJsonLdCache() does not re-add represented nested aliases", async () => {
572
- const context = {
573
- as: "https://www.w3.org/ns/activitystreams#",
574
- id: "@id",
575
- type: "@type",
576
- attachment: { "@id": "as:attachment" },
577
- actor: {
578
- "@id": "as:actor",
579
- "@type": "@id"
580
- }
581
- };
582
- const nestedContext = {
583
- id: "@id",
584
- type: "@type",
585
- actorRef: {
586
- "@id": "as:actor",
587
- "@type": "@id"
588
- }
589
- };
590
- const original = {
591
- "@context": context,
592
- type: "as:Note",
593
- id: "ap://did:key:z6Mkabc/objects/1",
594
- attachment: {
595
- "@context": nestedContext,
596
- type: "as:Object",
597
- actorRef: "ap://did:key:z6Mkabc/actor"
598
- }
599
- };
600
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id", "https://www.w3.org/ns/activitystreams#actor"])), original), {
601
- "@context": context,
602
- type: "as:Note",
603
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
604
- attachment: {
605
- "@context": nestedContext,
606
- type: "as:Object",
607
- actor: "ap+ef61://did:key:z6Mkabc/actor"
608
- }
609
- });
610
- });
611
- test("compactJsonLdCache() does not confuse dummy marker prefixes", async () => {
612
- const context = {
613
- ex: "https://example.com/ns#",
614
- id: "@id",
615
- represented: "ex:represented",
616
- key10: "ex:represented"
617
- };
618
- const original = {
619
- "@context": context,
620
- id: "ap://did:key:z6Mkabc/objects/1",
621
- represented: "Compacted term already present.",
622
- key0: "Extension 0",
623
- key1: "Extension 1",
624
- key2: "Extension 2",
625
- key3: "Extension 3",
626
- key4: "Extension 4",
627
- key5: "Extension 5",
628
- key6: "Extension 6",
629
- key7: "Extension 7",
630
- key8: "Extension 8",
631
- key9: "Extension 9",
632
- key10: "Alias represented by the compacted term."
633
- };
634
- deepStrictEqual(await compactJsonLdCache(normalizeJsonLdIris(await jsonld_default.expand(original), new Set(["@id"])), original), {
635
- "@context": context,
636
- id: "ap+ef61://did:key:z6Mkabc/objects/1",
637
- key0: "Extension 0",
638
- key1: "Extension 1",
639
- key2: "Extension 2",
640
- key3: "Extension 3",
641
- key4: "Extension 4",
642
- key5: "Extension 5",
643
- key6: "Extension 6",
644
- key7: "Extension 7",
645
- key8: "Extension 8",
646
- key9: "Extension 9",
647
- key10: ["Alias represented by the compacted term.", "Compacted term already present."]
648
- });
649
- });
650
- //#endregion
651
- export {};