@drfed/models 0.2.0-dev.698 → 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,24 +89,25 @@ 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,
99
99
  Person,
100
100
  Service
101
101
  ];
102
- const addressingProperties = [
102
+ /** The ActivityStreams properties that address a document's recipients. */
103
+ const ADDRESSING_PROPERTIES = [
103
104
  "to",
104
105
  "cc",
105
106
  "bto",
106
107
  "bcc",
107
108
  "audience"
108
109
  ];
109
- const objectOnlyProperties = /* @__PURE__ */ new Set([
110
+ const OBJECT_ONLY_PROPERTIES = /* @__PURE__ */ new Set([
110
111
  "attachment",
111
112
  "attributedTo",
112
113
  "audience",
@@ -133,7 +134,7 @@ const objectOnlyProperties = /* @__PURE__ */ new Set([
133
134
  "bcc",
134
135
  "duration"
135
136
  ]);
136
- const implementedTypes = /* @__PURE__ */ new Set([
137
+ const IMPLEMENTED_TYPES = /* @__PURE__ */ new Set([
137
138
  Note,
138
139
  Object$1,
139
140
  Article,
@@ -179,24 +180,57 @@ const implementedTypes = /* @__PURE__ */ new Set([
179
180
  Update,
180
181
  View
181
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"];
182
216
  function createReporter() {
183
217
  const diagnostics = [];
184
218
  let complete = true;
185
219
  let truncated = false;
186
220
  const report = (reason, path, message) => {
187
221
  if (truncated) return;
188
- if (diagnostics.length === maximumDiagnostics) {
222
+ if (diagnostics.length === MAXIMUM_DIAGNOSTICS) {
189
223
  complete = false;
190
224
  truncated = true;
191
225
  diagnostics[99] = {
192
226
  reason: "diagnostic_limit_reached",
193
- ...diagnosticReasons.diagnostic_limit_reached,
227
+ ...DIAGNOSTIC_REASONS.diagnostic_limit_reached,
194
228
  path: [],
195
229
  message: "Further diagnostics were omitted."
196
230
  };
197
231
  return;
198
232
  }
199
- const classification = diagnosticReasons[reason];
233
+ const classification = DIAGNOSTIC_REASONS[reason];
200
234
  if (classification.category === "support") complete = false;
201
235
  diagnostics.push({
202
236
  reason,
@@ -321,7 +355,7 @@ function resolveTerm(value, terms, pending) {
321
355
  const term = terms.get(value);
322
356
  if (term != null) return term.iri;
323
357
  const colon = value.indexOf(":");
324
- if (colon < 0) return void 0;
358
+ if (colon < 0 || value.startsWith("_:")) return void 0;
325
359
  if (pending?.has(value.slice(0, colon))) return void 0;
326
360
  const prefix = terms.get(value.slice(0, colon));
327
361
  if (prefix?.prefix) {
@@ -448,7 +482,7 @@ let contextTables;
448
482
  function getContextTables() {
449
483
  if (contextTables != null) return contextTables;
450
484
  const bundledContexts = /* @__PURE__ */ new Map();
451
- for (const url of [activityStreamsContext, "https://w3id.org/security/v1"]) {
485
+ for (const url of [ACTIVITY_STREAMS_CONTEXT, "https://w3id.org/security/v1"]) {
452
486
  const document = preloadedContexts[url];
453
487
  const terms = /* @__PURE__ */ new Map();
454
488
  applyContextTerms(document["@context"], [], terms, (_reason, _path, message) => {
@@ -456,7 +490,7 @@ function getContextTables() {
456
490
  });
457
491
  bundledContexts.set(url, terms);
458
492
  }
459
- const standardTerms = bundledContexts.get(activityStreamsContext);
493
+ const standardTerms = bundledContexts.get(ACTIVITY_STREAMS_CONTEXT);
460
494
  contextTables = {
461
495
  bundledContexts,
462
496
  standardTerms,
@@ -467,7 +501,7 @@ function getContextTables() {
467
501
  function applyContextEntry(value, path, terms, report) {
468
502
  const { bundledContexts, standardTerms } = getContextTables();
469
503
  if (typeof value === "string") {
470
- 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;
471
505
  const bundled = bundledContexts.get(url);
472
506
  if (bundled == null) {
473
507
  report("context_unavailable_offline", path, "This context is outside the offline allowlist.");
@@ -560,7 +594,7 @@ function checkType(value, path, report, context) {
560
594
  if (typeIri === Link.typeId.href) Type = Link;
561
595
  else if (typeIri === Mention.typeId.href) Type = Mention;
562
596
  }
563
- if (Type == null || !implementedTypes.has(Type)) {
597
+ if (Type == null || !IMPLEMENTED_TYPES.has(Type)) {
564
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.");
565
599
  return;
566
600
  }
@@ -573,7 +607,7 @@ function checkEmbeddedFedifyRange(Type, expected, path, report) {
573
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.`);
574
608
  }
575
609
  function getEmbeddedFedifyRange(property, collectionRange) {
576
- if (property === "actor" || property === "attributedTo") return actorTypes;
610
+ if (property === "actor" || property === "attributedTo") return ACTOR_TYPES;
577
611
  return collectionRange?.filter((Expected) => Expected !== Link);
578
612
  }
579
613
  function checkDocumentType(values, fields, path, context, report) {
@@ -597,7 +631,7 @@ function getCollectionRange(Type, property) {
597
631
  if (property === "partOf") return [Collection, Link];
598
632
  if (["next", "prev"].includes(property)) return [CollectionPage, Link];
599
633
  }
600
- if (actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor))) {
634
+ if (ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor))) {
601
635
  if (["inbox", "outbox"].includes(property)) return [OrderedCollection, OrderedCollectionPage];
602
636
  if ([
603
637
  "following",
@@ -765,7 +799,7 @@ function checkDateTime(value, path, report) {
765
799
  }
766
800
  function getCommonPropertyKind(Type, property) {
767
801
  const object = isTypeOrSubtype(Type, Object$1);
768
- const actor = actorTypes.some((Actor) => isTypeOrSubtype(Type, Actor));
802
+ const actor = ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor));
769
803
  const textProperties = object ? [
770
804
  "name",
771
805
  "summary",
@@ -842,6 +876,41 @@ function checkLinkProperty(property, value, path, report) {
842
876
  }
843
877
  else checkLanguageTag(value, path, report);
844
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
+ }
845
914
  function checkExtensionKeywords(values, path, report) {
846
915
  if (Object.hasOwn(values, "@id") && typeof values["@id"] !== "string") report("invalid_value_shape", [...path, "@id"], "Expected a string JSON-LD identifier.");
847
916
  let jsonLiteral = values["@type"] === "@json";
@@ -860,18 +929,18 @@ function checkExtensionKeywords(values, path, report) {
860
929
  return jsonLiteral;
861
930
  }
862
931
  function getInapplicableProperties(Type) {
863
- return isTypeOrSubtype(Type, Link) ? objectOnlyProperties : /* @__PURE__ */ new Set();
932
+ return isTypeOrSubtype(Type, Link) ? OBJECT_ONLY_PROPERTIES : /* @__PURE__ */ new Set();
864
933
  }
865
934
  function checkPathDepth(path, report) {
866
- if (path.length <= maximumPathDepth) return true;
867
- 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.`);
868
937
  return false;
869
938
  }
870
- function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocument, onUnavailableDocument = () => void 0) {
939
+ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocument, onUnavailableDocument = () => void 0, onExtensionNode) {
871
940
  const { report, isFull } = reporter;
872
941
  const pending = [];
873
942
  const ancestors = /* @__PURE__ */ new WeakSet();
874
- function checkExtension(value, path, keywords = true) {
943
+ function checkExtension(value, path, keywords = true, context) {
875
944
  if (!checkPathDepth(path, report)) return;
876
945
  if (!isJsonValueShape(value)) {
877
946
  report("non_json_value", path, "Expected a JSON value.");
@@ -886,15 +955,18 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
886
955
  pending.push(() => ancestors.delete(value));
887
956
  const children = [];
888
957
  if (Array.isArray(value)) visitList(value, path, report, (entry, entryPath) => {
889
- children.push(() => checkExtension(entry, entryPath, keywords));
958
+ children.push(() => checkExtension(entry, entryPath, keywords, context));
890
959
  });
891
960
  else {
892
961
  const values = readDocument(value, path, report);
893
962
  if (values != null) {
894
- 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);
895
967
  for (const key of Object.keys(values)) {
896
968
  const childKeywords = keywords && !(jsonLiteral && key === "@value");
897
- children.push(() => checkExtension(values[key], [...path, key], childKeywords));
969
+ children.push(() => checkExtension(values[key], [...path, key], childKeywords, extensionChildContext(key, values[key], own)));
898
970
  }
899
971
  }
900
972
  }
@@ -933,7 +1005,7 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
933
1005
  return;
934
1006
  }
935
1007
  checkEmbeddedFedifyRange(Type, fedifyTypes, path, report);
936
- onDocument?.(values, path, context);
1008
+ onDocument?.(values, path, context, Type, fields);
937
1009
  const isObject = isTypeOrSubtype(Type, Object$1);
938
1010
  const isLink = isTypeOrSubtype(Type, Link);
939
1011
  const isActivity = isTypeOrSubtype(Type, Activity);
@@ -948,7 +1020,7 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
948
1020
  ] : [];
949
1021
  const integerProperties = isLink ? ["height", "width"] : isOrderedPage ? ["totalItems", "startIndex"] : isCollection ? ["totalItems"] : [];
950
1022
  const referenceProperties = ["preview"];
951
- if (isObject) referenceProperties.push("attributedTo", "location", ...addressingProperties);
1023
+ if (isObject) referenceProperties.push("attributedTo", "location", ...ADDRESSING_PROPERTIES);
952
1024
  if (isActivity) referenceProperties.push("actor", "object", "target", "origin", "result", "instrument");
953
1025
  if (isCollection) referenceProperties.push("items", "orderedItems");
954
1026
  const omittableProperties = /* @__PURE__ */ new Set([
@@ -971,7 +1043,7 @@ function checkDocumentValue(rootDocument, rootPath, reporter, changes, onDocumen
971
1043
  if (key == null) {
972
1044
  if (isOpaqueExtension(field)) {
973
1045
  report("extension_property_unchecked", fieldPath, "This custom property's data is preserved; its semantics were not checked.");
974
- checkExtension(value, fieldPath);
1046
+ checkExtension(value, fieldPath, true, extensionChildContext(source, value, context));
975
1047
  return;
976
1048
  }
977
1049
  report("unsupported_property", fieldPath, "This property is outside the checker's Fedify vocabulary coverage.");
@@ -1258,7 +1330,257 @@ function prepareDocument(document, options) {
1258
1330
  normalizedDocument: preview
1259
1331
  };
1260
1332
  }
1333
+ function expandIdentifier(value, context) {
1334
+ const colon = value.indexOf(":");
1335
+ if (colon <= 0 || value.startsWith("//", colon + 1) || value.startsWith("_:")) return value;
1336
+ const prefix = context.terms.get(value.slice(0, colon));
1337
+ if (prefix?.prefix !== true || prefix.iri.startsWith("@")) return value;
1338
+ return prefix.iri + value.slice(colon + 1);
1339
+ }
1340
+ function nodeKind(Type) {
1341
+ if (isTypeOrSubtype(Type, Link)) return "link";
1342
+ if (isTypeOrSubtype(Type, Activity)) return "activity";
1343
+ if (ACTOR_TYPES.some((Actor) => isTypeOrSubtype(Type, Actor))) return "actor";
1344
+ if (isTypeOrSubtype(Type, Collection)) return "collection";
1345
+ return "object";
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
+ }
1519
+ /**
1520
+ * Describes the vocabulary documents the checker reads, so that write paths
1521
+ * interpret aliases, full-IRI keys, and keyword aliases exactly as
1522
+ * validation did, instead of matching literal key spellings. Opaque
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.
1526
+ * @returns The root and every embedded document in traversal order, or
1527
+ * `null` unless validation completes without errors.
1528
+ */
1529
+ function describeDocument(document) {
1530
+ const reporter = createReporter();
1531
+ const visited = [];
1532
+ const extensions = [];
1533
+ checkDocumentValue(document, [], reporter, [], (values, path, context, Type, fields) => {
1534
+ visited.push({
1535
+ values,
1536
+ path,
1537
+ context,
1538
+ Type,
1539
+ fields
1540
+ });
1541
+ }, void 0, (values, path, context) => {
1542
+ extensions.push({
1543
+ values,
1544
+ path,
1545
+ context
1546
+ });
1547
+ });
1548
+ const result = reporter.result();
1549
+ if (!result.complete || result.diagnostics.some(({ severity }) => severity === "error")) return null;
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;
1565
+ return {
1566
+ ...node,
1567
+ ...holders.get(JSON.stringify(node.path)),
1568
+ ...joined ? { uninterpreted: true } : {}
1569
+ };
1570
+ });
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
+ })));
1582
+ }
1261
1583
  //#endregion
1262
- export { diagnosticReasons, prepareDocument, validateDocument };
1584
+ export { ADDRESSING_PROPERTIES, DIAGNOSTIC_REASONS, describeDocument, prepareDocument, validateDocument };
1263
1585
 
1264
1586
  //# sourceMappingURL=document.mjs.map