@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.
- package/deno.json +2 -1
- package/dist/docloader-DnUMWHaJ.d.cts +202 -0
- package/dist/docloader-xRGn1azD.d.ts +202 -0
- package/dist/internal/jsonld-cache.cjs +279 -0
- package/dist/internal/jsonld-cache.d.cts +48 -0
- package/dist/internal/jsonld-cache.d.ts +48 -0
- package/dist/internal/jsonld-cache.js +275 -0
- package/dist/mod.cjs +85 -190
- package/dist/mod.d.cts +18 -200
- package/dist/mod.d.ts +18 -200
- package/dist/mod.js +75 -184
- package/dist/tests/decimal.test.cjs +2 -2
- package/dist/tests/decimal.test.mjs +2 -2
- package/dist/tests/{docloader-0m6FCm4u.mjs → docloader-Bfj7NaT_.mjs} +75 -10
- package/dist/tests/{docloader-60uMNsd3.cjs → docloader-Cvdl8PIZ.cjs} +80 -9
- package/dist/tests/docloader.test.cjs +58 -6
- package/dist/tests/docloader.test.mjs +58 -6
- package/dist/tests/jsonld-cache.test.cjs +652 -0
- package/dist/tests/jsonld-cache.test.d.cts +1 -0
- package/dist/tests/jsonld-cache.test.d.mts +1 -0
- package/dist/tests/jsonld-cache.test.mjs +651 -0
- package/dist/tests/{request-CO5esooK.mjs → request-B5Su2gl0.mjs} +1 -1
- package/dist/tests/{request-CcBmdgDa.cjs → request-DJvOZdo7.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/dist/tests/{url-C20FhC7p.cjs → url-2XwVbUS_.cjs} +108 -0
- package/dist/tests/{url-m9Qzxy-Y.mjs → url-YWJbnRlf.mjs} +85 -1
- package/dist/tests/url.test.cjs +104 -1
- package/dist/tests/url.test.mjs +105 -2
- package/dist/url-BAdyyqAa.cjs +315 -0
- package/dist/url-BuxPHxK2.js +261 -0
- package/package.json +11 -1
- package/src/docloader.test.ts +102 -6
- package/src/docloader.ts +76 -8
- package/src/internal/jsonld-cache.ts +538 -0
- package/src/jsonld-cache.test.ts +554 -0
- package/src/mod.ts +4 -0
- package/src/url.test.ts +252 -1
- package/src/url.ts +141 -0
- package/tsdown.config.ts +6 -1
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import type { DocumentLoader } from "../docloader.ts";
|
|
2
|
+
import jsonld from "../jsonld.ts";
|
|
3
|
+
import { formatIri, haveSameIriOrigin } from "../url.ts";
|
|
4
|
+
|
|
5
|
+
const noJsonLdContext = Symbol("noJsonLdContext");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for deciding whether two IRIs should be treated as same-origin.
|
|
9
|
+
*
|
|
10
|
+
* @internal Technically exported for generated vocabulary classes, but not
|
|
11
|
+
* part of the public API contract. This is not considered public API for
|
|
12
|
+
* Semantic Versioning decisions.
|
|
13
|
+
*/
|
|
14
|
+
export interface IriTrustOptions {
|
|
15
|
+
crossOrigin?: "ignore" | "throw" | "trust";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Checks whether an IRI is trusted relative to another IRI.
|
|
20
|
+
*
|
|
21
|
+
* @internal Technically exported for generated vocabulary classes, but not
|
|
22
|
+
* part of the public API contract. This is not considered public API for
|
|
23
|
+
* Semantic Versioning decisions.
|
|
24
|
+
*/
|
|
25
|
+
export function isTrustedIriOrigin(
|
|
26
|
+
options: IriTrustOptions,
|
|
27
|
+
left: URL | null | undefined,
|
|
28
|
+
right: URL | null | undefined,
|
|
29
|
+
): boolean {
|
|
30
|
+
return options.crossOrigin === "trust" || left == null ||
|
|
31
|
+
(right != null && haveSameIriOrigin(left, right));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Normalizes portable IRIs in JSON-LD cache data.
|
|
36
|
+
*
|
|
37
|
+
* @internal Technically exported for generated vocabulary classes, but not
|
|
38
|
+
* part of the public API contract. This is not considered public API for
|
|
39
|
+
* Semantic Versioning decisions.
|
|
40
|
+
*/
|
|
41
|
+
export function normalizeJsonLdIris(
|
|
42
|
+
value: unknown,
|
|
43
|
+
iriKeys: ReadonlySet<string>,
|
|
44
|
+
iriPattern: RegExp = /^ap(?:\+ef61)?:\/\//i,
|
|
45
|
+
): unknown {
|
|
46
|
+
return normalize(value, undefined, 0, undefined);
|
|
47
|
+
|
|
48
|
+
function isIriPosition(key: string, parentKey?: string): boolean {
|
|
49
|
+
return iriKeys.has(key) ||
|
|
50
|
+
((key === "@value" || key === "@list" || key === "@set") &&
|
|
51
|
+
parentKey != null && iriKeys.has(parentKey));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function arrayItemParentKey(
|
|
55
|
+
key?: string,
|
|
56
|
+
parentKey?: string,
|
|
57
|
+
): string | undefined {
|
|
58
|
+
return key === "@list" || key === "@set" ? parentKey : key;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function entryParentKey(
|
|
62
|
+
entryKey: string,
|
|
63
|
+
key?: string,
|
|
64
|
+
parentKey?: string,
|
|
65
|
+
): string | undefined {
|
|
66
|
+
if (entryKey === "@list" || entryKey === "@set") return parentKey ?? key;
|
|
67
|
+
return key === "@list" || key === "@set" ? parentKey : key;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalize(
|
|
71
|
+
value: unknown,
|
|
72
|
+
key?: string,
|
|
73
|
+
depth = 0,
|
|
74
|
+
parentKey?: string,
|
|
75
|
+
): unknown {
|
|
76
|
+
if (depth > 32 || key === "@context") return value;
|
|
77
|
+
if (typeof value === "string") {
|
|
78
|
+
if (
|
|
79
|
+
key != null && isIriPosition(key, parentKey) && iriPattern.test(value)
|
|
80
|
+
) {
|
|
81
|
+
try {
|
|
82
|
+
return formatIri(value);
|
|
83
|
+
} catch {
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
let clone: unknown[] | undefined;
|
|
91
|
+
const itemParentKey = arrayItemParentKey(key, parentKey);
|
|
92
|
+
for (let i = 0; i < value.length; i++) {
|
|
93
|
+
const result = normalize(value[i], key, depth + 1, itemParentKey);
|
|
94
|
+
if (result !== value[i]) {
|
|
95
|
+
clone ??= value.slice(0, i);
|
|
96
|
+
clone.push(result);
|
|
97
|
+
} else if (clone != null) {
|
|
98
|
+
clone.push(value[i]);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return clone ?? value;
|
|
102
|
+
}
|
|
103
|
+
if (value == null || typeof value !== "object") return value;
|
|
104
|
+
const object = value as Record<string, unknown>;
|
|
105
|
+
let clone: Record<string, unknown> | undefined;
|
|
106
|
+
for (const entryKey of globalThis.Object.keys(object)) {
|
|
107
|
+
const result = normalize(
|
|
108
|
+
object[entryKey],
|
|
109
|
+
entryKey,
|
|
110
|
+
depth + 1,
|
|
111
|
+
entryParentKey(entryKey, key, parentKey),
|
|
112
|
+
);
|
|
113
|
+
if (result !== object[entryKey]) {
|
|
114
|
+
clone ??= { ...object };
|
|
115
|
+
defineJsonLdProperty(clone, entryKey, result);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return clone ?? object;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Finds the first JSON-LD context in a value.
|
|
124
|
+
*
|
|
125
|
+
* @internal Technically exported for generated vocabulary classes, but not
|
|
126
|
+
* part of the public API contract. This is not considered public API for
|
|
127
|
+
* Semantic Versioning decisions.
|
|
128
|
+
*/
|
|
129
|
+
export function getJsonLdContext(value: unknown, depth = 0): unknown {
|
|
130
|
+
if (depth > 32) return undefined;
|
|
131
|
+
if (Array.isArray(value)) {
|
|
132
|
+
for (const item of value) {
|
|
133
|
+
const context = getJsonLdContext(item, depth + 1);
|
|
134
|
+
if (context !== undefined) return context;
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
if (
|
|
139
|
+
value == null || typeof value !== "object" ||
|
|
140
|
+
!("@context" in value)
|
|
141
|
+
) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
return (value as Record<string, unknown>)["@context"];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function getOwnJsonLdContext(value: unknown): unknown | typeof noJsonLdContext {
|
|
148
|
+
if (
|
|
149
|
+
value == null || typeof value !== "object" || Array.isArray(value) ||
|
|
150
|
+
!("@context" in value)
|
|
151
|
+
) {
|
|
152
|
+
return noJsonLdContext;
|
|
153
|
+
}
|
|
154
|
+
return (value as Record<string, unknown>)["@context"];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Recompacts normalized JSON-LD cache data against the original context.
|
|
159
|
+
*
|
|
160
|
+
* @internal Technically exported for generated vocabulary classes, but not
|
|
161
|
+
* part of the public API contract. This is not considered public API for
|
|
162
|
+
* Semantic Versioning decisions.
|
|
163
|
+
*/
|
|
164
|
+
export async function compactJsonLdCache(
|
|
165
|
+
normalized: unknown,
|
|
166
|
+
original: unknown,
|
|
167
|
+
documentLoader?: DocumentLoader,
|
|
168
|
+
depth = 0,
|
|
169
|
+
inheritedContext?: unknown,
|
|
170
|
+
): Promise<unknown> {
|
|
171
|
+
if (depth > 32) return normalized;
|
|
172
|
+
if (Array.isArray(original)) {
|
|
173
|
+
const normalizedArray = Array.isArray(normalized)
|
|
174
|
+
? normalized
|
|
175
|
+
: normalized != null && typeof normalized === "object" &&
|
|
176
|
+
"@graph" in normalized &&
|
|
177
|
+
Array.isArray((normalized as Record<string, unknown>)["@graph"])
|
|
178
|
+
? (normalized as Record<string, unknown>)["@graph"] as unknown[]
|
|
179
|
+
: undefined;
|
|
180
|
+
if (normalizedArray == null) return normalized;
|
|
181
|
+
let clone: unknown[] | undefined;
|
|
182
|
+
for (let i = 0; i < normalizedArray.length; i++) {
|
|
183
|
+
const item = await compactJsonLdCache(
|
|
184
|
+
normalizedArray[i],
|
|
185
|
+
original[i],
|
|
186
|
+
documentLoader,
|
|
187
|
+
depth + 1,
|
|
188
|
+
inheritedContext,
|
|
189
|
+
);
|
|
190
|
+
if (item !== normalizedArray[i]) {
|
|
191
|
+
clone ??= normalizedArray.slice(0, i);
|
|
192
|
+
clone.push(item);
|
|
193
|
+
} else if (clone != null) {
|
|
194
|
+
clone.push(normalizedArray[i]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return clone ?? (Array.isArray(normalized) ? normalized : normalizedArray);
|
|
198
|
+
}
|
|
199
|
+
const ownContext = getOwnJsonLdContext(original);
|
|
200
|
+
const context = ownContext === noJsonLdContext
|
|
201
|
+
? inheritedContext
|
|
202
|
+
: ownContext;
|
|
203
|
+
if (context == null) {
|
|
204
|
+
return preserveNoContextJsonLdShape(normalized, original, depth);
|
|
205
|
+
}
|
|
206
|
+
return await preserveJsonLdShape(
|
|
207
|
+
await mergeUnmappedTerms(
|
|
208
|
+
await jsonld.compact(
|
|
209
|
+
Array.isArray(normalized) && normalized.length === 1
|
|
210
|
+
? normalized[0]
|
|
211
|
+
: normalized,
|
|
212
|
+
context,
|
|
213
|
+
{ documentLoader },
|
|
214
|
+
),
|
|
215
|
+
original,
|
|
216
|
+
context,
|
|
217
|
+
documentLoader,
|
|
218
|
+
),
|
|
219
|
+
original,
|
|
220
|
+
context,
|
|
221
|
+
documentLoader,
|
|
222
|
+
depth,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function preserveNoContextJsonLdShape(
|
|
227
|
+
normalized: unknown,
|
|
228
|
+
original: unknown,
|
|
229
|
+
depth = 0,
|
|
230
|
+
): unknown {
|
|
231
|
+
if (depth > 32) return normalized;
|
|
232
|
+
if (
|
|
233
|
+
original == null || typeof original !== "object" ||
|
|
234
|
+
Array.isArray(original)
|
|
235
|
+
) {
|
|
236
|
+
return normalized;
|
|
237
|
+
}
|
|
238
|
+
const normalizedObject = Array.isArray(normalized) && normalized.length === 1
|
|
239
|
+
? normalized[0]
|
|
240
|
+
: normalized;
|
|
241
|
+
if (
|
|
242
|
+
normalizedObject == null || typeof normalizedObject !== "object" ||
|
|
243
|
+
Array.isArray(normalizedObject)
|
|
244
|
+
) {
|
|
245
|
+
return normalized;
|
|
246
|
+
}
|
|
247
|
+
const source = normalizedObject as Record<string, unknown>;
|
|
248
|
+
const originalObject = original as Record<string, unknown>;
|
|
249
|
+
let clone: Record<string, unknown> | undefined;
|
|
250
|
+
for (const key of globalThis.Object.keys(originalObject)) {
|
|
251
|
+
if (!globalThis.Object.prototype.hasOwnProperty.call(source, key)) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const value = preserveNoContextValue(
|
|
255
|
+
source[key],
|
|
256
|
+
originalObject[key],
|
|
257
|
+
depth + 1,
|
|
258
|
+
);
|
|
259
|
+
if (value !== originalObject[key]) {
|
|
260
|
+
clone ??= { ...originalObject };
|
|
261
|
+
defineJsonLdProperty(clone, key, value);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return clone ?? original;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function preserveNoContextValue(
|
|
268
|
+
normalized: unknown,
|
|
269
|
+
original: unknown,
|
|
270
|
+
depth: number,
|
|
271
|
+
): unknown {
|
|
272
|
+
if (depth > 32) return normalized;
|
|
273
|
+
if (Array.isArray(original)) {
|
|
274
|
+
const normalizedArray = Array.isArray(normalized)
|
|
275
|
+
? normalized
|
|
276
|
+
: [normalized];
|
|
277
|
+
let clone: unknown[] | undefined;
|
|
278
|
+
for (let i = 0; i < original.length; i++) {
|
|
279
|
+
const value = preserveNoContextValue(
|
|
280
|
+
normalizedArray[i],
|
|
281
|
+
original[i],
|
|
282
|
+
depth + 1,
|
|
283
|
+
);
|
|
284
|
+
if (value !== original[i]) {
|
|
285
|
+
clone ??= original.slice(0, i);
|
|
286
|
+
clone.push(value);
|
|
287
|
+
} else if (clone != null) {
|
|
288
|
+
clone.push(original[i]);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return clone ?? original;
|
|
292
|
+
}
|
|
293
|
+
const normalizedValue = Array.isArray(normalized)
|
|
294
|
+
? normalized[0]
|
|
295
|
+
: normalized;
|
|
296
|
+
if (
|
|
297
|
+
normalizedValue == null || typeof normalizedValue !== "object" ||
|
|
298
|
+
Array.isArray(normalizedValue)
|
|
299
|
+
) {
|
|
300
|
+
return normalizedValue;
|
|
301
|
+
}
|
|
302
|
+
const normalizedObject = normalizedValue as Record<string, unknown>;
|
|
303
|
+
if (
|
|
304
|
+
original != null && typeof original === "object" && !Array.isArray(original)
|
|
305
|
+
) {
|
|
306
|
+
return preserveNoContextJsonLdShape(normalizedObject, original, depth + 1);
|
|
307
|
+
}
|
|
308
|
+
if (typeof normalizedObject["@id"] === "string") {
|
|
309
|
+
return normalizedObject["@id"];
|
|
310
|
+
}
|
|
311
|
+
if ("@value" in normalizedObject) return normalizedObject["@value"];
|
|
312
|
+
return normalizedValue;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function getTopLevelTerms(value: unknown): ReadonlySet<string> {
|
|
316
|
+
const terms = new Set<string>();
|
|
317
|
+
const nodes = Array.isArray(value) ? value : [value];
|
|
318
|
+
for (const node of nodes) {
|
|
319
|
+
if (node == null || typeof node !== "object" || Array.isArray(node)) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
for (const key of globalThis.Object.keys(node)) {
|
|
323
|
+
if (key.startsWith("@")) continue;
|
|
324
|
+
terms.add(key);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return terms;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function mergeUnmappedTerms(
|
|
331
|
+
compacted: unknown,
|
|
332
|
+
original: unknown,
|
|
333
|
+
context: unknown,
|
|
334
|
+
documentLoader?: DocumentLoader,
|
|
335
|
+
): Promise<unknown> {
|
|
336
|
+
if (
|
|
337
|
+
original == null || typeof original !== "object" ||
|
|
338
|
+
Array.isArray(original) ||
|
|
339
|
+
compacted == null || typeof compacted !== "object" ||
|
|
340
|
+
Array.isArray(compacted)
|
|
341
|
+
) {
|
|
342
|
+
return compacted;
|
|
343
|
+
}
|
|
344
|
+
const unmappedKeys = globalThis.Object.keys(original).filter((key) =>
|
|
345
|
+
key !== "@context" &&
|
|
346
|
+
!globalThis.Object.prototype.hasOwnProperty.call(compacted, key)
|
|
347
|
+
);
|
|
348
|
+
if (unmappedKeys.length < 1) return compacted;
|
|
349
|
+
const result = { ...compacted as Record<string, unknown> };
|
|
350
|
+
const compactedWithContext = compacted != null &&
|
|
351
|
+
typeof compacted === "object" && !Array.isArray(compacted) &&
|
|
352
|
+
!("@context" in compacted)
|
|
353
|
+
? { "@context": context, ...compacted as Record<string, unknown> }
|
|
354
|
+
: compacted;
|
|
355
|
+
const compactedTerms = getTopLevelTerms(
|
|
356
|
+
await jsonld.expand(compactedWithContext, { documentLoader }),
|
|
357
|
+
);
|
|
358
|
+
const dummyPrefix = "urn:fedify:dummy:";
|
|
359
|
+
const dummy: Record<string, unknown> = { "@context": context };
|
|
360
|
+
for (let i = 0; i < unmappedKeys.length; i++) {
|
|
361
|
+
defineJsonLdProperty(dummy, unmappedKeys[i], `${dummyPrefix}${i}`);
|
|
362
|
+
}
|
|
363
|
+
const expanded = await jsonld.expand(dummy, { documentLoader });
|
|
364
|
+
const representedKeys = new Set<string>();
|
|
365
|
+
const nodes = Array.isArray(expanded) ? expanded : [expanded];
|
|
366
|
+
for (const node of nodes) {
|
|
367
|
+
if (node == null || typeof node !== "object" || Array.isArray(node)) {
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
for (const [term, termValue] of globalThis.Object.entries(node)) {
|
|
371
|
+
if (!compactedTerms.has(term)) continue;
|
|
372
|
+
for (let i = 0; i < unmappedKeys.length; i++) {
|
|
373
|
+
if (containsValue(termValue, `${dummyPrefix}${i}`)) {
|
|
374
|
+
representedKeys.add(unmappedKeys[i]);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
for (const key of unmappedKeys) {
|
|
380
|
+
if (!representedKeys.has(key)) {
|
|
381
|
+
defineJsonLdProperty(
|
|
382
|
+
result,
|
|
383
|
+
key,
|
|
384
|
+
(original as Record<string, unknown>)[key],
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function defineJsonLdProperty(
|
|
392
|
+
object: Record<string, unknown>,
|
|
393
|
+
key: string,
|
|
394
|
+
value: unknown,
|
|
395
|
+
): void {
|
|
396
|
+
globalThis.Object.defineProperty(object, key, {
|
|
397
|
+
value,
|
|
398
|
+
enumerable: true,
|
|
399
|
+
configurable: true,
|
|
400
|
+
writable: true,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function containsValue(value: unknown, expected: string, depth = 0): boolean {
|
|
405
|
+
if (depth > 32) return false;
|
|
406
|
+
if (value === expected) return true;
|
|
407
|
+
if (Array.isArray(value)) {
|
|
408
|
+
return value.some((item) => containsValue(item, expected, depth + 1));
|
|
409
|
+
}
|
|
410
|
+
if (value == null || typeof value !== "object") return false;
|
|
411
|
+
return globalThis.Object.entries(value).some(([key, item]) =>
|
|
412
|
+
key !== "@context" && containsValue(item, expected, depth + 1)
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async function preserveJsonLdShape(
|
|
417
|
+
compacted: unknown,
|
|
418
|
+
original: unknown,
|
|
419
|
+
context: unknown,
|
|
420
|
+
documentLoader?: DocumentLoader,
|
|
421
|
+
depth = 0,
|
|
422
|
+
): Promise<unknown> {
|
|
423
|
+
if (depth > 32) return compacted;
|
|
424
|
+
if (compacted === original) return compacted;
|
|
425
|
+
if (
|
|
426
|
+
original == null || typeof original !== "object" ||
|
|
427
|
+
compacted == null || typeof compacted !== "object"
|
|
428
|
+
) {
|
|
429
|
+
return compacted;
|
|
430
|
+
}
|
|
431
|
+
if (Array.isArray(original)) {
|
|
432
|
+
const compactedArray = Array.isArray(compacted)
|
|
433
|
+
? compacted
|
|
434
|
+
: "@graph" in compacted &&
|
|
435
|
+
Array.isArray((compacted as Record<string, unknown>)["@graph"])
|
|
436
|
+
? (compacted as Record<string, unknown>)["@graph"] as unknown[]
|
|
437
|
+
: undefined;
|
|
438
|
+
if (compactedArray == null) return compacted;
|
|
439
|
+
let clone: unknown[] | undefined;
|
|
440
|
+
for (let i = 0; i < compactedArray.length; i++) {
|
|
441
|
+
const value = await preserveJsonLdShape(
|
|
442
|
+
compactedArray[i],
|
|
443
|
+
original[i],
|
|
444
|
+
context,
|
|
445
|
+
documentLoader,
|
|
446
|
+
depth + 1,
|
|
447
|
+
);
|
|
448
|
+
const originalContext = original[i] != null &&
|
|
449
|
+
typeof original[i] === "object" && !Array.isArray(original[i]) &&
|
|
450
|
+
"@context" in original[i]
|
|
451
|
+
? (original[i] as Record<string, unknown>)["@context"]
|
|
452
|
+
: undefined;
|
|
453
|
+
const shaped = originalContext !== undefined &&
|
|
454
|
+
value != null && typeof value === "object" &&
|
|
455
|
+
!Array.isArray(value) && !("@context" in value)
|
|
456
|
+
? { "@context": originalContext, ...value as Record<string, unknown> }
|
|
457
|
+
: value;
|
|
458
|
+
if (shaped !== compactedArray[i]) {
|
|
459
|
+
clone ??= compactedArray.slice(0, i);
|
|
460
|
+
clone.push(shaped);
|
|
461
|
+
} else if (clone != null) {
|
|
462
|
+
clone.push(compactedArray[i]);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return clone ?? (Array.isArray(compacted) ? compacted : compactedArray);
|
|
466
|
+
}
|
|
467
|
+
if (Array.isArray(compacted)) return compacted;
|
|
468
|
+
let clone: Record<string, unknown> | undefined;
|
|
469
|
+
const ownContext = getOwnJsonLdContext(original);
|
|
470
|
+
const objectContext = combineContexts(context, ownContext);
|
|
471
|
+
const objectCompacted = depth > 0 && ownContext === null
|
|
472
|
+
? await compactWithEmptyContext(compacted, context, documentLoader)
|
|
473
|
+
: compacted;
|
|
474
|
+
const compactedObject = depth > 0
|
|
475
|
+
? await mergeUnmappedTerms(
|
|
476
|
+
objectCompacted,
|
|
477
|
+
original,
|
|
478
|
+
objectContext,
|
|
479
|
+
documentLoader,
|
|
480
|
+
) as Record<string, unknown>
|
|
481
|
+
: objectCompacted as Record<string, unknown>;
|
|
482
|
+
const originalObject = original as Record<string, unknown>;
|
|
483
|
+
for (const key of globalThis.Object.keys(compactedObject)) {
|
|
484
|
+
if (key === "@context") continue;
|
|
485
|
+
const value = await preserveJsonLdShape(
|
|
486
|
+
compactedObject[key],
|
|
487
|
+
originalObject[key],
|
|
488
|
+
objectContext,
|
|
489
|
+
documentLoader,
|
|
490
|
+
depth + 1,
|
|
491
|
+
);
|
|
492
|
+
const shaped = Array.isArray(originalObject[key]) && !Array.isArray(value)
|
|
493
|
+
? [value]
|
|
494
|
+
: value;
|
|
495
|
+
if (shaped !== compactedObject[key]) {
|
|
496
|
+
clone ??= { ...compactedObject };
|
|
497
|
+
defineJsonLdProperty(clone, key, shaped);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (depth > 0) {
|
|
501
|
+
if ("@context" in originalObject && !("@context" in compactedObject)) {
|
|
502
|
+
clone ??= { ...compactedObject };
|
|
503
|
+
clone["@context"] = originalObject["@context"];
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return clone ?? compactedObject;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
async function compactWithEmptyContext(
|
|
510
|
+
compacted: unknown,
|
|
511
|
+
context: unknown,
|
|
512
|
+
documentLoader?: DocumentLoader,
|
|
513
|
+
): Promise<unknown> {
|
|
514
|
+
const compactedWithContext = compacted != null &&
|
|
515
|
+
typeof compacted === "object" && !Array.isArray(compacted) &&
|
|
516
|
+
!("@context" in compacted)
|
|
517
|
+
? { "@context": context, ...compacted as Record<string, unknown> }
|
|
518
|
+
: compacted;
|
|
519
|
+
const expanded = await jsonld.expand(compactedWithContext, {
|
|
520
|
+
documentLoader,
|
|
521
|
+
});
|
|
522
|
+
return await jsonld.compact(expanded, {}, { documentLoader });
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function combineContexts(
|
|
526
|
+
inheritedContext: unknown,
|
|
527
|
+
ownContext: unknown,
|
|
528
|
+
): unknown {
|
|
529
|
+
if (ownContext === noJsonLdContext || ownContext === inheritedContext) {
|
|
530
|
+
return inheritedContext;
|
|
531
|
+
}
|
|
532
|
+
if (ownContext == null) return ownContext;
|
|
533
|
+
const inherited = Array.isArray(inheritedContext)
|
|
534
|
+
? inheritedContext
|
|
535
|
+
: [inheritedContext];
|
|
536
|
+
const own = Array.isArray(ownContext) ? ownContext : [ownContext];
|
|
537
|
+
return [...inherited, ...own];
|
|
538
|
+
}
|