@fedify/vocab-tools 2.3.9-dev.2048 → 2.3.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.3.9-dev.2048+c88fcc8c",
3
+ "version": "2.3.10",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts"
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.3.9-dev.2048+c88fcc8c";
11
+ var version = "2.3.10";
12
12
  //#endregion
13
13
  //#region src/type.ts
14
14
  const HEURISTICS_CONTEXTS = [
@@ -331,13 +331,16 @@ const scalarTypes = {
331
331
  return `${v}.href`;
332
332
  },
333
333
  dataCheck(v) {
334
- return `typeof ${v} === "object" && "@value" in ${v}
335
- && typeof ${v}["@value"] === "string"
336
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
334
+ return `typeof ${v} === "object" &&
335
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
336
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
337
337
  },
338
338
  decoder(v) {
339
- return `new URL(${v}["@value"])`;
340
- }
339
+ return `decodeIri(
340
+ typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"]
341
+ )`;
342
+ },
343
+ skipUnparsable: true
341
344
  },
342
345
  "fedify:publicKey": {
343
346
  name: "CryptoKey",
@@ -549,6 +552,9 @@ function getDecoder(typeUri, types, variable, optionsVariable, baseUrlVariable)
549
552
  )`;
550
553
  throw new Error(`Unknown type: ${typeUri}`);
551
554
  }
555
+ function skipsUnparsable(typeUri) {
556
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
557
+ }
552
558
  function getDataCheck(typeUri, types, variable) {
553
559
  if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
554
560
  if (typeUri in types) {
@@ -1114,8 +1120,15 @@ async function* generateDecoder(typeUri, types, moduleVarNames) {
1114
1120
  yield `
1115
1121
  const decoded =
1116
1122
  `;
1117
- if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", propertyBaseUrl);
1118
- else {
1123
+ const lenient = property.range.length == 1 && skipsUnparsable(property.range[0]);
1124
+ if (property.range.length == 1) {
1125
+ if (lenient) {
1126
+ yield getDataCheck(property.range[0], types, "v");
1127
+ yield " ? ";
1128
+ }
1129
+ yield getDecoder(property.range[0], types, "v", "options", propertyBaseUrl);
1130
+ if (lenient) yield " : undefined";
1131
+ } else {
1119
1132
  const decoders = getDecoders(property.range, types, "v", "options", propertyBaseUrl);
1120
1133
  for (const code of decoders) yield code;
1121
1134
  }
@@ -1763,8 +1776,7 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1763
1776
  return fetched;
1764
1777
  }
1765
1778
  `;
1766
- if (property.compactName != null) {
1767
- yield `
1779
+ if (property.compactName != null) yield `
1768
1780
  if (
1769
1781
  this._cachedJsonLd != null &&
1770
1782
  typeof this._cachedJsonLd === "object" &&
@@ -1775,18 +1787,22 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1775
1787
  ${JSON.stringify(property.compactName)}];
1776
1788
  const doc = Array.isArray(prop) ? prop[0] : prop;
1777
1789
  if (doc != null && typeof doc === "object" && "@context" in doc) {
1778
- `;
1779
- yield `
1780
- v = await this.#${property.singularName}_fromJsonLd(doc, {
1781
- ...options,
1782
- baseUrl: ${cachedPropertyBaseUrl},
1783
- });
1784
- `;
1785
- yield `
1790
+ try {
1791
+ v = await this.#${property.singularName}_fromJsonLd(doc, {
1792
+ ...options,
1793
+ baseUrl: ${cachedPropertyBaseUrl},
1794
+ });
1795
+ } catch (error) {
1796
+ if (!options.suppressError) throw error;
1797
+ getLogger(["fedify", "vocab"]).debug(
1798
+ "Failed to parse embedded value of {property}: {error}",
1799
+ { property: ${JSON.stringify(property.uri)}, error },
1800
+ );
1801
+ return null;
1802
+ }
1786
1803
  }
1787
1804
  }
1788
1805
  `;
1789
- }
1790
1806
  yield `
1791
1807
  if (options.crossOrigin !== "trust" && v?.id != null &&
1792
1808
  this.id != null && v.id.origin !== this.id.origin &&
@@ -1869,8 +1885,7 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1869
1885
  continue;
1870
1886
  }
