@drfed/models 0.2.0-dev.703 → 0.2.0-dev.712

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/document.mjs CHANGED
@@ -3,7 +3,7 @@ import { Accept, Activity, Add, Announce, Application, Arrive, Article, Block, C
3
3
  import { preloadedContexts } from "@fedify/vocab-runtime";
4
4
  //#region src/document.ts
5
5
  /** Stable classifications; human-readable diagnostic messages may change. */
6
- const diagnosticReasons = {
6
+ const DIAGNOSTIC_REASONS = {
7
7
  invalid_document_root: {
8
8
  category: "vocabulary",
9
9
  severity: "error"
@@ -89,10 +89,10 @@ const diagnosticReasons = {
89
89
  severity: "warning"
90
90
  }
91
91
  };
92
- const activityStreamsContext = "https://www.w3.org/ns/activitystreams";
93
- const maximumDiagnostics = 100;
94
- const maximumPathDepth = 64;
95
- const actorTypes = [
92
+ const ACTIVITY_STREAMS_CONTEXT = "https://www.w3.org/ns/activitystreams";
93
+ const MAXIMUM_DIAGNOSTICS = 100;
94
+ const MAXIMUM_PATH_DEPTH = 64;
95
+ const ACTOR_TYPES = [
96
96
  Application,
97
97
  Group,
98
98
  Organization,
@@ -100,14 +100,14 @@ const actorTypes = [
100
100
  Service
101
101
  ];
102
102
  /** The ActivityStreams properties that address a document's recipients. */
103
- const addressingProperties = [
103
+ const ADDRESSING_PROPERTIES = [
104
104
  "to",
105
105
  "cc",
106
106
  "bto",
107
107
  "bcc",
108
108
  "audience"
109
109
  ];
110
- const objectOnlyProperties = /* @__PURE__ */ new Set([
110
+ const OBJECT_ONLY_PROPERTIES = /* @__PURE__ */ new Set([
111
111
  "attachment",
112
112
  "attributedTo",
113
113
  "audience",
@@ -134,7 +134,7 @@ const objectOnlyProperties = /* @__PURE__ */ new Set([
134
134
  "bcc",
135
135
  "duration"
136
136
  ]);
137
- const implementedTypes = /* @__PURE__ */ new Set([
137
+ const IMPLEMENTED_TYPES = /* @__PURE__ */ new Set([
138
138
  Note,
139
139
  Object$1,
140
140
  Article,
@@ -180,24 +180,57 @@ const implementedTypes = /* @__PURE__ */ new Set([
180
180
  Update,
181
181
  View
182
182
  ]);
183
+ const VALUE_OBJECT_KEYWORDS = /* @__PURE__ */ new Set([
184
+ "@value",
185
+ "@list",
186
+ "@set"
187
+ ]);
188
+ const IDENTIFIER_PROPERTIES = /* @__PURE__ */ new Set([
189
+ "id",
190
+ "actor",
191
+ "object",
192
+ "target",
193
+ "origin",
194
+ "result",
195
+ "instrument",
196
+ "attributedTo",
197
+ "location",
198
+ "preview",
199
+ ...ADDRESSING_PROPERTIES,
200
+ "items",
201
+ "orderedItems",
202
+ "current",
203
+ "first",
204
+ "last",
205
+ "partOf",
206
+ "next",
207
+ "prev",
208
+ "inbox",
209
+ "outbox",
210
+ "following",
211
+ "followers",
212
+ "liked",
213
+ "href"
214
+ ]);
215
+ const FUNCTIONAL_REFERENCES = ["id", "href"];
183
216
  function createReporter() {
184
217
  const diagnostics = [];
185
218
  let complete = true;
186
219
  let truncated = false;
187
220
  const report = (reason, path, message) => {
188
221
  if (truncated) return;
189
- if (diagnostics.length === maximumDiagnostics) {
222
+ if (diagnostics.length === MAXIMUM_DIAGNOSTICS) {
190
223
  complete = false;
191
224
  truncated = true;
192
225
  diagnostics[99] = {
193
226
  reason: "diagnostic_limit_reached",
194
- ...diagnosticReasons.diagnostic_limit_reached,
227
+ ...DIAGNOSTIC_REASONS.diagnostic_limit_reached,
195
228
  path: [],
196
229
  message: "Further diagnostics were omitted."
197
230
  };
198
231
  return;
199
232
  }
200
- const classification = diagnosticReasons[reason];
233
+ const classification = DIAGNOSTIC_REASONS[reason];
201
234
  if (classification.category === "support") complete = false;
202
235
  diagnostics.push({
203
236
  reason,
@@ -322,7 +355,7 @@ function resolveTerm(value, terms, pending) {
322
355
  const term = terms.get(value);
323
356
  if (term != null) return term.iri;
324
357
  const colon = value.indexOf(":");
325
- if (colon < 0) return void 0;
358
+ if (colon < 0 || value.startsWith("_:")) return void 0;
326
359
  if (pending?.has(value.slice(0, colon))) return void 0;
327
360
  const prefix = terms.get(value.slice(0, colon));
328
361
  if (prefix?.prefix) {
@@ -449,7 +482,7 @@ let contextTables;
449
482
  function getContextTables() {
450
483
  if (contextTables != null) return contextTables;
451
484
  const bundledContexts = /* @__PURE__ */ new Map();
452
- for (const url of [activityStreamsContext, "https://w3id.org/security/v1"]) {
485
+ for (const url of [ACTIVITY_STREAMS_CONTEXT, "https://w3id.org/security/v1"]) {
453
486
  const document = preloadedContexts[url];
454
487
  const terms = /* @__PURE__ */ new Map();
455
488
  applyContextTerms(document["@context"], [], terms, (_reason, _path, message) => {
@@ -457,7 +490,7 @@ function getContextTables() {
457
490
  });
458
491
  bundledContexts.set(url, terms);
459
492
  }
460
- const standardTerms = bundledContexts.get(activityStreamsContext);
493
+ const standardTerms = bundledContexts.get(ACTIVITY_STREAMS_CONTEXT);
461
494
  contextTables = {
462
495
  bundledContexts,
463
496
  standardTerms,
@@ -468,7 +501,7 @@ function getContextTables() {
468
501
  function applyContextEntry(value, path, terms, report) {
469
502
  const { bundledContexts, standardTerms } = getContextTables();
470
503
  if (typeof value === "string") {
471
- const url = value === "http://www.w3.org/ns/activitystreams" ? activityStreamsContext : value;
504
+ const url = value === "http://www.w3.org/ns/activitystreams" ? ACTIVITY_STREAMS_CONTEXT : value;
472
505
  const bundled = bundledContexts.get(url);
473
506
  if (bundled == null) {
474
507
  report("context_unavailable_offline", path, "This context is outside the offline allowlist.");
@@ -561,7 +594,7 @@ function checkType(value, path, report, context) {
561
594
  if (typeIri === Link.typeId.href) Type = Link;
562
595
  else if (typeIri === Mention.typeId.href) Type = Mention;
563
596
  }
564
- if (Type == null || !implementedTypes.has(Type)) {
597
+ if (Type == null || !IMPLEMENTED_TYPES.has(Type)) {
565
598
  report(Array.isArray(value) ? "unsupported_value_form" : value == null || typeof value === "string" ? "unsupported_type" : "invalid_value_shape", path, isTypeCombination(value) ? "Type combinations are not supported. Provide a single supported type." : Array.isArray(value) ? "Type arrays are not supported. Provide a single type string." : "This type is not supported by the checker yet.");
566
599
  return;
567
600
  }
@@ -574,7 +607,7 @@ function checkEmbeddedFedifyRange(Type, expected, path, report) {
574
607
  if (expected?.every((Expected) => !isTypeOrSubtype(Type, Expected))) report("unsupported_value_form", path, `Fedify cannot preserve this embedded type for this property. Use ${expected.map((Expected) => Expected.name).join(", ")} or an IRI reference.`);
575
608
  }
576
609
  function getEmbeddedFedifyRange(property, collectionRange) {
577
- if (property === "actor" || property === "attributedTo") return actorTypes;
610
+ if (property === "actor" || property === "attributedTo") return ACTOR_TYPES;
578
611
  return collectionRange?.filter((Expected) => Expected !== Link);
579
612
  }
580
613
  function checkDocumentType(values, fields, path, context, report) {
@@ -598,7 +631,7 @@ function getCollectionRange(Type, property) {
598
631
  if (property === "partOf") return [Collection, Link];
599
632
  if (["next", "prev"].includes(property)) return [CollectionPage, Link];
600
633
  }
601
- if (actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor))) {
634
+ if (ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor))) {
602
635
  if (["inbox", "outbox"].includes(property)) return [OrderedCollection, OrderedCollectionPage];
603
636
  if ([
604
637
  "following",
@@ -766,7 +799,7 @@ function checkDateTime(value, path, report) {
766
799
  }
767
800
  function getCommonPropertyKind(Type, property) {
768
801
  const object = isTypeOrSubtype(Type, Object$1);
769
- const actor = actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor));
802
+ const actor = ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor));
770
803
  const textProperties = object ? [
771
804
  "name",
772
805
  "summary",
@@ -843,6 +876,41 @@ function checkLinkProperty(property, value, path, report) {
843
876
  }
844
877
  else checkLanguageTag(value, path, report);
845
878
  }
879
+ function keywordOf(key, context) {
880
+ if (key.startsWith("@")) return key;
881
+ const iri = context?.terms.get(key)?.iri;
882
+ return iri?.startsWith("@") === true ? iri : void 0;
883
+ }
884
+ function isNodeObject(values, context) {
885
+ return !Object.keys(values).some((key) => VALUE_OBJECT_KEYWORDS.has(keywordOf(key, context) ?? ""));
886
+ }
887
+ function nodeContext(values, context) {
888
+ if (context === void 0 || isNodeObject(values, context)) return context;
889
+ return Object.keys(values).some((key) => {
890
+ const keyword = keywordOf(key, context);
891
+ const value = values[key];
892
+ return keyword == null || keyword === "@value" && value !== null && typeof value === "object" && !isJsonLiteral(values, context);
893
+ }) ? null : void 0;
894
+ }
895
+ function isJsonLiteral(values, context) {
896
+ return Object.keys(values).some((key) => {
897
+ if (keywordOf(key, context) !== "@type") return false;
898
+ const type = values[key];
899
+ return type === "@json" || Array.isArray(type) && type.length === 1 && Object.getOwnPropertyDescriptor(type, 0)?.value === "@json";
900
+ });
901
+ }
902
+ function extensionChildContext(key, value, context) {
903
+ return key === "@context" || keywordOf(key, context) === "@value" || context?.terms.get(key)?.container === "@language" && isLanguageMap(value) ? void 0 : context;
904
+ }
905
+ function isLanguageMap(value) {
906
+ return value !== null && typeof value === "object" && !Array.isArray(value);
907
+ }
908
+ function checkJsonLiteral(values, path, report, context) {
909
+ return checkExtensionKeywords(values, path, report) || isJsonLiteral(values, context);
910
+ }
911
+ function extensionContext(values, context) {
912
+ return context !== void 0 && Object.hasOwn(values, "@context") ? null : context;
913
+ }
846
914
  function checkExtensionKeywords(values, path, report) {
847
915
  if (Object.hasOwn(values, "@id") && typeof values["@id"] !== "string") report("invalid_value_shape", [...path, "@id"], "Expected a string JSON-LD identifier.");
848
916
  let jsonLiteral = values["@type"] === "@json";
@@ -861,18 +929,18 @@ function checkExtensionKeywords(values, path, report) {
861
929
  return jsonLiteral;
862
930
  }
863
931
  function getInapplicableProperties(Type) {
864
- return isTypeOrSubtype(Type, Link) ? objectOnlyProperties : /* @__PURE__ */ new Set();
932
+ return isTypeOrSubtype(Type, Link) ? OBJECT_ONLY_PROPERTIES : /* @__PURE__ */ new Set();
865
933
  }
866
934
  function checkPathDepth(path, report) {
867
- if (path.length <= maximumPathDepth) return true;
868
- report("processing_limit_exceeded", path, `Nesting exceeds the supported limit of ${maximumPathDepth} path segments.`);
935
+ if (path.length <= MAXIMUM_PATH_DEPTH) return true;
936
+ report("processing_limit_exceeded", path, `Nesting exceeds the supported limit of ${MAXIMUM_PATH_DEPTH} path segments.`);
869
937
  return false;
870
938
  }
871
- function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocument, onUnavailableDocument = () => void 0) {
939
+ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocument, onUnavailableDocument = () => void 0, onExtensionNode) {
872
940
  const { report, isFull } = reporter;
873
941
  const pending = [];
874
942
  const ancestors = /* @__PURE__ */ new WeakSet();
875
- function checkExtension(value, path, keywords = true) {
943
+ function checkExtension(value, path, keywords = true, context) {
876
944
  if (!checkPathDepth(path, report)) return;
877
945
  if (!isJsonValueShape(value)) {
878
946
  report("non_json_value", path, "Expected a JSON value.");
@@ -887,15 +955,18 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
887
955
  pending.push(() => ancestors.delete(value));
888
956
  const children = [];
889
957
  if (Array.isArray(value)) visitList(value, path, report, (entry, entryPath) => {
890
- children.push(() => checkExtension(entry, entryPath, keywords));
958
+ children.push(() => checkExtension(entry, entryPath, keywords, context));
891
959
  });
892
960
  else {
893
961
  const values = readDocument(value, path, report);
894
962
  if (values != null) {
895
- const jsonLiteral = keywords && checkExtensionKeywords(values, path, report);
963
+ const own = extensionContext(values, context);
964
+ const jsonLiteral = keywords && checkJsonLiteral(values, path, report, own);
965
+ const node = nodeContext(values, own);
966
+ if (keywords && node !== void 0) onExtensionNode?.(values, path, node);
896
967
  for (const key of Object.keys(values)) {
897
968
  const childKeywords = keywords && !(jsonLiteral && key === "@value");
898
- children.push(() => checkExtension(values[key], [...path, key], childKeywords));
969
+ children.push(() => checkExtension(values[key], [...path, key], childKeywords, extensionChildContext(key, values[key], own)));
899
970
  }
900
971
  }
901
972
  }
@@ -949,7 +1020,7 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
949
1020
  ] : [];
950
1021
  const integerProperties = isLink ? ["height", "width"] : isOrderedPage ? ["totalItems", "startIndex"] : isCollection ? ["totalItems"] : [];
951
1022
  const referenceProperties = ["preview"];
952
- if (isObject) referenceProperties.push("attributedTo", "location", ...addressingProperties);
1023
+ if (isObject) referenceProperties.push("attributedTo", "location", ...ADDRESSING_PROPERTIES);
953
1024
  if (isActivity) referenceProperties.push("actor", "object", "target", "origin", "result", "instrument");
954
1025
  if (isCollection) referenceProperties.push("items", "orderedItems");
955
1026
  const omittableProperties = /* @__PURE__ */ new Set([
@@ -972,7 +1043,7 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
972
1043
  if (key == null) {
973
1044
  if (isOpaqueExtension(field)) {
974
1045
  report("extension_property_unchecked", fieldPath, "This custom property's data is preserved; its semantics were not checked.");
975
- checkExtension(value, fieldPath);
1046
+ checkExtension(value, fieldPath, true, extensionChildContext(source, value, context));
976
1047
  return;
977
1048
  }
978
1049
  report("unsupported_property", fieldPath, "This property is outside the checker's Fedify vocabulary coverage.");
@@ -1259,36 +1330,9 @@ function prepareDocument(document, options) {
1259
1330
  normalizedDocument: preview
1260
1331
  };
1261
1332
  }
1262
- const identifierProperties = /* @__PURE__ */ new Set([
1263
- "id",
1264
- "actor",
1265
- "object",
1266
- "target",
1267
- "origin",
1268
- "result",
1269
- "instrument",
1270
- "attributedTo",
1271
- "location",
1272
- "preview",
1273
- ...addressingProperties,
1274
- "items",
1275
- "orderedItems",
1276
- "current",
1277
- "first",
1278
- "last",
1279
- "partOf",
1280
- "next",
1281
- "prev",
1282
- "inbox",
1283
- "outbox",
1284
- "following",
1285
- "followers",
1286
- "liked",
1287
- "href"
1288
- ]);
1289
1333
  function expandIdentifier(value, context) {
1290
1334
  const colon = value.indexOf(":");
1291
- if (colon <= 0 || value.startsWith("//", colon + 1)) return value;
1335
+ if (colon <= 0 || value.startsWith("//", colon + 1) || value.startsWith("_:")) return value;
1292
1336
  const prefix = context.terms.get(value.slice(0, colon));
1293
1337
  if (prefix?.prefix !== true || prefix.iri.startsWith("@")) return value;
1294
1338
  return prefix.iri + value.slice(colon + 1);
@@ -1296,21 +1340,196 @@ function expandIdentifier(value, context) {
1296
1340
  function nodeKind(Type) {
1297
1341
  if (isTypeOrSubtype(Type, Link)) return "link";
1298
1342
  if (isTypeOrSubtype(Type, Activity)) return "activity";
1299
- if (actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor))) return "actor";
1343
+ if (ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor))) return "actor";
1300
1344
  if (isTypeOrSubtype(Type, Collection)) return "collection";
1301
1345
  return "object";
1302
1346
  }
1347
+ function describeProperties(values, path, fields, context) {
1348
+ const properties = /* @__PURE__ */ new Map();
1349
+ const identifiers = [];
1350
+ for (const { source, name } of fields) {
1351
+ if (name == null) continue;
1352
+ const sources = properties.get(name) ?? [];
1353
+ sources.push(source);
1354
+ properties.set(name, sources);
1355
+ if (!IDENTIFIER_PROPERTIES.has(name)) continue;
1356
+ const value = values[source];
1357
+ const entries = Array.isArray(value) ? value.map((entry, index) => [entry, [
1358
+ ...path,
1359
+ source,
1360
+ index
1361
+ ]]) : [[value, [...path, source]]];
1362
+ for (const [entry, entryPath] of entries) {
1363
+ if (typeof entry !== "string") continue;
1364
+ identifiers.push({
1365
+ property: name,
1366
+ path: entryPath,
1367
+ value: entry,
1368
+ iri: expandIdentifier(entry, context)
1369
+ });
1370
+ }
1371
+ }
1372
+ return {
1373
+ properties,
1374
+ identifiers
1375
+ };
1376
+ }
1377
+ function extensionProperty(source, context) {
1378
+ const { name, iri, compatible } = resolveProperty(source, context);
1379
+ if (name != null) return {
1380
+ name,
1381
+ compatible
1382
+ };
1383
+ if (iri == null) return void 0;
1384
+ const type = context.terms.get(source)?.type;
1385
+ for (const [standard, term] of getContextTables().standardTerms) if (term.iri === iri) return {
1386
+ name: standard,
1387
+ compatible: term.type === type
1388
+ };
1389
+ }
1390
+ const isReadableReference = (value) => value.startsWith("_:") || /^[a-z][a-z\d+.-]*:/iu.test(value) && URL.canParse(value);
1391
+ function typeKind(iri) {
1392
+ const Type = iri === Link.typeId.href ? Link : iri === Mention.typeId.href ? Mention : getEntityTypeById(iri);
1393
+ return Type == null ? void 0 : nodeKind(Type);
1394
+ }
1395
+ function valueEntries(value, path, context) {
1396
+ if (Array.isArray(value)) return value.flatMap((entry, index) => valueEntries(entry, [...path, index], context));
1397
+ if (value !== null && typeof value === "object") {
1398
+ const values = value;
1399
+ if (Object.hasOwn(values, "@context")) return [[value, path]];
1400
+ const wrapper = Object.keys(values).find((key) => {
1401
+ const keyword = keywordOf(key, context);
1402
+ return keyword === "@set" || keyword === "@list";
1403
+ });
1404
+ if (wrapper != null) return valueEntries(values[wrapper], [...path, wrapper], context);
1405
+ }
1406
+ return [[value, path]];
1407
+ }
1408
+ function readExtensionEntry(entry, path, property, context, reading) {
1409
+ const { name, compatible } = property;
1410
+ const reference = IDENTIFIER_PROPERTIES.has(name);
1411
+ if (typeof entry === "string") {
1412
+ if (name === "type") {
1413
+ reading.types.push(context.terms.get(entry)?.iri ?? expandIdentifier(entry, context));
1414
+ return true;
1415
+ }
1416
+ if (!reference) return true;
1417
+ const iri = expandIdentifier(entry, context);
1418
+ if (!compatible || !isReadableReference(iri)) return false;
1419
+ reading.identifiers.push({
1420
+ property: name,
1421
+ path,
1422
+ value: entry,
1423
+ iri
1424
+ });
1425
+ return !iri.startsWith("_:") || name === "id";
1426
+ }
1427
+ if (entry === null) return true;
1428
+ if (typeof entry !== "object") return !reference;
1429
+ if (isNodeObject(entry, context)) {
1430
+ reading.children.push([path, name]);
1431
+ return true;
1432
+ }
1433
+ return !reference;
1434
+ }
1435
+ function hasAmbiguousReference(reading) {
1436
+ return FUNCTIONAL_REFERENCES.some((name) => reading.identifiers.filter(({ property }) => property === name).length > 1 || reading.children.some(([, property]) => property === name));
1437
+ }
1438
+ function describeExtensionNode(values, path, context) {
1439
+ if (context == null) return {
1440
+ node: {
1441
+ path,
1442
+ uninterpreted: true,
1443
+ properties: /* @__PURE__ */ new Map(),
1444
+ identifiers: [],
1445
+ types: []
1446
+ },
1447
+ children: []
1448
+ };
1449
+ const properties = /* @__PURE__ */ new Map();
1450
+ const reading = {
1451
+ identifiers: [],
1452
+ types: [],
1453
+ children: []
1454
+ };
1455
+ let readable = true;
1456
+ for (const source of Object.keys(values).sort()) {
1457
+ const keyword = keywordOf(source, context);
1458
+ if (keyword === "@nest" || keyword === "@reverse") readable = false;
1459
+ const property = extensionProperty(source, context);
1460
+ if (property == null) continue;
1461
+ const { name } = property;
1462
+ properties.set(name, [...properties.get(name) ?? [], source]);
1463
+ if (context.terms.get(source)?.container === "@language" && isLanguageMap(values[source])) {
1464
+ if (IDENTIFIER_PROPERTIES.has(name) && values[source] != null) readable = false;
1465
+ continue;
1466
+ }
1467
+ for (const [entry, entryPath] of valueEntries(values[source], [...path, source], context)) if (!readExtensionEntry(entry, entryPath, property, context, reading)) readable = false;
1468
+ }
1469
+ const { identifiers, types, children } = reading;
1470
+ const kinds = types.map(typeKind).filter((kind) => kind != null);
1471
+ const kind = kinds.includes("link") ? "link" : kinds[0];
1472
+ return {
1473
+ node: {
1474
+ path,
1475
+ uninterpreted: !readable || hasAmbiguousReference(reading),
1476
+ properties,
1477
+ identifiers,
1478
+ types,
1479
+ ...kind == null ? {} : { kind }
1480
+ },
1481
+ children
1482
+ };
1483
+ }
1484
+ function languageMapObjects(values, path, context) {
1485
+ return Object.keys(values).flatMap((source) => {
1486
+ if (context.terms.get(source)?.container !== "@language") return [];
1487
+ const map = values[source];
1488
+ if (!isLanguageMap(map)) return [];
1489
+ return Object.entries(map).flatMap(([language, entry]) => {
1490
+ return (Array.isArray(entry) ? entry.map((item, index) => [item, [
1491
+ ...path,
1492
+ source,
1493
+ language,
1494
+ index
1495
+ ]]) : [[entry, [
1496
+ ...path,
1497
+ source,
1498
+ language
1499
+ ]]]).filter(([item]) => item !== null && typeof item === "object").map(([, itemPath]) => itemPath);
1500
+ });
1501
+ });
1502
+ }
1503
+ function nameEmbeddingProperties(nodes) {
1504
+ const byPath = new Map(nodes.map((node) => [JSON.stringify(node.path), node]));
1505
+ return nodes.map((node) => {
1506
+ for (const length of [node.path.length - 1, node.path.length - 2]) {
1507
+ const parent = byPath.get(JSON.stringify(node.path.slice(0, length)));
1508
+ const key = node.path[length];
1509
+ if (parent == null || typeof key !== "string") continue;
1510
+ if (length < node.path.length - 1 && typeof node.path.at(-1) !== "number") continue;
1511
+ for (const [name, sources] of parent.properties) if (sources.includes(key)) return {
1512
+ ...node,
1513
+ property: name
1514
+ };
1515
+ }
1516
+ return node;
1517
+ });
1518
+ }
1303
1519
  /**
1304
1520
  * Describes the vocabulary documents the checker reads, so that write paths
1305
1521
  * interpret aliases, full-IRI keys, and keyword aliases exactly as
1306
1522
  * validation did, instead of matching literal key spellings. Opaque
1307
- * extension values are not descended into.
1523
+ * extension values are not read as vocabulary documents, but the JSON-LD
1524
+ * node objects in them are described as extensions of the document that
1525
+ * holds them.
1308
1526
  * @returns The root and every embedded document in traversal order, or
1309
1527
  * `null` unless validation completes without errors.
1310
1528
  */
1311
1529
  function describeDocument(document) {
1312
1530
  const reporter = createReporter();
1313
1531
  const visited = [];
1532
+ const extensions = [];
1314
1533
  checkDocumentValue(document, [], reporter, [], (values, path, context, Type, fields) => {
1315
1534
  visited.push({
1316
1535
  values,
@@ -1319,58 +1538,49 @@ function describeDocument(document) {
1319
1538
  Type,
1320
1539
  fields
1321
1540
  });
1541
+ }, void 0, (values, path, context) => {
1542
+ extensions.push({
1543
+ values,
1544
+ path,
1545
+ context
1546
+ });
1322
1547
  });
1323
1548
  const result = reporter.result();
1324
1549
  if (!result.complete || result.diagnostics.some(({ severity }) => severity === "error")) return null;
1325
- const nodes = visited.map(({ values, path, context, Type, fields }) => {
1326
- const properties = /* @__PURE__ */ new Map();
1327
- const identifiers = [];
1328
- for (const { source, name, compatible } of fields) {
1329
- if (name == null || !compatible) continue;
1330
- const sources = properties.get(name) ?? [];
1331
- sources.push(source);
1332
- properties.set(name, sources);
1333
- if (!identifierProperties.has(name)) continue;
1334
- const value = values[source];
1335
- const entries = Array.isArray(value) ? value.map((entry, index) => [entry, [
1336
- ...path,
1337
- source,
1338
- index
1339
- ]]) : [[value, [...path, source]]];
1340
- for (const [entry, entryPath] of entries) {
1341
- if (typeof entry !== "string") continue;
1342
- identifiers.push({
1343
- property: name,
1344
- path: entryPath,
1345
- value: entry,
1346
- iri: expandIdentifier(entry, context)
1347
- });
1348
- }
1349
- }
1550
+ const documents = visited.map(({ values, path, context, Type, fields }) => ({
1551
+ path,
1552
+ type: Type.typeId.href,
1553
+ kind: nodeKind(Type),
1554
+ ...describeProperties(values, path, fields.filter(({ compatible }) => compatible), context)
1555
+ }));
1556
+ const described = [...extensions.map(({ values, path, context }) => describeExtensionNode(values, path, context)), ...[...visited, ...extensions].flatMap(({ values, path, context }) => context == null ? [] : languageMapObjects(values, path, context).map((object) => describeExtensionNode({}, object, null)))];
1557
+ const holders = new Map(described.flatMap(({ node, children }) => children.map(([path, property]) => [JSON.stringify(path), {
1558
+ property,
1559
+ parent: node.path
1560
+ }])));
1561
+ const uses = Map.groupBy([...documents, ...described.map(({ node }) => node)].flatMap(({ identifiers }) => identifiers), ({ iri }) => iri);
1562
+ const extensionNodes = described.map(({ node }) => {
1563
+ const id = node.identifiers.find(({ property }) => property === "id");
1564
+ const joined = id != null && (uses.get(id.iri)?.length ?? 0) > node.identifiers.filter(({ iri }) => iri === id.iri).length;
1350
1565
  return {
1351
- path,
1352
- type: Type.typeId.href,
1353
- kind: nodeKind(Type),
1354
- properties,
1355
- identifiers
1566
+ ...node,
1567
+ ...holders.get(JSON.stringify(node.path)),
1568
+ ...joined ? { uninterpreted: true } : {}
1356
1569
  };
1357
1570
  });
1358
- const byPath = new Map(nodes.map((node) => [JSON.stringify(node.path), node]));
1359
- return nodes.map((node) => {
1360
- if (node.path.length === 0) return node;
1361
- for (const length of [node.path.length - 1, node.path.length - 2]) {
1362
- const parent = byPath.get(JSON.stringify(node.path.slice(0, length)));
1363
- const key = node.path[length];
1364
- if (parent == null || typeof key !== "string") continue;
1365
- for (const [name, sources] of parent.properties) if (sources.includes(key)) return {
1366
- ...node,
1367
- property: name
1368
- };
1369
- }
1370
- return node;
1371
- });
1571
+ const owners = new Map(visited.map(({ path }) => [JSON.stringify(path), []]));
1572
+ for (const node of extensionNodes) for (let length = node.path.length - 1; length >= 0; length -= 1) {
1573
+ const owner = owners.get(JSON.stringify(node.path.slice(0, length)));
1574
+ if (owner == null) continue;
1575
+ owner.push(node);
1576
+ break;
1577
+ }
1578
+ return nameEmbeddingProperties(documents.map((node) => ({
1579
+ ...node,
1580
+ extensions: owners.get(JSON.stringify(node.path)) ?? []
1581
+ })));
1372
1582
  }
1373
1583
  //#endregion
1374
- export { addressingProperties, describeDocument, diagnosticReasons, prepareDocument, validateDocument };
1584
+ export { ADDRESSING_PROPERTIES, DIAGNOSTIC_REASONS, describeDocument, prepareDocument, validateDocument };
1375
1585
 
1376
1586
  //# sourceMappingURL=document.mjs.map