@fedify/vocab-tools 2.0.36-dev.2746 → 2.0.36-dev.2760
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 +1 -1
- package/dist/mod.cjs +228 -2
- package/dist/mod.js +228 -2
- package/package.json +1 -1
- package/src/__snapshots__/class.test.ts.deno.snap +539 -32
- package/src/__snapshots__/class.test.ts.node.snap +539 -32
- package/src/__snapshots__/class.test.ts.snap +539 -32
- package/src/class.test.ts +315 -2
- package/src/class.ts +2 -0
- package/src/codec.ts +23 -1
- package/src/context.ts +205 -0
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -8,7 +8,7 @@ let yaml = require("yaml");
|
|
|
8
8
|
let es_toolkit = require("es-toolkit");
|
|
9
9
|
//#region deno.json
|
|
10
10
|
var name = "@fedify/vocab-tools";
|
|
11
|
-
var version = "2.0.36-dev.
|
|
11
|
+
var version = "2.0.36-dev.2760+4342c983";
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/type.ts
|
|
14
14
|
const HEURISTICS_CONTEXTS = [
|
|
@@ -702,6 +702,10 @@ async function* generateEncoder(typeUri, types) {
|
|
|
702
702
|
if (isCompactableType(typeUri, types)) {
|
|
703
703
|
yield `
|
|
704
704
|
if (options.format == null && this.isCompactable()) {
|
|
705
|
+
const fromSuperclass = "_fromSuperclass" in options &&
|
|
706
|
+
options._fromSuperclass === true;
|
|
707
|
+
options = { ...options };
|
|
708
|
+
if ("_fromSuperclass" in options) delete options._fromSuperclass;
|
|
705
709
|
`;
|
|
706
710
|
if (type.extends == null) yield "const result: Record<string, unknown> = {};";
|
|
707
711
|
else {
|
|
@@ -710,6 +714,8 @@ async function* generateEncoder(typeUri, types) {
|
|
|
710
714
|
...options,
|
|
711
715
|
format: undefined,
|
|
712
716
|
context: undefined,
|
|
717
|
+
// @ts-ignore: an internal option
|
|
718
|
+
_fromSuperclass: true,
|
|
713
719
|
}) as Record<string, unknown>;
|
|
714
720
|
`;
|
|
715
721
|
const selfProperties = type.properties.map((p) => p.uri);
|
|
@@ -757,11 +763,23 @@ async function* generateEncoder(typeUri, types) {
|
|
|
757
763
|
}
|
|
758
764
|
`;
|
|
759
765
|
}
|
|
766
|
+
const nodeTerms = /* @__PURE__ */ new Set();
|
|
767
|
+
for (const property of getAllProperties(typeUri, types)) {
|
|
768
|
+
if (!property.range.some((range) => range in types)) continue;
|
|
769
|
+
if (property.compactName != null) nodeTerms.add(property.compactName);
|
|
770
|
+
if (property.functional && property.redundantProperties != null) {
|
|
771
|
+
for (const redundant of property.redundantProperties) if (redundant.compactName != null) nodeTerms.add(redundant.compactName);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
760
774
|
yield `
|
|
761
775
|
result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};
|
|
762
776
|
if (this.id != null) result["id"] = this.id.href;
|
|
763
777
|
result["@context"] = ${JSON.stringify(type.defaultContext)};
|
|
764
|
-
return result
|
|
778
|
+
return fromSuperclass ? result : await isolateJsonLdContexts(
|
|
779
|
+
result,
|
|
780
|
+
${JSON.stringify([...nodeTerms])},
|
|
781
|
+
options.contextLoader,
|
|
782
|
+
);
|
|
765
783
|
}
|
|
766
784
|
`;
|
|
767
785
|
}
|
|
@@ -1340,6 +1358,213 @@ async function* generateCloner(typeUri, types) {
|
|
|
1340
1358
|
`;
|
|
1341
1359
|
}
|
|
1342
1360
|
//#endregion
|
|
1361
|
+
//#region src/context.ts
|
|
1362
|
+
/** Generates helpers for composing independent JSON-LD documents. */
|
|
1363
|
+
function generateContextHelpers() {
|
|
1364
|
+
return `
|
|
1365
|
+
function hasJsonLdContext(value: unknown): boolean {
|
|
1366
|
+
if (Array.isArray(value)) return value.some(hasJsonLdContext);
|
|
1367
|
+
return value != null && typeof value === "object" &&
|
|
1368
|
+
globalThis.Object.hasOwn(value, "@context");
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
function hasSupportedJsonLdScalars(value: unknown, idReferences: boolean): boolean {
|
|
1372
|
+
if (Array.isArray(value)) {
|
|
1373
|
+
return value.every(item => hasSupportedJsonLdScalars(item, idReferences));
|
|
1374
|
+
}
|
|
1375
|
+
return value === null || typeof value === "object" ||
|
|
1376
|
+
(idReferences && typeof value === "string" && URL.canParse(value));
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
function equalJsonLdValues(left: unknown, right: unknown): boolean {
|
|
1380
|
+
if (left === right) return true;
|
|
1381
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
1382
|
+
return Array.isArray(left) && Array.isArray(right) &&
|
|
1383
|
+
left.length === right.length &&
|
|
1384
|
+
left.every((item, index) => equalJsonLdValues(item, right[index]));
|
|
1385
|
+
}
|
|
1386
|
+
if (left === null || right === null || typeof left !== "object" ||
|
|
1387
|
+
typeof right !== "object") return false;
|
|
1388
|
+
const leftObject = left as Record<string, unknown>;
|
|
1389
|
+
const rightObject = right as Record<string, unknown>;
|
|
1390
|
+
const keys = globalThis.Object.keys(leftObject);
|
|
1391
|
+
return keys.length === globalThis.Object.keys(rightObject).length &&
|
|
1392
|
+
keys.every(key => globalThis.Object.hasOwn(rightObject, key) &&
|
|
1393
|
+
equalJsonLdValues(leftObject[key], rightObject[key]));
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
async function isolateJsonLdSibling(
|
|
1397
|
+
value: unknown,
|
|
1398
|
+
term: string,
|
|
1399
|
+
originalContext: unknown,
|
|
1400
|
+
repairedContext: unknown,
|
|
1401
|
+
parentType: unknown,
|
|
1402
|
+
typeContext: unknown,
|
|
1403
|
+
contextLoader: DocumentLoader | undefined,
|
|
1404
|
+
): Promise<{ value: unknown } | undefined> {
|
|
1405
|
+
if (Array.isArray(value)) {
|
|
1406
|
+
const result: unknown[] = [];
|
|
1407
|
+
for (const item of value) {
|
|
1408
|
+
const rewritten = await isolateJsonLdSibling(
|
|
1409
|
+
item, term, originalContext, repairedContext, parentType, typeContext,
|
|
1410
|
+
contextLoader,
|
|
1411
|
+
);
|
|
1412
|
+
if (rewritten === undefined) return undefined;
|
|
1413
|
+
result.push(rewritten.value);
|
|
1414
|
+
}
|
|
1415
|
+
return { value: result };
|
|
1416
|
+
}
|
|
1417
|
+
if (value === null) return { value };
|
|
1418
|
+
if (typeof value === "string") {
|
|
1419
|
+
// These references were checked against the original term's @type:@id.
|
|
1420
|
+
// Resolve compact IRIs and @base before removing their enclosing context.
|
|
1421
|
+
const expanded = await jsonld.expand(
|
|
1422
|
+
{ "@context": originalContext, type: parentType, "@id": value },
|
|
1423
|
+
{ documentLoader: contextLoader, keepFreeFloatingNodes: true },
|
|
1424
|
+
);
|
|
1425
|
+
return { value: { "@context": { id: "@id" }, id: expanded[0]["@id"] } };
|
|
1426
|
+
}
|
|
1427
|
+
if (typeof value !== "object" ||
|
|
1428
|
+
globalThis.Object.hasOwn(value, "@context")) return { value };
|
|
1429
|
+
|
|
1430
|
+
// Comparing equal-shaped wrappers also preserves list and graph semantics.
|
|
1431
|
+
// Keep already independent expanded sources exactly as received: adding a
|
|
1432
|
+
// context to a signed source can invalidate its JSON canonicalization proof.
|
|
1433
|
+
const original = await jsonld.expand(
|
|
1434
|
+
{ "@context": originalContext, type: parentType, [term]: value },
|
|
1435
|
+
{ documentLoader: contextLoader },
|
|
1436
|
+
);
|
|
1437
|
+
const repaired = await jsonld.expand(
|
|
1438
|
+
{ "@context": repairedContext, type: parentType, [term]: value },
|
|
1439
|
+
{ documentLoader: contextLoader },
|
|
1440
|
+
);
|
|
1441
|
+
if (equalJsonLdValues(original, repaired)) return { value };
|
|
1442
|
+
const restorationContexts = [originalContext];
|
|
1443
|
+
if (typeContext !== undefined) {
|
|
1444
|
+
restorationContexts.push([
|
|
1445
|
+
...(Array.isArray(originalContext) ? originalContext : [originalContext]),
|
|
1446
|
+
...(Array.isArray(typeContext) ? typeContext : [typeContext]),
|
|
1447
|
+
]);
|
|
1448
|
+
}
|
|
1449
|
+
for (const context of restorationContexts) {
|
|
1450
|
+
const restored = { ...value, "@context": context };
|
|
1451
|
+
const expanded = await jsonld.expand(
|
|
1452
|
+
{ "@context": repairedContext, type: parentType, [term]: restored },
|
|
1453
|
+
{ documentLoader: contextLoader },
|
|
1454
|
+
);
|
|
1455
|
+
if (equalJsonLdValues(original, expanded)) return { value: restored };
|
|
1456
|
+
}
|
|
1457
|
+
// If neither context preserves the inherited meaning, keep the whole term
|
|
1458
|
+
// unchanged rather than committing only some of its sibling rewrites.
|
|
1459
|
+
return undefined;
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
function copyJsonLdTermDefinition(mapping: Record<string, unknown>): Record<string, unknown> {
|
|
1463
|
+
const definition: Record<string, unknown> = {};
|
|
1464
|
+
for (const keyword of [
|
|
1465
|
+
"@id", "@type", "@container", "@language", "@direction", "@index",
|
|
1466
|
+
"@nest", "@prefix",
|
|
1467
|
+
]) {
|
|
1468
|
+
if (!globalThis.Object.hasOwn(mapping, keyword)) continue;
|
|
1469
|
+
const value = mapping[keyword];
|
|
1470
|
+
definition[keyword] = Array.isArray(value)
|
|
1471
|
+
? value.length === 1 && keyword === "@container" ? value[0] : [...value]
|
|
1472
|
+
: value;
|
|
1473
|
+
}
|
|
1474
|
+
// The processor stores the semantic @prefix flag under an internal name.
|
|
1475
|
+
if (mapping._prefix === true) definition["@prefix"] = true;
|
|
1476
|
+
return definition;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
async function isolateJsonLdContexts(
|
|
1480
|
+
result: Record<string, unknown>,
|
|
1481
|
+
terms: readonly string[],
|
|
1482
|
+
contextLoader: DocumentLoader | undefined,
|
|
1483
|
+
): Promise<Record<string, unknown>> {
|
|
1484
|
+
const candidates = terms.filter(term =>
|
|
1485
|
+
globalThis.Object.hasOwn(result, term) && hasJsonLdContext(result[term])
|
|
1486
|
+
);
|
|
1487
|
+
if (candidates.length === 0) return result;
|
|
1488
|
+
|
|
1489
|
+
const originalContext = result["@context"];
|
|
1490
|
+
const contexts = Array.isArray(originalContext)
|
|
1491
|
+
? originalContext
|
|
1492
|
+
: [originalContext];
|
|
1493
|
+
let activeContext = await jsonld.processContext(
|
|
1494
|
+
await jsonld.processContext({}, null),
|
|
1495
|
+
originalContext,
|
|
1496
|
+
{ documentLoader: contextLoader },
|
|
1497
|
+
);
|
|
1498
|
+
const parentType = result.type;
|
|
1499
|
+
const typeMapping = typeof parentType === "string" &&
|
|
1500
|
+
jsonld.getContextValue(activeContext, "type", "@id") === "@type"
|
|
1501
|
+
? jsonld.getContextValue(activeContext, parentType) as
|
|
1502
|
+
Record<string, unknown> | null
|
|
1503
|
+
: null;
|
|
1504
|
+
const typeContext = typeMapping?.["@context"];
|
|
1505
|
+
if (typeContext !== undefined) {
|
|
1506
|
+
// A protected type definition cannot receive an augmented scoped context.
|
|
1507
|
+
// Leave that existing boundary unchanged, just as for protected properties.
|
|
1508
|
+
if (typeMapping!.protected === true) return result;
|
|
1509
|
+
activeContext = await jsonld.processContext(
|
|
1510
|
+
activeContext, typeContext, { documentLoader: contextLoader },
|
|
1511
|
+
);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
function repairContext(definitions: Record<string, unknown>): unknown[] {
|
|
1515
|
+
if (typeContext === undefined) return [...contexts, definitions];
|
|
1516
|
+
// Preserve an object scope's @propagate semantics: replacing it with an
|
|
1517
|
+
// array would change how the published context algorithm handles the flag.
|
|
1518
|
+
const scopedContext = typeContext !== null && typeof typeContext === "object" &&
|
|
1519
|
+
!Array.isArray(typeContext)
|
|
1520
|
+
? { ...typeContext, ...definitions }
|
|
1521
|
+
: [...(Array.isArray(typeContext) ? typeContext : [typeContext]), definitions];
|
|
1522
|
+
return [...contexts, {
|
|
1523
|
+
[parentType as string]: {
|
|
1524
|
+
...copyJsonLdTermDefinition(typeMapping!), "@context": scopedContext,
|
|
1525
|
+
},
|
|
1526
|
+
}];
|
|
1527
|
+
}
|
|
1528
|
+
let definitions: Record<string, unknown> = {};
|
|
1529
|
+
let siblings: Record<string, unknown> = {};
|
|
1530
|
+
for (const term of candidates) {
|
|
1531
|
+
const mapping = jsonld.getContextValue(activeContext, term) as
|
|
1532
|
+
Record<string, unknown> | null;
|
|
1533
|
+
if (mapping == null || mapping.protected === true || mapping.reverse === true ||
|
|
1534
|
+
globalThis.Object.hasOwn(mapping, "@context") ||
|
|
1535
|
+
mapping["@type"] === "@json" || typeof mapping["@id"] !== "string" ||
|
|
1536
|
+
mapping["@id"].startsWith("@")) continue;
|
|
1537
|
+
|
|
1538
|
+
// A scalar scoped reset does not override protected definitions in the
|
|
1539
|
+
// published expansion algorithm. Check the entire property before making
|
|
1540
|
+
// any changes; supported URL references receive their own local context.
|
|
1541
|
+
if (!hasSupportedJsonLdScalars(result[term], mapping["@type"] === "@id")) {
|
|
1542
|
+
continue;
|
|
1543
|
+
}
|
|
1544
|
+
const definition = copyJsonLdTermDefinition(mapping);
|
|
1545
|
+
definition["@context"] = null;
|
|
1546
|
+
if (typeContext !== null && typeof typeContext === "object" &&
|
|
1547
|
+
!Array.isArray(typeContext) &&
|
|
1548
|
+
(typeContext as Record<string, unknown>)["@protected"] === true) {
|
|
1549
|
+
definition["@protected"] = false;
|
|
1550
|
+
}
|
|
1551
|
+
const proposedDefinitions = { ...definitions, [term]: definition };
|
|
1552
|
+
const repairedContext = repairContext(proposedDefinitions);
|
|
1553
|
+
const rewritten = await isolateJsonLdSibling(
|
|
1554
|
+
result[term], term, originalContext, repairedContext, parentType, typeContext,
|
|
1555
|
+
contextLoader,
|
|
1556
|
+
);
|
|
1557
|
+
if (rewritten === undefined) continue;
|
|
1558
|
+
definitions = proposedDefinitions;
|
|
1559
|
+
siblings = { ...siblings, [term]: rewritten.value };
|
|
1560
|
+
}
|
|
1561
|
+
return globalThis.Object.keys(definitions).length === 0
|
|
1562
|
+
? result
|
|
1563
|
+
: { ...result, ...siblings, "@context": repairContext(definitions) };
|
|
1564
|
+
}
|
|
1565
|
+
`;
|
|
1566
|
+
}
|
|
1567
|
+
//#endregion
|
|
1343
1568
|
//#region src/inspector.ts
|
|
1344
1569
|
async function* generateInspector(typeUri, types) {
|
|
1345
1570
|
const type = types[typeUri];
|
|
@@ -1981,6 +2206,7 @@ function isValidLanguageTag(language: string): boolean {
|
|
|
1981
2206
|
}
|
|
1982
2207
|
`;
|
|
1983
2208
|
yield "\n\n";
|
|
2209
|
+
yield generateContextHelpers();
|
|
1984
2210
|
const sorted = sortTopologically(types);
|
|
1985
2211
|
for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
|
|
1986
2212
|
}
|
package/dist/mod.js
CHANGED
|
@@ -7,7 +7,7 @@ import { parse } from "yaml";
|
|
|
7
7
|
import { pascalCase } from "es-toolkit";
|
|
8
8
|
//#region deno.json
|
|
9
9
|
var name = "@fedify/vocab-tools";
|
|
10
|
-
var version = "2.0.36-dev.
|
|
10
|
+
var version = "2.0.36-dev.2760+4342c983";
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/type.ts
|
|
13
13
|
const HEURISTICS_CONTEXTS = [
|
|
@@ -701,6 +701,10 @@ async function* generateEncoder(typeUri, types) {
|
|
|
701
701
|
if (isCompactableType(typeUri, types)) {
|
|
702
702
|
yield `
|
|
703
703
|
if (options.format == null && this.isCompactable()) {
|
|
704
|
+
const fromSuperclass = "_fromSuperclass" in options &&
|
|
705
|
+
options._fromSuperclass === true;
|
|
706
|
+
options = { ...options };
|
|
707
|
+
if ("_fromSuperclass" in options) delete options._fromSuperclass;
|
|
704
708
|
`;
|
|
705
709
|
if (type.extends == null) yield "const result: Record<string, unknown> = {};";
|
|
706
710
|
else {
|
|
@@ -709,6 +713,8 @@ async function* generateEncoder(typeUri, types) {
|
|
|
709
713
|
...options,
|
|
710
714
|
format: undefined,
|
|
711
715
|
context: undefined,
|
|
716
|
+
// @ts-ignore: an internal option
|
|
717
|
+
_fromSuperclass: true,
|
|
712
718
|
}) as Record<string, unknown>;
|
|
713
719
|
`;
|
|
714
720
|
const selfProperties = type.properties.map((p) => p.uri);
|
|
@@ -756,11 +762,23 @@ async function* generateEncoder(typeUri, types) {
|
|
|
756
762
|
}
|
|
757
763
|
`;
|
|
758
764
|
}
|
|
765
|
+
const nodeTerms = /* @__PURE__ */ new Set();
|
|
766
|
+
for (const property of getAllProperties(typeUri, types)) {
|
|
767
|
+
if (!property.range.some((range) => range in types)) continue;
|
|
768
|
+
if (property.compactName != null) nodeTerms.add(property.compactName);
|
|
769
|
+
if (property.functional && property.redundantProperties != null) {
|
|
770
|
+
for (const redundant of property.redundantProperties) if (redundant.compactName != null) nodeTerms.add(redundant.compactName);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
759
773
|
yield `
|
|
760
774
|
result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};
|
|
761
775
|
if (this.id != null) result["id"] = this.id.href;
|
|
762
776
|
result["@context"] = ${JSON.stringify(type.defaultContext)};
|
|
763
|
-
return result
|
|
777
|
+
return fromSuperclass ? result : await isolateJsonLdContexts(
|
|
778
|
+
result,
|
|
779
|
+
${JSON.stringify([...nodeTerms])},
|
|
780
|
+
options.contextLoader,
|
|
781
|
+
);
|
|
764
782
|
}
|
|
765
783
|
`;
|
|
766
784
|
}
|
|
@@ -1339,6 +1357,213 @@ async function* generateCloner(typeUri, types) {
|
|
|
1339
1357
|
`;
|
|
1340
1358
|
}
|
|
1341
1359
|
//#endregion
|
|
1360
|
+
//#region src/context.ts
|
|
1361
|
+
/** Generates helpers for composing independent JSON-LD documents. */
|
|
1362
|
+
function generateContextHelpers() {
|
|
1363
|
+
return `
|
|
1364
|
+
function hasJsonLdContext(value: unknown): boolean {
|
|
1365
|
+
if (Array.isArray(value)) return value.some(hasJsonLdContext);
|
|
1366
|
+
return value != null && typeof value === "object" &&
|
|
1367
|
+
globalThis.Object.hasOwn(value, "@context");
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
function hasSupportedJsonLdScalars(value: unknown, idReferences: boolean): boolean {
|
|
1371
|
+
if (Array.isArray(value)) {
|
|
1372
|
+
return value.every(item => hasSupportedJsonLdScalars(item, idReferences));
|
|
1373
|
+
}
|
|
1374
|
+
return value === null || typeof value === "object" ||
|
|
1375
|
+
(idReferences && typeof value === "string" && URL.canParse(value));
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
function equalJsonLdValues(left: unknown, right: unknown): boolean {
|
|
1379
|
+
if (left === right) return true;
|
|
1380
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
1381
|
+
return Array.isArray(left) && Array.isArray(right) &&
|
|
1382
|
+
left.length === right.length &&
|
|
1383
|
+
left.every((item, index) => equalJsonLdValues(item, right[index]));
|
|
1384
|
+
}
|
|
1385
|
+
if (left === null || right === null || typeof left !== "object" ||
|
|
1386
|
+
typeof right !== "object") return false;
|
|
1387
|
+
const leftObject = left as Record<string, unknown>;
|
|
1388
|
+
const rightObject = right as Record<string, unknown>;
|
|
1389
|
+
const keys = globalThis.Object.keys(leftObject);
|
|
1390
|
+
return keys.length === globalThis.Object.keys(rightObject).length &&
|
|
1391
|
+
keys.every(key => globalThis.Object.hasOwn(rightObject, key) &&
|
|
1392
|
+
equalJsonLdValues(leftObject[key], rightObject[key]));
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
async function isolateJsonLdSibling(
|
|
1396
|
+
value: unknown,
|
|
1397
|
+
term: string,
|
|
1398
|
+
originalContext: unknown,
|
|
1399
|
+
repairedContext: unknown,
|
|
1400
|
+
parentType: unknown,
|
|
1401
|
+
typeContext: unknown,
|
|
1402
|
+
contextLoader: DocumentLoader | undefined,
|
|
1403
|
+
): Promise<{ value: unknown } | undefined> {
|
|
1404
|
+
if (Array.isArray(value)) {
|
|
1405
|
+
const result: unknown[] = [];
|
|
1406
|
+
for (const item of value) {
|
|
1407
|
+
const rewritten = await isolateJsonLdSibling(
|
|
1408
|
+
item, term, originalContext, repairedContext, parentType, typeContext,
|
|
1409
|
+
contextLoader,
|
|
1410
|
+
);
|
|
1411
|
+
if (rewritten === undefined) return undefined;
|
|
1412
|
+
result.push(rewritten.value);
|
|
1413
|
+
}
|
|
1414
|
+
return { value: result };
|
|
1415
|
+
}
|
|
1416
|
+
if (value === null) return { value };
|
|
1417
|
+
if (typeof value === "string") {
|
|
1418
|
+
// These references were checked against the original term's @type:@id.
|
|
1419
|
+
// Resolve compact IRIs and @base before removing their enclosing context.
|
|
1420
|
+
const expanded = await jsonld.expand(
|
|
1421
|
+
{ "@context": originalContext, type: parentType, "@id": value },
|
|
1422
|
+
{ documentLoader: contextLoader, keepFreeFloatingNodes: true },
|
|
1423
|
+
);
|
|
1424
|
+
return { value: { "@context": { id: "@id" }, id: expanded[0]["@id"] } };
|
|
1425
|
+
}
|
|
1426
|
+
if (typeof value !== "object" ||
|
|
1427
|
+
globalThis.Object.hasOwn(value, "@context")) return { value };
|
|
1428
|
+
|
|
1429
|
+
// Comparing equal-shaped wrappers also preserves list and graph semantics.
|
|
1430
|
+
// Keep already independent expanded sources exactly as received: adding a
|
|
1431
|
+
// context to a signed source can invalidate its JSON canonicalization proof.
|
|
1432
|
+
const original = await jsonld.expand(
|
|
1433
|
+
{ "@context": originalContext, type: parentType, [term]: value },
|
|
1434
|
+
{ documentLoader: contextLoader },
|
|
1435
|
+
);
|
|
1436
|
+
const repaired = await jsonld.expand(
|
|
1437
|
+
{ "@context": repairedContext, type: parentType, [term]: value },
|
|
1438
|
+
{ documentLoader: contextLoader },
|
|
1439
|
+
);
|
|
1440
|
+
if (equalJsonLdValues(original, repaired)) return { value };
|
|
1441
|
+
const restorationContexts = [originalContext];
|
|
1442
|
+
if (typeContext !== undefined) {
|
|
1443
|
+
restorationContexts.push([
|
|
1444
|
+
...(Array.isArray(originalContext) ? originalContext : [originalContext]),
|
|
1445
|
+
...(Array.isArray(typeContext) ? typeContext : [typeContext]),
|
|
1446
|
+
]);
|
|
1447
|
+
}
|
|
1448
|
+
for (const context of restorationContexts) {
|
|
1449
|
+
const restored = { ...value, "@context": context };
|
|
1450
|
+
const expanded = await jsonld.expand(
|
|
1451
|
+
{ "@context": repairedContext, type: parentType, [term]: restored },
|
|
1452
|
+
{ documentLoader: contextLoader },
|
|
1453
|
+
);
|
|
1454
|
+
if (equalJsonLdValues(original, expanded)) return { value: restored };
|
|
1455
|
+
}
|
|
1456
|
+
// If neither context preserves the inherited meaning, keep the whole term
|
|
1457
|
+
// unchanged rather than committing only some of its sibling rewrites.
|
|
1458
|
+
return undefined;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
function copyJsonLdTermDefinition(mapping: Record<string, unknown>): Record<string, unknown> {
|
|
1462
|
+
const definition: Record<string, unknown> = {};
|
|
1463
|
+
for (const keyword of [
|
|
1464
|
+
"@id", "@type", "@container", "@language", "@direction", "@index",
|
|
1465
|
+
"@nest", "@prefix",
|
|
1466
|
+
]) {
|
|
1467
|
+
if (!globalThis.Object.hasOwn(mapping, keyword)) continue;
|
|
1468
|
+
const value = mapping[keyword];
|
|
1469
|
+
definition[keyword] = Array.isArray(value)
|
|
1470
|
+
? value.length === 1 && keyword === "@container" ? value[0] : [...value]
|
|
1471
|
+
: value;
|
|
1472
|
+
}
|
|
1473
|
+
// The processor stores the semantic @prefix flag under an internal name.
|
|
1474
|
+
if (mapping._prefix === true) definition["@prefix"] = true;
|
|
1475
|
+
return definition;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
async function isolateJsonLdContexts(
|
|
1479
|
+
result: Record<string, unknown>,
|
|
1480
|
+
terms: readonly string[],
|
|
1481
|
+
contextLoader: DocumentLoader | undefined,
|
|
1482
|
+
): Promise<Record<string, unknown>> {
|
|
1483
|
+
const candidates = terms.filter(term =>
|
|
1484
|
+
globalThis.Object.hasOwn(result, term) && hasJsonLdContext(result[term])
|
|
1485
|
+
);
|
|
1486
|
+
if (candidates.length === 0) return result;
|
|
1487
|
+
|
|
1488
|
+
const originalContext = result["@context"];
|
|
1489
|
+
const contexts = Array.isArray(originalContext)
|
|
1490
|
+
? originalContext
|
|
1491
|
+
: [originalContext];
|
|
1492
|
+
let activeContext = await jsonld.processContext(
|
|
1493
|
+
await jsonld.processContext({}, null),
|
|
1494
|
+
originalContext,
|
|
1495
|
+
{ documentLoader: contextLoader },
|
|
1496
|
+
);
|
|
1497
|
+
const parentType = result.type;
|
|
1498
|
+
const typeMapping = typeof parentType === "string" &&
|
|
1499
|
+
jsonld.getContextValue(activeContext, "type", "@id") === "@type"
|
|
1500
|
+
? jsonld.getContextValue(activeContext, parentType) as
|
|
1501
|
+
Record<string, unknown> | null
|
|
1502
|
+
: null;
|
|
1503
|
+
const typeContext = typeMapping?.["@context"];
|
|
1504
|
+
if (typeContext !== undefined) {
|
|
1505
|
+
// A protected type definition cannot receive an augmented scoped context.
|
|
1506
|
+
// Leave that existing boundary unchanged, just as for protected properties.
|
|
1507
|
+
if (typeMapping!.protected === true) return result;
|
|
1508
|
+
activeContext = await jsonld.processContext(
|
|
1509
|
+
activeContext, typeContext, { documentLoader: contextLoader },
|
|
1510
|
+
);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
function repairContext(definitions: Record<string, unknown>): unknown[] {
|
|
1514
|
+
if (typeContext === undefined) return [...contexts, definitions];
|
|
1515
|
+
// Preserve an object scope's @propagate semantics: replacing it with an
|
|
1516
|
+
// array would change how the published context algorithm handles the flag.
|
|
1517
|
+
const scopedContext = typeContext !== null && typeof typeContext === "object" &&
|
|
1518
|
+
!Array.isArray(typeContext)
|
|
1519
|
+
? { ...typeContext, ...definitions }
|
|
1520
|
+
: [...(Array.isArray(typeContext) ? typeContext : [typeContext]), definitions];
|
|
1521
|
+
return [...contexts, {
|
|
1522
|
+
[parentType as string]: {
|
|
1523
|
+
...copyJsonLdTermDefinition(typeMapping!), "@context": scopedContext,
|
|
1524
|
+
},
|
|
1525
|
+
}];
|
|
1526
|
+
}
|
|
1527
|
+
let definitions: Record<string, unknown> = {};
|
|
1528
|
+
let siblings: Record<string, unknown> = {};
|
|
1529
|
+
for (const term of candidates) {
|
|
1530
|
+
const mapping = jsonld.getContextValue(activeContext, term) as
|
|
1531
|
+
Record<string, unknown> | null;
|
|
1532
|
+
if (mapping == null || mapping.protected === true || mapping.reverse === true ||
|
|
1533
|
+
globalThis.Object.hasOwn(mapping, "@context") ||
|
|
1534
|
+
mapping["@type"] === "@json" || typeof mapping["@id"] !== "string" ||
|
|
1535
|
+
mapping["@id"].startsWith("@")) continue;
|
|
1536
|
+
|
|
1537
|
+
// A scalar scoped reset does not override protected definitions in the
|
|
1538
|
+
// published expansion algorithm. Check the entire property before making
|
|
1539
|
+
// any changes; supported URL references receive their own local context.
|
|
1540
|
+
if (!hasSupportedJsonLdScalars(result[term], mapping["@type"] === "@id")) {
|
|
1541
|
+
continue;
|
|
1542
|
+
}
|
|
1543
|
+
const definition = copyJsonLdTermDefinition(mapping);
|
|
1544
|
+
definition["@context"] = null;
|
|
1545
|
+
if (typeContext !== null && typeof typeContext === "object" &&
|
|
1546
|
+
!Array.isArray(typeContext) &&
|
|
1547
|
+
(typeContext as Record<string, unknown>)["@protected"] === true) {
|
|
1548
|
+
definition["@protected"] = false;
|
|
1549
|
+
}
|
|
1550
|
+
const proposedDefinitions = { ...definitions, [term]: definition };
|
|
1551
|
+
const repairedContext = repairContext(proposedDefinitions);
|
|
1552
|
+
const rewritten = await isolateJsonLdSibling(
|
|
1553
|
+
result[term], term, originalContext, repairedContext, parentType, typeContext,
|
|
1554
|
+
contextLoader,
|
|
1555
|
+
);
|
|
1556
|
+
if (rewritten === undefined) continue;
|
|
1557
|
+
definitions = proposedDefinitions;
|
|
1558
|
+
siblings = { ...siblings, [term]: rewritten.value };
|
|
1559
|
+
}
|
|
1560
|
+
return globalThis.Object.keys(definitions).length === 0
|
|
1561
|
+
? result
|
|
1562
|
+
: { ...result, ...siblings, "@context": repairContext(definitions) };
|
|
1563
|
+
}
|
|
1564
|
+
`;
|
|
1565
|
+
}
|
|
1566
|
+
//#endregion
|
|
1342
1567
|
//#region src/inspector.ts
|
|
1343
1568
|
async function* generateInspector(typeUri, types) {
|
|
1344
1569
|
const type = types[typeUri];
|
|
@@ -1980,6 +2205,7 @@ function isValidLanguageTag(language: string): boolean {
|
|
|
1980
2205
|
}
|
|
1981
2206
|
`;
|
|
1982
2207
|
yield "\n\n";
|
|
2208
|
+
yield generateContextHelpers();
|
|
1983
2209
|
const sorted = sortTopologically(types);
|
|
1984
2210
|
for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
|
|
1985
2211
|
}
|