1871
1887
  `;
1872
- if (property.compactName != null) {
1873
- yield `
1888
+ if (property.compactName != null) yield `
1874
1889
  if (
1875
1890
  this._cachedJsonLd != null &&
1876
1891
  typeof this._cachedJsonLd === "object" &&
@@ -1881,18 +1896,22 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1881
1896
  ${JSON.stringify(property.compactName)}];
1882
1897
  const obj = Array.isArray(prop) ? prop[i] : prop;
1883
1898
  if (obj != null && typeof obj === "object" && "@context" in obj) {
1884
- `;
1885
- yield `
1886
- v = await this.#${property.singularName}_fromJsonLd(obj, {
1887
- ...options,
1888
- baseUrl: ${cachedPropertyBaseUrl},
1889
- });
1890
- `;
1891
- yield `
1899
+ try {
1900
+ v = await this.#${property.singularName}_fromJsonLd(obj, {
1901
+ ...options,
1902
+ baseUrl: ${cachedPropertyBaseUrl},
1903
+ });
1904
+ } catch (error) {
1905
+ if (!options.suppressError) throw error;
1906
+ getLogger(["fedify", "vocab"]).debug(
1907
+ "Failed to parse embedded value of {property} at index {index}: {error}",
1908
+ { property: ${JSON.stringify(property.uri)}, index: i, error },
1909
+ );
1910
+ continue;
1911
+ }
1892
1912
  }
1893
1913
  }
1894
1914
  `;
1895
- }
1896
1915
  yield `
1897
1916
  if (options.crossOrigin !== "trust" && v?.id != null &&
1898
1917
  this.id != null && v.id.origin !== this.id.origin &&
@@ -2111,6 +2130,17 @@ async function* generateClasses(types) {
2111
2130
  isTemporalInstant,
2112
2131
  } from "@fedify/vocab-runtime/temporal";\n`;
2113
2132
  yield `
2133
+ function canDecodeIri(iri: string): boolean {
2134
+ return URL.canParse(iri) || iri.startsWith("at://");
2135
+ }
2136
+
2137
+ function decodeIri(iri: string): URL {
2138
+ return !URL.canParse(iri) && iri.startsWith("at://")
2139
+ ? new URL("at://" + encodeURIComponent(iri.substring(5)))
2140
+ : new URL(iri);
2141
+ }
2142
+ `;
2143
+ yield `
2114
2144
  function isValidLanguageTag(language: string): boolean {
2115
2145
  try {
2116
2146
  new Intl.Locale(language);
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.3.9-dev.2048+c88fcc8c";
10
+ var version = "2.3.10";
11
11
  //#endregion
12
12
  //#region src/type.ts
13
13
  const HEURISTICS_CONTEXTS = [
@@ -330,13 +330,16 @@ const scalarTypes = {
330
330
  return `${v}.href`;
331
331
  },
332
332
  dataCheck(v) {
333
- return `typeof ${v} === "object" && "@value" in ${v}
334
- && typeof ${v}["@value"] === "string"
335
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
333
+ return `typeof ${v} === "object" &&
334
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
335
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
336
336
  },
337
337
  decoder(v) {
338
- return `new URL(${v}["@value"])`;
339
- }
338
+ return `decodeIri(
339
+ typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"]
340
+ )`;
341
+ },
342
+ skipUnparsable: true
340
343
  },
341
344
  "fedify:publicKey": {
342
345
  name: "CryptoKey",
@@ -548,6 +551,9 @@ function getDecoder(typeUri, types, variable, optionsVariable, baseUrlVariable)
548
551
  )`;
549
552
  throw new Error(`Unknown type: ${typeUri}`);
550
553
  }
