@fedify/vocab-tools 2.2.6 → 2.2.7

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.2.6",
3
+ "version": "2.2.7",
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.2.6";
11
+ var version = "2.2.7";
12
12
  //#endregion
13
13
  //#region src/type.ts
14
14
  const HEURISTICS_CONTEXTS = [
@@ -199,7 +199,8 @@ const scalarTypes = {
199
199
  dataCheck(v) {
200
200
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
201
201
  && typeof ${v}["@language"] === "string"
202
- && typeof ${v}["@value"] === "string"`;
202
+ && typeof ${v}["@value"] === "string"
203
+ && isValidLanguageTag(${v}["@language"])`;
203
204
  },
204
205
  decoder(v) {
205
206
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -1036,6 +1037,9 @@ async function* generateDecoder(typeUri, types) {
1036
1037
  throw new TypeError("Unexpected type: " + instance.constructor.name);
1037
1038
  }
1038
1039
  `;
1040
+ yield `
1041
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
1042
+ `;
1039
1043
  for (const property of type.properties) {
1040
1044
  const variable = await getFieldName(property.uri, "");
1041
1045
  yield await generateField(property, types, "const ");
@@ -1069,29 +1073,40 @@ async function* generateDecoder(typeUri, types) {
1069
1073
  continue;
1070
1074
  }
1071
1075
  `;
1072
- if (property.range.length == 1) yield `
1073
- const decoded = ${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)};
1074
- if (typeof decoded === "undefined") continue;
1075
- ${variable}.push(decoded);`;
1076
- else {
1077
- yield `
1076
+ yield `
1078
1077
  const decoded =
1079
- `;
1078
+ `;
1079
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1080
+ else {
1080
1081
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1081
1082
  for (const code of decoders) yield code;
1082
- yield `
1083
+ }
1084
+ yield `
1083
1085
  ;
1084
- if (typeof decoded === "undefined") continue;
1086
+ `;
1087
+ yield `
1088
+ if (typeof decoded === "undefined") {
1089
+ shouldCacheJsonLd = false;
1090
+ continue;
1091
+ }
1092
+ `;
1093
+ yield `
1094
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1095
+ shouldCacheJsonLd = false;
1096
+ }
1085
1097
  ${variable}.push(decoded);
1086
- `;
1087
- }
1098
+ `;
1088
1099
  yield `
1089
1100
  }
1090
1101
  instance.${await getFieldName(property.uri)} = ${variable};
1091
1102
  `;
1092
1103
  }
1093
1104
  yield `
1094
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1105
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1106
+ if (
1107
+ shouldCacheJsonLd &&
1108
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1109
+ ) {
1095
1110
  try {
1096
1111
  instance._cachedJsonLd = structuredClone(json);
1097
1112
  } catch {
@@ -1858,6 +1873,7 @@ async function* generateClass(typeUri, types) {
1858
1873
  values?: Record<string, unknown>;
1859
1874
  };
1860
1875
  #cachedJsonLd?: unknown;
1876
+ #shouldCacheJsonLd = true;
1861
1877
  readonly id: URL | null;
1862
1878
 
1863
1879
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1887,6 +1903,20 @@ async function* generateClass(typeUri, types) {
1887
1903
  protected set _cachedJsonLd(value: unknown | undefined) {
1888
1904
  this.#cachedJsonLd = value;
1889
1905
  }
1906
+
1907
+ protected get _shouldCacheJsonLd(): boolean {
1908
+ return this.#shouldCacheJsonLd;
1909
+ }
1910
+
1911
+ protected set _shouldCacheJsonLd(value: boolean) {
1912
+ this.#shouldCacheJsonLd = value;
1913
+ }
1914
+
1915
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1916
+ if (value == null || typeof value !== "object") return true;
1917
+ if (!("_shouldCacheJsonLd" in value)) return true;
1918
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1919
+ }
1890
1920
  `;
1891
1921
  yield `
1892
1922
  /**
@@ -1980,6 +2010,17 @@ async function* generateClasses(types) {
1980
2010
  isTemporalDuration,
1981
2011
  isTemporalInstant,
1982
2012
  } from "@fedify/vocab-runtime/temporal";\n`;
2013
+ yield `
2014
+ function isValidLanguageTag(language: string): boolean {
2015
+ try {
2016
+ new Intl.Locale(language);
2017
+ return true;
2018
+ } catch (error) {
2019
+ if (error instanceof RangeError) return false;
2020
+ throw error;
2021
+ }
2022
+ }
2023
+ `;
1983
2024
  yield "\n\n";
1984
2025
  const sorted = sortTopologically(types);
1985
2026
  for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
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.2.6";
10
+ var version = "2.2.7";
11
11
  //#endregion
12
12
  //#region src/type.ts
13
13
  const HEURISTICS_CONTEXTS = [
@@ -198,7 +198,8 @@ const scalarTypes = {
198
198
  dataCheck(v) {
199
199
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
200
200
  && typeof ${v}["@language"] === "string"
201
- && typeof ${v}["@value"] === "string"`;
201
+ && typeof ${v}["@value"] === "string"
202
+ && isValidLanguageTag(${v}["@language"])`;
202
203
  },
203
204
  decoder(v) {
204
205
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -1035,6 +1036,9 @@ async function* generateDecoder(typeUri, types) {
1035
1036
  throw new TypeError("Unexpected type: " + instance.constructor.name);
1036
1037
  }
1037
1038
  `;
1039
+ yield `
1040
+ let shouldCacheJsonLd = instance._shouldCacheJsonLd;
1041
+ `;
1038
1042
  for (const property of type.properties) {
1039
1043
  const variable = await getFieldName(property.uri, "");
1040
1044
  yield await generateField(property, types, "const ");
@@ -1068,29 +1072,40 @@ async function* generateDecoder(typeUri, types) {
1068
1072
  continue;
1069
1073
  }
1070
1074
  `;
1071
- if (property.range.length == 1) yield `
1072
- const decoded = ${getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`)};
1073
- if (typeof decoded === "undefined") continue;
1074
- ${variable}.push(decoded);`;
1075
- else {
1076
- yield `
1075
+ yield `
1077
1076
  const decoded =
1078
- `;
1077
+ `;
1078
+ if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1079
+ else {
1079
1080
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1080
1081
  for (const code of decoders) yield code;
1081
- yield `
1082
+ }
1083
+ yield `
1082
1084
  ;
1083
- if (typeof decoded === "undefined") continue;
1085
+ `;
1086
+ yield `
1087
+ if (typeof decoded === "undefined") {
1088
+ shouldCacheJsonLd = false;
1089
+ continue;
1090
+ }
1091
+ `;
1092
+ yield `
1093
+ if (!this._shouldCacheDecodedJsonLd(decoded)) {
1094
+ shouldCacheJsonLd = false;
1095
+ }
1084
1096
  ${variable}.push(decoded);
1085
- `;
1086
- }
1097
+ `;
1087
1098
  yield `
1088
1099
  }
1089
1100
  instance.${await getFieldName(property.uri)} = ${variable};
1090
1101
  `;
1091
1102
  }
1092
1103
  yield `
1093
- if (!("_fromSubclass" in options) || !options._fromSubclass) {
1104
+ instance._shouldCacheJsonLd = shouldCacheJsonLd;
1105
+ if (
1106
+ shouldCacheJsonLd &&
1107
+ (!("_fromSubclass" in options) || !options._fromSubclass)
1108
+ ) {
1094
1109
  try {
1095
1110
  instance._cachedJsonLd = structuredClone(json);
1096
1111
  } catch {
@@ -1857,6 +1872,7 @@ async function* generateClass(typeUri, types) {
1857
1872
  values?: Record<string, unknown>;
1858
1873
  };
1859
1874
  #cachedJsonLd?: unknown;
1875
+ #shouldCacheJsonLd = true;
1860
1876
  readonly id: URL | null;
1861
1877
 
1862
1878
  protected get _documentLoader(): DocumentLoader | undefined {
@@ -1886,6 +1902,20 @@ async function* generateClass(typeUri, types) {
1886
1902
  protected set _cachedJsonLd(value: unknown | undefined) {
1887
1903
  this.#cachedJsonLd = value;
1888
1904
  }
1905
+
1906
+ protected get _shouldCacheJsonLd(): boolean {
1907
+ return this.#shouldCacheJsonLd;
1908
+ }
1909
+
1910
+ protected set _shouldCacheJsonLd(value: boolean) {
1911
+ this.#shouldCacheJsonLd = value;
1912
+ }
1913
+
1914
+ protected static _shouldCacheDecodedJsonLd(value: unknown): boolean {
1915
+ if (value == null || typeof value !== "object") return true;
1916
+ if (!("_shouldCacheJsonLd" in value)) return true;
1917
+ return (value as { _shouldCacheJsonLd: boolean })._shouldCacheJsonLd;
1918
+ }
1889
1919
  `;
1890
1920
  yield `
1891
1921
  /**
@@ -1979,6 +2009,17 @@ async function* generateClasses(types) {
1979
2009
  isTemporalDuration,
1980
2010
  isTemporalInstant,
1981
2011
  } from "@fedify/vocab-runtime/temporal";\n`;
2012
+ yield `
2013
+ function isValidLanguageTag(language: string): boolean {
2014
+ try {
2015
+ new Intl.Locale(language);
2016
+ return true;
2017
+ } catch (error) {
2018
+ if (error instanceof RangeError) return false;
2019
+ throw error;
2020
+ }
2021
+ }
2022
+ `;
1982
2023
  yield "\n\n";
1983
2024
  const sorted = sortTopologically(types);
1984
2025
  for (const typeUri of sorted) for await (const code of generateClass(typeUri, types)) yield code;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "description": "Code generator for Activity Vocabulary APIs",
5
5
  "homepage": "https://fedify.dev/",
6
6
  "repository": {