@fedify/vocab-tools 2.4.0-dev.2115 → 2.4.0-dev.2127

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/class.ts CHANGED
@@ -14,6 +14,7 @@ const XSD_ANY_URI = "http://www.w3.org/2001/XMLSchema#anyURI";
14
14
  const FEDIFY_URL = "fedify:url";
15
15
  const INTERNAL_RUNTIME_IMPORTS = [
16
16
  "compactJsonLdCache",
17
+ "createScopedContextLoader",
17
18
  "getJsonLdContext",
18
19
  "isTrustedIriOrigin",
19
20
  "normalizeJsonLdIris",
@@ -109,6 +110,7 @@ async function* generateClass(
109
110
  readonly #documentLoader?: DocumentLoader;
110
111
  readonly #contextLoader?: DocumentLoader;
111
112
  readonly #tracerProvider?: TracerProvider;
113
+ readonly #verifyPortableObject?: PortableObjectVerifier;
112
114
  readonly #warning?: {
113
115
  category: string[];
114
116
  message: string;
@@ -131,6 +133,10 @@ async function* generateClass(
131
133
  return this.#tracerProvider;
132
134
  }
133
135
 
136
+ protected get _verifyPortableObject(): PortableObjectVerifier | undefined {
137
+ return this.#verifyPortableObject;
138
+ }
139
+
134
140
  protected get _warning(): {
135
141
  category: string[];
136
142
  message: string;
package/src/codec.ts CHANGED
@@ -43,6 +43,7 @@ function* generatePreprocessorBlock(
43
43
  documentLoader: options.documentLoader,
44
44
  contextLoader: options.contextLoader,
45
45
  tracerProvider: options.tracerProvider,
46
+ verifyPortableObject: options.verifyPortableObject,
46
47
  baseUrl: ${baseUrlExpr},
47
48
  });
48
49
  if (_result instanceof Error) throw _result;
@@ -376,6 +377,10 @@ export async function* generateDecoder(
376
377
  * - \`contextLoader\`: The loader for remote JSON-LD contexts.
377
378
  * - \`tracerProvider\`: The OpenTelemetry tracer provider to use.
378
379
  * If omitted, the global tracer provider is used.
380
+ * - \`verifyPortableObject\`: The default FEP-ef61 portable
381
+ * object verifier for the property accessors of the
382
+ * returned object and the objects obtained from it. It
383
+ * does not verify the given \`json\` itself.
379
384
  * @returns The object of this type.
380
385
  * @throws {TypeError} If the given \`json\` is invalid.
381
386
  */
@@ -385,6 +390,7 @@ export async function* generateDecoder(
385
390
  documentLoader?: DocumentLoader,
386
391
  contextLoader?: DocumentLoader,
387
392
  tracerProvider?: TracerProvider,
393
+ verifyPortableObject?: PortableObjectVerifier,
388
394
  baseUrl?: URL,
389
395
  } = {},
390
396
  ): Promise<${type.name}> {
@@ -423,6 +429,7 @@ export async function* generateDecoder(
423
429
  documentLoader?: DocumentLoader,
424
430
  contextLoader?: DocumentLoader,
425
431
  tracerProvider?: TracerProvider,
432
+ verifyPortableObject?: PortableObjectVerifier,
426
433
  baseUrl?: URL,
427
434
  } = {},
428
435
  ): Promise<${type.name}> {
@@ -89,6 +89,7 @@ export async function* generateConstructor(
89
89
  documentLoader?: DocumentLoader,
90
90
  contextLoader?: DocumentLoader,
91
91
  tracerProvider?: TracerProvider,
92
+ verifyPortableObject?: PortableObjectVerifier,
92
93
  } = {},
93
94
  ) {
94
95
  `;
@@ -97,6 +98,7 @@ export async function* generateConstructor(
97
98
  this.#documentLoader = options.documentLoader;
98
99
  this.#contextLoader = options.contextLoader;
99
100
  this.#tracerProvider = options.tracerProvider;
101
+ this.#verifyPortableObject = options.verifyPortableObject;
100
102
  const baseUrl = (options as { baseUrl?: URL }).baseUrl;
101
103
  this.#_baseUrl = baseUrl == null ? undefined : new URL(baseUrl.href);
102
104
  if ("$warning" in options) {
@@ -228,6 +230,7 @@ export async function* generateCloner(
228
230
  options: {
229
231
  documentLoader?: DocumentLoader,
230
232
  contextLoader?: DocumentLoader,
233
+ verifyPortableObject?: PortableObjectVerifier,
231
234
  } = {}
232
235
  ): ${type.name} {
233
236
  if (this._warning != null) {
package/src/property.ts CHANGED
@@ -118,6 +118,9 @@ async function* generateProperty(
118
118
  documentLoader,
119
119
  contextLoader: parseOptions.contextLoader,
120
120
  tracerProvider,
121
+ // Only the parent's own default is inherited, never the
122
+ // verifier given to this call:
123
+ verifyPortableObject: this._verifyPortableObject,
121
124
  baseUrl: parseOptions.baseUrl,
122
125
  }),
123
126
  });
@@ -159,7 +162,9 @@ async function* generateProperty(
159
162
  }
160
163
  let fetchResult: RemoteDocument;
161
164
  try {
162
- fetchResult = await documentLoader(lookupUrl);
165
+ fetchResult = await documentLoader(lookupUrl, {
166
+ suppressError: options.suppressError,
167
+ });
163
168
  } catch (error) {
164
169
  span.setStatus({
165
170
  code: SpanStatusCode.ERROR,
@@ -167,7 +172,7 @@ async function* generateProperty(
167
172
  });
168
173
  span.end();
169
174
  if (options.suppressError) {
170
- getLogger(["fedify", "vocab"]).error(
175
+ getLogger(["fedify", "vocab"]).warn(
171
176
  "Failed to fetch {url}: {error}",
172
177
  { error, url: lookupUrl }
173
178
  );
@@ -179,8 +184,12 @@ async function* generateProperty(
179
184
  // In portable mode, the document may turn out to be a portable object,
180
185
  // which has to be verified with the same context documents:
181
186
  const snapshot = portableMode
182
- ? createSnapshotContextLoader(contextLoader)
187
+ ? createSnapshotContextLoader(contextLoader, options.suppressError)
183
188
  : null;
189
+ const scopedContext = createScopedContextLoader(
190
+ snapshot?.loader ?? contextLoader,
191
+ snapshot == null && options.suppressError,
192
+ );
184
193
  try {
185
194
  let claim: PortableResponseClaim | null | undefined;
186
195
  try {
@@ -189,8 +198,9 @@ async function* generateProperty(
189
198
  document,
190
199
  {
191
200
  documentLoader,
192
- contextLoader: snapshot?.loader ?? contextLoader,
201
+ contextLoader: scopedContext.loader,
193
202
  tracerProvider,
203
+ verifyPortableObject: this._verifyPortableObject,
194
204
  baseUrl,
195
205
  }
196
206
  );
@@ -233,7 +243,7 @@ async function* generateProperty(
233
243
  }
234
244
  } catch (e) {
235
245
  if (options.suppressError) {
236
- getLogger(["fedify", "vocab"]).error(
246
+ getLogger(["fedify", "vocab"]).warn(
237
247
  "Failed to parse {url}: {error}",
238
248
  { error: e, url: lookupUrl }
239
249
  );
@@ -259,6 +269,7 @@ async function* generateProperty(
259
269
  contextLoader: snapshot?.loader,
260
270
  });
261
271
  } finally {
272
+ scopedContext.release();
262
273
  snapshot?.release();
263
274
  }
264
275
  });
@@ -270,6 +281,7 @@ async function* generateProperty(
270
281
  documentLoader?: DocumentLoader,
271
282
  contextLoader?: DocumentLoader,
272
283
  tracerProvider?: TracerProvider,
284
+ verifyPortableObject?: PortableObjectVerifier,
273
285
  baseUrl?: URL
274
286
  }
275
287
  ): Promise<${getTypeNames(property.range, types)}> {
@@ -280,6 +292,7 @@ async function* generateProperty(
280
292
  const tracerProvider = options.tracerProvider ??
281
293
  this._tracerProvider ?? trace.getTracerProvider();
282
294
  const baseUrl = options.baseUrl;
295
+ const verifyPortableObject = options.verifyPortableObject;
283
296
  `;
284
297
  if (
285
298
  property.preprocessors != null &&
@@ -310,6 +323,7 @@ async function* generateProperty(
310
323
  documentLoader,
311
324
  contextLoader,
312
325
  tracerProvider,
326
+ verifyPortableObject,
313
327
  baseUrl,
314
328
  });
315
329
  if (_result instanceof Error) throw _result;
@@ -331,7 +345,13 @@ async function* generateProperty(
331
345
  try {
332
346
  return await ${rangeType.name}.fromJsonLd(
333
347
  jsonLd,
334
- { documentLoader, contextLoader, tracerProvider, baseUrl },
348
+ {
349
+ documentLoader,
350
+ contextLoader,
351
+ tracerProvider,
352
+ verifyPortableObject,
353
+ baseUrl,
354
+ },
335
355
  );
336
356
  } catch (e) {
337
357
  if (!(e instanceof TypeError)) throw e;
@@ -384,6 +404,15 @@ async function* generateProperty(
384
404
  );
385
405
  }
386
406
  if (this.${await getFieldName(property.uri)}.length < 1) return null;
407
+ if (
408
+ options.verifyPortableObject == null &&
409
+ this._verifyPortableObject != null
410
+ ) {
411
+ options = {
412
+ ...options,
413
+ verifyPortableObject: this._verifyPortableObject,
414
+ };
415
+ }
387
416
  let v = this.${await getFieldName(property.uri)}[0];
388
417
  if (!(v instanceof URL) &&
389
418
  !this.${await getFieldName(property.uri, "#_trust")}.has(0)) {
@@ -434,6 +463,7 @@ async function* generateProperty(
434
463
  yield `
435
464
  v = await this.#${property.singularName}_fromJsonLd(doc, {
436
465
  ...options,
466
+ verifyPortableObject: this._verifyPortableObject,
437
467
  baseUrl: ${cachedPropertyBaseUrl},
438
468
  });
439
469
  `;
@@ -522,6 +552,15 @@ async function* generateProperty(
522
552
  this._warning.values
523
553
  );
524
554
  }
555
+ if (
556
+ options.verifyPortableObject == null &&
557
+ this._verifyPortableObject != null
558
+ ) {
559
+ options = {
560
+ ...options,
561
+ verifyPortableObject: this._verifyPortableObject,
562
+ };
563
+ }
525
564
  const vs = this.${await getFieldName(property.uri)};
526
565
  for (let i = 0; i < vs.length; i++) {
527
566
  let v = vs[i];
@@ -578,6 +617,7 @@ async function* generateProperty(
578
617
  yield `
579
618
  v = await this.#${property.singularName}_fromJsonLd(obj, {
580
619
  ...options,
620
+ verifyPortableObject: this._verifyPortableObject,
581
621
  baseUrl: ${cachedPropertyBaseUrl},
582
622
  });
583
623
  `;