554
+ function skipsUnparsable(typeUri) {
555
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
556
+ }
551
557
  function getDataCheck(typeUri, types, variable) {
552
558
  if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
553
559
  if (typeUri in types) {
@@ -1113,8 +1119,15 @@ async function* generateDecoder(typeUri, types, moduleVarNames) {
1113
1119
  yield `
1114
1120
  const decoded =
1115
1121
  `;
1116
- if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", propertyBaseUrl);
1117
- else {
1122
+ const lenient = property.range.length == 1 && skipsUnparsable(property.range[0]);
1123
+ if (property.range.length == 1) {
1124
+ if (lenient) {
1125
+ yield getDataCheck(property.range[0], types, "v");
1126
+ yield " ? ";
1127
+ }
1128
+ yield getDecoder(property.range[0], types, "v", "options", propertyBaseUrl);
1129
+ if (lenient) yield " : undefined";
1130
+ } else {
1118
1131
  const decoders = getDecoders(property.range, types, "v", "options", propertyBaseUrl);
1119
1132
  for (const code of decoders) yield code;
1120
1133
  }
@@ -1762,8 +1775,7 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1762
1775
  return fetched;
1763
1776
  }
1764
1777
  `;
1765
- if (property.compactName != null) {
1766
- yield `
1778
+ if (property.compactName != null) yield `
1767
1779
  if (
1768
1780
  this._cachedJsonLd != null &&
1769
1781
  typeof this._cachedJsonLd === "object" &&
@@ -1774,18 +1786,22 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1774
1786
  ${JSON.stringify(property.compactName)}];
1775
1787
  const doc = Array.isArray(prop) ? prop[0] : prop;
1776
1788
  if (doc != null && typeof doc === "object" && "@context" in doc) {
1777
- `;
1778
- yield `
1779
- v = await this.#${property.singularName}_fromJsonLd(doc, {
1780
- ...options,
1781
- baseUrl: ${cachedPropertyBaseUrl},
1782
- });
1783
- `;
1784
- yield `
1789
+ try {
1790
+ v = await this.#${property.singularName}_fromJsonLd(doc, {
1791
+ ...options,
1792
+ baseUrl: ${cachedPropertyBaseUrl},
1793
+ });
1794
+ } catch (error) {
1795
+ if (!options.suppressError) throw error;
1796
+ getLogger(["fedify", "vocab"]).debug(
1797
+ "Failed to parse embedded value of {property}: {error}",
1798
+ { property: ${JSON.stringify(property.uri)}, error },
1799
+ );
1800
+ return null;
1801
+ }
1785
1802
  }
1786
1803
  }
1787
1804
  `;
1788
- }
1789
1805
  yield `
1790
1806
  if (options.crossOrigin !== "trust" && v?.id != null &&
1791
1807
  this.id != null && v.id.origin !== this.id.origin &&
@@ -1868,8 +1884,7 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1868
1884
  continue;
1869
1885
  }
1870
1886
  `;
1871
- if (property.compactName != null) {
1872
- yield `
1887
+ if (property.compactName != null) yield `
1873
1888
  if (
1874
1889
  this._cachedJsonLd != null &&
1875
1890
  typeof this._cachedJsonLd === "object" &&
@@ -1880,18 +1895,22 @@ async function* generateProperty(type, property, types, moduleVarNames) {
1880
1895
  ${JSON.stringify(property.compactName)}];
1881
1896
  const obj = Array.isArray(prop) ? prop[i] : prop;
1882
1897
  if (obj != null && typeof obj === "object" && "@context" in obj) {
1883
- `;
1884
- yield `
1885
- v = await this.#${property.singularName}_fromJsonLd(obj, {
1886
- ...options,
1887
- baseUrl: ${cachedPropertyBaseUrl},
1888
- });
1889
- `;
1890
- yield `
1898
+ try {
1899
+ v = await this.#${property.singularName}_fromJsonLd(obj, {
1900
+ ...options,
1901
+ baseUrl: ${cachedPropertyBaseUrl},
1902
+ });
1903
+ } catch (error) {
1904
+ if (!options.suppressError) throw error;
1905
+ getLogger(["fedify", "vocab"]).debug(
1906
+ "Failed to parse embedded value of {property} at index {index}: {error}",
1907
+ { property: ${JSON.stringify(property.uri)}, index: i, error },
1908
+ );
1909
+ continue;
1910
+ }
1891
1911
  }
1892
1912
  }
1893
1913
  `;
1894
- }
1895
1914
  yield `
1896
1915
  if (options.crossOrigin !== "trust" && v?.id != null &&
1897
1916
  this.id != null && v.id.origin !== this.id.origin &&
@@ -2110,6 +2129,17 @@ async function* generateClasses(types) {
2110
2129
  isTemporalInstant,
2111
2130
  } from "@fedify/vocab-runtime/temporal";\n`;
2112
2131
  yield `
2132
+ function canDecodeIri(iri: string): boolean {
2133
+ return URL.canParse(iri) || iri.startsWith("at://");
2134
+ }
2135
+
2136
+ function decodeIri(iri: string): URL {
2137
+ return !URL.canParse(iri) && iri.startsWith("at://")
2138
+ ? new URL("at://" + encodeURIComponent(iri.substring(5)))
2139
+ : new URL(iri);
2140
+ }
2141
+ `;
2142
+ yield `
2113
2143
  function isValidLanguageTag(language: string): boolean {
2114
2144
  try {
2115
2145
  new Intl.Locale(language);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.3.9-dev.2048+c88fcc8c",
3
+ "version": "2.3.10",
4
4
  "description": "Code generator for Activity Vocabulary APIs",
5
5
  "homepage": "https://fedify.dev/",
6
6
  "repository": {