@fedify/vocab-tools 2.4.0-pr.934.40 → 2.4.0

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/src/type.ts CHANGED
@@ -6,8 +6,10 @@ const HEURISTICS_CONTEXTS: string[] = [
6
6
  "https://www.w3.org/ns/activitystreams",
7
7
  "https://w3id.org/security/v1",
8
8
  "https://w3id.org/security/data-integrity/v1",
9
+ "https://w3id.org/security/data-integrity/v2",
9
10
  "https://www.w3.org/ns/did/v1",
10
11
  "https://w3id.org/security/multikey/v1",
12
+ "https://w3id.org/fep/ef61",
11
13
  ];
12
14
 
13
15
  interface ScalarType {
@@ -17,6 +19,12 @@ interface ScalarType {
17
19
  compactEncoder?: (variable: string) => string;
18
20
  dataCheck(variable: string): string;
19
21
  decoder(variable: string, baseUrlVar: string): string;
22
+ /**
23
+ * Whether a value that fails `dataCheck()` should be skipped instead of
24
+ * being handed to `decoder()`. Set this for types decoded from untrusted
25
+ * remote input, where one malformed value must not fail the whole object.
26
+ */
27
+ skipUnparsable?: boolean;
20
28
  }
21
29
 
22
30
  const scalarTypes: Record<string, ScalarType> = {
@@ -185,7 +193,8 @@ const scalarTypes: Record<string, ScalarType> = {
185
193
  dataCheck(v) {
186
194
  return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
187
195
  && typeof ${v}["@language"] === "string"
188
- && typeof ${v}["@value"] === "string"`;
196
+ && typeof ${v}["@value"] === "string"
197
+ && isValidLanguageTag(${v}["@language"])`;
189
198
  },
190
199
  decoder(v) {
191
200
  return `new LanguageString(${v}["@value"], ${v}["@language"])`;
@@ -316,12 +325,35 @@ const scalarTypes: Record<string, ScalarType> = {
316
325
  return `formatIri(${v})`;
317
326
  },
318
327
  dataCheck(v) {
319
- return `typeof ${v} === "object" && "@value" in ${v}
320
- && typeof ${v}["@value"] === "string"
321
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
328
+ return `typeof ${v} === "object" &&
329
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
330
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
331
+ },
332
+ decoder(v) {
333
+ return `decodeIri(typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"])`;
334
+ },
335
+ skipUnparsable: true,
336
+ },
337
+ "fedify:gatewayUrl": {
338
+ name: "URL",
339
+ typeGuard(v) {
340
+ return `${v} instanceof URL && isGatewayUrl(${v})`;
341
+ },
342
+ encoder(v) {
343
+ return `{ "@id": ${v}.origin }`;
344
+ },
345
+ compactEncoder(v) {
346
+ return `${v}.origin`;
347
+ },
348
+ dataCheck(v) {
349
+ return `${v} != null && typeof ${v} === "object" &&
350
+ (("@id" in ${v} && typeof ${v}["@id"] === "string" &&
351
+ ${v}["@id"] !== "" && ${v}["@id"] !== "/") ||
352
+ ("@value" in ${v} && typeof ${v}["@value"] === "string" &&
353
+ ${v}["@value"] !== "" && ${v}["@value"] !== "/"))`;
322
354
  },
323
355
  decoder(v) {
324
- return `parseIri(${v}["@value"])`;
356
+ return `parseGatewayUrl(typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"])`;
325
357
  },
326
358
  },
327
359
  "fedify:publicKey": {
@@ -633,6 +665,10 @@ export function getDecoder(
633
665
  throw new Error(`Unknown type: ${typeUri}`);
634
666
  }
635
667
 
668
+ export function skipsUnparsable(typeUri: string): boolean {
669
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
670
+ }
671
+
636
672
  export function getDataCheck(
637
673
  typeUri: string,
638
674
  types: Record<string, TypeSchema>,